use of org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps in project netvirt by opendaylight.
the class VxlanGreConntrackBasedSnatService method createOutboundTblEntryForVxlanGre.
protected void createOutboundTblEntryForVxlanGre(BigInteger dpnId, long routerId, Long extNetVpnId, List<ExternalIps> externalIps, int elanId, int addOrRemove) {
LOG.info("createOutboundTblEntryForVxlanGre: Install Outbound table flow on dpId {} for routerId {}", dpnId, routerId);
List<MatchInfoBase> matches = new ArrayList<>();
matches.add(MatchEthernetType.IPV4);
matches.add(new NxMatchCtState(TRACKED_NEW_CT_STATE, TRACKED_NEW_CT_MASK));
matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
if (externalIps.isEmpty()) {
LOG.error("createOutboundTblEntryForVxlanGre: No externalIP present for routerId {}", routerId);
return;
}
// The logic now handle only one external IP per router, others if present will be ignored.
String externalIp = externalIps.get(0).getIpAddress();
List<ActionInfo> actionsInfos = new ArrayList<>();
if (addOrRemove == NwConstants.ADD_FLOW) {
ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil.getVpnIdMetadata(extNetVpnId));
actionsInfos.add(actionSetFieldMeta);
}
List<ActionNxConntrack.NxCtAction> ctActionsListCommit = new ArrayList<>();
int rangePresent = NxActionNatRangePresent.NXNATRANGEIPV4MIN.getIntValue();
int flags = NxActionNatFlags.NXNATFSRC.getIntValue();
ActionNxConntrack.NxCtAction nxCtActionCommit = new ActionNxConntrack.NxNat(0, flags, rangePresent, new IpPrefixOrAddress(externalIp.toCharArray()).getIpAddress(), null, 0, 0);
ctActionsListCommit.add(nxCtActionCommit);
int ctCommitFlag = 1;
ActionNxConntrack actionNxConntrackSubmit = new ActionNxConntrack(ctCommitFlag, 0, elanId, NwConstants.NAPT_PFIB_TABLE, ctActionsListCommit);
actionsInfos.add(actionNxConntrackSubmit);
List<InstructionInfo> instructions = new ArrayList<>();
instructions.add(new InstructionApplyActions(actionsInfos));
String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, NatConstants.SNAT_NEW_FLOW_PRIORITY, flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps in project netvirt by opendaylight.
the class NaptManager method getExternalAddressMapping.
/**
* method to get external ip/port mapping when provided with internal ip/port pair
* If already a mapping exist for the given input, then the existing mapping is returned
* instead of overwriting with new ip/port pair.
*
* @param segmentId - Router ID
* @param sourceAddress - internal ip address/port pair
* @param protocol - TCP/UDP
* @return external ip address/port
*/
// TODO Clean up the exception handling
@SuppressWarnings("checkstyle:IllegalCatch")
public SessionAddress getExternalAddressMapping(long segmentId, SessionAddress sourceAddress, NAPTEntryEvent.Protocol protocol) {
LOG.debug("getExternalAddressMapping : called with segmentId {}, internalIp {} and port {}", segmentId, sourceAddress.getIpAddress(), sourceAddress.getPortNumber());
/*
1. Get Internal IP, Port in IP:Port format
2. Inside DB with routerId get the list of entries and check if it matches with existing IP:Port
3. If True return SessionAddress of ExternalIp and Port
4. Else check ip Map and Form the ExternalIp and Port and update DB and then return ExternalIp and Port
*/
// SessionAddress externalIpPort = new SessionAddress();
String internalIpPort = sourceAddress.getIpAddress() + ":" + sourceAddress.getPortNumber();
// First check existing Port Map.
SessionAddress existingIpPort = checkIpPortMap(segmentId, internalIpPort, protocol);
if (existingIpPort != null) {
// populate externalIpPort from IpPortMap and return
LOG.debug("getExternalAddressMapping : successfully returning existingIpPort as {} and {}", existingIpPort.getIpAddress(), existingIpPort.getPortNumber());
return existingIpPort;
} else {
// Now check in ip-map
String externalIp = checkIpMap(segmentId, sourceAddress.getIpAddress());
if (externalIp == null) {
LOG.error("getExternalAddressMapping : Unexpected error, internal to external " + "ip map does not exist");
return null;
} else {
/* Logic assuming internalIp is always ip and not subnet
* case 1: externalIp is ip
* a) goto externalIp pool and getPort and return
* b) else return error
* case 2: externalIp is subnet
* a) Take first externalIp and goto that Pool and getPort
* if port -> return
* else Take second externalIp and create that Pool and getPort
* if port ->return
* else
* Continue same with third externalIp till we exhaust subnet
* b) Nothing worked return error
*/
SubnetUtils externalIpSubnet;
List<String> allIps = new ArrayList<>();
String subnetPrefix = "/" + String.valueOf(NatConstants.DEFAULT_PREFIX);
boolean extSubnetFlag = false;
if (!externalIp.contains(subnetPrefix)) {
extSubnetFlag = true;
externalIpSubnet = new SubnetUtils(externalIp);
allIps = Arrays.asList(externalIpSubnet.getInfo().getAllAddresses());
LOG.debug("getExternalAddressMapping : total count of externalIps available {}", externalIpSubnet.getInfo().getAddressCount());
} else {
LOG.debug("getExternalAddressMapping : getExternalAddress single ip case");
if (externalIp.contains(subnetPrefix)) {
// remove /32 what we got from checkIpMap
externalIp = externalIp.substring(0, externalIp.indexOf(subnetPrefix));
}
allIps.add(externalIp);
}
boolean nextExtIpFlag = false;
for (String extIp : allIps) {
LOG.info("getExternalAddressMapping : Looping externalIPs with externalIP now as {}", extIp);
if (nextExtIpFlag) {
createNaptPortPool(extIp);
LOG.debug("getExternalAddressMapping : Created Pool for next Ext IP {}", extIp);
}
AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(extIp).setIdKey(internalIpPort).build();
try {
Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
RpcResult<AllocateIdOutput> rpcResult;
if (result != null && result.get().isSuccessful()) {
LOG.debug("getExternalAddressMapping : Got id from idManager");
rpcResult = result.get();
} else {
LOG.error("getExternalAddressMapping : getExternalAddressMapping, idManager could not " + "allocate id retry if subnet");
if (!extSubnetFlag) {
LOG.error("getExternalAddressMapping : getExternalAddressMapping returning null " + "for single IP case, may be ports exhausted");
return null;
}
LOG.debug("getExternalAddressMapping : Could be ports exhausted case, " + "try with another externalIP if possible");
nextExtIpFlag = true;
continue;
}
int extPort = rpcResult.getResult().getIdValue().intValue();
// Write to ip-port-map before returning
IpPortExternalBuilder ipExt = new IpPortExternalBuilder();
IpPortExternal ipPortExt = ipExt.setIpAddress(extIp).setPortNum(extPort).build();
IpPortMap ipm = new IpPortMapBuilder().setKey(new IpPortMapKey(internalIpPort)).setIpPortInternal(internalIpPort).setIpPortExternal(ipPortExt).build();
LOG.debug("getExternalAddressMapping : writing into ip-port-map with " + "externalIP {} and port {}", ipPortExt.getIpAddress(), ipPortExt.getPortNum());
try {
MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, getIpPortMapIdentifier(segmentId, internalIpPort, protocol), ipm);
} catch (UncheckedExecutionException uee) {
LOG.error("getExternalAddressMapping : Failed to write into ip-port-map with exception", uee);
}
// Write to snat-internal-ip-port-info
String internalIpAddress = sourceAddress.getIpAddress();
int ipPort = sourceAddress.getPortNumber();
ProtocolTypes protocolType = NatUtil.getProtocolType(protocol);
List<Integer> portList = new ArrayList<>(NatUtil.getInternalIpPortListInfo(dataBroker, segmentId, internalIpAddress, protocolType));
portList.add(ipPort);
IntIpProtoTypeBuilder builder = new IntIpProtoTypeBuilder();
IntIpProtoType intIpProtocolType = builder.setKey(new IntIpProtoTypeKey(protocolType)).setPorts(portList).build();
try {
MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, NatUtil.buildSnatIntIpPortIdentifier(segmentId, internalIpAddress, protocolType), intIpProtocolType);
} catch (Exception ex) {
LOG.error("getExternalAddressMapping : Failed to write into snat-internal-ip-port-info " + "with exception", ex);
}
SessionAddress externalIpPort = new SessionAddress(extIp, extPort);
LOG.debug("getExternalAddressMapping : successfully returning externalIP {} " + "and port {}", externalIpPort.getIpAddress(), externalIpPort.getPortNumber());
return externalIpPort;
} catch (InterruptedException | ExecutionException e) {
LOG.error("getExternalAddressMapping : Exception caught", e);
return null;
}
}
// end of for loop
}
// end of else ipmap present
}
// end of else check ipmap
LOG.error("getExternalAddressMapping : Unable to handle external IP address and port mapping with segmentId {}," + "internalIp {} and internalPort {}", segmentId, sourceAddress.getIpAddress(), sourceAddress.getPortNumber());
return null;
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps in project netvirt by opendaylight.
the class NaptSwitchHA method bestEffortDeletion.
protected void bestEffortDeletion(long routerId, String routerName, Map<String, Long> externalIpLabel, WriteTransaction removeFlowInvTx) {
Collection<String> newExternalIps = NatUtil.getExternalIpsForRouter(dataBroker, routerId);
if (externalIpsCache != null) {
Set<String> removedExternalIps = new HashSet<>(externalIpsCache);
removedExternalIps.removeAll(newExternalIps);
if (removedExternalIps.isEmpty()) {
LOG.info("bestEffortDeletion : No external Ip needed to be removed in bestEffortDeletion " + "method for router {}", routerName);
return;
}
Uuid networkId = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
String vpnName = getExtNetworkVpnName(routerName, networkId);
if (vpnName == null) {
LOG.error("bestEffortDeletion : Vpn is not associated to externalN/w of router {}", routerName);
return;
}
BigInteger naptSwitch = NatUtil.getPrimaryNaptfromRouterName(dataBroker, routerName);
if (naptSwitch == null || naptSwitch.equals(BigInteger.ZERO)) {
LOG.error("bestEffortDeletion : No naptSwitch is selected for router {}", routerName);
return;
}
ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName, networkId);
if (extNwProvType == null) {
return;
}
String gwMacAddress = NatUtil.getExtGwMacAddFromRouterName(dataBroker, routerName);
if (gwMacAddress != null) {
LOG.debug("bestEffortDeletion : External Gateway MAC address {} found for External Router ID {}", gwMacAddress, routerId);
} else {
LOG.error("bestEffortDeletion : No External Gateway MAC address found for External Router ID {}", routerId);
return;
}
if (extNwProvType == ProviderTypes.VXLAN) {
for (String externalIp : removedExternalIps) {
externalRouterListener.clearBgpRoutes(externalIp, vpnName);
externalRouterListener.delFibTsAndReverseTraffic(naptSwitch, routerId, externalIp, vpnName, networkId, NatConstants.DEFAULT_LABEL_VALUE, gwMacAddress, true, removeFlowInvTx);
LOG.debug("bestEffortDeletion : Successfully removed fib entry for externalIp {} for routerId {} " + "on NAPT switch {} ", externalIp, routerId, naptSwitch);
}
} else {
if (externalIpLabel == null || externalIpLabel.isEmpty()) {
LOG.error("bestEffortDeletion : ExternalIpLabel map is empty for router {}", routerName);
return;
}
Long label;
for (String externalIp : removedExternalIps) {
if (externalIpLabel.containsKey(externalIp)) {
label = externalIpLabel.get(externalIp);
LOG.debug("bestEffortDeletion : Label {} for ExternalIp {} for router {}", label, externalIp, routerName);
} else {
LOG.debug("bestEffortDeletion : Label for ExternalIp {} is not found for router {}", externalIp, routerName);
continue;
}
externalRouterListener.clearBgpRoutes(externalIp, vpnName);
externalRouterListener.delFibTsAndReverseTraffic(naptSwitch, routerId, externalIp, vpnName, networkId, label, gwMacAddress, true, removeFlowInvTx);
LOG.debug("bestEffortDeletion : Successfully removed fib entries in switch {} for router {} " + "and externalIps {}", naptSwitch, routerId, externalIp);
}
}
} else {
LOG.debug("bestEffortDeletion : No external IP found for router {}", routerId);
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps in project netvirt by opendaylight.
the class NaptSwitchHA method installSnatFlows.
protected void installSnatFlows(String routerName, Long routerId, BigInteger naptSwitch, Long routerVpnId, WriteTransaction writeFlowInvTx) {
if (routerId.equals(routerVpnId)) {
LOG.debug("installSnatFlows : Installing flows for router with internalvpnId");
// 36 -> 46 ..Install flow forwarding packet to table46 from table36
LOG.debug("installSnatFlows : installTerminatingServiceTblEntry in naptswitch with dpnId {} for " + "routerName {} with routerId {}", naptSwitch, routerName, routerId);
externalRouterListener.installTerminatingServiceTblEntry(naptSwitch, routerName, routerId, writeFlowInvTx);
// Install default flows punting to controller in table 46(OutBoundNapt table)
LOG.debug("installSnatFlows : installOutboundMissEntry in naptswitch with dpnId {} for " + "routerName {} with routerId {}", naptSwitch, routerName, routerId);
externalRouterListener.createOutboundTblEntry(naptSwitch, routerId, writeFlowInvTx);
// Table 47 point to table 21 for inbound traffic
LOG.debug("installSnatFlows : installNaptPfibEntry in naptswitch with dpnId {} for router {}", naptSwitch, routerId);
externalRouterListener.installNaptPfibEntry(naptSwitch, routerId, writeFlowInvTx);
// Table 47 point to group
LOG.debug("installSnatFlows : installNaptPfibExternalOutputFlow in naptswitch with dpnId {} for router {}", naptSwitch, routerId);
externalRouterListener.installNaptPfibExternalOutputFlow(routerName, routerId, naptSwitch, writeFlowInvTx);
} else {
Uuid extNetworkUuid = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
if (extNetworkUuid == null) {
LOG.error("onRouterAssociatedToVpn : Unable to retrieve external network Uuid for router {}", routerName);
return;
}
ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName, extNetworkUuid);
if (extNwProvType == null) {
LOG.error("onRouterAssociatedToVpn : External Network Provider Type missing");
return;
}
// 36 -> 46 ..Install flow forwarding packet to table46 from table36
LOG.debug("installSnatFlows : installTerminatingServiceTblEntry in naptswitch with dpnId {} for " + "routerName {} with BgpVpnId {}", naptSwitch, routerName, routerVpnId);
externalRouterListener.installTerminatingServiceTblEntryWithUpdatedVpnId(naptSwitch, routerName, routerId, routerVpnId, writeFlowInvTx, extNwProvType);
// Install default flows punting to controller in table 46(OutBoundNapt table)
LOG.debug("installSnatFlows : installOutboundMissEntry in naptswitch with dpnId {} for " + "routerName {} with BgpVpnId {}", naptSwitch, routerName, routerVpnId);
externalRouterListener.createOutboundTblEntryWithBgpVpn(naptSwitch, routerId, routerVpnId, writeFlowInvTx);
// Table 47 point to table 21 for inbound traffic
LOG.debug("installSnatFlows : installNaptPfibEntry in naptswitch with dpnId {} for router {} " + "with BgpVpnId {}", naptSwitch, routerId, routerVpnId);
externalRouterListener.installNaptPfibEntryWithBgpVpn(naptSwitch, routerId, routerVpnId, writeFlowInvTx);
}
Uuid networkId = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
String vpnName = getExtNetworkVpnName(routerName, networkId);
if (vpnName != null) {
// NAPT PFIB point to FIB table for outbound traffic
long vpnId = NatUtil.getVpnId(dataBroker, vpnName);
boolean shouldInstallNaptPfibWithExtNetworkVpnId = true;
Collection<Uuid> externalSubnetIds = NatUtil.getExternalSubnetIdsForRouter(dataBroker, routerName);
if (!externalSubnetIds.isEmpty()) {
// NAPT PFIB point to FIB table for outbound traffic - using external subnetID as vpnID.
for (Uuid externalSubnetId : externalSubnetIds) {
long externalSubnetVpnId = NatUtil.getExternalSubnetVpnId(dataBroker, externalSubnetId);
if (externalSubnetVpnId != NatConstants.INVALID_ID) {
shouldInstallNaptPfibWithExtNetworkVpnId = false;
LOG.debug("installSnatFlows : installNaptPfibEntry fin naptswitch with dpnId {} for " + "BgpVpnId {}", naptSwitch, externalSubnetVpnId);
externalRouterListener.installNaptPfibEntry(naptSwitch, externalSubnetVpnId, writeFlowInvTx);
}
}
}
if (vpnId != NatConstants.INVALID_ID && shouldInstallNaptPfibWithExtNetworkVpnId) {
// NAPT PFIB table point to FIB table for outbound traffic - using external networkID as vpnID.
LOG.debug("installSnatFlows : installNaptPfibEntry fin naptswitch with dpnId {} for " + "BgpVpnId {}", naptSwitch, vpnId);
externalRouterListener.installNaptPfibEntry(naptSwitch, vpnId, writeFlowInvTx);
} else if (vpnId != NatConstants.INVALID_ID) {
LOG.debug("installSnatFlows : Associated BgpvpnId not found for router {}", routerId);
}
// Install Fib entries for ExternalIps & program 36 -> 44
Collection<String> externalIps = NatUtil.getExternalIpsForRouter(dataBroker, routerId);
String rd = NatUtil.getVpnRd(dataBroker, vpnName);
for (String externalIp : externalIps) {
removeFibEntry(rd, externalIp);
LOG.debug("installSnatFlows : advToBgpAndInstallFibAndTsFlows in naptswitch id {} " + "with vpnName {} and externalIp {}", naptSwitch, vpnName, externalIp);
externalRouterListener.advToBgpAndInstallFibAndTsFlows(naptSwitch, NwConstants.INBOUND_NAPT_TABLE, vpnName, routerId, routerName, externalIp, networkId, null, /* external-router */
writeFlowInvTx);
LOG.debug("installSnatFlows : Successfully added fib entries in naptswitch {} for " + "router {} with external IP {}", naptSwitch, routerId, externalIp);
}
} else {
LOG.debug("installSnatFlows : Associated vpnName not found for router {}", routerId);
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps in project netvirt by opendaylight.
the class NaptSwitchHA method removeSnatFlowsInOldNaptSwitch.
/* This method checks the switch that gone down is a NaptSwitch for a router.
If it is a NaptSwitch
1) selects new NAPT switch
2) installs nat flows in new NAPT switch
table 21(FIB)->26(PSNAT)->group(resubmit/napttunnel)->36(Terminating)->46(outbound)->47(resubmit)->21
3) modify the group and miss entry flow in other vSwitches pointing to newNaptSwitch
4) Remove nat flows in oldNaptSwitch
*/
/*public void handleNaptSwitchDown(BigInteger dpnId){
LOG.debug("handleNaptSwitchDown method is called with dpnId {}",dpnId);
BigInteger naptSwitch;
try {
NaptSwitches naptSwitches = NatUtil.getNaptSwitch(dataBroker);
if (naptSwitches == null || naptSwitches.getRouterToNaptSwitch() == null
|| naptSwitches.getRouterToNaptSwitch().isEmpty()) {
LOG.debug("NaptSwitchDown: NaptSwitch is not allocated for none of the routers");
return;
}
for (RouterToNaptSwitch routerToNaptSwitch : naptSwitches.getRouterToNaptSwitch()) {
String routerName = routerToNaptSwitch.getRouterName();
naptSwitch = routerToNaptSwitch.getPrimarySwitchId();
boolean naptStatus = isNaptSwitchDown(routerName,dpnId,naptSwitch);
if (!naptStatus) {
LOG.debug("NaptSwitchDown: Switch with DpnId {} is not naptSwitch for router {}",
dpnId, routerName);
} else {
removeSnatFlowsInOldNaptSwitch(routerName,naptSwitch);
return;
}
}
} catch (Exception ex) {
LOG.error("Exception in handleNaptSwitchDown method {}",ex);
}
}*/
protected void removeSnatFlowsInOldNaptSwitch(String routerName, Long routerId, BigInteger naptSwitch, Map<String, Long> externalIpmap, WriteTransaction removeFlowInvTx) {
// remove SNAT flows in old NAPT SWITCH
Uuid networkId = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
String vpnName = getExtNetworkVpnName(routerName, networkId);
if (vpnName == null) {
LOG.error("removeSnatFlowsInOldNaptSwitch : Vpn is not associated to externalN/w of router {}", routerName);
return;
}
ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName, networkId);
if (extNwProvType == null) {
LOG.error("removeSnatFlowsInOldNaptSwitch : Unable to retrieve the External Network Provider Type " + "for Router {}", routerName);
return;
}
if (extNwProvType == ProviderTypes.VXLAN) {
evpnNaptSwitchHA.evpnRemoveSnatFlowsInOldNaptSwitch(routerName, routerId, vpnName, naptSwitch, removeFlowInvTx);
} else {
// Remove the Terminating Service table entry which forwards the packet to Outbound NAPT Table
long tunnelId = NatUtil.getTunnelIdForNonNaptToNaptFlow(dataBroker, elanManager, idManager, routerId, routerName);
String tsFlowRef = externalRouterListener.getFlowRefTs(naptSwitch, NwConstants.INTERNAL_TUNNEL_TABLE, tunnelId);
FlowEntity tsNatFlowEntity = NatUtil.buildFlowEntity(naptSwitch, NwConstants.INTERNAL_TUNNEL_TABLE, tsFlowRef);
LOG.info("removeSnatFlowsInOldNaptSwitch : Remove the flow in table {} for the old napt switch " + "with the DPN ID {} and router ID {}", NwConstants.INTERNAL_TUNNEL_TABLE, naptSwitch, routerId);
mdsalManager.removeFlowToTx(tsNatFlowEntity, removeFlowInvTx);
}
if (NatUtil.isOpenStackVniSemanticsEnforcedForGreAndVxlan(elanManager, extNwProvType)) {
// Remove the flow table 25->44 If there is no FIP Match on table 25 (PDNAT_TABLE)
NatUtil.removePreDnatToSnatTableEntry(mdsalManager, naptSwitch, removeFlowInvTx);
}
// Remove the Outbound flow entry which forwards the packet to Outbound NAPT Table
String outboundNatFlowRef = externalRouterListener.getFlowRefOutbound(naptSwitch, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
FlowEntity outboundNatFlowEntity = NatUtil.buildFlowEntity(naptSwitch, NwConstants.OUTBOUND_NAPT_TABLE, outboundNatFlowRef);
LOG.info("Remove the flow in table {} for the old napt switch with the DPN ID {} and router ID {}", NwConstants.OUTBOUND_NAPT_TABLE, naptSwitch, routerId);
mdsalManager.removeFlowToTx(outboundNatFlowEntity, removeFlowInvTx);
// Remove the NAPT PFIB TABLE (47->21) which forwards the incoming packet to FIB Table matching on the
// External Subnet Vpn Id.
Collection<Uuid> externalSubnetIdsForRouter = NatUtil.getExternalSubnetIdsForRouter(dataBroker, routerName);
for (Uuid externalSubnetId : externalSubnetIdsForRouter) {
long subnetVpnId = NatUtil.getVpnId(dataBroker, externalSubnetId.getValue());
if (subnetVpnId != -1) {
String natPfibSubnetFlowRef = externalRouterListener.getFlowRefTs(naptSwitch, NwConstants.NAPT_PFIB_TABLE, subnetVpnId);
FlowEntity natPfibFlowEntity = NatUtil.buildFlowEntity(naptSwitch, NwConstants.NAPT_PFIB_TABLE, natPfibSubnetFlowRef);
mdsalManager.removeFlowToTx(natPfibFlowEntity, removeFlowInvTx);
LOG.debug("removeSnatFlowsInOldNaptSwitch : Removed the flow in table {} with external subnet " + "Vpn Id {} as metadata on Napt Switch {}", NwConstants.NAPT_PFIB_TABLE, subnetVpnId, naptSwitch);
}
}
// Remove the NAPT_PFIB_TABLE(47) flow entry forwards the packet to Fib Table for inbound traffic
// matching on the router ID.
String naptPFibflowRef = externalRouterListener.getFlowRefTs(naptSwitch, NwConstants.NAPT_PFIB_TABLE, routerId);
FlowEntity naptPFibFlowEntity = NatUtil.buildFlowEntity(naptSwitch, NwConstants.NAPT_PFIB_TABLE, naptPFibflowRef);
LOG.info("removeSnatFlowsInOldNaptSwitch : Remove the flow in table {} for the old napt switch " + "with the DPN ID {} and router ID {}", NwConstants.NAPT_PFIB_TABLE, naptSwitch, routerId);
mdsalManager.removeFlowToTx(naptPFibFlowEntity, removeFlowInvTx);
// Remove the NAPT_PFIB_TABLE(47) flow entry forwards the packet to Fib Table for outbound traffic
// matching on the vpn ID.
boolean switchSharedByRouters = false;
Uuid extNetworkId = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
if (extNetworkId != null) {
List<String> routerNamesAssociated = getRouterIdsForExtNetwork(extNetworkId);
for (String routerNameAssociated : routerNamesAssociated) {
if (!routerNameAssociated.equals(routerName)) {
Long routerIdAssociated = NatUtil.getVpnId(dataBroker, routerNameAssociated);
BigInteger naptDpn = NatUtil.getPrimaryNaptfromRouterName(dataBroker, routerNameAssociated);
if (naptDpn != null && naptDpn.equals(naptSwitch)) {
LOG.debug("removeSnatFlowsInOldNaptSwitch : Napt switch {} is also acting as primary " + "for router {}", naptSwitch, routerIdAssociated);
switchSharedByRouters = true;
break;
}
}
}
if (!switchSharedByRouters) {
Long vpnId = getVpnIdForRouter(routerId, extNetworkId);
if (vpnId != NatConstants.INVALID_ID) {
String naptFibflowRef = externalRouterListener.getFlowRefTs(naptSwitch, NwConstants.NAPT_PFIB_TABLE, vpnId);
FlowEntity naptFibFlowEntity = NatUtil.buildFlowEntity(naptSwitch, NwConstants.NAPT_PFIB_TABLE, naptFibflowRef);
LOG.info("removeSnatFlowsInOldNaptSwitch : Remove the flow in table {} for the old napt switch" + " with the DPN ID {} and vpnId {}", NwConstants.NAPT_PFIB_TABLE, naptSwitch, vpnId);
mdsalManager.removeFlowToTx(naptFibFlowEntity, removeFlowInvTx);
} else {
LOG.error("removeSnatFlowsInOldNaptSwitch : Invalid vpnId retrieved for routerId {}", routerId);
return;
}
}
}
// Remove Fib entries,tables 20->44 ,36-> 44
String gwMacAddress = NatUtil.getExtGwMacAddFromRouterName(dataBroker, routerName);
if (externalIpmap != null && !externalIpmap.isEmpty()) {
for (Entry<String, Long> entry : externalIpmap.entrySet()) {
String externalIp = entry.getKey();
Long label = entry.getValue();
externalRouterListener.delFibTsAndReverseTraffic(naptSwitch, routerId, externalIp, vpnName, extNetworkId, label, gwMacAddress, true, removeFlowInvTx);
LOG.debug("removeSnatFlowsInOldNaptSwitch : Successfully removed fib entries in old naptswitch {} " + "for router {} and externalIps {} label {}", naptSwitch, routerId, externalIp, label);
}
} else {
List<String> externalIps = NatUtil.getExternalIpsForRouter(dataBroker, routerName);
if (networkId != null) {
externalRouterListener.clearFibTsAndReverseTraffic(naptSwitch, routerId, networkId, externalIps, null, gwMacAddress, removeFlowInvTx);
LOG.debug("removeSnatFlowsInOldNaptSwitch : Successfully removed fib entries in old naptswitch {} for " + "router {} with networkId {} and externalIps {}", naptSwitch, routerId, networkId, externalIps);
} else {
LOG.debug("removeSnatFlowsInOldNaptSwitch : External network not associated to router {}", routerId);
}
externalRouterListener.removeNaptFibExternalOutputFlows(routerId, naptSwitch, extNetworkId, externalIps, removeFlowInvTx);
}
// For the router ID get the internal IP , internal port and the corresponding external IP and external Port.
IpPortMapping ipPortMapping = NatUtil.getIportMapping(dataBroker, routerId);
if (ipPortMapping == null || ipPortMapping.getIntextIpProtocolType() == null || ipPortMapping.getIntextIpProtocolType().isEmpty()) {
LOG.warn("removeSnatFlowsInOldNaptSwitch : No Internal Ip Port mapping associated to router {}, " + "no flows need to be removed in oldNaptSwitch {}", routerId, naptSwitch);
return;
}
BigInteger cookieSnatFlow = NatUtil.getCookieNaptFlow(routerId);
List<IntextIpProtocolType> intextIpProtocolTypes = ipPortMapping.getIntextIpProtocolType();
for (IntextIpProtocolType intextIpProtocolType : intextIpProtocolTypes) {
if (intextIpProtocolType.getIpPortMap() == null || intextIpProtocolType.getIpPortMap().isEmpty()) {
LOG.debug("removeSnatFlowsInOldNaptSwitch : No {} session associated to router {}," + "no flows need to be removed in oldNaptSwitch {}", intextIpProtocolType.getProtocol(), routerId, naptSwitch);
break;
}
List<IpPortMap> ipPortMaps = intextIpProtocolType.getIpPortMap();
for (IpPortMap ipPortMap : ipPortMaps) {
String ipPortInternal = ipPortMap.getIpPortInternal();
String[] ipPortParts = ipPortInternal.split(":");
if (ipPortParts.length != 2) {
LOG.error("removeSnatFlowsInOldNaptSwitch : Unable to retrieve the Internal IP and port");
continue;
}
String internalIp = ipPortParts[0];
String internalPort = ipPortParts[1];
// Build and remove flow in outbound NAPT table
String switchFlowRef = NatUtil.getNaptFlowRef(naptSwitch, NwConstants.OUTBOUND_NAPT_TABLE, String.valueOf(routerId), internalIp, Integer.parseInt(internalPort));
FlowEntity outboundNaptFlowEntity = NatUtil.buildFlowEntity(naptSwitch, NwConstants.OUTBOUND_NAPT_TABLE, cookieSnatFlow, switchFlowRef);
LOG.info("removeSnatFlowsInOldNaptSwitch : Remove the flow in table {} for old napt switch " + "with the DPN ID {} and router ID {}", NwConstants.OUTBOUND_NAPT_TABLE, naptSwitch, routerId);
mdsalManager.removeFlowToTx(outboundNaptFlowEntity, removeFlowInvTx);
IpPortExternal ipPortExternal = ipPortMap.getIpPortExternal();
if (ipPortExternal == null) {
LOG.debug("removeSnatFlowsInOldNaptSwitch : External Ipport mapping not found for internalIp {} " + "with port {} for router {}", internalIp, internalPort, routerId);
continue;
}
String externalIp = ipPortExternal.getIpAddress();
int externalPort = ipPortExternal.getPortNum();
// Build and remove flow in inbound NAPT table
switchFlowRef = NatUtil.getNaptFlowRef(naptSwitch, NwConstants.INBOUND_NAPT_TABLE, String.valueOf(routerId), externalIp, externalPort);
FlowEntity inboundNaptFlowEntity = NatUtil.buildFlowEntity(naptSwitch, NwConstants.INBOUND_NAPT_TABLE, cookieSnatFlow, switchFlowRef);
LOG.info("removeSnatFlowsInOldNaptSwitch : Remove the flow in table {} for old napt switch with the " + "DPN ID {} and router ID {}", NwConstants.INBOUND_NAPT_TABLE, naptSwitch, routerId);
mdsalManager.removeFlowToTx(inboundNaptFlowEntity, removeFlowInvTx);
}
}
}
Aggregations