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 java.util.Collections.emptyMap;
12 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Strings;
16 import com.google.common.collect.Lists;
17 import com.google.common.primitives.Ints;
18 import com.google.common.util.concurrent.FluentFuture;
19 import com.google.common.util.concurrent.FutureCallback;
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import com.google.common.util.concurrent.MoreExecutors;
23 import edu.umd.cs.findbugs.annotations.CheckReturnValue;
24 import java.math.BigInteger;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.LinkedHashSet;
30 import java.util.List;
31 import java.util.Locale;
33 import java.util.Objects;
34 import java.util.Optional;
36 import java.util.concurrent.ExecutionException;
37 import java.util.concurrent.Future;
38 import java.util.concurrent.atomic.AtomicLong;
39 import java.util.function.BiFunction;
40 import java.util.function.Function;
41 import java.util.stream.Collectors;
42 import javax.inject.Inject;
43 import javax.inject.Singleton;
44 import org.apache.commons.lang3.StringUtils;
45 import org.eclipse.jdt.annotation.NonNull;
46 import org.eclipse.jdt.annotation.Nullable;
47 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
48 import org.opendaylight.genius.infra.Datastore;
49 import org.opendaylight.genius.infra.Datastore.Configuration;
50 import org.opendaylight.genius.infra.Datastore.Operational;
51 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
52 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
53 import org.opendaylight.genius.infra.TypedReadTransaction;
54 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
55 import org.opendaylight.genius.infra.TypedWriteTransaction;
56 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
57 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
58 import org.opendaylight.genius.itm.api.IITMProvider;
59 import org.opendaylight.genius.itm.globals.ITMConstants;
60 import org.opendaylight.genius.mdsalutil.ActionInfo;
61 import org.opendaylight.genius.mdsalutil.FlowEntity;
62 import org.opendaylight.genius.mdsalutil.FlowEntityBuilder;
63 import org.opendaylight.genius.mdsalutil.InstructionInfo;
64 import org.opendaylight.genius.mdsalutil.MDSALUtil;
65 import org.opendaylight.genius.mdsalutil.MatchInfo;
66 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
67 import org.opendaylight.genius.mdsalutil.NWUtil;
68 import org.opendaylight.genius.mdsalutil.NwConstants;
69 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
70 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
71 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
72 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
73 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetDestination;
74 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetSource;
75 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
76 import org.opendaylight.genius.mdsalutil.matches.MatchTunnelId;
77 import org.opendaylight.genius.mdsalutil.packet.ARP;
78 import org.opendaylight.genius.mdsalutil.packet.Ethernet;
79 import org.opendaylight.genius.mdsalutil.packet.IPv4;
80 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
81 import org.opendaylight.infrautils.utils.concurrent.NamedLocks;
82 import org.opendaylight.infrautils.utils.concurrent.NamedSimpleReentrantLock.Acquired;
83 import org.opendaylight.mdsal.binding.api.DataBroker;
84 import org.opendaylight.mdsal.binding.api.ReadTransaction;
85 import org.opendaylight.mdsal.binding.api.WriteTransaction;
86 import org.opendaylight.mdsal.common.api.CommitInfo;
87 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
88 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
89 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderUtil;
90 import org.opendaylight.netvirt.elan.cache.ElanInterfaceCache;
91 import org.opendaylight.netvirt.elan.internal.ElanGroupCache;
92 import org.opendaylight.netvirt.elanmanager.api.ElanHelper;
93 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
94 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
95 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
96 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
97 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
98 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
99 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
100 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.IfIndexesInterfaceMap;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterfaceKey;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefs;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceInput;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceInputBuilder;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceOutput;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceBindings;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeIngress;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceTypeFlowBased;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflow;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflowBuilder;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfoKey;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesBuilder;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesKey;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.ExternalTunnelList;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnel;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnelKey;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.RemoveTerminatingServiceActionsInput;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.RemoveTerminatingServiceActionsInputBuilder;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.RemoveTerminatingServiceActionsOutput;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
149 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
153 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
154 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
155 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
156 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
157 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
158 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
159 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstanceBuilder;
160 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface;
161 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface.EtreeInterfaceType;
162 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTag;
163 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTagName;
164 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTagNameBuilder;
165 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
166 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanForwardingTables;
167 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaceForwardingEntries;
168 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
169 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanState;
170 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanTagNameMap;
171 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeFlat;
172 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeVlan;
173 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeVxlan;
174 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
175 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMacKey;
176 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
177 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
178 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
179 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfacesKey;
180 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTable;
181 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTableBuilder;
182 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTableKey;
183 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
184 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
185 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.elan.instance.ElanSegments;
186 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.elan.instance.ElanSegmentsKey;
187 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
188 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
189 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntries;
190 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntriesBuilder;
191 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntriesKey;
192 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
193 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.ElanBuilder;
194 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.ElanKey;
195 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
196 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagNameBuilder;
197 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagNameKey;
198 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
199 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
200 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
201 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
202 import org.opendaylight.yangtools.yang.binding.DataObject;
203 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
204 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
205 import org.opendaylight.yangtools.yang.common.RpcResult;
206 import org.opendaylight.yangtools.yang.common.Uint32;
207 import org.opendaylight.yangtools.yang.common.Uint64;
208 import org.slf4j.Logger;
209 import org.slf4j.LoggerFactory;
212 public class ElanUtils {
213 private static final class ElanLockName {
214 private final String macAddress;
215 private final Uint64 dpnId;
216 private final long elanTag;
218 ElanLockName(long elanTag, String macAddress, Uint64 dpnId) {
219 this.elanTag = elanTag;
220 this.macAddress = macAddress;
225 public int hashCode() {
226 return 31 * Long.hashCode(elanTag) + Objects.hash(macAddress, dpnId);
230 public boolean equals(Object obj) {
234 if (!(obj instanceof ElanLockName)) {
237 final ElanLockName other = (ElanLockName) obj;
238 return elanTag == other.elanTag && Objects.equals(macAddress, other.macAddress)
239 && Objects.equals(dpnId, other.dpnId);
243 private static final Logger LOG = LoggerFactory.getLogger(ElanUtils.class);
244 private static final NamedLocks<ElanLockName> ELAN_LOCKS = new NamedLocks<>();
246 private final DataBroker broker;
247 private final ManagedNewTransactionRunner txRunner;
248 private final IMdsalApiManager mdsalManager;
249 private final OdlInterfaceRpcService interfaceManagerRpcService;
250 private final ItmRpcService itmRpcService;
251 private final IInterfaceManager interfaceManager;
252 private final ElanConfig elanConfig;
253 private final ElanItmUtils elanItmUtils;
254 private final ElanEtreeUtils elanEtreeUtils;
255 private final ElanInterfaceCache elanInterfaceCache;
256 private final IITMProvider iitmProvider;
257 private final ElanGroupCache elanGroupCache;
259 private static final Function<Bucket, Bucket> TO_BUCKET_WITHOUT_ID = (bucket) -> new BucketBuilder(bucket)
260 .setBucketId(new BucketId(0L))
263 private static final BiFunction<Bucket, AtomicLong, Bucket> TO_BUCKET_WITH_ID = (bucket, id)
264 -> new BucketBuilder(bucket)
265 .setBucketId(new BucketId(id.incrementAndGet()))
268 public static final FutureCallback<CommitInfo> DEFAULT_CALLBACK = new FutureCallback<CommitInfo>() {
270 public void onSuccess(CommitInfo result) {
271 LOG.debug("Success in Datastore operation");
275 public void onFailure(Throwable error) {
276 LOG.error("Error in Datastore operation", error);
281 public ElanUtils(DataBroker dataBroker, IMdsalApiManager mdsalManager,
282 OdlInterfaceRpcService interfaceManagerRpcService, ItmRpcService itmRpcService, ElanConfig elanConfig,
283 IInterfaceManager interfaceManager, ElanEtreeUtils elanEtreeUtils, ElanItmUtils elanItmUtils,
284 ElanInterfaceCache elanInterfaceCache, IITMProvider iitmProvider, ElanGroupCache elanGroupCache) {
285 this.broker = dataBroker;
286 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
287 this.mdsalManager = mdsalManager;
288 this.interfaceManagerRpcService = interfaceManagerRpcService;
289 this.itmRpcService = itmRpcService;
290 this.interfaceManager = interfaceManager;
291 this.elanConfig = elanConfig;
292 this.elanEtreeUtils = elanEtreeUtils;
293 this.elanItmUtils = elanItmUtils;
294 this.elanInterfaceCache = elanInterfaceCache;
295 this.iitmProvider = iitmProvider;
296 this.elanGroupCache = elanGroupCache;
299 public final Boolean isOpenstackVniSemanticsEnforced() {
300 return elanConfig.isOpenstackVniSemanticsEnforced() != null
301 ? elanConfig.isOpenstackVniSemanticsEnforced() : false;
305 * Uses the IdManager to retrieve a brand new ElanTag.
311 * @return the integer
313 public static Uint32 retrieveNewElanTag(IdManagerService idManager, String idKey) {
315 AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(ElanConstants.ELAN_ID_POOL_NAME)
316 .setIdKey(idKey).build();
319 Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
320 RpcResult<AllocateIdOutput> rpcResult = result.get();
321 if (rpcResult.isSuccessful()) {
322 return rpcResult.getResult().getIdValue();
324 LOG.warn("RPC Call to Allocate Id returned with Errors {}", rpcResult.getErrors());
326 } catch (InterruptedException | ExecutionException e) {
327 LOG.warn("Exception when Allocating Id", e);
329 return Uint32.valueOf(0L);
332 public static void releaseId(IdManagerService idManager, String poolName, String idKey) {
333 ReleaseIdInput releaseIdInput = new ReleaseIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
334 LoggingFutures.addErrorLogging(idManager.releaseId(releaseIdInput), LOG, "Release Id");
340 * @deprecated Consider using {@link #read2(LogicalDatastoreType, InstanceIdentifier)} with proper exception
342 * @param broker dataBroker
343 * @param datastoreType Logical DataStore type
344 * @param path IID to read
345 * @param <T> T extends DataObject
346 * @return the read value T
349 @SuppressWarnings("checkstyle:IllegalCatch")
350 public static <T extends DataObject> Optional<T> read(@NonNull DataBroker broker,
351 LogicalDatastoreType datastoreType, InstanceIdentifier<T> path) {
352 try (ReadTransaction tx = broker.newReadOnlyTransaction()) {
353 return tx.read(datastoreType, path).get();
354 } catch (ExecutionException | InterruptedException e) {
355 throw new RuntimeException(e);
359 public <T extends DataObject> Optional<T> read2(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path)
360 throws InterruptedException, ExecutionException {
361 try (ReadTransaction tx = broker.newReadOnlyTransaction()) {
362 FluentFuture<Optional<T>> checkedFuture = tx.read(datastoreType, path);
363 return checkedFuture.get();
367 @SuppressWarnings("checkstyle:ForbidCertainMethod")
368 public static <T extends DataObject> void delete(DataBroker broker, LogicalDatastoreType datastoreType,
369 InstanceIdentifier<T> path) {
370 WriteTransaction tx = broker.newWriteOnlyTransaction();
371 tx.delete(datastoreType, path);
372 FluentFuture<? extends @NonNull CommitInfo> future = tx.commit();
373 future.addCallback(DEFAULT_CALLBACK, MoreExecutors.directExecutor());
376 public static InstanceIdentifier<ElanInterface> getElanInterfaceConfigurationDataPathId(String interfaceName) {
377 return InstanceIdentifier.builder(ElanInterfaces.class)
378 .child(ElanInterface.class, new ElanInterfaceKey(interfaceName)).build();
381 // elan-state Operational container
383 public static Elan getElanByName(DataBroker broker, String elanInstanceName) {
384 InstanceIdentifier<Elan> elanIdentifier = getElanInstanceOperationalDataPath(elanInstanceName);
386 return SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.OPERATIONAL,
387 elanIdentifier).orElse(null);
388 } catch (ExecutionException | InterruptedException e) {
389 LOG.error("getElanByName: Exception while reading elan-instance DS for the elan instance {}",
390 elanInstanceName, e);
396 public static Elan getElanByName(TypedReadTransaction<Operational> tx, String elanInstanceName)
397 throws ExecutionException, InterruptedException {
398 return tx.read(getElanInstanceOperationalDataPath(elanInstanceName)).get().orElse(null);
401 public static InstanceIdentifier<Elan> getElanInstanceOperationalDataPath(String elanInstanceName) {
402 return InstanceIdentifier.builder(ElanState.class).child(Elan.class, new ElanKey(elanInstanceName)).build();
405 // grouping of forwarding-entries
407 public MacEntry getInterfaceMacEntriesOperationalDataPath(String interfaceName, PhysAddress physAddress) {
408 InstanceIdentifier<MacEntry> existingMacEntryId = getInterfaceMacEntriesIdentifierOperationalDataPath(
409 interfaceName, physAddress);
410 return read(broker, LogicalDatastoreType.OPERATIONAL, existingMacEntryId).orElse(null);
414 public MacEntry getInterfaceMacEntriesOperationalDataPathFromId(TypedReadTransaction<Operational> tx,
415 InstanceIdentifier<MacEntry> identifier) throws ExecutionException, InterruptedException {
416 return tx.read(identifier).get().orElse(null);
419 public static InstanceIdentifier<MacEntry> getInterfaceMacEntriesIdentifierOperationalDataPath(String interfaceName,
420 PhysAddress physAddress) {
421 return InstanceIdentifier.builder(ElanInterfaceForwardingEntries.class)
422 .child(ElanInterfaceMac.class, new ElanInterfaceMacKey(interfaceName))
423 .child(MacEntry.class, new MacEntryKey(physAddress)).build();
427 // elan-forwarding-tables Operational container
428 public Optional<MacEntry> getMacEntryForElanInstance(String elanName, PhysAddress physAddress) {
429 InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanName, physAddress);
430 return read(broker, LogicalDatastoreType.OPERATIONAL, macId);
433 public Optional<MacEntry> getMacEntryForElanInstance(TypedReadTransaction<Operational> tx, String elanName,
434 PhysAddress physAddress) throws ExecutionException, InterruptedException {
435 InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanName, physAddress);
436 return tx.read(macId).get();
440 public MacEntry getMacEntryFromElanMacId(TypedReadTransaction<Operational> tx,
441 InstanceIdentifier<MacEntry> identifier) throws ExecutionException, InterruptedException {
442 return tx.read(identifier).get().orElse(null);
445 public static InstanceIdentifier<MacEntry> getMacEntryOperationalDataPath(String elanName,
446 PhysAddress physAddress) {
447 return InstanceIdentifier.builder(ElanForwardingTables.class).child(MacTable.class, new MacTableKey(elanName))
448 .child(MacEntry.class, new MacEntryKey(physAddress)).build();
451 public static InstanceIdentifier<MacTable> getElanMacTableOperationalDataPath(String elanName) {
452 return InstanceIdentifier.builder(ElanForwardingTables.class).child(MacTable.class, new MacTableKey(elanName))
456 // elan-interface-forwarding-entries Operational container
457 public ElanInterfaceMac getElanInterfaceMacByInterfaceName(String interfaceName) {
458 return getElanInterfaceMacByInterfaceName(broker, interfaceName);
462 public static ElanInterfaceMac getElanInterfaceMacByInterfaceName(DataBroker dataBroker, String interfaceName) {
463 InstanceIdentifier<ElanInterfaceMac> elanInterfaceId = getElanInterfaceMacEntriesOperationalDataPath(
465 return read(dataBroker, LogicalDatastoreType.OPERATIONAL, elanInterfaceId).orElse(null);
468 public static InstanceIdentifier<ElanInterfaceMac> getElanInterfaceMacEntriesOperationalDataPath(
469 String interfaceName) {
470 return InstanceIdentifier.builder(ElanInterfaceForwardingEntries.class)
471 .child(ElanInterfaceMac.class, new ElanInterfaceMacKey(interfaceName)).build();
475 * Returns the list of Interfaces that belong to an Elan on an specific DPN.
476 * Data retrieved from Elan's operational DS: elan-dpn-interfaces container.
478 * @param elanInstanceName
479 * name of the Elan to which the interfaces must belong to
481 * Id of the DPN where the interfaces are located
482 * @return the elan interface Info
485 public DpnInterfaces getElanInterfaceInfoByElanDpn(String elanInstanceName, Uint64 dpId) {
486 InstanceIdentifier<DpnInterfaces> elanDpnInterfacesId = getElanDpnInterfaceOperationalDataPath(elanInstanceName,
488 return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfacesId).orElse(null);
492 * Returns the InstanceIdentifier that points to the Interfaces of an Elan
493 * in a given DPN in the Operational DS. Data retrieved from Elans's
494 * operational DS: dpn-interfaces list
496 * @param elanInstanceName
497 * name of the Elan to which the interfaces must belong to
499 * Id of the DPN where the interfaces are located
500 * @return the elan dpn interface
502 public static InstanceIdentifier<DpnInterfaces> getElanDpnInterfaceOperationalDataPath(String elanInstanceName,
504 return InstanceIdentifier.builder(ElanDpnInterfaces.class)
505 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName))
506 .child(DpnInterfaces.class, new DpnInterfacesKey(dpId)).build();
509 // elan-tag-name-map Operational Container
511 public ElanTagName getElanInfoByElanTag(Uint32 elanTag) {
512 InstanceIdentifier<ElanTagName> elanId = getElanInfoEntriesOperationalDataPath(elanTag);
513 Optional<ElanTagName> existingElanInfo = read(broker,
514 LogicalDatastoreType.OPERATIONAL, elanId);
515 return existingElanInfo.orElse(null);
518 public static InstanceIdentifier<ElanTagName> getElanInfoEntriesOperationalDataPath(Uint32 elanTag) {
519 return InstanceIdentifier.builder(ElanTagNameMap.class).child(ElanTagName.class, new ElanTagNameKey(elanTag))
523 // interface-index-tag operational container
524 public Optional<IfIndexInterface> getInterfaceInfoByInterfaceTag(Uint32 interfaceTag) {
525 InstanceIdentifier<IfIndexInterface> interfaceId = getInterfaceInfoEntriesOperationalDataPath(interfaceTag);
526 return read(broker, LogicalDatastoreType.OPERATIONAL, interfaceId);
529 public static InstanceIdentifier<IfIndexInterface> getInterfaceInfoEntriesOperationalDataPath(Uint32 interfaceTag) {
530 return InstanceIdentifier.builder(IfIndexesInterfaceMap.class)
531 .child(IfIndexInterface.class,
532 new IfIndexInterfaceKey(Integer.valueOf(interfaceTag.intValue()))).build();
535 public static InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
536 return InstanceIdentifier.builder(ElanDpnInterfaces.class)
537 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
541 public ElanDpnInterfacesList getElanDpnInterfacesList(String elanName) {
542 InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanName);
543 return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId).orElse(null);
547 public ElanDpnInterfaces getElanDpnInterfacesList() {
548 InstanceIdentifier<ElanDpnInterfaces> elanDpnInterfaceId = InstanceIdentifier.builder(ElanDpnInterfaces.class)
550 return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId).orElse(null);
554 * This method is useful get all ELAN participating CSS dpIds to install
555 * program remote dmac entries and updating remote bc groups for tor
558 * @param elanInstanceName
559 * the elan instance name
560 * @return list of dpIds
563 public List<Uint64> getParticipatingDpnsInElanInstance(String elanInstanceName) {
564 List<Uint64> dpIds = new ArrayList<>();
565 InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanInstanceName);
566 Optional<ElanDpnInterfacesList> existingElanDpnInterfaces = read(broker,
567 LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId);
568 if (!existingElanDpnInterfaces.isPresent()) {
571 Map<DpnInterfacesKey, DpnInterfaces> dpnInterfaces = existingElanDpnInterfaces.get().nonnullDpnInterfaces();
572 for (DpnInterfaces dpnInterface : dpnInterfaces.values()) {
573 dpIds.add(dpnInterface.getDpId());
578 public static long getElanRemoteBroadCastGroupID(long elanTag) {
579 return ElanConstants.ELAN_GID_MIN + elanTag % ElanConstants.ELAN_GID_MIN * 2;
583 * Gets the elan mac table.
587 * @return the elan mac table
590 public MacTable getElanMacTable(String elanName) {
591 return getElanMacTable(broker, elanName);
595 public static MacTable getElanMacTable(DataBroker dataBroker, String elanName) {
596 InstanceIdentifier<MacTable> elanMacTableId = getElanMacTableOperationalDataPath(elanName);
597 return read(dataBroker, LogicalDatastoreType.OPERATIONAL, elanMacTableId).orElse(null);
600 public static long getElanLocalBCGId(long elanTag) {
601 return ElanConstants.ELAN_GID_MIN + (elanTag % ElanConstants.ELAN_GID_MIN * 2 - 1);
604 public static long getElanRemoteBCGId(long elanTag) {
605 return ElanConstants.ELAN_GID_MIN + elanTag % ElanConstants.ELAN_GID_MIN * 2;
608 public static long getEtreeLeafLocalBCGId(long etreeLeafTag) {
609 return ElanConstants.ELAN_GID_MIN + (etreeLeafTag % ElanConstants.ELAN_GID_MIN * 2 - 1);
612 public static long getEtreeLeafRemoteBCGId(long etreeLeafTag) {
613 return ElanConstants.ELAN_GID_MIN + etreeLeafTag % ElanConstants.ELAN_GID_MIN * 2;
616 public static Uint64 getElanMetadataLabel(long elanTag, boolean isSHFlagSet) {
617 int shBit = isSHFlagSet ? 1 : 0;
618 return Uint64.valueOf(BigInteger.valueOf(elanTag).shiftLeft(24).or(BigInteger.valueOf(shBit)));
622 * Setting SMAC, DMAC, UDMAC in this DPN and optionally in other DPNs.
626 * @param interfaceInfo
632 * @param configureRemoteFlows
633 * true if remote dmac flows should be configured as well
634 * @param writeFlowGroupTx
637 public void setupMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
638 long macTimeout, String macAddress, boolean configureRemoteFlows,
639 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
640 try (Acquired lock = lockElanMacDPN(elanInfo.getElanTag().toJava(), macAddress, interfaceInfo.getDpId())) {
641 setupKnownSmacFlow(elanInfo, interfaceInfo, macTimeout, macAddress, mdsalManager,
643 setupOrigDmacFlows(elanInfo, interfaceInfo, macAddress, configureRemoteFlows, mdsalManager,
648 public void setupDMacFlowOnRemoteDpn(ElanInstance elanInfo, InterfaceInfo interfaceInfo, Uint64 dstDpId,
649 String macAddress, TypedWriteTransaction<Configuration> writeFlowTx) {
650 String elanInstanceName = elanInfo.getElanInstanceName();
651 setupRemoteDmacFlow(dstDpId, interfaceInfo.getDpId(), interfaceInfo.getInterfaceTag(),
652 elanInfo.getElanTag(), macAddress, elanInstanceName, writeFlowTx,
653 interfaceInfo.getInterfaceName(), elanInfo);
654 LOG.info("Remote Dmac flow entry created for elan Name:{}, logical port Name:{} and"
655 + " mac address {} on dpn:{}", elanInstanceName, interfaceInfo.getPortName(),
656 macAddress, dstDpId);
660 * Inserts a Flow in SMAC table to state that the MAC has already been
663 private void setupKnownSmacFlow(ElanInstance elanInfo, InterfaceInfo interfaceInfo, long macTimeout,
664 String macAddress, IMdsalApiManager mdsalApiManager,
665 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
666 FlowEntity flowEntity = buildKnownSmacFlow(elanInfo, interfaceInfo, macTimeout, macAddress);
667 mdsalApiManager.addFlow(writeFlowGroupTx, flowEntity);
668 LOG.debug("Known Smac flow entry created for elan Name:{}, logical Interface port:{} and mac address:{}",
669 elanInfo.getElanInstanceName(), elanInfo.getDescription(), macAddress);
672 public FlowEntity buildKnownSmacFlow(ElanInstance elanInfo, InterfaceInfo interfaceInfo, long macTimeout,
674 int lportTag = interfaceInfo.getInterfaceTag();
675 // Matching metadata and eth_src fields
676 List<MatchInfo> mkMatches = new ArrayList<>();
677 mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanInfo.getElanTag().toJava(), lportTag),
678 ElanHelper.getElanMetadataMask()));
679 mkMatches.add(new MatchEthernetSource(new MacAddress(macAddress)));
680 List<InstructionInfo> mkInstructions = new ArrayList<>();
681 mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
682 Uint64 dpId = interfaceInfo.getDpId();
683 Uint32 elanTag = getElanTag(elanInfo, interfaceInfo);
684 return new FlowEntityBuilder()
686 .setTableId(NwConstants.ELAN_SMAC_TABLE)
687 .setFlowId(getKnownDynamicmacFlowRef(elanTag, macAddress))
689 .setFlowName(elanInfo.getDescription())
690 .setIdleTimeOut((int) macTimeout)
692 .setCookie(Uint64.valueOf(ElanConstants.COOKIE_ELAN_KNOWN_SMAC.longValue() + elanTag.longValue()))
693 .setMatchInfoList(mkMatches)
694 .setInstructionInfoList(mkInstructions)
696 // If Mac timeout is 0, the flow won't be deleted automatically, so no need to get notified
697 .setSendFlowRemFlag(macTimeout != 0)
701 private Uint32 getElanTag(ElanInstance elanInfo, InterfaceInfo interfaceInfo) {
702 EtreeInterface etreeInterface = elanInterfaceCache
703 .getEtreeInterface(interfaceInfo.getInterfaceName()).orElse(null);
704 if (etreeInterface == null || etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
705 return elanInfo.getElanTag();
707 EtreeInstance etreeInstance = elanInfo.augmentation(EtreeInstance.class);
708 if (etreeInstance == null) {
709 LOG.warn("EtreeInterface {} is connected to a non-Etree network: {}",
710 interfaceInfo.getInterfaceName(), elanInfo.getElanInstanceName());
711 return elanInfo.getElanTag();
713 return etreeInstance.getEtreeLeafTagVal().getValue();
719 * Installs a Flow in INTERNAL_TUNNEL_TABLE of the affected DPN that sends
720 * the packet through the specified interface if the tunnel_id matches the
721 * interface's lportTag.
723 * @param interfaceInfo
725 * @param mdsalApiManager
726 * the mdsal API manager
727 * @param writeFlowGroupTx
728 * the writeFLowGroup tx
730 public void setupTermDmacFlows(InterfaceInfo interfaceInfo, IMdsalApiManager mdsalApiManager,
731 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
732 Uint64 dpId = interfaceInfo.getDpId();
733 int lportTag = interfaceInfo.getInterfaceTag();
734 Flow flow = MDSALUtil.buildFlowNew(NwConstants.INTERNAL_TUNNEL_TABLE,
735 getIntTunnelTableFlowRef(NwConstants.INTERNAL_TUNNEL_TABLE, lportTag), 5,
736 String.format("%s:%d", "ITM Flow Entry ", lportTag), 0, 0,
737 Uint64.valueOf(ITMConstants.COOKIE_ITM.longValue() + lportTag),
738 getTunnelIdMatchForFilterEqualsLPortTag(lportTag),
739 getInstructionsInPortForOutGroup(interfaceInfo.getInterfaceName()));
740 mdsalApiManager.addFlow(writeFlowGroupTx, dpId, flow);
741 LOG.debug("Terminating service table flow entry created on dpn:{} for logical Interface port:{}", dpId,
742 interfaceInfo.getPortName());
746 * Constructs the FlowName for flows installed in the Internal Tunnel Table,
747 * consisting in tableId + elanTag.
753 * @return the Internal tunnel
755 public static String getIntTunnelTableFlowRef(short tableId, int elanTag) {
756 return new StringBuilder().append(tableId).append(elanTag).toString();
760 * Constructs the Matches that checks that the tunnel_id field contains a
764 * lportTag that must be checked against the tunnel_id field
765 * @return the list of match Info
767 public static List<MatchInfo> getTunnelIdMatchForFilterEqualsLPortTag(int lportTag) {
768 List<MatchInfo> mkMatches = new ArrayList<>();
770 mkMatches.add(new MatchTunnelId(Uint64.valueOf(lportTag)));
775 * Constructs the Instructions that take the packet over a given interface.
778 * Name of the interface where the packet must be sent over. It
779 * can be a local interface or a tunnel interface (internal or
781 * @return the Instruction
783 public Map<InstructionKey, Instruction> getInstructionsInPortForOutGroup(String ifName) {
784 int instructionsKey = 0;
785 Map<InstructionKey, Instruction> mkInstructions = new HashMap<>();
786 List<Action> actions = getEgressActionsForInterface(ifName, /* tunnelKey */ null);
788 mkInstructions.put(new InstructionKey(++instructionsKey), MDSALUtil.buildApplyActionsInstruction(actions));
789 return mkInstructions;
793 * Returns the list of Actions to be taken when sending the packet through
794 * an Elan interface. Note that this interface can refer to an ElanInterface
795 * where the Elan VM is attached to a DPN or an ITM tunnel interface where
796 * Elan traffic can be sent through. In this latter case, the tunnelKey is
797 * mandatory and it can hold serviceId for internal tunnels or the VNI for
804 * @return the egress actions for interface
806 @SuppressWarnings("checkstyle:IllegalCatch")
808 public List<Action> getEgressActionsForInterface(String ifName, @Nullable Long tunnelKey) {
809 List<Action> listAction = new ArrayList<>();
811 GetEgressActionsForInterfaceInput getEgressActionInput = new GetEgressActionsForInterfaceInputBuilder()
812 .setIntfName(ifName).setTunnelKey(tunnelKey).build();
813 Future<RpcResult<GetEgressActionsForInterfaceOutput>> result = interfaceManagerRpcService
814 .getEgressActionsForInterface(getEgressActionInput);
815 RpcResult<GetEgressActionsForInterfaceOutput> rpcResult = result.get();
816 if (!rpcResult.isSuccessful()) {
817 LOG.debug("RPC Call to Get egress actions for interface {} returned with Errors {}", ifName,
818 rpcResult.getErrors());
820 listAction = new ArrayList<Action>(rpcResult.getResult().nonnullAction().values());
822 } catch (Exception e) {
823 LOG.warn("Exception when egress actions for interface {}", ifName, e);
828 private void setupOrigDmacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
829 boolean configureRemoteFlows, IMdsalApiManager mdsalApiManager,
830 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
831 Uint64 dpId = interfaceInfo.getDpId();
832 String ifName = interfaceInfo.getInterfaceName();
833 long ifTag = interfaceInfo.getInterfaceTag();
834 String elanInstanceName = elanInfo.getElanInstanceName();
836 Uint32 elanTag = elanInfo.getElanTag();
838 setupLocalDmacFlow(elanTag, dpId, ifName, macAddress, elanInfo, mdsalApiManager, ifTag,
840 LOG.debug("Dmac flow entry created for elan Name:{}, logical port Name:{} mand mac address:{} "
841 + "on dpn:{}", elanInstanceName, interfaceInfo.getPortName(), macAddress, dpId);
843 if (!configureRemoteFlows) {
847 List<DpnInterfaces> elanDpns = getInvolvedDpnsInElan(elanInstanceName);
848 for (DpnInterfaces elanDpn : elanDpns) {
850 if (Objects.equals(elanDpn.getDpId(), dpId)) {
854 // Check for the Remote DPN present in Inventory Manager
855 if (!isDpnPresent(elanDpn.getDpId())) {
859 // For remote DPNs a flow is needed to indicate that packets of this ELAN going to this MAC need to be
860 // forwarded through the appropriate ITM tunnel
861 setupRemoteDmacFlow(elanDpn.getDpId(), // srcDpn (the remote DPN in this case)
862 dpId, // dstDpn (the local DPN)
863 interfaceInfo.getInterfaceTag(), // lportTag of the local interface
864 elanTag, // identifier of the Elan
865 macAddress, // MAC to be programmed in remote DPN
866 elanInstanceName, writeFlowGroupTx, ifName, elanInfo
868 LOG.debug("Remote Dmac flow entry created for elan Name:{}, logical port Name:{} and mac address:{} on"
869 + " dpn:{}", elanInstanceName, interfaceInfo.getPortName(), macAddress, elanDpn.getDpId());
872 // TODO: Make sure that the same is performed against the ElanDevices.
876 public List<DpnInterfaces> getInvolvedDpnsInElan(String elanName) {
877 return getElanDPNByName(elanName);
881 public List<DpnInterfaces> getElanDPNByName(String elanInstanceName) {
882 InstanceIdentifier<ElanDpnInterfacesList> elanIdentifier = getElanDpnOperationDataPath(elanInstanceName);
884 return new ArrayList<DpnInterfaces>((SingleTransactionDataBroker.syncReadOptional(broker,
885 LogicalDatastoreType.OPERATIONAL, elanIdentifier).map(ElanDpnInterfacesList::nonnullDpnInterfaces)
886 .orElse(emptyMap())).values());
887 } catch (ExecutionException | InterruptedException e) {
888 LOG.error("getElanDPNByName: Exception while reading elanDpnInterfaceList DS for the elan "
889 + "instance {}", elanInstanceName, e);
894 private void setupLocalDmacFlow(Uint32 elanTag, Uint64 dpId, String ifName, String macAddress,
895 ElanInstance elanInfo, IMdsalApiManager mdsalApiManager, long ifTag,
896 TypedWriteTransaction<Configuration> writeFlowGroupTx) {
897 Flow flowEntity = buildLocalDmacFlowEntry(elanTag, dpId, ifName, macAddress, elanInfo, ifTag);
898 mdsalApiManager.addFlow(writeFlowGroupTx, dpId, flowEntity);
899 installEtreeLocalDmacFlow(elanTag, dpId, ifName, macAddress, elanInfo, ifTag, writeFlowGroupTx);
902 private void installEtreeLocalDmacFlow(Uint32 elanTag, Uint64 dpId, String ifName, String macAddress,
903 ElanInstance elanInfo, long ifTag, TypedWriteTransaction<Configuration> writeFlowGroupTx) {
904 EtreeInterface etreeInterface = elanInterfaceCache.getEtreeInterface(ifName).orElse(null);
905 if (etreeInterface != null && etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
906 EtreeLeafTagName etreeTagName = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag.longValue());
907 if (etreeTagName == null) {
908 LOG.warn("Interface {} seems like it belongs to Etree but etreeTagName from elanTag {} is null",
911 Flow flowEntity = buildLocalDmacFlowEntry(etreeTagName.getEtreeLeafTag().getValue(), dpId,
912 ifName, macAddress, elanInfo, ifTag);
913 mdsalManager.addFlow(writeFlowGroupTx, dpId, flowEntity);
918 public static String getKnownDynamicmacFlowRef(Uint32 elanTag, String macAddress) {
919 return new StringBuffer().append(elanTag).append(macAddress.toLowerCase(Locale.getDefault())).toString();
922 public static String getKnownDynamicmacFlowRef(short elanDmacTable, Uint64 dpId, String extDeviceNodeId,
923 String dstMacAddress, long elanTag, boolean shFlag) {
924 return String.valueOf(elanDmacTable) + elanTag + dpId + extDeviceNodeId + dstMacAddress + shFlag;
928 * Builds the flow to be programmed in the DMAC table of the local DPN (that
929 * is, where the MAC is attached to). This flow consists in:
931 * <p>Match: + elanTag in metadata + packet goes to a MAC locally attached
932 * Actions: + optionally, pop-vlan + set-vlan-id + output to ifName's
949 public Flow buildLocalDmacFlowEntry(Uint32 elanTag, Uint64 dpId, String ifName, String macAddress,
950 ElanInstance elanInfo, long ifTag) {
952 List<MatchInfo> mkMatches = new ArrayList<>();
953 mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag.longValue()),
954 MetaDataUtil.METADATA_MASK_SERVICE));
955 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddress)));
957 Map<InstructionKey, Instruction> mkInstructions = new HashMap<>();
958 List<Action> actions = getEgressActionsForInterface(ifName, /* tunnelKey */ null);
959 mkInstructions.put(new InstructionKey(0), MDSALUtil.buildApplyActionsInstruction(actions));
960 Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE,
961 getKnownDynamicmacFlowRef(elanTag, macAddress), 20,
962 elanInfo.getElanInstanceName(), 0, 0,
963 Uint64.valueOf(ElanConstants.COOKIE_ELAN_KNOWN_DMAC.longValue() + elanTag.longValue()),
964 mkMatches, mkInstructions);
969 public void setupRemoteDmacFlow(Uint64 srcDpId, Uint64 destDpId, int lportTag, Uint32 elanTag,
970 String macAddress, String displayName, TypedWriteTransaction<Configuration> writeFlowGroupTx,
971 String interfaceName, ElanInstance elanInstance) {
972 if (interfaceManager.isExternalInterface(interfaceName)) {
973 LOG.debug("Ignoring install remote DMAC {} flow on provider interface {} elan {}",
974 macAddress, interfaceName, elanInstance.getElanInstanceName());
977 // if openstack-vni-semantics are enforced, segmentation ID is passed as network VNI for VxLAN based provider
978 // networks, 0 otherwise
979 long lportTagOrVni = !isOpenstackVniSemanticsEnforced() ? lportTag : isVxlanNetworkOrVxlanSegment(elanInstance)
980 ? getVxlanSegmentationId(elanInstance).longValue() : 0;
981 Flow flowEntity = buildRemoteDmacFlowEntry(srcDpId, destDpId, lportTagOrVni, elanTag, macAddress, displayName,
983 mdsalManager.addFlow(writeFlowGroupTx, srcDpId, flowEntity);
984 setupEtreeRemoteDmacFlow(srcDpId, destDpId, lportTagOrVni, elanTag, macAddress, displayName, interfaceName,
985 writeFlowGroupTx, elanInstance);
988 private void setupEtreeRemoteDmacFlow(Uint64 srcDpId, Uint64 destDpId, long lportTagOrVni, Uint32 elanTag,
989 String macAddress, String displayName, String interfaceName,
990 TypedWriteTransaction<Configuration> writeFlowGroupTx,
991 ElanInstance elanInstance) {
993 EtreeInterface etreeInterface = elanInterfaceCache.getEtreeInterface(interfaceName).orElse(null);
994 if (etreeInterface != null && etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
995 EtreeLeafTagName etreeTagName = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag.longValue());
996 if (etreeTagName == null) {
997 LOG.warn("Interface {} seems like it belongs to Etree but etreeTagName from elanTag {} is null.",
998 interfaceName, elanTag);
1000 flowEntity = buildRemoteDmacFlowEntry(srcDpId, destDpId, lportTagOrVni,
1001 etreeTagName.getEtreeLeafTag().getValue(), macAddress, displayName, elanInstance);
1002 mdsalManager.addFlow(writeFlowGroupTx, srcDpId, flowEntity);
1008 * Builds a Flow to be programmed in a remote DPN's DMAC table. This flow
1009 * consists in: Match: + elanTag in packet's metadata + packet going to a
1010 * MAC known to be located in another DPN Actions: + set_tunnel_id
1011 * + output ITM internal tunnel interface with the other DPN
1017 * @param lportTagOrVni
1018 * lportTag or network VNI
1023 * @param displayName
1025 * @param elanInstance
1027 * @return the flow remote Dmac
1029 @SuppressWarnings("checkstyle:IllegalCatch")
1030 public Flow buildRemoteDmacFlowEntry(Uint64 srcDpId, Uint64 destDpId, long lportTagOrVni, Uint32 elanTag,
1031 String macAddress, String displayName, ElanInstance elanInstance) {
1032 List<MatchInfo> mkMatches = new ArrayList<>();
1033 mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag.longValue()),
1034 MetaDataUtil.METADATA_MASK_SERVICE));
1035 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddress)));
1037 Map<InstructionKey, Instruction> mkInstructions = new HashMap<>();
1039 // List of Action for the provided Source and Destination DPIDs
1041 List<Action> actions = null;
1042 if (isVxlanNetworkOrVxlanSegment(elanInstance)) {
1043 actions = elanItmUtils.getInternalTunnelItmEgressAction(srcDpId, destDpId, lportTagOrVni);
1044 } else if (isVlan(elanInstance) || isFlat(elanInstance)) {
1045 String interfaceName = getExternalElanInterface(elanInstance.getElanInstanceName(), srcDpId);
1046 if (null == interfaceName) {
1047 LOG.info("buildRemoteDmacFlowEntry: Could not find interfaceName for {} {}", srcDpId,
1050 actions = getEgressActionsForInterface(interfaceName, null);
1052 mkInstructions.put(new InstructionKey(0), MDSALUtil.buildApplyActionsInstruction(actions));
1053 } catch (Exception e) {
1054 LOG.error("Could not get egress actions to add to flow for srcDpId {}, destDpId {}, lportTag/VNI {}",
1055 srcDpId, destDpId, lportTagOrVni, e);
1058 Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE,
1059 getKnownDynamicmacFlowRef(elanTag, macAddress),
1061 displayName, 0, /* idleTimeout */
1062 0, /* hardTimeout */
1063 Uint64.valueOf(ElanConstants.COOKIE_ELAN_KNOWN_DMAC.longValue() + elanTag.longValue()),
1064 mkMatches, mkInstructions);
1070 public void deleteMacFlows(@Nullable ElanInstance elanInfo, @Nullable InterfaceInfo interfaceInfo,
1071 MacEntry macEntry, TypedReadWriteTransaction<Configuration> flowTx)
1072 throws ExecutionException, InterruptedException {
1073 if (elanInfo == null || interfaceInfo == null) {
1076 String macAddress = macEntry.getMacAddress().getValue();
1077 try (Acquired lock = lockElanMacDPN(elanInfo.getElanTag().toJava(),
1078 macAddress, interfaceInfo.getDpId())) {
1079 deleteMacFlows(elanInfo, interfaceInfo, macAddress, /* alsoDeleteSMAC */ true, flowTx);
1083 public void deleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1084 boolean deleteSmac, TypedReadWriteTransaction<Configuration> flowTx)
1085 throws ExecutionException, InterruptedException {
1086 String elanInstanceName = elanInfo.getElanInstanceName();
1087 List<DpnInterfaces> remoteFEs = getInvolvedDpnsInElan(elanInstanceName);
1088 Uint64 srcdpId = interfaceInfo.getDpId();
1089 boolean isFlowsRemovedInSrcDpn = false;
1090 for (DpnInterfaces dpnInterface : remoteFEs) {
1091 Uint32 elanTag = elanInfo.getElanTag();
1092 Uint64 dstDpId = dpnInterface.getDpId();
1093 if (executeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1094 elanTag, dstDpId, flowTx)) {
1095 isFlowsRemovedInSrcDpn = true;
1097 executeEtreeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1098 elanTag, dstDpId, flowTx);
1100 if (!isFlowsRemovedInSrcDpn) {
1101 deleteSmacAndDmacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, flowTx);
1105 private void executeEtreeDeleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1106 boolean deleteSmac, String elanInstanceName, Uint64 srcdpId, Uint32 elanTag, Uint64 dstDpId,
1107 TypedReadWriteTransaction<Configuration> flowTx) throws ExecutionException, InterruptedException {
1108 EtreeLeafTagName etreeLeafTag = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag.longValue());
1109 if (etreeLeafTag != null) {
1110 executeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1111 etreeLeafTag.getEtreeLeafTag().getValue(), dstDpId, flowTx);
1115 private boolean executeDeleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1116 boolean deleteSmac, String elanInstanceName, Uint64 srcdpId, Uint32 elanTag, Uint64 dstDpId,
1117 TypedReadWriteTransaction<Configuration> flowTx) throws ExecutionException, InterruptedException {
1118 boolean isFlowsRemovedInSrcDpn = false;
1119 if (dstDpId.equals(srcdpId)) {
1120 isFlowsRemovedInSrcDpn = true;
1121 deleteSmacAndDmacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, flowTx);
1122 } else if (isDpnPresent(dstDpId)) {
1124 .removeFlow(flowTx, dstDpId,
1125 MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE, getKnownDynamicmacFlowRef(elanTag, macAddress)));
1126 LOG.debug("Dmac flow entry deleted for elan:{}, logical interface port:{} and mac address:{} on dpn:{}",
1127 elanInstanceName, interfaceInfo.getPortName(), macAddress, dstDpId);
1129 return isFlowsRemovedInSrcDpn;
1132 public void deleteSmacFlowOnly(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1133 TypedReadWriteTransaction<Configuration> flowTx) throws ExecutionException,
1134 InterruptedException {
1135 Uint64 srcdpId = interfaceInfo.getDpId();
1136 Uint32 elanTag = elanInfo.getElanTag();
1137 LOG.debug("Deleting SMAC flow with id {}", getKnownDynamicmacFlowRef(elanTag, macAddress));
1138 mdsalManager.removeFlow(flowTx, srcdpId,
1139 MDSALUtil.buildFlow(NwConstants.ELAN_SMAC_TABLE, getKnownDynamicmacFlowRef(elanTag, macAddress)));
1142 private void deleteSmacAndDmacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1143 boolean deleteSmac, TypedReadWriteTransaction<Configuration> flowTx)
1144 throws ExecutionException, InterruptedException {
1145 String elanInstanceName = elanInfo.getElanInstanceName();
1146 Uint64 srcdpId = interfaceInfo.getDpId();
1147 Uint32 elanTag = elanInfo.getElanTag();
1149 LOG.debug("Deleting SMAC flow with id {}", getKnownDynamicmacFlowRef(elanTag, macAddress));
1151 .removeFlow(flowTx, srcdpId,
1152 MDSALUtil.buildFlow(NwConstants.ELAN_SMAC_TABLE,
1153 getKnownDynamicmacFlowRef(elanTag, macAddress)));
1155 mdsalManager.removeFlow(flowTx, srcdpId,
1156 MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE,
1157 getKnownDynamicmacFlowRef(elanTag, macAddress)));
1158 LOG.debug("All the required flows deleted for elan:{}, logical Interface port:{} and MAC address:{} on dpn:{}",
1159 elanInstanceName, interfaceInfo.getPortName(), macAddress, srcdpId);
1163 * Updates the Elan information in the Operational DS. It also updates the
1164 * ElanInstance in the Config DS by setting the adquired elanTag.
1168 * @param elanInstanceAdded
1169 * the elan instance added
1170 * @param elanInterfaces
1171 * the elan interfaces
1175 * @return the updated ELAN instance.
1177 public static ElanInstance updateOperationalDataStore(IdManagerService idManager,
1178 ElanInstance elanInstanceAdded, List<String> elanInterfaces, TypedWriteTransaction<Configuration> confTx,
1179 TypedWriteTransaction<Operational> operTx) {
1180 String elanInstanceName = elanInstanceAdded.getElanInstanceName();
1181 Uint32 elanTag = elanInstanceAdded.getElanTag();
1182 if (elanTag == null || elanTag.longValue() == 0L) {
1183 elanTag = retrieveNewElanTag(idManager, elanInstanceName);
1185 if (elanTag.longValue() == 0L) {
1186 LOG.error("ELAN tag creation failed for elan instance {}. Not updating the ELAN DS. "
1187 + "Recreate network for recovery", elanInstanceName);
1190 Elan elanInfo = new ElanBuilder().setName(elanInstanceName).setElanInterfaces(elanInterfaces)
1191 .withKey(new ElanKey(elanInstanceName)).build();
1193 // Add the ElanState in the elan-state operational data-store
1194 operTx.mergeParentStructurePut(getElanInstanceOperationalDataPath(elanInstanceName), elanInfo);
1196 // Add the ElanMacTable in the elan-mac-table operational data-store
1197 MacTable elanMacTable = new MacTableBuilder().withKey(new MacTableKey(elanInstanceName)).build();
1198 operTx.mergeParentStructurePut(getElanMacTableOperationalDataPath(elanInstanceName), elanMacTable);
1200 ElanTagNameBuilder elanTagNameBuilder = new ElanTagNameBuilder().setElanTag(elanTag)
1201 .withKey(new ElanTagNameKey(elanTag)).setName(elanInstanceName);
1202 Uint32 etreeLeafTag = Uint32.valueOf(0L);
1203 if (isEtreeInstance(elanInstanceAdded)) {
1204 etreeLeafTag = retrieveNewElanTag(idManager,elanInstanceName + ElanConstants
1206 EtreeLeafTagName etreeLeafTagName = new EtreeLeafTagNameBuilder()
1207 .setEtreeLeafTag(new EtreeLeafTag(etreeLeafTag)).build();
1208 elanTagNameBuilder.addAugmentation(EtreeLeafTagName.class, etreeLeafTagName);
1209 addTheLeafTagAsElanTag(elanInstanceName, etreeLeafTag, operTx);
1211 ElanTagName elanTagName = elanTagNameBuilder.build();
1213 // Add the ElanTag to ElanName in the elan-tag-name Operational
1215 operTx.put(getElanInfoEntriesOperationalDataPath(elanTag), elanTagName);
1217 // Updates the ElanInstance Config DS by setting the just acquired
1219 ElanInstanceBuilder elanInstanceBuilder = new ElanInstanceBuilder(elanInstanceAdded)
1220 .setElanInstanceName(elanInstanceName)
1221 .setDescription(elanInstanceAdded.getDescription())
1222 .setMacTimeout(elanInstanceAdded.getMacTimeout() == null
1223 ? Uint32.valueOf(ElanConstants.DEFAULT_MAC_TIME_OUT) : elanInstanceAdded.getMacTimeout())
1224 .withKey(elanInstanceAdded.key()).setElanTag(elanTag);
1225 if (isEtreeInstance(elanInstanceAdded)) {
1226 EtreeInstance etreeInstance = new EtreeInstanceBuilder().setEtreeLeafTagVal(new EtreeLeafTag(etreeLeafTag))
1228 elanInstanceBuilder.addAugmentation(EtreeInstance.class, etreeInstance);
1230 ElanInstance elanInstanceWithTag = elanInstanceBuilder.build();
1231 LOG.trace("Updated elan Operational DS for elan: {} with elanTag: {} and interfaces: {}", elanInstanceName,
1232 elanTag, elanInterfaces);
1233 confTx.mergeParentStructureMerge(ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName),
1234 elanInstanceWithTag);
1235 return elanInstanceWithTag;
1238 private static void addTheLeafTagAsElanTag(String elanInstanceName,Uint32 etreeLeafTag,
1239 TypedWriteTransaction<Operational> tx) {
1240 ElanTagName etreeTagAsElanTag = new ElanTagNameBuilder().setElanTag(etreeLeafTag)
1241 .withKey(new ElanTagNameKey(etreeLeafTag)).setName(elanInstanceName).build();
1242 tx.put(getElanInfoEntriesOperationalDataPath(etreeLeafTag), etreeTagAsElanTag);
1245 private static boolean isEtreeInstance(ElanInstance elanInstanceAdded) {
1246 return elanInstanceAdded.augmentation(EtreeInstance.class) != null;
1249 public boolean isDpnPresent(Uint64 dpnId) {
1250 String dpn = String.format("%s:%s", "openflow", dpnId);
1251 NodeId nodeId = new NodeId(dpn);
1253 InstanceIdentifier<Node> node = InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(nodeId))
1255 return read(broker, LogicalDatastoreType.CONFIGURATION, node).isPresent();
1258 public static String getElanServiceName(String elanName, String interfaceName) {
1259 return "elan." + elanName + interfaceName;
1262 public static BoundServices getBoundServices(String serviceName, short servicePriority, int flowPriority,
1263 Uint64 cookie, List<Instruction> instructions) {
1264 StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookie).setFlowPriority(flowPriority)
1265 .setInstruction(instructions);
1266 return new BoundServicesBuilder().withKey(new BoundServicesKey(servicePriority)).setServiceName(serviceName)
1267 .setServicePriority(servicePriority).setServiceType(ServiceTypeFlowBased.class)
1268 .addAugmentation(StypeOpenflow.class, augBuilder.build()).build();
1271 public static InstanceIdentifier<BoundServices> buildServiceId(String interfaceName, short serviceIndex) {
1272 return InstanceIdentifier.builder(ServiceBindings.class)
1273 .child(ServicesInfo.class, new ServicesInfoKey(interfaceName, ServiceModeIngress.class))
1274 .child(BoundServices.class, new BoundServicesKey(serviceIndex)).build();
1277 public static List<MatchInfo> getTunnelMatchesForServiceId(Uint32 elanTag) {
1278 List<MatchInfo> mkMatches = new ArrayList<>();
1280 // Adding 270000 to avoid collision between LPort and elan tag for broadcast
1281 mkMatches.add(new MatchTunnelId(Uint64.valueOf(elanTag.longValue() + ElanConstants.ELAN_TAG_ADDEND)));
1286 public void removeTerminatingServiceAction(Uint64 destDpId, int serviceId) {
1287 RemoveTerminatingServiceActionsInput input = new RemoveTerminatingServiceActionsInputBuilder()
1288 .setDpnId(destDpId).setServiceId(serviceId).build();
1289 Future<RpcResult<RemoveTerminatingServiceActionsOutput>> futureObject =
1290 itmRpcService.removeTerminatingServiceActions(input);
1292 RpcResult<RemoveTerminatingServiceActionsOutput> result = futureObject.get();
1293 if (result.isSuccessful()) {
1294 LOG.debug("Successfully completed removeTerminatingServiceActions for ELAN with serviceId {} on "
1295 + "dpn {}", serviceId, destDpId);
1297 LOG.debug("Failure in removeTerminatingServiceAction RPC call for ELAN with serviceId {} on "
1298 + "dpn {}", serviceId, destDpId);
1300 } catch (InterruptedException | ExecutionException e) {
1301 LOG.error("Error in RPC call removeTerminatingServiceActions for ELAN with serviceId {} on "
1302 + "dpn {}", serviceId, destDpId, e);
1307 * Gets the external tunnel.
1309 * @param sourceDevice
1311 * @param destinationDevice
1312 * the destination device
1313 * @param datastoreType
1314 * the datastore type
1315 * @return the external tunnel
1318 public ExternalTunnel getExternalTunnel(String sourceDevice, String destinationDevice,
1319 LogicalDatastoreType datastoreType) {
1320 Class<? extends TunnelTypeBase> tunType = TunnelTypeVxlan.class;
1321 InstanceIdentifier<ExternalTunnel> iid = InstanceIdentifier.builder(ExternalTunnelList.class)
1322 .child(ExternalTunnel.class, new ExternalTunnelKey(destinationDevice, sourceDevice, tunType)).build();
1323 return read(broker, datastoreType, iid).orElse(null);
1327 * Gets the external tunnel.
1329 * @param interfaceName
1330 * the interface name
1331 * @param datastoreType
1332 * the datastore type
1333 * @return the external tunnel
1336 public ExternalTunnel getExternalTunnel(String interfaceName, LogicalDatastoreType datastoreType) {
1337 ExternalTunnel externalTunnel = null;
1338 List<ExternalTunnel> externalTunnels = getAllExternalTunnels(datastoreType);
1339 for (ExternalTunnel tunnel : externalTunnels) {
1340 if (StringUtils.equalsIgnoreCase(interfaceName, tunnel.getTunnelInterfaceName())) {
1341 externalTunnel = tunnel;
1345 return externalTunnel;
1349 * Gets the all external tunnels.
1351 * @param datastoreType
1352 * the data store type
1353 * @return the all external tunnels
1355 public List<ExternalTunnel> getAllExternalTunnels(LogicalDatastoreType datastoreType) {
1356 InstanceIdentifier<ExternalTunnelList> iid = InstanceIdentifier.builder(ExternalTunnelList.class).build();
1357 return new ArrayList<ExternalTunnel>(read(broker, datastoreType, iid).map(ExternalTunnelList
1358 ::nonnullExternalTunnel).orElse(Collections.emptyMap()).values());
1361 public static List<MatchInfo> buildMatchesForElanTagShFlagAndDstMac(long elanTag, boolean shFlag, String macAddr) {
1362 List<MatchInfo> mkMatches = new ArrayList<>();
1364 new MatchMetadata(getElanMetadataLabel(elanTag, shFlag), MetaDataUtil.METADATA_MASK_SERVICE_SH_FLAG));
1365 mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddr)));
1370 * Checks if is interface operational.
1372 * @param interfaceName
1373 * the interface name
1376 * @return true, if is interface operational
1378 public static boolean isInterfaceOperational(String interfaceName, DataBroker dataBroker) {
1379 if (StringUtils.isBlank(interfaceName)) {
1382 Interface ifState = getInterfaceStateFromOperDS(interfaceName, dataBroker);
1383 if (ifState == null) {
1386 return ifState.getOperStatus() == OperStatus.Up && ifState.getAdminStatus() == AdminStatus.Up;
1390 * Gets the interface state from operational ds.
1392 * @param interfaceName
1393 * the interface name
1396 * @return the interface state from oper ds
1398 public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
1399 .@Nullable Interface getInterfaceStateFromOperDS(String interfaceName, DataBroker dataBroker) {
1401 return SingleTransactionDataBroker.syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL,
1402 createInterfaceStateInstanceIdentifier(interfaceName)).orElse(null);
1403 } catch (ExecutionException | InterruptedException e) {
1404 LOG.error("getInterfaceStateFromOperDS: Exception while reading interface DS for the interface {}",
1411 * Creates the interface state instance identifier.
1413 * @param interfaceName
1414 * the interface name
1415 * @return the instance identifier
1417 public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1418 .ietf.interfaces.rev140508.interfaces.state.Interface> createInterfaceStateInstanceIdentifier(
1419 String interfaceName) {
1420 InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1421 .ietf.interfaces.rev140508.interfaces.state.Interface> idBuilder = InstanceIdentifier
1422 .builder(InterfacesState.class)
1423 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1424 .ietf.interfaces.rev140508.interfaces.state.Interface.class,
1425 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1426 .ietf.interfaces.rev140508.interfaces.state.InterfaceKey(
1428 return idBuilder.build();
1432 public static ListenableFuture<Void> waitForTransactionToComplete(ListenableFuture<Void> future) {
1435 } catch (InterruptedException | ExecutionException e) {
1436 // NETVIRT-1215: Do not log.error() here, only debug(); but callers *MUST* @CheckReturnValue
1437 LOG.debug("Error writing to datastore", e);
1442 public static boolean isVxlan(@Nullable ElanInstance elanInstance) {
1443 return elanInstance != null && elanInstance.getSegmentType() != null
1444 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1445 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId().toJava() != 0;
1448 private static boolean isVxlanSegment(@Nullable ElanInstance elanInstance) {
1449 if (elanInstance != null) {
1450 Map<ElanSegmentsKey, ElanSegments> elanSegments = elanInstance.nonnullElanSegments();
1451 if (elanSegments != null) {
1452 for (ElanSegments segment : elanSegments.values()) {
1453 if (segment != null && segment.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1454 && segment.getSegmentationId() != null
1455 && segment.getSegmentationId().longValue() != 0) {
1464 public static boolean isVxlanNetworkOrVxlanSegment(@Nullable ElanInstance elanInstance) {
1465 return isVxlan(elanInstance) || isVxlanSegment(elanInstance);
1468 public static Uint32 getVxlanSegmentationId(ElanInstance elanInstance) {
1469 Uint32 segmentationId = Uint32.ZERO;
1470 if (elanInstance == null) {
1471 return segmentationId;
1474 if (elanInstance.getSegmentType() != null
1475 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1476 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId().longValue() != 0) {
1477 segmentationId = elanInstance.getSegmentationId();
1479 for (ElanSegments segment: elanInstance.nonnullElanSegments().values()) {
1480 if (segment != null && segment.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1481 && segment.getSegmentationId() != null
1482 && segment.getSegmentationId().longValue() != 0) {
1483 segmentationId = segment.getSegmentationId();
1487 return segmentationId;
1490 public static boolean isVlan(ElanInstance elanInstance) {
1491 return elanInstance != null && elanInstance.getSegmentType() != null
1492 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVlan.class)
1493 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId().toJava() != 0;
1496 public static boolean isFlat(ElanInstance elanInstance) {
1497 return elanInstance != null && elanInstance.getSegmentType() != null
1498 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeFlat.class);
1501 public void addDmacRedirectToDispatcherFlows(Uint32 elanTag, String displayName,
1502 String macAddress, List<Uint64> dpnIds) {
1503 for (Uint64 dpId : dpnIds) {
1504 LoggingFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
1505 tx -> mdsalManager.addFlow(tx, buildDmacRedirectToDispatcherFlow(dpId, macAddress, displayName,
1507 "Error adding DMAC redirect to dispatcher flows");
1511 public void removeDmacRedirectToDispatcherFlows(Uint32 elanTag, String macAddress, List<Uint64> dpnIds) {
1512 LoggingFutures.addErrorLogging(
1513 txRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.CONFIGURATION, tx -> {
1514 for (Uint64 dpId : dpnIds) {
1515 String flowId = getKnownDynamicmacFlowRef(elanTag, macAddress);
1516 mdsalManager.removeFlow(tx, dpId, MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE, flowId));
1518 }), LOG, "Error removing DMAC redirect to dispatcher flows");
1521 public static FlowEntity buildDmacRedirectToDispatcherFlow(Uint64 dpId, String dstMacAddress,
1522 String displayName, Uint32 elanTag) {
1523 List<MatchInfo> matches = new ArrayList<>();
1524 matches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag.longValue()),
1525 MetaDataUtil.METADATA_MASK_SERVICE));
1526 matches.add(new MatchEthernetDestination(new MacAddress(dstMacAddress)));
1527 List<InstructionInfo> instructions = new ArrayList<>();
1528 List<ActionInfo> actions = new ArrayList<>();
1529 actions.add(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
1531 instructions.add(new InstructionApplyActions(actions));
1532 String flowId = getKnownDynamicmacFlowRef(elanTag, dstMacAddress);
1533 return MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 20, displayName, 0, 0,
1534 Uint64.valueOf(ElanConstants.COOKIE_ELAN_KNOWN_DMAC.longValue() + elanTag.longValue()),
1535 matches, instructions);
1539 public String getExternalElanInterface(String elanInstanceName, Uint64 dpnId) {
1540 DpnInterfaces dpnInterfaces = getElanInterfaceInfoByElanDpn(elanInstanceName, dpnId);
1541 if (dpnInterfaces == null || dpnInterfaces.getInterfaces() == null) {
1542 LOG.trace("Elan {} does not have interfaces in DPN {}", elanInstanceName, dpnId);
1546 for (String dpnInterface : dpnInterfaces.getInterfaces()) {
1547 if (interfaceManager.isExternalInterface(dpnInterface)) {
1548 return dpnInterface;
1552 LOG.trace("Elan {} does not have any external interace attached to DPN {}", elanInstanceName, dpnId);
1556 public static Acquired lockElanMacDPN(long elanTag, String macAddress, Uint64 dpnId) {
1557 return ELAN_LOCKS.acquire(new ElanLockName(elanTag, macAddress, dpnId));
1560 public static List<ListenableFuture<Void>>
1561 returnFailedListenableFutureIfTransactionCommitFailedExceptionCauseOrElseThrow(RuntimeException exception) {
1563 Throwable cause = exception.getCause();
1564 if (cause != null && cause instanceof TransactionCommitFailedException) {
1565 return Collections.singletonList(Futures.immediateFailedFuture(cause));
1571 public static List<PhysAddress> getPhysAddress(List<String> macAddress) {
1572 Preconditions.checkNotNull(macAddress, "macAddress cannot be null");
1573 List<PhysAddress> physAddresses = new ArrayList<>();
1574 for (String mac : macAddress) {
1575 physAddresses.add(new PhysAddress(mac));
1577 return physAddresses;
1580 public static List<StaticMacEntries> getStaticMacEntries(List<String> staticMacAddresses) {
1581 if (isEmpty(staticMacAddresses)) {
1582 return Collections.EMPTY_LIST;
1584 StaticMacEntriesBuilder staticMacEntriesBuilder = new StaticMacEntriesBuilder();
1585 List<StaticMacEntries> staticMacEntries = new ArrayList<>();
1586 List<PhysAddress> physAddressList = getPhysAddress(staticMacAddresses);
1587 for (PhysAddress physAddress : physAddressList) {
1588 staticMacEntries.add(staticMacEntriesBuilder.setMacAddress(physAddress).build());
1590 return staticMacEntries;
1593 public static InstanceIdentifier<StaticMacEntries> getStaticMacEntriesCfgDataPathIdentifier(String interfaceName,
1594 String macAddress) {
1595 return InstanceIdentifier.builder(ElanInterfaces.class)
1596 .child(ElanInterface.class, new ElanInterfaceKey(interfaceName)).child(StaticMacEntries.class,
1597 new StaticMacEntriesKey(new PhysAddress(macAddress))).build();
1600 public static <T> List<T> diffOf(List<T> orig, List<T> updated) {
1601 if (isEmpty(orig)) {
1602 return Collections.EMPTY_LIST;
1604 List<T> diff = Lists.newArrayList(orig);
1605 if (isNotEmpty(updated)) {
1606 diff.removeAll(updated);
1611 public static boolean isEmpty(Collection collection) {
1612 return collection == null || collection.isEmpty();
1615 public static boolean isNotEmpty(Collection collection) {
1616 return !isEmpty(collection);
1619 public Optional<IpAddress> getSourceIpAddress(Ethernet ethernet) {
1620 Optional<IpAddress> srcIpAddress = Optional.empty();
1621 if (ethernet.getPayload() == null) {
1622 return srcIpAddress;
1624 byte[] ipAddrBytes = null;
1625 if (ethernet.getPayload() instanceof IPv4) {
1626 IPv4 ipv4 = (IPv4) ethernet.getPayload();
1627 ipAddrBytes = Ints.toByteArray(ipv4.getSourceAddress());
1628 } else if (ethernet.getPayload() instanceof ARP) {
1629 ipAddrBytes = ((ARP) ethernet.getPayload()).getSenderProtocolAddress();
1631 if (ipAddrBytes != null) {
1632 String ipAddr = NWUtil.toStringIpAddress(ipAddrBytes);
1633 return Optional.of(IpAddressBuilder.getDefaultInstance(ipAddr));
1635 return srcIpAddress;
1638 public List<MacEntry> getElanMacEntries(String elanName) {
1639 MacTable macTable = getElanMacTable(elanName);
1640 if (macTable == null) {
1643 return new ArrayList<MacEntry>(macTable.nonnullMacEntry().values());
1646 public boolean isTunnelInLogicalGroup(String interfaceName) {
1647 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1648 .ietf.interfaces.rev140508.interfaces.Interface configIface =
1649 interfaceManager.getInterfaceInfoFromConfigDataStore(interfaceName);
1650 if (configIface == null) {
1651 configIface = iitmProvider.getInterface(interfaceName);
1653 if (configIface == null) {
1656 IfTunnel ifTunnel = configIface.augmentation(IfTunnel.class);
1657 if (ifTunnel != null && ifTunnel.getTunnelInterfaceType().isAssignableFrom(TunnelTypeVxlan.class)) {
1658 ParentRefs refs = configIface.augmentation(ParentRefs.class);
1659 return refs != null && !Strings.isNullOrEmpty(refs.getParentInterface());
1664 public static InstanceIdentifier<Flow> getFlowIid(Flow flow, Uint64 dpnId) {
1665 FlowKey flowKey = new FlowKey(new FlowId(flow.getId()));
1666 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId nodeId =
1667 new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId("openflow:" + dpnId);
1668 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node nodeDpn =
1669 new NodeBuilder().setId(nodeId).withKey(new NodeKey(nodeId)).build();
1670 return InstanceIdentifier.builder(Nodes.class)
1671 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class,
1672 nodeDpn.key()).augmentation(FlowCapableNode.class)
1673 .child(Table.class, new TableKey(flow.getTableId())).child(Flow.class, flowKey).build();
1676 public static String getElanInterfaceJobKey(String interfaceName) {
1677 return "elaninterface-" + interfaceName;
1680 public void removeArpResponderFlow(Uint64 dpnId, String ingressInterfaceName, String ipAddress,
1682 LOG.info("Removing the ARP responder flow on DPN {} of Interface {} with IP {}", dpnId, ingressInterfaceName,
1684 LoggingFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.CONFIGURATION,
1685 tx -> mdsalManager.removeFlow(tx, dpnId, ArpResponderUtil.getFlowId(lportTag, ipAddress),
1686 NwConstants.ARP_RESPONDER_TABLE)), LOG, "Error removing ARP responder flow");
1690 public static String getRouterPordIdFromElanInstance(DataBroker dataBroker, String elanInstanceName) {
1691 Optional<Subnetmaps> subnetMapsData =
1692 read(dataBroker, LogicalDatastoreType.CONFIGURATION, buildSubnetMapsWildCardPath());
1693 if (subnetMapsData.isPresent()) {
1694 List<Subnetmap> subnetMapList = new ArrayList<>(subnetMapsData.get().nonnullSubnetmap().values());
1695 if (subnetMapList != null && !subnetMapList.isEmpty()) {
1696 for (Subnetmap subnet : subnetMapList) {
1697 if (subnet.getNetworkId().getValue().equals(elanInstanceName)) {
1698 if (subnet.getRouterInterfacePortId() != null) {
1699 return subnet.getRouterInterfacePortId().getValue();
1708 protected Node buildDpnNode(Uint64 dpnId) {
1709 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId nodeId =
1710 new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819
1711 .NodeId("openflow:" + dpnId);
1712 Node nodeDpn = new NodeBuilder().setId(nodeId).withKey(new NodeKey(nodeId)).build();
1716 public void syncUpdateGroup(Uint64 dpnId, Group newGroup, long delayTime,
1717 TypedWriteTransaction<Datastore.Configuration> confTx) {
1718 Node nodeDpn = buildDpnNode(dpnId);
1719 long groupIdInfo = newGroup.getGroupId().getValue().toJava();
1720 GroupKey groupKey = new GroupKey(new GroupId(groupIdInfo));
1721 InstanceIdentifier<Group> groupInstanceId = InstanceIdentifier.builder(Nodes.class)
1722 .child(Node.class, nodeDpn.key()).augmentation(FlowCapableNode.class)
1723 .child(Group.class, groupKey).build();
1724 LOG.trace("Performing merge operation for remote BC group for node {} with group {}", nodeDpn, newGroup);
1725 Optional<Group> existingGroupOpt = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION, groupInstanceId);
1726 if (!existingGroupOpt.isPresent()) {
1727 LOG.debug("Group {} doesn't exist. Performing syncInstall", groupIdInfo);
1728 mdsalManager.addGroup(confTx, dpnId, newGroup);
1731 Buckets existingGroup = existingGroupOpt.get().getBuckets();
1732 if (existingGroup == null) {
1733 LOG.debug("Bucket doesn't exist for group {}. Performing syncInstall", groupIdInfo);
1734 mdsalManager.addGroup(confTx, dpnId, newGroup);
1737 if (newGroup.getBuckets() == null) {
1738 LOG.debug("Buckets are not sent for group {}. Skipping merge operation", groupIdInfo);
1741 List<Bucket> newBuckets = new ArrayList<Bucket>(newGroup.getBuckets().nonnullBucket().values());
1742 List<Bucket> existingBuckets = new ArrayList<Bucket>(existingGroup.nonnullBucket().values());
1743 Set<Bucket> toMergeBucketsWithoutId = new LinkedHashSet<>();
1745 existingBuckets.stream()
1746 .map(TO_BUCKET_WITHOUT_ID)
1747 .forEach(bucketWithoutId -> toMergeBucketsWithoutId.add(bucketWithoutId));
1750 .map(TO_BUCKET_WITHOUT_ID)
1751 .forEach(bucketWithoutId -> toMergeBucketsWithoutId.add(bucketWithoutId));
1753 if (toMergeBucketsWithoutId.size() == existingBuckets.size()) {
1754 //no new buckets got added
1755 //size matched and no extra buckets in existing buckets , rewrite the group
1756 LOG.debug("Buckets did not change group {}. Skipping merge operation", groupIdInfo);
1759 LOG.debug("Old group buckets size {} New group buckets size {} Dpn id {} Group id {} ",
1760 existingBuckets.size(), toMergeBucketsWithoutId.size(), dpnId, groupIdInfo);
1761 AtomicLong bucketIdValue = new AtomicLong(-1);
1762 //Change the bucket id of existing buckets
1763 List<Bucket> bucketsToBeAdded = toMergeBucketsWithoutId.stream()
1764 .map(bucketWithoutId -> TO_BUCKET_WITH_ID.apply(bucketWithoutId, bucketIdValue))
1765 .collect(Collectors.toList());
1767 Group group = MDSALUtil.buildGroup(newGroup.getGroupId().getValue().toJava(), newGroup.getGroupName(),
1768 GroupTypes.GroupAll, MDSALUtil.buildBucketLists(bucketsToBeAdded));
1769 mdsalManager.addGroup(confTx, dpnId, group);
1770 LOG.trace("Installed remote BC group for node {} with group {}", nodeDpn, group);
1773 static InstanceIdentifier<Subnetmaps> buildSubnetMapsWildCardPath() {
1774 return InstanceIdentifier.create(Subnetmaps.class);
1777 public static InstanceIdentifier<Group> getGroupInstanceid(Uint64 dpnId, long groupId) {
1778 return InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId)))
1779 .augmentation(FlowCapableNode.class).child(Group.class, new GroupKey(new GroupId(groupId))).build();
1782 public static String getBcGroupUpdateKey(String elanName) {
1783 return "bc.group.update." + elanName;