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.PreDestroy;
25 import javax.inject.Inject;
26 import javax.inject.Singleton;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.eclipse.jdt.annotation.Nullable;
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.infrautils.jobcoordinator.JobCoordinator;
35 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
36 import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpClusteredListener;
37 import org.opendaylight.netvirt.elan.l2gw.jobs.AssociateHwvtepToElanJob;
38 import org.opendaylight.netvirt.elan.l2gw.jobs.DisAssociateHwvtepFromElanJob;
39 import org.opendaylight.netvirt.elan.l2gw.listeners.LocalUcastMacListener;
40 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
41 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
42 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
43 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gatewayKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
63 public class L2GatewayConnectionUtils implements AutoCloseable {
64 private static final Logger LOG = LoggerFactory.getLogger(L2GatewayConnectionUtils.class);
66 private final DataBroker broker;
67 private final ElanL2GatewayUtils elanL2GatewayUtils;
68 private final ElanClusterUtils elanClusterUtils;
69 private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
70 private final JobCoordinator jobCoordinator;
71 private final L2GatewayCache l2GatewayCache;
72 private final ElanInstanceCache elanInstanceCache;
73 private final List<AutoCloseable> closeables = new CopyOnWriteArrayList<>();
74 private final HwvtepNodeHACache hwvtepNodeHACache;
75 private final HAOpClusteredListener haOpClusteredListener;
76 private final ElanRefUtil elanRefUtil;
79 public L2GatewayConnectionUtils(DataBroker dataBroker, ElanClusterUtils elanClusterUtils,
80 ElanL2GatewayUtils elanL2GatewayUtils, JobCoordinator jobCoordinator,
81 ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
82 L2GatewayCache l2GatewayCache, HAOpClusteredListener haOpClusteredListener,
83 ElanInstanceCache elanInstanceCache, HwvtepNodeHACache hwvtepNodeHACache,
84 ElanRefUtil elanRefUtil) {
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;
94 this.elanRefUtil = elanRefUtil;
99 @SuppressWarnings("checkstyle:IllegalCatch")
100 public void close() {
101 closeables.forEach(c -> {
104 } catch (Exception e) {
105 LOG.warn("Error closing {}", c, e);
110 public static boolean isGatewayAssociatedToL2Device(L2GatewayDevice l2GwDevice) {
111 return !l2GwDevice.getL2GatewayIds().isEmpty();
115 public static L2gateway getNeutronL2gateway(DataBroker broker, Uuid l2GatewayId) {
116 LOG.debug("getNeutronL2gateway for {}", l2GatewayId.getValue());
117 InstanceIdentifier<L2gateway> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class)
118 .child(L2gateway.class, new L2gatewayKey(l2GatewayId));
119 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).orNull();
123 public static List<L2gateway> getL2gatewayList(DataBroker broker) {
124 InstanceIdentifier<L2gateways> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class);
125 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).toJavaUtil().map(
126 L2gateways::getL2gateway).orElse(emptyList());
130 public static List<L2gatewayConnection> getAllL2gatewayConnections(DataBroker broker) {
131 InstanceIdentifier<L2gatewayConnections> inst = InstanceIdentifier.create(Neutron.class)
132 .child(L2gatewayConnections.class);
133 return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).toJavaUtil().map(
134 L2gatewayConnections::getL2gatewayConnection).orElse(emptyList());
138 * Gets the associated l2 gw connections.
142 * @param l2GatewayIds
144 * @return the associated l2 gw connections
147 public static List<L2gatewayConnection> getAssociatedL2GwConnections(DataBroker broker, Set<Uuid> l2GatewayIds) {
148 List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
149 List<L2gatewayConnection> l2GwConnections = new ArrayList<>();
150 for (Uuid l2GatewayId : l2GatewayIds) {
151 for (L2gatewayConnection l2GwConn : allL2GwConns) {
152 if (Objects.equals(l2GwConn.getL2gatewayId(), l2GatewayId)) {
153 l2GwConnections.add(l2GwConn);
157 return l2GwConnections;
161 * Gets the associated l2 gw connections.
167 * @return the associated l2 gw connection with elan
170 public static List<L2gatewayConnection> getL2GwConnectionsByElanName(DataBroker broker, String elanName) {
171 List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
172 List<L2gatewayConnection> elanL2GateWayConnections = new ArrayList<>();
173 for (L2gatewayConnection l2GwConn : allL2GwConns) {
174 if (l2GwConn.getNetworkId().getValue().equalsIgnoreCase(elanName)) {
175 elanL2GateWayConnections.add(l2GwConn);
178 return elanL2GateWayConnections;
181 public void addL2GatewayConnection(L2gatewayConnection input) {
182 addL2GatewayConnection(input, null/*deviceName*/, null);
185 public void addL2GatewayConnection(final L2gatewayConnection input,
186 final String l2GwDeviceName) {
187 addL2GatewayConnection(input, l2GwDeviceName, null);
190 public void addL2GatewayConnection(final L2gatewayConnection input,
191 @Nullable final String l2GwDeviceName ,
192 @Nullable L2gateway l2Gateway) {
193 LOG.info("Adding L2gateway Connection with ID: {}", input.key().getUuid());
195 Uuid networkUuid = input.getNetworkId();
197 // Taking cluster reboot scenario , if Elan instance is not available when l2GatewayConnection add events
198 // comes we need to wait for elaninstance to resolve. Hence updating the map with the runnable .
199 // When elanInstance add comes , it look in to the map and run the associated runnable associated with it.
200 ElanInstance elanInstance = elanInstanceCache.get(networkUuid.getValue(),
201 () -> addL2GatewayConnection(input, l2GwDeviceName)).orNull();
202 if (elanInstance == null) {
206 if (!isVxlanNetworkOrVxlanSegment(elanInstance)) {
207 LOG.error("Neutron network with id {} is not VxlanNetwork", networkUuid.getValue());
209 Uuid l2GatewayId = input.getL2gatewayId();
210 if (l2Gateway == null) {
211 l2Gateway = getNeutronL2gateway(broker, l2GatewayId);
213 if (l2Gateway == null) {
214 LOG.error("L2Gateway with id {} is not present", l2GatewayId.getValue());
216 associateHwvtepsToElan(elanInstance, l2Gateway, input, l2GwDeviceName);
221 public void deleteL2GatewayConnection(L2gatewayConnection input) {
222 LOG.info("Deleting L2gateway Connection with ID: {}", input.key().getUuid());
224 Uuid networkUuid = input.getNetworkId();
225 String elanName = networkUuid.getValue();
226 disAssociateHwvtepsFromElan(elanName, input);
229 private void disAssociateHwvtepsFromElan(String elanName, L2gatewayConnection input) {
230 Integer defaultVlan = input.getSegmentId();
231 List<L2GatewayDevice> l2Devices = ElanL2GwCacheUtils.getAllElanDevicesFromCache();
232 List<Devices> l2gwDevicesToBeDeleted = new ArrayList<>();
233 for (L2GatewayDevice elanL2gwDevice : l2Devices) {
234 if (elanL2gwDevice.getL2GatewayIds().contains(input.key().getUuid())) {
235 l2gwDevicesToBeDeleted.addAll(elanL2gwDevice.getDevicesForL2gwConnectionId(input.key().getUuid()));
238 if (l2gwDevicesToBeDeleted.isEmpty()) {
239 //delete logical switch
240 Uuid l2GatewayId = input.getL2gatewayId();
241 L2gateway l2Gateway = L2GatewayConnectionUtils.getNeutronL2gateway(broker, l2GatewayId);
242 if (l2Gateway == null) {
243 LOG.error("Failed to find the l2gateway for the connection {}", input.getUuid());
245 } else if (l2Gateway.getDevices() != null) {
246 l2gwDevicesToBeDeleted.addAll(l2Gateway.getDevices());
249 for (Devices l2Device : l2gwDevicesToBeDeleted) {
250 String l2DeviceName = l2Device.getDeviceName();
251 L2GatewayDevice l2GatewayDevice = l2GatewayCache.get(l2DeviceName);
252 String hwvtepNodeId = l2GatewayDevice.getHwvtepNodeId();
253 boolean isLastL2GwConnDeleted = false;
254 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
255 if (elanL2GwDevice != null && isLastL2GwConnBeingDeleted(elanL2GwDevice)) {
256 // Delete L2 Gateway device from 'ElanL2GwDevice' cache
257 LOG.debug("Elan L2Gw Conn cache removed for id {}", hwvtepNodeId);
258 ElanL2GwCacheUtils.removeL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
259 isLastL2GwConnDeleted = true;
261 Uuid l2GwConnId = input.key().getUuid();
262 LOG.debug("Elan L2Gw Conn cache with id {} is being referred by other L2Gw Conns; so only "
263 + "L2 Gw Conn {} reference is removed", hwvtepNodeId, l2GwConnId);
264 if (elanL2GwDevice != null) {
265 elanL2GwDevice.removeL2GatewayId(l2GwConnId);
267 isLastL2GwConnDeleted = true;
271 DisAssociateHwvtepFromElanJob disAssociateHwvtepToElanJob =
272 new DisAssociateHwvtepFromElanJob(elanL2GatewayUtils, elanL2GatewayMulticastUtils,
273 elanL2GwDevice, elanName,
274 l2Device, defaultVlan, hwvtepNodeId, isLastL2GwConnDeleted);
275 elanClusterUtils.runOnlyInOwnerNode(disAssociateHwvtepToElanJob.getJobKey(), "remove l2gw connection job",
276 disAssociateHwvtepToElanJob);
280 private void associateHwvtepsToElan(ElanInstance elanInstance,
281 L2gateway l2Gateway, L2gatewayConnection input, @Nullable String l2GwDeviceName) {
282 String elanName = elanInstance.getElanInstanceName();
283 Integer defaultVlan = input.getSegmentId();
284 Uuid l2GwConnId = input.key().getUuid();
285 List<Devices> l2Devices = l2Gateway.getDevices();
287 LOG.trace("Associating ELAN {} with L2Gw Conn Id {} having below L2Gw devices {}", elanName, l2GwConnId,
290 if (l2Devices == null) {
294 for (Devices l2Device : l2Devices) {
295 String l2DeviceName = l2Device.getDeviceName();
296 // L2gateway can have more than one L2 Gw devices. Configure Logical Switch, VLAN mappings,...
297 // only on the switch which has come up just now and exclude all other devices from
298 // preprovisioning/re-provisioning
299 if (l2GwDeviceName != null && !l2GwDeviceName.equals(l2DeviceName)) {
300 LOG.debug("Associating Hwvtep to ELAN is not been processed for {}; as only {} got connected now!",
301 l2DeviceName, l2GwDeviceName);
304 L2GatewayDevice l2GatewayDevice = l2GatewayCache.get(l2DeviceName);
305 if (isL2GwDeviceConnected(l2GatewayDevice)) {
306 NodeId hwvtepNodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
308 // Delete pending delete logical switch task if scheduled
309 elanL2GatewayUtils.cancelDeleteLogicalSwitch(hwvtepNodeId,
310 ElanL2GatewayUtils.getLogicalSwitchFromElan(elanName));
312 // Add L2 Gateway device to 'ElanL2GwDevice' cache
313 boolean createLogicalSwitch;
314 addL2DeviceToElanL2GwCache(elanName, l2GatewayDevice, l2GwConnId, l2Device);
316 AssociateHwvtepToElanJob associateHwvtepToElanJob = new AssociateHwvtepToElanJob(broker,
317 elanL2GatewayUtils, elanL2GatewayMulticastUtils, elanInstanceCache, l2GatewayDevice,
318 elanInstance, l2Device, defaultVlan, elanRefUtil);
320 elanClusterUtils.runOnlyInOwnerNode(associateHwvtepToElanJob.getJobKey(),
321 "create logical switch in hwvtep topo", associateHwvtepToElanJob);
324 LOG.info("L2GwConn create is not handled for device with id {} as it's not connected", l2DeviceName);
329 public L2GatewayDevice addL2DeviceToElanL2GwCache(String elanName, L2GatewayDevice l2GatewayDevice, Uuid l2GwConnId,
331 String l2gwDeviceNodeId = l2GatewayDevice.getHwvtepNodeId();
332 L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, l2gwDeviceNodeId);
333 if (elanL2GwDevice == null) {
334 elanL2GwDevice = new L2GatewayDevice(l2GatewayDevice.getDeviceName());
335 elanL2GwDevice.setHwvtepNodeId(l2gwDeviceNodeId);
336 elanL2GwDevice.setTunnelIps(l2GatewayDevice.getTunnelIps());
337 ElanL2GwCacheUtils.addL2GatewayDeviceToCache(elanName, elanL2GwDevice);
338 LOG.debug("Elan L2GwConn cache created for hwvtep id {}", l2gwDeviceNodeId);
340 LOG.debug("Elan L2GwConn cache already exists for hwvtep id {}; updating L2GwConn id {} to it",
341 l2gwDeviceNodeId, l2GwConnId);
343 elanL2GwDevice.addL2GatewayId(l2GwConnId);
344 elanL2GwDevice.addL2gwConnectionIdToDevice(l2GwConnId, l2Device);
346 //incase of cluster reboot scenario southbound device would have added more macs
347 //while odl is down, pull them now
348 readAndCopyLocalUcastMacsToCache(elanName, l2GatewayDevice);
350 LOG.trace("Elan L2GwConn cache updated with below details: {}", elanL2GwDevice);
351 return elanL2GwDevice;
354 private static boolean isL2GwDeviceConnected(L2GatewayDevice l2GwDevice) {
355 return l2GwDevice != null && l2GwDevice.getHwvtepNodeId() != null;
358 protected static boolean isLastL2GwConnBeingDeleted(@NonNull L2GatewayDevice l2GwDevice) {
359 return l2GwDevice.getL2GatewayIds().size() == 1;
362 private void readAndCopyLocalUcastMacsToCache(final String elanName, final L2GatewayDevice l2GatewayDevice) {
363 final InstanceIdentifier<Node> nodeIid = HwvtepSouthboundUtils.createInstanceIdentifier(
364 new NodeId(l2GatewayDevice.getHwvtepNodeId()));
365 jobCoordinator.enqueueJob(elanName + ":" + l2GatewayDevice.getDeviceName(), () -> {
366 final SettableFuture settableFuture = SettableFuture.create();
367 Futures.addCallback(broker.newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
368 nodeIid), new SettableFutureCallback<Optional<Node>>(settableFuture) {
370 public void onSuccess(@NonNull Optional<Node> resultNode) {
371 LocalUcastMacListener localUcastMacListener =
372 new LocalUcastMacListener(broker, haOpClusteredListener,
373 elanL2GatewayUtils, jobCoordinator, elanInstanceCache, hwvtepNodeHACache);
374 settableFuture.set(resultNode);
375 Optional<Node> nodeOptional = resultNode;
376 if (nodeOptional.isPresent()) {
377 Node node = nodeOptional.get();
378 if (node.augmentation(HwvtepGlobalAugmentation.class) != null) {
379 List<LocalUcastMacs> localUcastMacs =
380 node.augmentation(HwvtepGlobalAugmentation.class).getLocalUcastMacs();
381 if (localUcastMacs == null) {
384 localUcastMacs.stream()
385 .filter((mac) -> macBelongsToLogicalSwitch(mac, elanName))
387 InstanceIdentifier<LocalUcastMacs> macIid = getMacIid(nodeIid, mac);
388 localUcastMacListener.added(macIid, mac);
393 }, MoreExecutors.directExecutor());
394 return Lists.newArrayList(settableFuture);
399 * Gets the associated l2 gw connections.
401 * @param l2GatewayId the l2 gateway id
403 * @return the associated l2 gw connections
405 public List<L2gatewayConnection> getL2GwConnectionsByL2GatewayId(Uuid l2GatewayId) {
406 List<L2gatewayConnection> l2GwConnections = new ArrayList<>();
407 List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
408 for (L2gatewayConnection l2GwConn : allL2GwConns) {
409 if (Objects.equals(l2GwConn.getL2gatewayId(), l2GatewayId)) {
410 l2GwConnections.add(l2GwConn);
413 return l2GwConnections;
416 private static boolean macBelongsToLogicalSwitch(LocalUcastMacs mac, String logicalSwitchName) {
417 InstanceIdentifier<LogicalSwitches> iid = (InstanceIdentifier<LogicalSwitches>)
418 mac.getLogicalSwitchRef().getValue();
419 return iid.firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue().equals(logicalSwitchName);
422 static InstanceIdentifier<LocalUcastMacs> getMacIid(InstanceIdentifier<Node> nodeIid, LocalUcastMacs mac) {
423 return nodeIid.augmentation(HwvtepGlobalAugmentation.class).child(LocalUcastMacs.class, mac.key());