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