2 * Copyright © 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.netvirt.elan.utils;
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.List;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.Future;
25 import javax.inject.Inject;
26 import javax.inject.Singleton;
27 import org.apache.commons.lang3.StringUtils;
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.clustering.EntityOwnershipService;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
34 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
35 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
36 import org.opendaylight.genius.interfacemanager.globals.InterfaceServiceUtil;
37 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
38 import org.opendaylight.genius.itm.globals.ITMConstants;
39 import org.opendaylight.genius.mdsalutil.ActionInfo;
40 import org.opendaylight.genius.mdsalutil.FlowEntity;
41 import org.opendaylight.genius.mdsalutil.InstructionInfo;
42 import org.opendaylight.genius.mdsalutil.MDSALUtil;
43 import org.opendaylight.genius.mdsalutil.MDSALUtil.MdsalOp;
44 import org.opendaylight.genius.mdsalutil.MatchInfo;
45 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
46 import org.opendaylight.genius.mdsalutil.NwConstants;
47 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
48 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
49 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
50 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
51 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetDestination;
52 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetSource;
53 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
54 import org.opendaylight.genius.mdsalutil.matches.MatchTunnelId;
55 import org.opendaylight.genius.utils.ServiceIndex;
56 import org.opendaylight.netvirt.elan.ElanException;
57 import org.opendaylight.netvirt.elan.internal.ElanInstanceManager;
58 import org.opendaylight.netvirt.elan.internal.ElanInterfaceManager;
59 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
60 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
61 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
62 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
65 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
66 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
67 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.IfIndexesInterfaceMap;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterfaceKey;
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.TunnelTypeVxlan;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetDpidFromInterfaceInputBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetDpidFromInterfaceOutput;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceInput;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceInputBuilder;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceOutput;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceBindings;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeIngress;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceTypeFlowBased;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflow;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflowBuilder;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfoKey;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesBuilder;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesKey;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.ExternalTunnelList;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.TunnelList;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnel;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnelKey;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.CreateTerminatingServiceActionsInput;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.CreateTerminatingServiceActionsInputBuilder;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.GetExternalTunnelInterfaceNameInput;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.GetExternalTunnelInterfaceNameInputBuilder;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.GetExternalTunnelInterfaceNameOutput;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.GetTunnelInterfaceNameInput;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.GetTunnelInterfaceNameInputBuilder;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.GetTunnelInterfaceNameOutput;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.RemoveTerminatingServiceActionsInput;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.RemoveTerminatingServiceActionsInputBuilder;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstanceBuilder;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface.EtreeInterfaceType;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTag;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTagName;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTagNameBuilder;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanForwardingTables;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaceForwardingEntries;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanState;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanTagNameMap;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeFlat;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeVlan;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeVxlan;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMacKey;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfacesKey;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTable;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTableBuilder;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTableKey;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
149 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.ElanBuilder;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.ElanKey;
153 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
154 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagNameBuilder;
155 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagNameKey;
156 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
157 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryBuilder;
158 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
159 import org.opendaylight.yangtools.yang.binding.DataObject;
160 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
161 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
162 import org.opendaylight.yangtools.yang.common.RpcResult;
163 import org.slf4j.Logger;
164 import org.slf4j.LoggerFactory;
167 public class ElanUtils {
169 private static final Logger LOG = LoggerFactory.getLogger(ElanUtils.class);
171 private static Map<String, ElanInstance> elanInstanceLocalCache = new ConcurrentHashMap<>();
172 private static Map<String, ElanInterface> elanInterfaceLocalCache = new ConcurrentHashMap<>();
174 private final DataBroker broker;
175 private final IMdsalApiManager mdsalManager;
176 private final ElanInstanceManager elanInstanceManager;
177 private final OdlInterfaceRpcService interfaceManagerRpcService;
178 private final ItmRpcService itmRpcService;
179 private final ElanL2GatewayUtils elanL2GatewayUtils;
180 private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
181 private final L2GatewayConnectionUtils l2GatewayConnectionUtils;
182 private final IInterfaceManager interfaceManager;
184 public static final FutureCallback<Void> DEFAULT_CALLBACK = new FutureCallback<Void>() {
186 public void onSuccess(Void result) {
187 LOG.debug("Success in Datastore operation");
191 public void onFailure(Throwable error) {
192 LOG.error("Error in Datastore operation", error);
197 public ElanUtils(DataBroker dataBroker, IMdsalApiManager mdsalManager, ElanInstanceManager elanInstanceManager,
198 OdlInterfaceRpcService interfaceManagerRpcService, ItmRpcService itmRpcService,
199 ElanInterfaceManager elanInterfaceManager,
200 EntityOwnershipService entityOwnershipService, IInterfaceManager interfaceManager) {
201 this.broker = dataBroker;
202 this.mdsalManager = mdsalManager;
203 this.elanInstanceManager = elanInstanceManager;
204 this.interfaceManagerRpcService = interfaceManagerRpcService;
205 this.itmRpcService = itmRpcService;
206 this.interfaceManager = interfaceManager;
208 elanL2GatewayMulticastUtils =
209 new ElanL2GatewayMulticastUtils(broker, elanInstanceManager, elanInterfaceManager, this);
210 elanL2GatewayUtils = new ElanL2GatewayUtils(broker, itmRpcService, this,
211 entityOwnershipService, elanL2GatewayMulticastUtils);
212 elanL2GatewayMulticastUtils.setEElanL2GatewayUtils(elanL2GatewayUtils);
213 l2GatewayConnectionUtils = new L2GatewayConnectionUtils(broker,
214 elanInstanceManager, entityOwnershipService, this);
217 public ElanL2GatewayUtils getElanL2GatewayUtils() {
218 return elanL2GatewayUtils;
221 public ElanL2GatewayMulticastUtils getElanL2GatewayMulticastUtils() {
222 return elanL2GatewayMulticastUtils;
225 public L2GatewayConnectionUtils getL2GatewayConnectionUtils() {
226 return l2GatewayConnectionUtils;
229 public static void addElanInstanceIntoCache(String elanInstanceName, ElanInstance elanInstance) {
230 elanInstanceLocalCache.put(elanInstanceName, elanInstance);
233 public static void removeElanInstanceFromCache(String elanInstanceName) {
234 elanInstanceLocalCache.remove(elanInstanceName);
237 public static ElanInstance getElanInstanceFromCache(String elanInstanceName) {
238 return elanInstanceLocalCache.get(elanInstanceName);
241 public static Set<String> getAllElanNames() {
242 return elanInstanceLocalCache.keySet();
245 public static void addElanInterfaceIntoCache(String interfaceName, ElanInterface elanInterface) {
246 elanInterfaceLocalCache.put(interfaceName, elanInterface);
249 public static void removeElanInterfaceFromCache(String interfaceName) {
250 elanInterfaceLocalCache.remove(interfaceName);
253 public static ElanInterface getElanInterfaceFromCache(String interfaceName) {
254 return elanInterfaceLocalCache.get(interfaceName);
258 * Uses the IdManager to retrieve a brand new ElanTag.
264 * @return the integer
266 public static Long retrieveNewElanTag(IdManagerService idManager, String idKey) {
268 AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(ElanConstants.ELAN_ID_POOL_NAME)
269 .setIdKey(idKey).build();
272 Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
273 RpcResult<AllocateIdOutput> rpcResult = result.get();
274 if (rpcResult.isSuccessful()) {
275 return rpcResult.getResult().getIdValue();
277 LOG.warn("RPC Call to Allocate Id returned with Errors {}", rpcResult.getErrors());
279 } catch (InterruptedException | ExecutionException e) {
280 LOG.warn("Exception when Allocating Id", e);
285 public static void releaseId(IdManagerService idManager, String poolName, String idKey) {
286 ReleaseIdInput releaseIdInput = new ReleaseIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
287 Future<RpcResult<Void>> result = idManager.releaseId(releaseIdInput);
293 * @deprecated Consider using {@link #read2(LogicalDatastoreType, InstanceIdentifier)} with proper exception
297 @SuppressWarnings("checkstyle:IllegalCatch")
298 public <T extends DataObject> Optional<T> read(DataBroker broker, LogicalDatastoreType datastoreType,
299 InstanceIdentifier<T> path) {
300 try (ReadOnlyTransaction tx = broker != null ? broker.newReadOnlyTransaction()
301 : this.broker.newReadOnlyTransaction()) {
302 return tx.read(datastoreType, path).get();
303 } catch (Exception e) {
304 throw new RuntimeException(e);
308 public <T extends DataObject> Optional<T> read2(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path)
309 throws ReadFailedException {
310 try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
311 CheckedFuture<Optional<T>, ReadFailedException> checkedFuture = tx.read(datastoreType, path);
312 return checkedFuture.checkedGet();
316 public static <T extends DataObject> void delete(DataBroker broker, LogicalDatastoreType datastoreType,
317 InstanceIdentifier<T> path) {
318 WriteTransaction tx = broker.newWriteOnlyTransaction();
319 tx.delete(datastoreType, path);
320 Futures.addCallback(tx.submit(), DEFAULT_CALLBACK);
323 public static <T extends DataObject> void delete(DataBroker broker, LogicalDatastoreType datastoreType,
324 InstanceIdentifier<T> path, FutureCallback<Void> callback) {
325 WriteTransaction tx = broker.newWriteOnlyTransaction();
326 tx.delete(datastoreType, path);
327 Futures.addCallback(tx.submit(), callback);
330 public static InstanceIdentifier<ElanInstance> getElanInstanceIdentifier() {
331 return InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class).build();
334 // elan-instances config container
335 public static ElanInstance getElanInstanceByName(DataBroker broker, String elanInstanceName) {
336 ElanInstance elanObj = getElanInstanceFromCache(elanInstanceName);
337 if (elanObj != null) {
340 InstanceIdentifier<ElanInstance> elanIdentifierId = getElanInstanceConfigurationDataPath(elanInstanceName);
341 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, elanIdentifierId).orNull();
344 public static InstanceIdentifier<ElanInstance> getElanInstanceConfigurationDataPath(String elanInstanceName) {
345 return InstanceIdentifier.builder(ElanInstances.class)
346 .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
349 // elan-interfaces Config Container
350 public static ElanInterface getElanInterfaceByElanInterfaceName(DataBroker broker, String elanInterfaceName) {
351 ElanInterface elanInterfaceObj = getElanInterfaceFromCache(elanInterfaceName);
352 if (elanInterfaceObj != null) {
353 return elanInterfaceObj;
355 InstanceIdentifier<ElanInterface> elanInterfaceId = getElanInterfaceConfigurationDataPathId(elanInterfaceName);
356 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, elanInterfaceId).orNull();
359 public static EtreeInterface getEtreeInterfaceByElanInterfaceName(DataBroker broker, String elanInterfaceName) {
360 ElanInterface elanInterface = getElanInterfaceByElanInterfaceName(broker, elanInterfaceName);
361 if (elanInterface == null) {
364 return elanInterface.getAugmentation(EtreeInterface.class);
368 public static InstanceIdentifier<ElanInterface> getElanInterfaceConfigurationDataPathId(String interfaceName) {
369 return InstanceIdentifier.builder(ElanInterfaces.class)
370 .child(ElanInterface.class, new ElanInterfaceKey(interfaceName)).build();
373 // elan-state Operational container
374 public static Elan getElanByName(DataBroker broker, String elanInstanceName) {
375 InstanceIdentifier<Elan> elanIdentifier = getElanInstanceOperationalDataPath(elanInstanceName);
376 return MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, elanIdentifier).orNull();
379 public static InstanceIdentifier<Elan> getElanInstanceOperationalDataPath(String elanInstanceName) {
380 return InstanceIdentifier.builder(ElanState.class).child(Elan.class, new ElanKey(elanInstanceName)).build();
383 // grouping of forwarding-entries
384 public MacEntry getInterfaceMacEntriesOperationalDataPath(String interfaceName, PhysAddress physAddress) {
385 InstanceIdentifier<MacEntry> existingMacEntryId = getInterfaceMacEntriesIdentifierOperationalDataPath(
386 interfaceName, physAddress);
387 return read(broker, LogicalDatastoreType.OPERATIONAL, existingMacEntryId).orNull();
390 public MacEntry getInterfaceMacEntriesOperationalDataPathFromId(InstanceIdentifier identifier) {
391 Optional<MacEntry> existingInterfaceMacEntry = read(broker,
392 LogicalDatastoreType.OPERATIONAL, identifier);
393 return existingInterfaceMacEntry.orNull();
396 public static InstanceIdentifier<MacEntry> getInterfaceMacEntriesIdentifierOperationalDataPath(String interfaceName,
397 PhysAddress physAddress) {
398 return InstanceIdentifier.builder(ElanInterfaceForwardingEntries.class)
399 .child(ElanInterfaceMac.class, new ElanInterfaceMacKey(interfaceName))
400 .child(MacEntry.class, new MacEntryKey(physAddress)).build();
404 // elan-forwarding-tables Operational container
405 public Optional<MacEntry> getMacEntryForElanInstance(String elanName, PhysAddress physAddress) {
406 InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanName, physAddress);
407 return read(broker, LogicalDatastoreType.OPERATIONAL, macId);
410 public MacEntry getMacEntryFromElanMacId(InstanceIdentifier identifier) {
411 Optional<MacEntry> existingInterfaceMacEntry = read(broker,
412 LogicalDatastoreType.OPERATIONAL, identifier);
413 return existingInterfaceMacEntry.orNull();
416 public static InstanceIdentifier<MacEntry> getMacEntryOperationalDataPath(String elanName,
417 PhysAddress physAddress) {
418 return InstanceIdentifier.builder(ElanForwardingTables.class).child(MacTable.class, new MacTableKey(elanName))
419 .child(MacEntry.class, new MacEntryKey(physAddress)).build();
422 public static InstanceIdentifier<MacTable> getElanMacTableOperationalDataPath(String elanName) {
423 return InstanceIdentifier.builder(ElanForwardingTables.class).child(MacTable.class, new MacTableKey(elanName))
427 // elan-interface-forwarding-entries Operational container
428 public ElanInterfaceMac getElanInterfaceMacByInterfaceName(String interfaceName) {
429 InstanceIdentifier<ElanInterfaceMac> elanInterfaceId = getElanInterfaceMacEntriesOperationalDataPath(
431 return read(broker, LogicalDatastoreType.OPERATIONAL, elanInterfaceId).orNull();
435 * Gets the elan interface mac addresses.
437 * @param interfaceName
439 * @return the elan interface mac addresses
441 public List<PhysAddress> getElanInterfaceMacAddresses(String interfaceName) {
442 List<PhysAddress> macAddresses = new ArrayList<>();
443 ElanInterfaceMac elanInterfaceMac = getElanInterfaceMacByInterfaceName(interfaceName);
444 if (elanInterfaceMac != null && elanInterfaceMac.getMacEntry() != null) {
445 List<MacEntry> macEntries = elanInterfaceMac.getMacEntry();
446 for (MacEntry macEntry : macEntries) {
447 macAddresses.add(macEntry.getMacAddress());
453 public static InstanceIdentifier<ElanInterfaceMac> getElanInterfaceMacEntriesOperationalDataPath(
454 String interfaceName) {
455 return InstanceIdentifier.builder(ElanInterfaceForwardingEntries.class)
456 .child(ElanInterfaceMac.class, new ElanInterfaceMacKey(interfaceName)).build();
460 * Returns the list of Interfaces that belong to an Elan on an specific DPN.
461 * Data retrieved from Elan's operational DS: elan-dpn-interfaces container
463 * @param elanInstanceName
464 * name of the Elan to which the interfaces must belong to
466 * Id of the DPN where the interfaces are located
467 * @return the elan interface Info
469 public DpnInterfaces getElanInterfaceInfoByElanDpn(String elanInstanceName, BigInteger dpId) {
470 InstanceIdentifier<DpnInterfaces> elanDpnInterfacesId = getElanDpnInterfaceOperationalDataPath(elanInstanceName,
472 return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfacesId).orNull();
476 * Returns the InstanceIdentifier that points to the Interfaces of an Elan
477 * in a given DPN in the Operational DS. Data retrieved from Elans's
478 * operational DS: dpn-interfaces list
480 * @param elanInstanceName
481 * name of the Elan to which the interfaces must belong to
483 * Id of the DPN where the interfaces are located
484 * @return the elan dpn interface
486 public static InstanceIdentifier<DpnInterfaces> getElanDpnInterfaceOperationalDataPath(String elanInstanceName,
488 return InstanceIdentifier.builder(ElanDpnInterfaces.class)
489 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName))
490 .child(DpnInterfaces.class, new DpnInterfacesKey(dpId)).build();
493 // elan-tag-name-map Operational Container
494 public ElanTagName getElanInfoByElanTag(long elanTag) {
495 InstanceIdentifier<ElanTagName> elanId = getElanInfoEntriesOperationalDataPath(elanTag);
496 Optional<ElanTagName> existingElanInfo = read(broker,
497 LogicalDatastoreType.OPERATIONAL, elanId);
498 return existingElanInfo.orNull();
501 public EtreeLeafTagName getEtreeLeafTagByElanTag(long elanTag) {
502 InstanceIdentifier<ElanTagName> elanId = getElanInfoEntriesOperationalDataPath(elanTag);
503 Optional<ElanTagName> existingElanInfo = read(broker,
504 LogicalDatastoreType.OPERATIONAL, elanId);
505 if (existingElanInfo.isPresent()) {
506 ElanTagName elanTagName = existingElanInfo.get();
507 return elanTagName.getAugmentation(EtreeLeafTagName.class);
512 public static InstanceIdentifier<ElanTagName> getElanInfoEntriesOperationalDataPath(long elanTag) {
513 return InstanceIdentifier.builder(ElanTagNameMap.class).child(ElanTagName.class, new ElanTagNameKey(elanTag))
517 // interface-index-tag operational container
518 public Optional<IfIndexInterface> getInterfaceInfoByInterfaceTag(long interfaceTag) {
519 InstanceIdentifier<IfIndexInterface> interfaceId = getInterfaceInfoEntriesOperationalDataPath(interfaceTag);
520 return read(broker, LogicalDatastoreType.OPERATIONAL, interfaceId);
523 public static InstanceIdentifier<IfIndexInterface> getInterfaceInfoEntriesOperationalDataPath(long interfaceTag) {
524 return InstanceIdentifier.builder(IfIndexesInterfaceMap.class)
525 .child(IfIndexInterface.class, new IfIndexInterfaceKey((int) interfaceTag)).build();
528 public static InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
529 return InstanceIdentifier.builder(ElanDpnInterfaces.class)
530 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
533 public ElanDpnInterfacesList getElanDpnInterfacesList(String elanName) {
534 InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanName);
535 return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId).orNull();
538 public ElanDpnInterfaces getElanDpnInterfacesList() {
539 InstanceIdentifier<ElanDpnInterfaces> elanDpnInterfaceId = InstanceIdentifier.builder(ElanDpnInterfaces.class)
541 return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId).orNull();
545 * This method is useful get all ELAN participating CSS dpIds to install
546 * program remote dmac entries and updating remote bc groups for tor
549 * @param elanInstanceName
550 * the elan instance name
551 * @return list of dpIds
553 public List<BigInteger> getParticipatingDpnsInElanInstance(String elanInstanceName) {
554 List<BigInteger> dpIds = new ArrayList<>();
555 InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanInstanceName);
556 Optional<ElanDpnInterfacesList> existingElanDpnInterfaces = read(broker,
557 LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId);
558 if (!existingElanDpnInterfaces.isPresent()) {
561 List<DpnInterfaces> dpnInterfaces = existingElanDpnInterfaces.get().getDpnInterfaces();
562 for (DpnInterfaces dpnInterface : dpnInterfaces) {
563 dpIds.add(dpnInterface.getDpId());
569 * To check given dpId is already present in Elan instance. This can be used
570 * to program flow entry in external tunnel table when a new access port
571 * added for first time into the ELAN instance
575 * @param elanInstanceName
576 * the elan instance name
577 * @return true if dpId is already present, otherwise return false
579 public boolean isDpnAlreadyPresentInElanInstance(BigInteger dpId, String elanInstanceName) {
580 InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanInstanceName);
581 Optional<ElanDpnInterfacesList> existingElanDpnInterfaces = read(broker,
582 LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId);
583 if (!existingElanDpnInterfaces.isPresent()) {
586 List<DpnInterfaces> dpnInterfaces = existingElanDpnInterfaces.get().getDpnInterfaces();
587 for (DpnInterfaces dpnInterface : dpnInterfaces) {
588 if (dpnInterface.getDpId().equals(dpId)) {
595 public ElanForwardingTables getElanForwardingList() {
596 InstanceIdentifier<ElanForwardingTables> elanForwardingTableId = InstanceIdentifier
597 .builder(ElanForwardingTables.class).build();
598 return read(broker, LogicalDatastoreType.OPERATIONAL, elanForwardingTableId).orNull();
601 public static long getElanRemoteBroadCastGroupID(long elanTag) {
602 return ElanConstants.ELAN_GID_MIN + elanTag % ElanConstants.ELAN_GID_MIN * 2;
606 * Gets the elan mac table.
610 * @return the elan mac table
612 public MacTable getElanMacTable(String elanName) {
613 InstanceIdentifier<MacTable> elanMacTableId = getElanMacTableOperationalDataPath(elanName);
614 return read(broker, LogicalDatastoreType.OPERATIONAL, elanMacTableId).orNull();
617 public static long getElanLocalBCGId(long elanTag) {
618 return ElanConstants.ELAN_GID_MIN + (elanTag % ElanConstants.ELAN_GID_MIN * 2 - 1);
621 public static long getElanRemoteBCGId(long elanTag) {
622 return ElanConstants.ELAN_GID_MIN + elanTag % ElanConstants.ELAN_GID_MIN * 2;
625 public static long getEtreeLeafLocalBCGId(long etreeLeafTag) {
626 return ElanConstants.ELAN_GID_MIN + (etreeLeafTag % ElanConstants.ELAN_GID_MIN * 2 - 1);
629 public static long getEtreeLeafRemoteBCGId(long etreeLeafTag) {
630 return ElanConstants.ELAN_GID_MIN + etreeLeafTag % ElanConstants.ELAN_GID_MIN * 2;
633 public static BigInteger getElanMetadataLabel(long elanTag) {
634 return BigInteger.valueOf(elanTag).shiftLeft(24);
637 public static BigInteger getElanMetadataLabel(long elanTag, boolean isSHFlagSet) {
638 int shBit = isSHFlagSet ? 1 : 0;
639 return BigInteger.valueOf(elanTag).shiftLeft(24).or(BigInteger.valueOf(shBit));
642 public static BigInteger getElanMetadataLabel(long elanTag, int lportTag) {
643 return getElanMetadataLabel(elanTag).or(MetaDataUtil.getLportTagMetaData(lportTag));
646 public static BigInteger getElanMetadataMask() {
647 return MetaDataUtil.METADATA_MASK_SERVICE.or(MetaDataUtil.METADATA_MASK_LPORT_TAG);
651 * Setting INTERNAL_TUNNEL_TABLE, SMAC, DMAC, UDMAC in this DPN and optionally in other DPNs.
655 * @param interfaceInfo
661 * @param configureRemoteFlows
662 * true if remote dmac flows should be configured as well
663 * @param writeFlowGroupTx
665 * @throws ElanException in case of issues creating the flow objects
667 public void setupMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
668 long macTimeout, String macAddress, boolean configureRemoteFlows,
669 WriteTransaction writeFlowGroupTx) throws ElanException {
670 synchronized (getElanMacDPNKey(elanInfo.getElanTag(), macAddress, interfaceInfo.getDpId())) {
671 setupKnownSmacFlow(elanInfo, interfaceInfo, macTimeout, macAddress, mdsalManager,
673 setupOrigDmacFlows(elanInfo, interfaceInfo, macAddress, configureRemoteFlows, mdsalManager,
674 broker, writeFlowGroupTx);
678 public void setupDMacFlowonRemoteDpn(ElanInstance elanInfo, InterfaceInfo interfaceInfo, BigInteger dstDpId,
679 String macAddress, WriteTransaction writeFlowTx) throws ElanException {
680 synchronized (getElanMacDPNKey(elanInfo.getElanTag(), macAddress, dstDpId)) {
681 LOG.debug("Acquired lock for mac : " + macAddress + ". Proceeding with install operation.");
682 setupOrigDmacFlowsonRemoteDpn(elanInfo, interfaceInfo, dstDpId, macAddress, writeFlowTx);
687 * Inserts a Flow in SMAC table to state that the MAC has already been
690 private void setupKnownSmacFlow(ElanInstance elanInfo, InterfaceInfo interfaceInfo, long macTimeout,
691 String macAddress, IMdsalApiManager mdsalApiManager, WriteTransaction writeFlowGroupTx) {
692 FlowEntity flowEntity = buildKnownSmacFlow(elanInfo, interfaceInfo, macTimeout, macAddress);
693 mdsalApiManager.addFlowToTx(flowEntity, writeFlowGroupTx);
694 LOG.debug("Known Smac flow entry created for elan Name:{}, logical Interface port:{} and mac address:{}",
695 elanInfo.getElanInstanceName(), elanInfo.getDescription(), macAddress);
698 public FlowEntity buildKnownSmacFlow(ElanInstance elanInfo, InterfaceInfo interfaceInfo, long macTimeout,
700 int lportTag = interfaceInfo.getInterfaceTag();
701 // Matching metadata and eth_src fields
702 List<MatchInfo> mkMatches = new ArrayList<>();
703 mkMatches.add(new MatchMetadata(getElanMetadataLabel(elanInfo.getElanTag(), lportTag), getElanMetadataMask()));
704 mkMatches.add(new MatchEthernetSource(new MacAddress(macAddress)));
705 List<InstructionInfo> mkInstructions = new ArrayList<>();
706 mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
708 BigInteger dpId = interfaceInfo.getDpId();
709 long elanTag = getElanTag(broker, elanInfo, interfaceInfo);
710 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE,
711 getKnownDynamicmacFlowRef(NwConstants.ELAN_SMAC_TABLE, dpId, lportTag, macAddress, elanTag), 20,
712 elanInfo.getDescription(), (int) macTimeout, 0,
713 ElanConstants.COOKIE_ELAN_KNOWN_SMAC.add(BigInteger.valueOf(elanTag)), mkMatches, mkInstructions);
714 flowEntity.setStrictFlag(true);
715 flowEntity.setSendFlowRemFlag(macTimeout != 0); // If Mac timeout is 0,
718 // automatically, so no
719 // need to get notified
723 private static Long getElanTag(DataBroker broker, ElanInstance elanInfo, InterfaceInfo interfaceInfo) {
724 EtreeInterface etreeInterface = getEtreeInterfaceByElanInterfaceName(broker, interfaceInfo.getInterfaceName());
725 if (etreeInterface == null || etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
726 return elanInfo.getElanTag();
728 EtreeInstance etreeInstance = elanInfo.getAugmentation(EtreeInstance.class);
729 if (etreeInstance == null) {
730 LOG.warn("EtreeInterface {} is connected to a non-Etree network: {}",
731 interfaceInfo.getInterfaceName(), elanInfo.getElanInstanceName());
732 return elanInfo.getElanTag();
734 return etreeInstance.getEtreeLeafTagVal().getValue();
740 * Installs a Flow in INTERNAL_TUNNEL_TABLE of the affected DPN that sends
741 * the packet through the specified interface if the tunnel_id matches the
742 * interface's lportTag.
744 * @param interfaceInfo
746 * @param mdsalApiManager
747 * the mdsal API manager
748 * @param writeFlowGroupTx
749 * the writeFLowGroup tx
751 public void setupTermDmacFlows(InterfaceInfo interfaceInfo, IMdsalApiManager mdsalApiManager,
752 WriteTransaction writeFlowGroupTx) {
753 BigInteger dpId = interfaceInfo.getDpId();
754 int lportTag = interfaceInfo.getInterfaceTag();
755 Flow flow = MDSALUtil.buildFlowNew(NwConstants.INTERNAL_TUNNEL_TABLE,
756 getIntTunnelTableFlowRef(NwConstants.INTERNAL_TUNNEL_TABLE, lportTag), 5,
757 String.format("%s:%d", "ITM Flow Entry ", lportTag), 0, 0,
758 ITMConstants.COOKIE_ITM.add(BigInteger.valueOf(lportTag)),
759 getTunnelIdMatchForFilterEqualsLPortTag(lportTag),
760 getInstructionsInPortForOutGroup(interfaceInfo.getInterfaceName()));
761 mdsalApiManager.addFlowToTx(dpId, flow, writeFlowGroupTx);
762 LOG.debug("Terminating service table flow entry created on dpn:{} for logical Interface port:{}", dpId,
763 interfaceInfo.getPortName());
767 * Constructs the FlowName for flows installed in the Internal Tunnel Table,
768 * consisting in tableId + elanTag.
774 * @return the Internal tunnel
776 public static String getIntTunnelTableFlowRef(short tableId, int elanTag) {
777 return new StringBuffer().append(tableId).append(elanTag).toString();
781 * Constructs the Matches that checks that the tunnel_id field contains a
785 * lportTag that must be checked against the tunnel_id field
786 * @return the list of match Info
788 public static List<MatchInfo> getTunnelIdMatchForFilterEqualsLPortTag(int lportTag) {
789 List<MatchInfo> mkMatches = new ArrayList<>();
791 mkMatches.add(new MatchTunnelId(BigInteger.valueOf(lportTag)));
796 * Constructs the Instructions that take the packet over a given interface.
799 * Name of the interface where the packet must be sent over. It
800 * can be a local interface or a tunnel interface (internal or
802 * @return the Instruction
804 public List<Instruction> getInstructionsInPortForOutGroup(String ifName) {
805 List<Instruction> mkInstructions = new ArrayList<>();
806 List<Action> actions = getEgressActionsForInterface(ifName, /* tunnelKey */ null);
808 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
809 return mkInstructions;
813 * Returns the list of Actions to be taken when sending the packet through
814 * an Elan interface. Note that this interface can refer to an ElanInterface
815 * where the Elan VM is attached to a DPN or an ITM tunnel interface where
816 * Elan traffic can be sent through. In this latter case, the tunnelKey is
817 * mandatory and it can hold serviceId for internal tunnels or the VNI for
824 * @return the egress actions for interface
826 @SuppressWarnings("checkstyle:IllegalCatch")
827 public List<Action> getEgressActionsForInterface(String ifName, Long tunnelKey) {
828 List<Action> listAction = new ArrayList<>();
830 GetEgressActionsForInterfaceInput getEgressActionInput = new GetEgressActionsForInterfaceInputBuilder()
831 .setIntfName(ifName).setTunnelKey(tunnelKey).build();
832 Future<RpcResult<GetEgressActionsForInterfaceOutput>> result = interfaceManagerRpcService
833 .getEgressActionsForInterface(getEgressActionInput);
834 RpcResult<GetEgressActionsForInterfaceOutput> rpcResult = result.get();
835 if (!rpcResult.isSuccessful()) {
836 LOG.warn("RPC Call to Get egress actions for interface {} returned with Errors {}", ifName,
837 rpcResult.getErrors());
839 List<Action> actions = rpcResult.getResult().getAction();
840 listAction = actions;
842 } catch (Exception e) {
843 LOG.warn("Exception when egress actions for interface {}", ifName, e);
848 private void setupOrigDmacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
849 boolean configureRemoteFlows, IMdsalApiManager mdsalApiManager,
850 DataBroker broker, WriteTransaction writeFlowGroupTx)
851 throws ElanException {
852 BigInteger dpId = interfaceInfo.getDpId();
853 String ifName = interfaceInfo.getInterfaceName();
854 long ifTag = interfaceInfo.getInterfaceTag();
855 String elanInstanceName = elanInfo.getElanInstanceName();
857 Long elanTag = elanInfo.getElanTag();
859 setupLocalDmacFlow(elanTag, dpId, ifName, macAddress, elanInfo, mdsalApiManager, ifTag,
861 LOG.debug("Dmac flow entry created for elan Name:{}, logical port Name:{} mand mac address:{} "
862 + "on dpn:{}", elanInstanceName, interfaceInfo.getPortName(), macAddress, dpId);
864 if (!configureRemoteFlows) {
868 List<DpnInterfaces> elanDpns = getInvolvedDpnsInElan(elanInstanceName);
869 if (elanDpns == null) {
873 for (DpnInterfaces elanDpn : elanDpns) {
875 if (elanDpn.getDpId().equals(dpId)) {
879 // Check for the Remote DPN present in Inventory Manager
880 if (!isDpnPresent(elanDpn.getDpId())) {
884 // For remote DPNs a flow is needed to indicate that
885 // packets of this ELAN going to this MAC
886 // need to be forwarded through the appropiated ITM
888 setupRemoteDmacFlow(elanDpn.getDpId(), // srcDpn (the remote DPN in this case)
889 dpId, // dstDpn (the local DPN)
890 interfaceInfo.getInterfaceTag(), // lportTag of the local interface
891 elanTag, // identifier of the Elan
892 macAddress, // MAC to be programmed in remote DPN
893 elanInstanceName, writeFlowGroupTx, ifName, elanInfo);
894 LOG.debug("Dmac flow entry created for elan Name:{}, logical port Name:{} and mac address:{} on"
895 + " dpn:{}", elanInstanceName, interfaceInfo.getPortName(), macAddress, elanDpn.getDpId());
898 // TODO: Make sure that the same is performed against the ElanDevices.
901 private void setupOrigDmacFlowsonRemoteDpn(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
902 BigInteger dstDpId, String macAddress, WriteTransaction writeFlowTx) throws ElanException {
903 BigInteger dpId = interfaceInfo.getDpId();
904 String elanInstanceName = elanInfo.getElanInstanceName();
905 List<DpnInterfaces> remoteFEs = getInvolvedDpnsInElan(elanInstanceName);
906 for (DpnInterfaces remoteFE : remoteFEs) {
907 Long elanTag = elanInfo.getElanTag();
908 if (remoteFE.getDpId().equals(dstDpId)) {
909 // Check for the Remote DPN present in Inventory Manager
910 setupRemoteDmacFlow(dstDpId, dpId, interfaceInfo.getInterfaceTag(), elanTag, macAddress,
911 elanInstanceName, writeFlowTx, interfaceInfo.getInterfaceName(), elanInfo);
912 LOG.debug("Dmac flow entry created for elan Name:{}, logical port Name:{} and mac address {} on dpn:{}",
913 elanInstanceName, interfaceInfo.getPortName(), macAddress, remoteFE.getDpId());
919 @SuppressWarnings("unchecked")
920 public List<DpnInterfaces> getInvolvedDpnsInElan(String elanName) {
921 List<DpnInterfaces> dpns = elanInstanceManager.getElanDPNByName(elanName);
923 return Collections.emptyList();
928 private void setupLocalDmacFlow(long elanTag, BigInteger dpId, String ifName, String macAddress,
929 ElanInstance elanInfo, IMdsalApiManager mdsalApiManager, long ifTag, WriteTransaction writeFlowGroupTx) {
930 Flow flowEntity = buildLocalDmacFlowEntry(elanTag, dpId, ifName, macAddress, elanInfo, ifTag);
931 mdsalApiManager.addFlowToTx(dpId, flowEntity, writeFlowGroupTx);
932 installEtreeLocalDmacFlow(elanTag, dpId, ifName, macAddress, elanInfo,
933 mdsalApiManager, ifTag, writeFlowGroupTx);
936 private void installEtreeLocalDmacFlow(long elanTag, BigInteger dpId, String ifName, String macAddress,
937 ElanInstance elanInfo, IMdsalApiManager mdsalApiManager, long ifTag, WriteTransaction writeFlowGroupTx) {
938 EtreeInterface etreeInterface = getEtreeInterfaceByElanInterfaceName(broker, ifName);
939 if (etreeInterface != null) {
940 if (etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
941 EtreeLeafTagName etreeTagName = getEtreeLeafTagByElanTag(elanTag);
942 if (etreeTagName == null) {
943 LOG.warn("Interface {} seems like it belongs to Etree but etreeTagName from elanTag {} is null",
946 Flow flowEntity = buildLocalDmacFlowEntry(etreeTagName.getEtreeLeafTag().getValue(), dpId, ifName,
947 macAddress, elanInfo, ifTag);
948 mdsalApiManager.addFlowToTx(dpId, flowEntity, writeFlowGroupTx);
954 public static String getKnownDynamicmacFlowRef(short tableId, BigInteger dpId, long lporTag, String macAddress,
956 return new StringBuffer().append(tableId).append(elanTag).append(dpId).append(lporTag).append(macAddress)
960 public static String getKnownDynamicmacFlowRef(short tableId, BigInteger dpId, BigInteger remoteDpId,
961 String macAddress, long elanTag) {
962 return new StringBuffer().append(tableId).append(elanTag).append(dpId).append(remoteDpId).append(macAddress)
966 public static String getKnownDynamicmacFlowRef(short tableId, BigInteger dpId, String macAddress, long elanTag) {
967 return new StringBuffer().append(tableId).append(elanTag).append(dpId).append(macAddress).toString();
970 private static String getKnownDynamicmacFlowRef(short elanDmacTable, BigInteger dpId, String extDeviceNodeId,
971 String dstMacAddress, long elanTag, boolean shFlag) {
972 return new StringBuffer().append(elanDmacTable).append(elanTag).append(dpId).append(extDeviceNodeId)
973 .append(dstMacAddress).append(shFlag).toString();
977 * Builds the flow to be programmed in the DMAC table of the local DPN (that
978 * is, where the MAC is attached to). This flow consists in:
980 * <p>Match: + elanTag in metadata + packet goes to a MAC locally attached
981 * Actions: + optionally, pop-vlan + set-vlan-id + output to ifName's
998 public Flow buildLocalDmacFlowEntry(long elanTag, BigInteger dpId, String ifName, String macAddress,
999 ElanInstance elanInfo, long ifTag) {
1001 List<MatchInfo> mkMatches = new ArrayList<>();
1002 mkMatches.add(new MatchMetadata(getElanMetadataLabel(elanTag), MetaDataUtil.METADATA_MASK_SERVICE));
1003 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddress)));
1005 List<Instruction> mkInstructions = new ArrayList<>();
1006 List<Action> actions = getEgressActionsForInterface(ifName, /* tunnelKey */ null);
1007 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
1008 Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE,
1009 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, ifTag, macAddress, elanTag), 20,
1010 elanInfo.getElanInstanceName(), 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(
1011 BigInteger.valueOf(elanTag)), mkMatches, mkInstructions);
1016 public void setupRemoteDmacFlow(BigInteger srcDpId, BigInteger destDpId, int lportTag, long elanTag,
1017 String macAddress, String displayName, WriteTransaction writeFlowGroupTx, String interfaceName,
1018 ElanInstance elanInstance) throws ElanException {
1019 Flow flowEntity = buildRemoteDmacFlowEntry(srcDpId, destDpId, lportTag, elanTag, macAddress, displayName,
1021 mdsalManager.addFlowToTx(srcDpId, flowEntity, writeFlowGroupTx);
1022 setupEtreeRemoteDmacFlow(srcDpId, destDpId, lportTag, elanTag, macAddress, displayName, interfaceName,
1023 writeFlowGroupTx, elanInstance);
1026 private void setupEtreeRemoteDmacFlow(BigInteger srcDpId, BigInteger destDpId, int lportTag, long elanTag,
1027 String macAddress, String displayName, String interfaceName,
1028 WriteTransaction writeFlowGroupTx, ElanInstance elanInstance) throws ElanException {
1030 EtreeInterface etreeInterface = getEtreeInterfaceByElanInterfaceName(broker, interfaceName);
1031 if (etreeInterface != null) {
1032 if (etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
1033 EtreeLeafTagName etreeTagName = getEtreeLeafTagByElanTag(elanTag);
1034 if (etreeTagName == null) {
1035 LOG.warn("Interface " + interfaceName
1036 + " seems like it belongs to Etree but etreeTagName from elanTag " + elanTag + " is null.");
1038 flowEntity = buildRemoteDmacFlowEntry(srcDpId, destDpId, lportTag,
1039 etreeTagName.getEtreeLeafTag().getValue(), macAddress, displayName, elanInstance);
1040 mdsalManager.addFlowToTx(srcDpId, flowEntity, writeFlowGroupTx);
1047 * Builds a Flow to be programmed in a remote DPN's DMAC table. This flow
1048 * consists in: Match: + elanTag in packet's metadata + packet going to a
1049 * MAC known to be located in another DPN Actions: + set_tunnel_id(lportTag)
1050 * + output ITM internal tunnel interface with the other DPN
1062 * @param displayName
1064 * @return the flow remote Dmac
1065 * @throws ElanException in case of issues creating the flow objects
1067 @SuppressWarnings("checkstyle:IllegalCatch")
1068 public Flow buildRemoteDmacFlowEntry(BigInteger srcDpId, BigInteger destDpId, int lportTag, long elanTag,
1069 String macAddress, String displayName, ElanInstance elanInstance) throws ElanException {
1070 List<MatchInfo> mkMatches = new ArrayList<>();
1071 mkMatches.add(new MatchMetadata(getElanMetadataLabel(elanTag), MetaDataUtil.METADATA_MASK_SERVICE));
1072 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddress)));
1074 List<Instruction> mkInstructions = new ArrayList<>();
1076 // List of Action for the provided Source and Destination DPIDs
1078 List<Action> actions = null;
1079 if (isVlan(elanInstance) || isFlat(elanInstance)) {
1080 String interfaceName = getExternalElanInterface(elanInstance.getElanInstanceName(), srcDpId);
1081 if (null == interfaceName) {
1082 LOG.error("buildRemoteDmacFlowEntry: Could not find interfaceName for {} {}", srcDpId,
1085 actions = getEgressActionsForInterface(interfaceName, null);
1087 actions = getInternalTunnelItmEgressAction(srcDpId, destDpId, lportTag);
1089 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
1090 } catch (Exception e) {
1091 LOG.error("Could not get egress actions to add to flow for srcDpId=" + srcDpId + ", destDpId=" + destDpId
1092 + ", lportTag=" + lportTag, e);
1095 Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE,
1096 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, srcDpId, destDpId, macAddress, elanTag),
1098 displayName, 0, /* idleTimeout */
1099 0, /* hardTimeout */
1100 ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(BigInteger.valueOf(elanTag)), mkMatches, mkInstructions);
1106 public void deleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, MacEntry macEntry,
1107 WriteTransaction deleteFlowGroupTx) {
1108 if (elanInfo == null || interfaceInfo == null) {
1111 String macAddress = macEntry.getMacAddress().getValue();
1112 synchronized (getElanMacDPNKey(elanInfo.getElanTag(), macAddress, interfaceInfo.getDpId())) {
1113 deleteMacFlows(elanInfo, interfaceInfo, macAddress, /* alsoDeleteSMAC */ true, deleteFlowGroupTx);
1117 public void deleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1118 boolean deleteSmac, WriteTransaction deleteFlowGroupTx) {
1119 String elanInstanceName = elanInfo.getElanInstanceName();
1120 List<DpnInterfaces> remoteFEs = getInvolvedDpnsInElan(elanInstanceName);
1121 BigInteger srcdpId = interfaceInfo.getDpId();
1122 boolean isFlowsRemovedInSrcDpn = false;
1123 for (DpnInterfaces dpnInterface : remoteFEs) {
1124 Long elanTag = elanInfo.getElanTag();
1125 BigInteger dstDpId = dpnInterface.getDpId();
1126 if (executeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1127 elanTag, dstDpId, deleteFlowGroupTx)) {
1128 isFlowsRemovedInSrcDpn = true;
1130 executeEtreeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1131 elanTag, dstDpId, deleteFlowGroupTx);
1133 if (!isFlowsRemovedInSrcDpn) {
1134 deleteSmacAndDmacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, deleteFlowGroupTx);
1138 private void executeEtreeDeleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1139 boolean deleteSmac, String elanInstanceName, BigInteger srcdpId, Long elanTag, BigInteger dstDpId,
1140 WriteTransaction deleteFlowGroupTx) {
1141 EtreeLeafTagName etreeLeafTag = getEtreeLeafTagByElanTag(elanTag);
1142 if (etreeLeafTag != null) {
1143 executeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1144 etreeLeafTag.getEtreeLeafTag().getValue(), dstDpId, deleteFlowGroupTx);
1148 private boolean executeDeleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1149 boolean deleteSmac, String elanInstanceName, BigInteger srcdpId, Long elanTag, BigInteger dstDpId,
1150 WriteTransaction deleteFlowGroupTx) {
1151 boolean isFlowsRemovedInSrcDpn = false;
1152 if (dstDpId.equals(srcdpId)) {
1153 isFlowsRemovedInSrcDpn = true;
1154 deleteSmacAndDmacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, deleteFlowGroupTx);
1155 } else if (isDpnPresent(dstDpId)) {
1157 .removeFlowToTx(dstDpId,
1158 MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE, getKnownDynamicmacFlowRef(
1159 NwConstants.ELAN_DMAC_TABLE, dstDpId, srcdpId, macAddress, elanTag)),
1161 LOG.debug("Dmac flow entry deleted for elan:{}, logical interface port:{} and mac address:{} on dpn:{}",
1162 elanInstanceName, interfaceInfo.getPortName(), macAddress, dstDpId);
1164 return isFlowsRemovedInSrcDpn;
1167 private void deleteSmacAndDmacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1168 boolean deleteSmac, WriteTransaction deleteFlowGroupTx) {
1169 String elanInstanceName = elanInfo.getElanInstanceName();
1170 long ifTag = interfaceInfo.getInterfaceTag();
1171 BigInteger srcdpId = interfaceInfo.getDpId();
1172 Long elanTag = elanInfo.getElanTag();
1175 .removeFlowToTx(srcdpId,
1176 MDSALUtil.buildFlow(NwConstants.ELAN_SMAC_TABLE, getKnownDynamicmacFlowRef(
1177 NwConstants.ELAN_SMAC_TABLE, srcdpId, ifTag, macAddress, elanTag)),
1180 mdsalManager.removeFlowToTx(srcdpId,
1181 MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE,
1182 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, srcdpId, ifTag, macAddress, elanTag)),
1184 LOG.debug("All the required flows deleted for elan:{}, logical Interface port:{} and MAC address:{} on dpn:{}",
1185 elanInstanceName, interfaceInfo.getPortName(), macAddress, srcdpId);
1189 * Updates the Elan information in the Operational DS. It also updates the
1190 * ElanInstance in the Config DS by setting the adquired elanTag.
1196 * @param elanInstanceAdded
1197 * the elan instance added
1198 * @param elanInterfaces
1199 * the elan interfaces
1203 public static void updateOperationalDataStore(DataBroker broker, IdManagerService idManager,
1204 ElanInstance elanInstanceAdded, List<String> elanInterfaces, WriteTransaction tx) {
1205 String elanInstanceName = elanInstanceAdded.getElanInstanceName();
1206 Long elanTag = elanInstanceAdded.getElanTag();
1207 if (elanTag == null || elanTag == 0L) {
1208 elanTag = retrieveNewElanTag(idManager, elanInstanceName);
1210 Elan elanInfo = new ElanBuilder().setName(elanInstanceName).setElanInterfaces(elanInterfaces)
1211 .setKey(new ElanKey(elanInstanceName)).build();
1213 // Add the ElanState in the elan-state operational data-store
1214 tx.put(LogicalDatastoreType.OPERATIONAL, getElanInstanceOperationalDataPath(elanInstanceName),
1217 // Add the ElanMacTable in the elan-mac-table operational data-store
1218 MacTable elanMacTable = new MacTableBuilder().setKey(new MacTableKey(elanInstanceName)).build();
1219 tx.put(LogicalDatastoreType.OPERATIONAL, getElanMacTableOperationalDataPath(elanInstanceName),
1220 elanMacTable, true);
1222 ElanTagNameBuilder elanTagNameBuilder = new ElanTagNameBuilder().setElanTag(elanTag)
1223 .setKey(new ElanTagNameKey(elanTag)).setName(elanInstanceName);
1224 long etreeLeafTag = -1;
1225 if (isEtreeInstance(elanInstanceAdded)) {
1226 etreeLeafTag = retrieveNewElanTag(idManager, elanInstanceName + ElanConstants.LEAVES_POSTFIX);
1227 EtreeLeafTagName etreeLeafTagName = new EtreeLeafTagNameBuilder()
1228 .setEtreeLeafTag(new EtreeLeafTag(etreeLeafTag)).build();
1229 elanTagNameBuilder.addAugmentation(EtreeLeafTagName.class, etreeLeafTagName);
1230 addTheLeafTagAsElanTag(broker, elanInstanceName, etreeLeafTag, tx);
1232 ElanTagName elanTagName = elanTagNameBuilder.build();
1234 // Add the ElanTag to ElanName in the elan-tag-name Operational
1236 tx.put(LogicalDatastoreType.OPERATIONAL,
1237 getElanInfoEntriesOperationalDataPath(elanTag), elanTagName);
1239 // Updates the ElanInstance Config DS by setting the just acquired
1241 ElanInstanceBuilder elanInstanceBuilder = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
1242 .setDescription(elanInstanceAdded.getDescription())
1243 .setMacTimeout(elanInstanceAdded.getMacTimeout() == null ? ElanConstants.DEFAULT_MAC_TIME_OUT
1244 : elanInstanceAdded.getMacTimeout())
1245 .setKey(elanInstanceAdded.getKey()).setElanTag(elanTag);
1246 if (isEtreeInstance(elanInstanceAdded)) {
1247 EtreeInstance etreeInstance = new EtreeInstanceBuilder().setEtreeLeafTagVal(new EtreeLeafTag(etreeLeafTag))
1249 elanInstanceBuilder.addAugmentation(EtreeInstance.class, etreeInstance);
1251 ElanInstance elanInstanceWithTag = elanInstanceBuilder.build();
1252 tx.merge(LogicalDatastoreType.CONFIGURATION, getElanInstanceConfigurationDataPath(elanInstanceName),
1253 elanInstanceWithTag, true);
1256 private static void addTheLeafTagAsElanTag(DataBroker broker, String elanInstanceName, long etreeLeafTag,
1257 WriteTransaction tx) {
1258 ElanTagName etreeTagAsElanTag = new ElanTagNameBuilder().setElanTag(etreeLeafTag)
1259 .setKey(new ElanTagNameKey(etreeLeafTag)).setName(elanInstanceName).build();
1260 tx.put(LogicalDatastoreType.OPERATIONAL,
1261 getElanInfoEntriesOperationalDataPath(etreeLeafTag), etreeTagAsElanTag);
1264 private static boolean isEtreeInstance(ElanInstance elanInstanceAdded) {
1265 return elanInstanceAdded.getAugmentation(EtreeInstance.class) != null;
1268 public boolean isDpnPresent(BigInteger dpnId) {
1269 String dpn = String.format("%s:%s", "openflow", dpnId);
1270 NodeId nodeId = new NodeId(dpn);
1272 InstanceIdentifier<Node> node = InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(nodeId))
1274 return read(broker, LogicalDatastoreType.OPERATIONAL, node).isPresent();
1277 public static ServicesInfo getServiceInfo(String elanInstanceName, long elanTag, String interfaceName) {
1278 int priority = ElanConstants.ELAN_SERVICE_PRIORITY;
1279 int instructionKey = 0;
1280 List<Instruction> instructions = new ArrayList<>();
1281 instructions.add(MDSALUtil.buildAndGetWriteMetadaInstruction(getElanMetadataLabel(elanTag),
1282 MetaDataUtil.METADATA_MASK_SERVICE, ++instructionKey));
1283 instructions.add(MDSALUtil.buildAndGetGotoTableInstruction(NwConstants.ELAN_SMAC_TABLE, ++instructionKey));
1285 short serviceIndex = ServiceIndex.getIndex(NwConstants.ELAN_SERVICE_NAME, NwConstants.ELAN_SERVICE_INDEX);
1286 ServicesInfo serviceInfo = InterfaceServiceUtil.buildServiceInfo(
1287 String.format("%s.%s", elanInstanceName, interfaceName), serviceIndex, priority,
1288 NwConstants.COOKIE_ELAN_INGRESS_TABLE, instructions);
1292 public static <T extends DataObject> void syncWrite(DataBroker broker, LogicalDatastoreType datastoreType,
1293 InstanceIdentifier<T> path, T data) {
1294 WriteTransaction tx = broker.newWriteOnlyTransaction();
1295 tx.put(datastoreType, path, data, true);
1296 CheckedFuture<Void, TransactionCommitFailedException> futures = tx.submit();
1299 } catch (InterruptedException | ExecutionException e) {
1300 LOG.error("Error writing to datastore (path, data) : ({}, {})", path, data);
1301 throw new RuntimeException(e.getMessage());
1305 public static BoundServices getBoundServices(String serviceName, short servicePriority, int flowPriority,
1306 BigInteger cookie, List<Instruction> instructions) {
1307 StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookie).setFlowPriority(flowPriority)
1308 .setInstruction(instructions);
1309 return new BoundServicesBuilder().setKey(new BoundServicesKey(servicePriority)).setServiceName(serviceName)
1310 .setServicePriority(servicePriority).setServiceType(ServiceTypeFlowBased.class)
1311 .addAugmentation(StypeOpenflow.class, augBuilder.build()).build();
1314 public static InstanceIdentifier<BoundServices> buildServiceId(String vpnInterfaceName, short serviceIndex) {
1315 return InstanceIdentifier.builder(ServiceBindings.class)
1316 .child(ServicesInfo.class, new ServicesInfoKey(vpnInterfaceName, ServiceModeIngress.class))
1317 .child(BoundServices.class, new BoundServicesKey(serviceIndex)).build();
1321 * Builds the list of actions to be taken when sending the packet over a
1322 * VxLan Tunnel Interface, such as setting the tunnel_id field, the vlanId
1323 * if proceeds and output the packet over the right port.
1325 * @param tunnelIfaceName
1326 * the tunnel iface name
1331 public List<Action> buildTunnelItmEgressActions(String tunnelIfaceName, Long tunnelKey) {
1332 if (tunnelIfaceName != null && !tunnelIfaceName.isEmpty()) {
1333 return buildItmEgressActions(tunnelIfaceName, tunnelKey);
1336 return Collections.emptyList();
1340 * Builds the list of actions to be taken when sending the packet over
1341 * external port such as tunnel, physical port etc.
1343 * @param interfaceName
1344 * the interface name
1346 * can be VNI for VxLAN tunnel interfaces, Gre Key for GRE
1350 @SuppressWarnings("checkstyle:IllegalCatch")
1351 public List<Action> buildItmEgressActions(String interfaceName, Long tunnelKey) {
1352 List<Action> result = Collections.emptyList();
1354 GetEgressActionsForInterfaceInput getEgressActInput = new GetEgressActionsForInterfaceInputBuilder()
1355 .setIntfName(interfaceName).setTunnelKey(tunnelKey).build();
1357 Future<RpcResult<GetEgressActionsForInterfaceOutput>> egressActionsOutputFuture = interfaceManagerRpcService
1358 .getEgressActionsForInterface(getEgressActInput);
1359 if (egressActionsOutputFuture.get().isSuccessful()) {
1360 GetEgressActionsForInterfaceOutput egressActionsOutput = egressActionsOutputFuture.get().getResult();
1361 result = egressActionsOutput.getAction();
1363 } catch (Exception e) {
1364 LOG.error("Error in RPC call getEgressActionsForInterface {}", e);
1367 if (result == null || result.size() == 0) {
1368 LOG.warn("Could not build Egress actions for interface {} and tunnelId {}", interfaceName, tunnelKey);
1374 * Builds the list of actions to be taken when sending the packet over an
1375 * external VxLan tunnel interface, such as stamping the VNI on the VxLAN
1376 * header, setting the vlanId if it proceeds and output the packet over the
1380 * Dpn where the tunnelInterface is located
1382 * NodeId of the ExternalDevice where the packet must be sent to.
1384 * Vni to be stamped on the VxLAN Header.
1385 * @return the external itm egress action
1387 public List<Action> getExternalTunnelItmEgressAction(BigInteger srcDpnId, NodeId torNode, long vni) {
1388 List<Action> result = Collections.emptyList();
1390 GetExternalTunnelInterfaceNameInput input = new GetExternalTunnelInterfaceNameInputBuilder()
1391 .setDestinationNode(torNode.getValue()).setSourceNode(srcDpnId.toString())
1392 .setTunnelType(TunnelTypeVxlan.class).build();
1393 Future<RpcResult<GetExternalTunnelInterfaceNameOutput>> output = itmRpcService
1394 .getExternalTunnelInterfaceName(input);
1396 if (output.get().isSuccessful()) {
1397 GetExternalTunnelInterfaceNameOutput tunnelInterfaceNameOutput = output.get().getResult();
1398 String tunnelIfaceName = tunnelInterfaceNameOutput.getInterfaceName();
1399 LOG.debug("Received tunnelInterfaceName from getTunnelInterfaceName RPC {}", tunnelIfaceName);
1401 result = buildTunnelItmEgressActions(tunnelIfaceName, vni);
1404 } catch (InterruptedException | ExecutionException e) {
1405 LOG.error("Error in RPC call getTunnelInterfaceName {}", e);
1412 * Builds the list of actions to be taken when sending the packet over an
1413 * internal VxLan tunnel interface, such as setting the serviceTag on the
1414 * VNI field of the VxLAN header, setting the vlanId if it proceeds and
1415 * output the packet over the right port.
1417 * @param sourceDpnId
1418 * Dpn where the tunnelInterface is located
1419 * @param destinationDpnId
1420 * Dpn where the packet must be sent to. It is used here in order
1421 * to select the right tunnel interface.
1423 * serviceId to be sent on the VxLAN header.
1424 * @return the internal itm egress action
1426 public List<Action> getInternalTunnelItmEgressAction(BigInteger sourceDpnId, BigInteger destinationDpnId,
1428 List<Action> result = Collections.emptyList();
1430 LOG.debug("In getInternalItmEgressAction Action source {}, destination {}, elanTag {}", sourceDpnId,
1431 destinationDpnId, serviceTag);
1432 Class<? extends TunnelTypeBase> tunType = TunnelTypeVxlan.class;
1433 GetTunnelInterfaceNameInput input = new GetTunnelInterfaceNameInputBuilder()
1434 .setDestinationDpid(destinationDpnId).setSourceDpid(sourceDpnId).setTunnelType(tunType).build();
1435 Future<RpcResult<GetTunnelInterfaceNameOutput>> output = itmRpcService
1436 .getTunnelInterfaceName(input);
1438 if (output.get().isSuccessful()) {
1439 GetTunnelInterfaceNameOutput tunnelInterfaceNameOutput = output.get().getResult();
1440 String tunnelIfaceName = tunnelInterfaceNameOutput.getInterfaceName();
1441 LOG.debug("Received tunnelInterfaceName from getTunnelInterfaceName RPC {}", tunnelIfaceName);
1443 result = buildTunnelItmEgressActions(tunnelIfaceName, serviceTag);
1445 } catch (InterruptedException | ExecutionException e) {
1446 LOG.error("Error in RPC call getTunnelInterfaceName {}", e);
1453 * Build the list of actions to be taken when sending the packet to external
1456 * @param interfaceName
1458 * @return the external port itm egress actions
1460 public List<Action> getExternalPortItmEgressAction(String interfaceName) {
1461 return buildItmEgressActions(interfaceName, null);
1464 public static List<MatchInfo> getTunnelMatchesForServiceId(int elanTag) {
1465 List<MatchInfo> mkMatches = new ArrayList<>();
1466 // Matching metadata
1467 mkMatches.add(new MatchTunnelId(BigInteger.valueOf(elanTag)));
1472 public void removeTerminatingServiceAction(BigInteger destDpId, int serviceId) {
1473 RemoveTerminatingServiceActionsInput input = new RemoveTerminatingServiceActionsInputBuilder()
1474 .setDpnId(destDpId).setServiceId(serviceId).build();
1475 Future<RpcResult<Void>> futureObject = itmRpcService
1476 .removeTerminatingServiceActions(input);
1478 RpcResult<Void> result = futureObject.get();
1479 if (result.isSuccessful()) {
1480 LOG.debug("Successfully completed removeTerminatingServiceActions");
1482 LOG.debug("Failure in removeTerminatingServiceAction RPC call");
1484 } catch (InterruptedException | ExecutionException e) {
1485 LOG.error("Error in RPC call removeTerminatingServiceActions {}", e);
1489 public void createTerminatingServiceActions(BigInteger destDpId, int serviceId, List<Action> actions) {
1490 List<Instruction> mkInstructions = new ArrayList<>();
1491 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
1492 CreateTerminatingServiceActionsInput input = new CreateTerminatingServiceActionsInputBuilder()
1493 .setDpnId(destDpId).setServiceId(serviceId).setInstruction(mkInstructions).build();
1495 itmRpcService.createTerminatingServiceActions(input);
1498 public static TunnelList buildInternalTunnel(DataBroker broker) {
1499 InstanceIdentifier<TunnelList> tunnelListInstanceIdentifier = InstanceIdentifier.builder(TunnelList.class)
1501 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, tunnelListInstanceIdentifier).orNull();
1505 * Gets the external tunnel.
1507 * @param sourceDevice
1509 * @param destinationDevice
1510 * the destination device
1511 * @param datastoreType
1512 * the datastore type
1513 * @return the external tunnel
1515 public ExternalTunnel getExternalTunnel(String sourceDevice, String destinationDevice,
1516 LogicalDatastoreType datastoreType) {
1517 Class<? extends TunnelTypeBase> tunType = TunnelTypeVxlan.class;
1518 InstanceIdentifier<ExternalTunnel> iid = InstanceIdentifier.builder(ExternalTunnelList.class)
1519 .child(ExternalTunnel.class, new ExternalTunnelKey(destinationDevice, sourceDevice, tunType)).build();
1520 return read(broker, datastoreType, iid).orNull();
1524 * Gets the external tunnel.
1526 * @param interfaceName
1527 * the interface name
1528 * @param datastoreType
1529 * the datastore type
1530 * @return the external tunnel
1532 public ExternalTunnel getExternalTunnel(String interfaceName, LogicalDatastoreType datastoreType) {
1533 ExternalTunnel externalTunnel = null;
1534 List<ExternalTunnel> externalTunnels = getAllExternalTunnels(datastoreType);
1535 for (ExternalTunnel tunnel : externalTunnels) {
1536 if (StringUtils.equalsIgnoreCase(interfaceName, tunnel.getTunnelInterfaceName())) {
1537 externalTunnel = tunnel;
1541 return externalTunnel;
1545 * Gets the all external tunnels.
1547 * @param datastoreType
1548 * the data store type
1549 * @return the all external tunnels
1551 public List<ExternalTunnel> getAllExternalTunnels(LogicalDatastoreType datastoreType) {
1552 InstanceIdentifier<ExternalTunnelList> iid = InstanceIdentifier.builder(ExternalTunnelList.class).build();
1553 return read(broker, datastoreType, iid).transform(ExternalTunnelList::getExternalTunnel).or(
1554 Collections.emptyList());
1558 * Installs a Flow in a DPN's DMAC table. The Flow is for a MAC that is
1559 * connected remotely in another CSS and accessible through an internal
1560 * tunnel. It also installs the flow for dropping the packet if it came over
1561 * an ITM tunnel (that is, if the Split-Horizon flag is set)
1564 * Id of the DPN where the MAC Addr is accessible locally
1566 * Id of the DPN where the flow must be installed
1568 * lportTag of the interface where the mac is connected to.
1570 * Identifier of the ELAN
1572 * MAC to be installed in remoteDpId's DMAC table
1573 * @param displayName
1575 * @throws ElanException in case of issues creating the flow objects
1577 public void installDmacFlowsToInternalRemoteMac(BigInteger localDpId, BigInteger remoteDpId, int lportTag,
1578 long elanTag, String macAddress, String displayName) throws ElanException {
1579 Flow flow = buildDmacFlowForInternalRemoteMac(localDpId, remoteDpId, lportTag, elanTag, macAddress,
1581 mdsalManager.installFlow(remoteDpId, flow);
1585 * Installs a Flow in the specified DPN's DMAC table. The flow is for a MAC
1586 * that is connected remotely in an External Device (TOR) and that is
1587 * accessible through an external tunnel. It also installs the flow for
1588 * dropping the packet if it came over an ITM tunnel (that is, if the
1589 * Split-Horizon flag is set)
1592 * Id of the DPN where the flow must be installed
1593 * @param extDeviceNodeId
1594 * the ext device node id
1601 * @param displayName
1603 * @param interfaceName
1604 * the interface name
1606 * @return the dmac flows
1607 * @throws ElanException in case of issues creating the flow objects
1609 public List<ListenableFuture<Void>> installDmacFlowsToExternalRemoteMac(BigInteger dpnId,
1610 String extDeviceNodeId, Long elanTag, Long vni, String macAddress, String displayName,
1611 String interfaceName) throws ElanException {
1612 List<ListenableFuture<Void>> futures = new ArrayList<>();
1613 synchronized (getElanMacDPNKey(elanTag, macAddress, dpnId)) {
1614 Flow flow = buildDmacFlowForExternalRemoteMac(dpnId, extDeviceNodeId, elanTag, vni, macAddress,
1616 futures.add(mdsalManager.installFlow(dpnId, flow));
1618 Flow dropFlow = buildDmacFlowDropIfPacketComingFromTunnel(dpnId, extDeviceNodeId, elanTag, macAddress);
1619 futures.add(mdsalManager.installFlow(dpnId, dropFlow));
1620 installEtreeDmacFlowsToExternalRemoteMac(dpnId, extDeviceNodeId, elanTag, vni, macAddress, displayName,
1621 interfaceName, futures);
1626 private void installEtreeDmacFlowsToExternalRemoteMac(BigInteger dpnId, String extDeviceNodeId, Long elanTag,
1627 Long vni, String macAddress, String displayName, String interfaceName,
1628 List<ListenableFuture<Void>> futures) throws ElanException {
1629 EtreeLeafTagName etreeLeafTag = getEtreeLeafTagByElanTag(elanTag);
1630 if (etreeLeafTag != null) {
1631 buildEtreeDmacFlowDropIfPacketComingFromTunnel(dpnId, extDeviceNodeId, elanTag, macAddress, futures,
1633 buildEtreeDmacFlowForExternalRemoteMac(dpnId, extDeviceNodeId, vni, macAddress, displayName, interfaceName,
1634 futures, etreeLeafTag);
1638 private void buildEtreeDmacFlowForExternalRemoteMac(BigInteger dpnId, String extDeviceNodeId, Long vni,
1639 String macAddress, String displayName, String interfaceName, List<ListenableFuture<Void>> futures,
1640 EtreeLeafTagName etreeLeafTag) throws ElanException {
1641 boolean isRoot = false;
1642 if (interfaceName == null) {
1645 EtreeInterface etreeInterface = getEtreeInterfaceByElanInterfaceName(broker, interfaceName);
1646 if (etreeInterface != null) {
1647 if (etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
1653 Flow flow = buildDmacFlowForExternalRemoteMac(dpnId, extDeviceNodeId,
1654 etreeLeafTag.getEtreeLeafTag().getValue(), vni, macAddress, displayName);
1655 futures.add(mdsalManager.installFlow(dpnId, flow));
1659 private void buildEtreeDmacFlowDropIfPacketComingFromTunnel(BigInteger dpnId, String extDeviceNodeId,
1660 Long elanTag, String macAddress, List<ListenableFuture<Void>> futures, EtreeLeafTagName etreeLeafTag) {
1661 if (etreeLeafTag != null) {
1662 Flow dropFlow = buildDmacFlowDropIfPacketComingFromTunnel(dpnId, extDeviceNodeId,
1663 etreeLeafTag.getEtreeLeafTag().getValue(), macAddress);
1664 futures.add(mdsalManager.installFlow(dpnId, dropFlow));
1668 public static List<MatchInfo> buildMatchesForElanTagShFlagAndDstMac(long elanTag, boolean shFlag, String macAddr) {
1669 List<MatchInfo> mkMatches = new ArrayList<>();
1671 new MatchMetadata(getElanMetadataLabel(elanTag, shFlag), MetaDataUtil.METADATA_MASK_SERVICE_SH_FLAG));
1672 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddr)));
1678 * Builds a Flow to be programmed in a DPN's DMAC table. This method must be
1679 * used when the MAC is located in an External Device (TOR). The flow
1680 * matches on the specified MAC and 1) sends the packet over the CSS-TOR
1681 * tunnel if SHFlag is not set, or 2) drops it if SHFlag is set (what means
1682 * the packet came from an external tunnel)
1685 * DPN whose DMAC table is going to be modified
1686 * @param extDeviceNodeId
1687 * Hwvtep node where the mac is attached to
1689 * ElanId to which the MAC is being added to
1692 * @param dstMacAddress
1693 * The mac address to be programmed
1694 * @param displayName
1697 * @throws ElanException in case of issues creating the flow objects
1699 @SuppressWarnings("checkstyle:IllegalCatch")
1700 public Flow buildDmacFlowForExternalRemoteMac(BigInteger dpId, String extDeviceNodeId, long elanTag,
1701 Long vni, String dstMacAddress, String displayName) throws ElanException {
1702 List<MatchInfo> mkMatches = buildMatchesForElanTagShFlagAndDstMac(elanTag, /* shFlag */ false, dstMacAddress);
1703 List<Instruction> mkInstructions = new ArrayList<>();
1705 List<Action> actions = getExternalTunnelItmEgressAction(dpId, new NodeId(extDeviceNodeId), vni);
1706 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
1707 } catch (Exception e) {
1708 LOG.error("Could not get Egress Actions for DpId=" + dpId + ", externalNode=" + extDeviceNodeId, e);
1711 Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE,
1712 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, extDeviceNodeId, dstMacAddress, elanTag,
1715 displayName, 0, /* idleTimeout */
1716 0, /* hardTimeout */
1717 ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(BigInteger.valueOf(elanTag)), mkMatches, mkInstructions);
1723 * Builds the flow that drops the packet if it came through an external
1724 * tunnel, that is, if the Split-Horizon flag is set.
1727 * DPN whose DMAC table is going to be modified
1728 * @param extDeviceNodeId
1729 * Hwvtep node where the mac is attached to
1731 * ElanId to which the MAC is being added to
1732 * @param dstMacAddress
1733 * The mac address to be programmed
1735 private static Flow buildDmacFlowDropIfPacketComingFromTunnel(BigInteger dpnId, String extDeviceNodeId,
1736 Long elanTag, String dstMacAddress) {
1737 List<MatchInfo> mkMatches = buildMatchesForElanTagShFlagAndDstMac(elanTag, /* shFlag */ true, dstMacAddress);
1738 List<Instruction> mkInstructions = MDSALUtil.buildInstructionsDrop();
1739 String flowId = getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpnId, extDeviceNodeId, dstMacAddress,
1741 Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE, flowId, 20, /* prio */
1742 "Drop", 0, /* idleTimeout */
1743 0, /* hardTimeout */
1744 ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(BigInteger.valueOf(elanTag)), mkMatches, mkInstructions);
1749 private static String getDmacDropFlowId(Long elanTag, String dstMacAddress) {
1750 return new StringBuilder(NwConstants.ELAN_DMAC_TABLE).append(elanTag).append(dstMacAddress).append("Drop")
1755 * Builds a Flow to be programmed in a remote DPN's DMAC table. This method
1756 * must be used when the MAC is located in another CSS.
1758 * <p>This flow consists in: Match: + elanTag in packet's metadata + packet
1759 * going to a MAC known to be located in another DPN Actions: +
1760 * set_tunnel_id(lportTag) + output on ITM internal tunnel interface with
1773 * @param displayName
1776 * @throws ElanException in case of issues creating the flow objects
1778 @SuppressWarnings("checkstyle:IllegalCatch")
1779 public Flow buildDmacFlowForInternalRemoteMac(BigInteger localDpId, BigInteger remoteDpId, int lportTag,
1780 long elanTag, String macAddress, String displayName) throws ElanException {
1781 List<MatchInfo> mkMatches = buildMatchesForElanTagShFlagAndDstMac(elanTag, /* shFlag */ false, macAddress);
1783 List<Instruction> mkInstructions = new ArrayList<>();
1786 // List of Action for the provided Source and Destination DPIDs
1787 List<Action> actions = getInternalTunnelItmEgressAction(localDpId, remoteDpId, lportTag);
1788 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
1789 } catch (Exception e) {
1790 LOG.error("Could not get Egress Actions for localDpId=" + localDpId + ", remoteDpId="
1791 + remoteDpId + ", lportTag=" + lportTag, e);
1794 Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE,
1795 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, localDpId, remoteDpId, macAddress, elanTag),
1797 displayName, 0, /* idleTimeout */
1798 0, /* hardTimeout */
1799 ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(BigInteger.valueOf(elanTag)), mkMatches, mkInstructions);
1806 * Installs or removes flows in DMAC table for MACs that are/were located in
1807 * an external Elan Device.
1810 * Id of the DPN where the DMAC table is going to be modified
1812 * Id of the External Device where the MAC is located
1816 * VNI of the LogicalSwitch to which the MAC belongs to, and that
1817 * is associated with the ELAN
1820 * @param elanInstanceName
1821 * the elan instance name
1822 * @param addOrRemove
1823 * Indicates if flows must be installed or removed.
1824 * @param interfaceName
1825 * the interface name
1826 * @throws ElanException in case of issues creating the flow objects
1827 * @see org.opendaylight.genius.mdsalutil.MDSALUtil.MdsalOp
1829 public void setupDmacFlowsToExternalRemoteMac(BigInteger dpId, String extNodeId, Long elanTag, Long vni,
1830 String macAddress, String elanInstanceName, MdsalOp addOrRemove, String interfaceName)
1831 throws ElanException {
1832 if (addOrRemove == MdsalOp.CREATION_OP) {
1833 installDmacFlowsToExternalRemoteMac(dpId, extNodeId, elanTag, vni, macAddress, elanInstanceName,
1835 } else if (addOrRemove == MdsalOp.REMOVAL_OP) {
1836 deleteDmacFlowsToExternalMac(elanTag, dpId, extNodeId, macAddress);
1841 * Delete dmac flows to external mac.
1847 * @param extDeviceNodeId
1848 * the ext device node id
1849 * @param macToRemove
1853 public List<ListenableFuture<Void>> deleteDmacFlowsToExternalMac(long elanTag, BigInteger dpId,
1854 String extDeviceNodeId, String macToRemove) {
1855 List<ListenableFuture<Void>> futures = new ArrayList<>();
1856 synchronized (getElanMacDPNKey(elanTag, macToRemove, dpId)) {
1857 // Removing the flows that sends the packet on an external tunnel
1858 removeFlowThatSendsThePacketOnAnExternalTunnel(elanTag, dpId, extDeviceNodeId, macToRemove, futures);
1860 // And now removing the drop flow
1861 removeTheDropFlow(elanTag, dpId, extDeviceNodeId, macToRemove, futures);
1863 deleteEtreeDmacFlowsToExternalMac(elanTag, dpId, extDeviceNodeId, macToRemove, futures);
1868 private void deleteEtreeDmacFlowsToExternalMac(long elanTag, BigInteger dpId, String extDeviceNodeId,
1869 String macToRemove, List<ListenableFuture<Void>> futures) {
1870 EtreeLeafTagName etreeLeafTag = getEtreeLeafTagByElanTag(elanTag);
1871 if (etreeLeafTag != null) {
1872 removeFlowThatSendsThePacketOnAnExternalTunnel(etreeLeafTag.getEtreeLeafTag().getValue(), dpId,
1873 extDeviceNodeId, macToRemove, futures);
1874 removeTheDropFlow(etreeLeafTag.getEtreeLeafTag().getValue(), dpId, extDeviceNodeId, macToRemove, futures);
1878 private void removeTheDropFlow(long elanTag, BigInteger dpId, String extDeviceNodeId, String macToRemove,
1879 List<ListenableFuture<Void>> futures) {
1880 String flowId = getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, extDeviceNodeId, macToRemove,
1882 Flow flowToRemove = new FlowBuilder().setId(new FlowId(flowId)).setTableId(NwConstants.ELAN_DMAC_TABLE).build();
1883 futures.add(mdsalManager.removeFlow(dpId, flowToRemove));
1886 private void removeFlowThatSendsThePacketOnAnExternalTunnel(long elanTag, BigInteger dpId,
1887 String extDeviceNodeId, String macToRemove, List<ListenableFuture<Void>> futures) {
1888 String flowId = getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, extDeviceNodeId, macToRemove,
1890 Flow flowToRemove = new FlowBuilder().setId(new FlowId(flowId)).setTableId(NwConstants.ELAN_DMAC_TABLE).build();
1891 futures.add(mdsalManager.removeFlow(dpId, flowToRemove));
1895 * Gets the dpid from interface.
1897 * @param interfaceName
1898 * the interface name
1899 * @return the dpid from interface
1901 public BigInteger getDpidFromInterface(String interfaceName) {
1902 BigInteger dpId = null;
1903 Future<RpcResult<GetDpidFromInterfaceOutput>> output = interfaceManagerRpcService
1904 .getDpidFromInterface(new GetDpidFromInterfaceInputBuilder().setIntfName(interfaceName).build());
1906 RpcResult<GetDpidFromInterfaceOutput> rpcResult = output.get();
1907 if (rpcResult.isSuccessful()) {
1908 dpId = rpcResult.getResult().getDpid();
1910 } catch (NullPointerException | InterruptedException | ExecutionException e) {
1911 LOG.error("Failed to get the DPN ID: {} for interface {}: {} ", dpId, interfaceName, e);
1917 * Checks if is interface operational.
1919 * @param interfaceName
1920 * the interface name
1923 * @return true, if is interface operational
1925 public static boolean isInterfaceOperational(String interfaceName, DataBroker dataBroker) {
1926 if (StringUtils.isBlank(interfaceName)) {
1929 Interface ifState = getInterfaceStateFromOperDS(interfaceName, dataBroker);
1930 if (ifState == null) {
1933 return ifState.getOperStatus() == OperStatus.Up && ifState.getAdminStatus() == AdminStatus.Up;
1937 * Gets the interface state from operational ds.
1939 * @param interfaceName
1940 * the interface name
1943 * @return the interface state from oper ds
1945 public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1946 .ietf.interfaces.rev140508.interfaces.state.Interface getInterfaceStateFromOperDS(
1947 String interfaceName, DataBroker dataBroker) {
1948 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1949 .ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId = createInterfaceStateInstanceIdentifier(
1951 return MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, ifStateId).orNull();
1955 * Creates the interface state instance identifier.
1957 * @param interfaceName
1958 * the interface name
1959 * @return the instance identifier
1961 public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1962 .ietf.interfaces.rev140508.interfaces.state.Interface> createInterfaceStateInstanceIdentifier(
1963 String interfaceName) {
1964 InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1965 .ietf.interfaces.rev140508.interfaces.state.Interface> idBuilder = InstanceIdentifier
1966 .builder(InterfacesState.class)
1967 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1968 .ietf.interfaces.rev140508.interfaces.state.Interface.class,
1969 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1970 .ietf.interfaces.rev140508.interfaces.state.InterfaceKey(
1972 return idBuilder.build();
1975 public static CheckedFuture<Void, TransactionCommitFailedException> waitForTransactionToComplete(
1976 WriteTransaction tx) {
1977 CheckedFuture<Void, TransactionCommitFailedException> futures = tx.submit();
1980 } catch (InterruptedException | ExecutionException e) {
1981 LOG.error("Error writing to datastore {}", e);
1986 public static boolean isVxlan(ElanInstance elanInstance) {
1987 return elanInstance != null && elanInstance.getSegmentType() != null
1988 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1989 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId() != 0;
1992 public static boolean isVlan(ElanInstance elanInstance) {
1993 return elanInstance != null && elanInstance.getSegmentType() != null
1994 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVlan.class)
1995 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId() != 0;
1998 public static boolean isFlat(ElanInstance elanInstance) {
1999 return elanInstance != null && elanInstance.getSegmentType() != null
2000 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeFlat.class);
2003 public static boolean isEtreeRootInterfaceByInterfaceName(DataBroker broker, String interfaceName) {
2004 EtreeInterface etreeInterface = getEtreeInterfaceByElanInterfaceName(broker, interfaceName);
2005 if (etreeInterface != null && etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
2011 public void handleDmacRedirectToDispatcherFlows(Long elanTag, String displayName,
2012 String macAddress, int addOrRemove, List<BigInteger> dpnIds) {
2013 for (BigInteger dpId : dpnIds) {
2014 if (addOrRemove == NwConstants.ADD_FLOW) {
2015 WriteTransaction writeTx = broker.newWriteOnlyTransaction();
2016 mdsalManager.addFlowToTx(buildDmacRedirectToDispatcherFlow(dpId, macAddress, displayName, elanTag),
2020 String flowId = getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, macAddress, elanTag);
2021 mdsalManager.removeFlow(dpId, MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE, flowId));
2026 public static FlowEntity buildDmacRedirectToDispatcherFlow(BigInteger dpId, String dstMacAddress,
2027 String displayName, long elanTag) {
2028 List<MatchInfo> matches = new ArrayList<>();
2029 matches.add(new MatchMetadata(getElanMetadataLabel(elanTag), MetaDataUtil.METADATA_MASK_SERVICE));
2030 matches.add(new MatchEthernetDestination(new MacAddress(dstMacAddress)));
2031 List<InstructionInfo> instructions = new ArrayList<>();
2032 List<ActionInfo> actions = new ArrayList<>();
2033 actions.add(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
2035 instructions.add(new InstructionApplyActions(actions));
2036 String flowId = getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, dstMacAddress, elanTag);
2037 FlowEntity flow = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 20, displayName, 0, 0,
2038 ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(BigInteger.valueOf(elanTag)),
2039 matches, instructions);
2043 public static FlowEntity buildDmacRedirectToDispatcherFlowMacNoActions(BigInteger dpId, String dstMacAddress,
2044 String displayName, long elanTag) {
2045 List<MatchInfo> matches = new ArrayList<>();
2046 matches.add(new MatchMetadata(getElanMetadataLabel(elanTag), MetaDataUtil.METADATA_MASK_SERVICE));
2047 matches.add(new MatchEthernetDestination(new MacAddress(dstMacAddress)));
2049 String flowId = getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, dstMacAddress, elanTag);
2050 FlowEntity flow = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 20, displayName, 0, 0,
2051 ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(BigInteger.valueOf(elanTag)),
2052 matches, new ArrayList<>());
2057 * Add Mac Address to ElanInterfaceForwardingEntries and ElanForwardingTables
2058 * Install SMAC and DMAC flows.
2060 public void addMacEntryToDsAndSetupFlows(IInterfaceManager interfaceManager, String interfaceName,
2061 String macAddress, String elanName, WriteTransaction tx, WriteTransaction flowWritetx, int macTimeOut)
2062 throws ElanException {
2063 LOG.trace("Adding mac address {} and interface name {} to ElanInterfaceForwardingEntries and "
2064 + "ElanForwardingTables DS", macAddress, interfaceName);
2065 BigInteger timeStamp = new BigInteger(String.valueOf(System.currentTimeMillis()));
2066 PhysAddress physAddress = new PhysAddress(macAddress);
2067 MacEntry macEntry = new MacEntryBuilder().setInterface(interfaceName).setMacAddress(physAddress)
2068 .setKey(new MacEntryKey(physAddress)).setControllerLearnedForwardingEntryTimestamp(timeStamp)
2069 .setIsStaticAddress(false).build();
2070 InstanceIdentifier<MacEntry> macEntryId = ElanUtils
2071 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
2072 tx.put(LogicalDatastoreType.OPERATIONAL, macEntryId, macEntry);
2073 InstanceIdentifier<MacEntry> elanMacEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName, physAddress);
2074 tx.put(LogicalDatastoreType.OPERATIONAL, elanMacEntryId, macEntry);
2075 ElanInstance elanInstance = ElanUtils.getElanInstanceByName(broker, elanName);
2076 setupMacFlows(elanInstance, interfaceManager.getInterfaceInfo(interfaceName), macTimeOut, macAddress, true,
2081 * Remove Mac Address from ElanInterfaceForwardingEntries and ElanForwardingTables
2082 * Remove SMAC and DMAC flows.
2084 public void deleteMacEntryFromDsAndRemoveFlows(IInterfaceManager interfaceManager, String interfaceName,
2085 String macAddress, String elanName, WriteTransaction tx, WriteTransaction deleteFlowTx) {
2086 LOG.trace("Deleting mac address {} and interface name {} from ElanInterfaceForwardingEntries "
2087 + "and ElanForwardingTables DS", macAddress, interfaceName);
2088 PhysAddress physAddress = new PhysAddress(macAddress);
2089 MacEntry macEntry = getInterfaceMacEntriesOperationalDataPath(interfaceName, physAddress);
2090 InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
2091 if (macEntry != null && interfaceInfo != null) {
2092 deleteMacFlows(ElanUtils.getElanInstanceByName(broker, elanName), interfaceInfo, macEntry, deleteFlowTx);
2094 tx.delete(LogicalDatastoreType.OPERATIONAL,
2095 ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress));
2096 tx.delete(LogicalDatastoreType.OPERATIONAL,
2097 ElanUtils.getMacEntryOperationalDataPath(elanName, physAddress));
2100 public String getExternalElanInterface(String elanInstanceName, BigInteger dpnId) {
2101 DpnInterfaces dpnInterfaces = getElanInterfaceInfoByElanDpn(elanInstanceName, dpnId);
2102 if (dpnInterfaces == null || dpnInterfaces.getInterfaces() == null) {
2103 LOG.trace("Elan {} does not have interfaces in DPN {}", elanInstanceName, dpnId);
2107 for (String dpnInterface : dpnInterfaces.getInterfaces()) {
2108 if (interfaceManager.isExternalInterface(dpnInterface)) {
2109 return dpnInterface;
2113 LOG.trace("Elan {} does not have any external interace attached to DPN {}", elanInstanceName, dpnId);
2117 public static String getElanMacDPNKey(long elanTag, String macAddress, BigInteger dpnId) {
2118 return ("MAC-" + macAddress + " ELAN_TAG-" + elanTag + "DPN_ID-" + dpnId).intern();
2121 public static String getElanMacKey(long elanTag, String macAddress) {
2122 return ("MAC-" + macAddress + " ELAN_TAG-" + elanTag).intern();