Bulk merge of l2gw changes
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / cli / l2gw / NetworkL2gwDeviceInfoCli.java
index a1368692d9ec0c84d31b5e2c11069475136d5d82..1d01daf745aba19b09a4bc49af768c3bb732a326 100644 (file)
@@ -8,21 +8,27 @@
 
 package org.opendaylight.netvirt.elan.cli.l2gw;
 
-import com.google.common.base.Optional;
+import static org.opendaylight.mdsal.binding.util.Datastore.OPERATIONAL;
+
 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.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
 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.binding.util.ManagedNewTransactionRunner;
+import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunnerImpl;
+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;
@@ -31,10 +37,14 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hw
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacsKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacsKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSet;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
@@ -45,6 +55,7 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
 /**
@@ -75,7 +86,7 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
 
     private static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
         return InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
-                new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)).child(Node.class, new NodeKey(nodeId));
+            new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)).child(Node.class, new NodeKey(nodeId));
     }
 
     Map<NodeId, Node> opNodes = new HashMap<>();
@@ -90,70 +101,87 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
     }
 
     @Override
+    @Nullable
+    @SuppressWarnings("illegalcatch")
     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());
-            }
-        }
-        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());
+        ManagedNewTransactionRunner txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
+
+        try {
+            txRunner.callWithNewReadOnlyTransactionAndClose(OPERATIONAL, operTx -> {
+                List<Node> nodes = new ArrayList<>();
+                Set<String> networks = new HashSet<>();
+                if (nodeId == null) {
+                    Optional<Topology> topologyOptional = operTx.read(createHwvtepTopologyInstanceIdentifier()).get();
+                    if (topologyOptional.isPresent()) {
+                        nodes = new ArrayList<>(topologyOptional.get().getNode().values());
+                    }
+                } else {
+                    Optional<Node> nodeOptional = operTx
+                        .read(createInstanceIdentifier(new NodeId(new Uri(nodeId)))).get();
+                    if (nodeOptional.isPresent()) {
+                        nodes.add(nodeOptional.get());
                     }
                 }
-            }
-        } 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 (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 = new ArrayList<>(elanInstancesOptional.get()
+                            .getElanInstance().values());
+                        if (elans != null) {
+                            for (ElanInstance elan : elans) {
+                                networks.add(elan.getElanInstanceName());
+                            }
+                        }
+                    }
+                } else {
+                    networks.add(elanName);
+                }
 
-            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 (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);
+                        }
+                    }
                 }
-                session.getConsole().println("Printing for node " + node.getNodeId().getValue());
-                process(node.getNodeId(), network);
-            }
+                if (!networks.isEmpty()) {
+                    for (String network : networks) {
+                        session.getConsole().println("Network info for " + network);
+                        if (nodes != null) {
+                            for (Node node : nodes) {
+                                if (node.getNodeId().getValue().contains("physicalswitch")) {
+                                    continue;
+                                }
+                                session.getConsole().println("Printing for node " + node.getNodeId().getValue());
+                                process(node.getNodeId(), network);
+                            }
+                        }
+                    }
+                }
+            });
+        } catch (Exception e) {
+            session.getConsole().println("Failed with error " + e.getMessage());
         }
         return null;
     }
@@ -193,12 +221,12 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
         if (hwvtepNode == null || hwvtepNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
             return;
         }
-        List<RemoteUcastMacs> remoteUcastMacs =
-                hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getRemoteUcastMacs();
+        Map<RemoteUcastMacsKey, RemoteUcastMacs> remoteUcastMacs =
+            hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).nonnullRemoteUcastMacs();
         if (remoteUcastMacs == null || remoteUcastMacs.isEmpty()) {
             return;
         }
-        for (RemoteUcastMacs remoteMac : remoteUcastMacs) {
+        for (RemoteUcastMacs remoteMac : remoteUcastMacs.values()) {
             String lsFromRemoteMac = getLogicalSwitchValue(remoteMac.getLogicalSwitchRef());
             if (elanName.equals(lsFromRemoteMac)) {
                 String mac = remoteMac.getMacEntryKey().getValue();
@@ -217,12 +245,12 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
         if (hwvtepNode == null || hwvtepNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
             return;
         }
-        List<LocalUcastMacs> localUcastMacs =
-                hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getLocalUcastMacs();
+        Map<LocalUcastMacsKey, LocalUcastMacs> localUcastMacs =
+            hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).nonnullLocalUcastMacs();
         if (localUcastMacs == null || localUcastMacs.isEmpty()) {
             return;
         }
-        for (LocalUcastMacs localMac : localUcastMacs) {
+        for (LocalUcastMacs localMac : localUcastMacs.values()) {
             String lsFromLocalMac = getLogicalSwitchValue(localMac.getLogicalSwitchRef());
             if (elanName.equals(lsFromLocalMac)) {
                 String mac = localMac.getMacEntryKey().getValue();
@@ -241,17 +269,17 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
         if (hwvtepNode == null || hwvtepNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
             return;
         }
-        List<LocalMcastMacs> localMcastMacs =
-                hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getLocalMcastMacs();
+        Map<LocalMcastMacsKey, LocalMcastMacs> localMcastMacs =
+            hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).nonnullLocalMcastMacs();
         if (localMcastMacs == null || localMcastMacs.isEmpty()) {
             return;
         }
-        for (LocalMcastMacs localMac : localMcastMacs) {
+        for (LocalMcastMacs localMac : localMcastMacs.values()) {
             String lsFromLocalMac = getLogicalSwitchValue(localMac.getLogicalSwitchRef());
             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());
@@ -268,17 +296,17 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
         if (hwvtepNode == null || hwvtepNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
             return;
         }
-        List<RemoteMcastMacs> remoteMcastMacs =
-                hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getRemoteMcastMacs();
+        Map<RemoteMcastMacsKey, RemoteMcastMacs> remoteMcastMacs =
+            hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).nonnullRemoteMcastMacs();
         if (remoteMcastMacs == null || remoteMcastMacs.isEmpty()) {
             return;
         }
-        for (RemoteMcastMacs remoteMac : remoteMcastMacs) {
+        for (RemoteMcastMacs remoteMac : remoteMcastMacs.values()) {
             String lsFromremoteMac = getLogicalSwitchValue(remoteMac.getLogicalSwitchRef());
             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());
@@ -295,17 +323,17 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
         if (psNode == null) {
             return;
         }
-        List<TerminationPoint> terminationPoints = psNode.getTerminationPoint();
+        Map<TerminationPointKey, TerminationPoint> terminationPoints = psNode.nonnullTerminationPoint();
         if (terminationPoints == null || terminationPoints.isEmpty()) {
             return;
         }
-        for (TerminationPoint terminationPoint : terminationPoints) {
+        for (TerminationPoint terminationPoint : terminationPoints.values()) {
             HwvtepPhysicalPortAugmentation aug =
-                    terminationPoint.augmentation(HwvtepPhysicalPortAugmentation.class);
+                terminationPoint.augmentation(HwvtepPhysicalPortAugmentation.class);
             if (aug == null || aug.getVlanBindings() == null) {
                 continue;
             }
-            for (VlanBindings vlanBindings : aug.getVlanBindings()) {
+            for (VlanBindings vlanBindings : aug.nonnullVlanBindings().values()) {
                 String lsFromremoteMac = getLogicalSwitchValue(vlanBindings.getLogicalSwitchRef());
                 if (elanName.equals(lsFromremoteMac)) {
                     session.getConsole().println(terminationPoint.getTpId().getValue()
@@ -313,30 +341,33 @@ public class NetworkL2gwDeviceInfoCli extends OsgiCommandSupport {
                 }
             }
         }
-
-
     }
 
+    @Nullable
     String getLocatorValue(HwvtepPhysicalLocatorRef locatorRef) {
         if (locatorRef == null) {
             return null;
         }
         return locatorRef.getValue()
-                .firstKeyOf(TerminationPoint.class).getTpId().getValue();
+            .firstKeyOf(TerminationPoint.class).getTpId().getValue();
     }
 
+    @Nullable
     String getLogicalSwitchValue(HwvtepLogicalSwitchRef logicalSwitchRef) {
         if (logicalSwitchRef == null) {
             return null;
         }
         return logicalSwitchRef.getValue()
-                .firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
+            .firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
     }
 
-    Node getPSnode(Node hwvtepNode, LogicalDatastoreType datastoreType) {
+    @Nullable
+    Node getPSnode(Node hwvtepNode, LogicalDatastoreType datastoreType)
+        throws ExecutionException, InterruptedException {
         if (hwvtepNode.augmentation(HwvtepGlobalAugmentation.class) != null
-                && hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getSwitches() != null) {
-            for (Switches switches : hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getSwitches()) {
+            && hwvtepNode.augmentation(HwvtepGlobalAugmentation.class).getSwitches() != null) {
+            for (Switches switches : hwvtepNode.augmentation(HwvtepGlobalAugmentation.class)
+                .nonnullSwitches().values()) {
                 NodeId psNodeId = switches.getSwitchRef().getValue().firstKeyOf(Node.class).getNodeId();
                 return HwvtepUtils.getHwVtepNode(dataBroker, datastoreType, psNodeId);
             }