Use IpAddress-based createHwvtepPhysicalLocator
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / utils / ElanL2GatewayMulticastUtils.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.l2gw.utils;
9
10 import static java.util.Collections.emptyList;
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12 import static org.opendaylight.netvirt.elan.utils.ElanUtils.isVxlanNetworkOrVxlanSegment;
13
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.Objects;
21 import java.util.concurrent.ConcurrentMap;
22 import javax.annotation.Nonnull;
23 import javax.inject.Inject;
24 import javax.inject.Singleton;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
29 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
30 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
31 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
32 import org.opendaylight.genius.mdsalutil.MDSALUtil;
33 import org.opendaylight.genius.mdsalutil.actions.ActionGroup;
34 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
35 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
36 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
37 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
38 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
39 import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
40 import org.opendaylight.netvirt.elan.l2gw.jobs.HwvtepDeviceMcastMacUpdateJob;
41 import org.opendaylight.netvirt.elan.utils.ElanConstants;
42 import org.opendaylight.netvirt.elan.utils.ElanItmUtils;
43 import org.opendaylight.netvirt.elan.utils.ElanUtils;
44 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
45 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp.rev160428.DesignatedSwitchesForExternalTunnels;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp.rev160428.designated.switches._for.external.tunnels.DesignatedSwitchForTunnel;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.dhcp.rev160428.designated.switches._for.external.tunnels.DesignatedSwitchForTunnelKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.elan.instance.ExternalTeps;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.elan.instance.ExternalTepsBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.elan.instance.ExternalTepsKey;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSet;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSetBuilder;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
77 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
80
81 /**
82  * The utility class to handle ELAN L2 Gateway related to multicast.
83  */
84 @Singleton
85 public class ElanL2GatewayMulticastUtils {
86
87     /** The Constant LOG. */
88     private static final Logger LOG = LoggerFactory.getLogger(ElanL2GatewayMulticastUtils.class);
89
90     /** The broker. */
91     private final DataBroker broker;
92     private final ManagedNewTransactionRunner txRunner;
93
94     private final ElanItmUtils elanItmUtils;
95     private final JobCoordinator jobCoordinator;
96     private final ElanUtils elanUtils;
97     private final IMdsalApiManager mdsalManager;
98     private final IInterfaceManager interfaceManager;
99
100     @Inject
101     public ElanL2GatewayMulticastUtils(DataBroker broker, ElanItmUtils elanItmUtils, JobCoordinator jobCoordinator,
102             ElanUtils elanUtils, IMdsalApiManager mdsalManager, IInterfaceManager interfaceManager) {
103         this.broker = broker;
104         this.txRunner = new ManagedNewTransactionRunnerImpl(broker);
105         this.elanItmUtils = elanItmUtils;
106         this.jobCoordinator = jobCoordinator;
107         this.elanUtils = elanUtils;
108         this.mdsalManager = mdsalManager;
109         this.interfaceManager = interfaceManager;
110     }
111
112     /**
113      * Handle mcast for elan l2 gw device add.
114      * @param elanName the elan name
115      * @param device the device
116      * @return the listenable future
117      */
118     public ListenableFuture<Void> handleMcastForElanL2GwDeviceAdd(String elanName, L2GatewayDevice device) {
119         InstanceIdentifier<ExternalTeps> tepPath = buildExternalTepPath(elanName, device.getTunnelIp());
120         JdkFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
121             tx.put(tepPath, buildExternalTeps(device));
122         }), LOG, "Failed to write to config external tep {}", tepPath);
123         return updateMcastMacsForAllElanDevices(elanName, device, true/* updateThisDevice */);
124     }
125
126     public static InstanceIdentifier<ExternalTeps> buildExternalTepPath(String elan, IpAddress tepIp) {
127         return InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, new ElanInstanceKey(elan))
128                 .child(ExternalTeps.class, new ExternalTepsKey(tepIp)).build();
129     }
130
131     protected ExternalTeps buildExternalTeps(L2GatewayDevice device) {
132         return new ExternalTepsBuilder().setTepIp(device.getTunnelIp()).setNodeid(device.getHwvtepNodeId()).build();
133     }
134
135     /**
136      * Updates the remote mcast mac table for all the devices in this elan
137      * includes all the dpn tep ips and other devices tep ips in broadcast
138      * locator set.
139      *
140      * @param elanName
141      *            the elan to be updated
142      * @return the listenable future
143      */
144     @SuppressWarnings("checkstyle:IllegalCatch")
145     public ListenableFuture<Void> updateRemoteMcastMacOnElanL2GwDevices(String elanName) {
146         return txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
147             for (L2GatewayDevice device : ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).values()) {
148                 prepareRemoteMcastMacUpdateOnDevice(elanName, device);
149             }
150         });
151     }
152
153     public void scheduleMcastMacUpdateJob(String elanName, L2GatewayDevice device) {
154         HwvtepDeviceMcastMacUpdateJob job = new HwvtepDeviceMcastMacUpdateJob(this, elanName,device);
155         jobCoordinator.enqueueJob(job.getJobKey(), job);
156     }
157
158     /**
159      * Update remote mcast mac on elan l2 gw device.
160      *
161      * @param elanName
162      *            the elan name
163      * @param device
164      *            the device
165      * @return the listenable future
166      */
167     public ListenableFuture<Void> updateRemoteMcastMacOnElanL2GwDevice(String elanName, L2GatewayDevice device) {
168         return txRunner.callWithNewWriteOnlyTransactionAndSubmit(
169             tx -> prepareRemoteMcastMacUpdateOnDevice(elanName, device));
170     }
171
172     public void prepareRemoteMcastMacUpdateOnDevice(String elanName,
173             L2GatewayDevice device) {
174         ConcurrentMap<String, L2GatewayDevice> elanL2gwDevices = ElanL2GwCacheUtils
175                 .getInvolvedL2GwDevices(elanName);
176         List<DpnInterfaces> dpns = elanUtils.getElanDPNByName(elanName);
177         List<IpAddress> dpnsTepIps = getAllTepIpsOfDpns(device, dpns);
178         List<IpAddress> l2GwDevicesTepIps = getAllTepIpsOfL2GwDevices(elanL2gwDevices);
179         preapareRemoteMcastMacEntry(elanName, device, dpnsTepIps, l2GwDevicesTepIps);
180     }
181
182     /**
183      * Update mcast macs for this elan.
184      * for all dpns in this elan  recompute and update broadcast group
185      * for all l2gw devices in this elan recompute and update remote mcast mac entry
186      *
187      * @param elanName
188      *            the elan name
189      * @param device
190      *            the device
191      * @param updateThisDevice
192      *            the update this device
193      * @return the listenable future
194      */
195     private ListenableFuture<Void> updateMcastMacsForAllElanDevices(String elanName, L2GatewayDevice device,
196                                                                     boolean updateThisDevice) {
197
198         SettableFuture<Void> ft = SettableFuture.create();
199         ft.set(null);
200
201         List<DpnInterfaces> dpns = elanUtils.getElanDPNByName(elanName);
202
203         ConcurrentMap<String, L2GatewayDevice> devices = ElanL2GwCacheUtils
204                 .getInvolvedL2GwDevices(elanName);
205
206         List<IpAddress> dpnsTepIps = getAllTepIpsOfDpns(device, dpns);
207         List<IpAddress> l2GwDevicesTepIps = getAllTepIpsOfL2GwDevices(devices);
208         // if (allTepIps.size() < 2) {
209         // LOG.debug("no other devices are found in the elan {}", elanName);
210         // return ft;
211         // }
212
213         return txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
214             if (updateThisDevice) {
215                 preapareRemoteMcastMacEntry(elanName, device, dpnsTepIps, l2GwDevicesTepIps);
216             }
217
218             // TODO: Need to revisit below logic as logical switches might not be
219             // present to configure RemoteMcastMac entry
220             for (L2GatewayDevice otherDevice : devices.values()) {
221                 if (!otherDevice.getDeviceName().equals(device.getDeviceName())) {
222                     preapareRemoteMcastMacEntry(elanName, otherDevice, dpnsTepIps, l2GwDevicesTepIps);
223                 }
224             }
225         });
226
227     }
228
229     public void updateRemoteBroadcastGroupForAllElanDpns(ElanInstance elanInfo) {
230         List<DpnInterfaces> dpns = elanUtils.getInvolvedDpnsInElan(elanInfo.getElanInstanceName());
231         for (DpnInterfaces dpn : dpns) {
232             setupElanBroadcastGroups(elanInfo, dpn.getDpId());
233         }
234     }
235
236     public void setupElanBroadcastGroups(ElanInstance elanInfo, BigInteger dpnId) {
237         setupElanBroadcastGroups(elanInfo, null, dpnId);
238     }
239
240     public void setupElanBroadcastGroups(ElanInstance elanInfo, DpnInterfaces dpnInterfaces, BigInteger dpnId) {
241         setupStandardElanBroadcastGroups(elanInfo, dpnInterfaces, dpnId);
242         setupLeavesEtreeBroadcastGroups(elanInfo, dpnInterfaces, dpnId);
243     }
244
245     public void setupStandardElanBroadcastGroups(ElanInstance elanInfo, DpnInterfaces dpnInterfaces, BigInteger dpnId) {
246         List<Bucket> listBucket = new ArrayList<>();
247         int bucketId = 0;
248         int actionKey = 0;
249         Long elanTag = elanInfo.getElanTag();
250         List<Action> listAction = new ArrayList<>();
251         listAction.add(new ActionGroup(ElanUtils.getElanLocalBCGId(elanTag)).buildAction(++actionKey));
252         listBucket.add(MDSALUtil.buildBucket(listAction, MDSALUtil.GROUP_WEIGHT, bucketId, MDSALUtil.WATCH_PORT,
253                 MDSALUtil.WATCH_GROUP));
254         bucketId++;
255         List<Bucket> listBucketInfoRemote = getRemoteBCGroupBuckets(elanInfo, dpnInterfaces, dpnId, bucketId, elanTag);
256         listBucket.addAll(listBucketInfoRemote);
257         long groupId = ElanUtils.getElanRemoteBCGId(elanTag);
258         Group group = MDSALUtil.buildGroup(groupId, elanInfo.getElanInstanceName(), GroupTypes.GroupAll,
259                 MDSALUtil.buildBucketLists(listBucket));
260         LOG.trace("Installing the remote BroadCast Group:{}", group);
261         mdsalManager.syncInstallGroup(dpnId, group);
262     }
263
264     public void setupLeavesEtreeBroadcastGroups(ElanInstance elanInfo, DpnInterfaces dpnInterfaces, BigInteger dpnId) {
265         EtreeInstance etreeInstance = elanInfo.augmentation(EtreeInstance.class);
266         if (etreeInstance != null) {
267             long etreeLeafTag = etreeInstance.getEtreeLeafTagVal().getValue();
268             List<Bucket> listBucket = new ArrayList<>();
269             int bucketId = 0;
270             int actionKey = 0;
271             List<Action> listAction = new ArrayList<>();
272             listAction.add(new ActionGroup(ElanUtils.getEtreeLeafLocalBCGId(etreeLeafTag)).buildAction(++actionKey));
273             listBucket.add(MDSALUtil.buildBucket(listAction, MDSALUtil.GROUP_WEIGHT, bucketId, MDSALUtil.WATCH_PORT,
274                     MDSALUtil.WATCH_GROUP));
275             bucketId++;
276             List<Bucket> listBucketInfoRemote = getRemoteBCGroupBuckets(elanInfo, dpnInterfaces, dpnId, bucketId,
277                     etreeLeafTag);
278             listBucket.addAll(listBucketInfoRemote);
279             long groupId = ElanUtils.getEtreeLeafRemoteBCGId(etreeLeafTag);
280             Group group = MDSALUtil.buildGroup(groupId, elanInfo.getElanInstanceName(), GroupTypes.GroupAll,
281                     MDSALUtil.buildBucketLists(listBucket));
282             LOG.trace("Installing the remote BroadCast Group:{}", group);
283             mdsalManager.syncInstallGroup(dpnId, group);
284         }
285     }
286
287     private DpnInterfaces getDpnInterfaces(ElanDpnInterfacesList elanDpns, BigInteger dpnId) {
288         if (elanDpns != null) {
289             for (DpnInterfaces dpnInterface : elanDpns.getDpnInterfaces()) {
290                 if (dpnInterface.getDpId().equals(dpnId)) {
291                     return dpnInterface;
292                 }
293             }
294         }
295         return null;
296     }
297
298     private List<Bucket> getRemoteBCGroupExternalPortBuckets(ElanDpnInterfacesList elanDpns,
299             DpnInterfaces dpnInterfaces, BigInteger dpnId, int bucketId) {
300         DpnInterfaces currDpnInterfaces = dpnInterfaces != null ? dpnInterfaces : getDpnInterfaces(elanDpns, dpnId);
301         if (currDpnInterfaces == null || !elanUtils.isDpnPresent(currDpnInterfaces.getDpId())
302                 || currDpnInterfaces.getInterfaces() == null || currDpnInterfaces.getInterfaces().isEmpty()) {
303             return emptyList();
304         }
305         List<Bucket> listBucketInfo = new ArrayList<>();
306         for (String interfaceName : currDpnInterfaces.getInterfaces()) {
307             if (interfaceManager.isExternalInterface(interfaceName)) {
308                 List<Action> listActionInfo = elanItmUtils.getExternalPortItmEgressAction(interfaceName);
309                 if (!listActionInfo.isEmpty()) {
310                     listBucketInfo.add(MDSALUtil.buildBucket(listActionInfo, MDSALUtil.GROUP_WEIGHT, bucketId,
311                             MDSALUtil.WATCH_PORT, MDSALUtil.WATCH_GROUP));
312                     bucketId++;
313                 }
314             }
315         }
316         return listBucketInfo;
317     }
318
319     @Nonnull
320     public List<Bucket> getRemoteBCGroupBuckets(ElanInstance elanInfo, DpnInterfaces dpnInterfaces, BigInteger dpnId,
321                                                 int bucketId, long elanTag) {
322         List<Bucket> listBucketInfo = new ArrayList<>();
323         ElanDpnInterfacesList elanDpns = elanUtils.getElanDpnInterfacesList(elanInfo.getElanInstanceName());
324
325         if (isVxlanNetworkOrVxlanSegment(elanInfo)) {
326             listBucketInfo.addAll(getRemoteBCGroupTunnelBuckets(elanDpns, dpnId, bucketId,
327                     elanUtils.isOpenstackVniSemanticsEnforced()
328                             ? ElanUtils.getVxlanSegmentationId(elanInfo) : elanTag));
329         }
330         listBucketInfo.addAll(getRemoteBCGroupExternalPortBuckets(elanDpns, dpnInterfaces, dpnId,
331                 getNextAvailableBucketId(listBucketInfo.size())));
332         listBucketInfo.addAll(getRemoteBCGroupBucketsOfElanExternalTeps(elanInfo, dpnId,
333                 getNextAvailableBucketId(listBucketInfo.size())));
334         return listBucketInfo;
335     }
336
337     public List<Bucket> getRemoteBCGroupBucketsOfElanL2GwDevices(ElanInstance elanInfo, BigInteger dpnId,
338             int bucketId) {
339         List<Bucket> listBucketInfo = new ArrayList<>();
340         ConcurrentMap<String, L2GatewayDevice> map = ElanL2GwCacheUtils
341                 .getInvolvedL2GwDevices(elanInfo.getElanInstanceName());
342         for (L2GatewayDevice device : map.values()) {
343             String interfaceName = elanItmUtils.getExternalTunnelInterfaceName(String.valueOf(dpnId),
344                     device.getHwvtepNodeId());
345             if (interfaceName == null) {
346                 continue;
347             }
348             List<Action> listActionInfo = elanItmUtils.buildTunnelItmEgressActions(interfaceName,
349                     ElanUtils.getVxlanSegmentationId(elanInfo), true);
350             listBucketInfo.add(MDSALUtil.buildBucket(listActionInfo, MDSALUtil.GROUP_WEIGHT, bucketId,
351                     MDSALUtil.WATCH_PORT, MDSALUtil.WATCH_GROUP));
352             bucketId++;
353         }
354         return listBucketInfo;
355     }
356
357     public List<Bucket> getRemoteBCGroupBucketsOfElanExternalTeps(ElanInstance elanInfo, BigInteger dpnId,
358             int bucketId) {
359         ElanInstance operElanInstance = null;
360         try {
361             operElanInstance = new SingleTransactionDataBroker(broker).syncReadOptional(
362                 LogicalDatastoreType.OPERATIONAL,
363                 InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, elanInfo.key())
364                     .build()).orNull();
365         } catch (ReadFailedException e) {
366             LOG.error("Failed to read elan instance operational path {}", elanInfo, e);
367             return emptyList();
368         }
369         if (operElanInstance == null) {
370             return emptyList();
371         }
372         List<ExternalTeps> teps = operElanInstance.getExternalTeps();
373         if (teps == null || teps.isEmpty()) {
374             return emptyList();
375         }
376         List<Bucket> listBucketInfo = new ArrayList<>();
377         for (ExternalTeps tep : teps) {
378             String externalTep = tep.getNodeid() != null ? tep.getNodeid() : tep.getTepIp().toString();
379             String interfaceName = elanItmUtils.getExternalTunnelInterfaceName(String.valueOf(dpnId),
380                     externalTep);
381             if (interfaceName == null) {
382                 LOG.error("Could not get interface name to ext tunnel {} {}", dpnId, tep.getTepIp());
383                 continue;
384             }
385             List<Action> listActionInfo = elanItmUtils.buildTunnelItmEgressActions(interfaceName,
386                     ElanUtils.getVxlanSegmentationId(elanInfo), false);
387             listBucketInfo.add(MDSALUtil.buildBucket(listActionInfo, MDSALUtil.GROUP_WEIGHT, bucketId,
388                     MDSALUtil.WATCH_PORT, MDSALUtil.WATCH_GROUP));
389             bucketId++;
390         }
391         return listBucketInfo;
392     }
393
394     private int getNextAvailableBucketId(int bucketSize) {
395         return bucketSize + 1;
396     }
397
398     @SuppressWarnings("checkstyle:IllegalCatch")
399     private List<Bucket> getRemoteBCGroupTunnelBuckets(ElanDpnInterfacesList elanDpns, BigInteger dpnId, int bucketId,
400             long elanTagOrVni) {
401         List<Bucket> listBucketInfo = new ArrayList<>();
402         if (elanDpns != null) {
403             for (DpnInterfaces dpnInterface : elanDpns.getDpnInterfaces()) {
404                 if (elanUtils.isDpnPresent(dpnInterface.getDpId()) && !Objects.equals(dpnInterface.getDpId(), dpnId)
405                         && dpnInterface.getInterfaces() != null && !dpnInterface.getInterfaces().isEmpty()) {
406                     try {
407                         List<Action> listActionInfo = elanItmUtils.getInternalTunnelItmEgressAction(dpnId,
408                                 dpnInterface.getDpId(), elanTagOrVni);
409                         if (listActionInfo.isEmpty()) {
410                             continue;
411                         }
412                         listBucketInfo.add(MDSALUtil.buildBucket(listActionInfo, MDSALUtil.GROUP_WEIGHT, bucketId,
413                                 MDSALUtil.WATCH_PORT, MDSALUtil.WATCH_GROUP));
414                         bucketId++;
415                     } catch (Exception ex) {
416                         LOG.error("Logical Group Interface not found between source Dpn - {}, destination Dpn - {} ",
417                                 dpnId, dpnInterface.getDpId(), ex);
418                     }
419                 }
420             }
421         }
422         return listBucketInfo;
423     }
424
425     /**
426      * Update remote mcast mac.
427      *
428      * @param elanName
429      *            the elan name
430      * @param device
431      *            the device
432      * @param dpnsTepIps
433      *            the dpns tep ips
434      * @param l2GwDevicesTepIps
435      *            the l2 gw devices tep ips
436      * @return the write transaction
437      */
438     private void preapareRemoteMcastMacEntry(String elanName,
439                                              L2GatewayDevice device, List<IpAddress> dpnsTepIps,
440                                              List<IpAddress> l2GwDevicesTepIps) {
441         NodeId nodeId = new NodeId(device.getHwvtepNodeId());
442
443         ArrayList<IpAddress> remoteTepIps = new ArrayList<>(l2GwDevicesTepIps);
444         remoteTepIps.remove(device.getTunnelIp());
445         remoteTepIps.addAll(dpnsTepIps);
446         IpAddress dhcpDesignatedSwitchTepIp = getTepIpOfDesignatedSwitchForExternalTunnel(device, elanName);
447         if (dpnsTepIps.isEmpty()) {
448             // If no dpns in elan, configure dhcp designated switch Tep Ip as a
449             // physical locator in l2 gw device
450             if (dhcpDesignatedSwitchTepIp != null) {
451                 remoteTepIps.add(dhcpDesignatedSwitchTepIp);
452
453                 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
454                         .createHwvtepPhysicalLocatorAugmentation(dhcpDesignatedSwitchTepIp);
455                 InstanceIdentifier<TerminationPoint> iid =
456                         HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug);
457                 TerminationPoint terminationPoint = new TerminationPointBuilder()
458                                 .withKey(HwvtepSouthboundUtils.getTerminationPointKey(phyLocatorAug))
459                                 .addAugmentation(HwvtepPhysicalLocatorAugmentation.class, phyLocatorAug).build();
460                 ResourceBatchingManager.getInstance().put(ResourceBatchingManager.ShardResource.CONFIG_TOPOLOGY,
461                         iid, terminationPoint);
462                 LOG.info("Adding PhysicalLocator for node: {} with Dhcp designated switch Tep Ip {} "
463                         + "as physical locator, elan {}", device.getHwvtepNodeId(),
464                         dhcpDesignatedSwitchTepIp.stringValue(), elanName);
465             } else {
466                 LOG.warn("Dhcp designated switch Tep Ip not found for l2 gw node {} and elan {}",
467                         device.getHwvtepNodeId(), elanName);
468             }
469         }
470         if (dhcpDesignatedSwitchTepIp != null && !remoteTepIps.contains(dhcpDesignatedSwitchTepIp)) {
471             remoteTepIps.add(dhcpDesignatedSwitchTepIp);
472         }
473         String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(elanName);
474         putRemoteMcastMac(nodeId, logicalSwitchName, remoteTepIps);
475         LOG.info("Adding RemoteMcastMac for node: {} with physical locators: {}", device.getHwvtepNodeId(),
476                 remoteTepIps);
477     }
478
479     /**
480      * Put remote mcast mac in config DS.
481      *
482      * @param nodeId
483      *            the node id
484      * @param logicalSwitchName
485      *            the logical switch name
486      * @param tepIps
487      *            the tep ips
488      */
489     private static void putRemoteMcastMac(NodeId nodeId, String logicalSwitchName,
490             ArrayList<IpAddress> tepIps) {
491         List<LocatorSet> locators = new ArrayList<>();
492         for (IpAddress tepIp : tepIps) {
493             HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
494                     .createHwvtepPhysicalLocatorAugmentation(tepIp);
495             HwvtepPhysicalLocatorRef phyLocRef = new HwvtepPhysicalLocatorRef(
496                     HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug));
497             locators.add(new LocatorSetBuilder().setLocatorRef(phyLocRef).build());
498         }
499
500         HwvtepLogicalSwitchRef lsRef = new HwvtepLogicalSwitchRef(HwvtepSouthboundUtils
501                 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName)));
502         RemoteMcastMacs remoteMcastMac = new RemoteMcastMacsBuilder()
503                 .setMacEntryKey(new MacAddress(ElanConstants.UNKNOWN_DMAC)).setLogicalSwitchRef(lsRef)
504                 .setLocatorSet(locators).build();
505         InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
506                 remoteMcastMac.key());
507         ResourceBatchingManager.getInstance().put(ResourceBatchingManager.ShardResource.CONFIG_TOPOLOGY,
508                 iid, remoteMcastMac);
509
510     }
511
512     /**
513      * Gets all the tep ips of dpns.
514      *
515      * @param l2GwDevice
516      *            the device
517      * @param dpns
518      *            the dpns
519      * @return the all tep ips of dpns and devices
520      */
521     private List<IpAddress> getAllTepIpsOfDpns(L2GatewayDevice l2GwDevice, List<DpnInterfaces> dpns) {
522         List<IpAddress> tepIps = new ArrayList<>();
523         for (DpnInterfaces dpn : dpns) {
524             IpAddress internalTunnelIp = elanItmUtils.getSourceDpnTepIp(dpn.getDpId(),
525                     new NodeId(l2GwDevice.getHwvtepNodeId()));
526             if (internalTunnelIp != null) {
527                 tepIps.add(internalTunnelIp);
528             }
529         }
530         return tepIps;
531     }
532
533     /**
534      * Gets the all tep ips of l2 gw devices.
535      *
536      * @param devices
537      *            the devices
538      * @return the all tep ips of l2 gw devices
539      */
540     private static List<IpAddress> getAllTepIpsOfL2GwDevices(ConcurrentMap<String, L2GatewayDevice> devices) {
541         List<IpAddress> tepIps = new ArrayList<>();
542         for (L2GatewayDevice otherDevice : devices.values()) {
543             // There is no need to add the same tep ip to the list.
544             if (!tepIps.contains(otherDevice.getTunnelIp())) {
545                 tepIps.add(otherDevice.getTunnelIp());
546             }
547         }
548         return tepIps;
549     }
550
551     /**
552      * Handle mcast for elan l2 gw device delete.
553      *
554      * @param elanName
555      *            the elan instance name
556      * @param l2GatewayDevice
557      *            the l2 gateway device
558      * @return the listenable future
559      */
560     public List<ListenableFuture<Void>> handleMcastForElanL2GwDeviceDelete(String elanName,
561                                                                            L2GatewayDevice l2GatewayDevice) {
562         ListenableFuture<Void> deleteTepFuture = txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
563             tx.delete(LogicalDatastoreType.CONFIGURATION,
564                     buildExternalTepPath(elanName, l2GatewayDevice.getTunnelIp()));
565         });
566         ListenableFuture<Void> updateMcastMacsFuture = updateMcastMacsForAllElanDevices(
567                 elanName, l2GatewayDevice, false/* updateThisDevice */);
568         ListenableFuture<Void> deleteRemoteMcastMacFuture = deleteRemoteMcastMac(
569                 new NodeId(l2GatewayDevice.getHwvtepNodeId()), elanName);
570         return Arrays.asList(updateMcastMacsFuture, deleteRemoteMcastMacFuture, deleteTepFuture);
571     }
572
573     /**
574      * Delete remote mcast mac from Hwvtep node.
575      *
576      * @param nodeId
577      *            the node id
578      * @param logicalSwitchName
579      *            the logical switch name
580      * @return the listenable future
581      */
582     private ListenableFuture<Void> deleteRemoteMcastMac(NodeId nodeId, String logicalSwitchName) {
583         InstanceIdentifier<LogicalSwitches> logicalSwitch = HwvtepSouthboundUtils
584                 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
585         RemoteMcastMacsKey remoteMcastMacsKey = new RemoteMcastMacsKey(new HwvtepLogicalSwitchRef(logicalSwitch),
586                 new MacAddress(ElanConstants.UNKNOWN_DMAC));
587
588         LOG.info("Deleting RemoteMcastMacs entry on node: {} for logical switch: {}", nodeId.getValue(),
589                 logicalSwitchName);
590         return HwvtepUtils.deleteRemoteMcastMac(broker, nodeId, remoteMcastMacsKey);
591     }
592
593     /**
594      * Gets the tep ip of designated switch for external tunnel.
595      *
596      * @param l2GwDevice
597      *            the l2 gw device
598      * @param elanInstanceName
599      *            the elan instance name
600      * @return the tep ip of designated switch for external tunnel
601      */
602     public IpAddress getTepIpOfDesignatedSwitchForExternalTunnel(L2GatewayDevice l2GwDevice,
603             String elanInstanceName) {
604         IpAddress tepIp = null;
605         if (l2GwDevice.getTunnelIp() == null) {
606             LOG.warn("Tunnel IP not found for {}", l2GwDevice.getDeviceName());
607             return tepIp;
608         }
609         DesignatedSwitchForTunnel desgSwitch = getDesignatedSwitchForExternalTunnel(l2GwDevice.getTunnelIp(),
610                 elanInstanceName);
611         if (desgSwitch != null) {
612             tepIp = elanItmUtils.getSourceDpnTepIp(BigInteger.valueOf(desgSwitch.getDpId()),
613                     new NodeId(l2GwDevice.getHwvtepNodeId()));
614         }
615         return tepIp;
616     }
617
618     /**
619      * Gets the designated switch for external tunnel.
620      *
621      * @param tunnelIp
622      *            the tunnel ip
623      * @param elanInstanceName
624      *            the elan instance name
625      * @return the designated switch for external tunnel
626      */
627     public DesignatedSwitchForTunnel getDesignatedSwitchForExternalTunnel(IpAddress tunnelIp,
628             String elanInstanceName) {
629         InstanceIdentifier<DesignatedSwitchForTunnel> instanceIdentifier = InstanceIdentifier
630                 .builder(DesignatedSwitchesForExternalTunnels.class)
631                 .child(DesignatedSwitchForTunnel.class, new DesignatedSwitchForTunnelKey(elanInstanceName, tunnelIp))
632                 .build();
633         return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, instanceIdentifier).orNull();
634     }
635
636 }