8529a5016070839dcfcf6505584b8a6e1f59978d
[netvirt.git] / 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 com.google.common.util.concurrent.MoreExecutors;
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.cache.ElanInstanceCache;
20 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
21 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
22 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
23 import org.opendaylight.netvirt.elan.l2gw.utils.ElanRefUtil;
24 import org.opendaylight.netvirt.elan.utils.ElanUtils;
25 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
26 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35 * Created by ekvsver on 4/15/2016.
36 */
37 public class AssociateHwvtepToElanJob implements Callable<List<ListenableFuture<Void>>> {
38     private static final Logger LOG = LoggerFactory.getLogger(AssociateHwvtepToElanJob.class);
39
40     private final DataBroker broker;
41     private final ElanL2GatewayUtils elanL2GatewayUtils;
42     private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
43     private final ElanInstanceCache elanInstanceCache;
44     private final L2GatewayDevice l2GatewayDevice;
45     private final ElanInstance elanInstance;
46     private final Devices l2Device;
47     private final Integer defaultVlan;
48     private final ElanRefUtil elanRefUtil;
49
50     public AssociateHwvtepToElanJob(DataBroker broker, ElanL2GatewayUtils elanL2GatewayUtils,
51             ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils, ElanInstanceCache elanInstanceCache,
52             L2GatewayDevice l2GatewayDevice, ElanInstance elanInstance, Devices l2Device, Integer defaultVlan,
53             ElanRefUtil elanRefUtil) {
54         this.broker = broker;
55         this.elanL2GatewayUtils = elanL2GatewayUtils;
56         this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
57         this.elanInstanceCache = elanInstanceCache;
58         this.l2GatewayDevice = l2GatewayDevice;
59         this.elanInstance = elanInstance;
60         this.l2Device = l2Device;
61         this.defaultVlan = defaultVlan;
62         this.elanRefUtil = elanRefUtil;
63         LOG.debug("created assosiate l2gw connection job for {} {} ", elanInstance.getElanInstanceName(),
64                 l2GatewayDevice.getHwvtepNodeId());
65     }
66
67     public String getJobKey() {
68         return elanInstance.getElanInstanceName() + HwvtepHAUtil.L2GW_JOB_KEY;
69     }
70
71     @Override
72     public List<ListenableFuture<Void>> call() {
73         String hwvtepNodeId = l2GatewayDevice.getHwvtepNodeId();
74         String elanInstanceName = elanInstance.getElanInstanceName();
75         LOG.debug("running assosiate l2gw connection job for {} {} ", elanInstanceName, hwvtepNodeId);
76
77         elanL2GatewayUtils.cancelDeleteLogicalSwitch(new NodeId(hwvtepNodeId),
78                 ElanL2GatewayUtils.getLogicalSwitchFromElan(elanInstanceName));
79
80         // Create Logical Switch if it's not created already in the device
81         LOG.info("creating logical switch {} for {} ", elanInstanceName, hwvtepNodeId);
82         createLogicalSwitch();
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(elanL2GatewayUtils, elanL2GatewayMulticastUtils,
88                         logicalSwitchName, l2Device, l2GatewayDevice, defaultVlan, elanRefUtil, broker);
89         return logicalSwitchAddedJob.call();
90     }
91
92     private ListenableFuture<Void> createLogicalSwitch() {
93         final String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(
94                 elanInstance.getElanInstanceName());
95         String segmentationId = ElanUtils.getVxlanSegmentationId(elanInstance).toString();
96         String replicationMode = "";
97
98         LOG.trace("logical switch {} is created on {} with VNI {}", logicalSwitchName,
99                 l2GatewayDevice.getHwvtepNodeId(), segmentationId);
100         NodeId hwvtepNodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
101         String dbVersion = HwvtepUtils.getDbVersion(broker,hwvtepNodeId);
102         if (SouthboundUtils.compareDbVersionToMinVersion(dbVersion, "1.6.0")) {
103             replicationMode = "source_node";
104         }
105
106         LOG.trace("logical switch {} has schema version {}, replication mode set to {}", logicalSwitchName,
107                 dbVersion, replicationMode);
108
109         LogicalSwitches logicalSwitch = HwvtepSouthboundUtils.createLogicalSwitch(logicalSwitchName,
110                 elanInstance.getDescription(), segmentationId, replicationMode);
111
112         ListenableFuture<Void> lsCreateFuture = HwvtepUtils.addLogicalSwitch(broker, hwvtepNodeId, logicalSwitch);
113         Futures.addCallback(lsCreateFuture, new FutureCallback<Void>() {
114             @Override
115             public void onSuccess(Void noarg) {
116                 // Listener will be closed after all configuration completed
117                 // on hwvtep by
118                 // listener itself
119                 LOG.trace("Successful in initiating logical switch {} creation", logicalSwitchName);
120             }
121
122             @Override
123             public void onFailure(Throwable error) {
124                 LOG.error("Failed logical switch {} creation", logicalSwitchName, error);
125             }
126         }, MoreExecutors.directExecutor());
127         return lsCreateFuture;
128     }
129 }