2 * Copyright (c) 2016 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 java.util.List;
13 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
14 import org.opendaylight.netvirt.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
15 import org.opendaylight.yangtools.concepts.ListenerRegistration;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
25 import org.opendaylight.genius.utils.clustering.ClusteringUtils;
26 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase;
27 import org.opendaylight.genius.mdsalutil.MDSALUtil;
28 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
29 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.google.common.util.concurrent.ListenableFuture;
42 import com.google.common.util.concurrent.FutureCallback;
43 import com.google.common.util.concurrent.Futures;
45 public class L2GatewayListener extends AsyncClusteredDataChangeListenerBase<L2gateway, L2GatewayListener>
46 implements AutoCloseable {
47 private static final Logger LOG = LoggerFactory.getLogger(L2GatewayListener.class);
49 private ListenerRegistration<DataChangeListener> listenerRegistration;
50 private final DataBroker broker;
51 private ItmRpcService itmRpcService;
52 private EntityOwnershipService entityOwnershipService;
54 public L2GatewayListener(final DataBroker db, RpcProviderRegistry rpcRegistry,
55 EntityOwnershipService entityOwnershipService) {
56 super(L2gateway.class, L2GatewayListener.class);
58 this.entityOwnershipService = entityOwnershipService;
59 itmRpcService = rpcRegistry.getRpcService(ItmRpcService.class);
64 public void close() throws Exception {
65 if (listenerRegistration != null) {
67 listenerRegistration.close();
68 } catch (final Exception e) {
69 LOG.error("Error when cleaning up DataChangeListener.", e);
71 listenerRegistration = null;
73 LOG.info("L2 Gateway listener Closed");
76 private void registerListener(final DataBroker db) {
78 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
79 InstanceIdentifier.create(Neutron.class).child(L2gateways.class).child(L2gateway.class),
80 L2GatewayListener.this, DataChangeScope.SUBTREE);
81 } catch (final Exception e) {
82 LOG.error("Neutron Manager L2 Gateway DataChange listener registration fail!", e);
83 throw new IllegalStateException("Neutron Manager L2 Gateway DataChange listener registration failed.", e);
88 protected void add(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
89 LOG.info("Adding L2gateway with ID: {}", input.getUuid());
91 List<Devices> l2Devices = input.getDevices();
92 for (Devices l2Device : l2Devices) {
93 if (LOG.isTraceEnabled()) {
94 LOG.trace("Adding L2gateway device: {}", l2Device);
96 addL2Device(l2Device, input);
101 protected void remove(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
102 LOG.info("Removing L2gateway with ID: {}", input.getUuid());
104 List<Devices> l2Devices = input.getDevices();
105 for (Devices l2Device : l2Devices) {
106 if (LOG.isTraceEnabled()) {
107 LOG.trace("Removing L2gateway device: {}", l2Device);
109 removeL2Device(l2Device, input);
114 protected void update(InstanceIdentifier<L2gateway> identifier, L2gateway original, L2gateway update) {
115 if (LOG.isTraceEnabled()) {
116 LOG.trace("Updating L2gateway : key: " + identifier + ", original value=" + original + ", update value="
121 private void addL2Device(Devices l2Device, L2gateway input) {
122 final String l2DeviceName = l2Device.getDeviceName();
123 L2GatewayDevice l2GwDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
124 if (l2GwDevice != null) {
125 if (!L2GatewayUtils.isGatewayAssociatedToL2Device(l2GwDevice)
126 && l2GwDevice.isConnected()) {
127 // VTEP already discovered; create ITM tunnels
128 final String hwvtepId = l2GwDevice.getHwvtepNodeId();
129 InstanceIdentifier<Node> iid = HwvtepSouthboundUtils.createInstanceIdentifier(new NodeId(hwvtepId));
130 ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(
131 entityOwnershipService, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
132 HwvtepSouthboundConstants.ELAN_ENTITY_NAME);
133 final Set<IpAddress> tunnelIps = l2GwDevice.getTunnelIps();
134 Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
136 public void onSuccess(Boolean isOwner) {
138 LOG.info("Creating ITM Tunnels for {} connected to cluster node owner", l2DeviceName);
139 for (IpAddress tunnelIp : tunnelIps) {
140 L2GatewayUtils.createItmTunnels(itmRpcService, hwvtepId, l2DeviceName, tunnelIp);
143 LOG.info("ITM Tunnels are not created on the cluster node as this is not owner for {}",
149 public void onFailure(Throwable error) {
150 LOG.error("Failed to create ITM tunnels", error);
154 LOG.trace("ITM tunnels are already created for device {}", l2DeviceName);
157 LOG.trace("{} is not connected; ITM tunnels will be created when device comes up", l2DeviceName);
158 // Pre-provision scenario. Create L2GatewayDevice without VTEP
159 // details for pushing configurations as soon as device discovered
160 l2GwDevice = new L2GatewayDevice();
161 l2GwDevice.setDeviceName(l2DeviceName);
162 L2GatewayCacheUtils.addL2DeviceToCache(l2DeviceName, l2GwDevice);
164 l2GwDevice.addL2GatewayId(input.getUuid());
167 private void removeL2Device(Devices l2Device, L2gateway input) {
168 final String l2DeviceName = l2Device.getDeviceName();
169 L2GatewayDevice l2GwDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
170 if (l2GwDevice != null) {
171 // Delete ITM tunnels if it's last Gateway deleted and device is connected
172 // Also, do not delete device from cache if it's connected
173 if (L2GatewayUtils.isLastL2GatewayBeingDeleted(l2GwDevice)) {
174 if(l2GwDevice.isConnected()){
175 l2GwDevice.removeL2GatewayId(input.getUuid());
176 // Delete ITM tunnels
177 final String hwvtepId = l2GwDevice.getHwvtepNodeId();
178 InstanceIdentifier<Node> iid = HwvtepSouthboundUtils.createInstanceIdentifier(new NodeId(hwvtepId));
179 ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(
180 entityOwnershipService, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
181 HwvtepSouthboundConstants.ELAN_ENTITY_NAME);
182 final Set<IpAddress> tunnelIps = l2GwDevice.getTunnelIps();
183 Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
185 public void onSuccess(Boolean isOwner) {
187 LOG.info("Deleting ITM Tunnels for {} connected to cluster node owner", l2DeviceName);
188 for (IpAddress tunnelIp : tunnelIps) {
189 L2GatewayUtils.deleteItmTunnels(itmRpcService, hwvtepId, l2DeviceName, tunnelIp);
192 LOG.info("ITM Tunnels are not deleted on the cluster node as this is not owner for {}",
198 public void onFailure(Throwable error) {
199 LOG.error("Failed to delete ITM tunnels", error);
203 L2GatewayCacheUtils.removeL2DeviceFromCache(l2DeviceName);
204 // Cleaning up the config DS
205 NodeId nodeId = new NodeId(l2GwDevice.getHwvtepNodeId());
206 NodeId psNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, l2DeviceName);
207 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, HwvtepSouthboundUtils.createInstanceIdentifier(nodeId));
208 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, HwvtepSouthboundUtils.createInstanceIdentifier(psNodeId));
212 l2GwDevice.removeL2GatewayId(input.getUuid());
213 LOG.trace("ITM tunnels are not deleted for {} as this device has other L2gateway associations",
217 LOG.error("Unable to find L2 Gateway details for {}", l2DeviceName);
222 protected InstanceIdentifier<L2gateway> getWildCardPath() {
223 return InstanceIdentifier.create(L2gateway.class);
227 protected ClusteredDataChangeListener getDataChangeListener() {
228 return L2GatewayListener.this;
232 protected DataChangeScope getDataChangeScope() {
233 return AsyncDataBroker.DataChangeScope.BASE;