BUG-5280: switch transactionIdentifier
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DefaultShardDataChangeListenerPublisher.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.Stopwatch;
11 import java.util.concurrent.TimeUnit;
12 import javax.annotation.concurrent.NotThreadSafe;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
15 import org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent;
16 import org.opendaylight.controller.md.sal.dom.store.impl.DataChangeListenerRegistration;
17 import org.opendaylight.controller.md.sal.dom.store.impl.ResolveDataChangeEventsTask;
18 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerTree;
19 import org.opendaylight.yangtools.util.concurrent.NotificationManager;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Default implementation of ShardDataChangeListenerPublisher that directly generates and publishes
28  * notifications for DataChangeListeners.
29  *
30  * @author Thomas Pantelis
31  */
32 @NotThreadSafe
33 final class DefaultShardDataChangeListenerPublisher implements ShardDataChangeListenerPublisher,
34         NotificationManager<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> {
35     private static final Logger LOG = LoggerFactory.getLogger(DefaultShardDataChangeListenerPublisher.class);
36
37     private final ListenerTree dataChangeListenerTree = ListenerTree.create();
38     private final Stopwatch timer = Stopwatch.createUnstarted();
39
40     @Override
41     public void submitNotification(final DataChangeListenerRegistration<?> listener, final DOMImmutableDataChangeEvent notification) {
42         LOG.debug("Notifying listener {} about {}", listener.getInstance(), notification);
43
44         listener.getInstance().onDataChanged(notification);
45     }
46
47     @Override
48     public void submitNotifications(final DataChangeListenerRegistration<?> listener, final Iterable<DOMImmutableDataChangeEvent> notifications) {
49         final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> instance = listener.getInstance();
50         LOG.debug("Notifying listener {} about {}", instance, notifications);
51
52         for (DOMImmutableDataChangeEvent n : notifications) {
53             instance.onDataChanged(n);
54         }
55     }
56
57     @Override
58     public void publishChanges(DataTreeCandidate candidate, String logContext) {
59         timer.start();
60
61         try {
62             ResolveDataChangeEventsTask.create(candidate, dataChangeListenerTree).resolve(this);
63         } finally {
64             timer.stop();
65             long elapsedTime = timer.elapsed(TimeUnit.MILLISECONDS);
66             if(elapsedTime >= PUBLISH_DELAY_THRESHOLD_IN_MS) {
67                 LOG.warn("{}: Generation of DataChange events took longer than expected. Elapsed time: {}",
68                         logContext, timer);
69             } else {
70                 LOG.debug("{}: Elapsed time for generation of DataChange events: {}", logContext, timer);
71             }
72
73             timer.reset();
74         }
75     }
76
77     @Override
78     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> DataChangeListenerRegistration<L>
79             registerDataChangeListener(YangInstanceIdentifier path, L listener, DataChangeScope scope) {
80         return dataChangeListenerTree.registerDataChangeListener(path, listener, scope);
81     }
82
83     @Override
84     public ShardDataChangeListenerPublisher newInstance() {
85         return new DefaultShardDataChangeListenerPublisher();
86     }
87 }