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.PostConstruct;
25 import javax.inject.Inject;
26 import javax.inject.Singleton;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
30 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
31 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
32 import org.opendaylight.genius.mdsalutil.MDSALUtil;
33 import org.opendaylight.genius.utils.SystemPropertyReader;
34 import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils;
35 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
36 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
37 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
38 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.devices.Interfaces;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
62 public class L2GatewayListener extends AsyncClusteredDataTreeChangeListenerBase<L2gateway, L2GatewayListener>
63 implements RecoverableListener {
64 private static final Logger LOG = LoggerFactory.getLogger(L2GatewayListener.class);
65 private final DataBroker dataBroker;
66 private final ManagedNewTransactionRunner txRunner;
67 private final ItmRpcService itmRpcService;
68 private final IL2gwService l2gwService;
69 private final EntityOwnershipUtils entityOwnershipUtils;
70 private final JobCoordinator jobCoordinator;
71 private final L2GatewayCache l2GatewayCache;
74 public L2GatewayListener(final DataBroker dataBroker, final EntityOwnershipService entityOwnershipService,
75 final ItmRpcService itmRpcService, final IL2gwService l2gwService,
76 final JobCoordinator jobCoordinator, final L2GatewayCache l2GatewayCache,
77 L2GatewayInstanceRecoveryHandler l2GatewayInstanceRecoveryHandler,
78 ServiceRecoveryRegistry serviceRecoveryRegistry) {
79 this.dataBroker = dataBroker;
80 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
81 this.entityOwnershipUtils = new EntityOwnershipUtils(entityOwnershipService);
82 this.itmRpcService = itmRpcService;
83 this.l2gwService = l2gwService;
84 this.jobCoordinator = jobCoordinator;
85 this.l2GatewayCache = l2GatewayCache;
86 serviceRecoveryRegistry.addRecoverableListener(l2GatewayInstanceRecoveryHandler.buildServiceRegistryKey(),
92 LOG.info("{} init", getClass().getSimpleName());
96 public void registerListener() {
97 LOG.info("Registering L2Gateway Listener");
98 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
101 public void deregisterListener() {
102 LOG.info("Deregistering L2GatewayListener");
103 super.deregisterListener();
107 protected InstanceIdentifier<L2gateway> getWildCardPath() {
108 return InstanceIdentifier.create(Neutron.class).child(L2gateways.class).child(L2gateway.class);
112 protected void add(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
113 LOG.info("Adding L2gateway with ID: {}", input.getUuid());
115 for (Devices l2Device : input.nonnullDevices()) {
116 LOG.trace("Adding L2gateway device: {}", l2Device);
117 addL2Device(l2Device, input);
122 protected void remove(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
123 LOG.info("Removing L2gateway with ID: {}", input.getUuid());
124 List<L2gatewayConnection> connections = l2gwService
125 .getL2GwConnectionsByL2GatewayId(input.getUuid());
126 Futures.addCallback(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
127 for (L2gatewayConnection connection : connections) {
128 InstanceIdentifier<L2gatewayConnection> iid = InstanceIdentifier.create(Neutron.class)
129 .child(L2gatewayConnections.class).child(L2gatewayConnection.class, connection.key());
132 }), new FutureCallback<Void>() {
134 public void onSuccess(Void result) {
135 for (Devices l2Device : input.nonnullDevices()) {
136 LOG.trace("Removing L2gateway device: {}", l2Device);
137 removeL2Device(l2Device, input);
142 public void onFailure(Throwable throwable) {
143 LOG.error("Failed to delete associated l2gwconnection while deleting l2gw {} with id",
144 input.getUuid(), throwable);
146 }, MoreExecutors.directExecutor());
150 protected void update(InstanceIdentifier<L2gateway> identifier, L2gateway original, L2gateway update) {
151 LOG.trace("Updating L2gateway : key: {}, original value={}, update value={}", identifier, original, update);
152 List<L2gatewayConnection> connections = l2gwService.getAssociatedL2GwConnections(
153 Sets.newHashSet(update.getUuid()));
154 if (connections == null) {
155 LOG.warn("There are no connections associated with l2 gateway uuid {} name {}",
156 update.getUuid(), update.getName());
159 if (original.getDevices() == null) {
160 connections.forEach(l2gwService::addL2GatewayConnection);
163 jobCoordinator.enqueueJob("l2gw.update", () -> {
164 ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
165 DeviceInterfaces updatedDeviceInterfaces = new DeviceInterfaces(update);
166 original.getDevices()
168 .filter((originalDevice) -> originalDevice.getInterfaces() != null)
169 .forEach((originalDevice) -> {
170 String deviceName = originalDevice.getDeviceName();
171 L2GatewayDevice l2GwDevice = l2GatewayCache.get(deviceName);
172 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(
173 new NodeId(l2GwDevice.getHwvtepNodeId()), deviceName);
174 originalDevice.getInterfaces()
176 .filter((intf) -> !updatedDeviceInterfaces.containsInterface(
177 deviceName, intf.getInterfaceName()))
178 .forEach((intf) -> connections.forEach((connection) -> {
179 Integer vlanId = connection.getSegmentId();
180 if (intf.getSegmentationIds() != null
181 && !intf.getSegmentationIds().isEmpty()) {
182 for (Integer vlan : intf.getSegmentationIds()) {
183 HwvtepUtils.deleteVlanBinding(tx,
184 physicalSwitchNodeId, intf.getInterfaceName(), vlan);
187 LOG.debug("Deleting vlan binding {} {} {}",
188 physicalSwitchNodeId, intf.getInterfaceName(), vlanId);
189 HwvtepUtils.deleteVlanBinding(tx, physicalSwitchNodeId,
190 intf.getInterfaceName(), vlanId);
195 Futures.addCallback(future, new FutureCallback<Void>() {
197 public void onSuccess(Void success) {
198 LOG.debug("Successfully deleted vlan bindings for l2gw update {}", update);
199 connections.forEach((l2GwConnection) ->
200 l2gwService.addL2GatewayConnection(l2GwConnection, null, update));
204 public void onFailure(Throwable throwable) {
205 LOG.error("Failed to delete vlan bindings as part of l2gw udpate {}", update);
207 }, MoreExecutors.directExecutor());
208 return Collections.singletonList(future);
209 }, SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
212 private synchronized void addL2Device(Devices l2Device, L2gateway input) {
213 String l2DeviceName = l2Device.getDeviceName();
215 L2GatewayDevice l2GwDevice = l2GatewayCache.addOrGet(l2DeviceName);
216 l2GwDevice.addL2GatewayId(input.getUuid());
217 if (l2GwDevice.getHwvtepNodeId() == null) {
218 LOG.info("L2GW provisioning skipped for device {}",l2DeviceName);
220 LOG.info("Provisioning l2gw for device {}",l2DeviceName);
221 l2gwService.provisionItmAndL2gwConnection(l2GwDevice, l2DeviceName, l2GwDevice.getHwvtepNodeId(),
222 l2GwDevice.getTunnelIp());
226 protected static boolean isLastL2GatewayBeingDeleted(L2GatewayDevice l2GwDevice) {
227 return l2GwDevice.getL2GatewayIds().size() == 1;
230 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
231 justification = "https://github.com/spotbugs/spotbugs/issues/811")
232 private void removeL2Device(Devices l2Device, L2gateway input) {
233 final String l2DeviceName = l2Device.getDeviceName();
234 L2GatewayDevice l2GwDevice = l2GatewayCache.get(l2DeviceName);
235 if (l2GwDevice != null) {
236 // Delete ITM tunnels if it's last Gateway deleted and device is connected
237 // Also, do not delete device from cache if it's connected
238 if (L2GatewayUtils.isLastL2GatewayBeingDeleted(l2GwDevice)) {
239 if (l2GwDevice.isConnected()) {
240 l2GwDevice.removeL2GatewayId(input.getUuid());
241 // Delete ITM tunnels
242 final String hwvtepId = l2GwDevice.getHwvtepNodeId();
244 final Set<IpAddress> tunnelIps = l2GwDevice.getTunnelIps();
245 jobCoordinator.enqueueJob(hwvtepId, () -> {
246 if (entityOwnershipUtils.isEntityOwner(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
247 HwvtepSouthboundConstants.ELAN_ENTITY_NAME)) {
248 LOG.info("Deleting ITM Tunnels for {} connected to cluster node owner", l2DeviceName);
249 for (IpAddress tunnelIp : tunnelIps) {
250 L2GatewayUtils.deleteItmTunnels(itmRpcService, hwvtepId, l2DeviceName, tunnelIp);
253 LOG.info("ITM Tunnels are not deleted on the cluster node as this is not owner for {}",
260 l2GatewayCache.remove(l2DeviceName);
261 // Cleaning up the config DS
262 NodeId nodeId = new NodeId(l2GwDevice.getHwvtepNodeId());
263 NodeId psNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, l2DeviceName);
264 //FIXME: These should be removed
265 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
266 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId));
267 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
268 HwvtepSouthboundUtils.createInstanceIdentifier(psNodeId));
272 l2GwDevice.removeL2GatewayId(input.getUuid());
273 LOG.trace("ITM tunnels are not deleted for {} as this device has other L2gateway associations",
277 LOG.error("Unable to find L2 Gateway details for {}", l2DeviceName);
282 protected L2GatewayListener getDataTreeChangeListener() {
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);