Bug 8424: Don't output data tree and tree candidates wih debug
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DefaultShardDataTreeChangeListenerPublisher.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;
9
10 import com.google.common.base.Optional;
11 import java.util.Collection;
12 import java.util.function.Consumer;
13 import javax.annotation.concurrent.NotThreadSafe;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
15 import org.opendaylight.mdsal.dom.spi.AbstractDOMDataTreeChangeListenerRegistration;
16 import org.opendaylight.mdsal.dom.spi.store.AbstractDOMStoreTreeChangePublisher;
17 import org.opendaylight.yangtools.concepts.ListenerRegistration;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Default implementation of ShardDataTreeChangeListenerPublisher that directly generates and publishes
25  * notifications for DataTreeChangeListeners.
26  *
27  * @author Thomas Pantelis
28  */
29 @NotThreadSafe
30 final class DefaultShardDataTreeChangeListenerPublisher extends AbstractDOMStoreTreeChangePublisher
31         implements ShardDataTreeChangeListenerPublisher {
32     private static final Logger LOG = LoggerFactory.getLogger(DefaultShardDataTreeChangeListenerPublisher.class);
33     private String logContext;
34
35     DefaultShardDataTreeChangeListenerPublisher(String logContext) {
36         this.logContext = logContext;
37     }
38
39     @Override
40     public void publishChanges(final DataTreeCandidate candidate) {
41         if (LOG.isTraceEnabled()) {
42             LOG.trace("{}: publishChanges: {}", logContext, candidate);
43         } else {
44             LOG.debug("{}: publishChanges: rootPath: {}", logContext, candidate.getRootPath());
45         }
46
47         processCandidateTree(candidate);
48     }
49
50     @Override
51     protected void notifyListener(AbstractDOMDataTreeChangeListenerRegistration<?> registration,
52             Collection<DataTreeCandidate> changes) {
53         LOG.debug("{}: notifyListener: listener: {}", logContext, registration.getInstance());
54         registration.getInstance().onDataTreeChanged(changes);
55     }
56
57     @Override
58     protected void registrationRemoved(final AbstractDOMDataTreeChangeListenerRegistration<?> registration) {
59         LOG.debug("Registration {} removed", registration);
60     }
61
62     @Override
63     public void registerTreeChangeListener(YangInstanceIdentifier treeId, DOMDataTreeChangeListener listener,
64             Optional<DataTreeCandidate> initialState,
65             Consumer<ListenerRegistration<DOMDataTreeChangeListener>> onRegistration) {
66         LOG.debug("{}: registerTreeChangeListener: path: {}, listener: {}", logContext, treeId, listener);
67
68         AbstractDOMDataTreeChangeListenerRegistration<org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener>
69             registration = super.registerTreeChangeListener(treeId, new ForwardingDOMDataTreeChangeListener(listener));
70
71         onRegistration.accept(
72             new org.opendaylight.controller.md.sal.dom.spi.AbstractDOMDataTreeChangeListenerRegistration<
73                     DOMDataTreeChangeListener>(listener) {
74                 @Override
75                 protected void removeRegistration() {
76                     registration.close();
77                 }
78             });
79
80         if (initialState.isPresent()) {
81             notifySingleListener(treeId, listener, initialState.get(), logContext);
82         }
83     }
84
85     static void notifySingleListener(YangInstanceIdentifier treeId, DOMDataTreeChangeListener listener,
86             DataTreeCandidate state, String logContext) {
87         LOG.debug("{}: notifySingleListener: path: {}, listener: {}", logContext, treeId, listener);
88         DefaultShardDataTreeChangeListenerPublisher publisher =
89                 new DefaultShardDataTreeChangeListenerPublisher(logContext);
90         publisher.logContext = logContext;
91         publisher.registerTreeChangeListener(treeId, listener, Optional.absent(), noop -> { });
92         publisher.publishChanges(state);
93     }
94
95     private static class ForwardingDOMDataTreeChangeListener
96             implements org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener {
97         final DOMDataTreeChangeListener delegate;
98
99         ForwardingDOMDataTreeChangeListener(DOMDataTreeChangeListener delegate) {
100             this.delegate = delegate;
101         }
102
103         @Override
104         public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
105             delegate.onDataTreeChanged(changes);
106         }
107
108         @Override
109         public String toString() {
110             return delegate.toString();
111         }
112     }
113 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.