Bug 5268 - Handle l3vpn delete and recreate scenario 47/35947/2
authorDeepthi V V <deepthi.v.v@ericsson.com>
Wed, 9 Mar 2016 11:10:43 +0000 (16:40 +0530)
committerDeepthi V V <deepthi.v.v@ericsson.com>
Wed, 9 Mar 2016 13:33:55 +0000 (13:33 +0000)
-Handle switch restart and vm port down/up scenario
-Handle inter dc route deletion
-Remove usage of opstate in vpn-interfaces

Change-Id: Ia92cc62f0a68501e60591b5dd3712d9f7f866217
Signed-off-by: Deepthi V V <deepthi.v.v@ericsson.com>
(cherry picked from commit da9cc6536c6f57f7b2c548961e7adfe85ee2cdcf)

elanmanager/elanmanager-impl/src/main/java/org/opendaylight/vpnservice/elan/internal/ElanInterfaceManager.java
fibmanager/fibmanager-impl/src/main/java/org/opendaylight/vpnservice/fibmanager/FibManager.java
fibmanager/fibmanager-impl/src/main/java/org/opendaylight/vpnservice/fibmanager/FibUtil.java
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/vpnservice/mdsalutil/ActionType.java
vpnmanager/vpnmanager-impl/src/main/java/org/opendaylight/vpnservice/InterfaceStateChangeListener.java
vpnmanager/vpnmanager-impl/src/main/java/org/opendaylight/vpnservice/VpnInterfaceManager.java
vpnmanager/vpnmanager-impl/src/main/java/org/opendaylight/vpnservice/VpnUtil.java

index 5bffcfc4f6e3c1bd414c8285c2bce8636e6903f7..8dd1d769fd5b600417e502b0e114d6942b40f074 100644 (file)
@@ -884,7 +884,7 @@ public class ElanInterfaceManager extends AbstractDataChangeListener<ElanInterfa
         int actionKey = 0;
         listAction.add((new ActionInfo(ActionType.set_field_tunnel_id, new BigInteger[] {BigInteger.valueOf(interfaceInfo.getInterfaceTag())}, actionKey)).buildAction());
         actionKey++;
-        listAction.add((new ActionInfo(ActionType.nx_resubmit, new BigInteger[] {BigInteger.valueOf(55)}, actionKey)).buildAction());
+        listAction.add((new ActionInfo(ActionType.nx_resubmit, new String[] {Short.toString((short)55)}, actionKey)).buildAction());
         return listAction;
     }
 
index 493076af544cae7f6561f4de08980348302e584e..471336805194fd0edf9948466b224bef31cc1c92 100644 (file)
@@ -41,7 +41,6 @@ import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.OpState;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.PrefixToInterface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInstanceOpData;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnToExtraroute;
@@ -71,9 +70,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpc
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetTunnelTypeOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.OdlInterfaceRpcService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.l3nexthop.rev150409.l3nexthop.vpnnexthops.VpnNexthop;
-import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.Adjacencies;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.Adjacency;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
@@ -294,17 +291,12 @@ public class FibManager extends AbstractDataChangeListener<VrfEntry> implements
     if(localNextHopInfo != null) {
       localDpnId = localNextHopInfo.getDpnId();
       Prefixes prefix = getPrefixToInterface(vpnId, isExtraRoute ? localNextHopIP : vrfEntry.getDestPrefix());
-      Optional<OpState> opStateData = FibUtil.read(broker, LogicalDatastoreType.OPERATIONAL,
-                                                            FibUtil.getVpnInterfaceOpStateIdentifier(prefix.getVpnInterfaceName()));
-      if(opStateData.isPresent() && !opStateData.get().isStateUp())
-      {
         makeConnectedRoute(localDpnId, vpnId, vrfEntry, rd, null /* invalid */,
                            NwConstants.DEL_FLOW);
         makeLFibTableEntry(localDpnId, vrfEntry.getLabel(), 0 /* invalid */,
                            vrfEntry.getNextHopAddress(), NwConstants.DEL_FLOW);
         removeTunnelTableEntry(localDpnId, vrfEntry.getLabel());
         deleteLocalAdjacency(localDpnId, vpnId, localNextHopIP);
-      }
     }
     return localDpnId;
   }
@@ -428,11 +420,11 @@ public class FibManager extends AbstractDataChangeListener<VrfEntry> implements
 
             if (vpnInterfaces.remove(currVpnInterface)) {
                 if (vpnInterfaces.isEmpty()) {
-                    //FibUtil.delete(broker, LogicalDatastoreType.OPERATIONAL, id);
-                  LOG.trace("cleanUpOpDataForFib: cleanUpDpnForVpn: {}, {}", dpnId, vpnId);
-                    cleanUpDpnForVpn(dpnId, vpnId, rd);
+                  LOG.trace("Last vpn interface {} on dpn {} for vpn {}. Clean up fib in dpn", intfName, dpnId, rd);
+                  FibUtil.delete(broker, LogicalDatastoreType.OPERATIONAL, id);
+                  cleanUpDpnForVpn(dpnId, vpnId, rd);
                 } else {
-                  LOG.trace("cleanUpOpDataForFib: delete interface: {} on {}", intfName, dpnId);
+                  LOG.trace("Delete vpn interface {} from dpn {} to vpn {} list.", intfName, dpnId, rd);
                     FibUtil.delete(broker, LogicalDatastoreType.OPERATIONAL, id.child(
                             org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
                                     .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces.class,
@@ -460,35 +452,24 @@ public class FibManager extends AbstractDataChangeListener<VrfEntry> implements
       if (prefixInfo == null)
           return; //Don't have any info for this prefix (shouldn't happen); need to return
       String ifName = prefixInfo.getVpnInterfaceName();
-      Optional<OpState> opStateData = FibUtil.read(broker, LogicalDatastoreType.OPERATIONAL,
-                                                   FibUtil.getVpnInterfaceOpStateIdentifier(ifName));
-      if(opStateData.isPresent() && !opStateData.get().isStateUp()) {
-        Optional<Adjacencies>
-            optAdjacencies =
-            FibUtil.read(broker, LogicalDatastoreType.OPERATIONAL, FibUtil.getAdjListPath(ifName));
-        int numAdj = 0;
-        if (optAdjacencies.isPresent()) {
+      Optional<Adjacencies> optAdjacencies = FibUtil.read(broker, LogicalDatastoreType.OPERATIONAL, FibUtil.getAdjListPath(ifName));
+      int numAdj = 0;
+      if (optAdjacencies.isPresent()) {
           numAdj = optAdjacencies.get().getAdjacency().size();
-        }
-        LOG.trace("cleanUpOpDataForFib: remove adjacency for prefix: {} {}", vpnId,
-                  vrfEntry.getDestPrefix());
-        //remove adjacency corr to prefix
+      }
+      LOG.trace("cleanUpOpDataForFib: remove adjacency for prefix: {} {}", vpnId, vrfEntry.getDestPrefix());
+      //remove adjacency corr to prefix
+      if (numAdj > 1) {
         FibUtil.delete(broker, LogicalDatastoreType.OPERATIONAL,
-                       FibUtil.getAdjacencyIdentifier(ifName, vrfEntry.getDestPrefix()));
+                         FibUtil.getAdjacencyIdentifier(ifName, vrfEntry.getDestPrefix()));
+      }
 
-        if ((numAdj - 1) == 0) { //there are no adjacencies left for this vpn interface, clean up
+      if ((numAdj - 1) == 0) { //there are no adjacencies left for this vpn interface, clean up
           //clean up the vpn interface from DpnToVpn list
-          delIntfFromDpnToVpnList(vpnId, prefixInfo.getDpnId(), ifName, rd);
-          LOG.trace("cleanUpOpDataForFib: Delete prefix to interface and vpnInterface ");
-          FibUtil.delete(broker, LogicalDatastoreType.OPERATIONAL,
-                         FibUtil.getPrefixToInterfaceIdentifier(
-                             vpnId,
-                             (extra_route) ? vrfEntry.getNextHopAddress() + "/32"
-                                           : vrfEntry.getDestPrefix()));
+          LOG.trace("Clean up vpn interface {} from dpn {} to vpn {} list.", ifName, prefixInfo.getDpnId(), rd);
           FibUtil.delete(broker, LogicalDatastoreType.OPERATIONAL,
                          FibUtil.getVpnInterfaceIdentifier(ifName));
-        }
-      }
+       }
   }
 
   private void deleteFibEntries(final InstanceIdentifier<VrfEntry> identifier,
@@ -530,7 +511,9 @@ public class FibManager extends AbstractDataChangeListener<VrfEntry> implements
           localNextHopInfo = nextHopManager.getVpnNexthop(vpnId, extra_route.getNexthopIp());
         }
       }
-      isRemoteRoute = ((localNextHopInfo != null) && (!remoteDpnId.equals(localNextHopInfo.getDpnId())));
+      if (localNextHopInfo != null) {
+        isRemoteRoute = (!remoteDpnId.equals(localNextHopInfo.getDpnId()));
+      }
     }
     if (isRemoteRoute) {
       makeConnectedRoute(remoteDpnId, vpnId, vrfEntry, rd, null, NwConstants.DEL_FLOW);
@@ -646,12 +629,33 @@ public class FibManager extends AbstractDataChangeListener<VrfEntry> implements
   public void populateFibOnNewDpn(BigInteger dpnId, long vpnId, String rd) {
     LOG.trace("New dpn {} for vpn {} : populateFibOnNewDpn", dpnId, rd);
     InstanceIdentifier<VrfTables> id = buildVrfId(rd);
-    Optional<VrfTables> vrfTable = FibUtil.read(broker, LogicalDatastoreType.CONFIGURATION, id);
-    if(vrfTable.isPresent()) {
-      for(VrfEntry vrfEntry : vrfTable.get().getVrfEntry()) {
-        // Passing null as we don't know the dpn
-        // to which prefix is attached at this point
-        createRemoteFibEntry(null, dpnId, vpnId, vrfTable.get().getKey(), vrfEntry);
+    String lockOnDpnVpn = new String(dpnId.toString()+ vpnId);
+    synchronized (lockOnDpnVpn.intern()) {
+      Optional<VrfTables> vrfTable = FibUtil.read(broker, LogicalDatastoreType.CONFIGURATION, id);
+      if (vrfTable.isPresent()) {
+        for (VrfEntry vrfEntry : vrfTable.get().getVrfEntry()) {
+          // Passing null as we don't know the dpn
+          // to which prefix is attached at this point
+          createRemoteFibEntry(null, dpnId, vpnId, vrfTable.get().getKey(), vrfEntry);
+        }
+      }
+    }
+  }
+
+  public void populateFibOnDpn(BigInteger dpnId, long vpnId, String rd, String nexthopIp) {
+    LOG.trace("dpn {} for vpn {}, nexthopIp {} : populateFibOnDpn", dpnId, rd, nexthopIp);
+    InstanceIdentifier<VrfTables> id = buildVrfId(rd);
+    String lockOnDpnVpn = new String(dpnId.toString()+ vpnId);
+    synchronized (lockOnDpnVpn.intern()) {
+      Optional<VrfTables> vrfTable = FibUtil.read(broker, LogicalDatastoreType.CONFIGURATION, id);
+      if (vrfTable.isPresent()) {
+        for (VrfEntry vrfEntry : vrfTable.get().getVrfEntry()) {
+          // Passing null as we don't know the dpn
+          // to which prefix is attached at this point
+          if (nexthopIp == vrfEntry.getNextHopAddress()) {
+            createRemoteFibEntry(null, dpnId, vpnId, vrfTable.get().getKey(), vrfEntry);
+          }
+        }
       }
     }
   }
@@ -659,12 +663,33 @@ public class FibManager extends AbstractDataChangeListener<VrfEntry> implements
   public void cleanUpDpnForVpn(BigInteger dpnId, long vpnId, String rd) {
     LOG.trace("Remove dpn {} for vpn {} : cleanUpDpnForVpn", dpnId, rd);
     InstanceIdentifier<VrfTables> id = buildVrfId(rd);
-    Optional<VrfTables> vrfTable = FibUtil.read(broker, LogicalDatastoreType.CONFIGURATION, id);
-    if(vrfTable.isPresent()) {
-      for(VrfEntry vrfEntry : vrfTable.get().getVrfEntry()) {
-        // Passing null as we don't know the dpn
-        // to which prefix is attached at this point
-        deleteRemoteRoute(null, dpnId, vpnId, vrfTable.get().getKey(), vrfEntry);
+    String lockOnDpnVpn = new String(dpnId.toString()+ vpnId);
+    synchronized (lockOnDpnVpn.intern()) {
+      Optional<VrfTables> vrfTable = FibUtil.read(broker, LogicalDatastoreType.CONFIGURATION, id);
+      if (vrfTable.isPresent()) {
+        for (VrfEntry vrfEntry : vrfTable.get().getVrfEntry()) {
+          // Passing null as we don't know the dpn
+          // to which prefix is attached at this point
+          deleteRemoteRoute(null, dpnId, vpnId, vrfTable.get().getKey(), vrfEntry);
+        }
+      }
+    }
+  }
+
+  public void cleanUpDpnForVpn(BigInteger dpnId, long vpnId, String rd, String nexthopIp) {
+    LOG.trace("dpn {} for vpn {}, nexthopIp {} : cleanUpDpnForVpn", dpnId, rd, nexthopIp);
+    InstanceIdentifier<VrfTables> id = buildVrfId(rd);
+    String lockOnDpnVpn = new String(dpnId.toString()+ vpnId);
+    synchronized (lockOnDpnVpn.intern()) {
+      Optional<VrfTables> vrfTable = FibUtil.read(broker, LogicalDatastoreType.CONFIGURATION, id);
+      if (vrfTable.isPresent()) {
+        for (VrfEntry vrfEntry : vrfTable.get().getVrfEntry()) {
+          // Passing null as we don't know the dpn
+          // to which prefix is attached at this point
+          if (nexthopIp == vrfEntry.getNextHopAddress()) {
+            deleteRemoteRoute(null, dpnId, vpnId, vrfTable.get().getKey(), vrfEntry);
+          }
+        }
       }
     }
   }
@@ -816,7 +841,11 @@ public class FibManager extends AbstractDataChangeListener<VrfEntry> implements
     instructions.add(new InstructionInfo(InstructionType.clear_actions));
     // Instruction to goto L3 InterfaceTable
 
-    instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.LPORT_DISPATCHER_TABLE }));
+    List <ActionInfo> actionsInfos = new ArrayList <ActionInfo> ();
+    actionsInfos.add(new ActionInfo(ActionType.nx_resubmit, new String[]{
+        Short.toString(NwConstants.LPORT_DISPATCHER_TABLE)}));
+    instructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
+    //instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.LPORT_DISPATCHER_TABLE }));
 
     FlowEntity flowEntityL3Intf = MDSALUtil.buildFlowEntity(dpnId, NwConstants.L3_INTERFACE_TABLE,
             getFlowRef(dpnId, NwConstants.L3_INTERFACE_TABLE, NwConstants.TABLE_MISS_FLOW),
index 69c73e10915828f9e05f05ef088f11b4848e9be9..273d648114a5dc29700be3c072522ca1bb50cb50 100644 (file)
@@ -18,7 +18,6 @@ import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.Adjacencies;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.OpState;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.Adjacency;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.prefix.to._interface.vpn.ids.Prefixes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnList;
@@ -89,13 +88,6 @@ public class FibUtil {
                 .child(org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface.class, new org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey(vpnInterfaceName)).build();
     }
 
-    static InstanceIdentifier<OpState> getVpnInterfaceOpStateIdentifier(String vpnInterfaceName) {
-        return InstanceIdentifier.builder(org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces.class)
-            .child(org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface.class,
-                   new org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey(vpnInterfaceName))
-            .augmentation(OpState.class).build();
-    }
-
     static InstanceIdentifier<VpnToDpnList> getVpnToDpnListIdentifier(String rd, BigInteger dpnId) {
         return InstanceIdentifier.builder(org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInstanceOpData.class)
                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry.class, new org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntryKey(rd))
index 07e0e00dcb9cb4b0b18b5ae7b4fe69e3beb17ea6..8678917f33ccf028dc1cd95b71f4413a5805379d 100755 (executable)
@@ -373,8 +373,9 @@ public enum ActionType {
 
         @Override
         public Action buildAction(ActionInfo actionInfo) {
+            String[] actionValues = actionInfo.getActionValues();
             NxResubmitBuilder nxarsb = new NxResubmitBuilder();
-            nxarsb.setTable((short) 55);
+            nxarsb.setTable(Short.parseShort(actionValues[0]));
             ActionBuilder ab = new ActionBuilder();
             ab.setAction(new NxActionResubmitRpcAddGroupCaseBuilder().setNxResubmit(nxarsb.build()).build());
             ab.setKey(new ActionKey(actionInfo.getActionKey()));
index f4adb4c55f3d0bd83e73dda8ea6086c8b4921a31..de1f4aac3278d60214e89e682a4938e110ee2884 100644 (file)
@@ -80,7 +80,8 @@ public class InterfaceStateChangeListener extends AbstractDataChangeListener<Int
         if (configInterface != null && configInterface.getType().equals(Tunnel.class)) {
           if(intrf.getOperStatus().equals(Interface.OperStatus.Up)) {
             //advertise all prefixes in all vpns for this dpn to bgp
-            vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.ADVERTISE_ROUTE);
+            // FIXME: Blocked until tunnel event[vxlan/gre] support is available
+            // vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.ADVERTISE_ROUTE);
           }
         } else {
           vpnInterfaceManager.processVpnInterfaceUp(dpnId, interfaceName, intrf.getIfIndex());
@@ -104,7 +105,8 @@ public class InterfaceStateChangeListener extends AbstractDataChangeListener<Int
         BigInteger dpId = InterfaceUtils.getDpIdFromInterface(intrf);
         if (intrf != null && intrf.getType() != null && intrf.getType().equals(Tunnel.class)) {
           //withdraw all prefixes in all vpns for this dpn from bgp
-          vpnInterfaceManager.updatePrefixesForDPN(dpId, VpnInterfaceManager.UpdateRouteAction.WITHDRAW_ROUTE);
+          // FIXME: Blocked until tunnel event[vxlan/gre] support is available
+          // vpnInterfaceManager.updatePrefixesForDPN(dpId, VpnInterfaceManager.UpdateRouteAction.WITHDRAW_ROUTE);
         } else {
           if (VpnUtil.isVpnInterfaceConfigured(broker, interfaceName)) {
             vpnInterfaceManager.processVpnInterfaceDown(dpId, interfaceName, intrf.getIfIndex(), true);
@@ -123,14 +125,16 @@ public class InterfaceStateChangeListener extends AbstractDataChangeListener<Int
       org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface
           intf = InterfaceUtils.getInterface(broker, interfaceName);
       if (intf != null && intf.getType().equals(Tunnel.class)) {
+        /*
+        // FIXME: Blocked until tunnel event[vxlan/gre] support is available
         BigInteger dpnId = InterfaceUtils.getDpIdFromInterface(update);
         if(update.getOperStatus().equals(Interface.OperStatus.Up)) {
           //advertise all prefixes in all vpns for this dpn to bgp
-          vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.ADVERTISE_ROUTE);
+          // vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.ADVERTISE_ROUTE);
         } else if(update.getOperStatus().equals(Interface.OperStatus.Down)) {
           //withdraw all prefixes in all vpns for this dpn from bgp
-          vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.WITHDRAW_ROUTE);
-        }
+          // vpnInterfaceManager.updatePrefixesForDPN(dpnId, VpnInterfaceManager.UpdateRouteAction.WITHDRAW_ROUTE);
+        }*/
       }
 
     }
index e7e471747f1092c558a265aacc163a2318c294ad..f98e3fb78b6ee561c9de80e7397c1b54d46942e8 100644 (file)
@@ -17,9 +17,9 @@ import org.opendaylight.controller.md.sal.binding.api.*;
 import org.opendaylight.vpnservice.mdsalutil.*;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.OpState;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.OpStateBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.PrefixToInterface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.AdjacencyKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.prefix.to._interface.vpn.ids.Prefixes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnList;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnListBuilder;
@@ -210,15 +210,17 @@ public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface
                 return;
             }
             bindService(dpId, vpnName, interfaceName, lPortTag);
-            updateDpnDbs(vpnName, interfaceName, true);
-            processVpnInterfaceAdjacencies(VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getName()), vpnInterface, true);
+            updateDpnDbs(dpId, vpnName, interfaceName, true);
+            processVpnInterfaceAdjacencies(dpId, VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getName()), vpnInterface);
         }
 
     }
 
-    private void updateDpnDbs(String vpnName, String interfaceName, boolean add) {
+    private void updateDpnDbs(BigInteger dpId, String vpnName, String interfaceName, boolean add) {
         long vpnId = VpnUtil.getVpnId(broker, vpnName);
-        BigInteger dpId = InterfaceUtils.getDpnForInterface(interfaceManager, interfaceName);
+        if (dpId == null) {
+            dpId = InterfaceUtils.getDpnForInterface(interfaceManager, interfaceName);
+        }
         if(!dpId.equals(BigInteger.ZERO)) {
             if(add)
                 updateMappingDbs(vpnId, dpId, interfaceName, vpnName);
@@ -250,8 +252,7 @@ public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface
 
     }
 
-    private void processVpnInterfaceAdjacencies(final InstanceIdentifier<VpnInterface> identifier, VpnInterface intf,
-                                                boolean vpnInterfaceState) {
+    private void processVpnInterfaceAdjacencies(BigInteger dpnId, final InstanceIdentifier<VpnInterface> identifier, VpnInterface intf) {
         String intfName = intf.getName();
 
         synchronized (intfName) {
@@ -266,7 +267,6 @@ public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface
                 // Get the rd of the vpn instance
                 String rd = getRouteDistinguisher(intf.getVpnInstanceName());
 
-                BigInteger dpnId = InterfaceUtils.getDpnForInterface(interfaceManager, intfName);
                 String nextHopIp = InterfaceUtils.getEndpointIpAddressForDPN(broker, dpnId);
                 if (nextHopIp == null){
                     LOG.error("NextHop for interface {} is null", intfName);
@@ -291,8 +291,7 @@ public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface
                 }
 
                 Adjacencies aug = VpnUtil.getVpnInterfaceAugmentation(value);
-                VpnInterface opInterface = VpnUtil.getVpnInterface(intfName, intf.getVpnInstanceName(),
-                                                                   aug, vpnInterfaceState);
+                VpnInterface opInterface = VpnUtil.getVpnInterface(intfName, intf.getVpnInstanceName(), aug);
                 InstanceIdentifier<VpnInterface> interfaceId = VpnUtil.getVpnInterfaceIdentifier(intfName);
                 VpnUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, interfaceId, opInterface);
                 for (Adjacency nextHop : aug.getAdjacency()) {
@@ -492,8 +491,6 @@ public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface
 
         String rd = VpnUtil.getVpnRd(broker, intf.getVpnInstanceName());
         if (adjacencies.isPresent()) {
-            VpnUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, identifier.augmentation(OpState.class),
-                               new OpStateBuilder().setStateUp(false).build());
             List<Adjacency> nextHops = adjacencies.get().getAdjacency();
 
             if (!nextHops.isEmpty()) {
@@ -684,8 +681,7 @@ public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface
                     .setIpAddress(prefix).setKey(new AdjacencyKey(prefix)).build());
 
             Adjacencies aug = VpnUtil.getVpnInterfaceAugmentation(adjacencies);
-            VpnInterface newVpnIntf = VpnUtil.getVpnInterface(currVpnIntf.getName(), currVpnIntf.getVpnInstanceName(),
-                                                              aug, currVpnIntf.getAugmentation(OpState.class).isStateUp());
+            VpnInterface newVpnIntf = VpnUtil.getVpnInterface(currVpnIntf.getName(), currVpnIntf.getVpnInstanceName(), aug);
 
             VpnUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, identifier, newVpnIntf);
             addExtraRoute(adj.getIpAddress(), adj.getNextHopIp(), rd, currVpnIntf.getVpnInstanceName(), (int) label, currVpnIntf.getName());
@@ -719,8 +715,7 @@ public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface
                             Adjacencies aug = VpnUtil.getVpnInterfaceAugmentation(adjacencies);
                             VpnInterface newVpnIntf = VpnUtil.getVpnInterface(currVpnIntf.getName(),
                                                                               currVpnIntf.getVpnInstanceName(),
-                                                                              aug,
-                                                                              currVpnIntf.getAugmentation(OpState.class).isStateUp());
+                                                                              aug);
 
                             VpnUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, identifier, newVpnIntf);
 
@@ -785,7 +780,7 @@ public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface
             //increment the vpn interface count in Vpn Instance Op Data
             Long ifCnt = 0L;
             String rd = getRouteDistinguisher(del.getVpnInstanceName());
-            if(rd.isEmpty()) rd = del.getVpnInstanceName();
+            if(rd == null || rd.isEmpty()) rd = del.getVpnInstanceName();
             VpnInstanceOpDataEntry vpnInstOp = VpnUtil.getVpnInstanceOpData(broker, rd);
             if(vpnInstOp != null && vpnInstOp.getVpnInterfaceCount() != null) {
                 ifCnt = vpnInstOp.getVpnInterfaceCount();
@@ -793,12 +788,31 @@ public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface
 
             LOG.trace("VpnInterfaceOpListener remove: interface name {} rd {} interface count in Vpn Op Instance {}", interfaceName, rd, ifCnt);
 
-            VpnUtil.asyncUpdate(broker, LogicalDatastoreType.OPERATIONAL,
-                    VpnUtil.getVpnInstanceOpDataIdentifier(rd),
-                    VpnUtil.updateIntfCntInVpnInstOpData(ifCnt - 1, rd), VpnUtil.DEFAULT_CALLBACK);
-
-            //TODO: Clean up the DPN List in Vpn Instance Op if ifCnt is zero
+            if(ifCnt != 0) {
+                VpnUtil.asyncUpdate(broker, LogicalDatastoreType.OPERATIONAL,
+                                    VpnUtil.getVpnInstanceOpDataIdentifier(rd),
+                                    VpnUtil.updateIntfCntInVpnInstOpData(ifCnt - 1, rd), VpnUtil.DEFAULT_CALLBACK);
+            }
 
+            // Vpn Interface removed => No more adjacencies from it.
+            // Hence clean up interface from vpn-dpn-interface list.
+            Adjacency adjacency = del.getAugmentation(Adjacencies.class).getAdjacency().get(0);
+            Optional<Prefixes> prefixToInterface = Optional.absent();
+            prefixToInterface = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL,
+                         VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(),
+                                                VpnUtil.getIpPrefix(adjacency.getIpAddress())));
+            if (!prefixToInterface.isPresent()) {
+                prefixToInterface = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL,
+                                                 VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(),
+                                                         VpnUtil.getIpPrefix(adjacency.getNextHopIp())));
+            }
+            if (prefixToInterface.isPresent()) {
+                VpnUtil.delete(broker, LogicalDatastoreType.OPERATIONAL,
+                               VpnUtil.getPrefixToInterfaceIdentifier(vpnInstOp.getVpnId(),
+                                                 prefixToInterface.get().getIpAddress()),
+                               VpnUtil.DEFAULT_CALLBACK);
+                updateDpnDbs(prefixToInterface.get().getDpnId(), del.getVpnInstanceName(), interfaceName, false);
+            }
             notifyTaskIfRequired(interfaceName);
         }
 
@@ -822,7 +836,7 @@ public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface
             //increment the vpn interface count in Vpn Instance Op Data
             Long ifCnt = 0L;
             String rd = getRouteDistinguisher(add.getVpnInstanceName());
-            if(rd.isEmpty()) rd = add.getVpnInstanceName();
+            if(rd == null || rd.isEmpty()) rd = add.getVpnInstanceName();
             VpnInstanceOpDataEntry vpnInstOp = VpnUtil.getVpnInstanceOpData(broker, rd);
             if(vpnInstOp != null &&  vpnInstOp.getVpnInterfaceCount() != null) {
                 ifCnt = vpnInstOp.getVpnInterfaceCount();
@@ -850,46 +864,60 @@ public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface
             Iterator<VpnInstance> vpnInstIter = vpnInstanceList.iterator();
             while (vpnInstIter.hasNext()) {
                 VpnInstance vpnInstance = vpnInstIter.next();
-                VpnAfConfig vpnConfig = vpnInstance.getIpv4Family();
-                String rd = vpnConfig.getRouteDistinguisher();
-
-                InstanceIdentifier<VpnToDpnList> id = VpnUtil.getVpnToDpnListIdentifier(rd, dpnId);
-                Optional<VpnToDpnList> dpnInVpn = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, id);
-                if (dpnInVpn.isPresent()) {
-                    if (action == UpdateRouteAction.ADVERTISE_ROUTE) {
-                        fibManager.populateFibOnNewDpn(dpnId, VpnUtil
-                            .getVpnId(broker, vpnInstance.getVpnInstanceName()), rd);
+                try {
+                    VpnAfConfig vpnConfig = vpnInstance.getIpv4Family();
+                    String rd = vpnConfig.getRouteDistinguisher();
+                    if (rd == null || rd.isEmpty()) {
+                        rd = vpnInstance.getVpnInstanceName();
                     }
-                    List<org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
-                        .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces> vpnInterfaces = dpnInVpn.get().getVpnInterfaces();
-                    for(org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
-                        .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces vpnInterface : vpnInterfaces) {
-                        InstanceIdentifier<VpnInterface> vpnIntfId = VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getInterfaceName());
-                        InstanceIdentifier<Adjacencies> path = vpnIntfId.augmentation(Adjacencies.class);
-                        Optional<Adjacencies> adjacencies = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, path);
-
-                        if (adjacencies.isPresent()) {
-                            List<Adjacency> adjacencyList = adjacencies.get().getAdjacency();
-                            Iterator<Adjacency> adjacencyIterator = adjacencyList.iterator();
-
-                            while (adjacencyIterator.hasNext()) {
-                                Adjacency adjacency = adjacencyIterator.next();
-                                try {
-                                    if(action == UpdateRouteAction.ADVERTISE_ROUTE)
-                                        bgpManager.addPrefix(rd, adjacency.getIpAddress(), adjacency.getNextHopIp(), adjacency.getLabel().intValue());
-                                    else if(action == UpdateRouteAction.WITHDRAW_ROUTE)
-                                        bgpManager.deletePrefix(rd, adjacency.getIpAddress());
-                                } catch (Exception e) {
-                                    LOG.error("Exception when updating prefix {} in vrf {} to BGP", adjacency.getIpAddress(), rd);
+                    InstanceIdentifier<VpnToDpnList> id =
+                        VpnUtil.getVpnToDpnListIdentifier(rd, dpnId);
+                    Optional<VpnToDpnList> dpnInVpn =
+                        VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, id);
+                    if (dpnInVpn.isPresent()) {
+                        // if (action == UpdateRouteAction.ADVERTISE_ROUTE) {
+                        //    fibManager.populateFibOnNewDpn(dpnId, VpnUtil
+                        //        .getVpnId(broker, vpnInstance.getVpnInstanceName()), rd);
+                        // }
+                        List<org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
+                            .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces>
+                            vpnInterfaces = dpnInVpn.get().getVpnInterfaces();
+                        for (org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
+                            .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces vpnInterface : vpnInterfaces) {
+                            InstanceIdentifier<VpnInterface> vpnIntfId =
+                                VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getInterfaceName());
+                            InstanceIdentifier<Adjacencies> path =
+                                vpnIntfId.augmentation(Adjacencies.class);
+                            Optional<Adjacencies> adjacencies =
+                                VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, path);
+
+                            if (adjacencies.isPresent()) {
+                                List<Adjacency> adjacencyList = adjacencies.get().getAdjacency();
+                                Iterator<Adjacency> adjacencyIterator = adjacencyList.iterator();
+
+                                while (adjacencyIterator.hasNext()) {
+                                    Adjacency adjacency = adjacencyIterator.next();
+                                    try {
+                                        if (action == UpdateRouteAction.ADVERTISE_ROUTE)
+                                            bgpManager.addPrefix(rd, adjacency.getIpAddress(),
+                                                                 adjacency.getNextHopIp(),
+                                                                 adjacency.getLabel().intValue());
+                                        else if (action == UpdateRouteAction.WITHDRAW_ROUTE)
+                                            bgpManager.deletePrefix(rd, adjacency.getIpAddress());
+                                    } catch (Exception e) {
+                                        LOG.error("Exception when updating prefix {} in vrf {} to BGP",
+                                            adjacency.getIpAddress(), rd);
+                                    }
                                 }
                             }
-                        }
 
+                        }
+                        // if (action == UpdateRouteAction.WITHDRAW_ROUTE) {
+                        //    fibManager.cleanUpDpnForVpn(dpnId, VpnUtil.getVpnId(broker, vpnInstance.getVpnInstanceName()), rd);
+                        // }
                     }
-                    if (action == UpdateRouteAction.WITHDRAW_ROUTE) {
-                        fibManager.cleanUpDpnForVpn(dpnId, VpnUtil
-                            .getVpnId(broker, vpnInstance.getVpnInstanceName()), rd);
-                    }
+                } catch (Exception e) {
+                    LOG.error("updatePrefixesForDPN {} in vpn {} failed", dpnId, vpnInstance.getVpnInstanceName(), e);
                 }
             }
         }
index cbd29917a46ed52e834626f5f12beef070773bd7..67ad13b84ae3cd0dd150e0c9b5b3fe6b22ed1cf7 100644 (file)
@@ -83,10 +83,9 @@ public class VpnUtil {
                 .child(VpnInstance.class, new VpnInstanceKey(vpnName)).build();
     }
 
-    static VpnInterface getVpnInterface(String intfName, String vpnName, Adjacencies aug, boolean opState) {
+    static VpnInterface getVpnInterface(String intfName, String vpnName, Adjacencies aug) {
         return new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(intfName)).setVpnInstanceName(vpnName)
                 .addAugmentation(Adjacencies.class, aug)
-                .addAugmentation(OpState.class, new OpStateBuilder().setStateUp(opState).build())
                 .build();
     }