Bug 7669: Add multi-provider network support to NetVirt for L2 Gateway.
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / jobs / AssociateHwvtepToElanJob.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.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.ArrayList;
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.ElanL2GatewayMulticastUtils;
20 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
21 import org.opendaylight.netvirt.elan.utils.ElanUtils;
22 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
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 * Created by ekvsver on 4/15/2016.
34 */
35 public class AssociateHwvtepToElanJob implements Callable<List<ListenableFuture<Void>>> {
36     private static final Logger LOG = LoggerFactory.getLogger(AssociateHwvtepToElanJob.class);
37
38     private final DataBroker broker;
39     private final ElanL2GatewayUtils elanL2GatewayUtils;
40     private final ElanUtils elanUtils;
41     private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
42     private final L2GatewayDevice l2GatewayDevice;
43     private final ElanInstance elanInstance;
44     private final Devices l2Device;
45     private final Integer defaultVlan;
46     private final boolean createLogicalSwitch;
47
48     public AssociateHwvtepToElanJob(DataBroker broker, ElanL2GatewayUtils elanL2GatewayUtils, ElanUtils elanUtils,
49                                     ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
50                                     L2GatewayDevice l2GatewayDevice, ElanInstance elanInstance, Devices l2Device,
51                                     Integer defaultVlan, boolean createLogicalSwitch) {
52         this.broker = broker;
53         this.elanL2GatewayUtils = elanL2GatewayUtils;
54         this.elanUtils = elanUtils;
55         this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
56         this.l2GatewayDevice = l2GatewayDevice;
57         this.elanInstance = elanInstance;
58         this.l2Device = l2Device;
59         this.defaultVlan = defaultVlan;
60         this.createLogicalSwitch = createLogicalSwitch;
61         LOG.debug("created assosiate l2gw connection job for {} {} ", elanInstance.getElanInstanceName(),
62                 l2GatewayDevice.getHwvtepNodeId());
63     }
64
65     public String getJobKey() {
66         return elanInstance.getElanInstanceName();
67     }
68
69     @Override
70     public List<ListenableFuture<Void>> call() throws Exception {
71         List<ListenableFuture<Void>> futures = new ArrayList<>();
72         String hwvtepNodeId = l2GatewayDevice.getHwvtepNodeId();
73         String elanInstanceName = elanInstance.getElanInstanceName();
74         LOG.debug("running assosiate l2gw connection job for {} {} ", elanInstanceName, hwvtepNodeId);
75
76         // Create Logical Switch if it's not created already in the device
77         if (createLogicalSwitch) {
78             LOG.info("creating logical switch {} for {} ", elanInstanceName, hwvtepNodeId);
79
80             ListenableFuture<Void> lsCreateFuture = createLogicalSwitch(l2GatewayDevice, elanInstance);
81             futures.add(lsCreateFuture);
82         } else {
83             String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(elanInstanceName);
84             LOG.info("{} is already created in {}; adding remaining configurations", logicalSwitchName, hwvtepNodeId);
85
86             LogicalSwitchAddedJob logicalSwitchAddedJob =
87                     new LogicalSwitchAddedJob(broker, elanL2GatewayUtils, elanUtils, elanL2GatewayMulticastUtils,
88                             logicalSwitchName, l2Device,
89                             l2GatewayDevice, defaultVlan);
90             return logicalSwitchAddedJob.call();
91         }
92
93         return futures;
94     }
95
96     private ListenableFuture<Void> createLogicalSwitch(L2GatewayDevice l2GatewayDevice, ElanInstance elanInstance) {
97         final String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(
98                 elanInstance.getElanInstanceName());
99         String segmentationId = ElanUtils.getVxlanSegmentationId(elanInstance).toString();
100
101         LOG.trace("logical switch {} is created on {} with VNI {}", logicalSwitchName,
102                 l2GatewayDevice.getHwvtepNodeId(), segmentationId);
103         NodeId hwvtepNodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
104         InstanceIdentifier<LogicalSwitches> path = HwvtepSouthboundUtils
105                 .createLogicalSwitchesInstanceIdentifier(hwvtepNodeId, new HwvtepNodeName(logicalSwitchName));
106         LogicalSwitches logicalSwitch = HwvtepSouthboundUtils.createLogicalSwitch(logicalSwitchName,
107                 elanInstance.getDescription(), segmentationId);
108
109         ListenableFuture<Void> lsCreateFuture = HwvtepUtils.addLogicalSwitch(broker, hwvtepNodeId, logicalSwitch);
110         Futures.addCallback(lsCreateFuture, new FutureCallback<Void>() {
111             @Override
112             public void onSuccess(Void noarg) {
113                 // Listener will be closed after all configuration completed
114                 // on hwvtep by
115                 // listener itself
116                 LOG.trace("Successful in initiating logical switch {} creation", logicalSwitchName);
117             }
118
119             @Override
120             public void onFailure(Throwable error) {
121                 LOG.error("Failed logical switch {} creation", logicalSwitchName, error);
122             }
123         });
124         return lsCreateFuture;
125     }
126 }