2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.l2gw.utils;
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;
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;
82 * The utility class to handle ELAN L2 Gateway related to multicast.
85 public class ElanL2GatewayMulticastUtils {
87 /** The Constant LOG. */
88 private static final Logger LOG = LoggerFactory.getLogger(ElanL2GatewayMulticastUtils.class);
91 private final DataBroker broker;
92 private final ManagedNewTransactionRunner txRunner;
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;
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;
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
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 */);
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();
131 protected ExternalTeps buildExternalTeps(L2GatewayDevice device) {
132 return new ExternalTepsBuilder().setTepIp(device.getTunnelIp()).setNodeid(device.getHwvtepNodeId()).build();
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
141 * the elan to be updated
142 * @return the listenable future
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);
153 public void scheduleMcastMacUpdateJob(String elanName, L2GatewayDevice device) {
154 HwvtepDeviceMcastMacUpdateJob job = new HwvtepDeviceMcastMacUpdateJob(this, elanName,device);
155 jobCoordinator.enqueueJob(job.getJobKey(), job);
159 * Update remote mcast mac on elan l2 gw device.
165 * @return the listenable future
167 public ListenableFuture<Void> updateRemoteMcastMacOnElanL2GwDevice(String elanName, L2GatewayDevice device) {
168 return txRunner.callWithNewWriteOnlyTransactionAndSubmit(
169 tx -> prepareRemoteMcastMacUpdateOnDevice(elanName, device));
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);
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
191 * @param updateThisDevice
192 * the update this device
193 * @return the listenable future
195 private ListenableFuture<Void> updateMcastMacsForAllElanDevices(String elanName, L2GatewayDevice device,
196 boolean updateThisDevice) {
198 SettableFuture<Void> ft = SettableFuture.create();
201 List<DpnInterfaces> dpns = elanUtils.getElanDPNByName(elanName);
203 ConcurrentMap<String, L2GatewayDevice> devices = ElanL2GwCacheUtils
204 .getInvolvedL2GwDevices(elanName);
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);
213 return txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
214 if (updateThisDevice) {
215 preapareRemoteMcastMacEntry(elanName, device, dpnsTepIps, l2GwDevicesTepIps);
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);
229 public void updateRemoteBroadcastGroupForAllElanDpns(ElanInstance elanInfo) {
230 List<DpnInterfaces> dpns = elanUtils.getInvolvedDpnsInElan(elanInfo.getElanInstanceName());
231 for (DpnInterfaces dpn : dpns) {
232 setupElanBroadcastGroups(elanInfo, dpn.getDpId());
236 public void setupElanBroadcastGroups(ElanInstance elanInfo, BigInteger dpnId) {
237 setupElanBroadcastGroups(elanInfo, null, dpnId);
240 public void setupElanBroadcastGroups(ElanInstance elanInfo, DpnInterfaces dpnInterfaces, BigInteger dpnId) {
241 setupStandardElanBroadcastGroups(elanInfo, dpnInterfaces, dpnId);
242 setupLeavesEtreeBroadcastGroups(elanInfo, dpnInterfaces, dpnId);
245 public void setupStandardElanBroadcastGroups(ElanInstance elanInfo, DpnInterfaces dpnInterfaces, BigInteger dpnId) {
246 List<Bucket> listBucket = new ArrayList<>();
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));
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);
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<>();
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));
276 List<Bucket> listBucketInfoRemote = getRemoteBCGroupBuckets(elanInfo, dpnInterfaces, dpnId, bucketId,
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);
287 private DpnInterfaces getDpnInterfaces(ElanDpnInterfacesList elanDpns, BigInteger dpnId) {
288 if (elanDpns != null) {
289 for (DpnInterfaces dpnInterface : elanDpns.getDpnInterfaces()) {
290 if (dpnInterface.getDpId().equals(dpnId)) {
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()) {
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));
316 return listBucketInfo;
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());
325 if (isVxlanNetworkOrVxlanSegment(elanInfo)) {
326 listBucketInfo.addAll(getRemoteBCGroupTunnelBuckets(elanDpns, dpnId, bucketId,
327 elanUtils.isOpenstackVniSemanticsEnforced()
328 ? ElanUtils.getVxlanSegmentationId(elanInfo) : elanTag));
330 listBucketInfo.addAll(getRemoteBCGroupExternalPortBuckets(elanDpns, dpnInterfaces, dpnId,
331 getNextAvailableBucketId(listBucketInfo.size())));
332 listBucketInfo.addAll(getRemoteBCGroupBucketsOfElanExternalTeps(elanInfo, dpnId,
333 getNextAvailableBucketId(listBucketInfo.size())));
334 return listBucketInfo;
337 public List<Bucket> getRemoteBCGroupBucketsOfElanL2GwDevices(ElanInstance elanInfo, BigInteger dpnId,
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) {
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));
354 return listBucketInfo;
357 public List<Bucket> getRemoteBCGroupBucketsOfElanExternalTeps(ElanInstance elanInfo, BigInteger dpnId,
359 ElanInstance operElanInstance = null;
361 operElanInstance = new SingleTransactionDataBroker(broker).syncReadOptional(
362 LogicalDatastoreType.OPERATIONAL,
363 InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, elanInfo.key())
365 } catch (ReadFailedException e) {
366 LOG.error("Failed to read elan instance operational path {}", elanInfo, e);
369 if (operElanInstance == null) {
372 List<ExternalTeps> teps = operElanInstance.getExternalTeps();
373 if (teps == null || teps.isEmpty()) {
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),
381 if (interfaceName == null) {
382 LOG.error("Could not get interface name to ext tunnel {} {}", dpnId, tep.getTepIp());
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));
391 return listBucketInfo;
394 private int getNextAvailableBucketId(int bucketSize) {
395 return bucketSize + 1;
398 @SuppressWarnings("checkstyle:IllegalCatch")
399 private List<Bucket> getRemoteBCGroupTunnelBuckets(ElanDpnInterfacesList elanDpns, BigInteger dpnId, int bucketId,
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()) {
407 List<Action> listActionInfo = elanItmUtils.getInternalTunnelItmEgressAction(dpnId,
408 dpnInterface.getDpId(), elanTagOrVni);
409 if (listActionInfo.isEmpty()) {
412 listBucketInfo.add(MDSALUtil.buildBucket(listActionInfo, MDSALUtil.GROUP_WEIGHT, bucketId,
413 MDSALUtil.WATCH_PORT, MDSALUtil.WATCH_GROUP));
415 } catch (Exception ex) {
416 LOG.error("Logical Group Interface not found between source Dpn - {}, destination Dpn - {} ",
417 dpnId, dpnInterface.getDpId(), ex);
422 return listBucketInfo;
426 * Update remote mcast mac.
434 * @param l2GwDevicesTepIps
435 * the l2 gw devices tep ips
436 * @return the write transaction
438 private void preapareRemoteMcastMacEntry(String elanName,
439 L2GatewayDevice device, List<IpAddress> dpnsTepIps,
440 List<IpAddress> l2GwDevicesTepIps) {
441 NodeId nodeId = new NodeId(device.getHwvtepNodeId());
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);
453 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
454 .createHwvtepPhysicalLocatorAugmentation(dhcpDesignatedSwitchTepIp.stringValue());
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);
466 LOG.warn("Dhcp designated switch Tep Ip not found for l2 gw node {} and elan {}",
467 device.getHwvtepNodeId(), elanName);
470 if (dhcpDesignatedSwitchTepIp != null && !remoteTepIps.contains(dhcpDesignatedSwitchTepIp)) {
471 remoteTepIps.add(dhcpDesignatedSwitchTepIp);
473 String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(elanName);
474 putRemoteMcastMac(nodeId, logicalSwitchName, remoteTepIps);
475 LOG.info("Adding RemoteMcastMac for node: {} with physical locators: {}", device.getHwvtepNodeId(),
480 * Put remote mcast mac in config DS.
484 * @param logicalSwitchName
485 * the logical switch name
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.stringValue());
495 HwvtepPhysicalLocatorRef phyLocRef = new HwvtepPhysicalLocatorRef(
496 HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug));
497 locators.add(new LocatorSetBuilder().setLocatorRef(phyLocRef).build());
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);
513 * Gets all the tep ips of dpns.
519 * @return the all tep ips of dpns and devices
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);
534 * Gets the all tep ips of l2 gw devices.
538 * @return the all tep ips of l2 gw devices
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());
552 * Handle mcast for elan l2 gw device delete.
555 * the elan instance name
556 * @param l2GatewayDevice
557 * the l2 gateway device
558 * @return the listenable future
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()));
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);
574 * Delete remote mcast mac from Hwvtep node.
578 * @param logicalSwitchName
579 * the logical switch name
580 * @return the listenable future
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));
588 LOG.info("Deleting RemoteMcastMacs entry on node: {} for logical switch: {}", nodeId.getValue(),
590 return HwvtepUtils.deleteRemoteMcastMac(broker, nodeId, remoteMcastMacsKey);
594 * Gets the tep ip of designated switch for external tunnel.
598 * @param elanInstanceName
599 * the elan instance name
600 * @return the tep ip of designated switch for external tunnel
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());
609 DesignatedSwitchForTunnel desgSwitch = getDesignatedSwitchForExternalTunnel(l2GwDevice.getTunnelIp(),
611 if (desgSwitch != null) {
612 tepIp = elanItmUtils.getSourceDpnTepIp(BigInteger.valueOf(desgSwitch.getDpId()),
613 new NodeId(l2GwDevice.getHwvtepNodeId()));
619 * Gets the designated switch for external tunnel.
623 * @param elanInstanceName
624 * the elan instance name
625 * @return the designated switch for external tunnel
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))
633 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, instanceIdentifier).orNull();