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