Eliminate unnecessary blocking checks
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / transactions / NetconfRestconfStrategy.java
index 51bdd14d09742793ada7ffb261d2bf3293add718..19a60f376d17b7bbdaa3476fdb70e6b565735097 100644 (file)
@@ -26,7 +26,12 @@ import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+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.NormalizedNodeContainer;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -85,6 +90,11 @@ public final class NetconfRestconfStrategy extends RestconfStrategy {
         resultsFutures.add(netconfService.delete(store, path));
     }
 
+    @Override
+    public void remove(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
+        resultsFutures.add(netconfService.remove(store, path));
+    }
+
     @Override
     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
                       final NormalizedNode<?, ?> data) {
@@ -93,14 +103,36 @@ public final class NetconfRestconfStrategy extends RestconfStrategy {
 
     @Override
     public void create(final LogicalDatastoreType store, final YangInstanceIdentifier path,
-                       final NormalizedNode<?, ?> data) {
-        resultsFutures.add(netconfService.create(store, path, data, Optional.empty()));
+                       final NormalizedNode<?, ?> data, final SchemaContext schemaContext) {
+        if (data instanceof MapNode || data instanceof LeafSetNode) {
+            final NormalizedNode<?, ?> emptySubTree = ImmutableNodes.fromInstanceId(schemaContext, path);
+            merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create(emptySubTree.getIdentifier()),
+                emptySubTree);
+
+            for (final NormalizedNode<?, ?> child : ((NormalizedNodeContainer<?, ?, ?>) data).getValue()) {
+                final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
+                resultsFutures.add(netconfService.create(store, childPath, child, Optional.empty()));
+            }
+        } else {
+            resultsFutures.add(netconfService.create(store, path, data, Optional.empty()));
+        }
     }
 
     @Override
     public void replace(final LogicalDatastoreType store, final YangInstanceIdentifier path,
-                        final NormalizedNode<?, ?> data) {
-        resultsFutures.add(netconfService.replace(store, path, data, Optional.empty()));
+                        final NormalizedNode<?, ?> data, final SchemaContext schemaContext) {
+        if (data instanceof MapNode || data instanceof LeafSetNode) {
+            final NormalizedNode<?, ?> emptySubTree = ImmutableNodes.fromInstanceId(schemaContext, path);
+            merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create(emptySubTree.getIdentifier()),
+                emptySubTree);
+
+            for (final NormalizedNode<?, ?> child : ((NormalizedNodeContainer<?, ?, ?>) data).getValue()) {
+                final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
+                resultsFutures.add(netconfService.replace(store, childPath, child, Optional.empty()));
+            }
+        } else {
+            resultsFutures.add(netconfService.replace(store, path, data, Optional.empty()));
+        }
     }
 
     @Override
@@ -136,7 +168,7 @@ public final class NetconfRestconfStrategy extends RestconfStrategy {
             @Override
             public void onFailure(final Throwable cause) {
                 ret.setException(cause instanceof ReadFailedException ? cause
-                        : new ReadFailedException("NETCONF operation failed", cause));
+                    : new ReadFailedException("NETCONF operation failed", cause));
             }
         }, MoreExecutors.directExecutor());
         return FluentFuture.from(ret);