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