98083a0b1c9a7e5932e32d3aba29aa9c8334321a
[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.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;
26
27 /**
28  * Default implementation of ShardDataChangeListenerPublisher that directly generates and publishes
29  * notifications for DataChangeListeners.
30  *
31  * @author Thomas Pantelis
32  */
33 @NotThreadSafe
34 final class DefaultShardDataChangeListenerPublisher implements ShardDataChangeListenerPublisher,
35         NotificationManager<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> {
36     private static final Logger LOG = LoggerFactory.getLogger(DefaultShardDataChangeListenerPublisher.class);
37
38     private final ListenerTree dataChangeListenerTree = ListenerTree.create();
39
40     @Override
41     public void submitNotification(final DataChangeListenerRegistration<?> listener,
42             final DOMImmutableDataChangeEvent notification) {
43         LOG.debug("Notifying listener {} about {}", listener.getInstance(), notification);
44
45         listener.getInstance().onDataChanged(notification);
46     }
47
48     @Override
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);
53
54         for (DOMImmutableDataChangeEvent n : notifications) {
55             instance.onDataChanged(n);
56         }
57     }
58
59     @Override
60     public void publishChanges(DataTreeCandidate candidate, String logContext) {
61         ResolveDataChangeEventsTask.create(candidate, dataChangeListenerTree).resolve(this);
62     }
63
64     @Override
65     public void registerDataChangeListener(YangInstanceIdentifier path,
66             AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener, DataChangeScope scope,
67             Optional<DataTreeCandidate> initialState,
68             Consumer<ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>>
69                     onRegistration) {
70         final DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
71                 registration = dataChangeListenerTree.registerDataChangeListener(path, listener, scope);
72
73         onRegistration.accept(registration);
74
75         if (initialState.isPresent()) {
76             notifySingleListener(path, listener, scope, initialState.get());
77         }
78     }
79
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, "");
86     }
87 }