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 static java.util.Collections.emptyList;
11 import static org.opendaylight.controller.md.sal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
12 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
14 import com.google.common.base.Optional;
15 import com.google.common.base.Preconditions;
16 import com.google.common.base.Strings;
17 import com.google.common.collect.Lists;
18 import com.google.common.primitives.Ints;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import com.google.common.util.concurrent.FutureCallback;
21 import com.google.common.util.concurrent.Futures;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import com.google.common.util.concurrent.MoreExecutors;
24 import java.math.BigInteger;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Objects;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.Future;
32 import javax.annotation.CheckReturnValue;
33 import javax.annotation.Nonnull;
34 import javax.annotation.Nullable;
35 import javax.inject.Inject;
36 import javax.inject.Singleton;
37 import org.apache.commons.lang3.StringUtils;
38 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
39 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
40 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
41 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
42 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
43 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
44 import org.opendaylight.genius.infra.Datastore;
45 import org.opendaylight.genius.infra.Datastore.Configuration;
46 import org.opendaylight.genius.infra.Datastore.Operational;
47 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
48 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
49 import org.opendaylight.genius.infra.TypedReadTransaction;
50 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
51 import org.opendaylight.genius.infra.TypedWriteTransaction;
52 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
53 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
54 import org.opendaylight.genius.itm.api.IITMProvider;
55 import org.opendaylight.genius.itm.globals.ITMConstants;
56 import org.opendaylight.genius.mdsalutil.ActionInfo;
57 import org.opendaylight.genius.mdsalutil.FlowEntity;
58 import org.opendaylight.genius.mdsalutil.FlowEntityBuilder;
59 import org.opendaylight.genius.mdsalutil.InstructionInfo;
60 import org.opendaylight.genius.mdsalutil.MDSALUtil;
61 import org.opendaylight.genius.mdsalutil.MatchInfo;
62 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
63 import org.opendaylight.genius.mdsalutil.NWUtil;
64 import org.opendaylight.genius.mdsalutil.NwConstants;
65 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
66 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
67 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
68 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
69 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetDestination;
70 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetSource;
71 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
72 import org.opendaylight.genius.mdsalutil.matches.MatchTunnelId;
73 import org.opendaylight.genius.mdsalutil.packet.ARP;
74 import org.opendaylight.genius.mdsalutil.packet.Ethernet;
75 import org.opendaylight.genius.mdsalutil.packet.IPv4;
76 import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
77 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
78 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderUtil;
79 import org.opendaylight.netvirt.elan.cache.ElanInterfaceCache;
80 import org.opendaylight.netvirt.elanmanager.api.ElanHelper;
81 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
82 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
83 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
84 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
85 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
86 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
87 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
88 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.IfIndexesInterfaceMap;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterfaceKey;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefs;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceInput;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceInputBuilder;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceOutput;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceBindings;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeIngress;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceTypeFlowBased;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflow;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflowBuilder;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfoKey;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesBuilder;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesKey;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.ExternalTunnelList;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnel;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnelKey;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.RemoveTerminatingServiceActionsInput;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.RemoveTerminatingServiceActionsInputBuilder;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.RemoveTerminatingServiceActionsOutput;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstanceBuilder;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface.EtreeInterfaceType;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTag;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTagName;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTagNameBuilder;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanForwardingTables;
149 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaceForwardingEntries;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanState;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanTagNameMap;
153 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeFlat;
154 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeVlan;
155 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeVxlan;
156 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
157 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMacKey;
158 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
159 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
160 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
161 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfacesKey;
162 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTable;
163 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTableBuilder;
164 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTableKey;
165 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
166 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
167 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.elan.instance.ElanSegments;
168 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
169 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
170 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntries;
171 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntriesBuilder;
172 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntriesKey;
173 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
174 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.ElanBuilder;
175 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.ElanKey;
176 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
177 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagNameBuilder;
178 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagNameKey;
179 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
180 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
181 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
182 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
183 import org.opendaylight.yangtools.yang.binding.DataObject;
184 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
185 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
186 import org.opendaylight.yangtools.yang.common.RpcResult;
187 import org.slf4j.Logger;
188 import org.slf4j.LoggerFactory;
191 public class ElanUtils {
193 private static final Logger LOG = LoggerFactory.getLogger(ElanUtils.class);
195 private final DataBroker broker;
196 private final ManagedNewTransactionRunner txRunner;
197 private final IMdsalApiManager mdsalManager;
198 private final OdlInterfaceRpcService interfaceManagerRpcService;
199 private final ItmRpcService itmRpcService;
200 private final IInterfaceManager interfaceManager;
201 private final ElanConfig elanConfig;
202 private final ElanItmUtils elanItmUtils;
203 private final ElanEtreeUtils elanEtreeUtils;
204 private final ElanInterfaceCache elanInterfaceCache;
205 private final IITMProvider iitmProvider;
207 public static final FutureCallback<Void> DEFAULT_CALLBACK = new FutureCallback<Void>() {
209 public void onSuccess(Void result) {
210 LOG.debug("Success in Datastore operation");
214 public void onFailure(Throwable error) {
215 LOG.error("Error in Datastore operation", error);
220 public ElanUtils(DataBroker dataBroker, IMdsalApiManager mdsalManager,
221 OdlInterfaceRpcService interfaceManagerRpcService, ItmRpcService itmRpcService, ElanConfig elanConfig,
222 IInterfaceManager interfaceManager, ElanEtreeUtils elanEtreeUtils, ElanItmUtils elanItmUtils,
223 ElanInterfaceCache elanInterfaceCache, IITMProvider iitmProvider) {
224 this.broker = dataBroker;
225 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
226 this.mdsalManager = mdsalManager;
227 this.interfaceManagerRpcService = interfaceManagerRpcService;
228 this.itmRpcService = itmRpcService;
229 this.interfaceManager = interfaceManager;
230 this.elanConfig = elanConfig;
231 this.elanEtreeUtils = elanEtreeUtils;
232 this.elanItmUtils = elanItmUtils;
233 this.elanInterfaceCache = elanInterfaceCache;
234 this.iitmProvider = iitmProvider;
237 public final Boolean isOpenstackVniSemanticsEnforced() {
238 return elanConfig.isOpenstackVniSemanticsEnforced() != null
239 ? elanConfig.isOpenstackVniSemanticsEnforced() : false;
243 * Uses the IdManager to retrieve a brand new ElanTag.
249 * @return the integer
251 public static Long retrieveNewElanTag(IdManagerService idManager, String idKey) {
253 AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(ElanConstants.ELAN_ID_POOL_NAME)
254 .setIdKey(idKey).build();
257 Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
258 RpcResult<AllocateIdOutput> rpcResult = result.get();
259 if (rpcResult.isSuccessful()) {
260 return rpcResult.getResult().getIdValue();
262 LOG.warn("RPC Call to Allocate Id returned with Errors {}", rpcResult.getErrors());
264 } catch (InterruptedException | ExecutionException e) {
265 LOG.warn("Exception when Allocating Id", e);
270 public static void releaseId(IdManagerService idManager, String poolName, String idKey) {
271 ReleaseIdInput releaseIdInput = new ReleaseIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
272 JdkFutures.addErrorLogging(idManager.releaseId(releaseIdInput), LOG, "Release Id");
278 * @deprecated Consider using {@link #read2(LogicalDatastoreType, InstanceIdentifier)} with proper exception
280 * @param broker dataBroker
281 * @param datastoreType Logical DataStore type
282 * @param path IID to read
283 * @param <T> T extends DataObject
284 * @return the read value T
287 @SuppressWarnings("checkstyle:IllegalCatch")
288 public static <T extends DataObject> Optional<T> read(@Nonnull DataBroker broker,
289 LogicalDatastoreType datastoreType, InstanceIdentifier<T> path) {
290 try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
291 return tx.read(datastoreType, path).get();
292 } catch (Exception e) {
293 throw new RuntimeException(e);
297 public <T extends DataObject> Optional<T> read2(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path)
298 throws ReadFailedException {
299 try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
300 CheckedFuture<Optional<T>, ReadFailedException> checkedFuture = tx.read(datastoreType, path);
301 return checkedFuture.checkedGet();
305 @SuppressWarnings("checkstyle:ForbidCertainMethod")
306 public static <T extends DataObject> void delete(DataBroker broker, LogicalDatastoreType datastoreType,
307 InstanceIdentifier<T> path) {
308 WriteTransaction tx = broker.newWriteOnlyTransaction();
309 tx.delete(datastoreType, path);
310 Futures.addCallback(tx.submit(), DEFAULT_CALLBACK, MoreExecutors.directExecutor());
313 public static InstanceIdentifier<ElanInterface> getElanInterfaceConfigurationDataPathId(String interfaceName) {
314 return InstanceIdentifier.builder(ElanInterfaces.class)
315 .child(ElanInterface.class, new ElanInterfaceKey(interfaceName)).build();
318 // elan-state Operational container
320 public static Elan getElanByName(DataBroker broker, String elanInstanceName) {
321 InstanceIdentifier<Elan> elanIdentifier = getElanInstanceOperationalDataPath(elanInstanceName);
322 return MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, elanIdentifier).orNull();
326 public static Elan getElanByName(TypedReadTransaction<Operational> tx, String elanInstanceName)
327 throws ExecutionException, InterruptedException {
328 return tx.read(getElanInstanceOperationalDataPath(elanInstanceName)).get().orNull();
331 public static InstanceIdentifier<Elan> getElanInstanceOperationalDataPath(String elanInstanceName) {
332 return InstanceIdentifier.builder(ElanState.class).child(Elan.class, new ElanKey(elanInstanceName)).build();
335 // grouping of forwarding-entries
337 public MacEntry getInterfaceMacEntriesOperationalDataPath(String interfaceName, PhysAddress physAddress) {
338 InstanceIdentifier<MacEntry> existingMacEntryId = getInterfaceMacEntriesIdentifierOperationalDataPath(
339 interfaceName, physAddress);
340 return read(broker, LogicalDatastoreType.OPERATIONAL, existingMacEntryId).orNull();
344 public MacEntry getInterfaceMacEntriesOperationalDataPathFromId(TypedReadTransaction<Operational> tx,
345 InstanceIdentifier<MacEntry> identifier) throws ExecutionException, InterruptedException {
346 return tx.read(identifier).get().orNull();
349 public static InstanceIdentifier<MacEntry> getInterfaceMacEntriesIdentifierOperationalDataPath(String interfaceName,
350 PhysAddress physAddress) {
351 return InstanceIdentifier.builder(ElanInterfaceForwardingEntries.class)
352 .child(ElanInterfaceMac.class, new ElanInterfaceMacKey(interfaceName))
353 .child(MacEntry.class, new MacEntryKey(physAddress)).build();
357 // elan-forwarding-tables Operational container
358 public Optional<MacEntry> getMacEntryForElanInstance(String elanName, PhysAddress physAddress) {
359 InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanName, physAddress);
360 return read(broker, LogicalDatastoreType.OPERATIONAL, macId);
363 public Optional<MacEntry> getMacEntryForElanInstance(TypedReadTransaction<Operational> tx, String elanName,
364 PhysAddress physAddress) throws ExecutionException, InterruptedException {
365 InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanName, physAddress);
366 return tx.read(macId).get();
370 public MacEntry getMacEntryFromElanMacId(TypedReadTransaction<Operational> tx,
371 InstanceIdentifier<MacEntry> identifier) throws ExecutionException, InterruptedException {
372 return tx.read(identifier).get().orNull();
375 public static InstanceIdentifier<MacEntry> getMacEntryOperationalDataPath(String elanName,
376 PhysAddress physAddress) {
377 return InstanceIdentifier.builder(ElanForwardingTables.class).child(MacTable.class, new MacTableKey(elanName))
378 .child(MacEntry.class, new MacEntryKey(physAddress)).build();
381 public static InstanceIdentifier<MacTable> getElanMacTableOperationalDataPath(String elanName) {
382 return InstanceIdentifier.builder(ElanForwardingTables.class).child(MacTable.class, new MacTableKey(elanName))
386 // elan-interface-forwarding-entries Operational container
387 public ElanInterfaceMac getElanInterfaceMacByInterfaceName(String interfaceName) {
388 return getElanInterfaceMacByInterfaceName(broker, interfaceName);
392 public static ElanInterfaceMac getElanInterfaceMacByInterfaceName(DataBroker dataBroker, String interfaceName) {
393 InstanceIdentifier<ElanInterfaceMac> elanInterfaceId = getElanInterfaceMacEntriesOperationalDataPath(
395 return read(dataBroker, LogicalDatastoreType.OPERATIONAL, elanInterfaceId).orNull();
398 public static InstanceIdentifier<ElanInterfaceMac> getElanInterfaceMacEntriesOperationalDataPath(
399 String interfaceName) {
400 return InstanceIdentifier.builder(ElanInterfaceForwardingEntries.class)
401 .child(ElanInterfaceMac.class, new ElanInterfaceMacKey(interfaceName)).build();
405 * Returns the list of Interfaces that belong to an Elan on an specific DPN.
406 * Data retrieved from Elan's operational DS: elan-dpn-interfaces container.
408 * @param elanInstanceName
409 * name of the Elan to which the interfaces must belong to
411 * Id of the DPN where the interfaces are located
412 * @return the elan interface Info
415 public DpnInterfaces getElanInterfaceInfoByElanDpn(String elanInstanceName, BigInteger dpId) {
416 InstanceIdentifier<DpnInterfaces> elanDpnInterfacesId = getElanDpnInterfaceOperationalDataPath(elanInstanceName,
418 return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfacesId).orNull();
422 * Returns the InstanceIdentifier that points to the Interfaces of an Elan
423 * in a given DPN in the Operational DS. Data retrieved from Elans's
424 * operational DS: dpn-interfaces list
426 * @param elanInstanceName
427 * name of the Elan to which the interfaces must belong to
429 * Id of the DPN where the interfaces are located
430 * @return the elan dpn interface
432 public static InstanceIdentifier<DpnInterfaces> getElanDpnInterfaceOperationalDataPath(String elanInstanceName,
434 return InstanceIdentifier.builder(ElanDpnInterfaces.class)
435 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName))
436 .child(DpnInterfaces.class, new DpnInterfacesKey(dpId)).build();
439 // elan-tag-name-map Operational Container
441 public ElanTagName getElanInfoByElanTag(long elanTag) {
442 InstanceIdentifier<ElanTagName> elanId = getElanInfoEntriesOperationalDataPath(elanTag);
443 Optional<ElanTagName> existingElanInfo = read(broker,
444 LogicalDatastoreType.OPERATIONAL, elanId);
445 return existingElanInfo.orNull();
448 public static InstanceIdentifier<ElanTagName> getElanInfoEntriesOperationalDataPath(long elanTag) {
449 return InstanceIdentifier.builder(ElanTagNameMap.class).child(ElanTagName.class, new ElanTagNameKey(elanTag))
453 // interface-index-tag operational container
454 public Optional<IfIndexInterface> getInterfaceInfoByInterfaceTag(long interfaceTag) {
455 InstanceIdentifier<IfIndexInterface> interfaceId = getInterfaceInfoEntriesOperationalDataPath(interfaceTag);
456 return read(broker, LogicalDatastoreType.OPERATIONAL, interfaceId);
459 public static InstanceIdentifier<IfIndexInterface> getInterfaceInfoEntriesOperationalDataPath(long interfaceTag) {
460 return InstanceIdentifier.builder(IfIndexesInterfaceMap.class)
461 .child(IfIndexInterface.class, new IfIndexInterfaceKey((int) interfaceTag)).build();
464 public static InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
465 return InstanceIdentifier.builder(ElanDpnInterfaces.class)
466 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
470 public ElanDpnInterfacesList getElanDpnInterfacesList(String elanName) {
471 InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanName);
472 return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId).orNull();
476 public ElanDpnInterfaces getElanDpnInterfacesList() {
477 InstanceIdentifier<ElanDpnInterfaces> elanDpnInterfaceId = InstanceIdentifier.builder(ElanDpnInterfaces.class)
479 return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId).orNull();
483 * This method is useful get all ELAN participating CSS dpIds to install
484 * program remote dmac entries and updating remote bc groups for tor
487 * @param elanInstanceName
488 * the elan instance name
489 * @return list of dpIds
492 public List<BigInteger> getParticipatingDpnsInElanInstance(String elanInstanceName) {
493 List<BigInteger> dpIds = new ArrayList<>();
494 InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanInstanceName);
495 Optional<ElanDpnInterfacesList> existingElanDpnInterfaces = read(broker,
496 LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId);
497 if (!existingElanDpnInterfaces.isPresent()) {
500 List<DpnInterfaces> dpnInterfaces = existingElanDpnInterfaces.get().nonnullDpnInterfaces();
501 for (DpnInterfaces dpnInterface : dpnInterfaces) {
502 dpIds.add(dpnInterface.getDpId());
507 public static long getElanRemoteBroadCastGroupID(long elanTag) {
508 return ElanConstants.ELAN_GID_MIN + elanTag % ElanConstants.ELAN_GID_MIN * 2;
512 * Gets the elan mac table.
516 * @return the elan mac table
519 public MacTable getElanMacTable(String elanName) {
520 return getElanMacTable(broker, elanName);
524 public static MacTable getElanMacTable(DataBroker dataBroker, String elanName) {
525 InstanceIdentifier<MacTable> elanMacTableId = getElanMacTableOperationalDataPath(elanName);
526 return read(dataBroker, LogicalDatastoreType.OPERATIONAL, elanMacTableId).orNull();
529 public static long getElanLocalBCGId(long elanTag) {
530 return ElanConstants.ELAN_GID_MIN + (elanTag % ElanConstants.ELAN_GID_MIN * 2 - 1);
533 public static long getElanRemoteBCGId(long elanTag) {
534 return ElanConstants.ELAN_GID_MIN + elanTag % ElanConstants.ELAN_GID_MIN * 2;
537 public static long getEtreeLeafLocalBCGId(long etreeLeafTag) {
538 return ElanConstants.ELAN_GID_MIN + (etreeLeafTag % ElanConstants.ELAN_GID_MIN * 2 - 1);
541 public static long getEtreeLeafRemoteBCGId(long etreeLeafTag) {
542 return ElanConstants.ELAN_GID_MIN + etreeLeafTag % ElanConstants.ELAN_GID_MIN * 2;
545 public static BigInteger getElanMetadataLabel(long elanTag, boolean isSHFlagSet) {
546 int shBit = isSHFlagSet ? 1 : 0;
547 return BigInteger.valueOf(elanTag).shiftLeft(24).or(BigInteger.valueOf(shBit));
551 * Setting SMAC, DMAC, UDMAC in this DPN and optionally in other DPNs.
555 * @param interfaceInfo
561 * @param configureRemoteFlows
562 * true if remote dmac flows should be configured as well
563 * @param writeFlowGroupTx
566 public void setupMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
567 long macTimeout, String macAddress, boolean configureRemoteFlows,
568 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
569 synchronized (getElanMacDPNKey(elanInfo.getElanTag(), macAddress, interfaceInfo.getDpId())) {
570 setupKnownSmacFlow(elanInfo, interfaceInfo, macTimeout, macAddress, mdsalManager,
572 setupOrigDmacFlows(elanInfo, interfaceInfo, macAddress, configureRemoteFlows, mdsalManager,
577 public void setupDMacFlowOnRemoteDpn(ElanInstance elanInfo, InterfaceInfo interfaceInfo, BigInteger dstDpId,
578 String macAddress, TypedWriteTransaction<Configuration> writeFlowTx) {
579 String elanInstanceName = elanInfo.getElanInstanceName();
580 setupRemoteDmacFlow(dstDpId, interfaceInfo.getDpId(), interfaceInfo.getInterfaceTag(), elanInfo.getElanTag(),
581 macAddress, elanInstanceName, writeFlowTx, interfaceInfo.getInterfaceName(), elanInfo);
582 LOG.info("Remote Dmac flow entry created for elan Name:{}, logical port Name:{} and"
583 + " mac address {} on dpn:{}", elanInstanceName, interfaceInfo.getPortName(),
584 macAddress, dstDpId);
588 * Inserts a Flow in SMAC table to state that the MAC has already been
591 private void setupKnownSmacFlow(ElanInstance elanInfo, InterfaceInfo interfaceInfo, long macTimeout,
592 String macAddress, IMdsalApiManager mdsalApiManager,
593 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
594 FlowEntity flowEntity = buildKnownSmacFlow(elanInfo, interfaceInfo, macTimeout, macAddress);
595 mdsalApiManager.addFlow(writeFlowGroupTx, flowEntity);
596 LOG.debug("Known Smac flow entry created for elan Name:{}, logical Interface port:{} and mac address:{}",
597 elanInfo.getElanInstanceName(), elanInfo.getDescription(), macAddress);
600 public FlowEntity buildKnownSmacFlow(ElanInstance elanInfo, InterfaceInfo interfaceInfo, long macTimeout,
602 int lportTag = interfaceInfo.getInterfaceTag();
603 // Matching metadata and eth_src fields
604 List<MatchInfo> mkMatches = new ArrayList<>();
605 mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanInfo.getElanTag(), lportTag),
606 ElanHelper.getElanMetadataMask()));
607 mkMatches.add(new MatchEthernetSource(new MacAddress(macAddress)));
608 List<InstructionInfo> mkInstructions = new ArrayList<>();
609 mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
610 BigInteger dpId = interfaceInfo.getDpId();
611 long elanTag = getElanTag(elanInfo, interfaceInfo);
612 return new FlowEntityBuilder()
614 .setTableId(NwConstants.ELAN_SMAC_TABLE)
615 .setFlowId(getKnownDynamicmacFlowRef(NwConstants.ELAN_SMAC_TABLE, dpId, lportTag, macAddress, elanTag))
617 .setFlowName(elanInfo.getDescription())
618 .setIdleTimeOut((int) macTimeout)
620 .setCookie(ElanConstants.COOKIE_ELAN_KNOWN_SMAC.add(BigInteger.valueOf(elanTag)))
621 .setMatchInfoList(mkMatches)
622 .setInstructionInfoList(mkInstructions)
624 // If Mac timeout is 0, the flow won't be deleted automatically, so no need to get notified
625 .setSendFlowRemFlag(macTimeout != 0)
629 private Long getElanTag(ElanInstance elanInfo, InterfaceInfo interfaceInfo) {
630 EtreeInterface etreeInterface = elanInterfaceCache.getEtreeInterface(interfaceInfo.getInterfaceName()).orNull();
631 if (etreeInterface == null || etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
632 return elanInfo.getElanTag();
634 EtreeInstance etreeInstance = elanInfo.augmentation(EtreeInstance.class);
635 if (etreeInstance == null) {
636 LOG.warn("EtreeInterface {} is connected to a non-Etree network: {}",
637 interfaceInfo.getInterfaceName(), elanInfo.getElanInstanceName());
638 return elanInfo.getElanTag();
640 return etreeInstance.getEtreeLeafTagVal().getValue();
646 * Installs a Flow in INTERNAL_TUNNEL_TABLE of the affected DPN that sends
647 * the packet through the specified interface if the tunnel_id matches the
648 * interface's lportTag.
650 * @param interfaceInfo
652 * @param mdsalApiManager
653 * the mdsal API manager
654 * @param writeFlowGroupTx
655 * the writeFLowGroup tx
657 public void setupTermDmacFlows(InterfaceInfo interfaceInfo, IMdsalApiManager mdsalApiManager,
658 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
659 BigInteger dpId = interfaceInfo.getDpId();
660 int lportTag = interfaceInfo.getInterfaceTag();
661 Flow flow = MDSALUtil.buildFlowNew(NwConstants.INTERNAL_TUNNEL_TABLE,
662 getIntTunnelTableFlowRef(NwConstants.INTERNAL_TUNNEL_TABLE, lportTag), 5,
663 String.format("%s:%d", "ITM Flow Entry ", lportTag), 0, 0,
664 ITMConstants.COOKIE_ITM.add(BigInteger.valueOf(lportTag)),
665 getTunnelIdMatchForFilterEqualsLPortTag(lportTag),
666 getInstructionsInPortForOutGroup(interfaceInfo.getInterfaceName()));
667 mdsalApiManager.addFlow(writeFlowGroupTx, dpId, flow);
668 LOG.debug("Terminating service table flow entry created on dpn:{} for logical Interface port:{}", dpId,
669 interfaceInfo.getPortName());
673 * Constructs the FlowName for flows installed in the Internal Tunnel Table,
674 * consisting in tableId + elanTag.
680 * @return the Internal tunnel
682 public static String getIntTunnelTableFlowRef(short tableId, int elanTag) {
683 return new StringBuffer().append(tableId).append(elanTag).toString();
687 * Constructs the Matches that checks that the tunnel_id field contains a
691 * lportTag that must be checked against the tunnel_id field
692 * @return the list of match Info
694 public static List<MatchInfo> getTunnelIdMatchForFilterEqualsLPortTag(int lportTag) {
695 List<MatchInfo> mkMatches = new ArrayList<>();
697 mkMatches.add(new MatchTunnelId(BigInteger.valueOf(lportTag)));
702 * Constructs the Instructions that take the packet over a given interface.
705 * Name of the interface where the packet must be sent over. It
706 * can be a local interface or a tunnel interface (internal or
708 * @return the Instruction
710 public List<Instruction> getInstructionsInPortForOutGroup(String ifName) {
711 List<Instruction> mkInstructions = new ArrayList<>();
712 List<Action> actions = getEgressActionsForInterface(ifName, /* tunnelKey */ null);
714 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
715 return mkInstructions;
719 * Returns the list of Actions to be taken when sending the packet through
720 * an Elan interface. Note that this interface can refer to an ElanInterface
721 * where the Elan VM is attached to a DPN or an ITM tunnel interface where
722 * Elan traffic can be sent through. In this latter case, the tunnelKey is
723 * mandatory and it can hold serviceId for internal tunnels or the VNI for
730 * @return the egress actions for interface
732 @SuppressWarnings("checkstyle:IllegalCatch")
734 public List<Action> getEgressActionsForInterface(String ifName, @Nullable Long tunnelKey) {
735 List<Action> listAction = new ArrayList<>();
737 GetEgressActionsForInterfaceInput getEgressActionInput = new GetEgressActionsForInterfaceInputBuilder()
738 .setIntfName(ifName).setTunnelKey(tunnelKey).build();
739 Future<RpcResult<GetEgressActionsForInterfaceOutput>> result = interfaceManagerRpcService
740 .getEgressActionsForInterface(getEgressActionInput);
741 RpcResult<GetEgressActionsForInterfaceOutput> rpcResult = result.get();
742 if (!rpcResult.isSuccessful()) {
743 LOG.debug("RPC Call to Get egress actions for interface {} returned with Errors {}", ifName,
744 rpcResult.getErrors());
746 listAction = rpcResult.getResult().nonnullAction();
748 } catch (Exception e) {
749 LOG.warn("Exception when egress actions for interface {}", ifName, e);
754 private void setupOrigDmacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
755 boolean configureRemoteFlows, IMdsalApiManager mdsalApiManager,
756 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
757 BigInteger dpId = interfaceInfo.getDpId();
758 String ifName = interfaceInfo.getInterfaceName();
759 long ifTag = interfaceInfo.getInterfaceTag();
760 String elanInstanceName = elanInfo.getElanInstanceName();
762 Long elanTag = elanInfo.getElanTag();
764 setupLocalDmacFlow(elanTag, dpId, ifName, macAddress, elanInfo, mdsalApiManager, ifTag,
766 LOG.debug("Dmac flow entry created for elan Name:{}, logical port Name:{} mand mac address:{} "
767 + "on dpn:{}", elanInstanceName, interfaceInfo.getPortName(), macAddress, dpId);
769 if (!configureRemoteFlows) {
773 List<DpnInterfaces> elanDpns = getInvolvedDpnsInElan(elanInstanceName);
774 for (DpnInterfaces elanDpn : elanDpns) {
776 if (Objects.equals(elanDpn.getDpId(), dpId)) {
780 // Check for the Remote DPN present in Inventory Manager
781 if (!isDpnPresent(elanDpn.getDpId())) {
785 // For remote DPNs a flow is needed to indicate that packets of this ELAN going to this MAC need to be
786 // forwarded through the appropriate ITM tunnel
787 setupRemoteDmacFlow(elanDpn.getDpId(), // srcDpn (the remote DPN in this case)
788 dpId, // dstDpn (the local DPN)
789 interfaceInfo.getInterfaceTag(), // lportTag of the local interface
790 elanTag, // identifier of the Elan
791 macAddress, // MAC to be programmed in remote DPN
792 elanInstanceName, writeFlowGroupTx, ifName, elanInfo
794 LOG.debug("Remote Dmac flow entry created for elan Name:{}, logical port Name:{} and mac address:{} on"
795 + " dpn:{}", elanInstanceName, interfaceInfo.getPortName(), macAddress, elanDpn.getDpId());
798 // TODO: Make sure that the same is performed against the ElanDevices.
802 public List<DpnInterfaces> getInvolvedDpnsInElan(String elanName) {
803 return getElanDPNByName(elanName);
807 public List<DpnInterfaces> getElanDPNByName(String elanInstanceName) {
808 InstanceIdentifier<ElanDpnInterfacesList> elanIdentifier = getElanDpnOperationDataPath(elanInstanceName);
809 return MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, elanIdentifier).toJavaUtil().map(
810 ElanDpnInterfacesList::getDpnInterfaces).orElse(emptyList());
813 private void setupLocalDmacFlow(long elanTag, BigInteger dpId, String ifName, String macAddress,
814 ElanInstance elanInfo, IMdsalApiManager mdsalApiManager, long ifTag,
815 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
816 Flow flowEntity = buildLocalDmacFlowEntry(elanTag, dpId, ifName, macAddress, elanInfo, ifTag);
817 mdsalApiManager.addFlow(writeFlowGroupTx, dpId, flowEntity);
818 installEtreeLocalDmacFlow(elanTag, dpId, ifName, macAddress, elanInfo, ifTag, writeFlowGroupTx);
821 private void installEtreeLocalDmacFlow(long elanTag, BigInteger dpId, String ifName, String macAddress,
822 ElanInstance elanInfo, long ifTag, TypedWriteTransaction<Configuration> writeFlowGroupTx) {
823 EtreeInterface etreeInterface = elanInterfaceCache.getEtreeInterface(ifName).orNull();
824 if (etreeInterface != null && etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
825 EtreeLeafTagName etreeTagName = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag);
826 if (etreeTagName == null) {
827 LOG.warn("Interface {} seems like it belongs to Etree but etreeTagName from elanTag {} is null",
830 Flow flowEntity = buildLocalDmacFlowEntry(etreeTagName.getEtreeLeafTag().getValue(), dpId, ifName,
831 macAddress, elanInfo, ifTag);
832 mdsalManager.addFlow(writeFlowGroupTx, dpId, flowEntity);
837 public static String getKnownDynamicmacFlowRef(short tableId, BigInteger dpId, long lporTag, String macAddress,
839 return String.valueOf(tableId) + elanTag + dpId + lporTag + macAddress;
842 public static String getKnownDynamicmacFlowRef(short tableId, BigInteger dpId, BigInteger remoteDpId,
843 String macAddress, long elanTag) {
844 return String.valueOf(tableId) + elanTag + dpId + remoteDpId + macAddress;
847 public static String getKnownDynamicmacFlowRef(short tableId, BigInteger dpId, String macAddress, long elanTag) {
848 return String.valueOf(tableId) + elanTag + dpId + macAddress;
851 public static String getKnownDynamicmacFlowRef(short elanDmacTable, BigInteger dpId, String extDeviceNodeId,
852 String dstMacAddress, long elanTag, boolean shFlag) {
853 return String.valueOf(elanDmacTable) + elanTag + dpId + extDeviceNodeId + dstMacAddress + shFlag;
857 * Builds the flow to be programmed in the DMAC table of the local DPN (that
858 * is, where the MAC is attached to). This flow consists in:
860 * <p>Match: + elanTag in metadata + packet goes to a MAC locally attached
861 * Actions: + optionally, pop-vlan + set-vlan-id + output to ifName's
878 public Flow buildLocalDmacFlowEntry(long elanTag, BigInteger dpId, String ifName, String macAddress,
879 ElanInstance elanInfo, long ifTag) {
881 List<MatchInfo> mkMatches = new ArrayList<>();
882 mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag), MetaDataUtil.METADATA_MASK_SERVICE));
883 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddress)));
885 List<Instruction> mkInstructions = new ArrayList<>();
886 List<Action> actions = getEgressActionsForInterface(ifName, /* tunnelKey */ null);
887 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
888 Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE,
889 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, ifTag, macAddress, elanTag), 20,
890 elanInfo.getElanInstanceName(), 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(
891 BigInteger.valueOf(elanTag)), mkMatches, mkInstructions);
896 public void setupRemoteDmacFlow(BigInteger srcDpId, BigInteger destDpId, int lportTag, long elanTag,
897 String macAddress, String displayName, TypedWriteTransaction<Configuration> writeFlowGroupTx,
898 String interfaceName, ElanInstance elanInstance) {
899 if (interfaceManager.isExternalInterface(interfaceName)) {
900 LOG.debug("Ignoring install remote DMAC {} flow on provider interface {} elan {}",
901 macAddress, interfaceName, elanInstance.getElanInstanceName());
905 // if openstack-vni-semantics are enforced, segmentation ID is passed as network VNI for VxLAN based provider
906 // networks, 0 otherwise
907 long lportTagOrVni = !isOpenstackVniSemanticsEnforced() ? lportTag : isVxlanNetworkOrVxlanSegment(elanInstance)
908 ? getVxlanSegmentationId(elanInstance) : 0;
909 flowEntity = buildRemoteDmacFlowEntry(srcDpId, destDpId, lportTagOrVni, elanTag, macAddress, displayName,
911 mdsalManager.addFlow(writeFlowGroupTx, srcDpId, flowEntity);
912 setupEtreeRemoteDmacFlow(srcDpId, destDpId, lportTagOrVni, elanTag, macAddress, displayName, interfaceName,
913 writeFlowGroupTx, elanInstance);
916 private void setupEtreeRemoteDmacFlow(BigInteger srcDpId, BigInteger destDpId, long lportTagOrVni, long elanTag,
917 String macAddress, String displayName, String interfaceName,
918 TypedWriteTransaction<Configuration> writeFlowGroupTx,
919 ElanInstance elanInstance) {
921 EtreeInterface etreeInterface = elanInterfaceCache.getEtreeInterface(interfaceName).orNull();
922 if (etreeInterface != null && etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
923 EtreeLeafTagName etreeTagName = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag);
924 if (etreeTagName == null) {
925 LOG.warn("Interface {} seems like it belongs to Etree but etreeTagName from elanTag {} is null.",
926 interfaceName, elanTag);
928 flowEntity = buildRemoteDmacFlowEntry(srcDpId, destDpId, lportTagOrVni,
929 etreeTagName.getEtreeLeafTag().getValue(), macAddress, displayName, elanInstance);
930 mdsalManager.addFlow(writeFlowGroupTx, srcDpId, flowEntity);
936 * Builds a Flow to be programmed in a remote DPN's DMAC table. This flow
937 * consists in: Match: + elanTag in packet's metadata + packet going to a
938 * MAC known to be located in another DPN Actions: + set_tunnel_id
939 * + output ITM internal tunnel interface with the other DPN
945 * @param lportTagOrVni
946 * lportTag or network VNI
953 * @param elanInstance
955 * @return the flow remote Dmac
957 @SuppressWarnings("checkstyle:IllegalCatch")
958 public Flow buildRemoteDmacFlowEntry(BigInteger srcDpId, BigInteger destDpId, long lportTagOrVni, long elanTag,
959 String macAddress, String displayName, ElanInstance elanInstance) {
960 List<MatchInfo> mkMatches = new ArrayList<>();
961 mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag), MetaDataUtil.METADATA_MASK_SERVICE));
962 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddress)));
964 List<Instruction> mkInstructions = new ArrayList<>();
966 // List of Action for the provided Source and Destination DPIDs
968 List<Action> actions = null;
969 if (isVxlanNetworkOrVxlanSegment(elanInstance)) {
970 actions = elanItmUtils.getInternalTunnelItmEgressAction(srcDpId, destDpId, lportTagOrVni);
971 } else if (isVlan(elanInstance) || isFlat(elanInstance)) {
972 String interfaceName = getExternalElanInterface(elanInstance.getElanInstanceName(), srcDpId);
973 if (null == interfaceName) {
974 LOG.info("buildRemoteDmacFlowEntry: Could not find interfaceName for {} {}", srcDpId,
977 actions = getEgressActionsForInterface(interfaceName, null);
979 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
980 } catch (Exception e) {
981 LOG.error("Could not get egress actions to add to flow for srcDpId {}, destDpId {}, lportTag/VNI {}",
982 srcDpId, destDpId, lportTagOrVni, e);
985 Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE,
986 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, srcDpId, destDpId, macAddress, elanTag),
988 displayName, 0, /* idleTimeout */
990 ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(BigInteger.valueOf(elanTag)), mkMatches, mkInstructions);
996 public void deleteMacFlows(@Nullable ElanInstance elanInfo, @Nullable InterfaceInfo interfaceInfo,
997 MacEntry macEntry, TypedReadWriteTransaction<Configuration> flowTx)
998 throws ExecutionException, InterruptedException {
999 if (elanInfo == null || interfaceInfo == null) {
1002 String macAddress = macEntry.getMacAddress().getValue();
1003 synchronized (getElanMacDPNKey(elanInfo.getElanTag(), macAddress, interfaceInfo.getDpId())) {
1004 deleteMacFlows(elanInfo, interfaceInfo, macAddress, /* alsoDeleteSMAC */ true, flowTx);
1008 public void deleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1009 boolean deleteSmac, TypedReadWriteTransaction<Configuration> flowTx)
1010 throws ExecutionException, InterruptedException {
1011 String elanInstanceName = elanInfo.getElanInstanceName();
1012 List<DpnInterfaces> remoteFEs = getInvolvedDpnsInElan(elanInstanceName);
1013 BigInteger srcdpId = interfaceInfo.getDpId();
1014 boolean isFlowsRemovedInSrcDpn = false;
1015 for (DpnInterfaces dpnInterface : remoteFEs) {
1016 Long elanTag = elanInfo.getElanTag();
1017 BigInteger dstDpId = dpnInterface.getDpId();
1018 if (executeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1019 elanTag, dstDpId, flowTx)) {
1020 isFlowsRemovedInSrcDpn = true;
1022 executeEtreeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1023 elanTag, dstDpId, flowTx);
1025 if (!isFlowsRemovedInSrcDpn) {
1026 deleteSmacAndDmacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, flowTx);
1030 private void executeEtreeDeleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1031 boolean deleteSmac, String elanInstanceName, BigInteger srcdpId, Long elanTag, BigInteger dstDpId,
1032 TypedReadWriteTransaction<Configuration> flowTx) throws ExecutionException, InterruptedException {
1033 EtreeLeafTagName etreeLeafTag = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag);
1034 if (etreeLeafTag != null) {
1035 executeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1036 etreeLeafTag.getEtreeLeafTag().getValue(), dstDpId, flowTx);
1040 private boolean executeDeleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1041 boolean deleteSmac, String elanInstanceName, BigInteger srcdpId, Long elanTag, BigInteger dstDpId,
1042 TypedReadWriteTransaction<Configuration> flowTx) throws ExecutionException, InterruptedException {
1043 boolean isFlowsRemovedInSrcDpn = false;
1044 if (dstDpId.equals(srcdpId)) {
1045 isFlowsRemovedInSrcDpn = true;
1046 deleteSmacAndDmacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, flowTx);
1047 } else if (isDpnPresent(dstDpId)) {
1049 .removeFlow(flowTx, dstDpId,
1050 MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE, getKnownDynamicmacFlowRef(
1051 NwConstants.ELAN_DMAC_TABLE, dstDpId, srcdpId, macAddress, elanTag)));
1052 LOG.debug("Dmac flow entry deleted for elan:{}, logical interface port:{} and mac address:{} on dpn:{}",
1053 elanInstanceName, interfaceInfo.getPortName(), macAddress, dstDpId);
1055 return isFlowsRemovedInSrcDpn;
1058 private void deleteSmacAndDmacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1059 boolean deleteSmac, TypedReadWriteTransaction<Configuration> flowTx)
1060 throws ExecutionException, InterruptedException {
1061 String elanInstanceName = elanInfo.getElanInstanceName();
1062 long ifTag = interfaceInfo.getInterfaceTag();
1063 BigInteger srcdpId = interfaceInfo.getDpId();
1064 Long elanTag = elanInfo.getElanTag();
1067 .removeFlow(flowTx, srcdpId,
1068 MDSALUtil.buildFlow(NwConstants.ELAN_SMAC_TABLE, getKnownDynamicmacFlowRef(
1069 NwConstants.ELAN_SMAC_TABLE, srcdpId, ifTag, macAddress, elanTag)));
1071 mdsalManager.removeFlow(flowTx, srcdpId,
1072 MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE,
1073 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, srcdpId, ifTag, macAddress, elanTag)));
1074 LOG.debug("All the required flows deleted for elan:{}, logical Interface port:{} and MAC address:{} on dpn:{}",
1075 elanInstanceName, interfaceInfo.getPortName(), macAddress, srcdpId);
1079 * Updates the Elan information in the Operational DS. It also updates the
1080 * ElanInstance in the Config DS by setting the adquired elanTag.
1084 * @param elanInstanceAdded
1085 * the elan instance added
1086 * @param elanInterfaces
1087 * the elan interfaces
1091 * @return the updated ELAN instance.
1093 public static ElanInstance updateOperationalDataStore(IdManagerService idManager,
1094 ElanInstance elanInstanceAdded, List<String> elanInterfaces, TypedWriteTransaction<Configuration> confTx,
1095 TypedWriteTransaction<Operational> operTx) {
1096 String elanInstanceName = elanInstanceAdded.getElanInstanceName();
1097 Long elanTag = elanInstanceAdded.getElanTag();
1098 if (elanTag == null || elanTag == 0L) {
1099 elanTag = retrieveNewElanTag(idManager, elanInstanceName);
1101 Elan elanInfo = new ElanBuilder().setName(elanInstanceName).setElanInterfaces(elanInterfaces)
1102 .withKey(new ElanKey(elanInstanceName)).build();
1104 // Add the ElanState in the elan-state operational data-store
1105 operTx.put(getElanInstanceOperationalDataPath(elanInstanceName), elanInfo, CREATE_MISSING_PARENTS);
1107 // Add the ElanMacTable in the elan-mac-table operational data-store
1108 MacTable elanMacTable = new MacTableBuilder().withKey(new MacTableKey(elanInstanceName)).build();
1109 operTx.put(getElanMacTableOperationalDataPath(elanInstanceName), elanMacTable, CREATE_MISSING_PARENTS);
1111 ElanTagNameBuilder elanTagNameBuilder = new ElanTagNameBuilder().setElanTag(elanTag)
1112 .withKey(new ElanTagNameKey(elanTag)).setName(elanInstanceName);
1113 long etreeLeafTag = -1;
1114 if (isEtreeInstance(elanInstanceAdded)) {
1115 etreeLeafTag = retrieveNewElanTag(idManager,elanInstanceName + ElanConstants
1117 EtreeLeafTagName etreeLeafTagName = new EtreeLeafTagNameBuilder()
1118 .setEtreeLeafTag(new EtreeLeafTag(etreeLeafTag)).build();
1119 elanTagNameBuilder.addAugmentation(EtreeLeafTagName.class, etreeLeafTagName);
1120 addTheLeafTagAsElanTag(elanInstanceName, etreeLeafTag, operTx);
1122 ElanTagName elanTagName = elanTagNameBuilder.build();
1124 // Add the ElanTag to ElanName in the elan-tag-name Operational
1126 operTx.put(getElanInfoEntriesOperationalDataPath(elanTag), elanTagName);
1128 // Updates the ElanInstance Config DS by setting the just acquired
1130 ElanInstanceBuilder elanInstanceBuilder = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
1131 .setDescription(elanInstanceAdded.getDescription())
1132 .setMacTimeout(elanInstanceAdded.getMacTimeout() == null
1133 ? Long.valueOf(ElanConstants.DEFAULT_MAC_TIME_OUT) : elanInstanceAdded.getMacTimeout())
1134 .withKey(elanInstanceAdded.key()).setElanTag(elanTag);
1135 if (isEtreeInstance(elanInstanceAdded)) {
1136 EtreeInstance etreeInstance = new EtreeInstanceBuilder().setEtreeLeafTagVal(new EtreeLeafTag(etreeLeafTag))
1138 elanInstanceBuilder.addAugmentation(EtreeInstance.class, etreeInstance);
1140 ElanInstance elanInstanceWithTag = elanInstanceBuilder.build();
1141 confTx.merge(ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstanceWithTag,
1142 CREATE_MISSING_PARENTS);
1143 return elanInstanceWithTag;
1146 private static void addTheLeafTagAsElanTag(String elanInstanceName, long etreeLeafTag,
1147 TypedWriteTransaction<Operational> tx) {
1148 ElanTagName etreeTagAsElanTag = new ElanTagNameBuilder().setElanTag(etreeLeafTag)
1149 .withKey(new ElanTagNameKey(etreeLeafTag)).setName(elanInstanceName).build();
1150 tx.put(getElanInfoEntriesOperationalDataPath(etreeLeafTag), etreeTagAsElanTag);
1153 private static boolean isEtreeInstance(ElanInstance elanInstanceAdded) {
1154 return elanInstanceAdded.augmentation(EtreeInstance.class) != null;
1157 public boolean isDpnPresent(BigInteger dpnId) {
1158 String dpn = String.format("%s:%s", "openflow", dpnId);
1159 NodeId nodeId = new NodeId(dpn);
1161 InstanceIdentifier<Node> node = InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(nodeId))
1163 return read(broker, LogicalDatastoreType.CONFIGURATION, node).isPresent();
1166 public static String getElanServiceName(String elanName, String interfaceName) {
1167 return "elan." + elanName + interfaceName;
1170 public static BoundServices getBoundServices(String serviceName, short servicePriority, int flowPriority,
1171 BigInteger cookie, List<Instruction> instructions) {
1172 StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookie).setFlowPriority(flowPriority)
1173 .setInstruction(instructions);
1174 return new BoundServicesBuilder().withKey(new BoundServicesKey(servicePriority)).setServiceName(serviceName)
1175 .setServicePriority(servicePriority).setServiceType(ServiceTypeFlowBased.class)
1176 .addAugmentation(StypeOpenflow.class, augBuilder.build()).build();
1179 public static InstanceIdentifier<BoundServices> buildServiceId(String interfaceName, short serviceIndex) {
1180 return InstanceIdentifier.builder(ServiceBindings.class)
1181 .child(ServicesInfo.class, new ServicesInfoKey(interfaceName, ServiceModeIngress.class))
1182 .child(BoundServices.class, new BoundServicesKey(serviceIndex)).build();
1185 public static List<MatchInfo> getTunnelMatchesForServiceId(int elanTag) {
1186 List<MatchInfo> mkMatches = new ArrayList<>();
1187 // Matching metadata
1188 mkMatches.add(new MatchTunnelId(BigInteger.valueOf(elanTag)));
1193 public void removeTerminatingServiceAction(BigInteger destDpId, int serviceId) {
1194 RemoveTerminatingServiceActionsInput input = new RemoveTerminatingServiceActionsInputBuilder()
1195 .setDpnId(destDpId).setServiceId(serviceId).build();
1196 Future<RpcResult<RemoveTerminatingServiceActionsOutput>> futureObject =
1197 itmRpcService.removeTerminatingServiceActions(input);
1199 RpcResult<RemoveTerminatingServiceActionsOutput> result = futureObject.get();
1200 if (result.isSuccessful()) {
1201 LOG.debug("Successfully completed removeTerminatingServiceActions for ELAN with serviceId {} on "
1202 + "dpn {}", serviceId, destDpId);
1204 LOG.debug("Failure in removeTerminatingServiceAction RPC call for ELAN with serviceId {} on "
1205 + "dpn {}", serviceId, destDpId);
1207 } catch (InterruptedException | ExecutionException e) {
1208 LOG.error("Error in RPC call removeTerminatingServiceActions for ELAN with serviceId {} on "
1209 + "dpn {}", serviceId, destDpId, e);
1214 * Gets the external tunnel.
1216 * @param sourceDevice
1218 * @param destinationDevice
1219 * the destination device
1220 * @param datastoreType
1221 * the datastore type
1222 * @return the external tunnel
1225 public ExternalTunnel getExternalTunnel(String sourceDevice, String destinationDevice,
1226 LogicalDatastoreType datastoreType) {
1227 Class<? extends TunnelTypeBase> tunType = TunnelTypeVxlan.class;
1228 InstanceIdentifier<ExternalTunnel> iid = InstanceIdentifier.builder(ExternalTunnelList.class)
1229 .child(ExternalTunnel.class, new ExternalTunnelKey(destinationDevice, sourceDevice, tunType)).build();
1230 return read(broker, datastoreType, iid).orNull();
1234 * Gets the external tunnel.
1236 * @param interfaceName
1237 * the interface name
1238 * @param datastoreType
1239 * the datastore type
1240 * @return the external tunnel
1243 public ExternalTunnel getExternalTunnel(String interfaceName, LogicalDatastoreType datastoreType) {
1244 ExternalTunnel externalTunnel = null;
1245 List<ExternalTunnel> externalTunnels = getAllExternalTunnels(datastoreType);
1246 for (ExternalTunnel tunnel : externalTunnels) {
1247 if (StringUtils.equalsIgnoreCase(interfaceName, tunnel.getTunnelInterfaceName())) {
1248 externalTunnel = tunnel;
1252 return externalTunnel;
1256 * Gets the all external tunnels.
1258 * @param datastoreType
1259 * the data store type
1260 * @return the all external tunnels
1262 public List<ExternalTunnel> getAllExternalTunnels(LogicalDatastoreType datastoreType) {
1263 InstanceIdentifier<ExternalTunnelList> iid = InstanceIdentifier.builder(ExternalTunnelList.class).build();
1264 return read(broker, datastoreType, iid).toJavaUtil().map(ExternalTunnelList::getExternalTunnel).orElse(
1268 public static List<MatchInfo> buildMatchesForElanTagShFlagAndDstMac(long elanTag, boolean shFlag, String macAddr) {
1269 List<MatchInfo> mkMatches = new ArrayList<>();
1271 new MatchMetadata(getElanMetadataLabel(elanTag, shFlag), MetaDataUtil.METADATA_MASK_SERVICE_SH_FLAG));
1272 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddr)));
1277 * Checks if is interface operational.
1279 * @param interfaceName
1280 * the interface name
1283 * @return true, if is interface operational
1285 public static boolean isInterfaceOperational(String interfaceName, DataBroker dataBroker) {
1286 if (StringUtils.isBlank(interfaceName)) {
1289 Interface ifState = getInterfaceStateFromOperDS(interfaceName, dataBroker);
1290 if (ifState == null) {
1293 return ifState.getOperStatus() == OperStatus.Up && ifState.getAdminStatus() == AdminStatus.Up;
1297 * Gets the interface state from operational ds.
1299 * @param interfaceName
1300 * the interface name
1303 * @return the interface state from oper ds
1306 public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1307 .ietf.interfaces.rev140508.interfaces.state.Interface getInterfaceStateFromOperDS(
1308 String interfaceName, DataBroker dataBroker) {
1309 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1310 .ietf.interfaces.rev140508.interfaces.state.Interface> ifStateId = createInterfaceStateInstanceIdentifier(
1312 return MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, ifStateId).orNull();
1316 * Creates the interface state instance identifier.
1318 * @param interfaceName
1319 * the interface name
1320 * @return the instance identifier
1322 public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1323 .ietf.interfaces.rev140508.interfaces.state.Interface> createInterfaceStateInstanceIdentifier(
1324 String interfaceName) {
1325 InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1326 .ietf.interfaces.rev140508.interfaces.state.Interface> idBuilder = InstanceIdentifier
1327 .builder(InterfacesState.class)
1328 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1329 .ietf.interfaces.rev140508.interfaces.state.Interface.class,
1330 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1331 .ietf.interfaces.rev140508.interfaces.state.InterfaceKey(
1333 return idBuilder.build();
1337 public static ListenableFuture<Void> waitForTransactionToComplete(ListenableFuture<Void> future) {
1340 } catch (InterruptedException | ExecutionException e) {
1341 // NETVIRT-1215: Do not log.error() here, only debug(); but callers *MUST* @CheckReturnValue
1342 LOG.debug("Error writing to datastore", e);
1347 public static boolean isVxlan(@Nullable ElanInstance elanInstance) {
1348 return elanInstance != null && elanInstance.getSegmentType() != null
1349 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1350 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId() != 0;
1353 private static boolean isVxlanSegment(@Nullable ElanInstance elanInstance) {
1354 if (elanInstance != null) {
1355 List<ElanSegments> elanSegments = elanInstance.getElanSegments();
1356 if (elanSegments != null) {
1357 for (ElanSegments segment : elanSegments) {
1358 if (segment != null && segment.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1359 && segment.getSegmentationId() != null
1360 && segment.getSegmentationId().longValue() != 0) {
1369 public static boolean isVxlanNetworkOrVxlanSegment(@Nullable ElanInstance elanInstance) {
1370 return isVxlan(elanInstance) || isVxlanSegment(elanInstance);
1373 public static Long getVxlanSegmentationId(ElanInstance elanInstance) {
1374 Long segmentationId = 0L;
1375 if (elanInstance == null) {
1376 return segmentationId;
1379 if (elanInstance.getSegmentType() != null
1380 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1381 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId().longValue() != 0) {
1382 segmentationId = elanInstance.getSegmentationId();
1384 for (ElanSegments segment: elanInstance.getElanSegments()) {
1385 if (segment != null && segment.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1386 && segment.getSegmentationId() != null
1387 && segment.getSegmentationId().longValue() != 0) {
1388 segmentationId = segment.getSegmentationId();
1392 return segmentationId;
1395 public static boolean isVlan(ElanInstance elanInstance) {
1396 return elanInstance != null && elanInstance.getSegmentType() != null
1397 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVlan.class)
1398 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId() != 0;
1401 public static boolean isFlat(ElanInstance elanInstance) {
1402 return elanInstance != null && elanInstance.getSegmentType() != null
1403 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeFlat.class);
1406 public void addDmacRedirectToDispatcherFlows(Long elanTag, String displayName,
1407 String macAddress, List<BigInteger> dpnIds) {
1408 for (BigInteger dpId : dpnIds) {
1409 ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
1410 tx -> mdsalManager.addFlow(tx, buildDmacRedirectToDispatcherFlow(dpId, macAddress, displayName,
1412 "Error adding DMAC redirect to dispatcher flows");
1416 public void removeDmacRedirectToDispatcherFlows(Long elanTag, String macAddress, List<BigInteger> dpnIds) {
1417 ListenableFutures.addErrorLogging(
1418 txRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.CONFIGURATION, tx -> {
1419 for (BigInteger dpId : dpnIds) {
1420 String flowId = getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, macAddress, elanTag);
1421 mdsalManager.removeFlow(tx, dpId, MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE, flowId));
1423 }), LOG, "Error removing DMAC redirect to dispatcher flows");
1426 public static FlowEntity buildDmacRedirectToDispatcherFlow(BigInteger dpId, String dstMacAddress,
1427 String displayName, long elanTag) {
1428 List<MatchInfo> matches = new ArrayList<>();
1429 matches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag), MetaDataUtil.METADATA_MASK_SERVICE));
1430 matches.add(new MatchEthernetDestination(new MacAddress(dstMacAddress)));
1431 List<InstructionInfo> instructions = new ArrayList<>();
1432 List<ActionInfo> actions = new ArrayList<>();
1433 actions.add(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
1435 instructions.add(new InstructionApplyActions(actions));
1436 String flowId = getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, dstMacAddress, elanTag);
1437 return MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 20, displayName, 0, 0,
1438 ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(BigInteger.valueOf(elanTag)),
1439 matches, instructions);
1443 public String getExternalElanInterface(String elanInstanceName, BigInteger dpnId) {
1444 DpnInterfaces dpnInterfaces = getElanInterfaceInfoByElanDpn(elanInstanceName, dpnId);
1445 if (dpnInterfaces == null || dpnInterfaces.getInterfaces() == null) {
1446 LOG.trace("Elan {} does not have interfaces in DPN {}", elanInstanceName, dpnId);
1450 for (String dpnInterface : dpnInterfaces.getInterfaces()) {
1451 if (interfaceManager.isExternalInterface(dpnInterface)) {
1452 return dpnInterface;
1456 LOG.trace("Elan {} does not have any external interace attached to DPN {}", elanInstanceName, dpnId);
1460 public static String getElanMacDPNKey(long elanTag, String macAddress, BigInteger dpnId) {
1461 String elanMacDmacDpnKey = "MAC-" + macAddress + " ELAN_TAG-" + elanTag + "DPN_ID-" + dpnId;
1462 return elanMacDmacDpnKey.intern();
1465 public static String getElanMacKey(long elanTag, String macAddress) {
1466 String elanMacKey = "MAC-" + macAddress + " ELAN_TAG-" + elanTag;
1467 return elanMacKey.intern();
1470 public static List<ListenableFuture<Void>>
1471 returnFailedListenableFutureIfTransactionCommitFailedExceptionCauseOrElseThrow(RuntimeException exception) {
1473 Throwable cause = exception.getCause();
1474 if (cause != null && cause instanceof TransactionCommitFailedException) {
1475 return Collections.singletonList(Futures.immediateFailedFuture(cause));
1481 public static List<PhysAddress> getPhysAddress(List<String> macAddress) {
1482 Preconditions.checkNotNull(macAddress, "macAddress cannot be null");
1483 List<PhysAddress> physAddresses = new ArrayList<>();
1484 for (String mac : macAddress) {
1485 physAddresses.add(new PhysAddress(mac));
1487 return physAddresses;
1490 public static List<StaticMacEntries> getStaticMacEntries(List<String> staticMacAddresses) {
1491 if (isEmpty(staticMacAddresses)) {
1492 return Collections.EMPTY_LIST;
1494 StaticMacEntriesBuilder staticMacEntriesBuilder = new StaticMacEntriesBuilder();
1495 List<StaticMacEntries> staticMacEntries = new ArrayList<>();
1496 List<PhysAddress> physAddressList = getPhysAddress(staticMacAddresses);
1497 for (PhysAddress physAddress : physAddressList) {
1498 staticMacEntries.add(staticMacEntriesBuilder.setMacAddress(physAddress).build());
1500 return staticMacEntries;
1503 public static InstanceIdentifier<StaticMacEntries> getStaticMacEntriesCfgDataPathIdentifier(String interfaceName,
1504 String macAddress) {
1505 return InstanceIdentifier.builder(ElanInterfaces.class)
1506 .child(ElanInterface.class, new ElanInterfaceKey(interfaceName)).child(StaticMacEntries.class,
1507 new StaticMacEntriesKey(new PhysAddress(macAddress))).build();
1510 public static <T> List<T> diffOf(List<T> orig, List<T> updated) {
1511 if (isEmpty(orig)) {
1512 return Collections.EMPTY_LIST;
1514 List<T> diff = Lists.newArrayList(orig);
1515 if (isNotEmpty(updated)) {
1516 diff.removeAll(updated);
1521 public static boolean isEmpty(Collection collection) {
1522 return collection == null || collection.isEmpty();
1525 public static boolean isNotEmpty(Collection collection) {
1526 return !isEmpty(collection);
1529 public Optional<IpAddress> getSourceIpAddress(Ethernet ethernet) {
1530 Optional<IpAddress> srcIpAddress = Optional.absent();
1531 if (ethernet.getPayload() == null) {
1532 return srcIpAddress;
1534 byte[] ipAddrBytes = null;
1535 if (ethernet.getPayload() instanceof IPv4) {
1536 IPv4 ipv4 = (IPv4) ethernet.getPayload();
1537 ipAddrBytes = Ints.toByteArray(ipv4.getSourceAddress());
1538 } else if (ethernet.getPayload() instanceof ARP) {
1539 ipAddrBytes = ((ARP) ethernet.getPayload()).getSenderProtocolAddress();
1541 if (ipAddrBytes != null) {
1542 String ipAddr = NWUtil.toStringIpAddress(ipAddrBytes);
1543 return Optional.of(IpAddressBuilder.getDefaultInstance(ipAddr));
1545 return srcIpAddress;
1548 public List<MacEntry> getElanMacEntries(String elanName) {
1549 MacTable macTable = getElanMacTable(elanName);
1550 if (macTable == null) {
1553 return macTable.getMacEntry();
1556 public boolean isTunnelInLogicalGroup(String interfaceName) {
1557 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1558 .ietf.interfaces.rev140508.interfaces.Interface configIface =
1559 interfaceManager.getInterfaceInfoFromConfigDataStore(interfaceName);
1560 if (configIface == null) {
1561 configIface = iitmProvider.getInterface(interfaceName);
1563 if (configIface == null) {
1566 IfTunnel ifTunnel = configIface.augmentation(IfTunnel.class);
1567 if (ifTunnel != null && ifTunnel.getTunnelInterfaceType().isAssignableFrom(TunnelTypeVxlan.class)) {
1568 ParentRefs refs = configIface.augmentation(ParentRefs.class);
1569 return refs != null && !Strings.isNullOrEmpty(refs.getParentInterface());
1574 public static InstanceIdentifier<Flow> getFlowIid(Flow flow, BigInteger dpnId) {
1575 FlowKey flowKey = new FlowKey(new FlowId(flow.getId()));
1576 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId nodeId =
1577 new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId("openflow:" + dpnId);
1578 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node nodeDpn =
1579 new NodeBuilder().setId(nodeId).withKey(new NodeKey(nodeId)).build();
1580 return InstanceIdentifier.builder(Nodes.class)
1581 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class,
1582 nodeDpn.key()).augmentation(FlowCapableNode.class)
1583 .child(Table.class, new TableKey(flow.getTableId())).child(Flow.class, flowKey).build();
1586 public static String getElanInterfaceJobKey(String interfaceName) {
1587 return "elaninterface-" + interfaceName;
1590 public void removeArpResponderFlow(BigInteger dpnId, String ingressInterfaceName, String ipAddress,
1592 LOG.info("Removing the ARP responder flow on DPN {} of Interface {} with IP {}", dpnId, ingressInterfaceName,
1594 ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.CONFIGURATION,
1595 tx -> mdsalManager.removeFlow(tx, dpnId, ArpResponderUtil.getFlowId(lportTag, ipAddress),
1596 NwConstants.ARP_RESPONDER_TABLE)), LOG, "Error removing ARP responder flow");
1600 public static String getRouterPordIdFromElanInstance(DataBroker dataBroker, String elanInstanceName) {
1601 Optional<Subnetmaps> subnetMapsData =
1602 read(dataBroker, LogicalDatastoreType.CONFIGURATION, buildSubnetMapsWildCardPath());
1603 if (subnetMapsData.isPresent()) {
1604 List<Subnetmap> subnetMapList = subnetMapsData.get().getSubnetmap();
1605 if (subnetMapList != null && !subnetMapList.isEmpty()) {
1606 for (Subnetmap subnet : subnetMapList) {
1607 if (subnet.getNetworkId().getValue().equals(elanInstanceName)) {
1608 if (subnet.getRouterInterfacePortId() != null) {
1609 return subnet.getRouterInterfacePortId().getValue();
1618 static InstanceIdentifier<Subnetmaps> buildSubnetMapsWildCardPath() {
1619 return InstanceIdentifier.create(Subnetmaps.class);
1622 public static InstanceIdentifier<Group> getGroupInstanceid(BigInteger dpnId, long groupId) {
1623 return InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId)))
1624 .augmentation(FlowCapableNode.class).child(Group.class, new GroupKey(new GroupId(groupId))).build();