Bulk merge of l2gw changes
[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.mdsal.binding.util.Datastore.CONFIGURATION;
11
12 import java.util.List;
13 import java.util.Optional;
14 import java.util.Set;
15 import java.util.concurrent.ExecutionException;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.binding.api.DataObjectModification;
21 import org.opendaylight.mdsal.binding.util.Datastore.Configuration;
22 import org.opendaylight.mdsal.binding.util.TypedReadWriteTransaction;
23 import org.opendaylight.netvirt.elan.l2gw.ha.BatchedTransaction;
24 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
25 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.HAEventHandler;
26 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.IHAEventHandler;
27 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.NodeCopier;
28 import org.opendaylight.netvirt.elan.l2gw.recovery.impl.L2GatewayServiceRecoveryHandler;
29 import org.opendaylight.serviceutils.srm.RecoverableListener;
30 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 @Singleton
38 public class HAConfigNodeListener extends HwvtepNodeBaseListener<Configuration> implements RecoverableListener {
39
40     private static final Logger LOG = LoggerFactory.getLogger(HAConfigNodeListener.class);
41     private final IHAEventHandler haEventHandler;
42     private final NodeCopier nodeCopier;
43
44     @Inject
45     public HAConfigNodeListener(DataBroker db, HAEventHandler haEventHandler,
46                                 NodeCopier nodeCopier,
47                                 final L2GatewayServiceRecoveryHandler l2GatewayServiceRecoveryHandler,
48                                 final ServiceRecoveryRegistry serviceRecoveryRegistry) throws Exception {
49         super(CONFIGURATION, db);
50         this.haEventHandler = haEventHandler;
51         this.nodeCopier = nodeCopier;
52         serviceRecoveryRegistry.addRecoverableListener(l2GatewayServiceRecoveryHandler.buildServiceRegistryKey(), this);
53         ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(db);
54     }
55
56     @Override
57     @SuppressWarnings("all")
58     public void registerListener() {
59         try {
60             LOG.info("Registering HAConfigNodeListener");
61             registerListener(CONFIGURATION, getDataBroker());
62         } catch (Exception e) {
63             LOG.error("HA Config Node register listener error.", e);
64         }
65     }
66
67     public void deregisterListener() {
68         LOG.info("Deregistering HAConfigNodeListener");
69         super.close();
70     }
71
72     @Override
73     void onPsNodeAdd(InstanceIdentifier<Node> haPsPath,
74                     Node haPSNode,
75                     TypedReadWriteTransaction<Configuration> tx)
76             throws ExecutionException, InterruptedException {
77         //copy the ps node data to children
78         String psId = haPSNode.getNodeId().getValue();
79         Set<InstanceIdentifier<Node>> childSwitchIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId);
80         if (childSwitchIds.isEmpty()) {
81             if (!hwvtepHACache.isHAEnabledDevice(haPsPath)) {
82                 LOG.error("HAConfigNodeListener Failed to find any ha children {}", haPsPath);
83             }
84             return;
85         }
86
87         for (InstanceIdentifier<Node> childPsPath : childSwitchIds) {
88             String nodeId =
89                     HwvtepHAUtil.convertToGlobalNodeId(childPsPath.firstKeyOf(Node.class).getNodeId().getValue());
90             InstanceIdentifier<Node> childGlobalPath = HwvtepHAUtil.convertToInstanceIdentifier(nodeId);
91             nodeCopier.copyPSNode(Optional.ofNullable(haPSNode), haPsPath, childPsPath, childGlobalPath,
92                     CONFIGURATION, tx);
93         }
94         LOG.trace("Handle config ps node add {}", psId);
95     }
96
97     @Override
98     void onPsNodeUpdate(Node haPSUpdated,
99         DataObjectModification<Node> mod,
100         TypedReadWriteTransaction<Configuration> tx) {
101         //copy the ps node data to children
102         String psId = haPSUpdated.getNodeId().getValue();
103         ((BatchedTransaction)tx).setSrcNodeId(haPSUpdated.getNodeId());
104         ((BatchedTransaction)tx).updateMetric(true);
105         Set<InstanceIdentifier<Node>> childSwitchIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId);
106         for (InstanceIdentifier<Node> childSwitchId : childSwitchIds) {
107             haEventHandler.copyHAPSUpdateToChild(childSwitchId, mod, tx);
108             ((BatchedTransaction)tx).updateMetric(false);
109         }
110     }
111
112     @Override
113     void onGlobalNodeAdd(InstanceIdentifier<Node> haGlobalPath, Node haGlobalNode,
114         TypedReadWriteTransaction<Configuration> tx) {
115         //copy the parent global node data to children
116         String haParentId = haGlobalNode.getNodeId().getValue();
117         List<NodeId> childGlobalIds = HwvtepHAUtil
118                 .getChildNodeIdsFromManagerOtherConfig(Optional.ofNullable(haGlobalNode));
119         if (childGlobalIds.isEmpty()) {
120             if (!hwvtepHACache.isHAEnabledDevice(haGlobalPath)) {
121                 LOG.error("HAConfigNodeListener Failed to find any ha children {}", haGlobalPath);
122             }
123             return;
124         }
125         for (NodeId nodeId : childGlobalIds) {
126             InstanceIdentifier<Node> childGlobalPath = HwvtepHAUtil.convertToInstanceIdentifier(nodeId.getValue());
127             nodeCopier.copyGlobalNode(Optional.ofNullable(haGlobalNode), haGlobalPath, childGlobalPath,
128                     CONFIGURATION, tx);
129         }
130         LOG.trace("Handle config global node add {}", haParentId);
131     }
132
133     @Override
134     void onGlobalNodeUpdate(InstanceIdentifier<Node> key,
135                             Node haUpdated,
136                             Node haOriginal,
137                             DataObjectModification<Node> mod,
138                             TypedReadWriteTransaction<Configuration> tx) {
139         Set<InstanceIdentifier<Node>> childNodeIds = hwvtepHACache.getChildrenForHANode(key);
140         ((BatchedTransaction)tx).setSrcNodeId(haUpdated.getNodeId());
141         ((BatchedTransaction)tx).updateMetric(true);
142         for (InstanceIdentifier<Node> haChildNodeId : childNodeIds) {
143             haEventHandler.copyHAGlobalUpdateToChild(haChildNodeId, mod, tx);
144             ((BatchedTransaction)tx).updateMetric(false);
145         }
146     }
147
148     @Override
149     void onPsNodeDelete(InstanceIdentifier<Node> key,
150                         Node deletedPsNode,
151                         TypedReadWriteTransaction<Configuration> tx) {
152         //delete ps children nodes
153         String psId = deletedPsNode.getNodeId().getValue();
154         Set<InstanceIdentifier<Node>> childPsIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId);
155         for (InstanceIdentifier<Node> childPsId : childPsIds) {
156             try {
157                 HwvtepHAUtil.deleteNodeIfPresent(tx, childPsId);
158             } catch (ExecutionException | InterruptedException e) {
159                 LOG.error("Exception while deleting PS node {} from config topo", childPsId);
160             }
161         }
162     }
163
164     @Override
165     void onGlobalNodeDelete(InstanceIdentifier<Node> key,
166                             Node haNode,
167                             TypedReadWriteTransaction<Configuration> tx) {
168         //delete child nodes
169         Set<InstanceIdentifier<Node>> children = hwvtepHACache.getChildrenForHANode(key);
170         for (InstanceIdentifier<Node> childId : children) {
171             try {
172                 HwvtepHAUtil.deleteNodeIfPresent(tx, childId);
173             } catch (ExecutionException | InterruptedException e) {
174                 LOG.error("Exception while deleting Global node {} from config topo ", childId);
175             }
176         }
177         try {
178             HwvtepHAUtil.deletePSNodesOfNode(key, haNode, tx);
179         } catch (ExecutionException | InterruptedException e) {
180             LOG.error("Exception while deleting PS nodes for HA Node {} from config topo", haNode.getNodeId());
181         }
182     }
183 }