Code refactoring in restconf client yang serialisers. 26/85526/4
authorVidyashree Rama <vidyashree.rama@huawei.com>
Wed, 17 Apr 2019 03:40:56 +0000 (09:10 +0530)
committerVidyashree Rama <vidyashree.rama@huawei.com>
Wed, 17 Apr 2019 09:55:39 +0000 (09:55 +0000)
1. logging the exception when handling underscore in yang serialisers
2. Refactor code to not nest more than 3 if/for statements
3. Throw SvcLogicException instead of generic
4. Reduce switch case number of lines from 8 to at most 5

Issue-ID: CCSDK-1236
Change-Id: Ic02ba424c2d1748d071e38d25ae159f5d8bd6451
Signed-off-by: Vidyashree Rama <vidyashree.rama@huawei.com>
restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/MdsalPropertiesNodeSerializer.java
restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/MdsalPropertiesNodeUtils.java

index 759fe80..0eca40d 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;
@@ -52,6 +54,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;
 
@@ -111,6 +115,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());
             }
         }
@@ -158,26 +164,45 @@ 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);
-                node = node.endNode();
-                curSchema = ((SchemaNode) node.appInfo());
+                addLeafNode(value, MULTI_INSTANCE_LEAF_NODE, localName,
+                               ns, schema, name);
                 break;
 
             default:
                 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());
+    }
 }
index d0b34f9..2a3b176 100644 (file)
@@ -177,17 +177,20 @@ public final class MdsalPropertiesNodeUtils {
     public static PropertiesNode getAugmentationNode(
             AugmentationSchemaNode augSchema,
             PropertiesNode parent, String name) {
-        if (augSchema != null) {
-            Collection<PropertiesNode> childsFromAugmentation = parent
-                    .augmentations().get(augSchema);
-            if (!childsFromAugmentation.isEmpty()) {
-                for (PropertiesNode pNode : childsFromAugmentation) {
-                    if (pNode.name().equals(name)) {
-                        return pNode;
-                    }
+        if (augSchema == null) {
+            return null;
+        }
+
+        Collection<PropertiesNode> childsFromAugmentation = parent
+            .augmentations().get(augSchema);
+        if (!childsFromAugmentation.isEmpty()) {
+            for (PropertiesNode pNode : childsFromAugmentation) {
+                if (pNode.name().equals(name)) {
+                    return pNode;
                 }
             }
         }
+
         return null;
     }
 
@@ -218,10 +221,12 @@ public final class MdsalPropertiesNodeUtils {
      * @param appInfo application info
      * @param type node type
      * @return new properties node
+     * @throws SvcLogicException exception while creating properties node
      */
     public static PropertiesNode createNode(String name, Namespace namespace,
                                             String uri, PropertiesNode parent,
-                                            Object appInfo, NodeType type) {
+                                            Object appInfo, NodeType type)
+            throws SvcLogicException {
         switch (type) {
             case SINGLE_INSTANCE_NODE:
                 return new SingleInstanceNode(name, namespace, uri, parent, appInfo, type);
@@ -230,7 +235,7 @@ public final class MdsalPropertiesNodeUtils {
             case MULTI_INSTANCE_LEAF_HOLDER_NODE:
                 return new LeafListHolderNode(name, namespace, uri, parent, appInfo, type);
             default:
-                throw new RuntimeException("Invalid node type");
+                throw new SvcLogicException("Invalid node type " + type);
         }
     }
 
@@ -265,6 +270,9 @@ public final class MdsalPropertiesNodeUtils {
             return new SchemaPathHolder(id, uri1);
         } catch (IllegalArgumentException | RestconfDocumentedException
                 | NullPointerException e) {
+            log.info("Exception while converting uri to instance identifier" +
+                " context. Process each node in uri to get instance identifier" +
+                " context " + e);
             return processNodesAndAppendPath(uri, context);
         }
     }
@@ -294,6 +302,7 @@ public final class MdsalPropertiesNodeUtils {
             try {
                 id = processIdentifier(uriParts[i], context, actPath);
             } catch (IllegalArgumentException e) {
+                log.info(format(EXC_MSG, e));
                 id.setUri(actPath+ uriParts[i] + sec);
                 return id;
             }
@@ -340,7 +349,7 @@ public final class MdsalPropertiesNodeUtils {
                 return new SchemaPathHolder(id, val);
             } catch (IllegalArgumentException | RestconfDocumentedException |
                     NullPointerException e) {
-                log.info(format(INFO_MSG, val));
+                log.info(format(INFO_MSG, val, e));
             }
             firstHalf.append(values[i]).append(UNDERSCORE);
             secondHalf = secondHalf.replaceFirst(