Remove unused exceptions
[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.Collections;
14 import java.util.HashSet;
15 import java.util.Set;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
20 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
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;
33
34 @Singleton
35 public class HAConfigNodeListener extends HwvtepNodeBaseListener {
36
37     private static final Logger LOG = LoggerFactory.getLogger(HAConfigNodeListener.class);
38
39     private final IHAEventHandler haEventHandler;
40     private final NodeCopier nodeCopier;
41
42     @Inject
43     public HAConfigNodeListener(DataBroker db, HAEventHandler haEventHandler,
44                                 NodeCopier nodeCopier, HwvtepNodeHACache hwvtepNodeHACache,
45                                 MetricProvider metricProvider) throws Exception {
46         super(LogicalDatastoreType.CONFIGURATION, db, hwvtepNodeHACache, metricProvider, true);
47         this.haEventHandler = haEventHandler;
48         this.nodeCopier = nodeCopier;
49     }
50
51     @Override
52     void onPsNodeAdd(InstanceIdentifier<Node> haPsPath,
53                      Node haPSNode,
54                      ReadWriteTransaction tx) throws ReadFailedException {
55         //copy the ps node data to children
56         String psId = haPSNode.getNodeId().getValue();
57         Set<InstanceIdentifier<Node>> childSwitchIds = getPSChildrenIdsForHAPSNode(psId);
58         if (childSwitchIds.isEmpty()) {
59             LOG.error("Failed to find any ha children {}", haPsPath);
60             return;
61         }
62         for (InstanceIdentifier<Node> childPsPath : childSwitchIds) {
63             String nodeId =
64                     HwvtepHAUtil.convertToGlobalNodeId(childPsPath.firstKeyOf(Node.class).getNodeId().getValue());
65             InstanceIdentifier<Node> childGlobalPath = HwvtepHAUtil.convertToInstanceIdentifier(nodeId);
66             nodeCopier.copyPSNode(Optional.fromNullable(haPSNode), haPsPath, childPsPath, childGlobalPath,
67                     LogicalDatastoreType.CONFIGURATION, tx);
68         }
69         LOG.trace("Handle config ps node add {}", psId);
70     }
71
72     @Override
73     void onPsNodeUpdate(Node haPSUpdated,
74             Node haPSOriginal,
75             DataObjectModification<Node> mod,
76             ReadWriteTransaction 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);
82         }
83     }
84
85     @Override
86     void onGlobalNodeUpdate(InstanceIdentifier<Node> key,
87                             Node haUpdated,
88                             Node haOriginal,
89                             DataObjectModification<Node> mod,
90                             ReadWriteTransaction tx) {
91         Set<InstanceIdentifier<Node>> childNodeIds = getHwvtepNodeHACache().getChildrenForHANode(key);
92         for (InstanceIdentifier<Node> haChildNodeId : childNodeIds) {
93             haEventHandler.copyHAGlobalUpdateToChild(haChildNodeId, mod, tx);
94         }
95     }
96
97     @Override
98     void onPsNodeDelete(InstanceIdentifier<Node> key,
99                         Node deletedPsNode,
100                         ReadWriteTransaction tx) throws ReadFailedException {
101         //delete ps children nodes
102         String psId = deletedPsNode.getNodeId().getValue();
103         Set<InstanceIdentifier<Node>> childPsIds = getPSChildrenIdsForHAPSNode(psId);
104         for (InstanceIdentifier<Node> childPsId : childPsIds) {
105             HwvtepHAUtil.deleteNodeIfPresent(tx, CONFIGURATION, childPsId);
106         }
107     }
108
109     @Override
110     void onGlobalNodeDelete(InstanceIdentifier<Node> key,
111                             Node haNode,
112                             ReadWriteTransaction tx)
113             throws ReadFailedException {
114         //delete child nodes
115         Set<InstanceIdentifier<Node>> children = getHwvtepNodeHACache().getChildrenForHANode(key);
116         for (InstanceIdentifier<Node> childId : children) {
117             HwvtepHAUtil.deleteNodeIfPresent(tx, CONFIGURATION, childId);
118         }
119         HwvtepHAUtil.deletePSNodesOfNode(key, haNode, tx);
120     }
121
122     private Set<InstanceIdentifier<Node>> getPSChildrenIdsForHAPSNode(String psNodId) {
123         if (!psNodId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
124             return Collections.emptySet();
125         }
126         String nodeId = HwvtepHAUtil.convertToGlobalNodeId(psNodId);
127         InstanceIdentifier<Node> iid = HwvtepHAUtil.convertToInstanceIdentifier(nodeId);
128         if (getHwvtepNodeHACache().isHAParentNode(iid)) {
129             Set<InstanceIdentifier<Node>> childSwitchIds = new HashSet<>();
130             Set<InstanceIdentifier<Node>> childGlobalIds = getHwvtepNodeHACache().getChildrenForHANode(iid);
131             final String append = psNodId.substring(psNodId.indexOf(HwvtepHAUtil.PHYSICALSWITCH));
132             for (InstanceIdentifier<Node> childId : childGlobalIds) {
133                 String childIdVal = childId.firstKeyOf(Node.class).getNodeId().getValue();
134                 childSwitchIds.add(HwvtepHAUtil.convertToInstanceIdentifier(childIdVal + append));
135             }
136             return childSwitchIds;
137         }
138         return Collections.emptySet();
139     }
140 }