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.genius.infra.Datastore.CONFIGURATION;
12 import com.google.common.base.Optional;
13 import java.util.Collections;
14 import java.util.HashSet;
16 import java.util.concurrent.ExecutionException;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
21 import org.opendaylight.genius.infra.Datastore.Configuration;
22 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
23 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
24 import org.opendaylight.infrautils.metrics.MetricProvider;
25 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
26 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.HAEventHandler;
27 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.IHAEventHandler;
28 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.NodeCopier;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 public class HAConfigNodeListener extends HwvtepNodeBaseListener<Configuration> {
37 private static final Logger LOG = LoggerFactory.getLogger(HAConfigNodeListener.class);
39 private final IHAEventHandler haEventHandler;
40 private final NodeCopier nodeCopier;
43 public HAConfigNodeListener(DataBroker db, HAEventHandler haEventHandler,
44 NodeCopier nodeCopier, HwvtepNodeHACache hwvtepNodeHACache,
45 MetricProvider metricProvider) throws Exception {
46 super(CONFIGURATION, db, hwvtepNodeHACache, metricProvider, true);
47 this.haEventHandler = haEventHandler;
48 this.nodeCopier = nodeCopier;
52 void onPsNodeAdd(InstanceIdentifier<Node> haPsPath,
54 TypedReadWriteTransaction<Configuration> tx)
55 throws ExecutionException, InterruptedException {
56 //copy the ps node data to children
57 String psId = haPSNode.getNodeId().getValue();
58 Set<InstanceIdentifier<Node>> childSwitchIds = getPSChildrenIdsForHAPSNode(psId);
59 if (childSwitchIds.isEmpty()) {
60 LOG.error("Failed to find any ha children {}", haPsPath);
63 for (InstanceIdentifier<Node> childPsPath : childSwitchIds) {
65 HwvtepHAUtil.convertToGlobalNodeId(childPsPath.firstKeyOf(Node.class).getNodeId().getValue());
66 InstanceIdentifier<Node> childGlobalPath = HwvtepHAUtil.convertToInstanceIdentifier(nodeId);
67 nodeCopier.copyPSNode(Optional.fromNullable(haPSNode), haPsPath, childPsPath, childGlobalPath,
70 LOG.trace("Handle config ps node add {}", psId);
74 void onPsNodeUpdate(Node haPSUpdated,
75 DataObjectModification<Node> mod,
76 TypedReadWriteTransaction<Configuration> tx) {
77 //copy the ps node data to children
78 String psId = haPSUpdated.getNodeId().getValue();
79 Set<InstanceIdentifier<Node>> childSwitchIds = getPSChildrenIdsForHAPSNode(psId);
80 for (InstanceIdentifier<Node> childSwitchId : childSwitchIds) {
81 haEventHandler.copyHAPSUpdateToChild(childSwitchId, mod, tx);
86 void onGlobalNodeUpdate(InstanceIdentifier<Node> key,
89 DataObjectModification<Node> mod,
90 TypedReadWriteTransaction<Configuration> tx) {
91 Set<InstanceIdentifier<Node>> childNodeIds = getHwvtepNodeHACache().getChildrenForHANode(key);
92 for (InstanceIdentifier<Node> haChildNodeId : childNodeIds) {
93 haEventHandler.copyHAGlobalUpdateToChild(haChildNodeId, mod, tx);
98 void onPsNodeDelete(InstanceIdentifier<Node> key,
100 TypedReadWriteTransaction<Configuration> tx)
101 throws ExecutionException, InterruptedException {
102 //delete ps children nodes
103 String psId = deletedPsNode.getNodeId().getValue();
104 Set<InstanceIdentifier<Node>> childPsIds = getPSChildrenIdsForHAPSNode(psId);
105 for (InstanceIdentifier<Node> childPsId : childPsIds) {
106 HwvtepHAUtil.deleteNodeIfPresent(tx, childPsId);
111 void onGlobalNodeDelete(InstanceIdentifier<Node> key,
113 TypedReadWriteTransaction<Configuration> tx)
114 throws ExecutionException, InterruptedException {
116 Set<InstanceIdentifier<Node>> children = getHwvtepNodeHACache().getChildrenForHANode(key);
117 for (InstanceIdentifier<Node> childId : children) {
118 HwvtepHAUtil.deleteNodeIfPresent(tx, childId);
120 HwvtepHAUtil.deletePSNodesOfNode(key, haNode, tx);
123 private Set<InstanceIdentifier<Node>> getPSChildrenIdsForHAPSNode(String psNodId) {
124 if (!psNodId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
125 return Collections.emptySet();
127 String nodeId = HwvtepHAUtil.convertToGlobalNodeId(psNodId);
128 InstanceIdentifier<Node> iid = HwvtepHAUtil.convertToInstanceIdentifier(nodeId);
129 if (getHwvtepNodeHACache().isHAParentNode(iid)) {
130 Set<InstanceIdentifier<Node>> childSwitchIds = new HashSet<>();
131 Set<InstanceIdentifier<Node>> childGlobalIds = getHwvtepNodeHACache().getChildrenForHANode(iid);
132 final String append = psNodId.substring(psNodId.indexOf(HwvtepHAUtil.PHYSICALSWITCH));
133 for (InstanceIdentifier<Node> childId : childGlobalIds) {
134 String childIdVal = childId.firstKeyOf(Node.class).getNodeId().getValue();
135 childSwitchIds.add(HwvtepHAUtil.convertToInstanceIdentifier(childIdVal + append));
137 return childSwitchIds;
139 return Collections.emptySet();