Bug 7803: Implement agent RPCs for data tree change listener testing
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / IdIntsListener.java
1 /*
2  * Copyright (c) 2017 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
9 package org.opendaylight.controller.clustering.it.provider.impl;
10
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
19 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class IdIntsListener implements DOMDataTreeChangeListener {
24
25     private static final Logger LOG = LoggerFactory.getLogger(IdIntsListener.class);
26
27     private NormalizedNode<?, ?> localCopy = null;
28
29     @Override
30     public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
31
32         // There should only be one candidate reported
33         Preconditions.checkState(changes.size() == 1);
34
35         // do not log the change into debug, only use trace since it will lead to OOM on default heap settings
36         LOG.debug("Received data tree changed");
37
38         changes.forEach(change -> {
39             if (change.getRootNode().getDataAfter().isPresent()) {
40                 LOG.trace("Received change, data before: {}, data after: ", change.getRootNode().getDataBefore().get(),
41                         change.getRootNode().getDataAfter().get());
42
43                 if (localCopy == null || checkEqual(change.getRootNode().getDataBefore().get())) {
44                     localCopy = change.getRootNode().getDataAfter().get();
45                 } else {
46                     LOG.debug("Ignoring notification: {}", change);
47                 }
48             } else {
49                 LOG.warn("getDataAfter() is missing from notification. change: {}", change);
50             }
51         });
52     }
53
54     public boolean checkEqual(final NormalizedNode<?, ?> expected) {
55         return localCopy.equals(expected);
56     }
57 }