Bug 5199 : Fixed unbind & other issues in intrface
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / servicebindings / flowbased / statehelpers / FlowBasedServicesStateUnbindHelper.java
index 362a3bdfcff4353ebe342feb33b75a1303e0c972..619c6bf595fef0ca109212c17b17af643f1d7218 100644 (file)
@@ -7,13 +7,19 @@
  */
 package org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.statehelpers;
 
-import com.google.common.util.concurrent.ListenableFuture;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.vpnservice.interfacemgr.IfmConstants;
 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
-import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
@@ -21,9 +27,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.serviceb
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.math.BigInteger;
-import java.util.ArrayList;
-import java.util.List;
+import com.google.common.util.concurrent.ListenableFuture;
 
 public class FlowBasedServicesStateUnbindHelper {
     private static final Logger LOG = LoggerFactory.getLogger(FlowBasedServicesStateUnbindHelper.class);
@@ -31,7 +35,6 @@ public class FlowBasedServicesStateUnbindHelper {
     public static List<ListenableFuture<Void>> unbindServicesFromInterface(Interface ifaceState,
                                                                            DataBroker dataBroker) {
         List<ListenableFuture<Void>> futures = new ArrayList<>();
-        WriteTransaction t = dataBroker.newWriteOnlyTransaction();
 
         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(ifaceState.getName(), dataBroker);
         if (servicesInfo == null) {
@@ -43,33 +46,66 @@ public class FlowBasedServicesStateUnbindHelper {
             return futures;
         }
 
-        BoundServices highestPriorityBoundService = null;
-        short highestPriority = 0xFF;
-        for (BoundServices boundService : allServices) {
-            if (boundService.getServicePriority() < highestPriority) {
-                highestPriorityBoundService = boundService;
-                highestPriority = boundService.getServicePriority();
-            }
+        if (ifaceState.getType().isAssignableFrom(L2vlan.class)) {
+            return unbindServiceOnVlan(allServices, ifaceState, ifaceState.getIfIndex(), dataBroker);
+        } else if (ifaceState.getType().isAssignableFrom(Tunnel.class)){
+             return unbindServiceOnTunnel(allServices, ifaceState, ifaceState.getIfIndex(), dataBroker);
         }
+        return futures;
+    }
 
-        InterfaceKey interfaceKey = new InterfaceKey(ifaceState.getName());
-        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
-                InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
+    private static List<ListenableFuture<Void>> unbindServiceOnTunnel(
+            List<BoundServices> allServices,
+            Interface iface,
+            Integer ifIndex, DataBroker dataBroker) {
+        List<ListenableFuture<Void>> futures = new ArrayList<>();
+        WriteTransaction t = dataBroker.newWriteOnlyTransaction();
 
-        NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
+        List<String> ofportIds = iface.getLowerLayerIf();
+        NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
         if(nodeConnectorId == null){
             return futures;
         }
+        BoundServices highestPriorityBoundService = FlowBasedServicesUtils.getHighestPriorityService(allServices);
+
         BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
-        FlowBasedServicesUtils.removeIngressFlow(iface, highestPriorityBoundService, dpId,
-                dataBroker, t);
+        FlowBasedServicesUtils.removeIngressFlow(iface.getName(), highestPriorityBoundService, dpId, t);
 
         for (BoundServices boundService : allServices) {
             if (!boundService.equals(highestPriorityBoundService)) {
-                FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface, boundService, dataBroker, t);
+                FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, iface.getName(), boundService, t, boundService.getServicePriority());
             }
         }
 
+        futures.add(t.submit());
         return futures;
     }
-}
\ No newline at end of file
+
+    private static List<ListenableFuture<Void>> unbindServiceOnVlan(
+            List<BoundServices> allServices, Interface ifaceState,
+            Integer ifIndex, DataBroker dataBroker) {
+        List<ListenableFuture<Void>> futures = new ArrayList<>();
+        WriteTransaction t = dataBroker.newWriteOnlyTransaction();
+        List<String> ofportIds = ifaceState.getLowerLayerIf();
+        NodeConnectorId nodeConnectorId = new NodeConnectorId(ofportIds.get(0));
+        if(nodeConnectorId == null){
+            return futures;
+        }
+        BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
+        Collections.sort(allServices, new Comparator<BoundServices>() {
+            @Override
+            public int compare(BoundServices serviceInfo1, BoundServices serviceInfo2) {
+                return serviceInfo1.getServicePriority().compareTo(serviceInfo2.getServicePriority());
+            }
+        });
+        BoundServices highestPriority = allServices.remove(0);
+        FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), highestPriority, t, IfmConstants.DEFAULT_SERVICE_INDEX);
+        for (BoundServices boundService : allServices) {
+                FlowBasedServicesUtils.removeLPortDispatcherFlow(dpId, ifaceState.getName(), boundService, t, boundService.getServicePriority());
+        }
+        futures.add(t.submit());
+        return futures;
+
+    }
+
+}