Bug 4094: Fix DCNs on initial registration
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / MockDataTreeChangeListener.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.utils;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.fail;
13 import com.google.common.collect.Lists;
14 import com.google.common.util.concurrent.Uninterruptibles;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.concurrent.CountDownLatch;
22 import java.util.concurrent.TimeUnit;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
27
28 public class MockDataTreeChangeListener implements DOMDataTreeChangeListener {
29
30     private final List<Collection<DataTreeCandidate>> changeList =
31             Collections.synchronizedList(Lists.<Collection<DataTreeCandidate>>newArrayList());
32
33     private volatile CountDownLatch changeLatch;
34     private int expChangeEventCount;
35
36     public MockDataTreeChangeListener(int expChangeEventCount) {
37         reset(expChangeEventCount);
38     }
39
40     public void reset(int expChangeEventCount) {
41         changeLatch = new CountDownLatch(expChangeEventCount);
42         this.expChangeEventCount = expChangeEventCount;
43         changeList.clear();
44     }
45
46     @Override
47     public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
48         changeList.add(changes);
49         changeLatch.countDown();
50     }
51
52     public void waitForChangeEvents() {
53         boolean done = Uninterruptibles.awaitUninterruptibly(changeLatch, 5, TimeUnit.SECONDS);
54         if(!done) {
55             fail(String.format("Missing change notifications. Expected: %d. Actual: %d",
56                     expChangeEventCount, (expChangeEventCount - changeLatch.getCount())));
57         }
58     }
59
60     public void verifyNotifiedData(YangInstanceIdentifier... paths) {
61         Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
62         for(Collection<DataTreeCandidate> list: changeList) {
63             for(DataTreeCandidate c: list) {
64                 pathSet.remove(c.getRootPath());
65             }
66         }
67
68         if(!pathSet.isEmpty()) {
69             fail(pathSet + " not present in " + changeList);
70         }
71     }
72
73     public void expectNoMoreChanges(String assertMsg) {
74         Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
75         assertEquals(assertMsg, expChangeEventCount, changeList.size());
76     }
77
78     public void verifyNoNotifiedData(YangInstanceIdentifier... paths) {
79         Set<YangInstanceIdentifier> pathSet = new HashSet<>(Arrays.asList(paths));
80         for(Collection<DataTreeCandidate> list: changeList) {
81             for(DataTreeCandidate c: list) {
82                 assertFalse("Unexpected " + c.getRootPath() + " present in DataTreeCandidate",
83                         pathSet.contains(c.getRootPath()));
84             }
85         }
86     }
87 }