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.Optional;
11 import java.util.function.Consumer;
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.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.util.concurrent.NotificationManager;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Default implementation of ShardDataChangeListenerPublisher that directly generates and publishes
29 * notifications for DataChangeListeners.
31 * @author Thomas Pantelis
34 final class DefaultShardDataChangeListenerPublisher implements ShardDataChangeListenerPublisher,
35 NotificationManager<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> {
36 private static final Logger LOG = LoggerFactory.getLogger(DefaultShardDataChangeListenerPublisher.class);
38 private final ListenerTree dataChangeListenerTree = ListenerTree.create();
41 public void submitNotification(final DataChangeListenerRegistration<?> listener,
42 final DOMImmutableDataChangeEvent notification) {
43 LOG.debug("Notifying listener {} about {}", listener.getInstance(), notification);
45 listener.getInstance().onDataChanged(notification);
49 public void submitNotifications(final DataChangeListenerRegistration<?> listener,
50 final Iterable<DOMImmutableDataChangeEvent> notifications) {
51 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> instance = listener.getInstance();
52 LOG.debug("Notifying listener {} about {}", instance, notifications);
54 for (DOMImmutableDataChangeEvent n : notifications) {
55 instance.onDataChanged(n);
60 public void publishChanges(DataTreeCandidate candidate, String logContext) {
61 ResolveDataChangeEventsTask.create(candidate, dataChangeListenerTree).resolve(this);
65 public void registerDataChangeListener(YangInstanceIdentifier path,
66 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener, DataChangeScope scope,
67 Optional<DataTreeCandidate> initialState,
68 Consumer<ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>>
70 final DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
71 registration = dataChangeListenerTree.registerDataChangeListener(path, listener, scope);
73 onRegistration.accept(registration);
75 if (initialState.isPresent()) {
76 notifySingleListener(path, listener, scope, initialState.get());
80 static void notifySingleListener(final YangInstanceIdentifier path,
81 final AsyncDataChangeListener<YangInstanceIdentifier,NormalizedNode<?, ?>> listener,
82 final DataChangeScope scope, final DataTreeCandidate initialState) {
83 DefaultShardDataChangeListenerPublisher publisher = new DefaultShardDataChangeListenerPublisher();
84 publisher.registerDataChangeListener(path, listener, scope, Optional.absent(), noop -> { });
85 publisher.publishChanges(initialState, "");