Merge "Bug 5267 - Setting vlan id for termination end point not working"
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / servicebindings / flowbased / statehelpers / FlowBasedServicesStateBindHelper.java
index 0e067c4ea794782eadf04c856c97dfac5826ff26..3f6cac015d07afed60f5222a3676ba7511afc224 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.statehelpers;
 
 import com.google.common.util.concurrent.ListenableFuture;
+
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.vpnservice.interfacemgr.IfmConstants;
@@ -15,6 +16,7 @@ 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.vpnservice.mdsalutil.MatchInfo;
+import org.opendaylight.vpnservice.mdsalutil.NwConstants;
 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
 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;
@@ -29,6 +31,9 @@ import org.slf4j.LoggerFactory;
 
 import java.math.BigInteger;
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 
 public class FlowBasedServicesStateBindHelper {
@@ -37,7 +42,6 @@ public class FlowBasedServicesStateBindHelper {
     public static List<ListenableFuture<Void>> bindServicesOnInterface(Interface ifaceState,
                                                              DataBroker dataBroker) {
         List<ListenableFuture<Void>> futures = new ArrayList<>();
-        WriteTransaction t = dataBroker.newWriteOnlyTransaction();
         ServicesInfo servicesInfo = FlowBasedServicesUtils.getServicesInfoForInterface(ifaceState.getName(), dataBroker);
         if (servicesInfo == null) {
             return futures;
@@ -48,45 +52,74 @@ public class FlowBasedServicesStateBindHelper {
             return futures;
         }
 
-        BoundServices highestPriorityBoundService = null;
-        short highestPriority = 0xFF;
-        for (BoundServices boundService : allServices) {
-            if (boundService.getServicePriority() < highestPriority) {
-                highestPriorityBoundService = boundService;
-                highestPriority = boundService.getServicePriority();
-            }
-        }
-
         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);
 
-        NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
-        long portNo = Long.parseLong(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
-        BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
-        Long lportTag = FlowBasedServicesUtils.getLPortTag(iface, dataBroker);
-        int vlanId = 0;
-        List<MatchInfo> matches = null;
         if (iface.getType().isAssignableFrom(L2vlan.class)) {
-            vlanId = iface.getAugmentation(IfL2vlan.class).getVlanId().getValue();
-            matches = FlowBasedServicesUtils.getMatchInfoForVlanPortAtIngressTable(dpId, portNo, vlanId);
+            return bindServiceOnVlan(allServices, iface, ifaceState.getIfIndex(), dataBroker);
         } else if (iface.getType().isAssignableFrom(Tunnel.class)){
-            matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable (dpId, portNo, iface);
+             return bindServiceOnTunnel(allServices, iface, ifaceState.getIfIndex(), dataBroker);
         }
+        return futures;
+    }
 
+    private static List<ListenableFuture<Void>> bindServiceOnTunnel(
+            List<BoundServices> allServices,
+            org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface,
+            Integer ifIndex, DataBroker dataBroker) {
+        List<ListenableFuture<Void>> futures = new ArrayList<>();
+        NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
+        long portNo = Long.parseLong(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
+        BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
+        List<MatchInfo> matches = FlowBasedServicesUtils.getMatchInfoForTunnelPortAtIngressTable (dpId, portNo, iface);
+        BoundServices highestPriorityBoundService = FlowBasedServicesUtils.getHighestPriorityService(allServices);
+        WriteTransaction t = dataBroker.newWriteOnlyTransaction();
         if (matches != null) {
-            FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, vlanId, highestPriorityBoundService,
-                    dataBroker, t, matches, lportTag.intValue(), IfmConstants.VLAN_INTERFACE_INGRESS_TABLE);
+            FlowBasedServicesUtils.installInterfaceIngressFlow(dpId, iface, highestPriorityBoundService,
+                    t, matches, ifIndex, NwConstants.VLAN_INTERFACE_INGRESS_TABLE);
         }
 
         for (BoundServices boundService : allServices) {
             if (!boundService.equals(highestPriorityBoundService)) {
-                FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundService, iface,
-                         dataBroker, t, lportTag.intValue());
+                FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, boundService, iface, t, ifIndex, boundService.getServicePriority(), (short) (boundService.getServicePriority()+1));
             }
         }
 
         futures.add(t.submit());
         return futures;
     }
+
+    private static List<ListenableFuture<Void>> bindServiceOnVlan(
+            List<BoundServices> allServices,
+            org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface,
+            Integer ifIndex, DataBroker dataBroker) {
+        List<ListenableFuture<Void>> futures = new ArrayList<>();
+        NodeConnectorId nodeConnectorId = FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
+        BigInteger dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
+        WriteTransaction t = dataBroker.newWriteOnlyTransaction();
+        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);
+        short nextServiceIndex = (short) (allServices.size() > 0 ? allServices.get(0).getServicePriority() : highestPriority.getServicePriority() + 1);
+        FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, highestPriority, iface, t, ifIndex, IfmConstants.DEFAULT_SERVICE_INDEX, nextServiceIndex);
+        BoundServices prev = null;
+        for (BoundServices boundService : allServices) {
+            if (prev!=null) {
+                FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, prev, iface, t, ifIndex, prev.getServicePriority(), boundService.getServicePriority());
+            }
+            prev = boundService;
+        }
+        if (prev!=null) {
+            FlowBasedServicesUtils.installLPortDispatcherFlow(dpId, prev, iface, t, ifIndex, prev.getServicePriority(), (short) (prev.getServicePriority()+1));
+        }
+        futures.add(t.submit());
+        return futures;
+
+    }
+
 }
\ No newline at end of file