Merge "elanmanager CLI console commands impl should never catch Exception (II)"
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanServiceProvider.java
index fa8b5fe4da333d61e62c76c6e31b169ca86c2b98..3f922c2ca01539a26485d6f7edaf120a0855e9ee 100644 (file)
@@ -20,11 +20,15 @@ import java.util.Set;
 import java.util.concurrent.Future;
 import java.util.function.BiFunction;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.genius.interfacemanager.exceptions.InterfaceAlreadyExistsException;
 import org.opendaylight.genius.interfacemanager.globals.IfmConstants;
 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.opendaylight.genius.utils.clustering.EntityOwnerUtils;
+import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
 import org.opendaylight.netvirt.elan.statusanddiag.ElanStatusMonitor;
 import org.opendaylight.netvirt.elan.utils.ElanConstants;
 import org.opendaylight.netvirt.elan.utils.ElanUtils;
@@ -79,7 +83,7 @@ public class ElanServiceProvider implements IElanService {
                                ElanInstanceManager elanInstanceManager, ElanBridgeManager bridgeMgr,
                                DataBroker dataBroker,
                                ElanInterfaceManager elanInterfaceManager,
-                               ElanStatusMonitor elanStatusMonitor, ElanUtils elanUtils) {
+                               ElanStatusMonitor elanStatusMonitor, ElanUtils elanUtils, EntityOwnershipService entityOwnershipService) {
         this.idManager = idManager;
         this.interfaceManager = interfaceManager;
         this.elanInstanceManager = elanInstanceManager;
@@ -87,12 +91,14 @@ public class ElanServiceProvider implements IElanService {
         this.broker = dataBroker;
         this.elanStatusMonitor = elanStatusMonitor;
         this.elanUtils = elanUtils;
-        elanInstanceManager.setElanUtils(elanUtils);
         elanInterfaceManager.setElanUtils(elanUtils);
-    }
-
-    public static ElanUtils getElanutils() {
-        return ElanServiceProvider.elanUtils;
+        try {
+            EntityOwnerUtils.registerEntityCandidateForOwnerShip(entityOwnershipService,
+                    HwvtepSouthboundConstants.ELAN_ENTITY_TYPE, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
+                    null/*listener*/);
+        } catch (CandidateAlreadyRegisteredException e) {
+            LOG.error("failed to register the entity");
+        }
     }
 
     public void init() {
@@ -180,6 +186,11 @@ public class ElanServiceProvider implements IElanService {
         return isSuccess;
     }
 
+    @Override
+    public EtreeInterface getEtreeInterfaceByElanInterfaceName(String elanInterface) {
+        return ElanUtils.getEtreeInterfaceByElanInterfaceName(broker, elanInterface);
+    }
+
     public static boolean compareWithExistingElanInstance(ElanInstance existingElanInstance, long macTimeOut,
             String description) {
         boolean isEqual = false;
@@ -264,7 +275,7 @@ public class ElanServiceProvider implements IElanService {
     @Override
     public void updateElanInterface(String elanInstanceName, String interfaceName,
             List<String> updatedStaticMacAddresses, String newDescription) {
-        ElanInterface existingElanInterface = elanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
+        ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
         if (existingElanInterface == null) {
             return;
         }
@@ -289,7 +300,7 @@ public class ElanServiceProvider implements IElanService {
 
     @Override
     public void deleteElanInterface(String elanInstanceName, String interfaceName) {
-        ElanInterface existingElanInterface = elanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
+        ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
         if (existingElanInterface != null) {
             ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
                     ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName));
@@ -299,7 +310,7 @@ public class ElanServiceProvider implements IElanService {
 
     @Override
     public void addStaticMacAddress(String elanInstanceName, String interfaceName, String macAddress) {
-        ElanInterface existingElanInterface = elanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
+        ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
         PhysAddress updateStaticMacAddress = new PhysAddress(macAddress);
         if (existingElanInterface != null) {
             List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
@@ -319,7 +330,7 @@ public class ElanServiceProvider implements IElanService {
     @Override
     public void deleteStaticMacAddress(String elanInstanceName, String interfaceName, String macAddress)
             throws MacNotFoundException {
-        ElanInterface existingElanInterface = elanUtils.getElanInterfaceByElanInterfaceName(interfaceName);
+        ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
         PhysAddress physAddress = new PhysAddress(macAddress);
         if (existingElanInterface == null) {
             return;
@@ -340,7 +351,7 @@ public class ElanServiceProvider implements IElanService {
 
     @Override
     public Collection<MacEntry> getElanMacTable(String elanInstanceName) {
-        Elan elanInfo = elanUtils.getElanByName(elanInstanceName);
+        Elan elanInfo = ElanUtils.getElanByName(broker, elanInstanceName);
         List<MacEntry> macAddress = new ArrayList<>();
         if (elanInfo == null) {
             return macAddress;
@@ -360,7 +371,7 @@ public class ElanServiceProvider implements IElanService {
 
     @Override
     public void flushMACTable(String elanInstanceName) {
-        Elan elanInfo = elanUtils.getElanByName(elanInstanceName);
+        Elan elanInfo = ElanUtils.getElanByName(broker, elanInstanceName);
         if (elanInfo == null) {
             return;
         }
@@ -377,7 +388,6 @@ public class ElanServiceProvider implements IElanService {
                         deleteStaticMacAddress(elanInstanceName, elanInterface, macEntry.getMacAddress().getValue());
                     } catch (MacNotFoundException e) {
                         LOG.error("Mac Not Found Exception {}", e);
-                        e.printStackTrace();
                     }
                 }
             }
@@ -408,7 +418,7 @@ public class ElanServiceProvider implements IElanService {
 
     @Override
     public ElanInstance getElanInstance(String elanName) {
-        return elanUtils.getElanInstanceByName(elanName);
+        return ElanUtils.getElanInstanceByName(broker, elanName);
     }
 
     @Override
@@ -467,6 +477,16 @@ public class ElanServiceProvider implements IElanService {
         });
     }
 
+    private void createExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
+        if (interfaceName == null) {
+            LOG.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
+            return;
+        }
+
+        String elanInterfaceName = createIetfInterfaces(elanInstance, interfaceName);
+        addElanInterface(elanInstance.getElanInstanceName(), elanInterfaceName, null, null);
+    }
+
     @Override
     public void deleteExternalElanNetworks(Node node) {
         handleExternalElanNetworks(node, (elanInstance, interfaceName) -> {
@@ -483,6 +503,21 @@ public class ElanServiceProvider implements IElanService {
         });
     }
 
+    private void deleteExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
+        if (interfaceName == null) {
+            LOG.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
+            return;
+        }
+
+        String elanInstanceName = elanInstance.getElanInstanceName();
+        for (String elanInterface : getExternalElanInterfaces(elanInstanceName)) {
+            if (elanInterface.startsWith(interfaceName)) {
+                deleteIetfInterface(elanInterface);
+                deleteElanInterface(elanInstanceName, elanInterface);
+            }
+        }
+    }
+
     @Override
     public void updateExternalElanNetworks(Node origNode, Node updatedNode) {
         if (!bridgeMgr.isIntegrationBridge(updatedNode)) {
@@ -517,31 +552,6 @@ public class ElanServiceProvider implements IElanService {
         }
     }
 
-    private void createExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
-        if (interfaceName == null) {
-            LOG.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
-            return;
-        }
-
-        String elanInterfaceName = createIetfInterfaces(elanInstance, interfaceName);
-        addElanInterface(elanInstance.getElanInstanceName(), elanInterfaceName, null, null);
-    }
-
-    private void deleteExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
-        if (interfaceName == null) {
-            LOG.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
-            return;
-        }
-
-        String elanInstanceName = elanInstance.getElanInstanceName();
-        for (String elanInterface : getExternalElanInterfaces(elanInstanceName)) {
-            if (elanInterface.startsWith(interfaceName)) {
-                deleteIetfInterface(elanInterface);
-                deleteElanInterface(elanInstanceName, elanInterface);
-            }
-        }
-    }
-
     @Override
     public String getExternalElanInterface(String elanInstanceName, BigInteger dpnId) {
         DpnInterfaces dpnInterfaces = elanUtils.getElanInterfaceInfoByElanDpn(elanInstanceName, dpnId);
@@ -583,6 +593,11 @@ public class ElanServiceProvider implements IElanService {
         return elanUtils.isExternal(interfaceName);
     }
 
+    @Override
+    public ElanInterface getElanInterfaceByElanInterfaceName(String interfaceName) {
+        return ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
+    }
+
     /**
      * Create ietf-interfaces based on the ELAN segment type.<br>
      * For segment type flat - create transparent interface pointing to the
@@ -611,9 +626,12 @@ public class ElanServiceProvider implements IElanService {
                 Long segmentationId = elanInstance.getSegmentationId();
                 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + segmentationId;
                 String trunkName = parentRef + IfmConstants.OF_URI_SEPARATOR + "trunk";
-                interfaceManager.createVLANInterface(trunkName, parentRef, null, null, null,
-                        IfL2vlan.L2vlanMode.Trunk, true);
-
+                // trunk interface may have been created by other vlan network
+                Interface trunkInterface = ElanUtils.getInterfaceFromConfigDS(trunkName, broker);
+                if (trunkInterface == null) {
+                    interfaceManager.createVLANInterface(trunkName, parentRef, null, null, null,
+                            IfL2vlan.L2vlanMode.Trunk, true);
+                }
                 interfaceManager.createVLANInterface(interfaceName, trunkName, null, segmentationId.intValue(), null,
                         IfL2vlan.L2vlanMode.TrunkMember, true);
             }