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