Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / neutronvpn / neutronvpn-impl / src / main / java / org / opendaylight / vpnservice / neutronvpn / l2gw / L2GatewayListener.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.vpnservice.neutronvpn.l2gw;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.Future;
15
16 import org.opendaylight.yangtools.concepts.ListenerRegistration;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.opendaylight.yangtools.yang.common.RpcResult;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
24 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
25 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
26 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
27 import org.opendaylight.vpnservice.utils.clustering.ClusteringUtils;
28 import org.opendaylight.vpnservice.datastoreutils.AsyncClusteredDataChangeListenerBase;
29 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.L2GatewayDevice;
30 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
31 import org.opendaylight.vpnservice.utils.hwvtep.HwvtepSouthboundConstants;
32 import org.opendaylight.vpnservice.utils.hwvtep.HwvtepSouthboundUtils;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.ItmRpcService;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import com.google.common.util.concurrent.ListenableFuture;
45 import com.google.common.util.concurrent.FutureCallback;
46 import com.google.common.util.concurrent.Futures;
47
48 public class L2GatewayListener extends AsyncClusteredDataChangeListenerBase<L2gateway, L2GatewayListener>
49         implements AutoCloseable {
50     private static final Logger LOG = LoggerFactory.getLogger(L2GatewayListener.class);
51
52     private ListenerRegistration<DataChangeListener> listenerRegistration;
53     private final DataBroker broker;
54     private ItmRpcService itmRpcService;
55     private EntityOwnershipService entityOwnershipService;
56
57     public L2GatewayListener(final DataBroker db, RpcProviderRegistry rpcRegistry,
58             EntityOwnershipService entityOwnershipService) {
59         super(L2gateway.class, L2GatewayListener.class);
60         broker = db;
61         this.entityOwnershipService = entityOwnershipService;
62         itmRpcService = rpcRegistry.getRpcService(ItmRpcService.class);
63         registerListener(db);
64     }
65
66     @Override
67     public void close() throws Exception {
68         if (listenerRegistration != null) {
69             try {
70                 listenerRegistration.close();
71             } catch (final Exception e) {
72                 LOG.error("Error when cleaning up DataChangeListener.", e);
73             }
74             listenerRegistration = null;
75         }
76         LOG.info("L2 Gateway listener Closed");
77     }
78
79     private void registerListener(final DataBroker db) {
80         try {
81             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
82                     InstanceIdentifier.create(Neutron.class).child(L2gateways.class).child(L2gateway.class),
83                     L2GatewayListener.this, DataChangeScope.SUBTREE);
84         } catch (final Exception e) {
85             LOG.error("Neutron Manager L2 Gateway DataChange listener registration fail!", e);
86             throw new IllegalStateException("Neutron Manager L2 Gateway DataChange listener registration failed.", e);
87         }
88     }
89
90     @Override
91     protected void add(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
92         LOG.info("Adding L2gateway with ID: {}", input.getUuid());
93
94         List<Devices> l2Devices = input.getDevices();
95         for (Devices l2Device : l2Devices) {
96             if (LOG.isTraceEnabled()) {
97                 LOG.trace("Adding L2gateway device: {}", l2Device);
98             }
99             addL2Device(l2Device, input);
100         }
101     }
102
103     @Override
104     protected void remove(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
105         LOG.info("Removing L2gateway with ID: {}", input.getUuid());
106
107         List<Devices> l2Devices = input.getDevices();
108         for (Devices l2Device : l2Devices) {
109             if (LOG.isTraceEnabled()) {
110                 LOG.trace("Removing L2gateway device: {}", l2Device);
111             }
112             removeL2Device(l2Device, input);
113         }
114     }
115
116     @Override
117     protected void update(InstanceIdentifier<L2gateway> identifier, L2gateway original, L2gateway update) {
118         if (LOG.isTraceEnabled()) {
119             LOG.trace("Updating L2gateway : key: " + identifier + ", original value=" + original + ", update value="
120                     + update);
121         }
122     }
123
124     private void addL2Device(Devices l2Device, L2gateway input) {
125         final String l2DeviceName = l2Device.getDeviceName();
126         L2GatewayDevice l2GwDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
127         if (l2GwDevice != null) {
128             if (!L2GatewayUtils.isGatewayAssociatedToL2Device(l2GwDevice)
129                     && L2GatewayUtils.isL2GwDeviceConnected(l2GwDevice)) {
130                 // VTEP already discovered; create ITM tunnels
131                 final String hwvtepId = l2GwDevice.getHwvtepNodeId();
132                 InstanceIdentifier<Node> iid = HwvtepSouthboundUtils.createInstanceIdentifier(new NodeId(hwvtepId));
133                 ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(
134                         entityOwnershipService, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
135                         HwvtepSouthboundConstants.ELAN_ENTITY_NAME);
136                 final List<IpAddress> tunnelIps = l2GwDevice.getTunnelIps();
137                 Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
138                     @Override
139                     public void onSuccess(Boolean isOwner) {
140                         if (isOwner) {
141                             LOG.info("Creating ITM Tunnels for {} connected to cluster node owner", l2DeviceName);
142                             for (IpAddress tunnelIp : tunnelIps) {
143                                 L2GatewayUtils.createItmTunnels(itmRpcService, hwvtepId, l2DeviceName, tunnelIp);
144                             }
145                         } else {
146                             LOG.info("ITM Tunnels are not created on the cluster node as this is not owner for {}",
147                                     l2DeviceName);
148                         }
149                     }
150
151                     @Override
152                     public void onFailure(Throwable error) {
153                         LOG.error("Failed to create ITM tunnels", error);
154                     }
155                 });
156             } else {
157                 LOG.trace("ITM tunnels are already created for device {}", l2DeviceName);
158             }
159         } else {
160             LOG.trace("{} is not connected; ITM tunnels will be created when device comes up", l2DeviceName);
161             // Pre-provision scenario. Create L2GatewayDevice without VTEP
162             // details for pushing configurations as soon as device discovered
163             l2GwDevice = new L2GatewayDevice();
164             l2GwDevice.setDeviceName(l2DeviceName);
165             L2GatewayCacheUtils.addL2DeviceToCache(l2DeviceName, l2GwDevice);
166         }
167         l2GwDevice.addL2GatewayId(input.getUuid());
168     }
169
170     private void removeL2Device(Devices l2Device, L2gateway input) {
171         final String l2DeviceName = l2Device.getDeviceName();
172         L2GatewayDevice l2GwDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
173         if (l2GwDevice != null) {
174             // Delete ITM tunnels if it's last Gateway deleted and device is connected
175             // Also, do not delete device from cache if it's connected
176             if (L2GatewayUtils.isLastL2GatewayBeingDeleted(l2GwDevice)) {
177                 if (L2GatewayUtils.isL2GwDeviceConnected(l2GwDevice)) {
178                     l2GwDevice.removeL2GatewayId(input.getUuid());
179                     // Delete ITM tunnels
180                     final String hwvtepId = l2GwDevice.getHwvtepNodeId();
181                     InstanceIdentifier<Node> iid = HwvtepSouthboundUtils.createInstanceIdentifier(new NodeId(hwvtepId));
182                     ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(
183                             entityOwnershipService, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
184                             HwvtepSouthboundConstants.ELAN_ENTITY_NAME);
185                     final List<IpAddress> tunnelIps = l2GwDevice.getTunnelIps();
186                     Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
187                         @Override
188                         public void onSuccess(Boolean isOwner) {
189                             if (isOwner) {
190                                 LOG.info("Deleting ITM Tunnels for {} connected to cluster node owner", l2DeviceName);
191                                 for (IpAddress tunnelIp : tunnelIps) {
192                                     L2GatewayUtils.deleteItmTunnels(itmRpcService, hwvtepId, l2DeviceName, tunnelIp);
193                                 }
194                             } else {
195                                 LOG.info("ITM Tunnels are not deleted on the cluster node as this is not owner for {}",
196                                         l2DeviceName);
197                             }
198                         }
199
200                         @Override
201                         public void onFailure(Throwable error) {
202                             LOG.error("Failed to delete ITM tunnels", error);
203                         }
204                     });
205                 } else {
206                     L2GatewayCacheUtils.removeL2DeviceFromCache(l2DeviceName);
207                 }
208             } else {
209                 l2GwDevice.removeL2GatewayId(input.getUuid());
210                 LOG.trace("ITM tunnels are not deleted for {} as this device has other L2gateway associations",
211                         l2DeviceName);
212             }
213         } else {
214             LOG.error("Unable to find L2 Gateway details for {}", l2DeviceName);
215         }
216     }
217
218     @Override
219     protected InstanceIdentifier<L2gateway> getWildCardPath() {
220         return InstanceIdentifier.create(L2gateway.class);
221     }
222
223     @Override
224     protected ClusteredDataChangeListener getDataChangeListener() {
225         return L2GatewayListener.this;
226     }
227
228     @Override
229     protected DataChangeScope getDataChangeScope() {
230         return AsyncDataBroker.DataChangeScope.BASE;
231     }
232 }