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