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