2 * Copyright (c) 2019 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.listeners;
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
13 import com.google.common.collect.Sets;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
24 import javax.annotation.PreDestroy;
25 import javax.inject.Inject;
26 import javax.inject.Singleton;
27 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
28 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
29 import org.opendaylight.genius.mdsalutil.MDSALUtil;
30 import org.opendaylight.genius.utils.SystemPropertyReader;
31 import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils;
32 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
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.infrautils.utils.concurrent.Executors;
37 import org.opendaylight.mdsal.binding.api.DataBroker;
38 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
39 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
40 import org.opendaylight.netvirt.elan.l2gw.recovery.impl.L2GatewayInstanceRecoveryHandler;
41 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayUtils;
42 import org.opendaylight.netvirt.elanmanager.api.IL2gwService;
43 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
44 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
45 import org.opendaylight.serviceutils.srm.RecoverableListener;
46 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
47 import org.opendaylight.serviceutils.tools.listener.AbstractClusteredAsyncDataTreeChangeListener;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.devices.Interfaces;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
63 public class L2GatewayListener extends AbstractClusteredAsyncDataTreeChangeListener<L2gateway>
64 implements RecoverableListener {
65 private static final Logger LOG = LoggerFactory.getLogger(L2GatewayListener.class);
66 private final DataBroker dataBroker;
67 private final ManagedNewTransactionRunner txRunner;
68 private final ItmRpcService itmRpcService;
69 private final IL2gwService l2gwService;
70 private final EntityOwnershipUtils entityOwnershipUtils;
71 private final JobCoordinator jobCoordinator;
72 private final L2GatewayCache l2GatewayCache;
75 public L2GatewayListener(final DataBroker dataBroker, final EntityOwnershipService entityOwnershipService,
76 final ItmRpcService itmRpcService, final IL2gwService l2gwService,
77 final JobCoordinator jobCoordinator, final L2GatewayCache l2GatewayCache,
78 L2GatewayInstanceRecoveryHandler l2GatewayInstanceRecoveryHandler,
79 ServiceRecoveryRegistry serviceRecoveryRegistry) {
80 super(dataBroker, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Neutron.class)
81 .child(L2gateways.class).child(L2gateway.class),
82 Executors.newListeningSingleThreadExecutor("L2GatewayListener", LOG));
83 this.dataBroker = dataBroker;
84 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
85 this.entityOwnershipUtils = new EntityOwnershipUtils(entityOwnershipService);
86 this.itmRpcService = itmRpcService;
87 this.l2gwService = l2gwService;
88 this.jobCoordinator = jobCoordinator;
89 this.l2GatewayCache = l2GatewayCache;
90 serviceRecoveryRegistry.addRecoverableListener(l2GatewayInstanceRecoveryHandler.buildServiceRegistryKey(),
96 LOG.info("{} init", getClass().getSimpleName());
101 public void close() {
103 Executors.shutdownAndAwaitTermination(getExecutorService());
106 public void registerListener() {
108 LOG.info("Registering L2Gateway Listener");
111 public void deregisterListener() {
113 LOG.info("Deregistering L2GatewayListener");
117 public void add(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
118 LOG.info("Adding L2gateway with ID: {}", input.getUuid());
120 for (Devices l2Device : input.nonnullDevices()) {
121 LOG.trace("Adding L2gateway device: {}", l2Device);
122 addL2Device(l2Device, input);
127 public void remove(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
128 LOG.info("Removing L2gateway with ID: {}", input.getUuid());
129 List<L2gatewayConnection> connections = l2gwService
130 .getL2GwConnectionsByL2GatewayId(input.getUuid());
131 Futures.addCallback(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
132 for (L2gatewayConnection connection : connections) {
133 InstanceIdentifier<L2gatewayConnection> iid = InstanceIdentifier.create(Neutron.class)
134 .child(L2gatewayConnections.class).child(L2gatewayConnection.class, connection.key());
137 }), new FutureCallback<Void>() {
139 public void onSuccess(Void result) {
140 for (Devices l2Device : input.nonnullDevices()) {
141 LOG.trace("Removing L2gateway device: {}", l2Device);
142 removeL2Device(l2Device, input);
147 public void onFailure(Throwable throwable) {
148 LOG.error("Failed to delete associated l2gwconnection while deleting l2gw {} with id",
149 input.getUuid(), throwable);
151 }, MoreExecutors.directExecutor());
155 public void update(InstanceIdentifier<L2gateway> identifier, L2gateway original, L2gateway update) {
156 LOG.trace("Updating L2gateway : key: {}, original value={}, update value={}", identifier, original, update);
157 List<L2gatewayConnection> connections = l2gwService.getAssociatedL2GwConnections(
158 Sets.newHashSet(update.getUuid()));
159 if (connections == null) {
160 LOG.warn("There are no connections associated with l2 gateway uuid {} name {}",
161 update.getUuid(), update.getName());
164 if (original.getDevices() == null) {
165 connections.forEach(l2gwService::addL2GatewayConnection);
168 jobCoordinator.enqueueJob("l2gw.update", () -> {
169 ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
170 DeviceInterfaces updatedDeviceInterfaces = new DeviceInterfaces(update);
171 original.getDevices()
173 .filter((originalDevice) -> originalDevice.getInterfaces() != null)
174 .forEach((originalDevice) -> {
175 String deviceName = originalDevice.getDeviceName();
176 L2GatewayDevice l2GwDevice = l2GatewayCache.get(deviceName);
177 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(
178 new NodeId(l2GwDevice.getHwvtepNodeId()), deviceName);
179 originalDevice.getInterfaces()
181 .filter((intf) -> !updatedDeviceInterfaces.containsInterface(
182 deviceName, intf.getInterfaceName()))
183 .forEach((intf) -> connections.forEach((connection) -> {
184 Integer vlanId = connection.getSegmentId();
185 if (intf.getSegmentationIds() != null
186 && !intf.getSegmentationIds().isEmpty()) {
187 for (Integer vlan : intf.getSegmentationIds()) {
188 HwvtepUtils.deleteVlanBinding(tx,
189 physicalSwitchNodeId, intf.getInterfaceName(), vlan);
192 LOG.debug("Deleting vlan binding {} {} {}",
193 physicalSwitchNodeId, intf.getInterfaceName(), vlanId);
194 HwvtepUtils.deleteVlanBinding(tx, physicalSwitchNodeId,
195 intf.getInterfaceName(), vlanId);
200 Futures.addCallback(future, new FutureCallback<Void>() {
202 public void onSuccess(Void success) {
203 LOG.debug("Successfully deleted vlan bindings for l2gw update {}", update);
204 connections.forEach((l2GwConnection) ->
205 l2gwService.addL2GatewayConnection(l2GwConnection, null, update));
209 public void onFailure(Throwable throwable) {
210 LOG.error("Failed to delete vlan bindings as part of l2gw udpate {}", update);
212 }, MoreExecutors.directExecutor());
213 return Collections.singletonList(future);
214 }, SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
217 private synchronized void addL2Device(Devices l2Device, L2gateway input) {
218 String l2DeviceName = l2Device.getDeviceName();
220 L2GatewayDevice l2GwDevice = l2GatewayCache.addOrGet(l2DeviceName);
221 l2GwDevice.addL2GatewayId(input.getUuid());
222 if (l2GwDevice.getHwvtepNodeId() == null) {
223 LOG.info("L2GW provisioning skipped for device {}",l2DeviceName);
225 LOG.info("Provisioning l2gw for device {}",l2DeviceName);
226 l2gwService.provisionItmAndL2gwConnection(l2GwDevice, l2DeviceName, l2GwDevice.getHwvtepNodeId(),
227 l2GwDevice.getTunnelIp());
231 protected static boolean isLastL2GatewayBeingDeleted(L2GatewayDevice l2GwDevice) {
232 return l2GwDevice.getL2GatewayIds().size() == 1;
235 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
236 justification = "https://github.com/spotbugs/spotbugs/issues/811")
237 private void removeL2Device(Devices l2Device, L2gateway input) {
238 final String l2DeviceName = l2Device.getDeviceName();
239 L2GatewayDevice l2GwDevice = l2GatewayCache.get(l2DeviceName);
240 if (l2GwDevice != null) {
241 // Delete ITM tunnels if it's last Gateway deleted and device is connected
242 // Also, do not delete device from cache if it's connected
243 if (L2GatewayUtils.isLastL2GatewayBeingDeleted(l2GwDevice)) {
244 if (l2GwDevice.isConnected()) {
245 l2GwDevice.removeL2GatewayId(input.getUuid());
246 // Delete ITM tunnels
247 final String hwvtepId = l2GwDevice.getHwvtepNodeId();
249 final Set<IpAddress> tunnelIps = l2GwDevice.getTunnelIps();
250 jobCoordinator.enqueueJob(hwvtepId, () -> {
251 if (entityOwnershipUtils.isEntityOwner(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
252 HwvtepSouthboundConstants.ELAN_ENTITY_NAME)) {
253 LOG.info("Deleting ITM Tunnels for {} connected to cluster node owner", l2DeviceName);
254 for (IpAddress tunnelIp : tunnelIps) {
255 L2GatewayUtils.deleteItmTunnels(itmRpcService, hwvtepId, l2DeviceName, tunnelIp);
258 LOG.info("ITM Tunnels are not deleted on the cluster node as this is not owner for {}",
265 l2GatewayCache.remove(l2DeviceName);
266 // Cleaning up the config DS
267 NodeId nodeId = new NodeId(l2GwDevice.getHwvtepNodeId());
268 NodeId psNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, l2DeviceName);
269 //FIXME: These should be removed
270 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
271 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId));
272 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
273 HwvtepSouthboundUtils.createInstanceIdentifier(psNodeId));
277 l2GwDevice.removeL2GatewayId(input.getUuid());
278 LOG.trace("ITM tunnels are not deleted for {} as this device has other L2gateway associations",
282 LOG.error("Unable to find L2 Gateway details for {}", l2DeviceName);
286 static class DeviceInterfaces {
287 Map<String, Map<String, Interfaces>> deviceInterfacesMap = new HashMap<>();
289 DeviceInterfaces(L2gateway l2gateway) {
290 if (l2gateway.getDevices() != null) {
291 l2gateway.getDevices().forEach((device) -> {
292 deviceInterfacesMap.putIfAbsent(device.getDeviceName(), new HashMap<>());
293 if (device.getInterfaces() != null) {
294 device.getInterfaces().forEach((intf) ->
295 deviceInterfacesMap.get(device.getDeviceName()).put(intf.getInterfaceName(), intf));
301 boolean containsInterface(String deviceName, String interfaceName) {
302 if (deviceInterfacesMap.containsKey(deviceName)) {
303 return deviceInterfacesMap.get(deviceName).containsKey(interfaceName);