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 edu.umd.cs.findbugs.annotations.CheckReturnValue;
25 import java.math.BigInteger;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.LinkedHashSet;
30 import java.util.List;
31 import java.util.Objects;
33 import java.util.concurrent.ExecutionException;
34 import java.util.concurrent.Future;
35 import java.util.concurrent.atomic.AtomicLong;
36 import java.util.function.BiFunction;
37 import java.util.function.Function;
38 import java.util.stream.Collectors;
39 import javax.inject.Inject;
40 import javax.inject.Singleton;
41 import org.apache.commons.lang3.StringUtils;
42 import org.eclipse.jdt.annotation.NonNull;
43 import org.eclipse.jdt.annotation.Nullable;
44 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
45 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
46 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
47 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
48 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
49 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
50 import org.opendaylight.genius.infra.Datastore;
51 import org.opendaylight.genius.infra.Datastore.Configuration;
52 import org.opendaylight.genius.infra.Datastore.Operational;
53 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
54 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
55 import org.opendaylight.genius.infra.TypedReadTransaction;
56 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
57 import org.opendaylight.genius.infra.TypedWriteTransaction;
58 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
59 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
60 import org.opendaylight.genius.itm.api.IITMProvider;
61 import org.opendaylight.genius.itm.globals.ITMConstants;
62 import org.opendaylight.genius.mdsalutil.ActionInfo;
63 import org.opendaylight.genius.mdsalutil.FlowEntity;
64 import org.opendaylight.genius.mdsalutil.FlowEntityBuilder;
65 import org.opendaylight.genius.mdsalutil.InstructionInfo;
66 import org.opendaylight.genius.mdsalutil.MDSALUtil;
67 import org.opendaylight.genius.mdsalutil.MatchInfo;
68 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
69 import org.opendaylight.genius.mdsalutil.NWUtil;
70 import org.opendaylight.genius.mdsalutil.NwConstants;
71 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
72 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
73 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
74 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
75 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetDestination;
76 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetSource;
77 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
78 import org.opendaylight.genius.mdsalutil.matches.MatchTunnelId;
79 import org.opendaylight.genius.mdsalutil.packet.ARP;
80 import org.opendaylight.genius.mdsalutil.packet.Ethernet;
81 import org.opendaylight.genius.mdsalutil.packet.IPv4;
82 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
83 import org.opendaylight.infrautils.utils.concurrent.NamedLocks;
84 import org.opendaylight.infrautils.utils.concurrent.NamedSimpleReentrantLock.Acquired;
85 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderUtil;
86 import org.opendaylight.netvirt.elan.cache.ElanInterfaceCache;
87 import org.opendaylight.netvirt.elan.internal.ElanGroupCache;
88 import org.opendaylight.netvirt.elanmanager.api.ElanHelper;
89 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
90 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
91 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
92 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
93 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
94 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
95 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
96 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.IfIndexesInterfaceMap;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterfaceKey;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefs;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceInput;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceInputBuilder;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceOutput;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceBindings;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeIngress;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceTypeFlowBased;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflow;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflowBuilder;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfoKey;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesBuilder;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesKey;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.ExternalTunnelList;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnel;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnelKey;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.RemoveTerminatingServiceActionsInput;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.RemoveTerminatingServiceActionsInputBuilder;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.RemoveTerminatingServiceActionsOutput;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
149 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
153 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
154 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstanceBuilder;
155 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface;
156 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface.EtreeInterfaceType;
157 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTag;
158 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTagName;
159 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTagNameBuilder;
160 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
161 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanForwardingTables;
162 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaceForwardingEntries;
163 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
164 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanState;
165 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanTagNameMap;
166 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeFlat;
167 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeVlan;
168 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeVxlan;
169 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
170 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMacKey;
171 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
172 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
173 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
174 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfacesKey;
175 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTable;
176 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTableBuilder;
177 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTableKey;
178 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
179 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
180 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.elan.instance.ElanSegments;
181 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
182 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
183 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntries;
184 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntriesBuilder;
185 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntriesKey;
186 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
187 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.ElanBuilder;
188 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.ElanKey;
189 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
190 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagNameBuilder;
191 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagNameKey;
192 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
193 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
194 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
195 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
196 import org.opendaylight.yangtools.yang.binding.DataObject;
197 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
198 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
199 import org.opendaylight.yangtools.yang.common.RpcResult;
200 import org.slf4j.Logger;
201 import org.slf4j.LoggerFactory;
204 public class ElanUtils {
205 private static final class ElanLockName {
206 private final String macAddress;
207 private final BigInteger dpnId;
208 private final long elanTag;
210 ElanLockName(long elanTag, String macAddress, BigInteger dpnId) {
211 this.elanTag = elanTag;
212 this.macAddress = macAddress;
217 public int hashCode() {
218 return 31 * Long.hashCode(elanTag) + Objects.hash(macAddress, dpnId);
222 public boolean equals(Object obj) {
226 if (!(obj instanceof ElanLockName)) {
229 final ElanLockName other = (ElanLockName) obj;
230 return elanTag == other.elanTag && Objects.equals(macAddress, other.macAddress)
231 && Objects.equals(dpnId, other.dpnId);
235 private static final Logger LOG = LoggerFactory.getLogger(ElanUtils.class);
236 private static final NamedLocks<ElanLockName> ELAN_LOCKS = new NamedLocks<>();
238 private final DataBroker broker;
239 private final ManagedNewTransactionRunner txRunner;
240 private final IMdsalApiManager mdsalManager;
241 private final OdlInterfaceRpcService interfaceManagerRpcService;
242 private final ItmRpcService itmRpcService;
243 private final IInterfaceManager interfaceManager;
244 private final ElanConfig elanConfig;
245 private final ElanItmUtils elanItmUtils;
246 private final ElanEtreeUtils elanEtreeUtils;
247 private final ElanInterfaceCache elanInterfaceCache;
248 private final IITMProvider iitmProvider;
249 private final ElanGroupCache elanGroupCache;
251 private static final Function<Bucket, Bucket> TO_BUCKET_WITHOUT_ID = (bucket) -> new BucketBuilder(bucket)
252 .setBucketId(new BucketId(0L))
255 private static final BiFunction<Bucket, AtomicLong, Bucket> TO_BUCKET_WITH_ID = (bucket, id)
256 -> new BucketBuilder(bucket)
257 .setBucketId(new BucketId(id.incrementAndGet()))
260 public static final FutureCallback<Void> DEFAULT_CALLBACK = new FutureCallback<Void>() {
262 public void onSuccess(Void result) {
263 LOG.debug("Success in Datastore operation");
267 public void onFailure(Throwable error) {
268 LOG.error("Error in Datastore operation", error);
273 public ElanUtils(DataBroker dataBroker, IMdsalApiManager mdsalManager,
274 OdlInterfaceRpcService interfaceManagerRpcService, ItmRpcService itmRpcService, ElanConfig elanConfig,
275 IInterfaceManager interfaceManager, ElanEtreeUtils elanEtreeUtils, ElanItmUtils elanItmUtils,
276 ElanInterfaceCache elanInterfaceCache, IITMProvider iitmProvider, ElanGroupCache elanGroupCache) {
277 this.broker = dataBroker;
278 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
279 this.mdsalManager = mdsalManager;
280 this.interfaceManagerRpcService = interfaceManagerRpcService;
281 this.itmRpcService = itmRpcService;
282 this.interfaceManager = interfaceManager;
283 this.elanConfig = elanConfig;
284 this.elanEtreeUtils = elanEtreeUtils;
285 this.elanItmUtils = elanItmUtils;
286 this.elanInterfaceCache = elanInterfaceCache;
287 this.iitmProvider = iitmProvider;
288 this.elanGroupCache = elanGroupCache;
291 public final Boolean isOpenstackVniSemanticsEnforced() {
292 return elanConfig.isOpenstackVniSemanticsEnforced() != null
293 ? elanConfig.isOpenstackVniSemanticsEnforced() : false;
297 * Uses the IdManager to retrieve a brand new ElanTag.
303 * @return the integer
305 public static Long retrieveNewElanTag(IdManagerService idManager, String idKey) {
307 AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(ElanConstants.ELAN_ID_POOL_NAME)
308 .setIdKey(idKey).build();
311 Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
312 RpcResult<AllocateIdOutput> rpcResult = result.get();
313 if (rpcResult.isSuccessful()) {
314 return rpcResult.getResult().getIdValue();
316 LOG.warn("RPC Call to Allocate Id returned with Errors {}", rpcResult.getErrors());
318 } catch (InterruptedException | ExecutionException e) {
319 LOG.warn("Exception when Allocating Id", e);
324 public static void releaseId(IdManagerService idManager, String poolName, String idKey) {
325 ReleaseIdInput releaseIdInput = new ReleaseIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
326 LoggingFutures.addErrorLogging(idManager.releaseId(releaseIdInput), LOG, "Release Id");
332 * @deprecated Consider using {@link #read2(LogicalDatastoreType, InstanceIdentifier)} with proper exception
334 * @param broker dataBroker
335 * @param datastoreType Logical DataStore type
336 * @param path IID to read
337 * @param <T> T extends DataObject
338 * @return the read value T
341 @SuppressWarnings("checkstyle:IllegalCatch")
342 public static <T extends DataObject> Optional<T> read(@NonNull DataBroker broker,
343 LogicalDatastoreType datastoreType, InstanceIdentifier<T> path) {
344 try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
345 return tx.read(datastoreType, path).get();
346 } catch (Exception e) {
347 throw new RuntimeException(e);
351 public <T extends DataObject> Optional<T> read2(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path)
352 throws ReadFailedException {
353 try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
354 CheckedFuture<Optional<T>, ReadFailedException> checkedFuture = tx.read(datastoreType, path);
355 return checkedFuture.checkedGet();
359 @SuppressWarnings("checkstyle:ForbidCertainMethod")
360 public static <T extends DataObject> void delete(DataBroker broker, LogicalDatastoreType datastoreType,
361 InstanceIdentifier<T> path) {
362 WriteTransaction tx = broker.newWriteOnlyTransaction();
363 tx.delete(datastoreType, path);
364 Futures.addCallback(tx.submit(), DEFAULT_CALLBACK, MoreExecutors.directExecutor());
367 public static InstanceIdentifier<ElanInterface> getElanInterfaceConfigurationDataPathId(String interfaceName) {
368 return InstanceIdentifier.builder(ElanInterfaces.class)
369 .child(ElanInterface.class, new ElanInterfaceKey(interfaceName)).build();
372 // 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();
380 public static Elan getElanByName(TypedReadTransaction<Operational> tx, String elanInstanceName)
381 throws ExecutionException, InterruptedException {
382 return tx.read(getElanInstanceOperationalDataPath(elanInstanceName)).get().orNull();
385 public static InstanceIdentifier<Elan> getElanInstanceOperationalDataPath(String elanInstanceName) {
386 return InstanceIdentifier.builder(ElanState.class).child(Elan.class, new ElanKey(elanInstanceName)).build();
389 // grouping of forwarding-entries
391 public MacEntry getInterfaceMacEntriesOperationalDataPath(String interfaceName, PhysAddress physAddress) {
392 InstanceIdentifier<MacEntry> existingMacEntryId = getInterfaceMacEntriesIdentifierOperationalDataPath(
393 interfaceName, physAddress);
394 return read(broker, LogicalDatastoreType.OPERATIONAL, existingMacEntryId).orNull();
398 public MacEntry getInterfaceMacEntriesOperationalDataPathFromId(TypedReadTransaction<Operational> tx,
399 InstanceIdentifier<MacEntry> identifier) throws ExecutionException, InterruptedException {
400 return tx.read(identifier).get().orNull();
403 public static InstanceIdentifier<MacEntry> getInterfaceMacEntriesIdentifierOperationalDataPath(String interfaceName,
404 PhysAddress physAddress) {
405 return InstanceIdentifier.builder(ElanInterfaceForwardingEntries.class)
406 .child(ElanInterfaceMac.class, new ElanInterfaceMacKey(interfaceName))
407 .child(MacEntry.class, new MacEntryKey(physAddress)).build();
411 // elan-forwarding-tables Operational container
412 public Optional<MacEntry> getMacEntryForElanInstance(String elanName, PhysAddress physAddress) {
413 InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanName, physAddress);
414 return read(broker, LogicalDatastoreType.OPERATIONAL, macId);
417 public Optional<MacEntry> getMacEntryForElanInstance(TypedReadTransaction<Operational> tx, String elanName,
418 PhysAddress physAddress) throws ExecutionException, InterruptedException {
419 InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanName, physAddress);
420 return tx.read(macId).get();
424 public MacEntry getMacEntryFromElanMacId(TypedReadTransaction<Operational> tx,
425 InstanceIdentifier<MacEntry> identifier) throws ExecutionException, InterruptedException {
426 return tx.read(identifier).get().orNull();
429 public static InstanceIdentifier<MacEntry> getMacEntryOperationalDataPath(String elanName,
430 PhysAddress physAddress) {
431 return InstanceIdentifier.builder(ElanForwardingTables.class).child(MacTable.class, new MacTableKey(elanName))
432 .child(MacEntry.class, new MacEntryKey(physAddress)).build();
435 public static InstanceIdentifier<MacTable> getElanMacTableOperationalDataPath(String elanName) {
436 return InstanceIdentifier.builder(ElanForwardingTables.class).child(MacTable.class, new MacTableKey(elanName))
440 // elan-interface-forwarding-entries Operational container
441 public ElanInterfaceMac getElanInterfaceMacByInterfaceName(String interfaceName) {
442 return getElanInterfaceMacByInterfaceName(broker, interfaceName);
446 public static ElanInterfaceMac getElanInterfaceMacByInterfaceName(DataBroker dataBroker, String interfaceName) {
447 InstanceIdentifier<ElanInterfaceMac> elanInterfaceId = getElanInterfaceMacEntriesOperationalDataPath(
449 return read(dataBroker, LogicalDatastoreType.OPERATIONAL, elanInterfaceId).orNull();
452 public static InstanceIdentifier<ElanInterfaceMac> getElanInterfaceMacEntriesOperationalDataPath(
453 String interfaceName) {
454 return InstanceIdentifier.builder(ElanInterfaceForwardingEntries.class)
455 .child(ElanInterfaceMac.class, new ElanInterfaceMacKey(interfaceName)).build();
459 * Returns the list of Interfaces that belong to an Elan on an specific DPN.
460 * Data retrieved from Elan's operational DS: elan-dpn-interfaces container.
462 * @param elanInstanceName
463 * name of the Elan to which the interfaces must belong to
465 * Id of the DPN where the interfaces are located
466 * @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
495 public ElanTagName getElanInfoByElanTag(long elanTag) {
496 InstanceIdentifier<ElanTagName> elanId = getElanInfoEntriesOperationalDataPath(elanTag);
497 Optional<ElanTagName> existingElanInfo = read(broker,
498 LogicalDatastoreType.OPERATIONAL, elanId);
499 return existingElanInfo.orNull();
502 public static InstanceIdentifier<ElanTagName> getElanInfoEntriesOperationalDataPath(long elanTag) {
503 return InstanceIdentifier.builder(ElanTagNameMap.class).child(ElanTagName.class, new ElanTagNameKey(elanTag))
507 // interface-index-tag operational container
508 public Optional<IfIndexInterface> getInterfaceInfoByInterfaceTag(long interfaceTag) {
509 InstanceIdentifier<IfIndexInterface> interfaceId = getInterfaceInfoEntriesOperationalDataPath(interfaceTag);
510 return read(broker, LogicalDatastoreType.OPERATIONAL, interfaceId);
513 public static InstanceIdentifier<IfIndexInterface> getInterfaceInfoEntriesOperationalDataPath(long interfaceTag) {
514 return InstanceIdentifier.builder(IfIndexesInterfaceMap.class)
515 .child(IfIndexInterface.class, new IfIndexInterfaceKey((int) interfaceTag)).build();
518 public static InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
519 return InstanceIdentifier.builder(ElanDpnInterfaces.class)
520 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
524 public ElanDpnInterfacesList getElanDpnInterfacesList(String elanName) {
525 InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanName);
526 return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId).orNull();
530 public ElanDpnInterfaces getElanDpnInterfacesList() {
531 InstanceIdentifier<ElanDpnInterfaces> elanDpnInterfaceId = InstanceIdentifier.builder(ElanDpnInterfaces.class)
533 return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId).orNull();
537 * This method is useful get all ELAN participating CSS dpIds to install
538 * program remote dmac entries and updating remote bc groups for tor
541 * @param elanInstanceName
542 * the elan instance name
543 * @return list of dpIds
546 public List<BigInteger> getParticipatingDpnsInElanInstance(String elanInstanceName) {
547 List<BigInteger> dpIds = new ArrayList<>();
548 InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanInstanceName);
549 Optional<ElanDpnInterfacesList> existingElanDpnInterfaces = read(broker,
550 LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId);
551 if (!existingElanDpnInterfaces.isPresent()) {
554 List<DpnInterfaces> dpnInterfaces = existingElanDpnInterfaces.get().nonnullDpnInterfaces();
555 for (DpnInterfaces dpnInterface : dpnInterfaces) {
556 dpIds.add(dpnInterface.getDpId());
561 public static long getElanRemoteBroadCastGroupID(long elanTag) {
562 return ElanConstants.ELAN_GID_MIN + elanTag % ElanConstants.ELAN_GID_MIN * 2;
566 * Gets the elan mac table.
570 * @return the elan mac table
573 public MacTable getElanMacTable(String elanName) {
574 return getElanMacTable(broker, elanName);
578 public static MacTable getElanMacTable(DataBroker dataBroker, String elanName) {
579 InstanceIdentifier<MacTable> elanMacTableId = getElanMacTableOperationalDataPath(elanName);
580 return read(dataBroker, LogicalDatastoreType.OPERATIONAL, elanMacTableId).orNull();
583 public static long getElanLocalBCGId(long elanTag) {
584 return ElanConstants.ELAN_GID_MIN + (elanTag % ElanConstants.ELAN_GID_MIN * 2 - 1);
587 public static long getElanRemoteBCGId(long elanTag) {
588 return ElanConstants.ELAN_GID_MIN + elanTag % ElanConstants.ELAN_GID_MIN * 2;
591 public static long getEtreeLeafLocalBCGId(long etreeLeafTag) {
592 return ElanConstants.ELAN_GID_MIN + (etreeLeafTag % ElanConstants.ELAN_GID_MIN * 2 - 1);
595 public static long getEtreeLeafRemoteBCGId(long etreeLeafTag) {
596 return ElanConstants.ELAN_GID_MIN + etreeLeafTag % ElanConstants.ELAN_GID_MIN * 2;
599 public static BigInteger getElanMetadataLabel(long elanTag, boolean isSHFlagSet) {
600 int shBit = isSHFlagSet ? 1 : 0;
601 return BigInteger.valueOf(elanTag).shiftLeft(24).or(BigInteger.valueOf(shBit));
605 * Setting SMAC, DMAC, UDMAC in this DPN and optionally in other DPNs.
609 * @param interfaceInfo
615 * @param configureRemoteFlows
616 * true if remote dmac flows should be configured as well
617 * @param writeFlowGroupTx
620 public void setupMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
621 long macTimeout, String macAddress, boolean configureRemoteFlows,
622 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
623 try (Acquired lock = lockElanMacDPN(elanInfo.getElanTag(), macAddress, interfaceInfo.getDpId())) {
624 setupKnownSmacFlow(elanInfo, interfaceInfo, macTimeout, macAddress, mdsalManager,
626 setupOrigDmacFlows(elanInfo, interfaceInfo, macAddress, configureRemoteFlows, mdsalManager,
631 public void setupDMacFlowOnRemoteDpn(ElanInstance elanInfo, InterfaceInfo interfaceInfo, BigInteger dstDpId,
632 String macAddress, TypedWriteTransaction<Configuration> writeFlowTx) {
633 String elanInstanceName = elanInfo.getElanInstanceName();
634 setupRemoteDmacFlow(dstDpId, interfaceInfo.getDpId(), interfaceInfo.getInterfaceTag(), elanInfo.getElanTag(),
635 macAddress, elanInstanceName, writeFlowTx, interfaceInfo.getInterfaceName(), elanInfo);
636 LOG.info("Remote Dmac flow entry created for elan Name:{}, logical port Name:{} and"
637 + " mac address {} on dpn:{}", elanInstanceName, interfaceInfo.getPortName(),
638 macAddress, dstDpId);
642 * Inserts a Flow in SMAC table to state that the MAC has already been
645 private void setupKnownSmacFlow(ElanInstance elanInfo, InterfaceInfo interfaceInfo, long macTimeout,
646 String macAddress, IMdsalApiManager mdsalApiManager,
647 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
648 FlowEntity flowEntity = buildKnownSmacFlow(elanInfo, interfaceInfo, macTimeout, macAddress);
649 mdsalApiManager.addFlow(writeFlowGroupTx, flowEntity);
650 LOG.debug("Known Smac flow entry created for elan Name:{}, logical Interface port:{} and mac address:{}",
651 elanInfo.getElanInstanceName(), elanInfo.getDescription(), macAddress);
654 public FlowEntity buildKnownSmacFlow(ElanInstance elanInfo, InterfaceInfo interfaceInfo, long macTimeout,
656 int lportTag = interfaceInfo.getInterfaceTag();
657 // Matching metadata and eth_src fields
658 List<MatchInfo> mkMatches = new ArrayList<>();
659 mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanInfo.getElanTag(), lportTag),
660 ElanHelper.getElanMetadataMask()));
661 mkMatches.add(new MatchEthernetSource(new MacAddress(macAddress)));
662 List<InstructionInfo> mkInstructions = new ArrayList<>();
663 mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
664 BigInteger dpId = interfaceInfo.getDpId();
665 long elanTag = getElanTag(elanInfo, interfaceInfo);
666 return new FlowEntityBuilder()
668 .setTableId(NwConstants.ELAN_SMAC_TABLE)
669 .setFlowId(getKnownDynamicmacFlowRef(NwConstants.ELAN_SMAC_TABLE, dpId, lportTag, macAddress, elanTag))
671 .setFlowName(elanInfo.getDescription())
672 .setIdleTimeOut((int) macTimeout)
674 .setCookie(ElanConstants.COOKIE_ELAN_KNOWN_SMAC.add(BigInteger.valueOf(elanTag)))
675 .setMatchInfoList(mkMatches)
676 .setInstructionInfoList(mkInstructions)
678 // If Mac timeout is 0, the flow won't be deleted automatically, so no need to get notified
679 .setSendFlowRemFlag(macTimeout != 0)
683 private Long getElanTag(ElanInstance elanInfo, InterfaceInfo interfaceInfo) {
684 EtreeInterface etreeInterface = elanInterfaceCache.getEtreeInterface(interfaceInfo.getInterfaceName()).orNull();
685 if (etreeInterface == null || etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
686 return elanInfo.getElanTag();
688 EtreeInstance etreeInstance = elanInfo.augmentation(EtreeInstance.class);
689 if (etreeInstance == null) {
690 LOG.warn("EtreeInterface {} is connected to a non-Etree network: {}",
691 interfaceInfo.getInterfaceName(), elanInfo.getElanInstanceName());
692 return elanInfo.getElanTag();
694 return etreeInstance.getEtreeLeafTagVal().getValue();
700 * Installs a Flow in INTERNAL_TUNNEL_TABLE of the affected DPN that sends
701 * the packet through the specified interface if the tunnel_id matches the
702 * interface's lportTag.
704 * @param interfaceInfo
706 * @param mdsalApiManager
707 * the mdsal API manager
708 * @param writeFlowGroupTx
709 * the writeFLowGroup tx
711 public void setupTermDmacFlows(InterfaceInfo interfaceInfo, IMdsalApiManager mdsalApiManager,
712 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
713 BigInteger dpId = interfaceInfo.getDpId();
714 int lportTag = interfaceInfo.getInterfaceTag();
715 Flow flow = MDSALUtil.buildFlowNew(NwConstants.INTERNAL_TUNNEL_TABLE,
716 getIntTunnelTableFlowRef(NwConstants.INTERNAL_TUNNEL_TABLE, lportTag), 5,
717 String.format("%s:%d", "ITM Flow Entry ", lportTag), 0, 0,
718 ITMConstants.COOKIE_ITM.add(BigInteger.valueOf(lportTag)),
719 getTunnelIdMatchForFilterEqualsLPortTag(lportTag),
720 getInstructionsInPortForOutGroup(interfaceInfo.getInterfaceName()));
721 mdsalApiManager.addFlow(writeFlowGroupTx, dpId, flow);
722 LOG.debug("Terminating service table flow entry created on dpn:{} for logical Interface port:{}", dpId,
723 interfaceInfo.getPortName());
727 * Constructs the FlowName for flows installed in the Internal Tunnel Table,
728 * consisting in tableId + elanTag.
734 * @return the Internal tunnel
736 public static String getIntTunnelTableFlowRef(short tableId, int elanTag) {
737 return new StringBuilder().append(tableId).append(elanTag).toString();
741 * Constructs the Matches that checks that the tunnel_id field contains a
745 * lportTag that must be checked against the tunnel_id field
746 * @return the list of match Info
748 public static List<MatchInfo> getTunnelIdMatchForFilterEqualsLPortTag(int lportTag) {
749 List<MatchInfo> mkMatches = new ArrayList<>();
751 mkMatches.add(new MatchTunnelId(BigInteger.valueOf(lportTag)));
756 * Constructs the Instructions that take the packet over a given interface.
759 * Name of the interface where the packet must be sent over. It
760 * can be a local interface or a tunnel interface (internal or
762 * @return the Instruction
764 public List<Instruction> getInstructionsInPortForOutGroup(String ifName) {
765 List<Instruction> mkInstructions = new ArrayList<>();
766 List<Action> actions = getEgressActionsForInterface(ifName, /* tunnelKey */ null);
768 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
769 return mkInstructions;
773 * Returns the list of Actions to be taken when sending the packet through
774 * an Elan interface. Note that this interface can refer to an ElanInterface
775 * where the Elan VM is attached to a DPN or an ITM tunnel interface where
776 * Elan traffic can be sent through. In this latter case, the tunnelKey is
777 * mandatory and it can hold serviceId for internal tunnels or the VNI for
784 * @return the egress actions for interface
786 @SuppressWarnings("checkstyle:IllegalCatch")
788 public List<Action> getEgressActionsForInterface(String ifName, @Nullable Long tunnelKey) {
789 List<Action> listAction = new ArrayList<>();
791 GetEgressActionsForInterfaceInput getEgressActionInput = new GetEgressActionsForInterfaceInputBuilder()
792 .setIntfName(ifName).setTunnelKey(tunnelKey).build();
793 Future<RpcResult<GetEgressActionsForInterfaceOutput>> result = interfaceManagerRpcService
794 .getEgressActionsForInterface(getEgressActionInput);
795 RpcResult<GetEgressActionsForInterfaceOutput> rpcResult = result.get();
796 if (!rpcResult.isSuccessful()) {
797 LOG.debug("RPC Call to Get egress actions for interface {} returned with Errors {}", ifName,
798 rpcResult.getErrors());
800 listAction = rpcResult.getResult().nonnullAction();
802 } catch (Exception e) {
803 LOG.warn("Exception when egress actions for interface {}", ifName, e);
808 private void setupOrigDmacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
809 boolean configureRemoteFlows, IMdsalApiManager mdsalApiManager,
810 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
811 BigInteger dpId = interfaceInfo.getDpId();
812 String ifName = interfaceInfo.getInterfaceName();
813 long ifTag = interfaceInfo.getInterfaceTag();
814 String elanInstanceName = elanInfo.getElanInstanceName();
816 Long elanTag = elanInfo.getElanTag();
818 setupLocalDmacFlow(elanTag, dpId, ifName, macAddress, elanInfo, mdsalApiManager, ifTag,
820 LOG.debug("Dmac flow entry created for elan Name:{}, logical port Name:{} mand mac address:{} "
821 + "on dpn:{}", elanInstanceName, interfaceInfo.getPortName(), macAddress, dpId);
823 if (!configureRemoteFlows) {
827 List<DpnInterfaces> elanDpns = getInvolvedDpnsInElan(elanInstanceName);
828 for (DpnInterfaces elanDpn : elanDpns) {
830 if (Objects.equals(elanDpn.getDpId(), dpId)) {
834 // Check for the Remote DPN present in Inventory Manager
835 if (!isDpnPresent(elanDpn.getDpId())) {
839 // For remote DPNs a flow is needed to indicate that packets of this ELAN going to this MAC need to be
840 // forwarded through the appropriate ITM tunnel
841 setupRemoteDmacFlow(elanDpn.getDpId(), // srcDpn (the remote DPN in this case)
842 dpId, // dstDpn (the local DPN)
843 interfaceInfo.getInterfaceTag(), // lportTag of the local interface
844 elanTag, // identifier of the Elan
845 macAddress, // MAC to be programmed in remote DPN
846 elanInstanceName, writeFlowGroupTx, ifName, elanInfo
848 LOG.debug("Remote Dmac flow entry created for elan Name:{}, logical port Name:{} and mac address:{} on"
849 + " dpn:{}", elanInstanceName, interfaceInfo.getPortName(), macAddress, elanDpn.getDpId());
852 // TODO: Make sure that the same is performed against the ElanDevices.
856 public List<DpnInterfaces> getInvolvedDpnsInElan(String elanName) {
857 return getElanDPNByName(elanName);
861 public List<DpnInterfaces> getElanDPNByName(String elanInstanceName) {
862 InstanceIdentifier<ElanDpnInterfacesList> elanIdentifier = getElanDpnOperationDataPath(elanInstanceName);
863 return MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, elanIdentifier).toJavaUtil().map(
864 ElanDpnInterfacesList::getDpnInterfaces).orElse(emptyList());
867 private void setupLocalDmacFlow(long elanTag, BigInteger dpId, String ifName, String macAddress,
868 ElanInstance elanInfo, IMdsalApiManager mdsalApiManager, long ifTag,
869 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
870 Flow flowEntity = buildLocalDmacFlowEntry(elanTag, dpId, ifName, macAddress, elanInfo, ifTag);
871 mdsalApiManager.addFlow(writeFlowGroupTx, dpId, flowEntity);
872 installEtreeLocalDmacFlow(elanTag, dpId, ifName, macAddress, elanInfo, ifTag, writeFlowGroupTx);
875 private void installEtreeLocalDmacFlow(long elanTag, BigInteger dpId, String ifName, String macAddress,
876 ElanInstance elanInfo, long ifTag, TypedWriteTransaction<Configuration> writeFlowGroupTx) {
877 EtreeInterface etreeInterface = elanInterfaceCache.getEtreeInterface(ifName).orNull();
878 if (etreeInterface != null && etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
879 EtreeLeafTagName etreeTagName = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag);
880 if (etreeTagName == null) {
881 LOG.warn("Interface {} seems like it belongs to Etree but etreeTagName from elanTag {} is null",
884 Flow flowEntity = buildLocalDmacFlowEntry(etreeTagName.getEtreeLeafTag().getValue(), dpId, ifName,
885 macAddress, elanInfo, ifTag);
886 mdsalManager.addFlow(writeFlowGroupTx, dpId, flowEntity);
891 public static String getKnownDynamicmacFlowRef(short tableId, BigInteger dpId, long lporTag, String macAddress,
893 return String.valueOf(tableId) + elanTag + dpId + lporTag + macAddress;
896 public static String getKnownDynamicmacFlowRef(short tableId, BigInteger dpId, BigInteger remoteDpId,
897 String macAddress, long elanTag) {
898 return String.valueOf(tableId) + elanTag + dpId + remoteDpId + macAddress;
901 public static String getKnownDynamicmacFlowRef(short tableId, BigInteger dpId, String macAddress, long elanTag) {
902 return String.valueOf(tableId) + elanTag + dpId + macAddress;
905 public static String getKnownDynamicmacFlowRef(short elanDmacTable, BigInteger dpId, String extDeviceNodeId,
906 String dstMacAddress, long elanTag, boolean shFlag) {
907 return String.valueOf(elanDmacTable) + elanTag + dpId + extDeviceNodeId + dstMacAddress + shFlag;
911 * Builds the flow to be programmed in the DMAC table of the local DPN (that
912 * is, where the MAC is attached to). This flow consists in:
914 * <p>Match: + elanTag in metadata + packet goes to a MAC locally attached
915 * Actions: + optionally, pop-vlan + set-vlan-id + output to ifName's
932 public Flow buildLocalDmacFlowEntry(long elanTag, BigInteger dpId, String ifName, String macAddress,
933 ElanInstance elanInfo, long ifTag) {
935 List<MatchInfo> mkMatches = new ArrayList<>();
936 mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag), MetaDataUtil.METADATA_MASK_SERVICE));
937 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddress)));
939 List<Instruction> mkInstructions = new ArrayList<>();
940 List<Action> actions = getEgressActionsForInterface(ifName, /* tunnelKey */ null);
941 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
942 Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE,
943 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, ifTag, macAddress, elanTag), 20,
944 elanInfo.getElanInstanceName(), 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(
945 BigInteger.valueOf(elanTag)), mkMatches, mkInstructions);
950 public void setupRemoteDmacFlow(BigInteger srcDpId, BigInteger destDpId, int lportTag, long elanTag,
951 String macAddress, String displayName, TypedWriteTransaction<Configuration> writeFlowGroupTx,
952 String interfaceName, ElanInstance elanInstance) {
953 if (interfaceManager.isExternalInterface(interfaceName)) {
954 LOG.debug("Ignoring install remote DMAC {} flow on provider interface {} elan {}",
955 macAddress, interfaceName, elanInstance.getElanInstanceName());
959 // if openstack-vni-semantics are enforced, segmentation ID is passed as network VNI for VxLAN based provider
960 // networks, 0 otherwise
961 long lportTagOrVni = !isOpenstackVniSemanticsEnforced() ? lportTag : isVxlanNetworkOrVxlanSegment(elanInstance)
962 ? getVxlanSegmentationId(elanInstance) : 0;
963 flowEntity = buildRemoteDmacFlowEntry(srcDpId, destDpId, lportTagOrVni, elanTag, macAddress, displayName,
965 mdsalManager.addFlow(writeFlowGroupTx, srcDpId, flowEntity);
966 setupEtreeRemoteDmacFlow(srcDpId, destDpId, lportTagOrVni, elanTag, macAddress, displayName, interfaceName,
967 writeFlowGroupTx, elanInstance);
970 private void setupEtreeRemoteDmacFlow(BigInteger srcDpId, BigInteger destDpId, long lportTagOrVni, long elanTag,
971 String macAddress, String displayName, String interfaceName,
972 TypedWriteTransaction<Configuration> writeFlowGroupTx,
973 ElanInstance elanInstance) {
975 EtreeInterface etreeInterface = elanInterfaceCache.getEtreeInterface(interfaceName).orNull();
976 if (etreeInterface != null && etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
977 EtreeLeafTagName etreeTagName = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag);
978 if (etreeTagName == null) {
979 LOG.warn("Interface {} seems like it belongs to Etree but etreeTagName from elanTag {} is null.",
980 interfaceName, elanTag);
982 flowEntity = buildRemoteDmacFlowEntry(srcDpId, destDpId, lportTagOrVni,
983 etreeTagName.getEtreeLeafTag().getValue(), macAddress, displayName, elanInstance);
984 mdsalManager.addFlow(writeFlowGroupTx, srcDpId, flowEntity);
990 * Builds a Flow to be programmed in a remote DPN's DMAC table. This flow
991 * consists in: Match: + elanTag in packet's metadata + packet going to a
992 * MAC known to be located in another DPN Actions: + set_tunnel_id
993 * + output ITM internal tunnel interface with the other DPN
999 * @param lportTagOrVni
1000 * lportTag or network VNI
1005 * @param displayName
1007 * @param elanInstance
1009 * @return the flow remote Dmac
1011 @SuppressWarnings("checkstyle:IllegalCatch")
1012 public Flow buildRemoteDmacFlowEntry(BigInteger srcDpId, BigInteger destDpId, long lportTagOrVni, long elanTag,
1013 String macAddress, String displayName, ElanInstance elanInstance) {
1014 List<MatchInfo> mkMatches = new ArrayList<>();
1015 mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag), MetaDataUtil.METADATA_MASK_SERVICE));
1016 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddress)));
1018 List<Instruction> mkInstructions = new ArrayList<>();
1020 // List of Action for the provided Source and Destination DPIDs
1022 List<Action> actions = null;
1023 if (isVxlanNetworkOrVxlanSegment(elanInstance)) {
1024 actions = elanItmUtils.getInternalTunnelItmEgressAction(srcDpId, destDpId, lportTagOrVni);
1025 } else if (isVlan(elanInstance) || isFlat(elanInstance)) {
1026 String interfaceName = getExternalElanInterface(elanInstance.getElanInstanceName(), srcDpId);
1027 if (null == interfaceName) {
1028 LOG.info("buildRemoteDmacFlowEntry: Could not find interfaceName for {} {}", srcDpId,
1031 actions = getEgressActionsForInterface(interfaceName, null);
1033 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
1034 } catch (Exception e) {
1035 LOG.error("Could not get egress actions to add to flow for srcDpId {}, destDpId {}, lportTag/VNI {}",
1036 srcDpId, destDpId, lportTagOrVni, e);
1039 Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE,
1040 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, srcDpId, destDpId, macAddress, elanTag),
1042 displayName, 0, /* idleTimeout */
1043 0, /* hardTimeout */
1044 ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(BigInteger.valueOf(elanTag)), mkMatches, mkInstructions);
1050 public void deleteMacFlows(@Nullable ElanInstance elanInfo, @Nullable InterfaceInfo interfaceInfo,
1051 MacEntry macEntry, TypedReadWriteTransaction<Configuration> flowTx)
1052 throws ExecutionException, InterruptedException {
1053 if (elanInfo == null || interfaceInfo == null) {
1056 String macAddress = macEntry.getMacAddress().getValue();
1057 try (Acquired lock = lockElanMacDPN(elanInfo.getElanTag(), macAddress, interfaceInfo.getDpId())) {
1058 deleteMacFlows(elanInfo, interfaceInfo, macAddress, /* alsoDeleteSMAC */ true, flowTx);
1062 public void deleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1063 boolean deleteSmac, TypedReadWriteTransaction<Configuration> flowTx)
1064 throws ExecutionException, InterruptedException {
1065 String elanInstanceName = elanInfo.getElanInstanceName();
1066 List<DpnInterfaces> remoteFEs = getInvolvedDpnsInElan(elanInstanceName);
1067 BigInteger srcdpId = interfaceInfo.getDpId();
1068 boolean isFlowsRemovedInSrcDpn = false;
1069 for (DpnInterfaces dpnInterface : remoteFEs) {
1070 Long elanTag = elanInfo.getElanTag();
1071 BigInteger dstDpId = dpnInterface.getDpId();
1072 if (executeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1073 elanTag, dstDpId, flowTx)) {
1074 isFlowsRemovedInSrcDpn = true;
1076 executeEtreeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1077 elanTag, dstDpId, flowTx);
1079 if (!isFlowsRemovedInSrcDpn) {
1080 deleteSmacAndDmacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, flowTx);
1084 private void executeEtreeDeleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1085 boolean deleteSmac, String elanInstanceName, BigInteger srcdpId, Long elanTag, BigInteger dstDpId,
1086 TypedReadWriteTransaction<Configuration> flowTx) throws ExecutionException, InterruptedException {
1087 EtreeLeafTagName etreeLeafTag = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag);
1088 if (etreeLeafTag != null) {
1089 executeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1090 etreeLeafTag.getEtreeLeafTag().getValue(), dstDpId, flowTx);
1094 private boolean executeDeleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1095 boolean deleteSmac, String elanInstanceName, BigInteger srcdpId, Long elanTag, BigInteger dstDpId,
1096 TypedReadWriteTransaction<Configuration> flowTx) throws ExecutionException, InterruptedException {
1097 boolean isFlowsRemovedInSrcDpn = false;
1098 if (dstDpId.equals(srcdpId)) {
1099 isFlowsRemovedInSrcDpn = true;
1100 deleteSmacAndDmacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, flowTx);
1101 } else if (isDpnPresent(dstDpId)) {
1103 .removeFlow(flowTx, dstDpId,
1104 MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE, getKnownDynamicmacFlowRef(
1105 NwConstants.ELAN_DMAC_TABLE, dstDpId, srcdpId, macAddress, elanTag)));
1106 LOG.debug("Dmac flow entry deleted for elan:{}, logical interface port:{} and mac address:{} on dpn:{}",
1107 elanInstanceName, interfaceInfo.getPortName(), macAddress, dstDpId);
1109 return isFlowsRemovedInSrcDpn;
1112 private void deleteSmacAndDmacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1113 boolean deleteSmac, TypedReadWriteTransaction<Configuration> flowTx)
1114 throws ExecutionException, InterruptedException {
1115 String elanInstanceName = elanInfo.getElanInstanceName();
1116 long ifTag = interfaceInfo.getInterfaceTag();
1117 BigInteger srcdpId = interfaceInfo.getDpId();
1118 Long elanTag = elanInfo.getElanTag();
1121 .removeFlow(flowTx, srcdpId,
1122 MDSALUtil.buildFlow(NwConstants.ELAN_SMAC_TABLE, getKnownDynamicmacFlowRef(
1123 NwConstants.ELAN_SMAC_TABLE, srcdpId, ifTag, macAddress, elanTag)));
1125 mdsalManager.removeFlow(flowTx, srcdpId,
1126 MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE,
1127 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, srcdpId, ifTag, macAddress, elanTag)));
1128 LOG.debug("All the required flows deleted for elan:{}, logical Interface port:{} and MAC address:{} on dpn:{}",
1129 elanInstanceName, interfaceInfo.getPortName(), macAddress, srcdpId);
1133 * Updates the Elan information in the Operational DS. It also updates the
1134 * ElanInstance in the Config DS by setting the adquired elanTag.
1138 * @param elanInstanceAdded
1139 * the elan instance added
1140 * @param elanInterfaces
1141 * the elan interfaces
1145 * @return the updated ELAN instance.
1147 public static ElanInstance updateOperationalDataStore(IdManagerService idManager,
1148 ElanInstance elanInstanceAdded, List<String> elanInterfaces, TypedWriteTransaction<Configuration> confTx,
1149 TypedWriteTransaction<Operational> operTx) {
1150 String elanInstanceName = elanInstanceAdded.getElanInstanceName();
1151 Long elanTag = elanInstanceAdded.getElanTag();
1152 if (elanTag == null || elanTag == 0L) {
1153 elanTag = retrieveNewElanTag(idManager, elanInstanceName);
1155 Elan elanInfo = new ElanBuilder().setName(elanInstanceName).setElanInterfaces(elanInterfaces)
1156 .withKey(new ElanKey(elanInstanceName)).build();
1158 // Add the ElanState in the elan-state operational data-store
1159 operTx.put(getElanInstanceOperationalDataPath(elanInstanceName), elanInfo, CREATE_MISSING_PARENTS);
1161 // Add the ElanMacTable in the elan-mac-table operational data-store
1162 MacTable elanMacTable = new MacTableBuilder().withKey(new MacTableKey(elanInstanceName)).build();
1163 operTx.put(getElanMacTableOperationalDataPath(elanInstanceName), elanMacTable, CREATE_MISSING_PARENTS);
1165 ElanTagNameBuilder elanTagNameBuilder = new ElanTagNameBuilder().setElanTag(elanTag)
1166 .withKey(new ElanTagNameKey(elanTag)).setName(elanInstanceName);
1167 long etreeLeafTag = -1;
1168 if (isEtreeInstance(elanInstanceAdded)) {
1169 etreeLeafTag = retrieveNewElanTag(idManager,elanInstanceName + ElanConstants
1171 EtreeLeafTagName etreeLeafTagName = new EtreeLeafTagNameBuilder()
1172 .setEtreeLeafTag(new EtreeLeafTag(etreeLeafTag)).build();
1173 elanTagNameBuilder.addAugmentation(EtreeLeafTagName.class, etreeLeafTagName);
1174 addTheLeafTagAsElanTag(elanInstanceName, etreeLeafTag, operTx);
1176 ElanTagName elanTagName = elanTagNameBuilder.build();
1178 // Add the ElanTag to ElanName in the elan-tag-name Operational
1180 operTx.put(getElanInfoEntriesOperationalDataPath(elanTag), elanTagName);
1182 // Updates the ElanInstance Config DS by setting the just acquired
1184 ElanInstanceBuilder elanInstanceBuilder = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
1185 .setDescription(elanInstanceAdded.getDescription())
1186 .setMacTimeout(elanInstanceAdded.getMacTimeout() == null
1187 ? Long.valueOf(ElanConstants.DEFAULT_MAC_TIME_OUT) : elanInstanceAdded.getMacTimeout())
1188 .withKey(elanInstanceAdded.key()).setElanTag(elanTag);
1189 if (isEtreeInstance(elanInstanceAdded)) {
1190 EtreeInstance etreeInstance = new EtreeInstanceBuilder().setEtreeLeafTagVal(new EtreeLeafTag(etreeLeafTag))
1192 elanInstanceBuilder.addAugmentation(EtreeInstance.class, etreeInstance);
1194 ElanInstance elanInstanceWithTag = elanInstanceBuilder.build();
1195 LOG.trace("Updated elan Operational DS for elan: {} with elanTag: {} and interfaces: {}", elanInstanceName,
1196 elanTag, elanInterfaces);
1197 confTx.merge(ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstanceWithTag,
1198 CREATE_MISSING_PARENTS);
1199 return elanInstanceWithTag;
1202 private static void addTheLeafTagAsElanTag(String elanInstanceName, long etreeLeafTag,
1203 TypedWriteTransaction<Operational> tx) {
1204 ElanTagName etreeTagAsElanTag = new ElanTagNameBuilder().setElanTag(etreeLeafTag)
1205 .withKey(new ElanTagNameKey(etreeLeafTag)).setName(elanInstanceName).build();
1206 tx.put(getElanInfoEntriesOperationalDataPath(etreeLeafTag), etreeTagAsElanTag);
1209 private static boolean isEtreeInstance(ElanInstance elanInstanceAdded) {
1210 return elanInstanceAdded.augmentation(EtreeInstance.class) != null;
1213 public boolean isDpnPresent(BigInteger dpnId) {
1214 String dpn = String.format("%s:%s", "openflow", dpnId);
1215 NodeId nodeId = new NodeId(dpn);
1217 InstanceIdentifier<Node> node = InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(nodeId))
1219 return read(broker, LogicalDatastoreType.CONFIGURATION, node).isPresent();
1222 public static String getElanServiceName(String elanName, String interfaceName) {
1223 return "elan." + elanName + interfaceName;
1226 public static BoundServices getBoundServices(String serviceName, short servicePriority, int flowPriority,
1227 BigInteger cookie, List<Instruction> instructions) {
1228 StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookie).setFlowPriority(flowPriority)
1229 .setInstruction(instructions);
1230 return new BoundServicesBuilder().withKey(new BoundServicesKey(servicePriority)).setServiceName(serviceName)
1231 .setServicePriority(servicePriority).setServiceType(ServiceTypeFlowBased.class)
1232 .addAugmentation(StypeOpenflow.class, augBuilder.build()).build();
1235 public static InstanceIdentifier<BoundServices> buildServiceId(String interfaceName, short serviceIndex) {
1236 return InstanceIdentifier.builder(ServiceBindings.class)
1237 .child(ServicesInfo.class, new ServicesInfoKey(interfaceName, ServiceModeIngress.class))
1238 .child(BoundServices.class, new BoundServicesKey(serviceIndex)).build();
1241 public static List<MatchInfo> getTunnelMatchesForServiceId(int elanTag) {
1242 List<MatchInfo> mkMatches = new ArrayList<>();
1243 // Matching metadata
1244 mkMatches.add(new MatchTunnelId(BigInteger.valueOf(elanTag)));
1249 public void removeTerminatingServiceAction(BigInteger destDpId, int serviceId) {
1250 RemoveTerminatingServiceActionsInput input = new RemoveTerminatingServiceActionsInputBuilder()
1251 .setDpnId(destDpId).setServiceId(serviceId).build();
1252 Future<RpcResult<RemoveTerminatingServiceActionsOutput>> futureObject =
1253 itmRpcService.removeTerminatingServiceActions(input);
1255 RpcResult<RemoveTerminatingServiceActionsOutput> result = futureObject.get();
1256 if (result.isSuccessful()) {
1257 LOG.debug("Successfully completed removeTerminatingServiceActions for ELAN with serviceId {} on "
1258 + "dpn {}", serviceId, destDpId);
1260 LOG.debug("Failure in removeTerminatingServiceAction RPC call for ELAN with serviceId {} on "
1261 + "dpn {}", serviceId, destDpId);
1263 } catch (InterruptedException | ExecutionException e) {
1264 LOG.error("Error in RPC call removeTerminatingServiceActions for ELAN with serviceId {} on "
1265 + "dpn {}", serviceId, destDpId, e);
1270 * Gets the external tunnel.
1272 * @param sourceDevice
1274 * @param destinationDevice
1275 * the destination device
1276 * @param datastoreType
1277 * the datastore type
1278 * @return the external tunnel
1281 public ExternalTunnel getExternalTunnel(String sourceDevice, String destinationDevice,
1282 LogicalDatastoreType datastoreType) {
1283 Class<? extends TunnelTypeBase> tunType = TunnelTypeVxlan.class;
1284 InstanceIdentifier<ExternalTunnel> iid = InstanceIdentifier.builder(ExternalTunnelList.class)
1285 .child(ExternalTunnel.class, new ExternalTunnelKey(destinationDevice, sourceDevice, tunType)).build();
1286 return read(broker, datastoreType, iid).orNull();
1290 * Gets the external tunnel.
1292 * @param interfaceName
1293 * the interface name
1294 * @param datastoreType
1295 * the datastore type
1296 * @return the external tunnel
1299 public ExternalTunnel getExternalTunnel(String interfaceName, LogicalDatastoreType datastoreType) {
1300 ExternalTunnel externalTunnel = null;
1301 List<ExternalTunnel> externalTunnels = getAllExternalTunnels(datastoreType);
1302 for (ExternalTunnel tunnel : externalTunnels) {
1303 if (StringUtils.equalsIgnoreCase(interfaceName, tunnel.getTunnelInterfaceName())) {
1304 externalTunnel = tunnel;
1308 return externalTunnel;
1312 * Gets the all external tunnels.
1314 * @param datastoreType
1315 * the data store type
1316 * @return the all external tunnels
1318 public List<ExternalTunnel> getAllExternalTunnels(LogicalDatastoreType datastoreType) {
1319 InstanceIdentifier<ExternalTunnelList> iid = InstanceIdentifier.builder(ExternalTunnelList.class).build();
1320 return read(broker, datastoreType, iid).toJavaUtil().map(ExternalTunnelList::getExternalTunnel).orElse(
1324 public static List<MatchInfo> buildMatchesForElanTagShFlagAndDstMac(long elanTag, boolean shFlag, String macAddr) {
1325 List<MatchInfo> mkMatches = new ArrayList<>();
1327 new MatchMetadata(getElanMetadataLabel(elanTag, shFlag), MetaDataUtil.METADATA_MASK_SERVICE_SH_FLAG));
1328 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddr)));
1333 * Checks if is interface operational.
1335 * @param interfaceName
1336 * the interface name
1339 * @return true, if is interface operational
1341 public static boolean isInterfaceOperational(String interfaceName, DataBroker dataBroker) {
1342 if (StringUtils.isBlank(interfaceName)) {
1345 Interface ifState = getInterfaceStateFromOperDS(interfaceName, dataBroker);
1346 if (ifState == null) {
1349 return ifState.getOperStatus() == OperStatus.Up && ifState.getAdminStatus() == AdminStatus.Up;
1353 * Gets the interface state from operational ds.
1355 * @param interfaceName
1356 * the interface name
1359 * @return the interface state from oper ds
1361 public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
1362 .@Nullable Interface getInterfaceStateFromOperDS(String interfaceName, DataBroker dataBroker) {
1363 return MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL,
1364 createInterfaceStateInstanceIdentifier(interfaceName)).orNull();
1368 * Creates the interface state instance identifier.
1370 * @param interfaceName
1371 * the interface name
1372 * @return the instance identifier
1374 public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1375 .ietf.interfaces.rev140508.interfaces.state.Interface> createInterfaceStateInstanceIdentifier(
1376 String interfaceName) {
1377 InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1378 .ietf.interfaces.rev140508.interfaces.state.Interface> idBuilder = InstanceIdentifier
1379 .builder(InterfacesState.class)
1380 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1381 .ietf.interfaces.rev140508.interfaces.state.Interface.class,
1382 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1383 .ietf.interfaces.rev140508.interfaces.state.InterfaceKey(
1385 return idBuilder.build();
1389 public static ListenableFuture<Void> waitForTransactionToComplete(ListenableFuture<Void> future) {
1392 } catch (InterruptedException | ExecutionException e) {
1393 // NETVIRT-1215: Do not log.error() here, only debug(); but callers *MUST* @CheckReturnValue
1394 LOG.debug("Error writing to datastore", e);
1399 public static boolean isVxlan(@Nullable ElanInstance elanInstance) {
1400 return elanInstance != null && elanInstance.getSegmentType() != null
1401 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1402 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId() != 0;
1405 private static boolean isVxlanSegment(@Nullable ElanInstance elanInstance) {
1406 if (elanInstance != null) {
1407 List<ElanSegments> elanSegments = elanInstance.getElanSegments();
1408 if (elanSegments != null) {
1409 for (ElanSegments segment : elanSegments) {
1410 if (segment != null && segment.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1411 && segment.getSegmentationId() != null
1412 && segment.getSegmentationId().longValue() != 0) {
1421 public static boolean isVxlanNetworkOrVxlanSegment(@Nullable ElanInstance elanInstance) {
1422 return isVxlan(elanInstance) || isVxlanSegment(elanInstance);
1425 public static Long getVxlanSegmentationId(ElanInstance elanInstance) {
1426 Long segmentationId = 0L;
1427 if (elanInstance == null) {
1428 return segmentationId;
1431 if (elanInstance.getSegmentType() != null
1432 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1433 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId().longValue() != 0) {
1434 segmentationId = elanInstance.getSegmentationId();
1436 for (ElanSegments segment: elanInstance.getElanSegments()) {
1437 if (segment != null && segment.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1438 && segment.getSegmentationId() != null
1439 && segment.getSegmentationId().longValue() != 0) {
1440 segmentationId = segment.getSegmentationId();
1444 return segmentationId;
1447 public static boolean isVlan(ElanInstance elanInstance) {
1448 return elanInstance != null && elanInstance.getSegmentType() != null
1449 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVlan.class)
1450 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId() != 0;
1453 public static boolean isFlat(ElanInstance elanInstance) {
1454 return elanInstance != null && elanInstance.getSegmentType() != null
1455 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeFlat.class);
1458 public void addDmacRedirectToDispatcherFlows(Long elanTag, String displayName,
1459 String macAddress, List<BigInteger> dpnIds) {
1460 for (BigInteger dpId : dpnIds) {
1461 LoggingFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
1462 tx -> mdsalManager.addFlow(tx, buildDmacRedirectToDispatcherFlow(dpId, macAddress, displayName,
1464 "Error adding DMAC redirect to dispatcher flows");
1468 public void removeDmacRedirectToDispatcherFlows(Long elanTag, String macAddress, List<BigInteger> dpnIds) {
1469 LoggingFutures.addErrorLogging(
1470 txRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.CONFIGURATION, tx -> {
1471 for (BigInteger dpId : dpnIds) {
1472 String flowId = getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, macAddress, elanTag);
1473 mdsalManager.removeFlow(tx, dpId, MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE, flowId));
1475 }), LOG, "Error removing DMAC redirect to dispatcher flows");
1478 public static FlowEntity buildDmacRedirectToDispatcherFlow(BigInteger dpId, String dstMacAddress,
1479 String displayName, long elanTag) {
1480 List<MatchInfo> matches = new ArrayList<>();
1481 matches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag), MetaDataUtil.METADATA_MASK_SERVICE));
1482 matches.add(new MatchEthernetDestination(new MacAddress(dstMacAddress)));
1483 List<InstructionInfo> instructions = new ArrayList<>();
1484 List<ActionInfo> actions = new ArrayList<>();
1485 actions.add(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
1487 instructions.add(new InstructionApplyActions(actions));
1488 String flowId = getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, dstMacAddress, elanTag);
1489 return MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 20, displayName, 0, 0,
1490 ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(BigInteger.valueOf(elanTag)),
1491 matches, instructions);
1495 public String getExternalElanInterface(String elanInstanceName, BigInteger dpnId) {
1496 DpnInterfaces dpnInterfaces = getElanInterfaceInfoByElanDpn(elanInstanceName, dpnId);
1497 if (dpnInterfaces == null || dpnInterfaces.getInterfaces() == null) {
1498 LOG.trace("Elan {} does not have interfaces in DPN {}", elanInstanceName, dpnId);
1502 for (String dpnInterface : dpnInterfaces.getInterfaces()) {
1503 if (interfaceManager.isExternalInterface(dpnInterface)) {
1504 return dpnInterface;
1508 LOG.trace("Elan {} does not have any external interace attached to DPN {}", elanInstanceName, dpnId);
1512 public static Acquired lockElanMacDPN(long elanTag, String macAddress, BigInteger dpnId) {
1513 return ELAN_LOCKS.acquire(new ElanLockName(elanTag, macAddress, dpnId));
1516 public static List<ListenableFuture<Void>>
1517 returnFailedListenableFutureIfTransactionCommitFailedExceptionCauseOrElseThrow(RuntimeException exception) {
1519 Throwable cause = exception.getCause();
1520 if (cause != null && cause instanceof TransactionCommitFailedException) {
1521 return Collections.singletonList(Futures.immediateFailedFuture(cause));
1527 public static List<PhysAddress> getPhysAddress(List<String> macAddress) {
1528 Preconditions.checkNotNull(macAddress, "macAddress cannot be null");
1529 List<PhysAddress> physAddresses = new ArrayList<>();
1530 for (String mac : macAddress) {
1531 physAddresses.add(new PhysAddress(mac));
1533 return physAddresses;
1536 public static List<StaticMacEntries> getStaticMacEntries(List<String> staticMacAddresses) {
1537 if (isEmpty(staticMacAddresses)) {
1538 return Collections.EMPTY_LIST;
1540 StaticMacEntriesBuilder staticMacEntriesBuilder = new StaticMacEntriesBuilder();
1541 List<StaticMacEntries> staticMacEntries = new ArrayList<>();
1542 List<PhysAddress> physAddressList = getPhysAddress(staticMacAddresses);
1543 for (PhysAddress physAddress : physAddressList) {
1544 staticMacEntries.add(staticMacEntriesBuilder.setMacAddress(physAddress).build());
1546 return staticMacEntries;
1549 public static InstanceIdentifier<StaticMacEntries> getStaticMacEntriesCfgDataPathIdentifier(String interfaceName,
1550 String macAddress) {
1551 return InstanceIdentifier.builder(ElanInterfaces.class)
1552 .child(ElanInterface.class, new ElanInterfaceKey(interfaceName)).child(StaticMacEntries.class,
1553 new StaticMacEntriesKey(new PhysAddress(macAddress))).build();
1556 public static <T> List<T> diffOf(List<T> orig, List<T> updated) {
1557 if (isEmpty(orig)) {
1558 return Collections.EMPTY_LIST;
1560 List<T> diff = Lists.newArrayList(orig);
1561 if (isNotEmpty(updated)) {
1562 diff.removeAll(updated);
1567 public static boolean isEmpty(Collection collection) {
1568 return collection == null || collection.isEmpty();
1571 public static boolean isNotEmpty(Collection collection) {
1572 return !isEmpty(collection);
1575 public Optional<IpAddress> getSourceIpAddress(Ethernet ethernet) {
1576 Optional<IpAddress> srcIpAddress = Optional.absent();
1577 if (ethernet.getPayload() == null) {
1578 return srcIpAddress;
1580 byte[] ipAddrBytes = null;
1581 if (ethernet.getPayload() instanceof IPv4) {
1582 IPv4 ipv4 = (IPv4) ethernet.getPayload();
1583 ipAddrBytes = Ints.toByteArray(ipv4.getSourceAddress());
1584 } else if (ethernet.getPayload() instanceof ARP) {
1585 ipAddrBytes = ((ARP) ethernet.getPayload()).getSenderProtocolAddress();
1587 if (ipAddrBytes != null) {
1588 String ipAddr = NWUtil.toStringIpAddress(ipAddrBytes);
1589 return Optional.of(IpAddressBuilder.getDefaultInstance(ipAddr));
1591 return srcIpAddress;
1594 public List<MacEntry> getElanMacEntries(String elanName) {
1595 MacTable macTable = getElanMacTable(elanName);
1596 if (macTable == null) {
1599 return macTable.getMacEntry();
1602 public boolean isTunnelInLogicalGroup(String interfaceName) {
1603 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1604 .ietf.interfaces.rev140508.interfaces.Interface configIface =
1605 interfaceManager.getInterfaceInfoFromConfigDataStore(interfaceName);
1606 if (configIface == null) {
1607 configIface = iitmProvider.getInterface(interfaceName);
1609 if (configIface == null) {
1612 IfTunnel ifTunnel = configIface.augmentation(IfTunnel.class);
1613 if (ifTunnel != null && ifTunnel.getTunnelInterfaceType().isAssignableFrom(TunnelTypeVxlan.class)) {
1614 ParentRefs refs = configIface.augmentation(ParentRefs.class);
1615 return refs != null && !Strings.isNullOrEmpty(refs.getParentInterface());
1620 public static InstanceIdentifier<Flow> getFlowIid(Flow flow, BigInteger dpnId) {
1621 FlowKey flowKey = new FlowKey(new FlowId(flow.getId()));
1622 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId nodeId =
1623 new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId("openflow:" + dpnId);
1624 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node nodeDpn =
1625 new NodeBuilder().setId(nodeId).withKey(new NodeKey(nodeId)).build();
1626 return InstanceIdentifier.builder(Nodes.class)
1627 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class,
1628 nodeDpn.key()).augmentation(FlowCapableNode.class)
1629 .child(Table.class, new TableKey(flow.getTableId())).child(Flow.class, flowKey).build();
1632 public static String getElanInterfaceJobKey(String interfaceName) {
1633 return "elaninterface-" + interfaceName;
1636 public void removeArpResponderFlow(BigInteger dpnId, String ingressInterfaceName, String ipAddress,
1638 LOG.info("Removing the ARP responder flow on DPN {} of Interface {} with IP {}", dpnId, ingressInterfaceName,
1640 LoggingFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.CONFIGURATION,
1641 tx -> mdsalManager.removeFlow(tx, dpnId, ArpResponderUtil.getFlowId(lportTag, ipAddress),
1642 NwConstants.ARP_RESPONDER_TABLE)), LOG, "Error removing ARP responder flow");
1646 public static String getRouterPordIdFromElanInstance(DataBroker dataBroker, String elanInstanceName) {
1647 Optional<Subnetmaps> subnetMapsData =
1648 read(dataBroker, LogicalDatastoreType.CONFIGURATION, buildSubnetMapsWildCardPath());
1649 if (subnetMapsData.isPresent()) {
1650 List<Subnetmap> subnetMapList = subnetMapsData.get().getSubnetmap();
1651 if (subnetMapList != null && !subnetMapList.isEmpty()) {
1652 for (Subnetmap subnet : subnetMapList) {
1653 if (subnet.getNetworkId().getValue().equals(elanInstanceName)) {
1654 if (subnet.getRouterInterfacePortId() != null) {
1655 return subnet.getRouterInterfacePortId().getValue();
1664 protected Node buildDpnNode(BigInteger dpnId) {
1665 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId nodeId =
1666 new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819
1667 .NodeId("openflow:" + dpnId);
1668 Node nodeDpn = new NodeBuilder().setId(nodeId).withKey(new NodeKey(nodeId)).build();
1672 public void syncUpdateGroup(BigInteger dpnId, Group newGroup, long delayTime,
1673 TypedWriteTransaction<Datastore.Configuration> confTx) {
1674 Node nodeDpn = buildDpnNode(dpnId);
1675 long groupIdInfo = newGroup.getGroupId().getValue();
1676 GroupKey groupKey = new GroupKey(new GroupId(groupIdInfo));
1677 InstanceIdentifier<Group> groupInstanceId = InstanceIdentifier.builder(Nodes.class)
1678 .child(Node.class, nodeDpn.key()).augmentation(FlowCapableNode.class)
1679 .child(Group.class, groupKey).build();
1680 LOG.trace("Performing merge operation for remote BC group for node {} with group {}", nodeDpn, newGroup);
1681 Optional<Group> existingGroupOpt = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION, groupInstanceId);
1682 if (!existingGroupOpt.isPresent()) {
1683 LOG.debug("Group {} doesn't exist. Performing syncInstall", groupIdInfo);
1684 mdsalManager.addGroup(confTx, dpnId, newGroup);
1687 Buckets existingGroup = existingGroupOpt.get().getBuckets();
1688 if (existingGroup == null) {
1689 LOG.debug("Bucket doesn't exist for group {}. Performing syncInstall", groupIdInfo);
1690 mdsalManager.addGroup(confTx, dpnId, newGroup);
1693 if (newGroup.getBuckets() == null) {
1694 LOG.debug("Buckets are not sent for group {}. Skipping merge operation", groupIdInfo);
1697 List<Bucket> newBuckets = newGroup.getBuckets().getBucket();
1698 List<Bucket> existingBuckets = existingGroup.getBucket();
1699 Set<Bucket> toMergeBucketsWithoutId = new LinkedHashSet<>();
1701 existingBuckets.stream()
1702 .map(TO_BUCKET_WITHOUT_ID)
1703 .forEach(bucketWithoutId -> toMergeBucketsWithoutId.add(bucketWithoutId));
1706 .map(TO_BUCKET_WITHOUT_ID)
1707 .forEach(bucketWithoutId -> toMergeBucketsWithoutId.add(bucketWithoutId));
1709 if (toMergeBucketsWithoutId.size() == existingBuckets.size()) {
1710 //no new buckets got added
1711 //size matched and no extra buckets in existing buckets , rewrite the group
1712 LOG.debug("Buckets did not change group {}. Skipping merge operation", groupIdInfo);
1715 LOG.debug("Old group buckets size {} New group buckets size {} Dpn id {} Group id {} ",
1716 existingBuckets.size(), toMergeBucketsWithoutId.size(), dpnId, groupIdInfo);
1717 AtomicLong bucketIdValue = new AtomicLong(-1);
1718 //Change the bucket id of existing buckets
1719 List<Bucket> bucketsToBeAdded = toMergeBucketsWithoutId.stream()
1720 .map(bucketWithoutId -> TO_BUCKET_WITH_ID.apply(bucketWithoutId, bucketIdValue))
1721 .collect(Collectors.toList());
1723 Group group = MDSALUtil.buildGroup(newGroup.getGroupId().getValue(), newGroup.getGroupName(),
1724 GroupTypes.GroupAll, MDSALUtil.buildBucketLists(bucketsToBeAdded));
1725 mdsalManager.addGroup(confTx, dpnId, group);
1726 LOG.trace("Installed remote BC group for node {} with group {}", nodeDpn, group);
1729 static InstanceIdentifier<Subnetmaps> buildSubnetMapsWildCardPath() {
1730 return InstanceIdentifier.create(Subnetmaps.class);
1733 public static InstanceIdentifier<Group> getGroupInstanceid(BigInteger dpnId, long groupId) {
1734 return InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId)))
1735 .augmentation(FlowCapableNode.class).child(Group.class, new GroupKey(new GroupId(groupId))).build();
1738 public static String getBcGroupUpdateKey(String elanName) {
1739 return "bc.group.update." + elanName;