1a962ac35d2fcd25d7b4b59f01ff4a29699ed7ea
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / 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.netvirt.elan.l2gw.utils;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Set;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.netvirt.elan.l2gw.listeners.HwvtepLogicalSwitchListener;
18 import org.opendaylight.netvirt.elan.l2gw.jobs.AssociateHwvtepToElanJob;
19 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
20 import org.opendaylight.netvirt.elan.utils.ElanUtils;
21 import org.opendaylight.netvirt.elan.internal.ElanInstanceManager;
22 import org.opendaylight.netvirt.elan.l2gw.jobs.DisAssociateHwvtepFromElanJob;
23 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
24 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
25 import org.opendaylight.netvirt.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
26 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
27 import org.opendaylight.genius.mdsalutil.MDSALUtil;
28 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
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.l2gateways.rev150712.l2gateways.attributes.l2gateways.L2gatewayKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import com.google.common.base.Optional;
45
46 public class L2GatewayConnectionUtils {
47     private static final Logger LOG = LoggerFactory.getLogger(L2GatewayConnectionUtils.class);
48
49     private static DataBroker broker;
50     private static ElanInstanceManager elanInstanceManager;
51
52     static DataStoreJobCoordinator dataStoreJobCoordinator;
53
54     public static void setDataStoreJobCoordinator(DataStoreJobCoordinator ds) {
55         dataStoreJobCoordinator = ds;
56     }
57
58     public static void setBroker(DataBroker broker) {
59         L2GatewayConnectionUtils.broker = broker;
60     }
61
62     public static void setElanInstanceManager(ElanInstanceManager elanInstanceManager) {
63         L2GatewayConnectionUtils.elanInstanceManager = elanInstanceManager;
64     }
65
66     public static boolean isGatewayAssociatedToL2Device(L2GatewayDevice l2GwDevice) {
67         return (l2GwDevice.getL2GatewayIds().size() > 0);
68     }
69
70     public static L2gateway getNeutronL2gateway(DataBroker broker, Uuid l2GatewayId) {
71         LOG.debug("getNeutronL2gateway for {}", l2GatewayId.getValue());
72         InstanceIdentifier<L2gateway> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class)
73                 .child(L2gateway.class, new L2gatewayKey(l2GatewayId));
74         Optional<L2gateway> l2Gateway = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
75         if (l2Gateway.isPresent()) {
76             return l2Gateway.get();
77         }
78         return null;
79     }
80
81     public static List<L2gateway> getL2gatewayList(DataBroker broker) {
82         InstanceIdentifier<L2gateways> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class);
83         Optional<L2gateways> l2gateways = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
84
85         if (l2gateways.isPresent()) {
86             return l2gateways.get().getL2gateway();
87         }
88         return null;
89     }
90
91     public static List<L2gatewayConnection> getAllL2gatewayConnections(DataBroker broker) {
92         InstanceIdentifier<L2gatewayConnections> inst = InstanceIdentifier.create(Neutron.class)
93                 .child(L2gatewayConnections.class);
94         Optional<L2gatewayConnections> l2GwConns = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
95         if (l2GwConns.isPresent()) {
96             return l2GwConns.get().getL2gatewayConnection();
97         }
98         return null;
99     }
100
101     /**
102      * Gets the associated l2 gw connections.
103      *
104      * @param broker
105      *            the broker
106      * @param l2GatewayIds
107      *            the l2 gateway ids
108      * @return the associated l2 gw connections
109      */
110     public static List<L2gatewayConnection> getAssociatedL2GwConnections(DataBroker broker, Set<Uuid> l2GatewayIds) {
111         List<L2gatewayConnection> l2GwConnections = null;
112         List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
113         if (allL2GwConns != null) {
114             l2GwConnections = new ArrayList<L2gatewayConnection>();
115             for (Uuid l2GatewayId : l2GatewayIds) {
116                 for (L2gatewayConnection l2GwConn : allL2GwConns) {
117                     if (l2GwConn.getL2gatewayId().equals(l2GatewayId)) {
118                         l2GwConnections.add(l2GwConn);
119                     }
120                 }
121             }
122         }
123         return l2GwConnections;
124     }
125
126     public static void addL2GatewayConnection(L2gatewayConnection input) {
127         addL2GatewayConnection(input, null/*deviceName*/);
128     }
129
130     public static void addL2GatewayConnection(L2gatewayConnection input, String l2GwDeviceName) {
131         LOG.info("Adding L2gateway Connection with ID: {}", input.getKey().getUuid());
132
133         Uuid networkUuid = input.getNetworkId();
134         ElanInstance elanInstance = elanInstanceManager.getElanInstanceByName(networkUuid.getValue());
135         if (elanInstance == null || !ElanUtils.isVxlan(elanInstance)) {
136             LOG.error("Neutron network with id {} is not present", networkUuid.getValue());
137         } else {
138             Uuid l2GatewayId = input.getL2gatewayId();
139             L2gateway l2Gateway = getNeutronL2gateway(broker, l2GatewayId);
140             if (l2Gateway == null) {
141                 LOG.error("L2Gateway with id {} is not present", l2GatewayId.getValue());
142             } else {
143                 associateHwvtepsToElan(elanInstance, l2Gateway, input, l2GwDeviceName);
144             }
145         }
146     }
147
148     public static void deleteL2GatewayConnection(L2gatewayConnection input) {
149         LOG.info("Deleting L2gateway Connection with ID: {}", input.getKey().getUuid());
150
151         Uuid networkUuid = input.getNetworkId();
152         ElanInstance elanInstance = elanInstanceManager.getElanInstanceByName(networkUuid.getValue());
153         if (elanInstance == null) {
154             LOG.error("Neutron network with id {} is not present", networkUuid.getValue());
155         } else {
156             Uuid l2GatewayId = input.getL2gatewayId();
157             L2gateway l2Gateway = L2GatewayConnectionUtils.getNeutronL2gateway(broker, l2GatewayId);
158             if (l2Gateway == null) {
159                 LOG.error("L2Gateway with id {} is not present", l2GatewayId.getValue());
160             } else {
161                 disAssociateHwvtepsFromElan(elanInstance, l2Gateway, input);
162             }
163         }
164     }
165
166     private static void disAssociateHwvtepsFromElan(ElanInstance elanInstance, L2gateway l2Gateway,
167             L2gatewayConnection input) {
168         String elanName = elanInstance.getElanInstanceName();
169         Integer defaultVlan = input.getSegmentId();
170         List<Devices> l2Devices = l2Gateway.getDevices();
171         for (Devices l2Device : l2Devices) {
172             String l2DeviceName = l2Device.getDeviceName();
173             L2GatewayDevice l2GatewayDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
174             String hwvtepNodeId = l2GatewayDevice.getHwvtepNodeId();
175             boolean isLastL2GwConnDeleted = false;
176             L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
177             if (isLastL2GwConnBeingDeleted(elanL2GwDevice)) {
178                 // Delete L2 Gateway device from 'ElanL2GwDevice' cache
179                 LOG.debug("Elan L2Gw Conn cache removed for id {}", hwvtepNodeId);
180                 ElanL2GwCacheUtils.removeL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
181                 isLastL2GwConnDeleted = true;
182             } else {
183                 Uuid l2GwConnId = input.getKey().getUuid();
184                 LOG.debug("Elan L2Gw Conn cache with id {} is being referred by other L2Gw Conns; so only "
185                         + "L2 Gw Conn {} reference is removed", hwvtepNodeId, l2GwConnId);
186                 elanL2GwDevice.removeL2GatewayId(l2GwConnId);
187             }
188
189             DisAssociateHwvtepFromElanJob disAssociateHwvtepToElanJob = new DisAssociateHwvtepFromElanJob(broker,
190                     elanL2GwDevice, elanInstance, l2Device, defaultVlan, isLastL2GwConnDeleted);
191             ElanClusterUtils.runOnlyInLeaderNode(disAssociateHwvtepToElanJob.getJobKey(), "remove l2gw connection job ",
192                     disAssociateHwvtepToElanJob);
193         }
194     }
195
196     private static void associateHwvtepsToElan(ElanInstance elanInstance,
197             L2gateway l2Gateway, L2gatewayConnection input, String l2GwDeviceName) {
198         String elanName = elanInstance.getElanInstanceName();
199         Integer defaultVlan = input.getSegmentId();
200         Uuid l2GwConnId = input.getKey().getUuid();
201         List<Devices> l2Devices = l2Gateway.getDevices();
202
203         if (LOG.isTraceEnabled()) {
204             LOG.trace("Associating ELAN {} with L2Gw Conn Id {} having below L2Gw devices {}", elanName, l2GwConnId,
205                     l2Devices);
206         }
207
208         for (Devices l2Device : l2Devices) {
209             String l2DeviceName = l2Device.getDeviceName();
210             // L2gateway can have more than one L2 Gw devices. Configure Logical Switch, VLAN mappings,...
211             // only on the switch which has come up just now and exclude all other devices from
212             // preprovisioning/re-provisioning
213             if (l2GwDeviceName != null && !l2GwDeviceName.equals(l2DeviceName)) {
214                 LOG.debug("Associating Hwvtep to ELAN is not been processed for {}; as only {} got connected now!",
215                         l2DeviceName, l2GwDeviceName);
216                 continue;
217             }
218             L2GatewayDevice l2GatewayDevice = L2GatewayCacheUtils.getL2DeviceFromCache(l2DeviceName);
219             if (isL2GwDeviceConnected(l2GatewayDevice)) {
220                 NodeId hwvtepNodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
221
222                 // Delete pending delete logical switch task if scheduled
223                 ElanL2GatewayUtils.cancelDeleteLogicalSwitch(hwvtepNodeId,
224                         ElanL2GatewayUtils.getLogicalSwitchFromElan(elanName));
225
226                 // Add L2 Gateway device to 'ElanL2GwDevice' cache
227                 boolean createLogicalSwitch;
228                 LogicalSwitches logicalSwitch = HwvtepUtils.getLogicalSwitch(broker, LogicalDatastoreType.OPERATIONAL,
229                         hwvtepNodeId, elanName);
230                 if (logicalSwitch == null) {
231                     HwvtepLogicalSwitchListener hwVTEPLogicalSwitchListener = new HwvtepLogicalSwitchListener(
232                             l2GatewayDevice, elanName, l2Device, defaultVlan, l2GwConnId);
233                     hwVTEPLogicalSwitchListener.registerListener(LogicalDatastoreType.OPERATIONAL, broker);
234                     createLogicalSwitch = true;
235                 } else {
236                     addL2DeviceToElanL2GwCache(elanName, l2GatewayDevice, l2GwConnId);
237                     createLogicalSwitch = false;
238                 }
239                 AssociateHwvtepToElanJob associateHwvtepToElanJob = new AssociateHwvtepToElanJob(broker,
240                         l2GatewayDevice, elanInstance, l2Device, defaultVlan, createLogicalSwitch);
241
242                 ElanClusterUtils.runOnlyInLeaderNode( associateHwvtepToElanJob.getJobKey() ,
243                         "create logical switch in hwvtep topo",
244                         associateHwvtepToElanJob);
245
246             } else {
247                 LOG.info("L2GwConn create is not handled for device with id {} as it's not connected", l2DeviceName);
248             }
249         }
250     }
251
252     public static L2GatewayDevice addL2DeviceToElanL2GwCache(String elanName, L2GatewayDevice l2GatewayDevice,
253             Uuid l2GwConnId) {
254         String l2gwDeviceNodeId = l2GatewayDevice.getHwvtepNodeId();
255         L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, l2gwDeviceNodeId);
256         if (elanL2GwDevice == null) {
257             elanL2GwDevice = new L2GatewayDevice();
258             elanL2GwDevice.setHwvtepNodeId(l2gwDeviceNodeId);
259             elanL2GwDevice.setDeviceName(l2GatewayDevice.getDeviceName());
260             elanL2GwDevice.setTunnelIps(l2GatewayDevice.getTunnelIps());
261             ElanL2GwCacheUtils.addL2GatewayDeviceToCache(elanName, elanL2GwDevice);
262             LOG.debug("Elan L2GwConn cache created for hwvtep id {}", l2gwDeviceNodeId);
263         } else {
264             LOG.debug("Elan L2GwConn cache already exists for hwvtep id {}; updating L2GwConn id {} to it",
265                     l2gwDeviceNodeId, l2GwConnId);
266         }
267         elanL2GwDevice.addL2GatewayId(l2GwConnId);
268
269         if (LOG.isTraceEnabled()) {
270             LOG.trace("Elan L2GwConn cache updated with below details: {}", elanL2GwDevice);
271         }
272         return elanL2GwDevice;
273     }
274
275     private static boolean isL2GwDeviceConnected(L2GatewayDevice l2GwDevice) {
276         return (l2GwDevice != null && l2GwDevice.getHwvtepNodeId() != null && l2GwDevice.isConnected());
277     }
278
279     protected static boolean isLastL2GwConnBeingDeleted(L2GatewayDevice l2GwDevice) {
280         return (l2GwDevice.getL2GatewayIds().size() == 1);
281     }
282 }