Bug 8153: Enforce check-style rules for netconf - sal-netconf-connector
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / ReadOnlyTx.java
index dda54851094e612bf97a86c3020349427a384e8f..b974d9fc9ae6f6a8a56d798ee61441093f30ef3c 100644 (file)
@@ -8,27 +8,19 @@
 
 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
 
-import com.google.common.base.Function;
 import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 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.dom.api.DOMDataReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
-import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
+import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
 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,77 +31,26 @@ public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
 
     private final NetconfBaseOps netconfOps;
     private final RemoteDeviceId id;
-    private final FutureCallback<DOMRpcResult> loggingCallback;
 
     public ReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id) {
         this.netconfOps = netconfOps;
         this.id = id;
-
-        // Simple logging callback to log result of read operation
-        loggingCallback = new FutureCallback<DOMRpcResult>() {
-            @Override
-            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());
-                }
-
-            }
-
-            @Override
-            public void onFailure(final Throwable t) {
-                LOG.warn("{}: Reading data failed", id, t);
-            }
-        };
     }
 
     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readConfigurationData(
             final YangInstanceIdentifier path) {
-        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 DOMRpcResult result) {
-                checkReadSuccess(result, path);
+        final ListenableFuture<Optional<NormalizedNode<?, ?>>> configRunning = netconfOps.getConfigRunningData(
+                new NetconfRpcFutureCallback("Data read", id), Optional.fromNullable(path));
 
-                final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataNode = findDataNode(result);
-                return NormalizedNodes.findNode(dataNode, path.getPathArguments());
-            }
-        });
-
-        return MappingCheckedFuture.create(transformedFuture, ReadFailedException.MAPPER);
-    }
-
-    private DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> findDataNode(final DOMRpcResult result) {
-        return ((ContainerNode) result.getResult()).getChild(NetconfMessageTransformUtil.toId(NetconfMessageTransformUtil.NETCONF_DATA_QNAME)).get();
-    }
-
-    private void checkReadSuccess(final DOMRpcResult result, final YangInstanceIdentifier path) {
-        try {
-            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;
-        }
+        return MappingCheckedFuture.create(configRunning, ReadFailedException.MAPPER);
     }
 
     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readOperationalData(
             final YangInstanceIdentifier 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<DOMRpcResult, Optional<NormalizedNode<?, ?>>>() {
-            @Override
-            public Optional<NormalizedNode<?, ?>> apply(final DOMRpcResult result) {
-                checkReadSuccess(result, path);
-
-                final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataNode = findDataNode(result);
-                return NormalizedNodes.findNode(dataNode, path.getPathArguments());
-            }
-        });
+        final ListenableFuture<Optional<NormalizedNode<?, ?>>> configCandidate = netconfOps.getData(
+                new NetconfRpcFutureCallback("Data read", id), Optional.fromNullable(path));
 
-        return MappingCheckedFuture.create(transformedFuture, ReadFailedException.MAPPER);
+        return MappingCheckedFuture.create(configCandidate, ReadFailedException.MAPPER);
     }
 
     @Override
@@ -121,26 +62,29 @@ public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
             final LogicalDatastoreType store, final YangInstanceIdentifier path) {
         switch (store) {
-        case CONFIGURATION: {
-            return readConfigurationData(path);
-        }
-        case OPERATIONAL: {
-            return readOperationalData(path);
-        }
+            case CONFIGURATION: {
+                return readConfigurationData(path);
+            }
+            case OPERATIONAL: {
+                return readOperationalData(path);
+            }
+            default: {
+                LOG.info("Unknown datastore type: {}.", store);
+            }
         }
 
-        throw new IllegalArgumentException(String.format("%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
+        throw new IllegalArgumentException(String.format(
+                "%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
     }
 
     @Override
-    public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
+    public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
+                                                              final YangInstanceIdentifier path) {
         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> data = read(store, path);
-
-        try {
-            return Futures.immediateCheckedFuture(data.get().isPresent());
-        } catch (InterruptedException | ExecutionException e) {
-            return Futures.immediateFailedCheckedFuture(new ReadFailedException("Exists failed",e));
-        }
+        final ListenableFuture<Boolean> result =
+                Futures.transform(data, (Optional<NormalizedNode<?, ?>> optionalNode) ->
+                        optionalNode != null && optionalNode.isPresent());
+        return MappingCheckedFuture.create(result, ReadFailedException.MAPPER);
     }
 
     @Override