Merge "Updated BgpManager for Be"
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / servicebindings / flowbased / utilities / FlowBasedServicesUtils.java
index 539fa78cfb71d01e0e751c06727b4c05e7fe8b67..1624b0b593caf40a1b93505cf55a401f14393d6f 100644 (file)
@@ -35,6 +35,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.serviceb
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfo;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.ServicesInfoKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -63,13 +64,21 @@ public class FlowBasedServicesUtils {
     public static NodeConnectorId getNodeConnectorIdFromInterface(Interface iface, DataBroker dataBroker) {
         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(iface.getName(), dataBroker);
-        List<String> ofportIds = ifState.getLowerLayerIf();
-        return new NodeConnectorId(ofportIds.get(0));
+        if(ifState != null) {
+            List<String> ofportIds = ifState.getLowerLayerIf();
+            return new NodeConnectorId(ofportIds.get(0));
+        }
+        return null;
     }
 
-    public static List<MatchInfo> getMatchInfoForVlanPortAtIngressTable(BigInteger dpId, long portNo, long vlanId) {
+    public static List<MatchInfo> getMatchInfoForVlanPortAtIngressTable(BigInteger dpId, long portNo, Interface iface) {
         List<MatchInfo> matches = new ArrayList<>();
         matches.add(new MatchInfo(MatchFieldType.in_port, new BigInteger[] {dpId, BigInteger.valueOf(portNo)}));
+        int vlanId = 0;
+        IfL2vlan l2vlan = iface.getAugmentation(IfL2vlan.class);
+        if(l2vlan != null){
+            vlanId = l2vlan.getVlanId().getValue();
+        }
         if (vlanId > 0) {
             LOG.error("VlanId matching support is not fully available in Be.");
             matches.add(new MatchInfo(MatchFieldType.vlan_vid, new long[]{vlanId}));
@@ -113,14 +122,19 @@ public class FlowBasedServicesUtils {
         return 0L;
     }
 
-    public static void installInterfaceIngressFlow(BigInteger dpId, String interfaceName, int vlanId,
+    public static void installInterfaceIngressFlow(BigInteger dpId, Interface iface,
                                                    BoundServices boundServiceNew,
-                                                   DataBroker dataBroker, WriteTransaction t,
+                                                   WriteTransaction t,
                                                    List<MatchInfo> matches, int lportTag, short tableId) {
         List<Instruction> instructions = boundServiceNew.getAugmentation(StypeOpenflow.class).getInstruction();
 
         int serviceInstructionsSize = instructions.size();
         List<Instruction> instructionSet = new ArrayList<Instruction>();
+        int vlanId = 0;
+        IfL2vlan l2vlan = iface.getAugmentation(IfL2vlan.class);
+        if(l2vlan != null){
+            vlanId = l2vlan.getVlanId().getValue();
+        }
         if (vlanId != 0) {
             // incrementing instructionSize and using it as actionKey. Because it won't clash with any other instructions
             int actionKey = ++serviceInstructionsSize;
@@ -150,15 +164,15 @@ public class FlowBasedServicesUtils {
         }
 
         String serviceRef = boundServiceNew.getServiceName();
-        String flowRef = getFlowRef(dpId, interfaceName, boundServiceNew);
+        String flowRef = getFlowRef(dpId, iface.getName(), boundServiceNew);
         StypeOpenflow stypeOpenflow = boundServiceNew.getAugmentation(StypeOpenflow.class);
         Flow ingressFlow = MDSALUtil.buildFlowNew(tableId, flowRef,
                 stypeOpenflow.getFlowPriority(), serviceRef, 0, 0,
                 stypeOpenflow.getFlowCookie(), matches, instructionSet);
-        installFlow(dpId, ingressFlow, dataBroker, t);
+        installFlow(dpId, ingressFlow, t);
     }
 
-    private static void installFlow(BigInteger dpId, Flow flow, DataBroker dataBroker, WriteTransaction t) {
+    public static void installFlow(BigInteger dpId, Flow flow, WriteTransaction t) {
         FlowKey flowKey = new FlowKey(new FlowId(flow.getId()));
         Node nodeDpn = buildInventoryDpnNode(dpId);
         InstanceIdentifier<Flow> flowInstanceId = InstanceIdentifier.builder(Nodes.class)
@@ -168,6 +182,17 @@ public class FlowBasedServicesUtils {
         t.put(LogicalDatastoreType.CONFIGURATION, flowInstanceId, flow, true);
     }
 
+    public static void removeFlow(String flowRef, BigInteger dpId, WriteTransaction t) {
+        LOG.debug("Removing Ingress Flows");
+        FlowKey flowKey = new FlowKey(new FlowId(flowRef));
+        Node nodeDpn = buildInventoryDpnNode(dpId);
+        InstanceIdentifier<Flow> flowInstanceId = InstanceIdentifier.builder(Nodes.class)
+                .child(Node.class, nodeDpn.getKey()).augmentation(FlowCapableNode.class)
+                .child(Table.class, new TableKey(NwConstants.VLAN_INTERFACE_INGRESS_TABLE)).child(Flow.class, flowKey).build();
+
+        t.delete(LogicalDatastoreType.CONFIGURATION, flowInstanceId);
+    }
+
     private static Node buildInventoryDpnNode(BigInteger dpnId) {
         NodeId nodeId = new NodeId("openflow:" + dpnId);
         Node nodeDpn = new NodeBuilder().setId(nodeId).setKey(new NodeKey(nodeId)).build();
@@ -176,7 +201,7 @@ public class FlowBasedServicesUtils {
     }
 
     public static void installLPortDispatcherFlow(BigInteger dpId, BoundServices boundService, Interface iface,
-                                                  DataBroker dataBroker, WriteTransaction t, int interfaceTag) {
+                                                  WriteTransaction t, int interfaceTag) {
         LOG.debug("Installing LPort Dispatcher Flows {}, {}", dpId, iface);
         short serviceIndex = boundService.getServicePriority();
         String serviceRef = boundService.getServiceName();
@@ -209,31 +234,28 @@ public class FlowBasedServicesUtils {
         String flowRef = getFlowRef(dpId, iface.getName(), boundService);
         Flow ingressFlow = MDSALUtil.buildFlowNew(stypeOpenFlow.getDispatcherTableId(), flowRef,
                 boundService.getServicePriority(), serviceRef, 0, 0, stypeOpenFlow.getFlowCookie(), matches, instructions);
-        installFlow(dpId, ingressFlow, dataBroker, t);
+        installFlow(dpId, ingressFlow, t);
     }
 
-    public static void removeIngressFlow(Interface iface, BoundServices serviceOld, BigInteger dpId,
-                                         DataBroker dataBroker, WriteTransaction t) {
+    public static void removeIngressFlow(Interface iface, BoundServices serviceOld, BigInteger dpId, WriteTransaction t) {
         LOG.debug("Removing Ingress Flows");
         String flowKeyStr = getFlowRef(dpId, iface.getName(), serviceOld);
         FlowKey flowKey = new FlowKey(new FlowId(flowKeyStr));
         Node nodeDpn = buildInventoryDpnNode(dpId);
         InstanceIdentifier<Flow> flowInstanceId = InstanceIdentifier.builder(Nodes.class)
                 .child(Node.class, nodeDpn.getKey()).augmentation(FlowCapableNode.class)
-                .child(Table.class, new TableKey(IfmConstants.VLAN_INTERFACE_INGRESS_TABLE)).child(Flow.class, flowKey).build();
+                .child(Table.class, new TableKey(NwConstants.VLAN_INTERFACE_INGRESS_TABLE)).child(Flow.class, flowKey).build();
 
         t.delete(LogicalDatastoreType.CONFIGURATION, flowInstanceId);
     }
 
-    public static void removeLPortDispatcherFlow(BigInteger dpId, Interface iface, BoundServices boundServicesOld,
-                                                 DataBroker dataBroker, WriteTransaction t) {
+    public static void removeLPortDispatcherFlow(BigInteger dpId, Interface iface, BoundServices boundServicesOld, WriteTransaction t) {
         LOG.debug("Removing LPort Dispatcher Flows {}, {}", dpId, iface);
-        Long interfaceTag = FlowBasedServicesUtils.getLPortTag(iface, dataBroker);
 
         StypeOpenflow stypeOpenFlow = boundServicesOld.getAugmentation(StypeOpenflow.class);
-        String flowKeyStr = iface.getName() + boundServicesOld.getServicePriority() +
-                boundServicesOld.getServiceName() + stypeOpenFlow.getDispatcherTableId();
-        FlowKey flowKey = new FlowKey(new FlowId(flowKeyStr));
+        // build the flow and install it
+        String flowRef = getFlowRef(dpId, iface.getName(), boundServicesOld);
+        FlowKey flowKey = new FlowKey(new FlowId(flowRef));
         Node nodeDpn = buildInventoryDpnNode(dpId);
         InstanceIdentifier<Flow> flowInstanceId = InstanceIdentifier.builder(Nodes.class)
                 .child(Node.class, nodeDpn.getKey()).augmentation(FlowCapableNode.class)
@@ -243,7 +265,7 @@ public class FlowBasedServicesUtils {
     }
 
     private static String getFlowRef(BigInteger dpnId, String iface, BoundServices service) {
-        return new StringBuffer().append(dpnId).append(IfmConstants.VLAN_INTERFACE_INGRESS_TABLE).append(NwConstants.FLOWID_SEPARATOR)
+        return new StringBuffer().append(dpnId).append( NwConstants.VLAN_INTERFACE_INGRESS_TABLE).append(NwConstants.FLOWID_SEPARATOR)
                 .append(iface).append(NwConstants.FLOWID_SEPARATOR).append(service.getServiceName()).append(NwConstants.FLOWID_SEPARATOR)
                 .append(service.getServicePriority()).toString();
     }