X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=natservice%2Fimpl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetvirt%2Fnatservice%2Finternal%2FConntrackBasedSnatService.java;h=ab3e0a16b472b6b4bd32072c9b1b2b0e0178bf34;hb=5df4473cb0fbd4aa6b72d213bca1f2b6b1f0ea81;hp=ac5dc3c7f6d02eacbe2e41b8477cc5b16b1a4340;hpb=cd475ebf0c30c166aca780b398a784bb91d8e419;p=netvirt.git diff --git a/natservice/impl/src/main/java/org/opendaylight/netvirt/natservice/internal/ConntrackBasedSnatService.java b/natservice/impl/src/main/java/org/opendaylight/netvirt/natservice/internal/ConntrackBasedSnatService.java index ac5dc3c7f6..ab3e0a16b4 100644 --- a/natservice/impl/src/main/java/org/opendaylight/netvirt/natservice/internal/ConntrackBasedSnatService.java +++ b/natservice/impl/src/main/java/org/opendaylight/netvirt/natservice/internal/ConntrackBasedSnatService.java @@ -11,8 +11,13 @@ import com.google.common.base.Optional; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ExecutionException; import org.apache.commons.lang3.tuple.ImmutablePair; import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.genius.datastoreutils.listeners.DataTreeEventCallbackRegistrar; +import org.opendaylight.genius.infra.Datastore.Configuration; +import org.opendaylight.genius.infra.TypedReadWriteTransaction; +import org.opendaylight.genius.infra.TypedWriteTransaction; import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager; import org.opendaylight.genius.mdsalutil.ActionInfo; import org.opendaylight.genius.mdsalutil.InstructionInfo; @@ -23,6 +28,7 @@ import org.opendaylight.genius.mdsalutil.NWUtil; import org.opendaylight.genius.mdsalutil.NwConstants; import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack; import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack.NxCtAction; +import org.opendaylight.genius.mdsalutil.actions.ActionNxCtClear; import org.opendaylight.genius.mdsalutil.actions.ActionNxLoadInPort; import org.opendaylight.genius.mdsalutil.actions.ActionNxLoadMetadata; import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit; @@ -32,9 +38,9 @@ import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager; import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType; import org.opendaylight.genius.mdsalutil.matches.MatchIpv4Destination; import org.opendaylight.genius.mdsalutil.matches.MatchMetadata; -import org.opendaylight.genius.mdsalutil.matches.MatchTunnelId; import org.opendaylight.genius.mdsalutil.nxmatches.NxMatchCtState; import org.opendaylight.netvirt.fibmanager.api.IFibManager; +import org.opendaylight.netvirt.natservice.ha.NatDataUtil; import org.opendaylight.netvirt.vpnmanager.api.IVpnFootprintService; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress; import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService; @@ -44,9 +50,11 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers; import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps; import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.external.subnets.Subnets; -import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.types.rev160517.IpPrefixOrAddress; +import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.types.rev160517.IpPrefixOrAddressBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxActionNatFlags; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxActionNatRangePresent; +import org.opendaylight.yangtools.yang.common.Uint32; +import org.opendaylight.yangtools.yang.common.Uint64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -64,67 +72,117 @@ public abstract class ConntrackBasedSnatService extends AbstractSnatService { IdManagerService idManager, NAPTSwitchSelector naptSwitchSelector, OdlInterfaceRpcService odlInterfaceRpcService, IInterfaceManager interfaceManager, IVpnFootprintService vpnFootprintService, - IFibManager fibManager) { + IFibManager fibManager, NatDataUtil natDataUtil, + DataTreeEventCallbackRegistrar eventCallbacks) { super(dataBroker, mdsalManager, itmManager, odlInterfaceRpcService, idManager, naptSwitchSelector, - interfaceManager, vpnFootprintService, fibManager); + interfaceManager, vpnFootprintService, fibManager, natDataUtil, eventCallbacks); } @Override - protected void installSnatSpecificEntriesForNaptSwitch(Routers routers, BigInteger dpnId, int addOrRemove) { + protected void addSnatSpecificEntriesForNaptSwitch(TypedReadWriteTransaction confTx, + Routers routers, Uint64 dpnId) { LOG.info("installSnatSpecificEntriesForNaptSwitch: called for router {}", - routers.getRouterName()); + routers.getRouterName()); String routerName = routers.getRouterName(); - Long routerId = NatUtil.getVpnId(getDataBroker(), routerName); - int elanId = NatUtil.getElanInstanceByName(routers.getNetworkId().getValue(), getDataBroker()) - .getElanTag().intValue(); + Uint32 routerId = NatUtil.getVpnId(confTx, routerName); + int elanId = NatUtil.getElanInstanceByName(confTx, routers.getNetworkId().getValue()) + .getElanTag().intValue(); + if (routerId == NatConstants.INVALID_ID) { + LOG.error("InvalidRouterId: unable to installSnatSpecificEntriesForNaptSwitch on dpn {}", dpnId); + return; + } /* Install Outbound NAT entries */ - installSnatMissEntryForPrimrySwch(dpnId, routerId, elanId, addOrRemove); - installTerminatingServiceTblEntry(dpnId, routerId, elanId, addOrRemove); + addSnatMissEntryForPrimrySwch(confTx, dpnId, routerId, elanId); - String extGwMacAddress = NatUtil.getExtGwMacAddFromRouterName(getDataBroker(), routerName); - createOutboundTblTrackEntry(dpnId, routerId, extGwMacAddress, addOrRemove); - for (ExternalIps externalIp : routers.getExternalIps()) { + String extGwMacAddress = NatUtil.getExtGwMacAddFromRouterName(confTx, routerName); + addOutboundTblTrackEntry(confTx, dpnId, routerId, extGwMacAddress); + for (ExternalIps externalIp : routers.nonnullExternalIps()) { if (!NWUtil.isIpv4Address(externalIp.getIpAddress())) { // In this class we handle only IPv4 use-cases. continue; } //The logic now handle only one external IP per router, others if present will be ignored. - long extSubnetId = NatConstants.INVALID_ID; - if (addOrRemove == NwConstants.ADD_FLOW) { - extSubnetId = NatUtil.getExternalSubnetVpnId(getDataBroker(), externalIp.getSubnetId()); + Uint32 extSubnetId = NatUtil.getExternalSubnetVpnId(confTx, externalIp.getSubnetId()); + addOutboundTblEntry(confTx, dpnId, routerId, externalIp.getIpAddress(), elanId, extGwMacAddress); + addNaptPfibFlow(confTx, routers, dpnId, routerId, extSubnetId); + + //Install Inbound NAT entries + addInboundEntry(confTx, dpnId, routerId, externalIp.getIpAddress(), elanId, extSubnetId); + addNaptPfibEntry(confTx, dpnId, routerId); + + String fibExternalIp = NatUtil.validateAndAddNetworkMask(externalIp.getIpAddress()); + Optional externalSubnet = NatUtil.getOptionalExternalSubnets(confTx, externalIp.getSubnetId()); + if (externalSubnet.isPresent()) { + String externalVpn = externalIp.getSubnetId().getValue(); + String vpnRd = NatUtil.getVpnRd(confTx, externalVpn); + vpnFootprintService.updateVpnToDpnMapping(dpnId, externalVpn, vpnRd, + null /* interfaceName*/, new ImmutablePair<>(IpAddresses + .IpAddressSource.ExternalFixedIP, fibExternalIp), true); } - createOutboundTblEntry(dpnId, routerId, externalIp.getIpAddress(), elanId, extGwMacAddress, addOrRemove); - installNaptPfibFlow(routers, dpnId, routerId, extSubnetId, addOrRemove); + break; + } + } + + @Override + protected void removeSnatSpecificEntriesForNaptSwitch(TypedReadWriteTransaction confTx, + Routers routers, Uint64 dpnId) throws ExecutionException, InterruptedException { + LOG.info("installSnatSpecificEntriesForNaptSwitch: called for router {}", + routers.getRouterName()); + String routerName = routers.getRouterName(); + Uint32 routerId = NatUtil.getVpnId(confTx, routerName); + if (routerId == NatConstants.INVALID_ID) { + LOG.error("InvalidRouterId: unable to installSnatSpecificEntriesForNaptSwitch on dpn {}", dpnId); + return; + } + /* Remove Outbound NAT entries */ + + removeSnatMissEntryForPrimrySwch(confTx, dpnId, routerId); + + removeOutboundTblTrackEntry(confTx, dpnId, routerId); + for (ExternalIps externalIp : routers.nonnullExternalIps()) { + if (!NWUtil.isIpv4Address(externalIp.getIpAddress())) { + // In this class we handle only IPv4 use-cases. + continue; + } + //The logic now handle only one external IP per router, others if present will be ignored. + removeOutboundTblEntry(confTx, dpnId, routerId); + removeNaptPfibFlow(confTx, routers, dpnId, routerId); //Install Inbound NAT entries - installInboundEntry(dpnId, routerId, externalIp.getIpAddress(), elanId, extSubnetId, addOrRemove); - installNaptPfibEntry(dpnId, routerId, addOrRemove); + removeInboundEntry(confTx, dpnId, routerId); + removeNaptPfibEntry(confTx, dpnId, routerId); String fibExternalIp = NatUtil.validateAndAddNetworkMask(externalIp.getIpAddress()); - Optional externalSubnet = NatUtil.getOptionalExternalSubnets(dataBroker, externalIp.getSubnetId()); + Optional externalSubnet = NatUtil.getOptionalExternalSubnets(confTx, externalIp.getSubnetId()); if (externalSubnet.isPresent()) { String externalVpn = externalIp.getSubnetId().getValue(); - String vpnRd = NatUtil.getVpnRd(dataBroker, externalVpn); + String vpnRd = NatUtil.getVpnRd(confTx, externalVpn); vpnFootprintService.updateVpnToDpnMapping(dpnId, externalVpn, vpnRd, null /* interfaceName*/, - new ImmutablePair<>(IpAddresses.IpAddressSource.ExternalFixedIP, fibExternalIp), - addOrRemove == NwConstants.ADD_FLOW); + new ImmutablePair<>(IpAddresses.IpAddressSource.ExternalFixedIP, fibExternalIp), + false); } break; } } @Override - protected void installSnatSpecificEntriesForNonNaptSwitch(Routers routers, BigInteger dpnId, int addOrRemove) { - // Nothing to to do here. + protected void addSnatSpecificEntriesForNonNaptSwitch() { + // Nothing to to do here + } + @Override + protected void removeSnatSpecificEntriesForNonNaptSwitch() { + // Nothing to to do here } - protected void installSnatMissEntryForPrimrySwch(BigInteger dpnId, Long routerId, int elanId, int addOrRemove) { + protected void addSnatMissEntryForPrimrySwch(TypedWriteTransaction confTx, Uint64 dpnId, + Uint32 routerId, int elanId) { LOG.info("installSnatSpecificEntriesForNaptSwitch : called for the primary NAPT switch dpnId {}", dpnId); List matches = new ArrayList<>(); matches.add(MatchEthernetType.IPV4); - matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID)); + matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId.longValue()), + MetaDataUtil.METADATA_MASK_VRFID)); List instructions = new ArrayList<>(); List actionsInfos = new ArrayList<>(); List ctActionsList = new ArrayList<>(); @@ -137,165 +195,180 @@ public abstract class ConntrackBasedSnatService extends AbstractSnatService { instructions.add(new InstructionApplyActions(actionsInfos)); String flowRef = getFlowRef(dpnId, NwConstants.PSNAT_TABLE, routerId); - syncFlow(dpnId, NwConstants.PSNAT_TABLE, flowRef, NatConstants.DEFAULT_PSNAT_FLOW_PRIORITY, flowRef, - NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove); + NatUtil.addFlow(confTx, mdsalManager, dpnId, NwConstants.PSNAT_TABLE, flowRef, + NatConstants.DEFAULT_PSNAT_FLOW_PRIORITY, flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, + instructions); } - protected void installTerminatingServiceTblEntry(BigInteger dpnId, Long routerId, int elanId, int addOrRemove) { - LOG.info("installTerminatingServiceTblEntry : creating entry for Terminating Service Table " - + "for switch {}, routerId {}", dpnId, routerId); - List matches = new ArrayList<>(); - matches.add(MatchEthernetType.IPV4); - matches.add(new MatchTunnelId(BigInteger.valueOf(routerId))); - - - List actionsInfos = new ArrayList<>(); - List ctActionsList = new ArrayList<>(); - NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0); - ctActionsList.add(nxCtAction); - ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants - .OUTBOUND_NAPT_TABLE,ctActionsList); - ActionNxLoadMetadata actionLoadMeta = new ActionNxLoadMetadata(MetaDataUtil - .getVpnIdMetadata(routerId.longValue()), LOAD_START, LOAD_END); - actionsInfos.add(actionLoadMeta); - actionsInfos.add(actionNxConntrack); - List instructions = new ArrayList<>(); - instructions.add(new InstructionApplyActions(actionsInfos)); - String flowRef = getFlowRef(dpnId, NwConstants.INTERNAL_TUNNEL_TABLE, routerId.longValue()); - syncFlow(dpnId, NwConstants.INTERNAL_TUNNEL_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef, - NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove); + protected void removeSnatMissEntryForPrimrySwch(TypedReadWriteTransaction confTx, Uint64 dpnId, + Uint32 routerId) throws ExecutionException, InterruptedException { + LOG.info("installSnatSpecificEntriesForNaptSwitch : called for the primary NAPT switch dpnId {}", dpnId); + String flowRef = getFlowRef(dpnId, NwConstants.PSNAT_TABLE, routerId); + NatUtil.removeFlow(confTx, mdsalManager, dpnId, NwConstants.PSNAT_TABLE, flowRef); } - protected void createOutboundTblTrackEntry(BigInteger dpnId, Long routerId, String extGwMacAddress, - int addOrRemove) { + protected void addOutboundTblTrackEntry(TypedWriteTransaction confTx, Uint64 dpnId, + Uint32 routerId, String extGwMacAddress) { LOG.info("createOutboundTblTrackEntry : called for switch {}, routerId {}", dpnId, routerId); List matches = new ArrayList<>(); matches.add(MatchEthernetType.IPV4); matches.add(new NxMatchCtState(SNAT_CT_STATE, SNAT_CT_STATE_MASK)); - matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID)); + matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId.longValue()), + MetaDataUtil.METADATA_MASK_VRFID)); ArrayList listActionInfo = new ArrayList<>(); - if (addOrRemove == NwConstants.ADD_FLOW) { - listActionInfo.add(new ActionSetFieldEthernetSource(new MacAddress(extGwMacAddress))); - } + listActionInfo.add(new ActionSetFieldEthernetSource(new MacAddress(extGwMacAddress))); ArrayList instructionInfo = new ArrayList<>(); listActionInfo.add(new ActionNxResubmit(NwConstants.NAPT_PFIB_TABLE)); instructionInfo.add(new InstructionApplyActions(listActionInfo)); - String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId); - flowRef += "trkest"; - syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY, flowRef, - NwConstants.COOKIE_SNAT_TABLE, matches, instructionInfo, addOrRemove); + String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId) + "trkest"; + NatUtil.addFlow(confTx, mdsalManager, dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, + NatConstants.SNAT_TRK_FLOW_PRIORITY, flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, + instructionInfo); + } + + protected void removeOutboundTblTrackEntry(TypedReadWriteTransaction confTx, Uint64 dpnId, + Uint32 routerId) throws ExecutionException, InterruptedException { + LOG.info("createOutboundTblTrackEntry : called for switch {}, routerId {}", dpnId, routerId); + String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId) + "trkest"; + NatUtil.removeFlow(confTx, mdsalManager, dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef); } - protected void createOutboundTblEntry(BigInteger dpnId, long routerId, String externalIp, - int elanId, String extGwMacAddress, int addOrRemove) { + protected void addOutboundTblEntry(TypedWriteTransaction confTx, Uint64 dpnId, Uint32 routerId, + String externalIp, int elanId, String extGwMacAddress) { LOG.info("createOutboundTblEntry : dpId {} and routerId {}", dpnId, routerId); List 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)); + matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId.longValue()), + MetaDataUtil.METADATA_MASK_VRFID)); List actionsInfos = new ArrayList<>(); - if (addOrRemove == NwConstants.ADD_FLOW) { - actionsInfos.add(new ActionSetFieldEthernetSource(new MacAddress(extGwMacAddress))); - } + actionsInfos.add(new ActionSetFieldEthernetSource(new MacAddress(extGwMacAddress))); List ctActionsListCommit = new ArrayList<>(); int rangePresent = NxActionNatRangePresent.NXNATRANGEIPV4MIN.getIntValue(); int flags = NxActionNatFlags.NXNATFSRC.getIntValue(); NxCtAction nxCtActionCommit = new ActionNxConntrack.NxNat(0, flags, rangePresent, - new IpPrefixOrAddress(externalIp.toCharArray()).getIpAddress(), - null,0, 0); + IpPrefixOrAddressBuilder.getDefaultInstance(externalIp).getIpAddress(), null,0, 0); ctActionsListCommit.add(nxCtActionCommit); int ctCommitFlag = 1; ActionNxConntrack actionNxConntrackSubmit = new ActionNxConntrack(ctCommitFlag, 0, elanId, - NwConstants.NAPT_PFIB_TABLE, ctActionsListCommit); + NwConstants.NAPT_PFIB_TABLE, ctActionsListCommit); actionsInfos.add(actionNxConntrackSubmit); List 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); + NatUtil.addFlow(confTx, mdsalManager, dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, + NatConstants.SNAT_NEW_FLOW_PRIORITY, flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions); + } + + protected void removeOutboundTblEntry(TypedReadWriteTransaction confTx, Uint64 dpnId, + Uint32 routerId) throws ExecutionException, InterruptedException { + LOG.info("createOutboundTblEntry : dpId {} and routerId {}", dpnId, routerId); + String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId); + NatUtil.removeFlow(confTx, mdsalManager, dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef); } - protected void installNaptPfibFlow(Routers routers, BigInteger dpnId, long routerId, - long extSubnetId, int addOrRemove) { - Long extNetId = NatUtil.getVpnId(getDataBroker(), routers.getNetworkId().getValue()); + protected void addNaptPfibFlow(TypedReadWriteTransaction confTx, Routers routers, Uint64 dpnId, + Uint32 routerId, Uint32 extSubnetId) { + Uint32 extNetId = NatUtil.getVpnId(confTx, routers.getNetworkId().getValue()); LOG.info("installNaptPfibFlow : dpId {}, extNetId {}", dpnId, extNetId); List matches = new ArrayList<>(); matches.add(MatchEthernetType.IPV4); matches.add(new NxMatchCtState(SNAT_CT_STATE, SNAT_CT_STATE_MASK)); - matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID)); + matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId.longValue()), + MetaDataUtil.METADATA_MASK_VRFID)); List listActionInfo = new ArrayList<>(); - if (addOrRemove == NwConstants.ADD_FLOW) { - if (extSubnetId == NatConstants.INVALID_ID) { - LOG.error("installNaptPfibFlow : external subnet id is invalid."); - return; - } - ActionNxLoadMetadata actionLoadMeta = new ActionNxLoadMetadata(MetaDataUtil - .getVpnIdMetadata(extSubnetId), LOAD_START, LOAD_END); - listActionInfo.add(actionLoadMeta); + if (extSubnetId == NatConstants.INVALID_ID) { + LOG.error("installNaptPfibFlow : external subnet id is invalid."); + return; } - ArrayList instructions = new ArrayList<>(); - listActionInfo.add(new ActionNxLoadInPort(BigInteger.ZERO)); + ActionNxLoadMetadata actionLoadMeta = new ActionNxLoadMetadata(MetaDataUtil + .getVpnIdMetadata(extSubnetId.longValue()), LOAD_START, LOAD_END); + listActionInfo.add(actionLoadMeta); + listActionInfo.add(new ActionNxLoadInPort(Uint64.valueOf(BigInteger.ZERO))); + listActionInfo.add(new ActionNxCtClear()); listActionInfo.add(new ActionNxResubmit(NwConstants.L3_FIB_TABLE)); + ArrayList instructions = new ArrayList<>(); instructions.add(new InstructionApplyActions(listActionInfo)); String flowRef = getFlowRef(dpnId, NwConstants.NAPT_PFIB_TABLE, routerId); flowRef = flowRef + "OUTBOUND"; - syncFlow(dpnId, NwConstants.NAPT_PFIB_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY, - flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove); + NatUtil.addFlow(confTx, mdsalManager, dpnId, NwConstants.NAPT_PFIB_TABLE, flowRef, + NatConstants.SNAT_TRK_FLOW_PRIORITY, flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions); + } + + protected void removeNaptPfibFlow(TypedReadWriteTransaction confTx, Routers routers, + Uint64 dpnId, Uint32 routerId) throws ExecutionException, InterruptedException { + Uint32 extNetId = NatUtil.getVpnId(confTx, routers.getNetworkId().getValue()); + LOG.info("installNaptPfibFlow : dpId {}, extNetId {}", dpnId, extNetId); + String flowRef = getFlowRef(dpnId, NwConstants.NAPT_PFIB_TABLE, routerId) + "OUTBOUND"; + NatUtil.removeFlow(confTx, mdsalManager, dpnId, NwConstants.NAPT_PFIB_TABLE, flowRef); } - protected void installInboundEntry(BigInteger dpnId, long routerId, String externalIp, int elanId, long extSubnetId, - int addOrRemove) { + protected void addInboundEntry(TypedWriteTransaction confTx, Uint64 dpnId, Uint32 routerId, + String externalIp, int elanId, Uint32 extSubnetId) { LOG.info("installInboundEntry : dpId {} and routerId {}", dpnId, routerId); List matches = new ArrayList<>(); matches.add(MatchEthernetType.IPV4); matches.add(new MatchIpv4Destination(externalIp,"32")); - if (addOrRemove == NwConstants.ADD_FLOW) { - if (extSubnetId == NatConstants.INVALID_ID) { - LOG.error("installInboundEntry : external subnet id is invalid."); - return; - } - matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(extSubnetId), - MetaDataUtil.METADATA_MASK_VRFID)); + if (extSubnetId == NatConstants.INVALID_ID) { + LOG.error("installInboundEntry : external subnet id is invalid."); + return; } + matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(extSubnetId.longValue()), + MetaDataUtil.METADATA_MASK_VRFID)); List actionsInfos = new ArrayList<>(); List ctActionsList = new ArrayList<>(); NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0); ActionNxLoadMetadata actionLoadMeta = new ActionNxLoadMetadata(MetaDataUtil - .getVpnIdMetadata(routerId), LOAD_START, LOAD_END); + .getVpnIdMetadata(routerId.longValue()), LOAD_START, LOAD_END); actionsInfos.add(actionLoadMeta); ctActionsList.add(nxCtAction); ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants - .NAPT_PFIB_TABLE,ctActionsList); + .NAPT_PFIB_TABLE,ctActionsList); actionsInfos.add(actionNxConntrack); List instructions = new ArrayList<>(); instructions.add(new InstructionApplyActions(actionsInfos)); String flowRef = getFlowRef(dpnId, NwConstants.INBOUND_NAPT_TABLE, routerId); flowRef = flowRef + "OUTBOUND"; - syncFlow(dpnId, NwConstants.INBOUND_NAPT_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef, - NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove); + NatUtil.addFlow(confTx, mdsalManager, dpnId, NwConstants.INBOUND_NAPT_TABLE, flowRef, + NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions); + } + + protected void removeInboundEntry(TypedReadWriteTransaction confTx, Uint64 dpnId, + Uint32 routerId) throws ExecutionException, InterruptedException { + LOG.info("installInboundEntry : dpId {} and routerId {}", dpnId, routerId); + + String flowRef = getFlowRef(dpnId, NwConstants.INBOUND_NAPT_TABLE, routerId) + "OUTBOUND"; + NatUtil.removeFlow(confTx, mdsalManager, dpnId, NwConstants.INBOUND_NAPT_TABLE, flowRef); } - protected void installNaptPfibEntry(BigInteger dpnId, long routerId, int addOrRemove) { + protected void addNaptPfibEntry(TypedWriteTransaction confTx, Uint64 dpnId, Uint32 routerId) { LOG.info("installNaptPfibEntry : called for dpnId {} and routerId {} ", dpnId, routerId); List matches = new ArrayList<>(); matches.add(MatchEthernetType.IPV4); matches.add(new NxMatchCtState(DNAT_CT_STATE, DNAT_CT_STATE_MASK)); - matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID)); + matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId.longValue()), + MetaDataUtil.METADATA_MASK_VRFID)); ArrayList listActionInfo = new ArrayList<>(); ArrayList instructionInfo = new ArrayList<>(); - listActionInfo.add(new ActionNxLoadInPort(BigInteger.ZERO)); + listActionInfo.add(new ActionNxLoadInPort(Uint64.valueOf(BigInteger.ZERO))); listActionInfo.add(new ActionNxResubmit(NwConstants.L3_FIB_TABLE)); instructionInfo.add(new InstructionApplyActions(listActionInfo)); + String flowRef = getFlowRef(dpnId, NwConstants.NAPT_PFIB_TABLE, routerId) + "INBOUND"; + NatUtil.addFlow(confTx, mdsalManager, dpnId, NwConstants.NAPT_PFIB_TABLE, flowRef, + NatConstants.DEFAULT_PSNAT_FLOW_PRIORITY, flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, + instructionInfo); + } - String flowRef = getFlowRef(dpnId, NwConstants.NAPT_PFIB_TABLE, routerId); - flowRef = flowRef + "INBOUND"; - syncFlow(dpnId, NwConstants.NAPT_PFIB_TABLE, flowRef, NatConstants.DEFAULT_PSNAT_FLOW_PRIORITY, flowRef, - NwConstants.COOKIE_SNAT_TABLE, matches, instructionInfo, addOrRemove); + protected void removeNaptPfibEntry(TypedReadWriteTransaction confTx, Uint64 dpnId, + Uint32 routerId) throws ExecutionException, InterruptedException { + LOG.info("installNaptPfibEntry : called for dpnId {} and routerId {} ", dpnId, routerId); + String flowRef = getFlowRef(dpnId, NwConstants.NAPT_PFIB_TABLE, routerId) + "INBOUND"; + NatUtil.removeFlow(confTx, mdsalManager, dpnId, NwConstants.NAPT_PFIB_TABLE, flowRef); } }