logging issues in elanmanager
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / ha / listeners / HwvtepNodeBaseListener.java
1 /*
2  * Copyright © 2016, 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 java.util.Collection;
11 import java.util.concurrent.ExecutionException;
12 import javax.annotation.PreDestroy;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.genius.datastoreutils.TaskRetryLooper;
22 import org.opendaylight.genius.utils.hwvtep.HwvtepHACache;
23 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
24 import org.opendaylight.netvirt.elan.l2gw.ha.BatchedTransaction;
25 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
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.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public abstract class HwvtepNodeBaseListener implements DataTreeChangeListener<Node>, AutoCloseable {
36
37     private static final Logger LOG = LoggerFactory.getLogger(HwvtepNodeBaseListener.class);
38     private static final int STARTUP_LOOP_TICK = 500;
39     private static final int STARTUP_LOOP_MAX_RETRIES = 8;
40
41     static HwvtepHACache hwvtepHACache = HwvtepHACache.getInstance();
42
43     private final ListenerRegistration<HwvtepNodeBaseListener> registration;
44     private final DataBroker dataBroker;
45
46     public HwvtepNodeBaseListener(LogicalDatastoreType datastoreType, DataBroker dataBroker) throws Exception {
47         this.dataBroker = dataBroker;
48
49         final DataTreeIdentifier<Node> treeId = new DataTreeIdentifier<>(datastoreType, getWildcardPath());
50         TaskRetryLooper looper = new TaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
51         registration = looper.loopUntilNoException(() ->
52             dataBroker.registerDataTreeChangeListener(treeId, HwvtepNodeBaseListener.this));
53     }
54
55     protected DataBroker getDataBroker() {
56         return dataBroker;
57     }
58
59     @Override
60     public void onDataTreeChanged(final Collection<DataTreeModification<Node>> changes) {
61         HAJobScheduler.getInstance().submitJob(() -> {
62             ReadWriteTransaction tx = getTx();
63             try {
64                 processConnectedNodes(changes, tx);
65                 processUpdatedNodes(changes, tx);
66                 processDisconnectedNodes(changes, tx);
67                 tx.submit().get();
68             } catch (InterruptedException | ExecutionException | ReadFailedException e) {
69                 LOG.error("Error processing data-tree changes", e);
70             }
71         });
72     }
73
74     private void processUpdatedNodes(Collection<DataTreeModification<Node>> changes,
75                                      ReadWriteTransaction tx)
76             throws ReadFailedException, ExecutionException, InterruptedException {
77         for (DataTreeModification<Node> change : changes) {
78             final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
79             final DataObjectModification<Node> mod = change.getRootNode();
80             String nodeId = key.firstKeyOf(Node.class).getNodeId().getValue();
81             Node updated = HwvtepHAUtil.getUpdated(mod);
82             Node original = HwvtepHAUtil.getOriginal(mod);
83             if (updated != null && original != null) {
84                 if (!nodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
85                     onGlobalNodeUpdate(key, updated, original, mod, tx);
86                 } else {
87                     onPsNodeUpdate(updated, original, mod, tx);
88                 }
89             }
90         }
91     }
92
93     private void processDisconnectedNodes(Collection<DataTreeModification<Node>> changes,
94                                           ReadWriteTransaction tx)
95             throws InterruptedException, ExecutionException, ReadFailedException {
96
97         for (DataTreeModification<Node> change : changes) {
98             final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
99             final DataObjectModification<Node> mod = change.getRootNode();
100             Node deleted = HwvtepHAUtil.getRemoved(mod);
101             String nodeId = key.firstKeyOf(Node.class).getNodeId().getValue();
102             if (deleted != null) {
103                 if (!nodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
104                     LOG.trace("Handle global node delete {}", deleted.getNodeId().getValue());
105                     onGlobalNodeDelete(key, deleted, tx);
106                 } else {
107                     LOG.trace("Handle ps node node delete {}", deleted.getNodeId().getValue());
108                     onPsNodeDelete(key, deleted, tx);
109                 }
110             }
111         }
112     }
113
114     void processConnectedNodes(Collection<DataTreeModification<Node>> changes,
115                                ReadWriteTransaction tx)
116             throws ReadFailedException {
117         for (DataTreeModification<Node> change : changes) {
118             InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
119             DataObjectModification<Node> mod = change.getRootNode();
120             Node node = HwvtepHAUtil.getCreated(mod);
121             String nodeId = key.firstKeyOf(Node.class).getNodeId().getValue();
122             if (node != null) {
123                 if (!nodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
124                     LOG.trace("Handle global node add {}", node.getNodeId().getValue());
125                     onGlobalNodeAdd(key, node, tx);
126                 } else {
127                     LOG.trace("Handle ps node add {}", node.getNodeId().getValue());
128                     onPsNodeAdd(key, node, tx);
129                 }
130             }
131         }
132     }
133
134     private InstanceIdentifier<Node> getWildcardPath() {
135         InstanceIdentifier<Node> path = InstanceIdentifier
136                 .create(NetworkTopology.class)
137                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
138                 .child(Node.class);
139         return path;
140     }
141
142     @Override
143     @PreDestroy
144     public void close() {
145         if (registration != null) {
146             registration.close();
147         }
148     }
149
150     ReadWriteTransaction getTx() {
151         return new BatchedTransaction();
152     }
153
154     //default methods
155     void onGlobalNodeDelete(InstanceIdentifier<Node> key, Node added, ReadWriteTransaction tx)
156             throws ReadFailedException {
157     }
158
159     void onPsNodeDelete(InstanceIdentifier<Node> key, Node addedPSNode, ReadWriteTransaction tx)
160             throws ReadFailedException {
161
162     }
163
164     void onGlobalNodeAdd(InstanceIdentifier<Node> key, Node added, ReadWriteTransaction tx) {
165
166     }
167
168     void onPsNodeAdd(InstanceIdentifier<Node> key, Node addedPSNode, ReadWriteTransaction tx)
169             throws ReadFailedException {
170
171     }
172
173     void onGlobalNodeUpdate(InstanceIdentifier<Node> key, Node updated, Node original,
174                             DataObjectModification<Node> mod, ReadWriteTransaction tx)
175             throws ReadFailedException, InterruptedException, ExecutionException {
176
177     }
178
179     void onPsNodeUpdate(Node updated, Node original,
180                         DataObjectModification<Node> mod, ReadWriteTransaction tx)
181             throws ReadFailedException, InterruptedException, ExecutionException {
182
183     }
184
185 }