2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.l2gw.jobs;
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;
38 * Created by ekvsver on 4/15/2016.
40 public class AssociateHwvtepToElanJob implements Callable<List<? extends ListenableFuture<?>>> {
41 private static final Logger LOG = LoggerFactory.getLogger(AssociateHwvtepToElanJob.class);
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;
53 public AssociateHwvtepToElanJob(DataBroker broker, ElanL2GatewayUtils elanL2GatewayUtils,
54 ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils, ElanInstanceCache elanInstanceCache,
55 L2GatewayDevice l2GatewayDevice, ElanInstance elanInstance, Devices l2Device, Integer defaultVlan,
56 ElanRefUtil elanRefUtil) {
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());
70 public String getJobKey() {
71 return elanInstance.getElanInstanceName() + HwvtepHAUtil.L2GW_JOB_KEY;
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);
80 elanL2GatewayUtils.cancelDeleteLogicalSwitch(new NodeId(hwvtepNodeId),
81 ElanL2GatewayUtils.getLogicalSwitchFromElan(elanInstanceName));
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);
89 LogicalSwitchAddedJob logicalSwitchAddedJob =
90 new LogicalSwitchAddedJob(elanL2GatewayUtils, elanL2GatewayMulticastUtils,
91 logicalSwitchName, l2Device, l2GatewayDevice, defaultVlan, elanRefUtil, broker);
92 return logicalSwitchAddedJob.call();
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 = "";
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;
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);
110 if (SouthboundUtils.compareDbVersionToMinVersion(dbVersion, "1.6.0")) {
111 replicationMode = "source_node";
114 LOG.trace("logical switch {} has schema version {}, replication mode set to {}", logicalSwitchName,
115 dbVersion, replicationMode);
117 LogicalSwitches logicalSwitch = HwvtepSouthboundUtils.createLogicalSwitch(logicalSwitchName,
118 elanInstance.getDescription(), segmentationId, replicationMode);
120 FluentFuture<? extends @NonNull CommitInfo> lsCreateFuture = HwvtepUtils.addLogicalSwitch(broker, hwvtepNodeId,
122 lsCreateFuture.addCallback(new FutureCallback<CommitInfo>() {
124 public void onSuccess(CommitInfo noarg) {
125 // Listener will be closed after all configuration completed
128 LOG.trace("Successful in initiating logical switch {} creation", logicalSwitchName);
132 public void onFailure(Throwable error) {
133 LOG.error("Failed logical switch {} creation", logicalSwitchName, error);
135 }, MoreExecutors.directExecutor());
136 return lsCreateFuture;