Bug 5334 : Delete ELANport , ELAN instance VLAN and re-create
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / IfmUtil.java
index 44f754e86fa54b0f30f14a558384ae91afa4f5d3..0cd1a718c0a0d5888614e969ba41dff6ab1d118c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -14,13 +14,24 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 
 import com.google.common.base.Optional;
+
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
+import org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo;
+import org.opendaylight.vpnservice.interfacemgr.globals.VlanInterfaceInfo;
+import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
+import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
+import org.opendaylight.vpnservice.mdsalutil.ActionType;
+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.InterfaceType;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadata;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
@@ -36,6 +47,11 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.ReleaseIdInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.id.pools.IdPool;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.id.pools.IdPoolKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan.L2vlanMode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeGre;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeVxlan;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
@@ -54,6 +70,24 @@ public class IfmUtil {
         return split[1];
     }
 
+    public static BigInteger getDpnFromInterface(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState){
+        NodeConnectorId ncId = getNodeConnectorIdFromInterface(ifState);
+        if(ncId != null){
+            return new BigInteger(getDpnFromNodeConnectorId(ncId));
+        }
+        return null;
+    }
+    public static String getPortNoFromInterfaceName(String ifaceName, DataBroker broker) {
+        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
+                InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(ifaceName, broker);
+
+        String lowerLayerIf = ifState.getLowerLayerIf().get(0);
+        NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
+        String portNo = IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId);
+
+        return portNo;
+    }
+
     public static String getPortNoFromNodeConnectorId(NodeConnectorId portId) {
         /*
          * NodeConnectorId is of form 'openflow:dpnid:portnum'
@@ -105,6 +139,17 @@ public class IfmUtil {
         return strList;
     }
 
+    public static long getGroupId(long ifIndex, InterfaceInfo.InterfaceType infType) {
+        if (infType == InterfaceInfo.InterfaceType.LOGICAL_GROUP_INTERFACE) {
+            return ifIndex + IfmConstants.LOGICAL_GROUP_START;
+        }
+        else if (infType == InterfaceInfo.InterfaceType.VLAN_INTERFACE) {
+            return ifIndex + IfmConstants.VLAN_GROUP_START;
+        } else {
+            return ifIndex + IfmConstants.TRUNK_GROUP_START;
+        }
+    }
+
     public static List<String> getDpIdPortNameAndSuffixFromInterfaceName(String intfName) {
         List<String> strList = new ArrayList<>(3);
         int index1 = intfName.indexOf(":");
@@ -137,6 +182,67 @@ public class IfmUtil {
         return result;
     }
 
+    public static List<Action> getEgressActions(String interfaceName, long serviceTag, DataBroker dataBroker) {
+        List<ActionInfo> listActionInfo = new ArrayList<>();
+        List<ActionInfo> interfaceActionList = IfmUtil.getEgressActionInfosForInterface(interfaceName, serviceTag, dataBroker);
+        listActionInfo.addAll(interfaceActionList);
+        List<Action> actionsList = new ArrayList<>();
+        for (ActionInfo actionInfo : listActionInfo) {
+            actionsList.add(actionInfo.buildAction());
+        }
+        return actionsList;
+    }
+
+    public static List<Action> getEgressActionsForInterface(String interfaceName, DataBroker dataBroker) {
+        List<ActionInfo> listActionInfo = getEgressActionInfosForInterface(interfaceName, 0, dataBroker);
+        List<Action> actionsList = new ArrayList<>();
+        for (ActionInfo actionInfo : listActionInfo) {
+            actionsList.add(actionInfo.buildAction());
+        }
+        return actionsList;
+    }
+
+    public static List<ActionInfo> getEgressActionInfosForInterface(String interfaceName, int actionKeyStart, DataBroker dataBroker) {
+        Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(interfaceName),
+                dataBroker);
+        List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
+        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
+                InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
+
+        String lowerLayerIf = ifState.getLowerLayerIf().get(0);
+        NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
+        String portNo = IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId);
+        Class<? extends InterfaceType> ifType = interfaceInfo.getType();
+        if(L2vlan.class.equals(ifType)){
+            IfL2vlan vlanIface = interfaceInfo.getAugmentation(IfL2vlan.class);
+            LOG.trace("L2Vlan: {}",vlanIface);
+            long vlanVid = (vlanIface == null) ? 0 :
+                    vlanIface.getVlanId() == null ? 0 : vlanIface.getVlanId().getValue();
+            if (vlanVid != 0) {
+                listActionInfo.add(new ActionInfo(ActionType.push_vlan, new String[] {}, actionKeyStart));
+                actionKeyStart++;
+                listActionInfo.add(new ActionInfo(ActionType.set_field_vlan_vid,
+                        new String[] { Long.toString(vlanVid) }, actionKeyStart));
+                actionKeyStart++;
+            }
+            listActionInfo.add(new ActionInfo(ActionType.output, new String[] {portNo}, actionKeyStart));
+        }else if(Tunnel.class.equals(ifType)){
+            listActionInfo.add(new ActionInfo(ActionType.output, new String[] { portNo}, actionKeyStart));
+        }
+        return listActionInfo;
+    }
+
+    public static List<ActionInfo> getEgressActionInfosForInterface(String interfaceName, long serviceTag, DataBroker dataBroker) {
+        int actionKey = 0;
+        ActionInfo actionSetTunnel = new ActionInfo(ActionType.set_field_tunnel_id, new BigInteger[] {
+                BigInteger.valueOf(serviceTag)}, actionKey) ;
+        List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
+        listActionInfo.add(actionSetTunnel);
+        actionKey++;
+        listActionInfo.addAll(getEgressActionInfosForInterface(interfaceName, actionKey, dataBroker));
+        return listActionInfo;
+     }
+
     public static NodeId getNodeIdFromNodeConnectorId(NodeConnectorId ncId) {
         return new NodeId(ncId.getValue().substring(0,ncId.getValue().lastIndexOf(":")));
     }
@@ -195,9 +301,72 @@ public class IfmUtil {
 
     public static BigInteger getDpnId(DatapathId datapathId){
         if (datapathId != null) {
+            // Adding logs for a random issue spotted during datapath id conversion
+            LOG.info("Received datapathId {}",datapathId.getValue());
             String dpIdStr = datapathId.getValue().replace(":", "");
-            return new BigInteger(dpIdStr, 16);
+            LOG.info("Received datapathId {}",dpIdStr);
+            BigInteger dpnId =  new BigInteger(dpIdStr, 16);
+            LOG.info("After conversion datapathId {}",dpnId);
+            return dpnId;
         }
         return null;
     }
+
+    public static NodeConnectorId getNodeConnectorIdFromInterface(Interface iface, DataBroker dataBroker) {
+        return FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
+    }
+
+    public static NodeConnectorId getNodeConnectorIdFromInterface(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState){
+        if(ifState != null) {
+            List<String> ofportIds = ifState.getLowerLayerIf();
+            return new NodeConnectorId(ofportIds.get(0));
+        }
+        return null;
+    }
+
+    public static InterfaceInfo.InterfaceType getInterfaceType(Interface iface) {
+        InterfaceInfo.InterfaceType interfaceType =
+                org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo.InterfaceType.UNKNOWN_INTERFACE;
+        Class<? extends org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType> ifType = iface.getType();
+
+        if (ifType.isAssignableFrom(L2vlan.class)) {
+            interfaceType =  org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo.InterfaceType.VLAN_INTERFACE;
+        } else if (ifType.isAssignableFrom(Tunnel.class)) {
+            IfTunnel ifTunnel = iface.getAugmentation(IfTunnel.class);
+            Class<? extends  org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeBase> tunnelType = ifTunnel.getTunnelInterfaceType();
+            if (tunnelType.isAssignableFrom(TunnelTypeVxlan.class)) {
+                interfaceType = InterfaceInfo.InterfaceType.VXLAN_TRUNK_INTERFACE;
+            } else if (tunnelType.isAssignableFrom(TunnelTypeGre.class)) {
+                interfaceType = InterfaceInfo.InterfaceType.GRE_TRUNK_INTERFACE;
+            }
+        }
+        // TODO: Check if the below condition is still needed/valid
+        //else if (ifType.isAssignableFrom(InterfaceGroup.class)) {
+        //    interfaceType =  org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo.InterfaceType.LOGICAL_GROUP_INTERFACE;
+        //}
+        return interfaceType;
+    }
+
+    public static VlanInterfaceInfo getVlanInterfaceInfo(String interfaceName, Interface iface, BigInteger dpId){
+        IfL2vlan vlanIface = iface.getAugmentation(IfL2vlan.class);
+
+        short vlanId = 0;
+        //FIXME :Use this below thing properly
+        VlanInterfaceInfo vlanInterfaceInfo = new VlanInterfaceInfo(dpId, "someString", vlanId);
+
+        if (vlanIface != null) {
+            vlanId = vlanIface.getVlanId() == null ? 0 : vlanIface.getVlanId().getValue().shortValue();
+            L2vlanMode l2VlanMode = vlanIface.getL2vlanMode();
+
+            if (l2VlanMode == L2vlanMode.Transparent) {
+                vlanInterfaceInfo.setVlanTransparent(true);
+            }
+            if (l2VlanMode == L2vlanMode.NativeUntagged) {
+                vlanInterfaceInfo.setUntaggedVlan(true);
+            }
+            vlanInterfaceInfo.setVlanId(vlanId);
+
+        }
+        return vlanInterfaceInfo;
+    }
 }