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