NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / cli / l2gw / NetworkL2gwDeviceInfoCli.java
index 4f83281f24c4a9c9940d2f47be06476ec94e7b61..6894dfbc61b150c27466916a7214e5da6f733a12 100644 (file)
@@ -8,21 +8,23 @@
 
 package org.opendaylight.netvirt.elan.cli.l2gw;
 
-import com.google.common.base.Optional;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
+import java.util.concurrent.ExecutionException;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.commands.Option;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
@@ -90,70 +92,79 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
     }
 
     @Override
-    protected Object doExecute() throws Exception {
+    @Nullable
+    protected Object doExecute() {
         List<Node> nodes = new ArrayList<>();
         Set<String> networks = new HashSet<>();
-        if (nodeId == null) {
-            Optional<Topology> topologyOptional = MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL,
-                    createHwvtepTopologyInstanceIdentifier());
-            if (topologyOptional.isPresent()) {
-                nodes = topologyOptional.get().getNode();
-            }
-        } else {
-            Optional<Node> nodeOptional = MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL,
-                    createInstanceIdentifier(new NodeId(new Uri(nodeId))));
-            if (nodeOptional.isPresent()) {
-                nodes.add(nodeOptional.get());
+        try {
+            if (nodeId == null) {
+                Optional<Topology> topologyOptional = SingleTransactionDataBroker.syncReadOptional(dataBroker,
+                        LogicalDatastoreType.OPERATIONAL, createHwvtepTopologyInstanceIdentifier());
+                if (topologyOptional.isPresent()) {
+                    nodes.addAll(topologyOptional.get().nonnullNode());
+                }
+            } else {
+                Optional<Node> nodeOptional = SingleTransactionDataBroker.syncReadOptional(dataBroker,
+                        LogicalDatastoreType.OPERATIONAL, createInstanceIdentifier(new NodeId(new Uri(nodeId))));
+                if (nodeOptional.isPresent()) {
+                    nodes.add(nodeOptional.get());
+                }
             }
-        }
-        if (elanName == null) {
-            //get all elan instance
-            //get all device node id
-            //print result
-            Optional<ElanInstances> elanInstancesOptional = MDSALUtil.read(dataBroker,
-                    LogicalDatastoreType.CONFIGURATION,
-                    InstanceIdentifier.builder(ElanInstances.class).build());
-            if (elanInstancesOptional.isPresent()) {
-                List<ElanInstance> elans = elanInstancesOptional.get().getElanInstance();
-                if (elans != null) {
-                    for (ElanInstance elan : elans) {
-                        networks.add(elan.getElanInstanceName());
+            if (elanName == null) {
+                //get all elan instance
+                //get all device node id
+                //print result
+                Optional<ElanInstances> elanInstancesOptional = SingleTransactionDataBroker.syncReadOptional(dataBroker,
+                        LogicalDatastoreType.CONFIGURATION,
+                        InstanceIdentifier.builder(ElanInstances.class).build());
+                if (elanInstancesOptional.isPresent()) {
+                    List<ElanInstance> elans = elanInstancesOptional.get().getElanInstance();
+                    if (elans != null) {
+                        for (ElanInstance elan : elans) {
+                            networks.add(elan.getElanInstanceName());
+                        }
                     }
                 }
+            } else {
+                networks.add(elanName);
             }
-        } else {
-            networks.add(elanName);
-        }
 
-        for (Node node : nodes) {
-            if (node.getNodeId().getValue().contains("physicalswitch")) {
-                continue;
-            }
-            Node hwvtepConfigNode =
-                    HwvtepUtils.getHwVtepNode(dataBroker, LogicalDatastoreType.CONFIGURATION, node.getNodeId());
-            Node hwvtepOpPsNode = getPSnode(node, LogicalDatastoreType.OPERATIONAL);
-            Node hwvtepConfigPsNode = null;
-            if (hwvtepOpPsNode != null) {
-                hwvtepConfigPsNode = HwvtepUtils.getHwVtepNode(dataBroker, LogicalDatastoreType.CONFIGURATION,
-                        hwvtepOpPsNode.getNodeId());
-                opPSNodes.put(node.getNodeId(), hwvtepOpPsNode);
-            }
-            opNodes.put(node.getNodeId(), node);
-            configNodes.put(node.getNodeId(), hwvtepConfigNode);
+            if (nodes != null) {
+                for (Node node : nodes) {
+                    if (node.getNodeId().getValue().contains("physicalswitch")) {
+                        continue;
+                    }
+                    Node hwvtepConfigNode =
+                            HwvtepUtils.getHwVtepNode(dataBroker, LogicalDatastoreType.CONFIGURATION, node.getNodeId());
+                    Node hwvtepOpPsNode = getPSnode(node, LogicalDatastoreType.OPERATIONAL);
+                    Node hwvtepConfigPsNode = null;
+                    if (hwvtepOpPsNode != null) {
+                        hwvtepConfigPsNode = HwvtepUtils.getHwVtepNode(dataBroker, LogicalDatastoreType.CONFIGURATION,
+                                hwvtepOpPsNode.getNodeId());
+                        opPSNodes.put(node.getNodeId(), hwvtepOpPsNode);
+                    }
+                    opNodes.put(node.getNodeId(), node);
+                    configNodes.put(node.getNodeId(), hwvtepConfigNode);
 
-            if (hwvtepConfigPsNode != null) {
-                configPSNodes.put(node.getNodeId(), hwvtepConfigPsNode);
+                    if (hwvtepConfigPsNode != null) {
+                        configPSNodes.put(node.getNodeId(), hwvtepConfigPsNode);
+                    }
+                }
             }
-        }
-        for (String network : networks) {
-            session.getConsole().println("Network info for " + network);
-            for (Node node : nodes) {
-                if (node.getNodeId().getValue().contains("physicalswitch")) {
-                    continue;
+            if (!networks.isEmpty()) {
+                for (String network : networks) {
+                    session.getConsole().println("Network info for " + network);
+                    for (Node node : nodes) {
+                        if (node.getNodeId().getValue().contains("physicalswitch")) {
+                            continue;
+                        }
+                        session.getConsole().println("Printing for node " + node.getNodeId().getValue());
+                        process(node.getNodeId(), network);
+                    }
                 }
-                session.getConsole().println("Printing for node " + node.getNodeId().getValue());
-                process(node.getNodeId(), network);
             }
+        } catch (ExecutionException | InterruptedException e) {
+            session.getConsole().println("Failed with error " + e.getMessage());
         }
         return null;
     }
@@ -190,11 +201,11 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
     void printRemoteUcastMacs(Node hwvtepNode, String elanName) {
         session.getConsole().println("RemoteUCast macs :");
         session.getConsole().println(HEADINGUCAST);
-        if (hwvtepNode == null || hwvtepNode.getAugmentation(HwvtepGlobalAugmentation.class) == null) {
+        if (hwvtepNode == null || hwvtepNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
             return;
         }
         List<RemoteUcastMacs> remoteUcastMacs =
-                hwvtepNode.getAugmentation(HwvtepGlobalAugmentation.class).getRemoteUcastMacs();
+                hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getRemoteUcastMacs();
         if (remoteUcastMacs == null || remoteUcastMacs.isEmpty()) {
             return;
         }
@@ -214,11 +225,11 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
     void printLocalUcastMacs(Node hwvtepNode, String elanName) {
         session.getConsole().println("LocalUCast macs :");
         session.getConsole().println(HEADINGUCAST);
-        if (hwvtepNode == null || hwvtepNode.getAugmentation(HwvtepGlobalAugmentation.class) == null) {
+        if (hwvtepNode == null || hwvtepNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
             return;
         }
         List<LocalUcastMacs> localUcastMacs =
-                hwvtepNode.getAugmentation(HwvtepGlobalAugmentation.class).getLocalUcastMacs();
+                hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getLocalUcastMacs();
         if (localUcastMacs == null || localUcastMacs.isEmpty()) {
             return;
         }
@@ -238,11 +249,11 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
     void printLocalMcastMacs(Node hwvtepNode, String elanName) {
         session.getConsole().println("LocalMcast macs :");
         session.getConsole().println(HEADINGMCAST);
-        if (hwvtepNode == null || hwvtepNode.getAugmentation(HwvtepGlobalAugmentation.class) == null) {
+        if (hwvtepNode == null || hwvtepNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
             return;
         }
         List<LocalMcastMacs> localMcastMacs =
-                hwvtepNode.getAugmentation(HwvtepGlobalAugmentation.class).getLocalMcastMacs();
+                hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getLocalMcastMacs();
         if (localMcastMacs == null || localMcastMacs.isEmpty()) {
             return;
         }
@@ -251,7 +262,7 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
             if (elanName.equals(lsFromLocalMac)) {
                 String mac = localMac.getMacEntryKey().getValue();
                 List<String> locatorsets = new ArrayList<>();
-                for (LocatorSet locatorSet : localMac.getLocatorSet()) {
+                for (LocatorSet locatorSet : localMac.nonnullLocatorSet()) {
                     locatorsets.add(getLocatorValue(locatorSet.getLocatorRef()));
                 }
                 session.getConsole().println(mac + GAP + locatorsets.toString());
@@ -265,11 +276,11 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
     void printRemoteMcastMacs(Node hwvtepNode, String elanName) {
         session.getConsole().println("RemoteMCast macs :");
         session.getConsole().println(HEADINGMCAST);
-        if (hwvtepNode == null || hwvtepNode.getAugmentation(HwvtepGlobalAugmentation.class) == null) {
+        if (hwvtepNode == null || hwvtepNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
             return;
         }
         List<RemoteMcastMacs> remoteMcastMacs =
-                hwvtepNode.getAugmentation(HwvtepGlobalAugmentation.class).getRemoteMcastMacs();
+                hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getRemoteMcastMacs();
         if (remoteMcastMacs == null || remoteMcastMacs.isEmpty()) {
             return;
         }
@@ -278,7 +289,7 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
             if (elanName.equals(lsFromremoteMac)) {
                 String mac = remoteMac.getMacEntryKey().getValue();
                 List<String> locatorsets = new ArrayList<>();
-                for (LocatorSet locatorSet : remoteMac.getLocatorSet()) {
+                for (LocatorSet locatorSet : remoteMac.nonnullLocatorSet()) {
                     locatorsets.add(getLocatorValue(locatorSet.getLocatorRef()));
                 }
                 session.getConsole().println(mac + GAP + locatorsets.toString());
@@ -301,7 +312,7 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
         }
         for (TerminationPoint terminationPoint : terminationPoints) {
             HwvtepPhysicalPortAugmentation aug =
-                    terminationPoint.getAugmentation(HwvtepPhysicalPortAugmentation.class);
+                    terminationPoint.augmentation(HwvtepPhysicalPortAugmentation.class);
             if (aug == null || aug.getVlanBindings() == null) {
                 continue;
             }
@@ -317,6 +328,7 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
 
     }
 
+    @Nullable
     String getLocatorValue(HwvtepPhysicalLocatorRef locatorRef) {
         if (locatorRef == null) {
             return null;
@@ -325,6 +337,7 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
                 .firstKeyOf(TerminationPoint.class).getTpId().getValue();
     }
 
+    @Nullable
     String getLogicalSwitchValue(HwvtepLogicalSwitchRef logicalSwitchRef) {
         if (logicalSwitchRef == null) {
             return null;
@@ -333,12 +346,14 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
                 .firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
     }
 
-    Node getPSnode(Node hwvtepNode, LogicalDatastoreType datastoreType) {
-        if (hwvtepNode.getAugmentation(HwvtepGlobalAugmentation.class) != null
-                && hwvtepNode.getAugmentation(HwvtepGlobalAugmentation.class).getSwitches() != null) {
-            for (Switches switches : hwvtepNode.getAugmentation(HwvtepGlobalAugmentation.class).getSwitches()) {
-                NodeId psNodeId = switches.getSwitchRef().getValue().firstKeyOf(Node.class).getNodeId();
-                return HwvtepUtils.getHwVtepNode(dataBroker, datastoreType, psNodeId);
+    @Nullable
+    Node getPSnode(Node hwvtepNode, LogicalDatastoreType datastoreType) throws ExecutionException,
+            InterruptedException {
+        if (hwvtepNode.augmentation(HwvtepGlobalAugmentation.class) != null) {
+            List<Switches> switches = hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getSwitches();
+            if (switches != null) {
+                return HwvtepUtils.getHwVtepNode(dataBroker, datastoreType,
+                    switches.iterator().next().getSwitchRef().getValue().firstKeyOf(Node.class).getNodeId());
             }
         }
         return null;