use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Route in project netvirt by opendaylight.
the class ExternalRoutersListener method update.
@Override
protected void update(InstanceIdentifier<Routers> identifier, Routers original, Routers update) {
String routerName = original.getRouterName();
Long routerId = NatUtil.getVpnId(dataBroker, routerName);
if (routerId == NatConstants.INVALID_ID) {
LOG.error("update : external router event - Invalid routerId for routerName {}", routerName);
return;
}
// Check if its update on SNAT flag
boolean originalSNATEnabled = original.isEnableSnat();
boolean updatedSNATEnabled = update.isEnableSnat();
LOG.debug("update :called with originalFlag and updatedFlag for SNAT enabled " + "as {} and {}", originalSNATEnabled, updatedSNATEnabled);
if (natMode == NatMode.Conntrack && !upgradeState.isUpgradeInProgress()) {
if (originalSNATEnabled != updatedSNATEnabled) {
BigInteger primarySwitchId;
if (originalSNATEnabled) {
// SNAT disabled for the router
centralizedSwitchScheduler.releaseCentralizedSwitch(update);
} else {
centralizedSwitchScheduler.scheduleCentralizedSwitch(update);
}
} else if (updatedSNATEnabled) {
centralizedSwitchScheduler.updateCentralizedSwitch(original, update);
}
List<ExternalIps> originalExternalIps = original.getExternalIps();
List<ExternalIps> updateExternalIps = update.getExternalIps();
if (!Objects.equals(originalExternalIps, updateExternalIps)) {
if (originalExternalIps == null || originalExternalIps.isEmpty()) {
centralizedSwitchScheduler.scheduleCentralizedSwitch(update);
}
}
} else {
/* Get Primary Napt Switch for existing router from "router-to-napt-switch" DS.
* if dpnId value is null or zero then go for electing new Napt switch for existing router.
*/
long bgpVpnId = NatConstants.INVALID_ID;
Uuid bgpVpnUuid = NatUtil.getVpnForRouter(dataBroker, routerName);
if (bgpVpnUuid != null) {
bgpVpnId = NatUtil.getVpnId(dataBroker, bgpVpnUuid.getValue());
}
BigInteger dpnId = getPrimaryNaptSwitch(routerName);
if (dpnId == null || dpnId.equals(BigInteger.ZERO)) {
// Router has no interface attached
return;
}
final long finalBgpVpnId = bgpVpnId;
coordinator.enqueueJob(NatConstants.NAT_DJC_PREFIX + update.getKey(), () -> {
WriteTransaction writeFlowInvTx = dataBroker.newWriteOnlyTransaction();
WriteTransaction removeFlowInvTx = dataBroker.newWriteOnlyTransaction();
Uuid networkId = original.getNetworkId();
if (originalSNATEnabled != updatedSNATEnabled) {
if (originalSNATEnabled) {
// SNAT disabled for the router
Uuid networkUuid = original.getNetworkId();
LOG.info("update : SNAT disabled for Router {}", routerName);
Collection<String> externalIps = NatUtil.getExternalIpsForRouter(dataBroker, routerId);
handleDisableSnat(original, networkUuid, externalIps, false, null, dpnId, routerId, removeFlowInvTx);
} else {
LOG.info("update : SNAT enabled for Router {}", original.getRouterName());
handleEnableSnat(original, routerId, dpnId, finalBgpVpnId, removeFlowInvTx);
}
}
if (!Objects.equals(original.getExtGwMacAddress(), update.getExtGwMacAddress())) {
NatUtil.installRouterGwFlows(dataBroker, vpnManager, original, dpnId, NwConstants.DEL_FLOW);
NatUtil.installRouterGwFlows(dataBroker, vpnManager, update, dpnId, NwConstants.ADD_FLOW);
}
// Check if the Update is on External IPs
LOG.debug("update : Checking if this is update on External IPs");
List<String> originalExternalIps = NatUtil.getIpsListFromExternalIps(original.getExternalIps());
List<String> updatedExternalIps = NatUtil.getIpsListFromExternalIps(update.getExternalIps());
// Check if the External IPs are added during the update.
Set<String> addedExternalIps = new HashSet<>(updatedExternalIps);
addedExternalIps.removeAll(originalExternalIps);
if (addedExternalIps.size() != 0) {
LOG.debug("update : Start processing of the External IPs addition during the update operation");
vpnManager.addArpResponderFlowsToExternalNetworkIps(routerName, addedExternalIps, update.getExtGwMacAddress(), dpnId, update.getNetworkId(), null);
for (String addedExternalIp : addedExternalIps) {
/*
1) Do nothing in the IntExtIp model.
2) Initialise the count of the added external IP to 0 in the ExternalCounter model.
*/
String[] externalIpParts = NatUtil.getExternalIpAndPrefix(addedExternalIp);
String externalIp = externalIpParts[0];
String externalIpPrefix = externalIpParts[1];
String externalpStr = externalIp + "/" + externalIpPrefix;
LOG.debug("update : Initialise the count mapping of the external IP {} for the " + "router ID {} in the ExternalIpsCounter model.", externalpStr, routerId);
naptManager.initialiseNewExternalIpCounter(routerId, externalpStr);
}
LOG.debug("update : End processing of the External IPs addition during the update operation");
}
// Check if the External IPs are removed during the update.
Set<String> removedExternalIps = new HashSet<>(originalExternalIps);
removedExternalIps.removeAll(updatedExternalIps);
if (removedExternalIps.size() > 0) {
LOG.debug("update : Start processing of the External IPs removal during the update operation");
vpnManager.removeArpResponderFlowsToExternalNetworkIps(routerName, removedExternalIps, original.getExtGwMacAddress(), dpnId, networkId);
for (String removedExternalIp : removedExternalIps) {
/*
1) Remove the mappings in the IntExt IP model which has external IP.
2) Remove the external IP in the ExternalCounter model.
3) For the corresponding subnet IDs whose external IP mapping was removed, allocate one of the
least loaded external IP.
Store the subnet IP and the reallocated external IP mapping in the IntExtIp model.
4) Increase the count of the allocated external IP by one.
5) Advertise to the BGP if external IP is allocated for the first time for the router
i.e. the route for the external IP is absent.
6) Remove the NAPT translation entries from Inbound and Outbound NAPT tables for
the removed external IPs and also from the model.
7) Advertise to the BGP for removing the route for the removed external IPs.
*/
String[] externalIpParts = NatUtil.getExternalIpAndPrefix(removedExternalIp);
String externalIp = externalIpParts[0];
String externalIpPrefix = externalIpParts[1];
String externalIpAddrStr = externalIp + "/" + externalIpPrefix;
LOG.debug("update : Clear the routes from the BGP and remove the FIB and TS " + "entries for removed external IP {}", externalIpAddrStr);
Uuid vpnUuId = NatUtil.getVpnIdfromNetworkId(dataBroker, networkId);
String vpnName = "";
if (vpnUuId != null) {
vpnName = vpnUuId.getValue();
}
clrRtsFromBgpAndDelFibTs(dpnId, routerId, externalIpAddrStr, vpnName, networkId, update.getExtGwMacAddress(), removeFlowInvTx);
LOG.debug("update : Remove the mappings in the IntExtIP model which has external IP.");
// Get the internal IPs which are associated to the removed external IPs
List<IpMap> ipMaps = naptManager.getIpMapList(dataBroker, routerId);
List<String> removedInternalIps = new ArrayList<>();
for (IpMap ipMap : ipMaps) {
if (ipMap.getExternalIp().equals(externalIpAddrStr)) {
removedInternalIps.add(ipMap.getInternalIp());
}
}
LOG.debug("update : Remove the mappings of the internal IPs from the IntExtIP model.");
for (String removedInternalIp : removedInternalIps) {
LOG.debug("update : Remove the IP mapping of the internal IP {} for the " + "router ID {} from the IntExtIP model", removedInternalIp, routerId);
naptManager.removeFromIpMapDS(routerId, removedInternalIp);
}
LOG.debug("update : Remove the count mapping of the external IP {} for the " + "router ID {} from the ExternalIpsCounter model.", externalIpAddrStr, routerId);
naptManager.removeExternalIpCounter(routerId, externalIpAddrStr);
LOG.debug("update : Allocate the least loaded external IPs to the subnets " + "whose external IPs were removed.");
for (String removedInternalIp : removedInternalIps) {
allocateExternalIp(dpnId, update, routerId, routerName, networkId, removedInternalIp, writeFlowInvTx);
}
LOG.debug("update : Remove the NAPT translation entries from " + "Inbound and Outbound NAPT tables for the removed external IPs.");
// Get the internalIP and internal Port which were associated to the removed external IP.
List<Integer> externalPorts = new ArrayList<>();
Map<ProtocolTypes, List<String>> protoTypesIntIpPortsMap = new HashMap<>();
InstanceIdentifier<IpPortMapping> ipPortMappingId = InstanceIdentifier.builder(IntextIpPortMap.class).child(IpPortMapping.class, new IpPortMappingKey(routerId)).build();
Optional<IpPortMapping> ipPortMapping = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, ipPortMappingId);
if (ipPortMapping.isPresent()) {
List<IntextIpProtocolType> intextIpProtocolTypes = ipPortMapping.get().getIntextIpProtocolType();
for (IntextIpProtocolType intextIpProtocolType : intextIpProtocolTypes) {
ProtocolTypes protoType = intextIpProtocolType.getProtocol();
List<IpPortMap> ipPortMaps = intextIpProtocolType.getIpPortMap();
for (IpPortMap ipPortMap : ipPortMaps) {
IpPortExternal ipPortExternal = ipPortMap.getIpPortExternal();
if (ipPortExternal.getIpAddress().equals(externalIp)) {
externalPorts.add(ipPortExternal.getPortNum());
List<String> removedInternalIpPorts = protoTypesIntIpPortsMap.get(protoType);
if (removedInternalIpPorts != null) {
removedInternalIpPorts.add(ipPortMap.getIpPortInternal());
protoTypesIntIpPortsMap.put(protoType, removedInternalIpPorts);
} else {
removedInternalIpPorts = new ArrayList<>();
removedInternalIpPorts.add(ipPortMap.getIpPortInternal());
protoTypesIntIpPortsMap.put(protoType, removedInternalIpPorts);
}
}
}
}
}
// Remove the IP port map from the intext-ip-port-map model, which were containing
// the removed external IP.
Set<Map.Entry<ProtocolTypes, List<String>>> protoTypesIntIpPorts = protoTypesIntIpPortsMap.entrySet();
Map<String, List<String>> internalIpPortMap = new HashMap<>();
for (Map.Entry protoTypesIntIpPort : protoTypesIntIpPorts) {
ProtocolTypes protocolType = (ProtocolTypes) protoTypesIntIpPort.getKey();
List<String> removedInternalIpPorts = (List<String>) protoTypesIntIpPort.getValue();
for (String removedInternalIpPort : removedInternalIpPorts) {
// Remove the IP port map from the intext-ip-port-map model,
// which were containing the removed external IP
naptManager.removeFromIpPortMapDS(routerId, removedInternalIpPort, protocolType);
// Remove the IP port incomint packer map.
naptPacketInHandler.removeIncomingPacketMap(routerId + NatConstants.COLON_SEPARATOR + removedInternalIpPort);
String[] removedInternalIpPortParts = removedInternalIpPort.split(NatConstants.COLON_SEPARATOR);
if (removedInternalIpPortParts.length == 2) {
String removedInternalIp = removedInternalIpPortParts[0];
String removedInternalPort = removedInternalIpPortParts[1];
List<String> removedInternalPortsList = internalIpPortMap.get(removedInternalPort);
if (removedInternalPortsList != null) {
removedInternalPortsList.add(removedInternalPort);
internalIpPortMap.put(removedInternalIp, removedInternalPortsList);
} else {
removedInternalPortsList = new ArrayList<>();
removedInternalPortsList.add(removedInternalPort);
internalIpPortMap.put(removedInternalIp, removedInternalPortsList);
}
}
}
}
// Delete the entry from SnatIntIpPortMap DS
Set<String> internalIps = internalIpPortMap.keySet();
for (String internalIp : internalIps) {
LOG.debug("update : Removing IpPort having the internal IP {} from the " + "model SnatIntIpPortMap", internalIp);
naptManager.removeFromSnatIpPortDS(routerId, internalIp);
}
naptManager.removeNaptPortPool(externalIp);
LOG.debug("update : Remove the NAPT translation entries from Inbound NAPT tables for the " + "removed external IP {}", externalIp);
for (Integer externalPort : externalPorts) {
// Remove the NAPT translation entries from Inbound NAPT table
naptEventHandler.removeNatFlows(dpnId, NwConstants.INBOUND_NAPT_TABLE, routerId, externalIp, externalPort);
}
Set<Map.Entry<String, List<String>>> internalIpPorts = internalIpPortMap.entrySet();
for (Map.Entry<String, List<String>> internalIpPort : internalIpPorts) {
String internalIp = internalIpPort.getKey();
LOG.debug("update : Remove the NAPT translation entries from Outbound NAPT tables for " + "the removed internal IP {}", internalIp);
List<String> internalPorts = internalIpPort.getValue();
for (String internalPort : internalPorts) {
// Remove the NAPT translation entries from Outbound NAPT table
naptPacketInHandler.removeIncomingPacketMap(routerId + NatConstants.COLON_SEPARATOR + internalIp + NatConstants.COLON_SEPARATOR + internalPort);
naptEventHandler.removeNatFlows(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId, internalIp, Integer.parseInt(internalPort));
}
}
}
LOG.debug("update : End processing of the External IPs removal during the update operation");
}
// Check if its Update on subnets
LOG.debug("update : Checking if this is update on subnets");
List<Uuid> originalSubnetIds = original.getSubnetIds();
List<Uuid> updatedSubnetIds = update.getSubnetIds();
Set<Uuid> addedSubnetIds = new HashSet<>(updatedSubnetIds);
addedSubnetIds.removeAll(originalSubnetIds);
// Check if the Subnet IDs are added during the update.
if (addedSubnetIds.size() != 0) {
LOG.debug("update : Start processing of the Subnet IDs addition during the update operation");
for (Uuid addedSubnetId : addedSubnetIds) {
/*
1) Select the least loaded external IP for the subnet and store the mapping of the
subnet IP and the external IP in the IntExtIp model.
2) Increase the count of the selected external IP by one.
3) Advertise to the BGP if external IP is allocated for the first time for the
router i.e. the route for the external IP is absent.
*/
String subnetIp = NatUtil.getSubnetIp(dataBroker, addedSubnetId);
if (subnetIp != null) {
allocateExternalIp(dpnId, update, routerId, routerName, networkId, subnetIp, writeFlowInvTx);
}
}
LOG.debug("update : End processing of the Subnet IDs addition during the update operation");
}
// Check if the Subnet IDs are removed during the update.
Set<Uuid> removedSubnetIds = new HashSet<>(originalSubnetIds);
removedSubnetIds.removeAll(updatedSubnetIds);
List<ListenableFuture<Void>> futures = new ArrayList<>();
if (removedSubnetIds.size() != 0) {
LOG.debug("update : Start processing of the Subnet IDs removal during the update operation");
for (Uuid removedSubnetId : removedSubnetIds) {
String[] subnetAddr = NatUtil.getSubnetIpAndPrefix(dataBroker, removedSubnetId);
if (subnetAddr != null) {
/*
1) Remove the subnet IP and the external IP in the IntExtIp map
2) Decrease the count of the coresponding external IP by one.
3) Advertise to the BGP for removing the routes of the corresponding external
IP if its not allocated to any other internal IP.
*/
String externalIp = naptManager.getExternalIpAllocatedForSubnet(routerId, subnetAddr[0] + "/" + subnetAddr[1]);
if (externalIp == null) {
LOG.error("update : No mapping found for router ID {} and internal IP {}", routerId, subnetAddr[0]);
futures.add(NatUtil.waitForTransactionToComplete(writeFlowInvTx));
futures.add(NatUtil.waitForTransactionToComplete(removeFlowInvTx));
return futures;
}
naptManager.updateCounter(routerId, externalIp, false);
// used by any other internal ip in any router
if (!isExternalIpAllocated(externalIp)) {
LOG.debug("update : external ip is not allocated to any other " + "internal IP so proceeding to remove routes");
clrRtsFromBgpAndDelFibTs(dpnId, routerId, networkId, Collections.singleton(externalIp), null, update.getExtGwMacAddress(), removeFlowInvTx);
LOG.debug("update : Successfully removed fib entries in switch {} for " + "router {} with networkId {} and externalIp {}", dpnId, routerId, networkId, externalIp);
}
LOG.debug("update : Remove the IP mapping for the router ID {} and " + "internal IP {} external IP {}", routerId, subnetAddr[0], externalIp);
naptManager.removeIntExtIpMapDS(routerId, subnetAddr[0] + "/" + subnetAddr[1]);
}
}
LOG.debug("update : End processing of the Subnet IDs removal during the update operation");
}
futures.add(NatUtil.waitForTransactionToComplete(writeFlowInvTx));
futures.add(NatUtil.waitForTransactionToComplete(removeFlowInvTx));
return futures;
}, NatConstants.NAT_DJC_MAX_RETRIES);
}
// end of controller based SNAT
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Route in project netvirt by opendaylight.
the class ExternalRoutersListener method advToBgpAndInstallFibAndTsFlows.
public void advToBgpAndInstallFibAndTsFlows(final BigInteger dpnId, final short tableId, final String vpnName, final long routerId, final String routerName, final String externalIp, final Uuid extNetworkId, final Routers router, final WriteTransaction writeFlowInvTx) {
LOG.debug("advToBgpAndInstallFibAndTsFlows : entry for DPN ID {}, tableId {}, vpnname {} " + "and externalIp {}", dpnId, tableId, vpnName, externalIp);
String nextHopIp = NatUtil.getEndpointIpAddressForDPN(dataBroker, dpnId);
String rd = NatUtil.getVpnRd(dataBroker, vpnName);
if (rd == null || rd.isEmpty()) {
LOG.error("advToBgpAndInstallFibAndTsFlows : Unable to get RD for VPN Name {}", vpnName);
return;
}
ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName, extNetworkId);
if (extNwProvType == null) {
LOG.error("advToBgpAndInstallFibAndTsFlows : External Network Provider Type missing");
return;
}
if (extNwProvType == ProviderTypes.VXLAN) {
WriteTransaction writeTx = dataBroker.newWriteOnlyTransaction();
evpnSnatFlowProgrammer.evpnAdvToBgpAndInstallFibAndTsFlows(dpnId, tableId, externalIp, vpnName, rd, nextHopIp, writeTx, routerId, routerName, writeFlowInvTx);
return;
}
// Generate VPN label for the external IP
GenerateVpnLabelInput labelInput = new GenerateVpnLabelInputBuilder().setVpnName(vpnName).setIpPrefix(externalIp).build();
Future<RpcResult<GenerateVpnLabelOutput>> labelFuture = vpnService.generateVpnLabel(labelInput);
// On successful generation of the VPN label, advertise the route to the BGP and install the FIB routes.
ListenableFuture<RpcResult<Void>> future = Futures.transformAsync(JdkFutureAdapters.listenInPoolThread(labelFuture), (AsyncFunction<RpcResult<GenerateVpnLabelOutput>, RpcResult<Void>>) result -> {
if (result.isSuccessful()) {
LOG.debug("advToBgpAndInstallFibAndTsFlows : inside apply with result success");
GenerateVpnLabelOutput output = result.getResult();
final long label = output.getLabel();
int externalIpInDsFlag = 0;
List<IpMap> dbIpMaps = NaptManager.getIpMapList(dataBroker, routerId);
if (dbIpMaps != null) {
for (IpMap dbIpMap : dbIpMaps) {
String dbExternalIp = dbIpMap.getExternalIp();
if (dbExternalIp.contains(externalIp)) {
String dbInternalIp = dbIpMap.getInternalIp();
IpMapKey dbIpMapKey = dbIpMap.getKey();
LOG.debug("advToBgpAndInstallFibAndTsFlows : Setting label {} for internalIp {} " + "and externalIp {}", label, dbInternalIp, externalIp);
IpMap newIpm = new IpMapBuilder().setKey(dbIpMapKey).setInternalIp(dbInternalIp).setExternalIp(dbExternalIp).setLabel(label).build();
MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.OPERATIONAL, naptManager.getIpMapIdentifier(routerId, dbInternalIp), newIpm);
externalIpInDsFlag++;
}
}
if (externalIpInDsFlag <= 0) {
LOG.debug("advToBgpAndInstallFibAndTsFlows : External Ip {} not found in DS, " + "Failed to update label {} for routerId {} in DS", externalIp, label, routerId);
String errMsg = String.format("Failed to update label %s due to external Ip %s not" + " found in DS for router %s", label, externalIp, routerId);
return Futures.immediateFailedFuture(new Exception(errMsg));
}
} else {
LOG.error("advToBgpAndInstallFibAndTsFlows : Failed to write label {} for externalIp {} for" + " routerId {} in DS", label, externalIp, routerId);
}
long l3vni = 0;
if (NatUtil.isOpenStackVniSemanticsEnforcedForGreAndVxlan(elanManager, extNwProvType)) {
l3vni = NatOverVxlanUtil.getInternetVpnVni(idManager, vpnName, l3vni).longValue();
}
Routers extRouter = router != null ? router : NatUtil.getRoutersFromConfigDS(dataBroker, routerName);
Uuid externalSubnetId = NatUtil.getExternalSubnetForRouterExternalIp(externalIp, extRouter);
NatUtil.addPrefixToBGP(dataBroker, bgpManager, fibManager, vpnName, rd, externalSubnetId, externalIp, nextHopIp, extRouter.getNetworkId().getValue(), null, label, l3vni, RouteOrigin.STATIC, dpnId);
List<Instruction> tunnelTableCustomInstructions = new ArrayList<>();
tunnelTableCustomInstructions.add(new InstructionGotoTable(tableId).buildInstruction(0));
makeTunnelTableEntry(dpnId, label, l3vni, tunnelTableCustomInstructions, writeFlowInvTx, extNwProvType);
makeLFibTableEntry(dpnId, label, tableId, writeFlowInvTx);
List<Instruction> fibTableCustomInstructions = createFibTableCustomInstructions(tableId, routerName, externalIp);
if (NatUtil.isOpenStackVniSemanticsEnforcedForGreAndVxlan(elanManager, extNwProvType)) {
NatUtil.makePreDnatToSnatTableEntry(mdsalManager, dpnId, NwConstants.INBOUND_NAPT_TABLE, writeFlowInvTx);
}
String fibExternalIp = NatUtil.validateAndAddNetworkMask(externalIp);
Optional<Subnets> externalSubnet = NatUtil.getOptionalExternalSubnets(dataBroker, externalSubnetId);
String externalVpn = vpnName;
if (externalSubnet.isPresent()) {
externalVpn = externalSubnetId.getValue();
}
CreateFibEntryInput input = new CreateFibEntryInputBuilder().setVpnName(externalVpn).setSourceDpid(dpnId).setIpAddress(fibExternalIp).setServiceId(label).setIpAddressSource(CreateFibEntryInput.IpAddressSource.ExternalFixedIP).setInstruction(fibTableCustomInstructions).build();
Future<RpcResult<Void>> future1 = fibService.createFibEntry(input);
return JdkFutureAdapters.listenInPoolThread(future1);
} else {
LOG.error("advToBgpAndInstallFibAndTsFlows : inside apply with result failed");
String errMsg = String.format("Could not retrieve the label for prefix %s in VPN %s, %s", externalIp, vpnName, result.getErrors());
return Futures.immediateFailedFuture(new RuntimeException(errMsg));
}
}, MoreExecutors.directExecutor());
Futures.addCallback(future, new FutureCallback<RpcResult<Void>>() {
@Override
public void onFailure(@Nonnull Throwable error) {
LOG.error("advToBgpAndInstallFibAndTsFlows : Error in generate label or fib install process", error);
}
@Override
public void onSuccess(@Nonnull RpcResult<Void> result) {
if (result.isSuccessful()) {
LOG.info("advToBgpAndInstallFibAndTsFlows : Successfully installed custom FIB routes for prefix {}", externalIp);
} else {
LOG.error("advToBgpAndInstallFibAndTsFlows : Error in rpc call to create custom Fib entries " + "for prefix {} in DPN {}, {}", externalIp, dpnId, result.getErrors());
}
}
}, MoreExecutors.directExecutor());
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Route in project netvirt by opendaylight.
the class NatTunnelInterfaceStateListener method hndlTepAddOnNaptSwitch.
private boolean hndlTepAddOnNaptSwitch(BigInteger srcDpnId, String tunnelType, String srcTepIp, String destTepIp, String tunnelName, long routerId, Optional<Routers> routerData, String nextHopIp, Uuid vpnName, ProviderTypes extNwProvType, WriteTransaction writeFlowInvTx) {
if (!routerData.isPresent()) {
LOG.warn("hndlTepAddOnNaptSwitch: routerData is not present");
return false;
}
Routers router = routerData.get();
String routerName = router.getRouterName();
LOG.debug("hndlTepAddOnNaptSwitch : SNAT -> Processing TEP add for the DPN {} having the router {} since " + "its THE NAPT switch for the TUNNEL TYPE {} b/w SRC IP {} and DST IP {} " + "and TUNNEL NAME {} ", srcDpnId, routerName, tunnelType, srcTepIp, destTepIp, tunnelName);
Uuid networkId = router.getNetworkId();
if (networkId == null) {
LOG.warn("hndlTepAddOnNaptSwitch : SNAT -> Ignoring TEP add since the router {} is not associated to the " + "external network", routerName);
return false;
}
LOG.debug("hndlTepAddOnNaptSwitch : SNAT -> Router {} is associated with Ext nw {}", routerId, networkId);
Long vpnId;
if (vpnName == null) {
LOG.debug("hndlTepAddOnNaptSwitch : SNAT -> Internal VPN associated to router {}", routerId);
vpnId = NatUtil.getNetworkVpnIdFromRouterId(dataBroker, routerId);
if (vpnId == NatConstants.INVALID_ID) {
LOG.error("hndlTepAddOnNaptSwitch : Invalid External VPN-ID returned for routerName {}", routerName);
return false;
}
LOG.debug("hndlTepAddOnNaptSwitch : SNAT -> Retrieved External VPN-ID {} for router {}", vpnId, routerId);
} else {
LOG.debug("hndlTepAddOnNaptSwitch : SNAT -> Private BGP VPN associated to router {}", routerId);
vpnId = NatUtil.getVpnId(dataBroker, vpnName.getValue());
if (vpnId == null || vpnId == NatConstants.INVALID_ID) {
LOG.error("hndlTepAddOnNaptSwitch : Invalid vpnId returned for routerName {}", routerName);
return false;
}
LOG.debug("hndlTepAddOnNaptSwitch : SNAT -> Retrieved vpnId {} for router {}", vpnId, routerId);
}
/*1) Withdraw the old route to the external IP from the BGP which was having the
next hop as the old TEP IP.
2) Advertise to the BGP about the new route to the external IP having the
new TEP IP as the next hop.
3) Populate a new FIB entry with the next hop IP as the new TEP IP using the
FIB manager.
*/
// Withdraw the old route to the external IP from the BGP which was having the
// next hop as the old TEP IP.
final String externalVpnName = NatUtil.getAssociatedVPN(dataBroker, networkId);
if (externalVpnName == null) {
LOG.error("hndlTepAddOnNaptSwitch : SNAT -> No VPN associated with ext nw {} in router {}", networkId, routerId);
return false;
}
Collection<String> externalIps = NatUtil.getExternalIpsForRouter(dataBroker, routerId);
LOG.debug("hndlTepAddOnNaptSwitch : Clearing the FIB entries but not the BGP routes");
for (String externalIp : externalIps) {
String rd = NatUtil.getVpnRd(dataBroker, externalVpnName);
LOG.debug("hndlTepAddOnNaptSwitch : Removing Fib entry rd {} prefix {}", rd, externalIp);
fibManager.removeFibEntry(rd, externalIp, null);
}
/*
Advertise to the BGP about the new route to the external IP having the
new TEP IP as the next hop.
Populate a new FIB entry with the next hop IP as the new TEP IP using the
FIB manager.
*/
String rd = NatUtil.getVpnRd(dataBroker, externalVpnName);
if (extNwProvType == null) {
return false;
}
String gwMacAddress = null;
long l3Vni = 0;
if (extNwProvType == ProviderTypes.VXLAN) {
// Get the External Gateway MAC Address which is Router gateway MAC address for SNAT
gwMacAddress = NatUtil.getExtGwMacAddFromRouterName(dataBroker, routerName);
if (gwMacAddress != null) {
LOG.debug("hndlTepAddOnNaptSwitch : External Gateway MAC address {} found for External Router ID {}", gwMacAddress, routerId);
} else {
LOG.error("hndlTepAddOnNaptSwitch : No External Gateway MAC address found for External Router ID {}", routerId);
return false;
}
// get l3Vni value for external VPN
l3Vni = NatEvpnUtil.getL3Vni(dataBroker, rd);
if (l3Vni == NatConstants.DEFAULT_L3VNI_VALUE) {
LOG.debug("hndlTepAddOnNaptSwitch : L3VNI value is not configured in Internet VPN {} and RD {} " + "Carve-out L3VNI value from OpenDaylight VXLAN VNI Pool and continue to installing " + "NAT flows", vpnName, rd);
l3Vni = NatOverVxlanUtil.getInternetVpnVni(idManager, externalVpnName, routerId).longValue();
}
}
for (final String externalIp : externalIps) {
long serviceId = 0;
String fibExternalIp = NatUtil.validateAndAddNetworkMask(externalIp);
if (extNwProvType == ProviderTypes.VXLAN) {
LOG.debug("hndlTepAddOnNaptSwitch : SNAT -> Advertise the route to the externalIp {} " + "having nextHopIp {}", externalIp, nextHopIp);
NatEvpnUtil.addRoutesForVxLanProvType(dataBroker, bgpManager, fibManager, externalVpnName, rd, externalIp, nextHopIp, l3Vni, tunnelName, gwMacAddress, writeFlowInvTx, RouteOrigin.STATIC, srcDpnId);
serviceId = l3Vni;
} else {
Long label = externalRouterListner.checkExternalIpLabel(routerId, externalIp);
if (label == null || label == NatConstants.INVALID_ID) {
LOG.error("hndlTepAddOnNaptSwitch : SNAT->Unable to advertise to the DC GW " + "since label is invalid");
return false;
}
LOG.debug("hndlTepAddOnNaptSwitch : SNAT -> Advertise the route to the externalIp {} " + "having nextHopIp {}", externalIp, nextHopIp);
long l3vni = 0;
if (NatUtil.isOpenStackVniSemanticsEnforcedForGreAndVxlan(elanManager, extNwProvType)) {
l3vni = NatOverVxlanUtil.getInternetVpnVni(idManager, externalVpnName, l3vni).longValue();
}
Uuid externalSubnetId = NatUtil.getExternalSubnetForRouterExternalIp(externalIp, router);
NatUtil.addPrefixToBGP(dataBroker, bgpManager, fibManager, externalVpnName, rd, externalSubnetId, fibExternalIp, nextHopIp, networkId.getValue(), null, /* mac-address */
label, l3vni, RouteOrigin.STATIC, srcDpnId);
serviceId = label;
}
LOG.debug("hndlTepAddOnNaptSwitch: SNAT -> Install custom FIB routes " + "(Table 21 -> Push MPLS label to Tunnel port");
List<Instruction> customInstructions = new ArrayList<>();
int customInstructionIndex = 0;
long externalSubnetVpnId = NatUtil.getExternalSubnetVpnIdForRouterExternalIp(dataBroker, externalIp, router);
if (externalSubnetVpnId != NatConstants.INVALID_ID) {
LOG.debug("hndlTepAddOnNaptSwitch : Will install custom FIB router with external subnet VPN ID {}", externalSubnetVpnId);
BigInteger subnetIdMetaData = MetaDataUtil.getVpnIdMetadata(externalSubnetVpnId);
customInstructions.add(new InstructionWriteMetadata(subnetIdMetaData, MetaDataUtil.METADATA_MASK_VRFID).buildInstruction(customInstructionIndex));
customInstructionIndex++;
}
customInstructions.add(new InstructionGotoTable(NwConstants.INBOUND_NAPT_TABLE).buildInstruction(customInstructionIndex));
CreateFibEntryInput input = new CreateFibEntryInputBuilder().setVpnName(externalVpnName).setSourceDpid(srcDpnId).setInstruction(customInstructions).setIpAddress(fibExternalIp).setServiceId(serviceId).setInstruction(customInstructions).build();
Future<RpcResult<Void>> future = fibRpcService.createFibEntry(input);
ListenableFuture<RpcResult<Void>> listenableFuture = JdkFutureAdapters.listenInPoolThread(future);
Futures.addCallback(listenableFuture, new FutureCallback<RpcResult<Void>>() {
@Override
public void onFailure(@Nonnull Throwable error) {
LOG.error("hndlTepAddOnNaptSwitch : SNAT->Error in generate label or fib install process", error);
}
@Override
public void onSuccess(@Nonnull RpcResult<Void> result) {
if (result.isSuccessful()) {
LOG.info("hndlTepAddOnNaptSwitch : SNAT -> Successfully installed custom FIB routes " + "for prefix {}", externalIp);
} else {
LOG.error("hndlTepAddOnNaptSwitch : SNAT -> Error in rpc call to create custom Fib entries " + "for prefix {} in DPN {}, {}", externalIp, srcDpnId, result.getErrors());
}
}
}, MoreExecutors.directExecutor());
}
return true;
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Route in project netvirt by opendaylight.
the class RouterDpnChangeListener method add.
@Override
protected void add(final InstanceIdentifier<DpnVpninterfacesList> identifier, final DpnVpninterfacesList dpnInfo) {
LOG.trace("add : key: {}, value: {}", dpnInfo.getKey(), dpnInfo);
final String routerUuid = identifier.firstKeyOf(RouterDpnList.class).getRouterId();
BigInteger dpnId = dpnInfo.getDpnId();
// check router is associated to external network
InstanceIdentifier<Routers> id = NatUtil.buildRouterIdentifier(routerUuid);
Optional<Routers> routerData = SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker, LogicalDatastoreType.CONFIGURATION, id);
if (routerData.isPresent()) {
Routers router = routerData.get();
Uuid networkId = router.getNetworkId();
if (networkId != null) {
if (natMode == NatMode.Conntrack) {
BigInteger naptSwitch = NatUtil.getPrimaryNaptfromRouterName(dataBroker, router.getRouterName());
if (naptSwitch == null || naptSwitch.equals(BigInteger.ZERO)) {
LOG.warn("add : NAPT switch is not selected.");
return;
}
// If it is for NAPT switch skip as the flows would be already programmed.
if (naptSwitch.equals(dpnId)) {
LOG.debug("Skipping the notification recived for NAPT switch {}", routerUuid);
return;
}
natServiceManager.notify(router, naptSwitch, dpnId, SnatServiceManager.Action.SNAT_ROUTER_ENBL);
} else {
coordinator.enqueueJob(NatConstants.NAT_DJC_PREFIX + dpnInfo.getKey(), () -> {
WriteTransaction writeFlowInvTx = dataBroker.newWriteOnlyTransaction();
WriteTransaction removeFlowInvTx = dataBroker.newWriteOnlyTransaction();
LOG.debug("add : Router {} is associated with ext nw {}", routerUuid, networkId);
Uuid vpnName = NatUtil.getVpnForRouter(dataBroker, routerUuid);
Long routerId = NatUtil.getVpnId(dataBroker, routerUuid);
List<ListenableFuture<Void>> futures = new ArrayList<>();
if (routerId == NatConstants.INVALID_ID) {
LOG.error("add : Invalid routerId returned for routerName {}", routerUuid);
writeFlowInvTx.cancel();
removeFlowInvTx.cancel();
return futures;
}
extNetGroupInstaller.installExtNetGroupEntries(networkId, dpnId);
Long vpnId;
if (vpnName == null) {
LOG.debug("add : Internal vpn associated to router {}", routerUuid);
vpnId = routerId;
if (vpnId == NatConstants.INVALID_ID) {
LOG.error("add : Invalid vpnId returned for routerName {}", routerUuid);
writeFlowInvTx.cancel();
removeFlowInvTx.cancel();
return futures;
}
LOG.debug("add : Retrieved vpnId {} for router {}", vpnId, routerUuid);
// Install default entry in FIB to SNAT table
LOG.info("add : Installing default route in FIB on dpn {} for router {} with vpn {}", dpnId, routerUuid, vpnId);
installDefaultNatRouteForRouterExternalSubnets(dpnId, NatUtil.getExternalSubnetIdsFromExternalIps(router.getExternalIps()));
snatDefaultRouteProgrammer.installDefNATRouteInDPN(dpnId, vpnId, writeFlowInvTx);
} else {
LOG.debug("add : External BGP vpn associated to router {}", routerUuid);
vpnId = NatUtil.getVpnId(dataBroker, vpnName.getValue());
if (vpnId == NatConstants.INVALID_ID) {
LOG.error("add : Invalid vpnId returned for routerName {}", routerUuid);
writeFlowInvTx.cancel();
removeFlowInvTx.cancel();
return futures;
}
LOG.debug("add : Retrieved vpnId {} for router {}", vpnId, routerUuid);
// Install default entry in FIB to SNAT table
LOG.debug("add : Installing default route in FIB on dpn {} for routerId {} with " + "vpnId {}...", dpnId, routerUuid, vpnId);
installDefaultNatRouteForRouterExternalSubnets(dpnId, NatUtil.getExternalSubnetIdsFromExternalIps(router.getExternalIps()));
snatDefaultRouteProgrammer.installDefNATRouteInDPN(dpnId, vpnId, routerId, writeFlowInvTx);
}
if (router.isEnableSnat()) {
LOG.info("add : SNAT enabled for router {}", routerUuid);
ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerUuid, networkId);
if (extNwProvType == null) {
LOG.error("add : External Network Provider Type missing");
writeFlowInvTx.cancel();
removeFlowInvTx.cancel();
return futures;
}
handleSNATForDPN(dpnId, routerUuid, routerId, vpnId, writeFlowInvTx, removeFlowInvTx, extNwProvType);
} else {
LOG.info("add : SNAT is not enabled for router {} to handle addDPN event {}", routerUuid, dpnId);
}
futures.add(NatUtil.waitForTransactionToComplete(writeFlowInvTx));
futures.add(NatUtil.waitForTransactionToComplete(removeFlowInvTx));
return futures;
}, NatConstants.NAT_DJC_MAX_RETRIES);
}
// end of controller based SNAT
}
} else {
LOG.debug("add : Router {} is not associated with External network", routerUuid);
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Route in project netvirt by opendaylight.
the class FloatingIPListener method addOrDelDefaultFibRouteForDnat.
private void addOrDelDefaultFibRouteForDnat(BigInteger dpnId, String routerName, long routerId, WriteTransaction tx, boolean create) {
Boolean wrTxPresent = true;
if (tx == null) {
wrTxPresent = false;
tx = dataBroker.newWriteOnlyTransaction();
}
// Check if the router to bgp-vpn association is present
long associatedVpnId = NatConstants.INVALID_ID;
Uuid associatedVpn = NatUtil.getVpnForRouter(dataBroker, routerName);
if (associatedVpn != null) {
associatedVpnId = NatUtil.getVpnId(dataBroker, associatedVpn.getValue());
}
if (create) {
if (associatedVpnId != NatConstants.INVALID_ID) {
LOG.debug("addOrDelDefaultFibRouteForDnat: Install NAT default route on DPN {} for the router {} with " + "vpn-id {}", dpnId, routerName, associatedVpnId);
defaultRouteProgrammer.installDefNATRouteInDPN(dpnId, associatedVpnId, routerId, tx);
} else {
LOG.debug("addOrDelDefaultFibRouteForDnat: Install NAT default route on DPN {} for the router {} with " + "vpn-id {}", dpnId, routerName, routerId);
defaultRouteProgrammer.installDefNATRouteInDPN(dpnId, routerId, tx);
}
} else {
if (associatedVpnId != NatConstants.INVALID_ID) {
LOG.debug("addOrDelDefaultFibRouteForDnat: Remove NAT default route on DPN {} for the router {} " + "with vpn-id {}", dpnId, routerName, associatedVpnId);
defaultRouteProgrammer.removeDefNATRouteInDPN(dpnId, associatedVpnId, routerId, tx);
} else {
LOG.debug("addOrDelDefaultFibRouteForDnat: Remove NAT default route on DPN {} for the router {} " + "with vpn-id {}", dpnId, routerName, routerId);
defaultRouteProgrammer.removeDefNATRouteInDPN(dpnId, routerId, tx);
}
}
if (!wrTxPresent) {
tx.submit();
}
}
Aggregations