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.Collection;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Optional;
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.atomic.AtomicInteger;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
34 public class MockDataTreeChangeListener implements DOMDataTreeChangeListener {
36 private final List<DataTreeCandidate> changeList = new ArrayList<>();
38 private final CountDownLatch onInitialDataLatch = new CountDownLatch(1);
39 private final AtomicInteger onInitialDataEventCount = new AtomicInteger();
41 private volatile CountDownLatch changeLatch;
42 private int expChangeEventCount;
44 public MockDataTreeChangeListener(final int expChangeEventCount) {
45 reset(expChangeEventCount);
48 public void reset(final int newExpChangeEventCount) {
49 changeLatch = new CountDownLatch(newExpChangeEventCount);
50 this.expChangeEventCount = newExpChangeEventCount;
51 synchronized (changeList) {
57 public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
58 if (changeLatch.getCount() > 0) {
59 synchronized (changeList) {
60 changeList.addAll(changes);
62 changeLatch.countDown();
67 public void onInitialData() {
68 onInitialDataEventCount.incrementAndGet();
69 onInitialDataLatch.countDown();
72 public void verifyOnInitialDataEvent() {
73 assertTrue("onInitialData was not triggered",
74 Uninterruptibles.awaitUninterruptibly(onInitialDataLatch, 5, TimeUnit.SECONDS));
75 assertEquals("onInitialDataEventCount", 1, onInitialDataEventCount.get());
78 public void verifyNoOnInitialDataEvent() {
79 assertFalse("onInitialData was triggered unexpectedly",
80 Uninterruptibles.awaitUninterruptibly(onInitialDataLatch, 500, TimeUnit.MILLISECONDS));
83 @SuppressWarnings({ "unchecked", "rawtypes" })
84 public void waitForChangeEvents(final YangInstanceIdentifier... expPaths) {
85 boolean done = Uninterruptibles.awaitUninterruptibly(changeLatch, 5, TimeUnit.SECONDS);
87 fail(String.format("Missing change notifications. Expected: %d. Actual: %d",
88 expChangeEventCount, expChangeEventCount - changeLatch.getCount()));
91 for (int i = 0; i < expPaths.length; i++) {
92 final DataTreeCandidate candidate = changeList.get(i);
93 final Optional<NormalizedNode<?, ?>> maybeDataAfter = candidate.getRootNode().getDataAfter();
94 if (!maybeDataAfter.isPresent()) {
95 fail(String.format("Change %d does not contain data after. Actual: %s", i + 1,
96 candidate.getRootNode()));
99 final NormalizedNode<?, ?> dataAfter = maybeDataAfter.get();
100 final Optional<YangInstanceIdentifier> relativePath = expPaths[i].relativeTo(candidate.getRootPath());
101 if (!relativePath.isPresent()) {
102 assertEquals(String.format("Change %d does not contain %s. Actual: %s", i + 1, expPaths[i],
103 dataAfter), expPaths[i].getLastPathArgument(), dataAfter.getIdentifier());
105 NormalizedNode<?, ?> nextChild = dataAfter;
106 for (PathArgument pathArg: relativePath.get().getPathArguments()) {
107 boolean found = false;
108 if (nextChild instanceof NormalizedNodeContainer) {
109 Optional<NormalizedNode<?, ?>> maybeChild = ((NormalizedNodeContainer)nextChild)
111 if (maybeChild.isPresent()) {
113 nextChild = maybeChild.get();
118 fail(String.format("Change %d does not contain %s. Actual: %s", i + 1, expPaths[i], dataAfter));
125 public void verifyNotifiedData(final YangInstanceIdentifier... paths) {
126 Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
127 synchronized (changeList) {
128 for (DataTreeCandidate c : changeList) {
129 pathSet.remove(c.getRootPath());
133 if (!pathSet.isEmpty()) {
134 fail(pathSet + " not present in " + changeList);
138 public void expectNoMoreChanges(final String assertMsg) {
139 Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
140 synchronized (changeList) {
141 assertEquals(assertMsg, expChangeEventCount, changeList.size());
145 public void verifyNoNotifiedData(final YangInstanceIdentifier... paths) {
146 Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
147 synchronized (changeList) {
148 for (DataTreeCandidate c : changeList) {
149 assertFalse("Unexpected " + c.getRootPath() + " present in DataTreeCandidate",
150 pathSet.contains(c.getRootPath()));