2 * Copyright © 2016, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.elan.l2gw.ha.listeners;
10 import com.google.common.collect.ImmutableMap;
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.concurrent.ExecutionException;
14 import java.util.function.Function;
15 import javax.annotation.PreDestroy;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.genius.datastoreutils.TaskRetryLooper;
26 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
27 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
28 import org.opendaylight.infrautils.metrics.Labeled;
29 import org.opendaylight.infrautils.metrics.Meter;
30 import org.opendaylight.infrautils.metrics.MetricDescriptor;
31 import org.opendaylight.infrautils.metrics.MetricProvider;
32 import org.opendaylight.netvirt.elan.l2gw.ha.BatchedTransaction;
33 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Managers;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yangtools.concepts.ListenerRegistration;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public abstract class HwvtepNodeBaseListener implements DataTreeChangeListener<Node>, AutoCloseable {
52 private static final Logger LOG = LoggerFactory.getLogger(HwvtepNodeBaseListener.class);
53 private static final int STARTUP_LOOP_TICK = 500;
54 private static final int STARTUP_LOOP_MAX_RETRIES = 8;
56 private final ListenerRegistration<HwvtepNodeBaseListener> registration;
57 private final DataBroker dataBroker;
58 private final HwvtepNodeHACache hwvtepNodeHACache;
59 private final MetricProvider metricProvider;
60 private final LogicalDatastoreType datastoreType;
61 private final Function<DataObject, String> noLogicalSwitch = (data) -> "No_Ls";
63 private final Labeled<Labeled<Labeled<Labeled<Labeled<Meter>>>>> childModCounter;
64 private final Labeled<Labeled<Labeled<Meter>>> nodeModCounter;
65 private final boolean updateMetrics;
67 ImmutableMap<Class, Function<DataObject, String>> logicalSwitchExtractor =
68 new ImmutableMap.Builder<Class, Function<DataObject, String>>()
69 .put(LogicalSwitches.class, data -> ((LogicalSwitches) data).getHwvtepNodeName().getValue())
70 .put(RemoteMcastMacs.class, data -> {
71 return logicalSwitchNameFromIid(((RemoteMcastMacs) data).getKey().getLogicalSwitchRef()
74 .put(RemoteUcastMacs.class, data -> {
75 return logicalSwitchNameFromIid(((RemoteUcastMacs) data).getKey().getLogicalSwitchRef()
80 public HwvtepNodeBaseListener(LogicalDatastoreType datastoreType, DataBroker dataBroker,
81 HwvtepNodeHACache hwvtepNodeHACache, MetricProvider metricProvider,
82 boolean updateMetrics) throws Exception {
83 this.dataBroker = dataBroker;
84 this.datastoreType = datastoreType;
85 this.hwvtepNodeHACache = hwvtepNodeHACache;
86 this.metricProvider = metricProvider;
87 this.updateMetrics = updateMetrics;
88 this.childModCounter = metricProvider.newMeter(
89 MetricDescriptor.builder().anchor(this).project("netvirt").module("l2gw").id("child").build(),
90 "datastore", "modification", "class", "nodeid", "logicalswitch");
91 this.nodeModCounter = metricProvider.newMeter(
92 MetricDescriptor.builder().anchor(this).project("netvirt").module("l2gw").id("node").build(),
93 "datastore", "modification", "nodeid");
94 final DataTreeIdentifier<Node> treeId = new DataTreeIdentifier<>(datastoreType, getWildcardPath());
95 TaskRetryLooper looper = new TaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
96 registration = looper.loopUntilNoException(() ->
97 dataBroker.registerDataTreeChangeListener(treeId, HwvtepNodeBaseListener.this));
100 protected DataBroker getDataBroker() {
104 protected HwvtepNodeHACache getHwvtepNodeHACache() {
105 return hwvtepNodeHACache;
109 * If Normal non-ha node changes to HA node , its added to HA cache.
111 * @param childPath HA child path which got converted to HA node
112 * @param updatedChildNode updated Child node
113 * @param beforeChildNode non-ha node before updated to HA node
115 protected void addToHACacheIfBecameHAChild(InstanceIdentifier<Node> childPath, Node updatedChildNode,
116 Node beforeChildNode) {
117 HwvtepGlobalAugmentation updatedAugmentaion = updatedChildNode.getAugmentation(HwvtepGlobalAugmentation.class);
118 HwvtepGlobalAugmentation beforeAugmentaion = null;
119 if (beforeChildNode != null) {
120 beforeAugmentaion = beforeChildNode.getAugmentation(HwvtepGlobalAugmentation.class);
122 List<Managers> up = null;
123 List<Managers> be = null;
124 if (updatedAugmentaion != null) {
125 up = updatedAugmentaion.getManagers();
127 if (beforeAugmentaion != null) {
128 be = beforeAugmentaion.getManagers();
131 if (up != null && be != null && up.size() > 0 && be.size() > 0) {
132 Managers m1 = up.get(0);
133 Managers m2 = be.get(0);
134 if (!m1.equals(m2)) {
135 LOG.trace("Manager entry updated for node {} ", updatedChildNode.getNodeId().getValue());
136 HwvtepHAUtil.addToCacheIfHAChildNode(childPath, updatedChildNode, hwvtepNodeHACache);
142 public void onDataTreeChanged(final Collection<DataTreeModification<Node>> changes) {
143 HAJobScheduler.getInstance().submitJob(() -> {
144 ReadWriteTransaction tx = getTx();
146 processConnectedNodes(changes, tx);
147 processUpdatedNodes(changes, tx);
148 processDisconnectedNodes(changes, tx);
150 } catch (InterruptedException | ExecutionException | ReadFailedException e) {
151 LOG.error("Error processing data-tree changes", e);
156 private void processUpdatedNodes(Collection<DataTreeModification<Node>> changes,
157 ReadWriteTransaction tx)
158 throws ReadFailedException, ExecutionException, InterruptedException {
159 for (DataTreeModification<Node> change : changes) {
160 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
161 final DataObjectModification<Node> mod = change.getRootNode();
162 String nodeId = key.firstKeyOf(Node.class).getNodeId().getValue();
163 Node updated = HwvtepHAUtil.getUpdated(mod);
164 Node original = HwvtepHAUtil.getOriginal(mod);
165 updateCounters(nodeId, mod.getModifiedChildren());
166 if (updated != null && original != null) {
167 DataObjectModification subMod;
168 if (!nodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
169 onGlobalNodeUpdate(key, updated, original, mod, tx);
170 subMod = change.getRootNode().getModifiedAugmentation(HwvtepGlobalAugmentation.class);
172 onPsNodeUpdate(updated, original, mod, tx);
173 subMod = change.getRootNode().getModifiedAugmentation(PhysicalSwitchAugmentation.class);
175 if (subMod != null) {
176 updateCounters(nodeId, subMod.getModifiedChildren());
182 private String logicalSwitchNameFromChildMod(DataObjectModification<? extends DataObject> childMod) {
183 DataObject data = childMod.getDataAfter() != null ? childMod.getDataAfter() : childMod.getDataBefore();
184 return logicalSwitchExtractor.getOrDefault(childMod.getModificationType().getClass(), noLogicalSwitch)
188 private String logicalSwitchNameFromIid(InstanceIdentifier input) {
189 InstanceIdentifier<LogicalSwitches> iid = (InstanceIdentifier<LogicalSwitches>)input;
190 return iid.firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
193 private void updateCounters(String nodeId,
194 Collection<DataObjectModification<? extends DataObject>> childModCollection) {
195 if (childModCollection == null || !updateMetrics) {
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(datastoreType.name())
203 .label(modificationType)
206 .label(logicalSwitchName).mark();
210 private void processDisconnectedNodes(Collection<DataTreeModification<Node>> changes,
211 ReadWriteTransaction tx)
212 throws InterruptedException, ExecutionException, ReadFailedException {
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) {
220 nodeModCounter.label(datastoreType.name())
221 .label(DataObjectModification.ModificationType.DELETE.name()).label(nodeId).mark();
223 if (!nodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
224 LOG.trace("Handle global node delete {}", deleted.getNodeId().getValue());
225 onGlobalNodeDelete(key, deleted, tx);
227 LOG.trace("Handle ps node node delete {}", deleted.getNodeId().getValue());
228 onPsNodeDelete(key, deleted, tx);
234 void processConnectedNodes(Collection<DataTreeModification<Node>> changes,
235 ReadWriteTransaction tx)
236 throws ReadFailedException {
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();
244 nodeModCounter.label(datastoreType.name())
245 .label(DataObjectModification.ModificationType.WRITE.name()).label(nodeId).mark();
247 if (!nodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
248 LOG.trace("Handle global node add {}", node.getNodeId().getValue());
249 onGlobalNodeAdd(key, node, tx);
251 LOG.trace("Handle ps node add {}", node.getNodeId().getValue());
252 onPsNodeAdd(key, node, tx);
258 private InstanceIdentifier<Node> getWildcardPath() {
259 InstanceIdentifier<Node> path = InstanceIdentifier
260 .create(NetworkTopology.class)
261 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
268 public void close() {
269 if (registration != null) {
270 registration.close();
274 ReadWriteTransaction getTx() {
275 return new BatchedTransaction();
279 void onGlobalNodeDelete(InstanceIdentifier<Node> key, Node added, ReadWriteTransaction tx)
280 throws ReadFailedException {
283 void onPsNodeDelete(InstanceIdentifier<Node> key, Node addedPSNode, ReadWriteTransaction tx)
284 throws ReadFailedException {
288 void onGlobalNodeAdd(InstanceIdentifier<Node> key, Node added, ReadWriteTransaction tx) {
292 void onPsNodeAdd(InstanceIdentifier<Node> key, Node addedPSNode, ReadWriteTransaction tx)
293 throws ReadFailedException {
297 void onGlobalNodeUpdate(InstanceIdentifier<Node> key, Node updated, Node original,
298 DataObjectModification<Node> mod, ReadWriteTransaction tx)
299 throws ReadFailedException, InterruptedException, ExecutionException {
303 void onPsNodeUpdate(Node updated, Node original,
304 DataObjectModification<Node> mod, ReadWriteTransaction tx)
305 throws ReadFailedException, InterruptedException, ExecutionException {