7da13c64466eb140955251a4ff2b678e669bc922
[netvirt.git] / vpnservice / neutronvpn / neutronvpn-impl / src / main / java / org / opendaylight / netvirt / 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.netvirt.neutronvpn.l2gw;
9
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;
14 import java.util.Set;
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;
42
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;
49
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;
56     }
57
58     public void start() {
59         LOG.info("{} start", getClass().getSimpleName());
60         L2GatewayCacheUtils.createL2DeviceCache();
61         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
62     }
63
64     @Override
65     protected InstanceIdentifier<L2gateway> getWildCardPath() {
66         return InstanceIdentifier.create(Neutron.class).child(L2gateways.class).child(L2gateway.class);
67     }
68
69     @Override
70     protected void add(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
71         LOG.info("Adding L2gateway with ID: {}", input.getUuid());
72
73         List<Devices> l2Devices = input.getDevices();
74         for (Devices l2Device : l2Devices) {
75             LOG.trace("Adding L2gateway device: {}", l2Device);
76             addL2Device(l2Device, input);
77         }
78     }
79
80     @Override
81     protected void remove(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
82         LOG.info("Removing L2gateway with ID: {}", input.getUuid());
83
84         List<Devices> l2Devices = input.getDevices();
85         for (Devices l2Device : l2Devices) {
86             LOG.trace("Removing L2gateway device: {}", l2Device);
87             removeL2Device(l2Device, input);
88         }
89     }
90
91     @Override
92     protected void update(InstanceIdentifier<L2gateway> identifier, L2gateway original, L2gateway update) {
93         LOG.trace("Updating L2gateway : key: {}, original value={}, update value={}", identifier, original, update);
94     }
95
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>() {
110                     @Override
111                     public void onSuccess(Boolean isOwner) {
112                         if (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);
116                             }
117                         } else {
118                             LOG.info("ITM Tunnels are not created on the cluster node as this is not owner for {}",
119                                     l2DeviceName);
120                         }
121                     }
122
123                     @Override
124                     public void onFailure(Throwable error) {
125                         LOG.error("Failed to create ITM tunnels", error);
126                     }
127                 });
128             } else {
129                 LOG.trace("ITM tunnels are already created for device {}", l2DeviceName);
130             }
131         } else {
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);
138         }
139         l2GwDevice.addL2GatewayId(input.getUuid());
140     }
141
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>() {
159                         @Override
160                         public void onSuccess(Boolean isOwner) {
161                             if (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);
165                                 }
166                             } else {
167                                 LOG.info("ITM Tunnels are not deleted on the cluster node as this is not owner for {}",
168                                         l2DeviceName);
169                             }
170                         }
171
172                         @Override
173                         public void onFailure(Throwable error) {
174                             LOG.error("Failed to delete ITM tunnels", error);
175                         }
176                     });
177                 } else {
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));
184
185                 }
186             } else {
187                 l2GwDevice.removeL2GatewayId(input.getUuid());
188                 LOG.trace("ITM tunnels are not deleted for {} as this device has other L2gateway associations",
189                         l2DeviceName);
190             }
191         } else {
192             LOG.error("Unable to find L2 Gateway details for {}", l2DeviceName);
193         }
194     }
195
196     @Override
197     protected ClusteredDataChangeListener getDataChangeListener() {
198         return this;
199     }
200
201     @Override
202     protected DataChangeScope getDataChangeScope() {
203         return AsyncDataBroker.DataChangeScope.SUBTREE;
204     }
205 }