Fix DataNodeBuilder for ChoiceNodes 05/132505/8
authorLena Peuker <PeukerL@telekom.de>
Tue, 29 Nov 2022 14:38:00 +0000 (15:38 +0100)
committerLena Peuker <PeukerL@telekom.de>
Mon, 12 Dec 2022 13:19:08 +0000 (14:19 +0100)
Fix implemented to handle YangChoiceNode in right format

Issue-ID: CPS-1352
Signed-off-by: Lena Peuker <PeukerL@telekom.de>
Change-Id: I48d433bac96cfc647bc31c82817870995bace860

cps-service/src/main/java/org/onap/cps/spi/model/DataNodeBuilder.java
cps-service/src/test/groovy/org/onap/cps/spi/model/DataNodeBuilderSpec.groovy
cps-service/src/test/resources/data-with-choice-node.json [new file with mode: 0644]
cps-service/src/test/resources/yang-with-choice-node.yang [new file with mode: 0644]

index eaa2d77..af820de 100644 (file)
@@ -33,6 +33,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.onap.cps.spi.exceptions.DataValidationException;
 import org.onap.cps.utils.YangUtils;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
@@ -173,7 +174,9 @@ public class DataNodeBuilder {
     private static void addDataNodeFromNormalizedNode(final DataNode currentDataNode,
         final NormalizedNode normalizedNode) {
 
-        if (normalizedNode instanceof DataContainerNode) {
+        if (normalizedNode instanceof ChoiceNode) {
+            addChoiceNode(currentDataNode, (ChoiceNode) normalizedNode);
+        } else if (normalizedNode instanceof DataContainerNode) {
             addYangContainer(currentDataNode, (DataContainerNode) normalizedNode);
         } else if (normalizedNode instanceof MapNode) {
             addDataNodeForEachListElement(currentDataNode, (MapNode) normalizedNode);
@@ -236,4 +239,13 @@ public class DataNodeBuilder {
         return newChildDataNode;
     }
 
+    private static void addChoiceNode(final DataNode currentDataNode, final ChoiceNode choiceNode) {
+
+        final Collection<DataContainerChild> normalizedChildNodes = choiceNode.body();
+        for (final NormalizedNode normalizedNode : normalizedChildNodes) {
+            addDataNodeFromNormalizedNode(currentDataNode, normalizedNode);
+        }
+    }
+
+
 }
index fcfb482..a58504f 100644 (file)
@@ -140,6 +140,25 @@ class DataNodeBuilderSpec extends Specification {
             assert result.leaves['source-tp'] == '1-2-1'
     }
 
+    def 'Converting NormalizedNode (tree) to a DataNode (tree) -- with ChoiceNode.'() {
+        given: 'a schema context for expected model'
+            def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('yang-with-choice-node.yang')
+            def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent) getSchemaContext()
+        and: 'the json data fragment parsed into normalized node object'
+            def jsonData = TestUtils.getResourceFileContent('data-with-choice-node.json')
+            def normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext)
+        when: 'the normalized node is converted to a data node'
+            def result = new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build()
+            def mappedResult = TestUtils.getFlattenMapByXpath(result)
+        then: 'the resulting data node contains only one xpath with 3 leaves'
+            mappedResult.keySet().containsAll([
+                "/container-with-choice-leaves"
+            ])
+            assert result.leaves['leaf-1'] == "test"
+            assert result.leaves['choice-case1-leaf-a'] == "test"
+            assert result.leaves['choice-case1-leaf-b'] == "test"
+    }
+
     def 'Converting NormalizedNode into DataNode collection: #scenario.'() {
         given: 'a schema context for expected model'
             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
diff --git a/cps-service/src/test/resources/data-with-choice-node.json b/cps-service/src/test/resources/data-with-choice-node.json
new file mode 100644 (file)
index 0000000..5f81ed8
--- /dev/null
@@ -0,0 +1,8 @@
+{
+  "container-with-choice-leaves": {
+    "leaf-1": "test",
+    "choice-case1-leaf-a": "test",
+    "choice-case1-leaf-b": "test"
+  }
+}
+
diff --git a/cps-service/src/test/resources/yang-with-choice-node.yang b/cps-service/src/test/resources/yang-with-choice-node.yang
new file mode 100644 (file)
index 0000000..55c0bfb
--- /dev/null
@@ -0,0 +1,27 @@
+module yang-with-choice-node {
+  yang-version 1.1;
+  namespace "org:onap:cps:test:yang-with-choice-node";
+  prefix "yang-with-choice-node";
+
+  container container-with-choice-leaves {
+    leaf leaf-1 {
+      type string;
+    }
+
+    choice choicenode {
+      case case-1 {
+        leaf choice-case1-leaf-a {
+          type string;
+        }
+        leaf choice-case1-leaf-b {
+          type string;
+        }
+      }
+      case case-2 {
+        leaf choice-case2-leaf-a {
+          type string;
+        }
+      }
+    }
+  }
+}