Migrate HwvtepHACache users to HwvtepNodeHACache
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / HwvtepPhysicalSwitchListener.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.listeners;
10
11 import com.google.common.base.Optional;
12 import java.util.Collections;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Objects;
16 import java.util.Set;
17 import java.util.function.BiPredicate;
18 import java.util.function.Predicate;
19 import javax.annotation.PostConstruct;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepAbstractDataTreeChangeListener;
26 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
27 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
28 import org.opendaylight.genius.mdsalutil.MDSALUtil;
29 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
30 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
31 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
32 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
33 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
34 import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpClusteredListener;
35 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
36 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
37 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayUtils;
38 import org.opendaylight.netvirt.elan.l2gw.utils.L2gwServiceProvider;
39 import org.opendaylight.netvirt.elan.l2gw.utils.StaleVlanBindingsCleaner;
40 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
41 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
42 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
43 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentationBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIps;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 /**
60  * Listener to handle physical switch updates.
61  */
62 @Singleton
63 public class HwvtepPhysicalSwitchListener
64         extends HwvtepAbstractDataTreeChangeListener<PhysicalSwitchAugmentation, HwvtepPhysicalSwitchListener>
65         implements ClusteredDataTreeChangeListener<PhysicalSwitchAugmentation> {
66
67     /** The Constant LOG. */
68     private static final Logger LOG = LoggerFactory.getLogger(HwvtepPhysicalSwitchListener.class);
69
70     private static final BiPredicate<L2GatewayDevice, InstanceIdentifier<Node>> DEVICE_NOT_CACHED_OR_PARENT_CONNECTED =
71         (l2GatewayDevice, globalIid) -> l2GatewayDevice == null || l2GatewayDevice.getHwvtepNodeId() == null
72                 || !Objects.equals(l2GatewayDevice.getHwvtepNodeId(),
73                         globalIid.firstKeyOf(Node.class).getNodeId().getValue());
74
75     private static final Predicate<PhysicalSwitchAugmentation> TUNNEL_IP_AVAILABLE =
76         phySwitch -> !HwvtepHAUtil.isEmpty(phySwitch.getTunnelIps());
77
78     private static final Predicate<PhysicalSwitchAugmentation> TUNNEL_IP_NOT_AVAILABLE = TUNNEL_IP_AVAILABLE.negate();
79
80     private static final BiPredicate<PhysicalSwitchAugmentation, L2GatewayDevice> TUNNEL_IP_CHANGED =
81         (phySwitchAfter, existingDevice) -> TUNNEL_IP_AVAILABLE.test(phySwitchAfter)
82                 && !Objects.equals(
83                         existingDevice.getTunnelIp(),  phySwitchAfter.getTunnelIps().get(0).getTunnelIpsKey());
84
85     /** The data broker. */
86     private final DataBroker dataBroker;
87     private final ManagedNewTransactionRunner txRunner;
88
89     /** The itm rpc service. */
90     private final ItmRpcService itmRpcService;
91
92     private final ElanClusterUtils elanClusterUtils;
93
94     private final HwvtepNodeHACache hwvtepNodeHACache;
95
96     private final L2gwServiceProvider l2gwServiceProvider;
97
98     private final BiPredicate<L2GatewayDevice, InstanceIdentifier<Node>> childConnectedAfterParent;
99
100     private final Predicate<L2GatewayDevice> alreadyHasL2Gwids =
101         (l2GwDevice) -> l2GwDevice != null && HwvtepHAUtil.isEmpty(l2GwDevice.getL2GatewayIds());
102
103     private final BiPredicate<L2GatewayDevice, InstanceIdentifier<Node>> parentConnectedAfterChild;
104     private final HAOpClusteredListener haOpClusteredListener;
105
106     private final L2GatewayCache l2GatewayCache;
107
108     private final StaleVlanBindingsCleaner staleVlanBindingsCleaner;
109
110     /**
111      * Instantiates a new hwvtep physical switch listener.
112      */
113     @Inject
114     public HwvtepPhysicalSwitchListener(final DataBroker dataBroker, ItmRpcService itmRpcService,
115             ElanClusterUtils elanClusterUtils, L2gwServiceProvider l2gwServiceProvider,
116             HAOpClusteredListener haListener, L2GatewayCache l2GatewayCache,
117             StaleVlanBindingsCleaner staleVlanBindingsCleaner,
118             HwvtepNodeHACache hwvtepNodeHACache) {
119         super(PhysicalSwitchAugmentation.class, HwvtepPhysicalSwitchListener.class, hwvtepNodeHACache);
120         this.dataBroker = dataBroker;
121         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
122         this.itmRpcService = itmRpcService;
123         this.elanClusterUtils = elanClusterUtils;
124         this.l2gwServiceProvider = l2gwServiceProvider;
125         this.staleVlanBindingsCleaner = staleVlanBindingsCleaner;
126         this.haOpClusteredListener = haListener;
127         this.l2GatewayCache = l2GatewayCache;
128         this.hwvtepNodeHACache = hwvtepNodeHACache;
129
130         childConnectedAfterParent = (l2GwDevice, globalIid) -> {
131             return !hwvtepNodeHACache.isHAParentNode(globalIid)
132                     && l2GwDevice != null;
133             // FIXME: The following call to equals compares different types (String and InstanceIdentifier) and
134             // thus will always return false. I don't know what the intention is here so commented out for now.
135             //&& !Objects.equals(l2GwDevice.getHwvtepNodeId(), globalIid);
136         };
137
138         parentConnectedAfterChild = (l2GwDevice, globalIid) -> {
139             InstanceIdentifier<Node> existingIid = globalIid;
140             if (l2GwDevice != null && l2GwDevice.getHwvtepNodeId() != null) {
141                 existingIid = HwvtepHAUtil.convertToInstanceIdentifier(l2GwDevice.getHwvtepNodeId());
142             }
143             return hwvtepNodeHACache.isHAParentNode(globalIid)
144                     && l2GwDevice != null
145                     // FIXME: The following call to equals compares different types (String and InstanceIdentifier) and
146                     // thus will always return false. I don't know what the intention is here so commented out for now.
147                     //&& !Objects.equals(l2GwDevice.getHwvtepNodeId(), globalIid)
148                     && Objects.equals(globalIid, hwvtepNodeHACache.getParent(existingIid));
149         };
150     }
151
152     @Override
153     @PostConstruct
154     public void init() {
155         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
156     }
157
158     @Override
159     protected InstanceIdentifier<PhysicalSwitchAugmentation> getWildCardPath() {
160         return InstanceIdentifier.create(NetworkTopology.class)
161                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID)).child(Node.class)
162                 .augmentation(PhysicalSwitchAugmentation.class);
163     }
164
165     @Override
166     protected HwvtepPhysicalSwitchListener getDataTreeChangeListener() {
167         return HwvtepPhysicalSwitchListener.this;
168     }
169
170     @Override
171     protected void removed(InstanceIdentifier<PhysicalSwitchAugmentation> identifier,
172             PhysicalSwitchAugmentation phySwitchDeleted) {
173         NodeId nodeId = getNodeId(identifier);
174         String psName = phySwitchDeleted.getHwvtepNodeName().getValue();
175         LOG.info("Received physical switch {} removed event for node {}", psName, nodeId.getValue());
176
177         L2GatewayDevice l2GwDevice = l2GatewayCache.get(psName);
178         if (l2GwDevice != null) {
179             if (!L2GatewayConnectionUtils.isGatewayAssociatedToL2Device(l2GwDevice)) {
180                 l2GatewayCache.remove(psName);
181                 LOG.debug("{} details removed from L2Gateway Cache", psName);
182                 MDSALUtil.syncDelete(this.dataBroker, LogicalDatastoreType.CONFIGURATION,
183                         HwvtepSouthboundUtils.createInstanceIdentifier(nodeId));
184             } else {
185                 LOG.debug("{} details are not removed from L2Gateway Cache as it has L2Gateway reference", psName);
186             }
187
188             l2GwDevice.setConnected(false);
189             //ElanL2GwCacheUtils.removeL2GatewayDeviceFromAllElanCache(psName);
190         } else {
191             LOG.error("Unable to find L2 Gateway details for {}", psName);
192         }
193     }
194
195     /**
196      * Upon update checks if the tunnels Ip was null earlier and it got newly added.
197      * In that case simply call add.
198      * If not then check if Tunnel Ip has been updated from an old value to new value.
199      * If yes. delete old ITM tunnels of odl Tunnel Ipand add new ITM tunnels with new Tunnel
200      * IP then call added ().
201      *
202      * @param identifier iid
203      * @param phySwitchBefore ps Node before update
204      * @param phySwitchAfter ps Node after update
205      */
206     @Override
207     protected void updated(InstanceIdentifier<PhysicalSwitchAugmentation> identifier,
208             PhysicalSwitchAugmentation phySwitchBefore, PhysicalSwitchAugmentation phySwitchAfter) {
209         NodeId nodeId = getNodeId(identifier);
210         LOG.trace("Received PhysicalSwitch Update Event for node {}: PhysicalSwitch Before: {}, "
211                 + "PhysicalSwitch After: {}", nodeId.getValue(), phySwitchBefore, phySwitchAfter);
212         String psName = getPsName(identifier);
213         if (psName == null) {
214             LOG.error("Could not find the physical switch name for node {}", nodeId.getValue());
215             return;
216         }
217         L2GatewayDevice existingDevice = l2GatewayCache.get(psName);
218         LOG.info("Received physical switch {} update event for node {}", psName, nodeId.getValue());
219         InstanceIdentifier<Node> globalNodeIid = getManagedByNodeIid(identifier);
220
221         if (DEVICE_NOT_CACHED_OR_PARENT_CONNECTED.test(existingDevice, globalNodeIid)) {
222             if (TUNNEL_IP_AVAILABLE.test(phySwitchAfter)) {
223                 added(identifier, phySwitchAfter);
224             }
225         } else {
226             if (!Objects.equals(phySwitchAfter.getTunnelIps(), phySwitchBefore.getTunnelIps())
227                     && TUNNEL_IP_CHANGED.test(phySwitchAfter, existingDevice)) {
228
229                 final String hwvtepId = existingDevice.getHwvtepNodeId();
230                 elanClusterUtils.runOnlyInOwnerNode(existingDevice.getDeviceName(),
231                     "handling Physical Switch add create itm tunnels ",
232                     () -> {
233                         LOG.info("Deleting itm tunnels for device {}", existingDevice.getDeviceName());
234                         L2GatewayUtils.deleteItmTunnels(itmRpcService, hwvtepId,
235                                 existingDevice.getDeviceName(), existingDevice.getTunnelIp());
236                         Thread.sleep(10000L);//TODO remove these sleeps
237                         LOG.info("Creating itm tunnels for device {}", existingDevice.getDeviceName());
238                         ElanL2GatewayUtils.createItmTunnels(dataBroker, itmRpcService, hwvtepId, psName,
239                                 phySwitchAfter.getTunnelIps().get(0).getTunnelIpsKey());
240                         return Collections.emptyList();
241                     }
242                 );
243                 try {
244                     Thread.sleep(20000L);//TODO remove the sleep by using better itm api to detect finish of prev op
245                 } catch (InterruptedException e) {
246                     LOG.error("Interrupted ");
247                 }
248                 existingDevice.setTunnelIps(new HashSet<>());
249                 added(identifier, phySwitchAfter);
250             }
251         }
252     }
253
254     @Override
255     protected void added(InstanceIdentifier<PhysicalSwitchAugmentation> identifier,
256                          final PhysicalSwitchAugmentation phySwitchAdded) {
257         String globalNodeId = getManagedByNodeId(identifier);
258         final InstanceIdentifier<Node> globalNodeIid = getManagedByNodeIid(identifier);
259         NodeId nodeId = getNodeId(identifier);
260         if (TUNNEL_IP_NOT_AVAILABLE.test(phySwitchAdded)) {
261             LOG.error("Could not find the /tunnel ips for node {}", nodeId.getValue());
262             return;
263         }
264         final String psName = getPsName(identifier);
265         LOG.trace("Received physical switch {} added event received for node {}", psName, nodeId.getValue());
266
267         haOpClusteredListener.runAfterNodeIsConnected(globalNodeIid, (node) -> {
268             LOG.trace("Running job for node {} ", globalNodeIid);
269             if (!node.isPresent()) {
270                 LOG.error("Global node is absent {}", globalNodeId);
271                 return;
272             }
273             HwvtepHAUtil.addToCacheIfHAChildNode(globalNodeIid, node.get(), hwvtepNodeHACache);
274             if (hwvtepNodeHACache.isHAEnabledDevice(globalNodeIid)) {
275                 LOG.trace("Ha enabled device {}", globalNodeIid);
276                 return;
277             }
278             LOG.trace("Updating cache for node {}", globalNodeIid);
279             L2GatewayDevice l2GwDevice = l2GatewayCache.get(psName);
280             if (childConnectedAfterParent.test(l2GwDevice, globalNodeIid)) {
281                 LOG.trace("Device {} {} is already Connected by {}",
282                         psName, globalNodeId, l2GwDevice.getHwvtepNodeId());
283                 return;
284             }
285             InstanceIdentifier<Node> existingIid = globalNodeIid;
286             if (l2GwDevice != null && l2GwDevice.getHwvtepNodeId() != null) {
287                 existingIid = HwvtepHAUtil.convertToInstanceIdentifier(l2GwDevice.getHwvtepNodeId());
288             }
289             if (parentConnectedAfterChild.test(l2GwDevice, globalNodeIid)
290                     && alreadyHasL2Gwids.test(l2GwDevice)) {
291                 LOG.error("Child node {} having l2gw configured became ha node "
292                                 + " removing the l2device {} from all elan cache and provision parent node {}",
293                           existingIid, psName, globalNodeIid);
294                 ElanL2GwCacheUtils.removeL2GatewayDeviceFromAllElanCache(l2GwDevice.getHwvtepNodeId());
295             }
296
297             l2GwDevice = l2GatewayCache.addOrGet(psName);
298             l2GwDevice.setConnected(true);
299             l2GwDevice.setHwvtepNodeId(globalNodeId);
300
301             List<TunnelIps> tunnelIps = phySwitchAdded.getTunnelIps();
302             if (tunnelIps != null) {
303                 for (TunnelIps tunnelIp : tunnelIps) {
304                     IpAddress tunnelIpAddr = tunnelIp.getTunnelIpsKey();
305                     l2GwDevice.addTunnelIp(tunnelIpAddr);
306                 }
307             }
308
309             handleAdd(l2GwDevice);
310             elanClusterUtils.runOnlyInOwnerNode("Update config tunnels IP ",
311                 () -> updateConfigTunnelIp(identifier, phySwitchAdded));
312         });
313     }
314
315     /**
316      * Handle add.
317      *
318      * @param l2GwDevice
319      *            the l2 gw device
320      */
321     private void handleAdd(L2GatewayDevice l2GwDevice) {
322         final String psName = l2GwDevice.getDeviceName();
323         final String hwvtepNodeId = l2GwDevice.getHwvtepNodeId();
324         Set<IpAddress> tunnelIps = l2GwDevice.getTunnelIps();
325         for (final IpAddress tunnelIpAddr : tunnelIps) {
326             if (L2GatewayConnectionUtils.isGatewayAssociatedToL2Device(l2GwDevice)) {
327                 LOG.debug("L2Gateway {} associated for {} physical switch; creating ITM tunnels for {}",
328                         l2GwDevice.getL2GatewayIds(), psName, tunnelIpAddr);
329                 l2gwServiceProvider.provisionItmAndL2gwConnection(l2GwDevice, psName, hwvtepNodeId, tunnelIpAddr);
330             } else {
331                 LOG.info("l2gw.provision.skip {}:{}", hwvtepNodeId, psName);
332             }
333         }
334         elanClusterUtils.runOnlyInOwnerNode("Stale entry cleanup", () -> {
335             InstanceIdentifier<Node> globalNodeIid = HwvtepSouthboundUtils.createInstanceIdentifier(
336                     new NodeId(hwvtepNodeId));
337             InstanceIdentifier<Node> psIid = HwvtepSouthboundUtils.createInstanceIdentifier(
338                     HwvtepSouthboundUtils.createManagedNodeId(new NodeId(hwvtepNodeId), psName));
339             staleVlanBindingsCleaner.scheduleStaleCleanup(psName, globalNodeIid, psIid);
340         });
341     }
342
343
344     /**
345      * Gets the node id.
346      *
347      * @param identifier
348      *            the identifier
349      * @return the node id
350      */
351     private NodeId getNodeId(InstanceIdentifier<PhysicalSwitchAugmentation> identifier) {
352         return identifier.firstKeyOf(Node.class).getNodeId();
353     }
354
355     private String getManagedByNodeId(InstanceIdentifier<PhysicalSwitchAugmentation> identifier) {
356         String psNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
357         if (psNodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
358             return psNodeId.substring(0, psNodeId.indexOf(HwvtepHAUtil.PHYSICALSWITCH));
359         }
360         return psNodeId;
361     }
362
363     private InstanceIdentifier<Node> getManagedByNodeIid(InstanceIdentifier<PhysicalSwitchAugmentation> identifier) {
364         String psNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
365         if (psNodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
366             psNodeId = psNodeId.substring(0, psNodeId.indexOf(HwvtepHAUtil.PHYSICALSWITCH));
367             return identifier.firstIdentifierOf(Topology.class).child(Node.class, new NodeKey(new NodeId(psNodeId)));
368         }
369         return null;
370     }
371
372     private String getPsName(InstanceIdentifier<PhysicalSwitchAugmentation> identifier) {
373         String psNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
374         if (psNodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
375             return psNodeId.substring(psNodeId.indexOf(HwvtepHAUtil.PHYSICALSWITCH) + HwvtepHAUtil.PHYSICALSWITCH
376                     .length());
377         }
378         return null;
379     }
380
381     private void updateConfigTunnelIp(InstanceIdentifier<PhysicalSwitchAugmentation> identifier,
382                                       PhysicalSwitchAugmentation phySwitchAdded) {
383         if (phySwitchAdded.getTunnelIps() != null) {
384             ListenableFutures.addErrorLogging(
385                 txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
386                     Optional<PhysicalSwitchAugmentation> existingSwitch = tx.read(
387                             LogicalDatastoreType.CONFIGURATION, identifier).checkedGet();
388                     PhysicalSwitchAugmentationBuilder psBuilder = new PhysicalSwitchAugmentationBuilder();
389                     if (existingSwitch.isPresent()) {
390                         psBuilder = new PhysicalSwitchAugmentationBuilder(existingSwitch.get());
391                     }
392                     psBuilder.setTunnelIps(phySwitchAdded.getTunnelIps());
393                     tx.put(LogicalDatastoreType.CONFIGURATION, identifier, psBuilder.build(), true);
394                     LOG.trace("Updating config tunnel ips {}", identifier);
395                 }), LOG, "Failed to update the config tunnel ips {}", identifier);
396         }
397     }
398 }