Fix more bad logging in vpnmanager
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / ha / listeners / HAConfigNodeListener.java
1 /*
2  * Copyright (c) 2017 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.listeners;
9
10 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
11
12 import com.google.common.base.Optional;
13 import java.util.Set;
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;
28
29 @Singleton
30 public class HAConfigNodeListener extends HwvtepNodeBaseListener {
31     private final IHAEventHandler haEventHandler;
32     private final NodeCopier nodeCopier;
33
34     @Inject
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;
40     }
41
42     @Override
43     void onPsNodeAdd(InstanceIdentifier<Node> haPsPath,
44                      Node haPSNode,
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);
51             return;
52         }
53         for (InstanceIdentifier<Node> childPsPath : childSwitchIds) {
54             String nodeId =
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);
59         }
60         LOG.trace("Handle config ps node add {}", psId);
61     }
62
63     @Override
64     void onPsNodeUpdate(Node haPSUpdated,
65             Node haPSOriginal,
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);
73         }
74     }
75
76     @Override
77     void onGlobalNodeUpdate(InstanceIdentifier<Node> key,
78                             Node haUpdated,
79                             Node haOriginal,
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);
86         }
87     }
88
89     @Override
90     void onPsNodeDelete(InstanceIdentifier<Node> key,
91                         Node deletedPsNode,
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);
98         }
99     }
100
101     @Override
102     void onGlobalNodeDelete(InstanceIdentifier<Node> key,
103                             Node haNode,
104                             ReadWriteTransaction tx)
105             throws ReadFailedException {
106         //delete child nodes
107         Set<InstanceIdentifier<Node>> children = hwvtepHACache.getChildrenForHANode(key);
108         for (InstanceIdentifier<Node> childId : children) {
109             HwvtepHAUtil.deleteNodeIfPresent(tx, CONFIGURATION, childId);
110         }
111         HwvtepHAUtil.deletePSNodesOfNode(key, haNode, tx);
112     }
113 }