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();
39 private final String logContext;
41 DefaultShardDataChangeListenerPublisher(String logContext) {
42 this.logContext = logContext;
46 public void submitNotification(final DataChangeListenerRegistration<?> listener,
47 final DOMImmutableDataChangeEvent notification) {
48 LOG.debug("{}: Notifying listener {} about {}", logContext, listener.getInstance(), notification);
50 listener.getInstance().onDataChanged(notification);
54 public void submitNotifications(final DataChangeListenerRegistration<?> listener,
55 final Iterable<DOMImmutableDataChangeEvent> notifications) {
56 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> instance = listener.getInstance();
57 LOG.debug("{}: Notifying listener {} about {}", logContext, instance, notifications);
59 for (DOMImmutableDataChangeEvent n : notifications) {
60 instance.onDataChanged(n);
65 public void publishChanges(DataTreeCandidate candidate) {
66 ResolveDataChangeEventsTask.create(candidate, dataChangeListenerTree).resolve(this);
70 public void registerDataChangeListener(YangInstanceIdentifier path,
71 AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener, DataChangeScope scope,
72 Optional<DataTreeCandidate> initialState,
73 Consumer<ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>>
75 final DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
76 registration = dataChangeListenerTree.registerDataChangeListener(path, listener, scope);
78 onRegistration.accept(registration);
80 if (initialState.isPresent()) {
81 notifySingleListener(path, listener, scope, initialState.get(), logContext);
85 static void notifySingleListener(final YangInstanceIdentifier path,
86 final AsyncDataChangeListener<YangInstanceIdentifier,NormalizedNode<?, ?>> listener,
87 final DataChangeScope scope, final DataTreeCandidate initialState, String logContext) {
88 DefaultShardDataChangeListenerPublisher publisher = new DefaultShardDataChangeListenerPublisher(logContext);
89 publisher.registerDataChangeListener(path, listener, scope, Optional.absent(), noop -> { });
90 publisher.publishChanges(initialState);