2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.store.impl;
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;
28 final class InMemoryDOMStoreTreeChangePublisher extends AbstractDOMStoreTreeChangePublisher {
29 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMStoreTreeChangePublisher.class);
31 private final QueuedNotificationManager<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
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);
39 "DataTreeChangeListenerQueueMgr");
42 private InMemoryDOMStoreTreeChangePublisher(final QueuedNotificationManager<
43 AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate> notificationManager) {
44 this.notificationManager = notificationManager;
47 QueuedNotificationManager<AbstractDOMDataTreeChangeListenerRegistration<?>, DataTreeCandidate>
48 getNotificationManager() {
49 return notificationManager;
53 protected void notifyListeners(final Collection<AbstractDOMDataTreeChangeListenerRegistration<?>> registrations,
54 final YangInstanceIdentifier path, final DataTreeCandidateNode node) {
55 final DataTreeCandidate candidate = DataTreeCandidates.newDataTreeCandidate(path, node);
57 for (AbstractDOMDataTreeChangeListenerRegistration<?> reg : registrations) {
58 LOG.debug("Enqueueing candidate {} to registration {}", candidate, registrations);
59 notificationManager.submitNotification(reg, candidate);
64 protected synchronized void registrationRemoved(
65 final AbstractDOMDataTreeChangeListenerRegistration<?> registration) {
66 LOG.debug("Closing registration {}", registration);
68 // FIXME: remove the queue for this registration and make sure we clear it
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);
75 final Optional<NormalizedNode<?, ?>> node = snapshot.readNode(YangInstanceIdentifier.EMPTY);
76 if (node.isPresent()) {
77 final DataTreeCandidate candidate = DataTreeCandidates.fromNormalizedNode(
78 YangInstanceIdentifier.EMPTY, node.get());
80 InMemoryDOMStoreTreeChangePublisher publisher =
81 new InMemoryDOMStoreTreeChangePublisher(notificationManager);
82 publisher.registerTreeChangeListener(treeId, listener);
83 publisher.publishChange(candidate);
89 synchronized void publishChange(final @NonNull DataTreeCandidate candidate) {
90 // Runs synchronized with registrationRemoved()
91 processCandidateTree(candidate);