BUG-2138: Add shard DTO classes to spi
[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.mdsal.dom.spi.shard.ChildShardContext;
20 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
21 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager.BatchedInvoker;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 final class InMemoryDOMDataTreeShardChangePublisher extends AbstractDOMShardTreeChangePublisher {
29
30     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataTreeShardChangePublisher.class);
31
32     private static final BatchedInvoker<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
33         MANAGER_INVOKER = (listener, notifications) -> {
34             final DOMDataTreeChangeListener inst = listener.getInstance();
35             if (inst != null) {
36                 inst.onDataTreeChanged(ImmutableList.copyOf(notifications));
37             }
38         };
39
40     private final QueuedNotificationManager<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
41         notificationManager;
42
43     InMemoryDOMDataTreeShardChangePublisher(final Executor executor,
44                                             final int maxQueueSize,
45                                             final DataTree dataTree,
46                                             final YangInstanceIdentifier rootPath,
47                                             final Map<DOMDataTreeIdentifier, ChildShardContext> childShards) {
48         super(dataTree, rootPath, childShards);
49         notificationManager = QueuedNotificationManager.create(
50                 executor, MANAGER_INVOKER, maxQueueSize, "DataTreeChangeListenerQueueMgr");
51     }
52
53     @Override
54     protected void notifyListener(AbstractDOMDataTreeChangeListenerRegistration<?> registration,
55             Collection<DataTreeCandidate> changes) {
56         LOG.debug("Enqueueing candidates {} for registration {}", changes, registration);
57         notificationManager.submitNotifications(registration, changes);
58     }
59
60     @Override
61     protected void registrationRemoved(@Nonnull final AbstractDOMDataTreeChangeListenerRegistration<?> registration) {
62         LOG.debug("Closing registration {}", registration);
63
64     }
65
66     synchronized void publishChange(@Nonnull final DataTreeCandidate candidate) {
67         processCandidateTree(candidate);
68     }
69 }