NETCONF-512: Add cases for missing NormalizedNode types
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / ReadDataTransactionUtil.java
index b973d26e2f1c79083440b22314a0760821d335b6..aca94271bdf65807d8fad4654ed045cdcb1dbda2 100644 (file)
@@ -9,7 +9,9 @@ package org.opendaylight.restconf.nb.rfc8040.rests.utils;
 
 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAMS_PATH;
 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.STREAM_PATH_PART;
+
 import com.google.common.base.Optional;
+import com.google.common.collect.Iterables;
 import com.google.common.primitives.Ints;
 import com.google.common.util.concurrent.CheckedFuture;
 import java.net.URI;
@@ -49,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;
@@ -169,9 +178,9 @@ public final class ReadDataTransactionUtil {
         if (!depth.get(0).equals(RestconfDataServiceConstant.ReadData.UNBOUNDED)) {
             final Integer value = Ints.tryParse(depth.get(0));
 
-            if ((value == null)
-                    || (!((value >= RestconfDataServiceConstant.ReadData.MIN_DEPTH)
-                        && (value <= RestconfDataServiceConstant.ReadData.MAX_DEPTH)))) {
+            if (value == null
+                    || !(value >= RestconfDataServiceConstant.ReadData.MIN_DEPTH
+                        && value <= RestconfDataServiceConstant.ReadData.MAX_DEPTH)) {
                 throw new RestconfDocumentedException(
                         new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
                                 "Invalid depth parameter: " + depth, null,
@@ -197,6 +206,7 @@ public final class ReadDataTransactionUtil {
      * @param transactionNode
      *            {@link TransactionVarsWrapper} - wrapper for variables
      * @param schemaContext
+     *            schema context
      * @return {@link NormalizedNode}
      */
     @Nullable
@@ -215,6 +225,7 @@ public final class ReadDataTransactionUtil {
      * @param withDefa
      *            vaule of with-defaults parameter
      * @param ctx
+     *            schema context
      * @return {@link NormalizedNode}
      */
     @Nullable
@@ -277,9 +288,7 @@ public final class ReadDataTransactionUtil {
                 final List<NotificationListenerAdapter> notifiStreamJSON =
                         CreateStreamUtil.createYangNotifiStream(notificationDefinition, schemaContextRef,
                                 NotificationOutputType.JSON.getName());
-                notifiStreamJSON.addAll(notifiStreamXML);
-
-                for (final NotificationListenerAdapter listener : notifiStreamJSON) {
+                for (final NotificationListenerAdapter listener : Iterables.concat(notifiStreamXML, notifiStreamJSON)) {
                     final URI uri = SubscribeToStreamUtil.prepareUriByStreamName(uriInfo, listener.getStreamName());
                     final NormalizedNode mapToStreams =
                             RestconfMappingNodeUtil.mapYangNotificationStreamByIetfRestconfMonitoring(
@@ -345,8 +354,8 @@ public final class ReadDataTransactionUtil {
                         ((ListSchemaNode) childSchema).getKeyDefinition());
                 builder.withChild(childBuilder.build());
             } else if (child instanceof LeafNode) {
-                final String defaultVal = ((LeafSchemaNode) childSchema).getDefault();
-                final String nodeVal = ((LeafNode<String>) child).getValue();
+                final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
+                final Object nodeVal = ((LeafNode<?>) child).getValue();
                 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
                         Builders.leafBuilder((LeafSchemaNode) childSchema);
                 if (keys.contains(child.getNodeType())) {
@@ -354,12 +363,12 @@ public final class ReadDataTransactionUtil {
                     builder.withChild(leafBuilder.build());
                 } else {
                     if (trim) {
-                        if ((defaultVal == null) || !defaultVal.equals(nodeVal)) {
+                        if (defaultVal == null || !defaultVal.equals(nodeVal)) {
                             leafBuilder.withValue(((LeafNode) child).getValue());
                             builder.withChild(leafBuilder.build());
                         }
                     } else {
-                        if ((defaultVal != null) && defaultVal.equals(nodeVal)) {
+                        if (defaultVal != null && defaultVal.equals(nodeVal)) {
                             leafBuilder.withValue(((LeafNode) child).getValue());
                             builder.withChild(leafBuilder.build());
                         }
@@ -400,17 +409,17 @@ public final class ReadDataTransactionUtil {
                         ((ListSchemaNode) childSchema).getKeyDefinition());
                 builder.withChild(childBuilder.build());
             } else if (child instanceof LeafNode) {
-                final String defaultVal = ((LeafSchemaNode) childSchema).getDefault();
-                final String nodeVal = ((LeafNode<String>) child).getValue();
+                final Object defaultVal = ((LeafSchemaNode) childSchema).getType().getDefaultValue().orElse(null);
+                final Object nodeVal = ((LeafNode<?>) child).getValue();
                 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder =
                         Builders.leafBuilder((LeafSchemaNode) childSchema);
                 if (trim) {
-                    if ((defaultVal == null) || !defaultVal.equals(nodeVal)) {
+                    if (defaultVal == null || !defaultVal.equals(nodeVal)) {
                         leafBuilder.withValue(((LeafNode) child).getValue());
                         builder.withChild(leafBuilder.build());
                     }
                 } else {
-                    if ((defaultVal != null) && defaultVal.equals(nodeVal)) {
+                    if (defaultVal != null && defaultVal.equals(nodeVal)) {
                         leafBuilder.withValue(((LeafNode) child).getValue());
                         builder.withChild(leafBuilder.build());
                     }
@@ -450,6 +459,7 @@ public final class ReadDataTransactionUtil {
      * @param withDefa
      *            with-defaults parameter
      * @param ctx
+     *            schema context
      * @return {@link NormalizedNode}
      */
     @Nullable
@@ -470,7 +480,7 @@ public final class ReadDataTransactionUtil {
         }
 
         // if no data exists
-        if ((stateDataNode == null) && (configDataNode == null)) {
+        if (stateDataNode == null && configDataNode == null) {
             return null;
         }
 
@@ -572,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());
 
@@ -617,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());
         }
     }