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.ArrayList;
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.binding.api.ReadWriteTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
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 ItmRpcService itmRpcService;
58 private final IL2gwService l2gwService;
59 private final EntityOwnershipUtils entityOwnershipUtils;
60 private final JobCoordinator jobCoordinator;
61 private final L2GatewayCache l2GatewayCache;
64 public L2GatewayListener(final DataBroker dataBroker, final EntityOwnershipService entityOwnershipService,
65 final ItmRpcService itmRpcService, final IL2gwService l2gwService,
66 final JobCoordinator jobCoordinator, final L2GatewayCache l2GatewayCache) {
67 this.dataBroker = dataBroker;
68 this.entityOwnershipUtils = new EntityOwnershipUtils(entityOwnershipService);
69 this.itmRpcService = itmRpcService;
70 this.l2gwService = l2gwService;
71 this.jobCoordinator = jobCoordinator;
72 this.l2GatewayCache = l2GatewayCache;
77 LOG.info("{} init", getClass().getSimpleName());
78 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
82 protected InstanceIdentifier<L2gateway> getWildCardPath() {
83 return InstanceIdentifier.create(Neutron.class).child(L2gateways.class).child(L2gateway.class);
87 protected void add(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
88 LOG.info("Adding L2gateway with ID: {}", input.getUuid());
90 List<Devices> l2Devices = input.getDevices();
91 for (Devices l2Device : l2Devices) {
92 LOG.trace("Adding L2gateway device: {}", l2Device);
93 addL2Device(l2Device, input);
98 protected void remove(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
99 LOG.info("Removing L2gateway with ID: {}", input.getUuid());
100 List<L2gatewayConnection> connections = l2gwService
101 .getL2GwConnectionsByL2GatewayId(input.getUuid());
103 ReadWriteTransaction tx = this.dataBroker.newReadWriteTransaction();
104 for (L2gatewayConnection connection : connections) {
105 InstanceIdentifier<L2gatewayConnection> iid = InstanceIdentifier.create(Neutron.class)
106 .child(L2gatewayConnections.class).child(L2gatewayConnection.class, connection.getKey());
107 tx.delete(LogicalDatastoreType.CONFIGURATION, iid);
109 tx.submit().checkedGet();
110 } catch (TransactionCommitFailedException e) {
111 LOG.error("Failed to delete associated l2gwconnection while deleting l2gw {} with id beacause of {}",
112 input.getUuid(), e.getLocalizedMessage());
115 List<Devices> l2Devices = input.getDevices();
116 for (Devices l2Device : l2Devices) {
117 LOG.trace("Removing L2gateway device: {}", l2Device);
118 removeL2Device(l2Device, input);
123 protected void update(InstanceIdentifier<L2gateway> identifier, L2gateway original, L2gateway update) {
124 LOG.trace("Updating L2gateway : key: {}, original value={}, update value={}", identifier, original, update);
125 List<L2gatewayConnection> connections = l2gwService.getAssociatedL2GwConnections(
126 Sets.newHashSet(update.getUuid()));
127 if (connections == null) {
128 LOG.warn("There are no connections associated with l2 gateway uuid {} name {}",
129 update.getUuid(), update.getName());
132 if (original.getDevices() == null) {
134 (connection) -> l2gwService.addL2GatewayConnection(connection));
137 jobCoordinator.enqueueJob("l2gw.update", () -> {
138 ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
139 DeviceInterfaces updatedDeviceInterfaces = new DeviceInterfaces(update);
140 List<ListenableFuture<Void>> fts = new ArrayList<>();
141 original.getDevices()
143 .filter((originalDevice) -> originalDevice.getInterfaces() != null)
144 .forEach((originalDevice) -> {
145 String deviceName = originalDevice.getDeviceName();
146 L2GatewayDevice l2GwDevice = l2GatewayCache.get(deviceName);
147 NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(
148 new NodeId(l2GwDevice.getHwvtepNodeId()), deviceName);
149 originalDevice.getInterfaces()
151 .filter((intf) -> !updatedDeviceInterfaces.containsInterface(
152 deviceName, intf.getInterfaceName()))
154 connections.forEach((connection) -> {
155 Integer vlanId = connection.getSegmentId();
156 if (intf.getSegmentationIds() != null
157 && !intf.getSegmentationIds().isEmpty()) {
158 for (Integer vlan : intf.getSegmentationIds()) {
159 HwvtepUtils.deleteVlanBinding(transaction,
160 physicalSwitchNodeId, intf.getInterfaceName(), vlan);
163 LOG.debug("Deleting vlan binding {} {} {}",
164 physicalSwitchNodeId, intf.getInterfaceName(), vlanId);
165 HwvtepUtils.deleteVlanBinding(transaction, physicalSwitchNodeId,
166 intf.getInterfaceName(), vlanId);
171 fts.add(transaction.submit());
172 Futures.addCallback(fts.get(0), new FutureCallback<Void>() {
174 public void onSuccess(Void success) {
175 LOG.debug("Successfully deleted vlan bindings for l2gw update {}", update);
176 connections.forEach((l2GwConnection) ->
177 l2gwService.addL2GatewayConnection(l2GwConnection, null, update));
181 public void onFailure(Throwable throwable) {
182 LOG.error("Failed to delete vlan bindings as part of l2gw udpate {}", update);
184 }, MoreExecutors.directExecutor());
186 }, SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
189 private synchronized void addL2Device(Devices l2Device, L2gateway input) {
190 String l2DeviceName = l2Device.getDeviceName();
192 L2GatewayDevice l2GwDevice = l2GatewayCache.addOrGet(l2DeviceName);
193 l2GwDevice.addL2GatewayId(input.getUuid());
194 if (l2GwDevice.getHwvtepNodeId() == null) {
195 LOG.info("L2GW provisioning skipped for device {}",l2DeviceName);
197 LOG.info("Provisioning l2gw for device {}",l2DeviceName);
198 l2gwService.provisionItmAndL2gwConnection(l2GwDevice, l2DeviceName, l2GwDevice.getHwvtepNodeId(),
199 l2GwDevice.getTunnelIp());
203 private void removeL2Device(Devices l2Device, L2gateway input) {
204 final String l2DeviceName = l2Device.getDeviceName();
205 L2GatewayDevice l2GwDevice = l2GatewayCache.get(l2DeviceName);
206 if (l2GwDevice != null) {
207 // Delete ITM tunnels if it's last Gateway deleted and device is connected
208 // Also, do not delete device from cache if it's connected
209 if (L2GatewayUtils.isLastL2GatewayBeingDeleted(l2GwDevice)) {
210 if (l2GwDevice.isConnected()) {
211 l2GwDevice.removeL2GatewayId(input.getUuid());
212 // Delete ITM tunnels
213 final String hwvtepId = l2GwDevice.getHwvtepNodeId();
215 final Set<IpAddress> tunnelIps = l2GwDevice.getTunnelIps();
216 jobCoordinator.enqueueJob(hwvtepId, () -> {
217 if (entityOwnershipUtils.isEntityOwner(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
218 HwvtepSouthboundConstants.ELAN_ENTITY_NAME)) {
219 LOG.info("Deleting ITM Tunnels for {} connected to cluster node owner", l2DeviceName);
220 for (IpAddress tunnelIp : tunnelIps) {
221 L2GatewayUtils.deleteItmTunnels(itmRpcService, hwvtepId, l2DeviceName, tunnelIp);
224 LOG.info("ITM Tunnels are not deleted on the cluster node as this is not owner for {}",
231 l2GatewayCache.remove(l2DeviceName);
232 // Cleaning up the config DS
233 NodeId nodeId = new NodeId(l2GwDevice.getHwvtepNodeId());
234 NodeId psNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, l2DeviceName);
235 //FIXME: These should be removed
236 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
237 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId));
238 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
239 HwvtepSouthboundUtils.createInstanceIdentifier(psNodeId));
243 l2GwDevice.removeL2GatewayId(input.getUuid());
244 LOG.trace("ITM tunnels are not deleted for {} as this device has other L2gateway associations",
248 LOG.error("Unable to find L2 Gateway details for {}", l2DeviceName);
253 protected L2GatewayListener getDataTreeChangeListener() {
257 static class DeviceInterfaces {
258 Map<String, Map<String, Interfaces>> deviceInterfacesMap = new HashMap<>();
260 DeviceInterfaces(L2gateway l2gateway) {
261 if (l2gateway.getDevices() != null) {
262 l2gateway.getDevices().forEach((device) -> {
263 deviceInterfacesMap.putIfAbsent(device.getDeviceName(), new HashMap<>());
264 if (device.getInterfaces() != null) {
265 device.getInterfaces().forEach((intf) ->
266 deviceInterfacesMap.get(device.getDeviceName()).put(intf.getInterfaceName(), intf));
272 boolean containsInterface(String deviceName, String interfaceName) {
273 if (deviceInterfacesMap.containsKey(deviceName)) {
274 return deviceInterfacesMap.get(deviceName).containsKey(interfaceName);