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.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.ArrayList;
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.l2gw.utils.ElanL2GatewayMulticastUtils;
20 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
21 import org.opendaylight.netvirt.elan.utils.ElanUtils;
22 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Created by ekvsver on 4/15/2016.
35 public class AssociateHwvtepToElanJob implements Callable<List<ListenableFuture<Void>>> {
36 private static final Logger LOG = LoggerFactory.getLogger(AssociateHwvtepToElanJob.class);
38 private final DataBroker broker;
39 private final ElanL2GatewayUtils elanL2GatewayUtils;
40 private final ElanUtils elanUtils;
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;
48 public AssociateHwvtepToElanJob(DataBroker broker, ElanL2GatewayUtils elanL2GatewayUtils, ElanUtils elanUtils,
49 ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
50 L2GatewayDevice l2GatewayDevice, ElanInstance elanInstance, Devices l2Device,
51 Integer defaultVlan, boolean createLogicalSwitch) {
53 this.elanL2GatewayUtils = elanL2GatewayUtils;
54 this.elanUtils = elanUtils;
55 this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
56 this.l2GatewayDevice = l2GatewayDevice;
57 this.elanInstance = elanInstance;
58 this.l2Device = l2Device;
59 this.defaultVlan = defaultVlan;
60 this.createLogicalSwitch = createLogicalSwitch;
61 LOG.debug("created assosiate l2gw connection job for {} {} ", elanInstance.getElanInstanceName(),
62 l2GatewayDevice.getHwvtepNodeId());
65 public String getJobKey() {
66 return elanInstance.getElanInstanceName();
70 public List<ListenableFuture<Void>> call() throws Exception {
71 List<ListenableFuture<Void>> futures = new ArrayList<>();
72 String hwvtepNodeId = l2GatewayDevice.getHwvtepNodeId();
73 String elanInstanceName = elanInstance.getElanInstanceName();
74 LOG.debug("running assosiate l2gw connection job for {} {} ", elanInstanceName, hwvtepNodeId);
76 // Create Logical Switch if it's not created already in the device
77 if (createLogicalSwitch) {
78 LOG.info("creating logical switch {} for {} ", elanInstanceName, hwvtepNodeId);
80 ListenableFuture<Void> lsCreateFuture = createLogicalSwitch(l2GatewayDevice, elanInstance);
81 futures.add(lsCreateFuture);
83 String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(elanInstanceName);
84 LOG.info("{} is already created in {}; adding remaining configurations", logicalSwitchName, hwvtepNodeId);
86 LogicalSwitchAddedJob logicalSwitchAddedJob =
87 new LogicalSwitchAddedJob(broker, elanL2GatewayUtils, elanUtils, elanL2GatewayMulticastUtils,
88 logicalSwitchName, l2Device,
89 l2GatewayDevice, defaultVlan);
90 return logicalSwitchAddedJob.call();
96 private ListenableFuture<Void> createLogicalSwitch(L2GatewayDevice l2GatewayDevice, ElanInstance elanInstance) {
97 final String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(
98 elanInstance.getElanInstanceName());
99 String segmentationId = elanInstance.getSegmentationId().toString();
101 if (LOG.isTraceEnabled()) {
102 LOG.trace("logical switch {} is created on {} with VNI {}", logicalSwitchName,
103 l2GatewayDevice.getHwvtepNodeId(), segmentationId);
105 NodeId hwvtepNodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
106 InstanceIdentifier<LogicalSwitches> path = HwvtepSouthboundUtils
107 .createLogicalSwitchesInstanceIdentifier(hwvtepNodeId, new HwvtepNodeName(logicalSwitchName));
108 LogicalSwitches logicalSwitch = HwvtepSouthboundUtils.createLogicalSwitch(logicalSwitchName,
109 elanInstance.getDescription(), segmentationId);
111 ListenableFuture<Void> lsCreateFuture = HwvtepUtils.addLogicalSwitch(broker, hwvtepNodeId, logicalSwitch);
112 Futures.addCallback(lsCreateFuture, new FutureCallback<Void>() {
114 public void onSuccess(Void noarg) {
115 // Listener will be closed after all configuration completed
118 if (LOG.isTraceEnabled()) {
119 LOG.trace("Successful in initiating logical switch {} creation", logicalSwitchName);
124 public void onFailure(Throwable error) {
125 LOG.error("Failed logical switch {} creation", logicalSwitchName, error);
128 return lsCreateFuture;