Multiple VxLAN tunnels - egress service
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / IfmUtil.java
1 /*
2  * Copyright (c) 2016, 2017 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.genius.interfacemanager;
9
10 import static org.opendaylight.genius.interfacemanager.globals.InterfaceInfo.InterfaceType.GRE_TRUNK_INTERFACE;
11 import static org.opendaylight.genius.interfacemanager.globals.InterfaceInfo.InterfaceType.LOGICAL_GROUP_INTERFACE;
12 import static org.opendaylight.genius.interfacemanager.globals.InterfaceInfo.InterfaceType.MPLS_OVER_GRE;
13 import static org.opendaylight.genius.interfacemanager.globals.InterfaceInfo.InterfaceType.VLAN_INTERFACE;
14 import static org.opendaylight.genius.interfacemanager.globals.InterfaceInfo.InterfaceType.VXLAN_TRUNK_INTERFACE;
15
16 import com.google.common.base.Optional;
17 import com.google.common.collect.ImmutableMap;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import java.math.BigInteger;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.Future;
24 import org.apache.commons.lang3.BooleanUtils;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
30 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
31 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
32 import org.opendaylight.genius.interfacemanager.globals.VlanInterfaceInfo;
33 import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
34 import org.opendaylight.genius.mdsalutil.ActionInfo;
35 import org.opendaylight.genius.mdsalutil.MDSALUtil;
36 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
37 import org.opendaylight.genius.mdsalutil.NwConstants;
38 import org.opendaylight.genius.mdsalutil.actions.ActionGroup;
39 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
40 import org.opendaylight.genius.mdsalutil.actions.ActionOutput;
41 import org.opendaylight.genius.mdsalutil.actions.ActionPushVlan;
42 import org.opendaylight.genius.mdsalutil.actions.ActionRegLoad;
43 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldTunnelId;
44 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldVlanVid;
45 import org.opendaylight.genius.mdsalutil.actions.ActionSetTunnelDestinationIp;
46 import org.opendaylight.genius.mdsalutil.actions.ActionSetTunnelSourceIp;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCase;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadata;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdPools;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPoolKey;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan.L2vlanMode;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefs;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefsBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeGre;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeLogicalGroup;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeMplsOverGre;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlanGpe;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceBindings;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfoKey;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesKey;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
90 import org.opendaylight.yangtools.yang.binding.DataObject;
91 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
92 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
93 import org.opendaylight.yangtools.yang.common.RpcResult;
94 import org.slf4j.Logger;
95 import org.slf4j.LoggerFactory;
96
97 public class IfmUtil {
98     private static final Logger LOG = LoggerFactory.getLogger(IfmUtil.class);
99     private static final int INVALID_ID = 0;
100
101     private static final ImmutableMap<Class<? extends TunnelTypeBase>, InterfaceInfo.InterfaceType>
102         TUNNEL_TYPE_MAP = new ImmutableMap.Builder<Class<? extends TunnelTypeBase>, InterfaceInfo.InterfaceType>()
103             .put(TunnelTypeGre.class, GRE_TRUNK_INTERFACE).put(TunnelTypeMplsOverGre.class, MPLS_OVER_GRE)
104             .put(TunnelTypeVxlan.class, VXLAN_TRUNK_INTERFACE).put(TunnelTypeVxlanGpe.class, VXLAN_TRUNK_INTERFACE)
105             .put(TunnelTypeLogicalGroup.class, LOGICAL_GROUP_INTERFACE)
106             .build();
107
108     public static BigInteger getDpnFromNodeConnectorId(NodeConnectorId portId) {
109         /*
110          * NodeConnectorId is of form 'openflow:dpnid:portnum'
111          */
112         String[] split = portId.getValue().split(IfmConstants.OF_URI_SEPARATOR);
113         return new BigInteger(split[1]);
114     }
115
116     public static BigInteger getDpnFromInterface(
117             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
118                 .ietf.interfaces.rev140508.interfaces.state.Interface ifState) {
119         NodeConnectorId ncId = getNodeConnectorIdFromInterface(ifState);
120         if (ncId != null) {
121             return getDpnFromNodeConnectorId(ncId);
122         }
123         return null;
124     }
125
126     public static String getPortNoFromNodeConnectorId(NodeConnectorId portId) {
127         /*
128          * NodeConnectorId is of form 'openflow:dpnid:portnum'
129          */
130         String[] split = portId.getValue().split(IfmConstants.OF_URI_SEPARATOR);
131         return split[2];
132     }
133
134     public static Long getPortNumberFromNodeConnectorId(NodeConnectorId portId) {
135         String portNo = getPortNoFromNodeConnectorId(portId);
136         try {
137             return Long.valueOf(portNo);
138         } catch (NumberFormatException ex) {
139             LOG.trace("Unable to retrieve port number from nodeconnector id for {}", portId);
140         }
141         return IfmConstants.INVALID_PORT_NO;
142     }
143
144     public static NodeId buildDpnNodeId(BigInteger dpnId) {
145         return new NodeId(IfmConstants.OF_URI_PREFIX + dpnId);
146     }
147
148     public static InstanceIdentifier<Interface> buildId(String interfaceName) {
149         // TODO Make this generic and move to AbstractDataChangeListener or
150         // Utils.
151         InstanceIdentifierBuilder<Interface> idBuilder = InstanceIdentifier.builder(Interfaces.class)
152                 .child(Interface.class, new InterfaceKey(interfaceName));
153         return idBuilder.build();
154     }
155
156     public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn
157         .ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> buildStateInterfaceId(
158             String interfaceName) {
159         InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn
160             .ietf.params.xml.ns.yang.ietf.interfaces.rev140508
161                 .interfaces.state.Interface> idBuilder = InstanceIdentifier
162                 .builder(InterfacesState.class)
163                 .child(org.opendaylight.yang.gen.v1.urn
164                         .ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.class,
165                         new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
166                             .ietf.interfaces.rev140508.interfaces.state.InterfaceKey(
167                                 interfaceName));
168         InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
169             .ietf.interfaces.rev140508.interfaces.state.Interface> id = idBuilder
170                 .build();
171         return id;
172     }
173
174     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
175         .ietf.interfaces.rev140508.interfaces.state.InterfaceKey getStateInterfaceKeyFromName(
176             String name) {
177         return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
178                 .ietf.interfaces.rev140508.interfaces.state.InterfaceKey(
179                 name);
180     }
181
182     public static InstanceIdentifier<IdPool> getPoolId(String poolName) {
183         return InstanceIdentifier.builder(IdPools.class).child(IdPool.class, new IdPoolKey(poolName)).build();
184     }
185
186     public static long getGroupId(int ifIndex, InterfaceInfo.InterfaceType infType) {
187         if (infType == LOGICAL_GROUP_INTERFACE) {
188             return getLogicalTunnelSelectGroupId(ifIndex);
189         }
190         return 0;
191     }
192
193     public static <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
194             InstanceIdentifier<T> path, DataBroker broker) {
195         try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
196             return tx.read(datastoreType, path).get();
197         } catch (InterruptedException | ExecutionException e) {
198             LOG.error("Cannot read identifier", e);
199             throw new RuntimeException(e);
200         }
201     }
202
203     public static List<Action> getEgressActionsForInterface(String interfaceName, Long tunnelKey, Integer actionKey,
204             DataBroker dataBroker, Boolean isDefaultEgress) {
205         List<ActionInfo> listActionInfo = getEgressActionInfosForInterface(interfaceName, tunnelKey,
206                 actionKey == null ? 0 : actionKey, dataBroker, isDefaultEgress);
207         List<Action> actionsList = new ArrayList<>();
208         for (ActionInfo actionInfo : listActionInfo) {
209             actionsList.add(actionInfo.buildAction());
210         }
211         return actionsList;
212     }
213
214     public static List<Instruction> getEgressInstructionsForInterface(String interfaceName, Long tunnelKey,
215             DataBroker dataBroker, Boolean isDefaultEgress) {
216         List<Instruction> instructions = new ArrayList<>();
217         List<Action> actionList = MDSALUtil.buildActions(
218                 getEgressActionInfosForInterface(interfaceName, tunnelKey, 0, dataBroker, isDefaultEgress));
219         instructions.add(MDSALUtil.buildApplyActionsInstruction(actionList));
220         return instructions;
221     }
222
223     public static List<Instruction> getEgressInstructionsForInterface(Interface interfaceInfo, String portNo,
224                                                                       Long tunnelKey, boolean isDefaultEgress,
225                                                                       int ifIndex, long groupId) {
226         List<Instruction> instructions = new ArrayList<>();
227         InterfaceInfo.InterfaceType ifaceType = getInterfaceType(interfaceInfo);
228         List<Action> actionList = MDSALUtil.buildActions(
229                 getEgressActionInfosForInterface(interfaceInfo, portNo, ifaceType, tunnelKey, 0,
230                                                  isDefaultEgress, ifIndex, groupId));
231         instructions.add(MDSALUtil.buildApplyActionsInstruction(actionList));
232         return instructions;
233     }
234
235     public static List<ActionInfo> getEgressActionInfosForInterface(String interfaceName, int actionKeyStart,
236             DataBroker dataBroker, Boolean isDefaultEgress) {
237         return getEgressActionInfosForInterface(interfaceName, null, actionKeyStart, dataBroker, isDefaultEgress);
238     }
239
240     /**
241      * Returns a list of Actions to be taken when sending a packet over an
242      * interface.
243      *
244      * @param interfaceName
245      *            name of the interface
246      * @param tunnelKey
247      *            Optional.
248      * @param actionKeyStart
249      *            action key
250      * @param dataBroker
251      *            databroker
252      * @return list of actions
253      */
254     public static List<ActionInfo> getEgressActionInfosForInterface(String interfaceName, Long tunnelKey,
255             int actionKeyStart, DataBroker dataBroker, Boolean isDefaultEgress) {
256         Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(interfaceName),
257                 dataBroker);
258         if (interfaceInfo == null) {
259             throw new NullPointerException("Interface information not present in config DS for " + interfaceName);
260         }
261         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
262             .ietf.interfaces.rev140508.interfaces.state.Interface ifState = InterfaceManagerCommonUtils
263                 .getInterfaceState(interfaceName, dataBroker);
264         if (ifState == null) {
265             throw new NullPointerException("Interface information not present in oper DS for " + interfaceName);
266         }
267         String lowerLayerIf = ifState.getLowerLayerIf().get(0);
268         NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
269         String portNo = IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId);
270
271         InterfaceInfo.InterfaceType ifaceType = getInterfaceType(interfaceInfo);
272         return getEgressActionInfosForInterface(interfaceInfo, portNo, ifaceType, tunnelKey, actionKeyStart,
273                 isDefaultEgress, ifState.getIfIndex(), 0);
274     }
275
276     /**
277      * Returns the list of egress actions for a given interface.
278      *
279      * @param interfaceInfo the interface to look up
280      * @param portNo port number
281      * @param ifaceType the type of the interface
282      * @param tunnelKey the tunnel key
283      * @param actionKeyStart the start for the first key assigned for the new actions
284      * @param isDefaultEgress if it is the default egress
285      * @param ifIndex interface index
286      * @return list of actions for the interface
287      */
288     // The following suppression is for javac, not for checkstyle
289     @SuppressWarnings("fallthrough")
290     public static List<ActionInfo> getEgressActionInfosForInterface(Interface interfaceInfo, String portNo,
291             InterfaceInfo.InterfaceType ifaceType, Long tunnelKey, int actionKeyStart, boolean isDefaultEgress,
292             int ifIndex, long groupId) {
293         List<ActionInfo> result = new ArrayList<>();
294         switch (ifaceType) {
295             case MPLS_OVER_GRE:
296                 // fall through
297             case GRE_TRUNK_INTERFACE:
298                 if (!isDefaultEgress) {
299                     // TODO tunnel_id to encode GRE key, once it is supported
300                     // Until then, tunnel_id should be "cleaned", otherwise it
301                     // stores the value coming from a VXLAN tunnel
302                     if (tunnelKey == null) {
303                         tunnelKey = 0L;
304                     }
305                 }
306                 // fall through
307             case VXLAN_TRUNK_INTERFACE:
308                 if (!isDefaultEgress) {
309                     if (tunnelKey != null) {
310                         result.add(new ActionSetFieldTunnelId(actionKeyStart++, BigInteger.valueOf(tunnelKey)));
311                     }
312                 } else {
313                     // For OF Tunnels default egress actions need to set tunnelIps
314                     IfTunnel ifTunnel = interfaceInfo.getAugmentation(IfTunnel.class);
315                     if (BooleanUtils.isTrue(ifTunnel.isTunnelRemoteIpFlow()
316                             && ifTunnel.getTunnelDestination() != null)) {
317                         result.add(new ActionSetTunnelDestinationIp(actionKeyStart++, ifTunnel.getTunnelDestination()));
318                     }
319                     if (BooleanUtils.isTrue(ifTunnel.isTunnelSourceIpFlow()
320                             && ifTunnel.getTunnelSource() != null)) {
321                         result.add(new ActionSetTunnelSourceIp(actionKeyStart++, ifTunnel.getTunnelSource()));
322                     }
323                 }
324                 // fall through
325             case VLAN_INTERFACE:
326                 if (isDefaultEgress) {
327                     IfL2vlan vlanIface = interfaceInfo.getAugmentation(IfL2vlan.class);
328                     LOG.trace("L2Vlan: {}", vlanIface);
329                     boolean isVlanTransparent = false;
330                     int vlanVid = 0;
331                     if (vlanIface != null) {
332                         vlanVid = vlanIface.getVlanId() == null ? 0 : vlanIface.getVlanId().getValue();
333                         isVlanTransparent = vlanIface.getL2vlanMode() == IfL2vlan.L2vlanMode.Transparent;
334                     }
335                     if (vlanVid != 0 && !isVlanTransparent) {
336                         result.add(new ActionPushVlan(actionKeyStart++));
337                         result.add(new ActionSetFieldVlanVid(actionKeyStart++, vlanVid));
338                     }
339                     result.add(new ActionOutput(actionKeyStart++, new Uri(portNo)));
340                 } else {
341                     addEgressActionInfosForInterface(ifIndex, actionKeyStart, result);
342                 }
343                 break;
344             case LOGICAL_GROUP_INTERFACE:
345                 if (isDefaultEgress) {
346                     result.add(new ActionGroup(groupId));
347                 } else {
348                     addEgressActionInfosForInterface(ifIndex, actionKeyStart, result);
349                 }
350                 break;
351
352             default:
353                 LOG.warn("Interface Type {} not handled yet", ifaceType);
354                 break;
355         }
356         return result;
357     }
358
359     public static void addEgressActionInfosForInterface(int ifIndex, int actionKeyStart, List<ActionInfo> result) {
360         long regValue = MetaDataUtil.getReg6ValueForLPortDispatcher(ifIndex, NwConstants.DEFAULT_SERVICE_INDEX);
361         result.add(new ActionRegLoad(actionKeyStart++, NxmNxReg6.class, IfmConstants.REG6_START_INDEX,
362                 IfmConstants.REG6_END_INDEX, regValue));
363         result.add(new ActionNxResubmit(actionKeyStart++, NwConstants.EGRESS_LPORT_DISPATCHER_TABLE));
364     }
365
366     public static NodeId getNodeIdFromNodeConnectorId(NodeConnectorId ncId) {
367         return new NodeId(ncId.getValue().substring(0, ncId.getValue().lastIndexOf(":")));
368     }
369
370     public static BigInteger[] mergeOpenflowMetadataWriteInstructions(List<Instruction> instructions) {
371         BigInteger metadata = new BigInteger("0", 16);
372         BigInteger metadataMask = new BigInteger("0", 16);
373         if (instructions != null && !instructions.isEmpty()) {
374             // check if metadata write instruction is present
375             for (Instruction instruction : instructions) {
376                 org.opendaylight.yang.gen.v1.urn.opendaylight.flow
377                     .types.rev131026.instruction.Instruction actualInstruction = instruction
378                         .getInstruction();
379                 if (actualInstruction instanceof WriteMetadataCase) {
380                     WriteMetadataCase writeMetaDataInstruction = (WriteMetadataCase) actualInstruction;
381                     WriteMetadata availableMetaData = writeMetaDataInstruction.getWriteMetadata();
382                     metadata = metadata.or(availableMetaData.getMetadata());
383                     metadataMask = metadataMask.or(availableMetaData.getMetadataMask());
384                 }
385             }
386         }
387         return new BigInteger[] { metadata, metadataMask };
388     }
389
390     public static Integer allocateId(IdManagerService idManager, String poolName, String idKey) {
391         AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
392         try {
393             Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
394             RpcResult<AllocateIdOutput> rpcResult = result.get();
395             if (rpcResult.isSuccessful()) {
396                 return rpcResult.getResult().getIdValue().intValue();
397             } else {
398                 LOG.warn("RPC Call to Get Unique Id returned with Errors {}", rpcResult.getErrors());
399             }
400         } catch (InterruptedException | ExecutionException e) {
401             LOG.warn("Exception when getting Unique Id", e);
402         }
403         return INVALID_ID;
404     }
405
406     public static void releaseId(IdManagerService idManager, String poolName, String idKey) {
407         ReleaseIdInput idInput = new ReleaseIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
408         try {
409             Future<RpcResult<Void>> result = idManager.releaseId(idInput);
410             RpcResult<Void> rpcResult = result.get();
411             if (!rpcResult.isSuccessful()) {
412                 LOG.warn("RPC Call to release Id {} with Key {} returned with Errors {}", idKey, rpcResult.getErrors());
413             }
414         } catch (InterruptedException | ExecutionException e) {
415             LOG.warn("Exception when releasing Id for key {}", idKey, e);
416         }
417     }
418
419     public static BigInteger getDpnId(DatapathId datapathId) {
420         if (datapathId != null) {
421             // Adding logs for a random issue spotted during datapath id
422             // conversion
423             String dpIdStr = datapathId.getValue().replace(":", "");
424             return new BigInteger(dpIdStr, 16);
425         }
426         return null;
427     }
428
429     public static NodeConnectorId getNodeConnectorIdFromInterface(String interfaceName, DataBroker dataBroker) {
430         return FlowBasedServicesUtils.getNodeConnectorIdFromInterface(interfaceName, dataBroker);
431     }
432
433     public static NodeConnectorId getNodeConnectorIdFromInterface(
434             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
435                 .ietf.interfaces.rev140508.interfaces.state.Interface ifState) {
436         if (ifState != null) {
437             List<String> ofportIds = ifState.getLowerLayerIf();
438             return new NodeConnectorId(ofportIds.get(0));
439         }
440         return null;
441     }
442
443     public static InterfaceInfo.InterfaceType getInterfaceType(Interface iface) {
444         InterfaceInfo.InterfaceType interfaceType = org.opendaylight
445                 .genius.interfacemanager.globals.InterfaceInfo.InterfaceType.UNKNOWN_INTERFACE;
446         Class<? extends org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
447                 .ietf.interfaces.rev140508.InterfaceType> ifType = iface
448                 .getType();
449
450         if (ifType.isAssignableFrom(L2vlan.class)) {
451             interfaceType = VLAN_INTERFACE;
452         } else if (ifType.isAssignableFrom(Tunnel.class)) {
453             IfTunnel ifTunnel = iface.getAugmentation(IfTunnel.class);
454             Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight
455                     .genius.interfacemanager.rev160406.TunnelTypeBase> tunnelType = ifTunnel
456                     .getTunnelInterfaceType();
457             interfaceType = tunnelType.isAssignableFrom(TunnelTypeLogicalGroup.class)
458                     ? InterfaceInfo.InterfaceType.LOGICAL_GROUP_INTERFACE :  TUNNEL_TYPE_MAP.get(tunnelType);
459         }
460         return interfaceType;
461     }
462
463     public static VlanInterfaceInfo getVlanInterfaceInfo(String interfaceName, Interface iface, BigInteger dpId) {
464         short vlanId = 0;
465         String portName = null;
466         IfL2vlan vlanIface = iface.getAugmentation(IfL2vlan.class);
467         ParentRefs parentRefs = iface.getAugmentation(ParentRefs.class);
468         if (parentRefs != null && parentRefs.getParentInterface() != null) {
469             portName = parentRefs.getParentInterface();
470         } else {
471             LOG.warn("Portname set to null since parentRef is Null");
472         }
473         VlanInterfaceInfo vlanInterfaceInfo = new VlanInterfaceInfo(dpId, portName, vlanId);
474
475         if (vlanIface != null) {
476             vlanId = vlanIface.getVlanId() == null ? 0 : vlanIface.getVlanId().getValue().shortValue();
477             L2vlanMode l2VlanMode = vlanIface.getL2vlanMode();
478
479             if (l2VlanMode == L2vlanMode.Transparent) {
480                 vlanInterfaceInfo.setVlanTransparent(true);
481             }
482             if (l2VlanMode == L2vlanMode.NativeUntagged) {
483                 vlanInterfaceInfo.setUntaggedVlan(true);
484             }
485             vlanInterfaceInfo.setVlanId(vlanId);
486
487         }
488         return vlanInterfaceInfo;
489     }
490
491     public static BigInteger getDeadBeefBytesForMac() {
492         return new BigInteger("FFFFFFFF", 16).and(new BigInteger(IfmConstants.DEAD_BEEF_MAC_PREFIX, 16)).shiftLeft(16);
493     }
494
495     public static BigInteger fillPortNumberToMac(long portNumber) {
496         return new BigInteger("FFFF", 16).and(BigInteger.valueOf(portNumber));
497     }
498
499     public static String generateMacAddress(long portNo) {
500         String unformattedMAC = getDeadBeefBytesForMac().or(fillPortNumberToMac(portNo)).toString(16);
501         return unformattedMAC.replaceAll("(.{2})", "$1" + IfmConstants.MAC_SEPARATOR).substring(0,
502                 IfmConstants.MAC_STRING_LENGTH);
503     }
504
505     public static PhysAddress getPhyAddress(long portNo, FlowCapableNodeConnector flowCapableNodeConnector) {
506         String southboundMacAddress = flowCapableNodeConnector.getHardwareAddress().getValue();
507         if (IfmConstants.INVALID_MAC.equals(southboundMacAddress)) {
508             LOG.debug("Invalid MAC Address received for {}, generating MAC Address",
509                     flowCapableNodeConnector.getName());
510             southboundMacAddress = generateMacAddress(portNo);
511         }
512         return new PhysAddress(southboundMacAddress);
513     }
514
515     public static void updateInterfaceParentRef(WriteTransaction writeTransaction, String interfaceName,
516             String parentInterface) {
517         InstanceIdentifier<ParentRefs> parentRefIdentifier = InstanceIdentifier.builder(Interfaces.class)
518                 .child(Interface.class, new InterfaceKey(interfaceName)).augmentation(ParentRefs.class).build();
519         ParentRefs parentRefs = new ParentRefsBuilder().setParentInterface(parentInterface).build();
520         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, parentRefIdentifier, parentRefs);
521         LOG.debug(
522                 "Updating parentRefInterface for interfaceName {}. "
523                         + "interfaceKey {}, with parentRef augmentation pointing to {}",
524                 interfaceName, new InterfaceKey(interfaceName), parentInterface);
525     }
526
527     public static InstanceIdentifier<BoundServices> buildBoundServicesIId(short servicePriority, String interfaceName,
528             Class<? extends ServiceModeBase> serviceMode) {
529         return InstanceIdentifier.builder(ServiceBindings.class)
530                 .child(ServicesInfo.class, new ServicesInfoKey(interfaceName, serviceMode))
531                 .child(BoundServices.class, new BoundServicesKey(servicePriority)).build();
532     }
533
534     public static void bindService(WriteTransaction writeTransaction, String interfaceName, BoundServices serviceInfo,
535             Class<? extends ServiceModeBase> serviceMode) {
536         LOG.info("Binding Service {} for : {}", serviceInfo.getServiceName(), interfaceName);
537         InstanceIdentifier<BoundServices> boundServicesInstanceIdentifier = buildBoundServicesIId(
538                 serviceInfo.getServicePriority(), interfaceName, serviceMode);
539         writeTransaction.put(LogicalDatastoreType.CONFIGURATION, boundServicesInstanceIdentifier, serviceInfo, true);
540     }
541
542     public static void unbindService(DataBroker dataBroker, String interfaceName,
543             InstanceIdentifier<BoundServices> boundServicesInstanceIdentifier) {
544         LOG.info("Unbinding Service from : {}", interfaceName);
545         DataStoreJobCoordinator dataStoreJobCoordinator = DataStoreJobCoordinator.getInstance();
546         dataStoreJobCoordinator.enqueueJob(interfaceName, () -> {
547             WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
548             writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, boundServicesInstanceIdentifier);
549             List<ListenableFuture<Void>> futures = new ArrayList<>();
550             futures.add(writeTransaction.submit());
551             return futures;
552         });
553     }
554
555     public static long getLogicalTunnelSelectGroupId(int lportTag) {
556         return org.opendaylight.genius.interfacemanager.globals.IfmConstants.VXLAN_GROUPID_MIN + lportTag;
557     }
558 }