Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / vpnservice / 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.vpnservice.elan.l2gw.jobs;
10
11 import java.util.List;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.ConcurrentMap;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.elanmanager.utils.ElanL2GwCacheUtils;
17 import org.opendaylight.vpnservice.elan.l2gw.utils.ElanL2GatewayUtils;
18 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.L2GatewayDevice;
19 import org.opendaylight.vpnservice.utils.hwvtep.HwvtepUtils;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.collect.Lists;
26 import com.google.common.util.concurrent.FutureCallback;
27 import com.google.common.util.concurrent.Futures;
28 import com.google.common.util.concurrent.ListenableFuture;
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 broker. */
43     private final DataBroker broker;
44
45     /** The elan name. */
46     private final String elanName;
47
48     /** The l2 gw device. */
49     private final L2GatewayDevice l2GwDevice;
50
51     /** The mac addresses. */
52     private final List<MacAddress> macAddresses;
53
54     /**
55      * Instantiates a new delete l2 gw device macs from elan job.
56      *
57      * @param broker
58      *            the broker
59      * @param elanName
60      *            the elan name
61      * @param l2GwDevice
62      *            the l2 gw device
63      * @param macAddresses
64      *            the mac addresses
65      */
66     public DeleteL2GwDeviceMacsFromElanJob(DataBroker broker, String elanName, L2GatewayDevice l2GwDevice,
67             List<MacAddress> macAddresses) {
68         this.broker = broker;
69         this.elanName = elanName;
70         this.l2GwDevice = l2GwDevice;
71         this.macAddresses = macAddresses;
72     }
73
74     public String getJobKey() {
75         return JOB_KEY_PREFIX + this.elanName;
76     }
77
78     /*
79      * (non-Javadoc)
80      *
81      * @see java.util.concurrent.Callable#call()
82      */
83     @Override
84     public List<ListenableFuture<Void>> call() {
85         LOG.debug("Deleting l2gw device [{}] macs from other l2gw devices for elan [{}]",
86                 this.l2GwDevice.getHwvtepNodeId(), this.elanName);
87         final String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(this.elanName);
88
89         ConcurrentMap<String, L2GatewayDevice> elanL2GwDevices = ElanL2GwCacheUtils
90                 .getInvolvedL2GwDevices(this.elanName);
91         List<ListenableFuture<Void>> futures = Lists.newArrayList();
92         for (L2GatewayDevice otherDevice : elanL2GwDevices.values()) {
93             if (!otherDevice.getHwvtepNodeId().equals(this.l2GwDevice.getHwvtepNodeId())
94                     && !ElanL2GatewayUtils.areMLAGDevices(this.l2GwDevice, otherDevice)) {
95                 final String hwvtepId = otherDevice.getHwvtepNodeId();
96                 // never batch deletes
97                 ListenableFuture<Void> uninstallFuture = HwvtepUtils.deleteRemoteUcastMacs(this.broker,
98                         new NodeId(hwvtepId), logicalSwitchName, this.macAddresses);
99                 Futures.addCallback(uninstallFuture, new FutureCallback<Void>() {
100                     @Override
101                     public void onSuccess(Void noarg) {
102                         LOG.trace("Successful in initiating ucast_remote_macs deletion related to {} in {}",
103                                 logicalSwitchName, hwvtepId);
104                     }
105
106                     @Override
107                     public void onFailure(Throwable error) {
108                         LOG.error(String.format("Failed removing ucast_remote_macs related to %s in %s",
109                                 logicalSwitchName, hwvtepId), error);
110                     }
111                 });
112                 // TODO: why to create a new arraylist for uninstallFuture?
113                 futures.addAll(Lists.newArrayList(uninstallFuture));
114             }
115         }
116         return futures;
117     }
118 }