fc3f9adfddf71b2f4759d21875012b04b17fd92a
[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: ",
41                         change.getRootNode().getDataBefore().isPresent()
42                                 ? change.getRootNode().getDataBefore().get() : "",
43                         change.getRootNode().getDataAfter().get());
44
45                 if (localCopy == null || checkEqual(change.getRootNode().getDataBefore().get())) {
46                     localCopy = change.getRootNode().getDataAfter().get();
47                 } else {
48                     LOG.debug("Ignoring notification: {}", change);
49                 }
50             } else {
51                 LOG.warn("getDataAfter() is missing from notification. change: {}", change);
52             }
53         });
54     }
55
56     public boolean checkEqual(final NormalizedNode<?, ?> expected) {
57         return localCopy.equals(expected);
58     }
59 }