Migrate from YangInstanceIdentifier.EMPTY
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / 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.controller.md.sal.dom.store.impl;
9
10 import java.util.Collection;
11 import java.util.Optional;
12 import java.util.concurrent.ExecutorService;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
15 import org.opendaylight.controller.md.sal.dom.spi.AbstractDOMDataTreeChangeListenerRegistration;
16 import org.opendaylight.controller.sal.core.spi.data.AbstractDOMStoreTreeChangePublisher;
17 import org.opendaylight.yangtools.concepts.ListenerRegistration;
18 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 final class InMemoryDOMStoreTreeChangePublisher extends AbstractDOMStoreTreeChangePublisher {
29     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMStoreTreeChangePublisher.class);
30
31     private final QueuedNotificationManager<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
32             notificationManager;
33
34     InMemoryDOMStoreTreeChangePublisher(final ExecutorService listenerExecutor, final int maxQueueSize) {
35         notificationManager = QueuedNotificationManager.create(listenerExecutor, (listener, notifications) -> {
36             // FIXME: we are not checking for listener being closed
37             listener.getInstance().onDataTreeChanged(notifications);
38         }, maxQueueSize,
39                 "DataTreeChangeListenerQueueMgr");
40     }
41
42     private InMemoryDOMStoreTreeChangePublisher(final QueuedNotificationManager<
43             AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate> notificationManager) {
44         this.notificationManager = notificationManager;
45     }
46
47     QueuedNotificationManager<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
48             getNotificationManager() {
49         return notificationManager;
50     }
51
52     @Override
53     protected void notifyListeners(final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> registrations,
54             final YangInstanceIdentifier path, final DataTreeCandidateNode node) {
55         final DataTreeCandidate candidate = DataTreeCandidates.newDataTreeCandidate(path, node);
56
57         for (AbstractDOMDataTreeChangeListenerRegistration<?> reg : registrations) {
58             LOG.debug("Enqueueing candidate {} to registration {}", candidate, registrations);
59             notificationManager.submitNotification(reg, candidate);
60         }
61     }
62
63     @Override
64     protected synchronized void registrationRemoved(
65             final AbstractDOMDataTreeChangeListenerRegistration<?> registration) {
66         LOG.debug("Closing registration {}", registration);
67
68         // FIXME: remove the queue for this registration and make sure we clear it
69     }
70
71     <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
72             final YangInstanceIdentifier treeId, final L listener, final DataTreeSnapshot snapshot) {
73         final AbstractDOMDataTreeChangeListenerRegistration<L> reg = registerTreeChangeListener(treeId, listener);
74
75         final Optional<NormalizedNode<?, ?>> node = snapshot.readNode(YangInstanceIdentifier.empty());
76         if (node.isPresent()) {
77             final DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(
78                     YangInstanceIdentifier.empty(), node.get());
79
80             InMemoryDOMStoreTreeChangePublisher publisher =
81                     new InMemoryDOMStoreTreeChangePublisher(notificationManager);
82             publisher.registerTreeChangeListener(treeId, listener);
83             publisher.publishChange(candidate);
84         }
85
86         return reg;
87     }
88
89     synchronized void publishChange(final @NonNull DataTreeCandidate candidate) {
90         // Runs synchronized with registrationRemoved()
91         processCandidateTree(candidate);
92     }
93 }