2 * Copyright (c) 2015 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.cluster.datastore.utils;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
15 import com.google.common.util.concurrent.Uninterruptibles;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Optional;
22 import java.util.concurrent.CountDownLatch;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.atomic.AtomicInteger;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
28 import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
32 public class MockDataTreeChangeListener implements DOMDataTreeChangeListener {
34 private final List<DataTreeCandidate> changeList = new ArrayList<>();
36 private final CountDownLatch onInitialDataLatch = new CountDownLatch(1);
37 private final AtomicInteger onInitialDataEventCount = new AtomicInteger();
39 private volatile CountDownLatch changeLatch;
40 private int expChangeEventCount;
42 public MockDataTreeChangeListener(final int expChangeEventCount) {
43 reset(expChangeEventCount);
46 public void reset(final int newExpChangeEventCount) {
47 changeLatch = new CountDownLatch(newExpChangeEventCount);
48 expChangeEventCount = newExpChangeEventCount;
49 synchronized (changeList) {
55 public void onDataTreeChanged(final List<DataTreeCandidate> changes) {
56 if (changeLatch.getCount() > 0) {
57 synchronized (changeList) {
58 changeList.addAll(changes);
60 changeLatch.countDown();
65 public void onInitialData() {
66 onInitialDataEventCount.incrementAndGet();
67 onInitialDataLatch.countDown();
70 public void verifyOnInitialDataEvent() {
71 assertTrue("onInitialData was not triggered",
72 Uninterruptibles.awaitUninterruptibly(onInitialDataLatch, 5, TimeUnit.SECONDS));
73 assertEquals("onInitialDataEventCount", 1, onInitialDataEventCount.get());
76 public void verifyNoOnInitialDataEvent() {
77 assertFalse("onInitialData was triggered unexpectedly",
78 Uninterruptibles.awaitUninterruptibly(onInitialDataLatch, 500, TimeUnit.MILLISECONDS));
81 @SuppressWarnings({ "unchecked", "rawtypes" })
82 public void waitForChangeEvents(final YangInstanceIdentifier... expPaths) {
83 boolean done = Uninterruptibles.awaitUninterruptibly(changeLatch, 5, TimeUnit.SECONDS);
85 fail(String.format("Missing change notifications. Expected: %d. Actual: %d",
86 expChangeEventCount, expChangeEventCount - changeLatch.getCount()));
89 for (int i = 0; i < expPaths.length; i++) {
90 final DataTreeCandidate candidate = changeList.get(i);
91 final NormalizedNode dataAfter = candidate.getRootNode().dataAfter();
92 if (dataAfter == null) {
93 fail(String.format("Change %d does not contain data after. Actual: %s", i + 1,
94 candidate.getRootNode()));
97 final Optional<YangInstanceIdentifier> relativePath = expPaths[i].relativeTo(candidate.getRootPath());
98 if (!relativePath.isPresent()) {
99 assertEquals(String.format("Change %d does not contain %s. Actual: %s", i + 1, expPaths[i],
100 dataAfter), expPaths[i].getLastPathArgument(), dataAfter.name());
102 NormalizedNode nextChild = dataAfter;
103 for (PathArgument pathArg: relativePath.orElseThrow().getPathArguments()) {
104 boolean found = false;
105 if (nextChild instanceof DistinctNodeContainer) {
106 Optional<NormalizedNode> maybeChild = ((DistinctNodeContainer)nextChild)
107 .findChildByArg(pathArg);
108 if (maybeChild.isPresent()) {
110 nextChild = maybeChild.orElseThrow();
115 fail(String.format("Change %d does not contain %s. Actual: %s", i + 1, expPaths[i], dataAfter));
122 public void verifyNotifiedData(final YangInstanceIdentifier... paths) {
123 Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
124 synchronized (changeList) {
125 for (DataTreeCandidate c : changeList) {
126 pathSet.remove(c.getRootPath());
130 if (!pathSet.isEmpty()) {
131 fail(pathSet + " not present in " + changeList);
135 public void expectNoMoreChanges(final String assertMsg) {
136 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
137 synchronized (changeList) {
138 assertEquals(assertMsg, expChangeEventCount, changeList.size());
142 public void verifyNoNotifiedData(final YangInstanceIdentifier... paths) {
143 Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
144 synchronized (changeList) {
145 for (DataTreeCandidate c : changeList) {
146 assertFalse("Unexpected " + c.getRootPath() + " present in DataTreeCandidate",
147 pathSet.contains(c.getRootPath()));