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;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.eclipse.jdt.annotation.Nullable;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
import org.opendaylight.genius.mdsalutil.ActionInfo;
import org.opendaylight.genius.mdsalutil.BucketInfo;
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;
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;
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) {
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()) {
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(NatUtil.getDefaultFibRouteToSNATForSubnetJobKey(subnetName, dpnId), () -> {
GroupEntity groupEntity = buildExtNetGroupEntity(macAddress, subnetName, groupId, extInterface, dpnId);
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<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);
}
@Nullable
- private GroupEntity buildEmptyExtNetGroupEntity(String subnetName, long groupId, String extInterface) {
- BigInteger dpId = NatUtil.getDpnForInterface(odlInterfaceRpcService, extInterface);
- if (BigInteger.ZERO.equals(dpId)) {
+ 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;
}
}