bug 7599 hwvtep ucast mac add performance improv
[netvirt.git] / vpnservice / elanmanager / 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 java.util.Set;
13 import java.util.concurrent.ExecutionException;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.genius.utils.hwvtep.HwvtepHACache;
19 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
20 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.ConfigNodeUpdatedHandler;
21 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.HAEventHandler;
22 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.IHAEventHandler;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class HAConfigNodeListener extends HwvtepNodeBaseListener {
29     private static final Logger LOG = LoggerFactory.getLogger(HAConfigNodeListener.class);
30
31     static HwvtepHACache hwvtepHACache = HwvtepHACache.getInstance();
32
33     IHAEventHandler haEventHandler;
34     ConfigNodeUpdatedHandler configNodeUpdatedHandler = new ConfigNodeUpdatedHandler();
35
36     public HAConfigNodeListener(DataBroker db, HAEventHandler haEventHandler) throws Exception {
37         super(LogicalDatastoreType.CONFIGURATION, db);
38         this.haEventHandler = haEventHandler;
39     }
40
41     @Override
42     void onPsNodeAdd(InstanceIdentifier<Node> key,
43                      Node haPSNode,
44                      ReadWriteTransaction tx) throws InterruptedException, ExecutionException, ReadFailedException {
45         //copy the ps node data to children
46         String psId = haPSNode.getNodeId().getValue();
47         Set<InstanceIdentifier<Node>> childSwitchIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId);
48         for (InstanceIdentifier<Node> childSwitchId : childSwitchIds) {
49             haEventHandler.copyHAPSUpdateToChild(haPSNode, null/*haOriginal*/, childSwitchId, tx);
50         }
51         LOG.trace("Handle config ps node add {}", psId);
52     }
53
54     @Override
55     void onPsNodeUpdate(InstanceIdentifier<Node> key,
56                         Node haPSUpdated,
57                         Node haPSOriginal,
58                         ReadWriteTransaction tx) throws InterruptedException, ExecutionException, ReadFailedException {
59         //copy the ps node data to children
60         String psId = haPSUpdated.getNodeId().getValue();
61         Set<InstanceIdentifier<Node>> childSwitchIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId);
62         for (InstanceIdentifier<Node> childSwitchId : childSwitchIds) {
63             haEventHandler.copyHAPSUpdateToChild(haPSUpdated, haPSOriginal, childSwitchId, tx);
64         }
65     }
66
67     @Override
68     void onGlobalNodeUpdate(InstanceIdentifier<Node> key,
69                             Node haUpdated,
70                             Node haOriginal,
71                             ReadWriteTransaction tx)
72             throws InterruptedException, ExecutionException, ReadFailedException {
73         //copy the ha node data to children taken care of the HAListeners
74         /*
75         Set<InstanceIdentifier<Node>> childNodeIds = hwvtepHACache.getChildrenForHANode(key);
76         for (InstanceIdentifier<Node> haChildNodeId : childNodeIds) {
77             haEventHandler.copyHAGlobalUpdateToChild(haUpdated, haOriginal, haChildNodeId, tx);
78         }
79         */
80     }
81
82     @Override
83     void onPsNodeDelete(InstanceIdentifier<Node> key,
84                         Node deletedPsNode,
85                         ReadWriteTransaction tx) throws ReadFailedException {
86         //delete ps children nodes
87         String psId = deletedPsNode.getNodeId().getValue();
88         Set<InstanceIdentifier<Node>> childPsIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId);
89         for (InstanceIdentifier<Node> childPsId : childPsIds) {
90             HwvtepHAUtil.deleteNodeIfPresent(tx, CONFIGURATION, childPsId);
91         }
92     }
93
94     @Override
95     void onGlobalNodeDelete(InstanceIdentifier<Node> key,
96                             Node haNode,
97                             ReadWriteTransaction tx)
98             throws ReadFailedException, ExecutionException, InterruptedException {
99         //delete child nodes
100         String deletedNodeId = key.firstKeyOf(Node.class).getNodeId().getValue();
101         Set<InstanceIdentifier<Node>> children = hwvtepHACache.getChildrenForHANode(key);
102         for (InstanceIdentifier<Node> childId : children) {
103             HwvtepHAUtil.deleteNodeIfPresent(tx, CONFIGURATION, childId);
104         }
105         HwvtepHAUtil.deletePSNodesOfNode(key, haNode, tx);
106     }
107 }