Merge "Add ovsdb/netvirt ui to vpnservice netvirt code."
[netvirt.git] / vpnservice / natservice / natservice-impl / src / main / java / org / opendaylight / netvirt / natservice / internal / VpnFloatingIpHandler.java
index 89a8344f8849a228f60a71de07a6aec47c69347e..73b247cac647c6e72a8bfec63c818a631d87b8c4 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.netvirt.natservice.internal;
 
 import java.math.BigInteger;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.concurrent.Future;
 import java.util.List;
 
@@ -23,6 +24,8 @@ import org.opendaylight.genius.mdsalutil.MatchFieldType;
 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;
@@ -55,10 +58,10 @@ public class VpnFloatingIpHandler implements FloatingIPHandler {
     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;
@@ -78,10 +81,18 @@ public class VpnFloatingIpHandler implements FloatingIPHandler {
         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);
@@ -99,24 +110,25 @@ public class VpnFloatingIpHandler implements FloatingIPHandler {
                     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);
@@ -151,7 +163,7 @@ public class VpnFloatingIpHandler implements FloatingIPHandler {
     @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);
@@ -159,7 +171,7 @@ public class VpnFloatingIpHandler implements FloatingIPHandler {
         }
         //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);
@@ -203,20 +215,10 @@ public class VpnFloatingIpHandler implements FloatingIPHandler {
         });
     }
 
-    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
 
@@ -224,7 +226,7 @@ public class VpnFloatingIpHandler implements FloatingIPHandler {
         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
@@ -270,7 +272,7 @@ public class VpnFloatingIpHandler implements FloatingIPHandler {
 
     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,
@@ -282,7 +284,7 @@ public class VpnFloatingIpHandler implements FloatingIPHandler {
     }
 
     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);
 
@@ -296,13 +298,13 @@ public class VpnFloatingIpHandler implements FloatingIPHandler {
     }
 
     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);
@@ -313,7 +315,7 @@ public class VpnFloatingIpHandler implements FloatingIPHandler {
 
         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);
 
@@ -321,7 +323,7 @@ public class VpnFloatingIpHandler implements FloatingIPHandler {
     }
 
     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)}));
@@ -332,11 +334,15 @@ public class VpnFloatingIpHandler implements FloatingIPHandler {
 
         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;
+    }
+}