Fix bugs related to wrong exception handling
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanInterfaceManager.java
1 /*
2  * Copyright (c) 2016 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.internal;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Queue;
20 import java.util.Set;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.ConcurrentLinkedQueue;
23 import java.util.concurrent.ConcurrentMap;
24 import org.apache.commons.lang3.StringUtils;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
29 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
30 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
31 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
32 import org.opendaylight.genius.itm.globals.ITMConstants;
33 import org.opendaylight.genius.mdsalutil.ActionInfo;
34 import org.opendaylight.genius.mdsalutil.ActionType;
35 import org.opendaylight.genius.mdsalutil.FlowEntity;
36 import org.opendaylight.genius.mdsalutil.InstructionInfo;
37 import org.opendaylight.genius.mdsalutil.InstructionType;
38 import org.opendaylight.genius.mdsalutil.MDSALUtil;
39 import org.opendaylight.genius.mdsalutil.MatchFieldType;
40 import org.opendaylight.genius.mdsalutil.MatchInfo;
41 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
42 import org.opendaylight.genius.mdsalutil.NwConstants;
43 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
44 import org.opendaylight.genius.utils.ServiceIndex;
45 import org.opendaylight.netvirt.elan.ElanException;
46 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
47 import org.opendaylight.netvirt.elan.utils.ElanConstants;
48 import org.opendaylight.netvirt.elan.utils.ElanForwardingEntriesHandler;
49 import org.opendaylight.netvirt.elan.utils.ElanUtils;
50 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
51 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.external.tunnel.list.ExternalTunnel;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface.EtreeInterfaceType;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTagName;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanForwardingTables;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMacBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMacKey;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfacesBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfacesKey;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTable;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTableKey;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.ElanBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.ElanKey;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntryKey;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
91 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
92 import org.slf4j.Logger;
93 import org.slf4j.LoggerFactory;
94
95 /**
96  * Class in charge of handling creations, modifications and removals of
97  * ElanInterfaces.
98  *
99  * @see org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface
100  */
101 @SuppressWarnings("deprecation")
102 public class ElanInterfaceManager extends AsyncDataTreeChangeListenerBase<ElanInterface, ElanInterfaceManager>
103         implements AutoCloseable {
104
105     private final DataBroker broker;
106     private final IMdsalApiManager mdsalManager;
107     private final IInterfaceManager interfaceManager;
108     private final IdManagerService idManager;
109     private final ElanForwardingEntriesHandler elanForwardingEntriesHandler;
110     private ElanL2GatewayUtils elanL2GatewayUtils;
111     private ElanUtils elanUtils;
112
113     private static final long WAIT_TIME_FOR_SYNC_INSTALL = Long.getLong("wait.time.sync.install", 300L);
114
115     private Map<String, ConcurrentLinkedQueue<ElanInterface>> unProcessedElanInterfaces = new ConcurrentHashMap<>();
116
117     private static final Logger LOG = LoggerFactory.getLogger(ElanInterfaceManager.class);
118
119     public ElanInterfaceManager(final DataBroker dataBroker,
120                                 final IdManagerService managerService,
121                                 final IMdsalApiManager mdsalApiManager,
122                                 IInterfaceManager interfaceManager,
123                                 final ElanForwardingEntriesHandler elanForwardingEntriesHandler) {
124         super(ElanInterface.class, ElanInterfaceManager.class);
125         this.broker = dataBroker;
126         this.idManager = managerService;
127         this.mdsalManager = mdsalApiManager;
128         this.interfaceManager = interfaceManager;
129         this.elanForwardingEntriesHandler = elanForwardingEntriesHandler;
130     }
131
132     public void setElanUtils(ElanUtils elanUtils) {
133         this.elanUtils = elanUtils;
134         this.elanL2GatewayUtils = elanUtils.getElanL2GatewayUtils();
135         this.elanForwardingEntriesHandler.setElanUtils(elanUtils);
136     }
137
138     public void init() {
139         registerListener(LogicalDatastoreType.CONFIGURATION, broker);
140     }
141
142     @Override
143     protected InstanceIdentifier<ElanInterface> getWildCardPath() {
144         return InstanceIdentifier.create(ElanInterfaces.class).child(ElanInterface.class);
145     }
146
147     @Override
148     protected void remove(InstanceIdentifier<ElanInterface> identifier, ElanInterface del) {
149         String interfaceName = del.getName();
150         ElanInstance elanInfo = ElanUtils.getElanInstanceByName(broker, del.getElanInstanceName());
151         /*
152          * Handling in case the elan instance is deleted.If the Elan instance is
153          * deleted, there is no need to explicitly delete the elan interfaces
154          */
155         if (elanInfo == null) {
156             return;
157         }
158         InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
159         String elanInstanceName = elanInfo.getElanInstanceName();
160         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
161         InterfaceRemoveWorkerOnElan configWorker = new InterfaceRemoveWorkerOnElan(elanInstanceName, elanInfo,
162                 interfaceName, interfaceInfo, false, this);
163         coordinator.enqueueJob(elanInstanceName, configWorker, ElanConstants.JOB_MAX_RETRIES);
164     }
165
166     public void removeElanInterface(List<ListenableFuture<Void>> futures, ElanInstance elanInfo, String interfaceName,
167             InterfaceInfo interfaceInfo, boolean isInterfaceStateRemoved) {
168         String elanName = elanInfo.getElanInstanceName();
169         boolean isLastElanInterface = false;
170         long elanTag = elanInfo.getElanTag();
171         WriteTransaction tx = broker.newWriteOnlyTransaction();
172         WriteTransaction deleteFlowGroupTx = broker.newWriteOnlyTransaction();
173         Elan elanState = removeElanStateForInterface(elanInfo, interfaceName, tx);
174         if (elanState == null) {
175             return;
176         }
177         List<String> elanInterfaces = elanState.getElanInterfaces();
178         if (elanInterfaces.size() == 0) {
179             isLastElanInterface = true;
180         }
181         if (interfaceInfo != null) {
182             BigInteger dpId = interfaceInfo.getDpId();
183             DpnInterfaces dpnInterfaces = removeElanDpnInterfaceFromOperationalDataStore(elanName, dpId, interfaceName,
184                     elanTag, tx);
185             /*
186              * If there are not elan ports, remove the unknown dmac, terminating
187              * service table flows, remote/local bc group
188              */
189             if (dpnInterfaces == null || dpnInterfaces.getInterfaces() == null
190                     || dpnInterfaces.getInterfaces().isEmpty()) {
191                 // No more Elan Interfaces in this DPN
192                 LOG.debug("deleting the elan: {} present on dpId: {}", elanInfo.getElanInstanceName(), dpId);
193                 removeDefaultTermFlow(dpId, elanInfo.getElanTag());
194                 removeUnknownDmacFlow(dpId, elanInfo, deleteFlowGroupTx, elanInfo.getElanTag());
195                 removeEtreeUnknownDmacFlow(dpId, elanInfo, deleteFlowGroupTx);
196                 removeElanBroadcastGroup(elanInfo, interfaceInfo, deleteFlowGroupTx);
197                 removeLocalBroadcastGroup(elanInfo, interfaceInfo, deleteFlowGroupTx);
198                 removeEtreeBroadcastGrups(elanInfo, interfaceInfo, deleteFlowGroupTx);
199                 if (ElanUtils.isVxlan(elanInfo)) {
200                     unsetExternalTunnelTable(dpId, elanInfo);
201                 }
202             } else {
203                 setupLocalBroadcastGroups(elanInfo, dpnInterfaces, interfaceInfo);
204             }
205         }
206         futures.add(ElanUtils.waitForTransactionToComplete(tx));
207         futures.add(ElanUtils.waitForTransactionToComplete(deleteFlowGroupTx));
208         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
209         InterfaceRemoveWorkerOnElanInterface removeInterfaceWorker = new InterfaceRemoveWorkerOnElanInterface(
210                 interfaceName, elanInfo, interfaceInfo, isInterfaceStateRemoved, this, isLastElanInterface);
211         coordinator.enqueueJob(interfaceName, removeInterfaceWorker, ElanConstants.JOB_MAX_RETRIES);
212     }
213
214     private void removeEtreeUnknownDmacFlow(BigInteger dpId, ElanInstance elanInfo,
215             WriteTransaction deleteFlowGroupTx) {
216         EtreeLeafTagName etreeLeafTag = elanUtils.getEtreeLeafTagByElanTag(elanInfo.getElanTag());
217         if (etreeLeafTag != null) {
218             long leafTag = etreeLeafTag.getEtreeLeafTag().getValue();
219             removeUnknownDmacFlow(dpId, elanInfo, deleteFlowGroupTx, leafTag);
220         }
221     }
222
223     private void removeEtreeBroadcastGrups(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
224             WriteTransaction deleteFlowGroupTx) {
225         removeLeavesEtreeBroadcastGroup(elanInfo, interfaceInfo, deleteFlowGroupTx);
226         removeLeavesLocalBroadcastGroup(elanInfo, interfaceInfo, deleteFlowGroupTx);
227     }
228
229     private void removeLeavesLocalBroadcastGroup(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
230             WriteTransaction deleteFlowGroupTx) {
231         EtreeInstance etreeInstance = elanInfo.getAugmentation(EtreeInstance.class);
232         if (etreeInstance != null) {
233             BigInteger dpnId = interfaceInfo.getDpId();
234             long groupId = ElanUtils.getEtreeLeafLocalBCGId(etreeInstance.getEtreeLeafTagVal().getValue());
235             List<Bucket> listBuckets = new ArrayList<>();
236             int bucketId = 0;
237             listBuckets.add(getLocalBCGroupBucketInfo(interfaceInfo, bucketId));
238             Group group = MDSALUtil.buildGroup(groupId, elanInfo.getElanInstanceName(), GroupTypes.GroupAll,
239                     MDSALUtil.buildBucketLists(listBuckets));
240             LOG.trace("deleted the localBroadCast Group:{}", group);
241             mdsalManager.removeGroupToTx(dpnId, group, deleteFlowGroupTx);
242         }
243     }
244
245     private void removeLeavesEtreeBroadcastGroup(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
246             WriteTransaction deleteFlowGroupTx) {
247         EtreeInstance etreeInstance = elanInfo.getAugmentation(EtreeInstance.class);
248         if (etreeInstance != null) {
249             long etreeTag = etreeInstance.getEtreeLeafTagVal().getValue();
250             int bucketId = 0;
251             int actionKey = 0;
252             List<Bucket> listBuckets = new ArrayList<>();
253             List<Action> listAction = new ArrayList<>();
254             listAction.add(new ActionInfo(ActionType.group,
255                     new String[] { String.valueOf(ElanUtils.getEtreeLeafLocalBCGId(etreeTag)) }, ++actionKey)
256                             .buildAction());
257             listBuckets.add(MDSALUtil.buildBucket(listAction, MDSALUtil.GROUP_WEIGHT, bucketId, MDSALUtil.WATCH_PORT,
258                     MDSALUtil.WATCH_GROUP));
259             bucketId++;
260             listBuckets.addAll(getRemoteBCGroupBucketInfos(elanInfo, bucketId, interfaceInfo, etreeTag));
261             BigInteger dpnId = interfaceInfo.getDpId();
262             long groupId = ElanUtils.getEtreeLeafRemoteBCGId(etreeTag);
263             Group group = MDSALUtil.buildGroup(groupId, elanInfo.getElanInstanceName(), GroupTypes.GroupAll,
264                     MDSALUtil.buildBucketLists(listBuckets));
265             LOG.trace("deleting the remoteBroadCast group:{}", group);
266             mdsalManager.removeGroupToTx(dpnId, group, deleteFlowGroupTx);
267         }
268     }
269
270     private Elan removeElanStateForInterface(ElanInstance elanInfo, String interfaceName, WriteTransaction tx) {
271         String elanName = elanInfo.getElanInstanceName();
272         Elan elanState = ElanUtils.getElanByName(broker, elanName);
273         if (elanState == null) {
274             return elanState;
275         }
276         List<String> elanInterfaces = elanState.getElanInterfaces();
277         elanInterfaces.remove(interfaceName);
278         if (elanInterfaces.isEmpty()) {
279             tx.delete(LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInstanceOperationalDataPath(elanName));
280             tx.delete(LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanMacTableOperationalDataPath(elanName));
281             tx.delete(LogicalDatastoreType.OPERATIONAL,
282                     ElanUtils.getElanInfoEntriesOperationalDataPath(elanInfo.getElanTag()));
283         } else {
284             Elan updateElanState = new ElanBuilder().setElanInterfaces(elanInterfaces).setName(elanName)
285                     .setKey(new ElanKey(elanName)).build();
286             tx.put(LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInstanceOperationalDataPath(elanName),
287                     updateElanState);
288         }
289         return elanState;
290     }
291
292     private void deleteElanInterfaceFromConfigDS(String interfaceName, WriteTransaction tx) {
293         // removing the ElanInterface from the config data_store if interface is
294         // not present in Interface config DS
295         if (interfaceManager.getInterfaceInfoFromConfigDataStore(interfaceName) == null) {
296             tx.delete(LogicalDatastoreType.CONFIGURATION,
297                     ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName));
298         }
299     }
300
301     void removeEntriesForElanInterface(List<ListenableFuture<Void>> futures, ElanInstance elanInfo,
302             InterfaceInfo interfaceInfo, String interfaceName, boolean isInterfaceStateRemoved,
303             boolean isLastElanInterface) {
304         String elanName = elanInfo.getElanInstanceName();
305         WriteTransaction tx = broker.newWriteOnlyTransaction();
306         WriteTransaction deleteFlowGroupTx = broker.newWriteOnlyTransaction();
307         InstanceIdentifier<ElanInterfaceMac> elanInterfaceId = ElanUtils
308                 .getElanInterfaceMacEntriesOperationalDataPath(interfaceName);
309         LOG.debug("Removing the Interface:{} from elan:{}", interfaceName, elanName);
310         if (interfaceInfo != null) {
311             Optional<ElanInterfaceMac> existingElanInterfaceMac = elanUtils.read(broker,
312                     LogicalDatastoreType.OPERATIONAL, elanInterfaceId);
313             if (existingElanInterfaceMac.isPresent()) {
314                 List<PhysAddress> macAddresses = new ArrayList<>();
315                 List<MacEntry> existingMacEntries = existingElanInterfaceMac.get().getMacEntry();
316                 List<MacEntry> macEntries = new ArrayList<>();
317                 if (existingMacEntries != null && !existingMacEntries.isEmpty()) {
318                     macEntries.addAll(existingMacEntries);
319                 }
320                 if (!macEntries.isEmpty()) {
321                     for (MacEntry macEntry : macEntries) {
322                         LOG.debug("removing the  mac-entry:{} present on elanInterface:{}",
323                                 macEntry.getMacAddress().getValue(), interfaceName);
324                         if (!isLastElanInterface) {
325                             tx.delete(LogicalDatastoreType.OPERATIONAL,
326                                     ElanUtils.getMacEntryOperationalDataPath(elanName, macEntry.getMacAddress()));
327                         }
328                         elanUtils.deleteMacFlows(elanInfo, interfaceInfo, macEntry, deleteFlowGroupTx);
329                         macAddresses.add(macEntry.getMacAddress());
330                     }
331
332                     // Removing all those MACs from External Devices belonging
333                     // to this ELAN
334                     if (ElanUtils.isVxlan(elanInfo)) {
335                         elanL2GatewayUtils.removeMacsFromElanExternalDevices(elanInfo, macAddresses);
336                     }
337                 }
338             }
339             removeDefaultTermFlow(interfaceInfo.getDpId(), interfaceInfo.getInterfaceTag());
340             removeFilterEqualsTable(elanInfo, interfaceInfo, deleteFlowGroupTx);
341         } else {
342             // Interface does not exist in ConfigDS, so lets remove everything
343             // about that interface related to Elan
344             ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(interfaceName);
345             if (elanInterfaceMac != null && elanInterfaceMac.getMacEntry() != null) {
346                 List<MacEntry> macEntries = elanInterfaceMac.getMacEntry();
347                 for (MacEntry macEntry : macEntries) {
348                     tx.delete(LogicalDatastoreType.OPERATIONAL,
349                             ElanUtils.getMacEntryOperationalDataPath(elanName, macEntry.getMacAddress()));
350                 }
351             }
352         }
353         tx.delete(LogicalDatastoreType.OPERATIONAL, elanInterfaceId);
354         if (!isInterfaceStateRemoved) {
355             unbindService(elanInfo, interfaceName, tx);
356         }
357         deleteElanInterfaceFromConfigDS(interfaceName, tx);
358         futures.add(ElanUtils.waitForTransactionToComplete(tx));
359         futures.add(ElanUtils.waitForTransactionToComplete(deleteFlowGroupTx));
360     }
361
362     private DpnInterfaces removeElanDpnInterfaceFromOperationalDataStore(String elanName, BigInteger dpId,
363             String interfaceName, long elanTag, WriteTransaction tx) {
364         DpnInterfaces dpnInterfaces = elanUtils.getElanInterfaceInfoByElanDpn(elanName, dpId);
365         if (dpnInterfaces != null) {
366             List<String> interfaceLists = dpnInterfaces.getInterfaces();
367             interfaceLists.remove(interfaceName);
368
369             if (interfaceLists == null || interfaceLists.isEmpty()) {
370                 deleteAllRemoteMacsInADpn(elanName, dpId, elanTag);
371                 deleteElanDpnInterface(elanName, dpId, tx);
372             } else {
373                 dpnInterfaces = updateElanDpnInterfacesList(elanName, dpId, interfaceLists, tx);
374             }
375         }
376         return dpnInterfaces;
377     }
378
379     private void deleteAllRemoteMacsInADpn(String elanName, BigInteger dpId, long elanTag) {
380         List<DpnInterfaces> dpnInterfaces = elanUtils.getInvolvedDpnsInElan(elanName);
381         for (DpnInterfaces dpnInterface : dpnInterfaces) {
382             BigInteger currentDpId = dpnInterface.getDpId();
383             if (!currentDpId.equals(dpId)) {
384                 for (String elanInterface : dpnInterface.getInterfaces()) {
385                     ElanInterfaceMac macs = elanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
386                     if (macs == null || macs.getMacEntry() == null) {
387                         continue;
388                     }
389                     for (MacEntry mac : macs.getMacEntry()) {
390                         removeTheMacFlowInTheDPN(dpId, elanTag, currentDpId, mac);
391                         removeEtreeMacFlowInTheDPN(dpId, elanTag, currentDpId, mac);
392                     }
393                 }
394             }
395         }
396     }
397
398     private void removeEtreeMacFlowInTheDPN(BigInteger dpId, long elanTag, BigInteger currentDpId, MacEntry mac) {
399         EtreeLeafTagName etreeLeafTag = elanUtils.getEtreeLeafTagByElanTag(elanTag);
400         if (etreeLeafTag != null) {
401             removeTheMacFlowInTheDPN(dpId, etreeLeafTag.getEtreeLeafTag().getValue(), currentDpId, mac);
402         }
403     }
404
405     private void removeTheMacFlowInTheDPN(BigInteger dpId, long elanTag, BigInteger currentDpId, MacEntry mac) {
406         mdsalManager
407                 .removeFlow(dpId,
408                         MDSALUtil.buildFlow(NwConstants.ELAN_DMAC_TABLE,
409                                 ElanUtils.getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, currentDpId,
410                                         mac.getMacAddress().getValue(), elanTag)));
411     }
412
413     @Override
414     protected void update(InstanceIdentifier<ElanInterface> identifier, ElanInterface original, ElanInterface update) {
415         // updating the static-Mac Entries for the existing elanInterface
416         String elanName = update.getElanInstanceName();
417         String interfaceName = update.getName();
418         List<PhysAddress> existingPhysAddress = original.getStaticMacEntries();
419         List<PhysAddress> updatedPhysAddress = update.getStaticMacEntries();
420         if (updatedPhysAddress != null && !updatedPhysAddress.isEmpty()) {
421             List<PhysAddress> existingClonedPhyAddress = new ArrayList<>();
422             if (existingPhysAddress != null && !existingPhysAddress.isEmpty()) {
423                 existingClonedPhyAddress.addAll(0, existingPhysAddress);
424                 existingPhysAddress.removeAll(updatedPhysAddress);
425                 updatedPhysAddress.removeAll(existingClonedPhyAddress);
426                 // removing the PhyAddress which are not presented in the
427                 // updated List
428                 for (PhysAddress physAddress : existingPhysAddress) {
429                     removeInterfaceStaticMacEntires(elanName, interfaceName, physAddress);
430                 }
431             }
432             // Adding the new PhysAddress which are presented in the updated
433             // List
434             if (updatedPhysAddress.size() > 0) {
435                 for (PhysAddress physAddress : updatedPhysAddress) {
436                     InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanName, physAddress);
437                     Optional<MacEntry> existingMacEntry = elanUtils.read(broker,
438                             LogicalDatastoreType.OPERATIONAL, macId);
439                     WriteTransaction tx = broker.newWriteOnlyTransaction();
440                     if (existingMacEntry.isPresent()) {
441                         elanForwardingEntriesHandler.updateElanInterfaceForwardingTablesList(
442                                 elanName, interfaceName, existingMacEntry.get().getInterface(), existingMacEntry.get(),
443                                 tx);
444                     } else {
445                         elanForwardingEntriesHandler.addElanInterfaceForwardingTableList(
446                                 ElanUtils.getElanInstanceByName(broker, elanName), interfaceName, physAddress, tx);
447                     }
448                     ElanUtils.waitForTransactionToComplete(tx);
449                 }
450             }
451         } else if (existingPhysAddress != null && !existingPhysAddress.isEmpty()) {
452             for (PhysAddress physAddress : existingPhysAddress) {
453                 removeInterfaceStaticMacEntires(elanName, interfaceName, physAddress);
454             }
455         }
456     }
457
458     @Override
459     protected void add(InstanceIdentifier<ElanInterface> identifier, ElanInterface elanInterfaceAdded) {
460         String elanInstanceName = elanInterfaceAdded.getElanInstanceName();
461         String interfaceName = elanInterfaceAdded.getName();
462         InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
463         if (interfaceInfo == null) {
464             LOG.warn("Interface {} is removed from Interface Oper DS due to port down ", interfaceName);
465             return;
466         }
467         ElanInstance elanInstance = ElanUtils.getElanInstanceByName(broker, elanInstanceName);
468
469         if (elanInstance == null) {
470             elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
471                     .setDescription(elanInterfaceAdded.getDescription()).build();
472             // Add the ElanInstance in the Configuration data-store
473             WriteTransaction tx = broker.newWriteOnlyTransaction();
474             List<String> elanInterfaces = new ArrayList<>();
475             elanInterfaces.add(interfaceName);
476             ElanUtils.updateOperationalDataStore(broker, idManager,
477                     elanInstance, elanInterfaces, tx);
478             ElanUtils.waitForTransactionToComplete(tx);
479             elanInstance = ElanUtils.getElanInstanceByName(broker, elanInstanceName);
480         }
481
482         Long elanTag = elanInstance.getElanTag();
483         // If elan tag is not updated, then put the elan interface into
484         // unprocessed entry map and entry. Let entries
485         // in this map get processed during ELAN update DCN.
486         if (elanTag == null) {
487             ConcurrentLinkedQueue<ElanInterface> elanInterfaces = unProcessedElanInterfaces.get(elanInstanceName);
488             if (elanInterfaces == null) {
489                 elanInterfaces = new ConcurrentLinkedQueue<>();
490             }
491             elanInterfaces.add(elanInterfaceAdded);
492             unProcessedElanInterfaces.put(elanInstanceName, elanInterfaces);
493             return;
494         }
495         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
496         InterfaceAddWorkerOnElan addWorker = new InterfaceAddWorkerOnElan(elanInstanceName, elanInterfaceAdded,
497                 interfaceInfo, elanInstance, this);
498         coordinator.enqueueJob(elanInstanceName, addWorker, ElanConstants.JOB_MAX_RETRIES);
499     }
500
501     void handleunprocessedElanInterfaces(ElanInstance elanInstance) throws ElanException {
502         List<ListenableFuture<Void>> futures = new ArrayList<>();
503         Queue<ElanInterface> elanInterfaces = unProcessedElanInterfaces.get(elanInstance.getElanInstanceName());
504         if (elanInterfaces == null || elanInterfaces.isEmpty()) {
505             return;
506         }
507         for (ElanInterface elanInterface : elanInterfaces) {
508             String interfaceName = elanInterface.getName();
509             InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
510             addElanInterface(futures, elanInterface, interfaceInfo, elanInstance);
511         }
512     }
513
514     void programRemoteDmacFlow(ElanInstance elanInstance, InterfaceInfo interfaceInfo,
515             WriteTransaction writeFlowGroupTx) throws ElanException {
516         ElanDpnInterfacesList elanDpnInterfacesList = elanUtils
517                 .getElanDpnInterfacesList(elanInstance.getElanInstanceName());
518         List<DpnInterfaces> dpnInterfaceLists = null;
519         if (elanDpnInterfacesList != null) {
520             dpnInterfaceLists = elanDpnInterfacesList.getDpnInterfaces();
521         }
522         if (dpnInterfaceLists == null) {
523             dpnInterfaceLists = new ArrayList<>();
524         }
525         for (DpnInterfaces dpnInterfaces : dpnInterfaceLists) {
526             if (dpnInterfaces.getDpId().equals(interfaceInfo.getDpId())) {
527                 continue;
528             }
529             List<String> remoteElanInterfaces = dpnInterfaces.getInterfaces();
530             for (String remoteIf : remoteElanInterfaces) {
531                 ElanInterfaceMac elanIfMac = elanUtils.getElanInterfaceMacByInterfaceName(remoteIf);
532                 InterfaceInfo remoteInterface = interfaceManager.getInterfaceInfo(remoteIf);
533                 if (elanIfMac == null) {
534                     continue;
535                 }
536                 List<MacEntry> remoteMacEntries = elanIfMac.getMacEntry();
537                 if (remoteMacEntries != null) {
538                     for (MacEntry macEntry : remoteMacEntries) {
539                         PhysAddress physAddress = macEntry.getMacAddress();
540                         elanUtils.setupRemoteDmacFlow(interfaceInfo.getDpId(), remoteInterface.getDpId(),
541                                 remoteInterface.getInterfaceTag(), elanInstance.getElanTag(), physAddress.getValue(),
542                                 elanInstance.getElanInstanceName(), writeFlowGroupTx, remoteIf);
543                     }
544                 }
545             }
546         }
547     }
548
549     void addElanInterface(List<ListenableFuture<Void>> futures, ElanInterface elanInterface,
550             InterfaceInfo interfaceInfo, ElanInstance elanInstance) throws ElanException {
551         Preconditions.checkNotNull(elanInstance, "elanInstance cannot be null");
552         Preconditions.checkNotNull(interfaceInfo, "interfaceInfo cannot be null");
553         Preconditions.checkNotNull(elanInterface, "elanInterface cannot be null");
554
555         String interfaceName = elanInterface.getName();
556         String elanInstanceName = elanInterface.getElanInstanceName();
557
558         Elan elanInfo = ElanUtils.getElanByName(broker, elanInstanceName);
559         WriteTransaction tx = broker.newWriteOnlyTransaction();
560         if (elanInfo == null) {
561             List<String> elanInterfaces = new ArrayList<>();
562             elanInterfaces.add(interfaceName);
563             ElanUtils.updateOperationalDataStore(broker, idManager,
564                     elanInstance, elanInterfaces, tx);
565         } else {
566             createElanStateList(elanInstanceName, interfaceName, tx);
567         }
568         boolean isFirstInterfaceInDpn = false;
569         // Specific actions to the DPN where the ElanInterface has been added,
570         // for example, programming the
571         // External tunnel table if needed or adding the ElanInterface to the
572         // DpnInterfaces in the operational DS.
573         BigInteger dpId = interfaceInfo != null ? dpId = interfaceInfo.getDpId() : null;
574         DpnInterfaces dpnInterfaces = null;
575         if (dpId != null && !dpId.equals(ElanConstants.INVALID_DPN)) {
576             InstanceIdentifier<DpnInterfaces> elanDpnInterfaces = ElanUtils
577                     .getElanDpnInterfaceOperationalDataPath(elanInstanceName, dpId);
578             Optional<DpnInterfaces> existingElanDpnInterfaces = elanUtils.read(broker,
579                     LogicalDatastoreType.OPERATIONAL, elanDpnInterfaces);
580             if (!existingElanDpnInterfaces.isPresent()) {
581                 isFirstInterfaceInDpn = true;
582                 // ELAN's 1st ElanInterface added to this DPN
583                 dpnInterfaces = createElanInterfacesList(elanInstanceName, interfaceName, dpId, tx);
584                 // The 1st ElanInterface in a DPN must program the Ext Tunnel
585                 // table, but only if Elan has VNI
586                 if (ElanUtils.isVxlan(elanInstance)) {
587                     setExternalTunnelTable(dpId, elanInstance);
588                 }
589                 elanL2GatewayUtils.installElanL2gwDevicesLocalMacsInDpn(dpId, elanInstance, interfaceName);
590             } else {
591                 List<String> elanInterfaces = existingElanDpnInterfaces.get().getInterfaces();
592                 elanInterfaces.add(interfaceName);
593                 if (elanInterfaces.size() == 1) { // 1st dpn interface
594                     elanL2GatewayUtils.installElanL2gwDevicesLocalMacsInDpn(dpId, elanInstance, interfaceName);
595                 }
596                 dpnInterfaces = updateElanDpnInterfacesList(elanInstanceName, dpId, elanInterfaces, tx);
597             }
598         }
599
600         // add code to install Local/Remote BC group, unknow DMAC entry,
601         // terminating service table flow entry
602         // call bindservice of interfacemanager to create ingress table flow
603         // enty.
604         // Add interface to the ElanInterfaceForwardingEntires Container
605         createElanInterfaceTablesList(interfaceName, tx);
606         if (interfaceInfo != null) {
607             installEntriesForFirstInterfaceonDpn(elanInstance, interfaceInfo, dpnInterfaces, isFirstInterfaceInDpn, tx);
608         }
609         futures.add(ElanUtils.waitForTransactionToComplete(tx));
610
611         DataStoreJobCoordinator coordinator = DataStoreJobCoordinator.getInstance();
612         InterfaceAddWorkerOnElanInterface addWorker = new InterfaceAddWorkerOnElanInterface(interfaceName,
613                 elanInterface, interfaceInfo, elanInstance, isFirstInterfaceInDpn, this);
614         coordinator.enqueueJob(interfaceName, addWorker, ElanConstants.JOB_MAX_RETRIES);
615     }
616
617     void setupEntriesForElanInterface(List<ListenableFuture<Void>> futures, ElanInstance elanInstance,
618             ElanInterface elanInterface, InterfaceInfo interfaceInfo, boolean isFirstInterfaceInDpn)
619             throws ElanException {
620         String elanInstanceName = elanInstance.getElanInstanceName();
621         String interfaceName = elanInterface.getName();
622         WriteTransaction tx = broker.newWriteOnlyTransaction();
623         BigInteger dpId = interfaceInfo.getDpId();
624         WriteTransaction writeFlowGroupTx = broker.newWriteOnlyTransaction();
625         installEntriesForElanInterface(elanInstance, interfaceInfo, isFirstInterfaceInDpn, tx, writeFlowGroupTx);
626         List<PhysAddress> staticMacAddresses = elanInterface.getStaticMacEntries();
627         if (staticMacAddresses != null) {
628             boolean isInterfaceOperational = isOperational(interfaceInfo);
629             for (PhysAddress physAddress : staticMacAddresses) {
630                 InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanInstanceName, physAddress);
631                 Optional<MacEntry> existingMacEntry = elanUtils.read(broker,
632                         LogicalDatastoreType.OPERATIONAL, macId);
633                 if (existingMacEntry.isPresent()) {
634                     elanForwardingEntriesHandler.updateElanInterfaceForwardingTablesList(
635                             elanInstanceName, interfaceName, existingMacEntry.get().getInterface(),
636                             existingMacEntry.get(), tx);
637                 } else {
638                     elanForwardingEntriesHandler
639                             .addElanInterfaceForwardingTableList(elanInstance, interfaceName, physAddress, tx);
640                 }
641
642                 if (isInterfaceOperational) {
643                     // Setting SMAC, DMAC, UDMAC in this DPN and also in other
644                     // DPNs
645                     elanUtils.setupMacFlows(elanInstance, interfaceInfo, ElanConstants.STATIC_MAC_TIMEOUT,
646                             physAddress.getValue(), writeFlowGroupTx);
647                 }
648             }
649
650             if (isInterfaceOperational) {
651                 // Add MAC in TOR's remote MACs via OVSDB. Outside of the loop
652                 // on purpose.
653                 elanL2GatewayUtils.scheduleAddDpnMacInExtDevices(elanInstance.getElanInstanceName(), dpId,
654                         staticMacAddresses);
655             }
656         }
657         futures.add(ElanUtils.waitForTransactionToComplete(tx));
658         futures.add(ElanUtils.waitForTransactionToComplete(writeFlowGroupTx));
659     }
660
661     protected void removeInterfaceStaticMacEntires(String elanInstanceName, String interfaceName,
662             PhysAddress physAddress) {
663         InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(interfaceName);
664         InstanceIdentifier<MacEntry> macId = getMacEntryOperationalDataPath(elanInstanceName, physAddress);
665         Optional<MacEntry> existingMacEntry = elanUtils.read(broker,
666                 LogicalDatastoreType.OPERATIONAL, macId);
667
668         if (!existingMacEntry.isPresent()) {
669             return;
670         }
671
672         MacEntry macEntry = new MacEntryBuilder().setMacAddress(physAddress).setInterface(interfaceName)
673                 .setKey(new MacEntryKey(physAddress)).build();
674         WriteTransaction tx = broker.newWriteOnlyTransaction();
675         elanForwardingEntriesHandler.deleteElanInterfaceForwardingEntries(
676                 ElanUtils.getElanInstanceByName(broker, elanInstanceName), interfaceInfo, macEntry, tx);
677         elanForwardingEntriesHandler.deleteElanInterfaceMacForwardingEntries(interfaceName,
678                 physAddress, tx);
679         ElanUtils.waitForTransactionToComplete(tx);
680     }
681
682     private InstanceIdentifier<MacEntry> getMacEntryOperationalDataPath(String elanName, PhysAddress physAddress) {
683         return InstanceIdentifier.builder(ElanForwardingTables.class).child(MacTable.class, new MacTableKey(elanName))
684                 .child(MacEntry.class, new MacEntryKey(physAddress)).build();
685     }
686
687     private void installEntriesForElanInterface(ElanInstance elanInstance, InterfaceInfo interfaceInfo,
688             boolean isFirstInterfaceInDpn, WriteTransaction tx, WriteTransaction writeFlowGroupTx)
689             throws ElanException {
690         if (!isOperational(interfaceInfo)) {
691             return;
692         }
693         BigInteger dpId = interfaceInfo.getDpId();
694         elanUtils.setupTermDmacFlows(interfaceInfo, mdsalManager, writeFlowGroupTx);
695         setupFilterEqualsTable(elanInstance, interfaceInfo, writeFlowGroupTx);
696         if (isFirstInterfaceInDpn) {
697             // Terminating Service , UnknownDMAC Table.
698             setupTerminateServiceTable(elanInstance, dpId, writeFlowGroupTx);
699             setupUnknownDMacTable(elanInstance, dpId, writeFlowGroupTx);
700             // update the remote-DPNs remoteBC group entry with Tunnels
701             setElanBCGrouponOtherDpns(elanInstance, elanInstance.getElanTag().longValue(), dpId, writeFlowGroupTx);
702             /*
703              * Install remote DMAC flow. This is required since this DPN is
704              * added later to the elan instance and remote DMACs of other
705              * interfaces in this elan instance are not present in the current
706              * dpn.
707              */
708             programRemoteDmacFlow(elanInstance, interfaceInfo, writeFlowGroupTx);
709         }
710         // bind the Elan service to the Interface
711         bindService(elanInstance, ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceInfo.getInterfaceName()), tx);
712     }
713
714     public void installEntriesForFirstInterfaceonDpn(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
715             DpnInterfaces dpnInterfaces, boolean isFirstInterfaceInDpn, WriteTransaction tx) {
716         if (!isOperational(interfaceInfo)) {
717             return;
718         }
719         // LocalBroadcast Group creation with elan-Interfaces
720         setupLocalBroadcastGroups(elanInfo, dpnInterfaces, interfaceInfo);
721         if (isFirstInterfaceInDpn) {
722             LOG.trace("waitTimeForSyncInstall is {}", WAIT_TIME_FOR_SYNC_INSTALL);
723             BigInteger dpId = interfaceInfo.getDpId();
724             // RemoteBroadcast Group creation
725             try {
726                 Thread.sleep(WAIT_TIME_FOR_SYNC_INSTALL);
727             } catch (InterruptedException e1) {
728                 LOG.warn("Error while waiting for local BC group for ELAN {} to install", elanInfo);
729             }
730             setupElanBroadcastGroups(elanInfo, dpnInterfaces, dpId);
731             try {
732                 Thread.sleep(WAIT_TIME_FOR_SYNC_INSTALL);
733             } catch (InterruptedException e1) {
734                 LOG.warn("Error while waiting for local BC group for ELAN {} to install", elanInfo);
735             }
736         }
737     }
738
739     public void setupFilterEqualsTable(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
740             WriteTransaction writeFlowGroupTx) {
741         int ifTag = interfaceInfo.getInterfaceTag();
742         Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_FILTER_EQUALS_TABLE,
743                 getFlowRef(NwConstants.ELAN_FILTER_EQUALS_TABLE, ifTag), 9, elanInfo.getElanInstanceName(), 0, 0,
744                 ElanConstants.COOKIE_ELAN_FILTER_EQUALS.add(BigInteger.valueOf(ifTag)),
745                 getTunnelIdMatchForFilterEqualsLPortTag(ifTag),
746                 elanUtils.getInstructionsInPortForOutGroup(interfaceInfo.getInterfaceName()));
747
748         mdsalManager.addFlowToTx(interfaceInfo.getDpId(), flow, writeFlowGroupTx);
749
750         Flow flowEntry = MDSALUtil.buildFlowNew(NwConstants.ELAN_FILTER_EQUALS_TABLE,
751                 getFlowRef(NwConstants.ELAN_FILTER_EQUALS_TABLE, 1000 + ifTag), 10, elanInfo.getElanInstanceName(), 0,
752                 0, ElanConstants.COOKIE_ELAN_FILTER_EQUALS.add(BigInteger.valueOf(ifTag)),
753                 getMatchesForFilterEqualsLPortTag(ifTag), MDSALUtil.buildInstructionsDrop());
754
755         mdsalManager.addFlowToTx(interfaceInfo.getDpId(), flowEntry, writeFlowGroupTx);
756     }
757
758     public void removeFilterEqualsTable(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
759             WriteTransaction deleteFlowGroupTx) {
760         int ifTag = interfaceInfo.getInterfaceTag();
761         Flow flow = MDSALUtil.buildFlowNew(NwConstants.ELAN_FILTER_EQUALS_TABLE,
762                 getFlowRef(NwConstants.ELAN_FILTER_EQUALS_TABLE, ifTag), 9, elanInfo.getElanInstanceName(), 0, 0,
763                 ElanConstants.COOKIE_ELAN_FILTER_EQUALS.add(BigInteger.valueOf(ifTag)),
764                 getTunnelIdMatchForFilterEqualsLPortTag(ifTag),
765                 elanUtils.getInstructionsInPortForOutGroup(interfaceInfo.getInterfaceName()));
766
767         mdsalManager.removeFlowToTx(interfaceInfo.getDpId(), flow, deleteFlowGroupTx);
768
769         Flow flowEntity = MDSALUtil.buildFlowNew(NwConstants.ELAN_FILTER_EQUALS_TABLE,
770                 getFlowRef(NwConstants.ELAN_FILTER_EQUALS_TABLE, 1000 + ifTag), 10, elanInfo.getElanInstanceName(), 0,
771                 0, ElanConstants.COOKIE_ELAN_FILTER_EQUALS.add(BigInteger.valueOf(ifTag)),
772                 getMatchesForFilterEqualsLPortTag(ifTag), MDSALUtil.buildInstructionsDrop());
773
774         mdsalManager.removeFlowToTx(interfaceInfo.getDpId(), flowEntity, deleteFlowGroupTx);
775     }
776
777     private List<Bucket> getRemoteBCGroupBucketInfos(ElanInstance elanInfo, int bucketKeyStart,
778             InterfaceInfo interfaceInfo, long elanTag) {
779         return getRemoteBCGroupBuckets(elanInfo, null, interfaceInfo.getDpId(), bucketKeyStart, elanTag);
780     }
781
782     private List<Bucket> getRemoteBCGroupBuckets(ElanInstance elanInfo, DpnInterfaces dpnInterfaces, BigInteger dpnId,
783             int bucketId, long elanTag) {
784         List<Bucket> listBucketInfo = new ArrayList<>();
785         ElanDpnInterfacesList elanDpns = elanUtils.getElanDpnInterfacesList(elanInfo.getElanInstanceName());
786         listBucketInfo.addAll(getRemoteBCGroupTunnelBuckets(elanDpns, dpnId, bucketId, elanTag));
787         listBucketInfo.addAll(getRemoteBCGroupExternalPortBuckets(elanDpns, dpnInterfaces, dpnId, bucketId));
788         listBucketInfo.addAll(getRemoteBCGroupBucketsOfElanL2GwDevices(elanInfo, dpnId, bucketId));
789         return listBucketInfo;
790     }
791
792     @SuppressWarnings("checkstyle:IllegalCatch")
793     private List<Bucket> getRemoteBCGroupTunnelBuckets(ElanDpnInterfacesList elanDpns, BigInteger dpnId, int bucketId,
794             long elanTag) {
795         List<Bucket> listBucketInfo = new ArrayList<>();
796         if (elanDpns != null) {
797             for (DpnInterfaces dpnInterface : elanDpns.getDpnInterfaces()) {
798                 if (elanUtils.isDpnPresent(dpnInterface.getDpId()) && dpnInterface.getDpId() != dpnId
799                         && dpnInterface.getInterfaces() != null && !dpnInterface.getInterfaces().isEmpty()) {
800                     try {
801                         List<Action> listActionInfo = elanUtils.getInternalTunnelItmEgressAction(dpnId,
802                                 dpnInterface.getDpId(), elanTag);
803                         if (listActionInfo.isEmpty()) {
804                             continue;
805                         }
806                         listBucketInfo.add(MDSALUtil.buildBucket(listActionInfo, MDSALUtil.GROUP_WEIGHT, bucketId,
807                                 MDSALUtil.WATCH_PORT, MDSALUtil.WATCH_GROUP));
808                         bucketId++;
809                     } catch (Exception ex) {
810                         LOG.error("Logical Group Interface not found between source Dpn - {}, destination Dpn - {} ",
811                                 dpnId, dpnInterface.getDpId(), ex);
812                     }
813                 }
814             }
815         }
816         return listBucketInfo;
817     }
818
819     private List<Bucket> getRemoteBCGroupExternalPortBuckets(ElanDpnInterfacesList elanDpns,
820             DpnInterfaces dpnInterfaces, BigInteger dpnId, int bucketId) {
821         DpnInterfaces currDpnInterfaces = dpnInterfaces != null ? dpnInterfaces : getDpnInterfaces(elanDpns, dpnId);
822         if (currDpnInterfaces == null || !elanUtils.isDpnPresent(currDpnInterfaces.getDpId())
823                 || currDpnInterfaces.getInterfaces() == null || currDpnInterfaces.getInterfaces().isEmpty()) {
824             return Collections.emptyList();
825         }
826
827         List<Bucket> listBucketInfo = new ArrayList<>();
828         for (String interfaceName : currDpnInterfaces.getInterfaces()) {
829             if (elanUtils.isExternal(interfaceName)) {
830                 List<Action> listActionInfo = elanUtils.getExternalPortItmEgressAction(interfaceName);
831                 if (!listActionInfo.isEmpty()) {
832                     listBucketInfo.add(MDSALUtil.buildBucket(listActionInfo, MDSALUtil.GROUP_WEIGHT, bucketId,
833                             MDSALUtil.WATCH_PORT, MDSALUtil.WATCH_GROUP));
834                     bucketId++;
835                 }
836             }
837         }
838         return listBucketInfo;
839     }
840
841     private DpnInterfaces getDpnInterfaces(ElanDpnInterfacesList elanDpns, BigInteger dpnId) {
842         if (elanDpns != null) {
843             for (DpnInterfaces dpnInterface : elanDpns.getDpnInterfaces()) {
844                 if (dpnInterface.getDpId() == dpnId) {
845                     return dpnInterface;
846                 }
847             }
848         }
849         return null;
850     }
851
852     @SuppressWarnings("checkstyle:IllegalCatch")
853     private void setElanBCGrouponOtherDpns(ElanInstance elanInfo, long elanTag, BigInteger dpId, WriteTransaction tx) {
854         long groupId = ElanUtils.getElanRemoteBCGId(elanTag);
855         List<Bucket> listBucket = new ArrayList<>();
856         int bucketId = 0;
857         ElanDpnInterfacesList elanDpns = elanUtils.getElanDpnInterfacesList(elanInfo.getElanInstanceName());
858         if (elanDpns != null) {
859             List<DpnInterfaces> dpnInterfaceses = elanDpns.getDpnInterfaces();
860             for (DpnInterfaces dpnInterface : dpnInterfaceses) {
861                 List<Bucket> remoteListBucketInfo = new ArrayList<>();
862                 if (elanUtils.isDpnPresent(dpnInterface.getDpId()) && !dpnInterface.getDpId().equals(dpId)
863                         && dpnInterface.getInterfaces() != null && !dpnInterface.getInterfaces().isEmpty()) {
864                     List<Action> listAction = new ArrayList<>();
865                     int actionKey = 0;
866                     listAction.add(new ActionInfo(ActionType.group,
867                             new String[] { String.valueOf(ElanUtils.getElanLocalBCGId(elanTag)) }, ++actionKey)
868                                     .buildAction());
869                     listBucket.add(MDSALUtil.buildBucket(listAction, MDSALUtil.GROUP_WEIGHT, bucketId,
870                             MDSALUtil.WATCH_PORT, MDSALUtil.WATCH_GROUP));
871                     bucketId++;
872                     remoteListBucketInfo.addAll(listBucket);
873                     for (DpnInterfaces otherFes : dpnInterfaceses) {
874                         if (elanUtils.isDpnPresent(otherFes.getDpId()) && otherFes.getDpId() != dpnInterface.getDpId()
875                                 && otherFes.getInterfaces() != null && !otherFes.getInterfaces().isEmpty()) {
876                             try {
877                                 List<Action> remoteListActionInfo = elanUtils.getInternalTunnelItmEgressAction(
878                                         dpnInterface.getDpId(), otherFes.getDpId(), elanTag);
879                                 if (!remoteListActionInfo.isEmpty()) {
880                                     remoteListBucketInfo
881                                             .add(MDSALUtil.buildBucket(remoteListActionInfo, MDSALUtil.GROUP_WEIGHT,
882                                                     bucketId, MDSALUtil.WATCH_PORT, MDSALUtil.WATCH_GROUP));
883                                     bucketId++;
884                                 }
885                             } catch (Exception ex) {
886                                 LOG.error("setElanBCGrouponOtherDpns failed due to Exception caught; "
887                                         + "Logical Group Interface not found between source Dpn - {}, "
888                                         + "destination Dpn - {} ", dpnInterface.getDpId(), otherFes.getDpId(), ex);
889                                 return;
890                             }
891                         }
892                     }
893                     List<Bucket> elanL2GwDevicesBuckets = getRemoteBCGroupBucketsOfElanL2GwDevices(elanInfo, dpId,
894                             bucketId);
895                     remoteListBucketInfo.addAll(elanL2GwDevicesBuckets);
896
897                     if (remoteListBucketInfo.size() == 0) {
898                         LOG.debug("No ITM is present on Dpn - {} ", dpnInterface.getDpId());
899                         continue;
900                     }
901                     Group group = MDSALUtil.buildGroup(groupId, elanInfo.getElanInstanceName(), GroupTypes.GroupAll,
902                             MDSALUtil.buildBucketLists(remoteListBucketInfo));
903                     mdsalManager.addGroupToTx(dpnInterface.getDpId(), group, tx);
904                 }
905             }
906         }
907     }
908
909     /**
910      * Returns the bucket info with the given interface as the only bucket.
911      */
912     private Bucket getLocalBCGroupBucketInfo(InterfaceInfo interfaceInfo, int bucketIdStart) {
913         return MDSALUtil.buildBucket(getInterfacePortActions(interfaceInfo), MDSALUtil.GROUP_WEIGHT, bucketIdStart,
914                 MDSALUtil.WATCH_PORT, MDSALUtil.WATCH_GROUP);
915     }
916
917     private List<MatchInfo> buildMatchesForVni(Long vni) {
918         List<MatchInfo> mkMatches = new ArrayList<>();
919         MatchInfo match = new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] { BigInteger.valueOf(vni) });
920         mkMatches.add(match);
921         return mkMatches;
922     }
923
924     private List<Instruction> getInstructionsForOutGroup(long groupId) {
925         List<Instruction> mkInstructions = new ArrayList<>();
926         List<Action> actions = new ArrayList<>();
927         actions.add(new ActionInfo(ActionType.group, new String[] { Long.toString(groupId) }).buildAction());
928         mkInstructions.add(MDSALUtil.getWriteActionsInstruction(actions, 0));
929         return mkInstructions;
930     }
931
932     private List<MatchInfo> getMatchesForElanTag(long elanTag, boolean isSHFlagSet) {
933         List<MatchInfo> mkMatches = new ArrayList<>();
934         // Matching metadata
935         mkMatches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
936                 ElanUtils.getElanMetadataLabel(elanTag, isSHFlagSet), MetaDataUtil.METADATA_MASK_SERVICE_SH_FLAG }));
937         return mkMatches;
938     }
939
940     /**
941      * Builds the list of instructions to be installed in the External Tunnel
942      * table (38), which so far consists in writing the elanTag in metadata and
943      * send packet to the new DHCP table.
944      *
945      * @param elanTag
946      *            elanTag to be written in metadata when flow is selected
947      * @return the instructions ready to be installed in a flow
948      */
949     private List<InstructionInfo> getInstructionsExtTunnelTable(Long elanTag) {
950         List<InstructionInfo> mkInstructions = new ArrayList<>();
951         mkInstructions.add(new InstructionInfo(InstructionType.write_metadata,
952                 new BigInteger[] { ElanUtils.getElanMetadataLabel(elanTag), ElanUtils.getElanMetadataMask() }));
953         // TODO: We should point to SMAC or DMAC depending on a configuration
954         // property to enable
955         // mac learning
956         mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.ELAN_DMAC_TABLE }));
957
958         return mkInstructions;
959     }
960
961     // Install DMAC entry on dst DPN
962     public void installDMacAddressTables(ElanInstance elanInfo, InterfaceInfo interfaceInfo, BigInteger dstDpId) throws ElanException {
963         String interfaceName = interfaceInfo.getInterfaceName();
964         ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(interfaceName);
965         if (elanInterfaceMac != null && elanInterfaceMac.getMacEntry() != null) {
966             WriteTransaction writeFlowTx = broker.newWriteOnlyTransaction();
967             List<MacEntry> macEntries = elanInterfaceMac.getMacEntry();
968             for (MacEntry macEntry : macEntries) {
969                 PhysAddress physAddress = macEntry.getMacAddress();
970                 elanUtils.setupDMacFlowonRemoteDpn(elanInfo, interfaceInfo, dstDpId, physAddress.getValue(),
971                         writeFlowTx);
972             }
973             writeFlowTx.submit();
974         }
975     }
976
977     public void setupElanBroadcastGroups(ElanInstance elanInfo, BigInteger dpnId) {
978         setupElanBroadcastGroups(elanInfo, null, dpnId);
979     }
980
981     public void setupElanBroadcastGroups(ElanInstance elanInfo, DpnInterfaces dpnInterfaces, BigInteger dpnId) {
982         setupStandardElanBroadcastGroups(elanInfo, dpnInterfaces, dpnId);
983         setupLeavesEtreeBroadcastGroups(elanInfo, dpnInterfaces, dpnId);
984     }
985
986     public void setupStandardElanBroadcastGroups(ElanInstance elanInfo, DpnInterfaces dpnInterfaces, BigInteger dpnId) {
987         List<Bucket> listBucket = new ArrayList<>();
988         int bucketId = 0;
989         int actionKey = 0;
990         Long elanTag = elanInfo.getElanTag();
991         List<Action> listAction = new ArrayList<>();
992         listAction.add(new ActionInfo(ActionType.group,
993                 new String[] { String.valueOf(ElanUtils.getElanLocalBCGId(elanTag)) }, ++actionKey).buildAction());
994         listBucket.add(MDSALUtil.buildBucket(listAction, MDSALUtil.GROUP_WEIGHT, bucketId, MDSALUtil.WATCH_PORT,
995                 MDSALUtil.WATCH_GROUP));
996         bucketId++;
997         List<Bucket> listBucketInfoRemote = getRemoteBCGroupBuckets(elanInfo, dpnInterfaces, dpnId, bucketId,
998                 elanInfo.getElanTag());
999         listBucket.addAll(listBucketInfoRemote);
1000         long groupId = ElanUtils.getElanRemoteBCGId(elanTag);
1001         Group group = MDSALUtil.buildGroup(groupId, elanInfo.getElanInstanceName(), GroupTypes.GroupAll,
1002                 MDSALUtil.buildBucketLists(listBucket));
1003         LOG.trace("Installing the remote BroadCast Group:{}", group);
1004         mdsalManager.syncInstallGroup(dpnId, group, ElanConstants.DELAY_TIME_IN_MILLISECOND);
1005     }
1006
1007     public void setupLeavesEtreeBroadcastGroups(ElanInstance elanInfo, DpnInterfaces dpnInterfaces, BigInteger dpnId) {
1008         EtreeInstance etreeInstance = elanInfo.getAugmentation(EtreeInstance.class);
1009         if (etreeInstance != null) {
1010             long etreeLeafTag = etreeInstance.getEtreeLeafTagVal().getValue();
1011             List<Bucket> listBucket = new ArrayList<>();
1012             int bucketId = 0;
1013             int actionKey = 0;
1014             List<Action> listAction = new ArrayList<>();
1015             listAction.add(new ActionInfo(ActionType.group,
1016                     new String[] { String.valueOf(ElanUtils.getEtreeLeafLocalBCGId(etreeLeafTag)) }, ++actionKey)
1017                             .buildAction());
1018             listBucket.add(MDSALUtil.buildBucket(listAction, MDSALUtil.GROUP_WEIGHT, bucketId, MDSALUtil.WATCH_PORT,
1019                     MDSALUtil.WATCH_GROUP));
1020             bucketId++;
1021             List<Bucket> listBucketInfoRemote = getRemoteBCGroupBuckets(elanInfo, dpnInterfaces, dpnId, bucketId,
1022                     etreeLeafTag);
1023             listBucket.addAll(listBucketInfoRemote);
1024             long groupId = ElanUtils.getEtreeLeafRemoteBCGId(etreeLeafTag);
1025             Group group = MDSALUtil.buildGroup(groupId, elanInfo.getElanInstanceName(), GroupTypes.GroupAll,
1026                     MDSALUtil.buildBucketLists(listBucket));
1027             LOG.trace("Installing the remote BroadCast Group:{}", group);
1028             mdsalManager.syncInstallGroup(dpnId, group,
1029                     ElanConstants.DELAY_TIME_IN_MILLISECOND);
1030         }
1031     }
1032
1033     private void createDropBucket(List<Bucket> listBucket) {
1034         List<Action> actionsInfos = new ArrayList<>();
1035         actionsInfos.add(new ActionInfo(ActionType.drop_action, new String[] {}).buildAction());
1036         Bucket dropBucket = MDSALUtil.buildBucket(actionsInfos, MDSALUtil.GROUP_WEIGHT, 0, MDSALUtil.WATCH_PORT,
1037                 MDSALUtil.WATCH_GROUP);
1038         listBucket.add(dropBucket);
1039     }
1040
1041     public void setupLocalBroadcastGroups(ElanInstance elanInfo, DpnInterfaces newDpnInterface,
1042             InterfaceInfo interfaceInfo) {
1043         setupStandardLocalBroadcastGroups(elanInfo, newDpnInterface, interfaceInfo);
1044         setupLeavesLocalBroadcastGroups(elanInfo, newDpnInterface, interfaceInfo);
1045     }
1046
1047     public void setupStandardLocalBroadcastGroups(ElanInstance elanInfo, DpnInterfaces newDpnInterface,
1048             InterfaceInfo interfaceInfo) {
1049         List<Bucket> listBucket = new ArrayList<>();
1050         int bucketId = 0;
1051         long groupId = ElanUtils.getElanLocalBCGId(elanInfo.getElanTag());
1052
1053         List<String> interfaces = new ArrayList<>();
1054         if (newDpnInterface != null) {
1055             interfaces = newDpnInterface.getInterfaces();
1056         }
1057         for (String ifName : interfaces) {
1058             // In case if there is a InterfacePort in the cache which is not in
1059             // operational state, skip processing it
1060             InterfaceInfo ifInfo = interfaceManager
1061                     .getInterfaceInfoFromOperationalDataStore(ifName, interfaceInfo.getInterfaceType());
1062             if (!isOperational(ifInfo)) {
1063                 continue;
1064             }
1065
1066             if (!elanUtils.isExternal(ifName)) {
1067                 listBucket.add(MDSALUtil.buildBucket(getInterfacePortActions(ifInfo), MDSALUtil.GROUP_WEIGHT, bucketId,
1068                         MDSALUtil.WATCH_PORT, MDSALUtil.WATCH_GROUP));
1069                 bucketId++;
1070             }
1071         }
1072
1073         Group group = MDSALUtil.buildGroup(groupId, elanInfo.getElanInstanceName(), GroupTypes.GroupAll,
1074                 MDSALUtil.buildBucketLists(listBucket));
1075         LOG.trace("installing the localBroadCast Group:{}", group);
1076         mdsalManager.syncInstallGroup(interfaceInfo.getDpId(), group,
1077                 ElanConstants.DELAY_TIME_IN_MILLISECOND);
1078     }
1079
1080     private void setupLeavesLocalBroadcastGroups(ElanInstance elanInfo, DpnInterfaces newDpnInterface,
1081             InterfaceInfo interfaceInfo) {
1082         EtreeInstance etreeInstance = elanInfo.getAugmentation(EtreeInstance.class);
1083         if (etreeInstance != null) {
1084             List<Bucket> listBucket = new ArrayList<>();
1085             int bucketId = 0;
1086
1087             List<String> interfaces = new ArrayList<>();
1088             if (newDpnInterface != null) {
1089                 interfaces = newDpnInterface.getInterfaces();
1090             }
1091             for (String ifName : interfaces) {
1092                 // In case if there is a InterfacePort in the cache which is not
1093                 // in
1094                 // operational state, skip processing it
1095                 InterfaceInfo ifInfo = interfaceManager
1096                         .getInterfaceInfoFromOperationalDataStore(ifName, interfaceInfo.getInterfaceType());
1097                 if (!isOperational(ifInfo)) {
1098                     continue;
1099                 }
1100
1101                 if (!elanUtils.isExternal(ifName)) {
1102                     // only add root interfaces
1103                     bucketId = addInterfaceIfRootInterface(bucketId, ifName, listBucket, ifInfo);
1104                 }
1105             }
1106
1107             if (listBucket.size() == 0) { // No Buckets
1108                 createDropBucket(listBucket);
1109             }
1110
1111             long etreeLeafTag = etreeInstance.getEtreeLeafTagVal().getValue();
1112             long groupId = ElanUtils.getEtreeLeafLocalBCGId(etreeLeafTag);
1113             Group group = MDSALUtil.buildGroup(groupId, elanInfo.getElanInstanceName(), GroupTypes.GroupAll,
1114                     MDSALUtil.buildBucketLists(listBucket));
1115             LOG.trace("installing the localBroadCast Group:{}", group);
1116             mdsalManager.syncInstallGroup(interfaceInfo.getDpId(), group,
1117                     ElanConstants.DELAY_TIME_IN_MILLISECOND);
1118         }
1119     }
1120
1121     private int addInterfaceIfRootInterface(int bucketId, String ifName, List<Bucket> listBucket,
1122             InterfaceInfo ifInfo) {
1123         EtreeInterface etreeInterface = ElanUtils.getEtreeInterfaceByElanInterfaceName(broker, ifName);
1124         if (etreeInterface != null && etreeInterface.getEtreeInterfaceType() == EtreeInterfaceType.Root) {
1125             listBucket.add(MDSALUtil.buildBucket(getInterfacePortActions(ifInfo), MDSALUtil.GROUP_WEIGHT, bucketId,
1126                     MDSALUtil.WATCH_PORT, MDSALUtil.WATCH_GROUP));
1127             bucketId++;
1128         }
1129         return bucketId;
1130     }
1131
1132     public void removeLocalBroadcastGroup(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
1133             WriteTransaction deleteFlowGroupTx) {
1134         BigInteger dpnId = interfaceInfo.getDpId();
1135         long groupId = ElanUtils.getElanLocalBCGId(elanInfo.getElanTag());
1136         List<Bucket> listBuckets = new ArrayList<>();
1137         int bucketId = 0;
1138         listBuckets.add(getLocalBCGroupBucketInfo(interfaceInfo, bucketId));
1139         // listBuckets.addAll(getRemoteBCGroupBucketInfos(elanInfo, 1,
1140         // interfaceInfo));
1141         Group group = MDSALUtil.buildGroup(groupId, elanInfo.getElanInstanceName(), GroupTypes.GroupAll,
1142                 MDSALUtil.buildBucketLists(listBuckets));
1143         LOG.trace("deleted the localBroadCast Group:{}", group);
1144         mdsalManager.removeGroupToTx(dpnId, group, deleteFlowGroupTx);
1145     }
1146
1147     public void removeElanBroadcastGroup(ElanInstance elanInfo, InterfaceInfo interfaceInfo,
1148             WriteTransaction deleteFlowGroupTx) {
1149         int bucketId = 0;
1150         int actionKey = 0;
1151         Long elanTag = elanInfo.getElanTag();
1152         List<Bucket> listBuckets = new ArrayList<>();
1153         List<Action> listAction = new ArrayList<>();
1154         listAction.add(new ActionInfo(ActionType.group,
1155                 new String[] { String.valueOf(ElanUtils.getElanLocalBCGId(elanTag)) }, ++actionKey).buildAction());
1156         listBuckets.add(MDSALUtil.buildBucket(listAction, MDSALUtil.GROUP_WEIGHT, bucketId, MDSALUtil.WATCH_PORT,
1157                 MDSALUtil.WATCH_GROUP));
1158         bucketId++;
1159         listBuckets.addAll(getRemoteBCGroupBucketInfos(elanInfo, bucketId, interfaceInfo, elanInfo.getElanTag()));
1160         BigInteger dpnId = interfaceInfo.getDpId();
1161         long groupId = ElanUtils.getElanRemoteBCGId(elanInfo.getElanTag());
1162         Group group = MDSALUtil.buildGroup(groupId, elanInfo.getElanInstanceName(), GroupTypes.GroupAll,
1163                 MDSALUtil.buildBucketLists(listBuckets));
1164         LOG.trace("deleting the remoteBroadCast group:{}", group);
1165         mdsalManager.removeGroupToTx(dpnId, group, deleteFlowGroupTx);
1166     }
1167
1168     /**
1169      * Installs a flow in the External Tunnel table consisting in translating
1170      * the VNI retrieved from the packet that came over a tunnel with a TOR into
1171      * elanTag that will be used later in the ELANs pipeline.
1172      *
1173      * @param dpnId
1174      *            the dpn id
1175      * @param elanInfo
1176      *            the elan info
1177      */
1178     public void setExternalTunnelTable(BigInteger dpnId, ElanInstance elanInfo) {
1179         long elanTag = elanInfo.getElanTag();
1180         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpnId, NwConstants.EXTERNAL_TUNNEL_TABLE,
1181                 getFlowRef(NwConstants.EXTERNAL_TUNNEL_TABLE, elanTag), 5, // prio
1182                 elanInfo.getElanInstanceName(), // flowName
1183                 0, // idleTimeout
1184                 0, // hardTimeout
1185                 ITMConstants.COOKIE_ITM_EXTERNAL.add(BigInteger.valueOf(elanTag)),
1186                 buildMatchesForVni(elanInfo.getSegmentationId()), getInstructionsExtTunnelTable(elanTag));
1187
1188         mdsalManager.installFlow(flowEntity);
1189     }
1190
1191     /**
1192      * Removes, from External Tunnel table, the flow that translates from VNI to
1193      * elanTag. Important: ensure this method is only called whenever there is
1194      * no other ElanInterface in the specified DPN
1195      *
1196      * @param dpnId
1197      *            DPN whose Ext Tunnel table is going to be modified
1198      * @param elanInfo
1199      *            holds the elanTag needed for selecting the flow to be removed
1200      */
1201     public void unsetExternalTunnelTable(BigInteger dpnId, ElanInstance elanInfo) {
1202         // TODO: Use DataStoreJobCoordinator in order to avoid that removing the
1203         // last ElanInstance plus
1204         // adding a new one does (almost at the same time) are executed in that
1205         // exact order
1206
1207         String flowId = getFlowRef(NwConstants.EXTERNAL_TUNNEL_TABLE, elanInfo.getElanTag());
1208         FlowEntity flowEntity = new FlowEntity(dpnId);
1209         flowEntity.setTableId(NwConstants.EXTERNAL_TUNNEL_TABLE);
1210         flowEntity.setFlowId(flowId);
1211         mdsalManager.removeFlow(flowEntity);
1212     }
1213
1214     public void setupTerminateServiceTable(ElanInstance elanInfo, BigInteger dpId, WriteTransaction writeFlowGroupTx) {
1215         setupTerminateServiceTable(elanInfo, dpId, elanInfo.getElanTag(), writeFlowGroupTx);
1216         setupEtreeTerminateServiceTable(elanInfo, dpId, writeFlowGroupTx);
1217     }
1218
1219     public void setupTerminateServiceTable(ElanInstance elanInfo, BigInteger dpId, long elanTag,
1220             WriteTransaction writeFlowGroupTx) {
1221         Flow flowEntity = MDSALUtil.buildFlowNew(NwConstants.INTERNAL_TUNNEL_TABLE,
1222                 getFlowRef(NwConstants.INTERNAL_TUNNEL_TABLE, elanTag), 5,
1223                 String.format("%s:%d", "ITM Flow Entry ", elanTag), 0, 0,
1224                 ITMConstants.COOKIE_ITM.add(BigInteger.valueOf(elanTag)),
1225                 ElanUtils.getTunnelMatchesForServiceId((int) elanTag),
1226                 getInstructionsForOutGroup(ElanUtils.getElanLocalBCGId(elanTag)));
1227
1228         mdsalManager.addFlowToTx(dpId, flowEntity, writeFlowGroupTx);
1229     }
1230
1231     private void setupEtreeTerminateServiceTable(ElanInstance elanInfo, BigInteger dpId,
1232             WriteTransaction writeFlowGroupTx) {
1233         EtreeInstance etreeInstance = elanInfo.getAugmentation(EtreeInstance.class);
1234         if (etreeInstance != null) {
1235             setupTerminateServiceTable(elanInfo, dpId, etreeInstance.getEtreeLeafTagVal().getValue(), writeFlowGroupTx);
1236         }
1237     }
1238
1239     public void setupUnknownDMacTable(ElanInstance elanInfo, BigInteger dpId, WriteTransaction writeFlowGroupTx) {
1240         long elanTag = elanInfo.getElanTag();
1241         installLocalUnknownFlow(elanInfo, dpId, elanTag, writeFlowGroupTx);
1242         installRemoteUnknownFlow(elanInfo, dpId, elanTag, writeFlowGroupTx);
1243         setupEtreeUnknownDMacTable(elanInfo, dpId, elanTag, writeFlowGroupTx);
1244     }
1245
1246     private void setupEtreeUnknownDMacTable(ElanInstance elanInfo, BigInteger dpId, long elanTag,
1247             WriteTransaction writeFlowGroupTx) {
1248         EtreeLeafTagName etreeLeafTag = elanUtils.getEtreeLeafTagByElanTag(elanTag);
1249         if (etreeLeafTag != null) {
1250             long leafTag = etreeLeafTag.getEtreeLeafTag().getValue();
1251             installRemoteUnknownFlow(elanInfo, dpId, leafTag, writeFlowGroupTx);
1252             installLocalUnknownFlow(elanInfo, dpId, leafTag, writeFlowGroupTx);
1253         }
1254     }
1255
1256     private void installLocalUnknownFlow(ElanInstance elanInfo, BigInteger dpId, long elanTag,
1257             WriteTransaction writeFlowGroupTx) {
1258         Flow flowEntity = MDSALUtil.buildFlowNew(NwConstants.ELAN_UNKNOWN_DMAC_TABLE,
1259                 getUnknownDmacFlowRef(NwConstants.ELAN_UNKNOWN_DMAC_TABLE, elanTag,
1260                         /* SH flag */false),
1261                 5, elanInfo.getElanInstanceName(), 0, 0,
1262                 ElanConstants.COOKIE_ELAN_UNKNOWN_DMAC.add(BigInteger.valueOf(elanTag)),
1263                 getMatchesForElanTag(elanTag, /* SH flag */false),
1264                 getInstructionsForOutGroup(ElanUtils.getElanRemoteBCGId(elanTag)));
1265
1266         mdsalManager.addFlowToTx(dpId, flowEntity, writeFlowGroupTx);
1267     }
1268
1269     private void installRemoteUnknownFlow(ElanInstance elanInfo, BigInteger dpId, long elanTag,
1270             WriteTransaction writeFlowGroupTx) {
1271         // only if ELAN can connect to external network, perform the following
1272         if (ElanUtils.isVxlan(elanInfo) || ElanUtils.isVlan(elanInfo) || ElanUtils.isFlat(elanInfo)) {
1273             Flow flowEntity2 = MDSALUtil.buildFlowNew(NwConstants.ELAN_UNKNOWN_DMAC_TABLE,
1274                     getUnknownDmacFlowRef(NwConstants.ELAN_UNKNOWN_DMAC_TABLE, elanTag,
1275                             /* SH flag */true),
1276                     5, elanInfo.getElanInstanceName(), 0, 0,
1277                     ElanConstants.COOKIE_ELAN_UNKNOWN_DMAC.add(BigInteger.valueOf(elanTag)),
1278                     getMatchesForElanTag(elanTag, /* SH flag */true),
1279                     getInstructionsForOutGroup(ElanUtils.getElanLocalBCGId(elanTag)));
1280             mdsalManager.addFlowToTx(dpId, flowEntity2, writeFlowGroupTx);
1281         }
1282     }
1283
1284
1285     private void removeUnknownDmacFlow(BigInteger dpId, ElanInstance elanInfo, WriteTransaction deleteFlowGroupTx,
1286             long elanTag) {
1287         Flow flow = new FlowBuilder().setId(new FlowId(getUnknownDmacFlowRef(NwConstants.ELAN_UNKNOWN_DMAC_TABLE,
1288                 elanTag, /* SH flag */ false))).setTableId(NwConstants.ELAN_UNKNOWN_DMAC_TABLE).build();
1289         mdsalManager.removeFlowToTx(dpId, flow, deleteFlowGroupTx);
1290
1291         if (ElanUtils.isVxlan(elanInfo)) {
1292             Flow flow2 = new FlowBuilder().setId(new FlowId(getUnknownDmacFlowRef(NwConstants.ELAN_UNKNOWN_DMAC_TABLE,
1293                     elanTag, /* SH flag */ true))).setTableId(NwConstants.ELAN_UNKNOWN_DMAC_TABLE)
1294                     .build();
1295             mdsalManager.removeFlowToTx(dpId, flow2, deleteFlowGroupTx);
1296         }
1297     }
1298
1299     private void removeDefaultTermFlow(BigInteger dpId, long elanTag) {
1300         elanUtils.removeTerminatingServiceAction(dpId, (int) elanTag);
1301     }
1302
1303     private void bindService(ElanInstance elanInfo, ElanInterface elanInterface, WriteTransaction tx) {
1304         if (isStandardElanService(elanInterface)) {
1305             bindElanService(elanInfo.getElanTag(), elanInfo.getElanInstanceName(), elanInterface.getName(), tx);
1306         } else { // Etree service
1307             bindEtreeService(elanInfo, elanInterface, tx);
1308         }
1309     }
1310
1311     private void bindElanService(long elanTag, String elanInstanceName, String interfaceName, WriteTransaction tx) {
1312         int priority = ElanConstants.ELAN_SERVICE_PRIORITY;
1313         int instructionKey = 0;
1314         List<Instruction> instructions = new ArrayList<>();
1315         instructions.add(MDSALUtil.buildAndGetWriteMetadaInstruction(ElanUtils.getElanMetadataLabel(elanTag),
1316                 MetaDataUtil.METADATA_MASK_SERVICE, ++instructionKey));
1317         instructions.add(MDSALUtil.buildAndGetGotoTableInstruction(NwConstants.ELAN_SMAC_TABLE, ++instructionKey));
1318         short elanServiceIndex = ServiceIndex.getIndex(NwConstants.ELAN_SERVICE_NAME, NwConstants.ELAN_SERVICE_INDEX);
1319         BoundServices serviceInfo = ElanUtils.getBoundServices(
1320                 String.format("%s.%s.%s", "vpn", elanInstanceName, interfaceName), elanServiceIndex,
1321                 priority, NwConstants.COOKIE_ELAN_INGRESS_TABLE, instructions);
1322         tx.put(LogicalDatastoreType.CONFIGURATION,
1323                 ElanUtils.buildServiceId(interfaceName, elanServiceIndex), serviceInfo, true);
1324     }
1325
1326     private void bindEtreeService(ElanInstance elanInfo, ElanInterface elanInterface, WriteTransaction tx) {
1327         if (elanInterface.getAugmentation(EtreeInterface.class).getEtreeInterfaceType() == EtreeInterfaceType.Root) {
1328             bindElanService(elanInfo.getElanTag(), elanInfo.getElanInstanceName(), elanInterface.getName(), tx);
1329         } else {
1330             EtreeInstance etreeInstance = elanInfo.getAugmentation(EtreeInstance.class);
1331             if (etreeInstance == null) {
1332                 LOG.error("EtreeInterface " + elanInterface.getName() + " is associated with a non EtreeInstance: "
1333                         + elanInfo.getElanInstanceName());
1334             } else {
1335                 bindElanService(etreeInstance.getEtreeLeafTagVal().getValue(), elanInfo.getElanInstanceName(),
1336                         elanInterface.getName(), tx);
1337             }
1338         }
1339     }
1340
1341     private boolean isStandardElanService(ElanInterface elanInterface) {
1342         return elanInterface.getAugmentation(EtreeInterface.class) == null;
1343     }
1344
1345     private boolean isStandardElanService(ElanInstance elanInstance) {
1346         return elanInstance.getAugmentation(EtreeInstance.class) == null;
1347     }
1348
1349     private void unbindService(ElanInstance elanInfo, String interfaceName, WriteTransaction tx) {
1350         tx.delete(LogicalDatastoreType.CONFIGURATION, ElanUtils.buildServiceId(interfaceName,
1351                 ServiceIndex.getIndex(NwConstants.ELAN_SERVICE_NAME, NwConstants.ELAN_SERVICE_INDEX)));
1352     }
1353
1354     private String getFlowRef(long tableId, long elanTag) {
1355         return new StringBuffer().append(tableId).append(elanTag).toString();
1356     }
1357
1358     private String getUnknownDmacFlowRef(long tableId, long elanTag, boolean shFlag) {
1359         return new StringBuffer().append(tableId).append(elanTag).append(shFlag).toString();
1360     }
1361
1362     private List<Action> getInterfacePortActions(InterfaceInfo interfaceInfo) {
1363         List<Action> listAction = new ArrayList<>();
1364         int actionKey = 0;
1365         listAction.add(new ActionInfo(ActionType.set_field_tunnel_id,
1366                 new BigInteger[] { BigInteger.valueOf(interfaceInfo.getInterfaceTag()) }, actionKey).buildAction());
1367         actionKey++;
1368         listAction.add(new ActionInfo(ActionType.nx_resubmit,
1369                 new String[] { String.valueOf(NwConstants.ELAN_FILTER_EQUALS_TABLE) }, actionKey).buildAction());
1370         return listAction;
1371     }
1372
1373     private DpnInterfaces updateElanDpnInterfacesList(String elanInstanceName, BigInteger dpId,
1374             List<String> interfaceNames, WriteTransaction tx) {
1375         DpnInterfaces dpnInterface = new DpnInterfacesBuilder().setDpId(dpId).setInterfaces(interfaceNames)
1376                 .setKey(new DpnInterfacesKey(dpId)).build();
1377         tx.put(LogicalDatastoreType.OPERATIONAL,
1378                 ElanUtils.getElanDpnInterfaceOperationalDataPath(elanInstanceName, dpId), dpnInterface, true);
1379         return dpnInterface;
1380     }
1381
1382     /**
1383      * Delete elan dpn interface from operational DS.
1384      *
1385      * @param elanInstanceName
1386      *            the elan instance name
1387      * @param dpId
1388      *            the dp id
1389      */
1390     private void deleteElanDpnInterface(String elanInstanceName, BigInteger dpId, WriteTransaction tx) {
1391         tx.delete(LogicalDatastoreType.OPERATIONAL,
1392                 ElanUtils.getElanDpnInterfaceOperationalDataPath(elanInstanceName, dpId));
1393     }
1394
1395     private DpnInterfaces createElanInterfacesList(String elanInstanceName, String interfaceName, BigInteger dpId,
1396             WriteTransaction tx) {
1397         List<String> interfaceNames = new ArrayList<>();
1398         interfaceNames.add(interfaceName);
1399         DpnInterfaces dpnInterface = new DpnInterfacesBuilder().setDpId(dpId).setInterfaces(interfaceNames)
1400                 .setKey(new DpnInterfacesKey(dpId)).build();
1401         tx.put(LogicalDatastoreType.OPERATIONAL,
1402                 ElanUtils.getElanDpnInterfaceOperationalDataPath(elanInstanceName, dpId), dpnInterface, true);
1403         return dpnInterface;
1404     }
1405
1406     private void createElanInterfaceTablesList(String interfaceName, WriteTransaction tx) {
1407         InstanceIdentifier<ElanInterfaceMac> elanInterfaceMacTables = ElanUtils
1408                 .getElanInterfaceMacEntriesOperationalDataPath(interfaceName);
1409         Optional<ElanInterfaceMac> interfaceMacTables = elanUtils.read(broker,
1410                 LogicalDatastoreType.OPERATIONAL, elanInterfaceMacTables);
1411         // Adding new Elan Interface Port to the operational DataStore without
1412         // Static-Mac Entries..
1413         if (!interfaceMacTables.isPresent()) {
1414             ElanInterfaceMac elanInterfaceMacTable = new ElanInterfaceMacBuilder().setElanInterface(interfaceName)
1415                     .setKey(new ElanInterfaceMacKey(interfaceName)).build();
1416             tx.put(LogicalDatastoreType.OPERATIONAL,
1417                     ElanUtils.getElanInterfaceMacEntriesOperationalDataPath(interfaceName), elanInterfaceMacTable,
1418                     true);
1419         }
1420     }
1421
1422     private void createElanStateList(String elanInstanceName, String interfaceName, WriteTransaction tx) {
1423         InstanceIdentifier<Elan> elanInstance = ElanUtils.getElanInstanceOperationalDataPath(elanInstanceName);
1424         Optional<Elan> elanInterfaceLists = elanUtils.read(broker,
1425                 LogicalDatastoreType.OPERATIONAL, elanInstance);
1426         // Adding new Elan Interface Port to the operational DataStore without
1427         // Static-Mac Entries..
1428         if (elanInterfaceLists.isPresent()) {
1429             List<String> interfaceLists = elanInterfaceLists.get().getElanInterfaces();
1430             if (interfaceLists == null) {
1431                 interfaceLists = new ArrayList<>();
1432             }
1433             interfaceLists.add(interfaceName);
1434             Elan elanState = new ElanBuilder().setName(elanInstanceName).setElanInterfaces(interfaceLists)
1435                     .setKey(new ElanKey(elanInstanceName)).build();
1436             tx.put(LogicalDatastoreType.OPERATIONAL, ElanUtils.getElanInstanceOperationalDataPath(elanInstanceName),
1437                     elanState, true);
1438         }
1439     }
1440
1441     private boolean isOperational(InterfaceInfo interfaceInfo) {
1442         if (interfaceInfo == null) {
1443             return false;
1444         }
1445         return interfaceInfo.getAdminState() == InterfaceInfo.InterfaceAdminState.ENABLED;
1446     }
1447
1448     public void handleInternalTunnelStateEvent(BigInteger srcDpId, BigInteger dstDpId) throws ElanException {
1449         ElanDpnInterfaces dpnInterfaceLists = elanUtils.getElanDpnInterfacesList();
1450         if (dpnInterfaceLists == null) {
1451             return;
1452         }
1453         List<ElanDpnInterfacesList> elanDpnIf = dpnInterfaceLists.getElanDpnInterfacesList();
1454         for (ElanDpnInterfacesList elanDpns : elanDpnIf) {
1455             int cnt = 0;
1456             String elanName = elanDpns.getElanInstanceName();
1457             List<DpnInterfaces> dpnInterfaces = elanDpns.getDpnInterfaces();
1458             if (dpnInterfaces == null) {
1459                 continue;
1460             }
1461             for (DpnInterfaces dpnIf : dpnInterfaces) {
1462                 if (dpnIf.getDpId().equals(srcDpId) || dpnIf.getDpId().equals(dstDpId)) {
1463                     cnt++;
1464                 }
1465             }
1466             if (cnt == 2) {
1467                 LOG.debug("Elan instance:{} is present b/w srcDpn:{} and dstDpn:{}", elanName, srcDpId, dstDpId);
1468                 ElanInstance elanInfo = ElanUtils.getElanInstanceByName(broker, elanName);
1469                 // update Remote BC Group
1470                 setupElanBroadcastGroups(elanInfo, srcDpId);
1471
1472                 DpnInterfaces dpnInterface = elanUtils.getElanInterfaceInfoByElanDpn(elanName, dstDpId);
1473                 Set<String> interfaceLists = new HashSet<>();
1474                 interfaceLists.addAll(dpnInterface.getInterfaces());
1475                 for (String ifName : interfaceLists) {
1476                     InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfo(ifName);
1477                     if (isOperational(interfaceInfo)) {
1478                         installDMacAddressTables(elanInfo, interfaceInfo, srcDpId);
1479                     }
1480                 }
1481             }
1482
1483         }
1484     }
1485
1486     /**
1487      * Handle external tunnel state event.
1488      *
1489      * @param externalTunnel
1490      *            the external tunnel
1491      * @param intrf
1492      *            the interface
1493      * @throws ElanException in case of issues creating the flow objects
1494      */
1495     public void handleExternalTunnelStateEvent(ExternalTunnel externalTunnel, Interface intrf) throws ElanException {
1496         if (!validateExternalTunnelStateEvent(externalTunnel, intrf)) {
1497             return;
1498         }
1499         // dpId/externalNodeId will be available either in source or destination
1500         // based on the tunnel end point
1501         BigInteger dpId = null;
1502         NodeId externalNodeId = null;
1503         if (StringUtils.isNumeric(externalTunnel.getSourceDevice())) {
1504             dpId = new BigInteger(externalTunnel.getSourceDevice());
1505             externalNodeId = new NodeId(externalTunnel.getDestinationDevice());
1506         } else if (StringUtils.isNumeric(externalTunnel.getDestinationDevice())) {
1507             dpId = new BigInteger(externalTunnel.getDestinationDevice());
1508             externalNodeId = new NodeId(externalTunnel.getSourceDevice());
1509         }
1510         if (dpId == null || externalNodeId == null) {
1511             LOG.error("Dp ID / externalNodeId not found in external tunnel {}", externalTunnel);
1512             return;
1513         }
1514
1515         ElanDpnInterfaces dpnInterfaceLists = elanUtils.getElanDpnInterfacesList();
1516         if (dpnInterfaceLists == null) {
1517             return;
1518         }
1519         List<ElanDpnInterfacesList> elanDpnIf = dpnInterfaceLists.getElanDpnInterfacesList();
1520         for (ElanDpnInterfacesList elanDpns : elanDpnIf) {
1521             String elanName = elanDpns.getElanInstanceName();
1522             ElanInstance elanInfo = ElanUtils.getElanInstanceByName(broker, elanName);
1523
1524             DpnInterfaces dpnInterfaces = elanUtils.getElanInterfaceInfoByElanDpn(elanName, dpId);
1525             if (dpnInterfaces == null || dpnInterfaces.getInterfaces() == null
1526                     || dpnInterfaces.getInterfaces().isEmpty()) {
1527                 continue;
1528             }
1529             LOG.debug("Elan instance:{} is present in Dpn:{} ", elanName, dpId);
1530
1531             setupElanBroadcastGroups(elanInfo, dpId);
1532             // install L2gwDevices local macs in dpn.
1533             elanL2GatewayUtils.installL2gwDeviceMacsInDpn(dpId, externalNodeId, elanInfo, intrf.getName());
1534             // Install dpn macs on external device
1535             elanL2GatewayUtils.installDpnMacsInL2gwDevice(elanName, new HashSet<>(dpnInterfaces.getInterfaces()), dpId,
1536                     externalNodeId);
1537         }
1538         LOG.info("Handled ExternalTunnelStateEvent for {}", externalTunnel);
1539     }
1540
1541     /**
1542      * Validate external tunnel state event.
1543      *
1544      * @param externalTunnel
1545      *            the external tunnel
1546      * @param intrf
1547      *            the intrf
1548      * @return true, if successful
1549      */
1550     private boolean validateExternalTunnelStateEvent(ExternalTunnel externalTunnel, Interface intrf) {
1551         if (intrf.getOperStatus() == Interface.OperStatus.Up) {
1552             String srcDevice = externalTunnel.getDestinationDevice();
1553             String destDevice = externalTunnel.getSourceDevice();
1554             ExternalTunnel otherEndPointExtTunnel = elanUtils.getExternalTunnel(srcDevice, destDevice,
1555                     LogicalDatastoreType.CONFIGURATION);
1556             if (LOG.isTraceEnabled()) {
1557                 LOG.trace("Validating external tunnel state: src tunnel {}, dest tunnel {}", externalTunnel,
1558                         otherEndPointExtTunnel);
1559             }
1560             if (otherEndPointExtTunnel != null) {
1561                 boolean otherEndPointInterfaceOperational = ElanUtils.isInterfaceOperational(
1562                         otherEndPointExtTunnel.getTunnelInterfaceName(), broker);
1563                 if (otherEndPointInterfaceOperational) {
1564                     return true;
1565                 } else {
1566                     LOG.debug("Other end [{}] of the external tunnel is not yet UP for {}",
1567                             otherEndPointExtTunnel.getTunnelInterfaceName(), externalTunnel);
1568                 }
1569             }
1570         }
1571         return false;
1572     }
1573
1574     private List<MatchInfo> getMatchesForFilterEqualsLPortTag(int lportTag) {
1575         List<MatchInfo> mkMatches = new ArrayList<>();
1576         // Matching metadata
1577         mkMatches.add(new MatchInfo(MatchFieldType.metadata,
1578                 new BigInteger[] { MetaDataUtil.getLportTagMetaData(lportTag), MetaDataUtil.METADATA_MASK_LPORT_TAG }));
1579         mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] { BigInteger.valueOf(lportTag) }));
1580         return mkMatches;
1581     }
1582
1583     private List<MatchInfo> getTunnelIdMatchForFilterEqualsLPortTag(int lportTag) {
1584         List<MatchInfo> mkMatches = new ArrayList<>();
1585         // Matching metadata
1586         mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] { BigInteger.valueOf(lportTag) }));
1587         return mkMatches;
1588     }
1589
1590     public void updateRemoteBroadcastGroupForAllElanDpns(ElanInstance elanInfo) {
1591         List<DpnInterfaces> dpns = elanUtils.getInvolvedDpnsInElan(elanInfo.getElanInstanceName());
1592         if (dpns == null) {
1593             return;
1594         }
1595         for (DpnInterfaces dpn : dpns) {
1596             setupElanBroadcastGroups(elanInfo, dpn.getDpId());
1597         }
1598     }
1599
1600     public List<Bucket> getRemoteBCGroupBucketsOfElanL2GwDevices(ElanInstance elanInfo, BigInteger dpnId,
1601             int bucketId) {
1602         List<Bucket> listBucketInfo = new ArrayList<>();
1603         ConcurrentMap<String, L2GatewayDevice> map = ElanL2GwCacheUtils
1604                 .getInvolvedL2GwDevices(elanInfo.getElanInstanceName());
1605         for (L2GatewayDevice device : map.values()) {
1606             String interfaceName = elanL2GatewayUtils.getExternalTunnelInterfaceName(String.valueOf(dpnId),
1607                     device.getHwvtepNodeId());
1608             if (interfaceName == null) {
1609                 continue;
1610             }
1611             List<Action> listActionInfo = elanUtils.buildTunnelItmEgressActions(interfaceName,
1612                     elanInfo.getSegmentationId());
1613             listBucketInfo.add(MDSALUtil.buildBucket(listActionInfo, MDSALUtil.GROUP_WEIGHT, bucketId,
1614                     MDSALUtil.WATCH_PORT, MDSALUtil.WATCH_GROUP));
1615             bucketId++;
1616         }
1617         return listBucketInfo;
1618     }
1619
1620     @Override
1621     protected ElanInterfaceManager getDataTreeChangeListener() {
1622         return this;
1623     }
1624 }