BUG-2314 Migrate netconf-connector to NormalizedNode
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / sal / tx / ReadOnlyTx.java
index 00bdbb6db0d69f2cdd95e7fc81000de1b0646e82..e08731ed407525735e15c4ba424d85847433d49d 100644 (file)
@@ -7,8 +7,6 @@
  */
 package org.opendaylight.controller.sal.connect.netconf.sal.tx;
 
-import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DATA_QNAME;
-
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
@@ -19,17 +17,17 @@ import com.google.common.util.concurrent.ListenableFuture;
 import java.util.concurrent.ExecutionException;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
-import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
-import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 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.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -39,19 +37,18 @@ public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
     private static final Logger LOG  = LoggerFactory.getLogger(ReadOnlyTx.class);
 
     private final NetconfBaseOps netconfOps;
-    private final DataNormalizer normalizer;
     private final RemoteDeviceId id;
-    private final FutureCallback<RpcResult<CompositeNode>> loggingCallback;
+    private final FutureCallback<DOMRpcResult> loggingCallback;
 
-    public ReadOnlyTx(final NetconfBaseOps netconfOps, final DataNormalizer normalizer, final RemoteDeviceId id) {
+    public ReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id) {
         this.netconfOps = netconfOps;
-        this.normalizer = normalizer;
         this.id = id;
+
         // Simple logging callback to log result of read operation
-        loggingCallback = new FutureCallback<RpcResult<CompositeNode>>() {
+        loggingCallback = new FutureCallback<DOMRpcResult>() {
             @Override
-            public void onSuccess(final RpcResult<CompositeNode> result) {
-                if(result.isSuccessful()) {
+            public void onSuccess(final DOMRpcResult result) {
+                if(AbstractWriteTx.isSuccess(result)) {
                     LOG.trace("{}: Reading data successful", id);
                 } else {
                     LOG.warn("{}: Reading data unsuccessful: {}", id, result.getErrors());
@@ -68,62 +65,46 @@ public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
 
     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readConfigurationData(
             final YangInstanceIdentifier path) {
-        final ListenableFuture<RpcResult<CompositeNode>> configRunning = netconfOps.getConfigRunning(loggingCallback, Optional.fromNullable(path));
-        // Find data node and normalize its content
-        final ListenableFuture<Optional<NormalizedNode<?, ?>>> transformedFuture = Futures.transform(configRunning, new Function<RpcResult<CompositeNode>, Optional<NormalizedNode<?, ?>>>() {
+        final ListenableFuture<DOMRpcResult> configRunning = netconfOps.getConfigRunning(loggingCallback, Optional.fromNullable(path));
+
+        final ListenableFuture<Optional<NormalizedNode<?, ?>>> transformedFuture = Futures.transform(configRunning, new Function<DOMRpcResult, Optional<NormalizedNode<?, ?>>>() {
             @Override
-            public Optional<NormalizedNode<?, ?>> apply(final RpcResult<CompositeNode> result) {
+            public Optional<NormalizedNode<?, ?>> apply(final DOMRpcResult result) {
                 checkReadSuccess(result, path);
 
-                final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
-                final CompositeNode node = (CompositeNode) NetconfMessageTransformUtil.findNode(data, path);
-
-                return data == null ?
-                        Optional.<NormalizedNode<?, ?>>absent() :
-                        transform(path, node);
+                final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataNode = findDataNode(result);
+                return NormalizedNodes.findNode(dataNode, path.getPathArguments());
             }
         });
 
         return MappingCheckedFuture.create(transformedFuture, ReadFailedException.MAPPER);
     }
 
-    private void checkReadSuccess(final RpcResult<CompositeNode> result, final YangInstanceIdentifier path) {
-        try {
-            Preconditions.checkArgument(result.isSuccessful(), "%s: Unable to read data: %s, errors: %s", id, path, result.getErrors());
-        } catch (final IllegalArgumentException e) {
-            LOG.warn("{}: Unable to read data: {}, errors: {}", id, path, result.getErrors());
-            throw e;
-        }
+    private DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> findDataNode(final DOMRpcResult result) {
+        return ((ContainerNode) result.getResult()).getChild(NetconfMessageTransformUtil.toId(NetconfMessageTransformUtil.NETCONF_DATA_QNAME)).get();
     }
 
-    private Optional<NormalizedNode<?, ?>> transform(final YangInstanceIdentifier path, final CompositeNode node) {
-        if(node == null) {
-            return Optional.absent();
-        }
+    private void checkReadSuccess(final DOMRpcResult result, final YangInstanceIdentifier path) {
         try {
-            return Optional.<NormalizedNode<?, ?>>of(normalizer.toNormalized(path, node).getValue());
-        } catch (final Exception e) {
-            LOG.error("{}: Unable to normalize data for {}, data: {}", id, path, node, e);
+            Preconditions.checkArgument(AbstractWriteTx.isSuccess(result), "%s: Unable to read data: %s, errors: %s", id, path, result.getErrors());
+        } catch (final IllegalArgumentException e) {
+            LOG.warn("{}: Unable to read data: {}, errors: {}", id, path, result.getErrors());
             throw e;
         }
     }
 
     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readOperationalData(
             final YangInstanceIdentifier path) {
-        final ListenableFuture<RpcResult<CompositeNode>> configCandidate = netconfOps.get(loggingCallback, Optional.fromNullable(path));
+        final ListenableFuture<DOMRpcResult> configCandidate = netconfOps.get(loggingCallback, Optional.fromNullable(path));
 
         // Find data node and normalize its content
-        final ListenableFuture<Optional<NormalizedNode<?, ?>>> transformedFuture = Futures.transform(configCandidate, new Function<RpcResult<CompositeNode>, Optional<NormalizedNode<?, ?>>>() {
+        final ListenableFuture<Optional<NormalizedNode<?, ?>>> transformedFuture = Futures.transform(configCandidate, new Function<DOMRpcResult, Optional<NormalizedNode<?, ?>>>() {
             @Override
-            public Optional<NormalizedNode<?, ?>> apply(final RpcResult<CompositeNode> result) {
+            public Optional<NormalizedNode<?, ?>> apply(final DOMRpcResult result) {
                 checkReadSuccess(result, path);
 
-                final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
-                final CompositeNode node = (CompositeNode) NetconfMessageTransformUtil.findNode(data, path);
-
-                return data == null ?
-                        Optional.<NormalizedNode<?, ?>>absent() :
-                        transform(path, node);
+                final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataNode = findDataNode(result);
+                return NormalizedNodes.findNode(dataNode, path.getPathArguments());
             }
         });
 
@@ -138,14 +119,12 @@ public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
     @Override
     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
             final LogicalDatastoreType store, final YangInstanceIdentifier path) {
-        final YangInstanceIdentifier legacyPath = toLegacyPath(normalizer, path, id);
-
         switch (store) {
             case CONFIGURATION : {
-                return readConfigurationData(legacyPath);
+                return readConfigurationData(path);
             }
             case OPERATIONAL : {
-                return readOperationalData(legacyPath);
+                return readOperationalData(path);
             }
         }
 
@@ -154,8 +133,7 @@ public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
 
     @Override
     public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
-        final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException>
-            data = read(store, path);
+        final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> data = read(store, path);
 
         try {
             return Futures.immediateCheckedFuture(data.get().isPresent());
@@ -164,14 +142,6 @@ public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
         }
     }
 
-    static YangInstanceIdentifier toLegacyPath(final DataNormalizer normalizer, final YangInstanceIdentifier path, final RemoteDeviceId id) {
-        try {
-            return normalizer.toLegacy(path);
-        } catch (final DataNormalizationException e) {
-            throw new IllegalArgumentException(id + ": Cannot normalize path " + path, e);
-        }
-    }
-
     @Override
     public Object getIdentifier() {
         return this;