Remove redundant names in paths
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / ha / handlers / OpNodeUpdatedHandler.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 package org.opendaylight.netvirt.elan.l2gw.ha.handlers;
9
10 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
13 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
14 import org.opendaylight.netvirt.elan.l2gw.ha.merge.GlobalAugmentationMerger;
15 import org.opendaylight.netvirt.elan.l2gw.ha.merge.GlobalNodeMerger;
16 import org.opendaylight.netvirt.elan.l2gw.ha.merge.PSAugmentationMerger;
17 import org.opendaylight.netvirt.elan.l2gw.ha.merge.PSNodeMerger;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22
23 public class OpNodeUpdatedHandler {
24
25     GlobalAugmentationMerger globalAugmentationMerger = GlobalAugmentationMerger.getInstance();
26     PSAugmentationMerger psAugmentationMerger     = PSAugmentationMerger.getInstance();
27     GlobalNodeMerger globalNodeMerger         = GlobalNodeMerger.getInstance();
28     PSNodeMerger psNodeMerger             = PSNodeMerger.getInstance();
29
30     public void handle(Node updatedSrcNode, Node origSrcNode, InstanceIdentifier<Node> haPath, ReadWriteTransaction tx)
31             throws ReadFailedException {
32         if (updatedSrcNode.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
33             copyChildGlobalOpUpdateToHAParent(updatedSrcNode, origSrcNode, haPath, tx);
34         } else {
35             copyChildPsOpUpdateToHAParent(updatedSrcNode, origSrcNode, haPath, tx);
36         }
37     }
38
39     /**
40      * Copy HA ps node update to HA child ps node of operational data tree.
41      *
42      * @param updatedSrcPSNode Updated HA child ps node
43      * @param origSrcPSNode Original HA ps node
44      * @param haPath HA node path
45      * @param tx Transaction
46      * @throws ReadFailedException  Exception thrown if read fails
47      */
48     public void copyChildPsOpUpdateToHAParent(Node updatedSrcPSNode,
49                                               Node origSrcPSNode,
50                                               InstanceIdentifier<Node> haPath,
51                                               ReadWriteTransaction tx) throws ReadFailedException {
52
53         InstanceIdentifier<Node> haPSPath = HwvtepHAUtil.convertPsPath(updatedSrcPSNode, haPath);
54         Node existingHAPSNode = HwvtepHAUtil.readNode(tx, LogicalDatastoreType.OPERATIONAL, haPSPath);
55
56         PhysicalSwitchAugmentation updatedSrc   = HwvtepHAUtil.getPhysicalSwitchAugmentationOfNode(updatedSrcPSNode);
57         PhysicalSwitchAugmentation origSrc      = HwvtepHAUtil.getPhysicalSwitchAugmentationOfNode(origSrcPSNode);
58         PhysicalSwitchAugmentation existingData = HwvtepHAUtil.getPhysicalSwitchAugmentationOfNode(existingHAPSNode);
59
60         psAugmentationMerger.mergeOpUpdate(existingData, updatedSrc, origSrc, haPSPath, tx);
61         psNodeMerger.mergeOpUpdate(existingHAPSNode, updatedSrcPSNode, origSrcPSNode, haPSPath, tx);
62     }
63
64     /**
65      * Copy updated data from HA node to child node of operational data tree.
66      *
67      * @param updatedSrcNode Updated HA child node
68      * @param origSrcNode Original HA node
69      * @param haPath HA node path
70      * @param tx Transaction
71      * @throws ReadFailedException  Exception thrown if read fails
72      */
73     public void copyChildGlobalOpUpdateToHAParent(Node updatedSrcNode,
74                                                   Node origSrcNode,
75                                                   InstanceIdentifier<Node> haPath,
76                                                   ReadWriteTransaction tx) throws ReadFailedException {
77
78         Node existingDstNode = HwvtepHAUtil.readNode(tx, LogicalDatastoreType.OPERATIONAL, haPath);
79         if (existingDstNode == null) {
80             //No dst present nothing to copy
81             return;
82         }
83         HwvtepGlobalAugmentation existingData    = HwvtepHAUtil.getGlobalAugmentationOfNode(existingDstNode);
84         HwvtepGlobalAugmentation updatedSrc = HwvtepHAUtil.getGlobalAugmentationOfNode(updatedSrcNode);
85         HwvtepGlobalAugmentation origSrc    = HwvtepHAUtil.getGlobalAugmentationOfNode(origSrcNode);
86
87         globalAugmentationMerger.mergeOpUpdate(existingData, updatedSrc, origSrc, haPath, tx);
88         globalNodeMerger.mergeOpUpdate(existingDstNode, updatedSrcNode, origSrcNode, haPath, tx);
89     }
90
91 }