Log data after in IdIntsDOMDataTreeLIstener
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / IdIntsDOMDataTreeLIstener.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 java.util.Map;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeListeningException;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class IdIntsDOMDataTreeLIstener implements DOMDataTreeListener {
24
25     private static final Logger LOG = LoggerFactory.getLogger(IdIntsDOMDataTreeLIstener.class);
26
27     private NormalizedNode<?, ?> localCopy = null;
28
29     @Override
30     public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes,
31                                   @Nonnull final Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> subtrees) {
32
33         // There should only be one candidate reported
34         Preconditions.checkState(changes.size() == 1);
35
36         // do not log the change into debug, only use trace since it will lead to OOM on default heap settings
37         LOG.debug("Received data tree changed");
38
39         changes.forEach(change -> {
40             if (change.getRootNode().getDataAfter().isPresent()) {
41                 LOG.trace("Received change, data before: {}, data after: {}",
42                         change.getRootNode().getDataBefore().isPresent()
43                                 ? change.getRootNode().getDataBefore().get() : "",
44                         change.getRootNode().getDataAfter().get());
45
46                 if (localCopy == null || checkEqual(change.getRootNode().getDataBefore().get())) {
47                     localCopy = change.getRootNode().getDataAfter().get();
48                 } else {
49                     LOG.warn("Ignoring notification.");
50                     LOG.trace("Ignored notification content: {}", change);
51                 }
52             } else {
53                 LOG.warn("getDataAfter() is missing from notification. change: {}", change);
54             }
55         });
56     }
57
58     @Override
59     public void onDataTreeFailed(@Nonnull Collection<DOMDataTreeListeningException> causes) {
60
61     }
62
63     public boolean hasTriggered() {
64         return localCopy != null;
65     }
66
67     public boolean checkEqual(final NormalizedNode<?, ?> expected) {
68         return localCopy.equals(expected);
69     }
70 }