BUG 8525 Listeners not getting triggered from followers
[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.ClusteredDOMDataTreeChangeListener;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class IdIntsListener implements ClusteredDOMDataTreeChangeListener {
21
22     private static final Logger LOG = LoggerFactory.getLogger(IdIntsListener.class);
23
24     private NormalizedNode<?, ?> localCopy = null;
25
26     @Override
27     public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
28
29         // There should only be one candidate reported
30         Preconditions.checkState(changes.size() == 1);
31
32         // do not log the change into debug, only use trace since it will lead to OOM on default heap settings
33         LOG.debug("Received data tree changed");
34
35         changes.forEach(change -> {
36             if (change.getRootNode().getDataAfter().isPresent()) {
37                 LOG.trace("Received change, data before: {}, data after: ",
38                         change.getRootNode().getDataBefore().isPresent()
39                                 ? change.getRootNode().getDataBefore().get() : "",
40                         change.getRootNode().getDataAfter().get());
41
42                 if (localCopy == null || checkEqual(change.getRootNode().getDataBefore().get())) {
43                     localCopy = change.getRootNode().getDataAfter().get();
44                 } else {
45                     LOG.debug("Ignoring notification: {}", change);
46                 }
47             } else {
48                 LOG.warn("getDataAfter() is missing from notification. change: {}", change);
49             }
50         });
51     }
52
53     public boolean hasTriggered() {
54         return localCopy != null;
55     }
56
57     public boolean checkEqual(final NormalizedNode<?, ?> expected) {
58         return localCopy.equals(expected);
59     }
60 }