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 org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.Lists;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import java.math.BigInteger;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Locale;
25 import java.util.Objects;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentMap;
29 import java.util.concurrent.ExecutionException;
30 import java.util.concurrent.Future;
31 import java.util.concurrent.ScheduledFuture;
32 import java.util.concurrent.TimeUnit;
33 import java.util.function.Function;
34 import java.util.stream.Collectors;
35 import javax.annotation.PreDestroy;
36 import javax.inject.Inject;
37 import javax.inject.Singleton;
38 import org.apache.commons.lang3.tuple.ImmutablePair;
39 import org.apache.commons.lang3.tuple.Pair;
40 import org.eclipse.jdt.annotation.NonNull;
41 import org.eclipse.jdt.annotation.Nullable;
42 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
43 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
44 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
45 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
46 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
47 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
48 import org.opendaylight.genius.mdsalutil.MDSALUtil;
49 import org.opendaylight.genius.utils.SystemPropertyReader;
50 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
51 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
52 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
53 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
54 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
55 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
56 import org.opendaylight.netvirt.elan.cache.ElanInstanceDpnsCache;
57 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
58 import org.opendaylight.netvirt.elan.l2gw.jobs.DeleteL2GwDeviceMacsFromElanJob;
59 import org.opendaylight.netvirt.elan.l2gw.jobs.DeleteLogicalSwitchJob;
60 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
61 import org.opendaylight.netvirt.elan.utils.ElanConstants;
62 import org.opendaylight.netvirt.elan.utils.ElanDmacUtils;
63 import org.opendaylight.netvirt.elan.utils.ElanItmUtils;
64 import org.opendaylight.netvirt.elan.utils.ElanUtils;
65 import org.opendaylight.netvirt.elan.utils.Scheduler;
66 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
67 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
68 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
69 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
70 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
71 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
72 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
73 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
74 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetDpidFromInterfaceInputBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetDpidFromInterfaceOutput;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.TransportZones;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.DeviceVteps;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.AddL2GwDeviceInputBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.AddL2GwDeviceOutput;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.forwarding.tables.MacTable;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.elan.instance.ExternalTeps;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepMacTableGenericAttributes;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSet;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
105 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
106 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
107 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
108 import org.opendaylight.yangtools.yang.common.RpcResult;
109 import org.slf4j.Logger;
110 import org.slf4j.LoggerFactory;
113 * It gathers a set of utility methods that handle ELAN configuration in
114 * external Devices (where external means "not-CSS". As of now: TORs).
116 * <p>It makes use of HwvtepUtils class located under ovsdb/hwvtepsouthbound
117 * project for low-level mdsal operations
122 public class ElanL2GatewayUtils {
123 private static final Logger LOG = LoggerFactory.getLogger(ElanL2GatewayUtils.class);
124 private static final int DEFAULT_LOGICAL_SWITCH_DELETE_DELAY_SECS = 20;
126 private final DataBroker broker;
127 private final ManagedNewTransactionRunner txRunner;
128 private final ElanDmacUtils elanDmacUtils;
129 private final ElanItmUtils elanItmUtils;
130 private final ElanClusterUtils elanClusterUtils;
131 private final OdlInterfaceRpcService interfaceManagerRpcService;
132 private final JobCoordinator jobCoordinator;
133 private final ElanUtils elanUtils;
134 private final ElanInstanceCache elanInstanceCache;
135 private final ElanInstanceDpnsCache elanInstanceDpnsCache;
137 private final ConcurrentMap<Pair<NodeId, String>, ScheduledFuture> logicalSwitchDeletedTasks
138 = new ConcurrentHashMap<>();
139 private final ConcurrentMap<Pair<NodeId, String>, DeleteLogicalSwitchJob> deleteJobs = new ConcurrentHashMap<>();
140 private final Scheduler scheduler;
141 private final ElanConfig elanConfig;
144 public ElanL2GatewayUtils(DataBroker broker, ElanDmacUtils elanDmacUtils, ElanItmUtils elanItmUtils,
145 ElanClusterUtils elanClusterUtils, OdlInterfaceRpcService interfaceManagerRpcService,
146 JobCoordinator jobCoordinator, ElanUtils elanUtils,
147 Scheduler scheduler, ElanConfig elanConfig, ElanInstanceCache elanInstanceCache,
148 ElanInstanceDpnsCache elanInstanceDpnsCache) {
149 this.broker = broker;
150 this.txRunner = new ManagedNewTransactionRunnerImpl(broker);
151 this.elanDmacUtils = elanDmacUtils;
152 this.elanItmUtils = elanItmUtils;
153 this.elanClusterUtils = elanClusterUtils;
154 this.interfaceManagerRpcService = interfaceManagerRpcService;
155 this.jobCoordinator = jobCoordinator;
156 this.elanUtils = elanUtils;
157 this.scheduler = scheduler;
158 this.elanConfig = elanConfig;
159 this.elanInstanceCache = elanInstanceCache;
160 this.elanInstanceDpnsCache = elanInstanceDpnsCache;
164 public void close() {
167 public long getLogicalSwitchDeleteDelaySecs() {
168 return elanConfig.getL2gwLogicalSwitchDelaySecs() != null
169 ? elanConfig.getL2gwLogicalSwitchDelaySecs() : DEFAULT_LOGICAL_SWITCH_DELETE_DELAY_SECS;
173 * gets the macs addresses for elan interfaces.
175 * @param lstElanInterfaceNames
176 * the lst elan interface names
179 public List<PhysAddress> getElanDpnMacsFromInterfaces(Set<String> lstElanInterfaceNames) {
180 List<PhysAddress> result = new ArrayList<>();
181 for (String interfaceName : lstElanInterfaceNames) {
182 ElanInterfaceMac elanInterfaceMac = ElanUtils.getElanInterfaceMacByInterfaceName(broker, interfaceName);
183 if (elanInterfaceMac != null && elanInterfaceMac.getMacEntry() != null) {
184 for (MacEntry macEntry : elanInterfaceMac.getMacEntry()) {
185 result.add(macEntry.getMacAddress());
193 * Check if phy locator already exists in remote mcast entry.
197 * @param remoteMcastMac
198 * the remote mcast mac
199 * @param expectedPhyLocatorIp
200 * the expected phy locator ip
201 * @return true, if successful
203 public static boolean checkIfPhyLocatorAlreadyExistsInRemoteMcastEntry(NodeId nodeId,
204 RemoteMcastMacs remoteMcastMac, IpAddress expectedPhyLocatorIp) {
205 if (remoteMcastMac != null) {
206 HwvtepPhysicalLocatorAugmentation expectedPhyLocatorAug = HwvtepSouthboundUtils
207 .createHwvtepPhysicalLocatorAugmentation(expectedPhyLocatorIp);
208 HwvtepPhysicalLocatorRef expectedPhyLocRef = new HwvtepPhysicalLocatorRef(
209 HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId, expectedPhyLocatorAug));
210 if (remoteMcastMac.getLocatorSet() != null) {
211 for (LocatorSet locatorSet : remoteMcastMac.getLocatorSet()) {
212 if (Objects.equals(locatorSet.getLocatorRef(), expectedPhyLocRef)) {
213 LOG.trace("matched phyLocRef: {}", expectedPhyLocRef);
223 * Gets the remote mcast mac.
227 * @param logicalSwitchName
228 * the logical switch name
229 * @param datastoreType
231 * @return the remote mcast mac
233 public RemoteMcastMacs readRemoteMcastMac(NodeId nodeId, String logicalSwitchName,
234 LogicalDatastoreType datastoreType) {
235 InstanceIdentifier<LogicalSwitches> logicalSwitch = HwvtepSouthboundUtils
236 .createLogicalSwitchesInstanceIdentifier(nodeId, new HwvtepNodeName(logicalSwitchName));
237 RemoteMcastMacsKey remoteMcastMacsKey = new RemoteMcastMacsKey(new HwvtepLogicalSwitchRef(logicalSwitch),
238 new MacAddress(ElanConstants.UNKNOWN_DMAC));
239 return HwvtepUtils.getRemoteMcastMac(broker, datastoreType, nodeId, remoteMcastMacsKey);
243 * Removes the given MAC Addresses from all the External Devices belonging
244 * to the specified ELAN.
246 * @param elanInstance
248 * @param macAddresses
251 public void removeMacsFromElanExternalDevices(ElanInstance elanInstance, List<PhysAddress> macAddresses) {
252 String elanName = elanInstance.getElanInstanceName();
253 for (L2GatewayDevice l2GatewayDevice : ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName)) {
254 removeRemoteUcastMacsFromExternalDevice(l2GatewayDevice.getHwvtepNodeId(), elanName, macAddresses);
259 * Removes the given MAC Addresses from the specified External Device.
261 * @param deviceNodeId
263 * @param macAddresses
265 * @return the listenable future
267 private ListenableFuture<Void> removeRemoteUcastMacsFromExternalDevice(String deviceNodeId,
268 String logicalSwitchName, List<PhysAddress> macAddresses) {
269 NodeId nodeId = new NodeId(deviceNodeId);
272 List<MacAddress> lstMac = macAddresses.stream().filter(Objects::nonNull).map(
273 physAddress -> new MacAddress(physAddress.getValue())).collect(Collectors.toList());
274 return HwvtepUtils.deleteRemoteUcastMacs(broker, nodeId, logicalSwitchName, lstMac);
278 * Install external device local macs in dpn.
282 * @param l2gwDeviceNodeId
283 * the l2gw device node id
286 * @param interfaceName
289 public void installL2gwDeviceMacsInDpn(BigInteger dpnId, NodeId l2gwDeviceNodeId, ElanInstance elan,
290 String interfaceName) {
291 L2GatewayDevice l2gwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elan.getElanInstanceName(),
292 l2gwDeviceNodeId.getValue());
293 if (l2gwDevice == null) {
294 LOG.debug("L2 gw device not found in elan cache for device name {}", l2gwDeviceNodeId.getValue());
298 installDmacFlowsOnDpn(dpnId, l2gwDevice, elan, interfaceName);
302 * Install dmac flows on dpn.
310 * @param interfaceName
313 public List<ListenableFuture<Void>> installDmacFlowsOnDpn(BigInteger dpnId, L2GatewayDevice l2gwDevice,
314 ElanInstance elan, String interfaceName) {
315 String elanName = elan.getElanInstanceName();
316 List<ListenableFuture<Void>> fts = new ArrayList<>();
317 Collection<LocalUcastMacs> l2gwDeviceLocalMacs = l2gwDevice.getUcastLocalMacs();
318 if (!l2gwDeviceLocalMacs.isEmpty()) {
319 for (LocalUcastMacs localUcastMac : l2gwDeviceLocalMacs) {
320 fts.addAll(elanDmacUtils.installDmacFlowsToExternalRemoteMacInBatch(dpnId, l2gwDevice.getHwvtepNodeId(),
321 elan.getElanTag(), ElanUtils.getVxlanSegmentationId(elan),
322 localUcastMac.getMacEntryKey().getValue(), elanName, interfaceName));
324 LOG.debug("Installing L2gw device [{}] local macs [size: {}] in dpn [{}] for elan [{}]",
325 l2gwDevice.getHwvtepNodeId(), l2gwDeviceLocalMacs.size(), dpnId, elanName);
331 * Install elan l2gw devices local macs in dpn.
337 * @param interfaceName
340 public void installElanL2gwDevicesLocalMacsInDpn(BigInteger dpnId, ElanInstance elan, String interfaceName) {
341 for (L2GatewayDevice l2gwDevice : ElanL2GwCacheUtils.getInvolvedL2GwDevices(elan.getElanInstanceName())) {
342 installDmacFlowsOnDpn(dpnId, l2gwDevice, elan, interfaceName);
346 public void installL2GwUcastMacInElan(final ElanInstance elan, final L2GatewayDevice extL2GwDevice,
347 final String macToBeAdded, final LocalUcastMacs localUcastMacs, @Nullable String interfaceName) {
348 final String extDeviceNodeId = extL2GwDevice.getHwvtepNodeId();
349 final String elanInstanceName = elan.getElanInstanceName();
350 final Collection<DpnInterfaces> elanDpns = getElanDpns(elanInstanceName);
351 Collection<L2GatewayDevice> elanL2GwDevices = ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanInstanceName);
353 // Retrieve all participating DPNs in this Elan. Populate this MAC in
355 // Looping through all DPNs in order to add/remove mac flows in their
357 if (elanDpns.size() > 0 || elanL2GwDevices.size() > 0) {
358 String jobKey = elanInstanceName + ":" + macToBeAdded;
359 IpAddress extL2GwDeviceTepIp = extL2GwDevice.getTunnelIp();
360 List<PhysAddress> macList = Lists.newArrayList(new PhysAddress(macToBeAdded));
362 elanClusterUtils.runOnlyInOwnerNode(jobKey, "install l2gw macs in dmac table", () -> {
363 if (doesLocalUcastMacExistsInCache(extL2GwDevice, localUcastMacs)) {
364 List<ListenableFuture<Void>> futures = new ArrayList<>();
365 for (DpnInterfaces elanDpn : elanDpns) {
366 futures.addAll(elanDmacUtils.installDmacFlowsToExternalRemoteMacInBatch(elanDpn.getDpId(),
367 extDeviceNodeId, elan.getElanTag(), ElanUtils.getVxlanSegmentationId(elan),
368 macToBeAdded, elanInstanceName, interfaceName));
370 for (L2GatewayDevice otherDevice : elanL2GwDevices) {
371 if (!otherDevice.getHwvtepNodeId().equals(extDeviceNodeId)
372 && !areMLAGDevices(extL2GwDevice, otherDevice)) {
373 final String hwvtepId = otherDevice.getHwvtepNodeId();
374 final String logicalSwitchName = elanInstanceName;
375 futures.add(HwvtepUtils.installUcastMacs(
376 broker, hwvtepId, macList, logicalSwitchName, extL2GwDeviceTepIp));
381 LOG.trace("Skipping install of dmac flows for mac {} as it is not found in cache",
384 return Collections.emptyList();
390 * Does local ucast mac exists in cache.
392 * @param elanL2GwDevice
393 * the elan L2 Gw device
395 * the mac address to be verified
396 * @return true, if successful
398 private static boolean doesLocalUcastMacExistsInCache(L2GatewayDevice elanL2GwDevice, LocalUcastMacs macAddress) {
399 return elanL2GwDevice.containsUcastMac(macAddress);
403 * Uninstall l2gw macs from other l2gw devices in the elanName provided.
404 * @param elanName - Elan Name for which other l2gw devices will be scanned.
405 * @param l2GwDevice - l2gwDevice whose macs are required to be cleared from other devices.
406 * @param macAddresses - Mac address to be cleared.
408 public void unInstallL2GwUcastMacFromL2gwDevices(final String elanName,
409 final L2GatewayDevice l2GwDevice,
410 final Collection<MacAddress> macAddresses) {
411 if (macAddresses == null || macAddresses.isEmpty()) {
415 if (elanName == null) {
419 DeleteL2GwDeviceMacsFromElanJob job = new DeleteL2GwDeviceMacsFromElanJob(elanName, l2GwDevice,
421 elanClusterUtils.runOnlyInOwnerNode(job.getJobKey(), "delete remote ucast macs in l2gw devices", job);
425 * Uninstall l2gw macs from other DPNs in the elan instance provided.
426 * @param elan - Elan Instance for which other DPNs will be scanned.
427 * @param l2GwDevice - l2gwDevice whose macs are required to be cleared from other devices.
428 * @param macAddresses - Mac address to be cleared.
430 public void unInstallL2GwUcastMacFromElanDpns(final ElanInstance elan, final L2GatewayDevice l2GwDevice,
431 final Collection<MacAddress> macAddresses) {
432 if (macAddresses == null || macAddresses.isEmpty()) {
435 if (elan == null || elan.getElanInstanceName() == null) {
436 LOG.error("Could not delete l2gw ucast macs, Failed to find the elan for device {}",
437 l2GwDevice.getHwvtepNodeId());
441 final Collection<DpnInterfaces> elanDpns = getElanDpns(elan.getElanInstanceName());
443 // Retrieve all participating DPNs in this Elan. Populate this MAC in
444 // DMAC table. Looping through all DPNs in order to add/remove mac flows
445 // in their DMAC table
446 List<ListenableFuture<Void>> result = new ArrayList<>();
447 for (final MacAddress mac : macAddresses) {
448 elanClusterUtils.runOnlyInOwnerNode(elan.getElanInstanceName() + ":" + mac.getValue(),
449 "delete remote ucast macs in elan DPNs", () -> {
450 for (DpnInterfaces elanDpn : elanDpns) {
451 BigInteger dpnId = elanDpn.getDpId();
452 result.addAll(elanDmacUtils.deleteDmacFlowsToExternalMac(elan.getElanTag(), dpnId,
453 l2GwDevice.getHwvtepNodeId(),
454 IetfYangUtil.INSTANCE.canonizeMacAddress(mac).getValue()));
462 * Delete elan l2 gateway devices ucast local macs from dpn.
469 public void deleteElanL2GwDevicesUcastLocalMacsFromDpn(final String elanName, final BigInteger dpnId) {
470 Collection<L2GatewayDevice> elanL2GwDevices = ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName);
471 if (elanL2GwDevices.isEmpty()) {
472 LOG.trace("No L2 gateway devices in Elan [{}] cache.", elanName);
475 final ElanInstance elan = elanInstanceCache.get(elanName).orNull();
477 LOG.error("Could not find Elan by name: {}", elanName);
480 LOG.info("Deleting Elan [{}] L2GatewayDevices UcastLocalMacs from Dpn [{}]", elanName, dpnId);
482 final Long elanTag = elan.getElanTag();
483 for (final L2GatewayDevice l2GwDevice : elanL2GwDevices) {
484 getL2GwDeviceLocalMacsAndRunCallback(elan.getElanInstanceName(), l2GwDevice, (localMacs) -> {
485 for (MacAddress mac : localMacs) {
486 String jobKey = elanName + ":" + mac.getValue();
487 elanClusterUtils.runOnlyInOwnerNode(jobKey,
488 () -> elanDmacUtils.deleteDmacFlowsToExternalMac(elanTag, dpnId,
489 l2GwDevice.getHwvtepNodeId(), mac.getValue()));
496 public void getL2GwDeviceLocalMacsAndRunCallback(String elanName, L2GatewayDevice l2gwDevice,
497 Function<Collection<MacAddress>, Void> function) {
498 if (l2gwDevice == null) {
501 Set<MacAddress> macs = new HashSet<>();
502 Collection<LocalUcastMacs> lstUcastLocalMacs = l2gwDevice.getUcastLocalMacs();
503 if (!lstUcastLocalMacs.isEmpty()) {
504 macs.addAll(lstUcastLocalMacs.stream().filter(Objects::nonNull)
505 .map(mac -> new MacAddress(mac.getMacEntryKey().getValue().toLowerCase(Locale.ENGLISH)))
506 .collect(Collectors.toList()));
509 InstanceIdentifier<Node> nodeIid = HwvtepSouthboundUtils.createInstanceIdentifier(
510 new NodeId(l2gwDevice.getHwvtepNodeId()));
511 Futures.addCallback(broker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, nodeIid),
512 new FutureCallback<Optional<Node>>() {
514 public void onSuccess(Optional<Node> configNode) {
515 if (configNode != null && configNode.isPresent()) {
516 HwvtepGlobalAugmentation augmentation = configNode.get().augmentation(
517 HwvtepGlobalAugmentation.class);
518 if (augmentation != null && augmentation.getLocalUcastMacs() != null) {
519 macs.addAll(augmentation.getLocalUcastMacs().stream()
520 .filter(mac -> getLogicalSwitchName(mac).equals(elanName))
521 .map(HwvtepMacTableGenericAttributes::getMacEntryKey)
522 .collect(Collectors.toSet()));
524 function.apply(macs);
529 public void onFailure(Throwable throwable) {
530 LOG.error("Failed to read config topology node {}", nodeIid);
532 }, MoreExecutors.directExecutor());
535 private static String getLogicalSwitchName(LocalUcastMacs mac) {
536 return ((InstanceIdentifier<LogicalSwitches>)mac.getLogicalSwitchRef().getValue())
537 .firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
541 * Delete elan macs from L2 gateway device.<br>
542 * This includes deleting ELAN mac table entries plus external device
543 * UcastLocalMacs which are part of the same ELAN.
545 * @param hwvtepNodeId
549 * @return the listenable future
551 public ListenableFuture<Void> deleteElanMacsFromL2GatewayDevice(String hwvtepNodeId, String elanName) {
552 String logicalSwitch = getLogicalSwitchFromElan(elanName);
554 List<MacAddress> lstElanMacs = getRemoteUcastMacs(new NodeId(hwvtepNodeId), logicalSwitch,
555 LogicalDatastoreType.CONFIGURATION);
556 ListenableFuture<Void> future = HwvtepUtils.deleteRemoteUcastMacs(broker, new NodeId(hwvtepNodeId),
557 logicalSwitch, lstElanMacs);
559 Futures.addCallback(future, new FutureCallback<Void>() {
561 public void onSuccess(Void noarg) {
562 LOG.trace("Successful in batch deletion of elan [{}] macs from l2gw device [{}]", elanName,
567 public void onFailure(Throwable error) {
568 LOG.warn("Failed during batch delete of elan {} macs from l2gw device {}. "
569 + "Retrying with sequential deletes.", elanName, hwvtepNodeId, error);
570 if (lstElanMacs != null && !lstElanMacs.isEmpty()) {
571 for (MacAddress mac : lstElanMacs) {
572 HwvtepUtils.deleteRemoteUcastMac(broker, new NodeId(hwvtepNodeId), logicalSwitch, mac);
576 }, MoreExecutors.directExecutor());
578 if (LOG.isDebugEnabled()) {
579 List<String> elanMacs = lstElanMacs.stream().map(MacAddress::getValue).collect(Collectors.toList());
580 LOG.debug("Deleting elan [{}] macs from node [{}]. Deleted macs = {}", elanName, hwvtepNodeId, elanMacs);
586 * Gets the remote ucast macs from hwvtep node filtering based on logical
589 * @param hwvtepNodeId
591 * @param logicalSwitch
593 * @param datastoreType
595 * @return the remote ucast macs
597 public List<MacAddress> getRemoteUcastMacs(NodeId hwvtepNodeId, String logicalSwitch,
598 LogicalDatastoreType datastoreType) {
599 List<MacAddress> lstMacs = Collections.emptyList();
600 Node hwvtepNode = HwvtepUtils.getHwVtepNode(broker, datastoreType, hwvtepNodeId);
601 if (hwvtepNode != null) {
602 List<RemoteUcastMacs> remoteUcastMacs = hwvtepNode.augmentation(HwvtepGlobalAugmentation.class)
603 .getRemoteUcastMacs();
604 if (remoteUcastMacs != null && !remoteUcastMacs.isEmpty()) {
605 // Filtering remoteUcastMacs based on the logical switch and
606 // forming a list of MacAddress
607 lstMacs = remoteUcastMacs.stream()
608 .filter(mac -> logicalSwitch.equals(mac.getLogicalSwitchRef().getValue()
609 .firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue()))
610 .map(HwvtepMacTableGenericAttributes::getMacEntryKey).collect(Collectors.toList());
617 * Install ELAN macs in L2 Gateway device.<br>
618 * This includes installing ELAN mac table entries plus external device
619 * UcastLocalMacs which are part of the same ELAN.
623 * @param l2GatewayDevice
624 * the l2 gateway device which has to be configured
625 * @return the listenable future
627 public ListenableFuture<Void> installElanMacsInL2GatewayDevice(String elanName,
628 L2GatewayDevice l2GatewayDevice) {
629 String logicalSwitchName = getLogicalSwitchFromElan(elanName);
630 NodeId hwVtepNodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
632 List<RemoteUcastMacs> lstL2GatewayDevicesMacs = getOtherDevicesMacs(elanName, l2GatewayDevice, hwVtepNodeId,
634 List<RemoteUcastMacs> lstElanMacTableEntries = getElanMacTableEntriesMacs(elanName,
635 hwVtepNodeId, logicalSwitchName);
637 List<RemoteUcastMacs> lstRemoteUcastMacs = new ArrayList<>(lstL2GatewayDevicesMacs);
638 lstRemoteUcastMacs.addAll(lstElanMacTableEntries);
640 ListenableFuture<Void> future = HwvtepUtils.addRemoteUcastMacs(broker, hwVtepNodeId, lstRemoteUcastMacs);
642 LOG.info("Added RemoteUcastMacs entries [{}] in config DS. NodeID: {}, LogicalSwitch: {}",
643 lstRemoteUcastMacs.size(), hwVtepNodeId.getValue(), logicalSwitchName);
648 * Gets the l2 gateway devices ucast local macs as remote ucast macs.
652 * @param l2GatewayDeviceToBeConfigured
653 * the l2 gateway device to be configured
654 * @param hwVtepNodeId
655 * the hw vtep node Id to be configured
656 * @param logicalSwitchName
657 * the logical switch name
658 * @return the l2 gateway devices macs as remote ucast macs
660 public static List<RemoteUcastMacs> getOtherDevicesMacs(String elanName,
661 L2GatewayDevice l2GatewayDeviceToBeConfigured, NodeId hwVtepNodeId, String logicalSwitchName) {
662 List<RemoteUcastMacs> lstRemoteUcastMacs = new ArrayList<>();
664 for (L2GatewayDevice otherDevice : ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName)) {
665 if (l2GatewayDeviceToBeConfigured.getHwvtepNodeId().equals(otherDevice.getHwvtepNodeId())) {
668 if (!areMLAGDevices(l2GatewayDeviceToBeConfigured, otherDevice)) {
669 for (LocalUcastMacs localUcastMac : otherDevice.getUcastLocalMacs()) {
670 HwvtepPhysicalLocatorAugmentation physLocatorAug = HwvtepSouthboundUtils
671 .createHwvtepPhysicalLocatorAugmentation(otherDevice.getTunnelIp());
672 RemoteUcastMacs remoteUcastMac = HwvtepSouthboundUtils.createRemoteUcastMac(hwVtepNodeId,
673 IetfYangUtil.INSTANCE.canonizeMacAddress(localUcastMac.getMacEntryKey()).getValue(),
674 localUcastMac.getIpaddr(), logicalSwitchName, physLocatorAug);
675 lstRemoteUcastMacs.add(remoteUcastMac);
679 return lstRemoteUcastMacs;
685 * @param l2GatewayDevice
686 * the l2 gateway device
687 * @param otherL2GatewayDevice
688 * the other l2 gateway device
689 * @return true, if both the specified l2 gateway devices are part of same
692 public static boolean areMLAGDevices(L2GatewayDevice l2GatewayDevice, L2GatewayDevice otherL2GatewayDevice) {
693 // If tunnel IPs are same, then it is considered to be part of same MLAG
694 return Objects.equals(l2GatewayDevice.getTunnelIp(), otherL2GatewayDevice.getTunnelIp());
698 * Gets the elan mac table entries as remote ucast macs. <br>
699 * Note: ELAN MAC table only contains internal switches MAC's. It doesn't
700 * contain external device MAC's.
704 * @param hwVtepNodeId
705 * the hw vtep node id
706 * @param logicalSwitchName
707 * the logical switch name
708 * @return the elan mac table entries as remote ucast macs
710 public List<RemoteUcastMacs> getElanMacTableEntriesMacs(String elanName,
711 NodeId hwVtepNodeId, String logicalSwitchName) {
712 List<RemoteUcastMacs> lstRemoteUcastMacs = new ArrayList<>();
714 MacTable macTable = ElanUtils.getElanMacTable(broker, elanName);
715 if (macTable == null || macTable.getMacEntry() == null || macTable.getMacEntry().isEmpty()) {
716 LOG.trace("MacTable is empty for elan: {}", elanName);
717 return lstRemoteUcastMacs;
720 for (MacEntry macEntry : macTable.getMacEntry()) {
721 BigInteger dpnId = getDpidFromInterface(macEntry.getInterface());
723 LOG.error("DPN ID not found for interface {}", macEntry.getInterface());
727 IpAddress dpnTepIp = elanItmUtils.getSourceDpnTepIp(dpnId, hwVtepNodeId);
728 LOG.trace("Dpn Tep IP: {} for dpnId: {} and nodeId: {}", dpnTepIp, dpnId, hwVtepNodeId.getValue());
729 if (dpnTepIp == null) {
730 LOG.error("TEP IP not found for dpnId {} and nodeId {}", dpnId, hwVtepNodeId.getValue());
733 HwvtepPhysicalLocatorAugmentation physLocatorAug = HwvtepSouthboundUtils
734 .createHwvtepPhysicalLocatorAugmentation(dpnTepIp);
735 // TODO: Query ARP cache to get IP address corresponding to the
737 RemoteUcastMacs remoteUcastMac = HwvtepSouthboundUtils.createRemoteUcastMac(hwVtepNodeId,
738 IetfYangUtil.INSTANCE.canonizePhysAddress(macEntry.getMacAddress()).getValue(), null /*IpAddress*/,
739 logicalSwitchName, physLocatorAug);
740 lstRemoteUcastMacs.add(remoteUcastMac);
742 return lstRemoteUcastMacs;
746 * Gets the dpid from interface.
748 * @param interfaceName
750 * @return the dpid from interface
753 public BigInteger getDpidFromInterface(String interfaceName) {
754 BigInteger dpId = null;
755 Future<RpcResult<GetDpidFromInterfaceOutput>> output = interfaceManagerRpcService
756 .getDpidFromInterface(new GetDpidFromInterfaceInputBuilder().setIntfName(interfaceName).build());
758 RpcResult<GetDpidFromInterfaceOutput> rpcResult = output.get();
759 if (rpcResult != null && rpcResult.isSuccessful()) {
760 dpId = rpcResult.getResult().getDpid();
762 } catch (InterruptedException | ExecutionException e) {
763 LOG.error("Failed to get the DPN ID for interface {}", interfaceName, e);
769 * Update vlan bindings in l2 gateway device.
773 * @param logicalSwitchName
774 * the logical switch name
775 * @param hwVtepDevice
776 * the hardware device
777 * @param defaultVlanId
778 * the default vlan id
779 * @return the listenable future
781 public ListenableFuture<Void> updateVlanBindingsInL2GatewayDevice(NodeId nodeId, String logicalSwitchName,
782 Devices hwVtepDevice, Integer defaultVlanId) {
783 if (hwVtepDevice == null || hwVtepDevice.getInterfaces() == null || hwVtepDevice.getInterfaces().isEmpty()) {
784 String errMsg = "HwVtepDevice is null or interfaces are empty.";
786 return Futures.immediateFailedFuture(new RuntimeException(errMsg));
789 return txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
790 for (org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712
791 .l2gateway.attributes.devices.Interfaces deviceInterface : hwVtepDevice.getInterfaces()) {
792 //Removed the check for checking terminationPoint present in OP or not
793 //for coniguring vlan bindings
794 //As we are not any more dependent on it , plugin takes care of this
795 // with port reconcilation.
796 List<VlanBindings> vlanBindings = new ArrayList<>();
797 if (deviceInterface.getSegmentationIds() != null && !deviceInterface.getSegmentationIds().isEmpty()) {
798 for (Integer vlanId : deviceInterface.getSegmentationIds()) {
799 vlanBindings.add(HwvtepSouthboundUtils.createVlanBinding(nodeId, vlanId, logicalSwitchName));
802 // Use defaultVlanId (specified in L2GatewayConnection) if Vlan
803 // ID not specified at interface level.
804 vlanBindings.add(HwvtepSouthboundUtils.createVlanBinding(nodeId, defaultVlanId, logicalSwitchName));
806 HwvtepUtils.mergeVlanBindings(tx, nodeId, hwVtepDevice.getDeviceName(),
807 deviceInterface.getInterfaceName(), vlanBindings);
809 LOG.info("Updated Hwvtep VlanBindings in config DS. NodeID: {}, LogicalSwitch: {}", nodeId.getValue(),
815 * Update vlan bindings in l2 gateway device.
820 * the physical switch name
821 * @param interfaceName
822 * the interface in physical switch
823 * @param vlanBindings
824 * the vlan bindings to be configured
825 * @return the listenable future
827 public ListenableFuture<Void> updateVlanBindingsInL2GatewayDevice(NodeId nodeId, String psName,
828 String interfaceName, List<VlanBindings> vlanBindings) {
829 return txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
830 HwvtepUtils.mergeVlanBindings(tx, nodeId, psName, interfaceName, vlanBindings);
831 LOG.info("Updated Hwvtep VlanBindings in config DS. NodeID: {}", nodeId.getValue());
836 * Delete vlan bindings from l2 gateway device.
840 * @param hwVtepDevice
842 * @param defaultVlanId
843 * the default vlan id
844 * @return the listenable future
846 public ListenableFuture<Void> deleteVlanBindingsFromL2GatewayDevice(NodeId nodeId, Devices hwVtepDevice,
847 Integer defaultVlanId) {
848 if (hwVtepDevice == null || hwVtepDevice.getInterfaces() == null || hwVtepDevice.getInterfaces().isEmpty()) {
849 String errMsg = "HwVtepDevice is null or interfaces are empty.";
851 return Futures.immediateFailedFuture(new RuntimeException(errMsg));
853 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, hwVtepDevice.getDeviceName());
855 return txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
856 for (org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712
857 .l2gateway.attributes.devices.Interfaces deviceInterface : hwVtepDevice.getInterfaces()) {
858 String phyPortName = deviceInterface.getInterfaceName();
859 if (deviceInterface.getSegmentationIds() != null && !deviceInterface.getSegmentationIds().isEmpty()) {
860 for (Integer vlanId : deviceInterface.getSegmentationIds()) {
861 HwvtepUtils.deleteVlanBinding(tx, physicalSwitchNodeId, phyPortName, vlanId);
864 // Use defaultVlanId (specified in L2GatewayConnection) if Vlan
865 // ID not specified at interface level.
866 HwvtepUtils.deleteVlanBinding(tx, physicalSwitchNodeId, phyPortName, defaultVlanId);
869 LOG.info("Deleted Hwvtep VlanBindings from config DS. NodeID: {}, hwVtepDevice: {}, defaultVlanId: {} ",
870 nodeId.getValue(), hwVtepDevice, defaultVlanId);
875 * Gets the logical switch name from elan name.
879 * @return the logical switch from elan name
881 public static String getLogicalSwitchFromElan(String elanName) {
882 // Assuming logical switch name is same as elan name
883 String logicalSwitchName = elanName;
884 return logicalSwitchName;
888 * Gets the l2 gateway connection job key.
890 * @param logicalSwitchName
891 * the logical switch name
892 * @return the l2 gateway connection job key
894 public static String getL2GatewayConnectionJobKey(String logicalSwitchName) {
895 return logicalSwitchName;
898 public static InstanceIdentifier<Interface> getInterfaceIdentifier(InterfaceKey interfaceKey) {
899 InstanceIdentifier.InstanceIdentifierBuilder<Interface> interfaceInstanceIdentifierBuilder = InstanceIdentifier
900 .builder(Interfaces.class).child(Interface.class, interfaceKey);
901 return interfaceInstanceIdentifierBuilder.build();
905 public static Interface getInterfaceFromConfigDS(InterfaceKey interfaceKey, DataBroker dataBroker) {
906 InstanceIdentifier<Interface> interfaceId = getInterfaceIdentifier(interfaceKey);
908 return SingleTransactionDataBroker
909 .syncReadOptional(dataBroker, LogicalDatastoreType.CONFIGURATION, interfaceId).orNull();
910 } catch (ReadFailedException e) {
911 // TODO remove this, and propagate ReadFailedException instead of re-throw RuntimeException
912 LOG.error("getInterfaceFromConfigDS({}) failed", interfaceKey, e);
913 throw new RuntimeException(e);
918 * Delete l2 gateway device ucast local macs from elan.<br>
919 * Deletes macs from internal ELAN nodes and also on rest of external l2
920 * gateway devices which are part of the ELAN.
922 * @param l2GatewayDevice
923 * the l2 gateway device whose ucast local macs to be deleted
928 public void deleteL2GwDeviceUcastLocalMacsFromElan(L2GatewayDevice l2GatewayDevice,
930 LOG.info("Deleting L2GatewayDevice [{}] UcastLocalMacs from elan [{}]", l2GatewayDevice.getHwvtepNodeId(),
933 ElanInstance elan = elanInstanceCache.get(elanName).orNull();
935 LOG.error("Could not find Elan by name: {}", elanName);
939 Collection<MacAddress> localMacs = getL2GwDeviceLocalMacs(elanName, l2GatewayDevice);
940 unInstallL2GwUcastMacFromL2gwDevices(elanName, l2GatewayDevice, localMacs);
941 unInstallL2GwUcastMacFromElanDpns(elan, l2GatewayDevice, localMacs);
944 public static void createItmTunnels(DataBroker dataBroker, ItmRpcService itmRpcService,
945 String hwvtepId, String psName, IpAddress tunnelIp) {
946 AddL2GwDeviceInputBuilder builder = new AddL2GwDeviceInputBuilder();
947 builder.setTopologyId(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID.getValue());
948 builder.setNodeId(HwvtepSouthboundUtils.createManagedNodeId(new NodeId(hwvtepId), psName).getValue());
949 builder.setIpAddress(tunnelIp);
951 deleteStaleTunnelsOfHwvtepInITM(dataBroker, itmRpcService, hwvtepId, psName, tunnelIp);
952 RpcResult<AddL2GwDeviceOutput> rpcResult = itmRpcService.addL2GwDevice(builder.build()).get();
953 if (rpcResult.isSuccessful()) {
954 LOG.info("Created ITM tunnels for {}", hwvtepId);
956 LOG.error("Failed to create ITM Tunnels: {}", rpcResult.getErrors());
958 } catch (InterruptedException | ExecutionException e) {
959 LOG.error("RPC to create ITM tunnels failed", e);
963 private static void deleteStaleTunnelsOfHwvtepInITM(DataBroker dataBroker,
964 ItmRpcService itmRpcService,
967 IpAddress tunnelIp) {
969 Optional<TransportZones> tzonesoptional = readTransportZone(dataBroker);
970 if (!tzonesoptional.isPresent() || tzonesoptional.get().getTransportZone() == null) {
973 String psNodeId = globalNodeId + HwvtepHAUtil.PHYSICALSWITCH + psName;
974 tzonesoptional.get().nonnullTransportZone().stream()
975 .filter(zone -> zone.getDeviceVteps() != null)
976 .flatMap(zone -> zone.getDeviceVteps().stream())
977 .filter(deviceVteps -> Objects.equals(getPsName(deviceVteps), psName)) //get device with same ps name
978 .filter(deviceVteps -> !Objects.equals(psNodeId, deviceVteps.getNodeId())
979 || !Objects.equals(tunnelIp, deviceVteps.getIpAddress()))//node id or tunnel ip is changed
980 .forEach(deviceVteps -> deleteStaleL2gwTep(dataBroker, itmRpcService, deviceVteps));
981 } catch (ReadFailedException e) {
982 LOG.error("Failed delete stale tunnels for {}", globalNodeId);
986 private static Optional<TransportZones> readTransportZone(DataBroker dataBroker) throws ReadFailedException {
987 return new SingleTransactionDataBroker(dataBroker).syncReadOptional(LogicalDatastoreType.CONFIGURATION,
988 InstanceIdentifier.builder(TransportZones.class).build());
991 private static Optional<ElanInstances> readElanInstances(DataBroker dataBroker) throws ReadFailedException {
992 return new SingleTransactionDataBroker(dataBroker).syncReadOptional(LogicalDatastoreType.CONFIGURATION,
993 InstanceIdentifier.builder(ElanInstances.class).build());
996 private static String getPsName(DeviceVteps deviceVteps) {
997 return HwvtepHAUtil.getPsName(HwvtepHAUtil.convertToInstanceIdentifier(deviceVteps.getNodeId()));
1000 private static void deleteStaleL2gwTep(DataBroker dataBroker,
1001 ItmRpcService itmRpcService,
1002 DeviceVteps deviceVteps) {
1003 String psName = HwvtepHAUtil.getPsName(HwvtepHAUtil.convertToInstanceIdentifier(deviceVteps.getNodeId()));
1004 String globalNodeId = HwvtepHAUtil.convertToGlobalNodeId(deviceVteps.getNodeId());
1006 LOG.info("Deleting stale tep {} ", deviceVteps);
1007 L2GatewayUtils.deleteItmTunnels(itmRpcService, globalNodeId, psName, deviceVteps.getIpAddress());
1008 Optional<ElanInstances> optionalElan = readElanInstances(dataBroker);
1009 if (!optionalElan.isPresent()) {
1012 LoggingFutures.addErrorLogging(
1013 new ManagedNewTransactionRunnerImpl(dataBroker).callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
1014 tx -> optionalElan.get().nonnullElanInstance().stream()
1015 .flatMap(elan -> elan.nonnullExternalTeps().stream()
1016 .map(externalTep -> ElanL2GatewayMulticastUtils.buildExternalTepPath(
1017 elan.getElanInstanceName(), externalTep.getTepIp())))
1018 .filter(externalTepIid -> Objects.equals(
1019 deviceVteps.getIpAddress(), externalTepIid.firstKeyOf(ExternalTeps.class).getTepIp()))
1020 .peek(externalTepIid -> LOG.info("Deleting stale external tep {}", externalTepIid))
1021 .forEach(tx::delete)), LOG,
1022 "Failed to delete stale external teps {}", deviceVteps);
1023 Thread.sleep(10000);//TODO remove the sleep currently it waits for interfacemgr to finish the cleanup
1024 } catch (ReadFailedException | InterruptedException e) {
1025 LOG.error("Failed to delete stale l2gw tep {}", deviceVteps, e);
1029 public void scheduleAddDpnMacInExtDevices(String elanName, BigInteger dpId,
1030 List<PhysAddress> staticMacAddresses) {
1031 for (final L2GatewayDevice externalDevice : ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName)) {
1032 scheduleAddDpnMacsInExtDevice(elanName, dpId, staticMacAddresses, externalDevice);
1036 public void scheduleAddDpnMacsInExtDevice(final String elanName, BigInteger dpId,
1037 final List<PhysAddress> staticMacAddresses, final L2GatewayDevice externalDevice) {
1038 NodeId nodeId = new NodeId(externalDevice.getHwvtepNodeId());
1039 final IpAddress dpnTepIp = elanItmUtils.getSourceDpnTepIp(dpId, nodeId);
1040 LOG.trace("Dpn Tep IP: {} for dpnId: {} and nodeId: {}", dpnTepIp, dpId, nodeId);
1041 if (dpnTepIp == null) {
1042 LOG.error("could not install dpn mac in l2gw TEP IP not found for dpnId {} and nodeId {}", dpId, nodeId);
1046 //TODO: to be batched in genius
1047 HwvtepUtils.installUcastMacs(broker, externalDevice.getHwvtepNodeId(), staticMacAddresses, elanName, dpnTepIp);
1050 public void scheduleDeleteLogicalSwitch(NodeId hwvtepNodeId, String lsName) {
1051 scheduleDeleteLogicalSwitch(hwvtepNodeId, lsName, false);
1054 public void scheduleDeleteLogicalSwitch(final NodeId hwvtepNodeId, final String lsName, final boolean clearUcast) {
1055 final Pair<NodeId, String> nodeIdLogicalSwitchNamePair = new ImmutablePair<>(hwvtepNodeId, lsName);
1056 logicalSwitchDeletedTasks.computeIfAbsent(nodeIdLogicalSwitchNamePair,
1057 (key) -> scheduler.getScheduledExecutorService().schedule(() -> {
1058 DeleteLogicalSwitchJob deleteLsJob = new DeleteLogicalSwitchJob(broker,
1059 ElanL2GatewayUtils.this, hwvtepNodeId, lsName, clearUcast);
1060 jobCoordinator.enqueueJob(deleteLsJob.getJobKey(), deleteLsJob,
1061 SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
1062 deleteJobs.put(nodeIdLogicalSwitchNamePair, deleteLsJob);
1063 logicalSwitchDeletedTasks.remove(nodeIdLogicalSwitchNamePair);
1064 }, getLogicalSwitchDeleteDelaySecs(), TimeUnit.SECONDS));
1067 public void cancelDeleteLogicalSwitch(final NodeId hwvtepNodeId, final String lsName) {
1068 Pair<NodeId, String> nodeIdLogicalSwitchNamePair = new ImmutablePair<>(hwvtepNodeId, lsName);
1069 ScheduledFuture logicalSwitchDeleteTask = logicalSwitchDeletedTasks.remove(nodeIdLogicalSwitchNamePair);
1070 if (logicalSwitchDeleteTask != null) {
1071 LOG.debug("Delete logical switch {} action on node {} cancelled", lsName, hwvtepNodeId);
1072 logicalSwitchDeleteTask.cancel(true);
1073 DeleteLogicalSwitchJob deleteLogicalSwitchJob = deleteJobs.remove(nodeIdLogicalSwitchNamePair);
1074 if (deleteLogicalSwitchJob != null) {
1075 deleteLogicalSwitchJob.cancel();
1081 public Collection<DpnInterfaces> getElanDpns(String elanName) {
1082 Collection<DpnInterfaces> dpnInterfaces = elanInstanceDpnsCache.get(elanName);
1083 if (!dpnInterfaces.isEmpty()) {
1084 return dpnInterfaces;
1087 return elanUtils.getElanDPNByName(elanName);
1091 * Gets the l2 gw device local macs.
1096 * @return the l2 gw device local macs
1098 public Collection<MacAddress> getL2GwDeviceLocalMacs(String elanName, L2GatewayDevice l2gwDevice) {
1099 if (l2gwDevice == null) {
1100 return Collections.emptyList();
1102 Collection<LocalUcastMacs> lstUcastLocalMacs = l2gwDevice.getUcastLocalMacs();
1103 Set<MacAddress> macs = new HashSet<>();
1104 if (!lstUcastLocalMacs.isEmpty()) {
1105 macs.addAll(lstUcastLocalMacs.stream().filter(Objects::nonNull)
1106 .map(mac -> new MacAddress(mac.getMacEntryKey().getValue().toLowerCase(Locale.ENGLISH)))
1107 .collect(Collectors.toList()));
1109 Optional<Node> configNode = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION,
1110 HwvtepSouthboundUtils.createInstanceIdentifier(new NodeId(l2gwDevice.getHwvtepNodeId())));
1111 if (configNode.isPresent()) {
1112 HwvtepGlobalAugmentation augmentation = configNode.get().augmentation(HwvtepGlobalAugmentation.class);
1113 if (augmentation != null && augmentation.getLocalUcastMacs() != null) {
1114 macs.addAll(augmentation.getLocalUcastMacs().stream()
1115 .filter(mac -> getLogicalSwitchName(mac).equals(elanName))
1116 .map(HwvtepMacTableGenericAttributes::getMacEntryKey)
1117 .collect(Collectors.toSet()));