Adjust to yangtools-2.0.0 changes
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / InMemoryDOMStoreTreeChangePublisher.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.mdsal.dom.store.inmemory;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.Collection;
12 import java.util.Optional;
13 import java.util.concurrent.ExecutorService;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
16 import org.opendaylight.mdsal.dom.spi.AbstractDOMDataTreeChangeListenerRegistration;
17 import org.opendaylight.mdsal.dom.spi.store.AbstractDOMStoreTreeChangePublisher;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
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.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 final class InMemoryDOMStoreTreeChangePublisher extends AbstractDOMStoreTreeChangePublisher {
30     private static final BatchedInvoker<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
31         MANAGER_INVOKER = (listener, notifications) -> {
32             final DOMDataTreeChangeListener inst = listener.getInstance();
33             if (inst != null) {
34                 inst.onDataTreeChanged(ImmutableList.copyOf(notifications));
35             }
36         };
37     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMStoreTreeChangePublisher.class);
38
39     private final QueuedNotificationManager<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
40         notificationManager;
41
42     InMemoryDOMStoreTreeChangePublisher(final ExecutorService listenerExecutor, final int maxQueueSize) {
43         notificationManager = QueuedNotificationManager.create(listenerExecutor, MANAGER_INVOKER, maxQueueSize,
44                 "DataTreeChangeListenerQueueMgr");
45     }
46
47     private InMemoryDOMStoreTreeChangePublisher(QueuedNotificationManager<
48             AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate> notificationManager) {
49         this.notificationManager = notificationManager;
50     }
51
52     @Override
53     protected void notifyListener(final AbstractDOMDataTreeChangeListenerRegistration<?> registration,
54             final Collection<DataTreeCandidate> changes) {
55         LOG.debug("Enqueueing candidates {} for registration {}", changes, registration);
56         notificationManager.submitNotifications(registration, changes);
57     }
58
59     @Override
60     protected synchronized void registrationRemoved(
61             final AbstractDOMDataTreeChangeListenerRegistration<?> registration) {
62         LOG.debug("Closing registration {}", registration);
63
64         // FIXME: remove the queue for this registration and make sure we clear it
65     }
66
67     <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
68             final YangInstanceIdentifier treeId, final L listener, final DataTreeSnapshot snapshot) {
69         final AbstractDOMDataTreeChangeListenerRegistration<L> reg = registerTreeChangeListener(treeId, listener);
70
71         final Optional<NormalizedNode<?, ?>> node = snapshot.readNode(YangInstanceIdentifier.EMPTY);
72         if (node.isPresent()) {
73             final DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(
74                     YangInstanceIdentifier.EMPTY, node.get());
75
76             InMemoryDOMStoreTreeChangePublisher publisher =
77                     new InMemoryDOMStoreTreeChangePublisher(notificationManager);
78             publisher.registerTreeChangeListener(treeId, listener);
79             publisher.publishChange(candidate);
80         }
81
82         return reg;
83     }
84
85     synchronized void publishChange(@Nonnull final DataTreeCandidate candidate) {
86         // Runs synchronized with registrationRemoved()
87         processCandidateTree(candidate);
88     }
89 }