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