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