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.netvirt.elan.l2gw.utils;
11 import java.util.ArrayList;
12 import java.util.List;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.mdsalutil.MDSALUtil;
18 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
19 import org.opendaylight.netvirt.elan.internal.ElanInstanceManager;
20 import org.opendaylight.netvirt.elan.l2gw.jobs.AssociateHwvtepToElanJob;
21 import org.opendaylight.netvirt.elan.l2gw.jobs.DisAssociateHwvtepFromElanJob;
22 import org.opendaylight.netvirt.elan.l2gw.listeners.HwvtepLogicalSwitchListener;
23 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
24 import org.opendaylight.netvirt.elan.utils.ElanUtils;
25 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
26 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
27 import org.opendaylight.netvirt.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gatewayKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class L2GatewayConnectionUtils {
44 private static final Logger LOG = LoggerFactory.getLogger(L2GatewayConnectionUtils.class);
46 private final DataBroker broker;
47 private final ElanInstanceManager elanInstanceManager;
48 private final ElanL2GatewayUtils elanL2GatewayUtils;
49 private final EntityOwnershipService entityOwnershipService;
50 private final ElanUtils elanUtils;
51 private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
53 public L2GatewayConnectionUtils(DataBroker dataBroker, ElanInstanceManager elanInstanceManager,
54 EntityOwnershipService entityOwnershipService, ElanUtils elanUtils) {
55 this.broker = dataBroker;
56 this.elanInstanceManager = elanInstanceManager;
57 this.elanL2GatewayUtils = elanUtils.getElanL2GatewayUtils();
58 this.entityOwnershipService = entityOwnershipService;
59 this.elanUtils = elanUtils;
60 this.elanL2GatewayMulticastUtils = elanUtils.getElanL2GatewayMulticastUtils();
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 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).orNull();
74 public static List<L2gateway> getL2gatewayList(DataBroker broker) {
75 InstanceIdentifier<L2gateways> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class);
76 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).transform(
77 L2gateways::getL2gateway).orNull();
80 public static List<L2gatewayConnection> getAllL2gatewayConnections(DataBroker broker) {
81 InstanceIdentifier<L2gatewayConnections> inst = InstanceIdentifier.create(Neutron.class)
82 .child(L2gatewayConnections.class);
83 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).transform(
84 L2gatewayConnections::getL2gatewayConnection).orNull();
88 * Gets the associated l2 gw connections.
94 * @return the associated l2 gw connections
96 public static List<L2gatewayConnection> getAssociatedL2GwConnections(DataBroker broker, Set<Uuid> l2GatewayIds) {
97 List<L2gatewayConnection> l2GwConnections = null;
98 List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
99 if (allL2GwConns != null) {
100 l2GwConnections = new ArrayList<>();
101 for (Uuid l2GatewayId : l2GatewayIds) {
102 for (L2gatewayConnection l2GwConn : allL2GwConns) {
103 if (l2GwConn.getL2gatewayId().equals(l2GatewayId)) {
104 l2GwConnections.add(l2GwConn);
109 return l2GwConnections;
113 * Gets the associated l2 gw connections.
119 * @return the associated l2 gw connection with elan
121 public static List<L2gatewayConnection> getL2GwConnectionsByElanName(DataBroker broker, String elanName) {
122 List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
123 List<L2gatewayConnection> elanL2GateWayConnections = new ArrayList<>();
124 if (allL2GwConns != null) {
125 for (L2gatewayConnection l2GwConn : allL2GwConns) {
126 if (l2GwConn.getNetworkId().getValue().equalsIgnoreCase(elanName)) {
127 elanL2GateWayConnections.add(l2GwConn);
131 return elanL2GateWayConnections;
134 public void addL2GatewayConnection(L2gatewayConnection input) {
135 addL2GatewayConnection(input, null/*deviceName*/);
138 public void addL2GatewayConnection(L2gatewayConnection input, String l2GwDeviceName) {
139 LOG.info("Adding L2gateway Connection with ID: {}", input.getKey().getUuid());
141 Uuid networkUuid = input.getNetworkId();
142 ElanInstance elanInstance = elanInstanceManager.getElanInstanceByName(networkUuid.getValue());
143 if (elanInstance == null || !ElanUtils.isVxlan(elanInstance)) {
144 LOG.error("Neutron network with id {} is not present", networkUuid.getValue());
146 Uuid l2GatewayId = input.getL2gatewayId();
147 L2gateway l2Gateway = getNeutronL2gateway(broker, l2GatewayId);
148 if (l2Gateway == null) {
149 LOG.error("L2Gateway with id {} is not present", l2GatewayId.getValue());
151 associateHwvtepsToElan(elanInstance, l2Gateway, input, l2GwDeviceName);
156 public void deleteL2GatewayConnection(L2gatewayConnection input) {
157 LOG.info("Deleting L2gateway Connection with ID: {}", input.getKey().getUuid());
159 Uuid networkUuid = input.getNetworkId();
160 String elanName = networkUuid.getValue();
161 Uuid l2GatewayId = input.getL2gatewayId();
162 disAssociateHwvtepsFromElan(elanName, input);
165 private void disAssociateHwvtepsFromElan(String elanName, L2gatewayConnection input) {
166 Integer defaultVlan = input.getSegmentId();
167 List<L2GatewayDevice> l2Devices = ElanL2GwCacheUtils.getAllElanDevicesFromCache();
168 List<Devices> l2gwDevicesToBeDeleted = new ArrayList<>();
169 for (L2GatewayDevice elanL2gwDevice : l2Devices) {
170 if (elanL2gwDevice.getL2GatewayIds().contains(input.getKey().getUuid())) {
171 l2gwDevicesToBeDeleted.addAll(elanL2gwDevice.getL2gwConnectionIdToDevices()
172 .get(input.getKey().getUuid()));
175 for (Devices l2Device : l2gwDevicesToBeDeleted) {
176 String l2DeviceName = l2Device.getDeviceName();
177 L2GatewayDevice l2GatewayDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
178 String hwvtepNodeId = l2GatewayDevice.getHwvtepNodeId();
179 boolean isLastL2GwConnDeleted = false;
180 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
181 if (isLastL2GwConnBeingDeleted(elanL2GwDevice)) {
182 // Delete L2 Gateway device from 'ElanL2GwDevice' cache
183 LOG.debug("Elan L2Gw Conn cache removed for id {}", hwvtepNodeId);
184 ElanL2GwCacheUtils.removeL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
185 isLastL2GwConnDeleted = true;
187 Uuid l2GwConnId = input.getKey().getUuid();
188 LOG.debug("Elan L2Gw Conn cache with id {} is being referred by other L2Gw Conns; so only "
189 + "L2 Gw Conn {} reference is removed", hwvtepNodeId, l2GwConnId);
190 elanL2GwDevice.removeL2GatewayId(l2GwConnId);
193 DisAssociateHwvtepFromElanJob disAssociateHwvtepToElanJob =
194 new DisAssociateHwvtepFromElanJob(broker, elanL2GatewayUtils, elanL2GatewayMulticastUtils,
195 elanL2GwDevice, elanName,
196 l2Device, defaultVlan, isLastL2GwConnDeleted);
197 ElanClusterUtils.runOnlyInLeaderNode(entityOwnershipService, disAssociateHwvtepToElanJob.getJobKey(),
198 "remove l2gw connection job ", disAssociateHwvtepToElanJob);
202 private void associateHwvtepsToElan(ElanInstance elanInstance,
203 L2gateway l2Gateway, L2gatewayConnection input, String l2GwDeviceName) {
204 String elanName = elanInstance.getElanInstanceName();
205 Integer defaultVlan = input.getSegmentId();
206 Uuid l2GwConnId = input.getKey().getUuid();
207 List<Devices> l2Devices = l2Gateway.getDevices();
209 LOG.trace("Associating ELAN {} with L2Gw Conn Id {} having below L2Gw devices {}", elanName, l2GwConnId,
212 for (Devices l2Device : l2Devices) {
213 String l2DeviceName = l2Device.getDeviceName();
214 // L2gateway can have more than one L2 Gw devices. Configure Logical Switch, VLAN mappings,...
215 // only on the switch which has come up just now and exclude all other devices from
216 // preprovisioning/re-provisioning
217 if (l2GwDeviceName != null && !l2GwDeviceName.equals(l2DeviceName)) {
218 LOG.debug("Associating Hwvtep to ELAN is not been processed for {}; as only {} got connected now!",
219 l2DeviceName, l2GwDeviceName);
222 L2GatewayDevice l2GatewayDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
223 if (isL2GwDeviceConnected(l2GatewayDevice)) {
224 NodeId hwvtepNodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
226 // Delete pending delete logical switch task if scheduled
227 elanL2GatewayUtils.cancelDeleteLogicalSwitch(hwvtepNodeId,
228 ElanL2GatewayUtils.getLogicalSwitchFromElan(elanName));
230 // Add L2 Gateway device to 'ElanL2GwDevice' cache
231 boolean createLogicalSwitch;
232 LogicalSwitches logicalSwitch = HwvtepUtils.getLogicalSwitch(broker, LogicalDatastoreType.OPERATIONAL,
233 hwvtepNodeId, elanName);
234 if (logicalSwitch == null) {
235 HwvtepLogicalSwitchListener hwVTEPLogicalSwitchListener = new HwvtepLogicalSwitchListener(broker,
236 elanL2GatewayUtils, entityOwnershipService, elanUtils, elanL2GatewayMulticastUtils,
237 l2GatewayDevice, elanName, l2Device, defaultVlan, l2GwConnId);
238 hwVTEPLogicalSwitchListener.registerListener(LogicalDatastoreType.OPERATIONAL, broker);
239 createLogicalSwitch = true;
241 addL2DeviceToElanL2GwCache(elanName, l2GatewayDevice, l2GwConnId, l2Device);
242 createLogicalSwitch = false;
244 AssociateHwvtepToElanJob associateHwvtepToElanJob = new AssociateHwvtepToElanJob(broker,
245 elanL2GatewayUtils, elanUtils, elanL2GatewayMulticastUtils, l2GatewayDevice, elanInstance,
246 l2Device, defaultVlan, createLogicalSwitch);
248 ElanClusterUtils.runOnlyInLeaderNode(entityOwnershipService, associateHwvtepToElanJob.getJobKey() ,
249 "create logical switch in hwvtep topo",
250 associateHwvtepToElanJob);
253 LOG.info("L2GwConn create is not handled for device with id {} as it's not connected", l2DeviceName);
258 public static L2GatewayDevice addL2DeviceToElanL2GwCache(String elanName, L2GatewayDevice l2GatewayDevice,
259 Uuid l2GwConnId, Devices l2Device) {
260 String l2gwDeviceNodeId = l2GatewayDevice.getHwvtepNodeId();
261 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, l2gwDeviceNodeId);
262 if (elanL2GwDevice == null) {
263 elanL2GwDevice = new L2GatewayDevice();
264 elanL2GwDevice.setHwvtepNodeId(l2gwDeviceNodeId);
265 elanL2GwDevice.setDeviceName(l2GatewayDevice.getDeviceName());
266 elanL2GwDevice.setTunnelIps(l2GatewayDevice.getTunnelIps());
267 ElanL2GwCacheUtils.addL2GatewayDeviceToCache(elanName, elanL2GwDevice);
268 LOG.debug("Elan L2GwConn cache created for hwvtep id {}", l2gwDeviceNodeId);
270 LOG.debug("Elan L2GwConn cache already exists for hwvtep id {}; updating L2GwConn id {} to it",
271 l2gwDeviceNodeId, l2GwConnId);
273 elanL2GwDevice.addL2GatewayId(l2GwConnId);
274 if (elanL2GwDevice.getL2gwConnectionIdToDevices().get(l2GwConnId) == null) {
275 elanL2GwDevice.getL2gwConnectionIdToDevices().put(l2GwConnId, new ArrayList<Devices>());
277 elanL2GwDevice.getL2gwConnectionIdToDevices().get(l2GwConnId).add(l2Device);
279 LOG.trace("Elan L2GwConn cache updated with below details: {}", elanL2GwDevice);
280 return elanL2GwDevice;
283 private static boolean isL2GwDeviceConnected(L2GatewayDevice l2GwDevice) {
284 return l2GwDevice != null && l2GwDevice.getHwvtepNodeId() != null && l2GwDevice.isConnected();
287 protected static boolean isLastL2GwConnBeingDeleted(L2GatewayDevice l2GwDevice) {
288 return l2GwDevice.getL2GatewayIds().size() == 1;