2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
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;
27 * Default implementation of ShardDataChangeListenerPublisher that directly generates and publishes
28 * notifications for DataChangeListeners.
30 * @author Thomas Pantelis
33 final class DefaultShardDataChangeListenerPublisher implements ShardDataChangeListenerPublisher,
34 NotificationManager<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> {
35 private static final Logger LOG = LoggerFactory.getLogger(DefaultShardDataChangeListenerPublisher.class);
37 private final ListenerTree dataChangeListenerTree = ListenerTree.create();
38 private final Stopwatch timer = Stopwatch.createUnstarted();
41 public void submitNotification(final DataChangeListenerRegistration<?> listener, final DOMImmutableDataChangeEvent notification) {
42 LOG.debug("Notifying listener {} about {}", listener.getInstance(), notification);
44 listener.getInstance().onDataChanged(notification);
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);
52 for (DOMImmutableDataChangeEvent n : notifications) {
53 instance.onDataChanged(n);
58 public void publishChanges(DataTreeCandidate candidate, String logContext) {
62 ResolveDataChangeEventsTask.create(candidate, dataChangeListenerTree).resolve(this);
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: {}",
70 LOG.debug("{}: Elapsed time for generation of DataChange events: {}", logContext, timer);
78 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> DataChangeListenerRegistration<L>
79 registerDataChangeListener(YangInstanceIdentifier path, L listener, DataChangeScope scope) {
80 return dataChangeListenerTree.registerDataChangeListener(path, listener, scope);
84 public ShardDataChangeListenerPublisher newInstance() {
85 return new DefaultShardDataChangeListenerPublisher();