2 * Copyright (c) 2017 Cisco Systems, Inc. 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.controller.clustering.it.provider.impl;
10 import static org.opendaylight.controller.clustering.it.provider.impl.AbstractTransactionHandler.ITEM;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.SettableFuture;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.util.Collection;
16 import java.util.HashMap;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.Future;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.atomic.AtomicLong;
24 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class IdIntsListener implements ClusteredDOMDataTreeChangeListener {
36 private static final Logger LOG = LoggerFactory.getLogger(IdIntsListener.class);
37 private static final long SECOND_AS_NANO = 1000000000;
39 private volatile NormalizedNode<?, ?> localCopy;
40 private final AtomicLong lastNotifTimestamp = new AtomicLong(0);
41 private ScheduledExecutorService executorService;
42 private ScheduledFuture<?> scheduledFuture;
45 public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
47 // There should only be one candidate reported
48 Preconditions.checkState(changes.size() == 1);
50 lastNotifTimestamp.set(System.nanoTime());
52 // do not log the change into debug, only use trace since it will lead to OOM on default heap settings
53 LOG.debug("Received data tree changed");
55 changes.forEach(change -> {
56 if (change.getRootNode().getDataAfter().isPresent()) {
57 LOG.trace("Received change, data before: {}, data after: {}",
58 change.getRootNode().getDataBefore().isPresent()
59 ? change.getRootNode().getDataBefore().get() : "",
60 change.getRootNode().getDataAfter().get());
62 localCopy = change.getRootNode().getDataAfter().get();
64 LOG.warn("getDataAfter() is missing from notification. change: {}", change);
69 public boolean hasTriggered() {
70 return localCopy != null;
73 public boolean checkEqual(final NormalizedNode<?, ?> expected) {
74 return localCopy.equals(expected);
77 @SuppressFBWarnings("BC_UNCONFIRMED_CAST")
78 public String diffWithLocalCopy(final NormalizedNode<?, ?> expected) {
79 return diffNodes((MapNode)expected, (MapNode)localCopy);
82 public Future<Void> tryFinishProcessing() {
83 executorService = Executors.newSingleThreadScheduledExecutor();
84 final SettableFuture<Void> settableFuture = SettableFuture.create();
86 scheduledFuture = executorService.scheduleAtFixedRate(new CheckFinishedTask(settableFuture),
87 0, 1, TimeUnit.SECONDS);
88 return settableFuture;
91 public static String diffNodes(final MapNode expected, final MapNode actual) {
92 StringBuilder builder = new StringBuilder("MapNodes diff:");
94 final YangInstanceIdentifier.NodeIdentifier itemNodeId = new YangInstanceIdentifier.NodeIdentifier(ITEM);
96 Map<NodeIdentifierWithPredicates, MapEntryNode> expIdIntMap = new HashMap<>();
97 expected.getValue().forEach(node -> expIdIntMap.put(node.getIdentifier(), node));
99 actual.getValue().forEach(actIdInt -> {
100 final MapEntryNode expIdInt = expIdIntMap.remove(actIdInt.getIdentifier());
101 if (expIdInt == null) {
102 builder.append('\n').append(" Unexpected id-int entry for ").append(actIdInt.getIdentifier());
106 Map<NodeIdentifierWithPredicates, MapEntryNode> expItemMap = new HashMap<>();
107 ((MapNode)expIdInt.getChild(itemNodeId).get()).getValue()
108 .forEach(node -> expItemMap.put(node.getIdentifier(), node));
110 ((MapNode)actIdInt.getChild(itemNodeId).get()).getValue().forEach(actItem -> {
111 final MapEntryNode expItem = expItemMap.remove(actItem.getIdentifier());
112 if (expItem == null) {
113 builder.append('\n').append(" Unexpected item entry ").append(actItem.getIdentifier())
114 .append(" for id-int entry ").append(actIdInt.getIdentifier());
118 expItemMap.values().forEach(node -> builder.append('\n')
119 .append(" Actual is missing item entry ").append(node.getIdentifier())
120 .append(" for id-int entry ").append(actIdInt.getIdentifier()));
123 expIdIntMap.values().forEach(node -> builder.append('\n')
124 .append(" Actual is missing id-int entry for ").append(node.getIdentifier()));
126 return builder.toString();
129 private class CheckFinishedTask implements Runnable {
131 private final SettableFuture<Void> future;
133 CheckFinishedTask(final SettableFuture<Void> future) {
134 this.future = future;
139 if (System.nanoTime() - lastNotifTimestamp.get() > SECOND_AS_NANO * 4) {
140 scheduledFuture.cancel(false);
143 executorService.shutdown();