Use Java declarations instead of Google Collections
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / utils / L2GatewayConnectionUtils.java
index 2bb7d07a66da27e6f104ae55ba26cc74baf59daf..ca5528721a6ec62bcc0ad96c2b491befe2ceebd3 100644 (file)
@@ -11,21 +11,22 @@ package org.opendaylight.netvirt.elan.l2gw.utils;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
-
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.netvirt.elan.l2gw.listeners.HwvtepLogicalSwitchListener;
-import org.opendaylight.netvirt.elan.l2gw.jobs.AssociateHwvtepToElanJob;
-import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
+import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
 import org.opendaylight.netvirt.elan.internal.ElanInstanceManager;
+import org.opendaylight.netvirt.elan.l2gw.jobs.AssociateHwvtepToElanJob;
 import org.opendaylight.netvirt.elan.l2gw.jobs.DisAssociateHwvtepFromElanJob;
+import org.opendaylight.netvirt.elan.l2gw.listeners.HwvtepLogicalSwitchListener;
+import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
+import org.opendaylight.netvirt.elan.utils.ElanUtils;
 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
 import org.opendaylight.netvirt.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
-import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
-import org.opendaylight.genius.mdsalutil.MDSALUtil;
-import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
@@ -34,67 +35,53 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev15071
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gatewayKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
 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.netvirt.elan.rev150602.elan.instances.ElanInstance;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Optional;
-
 public class L2GatewayConnectionUtils {
     private static final Logger LOG = LoggerFactory.getLogger(L2GatewayConnectionUtils.class);
 
-    private static DataBroker broker;
-    private static ElanInstanceManager elanInstanceManager;
-
-    static DataStoreJobCoordinator dataStoreJobCoordinator;
-
-    public static void setDataStoreJobCoordinator(DataStoreJobCoordinator ds) {
-        dataStoreJobCoordinator = ds;
-    }
-
-    public static void setBroker(DataBroker broker) {
-        L2GatewayConnectionUtils.broker = broker;
-    }
-
-    public static void setElanInstanceManager(ElanInstanceManager elanInstanceManager) {
-        L2GatewayConnectionUtils.elanInstanceManager = elanInstanceManager;
+    private final DataBroker broker;
+    private final ElanInstanceManager elanInstanceManager;
+    private final ElanL2GatewayUtils elanL2GatewayUtils;
+    private final EntityOwnershipService entityOwnershipService;
+    private final ElanUtils elanUtils;
+    private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
+
+    public L2GatewayConnectionUtils(DataBroker dataBroker, ElanInstanceManager elanInstanceManager,
+                                    EntityOwnershipService entityOwnershipService, ElanUtils elanUtils) {
+        this.broker = dataBroker;
+        this.elanInstanceManager = elanInstanceManager;
+        this.elanL2GatewayUtils = elanUtils.getElanL2GatewayUtils();
+        this.entityOwnershipService = entityOwnershipService;
+        this.elanUtils = elanUtils;
+        this.elanL2GatewayMulticastUtils = elanUtils.getElanL2GatewayMulticastUtils();
     }
 
     public static boolean isGatewayAssociatedToL2Device(L2GatewayDevice l2GwDevice) {
-        return (l2GwDevice.getL2GatewayIds().size() > 0);
+        return l2GwDevice.getL2GatewayIds().size() > 0;
     }
 
     public static L2gateway getNeutronL2gateway(DataBroker broker, Uuid l2GatewayId) {
         LOG.debug("getNeutronL2gateway for {}", l2GatewayId.getValue());
         InstanceIdentifier<L2gateway> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class)
                 .child(L2gateway.class, new L2gatewayKey(l2GatewayId));
-        Optional<L2gateway> l2Gateway = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
-        if (l2Gateway.isPresent()) {
-            return l2Gateway.get();
-        }
-        return null;
+        return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).orNull();
     }
 
     public static List<L2gateway> getL2gatewayList(DataBroker broker) {
         InstanceIdentifier<L2gateways> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class);
-        Optional<L2gateways> l2gateways = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
-
-        if (l2gateways.isPresent()) {
-            return l2gateways.get().getL2gateway();
-        }
-        return null;
+        return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).transform(
+                L2gateways::getL2gateway).orNull();
     }
 
     public static List<L2gatewayConnection> getAllL2gatewayConnections(DataBroker broker) {
         InstanceIdentifier<L2gatewayConnections> inst = InstanceIdentifier.create(Neutron.class)
                 .child(L2gatewayConnections.class);
-        Optional<L2gatewayConnections> l2GwConns = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
-        if (l2GwConns.isPresent()) {
-            return l2GwConns.get().getL2gatewayConnection();
-        }
-        return null;
+        return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).transform(
+                L2gatewayConnections::getL2gatewayConnection).orNull();
     }
 
     /**
@@ -110,7 +97,7 @@ public class L2GatewayConnectionUtils {
         List<L2gatewayConnection> l2GwConnections = null;
         List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
         if (allL2GwConns != null) {
-            l2GwConnections = new ArrayList<L2gatewayConnection>();
+            l2GwConnections = new ArrayList<>();
             for (Uuid l2GatewayId : l2GatewayIds) {
                 for (L2gatewayConnection l2GwConn : allL2GwConns) {
                     if (l2GwConn.getL2gatewayId().equals(l2GatewayId)) {
@@ -122,16 +109,38 @@ public class L2GatewayConnectionUtils {
         return l2GwConnections;
     }
 
-    public static void addL2GatewayConnection(L2gatewayConnection input) {
+    /**
+     * Gets the associated l2 gw connections.
+     *
+     * @param broker
+     *            the broker
+     * @param elanName
+     *            the elan Name
+     * @return the associated l2 gw connection with elan
+     */
+    public static List<L2gatewayConnection> getL2GwConnectionsByElanName(DataBroker broker, String elanName) {
+        List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
+        List<L2gatewayConnection> elanL2GateWayConnections = new ArrayList<>();
+        if (allL2GwConns != null) {
+            for (L2gatewayConnection l2GwConn : allL2GwConns) {
+                if (l2GwConn.getNetworkId().getValue().equalsIgnoreCase(elanName)) {
+                    elanL2GateWayConnections.add(l2GwConn);
+                }
+            }
+        }
+        return elanL2GateWayConnections;
+    }
+
+    public void addL2GatewayConnection(L2gatewayConnection input) {
         addL2GatewayConnection(input, null/*deviceName*/);
     }
 
-    public static void addL2GatewayConnection(L2gatewayConnection input, String l2GwDeviceName) {
+    public void addL2GatewayConnection(L2gatewayConnection input, String l2GwDeviceName) {
         LOG.info("Adding L2gateway Connection with ID: {}", input.getKey().getUuid());
 
         Uuid networkUuid = input.getNetworkId();
         ElanInstance elanInstance = elanInstanceManager.getElanInstanceByName(networkUuid.getValue());
-        if (elanInstance == null || elanInstance.getVni() == null) {
+        if (elanInstance == null || !ElanUtils.isVxlan(elanInstance)) {
             LOG.error("Neutron network with id {} is not present", networkUuid.getValue());
         } else {
             Uuid l2GatewayId = input.getL2gatewayId();
@@ -144,30 +153,26 @@ public class L2GatewayConnectionUtils {
         }
     }
 
-    public static void deleteL2GatewayConnection(L2gatewayConnection input) {
+    public void deleteL2GatewayConnection(L2gatewayConnection input) {
         LOG.info("Deleting L2gateway Connection with ID: {}", input.getKey().getUuid());
 
         Uuid networkUuid = input.getNetworkId();
-        ElanInstance elanInstance = elanInstanceManager.getElanInstanceByName(networkUuid.getValue());
-        if (elanInstance == null) {
-            LOG.error("Neutron network with id {} is not present", networkUuid.getValue());
-        } else {
-            Uuid l2GatewayId = input.getL2gatewayId();
-            L2gateway l2Gateway = L2GatewayConnectionUtils.getNeutronL2gateway(broker, l2GatewayId);
-            if (l2Gateway == null) {
-                LOG.error("L2Gateway with id {} is not present", l2GatewayId.getValue());
-            } else {
-                disAssociateHwvtepsFromElan(elanInstance, l2Gateway, input);
-            }
-        }
+        String elanName = networkUuid.getValue();
+        Uuid l2GatewayId = input.getL2gatewayId();
+        disAssociateHwvtepsFromElan(elanName, input);
     }
 
-    private static void disAssociateHwvtepsFromElan(ElanInstance elanInstance, L2gateway l2Gateway,
-            L2gatewayConnection input) {
-        String elanName = elanInstance.getElanInstanceName();
+    private void disAssociateHwvtepsFromElan(String elanName, L2gatewayConnection input) {
         Integer defaultVlan = input.getSegmentId();
-        List<Devices> l2Devices = l2Gateway.getDevices();
-        for (Devices l2Device : l2Devices) {
+        List<L2GatewayDevice> l2Devices = ElanL2GwCacheUtils.getAllElanDevicesFromCache();
+        List<Devices> l2gwDevicesToBeDeleted = new ArrayList<>();
+        for (L2GatewayDevice elanL2gwDevice : l2Devices) {
+            if (elanL2gwDevice.getL2GatewayIds().contains(input.getKey().getUuid())) {
+                l2gwDevicesToBeDeleted.addAll(elanL2gwDevice.getL2gwConnectionIdToDevices()
+                        .get(input.getKey().getUuid()));
+            }
+        }
+        for (Devices l2Device : l2gwDevicesToBeDeleted) {
             String l2DeviceName = l2Device.getDeviceName();
             L2GatewayDevice l2GatewayDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
             String hwvtepNodeId = l2GatewayDevice.getHwvtepNodeId();
@@ -185,24 +190,24 @@ public class L2GatewayConnectionUtils {
                 elanL2GwDevice.removeL2GatewayId(l2GwConnId);
             }
 
-            DisAssociateHwvtepFromElanJob disAssociateHwvtepToElanJob = new DisAssociateHwvtepFromElanJob(broker,
-                    elanL2GwDevice, elanInstance, l2Device, defaultVlan, isLastL2GwConnDeleted);
-            ElanClusterUtils.runOnlyInLeaderNode(disAssociateHwvtepToElanJob.getJobKey(), "remove l2gw connection job ",
-                    disAssociateHwvtepToElanJob);
+            DisAssociateHwvtepFromElanJob disAssociateHwvtepToElanJob =
+                    new DisAssociateHwvtepFromElanJob(broker, elanL2GatewayUtils, elanL2GatewayMulticastUtils,
+                            elanL2GwDevice, elanName,
+                            l2Device, defaultVlan, isLastL2GwConnDeleted);
+            ElanClusterUtils.runOnlyInLeaderNode(entityOwnershipService, disAssociateHwvtepToElanJob.getJobKey(),
+                    "remove l2gw connection job ", disAssociateHwvtepToElanJob);
         }
     }
 
-    private static void associateHwvtepsToElan(ElanInstance elanInstance,
+    private void associateHwvtepsToElan(ElanInstance elanInstance,
             L2gateway l2Gateway, L2gatewayConnection input, String l2GwDeviceName) {
         String elanName = elanInstance.getElanInstanceName();
         Integer defaultVlan = input.getSegmentId();
         Uuid l2GwConnId = input.getKey().getUuid();
         List<Devices> l2Devices = l2Gateway.getDevices();
 
-        if (LOG.isTraceEnabled()) {
-            LOG.trace("Associating ELAN {} with L2Gw Conn Id {} having below L2Gw devices {}", elanName, l2GwConnId,
-                    l2Devices);
-        }
+        LOG.trace("Associating ELAN {} with L2Gw Conn Id {} having below L2Gw devices {}", elanName, l2GwConnId,
+                l2Devices);
 
         for (Devices l2Device : l2Devices) {
             String l2DeviceName = l2Device.getDeviceName();
@@ -219,7 +224,7 @@ public class L2GatewayConnectionUtils {
                 NodeId hwvtepNodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
 
                 // Delete pending delete logical switch task if scheduled
-                ElanL2GatewayUtils.cancelDeleteLogicalSwitch(hwvtepNodeId,
+                elanL2GatewayUtils.cancelDeleteLogicalSwitch(hwvtepNodeId,
                         ElanL2GatewayUtils.getLogicalSwitchFromElan(elanName));
 
                 // Add L2 Gateway device to 'ElanL2GwDevice' cache
@@ -227,18 +232,20 @@ public class L2GatewayConnectionUtils {
                 LogicalSwitches logicalSwitch = HwvtepUtils.getLogicalSwitch(broker, LogicalDatastoreType.OPERATIONAL,
                         hwvtepNodeId, elanName);
                 if (logicalSwitch == null) {
-                    HwvtepLogicalSwitchListener hwVTEPLogicalSwitchListener = new HwvtepLogicalSwitchListener(
+                    HwvtepLogicalSwitchListener hwVTEPLogicalSwitchListener = new HwvtepLogicalSwitchListener(broker,
+                            elanL2GatewayUtils, entityOwnershipService, elanUtils, elanL2GatewayMulticastUtils,
                             l2GatewayDevice, elanName, l2Device, defaultVlan, l2GwConnId);
                     hwVTEPLogicalSwitchListener.registerListener(LogicalDatastoreType.OPERATIONAL, broker);
                     createLogicalSwitch = true;
                 } else {
-                    addL2DeviceToElanL2GwCache(elanName, l2GatewayDevice, l2GwConnId);
+                    addL2DeviceToElanL2GwCache(elanName, l2GatewayDevice, l2GwConnId, l2Device);
                     createLogicalSwitch = false;
                 }
                 AssociateHwvtepToElanJob associateHwvtepToElanJob = new AssociateHwvtepToElanJob(broker,
-                        l2GatewayDevice, elanInstance, l2Device, defaultVlan, createLogicalSwitch);
+                        elanL2GatewayUtils, elanUtils, elanL2GatewayMulticastUtils, l2GatewayDevice, elanInstance,
+                        l2Device, defaultVlan, createLogicalSwitch);
 
-                ElanClusterUtils.runOnlyInLeaderNode( associateHwvtepToElanJob.getJobKey() ,
+                ElanClusterUtils.runOnlyInLeaderNode(entityOwnershipService, associateHwvtepToElanJob.getJobKey() ,
                         "create logical switch in hwvtep topo",
                         associateHwvtepToElanJob);
 
@@ -249,7 +256,7 @@ public class L2GatewayConnectionUtils {
     }
 
     public static L2GatewayDevice addL2DeviceToElanL2GwCache(String elanName, L2GatewayDevice l2GatewayDevice,
-            Uuid l2GwConnId) {
+            Uuid l2GwConnId, Devices l2Device) {
         String l2gwDeviceNodeId = l2GatewayDevice.getHwvtepNodeId();
         L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, l2gwDeviceNodeId);
         if (elanL2GwDevice == null) {
@@ -264,18 +271,20 @@ public class L2GatewayConnectionUtils {
                     l2gwDeviceNodeId, l2GwConnId);
         }
         elanL2GwDevice.addL2GatewayId(l2GwConnId);
-
-        if (LOG.isTraceEnabled()) {
-            LOG.trace("Elan L2GwConn cache updated with below details: {}", elanL2GwDevice);
+        if (elanL2GwDevice.getL2gwConnectionIdToDevices().get(l2GwConnId) == null) {
+            elanL2GwDevice.getL2gwConnectionIdToDevices().put(l2GwConnId, new ArrayList<Devices>());
         }
+        elanL2GwDevice.getL2gwConnectionIdToDevices().get(l2GwConnId).add(l2Device);
+
+        LOG.trace("Elan L2GwConn cache updated with below details: {}", elanL2GwDevice);
         return elanL2GwDevice;
     }
 
     private static boolean isL2GwDeviceConnected(L2GatewayDevice l2GwDevice) {
-        return (l2GwDevice != null && l2GwDevice.getHwvtepNodeId() != null && l2GwDevice.isConnected());
+        return l2GwDevice != null && l2GwDevice.getHwvtepNodeId() != null && l2GwDevice.isConnected();
     }
 
     protected static boolean isLastL2GwConnBeingDeleted(L2GatewayDevice l2GwDevice) {
-        return (l2GwDevice.getL2GatewayIds().size() == 1);
+        return l2GwDevice.getL2GatewayIds().size() == 1;
     }
 }