Bug 7271: Modify AbstractDOMStoreTreeChangePublisher to batch candidates
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / InMemoryDOMDataTreeShardChangePublisher.java
1 /*
2  * Copyright (c) 2016 Cisco 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
9 package org.opendaylight.mdsal.dom.store.inmemory;
10
11 import com.google.common.collect.ImmutableList;
12 import java.util.Collection;
13 import java.util.Map;
14 import java.util.concurrent.Executor;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
18 import org.opendaylight.mdsal.dom.spi.AbstractDOMDataTreeChangeListenerRegistration;
19 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
20 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager.BatchedInvoker;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 final class InMemoryDOMDataTreeShardChangePublisher extends AbstractDOMShardTreeChangePublisher {
28
29     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataTreeShardChangePublisher.class);
30
31     private static final BatchedInvoker<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
32         MANAGER_INVOKER = (listener, notifications) -> {
33             final DOMDataTreeChangeListener inst = listener.getInstance();
34             if (inst != null) {
35                 inst.onDataTreeChanged(ImmutableList.copyOf(notifications));
36             }
37         };
38
39     private final QueuedNotificationManager<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
40         notificationManager;
41
42     InMemoryDOMDataTreeShardChangePublisher(final Executor executor,
43                                             final int maxQueueSize,
44                                             final DataTree dataTree,
45                                             final YangInstanceIdentifier rootPath,
46                                             final Map<DOMDataTreeIdentifier, ChildShardContext> childShards) {
47         super(dataTree, rootPath, childShards);
48         notificationManager = QueuedNotificationManager.create(
49                 executor, MANAGER_INVOKER, maxQueueSize, "DataTreeChangeListenerQueueMgr");
50     }
51
52     @Override
53     protected void notifyListener(AbstractDOMDataTreeChangeListenerRegistration<?> registration,
54             Collection<DataTreeCandidate> changes) {
55         LOG.debug("Enqueueing candidates {} for registration {}", changes, registration);
56         notificationManager.submitNotifications(registration, changes);
57     }
58
59     @Override
60     protected void registrationRemoved(@Nonnull final AbstractDOMDataTreeChangeListenerRegistration<?> registration) {
61         LOG.debug("Closing registration {}", registration);
62
63     }
64
65     synchronized void publishChange(@Nonnull final DataTreeCandidate candidate) {
66         processCandidateTree(candidate);
67     }
68 }