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;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.genius.datastoreutils.TaskRetryLooper;
23 import org.opendaylight.genius.infra.Datastore;
24 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
25 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
26 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
27 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
28 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
29 import org.opendaylight.infrautils.metrics.Labeled;
30 import org.opendaylight.infrautils.metrics.Meter;
31 import org.opendaylight.infrautils.metrics.MetricDescriptor;
32 import org.opendaylight.infrautils.metrics.MetricProvider;
33 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
34 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Managers;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
45 import org.opendaylight.yangtools.concepts.ListenerRegistration;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public abstract class HwvtepNodeBaseListener<D extends Datastore>
52 implements DataTreeChangeListener<Node>, AutoCloseable {
54 private static final Logger LOG = LoggerFactory.getLogger(HwvtepNodeBaseListener.class);
55 private static final int STARTUP_LOOP_TICK = 500;
56 private static final int STARTUP_LOOP_MAX_RETRIES = 8;
58 private final ListenerRegistration<HwvtepNodeBaseListener> registration;
59 private final DataBroker dataBroker;
60 final ManagedNewTransactionRunner txRunner;
61 private final HwvtepNodeHACache hwvtepNodeHACache;
62 private final Class<D> datastoreType;
63 private final Function<DataObject, String> noLogicalSwitch = (data) -> "No_Ls";
65 private final Labeled<Labeled<Labeled<Labeled<Labeled<Meter>>>>> childModCounter;
66 private final Labeled<Labeled<Labeled<Meter>>> nodeModCounter;
67 private final boolean updateMetrics;
69 private static final ImmutableMap<Class, Function<DataObject, String>> LOGICAL_SWITCH_EXTRACTOR =
70 new ImmutableMap.Builder<Class, Function<DataObject, String>>()
71 .put(LogicalSwitches.class, data -> ((LogicalSwitches) data).getHwvtepNodeName().getValue())
72 .put(RemoteMcastMacs.class,
73 data -> logicalSwitchNameFromIid(((RemoteMcastMacs) data).key().getLogicalSwitchRef().getValue()))
74 .put(RemoteUcastMacs.class, data -> logicalSwitchNameFromIid(
75 ((RemoteUcastMacs) data).key().getLogicalSwitchRef().getValue())).build();
78 public HwvtepNodeBaseListener(Class<D> datastoreType, DataBroker dataBroker,
79 HwvtepNodeHACache hwvtepNodeHACache, MetricProvider metricProvider,
80 boolean updateMetrics) throws Exception {
81 this.dataBroker = dataBroker;
82 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
83 this.datastoreType = datastoreType;
84 this.hwvtepNodeHACache = hwvtepNodeHACache;
85 this.updateMetrics = updateMetrics;
86 this.childModCounter = metricProvider.newMeter(
87 MetricDescriptor.builder().anchor(this).project("netvirt").module("l2gw").id("child").build(),
88 "datastore", "modification", "class", "nodeid", "logicalswitch");
89 this.nodeModCounter = metricProvider.newMeter(
90 MetricDescriptor.builder().anchor(this).project("netvirt").module("l2gw").id("node").build(),
91 "datastore", "modification", "nodeid");
92 final DataTreeIdentifier<Node> treeId =
93 new DataTreeIdentifier<>(Datastore.toType(datastoreType), getWildcardPath());
94 TaskRetryLooper looper = new TaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
95 registration = looper.loopUntilNoException(() ->
96 dataBroker.registerDataTreeChangeListener(treeId, HwvtepNodeBaseListener.this));
99 protected DataBroker getDataBroker() {
103 protected HwvtepNodeHACache getHwvtepNodeHACache() {
104 return hwvtepNodeHACache;
108 * If Normal non-ha node changes to HA node , its added to HA cache.
110 * @param childPath HA child path which got converted to HA node
111 * @param updatedChildNode updated Child node
112 * @param beforeChildNode non-ha node before updated to HA node
114 protected void addToHACacheIfBecameHAChild(InstanceIdentifier<Node> childPath, Node updatedChildNode,
115 Node beforeChildNode) {
116 HwvtepGlobalAugmentation updatedAugmentaion = updatedChildNode.augmentation(HwvtepGlobalAugmentation.class);
117 HwvtepGlobalAugmentation beforeAugmentaion = null;
118 if (beforeChildNode != null) {
119 beforeAugmentaion = beforeChildNode.augmentation(HwvtepGlobalAugmentation.class);
121 List<Managers> up = null;
122 List<Managers> be = null;
123 if (updatedAugmentaion != null) {
124 up = updatedAugmentaion.getManagers();
126 if (beforeAugmentaion != null) {
127 be = beforeAugmentaion.getManagers();
130 if (up != null && be != null && up.size() > 0 && be.size() > 0) {
131 Managers m1 = up.get(0);
132 Managers m2 = be.get(0);
133 if (!m1.equals(m2)) {
134 LOG.trace("Manager entry updated for node {} ", updatedChildNode.getNodeId().getValue());
135 HwvtepHAUtil.addToCacheIfHAChildNode(childPath, updatedChildNode, hwvtepNodeHACache);
141 public void onDataTreeChanged(final Collection<DataTreeModification<Node>> changes) {
142 HAJobScheduler.getInstance().submitJob(() -> ListenableFutures.addErrorLogging(
143 txRunner.callWithNewReadWriteTransactionAndSubmit(datastoreType, tx -> {
144 processConnectedNodes(changes, tx);
145 processUpdatedNodes(changes, tx);
146 processDisconnectedNodes(changes, tx);
147 }), LOG, "Error processing data-tree changes"));
150 private void processUpdatedNodes(Collection<DataTreeModification<Node>> changes,
151 TypedReadWriteTransaction<D> tx)
152 throws ReadFailedException, ExecutionException, InterruptedException {
153 for (DataTreeModification<Node> change : changes) {
154 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
155 final DataObjectModification<Node> mod = change.getRootNode();
156 String nodeId = key.firstKeyOf(Node.class).getNodeId().getValue();
157 Node updated = HwvtepHAUtil.getUpdated(mod);
158 Node original = HwvtepHAUtil.getOriginal(mod);
159 updateCounters(nodeId, mod.getModifiedChildren());
160 if (updated != null && original != null) {
161 DataObjectModification subMod;
162 if (!nodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
163 onGlobalNodeUpdate(key, updated, original, mod, tx);
164 subMod = change.getRootNode().getModifiedAugmentation(HwvtepGlobalAugmentation.class);
166 onPsNodeUpdate(updated, original, mod, tx);
167 subMod = change.getRootNode().getModifiedAugmentation(PhysicalSwitchAugmentation.class);
169 if (subMod != null) {
170 updateCounters(nodeId, subMod.getModifiedChildren());
176 private String logicalSwitchNameFromChildMod(DataObjectModification<? extends DataObject> childMod) {
177 DataObject data = childMod.getDataAfter() != null ? childMod.getDataAfter() : childMod.getDataBefore();
178 return LOGICAL_SWITCH_EXTRACTOR.getOrDefault(childMod.getModificationType().getClass(), noLogicalSwitch)
182 private static String logicalSwitchNameFromIid(InstanceIdentifier<?> input) {
183 InstanceIdentifier<LogicalSwitches> iid = (InstanceIdentifier<LogicalSwitches>)input;
184 return iid.firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
187 private void updateCounters(String nodeId,
188 Collection<? extends DataObjectModification<? extends DataObject>> childModCollection) {
189 if (childModCollection == null || !updateMetrics) {
192 childModCollection.forEach(childMod -> {
193 String childClsName = childMod.getDataType().getClass().getSimpleName();
194 String modificationType = childMod.getModificationType().toString();
195 String logicalSwitchName = logicalSwitchNameFromChildMod(childMod);
196 childModCounter.label(Datastore.toType(datastoreType).name())
197 .label(modificationType)
200 .label(logicalSwitchName).mark();
204 private void processDisconnectedNodes(Collection<DataTreeModification<Node>> changes,
205 TypedReadWriteTransaction<D> tx)
206 throws InterruptedException, ExecutionException, ReadFailedException {
207 for (DataTreeModification<Node> change : changes) {
208 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
209 final DataObjectModification<Node> mod = change.getRootNode();
210 Node deleted = HwvtepHAUtil.getRemoved(mod);
211 String nodeId = key.firstKeyOf(Node.class).getNodeId().getValue();
212 if (deleted != null) {
214 nodeModCounter.label(Datastore.toType(datastoreType).name())
215 .label(DataObjectModification.ModificationType.DELETE.name()).label(nodeId).mark();
217 if (!nodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
218 LOG.trace("Handle global node delete {}", deleted.getNodeId().getValue());
219 onGlobalNodeDelete(key, deleted, tx);
221 LOG.trace("Handle ps node node delete {}", deleted.getNodeId().getValue());
222 onPsNodeDelete(key, deleted, tx);
228 void processConnectedNodes(Collection<DataTreeModification<Node>> changes,
229 TypedReadWriteTransaction<D> tx)
230 throws ExecutionException, InterruptedException {
231 for (DataTreeModification<Node> change : changes) {
232 InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
233 DataObjectModification<Node> mod = change.getRootNode();
234 Node node = HwvtepHAUtil.getCreated(mod);
235 String nodeId = key.firstKeyOf(Node.class).getNodeId().getValue();
238 nodeModCounter.label(Datastore.toType(datastoreType).name())
239 .label(DataObjectModification.ModificationType.WRITE.name()).label(nodeId).mark();
241 if (!nodeId.contains(HwvtepHAUtil.PHYSICALSWITCH)) {
242 LOG.trace("Handle global node add {}", node.getNodeId().getValue());
243 onGlobalNodeAdd(key, node, tx);
245 LOG.trace("Handle ps node add {}", node.getNodeId().getValue());
246 onPsNodeAdd(key, node, tx);
252 private InstanceIdentifier<Node> getWildcardPath() {
253 InstanceIdentifier<Node> path = InstanceIdentifier
254 .create(NetworkTopology.class)
255 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
262 public void close() {
263 if (registration != null) {
264 registration.close();
269 void onGlobalNodeDelete(InstanceIdentifier<Node> key, Node added, TypedReadWriteTransaction<D> tx)
270 throws ReadFailedException, ExecutionException, InterruptedException {
273 void onPsNodeDelete(InstanceIdentifier<Node> key, Node addedPSNode, TypedReadWriteTransaction<D> tx)
274 throws ReadFailedException, ExecutionException, InterruptedException {
278 void onGlobalNodeAdd(InstanceIdentifier<Node> key, Node added, TypedReadWriteTransaction<D> tx) {
282 void onPsNodeAdd(InstanceIdentifier<Node> key, Node addedPSNode, TypedReadWriteTransaction<D> tx)
283 throws InterruptedException, ExecutionException {
287 void onGlobalNodeUpdate(InstanceIdentifier<Node> key, Node updated, Node original,
288 DataObjectModification<Node> mod, TypedReadWriteTransaction<D> tx)
289 throws ReadFailedException, InterruptedException, ExecutionException {
293 void onPsNodeUpdate(Node updated, Node original,
294 DataObjectModification<Node> mod, TypedReadWriteTransaction<D> tx)
295 throws ReadFailedException, InterruptedException, ExecutionException {