BUG-5280: synchronize access to local histories
[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 javax.annotation.concurrent.NotThreadSafe;
11 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
13 import org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent;
14 import org.opendaylight.controller.md.sal.dom.store.impl.DataChangeListenerRegistration;
15 import org.opendaylight.controller.md.sal.dom.store.impl.ResolveDataChangeEventsTask;
16 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerTree;
17 import org.opendaylight.yangtools.util.concurrent.NotificationManager;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Default implementation of ShardDataChangeListenerPublisher that directly generates and publishes
26  * notifications for DataChangeListeners.
27  *
28  * @author Thomas Pantelis
29  */
30 @NotThreadSafe
31 final class DefaultShardDataChangeListenerPublisher implements ShardDataChangeListenerPublisher,
32         NotificationManager<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> {
33     private static final Logger LOG = LoggerFactory.getLogger(DefaultShardDataChangeListenerPublisher.class);
34
35     private final ListenerTree dataChangeListenerTree = ListenerTree.create();
36
37     @Override
38     public void submitNotification(final DataChangeListenerRegistration<?> listener,
39             final DOMImmutableDataChangeEvent notification) {
40         LOG.debug("Notifying listener {} about {}", listener.getInstance(), notification);
41
42         listener.getInstance().onDataChanged(notification);
43     }
44
45     @Override
46     public void submitNotifications(final DataChangeListenerRegistration<?> listener,
47             final Iterable<DOMImmutableDataChangeEvent> notifications) {
48         final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> instance = listener.getInstance();
49         LOG.debug("Notifying listener {} about {}", instance, notifications);
50
51         for (DOMImmutableDataChangeEvent n : notifications) {
52             instance.onDataChanged(n);
53         }
54     }
55
56     @Override
57     public void publishChanges(DataTreeCandidate candidate, String logContext) {
58         ResolveDataChangeEventsTask.create(candidate, dataChangeListenerTree).resolve(this);
59     }
60
61     @Override
62     public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
63             DataChangeListenerRegistration<L> registerDataChangeListener(YangInstanceIdentifier path, L listener,
64                     DataChangeScope scope) {
65         return dataChangeListenerTree.registerDataChangeListener(path, listener, scope);
66     }
67
68     @Override
69     public ShardDataChangeListenerPublisher newInstance() {
70         return new DefaultShardDataChangeListenerPublisher();
71     }
72 }