NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / internal / ExternalNetworkGroupInstaller.java
index aea5b53e40eb40c341de70935b7b4359d0c3d328..52878b14a25bd985033cefacab3fbd7e57ec99f0 100644 (file)
@@ -9,8 +9,6 @@
 package org.opendaylight.netvirt.natservice.internal;
 
 import com.google.common.base.Strings;
-
-import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -18,7 +16,7 @@ import java.util.Collections;
 import java.util.List;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
 import org.opendaylight.genius.mdsalutil.ActionInfo;
 import org.opendaylight.genius.mdsalutil.BucketInfo;
@@ -28,6 +26,7 @@ import org.opendaylight.genius.mdsalutil.actions.ActionDrop;
 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldEthernetDestination;
 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
+import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.netvirt.elanmanager.api.IElanService;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
@@ -35,7 +34,10 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.NetworkAttributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -85,13 +87,12 @@ public class ExternalNetworkGroupInstaller {
 
         Uuid networkId = subnetMap.getNetworkId();
         Uuid subnetId = subnetMap.getId();
-        if (networkId == null) {
-            LOG.error("installExtNetGroupEntries : No network associated subnet id {}", subnetId.getValue());
-            return;
+        NetworkAttributes.NetworkType extNetworkType = subnetMap.getNetworkType();
+        if (externalGroupInstallationRequiredForExtNetwork(networkId, subnetMap.isExternal(),
+            extNetworkType, subnetId)) {
+            String macAddress = NatUtil.getSubnetGwMac(broker, subnetId, networkId.getValue());
+            installExtNetGroupEntries(subnetMap, macAddress);
         }
-
-        String macAddress = NatUtil.getSubnetGwMac(broker, subnetId, networkId.getValue());
-        installExtNetGroupEntries(subnetMap, macAddress);
     }
 
     public void installExtNetGroupEntries(Uuid subnetId, String macAddress) {
@@ -106,13 +107,13 @@ public class ExternalNetworkGroupInstaller {
                     subnetMap.getId());
             return;
         }
-        installExtNetGroupEntries(subnetMap, macAddress);
+        if (externalGroupInstallationRequiredForExtNetwork(subnetMap.getNetworkId(), subnetMap.isExternal(),
+            subnetMap.getNetworkType(), subnetId)) {
+            installExtNetGroupEntries(subnetMap, macAddress);
+        }
     }
 
-    public void installExtNetGroupEntries(Uuid networkId, BigInteger dpnId) {
-        if (networkId == null) {
-            return;
-        }
+    public void installExtNetGroupEntries(Uuid networkId, Uint64 dpnId) {
 
         List<Uuid> subnetIds = NatUtil.getSubnetIdsFromNetworkId(broker, networkId);
         if (subnetIds.isEmpty()) {
@@ -142,44 +143,63 @@ public class ExternalNetworkGroupInstaller {
             return;
         }
 
-        long groupId = NatUtil.createGroupId(NatUtil.getGroupIdKey(subnetName), idManager);
-
-        LOG.info("installExtNetGroupEntries : Installing ext-net group {} entry for subnet {} with macAddress {} "
-                + "(extInterfaces: {})", groupId, subnetName, macAddress, Arrays.toString(extInterfaces.toArray()));
-        for (String extInterface : extInterfaces) {
-            BigInteger dpId = NatUtil.getDpnForInterface(odlInterfaceRpcService, extInterface);
-            if (BigInteger.ZERO.equals(dpId)) {
-                LOG.info("installExtNetGroupEntries: No DPN for interface {}. NAT ext-net flow will not be installed "
-                    + "for subnet {}", extInterface, subnetName);
-                return;
+        Uint32 groupId = NatUtil.getUniqueId(idManager, NatConstants.SNAT_IDPOOL_NAME,
+            NatUtil.getGroupIdKey(subnetName));
+        if (groupId != NatConstants.INVALID_ID) {
+            LOG.info("installExtNetGroupEntries : Installing ext-net group {} entry for subnet {} with macAddress {} "
+                    + "(extInterfaces: {})", groupId, subnetName, macAddress,
+                    Arrays.toString(extInterfaces.toArray()));
+            for (String extInterface : extInterfaces) {
+                Uint64 dpId = NatUtil.getDpnForInterface(odlInterfaceRpcService, extInterface);
+                if (Uint64.ZERO.equals(dpId)) {
+                    LOG.info(
+                        "installExtNetGroupEntries: No DPN for interface {}. NAT ext-net flow will not be installed "
+                            + "for subnet {}", extInterface, subnetName);
+                    return;
+                }
+                installExtNetGroupEntry(groupId, subnetName, extInterface, macAddress, dpId);
             }
-            installExtNetGroupEntry(groupId, subnetName, extInterface, macAddress, dpId);
+        } else {
+            LOG.error("installExtNetGroupEntries: Unable to get groupId for subnet:{}", subnetName);
         }
     }
 
-    public void installExtNetGroupEntry(Uuid networkId, Uuid subnetId, BigInteger dpnId, String macAddress) {
-        String subnetName = subnetId.getValue();
-        String extInterface = elanService.getExternalElanInterface(networkId.getValue(), dpnId);
-        if (extInterface == null) {
-            LOG.warn("installExtNetGroupEntry : No external ELAN interface attached to network {} subnet {} DPN id {}",
+    public void installExtNetGroupEntry(Uuid networkId, Uuid subnetId, Uint64 dpnId, String macAddress) {
+        Subnetmap subnetMap = NatUtil.getSubnetMap(broker, subnetId);
+        if (subnetMap == null) {
+            LOG.error("installExtNetGroupEntries : Subnetmap is null");
+            return;
+        }
+        if (externalGroupInstallationRequiredForExtNetwork(networkId, subnetMap.isExternal(),
+            subnetMap.getNetworkType(), subnetId)) {
+            String subnetName = subnetId.getValue();
+            String extInterface = elanService.getExternalElanInterface(networkId.getValue(), dpnId);
+            if (extInterface == null) {
+                LOG.warn(
+                    "installExtNetGroupEntry : No external ELAN interface attached to network {} subnet {} DPN id {}",
                     networkId, subnetName, dpnId);
-            //return;
+                //return;
+            }
+            Uint32 groupId = NatUtil.getUniqueId(idManager, NatConstants.SNAT_IDPOOL_NAME,
+                NatUtil.getGroupIdKey(subnetName));
+            if (groupId != NatConstants.INVALID_ID) {
+                LOG.info(
+                    "installExtNetGroupEntry : Installing ext-net group {} entry for subnet {} with macAddress {} "
+                        + "(extInterface: {})", groupId, subnetName, macAddress, extInterface);
+                installExtNetGroupEntry(groupId, subnetName, extInterface, macAddress, dpnId);
+            } else {
+                LOG.error("installExtNetGroupEntry: Unable to get groupId for subnet:{}",
+                    subnetName);
+            }
         }
-
-        long groupId = NatUtil.createGroupId(NatUtil.getGroupIdKey(subnetName), idManager);
-        LOG.info("installExtNetGroupEntry : Installing ext-net group {} entry for subnet {} with macAddress {} "
-                + "(extInterface: {})", groupId, subnetName, macAddress, extInterface);
-        installExtNetGroupEntry(groupId, subnetName, extInterface, macAddress, dpnId);
     }
 
-    private void installExtNetGroupEntry(long groupId, String subnetName, String extInterface,
-            String macAddress, BigInteger dpnId) {
+    private void installExtNetGroupEntry(Uint32 groupId, String subnetName, String extInterface,
+            String macAddress, Uint64 dpnId) {
 
-        coordinator.enqueueJob(NatConstants.NAT_DJC_PREFIX + subnetName + extInterface, () -> {
+        coordinator.enqueueJob(NatUtil.getDefaultFibRouteToSNATForSubnetJobKey(subnetName, dpnId), () -> {
             GroupEntity groupEntity = buildExtNetGroupEntity(macAddress, subnetName, groupId, extInterface, dpnId);
-            if (groupEntity != null) {
-                mdsalManager.syncInstallGroup(groupEntity);
-            }
+            mdsalManager.syncInstallGroup(groupEntity);
             return Collections.emptyList();
         });
     }
@@ -191,39 +211,47 @@ public class ExternalNetworkGroupInstaller {
 
         String subnetName = subnetMap.getId().getValue();
         Uuid networkId = subnetMap.getNetworkId();
-        if (networkId == null) {
-            LOG.error("removeExtNetGroupEntries : No external network associated subnet id {}", subnetName);
-            return;
-        }
-
-        Collection<String> extInterfaces = elanService.getExternalElanInterfaces(networkId.getValue());
-        if (extInterfaces == null || extInterfaces.isEmpty()) {
-            LOG.debug("removeExtNetGroupEntries : No external ELAN interfaces attached to network {} subnet {}",
+        NetworkAttributes.NetworkType extNetworkType = subnetMap.getNetworkType();
+        if (externalGroupInstallationRequiredForExtNetwork(networkId, subnetMap.isExternal(),
+            extNetworkType, subnetMap.getId())) {
+            Collection<String> extInterfaces = elanService.getExternalElanInterfaces(networkId.getValue());
+            if (extInterfaces == null || extInterfaces.isEmpty()) {
+                LOG.debug(
+                    "removeExtNetGroupEntries : No external ELAN interfaces attached to network {} subnet {}",
                     networkId, subnetName);
-            return;
-        }
-
-        long groupId = NatUtil.createGroupId(NatUtil.getGroupIdKey(subnetName), idManager);
+                return;
+            }
 
-        for (String extInterface : extInterfaces) {
-            GroupEntity groupEntity = buildEmptyExtNetGroupEntity(subnetName, groupId, extInterface);
-            if (groupEntity != null) {
-                LOG.info("removeExtNetGroupEntries : Remove ext-net Group: id {}, subnet id {}", groupId, subnetName);
-                natServiceCounters.removeExternalNetworkGroup();
-                mdsalManager.syncRemoveGroup(groupEntity);
+            Uint32 groupId = NatUtil.getUniqueId(idManager, NatConstants.SNAT_IDPOOL_NAME,
+                NatUtil.getGroupIdKey(subnetName));
+            if (groupId != NatConstants.INVALID_ID) {
+                for (String extInterface : extInterfaces) {
+                    GroupEntity groupEntity = buildEmptyExtNetGroupEntity(subnetName, groupId,
+                        extInterface);
+                    if (groupEntity != null) {
+                        LOG.info(
+                            "removeExtNetGroupEntries : Remove ext-net Group: id {}, subnet id {}",
+                            groupId, subnetName);
+                        natServiceCounters.removeExternalNetworkGroup();
+                        mdsalManager.syncRemoveGroup(groupEntity);
+                    }
+                }
+            } else {
+                LOG.error("removeExtNetGroupEntries: Unable to get groupId for subnet:{}",
+                    subnetName);
             }
         }
     }
 
     private GroupEntity buildExtNetGroupEntity(String macAddress, String subnetName,
-                                               long groupId, String extInterface, BigInteger dpnId) {
+                                               Uint32 groupId, String extInterface, Uint64 dpnId) {
 
         List<ActionInfo> actionList = new ArrayList<>();
         final int setFieldEthDestActionPos = 0;
         List<ActionInfo> egressActionList = new ArrayList<>();
         if (extInterface != null) {
             egressActionList = NatUtil.getEgressActionsForInterface(odlInterfaceRpcService, itmRpcService,
-                    interfaceManager, extInterface, null, setFieldEthDestActionPos + 1);
+                    interfaceManager, extInterface, null, setFieldEthDestActionPos + 1, false);
         }
         if (Strings.isNullOrEmpty(macAddress) || egressActionList.isEmpty()) {
             if (Strings.isNullOrEmpty(macAddress)) {
@@ -245,17 +273,39 @@ public class ExternalNetworkGroupInstaller {
 
         List<BucketInfo> listBucketInfo = new ArrayList<>();
         listBucketInfo.add(new BucketInfo(actionList));
-        return MDSALUtil.buildGroupEntity(dpnId, groupId, subnetName, GroupTypes.GroupAll, listBucketInfo);
+        return MDSALUtil.buildGroupEntity(dpnId, groupId.longValue(), subnetName, GroupTypes.GroupAll, listBucketInfo);
     }
 
-    private GroupEntity buildEmptyExtNetGroupEntity(String subnetName, long groupId, String extInterface) {
-        BigInteger dpId = NatUtil.getDpnForInterface(odlInterfaceRpcService, extInterface);
-        if (BigInteger.ZERO.equals(dpId)) {
+    @Nullable
+    private GroupEntity buildEmptyExtNetGroupEntity(String subnetName, Uint32 groupId, String extInterface) {
+        Uint64 dpId = NatUtil.getDpnForInterface(odlInterfaceRpcService, extInterface);
+        if (Uint64.ZERO.equals(dpId)) {
             LOG.error("buildEmptyExtNetGroupEntity: No DPN for interface {}. NAT ext-net flow will not be installed "
                     + "for subnet {}", extInterface, subnetName);
             return null;
         }
 
-        return MDSALUtil.buildGroupEntity(dpId, groupId, subnetName, GroupTypes.GroupAll, new ArrayList<>());
+        return MDSALUtil.buildGroupEntity(dpId, groupId.longValue(), subnetName,
+                GroupTypes.GroupAll, new ArrayList<>());
+    }
+
+    private boolean externalGroupInstallationRequiredForExtNetwork(Uuid networkId, boolean isExternal,
+        NetworkAttributes.NetworkType extNetworkType,
+        Uuid subnetId) {
+        if (networkId == null || !isExternal) {
+            LOG.debug("externalGroupInstallationRequiredForExtNetwork : network is null or not External Network:{} for"
+                + " subnet id {}", isExternal, subnetId.getValue());
+            return false;
+        }
+        // Installation of External Network Group is only required for flat/vlan use-cases.
+        if (extNetworkType == null
+            || extNetworkType == NetworkAttributes.NetworkType.GRE
+            || extNetworkType == NetworkAttributes.NetworkType.VXLAN) {
+            LOG.debug("Provider Type is either null or of type {} for network {}, subnet {} for which "
+                    + "ExternalNetwork Group installation not required",
+                extNetworkType, networkId.getValue(), subnetId.getValue());
+            return false;
+        }
+        return true;
     }
 }