Remove GENIUS UTIL references in Elanmanager Module
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / L2GatewayListener.java
1 /*
2  * Copyright (c) 2019 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netvirt.elan.l2gw.listeners;
10
11 import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
12
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;
22 import java.util.Map;
23 import java.util.Set;
24 import javax.annotation.PreDestroy;
25 import javax.inject.Inject;
26 import javax.inject.Singleton;
27 import org.opendaylight.genius.mdsalutil.MDSALUtil;
28 import org.opendaylight.genius.utils.SystemPropertyReader;
29 import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils;
30 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
31 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
32 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
33 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
34 import org.opendaylight.infrautils.utils.concurrent.Executors;
35 import org.opendaylight.mdsal.binding.api.DataBroker;
36 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
37 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunnerImpl;
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;
61
62 @Singleton
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;
73
74     @Inject
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(),
91                 this);
92         init();
93     }
94
95     public void init() {
96         LOG.info("{} init", getClass().getSimpleName());
97     }
98
99     @Override
100     @PreDestroy
101     public void close() {
102         super.close();
103         Executors.shutdownAndAwaitTermination(getExecutorService());
104     }
105
106     public void registerListener() {
107         super.register();
108         LOG.info("Registering L2Gateway Listener");
109     }
110
111     public void deregisterListener() {
112         super.close();
113         LOG.info("Deregistering L2GatewayListener");
114     }
115
116     @Override
117     public void add(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
118         LOG.info("Adding L2gateway with ID: {}", input.getUuid());
119
120         for (Devices l2Device : input.nonnullDevices().values()) {
121             LOG.trace("Adding L2gateway device: {}", l2Device);
122             addL2Device(l2Device, input);
123         }
124     }
125
126     @Override
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());
135                 tx.delete(iid);
136             }
137         }), new FutureCallback<Object>() {
138             @Override
139             public void onSuccess(Object result) {
140                 for (Devices l2Device : input.nonnullDevices().values()) {
141                     LOG.trace("Removing L2gateway device: {}", l2Device);
142                     removeL2Device(l2Device, input);
143                 }
144             }
145
146             @Override
147             public void onFailure(Throwable throwable) {
148                 LOG.error("Failed to delete associated l2gwconnection while deleting l2gw {} with id",
149                         input.getUuid(), throwable);
150             }
151         }, MoreExecutors.directExecutor());
152     }
153
154     @Override
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());
162             return;
163         }
164         if (original.getDevices() == null) {
165             connections.forEach(l2gwService::addL2GatewayConnection);
166             return;
167         }
168         jobCoordinator.enqueueJob("l2gw.update", () -> {
169             ListenableFuture<?> future = txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
170                 DeviceInterfaces updatedDeviceInterfaces = new DeviceInterfaces(update);
171                 original.nonnullDevices().values()
172                         .stream()
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.nonnullInterfaces().values()
180                                     .stream()
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);
190                                             }
191                                         } else {
192                                             LOG.debug("Deleting vlan binding {} {} {}",
193                                                     physicalSwitchNodeId, intf.getInterfaceName(), vlanId);
194                                             HwvtepUtils.deleteVlanBinding(tx, physicalSwitchNodeId,
195                                                     intf.getInterfaceName(), vlanId);
196                                         }
197                                     }));
198                         });
199             });
200             Futures.addCallback(future, new FutureCallback<Object>() {
201                 @Override
202                 public void onSuccess(Object success) {
203                     LOG.debug("Successfully deleted vlan bindings for l2gw update {}", update);
204                     connections.forEach((l2GwConnection) ->
205                             l2gwService.addL2GatewayConnection(l2GwConnection, null, update));
206                 }
207
208                 @Override
209                 public void onFailure(Throwable throwable) {
210                     LOG.error("Failed to delete vlan bindings as part of l2gw udpate {}", update);
211                 }
212             }, MoreExecutors.directExecutor());
213             return Collections.singletonList(future);
214         }, SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
215     }
216
217     private synchronized void addL2Device(Devices l2Device, L2gateway input) {
218         String l2DeviceName = l2Device.getDeviceName();
219
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);
224         } else {
225             LOG.info("Provisioning l2gw for device {}",l2DeviceName);
226             l2gwService.provisionItmAndL2gwConnection(l2GwDevice, l2DeviceName, l2GwDevice.getHwvtepNodeId(),
227                     l2GwDevice.getTunnelIp());
228         }
229     }
230
231     protected static boolean isLastL2GatewayBeingDeleted(L2GatewayDevice l2GwDevice) {
232         return l2GwDevice.getL2GatewayIds().size() == 1;
233     }
234
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();
248
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);
256                             }
257                         } else {
258                             LOG.info("ITM Tunnels are not deleted on the cluster node as this is not owner for {}",
259                                     l2DeviceName);
260                         }
261
262                         return null;
263                     });
264                 } else {
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));
274
275                 }
276             } else {
277                 l2GwDevice.removeL2GatewayId(input.getUuid());
278                 LOG.trace("ITM tunnels are not deleted for {} as this device has other L2gateway associations",
279                         l2DeviceName);
280             }
281         } else {
282             LOG.error("Unable to find L2 Gateway details for {}", l2DeviceName);
283         }
284     }
285
286     static class DeviceInterfaces {
287         Map<String, Map<String, Interfaces>> deviceInterfacesMap = new HashMap<>();
288
289         DeviceInterfaces(L2gateway l2gateway) {
290             if (l2gateway.nonnullDevices() != null) {
291                 l2gateway.nonnullDevices().values().forEach((device) -> {
292                     deviceInterfacesMap.putIfAbsent(device.getDeviceName(), new HashMap<>());
293                     if (device.nonnullInterfaces() != null) {
294                         device.nonnullInterfaces().values().forEach((intf) ->
295                                 deviceInterfacesMap.get(device.getDeviceName()).put(intf.getInterfaceName(), intf));
296                     }
297                 });
298             }
299         }
300
301         boolean containsInterface(String deviceName, String interfaceName) {
302             if (deviceInterfacesMap.containsKey(deviceName)) {
303                 return deviceInterfacesMap.get(deviceName).containsKey(interfaceName);
304             }
305             return false;
306         }
307     }
308 }