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.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
12 import com.google.common.base.Optional;
14 import java.util.concurrent.ExecutionException;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
19 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
23 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.HAEventHandler;
24 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.IHAEventHandler;
25 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.NodeCopier;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 public class HAConfigNodeListener extends HwvtepNodeBaseListener {
31 private final IHAEventHandler haEventHandler;
32 private final NodeCopier nodeCopier;
35 public HAConfigNodeListener(DataBroker db, HAEventHandler haEventHandler,
36 NodeCopier nodeCopier) throws Exception {
37 super(LogicalDatastoreType.CONFIGURATION, db);
38 this.haEventHandler = haEventHandler;
39 this.nodeCopier = nodeCopier;
43 void onPsNodeAdd(InstanceIdentifier<Node> haPsPath,
45 ReadWriteTransaction tx) throws ReadFailedException {
46 //copy the ps node data to children
47 String psId = haPSNode.getNodeId().getValue();
48 Set<InstanceIdentifier<Node>> childSwitchIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId);
49 if (childSwitchIds.isEmpty()) {
50 LOG.error("Failed to find any ha children {}", haPsPath);
53 for (InstanceIdentifier<Node> childPsPath : childSwitchIds) {
55 HwvtepHAUtil.convertToGlobalNodeId(childPsPath.firstKeyOf(Node.class).getNodeId().getValue());
56 InstanceIdentifier<Node> childGlobalPath = HwvtepHAUtil.convertToInstanceIdentifier(nodeId);
57 nodeCopier.copyPSNode(Optional.fromNullable(haPSNode), haPsPath, childPsPath, childGlobalPath,
58 LogicalDatastoreType.CONFIGURATION, tx);
60 LOG.trace("Handle config ps node add {}", psId);
64 void onPsNodeUpdate(Node haPSUpdated,
66 DataObjectModification<Node> mod,
67 ReadWriteTransaction tx) throws InterruptedException, ExecutionException, ReadFailedException {
68 //copy the ps node data to children
69 String psId = haPSUpdated.getNodeId().getValue();
70 Set<InstanceIdentifier<Node>> childSwitchIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId);
71 for (InstanceIdentifier<Node> childSwitchId : childSwitchIds) {
72 haEventHandler.copyHAPSUpdateToChild(childSwitchId, mod, tx);
77 void onGlobalNodeUpdate(InstanceIdentifier<Node> key,
80 DataObjectModification<Node> mod,
81 ReadWriteTransaction tx)
82 throws InterruptedException, ExecutionException, ReadFailedException {
83 Set<InstanceIdentifier<Node>> childNodeIds = hwvtepHACache.getChildrenForHANode(key);
84 for (InstanceIdentifier<Node> haChildNodeId : childNodeIds) {
85 haEventHandler.copyHAGlobalUpdateToChild(haChildNodeId, mod, tx);
90 void onPsNodeDelete(InstanceIdentifier<Node> key,
92 ReadWriteTransaction tx) throws ReadFailedException {
93 //delete ps children nodes
94 String psId = deletedPsNode.getNodeId().getValue();
95 Set<InstanceIdentifier<Node>> childPsIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId);
96 for (InstanceIdentifier<Node> childPsId : childPsIds) {
97 HwvtepHAUtil.deleteNodeIfPresent(tx, CONFIGURATION, childPsId);
102 void onGlobalNodeDelete(InstanceIdentifier<Node> key,
104 ReadWriteTransaction tx)
105 throws ReadFailedException {
107 Set<InstanceIdentifier<Node>> children = hwvtepHACache.getChildrenForHANode(key);
108 for (InstanceIdentifier<Node> childId : children) {
109 HwvtepHAUtil.deleteNodeIfPresent(tx, CONFIGURATION, childId);
111 HwvtepHAUtil.deletePSNodesOfNode(key, haNode, tx);