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.opendaylight.yangtools.yang.common.Uint32;
201 import org.opendaylight.yangtools.yang.common.Uint64;
202 import org.slf4j.Logger;
203 import org.slf4j.LoggerFactory;
206 public class ElanUtils {
207 private static final class ElanLockName {
208 private final String macAddress;
209 private final Uint64 dpnId;
210 private final long elanTag;
212 ElanLockName(long elanTag, String macAddress, Uint64 dpnId) {
213 this.elanTag = elanTag;
214 this.macAddress = macAddress;
219 public int hashCode() {
220 return 31 * Long.hashCode(elanTag) + Objects.hash(macAddress, dpnId);
224 public boolean equals(Object obj) {
228 if (!(obj instanceof ElanLockName)) {
231 final ElanLockName other = (ElanLockName) obj;
232 return elanTag == other.elanTag && Objects.equals(macAddress, other.macAddress)
233 && Objects.equals(dpnId, other.dpnId);
237 private static final Logger LOG = LoggerFactory.getLogger(ElanUtils.class);
238 private static final NamedLocks<ElanLockName> ELAN_LOCKS = new NamedLocks<>();
240 private final DataBroker broker;
241 private final ManagedNewTransactionRunner txRunner;
242 private final IMdsalApiManager mdsalManager;
243 private final OdlInterfaceRpcService interfaceManagerRpcService;
244 private final ItmRpcService itmRpcService;
245 private final IInterfaceManager interfaceManager;
246 private final ElanConfig elanConfig;
247 private final ElanItmUtils elanItmUtils;
248 private final ElanEtreeUtils elanEtreeUtils;
249 private final ElanInterfaceCache elanInterfaceCache;
250 private final IITMProvider iitmProvider;
251 private final ElanGroupCache elanGroupCache;
253 private static final Function<Bucket, Bucket> TO_BUCKET_WITHOUT_ID = (bucket) -> new BucketBuilder(bucket)
254 .setBucketId(new BucketId(0L))
257 private static final BiFunction<Bucket, AtomicLong, Bucket> TO_BUCKET_WITH_ID = (bucket, id)
258 -> new BucketBuilder(bucket)
259 .setBucketId(new BucketId(id.incrementAndGet()))
262 public static final FutureCallback<Void> DEFAULT_CALLBACK = new FutureCallback<Void>() {
264 public void onSuccess(Void result) {
265 LOG.debug("Success in Datastore operation");
269 public void onFailure(Throwable error) {
270 LOG.error("Error in Datastore operation", error);
275 public ElanUtils(DataBroker dataBroker, IMdsalApiManager mdsalManager,
276 OdlInterfaceRpcService interfaceManagerRpcService, ItmRpcService itmRpcService, ElanConfig elanConfig,
277 IInterfaceManager interfaceManager, ElanEtreeUtils elanEtreeUtils, ElanItmUtils elanItmUtils,
278 ElanInterfaceCache elanInterfaceCache, IITMProvider iitmProvider, ElanGroupCache elanGroupCache) {
279 this.broker = dataBroker;
280 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
281 this.mdsalManager = mdsalManager;
282 this.interfaceManagerRpcService = interfaceManagerRpcService;
283 this.itmRpcService = itmRpcService;
284 this.interfaceManager = interfaceManager;
285 this.elanConfig = elanConfig;
286 this.elanEtreeUtils = elanEtreeUtils;
287 this.elanItmUtils = elanItmUtils;
288 this.elanInterfaceCache = elanInterfaceCache;
289 this.iitmProvider = iitmProvider;
290 this.elanGroupCache = elanGroupCache;
293 public final Boolean isOpenstackVniSemanticsEnforced() {
294 return elanConfig.isOpenstackVniSemanticsEnforced() != null
295 ? elanConfig.isOpenstackVniSemanticsEnforced() : false;
299 * Uses the IdManager to retrieve a brand new ElanTag.
305 * @return the integer
307 public static Uint32 retrieveNewElanTag(IdManagerService idManager, String idKey) {
309 AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(ElanConstants.ELAN_ID_POOL_NAME)
310 .setIdKey(idKey).build();
313 Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
314 RpcResult<AllocateIdOutput> rpcResult = result.get();
315 if (rpcResult.isSuccessful()) {
316 return Uint32.valueOf(rpcResult.getResult().getIdValue().longValue());
318 LOG.warn("RPC Call to Allocate Id returned with Errors {}", rpcResult.getErrors());
320 } catch (InterruptedException | ExecutionException e) {
321 LOG.warn("Exception when Allocating Id", e);
323 return Uint32.valueOf(0L);
326 public static void releaseId(IdManagerService idManager, String poolName, String idKey) {
327 ReleaseIdInput releaseIdInput = new ReleaseIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
328 LoggingFutures.addErrorLogging(idManager.releaseId(releaseIdInput), LOG, "Release Id");
334 * @deprecated Consider using {@link #read2(LogicalDatastoreType, InstanceIdentifier)} with proper exception
336 * @param broker dataBroker
337 * @param datastoreType Logical DataStore type
338 * @param path IID to read
339 * @param <T> T extends DataObject
340 * @return the read value T
343 @SuppressWarnings("checkstyle:IllegalCatch")
344 public static <T extends DataObject> Optional<T> read(@NonNull DataBroker broker,
345 LogicalDatastoreType datastoreType, InstanceIdentifier<T> path) {
346 try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
347 return tx.read(datastoreType, path).get();
348 } catch (Exception e) {
349 throw new RuntimeException(e);
353 public <T extends DataObject> Optional<T> read2(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path)
354 throws ReadFailedException {
355 try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
356 CheckedFuture<Optional<T>, ReadFailedException> checkedFuture = tx.read(datastoreType, path);
357 return checkedFuture.checkedGet();
361 @SuppressWarnings("checkstyle:ForbidCertainMethod")
362 public static <T extends DataObject> void delete(DataBroker broker, LogicalDatastoreType datastoreType,
363 InstanceIdentifier<T> path) {
364 WriteTransaction tx = broker.newWriteOnlyTransaction();
365 tx.delete(datastoreType, path);
366 Futures.addCallback(tx.submit(), DEFAULT_CALLBACK, MoreExecutors.directExecutor());
369 public static InstanceIdentifier<ElanInterface> getElanInterfaceConfigurationDataPathId(String interfaceName) {
370 return InstanceIdentifier.builder(ElanInterfaces.class)
371 .child(ElanInterface.class, new ElanInterfaceKey(interfaceName)).build();
374 // elan-state Operational container
376 public static Elan getElanByName(DataBroker broker, String elanInstanceName) {
377 InstanceIdentifier<Elan> elanIdentifier = getElanInstanceOperationalDataPath(elanInstanceName);
378 return MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, elanIdentifier).orNull();
382 public static Elan getElanByName(TypedReadTransaction<Operational> tx, String elanInstanceName)
383 throws ExecutionException, InterruptedException {
384 return tx.read(getElanInstanceOperationalDataPath(elanInstanceName)).get().orNull();
387 public static InstanceIdentifier<Elan> getElanInstanceOperationalDataPath(String elanInstanceName) {
388 return InstanceIdentifier.builder(ElanState.class).child(Elan.class, new ElanKey(elanInstanceName)).build();
391 // grouping of forwarding-entries
393 public MacEntry getInterfaceMacEntriesOperationalDataPath(String interfaceName, PhysAddress physAddress) {
394 InstanceIdentifier<MacEntry> existingMacEntryId = getInterfaceMacEntriesIdentifierOperationalDataPath(
395 interfaceName, physAddress);
396 return read(broker, LogicalDatastoreType.OPERATIONAL, existingMacEntryId).orNull();
400 public MacEntry getInterfaceMacEntriesOperationalDataPathFromId(TypedReadTransaction<Operational> tx,
401 InstanceIdentifier<MacEntry> identifier) throws ExecutionException, InterruptedException {
402 return tx.read(identifier).get().orNull();
405 public static InstanceIdentifier<MacEntry> getInterfaceMacEntriesIdentifierOperationalDataPath(String interfaceName,
406 PhysAddress physAddress) {
407 return InstanceIdentifier.builder(ElanInterfaceForwardingEntries.class)
408 .child(ElanInterfaceMac.class, new ElanInterfaceMacKey(interfaceName))
409 .child(MacEntry.class, new MacEntryKey(physAddress)).build();
413 // elan-forwarding-tables Operational container
414 public Optional<MacEntry> getMacEntryForElanInstance(String elanName, PhysAddress physAddress) {
415 InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanName, physAddress);
416 return read(broker, LogicalDatastoreType.OPERATIONAL, macId);
419 public Optional<MacEntry> getMacEntryForElanInstance(TypedReadTransaction<Operational> tx, String elanName,
420 PhysAddress physAddress) throws ExecutionException, InterruptedException {
421 InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanName, physAddress);
422 return tx.read(macId).get();
426 public MacEntry getMacEntryFromElanMacId(TypedReadTransaction<Operational> tx,
427 InstanceIdentifier<MacEntry> identifier) throws ExecutionException, InterruptedException {
428 return tx.read(identifier).get().orNull();
431 public static InstanceIdentifier<MacEntry> getMacEntryOperationalDataPath(String elanName,
432 PhysAddress physAddress) {
433 return InstanceIdentifier.builder(ElanForwardingTables.class).child(MacTable.class, new MacTableKey(elanName))
434 .child(MacEntry.class, new MacEntryKey(physAddress)).build();
437 public static InstanceIdentifier<MacTable> getElanMacTableOperationalDataPath(String elanName) {
438 return InstanceIdentifier.builder(ElanForwardingTables.class).child(MacTable.class, new MacTableKey(elanName))
442 // elan-interface-forwarding-entries Operational container
443 public ElanInterfaceMac getElanInterfaceMacByInterfaceName(String interfaceName) {
444 return getElanInterfaceMacByInterfaceName(broker, interfaceName);
448 public static ElanInterfaceMac getElanInterfaceMacByInterfaceName(DataBroker dataBroker, String interfaceName) {
449 InstanceIdentifier<ElanInterfaceMac> elanInterfaceId = getElanInterfaceMacEntriesOperationalDataPath(
451 return read(dataBroker, LogicalDatastoreType.OPERATIONAL, elanInterfaceId).orNull();
454 public static InstanceIdentifier<ElanInterfaceMac> getElanInterfaceMacEntriesOperationalDataPath(
455 String interfaceName) {
456 return InstanceIdentifier.builder(ElanInterfaceForwardingEntries.class)
457 .child(ElanInterfaceMac.class, new ElanInterfaceMacKey(interfaceName)).build();
461 * Returns the list of Interfaces that belong to an Elan on an specific DPN.
462 * Data retrieved from Elan's operational DS: elan-dpn-interfaces container.
464 * @param elanInstanceName
465 * name of the Elan to which the interfaces must belong to
467 * Id of the DPN where the interfaces are located
468 * @return the elan interface Info
471 public DpnInterfaces getElanInterfaceInfoByElanDpn(String elanInstanceName, Uint64 dpId) {
472 InstanceIdentifier<DpnInterfaces> elanDpnInterfacesId = getElanDpnInterfaceOperationalDataPath(elanInstanceName,
474 return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfacesId).orNull();
478 * Returns the InstanceIdentifier that points to the Interfaces of an Elan
479 * in a given DPN in the Operational DS. Data retrieved from Elans's
480 * operational DS: dpn-interfaces list
482 * @param elanInstanceName
483 * name of the Elan to which the interfaces must belong to
485 * Id of the DPN where the interfaces are located
486 * @return the elan dpn interface
488 public static InstanceIdentifier<DpnInterfaces> getElanDpnInterfaceOperationalDataPath(String elanInstanceName,
490 return InstanceIdentifier.builder(ElanDpnInterfaces.class)
491 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName))
492 .child(DpnInterfaces.class, new DpnInterfacesKey(dpId)).build();
495 // elan-tag-name-map Operational Container
497 public ElanTagName getElanInfoByElanTag(Uint32 elanTag) {
498 InstanceIdentifier<ElanTagName> elanId = getElanInfoEntriesOperationalDataPath(elanTag);
499 Optional<ElanTagName> existingElanInfo = read(broker,
500 LogicalDatastoreType.OPERATIONAL, elanId);
501 return existingElanInfo.orNull();
504 public static InstanceIdentifier<ElanTagName> getElanInfoEntriesOperationalDataPath(Uint32 elanTag) {
505 return InstanceIdentifier.builder(ElanTagNameMap.class).child(ElanTagName.class, new ElanTagNameKey(elanTag))
509 // interface-index-tag operational container
510 public Optional<IfIndexInterface> getInterfaceInfoByInterfaceTag(Uint32 interfaceTag) {
511 InstanceIdentifier<IfIndexInterface> interfaceId = getInterfaceInfoEntriesOperationalDataPath(interfaceTag);
512 return read(broker, LogicalDatastoreType.OPERATIONAL, interfaceId);
515 public static InstanceIdentifier<IfIndexInterface> getInterfaceInfoEntriesOperationalDataPath(Uint32 interfaceTag) {
516 return InstanceIdentifier.builder(IfIndexesInterfaceMap.class)
517 .child(IfIndexInterface.class,
518 new IfIndexInterfaceKey(Integer.valueOf(interfaceTag.intValue()))).build();
521 public static InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
522 return InstanceIdentifier.builder(ElanDpnInterfaces.class)
523 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
527 public ElanDpnInterfacesList getElanDpnInterfacesList(String elanName) {
528 InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanName);
529 return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId).orNull();
533 public ElanDpnInterfaces getElanDpnInterfacesList() {
534 InstanceIdentifier<ElanDpnInterfaces> elanDpnInterfaceId = InstanceIdentifier.builder(ElanDpnInterfaces.class)
536 return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId).orNull();
540 * This method is useful get all ELAN participating CSS dpIds to install
541 * program remote dmac entries and updating remote bc groups for tor
544 * @param elanInstanceName
545 * the elan instance name
546 * @return list of dpIds
549 public List<Uint64> getParticipatingDpnsInElanInstance(String elanInstanceName) {
550 List<Uint64> dpIds = new ArrayList<>();
551 InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanInstanceName);
552 Optional<ElanDpnInterfacesList> existingElanDpnInterfaces = read(broker,
553 LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId);
554 if (!existingElanDpnInterfaces.isPresent()) {
557 List<DpnInterfaces> dpnInterfaces = existingElanDpnInterfaces.get().nonnullDpnInterfaces();
558 for (DpnInterfaces dpnInterface : dpnInterfaces) {
559 dpIds.add(dpnInterface.getDpId());
564 public static long getElanRemoteBroadCastGroupID(long elanTag) {
565 return ElanConstants.ELAN_GID_MIN + elanTag % ElanConstants.ELAN_GID_MIN * 2;
569 * Gets the elan mac table.
573 * @return the elan mac table
576 public MacTable getElanMacTable(String elanName) {
577 return getElanMacTable(broker, elanName);
581 public static MacTable getElanMacTable(DataBroker dataBroker, String elanName) {
582 InstanceIdentifier<MacTable> elanMacTableId = getElanMacTableOperationalDataPath(elanName);
583 return read(dataBroker, LogicalDatastoreType.OPERATIONAL, elanMacTableId).orNull();
586 public static long getElanLocalBCGId(long elanTag) {
587 return ElanConstants.ELAN_GID_MIN + (elanTag % ElanConstants.ELAN_GID_MIN * 2 - 1);
590 public static long getElanRemoteBCGId(long elanTag) {
591 return ElanConstants.ELAN_GID_MIN + elanTag % ElanConstants.ELAN_GID_MIN * 2;
594 public static long getEtreeLeafLocalBCGId(long etreeLeafTag) {
595 return ElanConstants.ELAN_GID_MIN + (etreeLeafTag % ElanConstants.ELAN_GID_MIN * 2 - 1);
598 public static long getEtreeLeafRemoteBCGId(long etreeLeafTag) {
599 return ElanConstants.ELAN_GID_MIN + etreeLeafTag % ElanConstants.ELAN_GID_MIN * 2;
602 public static Uint64 getElanMetadataLabel(long elanTag, boolean isSHFlagSet) {
603 int shBit = isSHFlagSet ? 1 : 0;
604 return Uint64.valueOf(BigInteger.valueOf(elanTag).shiftLeft(24).or(BigInteger.valueOf(shBit)));
608 * Setting SMAC, DMAC, UDMAC in this DPN and optionally in other DPNs.
612 * @param interfaceInfo
618 * @param configureRemoteFlows
619 * true if remote dmac flows should be configured as well
620 * @param writeFlowGroupTx
623 public void setupMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
624 long macTimeout, String macAddress, boolean configureRemoteFlows,
625 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
626 try (Acquired lock = lockElanMacDPN(elanInfo.getElanTag().toJava(), macAddress, interfaceInfo.getDpId())) {
627 setupKnownSmacFlow(elanInfo, interfaceInfo, macTimeout, macAddress, mdsalManager,
629 setupOrigDmacFlows(elanInfo, interfaceInfo, macAddress, configureRemoteFlows, mdsalManager,
634 public void setupDMacFlowOnRemoteDpn(ElanInstance elanInfo, InterfaceInfo interfaceInfo, Uint64 dstDpId,
635 String macAddress, TypedWriteTransaction<Configuration> writeFlowTx) {
636 String elanInstanceName = elanInfo.getElanInstanceName();
637 setupRemoteDmacFlow(dstDpId, interfaceInfo.getDpId(), interfaceInfo.getInterfaceTag(),
638 elanInfo.getElanTag(), macAddress, elanInstanceName, writeFlowTx,
639 interfaceInfo.getInterfaceName(), elanInfo);
640 LOG.info("Remote Dmac flow entry created for elan Name:{}, logical port Name:{} and"
641 + " mac address {} on dpn:{}", elanInstanceName, interfaceInfo.getPortName(),
642 macAddress, dstDpId);
646 * Inserts a Flow in SMAC table to state that the MAC has already been
649 private void setupKnownSmacFlow(ElanInstance elanInfo, InterfaceInfo interfaceInfo, long macTimeout,
650 String macAddress, IMdsalApiManager mdsalApiManager,
651 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
652 FlowEntity flowEntity = buildKnownSmacFlow(elanInfo, interfaceInfo, macTimeout, macAddress);
653 mdsalApiManager.addFlow(writeFlowGroupTx, flowEntity);
654 LOG.debug("Known Smac flow entry created for elan Name:{}, logical Interface port:{} and mac address:{}",
655 elanInfo.getElanInstanceName(), elanInfo.getDescription(), macAddress);
658 public FlowEntity buildKnownSmacFlow(ElanInstance elanInfo, InterfaceInfo interfaceInfo, long macTimeout,
660 int lportTag = interfaceInfo.getInterfaceTag();
661 // Matching metadata and eth_src fields
662 List<MatchInfo> mkMatches = new ArrayList<>();
663 mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanInfo.getElanTag().toJava(), lportTag),
664 ElanHelper.getElanMetadataMask()));
665 mkMatches.add(new MatchEthernetSource(new MacAddress(macAddress)));
666 List<InstructionInfo> mkInstructions = new ArrayList<>();
667 mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
668 Uint64 dpId = interfaceInfo.getDpId();
669 Uint32 elanTag = getElanTag(elanInfo, interfaceInfo);
670 return new FlowEntityBuilder()
672 .setTableId(NwConstants.ELAN_SMAC_TABLE)
673 .setFlowId(getKnownDynamicmacFlowRef(NwConstants.ELAN_SMAC_TABLE, dpId, lportTag, macAddress, elanTag))
675 .setFlowName(elanInfo.getDescription())
676 .setIdleTimeOut((int) macTimeout)
678 .setCookie(Uint64.valueOf(ElanConstants.COOKIE_ELAN_KNOWN_SMAC.longValue() + elanTag.longValue()))
679 .setMatchInfoList(mkMatches)
680 .setInstructionInfoList(mkInstructions)
682 // If Mac timeout is 0, the flow won't be deleted automatically, so no need to get notified
683 .setSendFlowRemFlag(macTimeout != 0)
687 private Uint32 getElanTag(ElanInstance elanInfo, InterfaceInfo interfaceInfo) {
688 EtreeInterface etreeInterface = elanInterfaceCache.getEtreeInterface(interfaceInfo.getInterfaceName()).orNull();
689 if (etreeInterface == null || etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
690 return elanInfo.getElanTag();
692 EtreeInstance etreeInstance = elanInfo.augmentation(EtreeInstance.class);
693 if (etreeInstance == null) {
694 LOG.warn("EtreeInterface {} is connected to a non-Etree network: {}",
695 interfaceInfo.getInterfaceName(), elanInfo.getElanInstanceName());
696 return elanInfo.getElanTag();
698 return etreeInstance.getEtreeLeafTagVal().getValue();
704 * Installs a Flow in INTERNAL_TUNNEL_TABLE of the affected DPN that sends
705 * the packet through the specified interface if the tunnel_id matches the
706 * interface's lportTag.
708 * @param interfaceInfo
710 * @param mdsalApiManager
711 * the mdsal API manager
712 * @param writeFlowGroupTx
713 * the writeFLowGroup tx
715 public void setupTermDmacFlows(InterfaceInfo interfaceInfo, IMdsalApiManager mdsalApiManager,
716 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
717 Uint64 dpId = interfaceInfo.getDpId();
718 int lportTag = interfaceInfo.getInterfaceTag();
719 Flow flow = MDSALUtil.buildFlowNew(NwConstants.INTERNAL_TUNNEL_TABLE,
720 getIntTunnelTableFlowRef(NwConstants.INTERNAL_TUNNEL_TABLE, lportTag), 5,
721 String.format("%s:%d", "ITM Flow Entry ", lportTag), 0, 0,
722 Uint64.valueOf(ITMConstants.COOKIE_ITM.longValue() + lportTag),
723 getTunnelIdMatchForFilterEqualsLPortTag(lportTag),
724 getInstructionsInPortForOutGroup(interfaceInfo.getInterfaceName()));
725 mdsalApiManager.addFlow(writeFlowGroupTx, dpId, flow);
726 LOG.debug("Terminating service table flow entry created on dpn:{} for logical Interface port:{}", dpId,
727 interfaceInfo.getPortName());
731 * Constructs the FlowName for flows installed in the Internal Tunnel Table,
732 * consisting in tableId + elanTag.
738 * @return the Internal tunnel
740 public static String getIntTunnelTableFlowRef(short tableId, int elanTag) {
741 return new StringBuilder().append(tableId).append(elanTag).toString();
745 * Constructs the Matches that checks that the tunnel_id field contains a
749 * lportTag that must be checked against the tunnel_id field
750 * @return the list of match Info
752 public static List<MatchInfo> getTunnelIdMatchForFilterEqualsLPortTag(int lportTag) {
753 List<MatchInfo> mkMatches = new ArrayList<>();
755 mkMatches.add(new MatchTunnelId(Uint64.valueOf(lportTag)));
760 * Constructs the Instructions that take the packet over a given interface.
763 * Name of the interface where the packet must be sent over. It
764 * can be a local interface or a tunnel interface (internal or
766 * @return the Instruction
768 public List<Instruction> getInstructionsInPortForOutGroup(String ifName) {
769 List<Instruction> mkInstructions = new ArrayList<>();
770 List<Action> actions = getEgressActionsForInterface(ifName, /* tunnelKey */ null);
772 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
773 return mkInstructions;
777 * Returns the list of Actions to be taken when sending the packet through
778 * an Elan interface. Note that this interface can refer to an ElanInterface
779 * where the Elan VM is attached to a DPN or an ITM tunnel interface where
780 * Elan traffic can be sent through. In this latter case, the tunnelKey is
781 * mandatory and it can hold serviceId for internal tunnels or the VNI for
788 * @return the egress actions for interface
790 @SuppressWarnings("checkstyle:IllegalCatch")
792 public List<Action> getEgressActionsForInterface(String ifName, @Nullable Long tunnelKey) {
793 List<Action> listAction = new ArrayList<>();
795 GetEgressActionsForInterfaceInput getEgressActionInput = new GetEgressActionsForInterfaceInputBuilder()
796 .setIntfName(ifName).setTunnelKey(tunnelKey).build();
797 Future<RpcResult<GetEgressActionsForInterfaceOutput>> result = interfaceManagerRpcService
798 .getEgressActionsForInterface(getEgressActionInput);
799 RpcResult<GetEgressActionsForInterfaceOutput> rpcResult = result.get();
800 if (!rpcResult.isSuccessful()) {
801 LOG.debug("RPC Call to Get egress actions for interface {} returned with Errors {}", ifName,
802 rpcResult.getErrors());
804 listAction = rpcResult.getResult().nonnullAction();
806 } catch (Exception e) {
807 LOG.warn("Exception when egress actions for interface {}", ifName, e);
812 private void setupOrigDmacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
813 boolean configureRemoteFlows, IMdsalApiManager mdsalApiManager,
814 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
815 Uint64 dpId = interfaceInfo.getDpId();
816 String ifName = interfaceInfo.getInterfaceName();
817 long ifTag = interfaceInfo.getInterfaceTag();
818 String elanInstanceName = elanInfo.getElanInstanceName();
820 Uint32 elanTag = elanInfo.getElanTag();
822 setupLocalDmacFlow(elanTag, dpId, ifName, macAddress, elanInfo, mdsalApiManager, ifTag,
824 LOG.debug("Dmac flow entry created for elan Name:{}, logical port Name:{} mand mac address:{} "
825 + "on dpn:{}", elanInstanceName, interfaceInfo.getPortName(), macAddress, dpId);
827 if (!configureRemoteFlows) {
831 List<DpnInterfaces> elanDpns = getInvolvedDpnsInElan(elanInstanceName);
832 for (DpnInterfaces elanDpn : elanDpns) {
834 if (Objects.equals(elanDpn.getDpId(), dpId)) {
838 // Check for the Remote DPN present in Inventory Manager
839 if (!isDpnPresent(elanDpn.getDpId())) {
843 // For remote DPNs a flow is needed to indicate that packets of this ELAN going to this MAC need to be
844 // forwarded through the appropriate ITM tunnel
845 setupRemoteDmacFlow(elanDpn.getDpId(), // srcDpn (the remote DPN in this case)
846 dpId, // dstDpn (the local DPN)
847 interfaceInfo.getInterfaceTag(), // lportTag of the local interface
848 elanTag, // identifier of the Elan
849 macAddress, // MAC to be programmed in remote DPN
850 elanInstanceName, writeFlowGroupTx, ifName, elanInfo
852 LOG.debug("Remote Dmac flow entry created for elan Name:{}, logical port Name:{} and mac address:{} on"
853 + " dpn:{}", elanInstanceName, interfaceInfo.getPortName(), macAddress, elanDpn.getDpId());
856 // TODO: Make sure that the same is performed against the ElanDevices.
860 public List<DpnInterfaces> getInvolvedDpnsInElan(String elanName) {
861 return getElanDPNByName(elanName);
865 public List<DpnInterfaces> getElanDPNByName(String elanInstanceName) {
866 InstanceIdentifier<ElanDpnInterfacesList> elanIdentifier = getElanDpnOperationDataPath(elanInstanceName);
867 return MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, elanIdentifier).toJavaUtil().map(
868 ElanDpnInterfacesList::getDpnInterfaces).orElse(emptyList());
871 private void setupLocalDmacFlow(Uint32 elanTag, Uint64 dpId, String ifName, String macAddress,
872 ElanInstance elanInfo, IMdsalApiManager mdsalApiManager, long ifTag,
873 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
874 Flow flowEntity = buildLocalDmacFlowEntry(elanTag, dpId, ifName, macAddress, elanInfo, ifTag);
875 mdsalApiManager.addFlow(writeFlowGroupTx, dpId, flowEntity);
876 installEtreeLocalDmacFlow(elanTag, dpId, ifName, macAddress, elanInfo, ifTag, writeFlowGroupTx);
879 private void installEtreeLocalDmacFlow(Uint32 elanTag, Uint64 dpId, String ifName, String macAddress,
880 ElanInstance elanInfo, long ifTag, TypedWriteTransaction<Configuration> writeFlowGroupTx) {
881 EtreeInterface etreeInterface = elanInterfaceCache.getEtreeInterface(ifName).orNull();
882 if (etreeInterface != null && etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
883 EtreeLeafTagName etreeTagName = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag.longValue());
884 if (etreeTagName == null) {
885 LOG.warn("Interface {} seems like it belongs to Etree but etreeTagName from elanTag {} is null",
888 Flow flowEntity = buildLocalDmacFlowEntry(etreeTagName.getEtreeLeafTag().getValue(), dpId,
889 ifName, macAddress, elanInfo, ifTag);
890 mdsalManager.addFlow(writeFlowGroupTx, dpId, flowEntity);
895 public static String getKnownDynamicmacFlowRef(short tableId, Uint64 dpId, long lporTag, String macAddress,
897 return String.valueOf(tableId) + elanTag + dpId + lporTag + macAddress;
900 public static String getKnownDynamicmacFlowRef(short tableId, Uint64 dpId, Uint64 remoteDpId,
901 String macAddress, Uint32 elanTag) {
902 return String.valueOf(tableId) + elanTag + dpId + remoteDpId + macAddress;
905 public static String getKnownDynamicmacFlowRef(short tableId, Uint64 dpId, String macAddress, Uint32 elanTag) {
906 return String.valueOf(tableId) + elanTag + dpId + macAddress;
909 public static String getKnownDynamicmacFlowRef(short elanDmacTable, Uint64 dpId, String extDeviceNodeId,
910 String dstMacAddress, long elanTag, boolean shFlag) {
911 return String.valueOf(elanDmacTable) + elanTag + dpId + extDeviceNodeId + dstMacAddress + shFlag;
915 * Builds the flow to be programmed in the DMAC table of the local DPN (that
916 * is, where the MAC is attached to). This flow consists in:
918 * <p>Match: + elanTag in metadata + packet goes to a MAC locally attached
919 * Actions: + optionally, pop-vlan + set-vlan-id + output to ifName's
936 public Flow buildLocalDmacFlowEntry(Uint32 elanTag, Uint64 dpId, String ifName, String macAddress,
937 ElanInstance elanInfo, long ifTag) {
939 List<MatchInfo> mkMatches = new ArrayList<>();
940 mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag.longValue()),
941 MetaDataUtil.METADATA_MASK_SERVICE));
942 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddress)));
944 List<Instruction> mkInstructions = new ArrayList<>();
945 List<Action> actions = getEgressActionsForInterface(ifName, /* tunnelKey */ null);
946 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
947 Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE,
948 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, ifTag, macAddress, elanTag), 20,
949 elanInfo.getElanInstanceName(), 0, 0,
950 Uint64.valueOf(ElanConstants.COOKIE_ELAN_KNOWN_DMAC.longValue() + elanTag.longValue()),
951 mkMatches, mkInstructions);
956 public void setupRemoteDmacFlow(Uint64 srcDpId, Uint64 destDpId, int lportTag, Uint32 elanTag,
957 String macAddress, String displayName, TypedWriteTransaction<Configuration> writeFlowGroupTx,
958 String interfaceName, ElanInstance elanInstance) {
959 if (interfaceManager.isExternalInterface(interfaceName)) {
960 LOG.debug("Ignoring install remote DMAC {} flow on provider interface {} elan {}",
961 macAddress, interfaceName, elanInstance.getElanInstanceName());
965 // if openstack-vni-semantics are enforced, segmentation ID is passed as network VNI for VxLAN based provider
966 // networks, 0 otherwise
967 long lportTagOrVni = !isOpenstackVniSemanticsEnforced() ? lportTag : isVxlanNetworkOrVxlanSegment(elanInstance)
968 ? getVxlanSegmentationId(elanInstance).longValue() : 0;
969 flowEntity = buildRemoteDmacFlowEntry(srcDpId, destDpId, lportTagOrVni, elanTag, macAddress, displayName,
971 mdsalManager.addFlow(writeFlowGroupTx, srcDpId, flowEntity);
972 setupEtreeRemoteDmacFlow(srcDpId, destDpId, lportTagOrVni, elanTag, macAddress, displayName, interfaceName,
973 writeFlowGroupTx, elanInstance);
976 private void setupEtreeRemoteDmacFlow(Uint64 srcDpId, Uint64 destDpId, long lportTagOrVni, Uint32 elanTag,
977 String macAddress, String displayName, String interfaceName,
978 TypedWriteTransaction<Configuration> writeFlowGroupTx,
979 ElanInstance elanInstance) {
981 EtreeInterface etreeInterface = elanInterfaceCache.getEtreeInterface(interfaceName).orNull();
982 if (etreeInterface != null && etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
983 EtreeLeafTagName etreeTagName = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag.longValue());
984 if (etreeTagName == null) {
985 LOG.warn("Interface {} seems like it belongs to Etree but etreeTagName from elanTag {} is null.",
986 interfaceName, elanTag);
988 flowEntity = buildRemoteDmacFlowEntry(srcDpId, destDpId, lportTagOrVni,
989 etreeTagName.getEtreeLeafTag().getValue(), macAddress, displayName, elanInstance);
990 mdsalManager.addFlow(writeFlowGroupTx, srcDpId, flowEntity);
996 * Builds a Flow to be programmed in a remote DPN's DMAC table. This flow
997 * consists in: Match: + elanTag in packet's metadata + packet going to a
998 * MAC known to be located in another DPN Actions: + set_tunnel_id
999 * + output ITM internal tunnel interface with the other DPN
1005 * @param lportTagOrVni
1006 * lportTag or network VNI
1011 * @param displayName
1013 * @param elanInstance
1015 * @return the flow remote Dmac
1017 @SuppressWarnings("checkstyle:IllegalCatch")
1018 public Flow buildRemoteDmacFlowEntry(Uint64 srcDpId, Uint64 destDpId, long lportTagOrVni, Uint32 elanTag,
1019 String macAddress, String displayName, ElanInstance elanInstance) {
1020 List<MatchInfo> mkMatches = new ArrayList<>();
1021 mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag.longValue()),
1022 MetaDataUtil.METADATA_MASK_SERVICE));
1023 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddress)));
1025 List<Instruction> mkInstructions = new ArrayList<>();
1027 // List of Action for the provided Source and Destination DPIDs
1029 List<Action> actions = null;
1030 if (isVxlanNetworkOrVxlanSegment(elanInstance)) {
1031 actions = elanItmUtils.getInternalTunnelItmEgressAction(srcDpId, destDpId, lportTagOrVni);
1032 } else if (isVlan(elanInstance) || isFlat(elanInstance)) {
1033 String interfaceName = getExternalElanInterface(elanInstance.getElanInstanceName(), srcDpId);
1034 if (null == interfaceName) {
1035 LOG.info("buildRemoteDmacFlowEntry: Could not find interfaceName for {} {}", srcDpId,
1038 actions = getEgressActionsForInterface(interfaceName, null);
1040 mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
1041 } catch (Exception e) {
1042 LOG.error("Could not get egress actions to add to flow for srcDpId {}, destDpId {}, lportTag/VNI {}",
1043 srcDpId, destDpId, lportTagOrVni, e);
1046 Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE,
1047 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, srcDpId, destDpId, macAddress, elanTag),
1049 displayName, 0, /* idleTimeout */
1050 0, /* hardTimeout */
1051 Uint64.valueOf(ElanConstants.COOKIE_ELAN_KNOWN_DMAC.longValue() + elanTag.longValue()),
1052 mkMatches, mkInstructions);
1058 public void deleteMacFlows(@Nullable ElanInstance elanInfo, @Nullable InterfaceInfo interfaceInfo,
1059 MacEntry macEntry, TypedReadWriteTransaction<Configuration> flowTx)
1060 throws ExecutionException, InterruptedException {
1061 if (elanInfo == null || interfaceInfo == null) {
1064 String macAddress = macEntry.getMacAddress().getValue();
1065 try (Acquired lock = lockElanMacDPN(elanInfo.getElanTag().toJava(),
1066 macAddress, interfaceInfo.getDpId())) {
1067 deleteMacFlows(elanInfo, interfaceInfo, macAddress, /* alsoDeleteSMAC */ true, flowTx);
1071 public void deleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1072 boolean deleteSmac, TypedReadWriteTransaction<Configuration> flowTx)
1073 throws ExecutionException, InterruptedException {
1074 String elanInstanceName = elanInfo.getElanInstanceName();
1075 List<DpnInterfaces> remoteFEs = getInvolvedDpnsInElan(elanInstanceName);
1076 Uint64 srcdpId = interfaceInfo.getDpId();
1077 boolean isFlowsRemovedInSrcDpn = false;
1078 for (DpnInterfaces dpnInterface : remoteFEs) {
1079 Uint32 elanTag = elanInfo.getElanTag();
1080 Uint64 dstDpId = dpnInterface.getDpId();
1081 if (executeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1082 elanTag, dstDpId, flowTx)) {
1083 isFlowsRemovedInSrcDpn = true;
1085 executeEtreeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1086 elanTag, dstDpId, flowTx);
1088 if (!isFlowsRemovedInSrcDpn) {
1089 deleteSmacAndDmacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, flowTx);
1093 private void executeEtreeDeleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1094 boolean deleteSmac, String elanInstanceName, Uint64 srcdpId, Uint32 elanTag, Uint64 dstDpId,
1095 TypedReadWriteTransaction<Configuration> flowTx) throws ExecutionException, InterruptedException {
1096 EtreeLeafTagName etreeLeafTag = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag.longValue());
1097 if (etreeLeafTag != null) {
1098 executeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1099 etreeLeafTag.getEtreeLeafTag().getValue(), dstDpId, flowTx);
1103 private boolean executeDeleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1104 boolean deleteSmac, String elanInstanceName, Uint64 srcdpId, Uint32 elanTag, Uint64 dstDpId,
1105 TypedReadWriteTransaction<Configuration> flowTx) throws ExecutionException, InterruptedException {
1106 boolean isFlowsRemovedInSrcDpn = false;
1107 if (dstDpId.equals(srcdpId)) {
1108 isFlowsRemovedInSrcDpn = true;
1109 deleteSmacAndDmacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, flowTx);
1110 } else if (isDpnPresent(dstDpId)) {
1112 .removeFlow(flowTx, dstDpId,
1113 MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE, getKnownDynamicmacFlowRef(
1114 NwConstants.ELAN_DMAC_TABLE, dstDpId, srcdpId, macAddress, elanTag)));
1115 LOG.debug("Dmac flow entry deleted for elan:{}, logical interface port:{} and mac address:{} on dpn:{}",
1116 elanInstanceName, interfaceInfo.getPortName(), macAddress, dstDpId);
1118 return isFlowsRemovedInSrcDpn;
1121 private void deleteSmacAndDmacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1122 boolean deleteSmac, TypedReadWriteTransaction<Configuration> flowTx)
1123 throws ExecutionException, InterruptedException {
1124 String elanInstanceName = elanInfo.getElanInstanceName();
1125 long ifTag = interfaceInfo.getInterfaceTag();
1126 Uint64 srcdpId = interfaceInfo.getDpId();
1127 Uint32 elanTag = elanInfo.getElanTag();
1130 .removeFlow(flowTx, srcdpId,
1131 MDSALUtil.buildFlow(NwConstants.ELAN_SMAC_TABLE, getKnownDynamicmacFlowRef(
1132 NwConstants.ELAN_SMAC_TABLE, srcdpId, ifTag, macAddress, elanTag)));
1134 mdsalManager.removeFlow(flowTx, srcdpId,
1135 MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE,
1136 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, srcdpId, ifTag, macAddress, elanTag)));
1137 LOG.debug("All the required flows deleted for elan:{}, logical Interface port:{} and MAC address:{} on dpn:{}",
1138 elanInstanceName, interfaceInfo.getPortName(), macAddress, srcdpId);
1142 * Updates the Elan information in the Operational DS. It also updates the
1143 * ElanInstance in the Config DS by setting the adquired elanTag.
1147 * @param elanInstanceAdded
1148 * the elan instance added
1149 * @param elanInterfaces
1150 * the elan interfaces
1154 * @return the updated ELAN instance.
1156 public static ElanInstance updateOperationalDataStore(IdManagerService idManager,
1157 ElanInstance elanInstanceAdded, List<String> elanInterfaces, TypedWriteTransaction<Configuration> confTx,
1158 TypedWriteTransaction<Operational> operTx) {
1159 String elanInstanceName = elanInstanceAdded.getElanInstanceName();
1160 Uint32 elanTag = elanInstanceAdded.getElanTag();
1161 if (elanTag == null || elanTag.longValue() == 0L) {
1162 elanTag = retrieveNewElanTag(idManager, elanInstanceName);
1164 Elan elanInfo = new ElanBuilder().setName(elanInstanceName).setElanInterfaces(elanInterfaces)
1165 .withKey(new ElanKey(elanInstanceName)).build();
1167 // Add the ElanState in the elan-state operational data-store
1168 operTx.put(getElanInstanceOperationalDataPath(elanInstanceName), elanInfo, CREATE_MISSING_PARENTS);
1170 // Add the ElanMacTable in the elan-mac-table operational data-store
1171 MacTable elanMacTable = new MacTableBuilder().withKey(new MacTableKey(elanInstanceName)).build();
1172 operTx.put(getElanMacTableOperationalDataPath(elanInstanceName), elanMacTable, CREATE_MISSING_PARENTS);
1174 ElanTagNameBuilder elanTagNameBuilder = new ElanTagNameBuilder().setElanTag(elanTag)
1175 .withKey(new ElanTagNameKey(elanTag)).setName(elanInstanceName);
1176 Uint32 etreeLeafTag = Uint32.valueOf(0L);
1177 if (isEtreeInstance(elanInstanceAdded)) {
1178 etreeLeafTag = retrieveNewElanTag(idManager,elanInstanceName + ElanConstants
1180 EtreeLeafTagName etreeLeafTagName = new EtreeLeafTagNameBuilder()
1181 .setEtreeLeafTag(new EtreeLeafTag(etreeLeafTag)).build();
1182 elanTagNameBuilder.addAugmentation(EtreeLeafTagName.class, etreeLeafTagName);
1183 addTheLeafTagAsElanTag(elanInstanceName, etreeLeafTag, operTx);
1185 ElanTagName elanTagName = elanTagNameBuilder.build();
1187 // Add the ElanTag to ElanName in the elan-tag-name Operational
1189 operTx.put(getElanInfoEntriesOperationalDataPath(elanTag), elanTagName);
1191 // Updates the ElanInstance Config DS by setting the just acquired
1193 ElanInstanceBuilder elanInstanceBuilder = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
1194 .setDescription(elanInstanceAdded.getDescription())
1195 .setMacTimeout(elanInstanceAdded.getMacTimeout() == null
1196 ? Uint32.valueOf(ElanConstants.DEFAULT_MAC_TIME_OUT) : elanInstanceAdded.getMacTimeout())
1197 .withKey(elanInstanceAdded.key()).setElanTag(elanTag);
1198 if (isEtreeInstance(elanInstanceAdded)) {
1199 EtreeInstance etreeInstance = new EtreeInstanceBuilder().setEtreeLeafTagVal(new EtreeLeafTag(etreeLeafTag))
1201 elanInstanceBuilder.addAugmentation(EtreeInstance.class, etreeInstance);
1203 ElanInstance elanInstanceWithTag = elanInstanceBuilder.build();
1204 LOG.trace("Updated elan Operational DS for elan: {} with elanTag: {} and interfaces: {}", elanInstanceName,
1205 elanTag, elanInterfaces);
1206 confTx.merge(ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstanceWithTag,
1207 CREATE_MISSING_PARENTS);
1208 return elanInstanceWithTag;
1211 private static void addTheLeafTagAsElanTag(String elanInstanceName,Uint32 etreeLeafTag,
1212 TypedWriteTransaction<Operational> tx) {
1213 ElanTagName etreeTagAsElanTag = new ElanTagNameBuilder().setElanTag(etreeLeafTag)
1214 .withKey(new ElanTagNameKey(etreeLeafTag)).setName(elanInstanceName).build();
1215 tx.put(getElanInfoEntriesOperationalDataPath(etreeLeafTag), etreeTagAsElanTag);
1218 private static boolean isEtreeInstance(ElanInstance elanInstanceAdded) {
1219 return elanInstanceAdded.augmentation(EtreeInstance.class) != null;
1222 public boolean isDpnPresent(Uint64 dpnId) {
1223 String dpn = String.format("%s:%s", "openflow", dpnId);
1224 NodeId nodeId = new NodeId(dpn);
1226 InstanceIdentifier<Node> node = InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(nodeId))
1228 return read(broker, LogicalDatastoreType.CONFIGURATION, node).isPresent();
1231 public static String getElanServiceName(String elanName, String interfaceName) {
1232 return "elan." + elanName + interfaceName;
1235 public static BoundServices getBoundServices(String serviceName, short servicePriority, int flowPriority,
1236 Uint64 cookie, List<Instruction> instructions) {
1237 StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookie).setFlowPriority(flowPriority)
1238 .setInstruction(instructions);
1239 return new BoundServicesBuilder().withKey(new BoundServicesKey(servicePriority)).setServiceName(serviceName)
1240 .setServicePriority(servicePriority).setServiceType(ServiceTypeFlowBased.class)
1241 .addAugmentation(StypeOpenflow.class, augBuilder.build()).build();
1244 public static InstanceIdentifier<BoundServices> buildServiceId(String interfaceName, short serviceIndex) {
1245 return InstanceIdentifier.builder(ServiceBindings.class)
1246 .child(ServicesInfo.class, new ServicesInfoKey(interfaceName, ServiceModeIngress.class))
1247 .child(BoundServices.class, new BoundServicesKey(serviceIndex)).build();
1250 public static List<MatchInfo> getTunnelMatchesForServiceId(Uint32 elanTag) {
1251 List<MatchInfo> mkMatches = new ArrayList<>();
1252 // Matching metadata
1253 mkMatches.add(new MatchTunnelId(Uint64.valueOf(elanTag.longValue())));
1258 public void removeTerminatingServiceAction(Uint64 destDpId, int serviceId) {
1259 RemoveTerminatingServiceActionsInput input = new RemoveTerminatingServiceActionsInputBuilder()
1260 .setDpnId(destDpId).setServiceId(serviceId).build();
1261 Future<RpcResult<RemoveTerminatingServiceActionsOutput>> futureObject =
1262 itmRpcService.removeTerminatingServiceActions(input);
1264 RpcResult<RemoveTerminatingServiceActionsOutput> result = futureObject.get();
1265 if (result.isSuccessful()) {
1266 LOG.debug("Successfully completed removeTerminatingServiceActions for ELAN with serviceId {} on "
1267 + "dpn {}", serviceId, destDpId);
1269 LOG.debug("Failure in removeTerminatingServiceAction RPC call for ELAN with serviceId {} on "
1270 + "dpn {}", serviceId, destDpId);
1272 } catch (InterruptedException | ExecutionException e) {
1273 LOG.error("Error in RPC call removeTerminatingServiceActions for ELAN with serviceId {} on "
1274 + "dpn {}", serviceId, destDpId, e);
1279 * Gets the external tunnel.
1281 * @param sourceDevice
1283 * @param destinationDevice
1284 * the destination device
1285 * @param datastoreType
1286 * the datastore type
1287 * @return the external tunnel
1290 public ExternalTunnel getExternalTunnel(String sourceDevice, String destinationDevice,
1291 LogicalDatastoreType datastoreType) {
1292 Class<? extends TunnelTypeBase> tunType = TunnelTypeVxlan.class;
1293 InstanceIdentifier<ExternalTunnel> iid = InstanceIdentifier.builder(ExternalTunnelList.class)
1294 .child(ExternalTunnel.class, new ExternalTunnelKey(destinationDevice, sourceDevice, tunType)).build();
1295 return read(broker, datastoreType, iid).orNull();
1299 * Gets the external tunnel.
1301 * @param interfaceName
1302 * the interface name
1303 * @param datastoreType
1304 * the datastore type
1305 * @return the external tunnel
1308 public ExternalTunnel getExternalTunnel(String interfaceName, LogicalDatastoreType datastoreType) {
1309 ExternalTunnel externalTunnel = null;
1310 List<ExternalTunnel> externalTunnels = getAllExternalTunnels(datastoreType);
1311 for (ExternalTunnel tunnel : externalTunnels) {
1312 if (StringUtils.equalsIgnoreCase(interfaceName, tunnel.getTunnelInterfaceName())) {
1313 externalTunnel = tunnel;
1317 return externalTunnel;
1321 * Gets the all external tunnels.
1323 * @param datastoreType
1324 * the data store type
1325 * @return the all external tunnels
1327 public List<ExternalTunnel> getAllExternalTunnels(LogicalDatastoreType datastoreType) {
1328 InstanceIdentifier<ExternalTunnelList> iid = InstanceIdentifier.builder(ExternalTunnelList.class).build();
1329 return read(broker, datastoreType, iid).toJavaUtil().map(ExternalTunnelList::getExternalTunnel).orElse(
1333 public static List<MatchInfo> buildMatchesForElanTagShFlagAndDstMac(long elanTag, boolean shFlag, String macAddr) {
1334 List<MatchInfo> mkMatches = new ArrayList<>();
1336 new MatchMetadata(getElanMetadataLabel(elanTag, shFlag), MetaDataUtil.METADATA_MASK_SERVICE_SH_FLAG));
1337 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddr)));
1342 * Checks if is interface operational.
1344 * @param interfaceName
1345 * the interface name
1348 * @return true, if is interface operational
1350 public static boolean isInterfaceOperational(String interfaceName, DataBroker dataBroker) {
1351 if (StringUtils.isBlank(interfaceName)) {
1354 Interface ifState = getInterfaceStateFromOperDS(interfaceName, dataBroker);
1355 if (ifState == null) {
1358 return ifState.getOperStatus() == OperStatus.Up && ifState.getAdminStatus() == AdminStatus.Up;
1362 * Gets the interface state from operational ds.
1364 * @param interfaceName
1365 * the interface name
1368 * @return the interface state from oper ds
1370 public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
1371 .@Nullable Interface getInterfaceStateFromOperDS(String interfaceName, DataBroker dataBroker) {
1372 return MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL,
1373 createInterfaceStateInstanceIdentifier(interfaceName)).orNull();
1377 * Creates the interface state instance identifier.
1379 * @param interfaceName
1380 * the interface name
1381 * @return the instance identifier
1383 public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1384 .ietf.interfaces.rev140508.interfaces.state.Interface> createInterfaceStateInstanceIdentifier(
1385 String interfaceName) {
1386 InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1387 .ietf.interfaces.rev140508.interfaces.state.Interface> idBuilder = InstanceIdentifier
1388 .builder(InterfacesState.class)
1389 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1390 .ietf.interfaces.rev140508.interfaces.state.Interface.class,
1391 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1392 .ietf.interfaces.rev140508.interfaces.state.InterfaceKey(
1394 return idBuilder.build();
1398 public static ListenableFuture<Void> waitForTransactionToComplete(ListenableFuture<Void> future) {
1401 } catch (InterruptedException | ExecutionException e) {
1402 // NETVIRT-1215: Do not log.error() here, only debug(); but callers *MUST* @CheckReturnValue
1403 LOG.debug("Error writing to datastore", e);
1408 public static boolean isVxlan(@Nullable ElanInstance elanInstance) {
1409 return elanInstance != null && elanInstance.getSegmentType() != null
1410 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1411 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId().toJava() != 0;
1414 private static boolean isVxlanSegment(@Nullable ElanInstance elanInstance) {
1415 if (elanInstance != null) {
1416 List<ElanSegments> elanSegments = elanInstance.getElanSegments();
1417 if (elanSegments != null) {
1418 for (ElanSegments segment : elanSegments) {
1419 if (segment != null && segment.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1420 && segment.getSegmentationId() != null
1421 && segment.getSegmentationId().longValue() != 0) {
1430 public static boolean isVxlanNetworkOrVxlanSegment(@Nullable ElanInstance elanInstance) {
1431 return isVxlan(elanInstance) || isVxlanSegment(elanInstance);
1434 public static Uint32 getVxlanSegmentationId(ElanInstance elanInstance) {
1435 Uint32 segmentationId = Uint32.ZERO;
1436 if (elanInstance == null) {
1437 return segmentationId;
1440 if (elanInstance.getSegmentType() != null
1441 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1442 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId().longValue() != 0) {
1443 segmentationId = elanInstance.getSegmentationId();
1445 for (ElanSegments segment: elanInstance.getElanSegments()) {
1446 if (segment != null && segment.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1447 && segment.getSegmentationId() != null
1448 && segment.getSegmentationId().longValue() != 0) {
1449 segmentationId = segment.getSegmentationId();
1453 return segmentationId;
1456 public static boolean isVlan(ElanInstance elanInstance) {
1457 return elanInstance != null && elanInstance.getSegmentType() != null
1458 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVlan.class)
1459 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId().toJava() != 0;
1462 public static boolean isFlat(ElanInstance elanInstance) {
1463 return elanInstance != null && elanInstance.getSegmentType() != null
1464 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeFlat.class);
1467 public void addDmacRedirectToDispatcherFlows(Uint32 elanTag, String displayName,
1468 String macAddress, List<Uint64> dpnIds) {
1469 for (Uint64 dpId : dpnIds) {
1470 LoggingFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
1471 tx -> mdsalManager.addFlow(tx, buildDmacRedirectToDispatcherFlow(dpId, macAddress, displayName,
1473 "Error adding DMAC redirect to dispatcher flows");
1477 public void removeDmacRedirectToDispatcherFlows(Uint32 elanTag, String macAddress, List<Uint64> dpnIds) {
1478 LoggingFutures.addErrorLogging(
1479 txRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.CONFIGURATION, tx -> {
1480 for (Uint64 dpId : dpnIds) {
1481 String flowId = getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, macAddress, elanTag);
1482 mdsalManager.removeFlow(tx, dpId, MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE, flowId));
1484 }), LOG, "Error removing DMAC redirect to dispatcher flows");
1487 public static FlowEntity buildDmacRedirectToDispatcherFlow(Uint64 dpId, String dstMacAddress,
1488 String displayName, Uint32 elanTag) {
1489 List<MatchInfo> matches = new ArrayList<>();
1490 matches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag.longValue()),
1491 MetaDataUtil.METADATA_MASK_SERVICE));
1492 matches.add(new MatchEthernetDestination(new MacAddress(dstMacAddress)));
1493 List<InstructionInfo> instructions = new ArrayList<>();
1494 List<ActionInfo> actions = new ArrayList<>();
1495 actions.add(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
1497 instructions.add(new InstructionApplyActions(actions));
1498 String flowId = getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, dstMacAddress, elanTag);
1499 return MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 20, displayName, 0, 0,
1500 Uint64.valueOf(ElanConstants.COOKIE_ELAN_KNOWN_DMAC.longValue() + elanTag.longValue()),
1501 matches, instructions);
1505 public String getExternalElanInterface(String elanInstanceName, Uint64 dpnId) {
1506 DpnInterfaces dpnInterfaces = getElanInterfaceInfoByElanDpn(elanInstanceName, dpnId);
1507 if (dpnInterfaces == null || dpnInterfaces.getInterfaces() == null) {
1508 LOG.trace("Elan {} does not have interfaces in DPN {}", elanInstanceName, dpnId);
1512 for (String dpnInterface : dpnInterfaces.getInterfaces()) {
1513 if (interfaceManager.isExternalInterface(dpnInterface)) {
1514 return dpnInterface;
1518 LOG.trace("Elan {} does not have any external interace attached to DPN {}", elanInstanceName, dpnId);
1522 public static Acquired lockElanMacDPN(long elanTag, String macAddress, Uint64 dpnId) {
1523 return ELAN_LOCKS.acquire(new ElanLockName(elanTag, macAddress, dpnId));
1526 public static List<ListenableFuture<Void>>
1527 returnFailedListenableFutureIfTransactionCommitFailedExceptionCauseOrElseThrow(RuntimeException exception) {
1529 Throwable cause = exception.getCause();
1530 if (cause != null && cause instanceof TransactionCommitFailedException) {
1531 return Collections.singletonList(Futures.immediateFailedFuture(cause));
1537 public static List<PhysAddress> getPhysAddress(List<String> macAddress) {
1538 Preconditions.checkNotNull(macAddress, "macAddress cannot be null");
1539 List<PhysAddress> physAddresses = new ArrayList<>();
1540 for (String mac : macAddress) {
1541 physAddresses.add(new PhysAddress(mac));
1543 return physAddresses;
1546 public static List<StaticMacEntries> getStaticMacEntries(List<String> staticMacAddresses) {
1547 if (isEmpty(staticMacAddresses)) {
1548 return Collections.EMPTY_LIST;
1550 StaticMacEntriesBuilder staticMacEntriesBuilder = new StaticMacEntriesBuilder();
1551 List<StaticMacEntries> staticMacEntries = new ArrayList<>();
1552 List<PhysAddress> physAddressList = getPhysAddress(staticMacAddresses);
1553 for (PhysAddress physAddress : physAddressList) {
1554 staticMacEntries.add(staticMacEntriesBuilder.setMacAddress(physAddress).build());
1556 return staticMacEntries;
1559 public static InstanceIdentifier<StaticMacEntries> getStaticMacEntriesCfgDataPathIdentifier(String interfaceName,
1560 String macAddress) {
1561 return InstanceIdentifier.builder(ElanInterfaces.class)
1562 .child(ElanInterface.class, new ElanInterfaceKey(interfaceName)).child(StaticMacEntries.class,
1563 new StaticMacEntriesKey(new PhysAddress(macAddress))).build();
1566 public static <T> List<T> diffOf(List<T> orig, List<T> updated) {
1567 if (isEmpty(orig)) {
1568 return Collections.EMPTY_LIST;
1570 List<T> diff = Lists.newArrayList(orig);
1571 if (isNotEmpty(updated)) {
1572 diff.removeAll(updated);
1577 public static boolean isEmpty(Collection collection) {
1578 return collection == null || collection.isEmpty();
1581 public static boolean isNotEmpty(Collection collection) {
1582 return !isEmpty(collection);
1585 public Optional<IpAddress> getSourceIpAddress(Ethernet ethernet) {
1586 Optional<IpAddress> srcIpAddress = Optional.absent();
1587 if (ethernet.getPayload() == null) {
1588 return srcIpAddress;
1590 byte[] ipAddrBytes = null;
1591 if (ethernet.getPayload() instanceof IPv4) {
1592 IPv4 ipv4 = (IPv4) ethernet.getPayload();
1593 ipAddrBytes = Ints.toByteArray(ipv4.getSourceAddress());
1594 } else if (ethernet.getPayload() instanceof ARP) {
1595 ipAddrBytes = ((ARP) ethernet.getPayload()).getSenderProtocolAddress();
1597 if (ipAddrBytes != null) {
1598 String ipAddr = NWUtil.toStringIpAddress(ipAddrBytes);
1599 return Optional.of(IpAddressBuilder.getDefaultInstance(ipAddr));
1601 return srcIpAddress;
1604 public List<MacEntry> getElanMacEntries(String elanName) {
1605 MacTable macTable = getElanMacTable(elanName);
1606 if (macTable == null) {
1609 return macTable.getMacEntry();
1612 public boolean isTunnelInLogicalGroup(String interfaceName) {
1613 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1614 .ietf.interfaces.rev140508.interfaces.Interface configIface =
1615 interfaceManager.getInterfaceInfoFromConfigDataStore(interfaceName);
1616 if (configIface == null) {
1617 configIface = iitmProvider.getInterface(interfaceName);
1619 if (configIface == null) {
1622 IfTunnel ifTunnel = configIface.augmentation(IfTunnel.class);
1623 if (ifTunnel != null && ifTunnel.getTunnelInterfaceType().isAssignableFrom(TunnelTypeVxlan.class)) {
1624 ParentRefs refs = configIface.augmentation(ParentRefs.class);
1625 return refs != null && !Strings.isNullOrEmpty(refs.getParentInterface());
1630 public static InstanceIdentifier<Flow> getFlowIid(Flow flow, Uint64 dpnId) {
1631 FlowKey flowKey = new FlowKey(new FlowId(flow.getId()));
1632 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId nodeId =
1633 new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId("openflow:" + dpnId);
1634 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node nodeDpn =
1635 new NodeBuilder().setId(nodeId).withKey(new NodeKey(nodeId)).build();
1636 return InstanceIdentifier.builder(Nodes.class)
1637 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class,
1638 nodeDpn.key()).augmentation(FlowCapableNode.class)
1639 .child(Table.class, new TableKey(flow.getTableId())).child(Flow.class, flowKey).build();
1642 public static String getElanInterfaceJobKey(String interfaceName) {
1643 return "elaninterface-" + interfaceName;
1646 public void removeArpResponderFlow(Uint64 dpnId, String ingressInterfaceName, String ipAddress,
1648 LOG.info("Removing the ARP responder flow on DPN {} of Interface {} with IP {}", dpnId, ingressInterfaceName,
1650 LoggingFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.CONFIGURATION,
1651 tx -> mdsalManager.removeFlow(tx, dpnId, ArpResponderUtil.getFlowId(lportTag, ipAddress),
1652 NwConstants.ARP_RESPONDER_TABLE)), LOG, "Error removing ARP responder flow");
1656 public static String getRouterPordIdFromElanInstance(DataBroker dataBroker, String elanInstanceName) {
1657 Optional<Subnetmaps> subnetMapsData =
1658 read(dataBroker, LogicalDatastoreType.CONFIGURATION, buildSubnetMapsWildCardPath());
1659 if (subnetMapsData.isPresent()) {
1660 List<Subnetmap> subnetMapList = subnetMapsData.get().getSubnetmap();
1661 if (subnetMapList != null && !subnetMapList.isEmpty()) {
1662 for (Subnetmap subnet : subnetMapList) {
1663 if (subnet.getNetworkId().getValue().equals(elanInstanceName)) {
1664 if (subnet.getRouterInterfacePortId() != null) {
1665 return subnet.getRouterInterfacePortId().getValue();
1674 protected Node buildDpnNode(Uint64 dpnId) {
1675 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId nodeId =
1676 new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819
1677 .NodeId("openflow:" + dpnId);
1678 Node nodeDpn = new NodeBuilder().setId(nodeId).withKey(new NodeKey(nodeId)).build();
1682 public void syncUpdateGroup(Uint64 dpnId, Group newGroup, long delayTime,
1683 TypedWriteTransaction<Datastore.Configuration> confTx) {
1684 Node nodeDpn = buildDpnNode(dpnId);
1685 long groupIdInfo = newGroup.getGroupId().getValue().toJava();
1686 GroupKey groupKey = new GroupKey(new GroupId(groupIdInfo));
1687 InstanceIdentifier<Group> groupInstanceId = InstanceIdentifier.builder(Nodes.class)
1688 .child(Node.class, nodeDpn.key()).augmentation(FlowCapableNode.class)
1689 .child(Group.class, groupKey).build();
1690 LOG.trace("Performing merge operation for remote BC group for node {} with group {}", nodeDpn, newGroup);
1691 Optional<Group> existingGroupOpt = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION, groupInstanceId);
1692 if (!existingGroupOpt.isPresent()) {
1693 LOG.debug("Group {} doesn't exist. Performing syncInstall", groupIdInfo);
1694 mdsalManager.addGroup(confTx, dpnId, newGroup);
1697 Buckets existingGroup = existingGroupOpt.get().getBuckets();
1698 if (existingGroup == null) {
1699 LOG.debug("Bucket doesn't exist for group {}. Performing syncInstall", groupIdInfo);
1700 mdsalManager.addGroup(confTx, dpnId, newGroup);
1703 if (newGroup.getBuckets() == null) {
1704 LOG.debug("Buckets are not sent for group {}. Skipping merge operation", groupIdInfo);
1707 List<Bucket> newBuckets = newGroup.getBuckets().getBucket();
1708 List<Bucket> existingBuckets = existingGroup.getBucket();
1709 Set<Bucket> toMergeBucketsWithoutId = new LinkedHashSet<>();
1711 existingBuckets.stream()
1712 .map(TO_BUCKET_WITHOUT_ID)
1713 .forEach(bucketWithoutId -> toMergeBucketsWithoutId.add(bucketWithoutId));
1716 .map(TO_BUCKET_WITHOUT_ID)
1717 .forEach(bucketWithoutId -> toMergeBucketsWithoutId.add(bucketWithoutId));
1719 if (toMergeBucketsWithoutId.size() == existingBuckets.size()) {
1720 //no new buckets got added
1721 //size matched and no extra buckets in existing buckets , rewrite the group
1722 LOG.debug("Buckets did not change group {}. Skipping merge operation", groupIdInfo);
1725 LOG.debug("Old group buckets size {} New group buckets size {} Dpn id {} Group id {} ",
1726 existingBuckets.size(), toMergeBucketsWithoutId.size(), dpnId, groupIdInfo);
1727 AtomicLong bucketIdValue = new AtomicLong(-1);
1728 //Change the bucket id of existing buckets
1729 List<Bucket> bucketsToBeAdded = toMergeBucketsWithoutId.stream()
1730 .map(bucketWithoutId -> TO_BUCKET_WITH_ID.apply(bucketWithoutId, bucketIdValue))
1731 .collect(Collectors.toList());
1733 Group group = MDSALUtil.buildGroup(newGroup.getGroupId().getValue().toJava(), newGroup.getGroupName(),
1734 GroupTypes.GroupAll, MDSALUtil.buildBucketLists(bucketsToBeAdded));
1735 mdsalManager.addGroup(confTx, dpnId, group);
1736 LOG.trace("Installed remote BC group for node {} with group {}", nodeDpn, group);
1739 static InstanceIdentifier<Subnetmaps> buildSubnetMapsWildCardPath() {
1740 return InstanceIdentifier.create(Subnetmaps.class);
1743 public static InstanceIdentifier<Group> getGroupInstanceid(Uint64 dpnId, long groupId) {
1744 return InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId)))
1745 .augmentation(FlowCapableNode.class).child(Group.class, new GroupKey(new GroupId(groupId))).build();
1748 public static String getBcGroupUpdateKey(String elanName) {
1749 return "bc.group.update." + elanName;