2 * Copyright (c) 2016, 2017 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
8 package org.opendaylight.netvirt.neutronvpn.l2gw;
10 import com.google.common.collect.Sets;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.List;
20 import javax.annotation.PostConstruct;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
26 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
27 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
28 import org.opendaylight.genius.mdsalutil.MDSALUtil;
29 import org.opendaylight.genius.utils.SystemPropertyReader;
30 import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils;
31 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
32 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
33 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
34 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
35 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
36 import org.opendaylight.netvirt.elanmanager.api.IL2gwService;
37 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
38 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.devices.Interfaces;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 public class L2GatewayListener extends AsyncClusteredDataTreeChangeListenerBase<L2gateway, L2GatewayListener> {
55 private static final Logger LOG = LoggerFactory.getLogger(L2GatewayListener.class);
56 private final DataBroker dataBroker;
57 private final ManagedNewTransactionRunner txRunner;
58 private final ItmRpcService itmRpcService;
59 private final IL2gwService l2gwService;
60 private final EntityOwnershipUtils entityOwnershipUtils;
61 private final JobCoordinator jobCoordinator;
62 private final L2GatewayCache l2GatewayCache;
65 public L2GatewayListener(final DataBroker dataBroker, final EntityOwnershipService entityOwnershipService,
66 final ItmRpcService itmRpcService, final IL2gwService l2gwService,
67 final JobCoordinator jobCoordinator, final L2GatewayCache l2GatewayCache) {
68 this.dataBroker = dataBroker;
69 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
70 this.entityOwnershipUtils = new EntityOwnershipUtils(entityOwnershipService);
71 this.itmRpcService = itmRpcService;
72 this.l2gwService = l2gwService;
73 this.jobCoordinator = jobCoordinator;
74 this.l2GatewayCache = l2GatewayCache;
79 LOG.info("{} init", getClass().getSimpleName());
80 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
84 protected InstanceIdentifier<L2gateway> getWildCardPath() {
85 return InstanceIdentifier.create(Neutron.class).child(L2gateways.class).child(L2gateway.class);
89 protected void add(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
90 LOG.info("Adding L2gateway with ID: {}", input.getUuid());
92 List<Devices> l2Devices = input.getDevices();
93 for (Devices l2Device : l2Devices) {
94 LOG.trace("Adding L2gateway device: {}", l2Device);
95 addL2Device(l2Device, input);
100 protected void remove(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
101 LOG.info("Removing L2gateway with ID: {}", input.getUuid());
102 List<L2gatewayConnection> connections = l2gwService
103 .getL2GwConnectionsByL2GatewayId(input.getUuid());
104 Futures.addCallback(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
105 for (L2gatewayConnection connection : connections) {
106 InstanceIdentifier<L2gatewayConnection> iid = InstanceIdentifier.create(Neutron.class)
107 .child(L2gatewayConnections.class).child(L2gatewayConnection.class, connection.key());
108 tx.delete(LogicalDatastoreType.CONFIGURATION, iid);
110 }), new FutureCallback<Void>() {
112 public void onSuccess(Void result) {
113 List<Devices> l2Devices = input.getDevices();
114 for (Devices l2Device : l2Devices) {
115 LOG.trace("Removing L2gateway device: {}", l2Device);
116 removeL2Device(l2Device, input);
121 public void onFailure(Throwable throwable) {
122 LOG.error("Failed to delete associated l2gwconnection while deleting l2gw {} with id",
123 input.getUuid(), throwable);
125 }, MoreExecutors.directExecutor());
129 protected void update(InstanceIdentifier<L2gateway> identifier, L2gateway original, L2gateway update) {
130 LOG.trace("Updating L2gateway : key: {}, original value={}, update value={}", identifier, original, update);
131 List<L2gatewayConnection> connections = l2gwService.getAssociatedL2GwConnections(
132 Sets.newHashSet(update.getUuid()));
133 if (connections == null) {
134 LOG.warn("There are no connections associated with l2 gateway uuid {} name {}",
135 update.getUuid(), update.getName());
138 if (original.getDevices() == null) {
139 connections.forEach(l2gwService::addL2GatewayConnection);
142 jobCoordinator.enqueueJob("l2gw.update", () -> {
143 ListenableFuture<Void> future = txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
144 DeviceInterfaces updatedDeviceInterfaces = new DeviceInterfaces(update);
145 original.getDevices()
147 .filter((originalDevice) -> originalDevice.getInterfaces() != null)
148 .forEach((originalDevice) -> {
149 String deviceName = originalDevice.getDeviceName();
150 L2GatewayDevice l2GwDevice = l2GatewayCache.get(deviceName);
151 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(
152 new NodeId(l2GwDevice.getHwvtepNodeId()), deviceName);
153 originalDevice.getInterfaces()
155 .filter((intf) -> !updatedDeviceInterfaces.containsInterface(
156 deviceName, intf.getInterfaceName()))
157 .forEach((intf) -> connections.forEach((connection) -> {
158 Integer vlanId = connection.getSegmentId();
159 if (intf.getSegmentationIds() != null
160 && !intf.getSegmentationIds().isEmpty()) {
161 for (Integer vlan : intf.getSegmentationIds()) {
162 HwvtepUtils.deleteVlanBinding(tx,
163 physicalSwitchNodeId, intf.getInterfaceName(), vlan);
166 LOG.debug("Deleting vlan binding {} {} {}",
167 physicalSwitchNodeId, intf.getInterfaceName(), vlanId);
168 HwvtepUtils.deleteVlanBinding(tx, physicalSwitchNodeId,
169 intf.getInterfaceName(), vlanId);
174 Futures.addCallback(future, new FutureCallback<Void>() {
176 public void onSuccess(Void success) {
177 LOG.debug("Successfully deleted vlan bindings for l2gw update {}", update);
178 connections.forEach((l2GwConnection) ->
179 l2gwService.addL2GatewayConnection(l2GwConnection, null, update));
183 public void onFailure(Throwable throwable) {
184 LOG.error("Failed to delete vlan bindings as part of l2gw udpate {}", update);
186 }, MoreExecutors.directExecutor());
187 return Collections.singletonList(future);
188 }, SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
191 private synchronized void addL2Device(Devices l2Device, L2gateway input) {
192 String l2DeviceName = l2Device.getDeviceName();
194 L2GatewayDevice l2GwDevice = l2GatewayCache.addOrGet(l2DeviceName);
195 l2GwDevice.addL2GatewayId(input.getUuid());
196 if (l2GwDevice.getHwvtepNodeId() == null) {
197 LOG.info("L2GW provisioning skipped for device {}",l2DeviceName);
199 LOG.info("Provisioning l2gw for device {}",l2DeviceName);
200 l2gwService.provisionItmAndL2gwConnection(l2GwDevice, l2DeviceName, l2GwDevice.getHwvtepNodeId(),
201 l2GwDevice.getTunnelIp());
205 private void removeL2Device(Devices l2Device, L2gateway input) {
206 final String l2DeviceName = l2Device.getDeviceName();
207 L2GatewayDevice l2GwDevice = l2GatewayCache.get(l2DeviceName);
208 if (l2GwDevice != null) {
209 // Delete ITM tunnels if it's last Gateway deleted and device is connected
210 // Also, do not delete device from cache if it's connected
211 if (L2GatewayUtils.isLastL2GatewayBeingDeleted(l2GwDevice)) {
212 if (l2GwDevice.isConnected()) {
213 l2GwDevice.removeL2GatewayId(input.getUuid());
214 // Delete ITM tunnels
215 final String hwvtepId = l2GwDevice.getHwvtepNodeId();
217 final Set<IpAddress> tunnelIps = l2GwDevice.getTunnelIps();
218 jobCoordinator.enqueueJob(hwvtepId, () -> {
219 if (entityOwnershipUtils.isEntityOwner(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
220 HwvtepSouthboundConstants.ELAN_ENTITY_NAME)) {
221 LOG.info("Deleting ITM Tunnels for {} connected to cluster node owner", l2DeviceName);
222 for (IpAddress tunnelIp : tunnelIps) {
223 L2GatewayUtils.deleteItmTunnels(itmRpcService, hwvtepId, l2DeviceName, tunnelIp);
226 LOG.info("ITM Tunnels are not deleted on the cluster node as this is not owner for {}",
233 l2GatewayCache.remove(l2DeviceName);
234 // Cleaning up the config DS
235 NodeId nodeId = new NodeId(l2GwDevice.getHwvtepNodeId());
236 NodeId psNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, l2DeviceName);
237 //FIXME: These should be removed
238 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
239 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId));
240 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
241 HwvtepSouthboundUtils.createInstanceIdentifier(psNodeId));
245 l2GwDevice.removeL2GatewayId(input.getUuid());
246 LOG.trace("ITM tunnels are not deleted for {} as this device has other L2gateway associations",
250 LOG.error("Unable to find L2 Gateway details for {}", l2DeviceName);
255 protected L2GatewayListener getDataTreeChangeListener() {
259 static class DeviceInterfaces {
260 Map<String, Map<String, Interfaces>> deviceInterfacesMap = new HashMap<>();
262 DeviceInterfaces(L2gateway l2gateway) {
263 if (l2gateway.getDevices() != null) {
264 l2gateway.getDevices().forEach((device) -> {
265 deviceInterfacesMap.putIfAbsent(device.getDeviceName(), new HashMap<>());
266 if (device.getInterfaces() != null) {
267 device.getInterfaces().forEach((intf) ->
268 deviceInterfacesMap.get(device.getDeviceName()).put(intf.getInterfaceName(), intf));
274 boolean containsInterface(String deviceName, String interfaceName) {
275 if (deviceInterfacesMap.containsKey(deviceName)) {
276 return deviceInterfacesMap.get(deviceName).containsKey(interfaceName);