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 static java.util.Collections.emptyList;
12 import static org.opendaylight.netvirt.elan.utils.ElanUtils.isVxlanNetworkOrVxlanSegment;
14 import com.google.common.base.Optional;
15 import com.google.common.collect.Lists;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import com.google.common.util.concurrent.SettableFuture;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Objects;
23 import java.util.concurrent.CopyOnWriteArrayList;
24 import javax.annotation.Nonnull;
25 import javax.annotation.Nullable;
26 import javax.annotation.PreDestroy;
27 import javax.inject.Inject;
28 import javax.inject.Singleton;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.genius.mdsalutil.MDSALUtil;
32 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
33 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
34 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
35 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
36 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
37 import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpClusteredListener;
38 import org.opendaylight.netvirt.elan.l2gw.jobs.AssociateHwvtepToElanJob;
39 import org.opendaylight.netvirt.elan.l2gw.jobs.DisAssociateHwvtepFromElanJob;
40 import org.opendaylight.netvirt.elan.l2gw.listeners.HwvtepLogicalSwitchListener;
41 import org.opendaylight.netvirt.elan.l2gw.listeners.LocalUcastMacListener;
42 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
43 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
44 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
45 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gatewayKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
65 public class L2GatewayConnectionUtils implements AutoCloseable {
66 private static final Logger LOG = LoggerFactory.getLogger(L2GatewayConnectionUtils.class);
68 private final DataBroker broker;
69 private final ElanL2GatewayUtils elanL2GatewayUtils;
70 private final ElanClusterUtils elanClusterUtils;
71 private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
72 private final JobCoordinator jobCoordinator;
73 private final L2GatewayCache l2GatewayCache;
74 private final ElanInstanceCache elanInstanceCache;
75 private final List<AutoCloseable> closeables = new CopyOnWriteArrayList<>();
76 private final HwvtepNodeHACache hwvtepNodeHACache;
77 private final HAOpClusteredListener haOpClusteredListener;
80 public L2GatewayConnectionUtils(DataBroker dataBroker,
81 ElanClusterUtils elanClusterUtils, ElanL2GatewayUtils elanL2GatewayUtils,
82 JobCoordinator jobCoordinator, ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
83 L2GatewayCache l2GatewayCache, HAOpClusteredListener haOpClusteredListener,
84 ElanInstanceCache elanInstanceCache, HwvtepNodeHACache hwvtepNodeHACache) {
85 this.broker = dataBroker;
86 this.elanL2GatewayUtils = elanL2GatewayUtils;
87 this.elanClusterUtils = elanClusterUtils;
88 this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
89 this.jobCoordinator = jobCoordinator;
90 this.l2GatewayCache = l2GatewayCache;
91 this.haOpClusteredListener = haOpClusteredListener;
92 this.elanInstanceCache = elanInstanceCache;
93 this.hwvtepNodeHACache = hwvtepNodeHACache;
98 @SuppressWarnings("checkstyle:IllegalCatch")
100 closeables.forEach(c -> {
103 } catch (Exception e) {
104 LOG.warn("Error closing {}", c, e);
109 public static boolean isGatewayAssociatedToL2Device(L2GatewayDevice l2GwDevice) {
110 return !l2GwDevice.getL2GatewayIds().isEmpty();
114 public static L2gateway getNeutronL2gateway(DataBroker broker, Uuid l2GatewayId) {
115 LOG.debug("getNeutronL2gateway for {}", l2GatewayId.getValue());
116 InstanceIdentifier<L2gateway> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class)
117 .child(L2gateway.class, new L2gatewayKey(l2GatewayId));
118 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).orNull();
122 public static List<L2gateway> getL2gatewayList(DataBroker broker) {
123 InstanceIdentifier<L2gateways> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class);
124 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).toJavaUtil().map(
125 L2gateways::getL2gateway).orElse(emptyList());
129 public static List<L2gatewayConnection> getAllL2gatewayConnections(DataBroker broker) {
130 InstanceIdentifier<L2gatewayConnections> inst = InstanceIdentifier.create(Neutron.class)
131 .child(L2gatewayConnections.class);
132 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).toJavaUtil().map(
133 L2gatewayConnections::getL2gatewayConnection).orElse(emptyList());
137 * Gets the associated l2 gw connections.
141 * @param l2GatewayIds
143 * @return the associated l2 gw connections
146 public static List<L2gatewayConnection> getAssociatedL2GwConnections(DataBroker broker, Set<Uuid> l2GatewayIds) {
147 List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
148 List<L2gatewayConnection> l2GwConnections = new ArrayList<>();
149 for (Uuid l2GatewayId : l2GatewayIds) {
150 for (L2gatewayConnection l2GwConn : allL2GwConns) {
151 if (Objects.equals(l2GwConn.getL2gatewayId(), l2GatewayId)) {
152 l2GwConnections.add(l2GwConn);
156 return l2GwConnections;
160 * Gets the associated l2 gw connections.
166 * @return the associated l2 gw connection with elan
169 public static List<L2gatewayConnection> getL2GwConnectionsByElanName(DataBroker broker, String elanName) {
170 List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
171 List<L2gatewayConnection> elanL2GateWayConnections = new ArrayList<>();
172 for (L2gatewayConnection l2GwConn : allL2GwConns) {
173 if (l2GwConn.getNetworkId().getValue().equalsIgnoreCase(elanName)) {
174 elanL2GateWayConnections.add(l2GwConn);
177 return elanL2GateWayConnections;
180 public void addL2GatewayConnection(L2gatewayConnection input) {
181 addL2GatewayConnection(input, null/*deviceName*/, null);
184 public void addL2GatewayConnection(final L2gatewayConnection input,
185 final String l2GwDeviceName) {
186 addL2GatewayConnection(input, l2GwDeviceName, null);
189 public void addL2GatewayConnection(final L2gatewayConnection input,
190 @Nullable final String l2GwDeviceName ,
191 @Nullable L2gateway l2Gateway) {
192 LOG.info("Adding L2gateway Connection with ID: {}", input.key().getUuid());
194 Uuid networkUuid = input.getNetworkId();
196 // Taking cluster reboot scenario , if Elan instance is not available when l2GatewayConnection add events
197 // comes we need to wait for elaninstance to resolve. Hence updating the map with the runnable .
198 // When elanInstance add comes , it look in to the map and run the associated runnable associated with it.
199 ElanInstance elanInstance = elanInstanceCache.get(networkUuid.getValue(),
200 () -> addL2GatewayConnection(input, l2GwDeviceName)).orNull();
201 if (elanInstance == null) {
205 if (!isVxlanNetworkOrVxlanSegment(elanInstance)) {
206 LOG.error("Neutron network with id {} is not VxlanNetwork", networkUuid.getValue());
208 Uuid l2GatewayId = input.getL2gatewayId();
209 if (l2Gateway == null) {
210 l2Gateway = getNeutronL2gateway(broker, l2GatewayId);
212 if (l2Gateway == null) {
213 LOG.error("L2Gateway with id {} is not present", l2GatewayId.getValue());
215 associateHwvtepsToElan(elanInstance, l2Gateway, input, l2GwDeviceName);
220 public void deleteL2GatewayConnection(L2gatewayConnection input) {
221 LOG.info("Deleting L2gateway Connection with ID: {}", input.key().getUuid());
223 Uuid networkUuid = input.getNetworkId();
224 String elanName = networkUuid.getValue();
225 disAssociateHwvtepsFromElan(elanName, input);
228 private void disAssociateHwvtepsFromElan(String elanName, L2gatewayConnection input) {
229 Integer defaultVlan = input.getSegmentId();
230 List<L2GatewayDevice> l2Devices = ElanL2GwCacheUtils.getAllElanDevicesFromCache();
231 List<Devices> l2gwDevicesToBeDeleted = new ArrayList<>();
232 for (L2GatewayDevice elanL2gwDevice : l2Devices) {
233 if (elanL2gwDevice.getL2GatewayIds().contains(input.key().getUuid())) {
234 l2gwDevicesToBeDeleted.addAll(elanL2gwDevice.getDevicesForL2gwConnectionId(input.key().getUuid()));
237 if (l2gwDevicesToBeDeleted.isEmpty()) {
238 //delete logical switch
239 Uuid l2GatewayId = input.getL2gatewayId();
240 L2gateway l2Gateway = L2GatewayConnectionUtils.getNeutronL2gateway(broker, l2GatewayId);
241 if (l2Gateway == null) {
242 LOG.error("Failed to find the l2gateway for the connection {}", input.getUuid());
244 } else if (l2Gateway.getDevices() != null) {
245 l2gwDevicesToBeDeleted.addAll(l2Gateway.getDevices());
248 for (Devices l2Device : l2gwDevicesToBeDeleted) {
249 String l2DeviceName = l2Device.getDeviceName();
250 L2GatewayDevice l2GatewayDevice = l2GatewayCache.get(l2DeviceName);
251 String hwvtepNodeId = l2GatewayDevice.getHwvtepNodeId();
252 boolean isLastL2GwConnDeleted = false;
253 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
254 if (elanL2GwDevice != null && isLastL2GwConnBeingDeleted(elanL2GwDevice)) {
255 // Delete L2 Gateway device from 'ElanL2GwDevice' cache
256 LOG.debug("Elan L2Gw Conn cache removed for id {}", hwvtepNodeId);
257 ElanL2GwCacheUtils.removeL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
258 isLastL2GwConnDeleted = true;
260 Uuid l2GwConnId = input.key().getUuid();
261 LOG.debug("Elan L2Gw Conn cache with id {} is being referred by other L2Gw Conns; so only "
262 + "L2 Gw Conn {} reference is removed", hwvtepNodeId, l2GwConnId);
263 if (elanL2GwDevice != null) {
264 elanL2GwDevice.removeL2GatewayId(l2GwConnId);
266 isLastL2GwConnDeleted = true;
270 DisAssociateHwvtepFromElanJob disAssociateHwvtepToElanJob =
271 new DisAssociateHwvtepFromElanJob(elanL2GatewayUtils, elanL2GatewayMulticastUtils,
272 elanL2GwDevice, elanName,
273 l2Device, defaultVlan, hwvtepNodeId, isLastL2GwConnDeleted);
274 elanClusterUtils.runOnlyInOwnerNode(disAssociateHwvtepToElanJob.getJobKey(), "remove l2gw connection job",
275 disAssociateHwvtepToElanJob);
279 private void associateHwvtepsToElan(ElanInstance elanInstance,
280 L2gateway l2Gateway, L2gatewayConnection input, @Nullable String l2GwDeviceName) {
281 String elanName = elanInstance.getElanInstanceName();
282 Integer defaultVlan = input.getSegmentId();
283 Uuid l2GwConnId = input.key().getUuid();
284 List<Devices> l2Devices = l2Gateway.getDevices();
286 LOG.trace("Associating ELAN {} with L2Gw Conn Id {} having below L2Gw devices {}", elanName, l2GwConnId,
289 if (l2Devices == null) {
293 for (Devices l2Device : l2Devices) {
294 String l2DeviceName = l2Device.getDeviceName();
295 // L2gateway can have more than one L2 Gw devices. Configure Logical Switch, VLAN mappings,...
296 // only on the switch which has come up just now and exclude all other devices from
297 // preprovisioning/re-provisioning
298 if (l2GwDeviceName != null && !l2GwDeviceName.equals(l2DeviceName)) {
299 LOG.debug("Associating Hwvtep to ELAN is not been processed for {}; as only {} got connected now!",
300 l2DeviceName, l2GwDeviceName);
303 L2GatewayDevice l2GatewayDevice = l2GatewayCache.get(l2DeviceName);
304 if (isL2GwDeviceConnected(l2GatewayDevice)) {
305 NodeId hwvtepNodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
307 // Delete pending delete logical switch task if scheduled
308 elanL2GatewayUtils.cancelDeleteLogicalSwitch(hwvtepNodeId,
309 ElanL2GatewayUtils.getLogicalSwitchFromElan(elanName));
311 // Add L2 Gateway device to 'ElanL2GwDevice' cache
312 boolean createLogicalSwitch;
313 LogicalSwitches logicalSwitch = HwvtepUtils.getLogicalSwitch(broker, LogicalDatastoreType.CONFIGURATION,
314 hwvtepNodeId, elanName);
315 if (logicalSwitch == null) {
316 HwvtepLogicalSwitchListener hwVTEPLogicalSwitchListener = new HwvtepLogicalSwitchListener(
317 elanInstanceCache, elanL2GatewayUtils, elanClusterUtils, elanL2GatewayMulticastUtils,
318 this, l2GatewayDevice, elanName, l2Device, defaultVlan, l2GwConnId, hwvtepNodeHACache);
319 hwVTEPLogicalSwitchListener.registerListener(LogicalDatastoreType.OPERATIONAL, broker);
320 closeables.add(hwVTEPLogicalSwitchListener);
321 createLogicalSwitch = true;
323 addL2DeviceToElanL2GwCache(elanName, l2GatewayDevice, l2GwConnId, l2Device);
324 createLogicalSwitch = false;
326 AssociateHwvtepToElanJob associateHwvtepToElanJob = new AssociateHwvtepToElanJob(broker,
327 elanL2GatewayUtils, elanL2GatewayMulticastUtils, elanInstanceCache, l2GatewayDevice,
328 elanInstance, l2Device, defaultVlan, createLogicalSwitch);
330 elanClusterUtils.runOnlyInOwnerNode(associateHwvtepToElanJob.getJobKey(),
331 "create logical switch in hwvtep topo", associateHwvtepToElanJob);
334 LOG.info("L2GwConn create is not handled for device with id {} as it's not connected", l2DeviceName);
339 public L2GatewayDevice addL2DeviceToElanL2GwCache(String elanName, L2GatewayDevice l2GatewayDevice, Uuid l2GwConnId,
341 String l2gwDeviceNodeId = l2GatewayDevice.getHwvtepNodeId();
342 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, l2gwDeviceNodeId);
343 if (elanL2GwDevice == null) {
344 elanL2GwDevice = new L2GatewayDevice(l2GatewayDevice.getDeviceName());
345 elanL2GwDevice.setHwvtepNodeId(l2gwDeviceNodeId);
346 elanL2GwDevice.setTunnelIps(l2GatewayDevice.getTunnelIps());
347 ElanL2GwCacheUtils.addL2GatewayDeviceToCache(elanName, elanL2GwDevice);
348 LOG.debug("Elan L2GwConn cache created for hwvtep id {}", l2gwDeviceNodeId);
350 LOG.debug("Elan L2GwConn cache already exists for hwvtep id {}; updating L2GwConn id {} to it",
351 l2gwDeviceNodeId, l2GwConnId);
353 elanL2GwDevice.addL2GatewayId(l2GwConnId);
354 elanL2GwDevice.addL2gwConnectionIdToDevice(l2GwConnId, l2Device);
356 //incase of cluster reboot scenario southbound device would have added more macs
357 //while odl is down, pull them now
358 readAndCopyLocalUcastMacsToCache(elanName, l2GatewayDevice);
360 LOG.trace("Elan L2GwConn cache updated with below details: {}", elanL2GwDevice);
361 return elanL2GwDevice;
364 private static boolean isL2GwDeviceConnected(L2GatewayDevice l2GwDevice) {
365 return l2GwDevice != null && l2GwDevice.getHwvtepNodeId() != null;
368 protected static boolean isLastL2GwConnBeingDeleted(@Nonnull L2GatewayDevice l2GwDevice) {
369 return l2GwDevice.getL2GatewayIds().size() == 1;
372 private void readAndCopyLocalUcastMacsToCache(final String elanName, final L2GatewayDevice l2GatewayDevice) {
373 final InstanceIdentifier<Node> nodeIid = HwvtepSouthboundUtils.createInstanceIdentifier(
374 new NodeId(l2GatewayDevice.getHwvtepNodeId()));
375 jobCoordinator.enqueueJob(elanName + ":" + l2GatewayDevice.getDeviceName(), () -> {
376 final SettableFuture settableFuture = SettableFuture.create();
377 Futures.addCallback(broker.newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
378 nodeIid), new SettableFutureCallback<Optional<Node>>(settableFuture) {
380 public void onSuccess(@Nonnull Optional<Node> resultNode) {
381 LocalUcastMacListener localUcastMacListener =
382 new LocalUcastMacListener(broker, haOpClusteredListener,
383 elanL2GatewayUtils, jobCoordinator, elanInstanceCache, hwvtepNodeHACache);
384 settableFuture.set(resultNode);
385 Optional<Node> nodeOptional = resultNode;
386 if (nodeOptional.isPresent()) {
387 Node node = nodeOptional.get();
388 if (node.augmentation(HwvtepGlobalAugmentation.class) != null) {
389 List<LocalUcastMacs> localUcastMacs =
390 node.augmentation(HwvtepGlobalAugmentation.class).getLocalUcastMacs();
391 if (localUcastMacs == null) {
394 localUcastMacs.stream()
395 .filter((mac) -> macBelongsToLogicalSwitch(mac, elanName))
397 InstanceIdentifier<LocalUcastMacs> macIid = getMacIid(nodeIid, mac);
398 localUcastMacListener.added(macIid, mac);
403 }, MoreExecutors.directExecutor());
404 return Lists.newArrayList(settableFuture);
409 * Gets the associated l2 gw connections.
411 * @param l2GatewayId the l2 gateway id
413 * @return the associated l2 gw connections
415 public List<L2gatewayConnection> getL2GwConnectionsByL2GatewayId(Uuid l2GatewayId) {
416 List<L2gatewayConnection> l2GwConnections = new ArrayList<>();
417 List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
418 for (L2gatewayConnection l2GwConn : allL2GwConns) {
419 if (Objects.equals(l2GwConn.getL2gatewayId(), l2GatewayId)) {
420 l2GwConnections.add(l2GwConn);
423 return l2GwConnections;
426 private static boolean macBelongsToLogicalSwitch(LocalUcastMacs mac, String logicalSwitchName) {
427 InstanceIdentifier<LogicalSwitches> iid = (InstanceIdentifier<LogicalSwitches>)
428 mac.getLogicalSwitchRef().getValue();
429 return iid.firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue().equals(logicalSwitchName);
432 static InstanceIdentifier<LocalUcastMacs> getMacIid(InstanceIdentifier<Node> nodeIid, LocalUcastMacs mac) {
433 return nodeIid.augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class, mac.key());