Handling port up down event for Bug 3558
[vpnservice.git] / nexthopmgr / nexthopmgr-impl / src / main / java / org / opendaylight / vpnservice / nexthopmgr / NexthopManager.java
index e5aa70c1bee556d12a45af64318c792cc939e9e6..1543a81947a6de5e16e3f9a4d42d4a5d02fdbda4 100644 (file)
@@ -12,9 +12,8 @@ import org.opendaylight.vpnservice.mdsalutil.InstructionInfo;
 import org.opendaylight.vpnservice.mdsalutil.InstructionType;
 import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
-import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.L3tunnel;
+import org.opendaylight.vpnservice.mdsalutil.NwConstants;
+
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.List;
@@ -140,9 +139,9 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
         }
     }
 
-    private long getDpnId(String ifName) {
-        String[] fields = ifName.split(":");
-        long dpn = Integer.parseInt(fields[1]);
+    private BigInteger getDpnId(String ofPortId) {
+        String[] fields = ofPortId.split(":");
+        BigInteger dpn = new BigInteger(fields[1]);
         LOG.debug("DpnId: {}", dpn);
         return dpn;
     }
@@ -167,7 +166,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
         int groupId = createNextHopPointer(nhKey);
 
         long vpnId = getVpnId(vpnName);
-        long dpnId = interfaceManager.getDpnForInterface(ifName);
+        BigInteger dpnId = interfaceManager.getDpnForInterface(ifName);
         VpnNexthop nexthop = getVpnNexthop(vpnId, ipAddress, 0);
         LOG.trace("nexthop: {}", nexthop);
         if (nexthop == null) {
@@ -200,7 +199,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
         String nhKey = new String("nexthop." + ifName + ipAddress);
         int groupId = createNextHopPointer(nhKey);
 
-        long dpnId = getDpnId(ofPortId);
+        BigInteger dpnId = getDpnId(ofPortId);
         TunnelNexthop nexthop = getTunnelNexthop(dpnId, ipAddress);
         if (nexthop == null) {
             List<BucketInfo> listBucketInfo = new ArrayList<BucketInfo>();
@@ -211,7 +210,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
             GroupEntity groupEntity = MDSALUtil.buildGroupEntity(
                 dpnId, groupId, ipAddress, GroupTypes.GroupIndirect, listBucketInfo);
             mdsalManager.installGroup(groupEntity);
-            addRemoteFlow(dpnId, ifName);
+            //makeRemoteFlow(dpnId, ifName, NwConstants.ADD_FLOW);
 
             //update MD-SAL DS
             addTunnelNexthopToDS(dpnId, ipAddress, groupId);
@@ -220,37 +219,38 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
         }
     }
 
-    private void addRemoteFlow(long dpId, String ifName) {
-
-            long portNo = interfaceManager.getPortForInterface(ifName);
-            String flowRef = getTunnelInterfaceFlowRef(dpId, LPORT_INGRESS_TABLE, portNo);
-
-            String flowName = ifName;
-            BigInteger COOKIE_VM_INGRESS_TABLE = new BigInteger("8000001", 16);
-
-            int priority = DEFAULT_FLOW_PRIORITY;
-            short gotoTableId = LFIB_TABLE;
-
-            List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
-            mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { gotoTableId }));
-
-            List<MatchInfo> matches = new ArrayList<MatchInfo>();
-            matches.add(new MatchInfo(MatchFieldType.in_port, new long[] {
-                    dpId, portNo }));
+    private void makeRemoteFlow(BigInteger dpnId, String ifName, int addOrRemoveFlow) {
+        long portNo = 0;
+        String flowName = ifName;
+        String flowRef = getTunnelInterfaceFlowRef(dpnId, LPORT_INGRESS_TABLE, ifName);
+        List<MatchInfo> matches = new ArrayList<MatchInfo>();
+        List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
+        if (NwConstants.ADD_FLOW == addOrRemoveFlow) {
+            portNo = interfaceManager.getPortForInterface(ifName);
+            matches.add(new MatchInfo(MatchFieldType.in_port, new BigInteger[] {
+                dpnId, BigInteger.valueOf(portNo) }));
+            mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] {LFIB_TABLE}));
+        }
 
-            FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, LPORT_INGRESS_TABLE, flowRef,
-                              priority, flowName, 0, 0, COOKIE_VM_INGRESS_TABLE, matches, mkInstructions);
+        BigInteger COOKIE_VM_INGRESS_TABLE = new BigInteger("8000001", 16);
+        FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpnId, LPORT_INGRESS_TABLE, flowRef,
+                                                          DEFAULT_FLOW_PRIORITY, flowName, 0, 0, COOKIE_VM_INGRESS_TABLE, matches, mkInstructions);
 
+        if (NwConstants.ADD_FLOW == addOrRemoveFlow) {
             mdsalManager.installFlow(flowEntity);
+        } else {
+            mdsalManager.removeFlow(flowEntity);
+        }
     }
 
-    private String getTunnelInterfaceFlowRef(long dpId, short tableId, long portNo) {
-                return new StringBuilder().append(dpId).append(tableId).append(portNo).toString();
+    private String getTunnelInterfaceFlowRef(BigInteger dpnId, short tableId, String ifName) {
+                return new StringBuilder().append(dpnId).append(tableId).append(ifName).toString();
             }
 
     protected void addVpnNexthopToDS(long vpnId, String ipPrefix, long egressPointer) {
 
-        InstanceIdentifierBuilder<VpnNexthops> idBuilder = InstanceIdentifier.builder(L3nexthop.class)
+        InstanceIdentifierBuilder<VpnNexthops> idBuilder = InstanceIdentifier.builder(
+            L3nexthop.class)
                 .child(VpnNexthops.class, new VpnNexthopsKey(vpnId));
 
         // Add nexthop to vpn node
@@ -266,7 +266,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
 
     }
 
-    private void addTunnelNexthopToDS(long dpnId, String ipPrefix, long egressPointer) {
+    private void addTunnelNexthopToDS(BigInteger dpnId, String ipPrefix, long egressPointer) {
         InstanceIdentifierBuilder<TunnelNexthops> idBuilder = InstanceIdentifier.builder(L3nexthop.class)
                 .child(TunnelNexthops.class, new TunnelNexthopsKey(dpnId));
 
@@ -313,7 +313,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
         return null;
     }
 
-    private TunnelNexthop getTunnelNexthop(long dpnId, String ipAddress) {
+    private TunnelNexthop getTunnelNexthop(BigInteger dpnId, String ipAddress) {
         
         InstanceIdentifierBuilder<TunnelNexthops> idBuilder = InstanceIdentifier.builder(L3nexthop.class)
                 .child(TunnelNexthops.class, new TunnelNexthopsKey(dpnId));
@@ -333,7 +333,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
         return null;
     }
 
-    public long getNextHopPointer(long dpnId, long vpnId, String prefixIp, String nextHopIp) {
+    public long getNextHopPointer(BigInteger dpnId, long vpnId, String prefixIp, String nextHopIp) {
         String endpointIp = interfaceManager.getEndpointIpForDpn(dpnId);
         if (nextHopIp.equals(endpointIp)) {
             VpnNexthop vpnNextHop = getVpnNexthop(vpnId, prefixIp, 0);
@@ -345,7 +345,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
         }
     }
 
-    private void removeTunnelNexthopFromDS(long dpnId, String ipPrefix) {
+    private void removeTunnelNexthopFromDS(BigInteger dpnId, String ipPrefix) {
 
         InstanceIdentifierBuilder<TunnelNexthop> idBuilder = InstanceIdentifier.builder(L3nexthop.class)
                 .child(TunnelNexthops.class, new TunnelNexthopsKey(dpnId))
@@ -368,14 +368,14 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
     }
 
  
-    public void removeLocalNextHop(Long dpId, Long vpnId, String ipAddress) {
+    public void removeLocalNextHop(BigInteger dpnId, Long vpnId, String ipAddress) {
 
         VpnNexthop nh = getVpnNexthop(vpnId, ipAddress, 0);
         if (nh != null) {
             // how to inform and remove dependent FIB entries??
             // we need to do it before the group is removed
             GroupEntity groupEntity = MDSALUtil.buildGroupEntity(
-                    dpId, nh.getEgressPointer(), ipAddress, GroupTypes.GroupIndirect, null);
+                    dpnId, nh.getEgressPointer(), ipAddress, GroupTypes.GroupIndirect, null);
             // remove Group ...
             mdsalManager.removeGroup(groupEntity);
             //update MD-SAL DS
@@ -387,7 +387,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
 
     }
 
-    public void removeRemoteNextHop(long dpnId, String ipAddress) {
+    public void removeRemoteNextHop(BigInteger dpnId, String ifName, String ipAddress) {
 
         TunnelNexthop nh = getTunnelNexthop(dpnId, ipAddress);
         if (nh != null) {
@@ -399,6 +399,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
                     dpnId, nh.getEgressPointer(), ipAddress, GroupTypes.GroupIndirect, null);
             // remove Group ...
             mdsalManager.removeGroup(groupEntity);
+            //makeRemoteFlow(dpnId, ifName, NwConstants.DEL_FLOW);
             //update MD-SAL DS
             removeTunnelNexthopFromDS(dpnId, ipAddress);
         } else {
@@ -416,7 +417,7 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
 
         String endpointIp = interfaceManager.getEndpointIpForDpn(input.getDpnId());
         LOG.trace("getEgressPointer: input {}, endpointIp {}", input, endpointIp);
-        if (input.getNexthopIp().equals(endpointIp)) {
+        if (input.getNexthopIp() == null || input.getNexthopIp().equals(endpointIp)) {
             VpnNexthop vpnNextHop = getVpnNexthop(input.getVpnId(), input.getIpPrefix(), 5);
             output.setEgressPointer(vpnNextHop.getEgressPointer());
             output.setLocalDestination(true);
@@ -451,14 +452,14 @@ public class NexthopManager implements L3nexthopService, AutoCloseable {
     private <T extends DataObject> void asyncWrite(LogicalDatastoreType datastoreType,
             InstanceIdentifier<T> path, T data, FutureCallback<Void> callback) {
         WriteTransaction tx = broker.newWriteOnlyTransaction();
-        tx.put(datastoreType, path, data, true);
+        tx.merge(datastoreType, path, data, true);
         Futures.addCallback(tx.submit(), callback);
     }
 
     private <T extends DataObject> void syncWrite(LogicalDatastoreType datastoreType,
             InstanceIdentifier<T> path, T data, FutureCallback<Void> callback) {
         WriteTransaction tx = broker.newWriteOnlyTransaction();
-        tx.put(datastoreType, path, data, true);
+        tx.merge(datastoreType, path, data, true);
         tx.submit();
     }