MRI version bumpup for Aluminium
[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 com.google.common.collect.ImmutableMap;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Objects;
15 import java.util.concurrent.ExecutionException;
16 import java.util.function.Function;
17 import javax.annotation.PreDestroy;
18 import org.opendaylight.genius.datastoreutils.TaskRetryLooper;
19 import org.opendaylight.genius.infra.Datastore;
20 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
21 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
22 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
23 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
24 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
25 import org.opendaylight.infrautils.metrics.Labeled;
26 import org.opendaylight.infrautils.metrics.Meter;
27 import org.opendaylight.infrautils.metrics.MetricDescriptor;
28 import org.opendaylight.infrautils.metrics.MetricProvider;
29 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
30 import org.opendaylight.mdsal.binding.api.DataBroker;
31 import org.opendaylight.mdsal.binding.api.DataObjectModification;
32 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
33 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
34 import org.opendaylight.mdsal.binding.api.DataTreeModification;
35 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Managers;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.opendaylight.yangtools.concepts.ListenerRegistration;
47 import org.opendaylight.yangtools.yang.binding.DataObject;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 public abstract class HwvtepNodeBaseListener<D extends Datastore>
53     implements DataTreeChangeListener<Node>, AutoCloseable {
54
55     private static final Logger LOG = LoggerFactory.getLogger(HwvtepNodeBaseListener.class);
56     private static final int STARTUP_LOOP_TICK = 500;
57     private static final int STARTUP_LOOP_MAX_RETRIES = 8;
58
59     private ListenerRegistration<HwvtepNodeBaseListener> registration;
60     private final DataBroker dataBroker;
61     final ManagedNewTransactionRunner txRunner;
62     private final HwvtepNodeHACache hwvtepNodeHACache;
63     private final Class<D> datastoreType;
64     private final Function<DataObject, String> noLogicalSwitch = (data) -> "No_Ls";
65
66     private final Labeled<Labeled<Labeled<Labeled<Labeled<Meter>>>>> childModCounter;
67     private final Labeled<Labeled<Labeled<Meter>>> nodeModCounter;
68     private final boolean updateMetrics;
69
70     private static final ImmutableMap<Class, Function<DataObject, String>> LOGICAL_SWITCH_EXTRACTOR =
71         new ImmutableMap.Builder<Class, Function<DataObject, String>>()
72             .put(LogicalSwitches.class, data -> ((LogicalSwitches) data).getHwvtepNodeName().getValue())
73             .put(RemoteMcastMacs.class,
74                 data -> logicalSwitchNameFromIid(((RemoteMcastMacs) data).key().getLogicalSwitchRef().getValue()))
75             .put(RemoteUcastMacs.class, data -> logicalSwitchNameFromIid(
76                 ((RemoteUcastMacs) data).key().getLogicalSwitchRef().getValue())).build();
77
78
79     public HwvtepNodeBaseListener(Class<D> datastoreType, DataBroker dataBroker,
80                                   HwvtepNodeHACache hwvtepNodeHACache, MetricProvider metricProvider,
81                                   boolean updateMetrics) throws Exception {
82         this.dataBroker = dataBroker;
83         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
84         this.datastoreType = datastoreType;
85         this.hwvtepNodeHACache = hwvtepNodeHACache;
86         this.updateMetrics = updateMetrics;
87         this.childModCounter = metricProvider.newMeter(
88                 MetricDescriptor.builder().anchor(this).project("netvirt").module("l2gw").id("child").build(),
89                 "datastore", "modification", "class", "nodeid", "logicalswitch");
90         this.nodeModCounter = metricProvider.newMeter(
91                 MetricDescriptor.builder().anchor(this).project("netvirt").module("l2gw").id("node").build(),
92                 "datastore", "modification", "nodeid");
93         registerListener(datastoreType, dataBroker);
94     }
95
96     protected void registerListener(Class<D> dsType, DataBroker broker) throws Exception {
97         final DataTreeIdentifier<Node> treeId = DataTreeIdentifier.create(Datastore.toType(dsType),
98                 getWildcardPath());
99         TaskRetryLooper looper = new TaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
100         registration = looper.loopUntilNoException(() ->
101                 broker.registerDataTreeChangeListener(treeId, HwvtepNodeBaseListener.this));
102     }
103
104     protected DataBroker getDataBroker() {
105         return dataBroker;
106     }
107
108     protected HwvtepNodeHACache getHwvtepNodeHACache() {
109         return hwvtepNodeHACache;
110     }
111
112     /**
113      * If Normal non-ha node changes to HA node , its added to HA cache.
114      *
115      * @param childPath HA child path which got converted to HA node
116      * @param updatedChildNode updated Child node
117      * @param beforeChildNode non-ha node before updated to HA node
118      */
119     protected void addToHACacheIfBecameHAChild(InstanceIdentifier<Node> childPath, Node updatedChildNode,
120                                                Node beforeChildNode) {
121         HwvtepGlobalAugmentation updatedAugmentaion = updatedChildNode.augmentation(HwvtepGlobalAugmentation.class);
122         HwvtepGlobalAugmentation beforeAugmentaion = null;
123         if (beforeChildNode != null) {
124             beforeAugmentaion = beforeChildNode.augmentation(HwvtepGlobalAugmentation.class);
125         }
126         List<Managers> up = null;
127         List<Managers> be = null;
128         if (updatedAugmentaion != null) {
129             up = new ArrayList<Managers>(updatedAugmentaion.getManagers().values());
130         }
131         if (beforeAugmentaion != null) {
132             be = new ArrayList<Managers>(beforeAugmentaion.getManagers().values());
133         }
134
135         if (up != null) {
136             Managers m1 = up.get(0);
137             Managers m2 = be.get(0);
138             if (!Objects.equals(m1, m2)) {
139                 LOG.trace("Manager entry updated for node {} ", updatedChildNode.getNodeId().getValue());
140                 HwvtepHAUtil.addToCacheIfHAChildNode(childPath, updatedChildNode, hwvtepNodeHACache);
141             }
142         }
143     }
144
145     @Override
146     public void onDataTreeChanged(final Collection<DataTreeModification<Node>> changes) {
147         HAJobScheduler.getInstance().submitJob(() -> LoggingFutures.addErrorLogging(
148             txRunner.callWithNewReadWriteTransactionAndSubmit(datastoreType, tx -> {
149                 processConnectedNodes(changes, tx);
150                 processUpdatedNodes(changes, tx);
151                 processDisconnectedNodes(changes, tx);
152             }), LOG, "Error processing data-tree changes"));
153     }
154
155     private void processUpdatedNodes(Collection<DataTreeModification<Node>> changes,
156                                      TypedReadWriteTransaction<D> tx)
157             throws ExecutionException, InterruptedException {
158         for (DataTreeModification<Node> change : changes) {
159             final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
160             final DataObjectModification<Node> mod = change.getRootNode();
161             String nodeId = key.firstKeyOf(Node.class).getNodeId().getValue();
162             Node updated = HwvtepHAUtil.getUpdated(mod);
163             Node original = HwvtepHAUtil.getOriginal(mod);
164             updateCounters(nodeId, mod.getModifiedChildren());
165             if (updated != null && original != null) {
166                 DataObjectModification subMod;
167                 if (!nodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
168                     onGlobalNodeUpdate(key, updated, original, mod, tx);
169                     subMod = change.getRootNode().getModifiedAugmentation(HwvtepGlobalAugmentation.class);
170                 } else {
171                     onPsNodeUpdate(updated, mod, tx);
172                     subMod = change.getRootNode().getModifiedAugmentation(PhysicalSwitchAugmentation.class);
173                 }
174                 if (subMod != null) {
175                     updateCounters(nodeId, subMod.getModifiedChildren());
176                 }
177             }
178         }
179     }
180
181     private String logicalSwitchNameFromChildMod(DataObjectModification<? extends DataObject> childMod) {
182         DataObject dataAfter = childMod.getDataAfter();
183         DataObject data = dataAfter != null ? dataAfter : childMod.getDataBefore();
184         return LOGICAL_SWITCH_EXTRACTOR.getOrDefault(childMod.getModificationType().getClass(), noLogicalSwitch)
185                 .apply(data);
186     }
187
188     private static String logicalSwitchNameFromIid(InstanceIdentifier<?> input) {
189         InstanceIdentifier<LogicalSwitches> iid = (InstanceIdentifier<LogicalSwitches>)input;
190         return iid.firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
191     }
192
193     private void updateCounters(String nodeId,
194                                 Collection<? extends DataObjectModification<? extends DataObject>> childModCollection) {
195         if (childModCollection == null || !updateMetrics) {
196             return;
197         }
198         childModCollection.forEach(childMod -> {
199             String childClsName = childMod.getDataType().getClass().getSimpleName();
200             String modificationType = childMod.getModificationType().toString();
201             String logicalSwitchName = logicalSwitchNameFromChildMod(childMod);
202             childModCounter.label(Datastore.toType(datastoreType).name())
203                     .label(modificationType)
204                     .label(childClsName)
205                     .label(nodeId)
206                     .label(logicalSwitchName).mark();
207         });
208     }
209
210     private void processDisconnectedNodes(Collection<DataTreeModification<Node>> changes,
211                                           TypedReadWriteTransaction<D> tx)
212             throws InterruptedException, ExecutionException {
213         for (DataTreeModification<Node> change : changes) {
214             final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
215             final DataObjectModification<Node> mod = change.getRootNode();
216             Node deleted = HwvtepHAUtil.getRemoved(mod);
217             String nodeId = key.firstKeyOf(Node.class).getNodeId().getValue();
218             if (deleted != null) {
219                 if (updateMetrics) {
220                     nodeModCounter.label(Datastore.toType(datastoreType).name())
221                             .label(DataObjectModification.ModificationType.DELETE.name()).label(nodeId).mark();
222                 }
223                 if (!nodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
224                     LOG.trace("Handle global node delete {}", deleted.getNodeId().getValue());
225                     onGlobalNodeDelete(key, deleted, tx);
226                 } else {
227                     LOG.trace("Handle ps node node delete {}", deleted.getNodeId().getValue());
228                     onPsNodeDelete(key, deleted, tx);
229                 }
230             }
231         }
232     }
233
234     void processConnectedNodes(Collection<DataTreeModification<Node>> changes,
235                                TypedReadWriteTransaction<D> tx)
236             throws ExecutionException, InterruptedException {
237         for (DataTreeModification<Node> change : changes) {
238             InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
239             DataObjectModification<Node> mod = change.getRootNode();
240             Node node = HwvtepHAUtil.getCreated(mod);
241             String nodeId = key.firstKeyOf(Node.class).getNodeId().getValue();
242             if (node != null) {
243                 if (updateMetrics) {
244                     nodeModCounter.label(Datastore.toType(datastoreType).name())
245                             .label(DataObjectModification.ModificationType.WRITE.name()).label(nodeId).mark();
246                 }
247                 if (!nodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
248                     LOG.trace("Handle global node add {}", node.getNodeId().getValue());
249                     onGlobalNodeAdd(key, node, tx);
250                 } else {
251                     LOG.trace("Handle ps node add {}", node.getNodeId().getValue());
252                     onPsNodeAdd(key, node, tx);
253                 }
254             }
255         }
256     }
257
258     private static InstanceIdentifier<Node> getWildcardPath() {
259         InstanceIdentifier<Node> path = InstanceIdentifier
260                 .create(NetworkTopology.class)
261                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
262                 .child(Node.class);
263         return path;
264     }
265
266     @Override
267     @PreDestroy
268     public void close() {
269         if (registration != null) {
270             registration.close();
271         }
272     }
273
274     //default methods
275     void onGlobalNodeDelete(InstanceIdentifier<Node> key, Node added, TypedReadWriteTransaction<D> tx)
276         throws ExecutionException, InterruptedException {
277     }
278
279     void onPsNodeDelete(InstanceIdentifier<Node> key, Node addedPSNode, TypedReadWriteTransaction<D> tx)
280         throws ExecutionException, InterruptedException {
281
282     }
283
284     void onGlobalNodeAdd(InstanceIdentifier<Node> key, Node added, TypedReadWriteTransaction<D> tx) {
285
286     }
287
288     void onPsNodeAdd(InstanceIdentifier<Node> key, Node addedPSNode, TypedReadWriteTransaction<D> tx)
289             throws InterruptedException, ExecutionException {
290
291     }
292
293     void onGlobalNodeUpdate(InstanceIdentifier<Node> key, Node updated, Node original,
294                             DataObjectModification<Node> mod, TypedReadWriteTransaction<D> tx) {
295
296     }
297
298     void onPsNodeUpdate(Node updated,
299                         DataObjectModification<Node> mod, TypedReadWriteTransaction<D> tx) {
300
301     }
302
303 }