Migrate ElanUtils to use NamedLocks
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / utils / ElanUtils.java
1 /*
2  * Copyright © 2016, 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.elan.utils;
9
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;
13
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.List;
30 import java.util.Objects;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.Future;
33 import javax.inject.Inject;
34 import javax.inject.Singleton;
35 import org.apache.commons.lang3.StringUtils;
36 import org.eclipse.jdt.annotation.NonNull;
37 import org.eclipse.jdt.annotation.Nullable;
38 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
39 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
40 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
41 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
42 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
43 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
44 import org.opendaylight.genius.infra.Datastore;
45 import org.opendaylight.genius.infra.Datastore.Configuration;
46 import org.opendaylight.genius.infra.Datastore.Operational;
47 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
48 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
49 import org.opendaylight.genius.infra.TypedReadTransaction;
50 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
51 import org.opendaylight.genius.infra.TypedWriteTransaction;
52 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
53 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
54 import org.opendaylight.genius.itm.api.IITMProvider;
55 import org.opendaylight.genius.itm.globals.ITMConstants;
56 import org.opendaylight.genius.mdsalutil.ActionInfo;
57 import org.opendaylight.genius.mdsalutil.FlowEntity;
58 import org.opendaylight.genius.mdsalutil.FlowEntityBuilder;
59 import org.opendaylight.genius.mdsalutil.InstructionInfo;
60 import org.opendaylight.genius.mdsalutil.MDSALUtil;
61 import org.opendaylight.genius.mdsalutil.MatchInfo;
62 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
63 import org.opendaylight.genius.mdsalutil.NWUtil;
64 import org.opendaylight.genius.mdsalutil.NwConstants;
65 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
66 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
67 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
68 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
69 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetDestination;
70 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetSource;
71 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
72 import org.opendaylight.genius.mdsalutil.matches.MatchTunnelId;
73 import org.opendaylight.genius.mdsalutil.packet.ARP;
74 import org.opendaylight.genius.mdsalutil.packet.Ethernet;
75 import org.opendaylight.genius.mdsalutil.packet.IPv4;
76 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
77 import org.opendaylight.infrautils.utils.concurrent.NamedLocks;
78 import org.opendaylight.infrautils.utils.concurrent.NamedSimpleReentrantLock.Acquired;
79 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderUtil;
80 import org.opendaylight.netvirt.elan.cache.ElanInterfaceCache;
81 import org.opendaylight.netvirt.elanmanager.api.ElanHelper;
82 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
83 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
84 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
85 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
86 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.AdminStatus;
87 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
88 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
89 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.IfIndexesInterfaceMap;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterface;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._if.indexes._interface.map.IfIndexInterfaceKey;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefs;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeBase;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
111 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceInput;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceInputBuilder;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEgressActionsForInterfaceOutput;
114 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceBindings;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeIngress;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceTypeFlowBased;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflow;
119 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.StypeOpenflowBuilder;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfo;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.ServicesInfoKey;
122 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
123 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesBuilder;
124 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServicesKey;
125 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.ExternalTunnelList;
126 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnel;
127 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnelKey;
128 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
129 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.RemoveTerminatingServiceActionsInput;
130 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.RemoveTerminatingServiceActionsInputBuilder;
131 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.RemoveTerminatingServiceActionsOutput;
132 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
133 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
134 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
135 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
136 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
137 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
138 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
139 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
140 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
141 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
142 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstanceBuilder;
143 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface;
144 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface.EtreeInterfaceType;
145 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTag;
146 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTagName;
147 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTagNameBuilder;
148 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
149 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanForwardingTables;
150 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaceForwardingEntries;
151 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
152 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanState;
153 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanTagNameMap;
154 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeFlat;
155 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeVlan;
156 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.SegmentTypeVxlan;
157 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
158 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMacKey;
159 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
160 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesListKey;
161 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
162 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfacesKey;
163 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTable;
164 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTableBuilder;
165 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTableKey;
166 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
167 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
168 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.elan.instance.ElanSegments;
169 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
170 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
171 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntries;
172 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntriesBuilder;
173 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntriesKey;
174 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
175 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.ElanBuilder;
176 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.ElanKey;
177 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagName;
178 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagNameBuilder;
179 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.tag.name.map.ElanTagNameKey;
180 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
181 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
182 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
183 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
184 import org.opendaylight.yangtools.yang.binding.DataObject;
185 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
186 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
187 import org.opendaylight.yangtools.yang.common.RpcResult;
188 import org.slf4j.Logger;
189 import org.slf4j.LoggerFactory;
190
191 @Singleton
192 public class ElanUtils {
193     private static final class ElanLockName {
194         private final String macAddress;
195         private final BigInteger dpnId;
196         private final long elanTag;
197
198         ElanLockName(long elanTag, String macAddress, BigInteger dpnId) {
199             this.elanTag = elanTag;
200             this.macAddress = macAddress;
201             this.dpnId = dpnId;
202         }
203
204         @Override
205         public int hashCode() {
206             return 31 * Long.hashCode(elanTag) + Objects.hash(macAddress, dpnId);
207         }
208
209         @Override
210         public boolean equals(Object obj) {
211             if (this == obj) {
212                 return true;
213             }
214             if (!(obj instanceof ElanLockName)) {
215                 return false;
216             }
217             final ElanLockName other = (ElanLockName) obj;
218             return elanTag == other.elanTag && Objects.equals(macAddress, other.macAddress)
219                     && Objects.equals(dpnId, other.dpnId);
220         }
221     }
222
223     private static final Logger LOG = LoggerFactory.getLogger(ElanUtils.class);
224     private static final NamedLocks<ElanLockName> ELAN_LOCKS = new NamedLocks<>();
225
226     private final DataBroker broker;
227     private final ManagedNewTransactionRunner txRunner;
228     private final IMdsalApiManager mdsalManager;
229     private final OdlInterfaceRpcService interfaceManagerRpcService;
230     private final ItmRpcService itmRpcService;
231     private final IInterfaceManager interfaceManager;
232     private final ElanConfig elanConfig;
233     private final ElanItmUtils elanItmUtils;
234     private final ElanEtreeUtils elanEtreeUtils;
235     private final ElanInterfaceCache elanInterfaceCache;
236     private final IITMProvider iitmProvider;
237
238     public static final FutureCallback<Void> DEFAULT_CALLBACK = new FutureCallback<Void>() {
239         @Override
240         public void onSuccess(Void result) {
241             LOG.debug("Success in Datastore operation");
242         }
243
244         @Override
245         public void onFailure(Throwable error) {
246             LOG.error("Error in Datastore operation", error);
247         }
248     };
249
250     @Inject
251     public ElanUtils(DataBroker dataBroker, IMdsalApiManager mdsalManager,
252             OdlInterfaceRpcService interfaceManagerRpcService, ItmRpcService itmRpcService, ElanConfig elanConfig,
253             IInterfaceManager interfaceManager, ElanEtreeUtils elanEtreeUtils, ElanItmUtils elanItmUtils,
254             ElanInterfaceCache elanInterfaceCache, IITMProvider iitmProvider) {
255         this.broker = dataBroker;
256         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
257         this.mdsalManager = mdsalManager;
258         this.interfaceManagerRpcService = interfaceManagerRpcService;
259         this.itmRpcService = itmRpcService;
260         this.interfaceManager = interfaceManager;
261         this.elanConfig = elanConfig;
262         this.elanEtreeUtils = elanEtreeUtils;
263         this.elanItmUtils = elanItmUtils;
264         this.elanInterfaceCache = elanInterfaceCache;
265         this.iitmProvider = iitmProvider;
266     }
267
268     public final Boolean isOpenstackVniSemanticsEnforced() {
269         return elanConfig.isOpenstackVniSemanticsEnforced() != null
270                 ? elanConfig.isOpenstackVniSemanticsEnforced() : false;
271     }
272
273     /**
274      * Uses the IdManager to retrieve a brand new ElanTag.
275      *
276      * @param idManager
277      *            the id manager
278      * @param idKey
279      *            the id key
280      * @return the integer
281      */
282     public static Long retrieveNewElanTag(IdManagerService idManager, String idKey) {
283
284         AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(ElanConstants.ELAN_ID_POOL_NAME)
285                 .setIdKey(idKey).build();
286
287         try {
288             Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
289             RpcResult<AllocateIdOutput> rpcResult = result.get();
290             if (rpcResult.isSuccessful()) {
291                 return rpcResult.getResult().getIdValue();
292             } else {
293                 LOG.warn("RPC Call to Allocate Id returned with Errors {}", rpcResult.getErrors());
294             }
295         } catch (InterruptedException | ExecutionException e) {
296             LOG.warn("Exception when Allocating Id", e);
297         }
298         return 0L;
299     }
300
301     public static void releaseId(IdManagerService idManager, String poolName, String idKey) {
302         ReleaseIdInput releaseIdInput = new ReleaseIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
303         LoggingFutures.addErrorLogging(idManager.releaseId(releaseIdInput), LOG, "Release Id");
304     }
305
306     /**
307      * Read utility.
308      *
309      * @deprecated Consider using {@link #read2(LogicalDatastoreType, InstanceIdentifier)} with proper exception
310      *             handling instead
311      * @param broker dataBroker
312      * @param datastoreType Logical DataStore type
313      * @param path IID to read
314      * @param <T> T extends DataObject
315      * @return the read value T
316      */
317     @Deprecated
318     @SuppressWarnings("checkstyle:IllegalCatch")
319     public static <T extends DataObject> Optional<T> read(@NonNull DataBroker broker,
320             LogicalDatastoreType datastoreType, InstanceIdentifier<T> path) {
321         try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
322             return tx.read(datastoreType, path).get();
323         } catch (Exception e) {
324             throw new RuntimeException(e);
325         }
326     }
327
328     public <T extends DataObject> Optional<T> read2(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path)
329             throws ReadFailedException {
330         try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
331             CheckedFuture<Optional<T>, ReadFailedException> checkedFuture = tx.read(datastoreType, path);
332             return checkedFuture.checkedGet();
333         }
334     }
335
336     @SuppressWarnings("checkstyle:ForbidCertainMethod")
337     public static <T extends DataObject> void delete(DataBroker broker, LogicalDatastoreType datastoreType,
338             InstanceIdentifier<T> path) {
339         WriteTransaction tx = broker.newWriteOnlyTransaction();
340         tx.delete(datastoreType, path);
341         Futures.addCallback(tx.submit(), DEFAULT_CALLBACK, MoreExecutors.directExecutor());
342     }
343
344     public static InstanceIdentifier<ElanInterface> getElanInterfaceConfigurationDataPathId(String interfaceName) {
345         return InstanceIdentifier.builder(ElanInterfaces.class)
346                 .child(ElanInterface.class, new ElanInterfaceKey(interfaceName)).build();
347     }
348
349     // elan-state Operational container
350     @Nullable
351     public static Elan getElanByName(DataBroker broker, String elanInstanceName) {
352         InstanceIdentifier<Elan> elanIdentifier = getElanInstanceOperationalDataPath(elanInstanceName);
353         return MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, elanIdentifier).orNull();
354     }
355
356     @Nullable
357     public static Elan getElanByName(TypedReadTransaction<Operational> tx, String elanInstanceName)
358             throws ExecutionException, InterruptedException {
359         return tx.read(getElanInstanceOperationalDataPath(elanInstanceName)).get().orNull();
360     }
361
362     public static InstanceIdentifier<Elan> getElanInstanceOperationalDataPath(String elanInstanceName) {
363         return InstanceIdentifier.builder(ElanState.class).child(Elan.class, new ElanKey(elanInstanceName)).build();
364     }
365
366     // grouping of forwarding-entries
367     @Nullable
368     public MacEntry getInterfaceMacEntriesOperationalDataPath(String interfaceName, PhysAddress physAddress) {
369         InstanceIdentifier<MacEntry> existingMacEntryId = getInterfaceMacEntriesIdentifierOperationalDataPath(
370                 interfaceName, physAddress);
371         return read(broker, LogicalDatastoreType.OPERATIONAL, existingMacEntryId).orNull();
372     }
373
374     @Nullable
375     public MacEntry getInterfaceMacEntriesOperationalDataPathFromId(TypedReadTransaction<Operational> tx,
376             InstanceIdentifier<MacEntry> identifier) throws ExecutionException, InterruptedException {
377         return tx.read(identifier).get().orNull();
378     }
379
380     public static InstanceIdentifier<MacEntry> getInterfaceMacEntriesIdentifierOperationalDataPath(String interfaceName,
381             PhysAddress physAddress) {
382         return InstanceIdentifier.builder(ElanInterfaceForwardingEntries.class)
383                 .child(ElanInterfaceMac.class, new ElanInterfaceMacKey(interfaceName))
384                 .child(MacEntry.class, new MacEntryKey(physAddress)).build();
385
386     }
387
388     // elan-forwarding-tables Operational container
389     public Optional<MacEntry> getMacEntryForElanInstance(String elanName, PhysAddress physAddress) {
390         InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanName, physAddress);
391         return read(broker, LogicalDatastoreType.OPERATIONAL, macId);
392     }
393
394     public Optional<MacEntry> getMacEntryForElanInstance(TypedReadTransaction<Operational> tx, String elanName,
395             PhysAddress physAddress) throws ExecutionException, InterruptedException {
396         InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanName, physAddress);
397         return tx.read(macId).get();
398     }
399
400     @Nullable
401     public MacEntry getMacEntryFromElanMacId(TypedReadTransaction<Operational> tx,
402             InstanceIdentifier<MacEntry> identifier) throws ExecutionException, InterruptedException {
403         return tx.read(identifier).get().orNull();
404     }
405
406     public static InstanceIdentifier<MacEntry> getMacEntryOperationalDataPath(String elanName,
407             PhysAddress physAddress) {
408         return InstanceIdentifier.builder(ElanForwardingTables.class).child(MacTable.class, new MacTableKey(elanName))
409                 .child(MacEntry.class, new MacEntryKey(physAddress)).build();
410     }
411
412     public static InstanceIdentifier<MacTable> getElanMacTableOperationalDataPath(String elanName) {
413         return InstanceIdentifier.builder(ElanForwardingTables.class).child(MacTable.class, new MacTableKey(elanName))
414                 .build();
415     }
416
417     // elan-interface-forwarding-entries Operational container
418     public ElanInterfaceMac getElanInterfaceMacByInterfaceName(String interfaceName) {
419         return getElanInterfaceMacByInterfaceName(broker, interfaceName);
420     }
421
422     @Nullable
423     public static ElanInterfaceMac getElanInterfaceMacByInterfaceName(DataBroker dataBroker, String interfaceName) {
424         InstanceIdentifier<ElanInterfaceMac> elanInterfaceId = getElanInterfaceMacEntriesOperationalDataPath(
425                 interfaceName);
426         return read(dataBroker, LogicalDatastoreType.OPERATIONAL, elanInterfaceId).orNull();
427     }
428
429     public static InstanceIdentifier<ElanInterfaceMac> getElanInterfaceMacEntriesOperationalDataPath(
430             String interfaceName) {
431         return InstanceIdentifier.builder(ElanInterfaceForwardingEntries.class)
432                 .child(ElanInterfaceMac.class, new ElanInterfaceMacKey(interfaceName)).build();
433     }
434
435     /**
436      * Returns the list of Interfaces that belong to an Elan on an specific DPN.
437      * Data retrieved from Elan's operational DS: elan-dpn-interfaces container.
438      *
439      * @param elanInstanceName
440      *            name of the Elan to which the interfaces must belong to
441      * @param dpId
442      *            Id of the DPN where the interfaces are located
443      * @return the elan interface Info
444      */
445     @Nullable
446     public DpnInterfaces getElanInterfaceInfoByElanDpn(String elanInstanceName, BigInteger dpId) {
447         InstanceIdentifier<DpnInterfaces> elanDpnInterfacesId = getElanDpnInterfaceOperationalDataPath(elanInstanceName,
448                 dpId);
449         return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfacesId).orNull();
450     }
451
452     /**
453      * Returns the InstanceIdentifier that points to the Interfaces of an Elan
454      * in a given DPN in the Operational DS. Data retrieved from Elans's
455      * operational DS: dpn-interfaces list
456      *
457      * @param elanInstanceName
458      *            name of the Elan to which the interfaces must belong to
459      * @param dpId
460      *            Id of the DPN where the interfaces are located
461      * @return the elan dpn interface
462      */
463     public static InstanceIdentifier<DpnInterfaces> getElanDpnInterfaceOperationalDataPath(String elanInstanceName,
464             BigInteger dpId) {
465         return InstanceIdentifier.builder(ElanDpnInterfaces.class)
466                 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName))
467                 .child(DpnInterfaces.class, new DpnInterfacesKey(dpId)).build();
468     }
469
470     // elan-tag-name-map Operational Container
471     @Nullable
472     public ElanTagName getElanInfoByElanTag(long elanTag) {
473         InstanceIdentifier<ElanTagName> elanId = getElanInfoEntriesOperationalDataPath(elanTag);
474         Optional<ElanTagName> existingElanInfo = read(broker,
475                 LogicalDatastoreType.OPERATIONAL, elanId);
476         return existingElanInfo.orNull();
477     }
478
479     public static InstanceIdentifier<ElanTagName> getElanInfoEntriesOperationalDataPath(long elanTag) {
480         return InstanceIdentifier.builder(ElanTagNameMap.class).child(ElanTagName.class, new ElanTagNameKey(elanTag))
481                 .build();
482     }
483
484     // interface-index-tag operational container
485     public Optional<IfIndexInterface> getInterfaceInfoByInterfaceTag(long interfaceTag) {
486         InstanceIdentifier<IfIndexInterface> interfaceId = getInterfaceInfoEntriesOperationalDataPath(interfaceTag);
487         return read(broker, LogicalDatastoreType.OPERATIONAL, interfaceId);
488     }
489
490     public static InstanceIdentifier<IfIndexInterface> getInterfaceInfoEntriesOperationalDataPath(long interfaceTag) {
491         return InstanceIdentifier.builder(IfIndexesInterfaceMap.class)
492                 .child(IfIndexInterface.class, new IfIndexInterfaceKey((int) interfaceTag)).build();
493     }
494
495     public static InstanceIdentifier<ElanDpnInterfacesList> getElanDpnOperationDataPath(String elanInstanceName) {
496         return InstanceIdentifier.builder(ElanDpnInterfaces.class)
497                 .child(ElanDpnInterfacesList.class, new ElanDpnInterfacesListKey(elanInstanceName)).build();
498     }
499
500     @Nullable
501     public ElanDpnInterfacesList getElanDpnInterfacesList(String elanName) {
502         InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanName);
503         return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId).orNull();
504     }
505
506     @Nullable
507     public ElanDpnInterfaces getElanDpnInterfacesList() {
508         InstanceIdentifier<ElanDpnInterfaces> elanDpnInterfaceId = InstanceIdentifier.builder(ElanDpnInterfaces.class)
509                 .build();
510         return read(broker, LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId).orNull();
511     }
512
513     /**
514      * This method is useful get all ELAN participating CSS dpIds to install
515      * program remote dmac entries and updating remote bc groups for tor
516      * integration.
517      *
518      * @param elanInstanceName
519      *            the elan instance name
520      * @return list of dpIds
521      */
522     @NonNull
523     public List<BigInteger> getParticipatingDpnsInElanInstance(String elanInstanceName) {
524         List<BigInteger> dpIds = new ArrayList<>();
525         InstanceIdentifier<ElanDpnInterfacesList> elanDpnInterfaceId = getElanDpnOperationDataPath(elanInstanceName);
526         Optional<ElanDpnInterfacesList> existingElanDpnInterfaces = read(broker,
527                 LogicalDatastoreType.OPERATIONAL, elanDpnInterfaceId);
528         if (!existingElanDpnInterfaces.isPresent()) {
529             return dpIds;
530         }
531         List<DpnInterfaces> dpnInterfaces = existingElanDpnInterfaces.get().nonnullDpnInterfaces();
532         for (DpnInterfaces dpnInterface : dpnInterfaces) {
533             dpIds.add(dpnInterface.getDpId());
534         }
535         return dpIds;
536     }
537
538     public static long getElanRemoteBroadCastGroupID(long elanTag) {
539         return ElanConstants.ELAN_GID_MIN + elanTag % ElanConstants.ELAN_GID_MIN * 2;
540     }
541
542     /**
543      * Gets the elan mac table.
544      *
545      * @param elanName
546      *            the elan name
547      * @return the elan mac table
548      */
549     @Nullable
550     public MacTable getElanMacTable(String elanName) {
551         return getElanMacTable(broker, elanName);
552     }
553
554     @Nullable
555     public static MacTable getElanMacTable(DataBroker dataBroker, String elanName) {
556         InstanceIdentifier<MacTable> elanMacTableId = getElanMacTableOperationalDataPath(elanName);
557         return read(dataBroker, LogicalDatastoreType.OPERATIONAL, elanMacTableId).orNull();
558     }
559
560     public static long getElanLocalBCGId(long elanTag) {
561         return ElanConstants.ELAN_GID_MIN + (elanTag % ElanConstants.ELAN_GID_MIN * 2 - 1);
562     }
563
564     public static long getElanRemoteBCGId(long elanTag) {
565         return ElanConstants.ELAN_GID_MIN + elanTag % ElanConstants.ELAN_GID_MIN * 2;
566     }
567
568     public static long getEtreeLeafLocalBCGId(long etreeLeafTag) {
569         return ElanConstants.ELAN_GID_MIN + (etreeLeafTag % ElanConstants.ELAN_GID_MIN * 2 - 1);
570     }
571
572     public static long getEtreeLeafRemoteBCGId(long etreeLeafTag) {
573         return ElanConstants.ELAN_GID_MIN + etreeLeafTag % ElanConstants.ELAN_GID_MIN * 2;
574     }
575
576     public static BigInteger getElanMetadataLabel(long elanTag, boolean isSHFlagSet) {
577         int shBit = isSHFlagSet ? 1 : 0;
578         return BigInteger.valueOf(elanTag).shiftLeft(24).or(BigInteger.valueOf(shBit));
579     }
580
581     /**
582      * Setting SMAC, DMAC, UDMAC in this DPN and optionally in other DPNs.
583      *
584      * @param elanInfo
585      *            the elan info
586      * @param interfaceInfo
587      *            the interface info
588      * @param macTimeout
589      *            the mac timeout
590      * @param macAddress
591      *            the mac address
592      * @param configureRemoteFlows
593      *            true if remote dmac flows should be configured as well
594      * @param writeFlowGroupTx
595      *            the flow group tx
596      */
597     public void setupMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
598                               long macTimeout, String macAddress, boolean configureRemoteFlows,
599                               TypedWriteTransaction<Configuration> writeFlowGroupTx) {
600         try (Acquired lock = lockElanMacDPN(elanInfo.getElanTag(), macAddress, interfaceInfo.getDpId())) {
601             setupKnownSmacFlow(elanInfo, interfaceInfo, macTimeout, macAddress, mdsalManager,
602                 writeFlowGroupTx);
603             setupOrigDmacFlows(elanInfo, interfaceInfo, macAddress, configureRemoteFlows, mdsalManager,
604                     writeFlowGroupTx);
605         }
606     }
607
608     public void setupDMacFlowOnRemoteDpn(ElanInstance elanInfo, InterfaceInfo interfaceInfo, BigInteger dstDpId,
609                                          String macAddress, TypedWriteTransaction<Configuration> writeFlowTx) {
610         String elanInstanceName = elanInfo.getElanInstanceName();
611         setupRemoteDmacFlow(dstDpId, interfaceInfo.getDpId(), interfaceInfo.getInterfaceTag(), elanInfo.getElanTag(),
612                 macAddress, elanInstanceName, writeFlowTx, interfaceInfo.getInterfaceName(), elanInfo);
613         LOG.info("Remote Dmac flow entry created for elan Name:{}, logical port Name:{} and"
614                 + " mac address {} on dpn:{}", elanInstanceName, interfaceInfo.getPortName(),
615                 macAddress, dstDpId);
616     }
617
618     /**
619      * Inserts a Flow in SMAC table to state that the MAC has already been
620      * learnt.
621      */
622     private void setupKnownSmacFlow(ElanInstance elanInfo, InterfaceInfo interfaceInfo, long macTimeout,
623             String macAddress, IMdsalApiManager mdsalApiManager,
624             TypedWriteTransaction<Configuration> writeFlowGroupTx) {
625         FlowEntity flowEntity = buildKnownSmacFlow(elanInfo, interfaceInfo, macTimeout, macAddress);
626         mdsalApiManager.addFlow(writeFlowGroupTx, flowEntity);
627         LOG.debug("Known Smac flow entry created for elan Name:{}, logical Interface port:{} and mac address:{}",
628                 elanInfo.getElanInstanceName(), elanInfo.getDescription(), macAddress);
629     }
630
631     public FlowEntity buildKnownSmacFlow(ElanInstance elanInfo, InterfaceInfo interfaceInfo, long macTimeout,
632             String macAddress) {
633         int lportTag = interfaceInfo.getInterfaceTag();
634         // Matching metadata and eth_src fields
635         List<MatchInfo> mkMatches = new ArrayList<>();
636         mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanInfo.getElanTag(), lportTag),
637                 ElanHelper.getElanMetadataMask()));
638         mkMatches.add(new MatchEthernetSource(new MacAddress(macAddress)));
639         List<InstructionInfo> mkInstructions = new ArrayList<>();
640         mkInstructions.add(new InstructionGotoTable(NwConstants.ELAN_DMAC_TABLE));
641         BigInteger dpId = interfaceInfo.getDpId();
642         long elanTag = getElanTag(elanInfo, interfaceInfo);
643         return new FlowEntityBuilder()
644             .setDpnId(dpId)
645             .setTableId(NwConstants.ELAN_SMAC_TABLE)
646             .setFlowId(getKnownDynamicmacFlowRef(NwConstants.ELAN_SMAC_TABLE, dpId, lportTag, macAddress, elanTag))
647             .setPriority(20)
648             .setFlowName(elanInfo.getDescription())
649             .setIdleTimeOut((int) macTimeout)
650             .setHardTimeOut(0)
651             .setCookie(ElanConstants.COOKIE_ELAN_KNOWN_SMAC.add(BigInteger.valueOf(elanTag)))
652             .setMatchInfoList(mkMatches)
653             .setInstructionInfoList(mkInstructions)
654             .setStrictFlag(true)
655             // If Mac timeout is 0, the flow won't be deleted automatically, so no need to get notified
656             .setSendFlowRemFlag(macTimeout != 0)
657             .build();
658     }
659
660     private Long getElanTag(ElanInstance elanInfo, InterfaceInfo interfaceInfo) {
661         EtreeInterface etreeInterface = elanInterfaceCache.getEtreeInterface(interfaceInfo.getInterfaceName()).orNull();
662         if (etreeInterface == null || etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
663             return elanInfo.getElanTag();
664         } else { // Leaf
665             EtreeInstance etreeInstance = elanInfo.augmentation(EtreeInstance.class);
666             if (etreeInstance == null) {
667                 LOG.warn("EtreeInterface {} is connected to a non-Etree network: {}",
668                          interfaceInfo.getInterfaceName(), elanInfo.getElanInstanceName());
669                 return elanInfo.getElanTag();
670             } else {
671                 return etreeInstance.getEtreeLeafTagVal().getValue();
672             }
673         }
674     }
675
676     /**
677      * Installs a Flow in INTERNAL_TUNNEL_TABLE of the affected DPN that sends
678      * the packet through the specified interface if the tunnel_id matches the
679      * interface's lportTag.
680      *
681      * @param interfaceInfo
682      *            the interface info
683      * @param mdsalApiManager
684      *            the mdsal API manager
685      * @param writeFlowGroupTx
686      *            the writeFLowGroup tx
687      */
688     public void setupTermDmacFlows(InterfaceInfo interfaceInfo, IMdsalApiManager mdsalApiManager,
689                                    TypedWriteTransaction<Configuration> writeFlowGroupTx) {
690         BigInteger dpId = interfaceInfo.getDpId();
691         int lportTag = interfaceInfo.getInterfaceTag();
692         Flow flow = MDSALUtil.buildFlowNew(NwConstants.INTERNAL_TUNNEL_TABLE,
693                 getIntTunnelTableFlowRef(NwConstants.INTERNAL_TUNNEL_TABLE, lportTag), 5,
694                 String.format("%s:%d", "ITM Flow Entry ", lportTag), 0, 0,
695                 ITMConstants.COOKIE_ITM.add(BigInteger.valueOf(lportTag)),
696                 getTunnelIdMatchForFilterEqualsLPortTag(lportTag),
697                 getInstructionsInPortForOutGroup(interfaceInfo.getInterfaceName()));
698         mdsalApiManager.addFlow(writeFlowGroupTx, dpId, flow);
699         LOG.debug("Terminating service table flow entry created on dpn:{} for logical Interface port:{}", dpId,
700                 interfaceInfo.getPortName());
701     }
702
703     /**
704      * Constructs the FlowName for flows installed in the Internal Tunnel Table,
705      * consisting in tableId + elanTag.
706      *
707      * @param tableId
708      *            table Id
709      * @param elanTag
710      *            elan Tag
711      * @return the Internal tunnel
712      */
713     public static String getIntTunnelTableFlowRef(short tableId, int elanTag) {
714         return new StringBuilder().append(tableId).append(elanTag).toString();
715     }
716
717     /**
718      * Constructs the Matches that checks that the tunnel_id field contains a
719      * specific lportTag.
720      *
721      * @param lportTag
722      *            lportTag that must be checked against the tunnel_id field
723      * @return the list of match Info
724      */
725     public static List<MatchInfo> getTunnelIdMatchForFilterEqualsLPortTag(int lportTag) {
726         List<MatchInfo> mkMatches = new ArrayList<>();
727         // Matching metadata
728         mkMatches.add(new MatchTunnelId(BigInteger.valueOf(lportTag)));
729         return mkMatches;
730     }
731
732     /**
733      * Constructs the Instructions that take the packet over a given interface.
734      *
735      * @param ifName
736      *            Name of the interface where the packet must be sent over. It
737      *            can be a local interface or a tunnel interface (internal or
738      *            external)
739      * @return the Instruction
740      */
741     public List<Instruction> getInstructionsInPortForOutGroup(String ifName) {
742         List<Instruction> mkInstructions = new ArrayList<>();
743         List<Action> actions = getEgressActionsForInterface(ifName, /* tunnelKey */ null);
744
745         mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
746         return mkInstructions;
747     }
748
749     /**
750      * Returns the list of Actions to be taken when sending the packet through
751      * an Elan interface. Note that this interface can refer to an ElanInterface
752      * where the Elan VM is attached to a DPN or an ITM tunnel interface where
753      * Elan traffic can be sent through. In this latter case, the tunnelKey is
754      * mandatory and it can hold serviceId for internal tunnels or the VNI for
755      * external tunnels.
756      *
757      * @param ifName
758      *            the if name
759      * @param tunnelKey
760      *            the tunnel key
761      * @return the egress actions for interface
762      */
763     @SuppressWarnings("checkstyle:IllegalCatch")
764     @NonNull
765     public List<Action> getEgressActionsForInterface(String ifName, @Nullable Long tunnelKey) {
766         List<Action> listAction = new ArrayList<>();
767         try {
768             GetEgressActionsForInterfaceInput getEgressActionInput = new GetEgressActionsForInterfaceInputBuilder()
769                     .setIntfName(ifName).setTunnelKey(tunnelKey).build();
770             Future<RpcResult<GetEgressActionsForInterfaceOutput>> result = interfaceManagerRpcService
771                     .getEgressActionsForInterface(getEgressActionInput);
772             RpcResult<GetEgressActionsForInterfaceOutput> rpcResult = result.get();
773             if (!rpcResult.isSuccessful()) {
774                 LOG.debug("RPC Call to Get egress actions for interface {} returned with Errors {}", ifName,
775                         rpcResult.getErrors());
776             } else {
777                 listAction = rpcResult.getResult().nonnullAction();
778             }
779         } catch (Exception e) {
780             LOG.warn("Exception when egress actions for interface {}", ifName, e);
781         }
782         return listAction;
783     }
784
785     private void setupOrigDmacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
786                                     boolean configureRemoteFlows, IMdsalApiManager mdsalApiManager,
787                                     TypedWriteTransaction<Configuration> writeFlowGroupTx) {
788         BigInteger dpId = interfaceInfo.getDpId();
789         String ifName = interfaceInfo.getInterfaceName();
790         long ifTag = interfaceInfo.getInterfaceTag();
791         String elanInstanceName = elanInfo.getElanInstanceName();
792
793         Long elanTag = elanInfo.getElanTag();
794
795         setupLocalDmacFlow(elanTag, dpId, ifName, macAddress, elanInfo, mdsalApiManager, ifTag,
796                 writeFlowGroupTx);
797         LOG.debug("Dmac flow entry created for elan Name:{}, logical port Name:{} mand mac address:{} "
798                                     + "on dpn:{}", elanInstanceName, interfaceInfo.getPortName(), macAddress, dpId);
799
800         if (!configureRemoteFlows) {
801             return;
802         }
803
804         List<DpnInterfaces> elanDpns = getInvolvedDpnsInElan(elanInstanceName);
805         for (DpnInterfaces elanDpn : elanDpns) {
806
807             if (Objects.equals(elanDpn.getDpId(), dpId)) {
808                 continue;
809             }
810
811             // Check for the Remote DPN present in Inventory Manager
812             if (!isDpnPresent(elanDpn.getDpId())) {
813                 continue;
814             }
815
816             // For remote DPNs a flow is needed to indicate that packets of this ELAN going to this MAC need to be
817             // forwarded through the appropriate ITM tunnel
818             setupRemoteDmacFlow(elanDpn.getDpId(), // srcDpn (the remote DPN in this case)
819                     dpId, // dstDpn (the local DPN)
820                     interfaceInfo.getInterfaceTag(), // lportTag of the local interface
821                     elanTag,  // identifier of the Elan
822                     macAddress, // MAC to be programmed in remote DPN
823                     elanInstanceName, writeFlowGroupTx, ifName, elanInfo
824             );
825             LOG.debug("Remote Dmac flow entry created for elan Name:{}, logical port Name:{} and mac address:{} on"
826                         + " dpn:{}", elanInstanceName, interfaceInfo.getPortName(), macAddress, elanDpn.getDpId());
827         }
828
829         // TODO: Make sure that the same is performed against the ElanDevices.
830     }
831
832     @NonNull
833     public List<DpnInterfaces> getInvolvedDpnsInElan(String elanName) {
834         return getElanDPNByName(elanName);
835     }
836
837     @NonNull
838     public List<DpnInterfaces> getElanDPNByName(String elanInstanceName) {
839         InstanceIdentifier<ElanDpnInterfacesList> elanIdentifier = getElanDpnOperationDataPath(elanInstanceName);
840         return MDSALUtil.read(broker, LogicalDatastoreType.OPERATIONAL, elanIdentifier).toJavaUtil().map(
841                 ElanDpnInterfacesList::getDpnInterfaces).orElse(emptyList());
842     }
843
844     private void setupLocalDmacFlow(long elanTag, BigInteger dpId, String ifName, String macAddress,
845             ElanInstance elanInfo, IMdsalApiManager mdsalApiManager, long ifTag,
846             TypedWriteTransaction<Configuration> writeFlowGroupTx) {
847         Flow flowEntity = buildLocalDmacFlowEntry(elanTag, dpId, ifName, macAddress, elanInfo, ifTag);
848         mdsalApiManager.addFlow(writeFlowGroupTx, dpId, flowEntity);
849         installEtreeLocalDmacFlow(elanTag, dpId, ifName, macAddress, elanInfo, ifTag, writeFlowGroupTx);
850     }
851
852     private void installEtreeLocalDmacFlow(long elanTag, BigInteger dpId, String ifName, String macAddress,
853             ElanInstance elanInfo, long ifTag, TypedWriteTransaction<Configuration> writeFlowGroupTx) {
854         EtreeInterface etreeInterface = elanInterfaceCache.getEtreeInterface(ifName).orNull();
855         if (etreeInterface != null && etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
856             EtreeLeafTagName etreeTagName = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag);
857             if (etreeTagName == null) {
858                 LOG.warn("Interface {} seems like it belongs to Etree but etreeTagName from elanTag {} is null",
859                         ifName, elanTag);
860             } else {
861                 Flow flowEntity = buildLocalDmacFlowEntry(etreeTagName.getEtreeLeafTag().getValue(), dpId, ifName,
862                         macAddress, elanInfo, ifTag);
863                 mdsalManager.addFlow(writeFlowGroupTx, dpId, flowEntity);
864             }
865         }
866     }
867
868     public static String getKnownDynamicmacFlowRef(short tableId, BigInteger dpId, long lporTag, String macAddress,
869             long elanTag) {
870         return String.valueOf(tableId) + elanTag + dpId + lporTag + macAddress;
871     }
872
873     public static String getKnownDynamicmacFlowRef(short tableId, BigInteger dpId, BigInteger remoteDpId,
874             String macAddress, long elanTag) {
875         return String.valueOf(tableId) + elanTag + dpId + remoteDpId + macAddress;
876     }
877
878     public static String getKnownDynamicmacFlowRef(short tableId, BigInteger dpId, String macAddress, long elanTag) {
879         return String.valueOf(tableId) + elanTag + dpId + macAddress;
880     }
881
882     public static String getKnownDynamicmacFlowRef(short elanDmacTable, BigInteger dpId, String extDeviceNodeId,
883             String dstMacAddress, long elanTag, boolean shFlag) {
884         return String.valueOf(elanDmacTable) + elanTag + dpId + extDeviceNodeId + dstMacAddress + shFlag;
885     }
886
887     /**
888      * Builds the flow to be programmed in the DMAC table of the local DPN (that
889      * is, where the MAC is attached to). This flow consists in:
890      *
891      * <p>Match: + elanTag in metadata + packet goes to a MAC locally attached
892      * Actions: + optionally, pop-vlan + set-vlan-id + output to ifName's
893      * portNumber
894      *
895      * @param elanTag
896      *            the elan tag
897      * @param dpId
898      *            the dp id
899      * @param ifName
900      *            the if name
901      * @param macAddress
902      *            the mac address
903      * @param elanInfo
904      *            the elan info
905      * @param ifTag
906      *            the if tag
907      * @return the flow
908      */
909     public Flow buildLocalDmacFlowEntry(long elanTag, BigInteger dpId, String ifName, String macAddress,
910             ElanInstance elanInfo, long ifTag) {
911
912         List<MatchInfo> mkMatches = new ArrayList<>();
913         mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag), MetaDataUtil.METADATA_MASK_SERVICE));
914         mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddress)));
915
916         List<Instruction> mkInstructions = new ArrayList<>();
917         List<Action> actions = getEgressActionsForInterface(ifName, /* tunnelKey */ null);
918         mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
919         Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE,
920                 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, ifTag, macAddress, elanTag), 20,
921                 elanInfo.getElanInstanceName(), 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(
922                         BigInteger.valueOf(elanTag)), mkMatches, mkInstructions);
923
924         return flow;
925     }
926
927     public void setupRemoteDmacFlow(BigInteger srcDpId, BigInteger destDpId, int lportTag, long elanTag,
928             String macAddress, String displayName, TypedWriteTransaction<Configuration> writeFlowGroupTx,
929             String interfaceName, ElanInstance elanInstance) {
930         if (interfaceManager.isExternalInterface(interfaceName)) {
931             LOG.debug("Ignoring install remote DMAC {} flow on provider interface {} elan {}",
932                     macAddress, interfaceName, elanInstance.getElanInstanceName());
933             return;
934         }
935         Flow flowEntity;
936         // if openstack-vni-semantics are enforced, segmentation ID is passed as network VNI for VxLAN based provider
937         // networks, 0 otherwise
938         long lportTagOrVni = !isOpenstackVniSemanticsEnforced() ? lportTag : isVxlanNetworkOrVxlanSegment(elanInstance)
939                 ? getVxlanSegmentationId(elanInstance) : 0;
940         flowEntity = buildRemoteDmacFlowEntry(srcDpId, destDpId, lportTagOrVni, elanTag, macAddress, displayName,
941                 elanInstance);
942         mdsalManager.addFlow(writeFlowGroupTx, srcDpId, flowEntity);
943         setupEtreeRemoteDmacFlow(srcDpId, destDpId, lportTagOrVni, elanTag, macAddress, displayName, interfaceName,
944                 writeFlowGroupTx, elanInstance);
945     }
946
947     private void setupEtreeRemoteDmacFlow(BigInteger srcDpId, BigInteger destDpId, long lportTagOrVni, long elanTag,
948                                           String macAddress, String displayName, String interfaceName,
949                                           TypedWriteTransaction<Configuration> writeFlowGroupTx,
950                                           ElanInstance elanInstance) {
951         Flow flowEntity;
952         EtreeInterface etreeInterface = elanInterfaceCache.getEtreeInterface(interfaceName).orNull();
953         if (etreeInterface != null && etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
954             EtreeLeafTagName etreeTagName = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag);
955             if (etreeTagName == null) {
956                 LOG.warn("Interface {} seems like it belongs to Etree but etreeTagName from elanTag {} is null.",
957                         interfaceName, elanTag);
958             } else {
959                 flowEntity = buildRemoteDmacFlowEntry(srcDpId, destDpId, lportTagOrVni,
960                         etreeTagName.getEtreeLeafTag().getValue(), macAddress, displayName, elanInstance);
961                 mdsalManager.addFlow(writeFlowGroupTx, srcDpId, flowEntity);
962             }
963         }
964     }
965
966     /**
967      * Builds a Flow to be programmed in a remote DPN's DMAC table. This flow
968      * consists in: Match: + elanTag in packet's metadata + packet going to a
969      * MAC known to be located in another DPN Actions: + set_tunnel_id
970      * + output ITM internal tunnel interface with the other DPN
971      *
972      * @param srcDpId
973      *            the src Dpn Id
974      * @param destDpId
975      *            dest Dp Id
976      * @param lportTagOrVni
977      *            lportTag or network VNI
978      * @param elanTag
979      *            elan Tag
980      * @param macAddress
981      *            macAddress
982      * @param displayName
983      *            display Name
984      * @param elanInstance
985      *            elanInstance
986      * @return the flow remote Dmac
987      */
988     @SuppressWarnings("checkstyle:IllegalCatch")
989     public Flow buildRemoteDmacFlowEntry(BigInteger srcDpId, BigInteger destDpId, long lportTagOrVni, long elanTag,
990             String macAddress, String displayName, ElanInstance elanInstance) {
991         List<MatchInfo> mkMatches = new ArrayList<>();
992         mkMatches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag), MetaDataUtil.METADATA_MASK_SERVICE));
993         mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddress)));
994
995         List<Instruction> mkInstructions = new ArrayList<>();
996
997         // List of Action for the provided Source and Destination DPIDs
998         try {
999             List<Action> actions = null;
1000             if (isVxlanNetworkOrVxlanSegment(elanInstance)) {
1001                 actions = elanItmUtils.getInternalTunnelItmEgressAction(srcDpId, destDpId, lportTagOrVni);
1002             } else if (isVlan(elanInstance) || isFlat(elanInstance)) {
1003                 String interfaceName = getExternalElanInterface(elanInstance.getElanInstanceName(), srcDpId);
1004                 if (null == interfaceName) {
1005                     LOG.info("buildRemoteDmacFlowEntry: Could not find interfaceName for {} {}", srcDpId,
1006                             elanInstance);
1007                 }
1008                 actions = getEgressActionsForInterface(interfaceName, null);
1009             }
1010             mkInstructions.add(MDSALUtil.buildApplyActionsInstruction(actions));
1011         } catch (Exception e) {
1012             LOG.error("Could not get egress actions to add to flow for srcDpId {}, destDpId {}, lportTag/VNI {}",
1013                     srcDpId,  destDpId, lportTagOrVni, e);
1014         }
1015
1016         Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_DMAC_TABLE,
1017                 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, srcDpId, destDpId, macAddress, elanTag),
1018                 20, /* prio */
1019                 displayName, 0, /* idleTimeout */
1020                 0, /* hardTimeout */
1021                 ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(BigInteger.valueOf(elanTag)), mkMatches, mkInstructions);
1022
1023         return flow;
1024
1025     }
1026
1027     public void deleteMacFlows(@Nullable ElanInstance elanInfo, @Nullable InterfaceInfo interfaceInfo,
1028             MacEntry macEntry, TypedReadWriteTransaction<Configuration> flowTx)
1029             throws ExecutionException, InterruptedException {
1030         if (elanInfo == null || interfaceInfo == null) {
1031             return;
1032         }
1033         String macAddress = macEntry.getMacAddress().getValue();
1034         try (Acquired lock = lockElanMacDPN(elanInfo.getElanTag(), macAddress, interfaceInfo.getDpId())) {
1035             deleteMacFlows(elanInfo, interfaceInfo, macAddress, /* alsoDeleteSMAC */ true, flowTx);
1036         }
1037     }
1038
1039     public void deleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1040             boolean deleteSmac, TypedReadWriteTransaction<Configuration> flowTx)
1041             throws ExecutionException, InterruptedException {
1042         String elanInstanceName = elanInfo.getElanInstanceName();
1043         List<DpnInterfaces> remoteFEs = getInvolvedDpnsInElan(elanInstanceName);
1044         BigInteger srcdpId = interfaceInfo.getDpId();
1045         boolean isFlowsRemovedInSrcDpn = false;
1046         for (DpnInterfaces dpnInterface : remoteFEs) {
1047             Long elanTag = elanInfo.getElanTag();
1048             BigInteger dstDpId = dpnInterface.getDpId();
1049             if (executeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1050                     elanTag, dstDpId, flowTx)) {
1051                 isFlowsRemovedInSrcDpn = true;
1052             }
1053             executeEtreeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1054                     elanTag, dstDpId, flowTx);
1055         }
1056         if (!isFlowsRemovedInSrcDpn) {
1057             deleteSmacAndDmacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, flowTx);
1058         }
1059     }
1060
1061     private void executeEtreeDeleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1062             boolean deleteSmac, String elanInstanceName, BigInteger srcdpId, Long elanTag, BigInteger dstDpId,
1063             TypedReadWriteTransaction<Configuration> flowTx) throws ExecutionException, InterruptedException {
1064         EtreeLeafTagName etreeLeafTag = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag);
1065         if (etreeLeafTag != null) {
1066             executeDeleteMacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, elanInstanceName, srcdpId,
1067                     etreeLeafTag.getEtreeLeafTag().getValue(), dstDpId, flowTx);
1068         }
1069     }
1070
1071     private boolean executeDeleteMacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1072             boolean deleteSmac, String elanInstanceName, BigInteger srcdpId, Long elanTag, BigInteger dstDpId,
1073             TypedReadWriteTransaction<Configuration> flowTx) throws ExecutionException, InterruptedException {
1074         boolean isFlowsRemovedInSrcDpn = false;
1075         if (dstDpId.equals(srcdpId)) {
1076             isFlowsRemovedInSrcDpn = true;
1077             deleteSmacAndDmacFlows(elanInfo, interfaceInfo, macAddress, deleteSmac, flowTx);
1078         } else if (isDpnPresent(dstDpId)) {
1079             mdsalManager
1080                 .removeFlow(flowTx, dstDpId,
1081                     MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE, getKnownDynamicmacFlowRef(
1082                         NwConstants.ELAN_DMAC_TABLE, dstDpId, srcdpId, macAddress, elanTag)));
1083             LOG.debug("Dmac flow entry deleted for elan:{}, logical interface port:{} and mac address:{} on dpn:{}",
1084                     elanInstanceName, interfaceInfo.getPortName(), macAddress, dstDpId);
1085         }
1086         return isFlowsRemovedInSrcDpn;
1087     }
1088
1089     private void deleteSmacAndDmacFlows(ElanInstance elanInfo, InterfaceInfo interfaceInfo, String macAddress,
1090             boolean deleteSmac, TypedReadWriteTransaction<Configuration> flowTx)
1091             throws ExecutionException, InterruptedException {
1092         String elanInstanceName = elanInfo.getElanInstanceName();
1093         long ifTag = interfaceInfo.getInterfaceTag();
1094         BigInteger srcdpId = interfaceInfo.getDpId();
1095         Long elanTag = elanInfo.getElanTag();
1096         if (deleteSmac) {
1097             mdsalManager
1098                     .removeFlow(flowTx, srcdpId,
1099                             MDSALUtil.buildFlow(NwConstants.ELAN_SMAC_TABLE, getKnownDynamicmacFlowRef(
1100                                     NwConstants.ELAN_SMAC_TABLE, srcdpId, ifTag, macAddress, elanTag)));
1101         }
1102         mdsalManager.removeFlow(flowTx, srcdpId,
1103             MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE,
1104                 getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, srcdpId, ifTag, macAddress, elanTag)));
1105         LOG.debug("All the required flows deleted for elan:{}, logical Interface port:{} and MAC address:{} on dpn:{}",
1106                 elanInstanceName, interfaceInfo.getPortName(), macAddress, srcdpId);
1107     }
1108
1109     /**
1110      * Updates the Elan information in the Operational DS. It also updates the
1111      * ElanInstance in the Config DS by setting the adquired elanTag.
1112      *
1113      * @param idManager
1114      *            the id manager
1115      * @param elanInstanceAdded
1116      *            the elan instance added
1117      * @param elanInterfaces
1118      *            the elan interfaces
1119      * @param operTx
1120      *            transaction
1121      *
1122      * @return the updated ELAN instance.
1123      */
1124     public static ElanInstance updateOperationalDataStore(IdManagerService idManager,
1125             ElanInstance elanInstanceAdded, List<String> elanInterfaces, TypedWriteTransaction<Configuration> confTx,
1126             TypedWriteTransaction<Operational> operTx) {
1127         String elanInstanceName = elanInstanceAdded.getElanInstanceName();
1128         Long elanTag = elanInstanceAdded.getElanTag();
1129         if (elanTag == null || elanTag == 0L) {
1130             elanTag = retrieveNewElanTag(idManager, elanInstanceName);
1131         }
1132         Elan elanInfo = new ElanBuilder().setName(elanInstanceName).setElanInterfaces(elanInterfaces)
1133                 .withKey(new ElanKey(elanInstanceName)).build();
1134
1135         // Add the ElanState in the elan-state operational data-store
1136         operTx.put(getElanInstanceOperationalDataPath(elanInstanceName), elanInfo, CREATE_MISSING_PARENTS);
1137
1138         // Add the ElanMacTable in the elan-mac-table operational data-store
1139         MacTable elanMacTable = new MacTableBuilder().withKey(new MacTableKey(elanInstanceName)).build();
1140         operTx.put(getElanMacTableOperationalDataPath(elanInstanceName), elanMacTable, CREATE_MISSING_PARENTS);
1141
1142         ElanTagNameBuilder elanTagNameBuilder = new ElanTagNameBuilder().setElanTag(elanTag)
1143                 .withKey(new ElanTagNameKey(elanTag)).setName(elanInstanceName);
1144         long etreeLeafTag = -1;
1145         if (isEtreeInstance(elanInstanceAdded)) {
1146             etreeLeafTag = retrieveNewElanTag(idManager,elanInstanceName + ElanConstants
1147                     .LEAVES_POSTFIX);
1148             EtreeLeafTagName etreeLeafTagName = new EtreeLeafTagNameBuilder()
1149                     .setEtreeLeafTag(new EtreeLeafTag(etreeLeafTag)).build();
1150             elanTagNameBuilder.addAugmentation(EtreeLeafTagName.class, etreeLeafTagName);
1151             addTheLeafTagAsElanTag(elanInstanceName, etreeLeafTag, operTx);
1152         }
1153         ElanTagName elanTagName = elanTagNameBuilder.build();
1154
1155         // Add the ElanTag to ElanName in the elan-tag-name Operational
1156         // data-store
1157         operTx.put(getElanInfoEntriesOperationalDataPath(elanTag), elanTagName);
1158
1159         // Updates the ElanInstance Config DS by setting the just acquired
1160         // elanTag
1161         ElanInstanceBuilder elanInstanceBuilder = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
1162                 .setDescription(elanInstanceAdded.getDescription())
1163                 .setMacTimeout(elanInstanceAdded.getMacTimeout() == null
1164                         ? Long.valueOf(ElanConstants.DEFAULT_MAC_TIME_OUT) : elanInstanceAdded.getMacTimeout())
1165                 .withKey(elanInstanceAdded.key()).setElanTag(elanTag);
1166         if (isEtreeInstance(elanInstanceAdded)) {
1167             EtreeInstance etreeInstance = new EtreeInstanceBuilder().setEtreeLeafTagVal(new EtreeLeafTag(etreeLeafTag))
1168                     .build();
1169             elanInstanceBuilder.addAugmentation(EtreeInstance.class, etreeInstance);
1170         }
1171         ElanInstance elanInstanceWithTag = elanInstanceBuilder.build();
1172         confTx.merge(ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstanceWithTag,
1173             CREATE_MISSING_PARENTS);
1174         return elanInstanceWithTag;
1175     }
1176
1177     private static void addTheLeafTagAsElanTag(String elanInstanceName, long etreeLeafTag,
1178             TypedWriteTransaction<Operational> tx) {
1179         ElanTagName etreeTagAsElanTag = new ElanTagNameBuilder().setElanTag(etreeLeafTag)
1180                 .withKey(new ElanTagNameKey(etreeLeafTag)).setName(elanInstanceName).build();
1181         tx.put(getElanInfoEntriesOperationalDataPath(etreeLeafTag), etreeTagAsElanTag);
1182     }
1183
1184     private static boolean isEtreeInstance(ElanInstance elanInstanceAdded) {
1185         return elanInstanceAdded.augmentation(EtreeInstance.class) != null;
1186     }
1187
1188     public boolean isDpnPresent(BigInteger dpnId) {
1189         String dpn = String.format("%s:%s", "openflow", dpnId);
1190         NodeId nodeId = new NodeId(dpn);
1191
1192         InstanceIdentifier<Node> node = InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(nodeId))
1193                 .build();
1194         return read(broker, LogicalDatastoreType.CONFIGURATION, node).isPresent();
1195     }
1196
1197     public static String getElanServiceName(String elanName, String interfaceName) {
1198         return "elan." + elanName + interfaceName;
1199     }
1200
1201     public static BoundServices getBoundServices(String serviceName, short servicePriority, int flowPriority,
1202             BigInteger cookie, List<Instruction> instructions) {
1203         StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookie).setFlowPriority(flowPriority)
1204                 .setInstruction(instructions);
1205         return new BoundServicesBuilder().withKey(new BoundServicesKey(servicePriority)).setServiceName(serviceName)
1206                 .setServicePriority(servicePriority).setServiceType(ServiceTypeFlowBased.class)
1207                 .addAugmentation(StypeOpenflow.class, augBuilder.build()).build();
1208     }
1209
1210     public static InstanceIdentifier<BoundServices> buildServiceId(String interfaceName, short serviceIndex) {
1211         return InstanceIdentifier.builder(ServiceBindings.class)
1212                 .child(ServicesInfo.class, new ServicesInfoKey(interfaceName, ServiceModeIngress.class))
1213                 .child(BoundServices.class, new BoundServicesKey(serviceIndex)).build();
1214     }
1215
1216     public static List<MatchInfo> getTunnelMatchesForServiceId(int elanTag) {
1217         List<MatchInfo> mkMatches = new ArrayList<>();
1218         // Matching metadata
1219         mkMatches.add(new MatchTunnelId(BigInteger.valueOf(elanTag)));
1220
1221         return mkMatches;
1222     }
1223
1224     public void removeTerminatingServiceAction(BigInteger destDpId, int serviceId) {
1225         RemoveTerminatingServiceActionsInput input = new RemoveTerminatingServiceActionsInputBuilder()
1226                 .setDpnId(destDpId).setServiceId(serviceId).build();
1227         Future<RpcResult<RemoveTerminatingServiceActionsOutput>> futureObject =
1228                 itmRpcService.removeTerminatingServiceActions(input);
1229         try {
1230             RpcResult<RemoveTerminatingServiceActionsOutput> result = futureObject.get();
1231             if (result.isSuccessful()) {
1232                 LOG.debug("Successfully completed removeTerminatingServiceActions for ELAN with serviceId {} on "
1233                                 + "dpn {}", serviceId, destDpId);
1234             } else {
1235                 LOG.debug("Failure in removeTerminatingServiceAction RPC call for ELAN with serviceId {} on "
1236                         + "dpn {}", serviceId, destDpId);
1237             }
1238         } catch (InterruptedException | ExecutionException e) {
1239             LOG.error("Error in RPC call removeTerminatingServiceActions for ELAN with serviceId {} on "
1240                     + "dpn {}", serviceId, destDpId, e);
1241         }
1242     }
1243
1244     /**
1245      * Gets the external tunnel.
1246      *
1247      * @param sourceDevice
1248      *            the source device
1249      * @param destinationDevice
1250      *            the destination device
1251      * @param datastoreType
1252      *            the datastore type
1253      * @return the external tunnel
1254      */
1255     @Nullable
1256     public ExternalTunnel getExternalTunnel(String sourceDevice, String destinationDevice,
1257             LogicalDatastoreType datastoreType) {
1258         Class<? extends TunnelTypeBase> tunType = TunnelTypeVxlan.class;
1259         InstanceIdentifier<ExternalTunnel> iid = InstanceIdentifier.builder(ExternalTunnelList.class)
1260                 .child(ExternalTunnel.class, new ExternalTunnelKey(destinationDevice, sourceDevice, tunType)).build();
1261         return read(broker, datastoreType, iid).orNull();
1262     }
1263
1264     /**
1265      * Gets the external tunnel.
1266      *
1267      * @param interfaceName
1268      *            the interface name
1269      * @param datastoreType
1270      *            the datastore type
1271      * @return the external tunnel
1272      */
1273     @Nullable
1274     public ExternalTunnel getExternalTunnel(String interfaceName, LogicalDatastoreType datastoreType) {
1275         ExternalTunnel externalTunnel = null;
1276         List<ExternalTunnel> externalTunnels = getAllExternalTunnels(datastoreType);
1277         for (ExternalTunnel tunnel : externalTunnels) {
1278             if (StringUtils.equalsIgnoreCase(interfaceName, tunnel.getTunnelInterfaceName())) {
1279                 externalTunnel = tunnel;
1280                 break;
1281             }
1282         }
1283         return externalTunnel;
1284     }
1285
1286     /**
1287      * Gets the all external tunnels.
1288      *
1289      * @param datastoreType
1290      *            the data store type
1291      * @return the all external tunnels
1292      */
1293     public List<ExternalTunnel> getAllExternalTunnels(LogicalDatastoreType datastoreType) {
1294         InstanceIdentifier<ExternalTunnelList> iid = InstanceIdentifier.builder(ExternalTunnelList.class).build();
1295         return read(broker, datastoreType, iid).toJavaUtil().map(ExternalTunnelList::getExternalTunnel).orElse(
1296                 emptyList());
1297     }
1298
1299     public static List<MatchInfo> buildMatchesForElanTagShFlagAndDstMac(long elanTag, boolean shFlag, String macAddr) {
1300         List<MatchInfo> mkMatches = new ArrayList<>();
1301         mkMatches.add(
1302                 new MatchMetadata(getElanMetadataLabel(elanTag, shFlag), MetaDataUtil.METADATA_MASK_SERVICE_SH_FLAG));
1303         mkMatches.add(new MatchEthernetDestination(new MacAddress(macAddr)));
1304         return mkMatches;
1305     }
1306
1307     /**
1308      * Checks if is interface operational.
1309      *
1310      * @param interfaceName
1311      *            the interface name
1312      * @param dataBroker
1313      *            the data broker
1314      * @return true, if is interface operational
1315      */
1316     public static boolean isInterfaceOperational(String interfaceName, DataBroker dataBroker) {
1317         if (StringUtils.isBlank(interfaceName)) {
1318             return false;
1319         }
1320         Interface ifState = getInterfaceStateFromOperDS(interfaceName, dataBroker);
1321         if (ifState == null) {
1322             return false;
1323         }
1324         return ifState.getOperStatus() == OperStatus.Up && ifState.getAdminStatus() == AdminStatus.Up;
1325     }
1326
1327     /**
1328      * Gets the interface state from operational ds.
1329      *
1330      * @param interfaceName
1331      *            the interface name
1332      * @param dataBroker
1333      *            the data broker
1334      * @return the interface state from oper ds
1335      */
1336     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
1337             .@Nullable Interface getInterfaceStateFromOperDS(String interfaceName, DataBroker dataBroker) {
1338         return MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL,
1339             createInterfaceStateInstanceIdentifier(interfaceName)).orNull();
1340     }
1341
1342     /**
1343      * Creates the interface state instance identifier.
1344      *
1345      * @param interfaceName
1346      *            the interface name
1347      * @return the instance identifier
1348      */
1349     public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1350         .ietf.interfaces.rev140508.interfaces.state.Interface> createInterfaceStateInstanceIdentifier(
1351             String interfaceName) {
1352         InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1353             .ietf.interfaces.rev140508.interfaces.state.Interface> idBuilder = InstanceIdentifier
1354                 .builder(InterfacesState.class)
1355                 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1356                         .ietf.interfaces.rev140508.interfaces.state.Interface.class,
1357                         new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1358                             .ietf.interfaces.rev140508.interfaces.state.InterfaceKey(
1359                                 interfaceName));
1360         return idBuilder.build();
1361     }
1362
1363     @CheckReturnValue
1364     public static ListenableFuture<Void> waitForTransactionToComplete(ListenableFuture<Void> future) {
1365         try {
1366             future.get();
1367         } catch (InterruptedException | ExecutionException e) {
1368             // NETVIRT-1215: Do not log.error() here, only debug(); but callers *MUST* @CheckReturnValue
1369             LOG.debug("Error writing to datastore", e);
1370         }
1371         return future;
1372     }
1373
1374     public static boolean isVxlan(@Nullable ElanInstance elanInstance) {
1375         return elanInstance != null && elanInstance.getSegmentType() != null
1376                 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1377                 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId() != 0;
1378     }
1379
1380     private static boolean isVxlanSegment(@Nullable ElanInstance elanInstance) {
1381         if (elanInstance != null) {
1382             List<ElanSegments> elanSegments = elanInstance.getElanSegments();
1383             if (elanSegments != null) {
1384                 for (ElanSegments segment : elanSegments) {
1385                     if (segment != null && segment.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1386                             && segment.getSegmentationId() != null
1387                             && segment.getSegmentationId().longValue() != 0) {
1388                         return true;
1389                     }
1390                 }
1391             }
1392         }
1393         return false;
1394     }
1395
1396     public static boolean isVxlanNetworkOrVxlanSegment(@Nullable ElanInstance elanInstance) {
1397         return isVxlan(elanInstance) || isVxlanSegment(elanInstance);
1398     }
1399
1400     public static Long getVxlanSegmentationId(ElanInstance elanInstance) {
1401         Long segmentationId = 0L;
1402         if (elanInstance == null) {
1403             return segmentationId;
1404         }
1405
1406         if (elanInstance.getSegmentType() != null
1407                 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1408                 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId().longValue() != 0) {
1409             segmentationId = elanInstance.getSegmentationId();
1410         } else {
1411             for (ElanSegments segment: elanInstance.getElanSegments()) {
1412                 if (segment != null && segment.getSegmentType().isAssignableFrom(SegmentTypeVxlan.class)
1413                     && segment.getSegmentationId() != null
1414                     && segment.getSegmentationId().longValue() != 0) {
1415                     segmentationId = segment.getSegmentationId();
1416                 }
1417             }
1418         }
1419         return segmentationId;
1420     }
1421
1422     public static boolean isVlan(ElanInstance elanInstance) {
1423         return elanInstance != null && elanInstance.getSegmentType() != null
1424                 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeVlan.class)
1425                 && elanInstance.getSegmentationId() != null && elanInstance.getSegmentationId() != 0;
1426     }
1427
1428     public static boolean isFlat(ElanInstance elanInstance) {
1429         return elanInstance != null && elanInstance.getSegmentType() != null
1430                 && elanInstance.getSegmentType().isAssignableFrom(SegmentTypeFlat.class);
1431     }
1432
1433     public void addDmacRedirectToDispatcherFlows(Long elanTag, String displayName,
1434             String macAddress, List<BigInteger> dpnIds) {
1435         for (BigInteger dpId : dpnIds) {
1436             LoggingFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
1437                 tx -> mdsalManager.addFlow(tx, buildDmacRedirectToDispatcherFlow(dpId, macAddress, displayName,
1438                         elanTag))), LOG,
1439                 "Error adding DMAC redirect to dispatcher flows");
1440         }
1441     }
1442
1443     public void removeDmacRedirectToDispatcherFlows(Long elanTag, String macAddress, List<BigInteger> dpnIds) {
1444         LoggingFutures.addErrorLogging(
1445             txRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.CONFIGURATION, tx -> {
1446                 for (BigInteger dpId : dpnIds) {
1447                     String flowId = getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, macAddress, elanTag);
1448                     mdsalManager.removeFlow(tx, dpId, MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE, flowId));
1449                 }
1450             }), LOG, "Error removing DMAC redirect to dispatcher flows");
1451     }
1452
1453     public static FlowEntity buildDmacRedirectToDispatcherFlow(BigInteger dpId, String dstMacAddress,
1454             String displayName, long elanTag) {
1455         List<MatchInfo> matches = new ArrayList<>();
1456         matches.add(new MatchMetadata(ElanHelper.getElanMetadataLabel(elanTag), MetaDataUtil.METADATA_MASK_SERVICE));
1457         matches.add(new MatchEthernetDestination(new MacAddress(dstMacAddress)));
1458         List<InstructionInfo> instructions = new ArrayList<>();
1459         List<ActionInfo> actions = new ArrayList<>();
1460         actions.add(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
1461
1462         instructions.add(new InstructionApplyActions(actions));
1463         String flowId = getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, dstMacAddress, elanTag);
1464         return MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE, flowId, 20, displayName, 0, 0,
1465                 ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(BigInteger.valueOf(elanTag)),
1466                 matches, instructions);
1467     }
1468
1469     @Nullable
1470     public String getExternalElanInterface(String elanInstanceName, BigInteger dpnId) {
1471         DpnInterfaces dpnInterfaces = getElanInterfaceInfoByElanDpn(elanInstanceName, dpnId);
1472         if (dpnInterfaces == null || dpnInterfaces.getInterfaces() == null) {
1473             LOG.trace("Elan {} does not have interfaces in DPN {}", elanInstanceName, dpnId);
1474             return null;
1475         }
1476
1477         for (String dpnInterface : dpnInterfaces.getInterfaces()) {
1478             if (interfaceManager.isExternalInterface(dpnInterface)) {
1479                 return dpnInterface;
1480             }
1481         }
1482
1483         LOG.trace("Elan {} does not have any external interace attached to DPN {}", elanInstanceName, dpnId);
1484         return null;
1485     }
1486
1487     public static Acquired lockElanMacDPN(long elanTag, String macAddress, BigInteger dpnId) {
1488         return ELAN_LOCKS.acquire(new ElanLockName(elanTag, macAddress, dpnId));
1489     }
1490
1491     public static List<ListenableFuture<Void>>
1492         returnFailedListenableFutureIfTransactionCommitFailedExceptionCauseOrElseThrow(RuntimeException exception) {
1493
1494         Throwable cause = exception.getCause();
1495         if (cause != null && cause instanceof TransactionCommitFailedException) {
1496             return Collections.singletonList(Futures.immediateFailedFuture(cause));
1497         } else {
1498             throw exception;
1499         }
1500     }
1501
1502     public static List<PhysAddress> getPhysAddress(List<String> macAddress) {
1503         Preconditions.checkNotNull(macAddress, "macAddress cannot be null");
1504         List<PhysAddress> physAddresses = new ArrayList<>();
1505         for (String mac : macAddress) {
1506             physAddresses.add(new PhysAddress(mac));
1507         }
1508         return physAddresses;
1509     }
1510
1511     public static List<StaticMacEntries> getStaticMacEntries(List<String> staticMacAddresses) {
1512         if (isEmpty(staticMacAddresses)) {
1513             return Collections.EMPTY_LIST;
1514         }
1515         StaticMacEntriesBuilder staticMacEntriesBuilder = new StaticMacEntriesBuilder();
1516         List<StaticMacEntries> staticMacEntries = new ArrayList<>();
1517         List<PhysAddress> physAddressList = getPhysAddress(staticMacAddresses);
1518         for (PhysAddress physAddress : physAddressList) {
1519             staticMacEntries.add(staticMacEntriesBuilder.setMacAddress(physAddress).build());
1520         }
1521         return staticMacEntries;
1522     }
1523
1524     public static InstanceIdentifier<StaticMacEntries> getStaticMacEntriesCfgDataPathIdentifier(String interfaceName,
1525                                                                                                 String macAddress) {
1526         return InstanceIdentifier.builder(ElanInterfaces.class)
1527                 .child(ElanInterface.class, new ElanInterfaceKey(interfaceName)).child(StaticMacEntries.class,
1528                         new StaticMacEntriesKey(new PhysAddress(macAddress))).build();
1529     }
1530
1531     public static <T> List<T> diffOf(List<T> orig, List<T> updated) {
1532         if (isEmpty(orig)) {
1533             return Collections.EMPTY_LIST;
1534         }
1535         List<T> diff = Lists.newArrayList(orig);
1536         if (isNotEmpty(updated)) {
1537             diff.removeAll(updated);
1538         }
1539         return diff;
1540     }
1541
1542     public static boolean isEmpty(Collection collection) {
1543         return collection == null || collection.isEmpty();
1544     }
1545
1546     public static boolean isNotEmpty(Collection collection) {
1547         return !isEmpty(collection);
1548     }
1549
1550     public Optional<IpAddress> getSourceIpAddress(Ethernet ethernet) {
1551         Optional<IpAddress> srcIpAddress = Optional.absent();
1552         if (ethernet.getPayload() == null) {
1553             return srcIpAddress;
1554         }
1555         byte[] ipAddrBytes = null;
1556         if (ethernet.getPayload() instanceof IPv4) {
1557             IPv4 ipv4 = (IPv4) ethernet.getPayload();
1558             ipAddrBytes = Ints.toByteArray(ipv4.getSourceAddress());
1559         } else if (ethernet.getPayload() instanceof ARP) {
1560             ipAddrBytes = ((ARP) ethernet.getPayload()).getSenderProtocolAddress();
1561         }
1562         if (ipAddrBytes != null) {
1563             String ipAddr = NWUtil.toStringIpAddress(ipAddrBytes);
1564             return Optional.of(IpAddressBuilder.getDefaultInstance(ipAddr));
1565         }
1566         return srcIpAddress;
1567     }
1568
1569     public List<MacEntry> getElanMacEntries(String elanName) {
1570         MacTable macTable = getElanMacTable(elanName);
1571         if (macTable == null) {
1572             return emptyList();
1573         }
1574         return macTable.getMacEntry();
1575     }
1576
1577     public boolean isTunnelInLogicalGroup(String interfaceName) {
1578         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
1579             .ietf.interfaces.rev140508.interfaces.Interface configIface =
1580             interfaceManager.getInterfaceInfoFromConfigDataStore(interfaceName);
1581         if (configIface == null) {
1582             configIface = iitmProvider.getInterface(interfaceName);
1583         }
1584         if (configIface == null) {
1585             return  false;
1586         }
1587         IfTunnel ifTunnel = configIface.augmentation(IfTunnel.class);
1588         if (ifTunnel != null && ifTunnel.getTunnelInterfaceType().isAssignableFrom(TunnelTypeVxlan.class)) {
1589             ParentRefs refs = configIface.augmentation(ParentRefs.class);
1590             return refs != null && !Strings.isNullOrEmpty(refs.getParentInterface());
1591         }
1592         return false;
1593     }
1594
1595     public static InstanceIdentifier<Flow> getFlowIid(Flow flow, BigInteger dpnId) {
1596         FlowKey flowKey = new FlowKey(new FlowId(flow.getId()));
1597         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId nodeId =
1598                 new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId("openflow:" + dpnId);
1599         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node nodeDpn =
1600                 new NodeBuilder().setId(nodeId).withKey(new NodeKey(nodeId)).build();
1601         return InstanceIdentifier.builder(Nodes.class)
1602                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class,
1603                         nodeDpn.key()).augmentation(FlowCapableNode.class)
1604                 .child(Table.class, new TableKey(flow.getTableId())).child(Flow.class, flowKey).build();
1605     }
1606
1607     public static String getElanInterfaceJobKey(String interfaceName) {
1608         return "elaninterface-" + interfaceName;
1609     }
1610
1611     public void removeArpResponderFlow(BigInteger dpnId, String ingressInterfaceName, String ipAddress,
1612             int lportTag) {
1613         LOG.info("Removing the ARP responder flow on DPN {} of Interface {} with IP {}", dpnId, ingressInterfaceName,
1614                 ipAddress);
1615         LoggingFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.CONFIGURATION,
1616             tx -> mdsalManager.removeFlow(tx, dpnId, ArpResponderUtil.getFlowId(lportTag, ipAddress),
1617                 NwConstants.ARP_RESPONDER_TABLE)), LOG, "Error removing ARP responder flow");
1618     }
1619
1620     @Nullable
1621     public static String getRouterPordIdFromElanInstance(DataBroker dataBroker, String elanInstanceName) {
1622         Optional<Subnetmaps> subnetMapsData =
1623                 read(dataBroker, LogicalDatastoreType.CONFIGURATION, buildSubnetMapsWildCardPath());
1624         if (subnetMapsData.isPresent()) {
1625             List<Subnetmap> subnetMapList = subnetMapsData.get().getSubnetmap();
1626             if (subnetMapList != null && !subnetMapList.isEmpty()) {
1627                 for (Subnetmap subnet : subnetMapList) {
1628                     if (subnet.getNetworkId().getValue().equals(elanInstanceName)) {
1629                         if (subnet.getRouterInterfacePortId() != null) {
1630                             return subnet.getRouterInterfacePortId().getValue();
1631                         }
1632                     }
1633                 }
1634             }
1635         }
1636         return null;
1637     }
1638
1639     static InstanceIdentifier<Subnetmaps> buildSubnetMapsWildCardPath() {
1640         return InstanceIdentifier.create(Subnetmaps.class);
1641     }
1642
1643     public static InstanceIdentifier<Group> getGroupInstanceid(BigInteger dpnId, long groupId) {
1644         return InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId)))
1645                 .augmentation(FlowCapableNode.class).child(Group.class, new GroupKey(new GroupId(groupId))).build();
1646     }
1647 }