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