Use Java declarations instead of Google Collections
[netvirt.git] / vpnservice / elanmanager / 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.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.concurrent.Callable;
17 import java.util.concurrent.ConcurrentMap;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
20 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
21 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
22 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * The Job class to delete L2 gateway device local ucast macs from other Elan L2
30  * gateway devices.
31  */
32 public class DeleteL2GwDeviceMacsFromElanJob implements Callable<List<ListenableFuture<Void>>> {
33
34     /** The Constant JOB_KEY_PREFIX. */
35     private static final String JOB_KEY_PREFIX = "hwvtep:";
36
37     /** The Constant LOG. */
38     private static final Logger LOG = LoggerFactory.getLogger(DeleteL2GwDeviceMacsFromElanJob.class);
39
40     /** The broker. */
41     private final DataBroker broker;
42
43     /** The elan name. */
44     private final String elanName;
45
46     /** The l2 gw device. */
47     private final L2GatewayDevice l2GwDevice;
48
49     /** The mac addresses. */
50     private final List<MacAddress> macAddresses;
51
52     /**
53      * Instantiates a new delete l2 gw device macs from elan job.
54      *
55      * @param broker
56      *            the broker
57      * @param elanName
58      *            the elan name
59      * @param l2GwDevice
60      *            the l2 gw device
61      * @param macAddresses
62      *            the mac addresses
63      */
64     public DeleteL2GwDeviceMacsFromElanJob(DataBroker broker, String elanName, L2GatewayDevice l2GwDevice,
65             List<MacAddress> macAddresses) {
66         this.broker = broker;
67         this.elanName = elanName;
68         this.l2GwDevice = l2GwDevice;
69         this.macAddresses = macAddresses;
70     }
71
72     /**
73      * Gets the job key.
74      *
75      * @return the job key
76      */
77     public String getJobKey() {
78         String jobKey = JOB_KEY_PREFIX + this.elanName;
79         if (macAddresses != null && macAddresses.size() == 1) {
80             jobKey += ":" + macAddresses.get(0).getValue();
81         }
82         return jobKey;
83     }
84
85     /*
86      * (non-Javadoc)
87      *
88      * @see java.util.concurrent.Callable#call()
89      */
90     @Override
91     public List<ListenableFuture<Void>> call() {
92         LOG.debug("Deleting l2gw device [{}] macs from other l2gw devices for elan [{}]",
93                 this.l2GwDevice.getHwvtepNodeId(), this.elanName);
94         final String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(this.elanName);
95
96         ConcurrentMap<String, L2GatewayDevice> elanL2GwDevices = ElanL2GwCacheUtils
97                 .getInvolvedL2GwDevices(this.elanName);
98         List<ListenableFuture<Void>> futures = new ArrayList<>();
99         for (L2GatewayDevice otherDevice : elanL2GwDevices.values()) {
100             if (!otherDevice.getHwvtepNodeId().equals(this.l2GwDevice.getHwvtepNodeId())
101                     && !ElanL2GatewayUtils.areMLAGDevices(this.l2GwDevice, otherDevice)) {
102                 final String hwvtepId = otherDevice.getHwvtepNodeId();
103                 // never batch deletes
104                 ListenableFuture<Void> uninstallFuture = HwvtepUtils.deleteRemoteUcastMacs(this.broker,
105                         new NodeId(hwvtepId), logicalSwitchName, this.macAddresses);
106                 Futures.addCallback(uninstallFuture, new FutureCallback<Void>() {
107                     @Override
108                     public void onSuccess(Void noarg) {
109                         LOG.trace("Successful in initiating ucast_remote_macs deletion related to {} in {}",
110                                 logicalSwitchName, hwvtepId);
111                     }
112
113                     @Override
114                     public void onFailure(Throwable error) {
115                         LOG.error(String.format("Failed removing ucast_remote_macs related to %s in %s",
116                                 logicalSwitchName, hwvtepId), error);
117                     }
118                 });
119                 futures.add(uninstallFuture);
120             }
121         }
122         return futures;
123     }
124 }