df0223e58723ab8e53850b91ea3c2595c067f425
[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.Invoker;
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.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 final class InMemoryDOMDataTreeShardChangePublisher extends AbstractDOMShardTreeChangePublisher {
30
31     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataTreeShardChangePublisher.class);
32
33     private static final Invoker<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
34         MANAGER_INVOKER = (listener, notification) -> {
35             final DOMDataTreeChangeListener inst = listener.getInstance();
36             if (inst != null) {
37                 inst.onDataTreeChanged(ImmutableList.of(notification));
38             }
39         };
40
41     private final QueuedNotificationManager<AbstractDOMDataTreeChangeListenerRegistration<?>,
42         DataTreeCandidate> notificationManager;
43
44     InMemoryDOMDataTreeShardChangePublisher(final Executor executor,
45                                             final int maxQueueSize,
46                                             final DataTree dataTree,
47                                             final YangInstanceIdentifier rootPath,
48                                             final Map<DOMDataTreeIdentifier, ChildShardContext> childShards) {
49         super(dataTree, rootPath, childShards);
50         notificationManager = new QueuedNotificationManager<>(
51                 executor, MANAGER_INVOKER, maxQueueSize, "DataTreeChangeListenerQueueMgr");
52     }
53
54     @Override
55     protected void notifyListeners(
56             @Nonnull final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> registrations,
57                                    @Nonnull final YangInstanceIdentifier path,
58                                    @Nonnull final DataTreeCandidateNode node) {
59         final DataTreeCandidate candidate = DataTreeCandidates.newDataTreeCandidate(path, node);
60
61         for (final AbstractDOMDataTreeChangeListenerRegistration<?> reg : registrations) {
62             LOG.debug("Enqueueing candidate {} to registration {}", candidate, registrations);
63             notificationManager.submitNotification(reg, candidate);
64         }
65     }
66
67     @Override
68     protected void registrationRemoved(@Nonnull final AbstractDOMDataTreeChangeListenerRegistration<?> registration) {
69         LOG.debug("Closing registration {}", registration);
70
71     }
72
73     @Override
74     public <L extends DOMDataTreeChangeListener> AbstractDOMDataTreeChangeListenerRegistration<L>
75             registerTreeChangeListener(final YangInstanceIdentifier path, final L listener) {
76         return super.registerTreeChangeListener(path, listener);
77     }
78
79     synchronized void publishChange(@Nonnull final DataTreeCandidate candidate) {
80         processCandidateTree(candidate);
81     }
82 }