2 * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.genius.interfacemanager;
10 import static org.opendaylight.controller.md.sal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12 import static org.opendaylight.genius.interfacemanager.globals.InterfaceInfo.InterfaceType.GRE_TRUNK_INTERFACE;
13 import static org.opendaylight.genius.interfacemanager.globals.InterfaceInfo.InterfaceType.LOGICAL_GROUP_INTERFACE;
14 import static org.opendaylight.genius.interfacemanager.globals.InterfaceInfo.InterfaceType.MPLS_OVER_GRE;
15 import static org.opendaylight.genius.interfacemanager.globals.InterfaceInfo.InterfaceType.VLAN_INTERFACE;
16 import static org.opendaylight.genius.interfacemanager.globals.InterfaceInfo.InterfaceType.VXLAN_TRUNK_INTERFACE;
18 import com.google.common.base.Optional;
19 import com.google.common.collect.ImmutableMap;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22 import java.math.BigInteger;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.Future;
28 import org.apache.commons.lang3.BooleanUtils;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
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.TypedWriteTransaction;
36 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
37 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
38 import org.opendaylight.genius.interfacemanager.globals.VlanInterfaceInfo;
39 import org.opendaylight.genius.mdsalutil.ActionInfo;
40 import org.opendaylight.genius.mdsalutil.MDSALUtil;
41 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
42 import org.opendaylight.genius.mdsalutil.NwConstants;
43 import org.opendaylight.genius.mdsalutil.actions.ActionGroup;
44 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
45 import org.opendaylight.genius.mdsalutil.actions.ActionOutput;
46 import org.opendaylight.genius.mdsalutil.actions.ActionPushVlan;
47 import org.opendaylight.genius.mdsalutil.actions.ActionRegLoad;
48 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldTunnelId;
49 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldVlanVid;
50 import org.opendaylight.genius.mdsalutil.actions.ActionSetTunnelDestinationIp;
51 import org.opendaylight.genius.mdsalutil.actions.ActionSetTunnelSourceIp;
52 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.L2vlan;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.Tunnel;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCase;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadata;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdPools;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdOutput;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPoolKey;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan.L2vlanMode;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefs;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefsBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeGre;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeLogicalGroup;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeMplsOverGre;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlanGpe;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceBindings;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfoKey;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesKey;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
97 import org.opendaylight.yangtools.yang.binding.DataObject;
98 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
99 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
100 import org.opendaylight.yangtools.yang.common.RpcResult;
101 import org.slf4j.Logger;
102 import org.slf4j.LoggerFactory;
104 public final class IfmUtil {
106 private static final Logger LOG = LoggerFactory.getLogger(IfmUtil.class);
107 private static final int INVALID_ID = 0;
110 throw new IllegalStateException("Utility class");
113 private static final ImmutableMap<Class<? extends TunnelTypeBase>, InterfaceInfo.InterfaceType>
114 TUNNEL_TYPE_MAP = new ImmutableMap.Builder<Class<? extends TunnelTypeBase>, InterfaceInfo.InterfaceType>()
115 .put(TunnelTypeGre.class, GRE_TRUNK_INTERFACE).put(TunnelTypeMplsOverGre.class, MPLS_OVER_GRE)
116 .put(TunnelTypeVxlan.class, VXLAN_TRUNK_INTERFACE).put(TunnelTypeVxlanGpe.class, VXLAN_TRUNK_INTERFACE)
117 .put(TunnelTypeLogicalGroup.class, LOGICAL_GROUP_INTERFACE)
120 public static BigInteger getDpnFromNodeConnectorId(NodeConnectorId portId) {
121 return new BigInteger(getDpnStringFromNodeConnectorId(portId));
124 public static String getDpnStringFromNodeConnectorId(NodeConnectorId portId) {
126 * NodeConnectorId is of form 'openflow:dpnid:portnum'
128 return portId.getValue().split(IfmConstants.OF_URI_SEPARATOR)[1];
131 public static BigInteger getDpnFromInterface(
132 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
133 .ietf.interfaces.rev140508.interfaces.state.Interface ifState) {
134 NodeConnectorId ncId = getNodeConnectorIdFromInterface(ifState);
136 return getDpnFromNodeConnectorId(ncId);
141 public static String getPortNoFromNodeConnectorId(NodeConnectorId portId) {
143 * NodeConnectorId is of form 'openflow:dpnid:portnum'
145 String[] split = portId.getValue().split(IfmConstants.OF_URI_SEPARATOR);
149 public static Long getPortNumberFromNodeConnectorId(NodeConnectorId portId) {
150 String portNo = getPortNoFromNodeConnectorId(portId);
152 return Long.valueOf(portNo);
153 } catch (NumberFormatException ex) {
154 LOG.trace("Unable to retrieve port number from nodeconnector id for {}", portId);
156 return IfmConstants.INVALID_PORT_NO;
159 public static NodeId buildDpnNodeId(BigInteger dpnId) {
160 return new NodeId(IfmConstants.OF_URI_PREFIX + dpnId);
163 public static InstanceIdentifier<Interface> buildId(String interfaceName) {
164 // TODO Make this generic and move to AbstractDataChangeListener or
166 InstanceIdentifierBuilder<Interface> idBuilder = InstanceIdentifier.builder(Interfaces.class)
167 .child(Interface.class, new InterfaceKey(interfaceName));
168 return idBuilder.build();
171 public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn
172 .ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> buildStateInterfaceId(
173 String interfaceName) {
174 InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn
175 .ietf.params.xml.ns.yang.ietf.interfaces.rev140508
176 .interfaces.state.Interface> idBuilder = InstanceIdentifier
177 .builder(InterfacesState.class)
178 .child(org.opendaylight.yang.gen.v1.urn
179 .ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.class,
180 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
181 .ietf.interfaces.rev140508.interfaces.state.InterfaceKey(
183 return idBuilder.build();
186 public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
187 .ietf.interfaces.rev140508.interfaces.state.InterfaceKey getStateInterfaceKeyFromName(
189 return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
190 .ietf.interfaces.rev140508.interfaces.state.InterfaceKey(
194 public static InstanceIdentifier<IdPool> getPoolId(String poolName) {
195 return InstanceIdentifier.builder(IdPools.class).child(IdPool.class, new IdPoolKey(poolName)).build();
198 public static long getGroupId(int ifIndex, InterfaceInfo.InterfaceType infType) {
199 if (infType == LOGICAL_GROUP_INTERFACE) {
200 return getLogicalTunnelSelectGroupId(ifIndex);
206 * Synchronous blocking read from data store.
209 * {@link SingleTransactionDataBroker#syncReadOptional(DataBroker, LogicalDatastoreType, InstanceIdentifier)}
213 public static <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
214 InstanceIdentifier<T> path, DataBroker broker) {
215 try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
216 return tx.read(datastoreType, path).get();
217 } catch (InterruptedException | ExecutionException e) {
218 LOG.error("Cannot read identifier", e);
219 throw new RuntimeException(e);
223 public static List<Action> getEgressActionsForInterface(String interfaceName, Long tunnelKey, Integer actionKey,
224 InterfaceManagerCommonUtils interfaceUtils, Boolean isDefaultEgress) {
225 List<ActionInfo> listActionInfo = getEgressActionInfosForInterface(interfaceName, tunnelKey,
226 actionKey == null ? 0 : actionKey, interfaceUtils, isDefaultEgress);
227 List<Action> actionsList = new ArrayList<>();
228 for (ActionInfo actionInfo : listActionInfo) {
229 actionsList.add(actionInfo.buildAction());
234 public static List<Instruction> getEgressInstructionsForInterface(String interfaceName, Long tunnelKey,
235 InterfaceManagerCommonUtils interfaceUtils, Boolean isDefaultEgress) {
236 List<Instruction> instructions = new ArrayList<>();
237 List<Action> actionList = MDSALUtil.buildActions(
238 getEgressActionInfosForInterface(interfaceName, tunnelKey, 0, interfaceUtils, isDefaultEgress));
239 instructions.add(MDSALUtil.buildApplyActionsInstruction(actionList));
243 public static List<Instruction> getEgressInstructionsForInterface(Interface interfaceInfo, String portNo,
244 Long tunnelKey, boolean isDefaultEgress,
245 int ifIndex, long groupId) {
246 List<Instruction> instructions = new ArrayList<>();
247 InterfaceInfo.InterfaceType ifaceType = getInterfaceType(interfaceInfo);
248 List<Action> actionList = MDSALUtil.buildActions(
249 getEgressActionInfosForInterface(interfaceInfo, portNo, ifaceType, tunnelKey, 0,
250 isDefaultEgress, ifIndex, groupId));
251 instructions.add(MDSALUtil.buildApplyActionsInstruction(actionList));
255 public static List<ActionInfo> getEgressActionInfosForInterface(String interfaceName, int actionKeyStart,
256 InterfaceManagerCommonUtils interfaceUtils, Boolean isDefaultEgress) {
257 return getEgressActionInfosForInterface(interfaceName, null, actionKeyStart, interfaceUtils, isDefaultEgress);
261 * Returns a list of Actions to be taken when sending a packet over an
264 * @param interfaceName
265 * name of the interface
268 * @param actionKeyStart
270 * @param interfaceUtils
271 * InterfaceManagerCommonUtils
272 * @return list of actions
274 public static List<ActionInfo> getEgressActionInfosForInterface(String interfaceName, Long tunnelKey,
275 int actionKeyStart, InterfaceManagerCommonUtils interfaceUtils, Boolean isDefaultEgress) {
276 Interface interfaceInfo = interfaceUtils.getInterfaceFromConfigDS(new InterfaceKey(interfaceName));
277 if (interfaceInfo == null) {
278 throw new NullPointerException("Interface information not present in config DS for " + interfaceName);
280 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
281 .ietf.interfaces.rev140508.interfaces.state.Interface ifState =
282 interfaceUtils.getInterfaceState(interfaceName);
283 if (ifState == null) {
284 throw new NullPointerException("Interface information not present in oper DS for " + interfaceName);
286 String lowerLayerIf = ifState.getLowerLayerIf().get(0);
287 NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
288 String portNo = IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId);
290 InterfaceInfo.InterfaceType ifaceType = getInterfaceType(interfaceInfo);
291 return getEgressActionInfosForInterface(interfaceInfo, portNo, ifaceType, tunnelKey, actionKeyStart,
292 isDefaultEgress, ifState.getIfIndex(), 0);
296 * Returns the list of egress actions for a given interface.
298 * @param interfaceInfo the interface to look up
299 * @param portNo port number
300 * @param ifaceType the type of the interface
301 * @param tunnelKey the tunnel key
302 * @param actionKeyStart the start for the first key assigned for the new actions
303 * @param isDefaultEgress if it is the default egress
304 * @param ifIndex interface index
305 * @param groupId group Id
306 * @return list of actions for the interface
308 // The following suppression is for javac, not for checkstyle
309 @SuppressWarnings("fallthrough")
310 @SuppressFBWarnings("SF_SWITCH_FALLTHROUGH")
311 public static List<ActionInfo> getEgressActionInfosForInterface(Interface interfaceInfo, String portNo,
312 InterfaceInfo.InterfaceType ifaceType, Long tunnelKey, int actionKeyStart, boolean isDefaultEgress,
313 int ifIndex, long groupId) {
314 List<ActionInfo> result = new ArrayList<>();
318 case GRE_TRUNK_INTERFACE:
319 if (!isDefaultEgress) {
320 // TODO tunnel_id to encode GRE key, once it is supported
321 // Until then, tunnel_id should be "cleaned", otherwise it
322 // stores the value coming from a VXLAN tunnel
323 if (tunnelKey == null) {
328 case VXLAN_TRUNK_INTERFACE:
329 if (!isDefaultEgress) {
330 if (tunnelKey != null) {
331 result.add(new ActionSetFieldTunnelId(actionKeyStart++, BigInteger.valueOf(tunnelKey)));
334 // For OF Tunnels default egress actions need to set tunnelIps
335 IfTunnel ifTunnel = interfaceInfo.augmentation(IfTunnel.class);
336 if (BooleanUtils.isTrue(ifTunnel.isTunnelRemoteIpFlow()
337 && ifTunnel.getTunnelDestination() != null)) {
338 result.add(new ActionSetTunnelDestinationIp(actionKeyStart++, ifTunnel.getTunnelDestination()));
340 if (BooleanUtils.isTrue(ifTunnel.isTunnelSourceIpFlow()
341 && ifTunnel.getTunnelSource() != null)) {
342 result.add(new ActionSetTunnelSourceIp(actionKeyStart++, ifTunnel.getTunnelSource()));
347 if (isDefaultEgress) {
348 IfL2vlan vlanIface = interfaceInfo.augmentation(IfL2vlan.class);
349 LOG.trace("get egress actions for l2vlan interface: {}", vlanIface);
350 boolean isVlanTransparent = false;
352 if (vlanIface != null) {
353 vlanVid = vlanIface.getVlanId() == null ? 0 : vlanIface.getVlanId().getValue();
354 isVlanTransparent = vlanIface.getL2vlanMode() == IfL2vlan.L2vlanMode.Transparent;
356 if (vlanVid != 0 && !isVlanTransparent) {
357 result.add(new ActionPushVlan(actionKeyStart++));
358 result.add(new ActionSetFieldVlanVid(actionKeyStart++, vlanVid));
360 result.add(new ActionOutput(actionKeyStart++, new Uri(portNo)));
362 addEgressActionInfosForInterface(ifIndex, actionKeyStart, result);
365 case LOGICAL_GROUP_INTERFACE:
366 if (isDefaultEgress) {
367 result.add(new ActionGroup(groupId));
369 addEgressActionInfosForInterface(ifIndex, actionKeyStart, result);
374 LOG.warn("Interface Type {} not handled yet", ifaceType);
380 public static void addEgressActionInfosForInterface(int ifIndex, int actionKeyStart, List<ActionInfo> result) {
381 long regValue = MetaDataUtil.getReg6ValueForLPortDispatcher(ifIndex, NwConstants.DEFAULT_SERVICE_INDEX);
382 result.add(new ActionRegLoad(actionKeyStart++, NxmNxReg6.class, IfmConstants.REG6_START_INDEX,
383 IfmConstants.REG6_END_INDEX, regValue));
384 result.add(new ActionNxResubmit(actionKeyStart, NwConstants.EGRESS_LPORT_DISPATCHER_TABLE));
387 public static NodeId getNodeIdFromNodeConnectorId(NodeConnectorId ncId) {
388 return new NodeId(ncId.getValue().substring(0, ncId.getValue().lastIndexOf(':')));
391 public static BigInteger[] mergeOpenflowMetadataWriteInstructions(List<Instruction> instructions) {
392 BigInteger metadata = new BigInteger("0", 16);
393 BigInteger metadataMask = new BigInteger("0", 16);
394 if (instructions != null && !instructions.isEmpty()) {
395 // check if metadata write instruction is present
396 for (Instruction instruction : instructions) {
397 org.opendaylight.yang.gen.v1.urn.opendaylight.flow
398 .types.rev131026.instruction.Instruction actualInstruction = instruction
400 if (actualInstruction instanceof WriteMetadataCase) {
401 WriteMetadataCase writeMetaDataInstruction = (WriteMetadataCase) actualInstruction;
402 WriteMetadata availableMetaData = writeMetaDataInstruction.getWriteMetadata();
403 metadata = metadata.or(availableMetaData.getMetadata());
404 metadataMask = metadataMask.or(availableMetaData.getMetadataMask());
408 return new BigInteger[] { metadata, metadataMask };
411 public static Integer allocateId(IdManagerService idManager, String poolName, String idKey) {
412 AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
414 Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
415 RpcResult<AllocateIdOutput> rpcResult = result.get();
416 if (rpcResult.isSuccessful()) {
417 return rpcResult.getResult().getIdValue().intValue();
419 LOG.warn("RPC Call to Get Unique Id returned with Errors {}", rpcResult.getErrors());
421 } catch (InterruptedException | ExecutionException e) {
422 LOG.warn("Exception when getting Unique Id", e);
427 public static void releaseId(IdManagerService idManager, String poolName, String idKey) {
428 ReleaseIdInput idInput = new ReleaseIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
430 ListenableFuture<RpcResult<ReleaseIdOutput>> result = idManager.releaseId(idInput);
431 RpcResult<ReleaseIdOutput> rpcResult = result.get();
432 if (!rpcResult.isSuccessful()) {
433 LOG.warn("RPC Call to release Id with Key {} returned with Errors {}", idKey, rpcResult.getErrors());
435 } catch (InterruptedException | ExecutionException e) {
436 LOG.warn("Exception when releasing Id for key {}", idKey, e);
440 public static BigInteger getDpnId(DatapathId datapathId) {
441 if (datapathId != null) {
442 // Adding logs for a random issue spotted during datapath id
444 String dpIdStr = datapathId.getValue().replace(":", "");
445 return new BigInteger(dpIdStr, 16);
450 public static NodeConnectorId getNodeConnectorIdFromInterface(
451 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
452 .ietf.interfaces.rev140508.interfaces.state.Interface ifState) {
453 if (ifState != null) {
454 List<String> ofportIds = ifState.getLowerLayerIf();
455 return new NodeConnectorId(ofportIds.get(0));
460 public static InterfaceInfo.InterfaceType getInterfaceType(Interface iface) {
461 InterfaceInfo.InterfaceType interfaceType = org.opendaylight
462 .genius.interfacemanager.globals.InterfaceInfo.InterfaceType.UNKNOWN_INTERFACE;
463 Class<? extends org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
464 .ietf.interfaces.rev140508.InterfaceType> ifType = iface
467 if (ifType.isAssignableFrom(L2vlan.class)) {
468 interfaceType = VLAN_INTERFACE;
469 } else if (ifType.isAssignableFrom(Tunnel.class)) {
470 IfTunnel ifTunnel = iface.augmentation(IfTunnel.class);
471 Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight
472 .genius.interfacemanager.rev160406.TunnelTypeBase> tunnelType = ifTunnel
473 .getTunnelInterfaceType();
474 interfaceType = tunnelType.isAssignableFrom(TunnelTypeLogicalGroup.class)
475 ? InterfaceInfo.InterfaceType.LOGICAL_GROUP_INTERFACE : TUNNEL_TYPE_MAP.get(tunnelType);
477 return interfaceType;
480 public static VlanInterfaceInfo getVlanInterfaceInfo(Interface iface, BigInteger dpId) {
482 String portName = null;
483 IfL2vlan vlanIface = iface.augmentation(IfL2vlan.class);
484 ParentRefs parentRefs = iface.augmentation(ParentRefs.class);
485 if (parentRefs != null && parentRefs.getParentInterface() != null) {
486 portName = parentRefs.getParentInterface();
488 LOG.warn("Portname set to null since parentRef is Null");
490 VlanInterfaceInfo vlanInterfaceInfo = new VlanInterfaceInfo(dpId, portName, vlanId);
492 if (vlanIface != null) {
493 vlanId = vlanIface.getVlanId() == null ? 0 : vlanIface.getVlanId().getValue().shortValue();
494 L2vlanMode l2VlanMode = vlanIface.getL2vlanMode();
496 if (l2VlanMode == L2vlanMode.Transparent) {
497 vlanInterfaceInfo.setVlanTransparent(true);
499 if (l2VlanMode == L2vlanMode.NativeUntagged) {
500 vlanInterfaceInfo.setUntaggedVlan(true);
502 vlanInterfaceInfo.setVlanId(vlanId);
505 return vlanInterfaceInfo;
508 public static BigInteger getDeadBeefBytesForMac() {
509 return new BigInteger("FFFFFFFF", 16).and(new BigInteger(IfmConstants.DEAD_BEEF_MAC_PREFIX, 16)).shiftLeft(16);
512 public static BigInteger fillPortNumberToMac(long portNumber) {
513 return new BigInteger("FFFF", 16).and(BigInteger.valueOf(portNumber));
516 public static String generateMacAddress(long portNo) {
517 String unformattedMAC = getDeadBeefBytesForMac().or(fillPortNumberToMac(portNo)).toString(16);
518 return unformattedMAC.replaceAll("(.{2})", "$1" + IfmConstants.MAC_SEPARATOR).substring(0,
519 IfmConstants.MAC_STRING_LENGTH);
522 public static PhysAddress getPhyAddress(long portNo, FlowCapableNodeConnector flowCapableNodeConnector) {
523 String southboundMacAddress = flowCapableNodeConnector.getHardwareAddress().getValue();
524 if (IfmConstants.INVALID_MAC.equals(southboundMacAddress)) {
525 LOG.debug("Invalid MAC Address received for {}, generating MAC Address",
526 flowCapableNodeConnector.getName());
527 southboundMacAddress = generateMacAddress(portNo);
529 return new PhysAddress(southboundMacAddress);
532 public static void updateInterfaceParentRef(TypedWriteTransaction<Configuration> tx, String interfaceName,
533 String parentInterface) {
534 InstanceIdentifier<ParentRefs> parentRefIdentifier = InstanceIdentifier.builder(Interfaces.class)
535 .child(Interface.class, new InterfaceKey(interfaceName)).augmentation(ParentRefs.class).build();
536 ParentRefs parentRefs = new ParentRefsBuilder().setParentInterface(parentInterface).build();
537 tx.merge(parentRefIdentifier, parentRefs);
539 "Updating parentRefInterface for interfaceName {}. "
540 + "interfaceKey {}, with parentRef augmentation pointing to {}",
541 interfaceName, new InterfaceKey(interfaceName), parentInterface);
544 public static InstanceIdentifier<BoundServices> buildBoundServicesIId(short servicePriority, String interfaceName,
545 Class<? extends ServiceModeBase> serviceMode) {
546 return InstanceIdentifier.builder(ServiceBindings.class)
547 .child(ServicesInfo.class, new ServicesInfoKey(interfaceName, serviceMode))
548 .child(BoundServices.class, new BoundServicesKey(servicePriority)).build();
551 public static void bindService(TypedWriteTransaction<Configuration> tx, String interfaceName,
552 BoundServices serviceInfo, Class<? extends ServiceModeBase> serviceMode) {
553 LOG.info("Binding Service {} for : {}", serviceInfo.getServiceName(), interfaceName);
554 InstanceIdentifier<BoundServices> boundServicesInstanceIdentifier = buildBoundServicesIId(
555 serviceInfo.getServicePriority(), interfaceName, serviceMode);
556 tx.put(boundServicesInstanceIdentifier, serviceInfo, CREATE_MISSING_PARENTS);
559 public static void unbindService(ManagedNewTransactionRunner txRunner, JobCoordinator coordinator,
560 String interfaceName, InstanceIdentifier<BoundServices> boundServicesInstanceIdentifier) {
561 coordinator.enqueueJob(interfaceName, () -> Collections.singletonList(
562 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
563 tx -> unbindService(tx, interfaceName, boundServicesInstanceIdentifier))));
566 public static void unbindService(TypedWriteTransaction<Configuration> tx, String interfaceName,
567 InstanceIdentifier<BoundServices> boundServicesInstanceIdentifier) {
568 LOG.info("Unbinding Service from : {}", interfaceName);
569 tx.delete(boundServicesInstanceIdentifier);
572 public static long getLogicalTunnelSelectGroupId(int lportTag) {
573 return org.opendaylight.genius.interfacemanager.globals.IfmConstants.VXLAN_GROUPID_MIN + lportTag;