NETCONF-512: Add cases for missing NormalizedNode types 51/68651/2
authorTom Pantelis <tompantelis@gmail.com>
Sat, 24 Feb 2018 01:17:02 +0000 (20:17 -0500)
committerTom Pantelis <tompantelis@gmail.com>
Mon, 26 Feb 2018 19:16:40 +0000 (19:16 +0000)
ReadDataTransactionUtil.prepareData was missing cases for
LeafSetNode, OrderedLeafSetNode, OrderedMapNode and UnkeyedListNode.

Change-Id: Iaf44ce9b24044584e37d524835ec4a51843acd32
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtil.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtilTest.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/TestData.java

index fc55fa764909993f87ad47d49254d721f9e56df7..aca94271bdf65807d8fad4654ed045cdcb1dbda2 100644 (file)
@@ -51,14 +51,21 @@ import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
+import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
+import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
@@ -575,10 +582,19 @@ public final class ReadDataTransactionUtil {
      *             data node of state data
      * @return {@link NormalizedNode}
      */
+    @SuppressWarnings("unchecked")
     @Nonnull
     private static NormalizedNode<?, ?> prepareData(@Nonnull final NormalizedNode<?, ?> configDataNode,
                                                              @Nonnull final NormalizedNode<?, ?> stateDataNode) {
-        if (configDataNode instanceof MapNode) {
+        if (configDataNode instanceof OrderedMapNode) {
+            final CollectionNodeBuilder<MapEntryNode, OrderedMapNode> builder = Builders
+                    .orderedMapBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
+
+            mapValueToBuilder(
+                    ((OrderedMapNode) configDataNode).getValue(), ((OrderedMapNode) stateDataNode).getValue(), builder);
+
+            return builder.build();
+        } else if (configDataNode instanceof MapNode) {
             final CollectionNodeBuilder<MapEntryNode, MapNode> builder = ImmutableNodes
                     .mapNodeBuilder().withNodeIdentifier(((MapNode) configDataNode).getIdentifier());
 
@@ -620,8 +636,36 @@ public final class ReadDataTransactionUtil {
             return builder.build();
         } else if (configDataNode instanceof LeafNode) {
             return ImmutableNodes.leafNode(configDataNode.getNodeType(), configDataNode.getValue());
+        } else if (configDataNode instanceof OrderedLeafSetNode) {
+            final ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder = Builders
+                .orderedLeafSetBuilder().withNodeIdentifier(((OrderedLeafSetNode<?>) configDataNode).getIdentifier());
+
+            mapValueToBuilder(((OrderedLeafSetNode<Object>) configDataNode).getValue(),
+                    ((OrderedLeafSetNode<Object>) stateDataNode).getValue(), builder);
+            return builder.build();
+        } else if (configDataNode instanceof LeafSetNode) {
+            final ListNodeBuilder<Object, LeafSetEntryNode<Object>> builder = Builders
+                    .leafSetBuilder().withNodeIdentifier(((LeafSetNode<?>) configDataNode).getIdentifier());
+
+            mapValueToBuilder(((LeafSetNode<Object>) configDataNode).getValue(),
+                    ((LeafSetNode<Object>) stateDataNode).getValue(), builder);
+            return builder.build();
+        } else if (configDataNode instanceof UnkeyedListNode) {
+            final CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> builder = Builders
+                    .unkeyedListBuilder().withNodeIdentifier(((UnkeyedListNode) configDataNode).getIdentifier());
+
+            mapValueToBuilder(((UnkeyedListNode) configDataNode).getValue(),
+                    ((UnkeyedListNode) stateDataNode).getValue(), builder);
+            return builder.build();
+        } else if (configDataNode instanceof UnkeyedListEntryNode) {
+            final DataContainerNodeAttrBuilder<NodeIdentifier, UnkeyedListEntryNode> builder = Builders
+                .unkeyedListEntryBuilder().withNodeIdentifier(((UnkeyedListEntryNode) configDataNode).getIdentifier());
+
+            mapValueToBuilder(((UnkeyedListEntryNode) configDataNode).getValue(),
+                    ((UnkeyedListEntryNode) stateDataNode).getValue(), builder);
+            return builder.build();
         } else {
-            throw new RestconfDocumentedException("Bad type of node.");
+            throw new RestconfDocumentedException("Unexpected node type: " + configDataNode.getClass().getName());
         }
     }
 
index 546f0909439f995bc472846b951a626c49b0d09a..5199c6e527b6464b70cb77271738a0fb68420cbe 100644 (file)
@@ -16,6 +16,7 @@ import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.when;
 
 import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableList;
 import com.google.common.util.concurrent.Futures;
 import java.util.Collections;
 import javax.ws.rs.core.MultivaluedHashMap;
@@ -37,8 +38,11 @@ import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWr
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
@@ -184,6 +188,79 @@ public class ReadDataTransactionUtilTest {
         assertEquals(checkingData, normalizedNode);
     }
 
+    @Test
+    public void readOrderedListDataAllTest() {
+        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.orderedMapNode1))).when(read)
+                .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
+        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.orderedMapNode2))).when(read)
+                .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
+        doReturn(DATA.path3).when(context).getInstanceIdentifier();
+
+        final NormalizedNode<?, ?> normalizedNode =
+                ReadDataTransactionUtil.readData(RestconfDataServiceConstant.ReadData.ALL, wrapper, schemaContext);
+
+        final MapNode expectedData = Builders.orderedMapBuilder()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(DATA.listQname)).withChild(DATA.checkData)
+                .build();
+        assertEquals(expectedData, normalizedNode);
+    }
+
+    @Test
+    public void readUnkeyedListDataAllTest() {
+        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.unkeyedListNode1))).when(read)
+                .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
+        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.unkeyedListNode2))).when(read)
+                .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
+        doReturn(DATA.path3).when(context).getInstanceIdentifier();
+
+        final NormalizedNode<?, ?> normalizedNode =
+                ReadDataTransactionUtil.readData(RestconfDataServiceConstant.ReadData.ALL, wrapper, schemaContext);
+
+        final UnkeyedListNode expectedData = Builders.unkeyedListBuilder()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(DATA.listQname))
+                .withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(
+                        new YangInstanceIdentifier.NodeIdentifier(DATA.listQname))
+                        .withChild(DATA.unkeyedListEntryNode1.getValue().iterator().next())
+                        .withChild(DATA.unkeyedListEntryNode2.getValue().iterator().next()).build()).build();
+        assertEquals(expectedData, normalizedNode);
+    }
+
+    @Test
+    public void readLeafListDataAllTest() {
+        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.leafSetNode1))).when(read)
+                .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath);
+        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.leafSetNode2))).when(read)
+                .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath);
+        doReturn(DATA.leafSetNodePath).when(context).getInstanceIdentifier();
+
+        final NormalizedNode<?, ?> normalizedNode =
+                ReadDataTransactionUtil.readData(RestconfDataServiceConstant.ReadData.ALL, wrapper, schemaContext);
+
+        final LeafSetNode<String> expectedData = Builders.<String>leafSetBuilder().withNodeIdentifier(
+                new YangInstanceIdentifier.NodeIdentifier(DATA.leafListQname)).withValue(
+                        ImmutableList.<LeafSetEntryNode<String>>builder().addAll(DATA.leafSetNode1.getValue())
+                        .addAll(DATA.leafSetNode2.getValue()).build()).build();
+        assertEquals(expectedData, normalizedNode);
+    }
+
+    @Test
+    public void readOrderedLeafListDataAllTest() {
+        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.orderedLeafSetNode1))).when(read)
+                .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath);
+        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.orderedLeafSetNode2))).when(read)
+                .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath);
+        doReturn(DATA.leafSetNodePath).when(context).getInstanceIdentifier();
+
+        final NormalizedNode<?, ?> normalizedNode =
+                ReadDataTransactionUtil.readData(RestconfDataServiceConstant.ReadData.ALL, wrapper, schemaContext);
+
+        final LeafSetNode<String> expectedData = Builders.<String>orderedLeafSetBuilder().withNodeIdentifier(
+                new YangInstanceIdentifier.NodeIdentifier(DATA.leafListQname)).withValue(
+                        ImmutableList.<LeafSetEntryNode<String>>builder().addAll(DATA.orderedLeafSetNode1.getValue())
+                        .addAll(DATA.orderedLeafSetNode2.getValue()).build()).build();
+        assertEquals(expectedData, normalizedNode);
+    }
+
     @Test
     public void readDataWrongPathOrNoContentTest() {
         doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(read)
index 832e8205db50845df75f8d8c6104966b62832e0c..94905d21b18001c332f7fa32c756a08d2d6ddf80 100644 (file)
@@ -12,9 +12,12 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
+import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
@@ -29,7 +32,8 @@ class TestData {
     final ContainerNode data4;
     final MapNode listData;
     final MapNode listData2;
-    final UnkeyedListEntryNode unkeyedListEntryNode;
+    final OrderedMapNode orderedMapNode1;
+    final OrderedMapNode orderedMapNode2;
     final LeafNode contentLeaf;
     final LeafNode contentLeaf2;
     final MapEntryNode checkData;
@@ -37,11 +41,22 @@ class TestData {
     final SchemaPath errorRpc;
     final ContainerNode input;
     final ContainerNode output;
+    final LeafSetNode<String> leafSetNode1;
+    final LeafSetNode<String> leafSetNode2;
+    final LeafSetNode<String> orderedLeafSetNode1;
+    final LeafSetNode<String> orderedLeafSetNode2;
+    final YangInstanceIdentifier leafSetNodePath;
+    final UnkeyedListNode unkeyedListNode1;
+    final UnkeyedListNode unkeyedListNode2;
+    final UnkeyedListEntryNode unkeyedListEntryNode1;
+    final UnkeyedListEntryNode unkeyedListEntryNode2;
+
+    final QName base = QName.create("ns", "2016-02-28", "base");
+    final QName listKeyQName = QName.create(base, "list-key");
+    final QName leafListQname = QName.create(base, "leaf-list");
+    final QName listQname = QName.create(base, "list");
 
     TestData() {
-        final QName base = QName.create("ns", "2016-02-28", "base");
-        final QName listQname = QName.create(base, "list");
-        final QName listKeyQName = QName.create(base, "list-key");
         final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeWithKey =
                 new YangInstanceIdentifier.NodeIdentifierWithPredicates(listQname, listKeyQName, "keyValue");
         final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeWithKey2 =
@@ -60,10 +75,6 @@ class TestData {
                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(listQname, "identifier")))
                 .withValue("id")
                 .build();
-        unkeyedListEntryNode = Builders.unkeyedListEntryBuilder()
-                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create(listQname, "list")))
-                .withChild(dataContainer)
-                .build();
         data = Builders.mapEntryBuilder()
                 .withNodeIdentifier(nodeWithKey)
                 .withChild(content)
@@ -117,6 +128,37 @@ class TestData {
                 .withChild(contentLeaf2)
                 .build();
 
+        leafSetNodePath = YangInstanceIdentifier.builder().node(QName.create(base, "cont"))
+                .node(leafListQname).build();
+        leafSetNode1 = Builders.<String>leafSetBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(
+                leafListQname)).withChildValue("one").withChildValue("two").build();
+
+        leafSetNode2 = Builders.<String>leafSetBuilder().withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(
+                leafListQname)).withChildValue("three").build();
+
+        orderedLeafSetNode1 = Builders.<String>orderedLeafSetBuilder().withNodeIdentifier(
+                new YangInstanceIdentifier.NodeIdentifier(leafListQname)).withChildValue("one")
+                .withChildValue("two").build();
+        orderedLeafSetNode2 = Builders.<String>orderedLeafSetBuilder().withNodeIdentifier(
+                new YangInstanceIdentifier.NodeIdentifier(leafListQname)).withChildValue("three")
+                .withChildValue("four").build();
+
+        orderedMapNode1 = Builders.orderedMapBuilder()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(listQname)).withChild(data).build();
+
+        orderedMapNode2 = Builders.orderedMapBuilder()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(listQname)).withChild(data)
+                .withChild(data2).build();
+
+        unkeyedListEntryNode1 = Builders.unkeyedListEntryBuilder().withNodeIdentifier(
+                new YangInstanceIdentifier.NodeIdentifier(listQname)).withChild(content).build();
+        unkeyedListNode1 = Builders.unkeyedListBuilder().withNodeIdentifier(
+                new YangInstanceIdentifier.NodeIdentifier(listQname)).withChild(unkeyedListEntryNode1).build();
+
+        unkeyedListEntryNode2 = Builders.unkeyedListEntryBuilder().withNodeIdentifier(
+                new YangInstanceIdentifier.NodeIdentifier(listQname)).withChild(content2).build();
+        unkeyedListNode2 = Builders.unkeyedListBuilder().withNodeIdentifier(
+                new YangInstanceIdentifier.NodeIdentifier(listQname)).withChild(unkeyedListEntryNode2).build();
 
         final QName rpcQname = QName.create("ns", "2015-02-28", "test-rpc");
         final QName errorRpcQname = QName.create(rpcQname, "error-rpc");
@@ -139,4 +181,4 @@ class TestData {
                 .withChild(resultLeafNode)
                 .build();
     }
-}
\ No newline at end of file
+}