2 * Copyright (c) 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.mdsal.binding.util.Datastore.CONFIGURATION;
12 import java.util.List;
13 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.binding.api.DataObjectModification;
21 import org.opendaylight.mdsal.binding.util.Datastore.Configuration;
22 import org.opendaylight.mdsal.binding.util.TypedReadWriteTransaction;
23 import org.opendaylight.netvirt.elan.l2gw.ha.BatchedTransaction;
24 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
25 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.HAEventHandler;
26 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.IHAEventHandler;
27 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.NodeCopier;
28 import org.opendaylight.netvirt.elan.l2gw.recovery.impl.L2GatewayServiceRecoveryHandler;
29 import org.opendaylight.serviceutils.srm.RecoverableListener;
30 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 public class HAConfigNodeListener extends HwvtepNodeBaseListener<Configuration> implements RecoverableListener {
40 private static final Logger LOG = LoggerFactory.getLogger(HAConfigNodeListener.class);
41 private final IHAEventHandler haEventHandler;
42 private final NodeCopier nodeCopier;
45 public HAConfigNodeListener(DataBroker db, HAEventHandler haEventHandler,
46 NodeCopier nodeCopier,
47 final L2GatewayServiceRecoveryHandler l2GatewayServiceRecoveryHandler,
48 final ServiceRecoveryRegistry serviceRecoveryRegistry) throws Exception {
49 super(CONFIGURATION, db);
50 this.haEventHandler = haEventHandler;
51 this.nodeCopier = nodeCopier;
52 serviceRecoveryRegistry.addRecoverableListener(l2GatewayServiceRecoveryHandler.buildServiceRegistryKey(), this);
53 ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(db);
57 @SuppressWarnings("all")
58 public void registerListener() {
60 LOG.info("Registering HAConfigNodeListener");
61 registerListener(CONFIGURATION, getDataBroker());
62 } catch (Exception e) {
63 LOG.error("HA Config Node register listener error.", e);
67 public void deregisterListener() {
68 LOG.info("Deregistering HAConfigNodeListener");
73 void onPsNodeAdd(InstanceIdentifier<Node> haPsPath,
75 TypedReadWriteTransaction<Configuration> tx)
76 throws ExecutionException, InterruptedException {
77 //copy the ps node data to children
78 String psId = haPSNode.getNodeId().getValue();
79 Set<InstanceIdentifier<Node>> childSwitchIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId);
80 if (childSwitchIds.isEmpty()) {
81 if (!hwvtepHACache.isHAEnabledDevice(haPsPath)) {
82 LOG.error("HAConfigNodeListener Failed to find any ha children {}", haPsPath);
87 for (InstanceIdentifier<Node> childPsPath : childSwitchIds) {
89 HwvtepHAUtil.convertToGlobalNodeId(childPsPath.firstKeyOf(Node.class).getNodeId().getValue());
90 InstanceIdentifier<Node> childGlobalPath = HwvtepHAUtil.convertToInstanceIdentifier(nodeId);
91 nodeCopier.copyPSNode(Optional.ofNullable(haPSNode), haPsPath, childPsPath, childGlobalPath,
94 LOG.trace("Handle config ps node add {}", psId);
98 void onPsNodeUpdate(Node haPSUpdated,
99 DataObjectModification<Node> mod,
100 TypedReadWriteTransaction<Configuration> tx) {
101 //copy the ps node data to children
102 String psId = haPSUpdated.getNodeId().getValue();
103 ((BatchedTransaction)tx).setSrcNodeId(haPSUpdated.getNodeId());
104 ((BatchedTransaction)tx).updateMetric(true);
105 Set<InstanceIdentifier<Node>> childSwitchIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId);
106 for (InstanceIdentifier<Node> childSwitchId : childSwitchIds) {
107 haEventHandler.copyHAPSUpdateToChild(childSwitchId, mod, tx);
108 ((BatchedTransaction)tx).updateMetric(false);
113 void onGlobalNodeAdd(InstanceIdentifier<Node> haGlobalPath, Node haGlobalNode,
114 TypedReadWriteTransaction<Configuration> tx) {
115 //copy the parent global node data to children
116 String haParentId = haGlobalNode.getNodeId().getValue();
117 List<NodeId> childGlobalIds = HwvtepHAUtil
118 .getChildNodeIdsFromManagerOtherConfig(Optional.ofNullable(haGlobalNode));
119 if (childGlobalIds.isEmpty()) {
120 if (!hwvtepHACache.isHAEnabledDevice(haGlobalPath)) {
121 LOG.error("HAConfigNodeListener Failed to find any ha children {}", haGlobalPath);
125 for (NodeId nodeId : childGlobalIds) {
126 InstanceIdentifier<Node> childGlobalPath = HwvtepHAUtil.convertToInstanceIdentifier(nodeId.getValue());
127 nodeCopier.copyGlobalNode(Optional.ofNullable(haGlobalNode), haGlobalPath, childGlobalPath,
130 LOG.trace("Handle config global node add {}", haParentId);
134 void onGlobalNodeUpdate(InstanceIdentifier<Node> key,
137 DataObjectModification<Node> mod,
138 TypedReadWriteTransaction<Configuration> tx) {
139 Set<InstanceIdentifier<Node>> childNodeIds = hwvtepHACache.getChildrenForHANode(key);
140 ((BatchedTransaction)tx).setSrcNodeId(haUpdated.getNodeId());
141 ((BatchedTransaction)tx).updateMetric(true);
142 for (InstanceIdentifier<Node> haChildNodeId : childNodeIds) {
143 haEventHandler.copyHAGlobalUpdateToChild(haChildNodeId, mod, tx);
144 ((BatchedTransaction)tx).updateMetric(false);
149 void onPsNodeDelete(InstanceIdentifier<Node> key,
151 TypedReadWriteTransaction<Configuration> tx) {
152 //delete ps children nodes
153 String psId = deletedPsNode.getNodeId().getValue();
154 Set<InstanceIdentifier<Node>> childPsIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId);
155 for (InstanceIdentifier<Node> childPsId : childPsIds) {
157 HwvtepHAUtil.deleteNodeIfPresent(tx, childPsId);
158 } catch (ExecutionException | InterruptedException e) {
159 LOG.error("Exception while deleting PS node {} from config topo", childPsId);
165 void onGlobalNodeDelete(InstanceIdentifier<Node> key,
167 TypedReadWriteTransaction<Configuration> tx) {
169 Set<InstanceIdentifier<Node>> children = hwvtepHACache.getChildrenForHANode(key);
170 for (InstanceIdentifier<Node> childId : children) {
172 HwvtepHAUtil.deleteNodeIfPresent(tx, childId);
173 } catch (ExecutionException | InterruptedException e) {
174 LOG.error("Exception while deleting Global node {} from config topo ", childId);
178 HwvtepHAUtil.deletePSNodesOfNode(key, haNode, tx);
179 } catch (ExecutionException | InterruptedException e) {
180 LOG.error("Exception while deleting PS nodes for HA Node {} from config topo", haNode.getNodeId());