/**
   * Internally create a cse base based on the restconf call which is designed to be called from a
   * provisioning server or management app.
   *
   * @param onem2mResponse response
   */
  public void provisionCse(ResponsePrimitive onem2mResponse) {

    this.crudMonitor.enter();
    try {
      String cseId = this.getPrimitive("CSE_ID");
      if (cseId == null) {
        onem2mResponse.setRSC(Onem2m.ResponseStatusCode.BAD_REQUEST, "CSE_ID not specified!");
        return;
      }
      String cseType = this.getPrimitive("CSE_TYPE");
      if (cseType == null) {
        cseType = "IN-CSE";
      } else if (!cseType.contentEquals(
          "IN-CSE")) { // TODO: what is the difference between CSE types
        onem2mResponse.setRSC(
            Onem2m.ResponseStatusCode.BAD_REQUEST, "IN-CSE is the only one supported :-(");
        return;
      }

      this.setPrimitive(RequestPrimitive.RESOURCE_TYPE, Onem2m.ResourceType.CSE_BASE);
      this.setPrimitive(RequestPrimitive.NAME, cseId);
      this.setResourceName(cseId);

      if (Onem2mDb.getInstance().findCseByName(cseId)) {
        onem2mResponse.setRSC(
            Onem2m.ResponseStatusCode.ALREADY_EXISTS, "CSE name already exists: " + cseId);
        return;
      }

      this.setPrimitive(RequestPrimitive.CONTENT_FORMAT, Onem2m.ContentFormat.JSON);
      JSONObject jCse = new JSONObject();
      jCse.put(ResourceCse.CSE_ID, cseId);
      jCse.put(ResourceCse.CSE_TYPE, cseType);
      JSONObject j = new JSONObject();
      j.put(Onem2m.ResourceTypeString.CSE_BASE, jCse);
      this.setPrimitive(RequestPrimitive.CONTENT, j.toString());

      // process the resource specific attributes
      ResourceContentProcessor.handleCreate(this, onem2mResponse);
      if (onem2mResponse.getPrimitive(ResponsePrimitive.RESPONSE_STATUS_CODE) != null) {
        return;
      }

      // TODO: see TS0004 6.8
      // if the create was successful, ie no error has already happened, set CREATED for status code
      // here
      if (onem2mResponse.getPrimitive(ResponsePrimitive.RESPONSE_STATUS_CODE) == null) {
        onem2mResponse.setPrimitive(
            ResponsePrimitive.RESPONSE_STATUS_CODE,
            "Provisioned cseBase: " + cseId + " type: " + cseType);
      }

      // TODO: add default ACP here?
      //            RequestPrimitive defaultACPrequest = new RequestPrimitive();
      //            defaultACPrequest.setPrimitive("protocol","Http");
      //            defaultACPrequest.setPrimitive("contentFormat","json");
      //            defaultACPrequest.setPrimitive("to","/"+ cseId);
      //            defaultACPrequest.setPrimitive("fr","//localhost:10000");
      //            defaultACPrequest.setPrimitive("rqi","12345");
      //            defaultACPrequest.setPrimitive("ot","NOW");
      //            defaultACPrequest.setPrimitive("ty","1");
      //            defaultACPrequest.setPrimitive("rcn","1");
      //            defaultACPrequest.setPrimitive("op","1");
      //            defaultACPrequest.setPrimitive("pc","{\n" +
      //                    "\n" +
      //                    "    \"m2m:acp\":{\n" +
      //                    "      \"pv\":\n" +
      //                    "        {\"acr\":[{\n" +
      //                    "              \n" +
      //                    "          \"acor\" : [\"111\",\"222\"],\n" +
      //                    "          \"acop\":35\n" +
      //                    "              \n" +
      //                    "        },\n" +
      //                    "         {\n" +
      //                    "          \"acor\" : [\"111\",\"222\"],\n" +
      //                    "          \n" +
      //                    "          \"acop\":35\n" +
      //                    "         }\n" +
      //                    "        \n" +
      //                    "        ]},\n" +
      //                    "        \n" +
      //                    "      \"pvs\":\n" +
      //                    "        {\"acr\":[{\n" +
      //                    "              \n" +
      //                    "          \"acor\" : [\"111\",\"222\"],\n" +
      //                    "          \"acop\":7\n" +
      //                    "              \n" +
      //                    "        },\n" +
      //                    "         {\n" +
      //                    "          \"acor\" : [\"111\",\"222\"],\n" +
      //                    "          \"acop\":9\n" +
      //                    "         }\n" +
      //                    "        \n" +
      //                    "        ]}\n" +
      //                    "       \n" +
      //                    "    }\n" +
      //                    "  \n" +
      //                    "}");
      //            ResourceContentProcessor.handleCreate(defaultACPrequest, onem2mResponse);

      //            this.setPrimitive("to","/"+ cseId);
      //            this.setPrimitive("ty","1");
      //            this.setPrimitive("pc","{\n" +
      //                    "\n" +
      //                    "    \"m2m:acp\":{\n" +
      //                    "      \"pv\":\n" +
      //                    "        {\"acr\":[{\n" +
      //                    "              \n" +
      //                    "          \"acor\" : [\"111\",\"222\"],\n" +
      //                    "          \"acop\":35\n" +
      //                    "              \n" +
      //                    "        },\n" +
      //                    "         {\n" +
      //                    "          \"acor\" : [\"111\",\"222\"],\n" +
      //                    "          \n" +
      //                    "          \"acop\":35\n" +
      //                    "         }\n" +
      //                    "        \n" +
      //                    "        ]},\n" +
      //                    "        \n" +
      //                    "      \"pvs\":\n" +
      //                    "        {\"acr\":[{\n" +
      //                    "              \n" +
      //                    "          \"acor\" : [\"111\",\"222\"],\n" +
      //                    "          \"acop\":7\n" +
      //                    "              \n" +
      //                    "        },\n" +
      //                    "         {\n" +
      //                    "          \"acor\" : [\"111\",\"222\"],\n" +
      //                    "          \"acop\":9\n" +
      //                    "         }\n" +
      //                    "        \n" +
      //                    "        ]}\n" +
      //                    "       \n" +
      //                    "    }\n" +
      //                    "  \n" +
      //                    "}");
      //            ResourceContentProcessor.handleCreate(this, onem2mResponse);
    } finally {
      this.crudMonitor.leave();
    }
  }
  /**
   * Handle the request primitive create ... TODO: Strategy for error handling ... TS0004 7.1.1.2
   *
   * @param onem2mResponse response
   */
  public void handleOperationCreate(ResponsePrimitive onem2mResponse) {

    // Use table TS0004: 7.1.1.1-1 to validate CREATE specific parameters that were not handled in
    // the
    // handleOperation

    // ensure the create has content ...
    String cf = getPrimitive(RequestPrimitive.CONTENT_FORMAT);
    if (cf == null) {
      onem2mResponse.setRSC(
          Onem2m.ResponseStatusCode.BAD_REQUEST,
          "CONTENT_FORMAT(" + RequestPrimitive.CONTENT_FORMAT + ") not specified");
      return;
    } else if (!cf.contentEquals(Onem2m.ContentFormat.JSON)
        && !cf.contentEquals(Onem2m.ContentFormat.XML)) {
      onem2mResponse.setRSC(
          Onem2m.ResponseStatusCode.BAD_REQUEST,
          "CONTENT_FORMAT(" + RequestPrimitive.CONTENT_FORMAT + ") not accepted (" + cf + ")");
      return;
    }

    String cn = getPrimitive(RequestPrimitive.CONTENT);
    if (cn == null) {
      if (cf.contentEquals(Onem2m.ContentFormat.JSON)) {
        this.setPrimitive(RequestPrimitive.CONTENT, "{}");
      } else {
        onem2mResponse.setRSC(
            Onem2m.ResponseStatusCode.BAD_REQUEST,
            "CONTENT(" + RequestPrimitive.CONTENT_FORMAT + ") not specified");
        return;
      }
    }

    // validate result content options for create
    String rc = getPrimitive(RequestPrimitive.RESULT_CONTENT);
    if (rc != null) {
      if (!(rc.contentEquals(Onem2m.ResultContent.ATTRIBUTES)
          || rc.contentEquals(Onem2m.ResultContent.NOTHING)
          || rc.contentEquals(Onem2m.ResultContent.HIERARCHICAL_ADDRESS)
          || rc.contentEquals(Onem2m.ResultContent.HIERARCHICAL_ADDRESS_ATTRIBUTES))) {
        onem2mResponse.setRSC(
            Onem2m.ResponseStatusCode.CONTENTS_UNACCEPTABLE,
            "RESULT_CONTENT(" + RequestPrimitive.RESULT_CONTENT + ") not accepted (" + rc + ")");
        return;
      }
    }

    // lookup the resource ... this will be the parent where the new resource will be created
    String resourceType = getPrimitive(RequestPrimitive.RESOURCE_TYPE);
    if (!resourceType.contentEquals(Onem2m.ResourceType.CSE_BASE)) {
      String to = getPrimitive(RequestPrimitive.TO);
      if (!Onem2mDb.getInstance().findResourceUsingURI(to, this, onem2mResponse)) {
        onem2mResponse.setRSC(
            Onem2m.ResponseStatusCode.NOT_FOUND, "Resource target URI not found: " + to);
        return;
      }

      // the Onem2mResource is now stored in the onem2mRequest ... as it has been read in from the
      // data store

      // special case for AE resources ... where resource name is derived from FROM parameter
      String resourceName = this.getPrimitive((RequestPrimitive.NAME));
      //            if (resourceName == null && resourceType.contentEquals(Onem2m.ResourceType.AE))
      // {
      //                String from = this.getPrimitive(RequestPrimitive.FROM);
      //                if (from != null) {
      //                    resourceName = from;
      //                }
      //            }

      // if the a name is provided, ensure it is valid and unique at this hierarchical level
      if (resourceName != null) {
        // using the parent, see if this new resource name already exists under this parent resource
        if (Onem2mDb.getInstance()
            .findResourceUsingIdAndName(this.getOnem2mResource().getResourceId(), resourceName)) {
          // TS0004: 7.2.3.2
          onem2mResponse.setRSC(
              Onem2m.ResponseStatusCode.CONFLICT,
              "Resource already exists: "
                  + this.getPrimitive(RequestPrimitive.TO)
                  + "/"
                  + resourceName);
          return;
        }
        this.setResourceName(resourceName);
      }
    } else {
      String resourceName = this.getPrimitive((RequestPrimitive.NAME));
      if (resourceName == null) {
        onem2mResponse.setRSC(Onem2m.ResponseStatusCode.BAD_REQUEST, "CSE name not specified");
        return;
      }
      if (Onem2mDb.getInstance().findCseByName(resourceName)) {
        onem2mResponse.setRSC(
            Onem2m.ResponseStatusCode.CONFLICT, "CSE name already exists: " + resourceName);
        return;
      }
      this.setResourceName(resourceName);
    }

    // process the resource specific attributes
    ResourceContentProcessor.handleCreate(this, onem2mResponse);
    if (onem2mResponse.getPrimitive(ResponsePrimitive.RESPONSE_STATUS_CODE) != null) {
      return;
    }
    //        CheckAccessControlProcessor.handleCreate(this, onem2mResponse);
    //        if (onem2mResponse.getPrimitive(ResponsePrimitive.RESPONSE_STATUS_CODE) != null) {
    //            return;
    //        }

    // now format a response based on result content desired
    ResultContentProcessor.handleCreate(this, onem2mResponse);

    // not process notifications
    NotificationProcessor.handleCreate(this);

    // TODO: see TS0004 6.8
    // if the create was successful, ie no error has already happened, set CREATED for status code
    // here
    if (onem2mResponse.getPrimitive(ResponsePrimitive.RESPONSE_STATUS_CODE) == null) {
      onem2mResponse.setPrimitive(
          ResponsePrimitive.RESPONSE_STATUS_CODE, Onem2m.ResponseStatusCode.CREATED);
    }
  }