Migrate netconf to MD-SAL APIs
[netconf.git] / netconf / netconf-console / src / main / java / org / opendaylight / netconf / console / impl / NetconfCommandsImpl.java
index 810a4389d6a1f72a2e980e2a120dc9e2491200c7..7c698b31626bdb583bf0913790d5620ca051d03c 100644 (file)
@@ -5,23 +5,25 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.console.impl;
 
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableList;
 import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.MoreExecutors;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.UUID;
+import java.util.concurrent.ExecutionException;
 import java.util.stream.Collectors;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.WriteTransaction;
+import org.opendaylight.mdsal.common.api.CommitInfo;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netconf.console.api.NetconfCommands;
 import org.opendaylight.netconf.console.utils.NetconfConsoleConstants;
 import org.opendaylight.netconf.console.utils.NetconfConsoleUtils;
@@ -32,6 +34,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
@@ -64,13 +67,14 @@ public class NetconfCommandsImpl implements NetconfCommands {
         }
         final Map<String, Map<String, String>> netconfNodes = new HashMap<>();
         for (final Node node : topology.getNode()) {
-            final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
+            final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
             final Map<String, String> attributes = new HashMap<>();
             attributes.put(NetconfConsoleConstants.NETCONF_ID, node.getNodeId().getValue());
             attributes.put(NetconfConsoleConstants.NETCONF_IP,
                     netconfNode.getHost().getIpAddress().getIpv4Address().getValue());
             attributes.put(NetconfConsoleConstants.NETCONF_PORT, netconfNode.getPort().getValue().toString());
-            attributes.put(NetconfConsoleConstants.STATUS, netconfNode.getConnectionStatus().name().toLowerCase());
+            attributes.put(NetconfConsoleConstants.STATUS, netconfNode.getConnectionStatus().name()
+                    .toLowerCase(Locale.ROOT));
             netconfNodes.put(node.getNodeId().getValue(), attributes);
         }
         return netconfNodes;
@@ -88,7 +92,7 @@ public class NetconfCommandsImpl implements NetconfCommands {
         if (nodeList != null) {
             for (final Node node : nodeList) {
                 if (node != null) {
-                    final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
+                    final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
                     final Map<String, List<String>> attributes = new HashMap<>();
                     attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue()));
                     attributes.put(NetconfConsoleConstants.NETCONF_IP,
@@ -97,9 +101,14 @@ public class NetconfCommandsImpl implements NetconfCommands {
                             ImmutableList.of(netconfNode.getPort().getValue().toString()));
                     attributes.put(NetconfConsoleConstants.STATUS,
                             ImmutableList.of(netconfNode.getConnectionStatus().name()));
-                    attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES,
-                            netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
-                                    .map(AvailableCapability::getCapability).collect(Collectors.toList()));
+                    if (netconfNode.getConnectionStatus().equals(
+                            NetconfNodeConnectionStatus.ConnectionStatus.Connected)) {
+                        attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES,
+                                netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
+                                        .map(AvailableCapability::getCapability).collect(Collectors.toList()));
+                    } else {
+                        attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Collections.singletonList(""));
+                    }
                     device.put(node.getNodeId().getValue(), attributes);
                 }
             }
@@ -113,7 +122,7 @@ public class NetconfCommandsImpl implements NetconfCommands {
         final List<Node> nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceId, dataBroker);
         if (nodeList != null && nodeList.size() > 0) {
             for (final Node node : nodeList) {
-                final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
+                final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
                 final Map<String, List<String>> attributes = new HashMap<>();
                 attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue()));
                 attributes.put(NetconfConsoleConstants.NETCONF_IP,
@@ -122,9 +131,13 @@ public class NetconfCommandsImpl implements NetconfCommands {
                         ImmutableList.of(netconfNode.getPort().getValue().toString()));
                 attributes.put(NetconfConsoleConstants.STATUS,
                         ImmutableList.of(netconfNode.getConnectionStatus().name()));
-                attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES,
-                        netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
-                                .map(AvailableCapability::getCapability).collect(Collectors.toList()));
+                if (netconfNode.getConnectionStatus().equals(NetconfNodeConnectionStatus.ConnectionStatus.Connected)) {
+                    attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES,
+                            netconfNode.getAvailableCapabilities().getAvailableCapability().stream()
+                                    .map(AvailableCapability::getCapability).collect(Collectors.toList()));
+                } else {
+                    attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Collections.singletonList(""));
+                }
                 device.put(node.getNodeId().getValue(), attributes);
             }
         }
@@ -132,7 +145,7 @@ public class NetconfCommandsImpl implements NetconfCommands {
     }
 
     @Override
-    public void connectDevice(NetconfNode netconfNode, String netconfNodeId) {
+    public void connectDevice(final NetconfNode netconfNode, final String netconfNodeId) {
         final NodeId nodeId;
         if (!Strings.isNullOrEmpty(netconfNodeId)) {
             nodeId = new NodeId(netconfNodeId);
@@ -140,7 +153,7 @@ public class NetconfCommandsImpl implements NetconfCommands {
             nodeId = new NodeId(UUID.randomUUID().toString().replace("-", ""));
         }
         final Node node = new NodeBuilder()
-                .setKey(new NodeKey(nodeId))
+                .withKey(new NodeKey(nodeId))
                 .setNodeId(nodeId)
                 .addAugmentation(NetconfNode.class, netconfNode)
                 .build();
@@ -148,36 +161,33 @@ public class NetconfCommandsImpl implements NetconfCommands {
         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
         transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(nodeId.getValue()), node);
 
-        Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
-
+        transaction.commit().addCallback(new FutureCallback<CommitInfo>() {
             @Override
-            public void onSuccess(Void result) {
+            public void onSuccess(final CommitInfo result) {
                 LOG.debug("NetconfNode={} created successfully", netconfNode);
             }
 
             @Override
-            public void onFailure(Throwable throwable) {
-                LOG.error("Failed to created NetconfNode={}", netconfNode);
-                throw new RuntimeException(throwable);
+            public void onFailure(final Throwable throwable) {
+                LOG.error("Failed to created NetconfNode={}", netconfNode, throwable);
             }
-        });
+        }, MoreExecutors.directExecutor());
     }
 
     @Override
-    public boolean disconnectDevice(String netconfNodeId) {
-        boolean result = false;
+    public boolean disconnectDevice(final String netconfNodeId) {
         final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
-        InstanceIdentifier<Node> iid = NetconfIidFactory.netconfNodeIid(netconfNodeId);
+        final InstanceIdentifier<Node> iid = NetconfIidFactory.netconfNodeIid(netconfNodeId);
         transaction.delete(LogicalDatastoreType.CONFIGURATION, iid);
 
         try {
             LOG.debug("Deleting netconf node: {}", netconfNodeId);
-            transaction.submit().checkedGet();
-            result = true;
-        } catch (final TransactionCommitFailedException e) {
+            transaction.commit().get();
+            return true;
+        } catch (final InterruptedException | ExecutionException e) {
             LOG.error("Unable to remove node with Iid {}", iid, e);
+            return false;
         }
-        return result;
     }
 
     @Override
@@ -188,13 +198,13 @@ public class NetconfCommandsImpl implements NetconfCommands {
     }
 
     @Override
-    public String updateDevice(final String netconfNodeId, String username, String password,
-                               Map<String, String> updated) {
+    public String updateDevice(final String netconfNodeId, final String username, final String password,
+                               final Map<String, String> updated) {
         final Node node = NetconfConsoleUtils
                 .read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.netconfNodeIid(netconfNodeId), dataBroker);
 
-        if (node != null && node.getAugmentation(NetconfNode.class) != null) {
-            final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
+        if (node != null && node.augmentation(NetconfNode.class) != null) {
+            final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
 
             // Get NETCONF attributes to update if present else get their original values from NetconfNode instance
             final String deviceIp = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.NETCONF_IP))
@@ -202,9 +212,9 @@ public class NetconfCommandsImpl implements NetconfCommands {
                     : updated.get(NetconfConsoleConstants.NETCONF_IP);
             final String devicePort = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.NETCONF_PORT))
                     ? netconfNode.getPort().getValue().toString() : updated.get(NetconfConsoleConstants.NETCONF_PORT);
-            final Boolean tcpOnly = (updated.get(NetconfConsoleConstants.TCP_ONLY).equals("true")) ? true : false;
+            final Boolean tcpOnly = updated.get(NetconfConsoleConstants.TCP_ONLY).equals("true");
             final Boolean isSchemaless =
-                    (updated.get(NetconfConsoleConstants.SCHEMALESS).equals("true")) ? true : false;
+                    updated.get(NetconfConsoleConstants.SCHEMALESS).equals("true");
             final String newUsername = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.USERNAME))
                     ? updated.get(NetconfConsoleConstants.USERNAME) : username;
             final String newPassword = Strings.isNullOrEmpty(updated.get(NetconfConsoleConstants.PASSWORD))
@@ -221,7 +231,7 @@ public class NetconfCommandsImpl implements NetconfCommands {
                     .build();
 
             final Node updatedNode = new NodeBuilder()
-                    .setKey(node.getKey())
+                    .withKey(node.key())
                     .setNodeId(node.getNodeId())
                     .addAugmentation(NetconfNode.class, updatedNetconfNode)
                     .build();
@@ -230,19 +240,17 @@ public class NetconfCommandsImpl implements NetconfCommands {
             transaction.put(LogicalDatastoreType.CONFIGURATION,
                     NetconfIidFactory.netconfNodeIid(updatedNode.getNodeId().getValue()), updatedNode);
 
-            Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
-
+            transaction.commit().addCallback(new FutureCallback<CommitInfo>() {
                 @Override
-                public void onSuccess(Void result) {
+                public void onSuccess(final CommitInfo result) {
                     LOG.debug("NetconfNode={} updated successfully", netconfNode);
                 }
 
                 @Override
-                public void onFailure(Throwable throwable) {
-                    LOG.error("Failed to updated NetconfNode={}", netconfNode);
-                    throw new RuntimeException(throwable);
+                public void onFailure(final Throwable throwable) {
+                    LOG.error("Failed to updated NetconfNode={}", netconfNode, throwable);
                 }
-            });
+            }, MoreExecutors.directExecutor());
 
             return "NETCONF node: " + netconfNodeId + " updated successfully.";
         } else {