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