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