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 com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.List;
15 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
23 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase;
24 import org.opendaylight.genius.mdsalutil.MDSALUtil;
25 import org.opendaylight.genius.utils.clustering.ClusteringUtils;
26 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
27 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
28 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
29 import org.opendaylight.netvirt.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
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.genius.itm.rpcs.rev160406.ItmRpcService;
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.neutron.rev150712.Neutron;
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.opendaylight.yangtools.concepts.ListenerRegistration;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class L2GatewayListener extends AsyncClusteredDataChangeListenerBase<L2gateway, L2GatewayListener>
44 implements AutoCloseable {
45 private static final Logger LOG = LoggerFactory.getLogger(L2GatewayListener.class);
46 private final DataBroker dataBroker;
47 private final ItmRpcService itmRpcService;
48 private final EntityOwnershipService entityOwnershipService;
50 public L2GatewayListener(final DataBroker dataBroker, final EntityOwnershipService entityOwnershipService,
51 ItmRpcService itmRpcService) {
52 super(L2gateway.class, L2GatewayListener.class);
53 this.dataBroker = dataBroker;
54 this.entityOwnershipService = entityOwnershipService;
55 this.itmRpcService = itmRpcService;
59 LOG.info("{} start", getClass().getSimpleName());
60 L2GatewayCacheUtils.createL2DeviceCache();
61 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
65 protected InstanceIdentifier<L2gateway> getWildCardPath() {
66 return InstanceIdentifier.create(Neutron.class).child(L2gateways.class).child(L2gateway.class);
70 protected void add(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
71 LOG.info("Adding L2gateway with ID: {}", input.getUuid());
73 List<Devices> l2Devices = input.getDevices();
74 for (Devices l2Device : l2Devices) {
75 LOG.trace("Adding L2gateway device: {}", l2Device);
76 addL2Device(l2Device, input);
81 protected void remove(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
82 LOG.info("Removing L2gateway with ID: {}", input.getUuid());
84 List<Devices> l2Devices = input.getDevices();
85 for (Devices l2Device : l2Devices) {
86 LOG.trace("Removing L2gateway device: {}", l2Device);
87 removeL2Device(l2Device, input);
92 protected void update(InstanceIdentifier<L2gateway> identifier, L2gateway original, L2gateway update) {
93 LOG.trace("Updating L2gateway : key: {}, original value={}, update value={}", identifier, original, update);
96 private void addL2Device(Devices l2Device, L2gateway input) {
97 final String l2DeviceName = l2Device.getDeviceName();
98 L2GatewayDevice l2GwDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
99 if (l2GwDevice != null) {
100 if (!L2GatewayUtils.isGatewayAssociatedToL2Device(l2GwDevice)
101 && l2GwDevice.isConnected()) {
102 // VTEP already discovered; create ITM tunnels
103 final String hwvtepId = l2GwDevice.getHwvtepNodeId();
104 InstanceIdentifier<Node> iid = HwvtepSouthboundUtils.createInstanceIdentifier(new NodeId(hwvtepId));
105 ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(
106 entityOwnershipService, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
107 HwvtepSouthboundConstants.ELAN_ENTITY_NAME);
108 final Set<IpAddress> tunnelIps = l2GwDevice.getTunnelIps();
109 Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
111 public void onSuccess(Boolean isOwner) {
113 LOG.info("Creating ITM Tunnels for {} connected to cluster node owner", l2DeviceName);
114 for (IpAddress tunnelIp : tunnelIps) {
115 L2GatewayUtils.createItmTunnels(itmRpcService, hwvtepId, l2DeviceName, tunnelIp);
118 LOG.info("ITM Tunnels are not created on the cluster node as this is not owner for {}",
124 public void onFailure(Throwable error) {
125 LOG.error("Failed to create ITM tunnels", error);
129 LOG.trace("ITM tunnels are already created for device {}", l2DeviceName);
132 LOG.trace("{} is not connected; ITM tunnels will be created when device comes up", l2DeviceName);
133 // Pre-provision scenario. Create L2GatewayDevice without VTEP
134 // details for pushing configurations as soon as device discovered
135 l2GwDevice = new L2GatewayDevice();
136 l2GwDevice.setDeviceName(l2DeviceName);
137 L2GatewayCacheUtils.addL2DeviceToCache(l2DeviceName, l2GwDevice);
139 l2GwDevice.addL2GatewayId(input.getUuid());
142 private void removeL2Device(Devices l2Device, L2gateway input) {
143 final String l2DeviceName = l2Device.getDeviceName();
144 L2GatewayDevice l2GwDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
145 if (l2GwDevice != null) {
146 // Delete ITM tunnels if it's last Gateway deleted and device is connected
147 // Also, do not delete device from cache if it's connected
148 if (L2GatewayUtils.isLastL2GatewayBeingDeleted(l2GwDevice)) {
149 if(l2GwDevice.isConnected()){
150 l2GwDevice.removeL2GatewayId(input.getUuid());
151 // Delete ITM tunnels
152 final String hwvtepId = l2GwDevice.getHwvtepNodeId();
153 InstanceIdentifier<Node> iid = HwvtepSouthboundUtils.createInstanceIdentifier(new NodeId(hwvtepId));
154 ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(
155 entityOwnershipService, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
156 HwvtepSouthboundConstants.ELAN_ENTITY_NAME);
157 final Set<IpAddress> tunnelIps = l2GwDevice.getTunnelIps();
158 Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
160 public void onSuccess(Boolean isOwner) {
162 LOG.info("Deleting ITM Tunnels for {} connected to cluster node owner", l2DeviceName);
163 for (IpAddress tunnelIp : tunnelIps) {
164 L2GatewayUtils.deleteItmTunnels(itmRpcService, hwvtepId, l2DeviceName, tunnelIp);
167 LOG.info("ITM Tunnels are not deleted on the cluster node as this is not owner for {}",
173 public void onFailure(Throwable error) {
174 LOG.error("Failed to delete ITM tunnels", error);
178 L2GatewayCacheUtils.removeL2DeviceFromCache(l2DeviceName);
179 // Cleaning up the config DS
180 NodeId nodeId = new NodeId(l2GwDevice.getHwvtepNodeId());
181 NodeId psNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, l2DeviceName);
182 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION, HwvtepSouthboundUtils.createInstanceIdentifier(nodeId));
183 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION, HwvtepSouthboundUtils.createInstanceIdentifier(psNodeId));
187 l2GwDevice.removeL2GatewayId(input.getUuid());
188 LOG.trace("ITM tunnels are not deleted for {} as this device has other L2gateway associations",
192 LOG.error("Unable to find L2 Gateway details for {}", l2DeviceName);
197 protected ClusteredDataChangeListener getDataChangeListener() {
202 protected DataChangeScope getDataChangeScope() {
203 return AsyncDataBroker.DataChangeScope.SUBTREE;