import java.math.BigInteger;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.concurrent.Future;
import java.util.List;
import org.opendaylight.genius.mdsalutil.MatchInfo;
import org.opendaylight.genius.mdsalutil.NwConstants;
import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
+import org.opendaylight.netvirt.fibmanager.api.IFibManager;
+import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
private DataBroker dataBroker;
private IMdsalApiManager mdsalManager;
private FloatingIPListener listener;
+ private IFibManager fibManager;
static final BigInteger COOKIE_TUNNEL = new BigInteger("9000000", 16);
static final String FLOWID_PREFIX = "NAT.";
- static final BigInteger COOKIE_VM_LFIB_TABLE = new BigInteger("8000002", 16);
public VpnFloatingIpHandler(VpnRpcService vpnService, IBgpManager bgpManager, FibRpcService fibService) {
this.vpnService = vpnService;
this.mdsalManager = mdsalManager;
}
+ void setFibManager(IFibManager fibManager) {
+ this.fibManager = fibManager;
+ }
+
+ void setBgpManager(IBgpManager bgpManager) {
+ this.bgpManager = bgpManager;
+ }
+
@Override
public void onAddFloatingIp(final BigInteger dpnId, final String routerId,
Uuid networkId, final String interfaceName, final String externalIp, final String internalIp) {
- final String vpnName = NatUtil.getAssociatedVPN(dataBroker, networkId, LOG);
+ final String vpnName = getAssociatedVPN(networkId, routerId);
if(vpnName == null) {
LOG.info("No VPN associated with ext nw {} to handle add floating ip configuration {} in router {}",
networkId, externalIp, routerId);
GenerateVpnLabelOutput output = result.getResult();
long label = output.getLabel();
LOG.debug("Generated label {} for prefix {}", label, externalIp);
- listener.updateOperationalDS(routerId, interfaceName, (int)label, internalIp, externalIp);
+ listener.updateOperationalDS(routerId, interfaceName, label, internalIp, externalIp);
//Inform BGP
String rd = NatUtil.getVpnRd(dataBroker, vpnName);
String nextHopIp = NatUtil.getEndpointIpAddressForDPN(dataBroker, dpnId);
LOG.debug("Nexthop ip for prefix {} is {}", externalIp, nextHopIp);
- NatUtil.addPrefixToBGP(bgpManager, rd, externalIp + "/32", nextHopIp, label, LOG);
+ NatUtil.addPrefixToBGP(dataBroker, bgpManager, fibManager, rd, externalIp + "/32", nextHopIp,
+ label, LOG, RouteOrigin.STATIC);
- List<Instruction> instructions = new ArrayList<Instruction>();
- List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
- actionsInfos.add(new ActionInfo(ActionType.nx_resubmit, new String[] { Integer.toString(NatConstants.PDNAT_TABLE) }));
+ List<Instruction> instructions = new ArrayList<>();
+ List<ActionInfo> actionsInfos = new ArrayList<>();
+ actionsInfos.add(new ActionInfo(ActionType.nx_resubmit, new String[] { Integer.toString(NwConstants.PDNAT_TABLE) }));
instructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos).buildInstruction(0));
makeTunnelTableEntry(dpnId, label, instructions);
//Install custom FIB routes
List<Instruction> customInstructions = new ArrayList<>();
- customInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NatConstants.PDNAT_TABLE }).buildInstruction(0));
- makeLFibTableEntry(dpnId, label, NatConstants.PDNAT_TABLE);
+ customInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.PDNAT_TABLE }).buildInstruction(0));
+ makeLFibTableEntry(dpnId, label, NwConstants.PDNAT_TABLE);
CreateFibEntryInput input = new CreateFibEntryInputBuilder().setVpnName(vpnName).setSourceDpid(dpnId).setInstruction(customInstructions)
.setIpAddress(externalIp + "/32").setServiceId(label).setInstruction(customInstructions).build();
//Future<RpcResult<java.lang.Void>> createFibEntry(CreateFibEntryInput input);
@Override
public void onRemoveFloatingIp(final BigInteger dpnId, String routerId, Uuid networkId, final String externalIp,
String internalIp, final long label) {
- final String vpnName = NatUtil.getAssociatedVPN(dataBroker, networkId, LOG);
+ final String vpnName = getAssociatedVPN(networkId, routerId);
if(vpnName == null) {
LOG.info("No VPN associated with ext nw {} to handle remove floating ip configuration {} in router {}",
networkId, externalIp, routerId);
}
//Remove Prefix from BGP
String rd = NatUtil.getVpnRd(dataBroker, vpnName);
- removePrefixFromBGP(rd, externalIp + "/32");
+ NatUtil.removePrefixFromBGP(dataBroker, bgpManager, fibManager, rd, externalIp + "/32", LOG);
//Remove custom FIB routes
//Future<RpcResult<java.lang.Void>> removeFibEntry(RemoveFibEntryInput input);
});
}
- private void removePrefixFromBGP(String rd, String prefix) {
- try {
- LOG.info("VPN REMOVE: Removing Fib Entry rd {} prefix {}", rd, prefix);
- bgpManager.deletePrefix(rd, prefix);
- LOG.info("VPN REMOVE: Removed Fib Entry rd {} prefix {}", rd, prefix);
- } catch(Exception e) {
- LOG.error("Delete prefix failed", e);
- }
- }
-
void cleanupFibEntries(final BigInteger dpnId, final String vpnName, final String externalIp, final long label ) {
//Remove Prefix from BGP
String rd = NatUtil.getVpnRd(dataBroker, vpnName);
- removePrefixFromBGP(rd, externalIp + "/32");
+ NatUtil.removePrefixFromBGP(dataBroker, bgpManager, fibManager, rd, externalIp + "/32", LOG);
//Remove custom FIB routes
RemoveFibEntryInput input = new RemoveFibEntryInputBuilder().setVpnName(vpnName).setSourceDpid(dpnId).setIpAddress(externalIp + "/32").setServiceId(label).build();
Future<RpcResult<Void>> future = fibService.removeFibEntry(input);
- ListenableFuture<RpcResult<Void>> labelFuture = Futures.transform(JdkFutureAdapters.listenInPoolThread(future),
+ ListenableFuture<RpcResult<Void>> labelFuture = Futures.transform(JdkFutureAdapters.listenInPoolThread(future),
new AsyncFunction<RpcResult<Void>, RpcResult<Void>>() {
@Override
private void removeTunnelTableEntry(BigInteger dpnId, long serviceId) {
LOG.info("remove terminatingServiceActions called with DpnId = {} and label = {}", dpnId , serviceId);
- List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
+ List<MatchInfo> mkMatches = new ArrayList<>();
// Matching metadata
mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {BigInteger.valueOf(serviceId)}));
Flow flowEntity = MDSALUtil.buildFlowNew(NwConstants.INTERNAL_TUNNEL_TABLE,
}
private void makeTunnelTableEntry(BigInteger dpnId, long serviceId, List<Instruction> customInstructions) {
- List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
+ List<MatchInfo> mkMatches = new ArrayList<>();
LOG.info("create terminatingServiceAction on DpnId = {} and serviceId = {} and actions = {}", dpnId , serviceId);
}
private void makeLFibTableEntry(BigInteger dpId, long serviceId, long tableId) {
- List<MatchInfo> matches = new ArrayList<MatchInfo>();
+ List<MatchInfo> matches = new ArrayList<>();
matches.add(new MatchInfo(MatchFieldType.eth_type,
new long[] { 0x8847L }));
matches.add(new MatchInfo(MatchFieldType.mpls_label, new String[]{Long.toString(serviceId)}));
- List<Instruction> instructions = new ArrayList<Instruction>();
- List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
+ List<Instruction> instructions = new ArrayList<>();
+ List<ActionInfo> actionsInfos = new ArrayList<>();
actionsInfos.add(new ActionInfo(ActionType.pop_mpls, new String[]{}));
Instruction writeInstruction = new InstructionInfo(InstructionType.apply_actions, actionsInfos).buildInstruction(0);
instructions.add(writeInstruction);
Flow flowEntity = MDSALUtil.buildFlowNew(NwConstants.L3_LFIB_TABLE, flowRef,
10, flowRef, 0, 0,
- COOKIE_VM_LFIB_TABLE, matches, instructions);
+ NwConstants.COOKIE_VM_LFIB_TABLE, matches, instructions);
mdsalManager.installFlow(dpId, flowEntity);
}
private void removeLFibTableEntry(BigInteger dpnId, long serviceId) {
- List<MatchInfo> matches = new ArrayList<MatchInfo>();
+ List<MatchInfo> matches = new ArrayList<>();
matches.add(new MatchInfo(MatchFieldType.eth_type,
new long[] { 0x8847L }));
matches.add(new MatchInfo(MatchFieldType.mpls_label, new String[]{Long.toString(serviceId)}));
Flow flowEntity = MDSALUtil.buildFlowNew(NwConstants.L3_LFIB_TABLE, flowRef,
10, flowRef, 0, 0,
- COOKIE_VM_LFIB_TABLE, matches, null);
+ NwConstants.COOKIE_VM_LFIB_TABLE, matches, null);
mdsalManager.removeFlow(dpnId, flowEntity);
LOG.debug("LFIB Entry for dpID : {} label : {} removed successfully {}",dpnId, serviceId);
}
-}
\ No newline at end of file
+ private String getAssociatedVPN(Uuid networkId, String routerId) {
+ String vpnName = NatUtil.getAssociatedVPN(dataBroker, networkId, LOG);
+ return vpnName != null ? vpnName : routerId;
+ }
+}