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