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.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.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
19 import org.opendaylight.genius.mdsalutil.MDSALUtil;
20 import org.opendaylight.genius.utils.clustering.ClusteringUtils;
21 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
22 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
23 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
24 import org.opendaylight.netvirt.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class L2GatewayListener extends AsyncClusteredDataTreeChangeListenerBase<L2gateway, L2GatewayListener>
38 implements AutoCloseable {
39 private static final Logger LOG = LoggerFactory.getLogger(L2GatewayListener.class);
40 private final DataBroker dataBroker;
41 private final ItmRpcService itmRpcService;
42 private final EntityOwnershipService entityOwnershipService;
44 public L2GatewayListener(final DataBroker dataBroker, final EntityOwnershipService entityOwnershipService,
45 ItmRpcService itmRpcService) {
46 this.dataBroker = dataBroker;
47 this.entityOwnershipService = entityOwnershipService;
48 this.itmRpcService = itmRpcService;
52 LOG.info("{} start", getClass().getSimpleName());
53 L2GatewayCacheUtils.createL2DeviceCache();
54 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
58 protected InstanceIdentifier<L2gateway> getWildCardPath() {
59 return InstanceIdentifier.create(Neutron.class).child(L2gateways.class).child(L2gateway.class);
63 protected void add(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
64 LOG.info("Adding L2gateway with ID: {}", input.getUuid());
66 List<Devices> l2Devices = input.getDevices();
67 for (Devices l2Device : l2Devices) {
68 LOG.trace("Adding L2gateway device: {}", l2Device);
69 addL2Device(l2Device, input);
74 protected void remove(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
75 LOG.info("Removing L2gateway with ID: {}", input.getUuid());
77 List<Devices> l2Devices = input.getDevices();
78 for (Devices l2Device : l2Devices) {
79 LOG.trace("Removing L2gateway device: {}", l2Device);
80 removeL2Device(l2Device, input);
85 protected void update(InstanceIdentifier<L2gateway> identifier, L2gateway original, L2gateway update) {
86 LOG.trace("Updating L2gateway : key: {}, original value={}, update value={}", identifier, original, update);
89 private void addL2Device(Devices l2Device, L2gateway input) {
90 final String l2DeviceName = l2Device.getDeviceName();
91 L2GatewayDevice l2GwDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
92 if (l2GwDevice != null) {
93 if (!L2GatewayUtils.isGatewayAssociatedToL2Device(l2GwDevice)
94 && l2GwDevice.isConnected()) {
95 // VTEP already discovered; create ITM tunnels
96 final String hwvtepId = l2GwDevice.getHwvtepNodeId();
97 InstanceIdentifier<Node> iid = HwvtepSouthboundUtils.createInstanceIdentifier(new NodeId(hwvtepId));
98 ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(
99 entityOwnershipService, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
100 HwvtepSouthboundConstants.ELAN_ENTITY_NAME);
101 final Set<IpAddress> tunnelIps = l2GwDevice.getTunnelIps();
102 Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
104 public void onSuccess(Boolean isOwner) {
106 LOG.info("Creating ITM Tunnels for {} connected to cluster node owner", l2DeviceName);
107 for (IpAddress tunnelIp : tunnelIps) {
108 L2GatewayUtils.createItmTunnels(itmRpcService, hwvtepId, l2DeviceName, tunnelIp);
111 LOG.info("ITM Tunnels are not created on the cluster node as this is not owner for {}",
117 public void onFailure(Throwable error) {
118 LOG.error("Failed to create ITM tunnels", error);
122 LOG.trace("ITM tunnels are already created for device {}", l2DeviceName);
125 LOG.trace("{} is not connected; ITM tunnels will be created when device comes up", l2DeviceName);
126 // Pre-provision scenario. Create L2GatewayDevice without VTEP
127 // details for pushing configurations as soon as device discovered
128 l2GwDevice = new L2GatewayDevice();
129 l2GwDevice.setDeviceName(l2DeviceName);
130 L2GatewayCacheUtils.addL2DeviceToCache(l2DeviceName, l2GwDevice);
132 l2GwDevice.addL2GatewayId(input.getUuid());
135 private void removeL2Device(Devices l2Device, L2gateway input) {
136 final String l2DeviceName = l2Device.getDeviceName();
137 L2GatewayDevice l2GwDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
138 if (l2GwDevice != null) {
139 // Delete ITM tunnels if it's last Gateway deleted and device is connected
140 // Also, do not delete device from cache if it's connected
141 if (L2GatewayUtils.isLastL2GatewayBeingDeleted(l2GwDevice)) {
142 if (l2GwDevice.isConnected()) {
143 l2GwDevice.removeL2GatewayId(input.getUuid());
144 // Delete ITM tunnels
145 final String hwvtepId = l2GwDevice.getHwvtepNodeId();
146 InstanceIdentifier<Node> iid = HwvtepSouthboundUtils.createInstanceIdentifier(new NodeId(hwvtepId));
147 ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(
148 entityOwnershipService, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
149 HwvtepSouthboundConstants.ELAN_ENTITY_NAME);
150 final Set<IpAddress> tunnelIps = l2GwDevice.getTunnelIps();
151 Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
153 public void onSuccess(Boolean isOwner) {
155 LOG.info("Deleting ITM Tunnels for {} connected to cluster node owner", l2DeviceName);
156 for (IpAddress tunnelIp : tunnelIps) {
157 L2GatewayUtils.deleteItmTunnels(itmRpcService, hwvtepId, l2DeviceName, tunnelIp);
160 LOG.info("ITM Tunnels are not deleted on the cluster node as this is not owner for {}",
166 public void onFailure(Throwable error) {
167 LOG.error("Failed to delete ITM tunnels", error);
171 L2GatewayCacheUtils.removeL2DeviceFromCache(l2DeviceName);
172 // Cleaning up the config DS
173 NodeId nodeId = new NodeId(l2GwDevice.getHwvtepNodeId());
174 NodeId psNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, l2DeviceName);
175 //FIXME: These should be removed
176 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
177 HwvtepSouthboundUtils.createInstanceIdentifier(nodeId));
178 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
179 HwvtepSouthboundUtils.createInstanceIdentifier(psNodeId));
183 l2GwDevice.removeL2GatewayId(input.getUuid());
184 LOG.trace("ITM tunnels are not deleted for {} as this device has other L2gateway associations",
188 LOG.error("Unable to find L2 Gateway details for {}", l2DeviceName);
193 protected L2GatewayListener getDataTreeChangeListener() {