BUG:5128 Elan Multi-DPN datapath fix
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / IfmUtil.java
1 /*
2  * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.vpnservice.interfacemgr;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.Future;
15
16 import com.google.common.base.Optional;
17
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
22 import org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo;
23 import org.opendaylight.vpnservice.interfacemgr.globals.VlanInterfaceInfo;
24 import org.opendaylight.vpnservice.interfacemgr.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
25 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
26 import org.opendaylight.vpnservice.mdsalutil.ActionType;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadata;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeGre;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.AllocateIdInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.AllocateIdInputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.AllocateIdOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdPools;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.ReleaseIdInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.ReleaseIdInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.id.pools.IdPool;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.id.pools.IdPoolKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan.L2vlanMode;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
55 import org.opendaylight.yangtools.yang.binding.DataObject;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
58 import org.opendaylight.yangtools.yang.common.RpcResult;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 public class IfmUtil {
63     private static final Logger LOG = LoggerFactory.getLogger(IfmUtil.class);
64     private static final int INVALID_ID = 0;
65     public static String getDpnFromNodeConnectorId(NodeConnectorId portId) {
66         /*
67          * NodeConnectorId is of form 'openflow:dpnid:portnum'
68          */
69         String[] split = portId.getValue().split(IfmConstants.OF_URI_SEPARATOR);
70         return split[1];
71     }
72
73     public static String getPortNoFromNodeConnectorId(NodeConnectorId portId) {
74         /*
75          * NodeConnectorId is of form 'openflow:dpnid:portnum'
76          */
77         String[] split = portId.getValue().split(IfmConstants.OF_URI_SEPARATOR);
78         return split[2];
79     }
80
81     public static NodeId buildDpnNodeId(BigInteger dpnId) {
82         return new NodeId(IfmConstants.OF_URI_PREFIX + dpnId);
83     }
84
85     public static InstanceIdentifier<Interface> buildId(String interfaceName) {
86         //TODO Make this generic and move to AbstractDataChangeListener or Utils.
87         InstanceIdentifierBuilder<Interface> idBuilder =
88                 InstanceIdentifier.builder(Interfaces.class).child(Interface.class, new InterfaceKey(interfaceName));
89         InstanceIdentifier<Interface> id = idBuilder.build();
90         return id;
91     }
92
93     public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> buildStateInterfaceId(String interfaceName) {
94         InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> idBuilder =
95                 InstanceIdentifier.builder(InterfacesState.class)
96                 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.class,
97                         new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey(interfaceName));
98         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> id = idBuilder.build();
99         return id;
100     }
101
102     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey getStateInterfaceKeyFromName(
103                     String name) {
104         return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey(name);
105     }
106
107     public static InstanceIdentifier<IdPool> getPoolId(String poolName){
108         InstanceIdentifier.InstanceIdentifierBuilder<IdPool> idBuilder =
109                         InstanceIdentifier.builder(IdPools.class).child(IdPool.class, new IdPoolKey(poolName));
110         InstanceIdentifier<IdPool> id = idBuilder.build();
111         return id;
112     }
113
114     public static List<String> getPortNameAndSuffixFromInterfaceName(String intfName) {
115         List<String> strList = new ArrayList<>(2);
116         int index = intfName.indexOf(":");
117         if (index != -1) {
118             strList.add(0, intfName.substring(0, index));
119             strList.add(1, intfName.substring(index));
120         }
121         return strList;
122     }
123
124     public static long getGroupId(long ifIndex, InterfaceInfo.InterfaceType infType) {
125         if (infType == InterfaceInfo.InterfaceType.LOGICAL_GROUP_INTERFACE) {
126             return ifIndex + IfmConstants.LOGICAL_GROUP_START;
127         }
128         else if (infType == InterfaceInfo.InterfaceType.VLAN_INTERFACE) {
129             return ifIndex + IfmConstants.VLAN_GROUP_START;
130         } else {
131             return ifIndex + IfmConstants.TRUNK_GROUP_START;
132         }
133     }
134
135     public static List<String> getDpIdPortNameAndSuffixFromInterfaceName(String intfName) {
136         List<String> strList = new ArrayList<>(3);
137         int index1 = intfName.indexOf(":");
138         if (index1 != -1) {
139             int index2 = intfName.indexOf(":", index1 + 1 );
140             strList.add(0, intfName.substring(0, index1));
141             if (index2 != -1) {
142                 strList.add(1, intfName.substring(index1, index2));
143                 strList.add(2, intfName.substring(index2));
144             } else {
145                 strList.add(1, intfName.substring(index1));
146                 strList.add(2, "");
147             }
148         }
149         return strList;
150     }
151
152     public static <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
153                                                           InstanceIdentifier<T> path, DataBroker broker) {
154
155         ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
156
157         Optional<T> result = Optional.absent();
158         try {
159             result = tx.read(datastoreType, path).get();
160         } catch (Exception e) {
161             throw new RuntimeException(e);
162         }
163
164         return result;
165     }
166
167     public static List<Action> getEgressActions(String interfaceName, long serviceTag, DataBroker dataBroker) {
168         List<ActionInfo> listActionInfo = new ArrayList<>();
169         List<ActionInfo> interfaceActionList = IfmUtil.getEgressActionInfosForInterface(interfaceName, serviceTag, dataBroker);
170         listActionInfo.addAll(interfaceActionList);
171         List<Action> actionsList = new ArrayList<>();
172         for (ActionInfo actionInfo : listActionInfo) {
173             actionsList.add(actionInfo.buildAction());
174         }
175         return actionsList;
176     }
177
178     public static List<Action> getEgressActionsForInterface(String interfaceName, DataBroker dataBroker) {
179         List<ActionInfo> listActionInfo = getEgressActionInfosForInterface(interfaceName, 0, dataBroker);
180         List<Action> actionsList = new ArrayList<>();
181         for (ActionInfo actionInfo : listActionInfo) {
182             actionsList.add(actionInfo.buildAction());
183         }
184         return actionsList;
185     }
186
187     public static List<ActionInfo> getEgressActionInfosForInterface(String interfaceName, int actionKeyStart, DataBroker dataBroker) {
188         Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(interfaceName),
189                 dataBroker);
190         List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
191         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
192                 InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
193
194         String lowerLayerIf = ifState.getLowerLayerIf().get(0);
195         NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
196         String portNo = IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId);
197         Class<? extends InterfaceType> ifType = interfaceInfo.getType();
198         if(L2vlan.class.equals(ifType)){
199             IfL2vlan vlanIface = interfaceInfo.getAugmentation(IfL2vlan.class);
200             LOG.trace("L2Vlan: {}",vlanIface);
201             long vlanVid = (vlanIface == null) ? 0 : vlanIface.getVlanId().getValue();
202             if (vlanVid != 0) {
203                 listActionInfo.add(new ActionInfo(ActionType.push_vlan, new String[] {}, actionKeyStart));
204                 actionKeyStart++;
205                 listActionInfo.add(new ActionInfo(ActionType.set_field_vlan_vid,
206                         new String[] { Long.toString(vlanVid) }, actionKeyStart));
207                 actionKeyStart++;
208             }
209             listActionInfo.add(new ActionInfo(ActionType.output, new String[] {portNo}, actionKeyStart));
210         }else if(Tunnel.class.equals(ifType)){
211             listActionInfo.add(new ActionInfo(ActionType.output, new String[] { portNo}, actionKeyStart));
212         }
213         return listActionInfo;
214     }
215
216     public static List<ActionInfo> getEgressActionInfosForInterface(String interfaceName, long serviceTag, DataBroker dataBroker) {
217         int actionKey = 0;
218         ActionInfo actionSetTunnel = new ActionInfo(ActionType.set_field_tunnel_id, new BigInteger[] {
219                 BigInteger.valueOf(serviceTag)}, actionKey) ;
220         List<ActionInfo> listActionInfo = new ArrayList<ActionInfo>();
221         listActionInfo.add(actionSetTunnel);
222         actionKey++;
223         listActionInfo.addAll(getEgressActionInfosForInterface(interfaceName, actionKey, dataBroker));
224         return listActionInfo;
225      }
226
227     public static NodeId getNodeIdFromNodeConnectorId(NodeConnectorId ncId) {
228         return new NodeId(ncId.getValue().substring(0,ncId.getValue().lastIndexOf(":")));
229     }
230
231     public static BigInteger[] mergeOpenflowMetadataWriteInstructions(List<Instruction> instructions) {
232         BigInteger metadata = new BigInteger("0", 16);
233         BigInteger metadataMask = new BigInteger("0", 16);
234         if (instructions != null && !instructions.isEmpty()) {
235             // check if metadata write instruction is present
236             for (Instruction instruction : instructions) {
237                 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction actualInstruction = instruction.getInstruction();
238                 if (actualInstruction instanceof WriteMetadataCase) {
239                     WriteMetadataCase writeMetaDataInstruction = (WriteMetadataCase) actualInstruction ;
240                     WriteMetadata availableMetaData = writeMetaDataInstruction.getWriteMetadata();
241                     metadata = metadata.or(availableMetaData.getMetadata());
242                     metadataMask = metadataMask.or(availableMetaData.getMetadataMask());
243                 }
244             }
245         }
246         return new BigInteger[] { metadata, metadataMask };
247     }
248
249     public static Integer allocateId(IdManagerService idManager, String poolName, String idKey) {
250         AllocateIdInput getIdInput = new AllocateIdInputBuilder()
251                 .setPoolName(poolName)
252                 .setIdKey(idKey).build();
253         try {
254             Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
255             RpcResult<AllocateIdOutput> rpcResult = result.get();
256             if(rpcResult.isSuccessful()) {
257                 return rpcResult.getResult().getIdValue().intValue();
258             } else {
259                 LOG.warn("RPC Call to Get Unique Id returned with Errors {}", rpcResult.getErrors());
260             }
261         } catch (InterruptedException | ExecutionException e) {
262             LOG.warn("Exception when getting Unique Id",e);
263         }
264         return INVALID_ID;
265     }
266
267     public static void releaseId(IdManagerService idManager, String poolName, String idKey) {
268         ReleaseIdInput idInput = new ReleaseIdInputBuilder()
269                 .setPoolName(poolName)
270                 .setIdKey(idKey).build();
271         try {
272             Future<RpcResult<Void>> result = idManager.releaseId(idInput);
273             RpcResult<Void> rpcResult = result.get();
274             if(!rpcResult.isSuccessful()) {
275                 LOG.warn("RPC Call to release Id {} with Key {} returned with Errors {}",
276                         idKey, rpcResult.getErrors());
277             }
278         } catch (InterruptedException | ExecutionException e) {
279             LOG.warn("Exception when releasing Id for key {}", idKey, e);
280         }
281     }
282
283     public static BigInteger getDpnId(DatapathId datapathId){
284         if (datapathId != null) {
285             String dpIdStr = datapathId.getValue().replace(":", "");
286             return new BigInteger(dpIdStr, 16);
287         }
288         return null;
289     }
290
291     public static NodeConnectorId getNodeConnectorIdFromInterface(Interface iface, DataBroker dataBroker) {
292         return FlowBasedServicesUtils.getNodeConnectorIdFromInterface(iface, dataBroker);
293     }
294
295     public static NodeConnectorId getNodeConnectorIdFromInterface(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState){
296         if(ifState != null) {
297             List<String> ofportIds = ifState.getLowerLayerIf();
298             return new NodeConnectorId(ofportIds.get(0));
299         }
300         return null;
301     }
302
303     public static InterfaceInfo.InterfaceType getInterfaceType(Interface iface) {
304         InterfaceInfo.InterfaceType interfaceType =
305                 org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo.InterfaceType.UNKNOWN_INTERFACE;
306         Class<? extends org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType> ifType = iface.getType();
307
308         if (ifType.isAssignableFrom(L2vlan.class)) {
309             interfaceType =  org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo.InterfaceType.VLAN_INTERFACE;
310         } else if (ifType.isAssignableFrom(Tunnel.class)) {
311             IfTunnel ifTunnel = iface.getAugmentation(IfTunnel.class);
312             Class<? extends  org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeBase> tunnelType = ifTunnel.getTunnelInterfaceType();
313             if (tunnelType.isAssignableFrom(TunnelTypeVxlan.class)) {
314                 interfaceType = InterfaceInfo.InterfaceType.VXLAN_TRUNK_INTERFACE;
315             } else if (tunnelType.isAssignableFrom(TunnelTypeGre.class)) {
316                 interfaceType = InterfaceInfo.InterfaceType.GRE_TRUNK_INTERFACE;
317             }
318         }
319         // TODO: Check if the below condition is still needed/valid
320         //else if (ifType.isAssignableFrom(InterfaceGroup.class)) {
321         //    interfaceType =  org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo.InterfaceType.LOGICAL_GROUP_INTERFACE;
322         //}
323         return interfaceType;
324     }
325
326     public static VlanInterfaceInfo getVlanInterfaceInfo(String interfaceName, Interface iface, BigInteger dpId){
327         IfL2vlan vlanIface = iface.getAugmentation(IfL2vlan.class);
328
329         short vlanId = 0;
330         //FIXME :Use this below thing properly
331         VlanInterfaceInfo vlanInterfaceInfo = new VlanInterfaceInfo(dpId, "someString", vlanId);
332
333         if (vlanIface != null) {
334             vlanId = vlanIface.getVlanId() == null ? 0 : vlanIface.getVlanId().getValue().shortValue();
335             L2vlanMode l2VlanMode = vlanIface.getL2vlanMode();
336
337             if (l2VlanMode == L2vlanMode.Transparent) {
338                 vlanInterfaceInfo.setVlanTransparent(true);
339             }
340             if (l2VlanMode == L2vlanMode.NativeUntagged) {
341                 vlanInterfaceInfo.setUntaggedVlan(true);
342             }
343             vlanInterfaceInfo.setVlanId(vlanId);
344
345         }
346         return vlanInterfaceInfo;
347     }
348 }