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
9 package org.opendaylight.netvirt.elan.l2gw.jobs;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Locale;
17 import java.util.concurrent.Callable;
18 import java.util.stream.Collectors;
19 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
20 import org.opendaylight.genius.utils.batching.ResourceBatchingManager.ShardResource;
21 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
22 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
23 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
24 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The Job class to delete L2 gateway device local ucast macs from other Elan L2
34 public class DeleteL2GwDeviceMacsFromElanJob implements Callable<List<ListenableFuture<Void>>> {
36 /** The Constant JOB_KEY_PREFIX. */
37 private static final String JOB_KEY_PREFIX = "hwvtep:";
39 /** The Constant LOG. */
40 private static final Logger LOG = LoggerFactory.getLogger(DeleteL2GwDeviceMacsFromElanJob.class);
43 private final String elanName;
45 /** The l2 gw device. */
46 private final L2GatewayDevice l2GwDevice;
48 /** The mac addresses. */
49 private final Collection<MacAddress> macAddresses;
52 * Instantiates a new delete l2 gw device macs from elan job.
61 public DeleteL2GwDeviceMacsFromElanJob(String elanName, L2GatewayDevice l2GwDevice,
62 Collection<MacAddress> macAddresses) {
63 this.elanName = elanName;
64 this.l2GwDevice = l2GwDevice;
65 this.macAddresses = macAddresses;
73 public String getJobKey() {
74 String jobKey = JOB_KEY_PREFIX + this.elanName;
75 if (macAddresses != null && macAddresses.size() == 1) {
76 jobKey += ":" + macAddresses.iterator().next().getValue();
84 * @see java.util.concurrent.Callable#call()
87 public List<ListenableFuture<Void>> call() {
88 LOG.debug("Deleting l2gw device [{}] macs from other l2gw devices for elan [{}]",
89 this.l2GwDevice.getHwvtepNodeId(), this.elanName);
90 final String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(this.elanName);
91 List<MacAddress> macs = new ArrayList<>();
92 macAddresses.forEach((mac) -> macs.add(new MacAddress(mac.getValue().toLowerCase(Locale.ENGLISH))));
94 List<ListenableFuture<Void>> futures = new ArrayList<>();
95 for (L2GatewayDevice otherDevice : ElanL2GwCacheUtils.getInvolvedL2GwDevices(this.elanName)) {
96 if (!otherDevice.getHwvtepNodeId().equals(this.l2GwDevice.getHwvtepNodeId())
97 && !ElanL2GatewayUtils.areMLAGDevices(this.l2GwDevice, otherDevice)) {
98 final String hwvtepId = otherDevice.getHwvtepNodeId();
99 //This delete is batched using resourcebatchingmnagaer
100 futures.addAll(deleteRemoteUcastMacs(new NodeId(hwvtepId), logicalSwitchName, macs));
107 * Batched operation to delete list of Uast mac from the given nodeId and logical Switch .
108 * @param nodeId NodeId of device
109 * @param logicalSwitchName logicalSwitch Name
110 * @param lstMac list of macs to be deleted
111 * @return list of futures
113 public static List<ListenableFuture<Void>> deleteRemoteUcastMacs(final NodeId nodeId,
114 String logicalSwitchName, final List<MacAddress> lstMac) {
115 if (lstMac != null) {
116 return lstMac.stream()
117 .map(mac -> HwvtepSouthboundUtils.createRemoteUcastMacsInstanceIdentifier(
118 nodeId, logicalSwitchName, mac))
119 .map(iid -> ResourceBatchingManager.getInstance().delete(ShardResource.CONFIG_TOPOLOGY, iid))
120 .collect(Collectors.toList());
122 return Collections.emptyList();