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
9 package org.opendaylight.vpnservice.elan.l2gw.utils;
11 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.elanmanager.utils.ElanL2GwCacheUtils;
16 import org.opendaylight.vpnservice.datastoreutils.DataStoreJobCoordinator;
17 import org.opendaylight.vpnservice.elan.internal.ElanInstanceManager;
18 import org.opendaylight.vpnservice.elan.l2gw.jobs.AssociateHwvtepToElanJob;
19 import org.opendaylight.vpnservice.elan.l2gw.jobs.DisAssociateHwvtepFromElanJob;
20 import org.opendaylight.vpnservice.elan.l2gw.listeners.HwvtepLogicalSwitchListener;
21 import org.opendaylight.vpnservice.elan.utils.ElanClusterUtils;
22 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
23 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.L2GatewayDevice;
24 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
25 import org.opendaylight.vpnservice.utils.hwvtep.HwvtepUtils;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gatewayKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
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.opendaylight.vpnservice.elan.rev150602.elan.instances.ElanInstance;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.google.common.base.Optional;
43 public class L2GatewayConnectionUtils {
44 private static final Logger LOG = LoggerFactory.getLogger(L2GatewayConnectionUtils.class);
46 private static DataBroker broker;
47 private static ElanInstanceManager elanInstanceManager;
49 static DataStoreJobCoordinator dataStoreJobCoordinator;
51 public static void setDataStoreJobCoordinator(DataStoreJobCoordinator ds) {
52 dataStoreJobCoordinator = ds;
55 public static void setBroker(DataBroker broker) {
56 L2GatewayConnectionUtils.broker = broker;
59 public static void setElanInstanceManager(ElanInstanceManager elanInstanceManager) {
60 L2GatewayConnectionUtils.elanInstanceManager = elanInstanceManager;
63 public static boolean isGatewayAssociatedToL2Device(L2GatewayDevice l2GwDevice) {
64 return (l2GwDevice.getL2GatewayIds().size() > 0);
67 public static L2gateway getNeutronL2gateway(DataBroker broker, Uuid l2GatewayId) {
68 LOG.debug("getNeutronL2gateway for {}", l2GatewayId.getValue());
69 InstanceIdentifier<L2gateway> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class)
70 .child(L2gateway.class, new L2gatewayKey(l2GatewayId));
71 Optional<L2gateway> l2Gateway = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
72 if (l2Gateway.isPresent()) {
73 return l2Gateway.get();
78 public static List<L2gateway> getL2gatewayList(DataBroker broker) {
79 InstanceIdentifier<L2gateways> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class);
80 Optional<L2gateways> l2gateways = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
82 if (l2gateways.isPresent()) {
83 return l2gateways.get().getL2gateway();
88 public static List<L2gatewayConnection> getAllL2gatewayConnections(DataBroker broker) {
89 InstanceIdentifier<L2gatewayConnections> inst = InstanceIdentifier.create(Neutron.class)
90 .child(L2gatewayConnections.class);
91 Optional<L2gatewayConnections> l2GwConns = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
92 if (l2GwConns.isPresent()) {
93 return l2GwConns.get().getL2gatewayConnection();
98 public static void addL2GatewayConnection(L2gatewayConnection input) {
99 addL2GatewayConnection(input, null/*deviceName*/);
102 public static void addL2GatewayConnection(L2gatewayConnection input, String l2GwDeviceName) {
103 LOG.info("Adding L2gateway Connection with ID: {}", input.getKey().getUuid());
105 Uuid networkUuid = input.getNetworkId();
106 ElanInstance elanInstance = elanInstanceManager.getElanInstanceByName(networkUuid.getValue());
107 if (elanInstance == null || elanInstance.getVni() == null) {
108 LOG.error("Neutron network with id {} is not present", networkUuid.getValue());
110 Uuid l2GatewayId = input.getL2gatewayId();
111 L2gateway l2Gateway = getNeutronL2gateway(broker, l2GatewayId);
112 if (l2Gateway == null) {
113 LOG.error("L2Gateway with id {} is not present", l2GatewayId.getValue());
115 associateHwvtepsToElan(elanInstance, l2Gateway, input, l2GwDeviceName);
120 public static void deleteL2GatewayConnection(L2gatewayConnection input) {
121 LOG.info("Deleting L2gateway Connection with ID: {}", input.getKey().getUuid());
123 Uuid networkUuid = input.getNetworkId();
124 ElanInstance elanInstance = elanInstanceManager.getElanInstanceByName(networkUuid.getValue());
125 if (elanInstance == null) {
126 LOG.error("Neutron network with id {} is not present", networkUuid.getValue());
128 Uuid l2GatewayId = input.getL2gatewayId();
129 L2gateway l2Gateway = L2GatewayConnectionUtils.getNeutronL2gateway(broker, l2GatewayId);
130 if (l2Gateway == null) {
131 LOG.error("L2Gateway with id {} is not present", l2GatewayId.getValue());
133 disAssociateHwvtepsFromElan(elanInstance, l2Gateway, input);
138 private static void disAssociateHwvtepsFromElan(ElanInstance elanInstance, L2gateway l2Gateway,
139 L2gatewayConnection input) {
140 String elanName = elanInstance.getElanInstanceName();
141 Integer defaultVlan = input.getSegmentId();
142 List<Devices> l2Devices = l2Gateway.getDevices();
143 for (Devices l2Device : l2Devices) {
144 String l2DeviceName = l2Device.getDeviceName();
145 L2GatewayDevice l2GatewayDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
146 if (isL2GwDeviceConnected(l2GatewayDevice)) {//TODO handle delete while device is offline
147 // Delete L2 Gateway device from 'ElanL2GwDevice' cache
148 String hwvtepNodeId = l2GatewayDevice.getHwvtepNodeId();
149 boolean isLastL2GwConnDeleted = false;
150 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
151 if (isLastL2GwConnBeingDeleted(elanL2GwDevice)) {
152 LOG.debug("Elan L2Gw Conn cache removed for id {}", hwvtepNodeId);
153 ElanL2GwCacheUtils.removeL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
154 isLastL2GwConnDeleted = true;
156 Uuid l2GwConnId = input.getKey().getUuid();
157 LOG.debug("Elan L2Gw Conn cache with id {} is being referred by other L2Gw Conns; so only " +
158 "L2 Gw Conn {} reference is removed", hwvtepNodeId, l2GwConnId);
159 elanL2GwDevice.removeL2GatewayId(l2GwConnId);
162 DisAssociateHwvtepFromElanJob disAssociateHwvtepToElanJob = new DisAssociateHwvtepFromElanJob(broker,
163 elanL2GwDevice, elanInstance, l2Device, defaultVlan, isLastL2GwConnDeleted);
164 ElanClusterUtils.runOnlyInLeaderNode(disAssociateHwvtepToElanJob.getJobKey(),
165 "remove l2gw connection job ",
166 disAssociateHwvtepToElanJob);
168 LOG.info("L2GwConn delete is not handled for device with id {} as it's not connected", l2DeviceName);
173 private static void associateHwvtepsToElan(ElanInstance elanInstance,
174 L2gateway l2Gateway, L2gatewayConnection input, String l2GwDeviceName) {
175 String elanName = elanInstance.getElanInstanceName();
176 Integer defaultVlan = input.getSegmentId();
177 Uuid l2GwConnId = input.getKey().getUuid();
178 List<Devices> l2Devices = l2Gateway.getDevices();
180 if (LOG.isTraceEnabled()) {
181 LOG.trace("Associating ELAN {} with L2Gw Conn Id {} having below L2Gw devices {}", elanName, l2GwConnId,
185 for (Devices l2Device : l2Devices) {
186 String l2DeviceName = l2Device.getDeviceName();
187 // L2gateway can have more than one L2 Gw devices. Configure Logical Switch, VLAN mappings,...
188 // only on the switch which has come up just now and exclude all other devices from
189 // preprovisioning/re-provisioning
190 if (l2GwDeviceName != null && !l2GwDeviceName.equals(l2DeviceName)) {
191 LOG.debug("Associating Hwvtep to ELAN is not been processed for {}; as only {} got connected now!",
192 l2DeviceName, l2GwDeviceName);
195 L2GatewayDevice l2GatewayDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
196 if (isL2GwDeviceConnected(l2GatewayDevice)) {
197 NodeId hwvtepNodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
199 // Delete pending delete logical switch task if scheduled
200 ElanL2GatewayUtils.cancelDeleteLogicalSwitch(hwvtepNodeId,
201 ElanL2GatewayUtils.getLogicalSwitchFromElan(elanName));
203 // Add L2 Gateway device to 'ElanL2GwDevice' cache
204 boolean createLogicalSwitch;
205 LogicalSwitches logicalSwitch = HwvtepUtils.getLogicalSwitch(broker, LogicalDatastoreType.OPERATIONAL,
206 hwvtepNodeId, elanName);
207 if (logicalSwitch == null) {
208 HwvtepLogicalSwitchListener hwVTEPLogicalSwitchListener = new HwvtepLogicalSwitchListener(
209 l2GatewayDevice, elanName, l2Device, defaultVlan, l2GwConnId);
210 hwVTEPLogicalSwitchListener.registerListener(LogicalDatastoreType.OPERATIONAL, broker);
211 createLogicalSwitch = true;
213 addL2DeviceToElanL2GwCache(elanName, l2GatewayDevice, l2GwConnId);
214 createLogicalSwitch = false;
216 AssociateHwvtepToElanJob associateHwvtepToElanJob = new AssociateHwvtepToElanJob(broker,
217 l2GatewayDevice, elanInstance, l2Device, defaultVlan, createLogicalSwitch);
219 ElanClusterUtils.runOnlyInLeaderNode( associateHwvtepToElanJob.getJobKey() ,
220 "create logical switch in hwvtep topo",
221 associateHwvtepToElanJob);
224 LOG.info("L2GwConn create is not handled for device with id {} as it's not connected", l2DeviceName);
229 public static L2GatewayDevice addL2DeviceToElanL2GwCache(String elanName, L2GatewayDevice l2GatewayDevice,
231 String l2gwDeviceNodeId = l2GatewayDevice.getHwvtepNodeId();
232 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, l2gwDeviceNodeId);
233 if (elanL2GwDevice == null) {
234 elanL2GwDevice = new L2GatewayDevice();
235 elanL2GwDevice.setHwvtepNodeId(l2gwDeviceNodeId);
236 elanL2GwDevice.setDeviceName(l2GatewayDevice.getDeviceName());
237 elanL2GwDevice.setTunnelIps(l2GatewayDevice.getTunnelIps());
238 ElanL2GwCacheUtils.addL2GatewayDeviceToCache(elanName, elanL2GwDevice);
239 LOG.debug("Elan L2GwConn cache created for hwvtep id {}", l2gwDeviceNodeId);
241 LOG.debug("Elan L2GwConn cache already exists for hwvtep id {}; updating L2GwConn id {} to it",
242 l2gwDeviceNodeId, l2GwConnId);
244 elanL2GwDevice.addL2GatewayId(l2GwConnId);
246 if (LOG.isTraceEnabled()) {
247 LOG.trace("Elan L2GwConn cache updated with below details: {}", elanL2GwDevice);
249 return elanL2GwDevice;
252 private static boolean isL2GwDeviceConnected(L2GatewayDevice l2GwDevice) {
253 return (l2GwDevice != null && l2GwDevice.getHwvtepNodeId() != null && l2GwDevice.isConnected());
256 protected static boolean isLastL2GwConnBeingDeleted(L2GatewayDevice l2GwDevice) {
257 return (l2GwDevice.getL2GatewayIds().size() == 1);