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