Bulk merge of l2gw changes
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / jobs / DeleteLogicalSwitchJob.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 package org.opendaylight.netvirt.elan.l2gw.jobs;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.concurrent.Callable;
15 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
16 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
19 import org.opendaylight.netvirt.elan.utils.ElanConstants;
20 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The Class DeleteLogicalSwitchJob.
33  */
34 public class DeleteLogicalSwitchJob implements Callable<List<? extends ListenableFuture<?>>> {
35     private DataBroker broker;
36
37     /** The logical switch name. */
38     private String logicalSwitchName;
39
40     /** The physical device. */
41     private NodeId hwvtepNodeId;
42
43     private final ElanL2GatewayUtils elanL2GatewayUtils;
44     private final boolean clearUcast;
45     private volatile boolean cancelled = false;
46
47     private static final Logger LOG = LoggerFactory.getLogger(DeleteLogicalSwitchJob.class);
48
49     public DeleteLogicalSwitchJob(DataBroker broker, ElanL2GatewayUtils elanL2GatewayUtils,
50                                   NodeId hwvtepNodeId, String logicalSwitchName, boolean clearUcast) {
51         this.broker = broker;
52         this.hwvtepNodeId = hwvtepNodeId;
53         this.logicalSwitchName = logicalSwitchName;
54         this.elanL2GatewayUtils = elanL2GatewayUtils;
55         this.clearUcast = clearUcast;
56         LOG.debug("created logical switch deleted job for {} on {}", logicalSwitchName, hwvtepNodeId);
57     }
58
59     public void cancel() {
60         this.cancelled = true;
61     }
62
63     public String getJobKey() {
64         return logicalSwitchName;
65     }
66
67     @Override
68     public List<ListenableFuture<?>> call() {
69         if (cancelled) {
70             LOG.info("Delete logical switch job cancelled for {}", logicalSwitchName);
71             return Collections.emptyList();
72         }
73         LOG.debug("running logical switch deleted job for {} in {}", logicalSwitchName, hwvtepNodeId);
74         elanL2GatewayUtils.deleteElanMacsFromL2GatewayDevice(hwvtepNodeId.getValue(), logicalSwitchName);
75         InstanceIdentifier<LogicalSwitches> logicalSwitch = HwvtepSouthboundUtils
76                 .createLogicalSwitchesInstanceIdentifier(hwvtepNodeId, new HwvtepNodeName(logicalSwitchName));
77         RemoteMcastMacsKey remoteMcastMacsKey = new RemoteMcastMacsKey(new HwvtepLogicalSwitchRef(logicalSwitch),
78                 new MacAddress(ElanConstants.UNKNOWN_DMAC));
79         HwvtepUtils.deleteRemoteMcastMac(broker, hwvtepNodeId, remoteMcastMacsKey);
80
81         L2GatewayDevice l2GatewayDevice = new L2GatewayDevice("");
82         l2GatewayDevice.setHwvtepNodeId(hwvtepNodeId.getValue());
83
84         List<ListenableFuture<?>> futures = new ArrayList<>();
85         futures.add(HwvtepUtils.deleteLogicalSwitch(broker, hwvtepNodeId, logicalSwitchName));
86         if (clearUcast) {
87             LOG.trace("Clearing the local ucast macs of device {} macs ", hwvtepNodeId.getValue());
88             elanL2GatewayUtils.deleteL2GwDeviceUcastLocalMacsFromElan(l2GatewayDevice, logicalSwitchName);
89         }
90         return futures;
91     }
92 }