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