85d3488260fd9fc7270cb084bcc286826272d752
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / jobs / DisAssociateHwvtepFromElanJob.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.List;
13 import java.util.concurrent.Callable;
14 import java.util.function.Supplier;
15 import javax.annotation.Nullable;
16 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
17 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
18 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
19 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27 * Created by ekvsver on 4/15/2016.
28 */
29 public class DisAssociateHwvtepFromElanJob implements Callable<List<ListenableFuture<Void>>> {
30     private static final Logger LOG = LoggerFactory.getLogger(DisAssociateHwvtepFromElanJob.class);
31
32     private final ElanL2GatewayUtils elanL2GatewayUtils;
33     private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
34     private final L2GatewayDevice l2GatewayDevice;
35     private final String elanName;
36     private final Devices l2Device;
37     private final Integer defaultVlan;
38     private final boolean isLastL2GwConnDeleted;
39     private final NodeId hwvtepNodeId;
40     private final Supplier<ElanInstance> elanInstanceSupplier;
41
42     public DisAssociateHwvtepFromElanJob(ElanL2GatewayUtils elanL2GatewayUtils,
43                                          ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
44                                          @Nullable L2GatewayDevice l2GatewayDevice,  String elanName,
45                                          Supplier<ElanInstance> elanInstanceSupplier, Devices l2Device,
46                                          Integer defaultVlan, String nodeId, boolean isLastL2GwConnDeleted) {
47         this.elanL2GatewayUtils = elanL2GatewayUtils;
48         this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
49         this.l2GatewayDevice = l2GatewayDevice;
50         this.elanName = elanName;
51         this.elanInstanceSupplier = elanInstanceSupplier;
52         this.l2Device = l2Device;
53         this.defaultVlan = defaultVlan;
54         this.isLastL2GwConnDeleted = isLastL2GwConnDeleted;
55         this.hwvtepNodeId = new NodeId(nodeId);
56         LOG.info("created disassociate l2gw connection job for {}", elanName);
57     }
58
59     public String getJobKey() {
60         return elanName + HwvtepHAUtil.L2GW_JOB_KEY;
61     }
62
63     @Override
64     public List<ListenableFuture<Void>> call() {
65         String strHwvtepNodeId = hwvtepNodeId.getValue();
66         LOG.info("running disassosiate l2gw connection job for {} {}", elanName, strHwvtepNodeId);
67
68         List<ListenableFuture<Void>> futures = new ArrayList<>();
69
70         // Remove remote MACs and vlan mappings from physical port
71         // Once all above configurations are deleted, delete logical
72         // switch
73         LOG.info("delete vlan bindings for {} {}", elanName, strHwvtepNodeId);
74         futures.add(elanL2GatewayUtils.deleteVlanBindingsFromL2GatewayDevice(hwvtepNodeId, l2Device, defaultVlan));
75
76         if (isLastL2GwConnDeleted) {
77             if (l2GatewayDevice == null) {
78                 LOG.info("Scheduled delete logical switch {} {}", elanName, strHwvtepNodeId);
79                 elanL2GatewayUtils.scheduleDeleteLogicalSwitch(hwvtepNodeId,
80                         ElanL2GatewayUtils.getLogicalSwitchFromElan(elanName));
81                 return futures;
82             }
83             LOG.info("delete remote ucast macs {} {}", elanName, strHwvtepNodeId);
84             futures.add(elanL2GatewayUtils.deleteElanMacsFromL2GatewayDevice(hwvtepNodeId.getValue(), elanName));
85
86             LOG.info("delete mcast mac for {} {}", elanName, strHwvtepNodeId);
87             futures.addAll(elanL2GatewayMulticastUtils.handleMcastForElanL2GwDeviceDelete(this.elanName,
88                     elanInstanceSupplier.get(), l2GatewayDevice));
89
90             LOG.info("delete local ucast macs {} {}", elanName, strHwvtepNodeId);
91             elanL2GatewayUtils.deleteL2GwDeviceUcastLocalMacsFromElan(l2GatewayDevice, elanName);
92
93             LOG.info("scheduled delete logical switch {} {}", elanName, strHwvtepNodeId);
94             elanL2GatewayUtils.scheduleDeleteLogicalSwitch(hwvtepNodeId,
95                     ElanL2GatewayUtils.getLogicalSwitchFromElan(elanName));
96         } else {
97             LOG.info("l2gw mcast delete not triggered for nodeId {}  with elan {}",
98                     l2GatewayDevice != null ? l2GatewayDevice.getHwvtepNodeId() : null, elanName);
99         }
100
101         return futures;
102     }
103 }