Optional clean-up
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / utils / ElanUtils.java
index 2d713564226e3574970c4ec77f6296e75a94472c..18b3876f20281d39549d8aac35be242224492828 100755 (executable)
@@ -340,12 +340,7 @@ public class ElanUtils {
             return elanObj;
         }
         InstanceIdentifier<ElanInstance> elanIdentifierId = getElanInstanceConfigurationDataPath(elanInstanceName);
-        Optional<ElanInstance> elanInstance = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION,
-                elanIdentifierId);
-        if (elanInstance.isPresent()) {
-            return elanInstance.get();
-        }
-        return null;
+        return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, elanIdentifierId).orNull();
     }
 
     public static InstanceIdentifier<ElanInstance> getElanInstanceConfigurationDataPath(String elanInstanceName) {
@@ -360,12 +355,7 @@ public class ElanUtils {
             return elanInterfaceObj;
         }
         InstanceIdentifier<ElanInterface> elanInterfaceId = getElanInterfaceConfigurationDataPathId(elanInterfaceName);
-        Optional<ElanInterface> existingElanInterface = MDSALUtil.read(broker,
-                LogicalDatastoreType.CONFIGURATION, elanInterfaceId);
-        if (existingElanInterface.isPresent()) {
-            return existingElanInterface.get();
-        }
-        return null;
+        return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, elanInterfaceId).orNull();
     }
 
     public static EtreeInterface getEtreeInterfaceByElanInterfaceName(DataBroker broker, String elanInterfaceName) {
@@ -381,12 +371,7 @@ public class ElanUtils {
     // elan-state Operational container
     public static Elan getElanByName(DataBroker broker, String elanInstanceName) {
         InstanceIdentifier<Elan> elanIdentifier = getElanInstanceOperationalDataPath(elanInstanceName);
-        Optional<Elan> elanInstance = MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL,
-                elanIdentifier);
-        if (elanInstance.isPresent()) {
-            return elanInstance.get();
-        }
-        return null;
+        return MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, elanIdentifier).orNull();
     }
 
     public static InstanceIdentifier<Elan> getElanInstanceOperationalDataPath(String elanInstanceName) {
@@ -397,21 +382,13 @@ public class ElanUtils {
     public MacEntry getInterfaceMacEntriesOperationalDataPath(String interfaceName, PhysAddress physAddress) {
         InstanceIdentifier<MacEntry> existingMacEntryId = getInterfaceMacEntriesIdentifierOperationalDataPath(
                 interfaceName, physAddress);
-        Optional<MacEntry> existingInterfaceMacEntry = read(broker,
-                LogicalDatastoreType.OPERATIONAL, existingMacEntryId);
-        if (existingInterfaceMacEntry.isPresent()) {
-            return existingInterfaceMacEntry.get();
-        }
-        return null;
+        return read(broker, LogicalDatastoreType.OPERATIONAL, existingMacEntryId).orNull();
     }
 
     public MacEntry getInterfaceMacEntriesOperationalDataPathFromId(InstanceIdentifier identifier) {
         Optional<MacEntry> existingInterfaceMacEntry = read(broker,
                 LogicalDatastoreType.OPERATIONAL, identifier);
-        if (existingInterfaceMacEntry.isPresent()) {
-            return existingInterfaceMacEntry.get();
-        }
-        return null;
+        return existingInterfaceMacEntry.orNull();
     }
 
     public static InstanceIdentifier<MacEntry> getInterfaceMacEntriesIdentifierOperationalDataPath(String interfaceName,
@@ -423,23 +400,15 @@ public class ElanUtils {
     }
 
     // elan-forwarding-tables Operational container
-    public MacEntry getMacTableByElanName(String elanName, PhysAddress physAddress) {
+    public Optional<MacEntry> getMacEntryForElanInstance(String elanName, PhysAddress physAddress) {
         InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanName, physAddress);
-        Optional<MacEntry> existingElanMacEntry = read(broker,
-                LogicalDatastoreType.OPERATIONAL, macId);
-        if (existingElanMacEntry.isPresent()) {
-            return existingElanMacEntry.get();
-        }
-        return null;
+        return read(broker, LogicalDatastoreType.OPERATIONAL, macId);
     }
 
     public MacEntry getMacEntryFromElanMacId(InstanceIdentifier identifier) {
         Optional<MacEntry> existingInterfaceMacEntry = read(broker,
                 LogicalDatastoreType.OPERATIONAL, identifier);
-        if (existingInterfaceMacEntry.isPresent()) {
-            return existingInterfaceMacEntry.get();
-        }
-        return null;
+        return existingInterfaceMacEntry.orNull();
     }
 
     public static InstanceIdentifier<MacEntry> getMacEntryOperationalDataPath(String elanName,
@@ -457,12 +426,7 @@ public class ElanUtils {
     public ElanInterfaceMac getElanInterfaceMacByInterfaceName(String interfaceName) {
         InstanceIdentifier<ElanInterfaceMac> elanInterfaceId = getElanInterfaceMacEntriesOperationalDataPath(
                 interfaceName);
-        Optional<ElanInterfaceMac> existingElanInterface = read(broker,
-                LogicalDatastoreType.OPERATIONAL, elanInterfaceId);
-        if (existingElanInterface.isPresent()) {
-            return existingElanInterface.get();
-        }
-        return null;
+        return read(broker, LogicalDatastoreType.OPERATIONAL, elanInterfaceId).orNull();
     }
 
     /**
@@ -503,12 +467,7 @@ public class ElanUtils {
     public DpnInterfaces getElanInterfaceInfoByElanDpn(String elanInstanceName, BigInteger dpId) {
         InstanceIdentifier<DpnInterfaces> elanDpnInterfacesId = getElanDpnInterfaceOperationalDataPath(elanInstanceName,
                 dpId);
-        Optional<DpnInterfaces> elanDpnInterfaces = read(broker,
-                LogicalDatastoreType.OPERATIONAL, elanDpnInterfacesId);
-        if (elanDpnInterfaces.isPresent()) {
-            return elanDpnInterfaces.get();
-        }
-        return null;
+        return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfacesId).orNull();
     }
 
     /**
@@ -534,10 +493,7 @@ public class ElanUtils {
         InstanceIdentifier<ElanTagName> elanId = getElanInfoEntriesOperationalDataPath(elanTag);
         Optional<ElanTagName> existingElanInfo = read(broker,
                 LogicalDatastoreType.OPERATIONAL, elanId);
-        if (existingElanInfo.isPresent()) {
-            return existingElanInfo.get();
-        }
-        return null;
+        return existingElanInfo.orNull();
     }
 
     public EtreeLeafTagName getEtreeLeafTagByElanTag(long elanTag) {
@@ -546,8 +502,7 @@ public class ElanUtils {
                 LogicalDatastoreType.OPERATIONAL, elanId);
         if (existingElanInfo.isPresent()) {
             ElanTagName elanTagName = existingElanInfo.get();
-            EtreeLeafTagName etreeAugmentation = elanTagName.getAugmentation(EtreeLeafTagName.class);
-            return etreeAugmentation;
+            return elanTagName.getAugmentation(EtreeLeafTagName.class);
         }
         return null;
     }
@@ -575,23 +530,13 @@ public class ElanUtils {
 
     public ElanDpnInterfacesList getElanDpnInterfacesList(String elanName) {
         InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanName);
-        Optional<ElanDpnInterfacesList> existingElanDpnInterfaces = read(broker,
-                LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId);
-        if (existingElanDpnInterfaces.isPresent()) {
-            return existingElanDpnInterfaces.get();
-        }
-        return null;
+        return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId).orNull();
     }
 
     public ElanDpnInterfaces getElanDpnInterfacesList() {
         InstanceIdentifier<ElanDpnInterfaces> elanDpnInterfaceId = InstanceIdentifier.builder(ElanDpnInterfaces.class)
                 .build();
-        Optional<ElanDpnInterfaces> existingElanDpnInterfaces = read(broker,
-                LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId);
-        if (existingElanDpnInterfaces.isPresent()) {
-            return existingElanDpnInterfaces.get();
-        }
-        return null;
+        return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId).orNull();
     }
 
     /**
@@ -630,32 +575,25 @@ public class ElanUtils {
      * @return true if dpId is already present, otherwise return false
      */
     public boolean isDpnAlreadyPresentInElanInstance(BigInteger dpId, String elanInstanceName) {
-        boolean isDpIdPresent = false;
         InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanInstanceName);
         Optional<ElanDpnInterfacesList> existingElanDpnInterfaces = read(broker,
                 LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId);
         if (!existingElanDpnInterfaces.isPresent()) {
-            return isDpIdPresent;
+            return false;
         }
         List<DpnInterfaces> dpnInterfaces = existingElanDpnInterfaces.get().getDpnInterfaces();
         for (DpnInterfaces dpnInterface : dpnInterfaces) {
             if (dpnInterface.getDpId().equals(dpId)) {
-                isDpIdPresent = true;
-                break;
+                return true;
             }
         }
-        return isDpIdPresent;
+        return false;
     }
 
     public ElanForwardingTables getElanForwardingList() {
         InstanceIdentifier<ElanForwardingTables> elanForwardingTableId = InstanceIdentifier
                 .builder(ElanForwardingTables.class).build();
-        Optional<ElanForwardingTables> existingElanForwardingList = read(broker,
-                LogicalDatastoreType.OPERATIONAL, elanForwardingTableId);
-        if (existingElanForwardingList.isPresent()) {
-            return existingElanForwardingList.get();
-        }
-        return null;
+        return read(broker, LogicalDatastoreType.OPERATIONAL, elanForwardingTableId).orNull();
     }
 
     public static long getElanRemoteBroadCastGroupID(long elanTag) {
@@ -671,12 +609,7 @@ public class ElanUtils {
      */
     public MacTable getElanMacTable(String elanName) {
         InstanceIdentifier<MacTable> elanMacTableId = getElanMacTableOperationalDataPath(elanName);
-        Optional<MacTable> existingElanMacTable = read(broker,
-                LogicalDatastoreType.OPERATIONAL, elanMacTableId);
-        if (existingElanMacTable.isPresent()) {
-            return existingElanMacTable.get();
-        }
-        return null;
+        return read(broker, LogicalDatastoreType.OPERATIONAL, elanMacTableId).orNull();
     }
 
     public static long getElanLocalBCGId(long elanTag) {
@@ -729,40 +662,20 @@ public class ElanUtils {
      *            the flow group tx
      * @throws ElanException in case of issues creating the flow objects
      */
-    public void setupMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, long macTimeout,
-            String macAddress, boolean configureRemoteFlows, WriteTransaction writeFlowGroupTx) throws ElanException {
-        synchronized (macAddress) {
-            LOG.debug("Acquired lock for mac : " + macAddress + ". Proceeding with install operation.");
+    public void setupMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
+                              long macTimeout, String macAddress, boolean configureRemoteFlows,
+                              WriteTransaction writeFlowGroupTx) throws ElanException {
+        synchronized (getElanMacKey(elanInfo.getElanTag(), macAddress)) {
             setupKnownSmacFlow(elanInfo, interfaceInfo, macTimeout, macAddress, mdsalManager,
-                    writeFlowGroupTx);
+                writeFlowGroupTx);
             setupOrigDmacFlows(elanInfo, interfaceInfo, macAddress, configureRemoteFlows, mdsalManager,
-                    broker, writeFlowGroupTx);
+                broker, writeFlowGroupTx);
         }
     }
 
-    /**
-     * Setting INTERNAL_TUNNEL_TABLE, SMAC, DMAC, UDMAC in this DPN and on other DPNs.
-     *
-     * @param elanInfo
-     *            the elan info
-     * @param interfaceInfo
-     *            the interface info
-     * @param macTimeout
-     *            the mac timeout
-     * @param macAddress
-     *            the mac address
-     * @param writeFlowGroupTx
-     *            the flow group tx
-     * @throws ElanException in case of issues creating the flow objects
-     */
-    public void setupMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, long macTimeout,
-                              String macAddress, WriteTransaction writeFlowGroupTx) throws ElanException {
-        setupMacFlows(elanInfo, interfaceInfo, macTimeout, macAddress, true, writeFlowGroupTx);
-    }
-
     public void setupDMacFlowonRemoteDpn(ElanInstance elanInfo, InterfaceInfo interfaceInfo, BigInteger dstDpId,
             String macAddress, WriteTransaction writeFlowTx) throws ElanException {
-        synchronized (macAddress) {
+        synchronized (getElanMacKey(elanInfo.getElanTag(), macAddress)) {
             LOG.debug("Acquired lock for mac : " + macAddress + ". Proceeding with install operation.");
             setupOrigDmacFlowsonRemoteDpn(elanInfo, interfaceInfo, dstDpId, macAddress, writeFlowTx);
         }
@@ -1196,8 +1109,7 @@ public class ElanUtils {
             return;
         }
         String macAddress = macEntry.getMacAddress().getValue();
-        synchronized (macAddress) {
-            LOG.debug("Acquired lock for mac : " + macAddress + ". Proceeding with remove operation.");
+        synchronized (getElanMacKey(elanInfo.getElanTag(), macAddress)) {
             deleteMacFlows(elanInfo, interfaceInfo, macAddress, /* alsoDeleteSMAC */ true, deleteFlowGroupTx);
         }
     }
@@ -1359,8 +1271,7 @@ public class ElanUtils {
 
         InstanceIdentifier<Node> node = InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(nodeId))
                 .build();
-        Optional<Node> nodePresent = read(broker, LogicalDatastoreType.OPERATIONAL, node);
-        return nodePresent.isPresent();
+        return read(broker, LogicalDatastoreType.OPERATIONAL, node).isPresent();
     }
 
     public static ServicesInfo getServiceInfo(String elanInstanceName, long elanTag, String interfaceName) {
@@ -1587,12 +1498,7 @@ public class ElanUtils {
     public static TunnelList buildInternalTunnel(DataBroker broker) {
         InstanceIdentifier<TunnelList> tunnelListInstanceIdentifier = InstanceIdentifier.builder(TunnelList.class)
                 .build();
-        Optional<TunnelList> tunnelList = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION,
-                tunnelListInstanceIdentifier);
-        if (tunnelList.isPresent()) {
-            return tunnelList.get();
-        }
-        return null;
+        return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, tunnelListInstanceIdentifier).orNull();
     }
 
     /**
@@ -1608,15 +1514,10 @@ public class ElanUtils {
      */
     public ExternalTunnel getExternalTunnel(String sourceDevice, String destinationDevice,
             LogicalDatastoreType datastoreType) {
-        ExternalTunnel externalTunnel = null;
         Class<? extends TunnelTypeBase> tunType = TunnelTypeVxlan.class;
         InstanceIdentifier<ExternalTunnel> iid = InstanceIdentifier.builder(ExternalTunnelList.class)
                 .child(ExternalTunnel.class, new ExternalTunnelKey(destinationDevice, sourceDevice, tunType)).build();
-        Optional<ExternalTunnel> tunnelList = read(broker, datastoreType, iid);
-        if (tunnelList.isPresent()) {
-            externalTunnel = tunnelList.get();
-        }
-        return externalTunnel;
+        return read(broker, datastoreType, iid).orNull();
     }
 
     /**
@@ -1648,16 +1549,9 @@ public class ElanUtils {
      * @return the all external tunnels
      */
     public List<ExternalTunnel> getAllExternalTunnels(LogicalDatastoreType datastoreType) {
-        List<ExternalTunnel> result = null;
         InstanceIdentifier<ExternalTunnelList> iid = InstanceIdentifier.builder(ExternalTunnelList.class).build();
-        Optional<ExternalTunnelList> tunnelList = read(broker, datastoreType, iid);
-        if (tunnelList.isPresent()) {
-            result = tunnelList.get().getExternalTunnel();
-        }
-        if (result == null) {
-            result = Collections.emptyList();
-        }
-        return result;
+        return read(broker, datastoreType, iid).transform(ExternalTunnelList::getExternalTunnel).or(
+                Collections.emptyList());
     }
 
     /**
@@ -1716,7 +1610,7 @@ public class ElanUtils {
             String extDeviceNodeId, Long elanTag, Long vni, String macAddress, String displayName,
             String interfaceName) throws ElanException {
         List<ListenableFuture<Void>> futures = new ArrayList<>();
-        synchronized (macAddress) {
+        synchronized (getElanMacKey(elanTag, macAddress)) {
             Flow flow = buildDmacFlowForExternalRemoteMac(dpnId, extDeviceNodeId, elanTag, vni, macAddress,
                     displayName);
             futures.add(mdsalManager.installFlow(dpnId, flow));
@@ -1959,7 +1853,7 @@ public class ElanUtils {
     public List<ListenableFuture<Void>> deleteDmacFlowsToExternalMac(long elanTag, BigInteger dpId,
             String extDeviceNodeId, String macToRemove) {
         List<ListenableFuture<Void>> futures = new ArrayList<>();
-        synchronized (macToRemove) {
+        synchronized (getElanMacKey(elanTag, macToRemove)) {
             // Removing the flows that sends the packet on an external tunnel
             removeFlowThatSendsThePacketOnAnExternalTunnel(elanTag, dpId, extDeviceNodeId, macToRemove, futures);
 
@@ -2054,13 +1948,7 @@ public class ElanUtils {
         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
             .ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId = createInterfaceStateInstanceIdentifier(
                 interfaceName);
-        Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
-            .ietf.interfaces.rev140508.interfaces.state.Interface> ifStateOptional = MDSALUtil
-                .read(dataBroker, LogicalDatastoreType.OPERATIONAL, ifStateId);
-        if (ifStateOptional.isPresent()) {
-            return ifStateOptional.get();
-        }
-        return null;
+        return MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, ifStateId).orNull();
     }
 
     /**
@@ -2188,7 +2076,7 @@ public class ElanUtils {
         InstanceIdentifier<MacEntry> elanMacEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName, physAddress);
         tx.put(LogicalDatastoreType.OPERATIONAL, elanMacEntryId, macEntry);
         ElanInstance elanInstance = ElanUtils.getElanInstanceByName(broker, elanName);
-        setupMacFlows(elanInstance, interfaceManager.getInterfaceInfo(interfaceName), macTimeOut, macAddress,
+        setupMacFlows(elanInstance, interfaceManager.getInterfaceInfo(interfaceName), macTimeOut, macAddress, true,
                 flowWritetx);
     }
 
@@ -2228,4 +2116,8 @@ public class ElanUtils {
         LOG.trace("Elan {} does not have any external interace attached to DPN {}", elanInstanceName, dpnId);
         return null;
     }
+
+    public static String getElanMacKey(long elanTag, String macAddress) {
+        return ("MAC-" + macAddress + " ELAN_TAG-" + elanTag).intern();
+    }
 }