2 * Copyright (c) 2016 ,2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.l2gw.ha.listeners;
10 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.Sets;
14 import java.util.Collections;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ExecutionException;
19 import java.util.function.Consumer;
20 import java.util.stream.Collectors;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
29 import org.opendaylight.genius.infra.Datastore.Operational;
30 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
31 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
32 import org.opendaylight.infrautils.metrics.MetricProvider;
33 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 public class HAOpClusteredListener extends HwvtepNodeBaseListener<Operational>
41 implements ClusteredDataTreeChangeListener<Node> {
42 private static final Logger LOG = LoggerFactory.getLogger(HAOpClusteredListener.class);
44 private final Set<InstanceIdentifier<Node>> connectedNodes = ConcurrentHashMap.newKeySet();
45 private final Map<InstanceIdentifier<Node>, Set<Consumer<Optional<Node>>>> waitingJobs = new ConcurrentHashMap<>();
48 public HAOpClusteredListener(DataBroker db, HwvtepNodeHACache hwvtepNodeHACache,
49 MetricProvider metricProvider) throws Exception {
50 super(OPERATIONAL, db, hwvtepNodeHACache, metricProvider, false);
51 LOG.info("Registering HAOpClusteredListener");
54 public Set<InstanceIdentifier<Node>> getConnectedNodes() {
55 return connectedNodes;
59 synchronized void onGlobalNodeDelete(InstanceIdentifier<Node> key, Node added,
60 TypedReadWriteTransaction<Operational> tx) {
61 connectedNodes.remove(key);
62 getHwvtepNodeHACache().updateDisconnectedNodeStatus(key);
66 void onPsNodeDelete(InstanceIdentifier<Node> key, Node addedPSNode, TypedReadWriteTransaction<Operational> tx) {
67 connectedNodes.remove(key);
68 getHwvtepNodeHACache().updateDisconnectedNodeStatus(key);
72 void onPsNodeAdd(InstanceIdentifier<Node> key, Node addedPSNode, TypedReadWriteTransaction<Operational> tx) {
73 connectedNodes.add(key);
74 getHwvtepNodeHACache().updateConnectedNodeStatus(key);
78 public synchronized void onGlobalNodeAdd(InstanceIdentifier<Node> key, Node updated,
79 TypedReadWriteTransaction<Operational> tx) {
80 connectedNodes. add(key);
81 HwvtepHAUtil.addToCacheIfHAChildNode(key, updated, getHwvtepNodeHACache());
82 getHwvtepNodeHACache().updateConnectedNodeStatus(key);
83 if (waitingJobs.containsKey(key) && !waitingJobs.get(key).isEmpty()) {
85 HAJobScheduler jobScheduler = HAJobScheduler.getInstance();
86 Optional<Node> nodeOptional = tx.read(key).get();
87 if (nodeOptional.isPresent()) {
88 waitingJobs.get(key).forEach(
89 (waitingJob) -> jobScheduler.submitJob(() -> waitingJob.accept(nodeOptional)));
90 waitingJobs.get(key).clear();
92 LOG.error("Failed to read oper node {}", key);
94 } catch (InterruptedException | ExecutionException e) {
95 LOG.error("Failed to read oper node {}", key);
101 void onGlobalNodeUpdate(InstanceIdentifier<Node> childPath,
102 Node updatedChildNode,
103 Node beforeChildNode,
104 DataObjectModification<Node> mod,
105 TypedReadWriteTransaction<Operational> tx) {
106 boolean wasHAChild = getHwvtepNodeHACache().isHAEnabledDevice(childPath);
107 addToHACacheIfBecameHAChild(childPath, updatedChildNode, beforeChildNode);
108 boolean isHAChild = getHwvtepNodeHACache().isHAEnabledDevice(childPath);
111 if (!wasHAChild && isHAChild) {
112 LOG.debug("{} became ha_child", getNodeId(childPath));
113 } else if (wasHAChild && !isHAChild) {
114 LOG.debug("{} unbecome ha_child", getNodeId(childPath));
118 static String getNodeId(InstanceIdentifier<Node> key) {
119 String nodeId = key.firstKeyOf(Node.class).getNodeId().getValue();
120 int idx = nodeId.indexOf("uuid/");
122 nodeId = nodeId.substring(idx + "uuid/".length());
127 public Set<InstanceIdentifier<Node>> getConnected(Set<InstanceIdentifier<Node>> candidateds) {
128 if (candidateds == null) {
129 return Collections.emptySet();
131 return candidateds.stream()
132 .filter(connectedNodes::contains)
133 .collect(Collectors.toSet());
136 public synchronized void runAfterNodeIsConnected(InstanceIdentifier<Node> iid, Consumer<Optional<Node>> consumer) {
137 if (connectedNodes.contains(iid)) {
138 HAJobScheduler.getInstance().submitJob(() -> {
139 try (ReadOnlyTransaction tx = getDataBroker().newReadOnlyTransaction()) {
140 consumer.accept(tx.read(LogicalDatastoreType.OPERATIONAL, iid).checkedGet());
141 } catch (ReadFailedException e) {
142 LOG.error("Failed to read oper ds {}", iid);
146 waitingJobs.computeIfAbsent(iid, key -> Sets.newConcurrentHashSet()).add(consumer);