Bug fix to add anyxml node.
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / yangserializers / pnserializer / MdsalPropertiesNodeSerializer.java
index 759fe80..8a6e756 100644 (file)
@@ -29,6 +29,8 @@ import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.DOT_REGEX;
 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.SLASH;
@@ -42,6 +44,7 @@ import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPrope
 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getRevision;
 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getValueNamespace;
 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.resolveName;
+import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.ANY_XML_NODE;
 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_NODE;
 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_NODE;
 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_LEAF_NODE;
@@ -52,6 +55,8 @@ import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.S
  */
 public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer<SchemaNode, SchemaContext> {
 
+    private static final Logger log = LoggerFactory.getLogger(
+            MdsalPropertiesNodeSerializer.class);
     private SchemaNode curSchema;
     private PropertiesNode node;
 
@@ -77,6 +82,8 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer<Sche
 
         paramMap = convertToValidParam(paramMap);
 
+        updateModNameReq(paramMap, rootUri);
+
         for (Map.Entry<String, String> entry : paramMap.entrySet()) {
             String[] names = entry.getKey().split("\\.");
             for (int i = 0; i < names.length; i++) {
@@ -93,6 +100,15 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer<Sche
         return node;
     }
 
+    private void updateModNameReq(Map<String, String> paramMap,
+                                  String rootUri) {
+        String isReqStr = rootUri + "." + "isNonAppend";
+        String val = paramMap.get(isReqStr);
+        if (val != null && val.equals("true")) {
+            node.nonAppend(true);
+        }
+    }
+
     /**
      * Converts all the params in the svc logic context into a valid param by
      * replacing the underscore in module name to colon at necessary places.
@@ -111,6 +127,8 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer<Sche
                 fixedParams.put(fixedUri, entry.getValue());
             } catch (IllegalArgumentException | RestconfDocumentedException
                     | NullPointerException e) {
+                log.info("Exception while processing properties by replacing " +
+                    "underscore with colon. Process the properties as it is." + e);
                 fixedParams.put(entry.getKey(), entry.getValue());
             }
         }
@@ -144,7 +162,7 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer<Sche
             return;
         }
 
-        switch (getNodeType(index, length, name)) {
+        switch (getNodeType(index, length, name, schema)) {
             case SINGLE_INSTANCE_NODE:
                 node = node.addChild(localName, ns,
                                      SINGLE_INSTANCE_NODE, schema);
@@ -158,20 +176,18 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer<Sche
                 break;
 
             case SINGLE_INSTANCE_LEAF_NODE:
-                Namespace valNs = getValueNamespace(value, schemaCtx());
-                value = getParsedValue(valNs, value);
-                node = node.addChild(localName, ns, SINGLE_INSTANCE_LEAF_NODE,
-                                     value, valNs, schema);
-                node = node.endNode();
-                curSchema = ((SchemaNode) node.appInfo());
+                addLeafNode(value, SINGLE_INSTANCE_LEAF_NODE, localName,
+                               ns, schema, name);
                 break;
 
             case MULTI_INSTANCE_LEAF_NODE:
-                valNs = getValueNamespace(value, schemaCtx());
-                value = getParsedValue(valNs, value);
-                node = node.addChild(getIndex(name), localName, ns,
-                                     MULTI_INSTANCE_LEAF_NODE, value,
-                                     valNs, schema);
+                addLeafNode(value, MULTI_INSTANCE_LEAF_NODE, localName,
+                               ns, schema, name);
+                break;
+
+            case ANY_XML_NODE:
+                node = node.addChild(localName, ns, ANY_XML_NODE,
+                                     value, null, schema);
                 node = node.endNode();
                 curSchema = ((SchemaNode) node.appInfo());
                 break;
@@ -180,4 +196,32 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer<Sche
                 throw new SvcLogicException("Invalid node type");
         }
     }
+
+    /**
+     * Adds leaf property node to the current node.
+     *
+     * @param value value of the leaf node
+     * @param type single instance or multi instance leaf node
+     * @param localName name of the leaf node
+     * @param ns namespace of the leaf node
+     * @param schema schema of the leaf node
+     * @param name name of the leaf in properties
+     * @throws SvcLogicException exception while adding leaf node
+     */
+    private void addLeafNode(String value, NodeType type,
+                                String localName, Namespace ns,
+                                SchemaNode schema, String name) throws SvcLogicException {
+        Namespace valNs = getValueNamespace(value, schemaCtx());
+        value = getParsedValue(valNs, value);
+        if (SINGLE_INSTANCE_LEAF_NODE == type) {
+            node = node.addChild(localName, ns, SINGLE_INSTANCE_LEAF_NODE,
+                                 value, valNs, schema);
+        } else {
+            node = node.addChild(getIndex(name), localName, ns,
+                                 MULTI_INSTANCE_LEAF_NODE, value,
+                                 valNs, schema);
+        }
+        node = node.endNode();
+        curSchema = ((SchemaNode) node.appInfo());
+    }
 }