cf17b1fab845408cc89d2c860f324d244cf0ec7f
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / jobs / DeleteL2GwDeviceMacsFromElanJob.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
9 package org.opendaylight.netvirt.elan.l2gw.jobs;
10
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;
29
30 /**
31  * The Job class to delete L2 gateway device local ucast macs from other Elan L2
32  * gateway devices.
33  */
34 public class DeleteL2GwDeviceMacsFromElanJob implements Callable<List<ListenableFuture<Void>>> {
35
36     /** The Constant JOB_KEY_PREFIX. */
37     private static final String JOB_KEY_PREFIX = "hwvtep:";
38
39     /** The Constant LOG. */
40     private static final Logger LOG = LoggerFactory.getLogger(DeleteL2GwDeviceMacsFromElanJob.class);
41
42     /** The elan name. */
43     private final String elanName;
44
45     /** The l2 gw device. */
46     private final L2GatewayDevice l2GwDevice;
47
48     /** The mac addresses. */
49     private final Collection<MacAddress> macAddresses;
50
51     /**
52      * Instantiates a new delete l2 gw device macs from elan job.
53      *
54      * @param elanName
55      *            the elan name
56      * @param l2GwDevice
57      *            the l2 gw device
58      * @param macAddresses
59      *            the mac addresses
60      */
61     public DeleteL2GwDeviceMacsFromElanJob(String elanName, L2GatewayDevice l2GwDevice,
62                                            Collection<MacAddress> macAddresses) {
63         this.elanName = elanName;
64         this.l2GwDevice = l2GwDevice;
65         this.macAddresses = macAddresses;
66     }
67
68     /**
69      * Gets the job key.
70      *
71      * @return the job key
72      */
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();
77         }
78         return jobKey;
79     }
80
81     /*
82      * (non-Javadoc)
83      *
84      * @see java.util.concurrent.Callable#call()
85      */
86     @Override
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))));
93
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));
101             }
102         }
103         return futures;
104     }
105
106     /**
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
112      */
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());
121         }
122         return Collections.emptyList();
123     }
124 }