+ * Updates request message for JSON and XML data format, when the HTTP
+ * method points it as PUT or PATCH.
+ *
+ * @param req current request message
+ * @param p YANG parameters
+ * @param insIdCtx instance identifier context
+ * @return update request message
+ * @throws SvcLogicException when the data format type is wrong
+ */
+ public String updateReq(String req, YangParameters p,
+ InstanceIdentifierContext<?> insIdCtx)
+ throws SvcLogicException {
+
+ SchemaNode schemaNode = insIdCtx.getSchemaNode();
+ Namespace modNs = getModuleNamespace(schemaNode.getQName(),
+ insIdCtx.getSchemaContext());
+ String nodeName = schemaNode.getQName().getLocalName();
+
+ switch (p.format) {
+ case JSON:
+ return getUpdatedJsonReq(req, nodeName, modNs.moduleName());
+
+ case XML:
+ return getXmlReqForPutOp(req, nodeName, modNs.moduleNs());
+
+ default:
+ throw new SvcLogicException(format(FORMAT_ERR, p.format));
+ }
+ }
+
+ /**
+ * Returns the updated JSON request message, when the HTTP method
+ * points to PUT or PATCH.
+ *
+ * @param req current JSON request message
+ * @param nodeName root node name
+ * @param modName module name of the root node
+ * @return update JSON request message
+ */
+ private String getUpdatedJsonReq(String req, String nodeName,
+ String modName) {
+ Writer writer = new StringWriter();
+ JsonWriter jsonWriter = new JsonWriter(writer);
+ jsonWriter.setIndent(repeat(" ", 4));
+
+ JsonParser jsonParser = new JsonParser();
+ JsonObject oldJson = (JsonObject)jsonParser.parse(req);
+ oldJson = remChildModName(oldJson, modName);
+ JsonObject newJson = new JsonObject();
+ newJson.add(modName + COLON + nodeName, oldJson.deepCopy());
+
+ Gson gson= new Gson();
+ gson.toJson(newJson, jsonWriter);
+ return writer.toString();
+ }
+
+ /**
+ * Removes module name from all the updated first level child node, if it
+ * is same as the root node added.
+ *
+ * @param oldJson JSON object for old request
+ * @param modName module name of root node
+ * @return JSON object for old request with updated child module name
+ */
+ private JsonObject remChildModName(JsonObject oldJson, String modName) {
+ Iterator<Map.Entry<String, JsonElement>> it = oldJson.entrySet().iterator();
+ Map<String, JsonElement> m = new HashMap<>();
+ while (it.hasNext()) {
+ Map.Entry<String, JsonElement> jNode = it.next();
+ if (jNode.getKey().contains(COLON)) {
+ String[] modArr = jNode.getKey().split(COLON);
+ if (modArr[0].equals(modName)) {
+ it.remove();
+ m.put(modArr[1], jNode.getValue());
+ }
+ }
+ }
+ if (!m.isEmpty()) {
+ for (Map.Entry<String, JsonElement> element : m.entrySet()) {
+ oldJson.add(element.getKey(), element.getValue());
+ }
+ }
+ return oldJson;
+ }
+
+ /**
+ * Returns the updated XML request message, when the HTTP method points
+ * to PUT or PATCH.