Bulk merge of l2gw changes
[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 org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
15 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
16 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayBcGroupUtils;
17 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
18 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
19 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
20 import org.opendaylight.netvirt.elan.utils.Scheduler;
21 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Created by ekvsver on 4/15/2016.
29  */
30 public class DisAssociateHwvtepFromElanJob implements Callable<List<? extends ListenableFuture<?>>> {
31     private static final Logger LOG = LoggerFactory.getLogger(DisAssociateHwvtepFromElanJob.class);
32
33     private final ElanL2GatewayUtils elanL2GatewayUtils;
34     private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
35     private final ElanL2GatewayBcGroupUtils elanL2GatewayBcGroupUtils;
36     private final ElanClusterUtils elanClusterUtils;
37     private final Scheduler scheduler;
38     private final JobCoordinator jobCoordinator;
39     private final L2GatewayDevice l2GatewayDevice;
40     private final String elanName;
41     private final Devices l2Device;
42     private final Integer defaultVlan;
43     private final boolean isLastL2GwConnDeleted;
44     private final NodeId hwvtepNodeId;
45     private final String hwvtepNodeIdString;
46
47     public DisAssociateHwvtepFromElanJob(ElanL2GatewayUtils elanL2GatewayUtils,
48                                          ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
49                                          ElanL2GatewayBcGroupUtils elanL2GatewayBcGroupUtils,
50                                          ElanClusterUtils elanClusterUtils, Scheduler scheduler,
51                                          JobCoordinator jobCoordinator,
52                                          L2GatewayDevice l2GatewayDevice,  String elanName,
53                                          Devices l2Device, Integer defaultVlan,
54                                          String nodeId, boolean isLastL2GwConnDeleted) {
55         this.elanL2GatewayUtils = elanL2GatewayUtils;
56         this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
57         this.elanL2GatewayBcGroupUtils = elanL2GatewayBcGroupUtils;
58         this.elanClusterUtils = elanClusterUtils;
59         this.scheduler = scheduler;
60         this.jobCoordinator = jobCoordinator;
61         this.l2GatewayDevice = l2GatewayDevice;
62         this.elanName = elanName;
63         this.l2Device = l2Device;
64         this.defaultVlan = defaultVlan;
65         this.isLastL2GwConnDeleted = isLastL2GwConnDeleted;
66         this.hwvtepNodeId = new NodeId(nodeId);
67         this.hwvtepNodeIdString = nodeId;
68         LOG.trace("created disassociate l2gw connection job for {}", elanName);
69     }
70
71     public String getJobKey() {
72         return hwvtepNodeIdString + HwvtepHAUtil.L2GW_JOB_KEY;
73     }
74
75     @Override
76     public List<ListenableFuture<?>> call() throws Exception {
77         String strHwvtepNodeId = hwvtepNodeId.getValue();
78         LOG.info("running disassociate l2gw connection job for elanName:{},strHwvtepNodeId:{},"
79             + "isLastL2GwConnDeleted:{}", elanName, strHwvtepNodeId, isLastL2GwConnDeleted);
80
81         List<ListenableFuture<?>> futures = new ArrayList<>();
82
83         // Remove remote MACs and vlan mappings from physical port
84         // Once all above configurations are deleted, delete logical
85         // switch
86         LOG.trace("delete vlan bindings for {} {}", elanName, strHwvtepNodeId);
87         futures.add(elanL2GatewayUtils.deleteVlanBindingsFromL2GatewayDevice(hwvtepNodeId, l2Device, defaultVlan));
88
89         if (isLastL2GwConnDeleted) {
90             if (l2GatewayDevice == null) {
91                 LOG.info("Scheduled delete logical switch {} {}", elanName, strHwvtepNodeId);
92                 elanL2GatewayUtils.scheduleDeleteLogicalSwitch(hwvtepNodeId,
93                         ElanL2GatewayUtils.getLogicalSwitchFromElan(elanName));
94                 return futures;
95             }
96             LOG.trace("delete remote ucast macs {} {}", elanName, strHwvtepNodeId);
97             elanL2GatewayUtils.deleteElanMacsFromL2GatewayDevice(hwvtepNodeId.getValue(), elanName);
98
99             LOG.trace("delete mcast mac for {} {}", elanName, strHwvtepNodeId);
100             McastUpdateJob.removeMcastForNode(elanName, l2GatewayDevice.getHwvtepNodeId(),
101                     elanL2GatewayMulticastUtils, elanClusterUtils, scheduler, jobCoordinator);
102             elanL2GatewayBcGroupUtils.updateBcGroupForAllDpns(elanName, l2GatewayDevice, false);
103             elanL2GatewayMulticastUtils.updateMcastMacsForAllElanDevices(elanName, l2GatewayDevice, false);
104
105 //            futures.addAll(elanL2GatewayMulticastUtils.handleMcastForElanL2GwDeviceDelete(this.elanName,
106 //                    l2GatewayDevice));
107
108             LOG.trace("delete local ucast macs {} {}", elanName, strHwvtepNodeId);
109             elanL2GatewayUtils.deleteL2GwDeviceUcastLocalMacsFromElan(l2GatewayDevice, elanName);
110
111             LOG.info("scheduled delete logical switch {} {}", elanName, strHwvtepNodeId);
112             elanL2GatewayUtils.scheduleDeleteLogicalSwitch(hwvtepNodeId,
113                     ElanL2GatewayUtils.getLogicalSwitchFromElan(elanName));
114         } else {
115             LOG.trace("l2gw mcast delete not triggered for nodeId {} with elan {}",
116                     l2GatewayDevice != null ? l2GatewayDevice.getHwvtepNodeId() : null, elanName);
117         }
118         return futures;
119     }
120 }