be0012f47b899f0f27d86dbae8550271d48095ad
[vpnservice.git] / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / vpnservice / elan / l2gw / utils / L2GatewayConnectionUtils.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
9 package org.opendaylight.vpnservice.elan.l2gw.utils;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.concurrent.Callable;
14
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.elanmanager.utils.ElanL2GwCacheUtils;
19 import org.opendaylight.vpnservice.datastoreutils.DataStoreJobCoordinator;
20 import org.opendaylight.vpnservice.elan.internal.ElanInstanceManager;
21 import org.opendaylight.vpnservice.elan.l2gw.listeners.HwvtepLogicalSwitchListener;
22 import org.opendaylight.vpnservice.elan.l2gw.listeners.HwvtepRemoteMcastMacListener;
23 import org.opendaylight.vpnservice.elan.utils.ElanUtils;
24 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
25 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.L2GatewayDevice;
26 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
27 import org.opendaylight.vpnservice.utils.SystemPropertyReader;
28 import org.opendaylight.vpnservice.utils.clustering.ClusteringUtils;
29 import org.opendaylight.vpnservice.utils.hwvtep.HwvtepSouthboundConstants;
30 import org.opendaylight.vpnservice.utils.hwvtep.HwvtepSouthboundUtils;
31 import org.opendaylight.vpnservice.utils.hwvtep.HwvtepUtils;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
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.neutron.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gatewayKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.instances.ElanInstance;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
45 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import com.google.common.base.Optional;
51 import com.google.common.util.concurrent.FutureCallback;
52 import com.google.common.util.concurrent.Futures;
53 import com.google.common.util.concurrent.ListenableFuture;
54
55 public class L2GatewayConnectionUtils {
56     private static final Logger LOG = LoggerFactory.getLogger(L2GatewayConnectionUtils.class);
57
58     public static boolean isGatewayAssociatedToL2Device(L2GatewayDevice l2GwDevice) {
59         return (l2GwDevice.getL2GatewayIds().size() > 0);
60     }
61
62     public static L2gateway getNeutronL2gateway(DataBroker broker, Uuid l2GatewayId) {
63         LOG.debug("getNeutronL2gateway for {}", l2GatewayId.getValue());
64         InstanceIdentifier<L2gateway> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class)
65                 .child(L2gateway.class, new L2gatewayKey(l2GatewayId));
66         Optional<L2gateway> l2Gateway = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
67         if (l2Gateway.isPresent()) {
68             return l2Gateway.get();
69         }
70         return null;
71     }
72
73     public static List<L2gateway> getL2gatewayList(DataBroker broker) {
74         InstanceIdentifier<L2gateways> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class);
75         Optional<L2gateways> l2gateways = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
76
77         if (l2gateways.isPresent()) {
78             return l2gateways.get().getL2gateway();
79         }
80         return null;
81     }
82
83     public static List<L2gatewayConnection> getAllL2gatewayConnections(DataBroker broker) {
84         InstanceIdentifier<L2gatewayConnections> inst = InstanceIdentifier.create(Neutron.class)
85                 .child(L2gatewayConnections.class);
86         Optional<L2gatewayConnections> l2GwConns = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
87         if (l2GwConns.isPresent()) {
88             return l2GwConns.get().getL2gatewayConnection();
89         }
90         return null;
91     }
92
93     public static void addL2GatewayConnection(DataBroker broker, EntityOwnershipService entityOwnershipService,
94             BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer, ElanInstanceManager elanInstanceManager,
95             L2gatewayConnection input) {
96         addL2GatewayConnection(broker, entityOwnershipService, bindingNormalizedNodeSerializer, elanInstanceManager,
97                 input, null);
98     }
99
100     public static void addL2GatewayConnection(DataBroker broker, EntityOwnershipService entityOwnershipService,
101             BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer, ElanInstanceManager elanInstanceManager,
102             L2gatewayConnection input, String l2GwDeviceName) {
103         Uuid networkUuid = input.getNetworkId();
104         ElanInstance elanInstance = elanInstanceManager.getElanInstanceByName(networkUuid.getValue());
105         if (elanInstance == null || elanInstance.getVni() == null) {
106             LOG.error("Neutron network with id {} is not present", networkUuid.getValue());
107         } else {
108             Uuid l2GatewayId = input.getL2gatewayId();
109             L2gateway l2Gateway = getNeutronL2gateway(broker, l2GatewayId);
110             if (l2Gateway == null) {
111                 LOG.error("L2Gateway with id {} is not present", l2GatewayId.getValue());
112             } else {
113                 associateHwvtepsToElan(broker, entityOwnershipService, bindingNormalizedNodeSerializer, elanInstance,
114                         l2Gateway, input.getSegmentId(), l2GwDeviceName);
115             }
116         }
117     }
118
119     public static void deleteL2GatewayConnection(DataBroker broker, EntityOwnershipService entityOwnershipService,
120             BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer, ElanInstanceManager elanInstanceManager,
121             L2gatewayConnection input) {
122         Uuid networkUuid = input.getNetworkId();
123         ElanInstance elanInstance = elanInstanceManager.getElanInstanceByName(networkUuid.getValue());
124         if (elanInstance == null) {
125             LOG.error("Neutron network with id {} is not present", networkUuid.getValue());
126         } else {
127             Uuid l2GatewayId = input.getL2gatewayId();
128             L2gateway l2Gateway = L2GatewayConnectionUtils.getNeutronL2gateway(broker, l2GatewayId);
129             if (l2Gateway == null) {
130                 LOG.error("L2Gateway with id {} is not present", l2GatewayId.getValue());
131             } else {
132                 disAssociateHwvtepsToElan(broker, entityOwnershipService, bindingNormalizedNodeSerializer, elanInstance,
133                         l2Gateway, input.getSegmentId());
134             }
135         }
136     }
137
138     private static void disAssociateHwvtepsToElan(final DataBroker broker, EntityOwnershipService entityOwnershipService,
139             BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer, final ElanInstance elanInstance,
140             L2gateway l2Gateway, final Integer defaultVlan) {
141         final String elanName = elanInstance.getElanInstanceName();
142         List<Devices> l2Devices = l2Gateway.getDevices();
143         for (final Devices l2Device : l2Devices) {
144             final String l2DeviceName = l2Device.getDeviceName();
145             final L2GatewayDevice l2GatewayDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
146             if (isL2GwDeviceConnected(l2GatewayDevice)) {//TODO handle delete while device is offline
147                 // Delete L2 Gateway device from 'ElanL2GwDevice' cache
148                 ElanL2GwCacheUtils.removeL2GatewayDeviceFromCache(elanName, l2GatewayDevice.getHwvtepNodeId());
149
150                 final String hwvtepId = l2GatewayDevice.getHwvtepNodeId();
151                 InstanceIdentifier<Node> iid = HwvtepSouthboundUtils.createInstanceIdentifier(new NodeId(hwvtepId));
152                 ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(
153                         entityOwnershipService, HwvtepSouthboundConstants.HWVTEP_ENTITY_TYPE,
154                         bindingNormalizedNodeSerializer.toYangInstanceIdentifier(iid));
155                 Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
156                     @Override
157                     public void onSuccess(Boolean isOwner) {
158                         if (isOwner) {
159                             LOG.info("L2 Gateway device delete is triggered for {} connected to cluster owner node",
160                                     l2DeviceName);
161
162                             // Create DataStoreJobCoordinator jobs to create Logical
163                             // switches on all physical switches
164                             // which are part of L2 Gateway
165                             DataStoreJobCoordinator dataStoreCoordinator = DataStoreJobCoordinator.getInstance();
166                             DisAssociateHwvtepFromElan disAssociateHwvtepToElan = new DisAssociateHwvtepFromElan(broker,
167                                     l2GatewayDevice, elanInstance, l2Device, defaultVlan);
168                             String jobKey = ElanL2GatewayUtils.getL2GatewayConnectionJobKey(hwvtepId, elanName);
169                             dataStoreCoordinator.enqueueJob(jobKey, disAssociateHwvtepToElan,
170                                     SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
171                         } else {
172                             LOG.info("L2 Gateway device delete is not triggered on the cluster node as this is not " +
173                                     "owner for {}", l2DeviceName);
174                         }
175                     }
176
177                     @Override
178                     public void onFailure(Throwable error) {
179                         LOG.error("Failed to trigger L2 Gateway device delete action", error);
180                     }
181                 });
182             } else {
183                 LOG.error("could not handle connection delete L2 Gateway device with id {} is not present",
184                         l2DeviceName);
185             }
186         }
187     }
188
189     private static void associateHwvtepsToElan(final DataBroker broker, EntityOwnershipService entityOwnershipService,
190             BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer, final ElanInstance elanInstance,
191             L2gateway l2Gateway, final Integer defaultVlan, String l2GwDeviceName) {
192         final String elanName = elanInstance.getElanInstanceName();
193         List<Devices> l2Devices = l2Gateway.getDevices();
194         for (final Devices l2Device : l2Devices) {
195             final String l2DeviceName = l2Device.getDeviceName();
196             // L2gateway can have more than one L2 Gw devices. Configure Logical Switch, VLAN mappings,...
197             // only on the switch which has come up just now and exclude all other devices from
198             // preprovisioning/re-provisioning
199             if (l2GwDeviceName != null && !l2GwDeviceName.equals(l2DeviceName)) {
200                 continue;
201             }
202             final L2GatewayDevice l2GatewayDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
203             if (isL2GwDeviceConnected(l2GatewayDevice)) {
204                 // Add L2 Gateway device to 'ElanL2GwDevice' cache
205                 final boolean createLogicalSwitch;
206                 LogicalSwitches logicalSwitch = HwvtepUtils.getLogicalSwitch(broker, LogicalDatastoreType.OPERATIONAL,
207                         new NodeId(l2GatewayDevice.getHwvtepNodeId()), elanName);
208                 if (logicalSwitch == null) {
209                     final HwvtepLogicalSwitchListener hwVTEPLogicalSwitchListener = new HwvtepLogicalSwitchListener(
210                             l2GatewayDevice, elanName, l2Device, defaultVlan);
211                     hwVTEPLogicalSwitchListener.registerListener(LogicalDatastoreType.OPERATIONAL, broker);
212                     createLogicalSwitch = true;
213                 } else {
214                     addL2DeviceToElanL2GwCache(elanName, l2GatewayDevice);
215                     createLogicalSwitch = false;
216                 }
217                 final String hwvtepId = l2GatewayDevice.getHwvtepNodeId();
218                 InstanceIdentifier<Node> iid = HwvtepSouthboundUtils.createInstanceIdentifier(new NodeId(hwvtepId));
219                 ListenableFuture<Boolean> checkEntityOwnerFuture = ClusteringUtils.checkNodeEntityOwner(
220                         entityOwnershipService, HwvtepSouthboundConstants.HWVTEP_ENTITY_TYPE,
221                         bindingNormalizedNodeSerializer.toYangInstanceIdentifier(iid));
222                 Futures.addCallback(checkEntityOwnerFuture, new FutureCallback<Boolean>() {
223                     @Override
224                     public void onSuccess(Boolean isOwner) {
225                         if (isOwner) {
226                             LOG.info("Creating Logical switch on {} connected to cluster owner node", l2DeviceName);
227
228                             // Create DataStoreJobCoordinator jobs to create Logical
229                             // switches on all physical switches
230                             // which are part of L2 Gateway
231                             DataStoreJobCoordinator dataStoreCoordinator = DataStoreJobCoordinator.getInstance();
232                             AssociateHwvtepToElan associateHwvtepToElan = new AssociateHwvtepToElan(broker,
233                                     l2GatewayDevice, elanInstance, l2Device, defaultVlan, createLogicalSwitch);
234                             String jobKey = ElanL2GatewayUtils.getL2GatewayConnectionJobKey(hwvtepId, elanName);
235                             dataStoreCoordinator.enqueueJob(jobKey, associateHwvtepToElan,
236                                     SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
237                         } else {
238                             LOG.info("Logical switch creation is not triggered on the cluster node as this is not " +
239                                     "owner for {}", l2DeviceName);
240                         }
241                     }
242
243                     @Override
244                     public void onFailure(Throwable error) {
245                         LOG.error("Failed to trigger Logical switch creation action", error);
246                     }
247                 });
248             } else {
249                 LOG.error("L2 Gateway device with id {} is not present", l2DeviceName);
250             }
251         }
252     }
253
254     public static void addL2DeviceToElanL2GwCache(String elanName, L2GatewayDevice l2GatewayDevice) {
255         L2GatewayDevice elanL2GwDevice = new L2GatewayDevice();
256         elanL2GwDevice.setHwvtepNodeId(l2GatewayDevice.getHwvtepNodeId());
257         elanL2GwDevice.setDeviceName(l2GatewayDevice.getDeviceName());
258         elanL2GwDevice.setTunnelIps(l2GatewayDevice.getTunnelIps());
259         ElanL2GwCacheUtils.addL2GatewayDeviceToCache(elanName, elanL2GwDevice);
260     }
261
262     private static boolean isL2GwDeviceConnected(L2GatewayDevice l2GwDevice) {
263         return (l2GwDevice != null && l2GwDevice.getHwvtepNodeId() != null && l2GwDevice.isConnected());
264     }
265
266     private static class AssociateHwvtepToElan implements Callable<List<ListenableFuture<Void>>> {
267         DataBroker broker;
268         L2GatewayDevice l2GatewayDevice;
269         ElanInstance elanInstance;
270         Devices l2Device;
271         Integer defaultVlan;
272         boolean createLogicalSwitch;
273
274         public AssociateHwvtepToElan(DataBroker broker, L2GatewayDevice l2GatewayDevice, ElanInstance elanInstance,
275                 Devices l2Device, Integer defaultVlan, boolean createLogicalSwitch) {
276             this.broker = broker;
277             this.l2GatewayDevice = l2GatewayDevice;
278             this.elanInstance = elanInstance;
279             this.l2Device = l2Device;
280             this.defaultVlan = defaultVlan;
281             this.createLogicalSwitch = createLogicalSwitch;
282         }
283
284         @Override
285         public List<ListenableFuture<Void>> call() throws Exception {
286             List<ListenableFuture<Void>> futures = new ArrayList<>();
287
288             final String logicalSwitchName = ElanL2GatewayUtils.getLogicalSwitchFromElan(elanInstance.getElanInstanceName());
289
290             // Create Logical Switch if it's not created already in
291             // the device
292             if (createLogicalSwitch) {
293                 ListenableFuture<Void> lsCreateFuture = createLogicalSwitch(l2GatewayDevice, elanInstance, l2Device);
294                 futures.add(lsCreateFuture);
295             } else {
296                 // Logical switch is already created; do the rest of
297                 // configuration
298                 futures.add(ElanL2GatewayUtils.updateVlanBindingsInL2GatewayDevice(
299                         new NodeId(l2GatewayDevice.getHwvtepNodeId()), logicalSwitchName, l2Device, defaultVlan));
300                 futures.add(ElanL2GatewayMulticastUtils.handleMcastForElanL2GwDeviceAdd(logicalSwitchName, l2GatewayDevice));
301                 HwvtepRemoteMcastMacListener list = new HwvtepRemoteMcastMacListener(ElanUtils.getDataBroker(),
302                         logicalSwitchName, l2GatewayDevice,
303                         new Callable<List<ListenableFuture<Void>>>() {
304
305                         @Override
306                         public List<ListenableFuture<Void>> call() {
307                             List<ListenableFuture<Void>> futures = new ArrayList<>();
308                             futures.add(ElanL2GatewayUtils.installElanMacsInL2GatewayDevice(
309                                     logicalSwitchName, l2GatewayDevice));
310                             return futures;
311                         }}
312                     );
313             }
314
315             return futures;
316         }
317
318         private ListenableFuture<Void> createLogicalSwitch(L2GatewayDevice l2GatewayDevice, ElanInstance elanInstance,
319                 Devices l2Device) {
320             final String logicalSwitchName = ElanL2GatewayUtils
321                     .getLogicalSwitchFromElan(elanInstance.getElanInstanceName());
322             String segmentationId = elanInstance.getVni().toString();
323
324             // Register for Logical switch update in opearational DS
325             final HwvtepLogicalSwitchListener hwVTEPLogicalSwitchListener = new HwvtepLogicalSwitchListener(
326                     l2GatewayDevice, logicalSwitchName, l2Device, defaultVlan);
327             hwVTEPLogicalSwitchListener.registerListener(LogicalDatastoreType.OPERATIONAL, broker);
328
329             NodeId hwvtepNodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
330             InstanceIdentifier<LogicalSwitches> path = HwvtepSouthboundUtils
331                     .createLogicalSwitchesInstanceIdentifier(hwvtepNodeId, new HwvtepNodeName(logicalSwitchName));
332             LogicalSwitches logicalSwitch = HwvtepSouthboundUtils.createLogicalSwitch(logicalSwitchName,
333                     elanInstance.getDescription(), segmentationId);
334
335             ListenableFuture<Void> lsCreateFuture = HwvtepUtils.addLogicalSwitch(broker, hwvtepNodeId, logicalSwitch);
336             Futures.addCallback(lsCreateFuture, new FutureCallback<Void>() {
337                 @Override
338                 public void onSuccess(Void noarg) {
339                     // Listener will be closed after all configuration completed
340                     // on hwvtep by
341                     // listener itself
342                     if (LOG.isTraceEnabled()) {
343                         LOG.trace("Successful in initiating logical switch {} creation", logicalSwitchName);
344                     }
345                 }
346
347                 @Override
348                 public void onFailure(Throwable error) {
349                     LOG.error("Failed logical switch {} creation", logicalSwitchName, error);
350                     try {
351                         hwVTEPLogicalSwitchListener.close();
352                     } catch (final Exception e) {
353                         LOG.error("Error when cleaning up DataChangeListener.", e);
354                     }
355                 }
356             });
357             return lsCreateFuture;
358         }
359     }
360
361     private static class DisAssociateHwvtepFromElan implements Callable<List<ListenableFuture<Void>>> {
362         DataBroker broker;
363         L2GatewayDevice l2GatewayDevice;
364         ElanInstance elanInstance;
365         Devices l2Device;
366         Integer defaultVlan;
367
368         public DisAssociateHwvtepFromElan(DataBroker broker, L2GatewayDevice l2GatewayDevice, ElanInstance elanInstance,
369                 Devices l2Device, Integer defaultVlan) {
370             this.broker = broker;
371             this.l2GatewayDevice = l2GatewayDevice;
372             this.elanInstance = elanInstance;
373             this.l2Device = l2Device;
374             this.defaultVlan = defaultVlan;
375         }
376
377         @Override
378         public List<ListenableFuture<Void>> call() throws Exception {
379             List<ListenableFuture<Void>> futures = new ArrayList<>();
380
381             // Remove remote MACs and vlan mappings from physical port
382             // Once all above configurations are deleted, delete logical
383             // switch
384             NodeId hwvtepNodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
385             String elanName = elanInstance.getElanInstanceName();
386             futures.add(ElanL2GatewayUtils.deleteElanMacsFromL2GatewayDevice(l2GatewayDevice, elanName));
387             futures.addAll(ElanL2GatewayMulticastUtils.handleMcastForElanL2GwDeviceDelete(elanInstance,
388                     l2GatewayDevice));
389             futures.addAll(ElanL2GatewayUtils.deleteL2GatewayDeviceUcastLocalMacsFromElan(l2GatewayDevice, elanName));
390             futures.add(ElanL2GatewayUtils.deleteVlanBindingsFromL2GatewayDevice(hwvtepNodeId, l2Device, defaultVlan));
391             Thread.sleep(30000);
392             futures.add(HwvtepUtils.deleteLogicalSwitch(this.broker, hwvtepNodeId, elanName));
393
394             return futures;
395         }
396     }
397 }