edd2cdaf1fda9d955f2294c6a22551eab7256ba7
[netvirt.git] /
1 /*
2  * Copyright (c) 2016, 2017 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.collect.Sets;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21 import javax.annotation.PostConstruct;
22 import javax.inject.Inject;
23 import javax.inject.Singleton;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
29 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
30 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
31 import org.opendaylight.genius.mdsalutil.MDSALUtil;
32 import org.opendaylight.genius.utils.SystemPropertyReader;
33 import org.opendaylight.genius.utils.clustering.ClusteringUtils;
34 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
35 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
36 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
37 import org.opendaylight.netvirt.elanmanager.api.IL2gwService;
38 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
39 import org.opendaylight.netvirt.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.devices.Interfaces;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gateway;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 @Singleton
56 public class L2GatewayListener extends AsyncClusteredDataTreeChangeListenerBase<L2gateway, L2GatewayListener>
57         implements AutoCloseable {
58     private static final Logger LOG = LoggerFactory.getLogger(L2GatewayListener.class);
59     private final DataBroker dataBroker;
60     private final ItmRpcService itmRpcService;
61     private final IL2gwService l2gwService;
62     private final EntityOwnershipService entityOwnershipService;
63
64     @Inject
65     public L2GatewayListener(final DataBroker dataBroker, final EntityOwnershipService entityOwnershipService,
66                              final ItmRpcService itmRpcService, IL2gwService l2gwService) {
67         this.dataBroker = dataBroker;
68         this.entityOwnershipService = entityOwnershipService;
69         this.itmRpcService = itmRpcService;
70         this.l2gwService = l2gwService;
71     }
72
73     @PostConstruct
74     public void init() {
75         LOG.info("{} init", getClass().getSimpleName());
76         L2GatewayCacheUtils.createL2DeviceCache();
77         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
78     }
79
80     @Override
81     protected InstanceIdentifier<L2gateway> getWildCardPath() {
82         return InstanceIdentifier.create(Neutron.class).child(L2gateways.class).child(L2gateway.class);
83     }
84
85     @Override
86     protected void add(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
87         LOG.info("Adding L2gateway with ID: {}", input.getUuid());
88
89         List<Devices> l2Devices = input.getDevices();
90         for (Devices l2Device : l2Devices) {
91             LOG.trace("Adding L2gateway device: {}", l2Device);
92             addL2Device(l2Device, input);
93         }
94     }
95
96     @Override
97     protected void remove(final InstanceIdentifier<L2gateway> identifier, final L2gateway input) {
98         LOG.info("Removing L2gateway with ID: {}", input.getUuid());
99         List<L2gatewayConnection> connections = l2gwService
100                 .getL2GwConnectionsByL2GatewayId(input.getUuid());
101         try {
102             ReadWriteTransaction tx = this.dataBroker.newReadWriteTransaction();
103             for (L2gatewayConnection connection : connections) {
104                 InstanceIdentifier<L2gatewayConnection> iid = InstanceIdentifier.create(Neutron.class)
105                         .child(L2gatewayConnections.class).child(L2gatewayConnection.class, connection.getKey());
106                 tx.delete(LogicalDatastoreType.CONFIGURATION, iid);
107             }
108             tx.submit().checkedGet();
109         } catch (TransactionCommitFailedException e) {
110             LOG.error("Failed to delete associated l2gwconnection while deleting l2gw {} with id beacause of {}",
111                     input.getUuid(), e.getLocalizedMessage());
112             //TODO :retry
113         }
114         List<Devices> l2Devices = input.getDevices();
115         for (Devices l2Device : l2Devices) {
116             LOG.trace("Removing L2gateway device: {}", l2Device);
117             removeL2Device(l2Device, input);
118         }
119     }
120
121     @Override
122     protected void update(InstanceIdentifier<L2gateway> identifier, L2gateway original, L2gateway update) {
123         LOG.trace("Updating L2gateway : key: {}, original value={}, update value={}", identifier, original, update);
124         List<L2gatewayConnection> connections = l2gwService.getAssociatedL2GwConnections(
125                 dataBroker, Sets.newHashSet(update.getUuid()));
126         if (connections == null) {
127             LOG.warn("There are no connections associated with l2 gateway uuid {} name {}",
128                     update.getUuid(), update.getName());
129             return;
130         }
131         if (original.getDevices() == null) {
132             connections.forEach(
133                 (connection) -> l2gwService.addL2GatewayConnection(connection));
134             return;
135         }
136         DataStoreJobCoordinator.getInstance().enqueueJob("l2gw.update", () -> {
137             ReadWriteTransaction transaction = dataBroker.newReadWriteTransaction();
138             DeviceInterfaces updatedDeviceInterfaces = new DeviceInterfaces(update);
139             List<ListenableFuture<Void>> fts = new ArrayList<ListenableFuture<Void>>();
140             original.getDevices()
141                     .stream()
142                     .filter((originalDevice) -> originalDevice.getInterfaces() != null)
143                     .forEach((originalDevice) -> {
144                         String deviceName = originalDevice.getDeviceName();
145                         L2GatewayDevice l2GwDevice = L2GatewayCacheUtils.getL2DeviceFromCache(deviceName);
146                         NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(
147                                 new NodeId(l2GwDevice.getHwvtepNodeId()), deviceName);
148                         originalDevice.getInterfaces()
149                                 .stream()
150                                 .filter((intf) -> !updatedDeviceInterfaces.containsInterface(
151                                         deviceName, intf.getInterfaceName()))
152                                 .forEach((intf) -> {
153                                     connections.forEach((connection) -> {
154                                         Integer vlanId = connection.getSegmentId();
155                                         if (intf.getSegmentationIds() != null
156                                                 && !intf.getSegmentationIds().isEmpty()) {
157                                             for (Integer vlan : intf.getSegmentationIds()) {
158                                                 HwvtepUtils.deleteVlanBinding(transaction,
159                                                         physicalSwitchNodeId, intf.getInterfaceName(), vlan);
160                                             }
161                                         } else {
162                                             LOG.debug("Deleting vlan binding {} {} {}",
163                                                     physicalSwitchNodeId, intf.getInterfaceName(), vlanId);
164                                             HwvtepUtils.deleteVlanBinding(transaction, physicalSwitchNodeId,
165                                                     intf.getInterfaceName(), vlanId);
166                                         }
167                                     });
168                                 });
169                     });
170             fts.add(transaction.submit());
171             Futures.addCallback(fts.get(0), new FutureCallback<Void>() {
172                 @Override
173                 public void onSuccess(Void success) {
174                     LOG.debug("Successfully deleted vlan bindings for l2gw update {}", update);
175                         connections.forEach((l2GwConnection) ->
176                                 l2gwService.addL2GatewayConnection(l2GwConnection, null, update));
177                 }
178
179                 @Override
180                 public void onFailure(Throwable throwable) {
181                     LOG.error("Failed to delete vlan bindings as part of l2gw udpate {}", update);
182                 }
183             }, MoreExecutors.directExecutor());
184             return fts;
185         }, SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
186     }
187
188     private synchronized void addL2Device(Devices l2Device, L2gateway input) {
189         String l2DeviceName = l2Device.getDeviceName();
190         L2GatewayDevice l2GwDevice = L2GatewayCacheUtils.updateCacheUponL2GatewayAdd(l2DeviceName, input.getUuid());
191         if (l2GwDevice.getHwvtepNodeId() == null) {
192             LOG.info("L2GW provisioning skipped for device {}",l2DeviceName);
193         } else {
194             LOG.info("Provisioning l2gw for device {}",l2DeviceName);
195             l2gwService.provisionItmAndL2gwConnection(l2GwDevice, l2DeviceName, l2GwDevice.getHwvtepNodeId(),
196                     l2GwDevice.getTunnelIp());
197         }
198     }
199
200     private void removeL2Device(Devices l2Device, L2gateway input) {
201         final String l2DeviceName = l2Device.getDeviceName();
202         L2GatewayDevice l2GwDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
203         if (l2GwDevice != null) {
204             // Delete ITM tunnels if it's last Gateway deleted and device is connected
205             // Also, do not delete device from cache if it's connected
206             if (L2GatewayUtils.isLastL2GatewayBeingDeleted(l2GwDevice)) {
207                 if (l2GwDevice.isConnected()) {
208                     l2GwDevice.removeL2GatewayId(input.getUuid());
209                     // Delete ITM tunnels
210                     final String hwvtepId = l2GwDevice.getHwvtepNodeId();
211                     InstanceIdentifier<Node> iid = HwvtepSouthboundUtils.createInstanceIdentifier(new NodeId(hwvtepId));
212                     ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(
213                             entityOwnershipService, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
214                             HwvtepSouthboundConstants.ELAN_ENTITY_NAME);
215                     final Set<IpAddress> tunnelIps = l2GwDevice.getTunnelIps();
216                     Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
217                         @Override
218                         public void onSuccess(Boolean isOwner) {
219                             if (isOwner) {
220                                 LOG.info("Deleting ITM Tunnels for {} connected to cluster node owner", l2DeviceName);
221                                 for (IpAddress tunnelIp : tunnelIps) {
222                                     L2GatewayUtils.deleteItmTunnels(itmRpcService, hwvtepId, l2DeviceName, tunnelIp);
223                                 }
224                             } else {
225                                 LOG.info("ITM Tunnels are not deleted on the cluster node as this is not owner for {}",
226                                         l2DeviceName);
227                             }
228                         }
229
230                         @Override
231                         public void onFailure(Throwable error) {
232                             LOG.error("Failed to delete ITM tunnels", error);
233                         }
234                     }, MoreExecutors.directExecutor());
235                 } else {
236                     L2GatewayCacheUtils.removeL2DeviceFromCache(l2DeviceName);
237                     // Cleaning up the config DS
238                     NodeId nodeId = new NodeId(l2GwDevice.getHwvtepNodeId());
239                     NodeId psNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, l2DeviceName);
240                     //FIXME: These should be removed
241                     MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
242                             HwvtepSouthboundUtils.createInstanceIdentifier(nodeId));
243                     MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
244                             HwvtepSouthboundUtils.createInstanceIdentifier(psNodeId));
245
246                 }
247             } else {
248                 l2GwDevice.removeL2GatewayId(input.getUuid());
249                 LOG.trace("ITM tunnels are not deleted for {} as this device has other L2gateway associations",
250                         l2DeviceName);
251             }
252         } else {
253             LOG.error("Unable to find L2 Gateway details for {}", l2DeviceName);
254         }
255     }
256
257     @Override
258     protected L2GatewayListener getDataTreeChangeListener() {
259         return this;
260     }
261
262     static class DeviceInterfaces {
263         Map<String, Map<String, Interfaces>> deviceInterfacesMap = new HashMap<>();
264
265         DeviceInterfaces(L2gateway l2gateway) {
266             if (l2gateway.getDevices() != null) {
267                 l2gateway.getDevices().forEach((device) -> {
268                     deviceInterfacesMap.putIfAbsent(device.getDeviceName(), new HashMap<>());
269                     if (device.getInterfaces() != null) {
270                         device.getInterfaces().forEach((intf) ->
271                                 deviceInterfacesMap.get(device.getDeviceName()).put(intf.getInterfaceName(), intf));
272                     }
273                 });
274             }
275         }
276
277         boolean containsInterface(String deviceName, String interfaceName) {
278             if (deviceInterfacesMap.containsKey(deviceName)) {
279                 return deviceInterfacesMap.get(deviceName).containsKey(interfaceName);
280             }
281             return false;
282         }
283     }
284 }