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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.atomic.AtomicLong;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
18 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerTree;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTreeChangePublisher;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
26 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedTransactions;
27 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
28 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
29 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
30 import org.opendaylight.yangtools.concepts.Identifiable;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
33 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
34 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager.Invoker;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
39 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
40 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
42 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * In-memory DOM Data Store
51 * Implementation of {@link DOMStore} which uses {@link DataTree} and other
52 * classes such as {@link SnapshotBackedWriteTransaction}.
53 * {@link org.opendaylight.controller.sal.core.spi.data.SnapshotBackedReadTransaction} and {@link ResolveDataChangeEventsTask}
54 * to implement {@link DOMStore} contract.
57 public class InMemoryDOMDataStore extends TransactionReadyPrototype<String> implements DOMStore, Identifiable<String>, SchemaContextListener, AutoCloseable, DOMStoreTreeChangePublisher {
58 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataStore.class);
60 private static final Invoker<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> DCL_NOTIFICATION_MGR_INVOKER =
61 new Invoker<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent>() {
63 public void invokeListener(final DataChangeListenerRegistration<?> listener,
64 final DOMImmutableDataChangeEvent notification ) {
65 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> inst = listener.getInstance();
67 inst.onDataChanged(notification);
72 private final DataTree dataTree = InMemoryDataTreeFactory.getInstance().create();
73 private final ListenerTree listenerTree = ListenerTree.create();
74 private final AtomicLong txCounter = new AtomicLong(0);
76 private final QueuedNotificationManager<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent> dataChangeListenerNotificationManager;
77 private final InMemoryDOMStoreTreeChangePublisher changePublisher;
78 private final ExecutorService dataChangeListenerExecutor;
79 private final boolean debugTransactions;
80 private final String name;
82 private volatile AutoCloseable closeable;
84 public InMemoryDOMDataStore(final String name, final ExecutorService dataChangeListenerExecutor) {
85 this(name, dataChangeListenerExecutor, InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE, false);
88 public InMemoryDOMDataStore(final String name, final ExecutorService dataChangeListenerExecutor,
89 final int maxDataChangeListenerQueueSize, final boolean debugTransactions) {
90 this.name = Preconditions.checkNotNull(name);
91 this.dataChangeListenerExecutor = Preconditions.checkNotNull(dataChangeListenerExecutor);
92 this.debugTransactions = debugTransactions;
94 dataChangeListenerNotificationManager =
95 new QueuedNotificationManager<>(this.dataChangeListenerExecutor,
96 DCL_NOTIFICATION_MGR_INVOKER, maxDataChangeListenerQueueSize,
97 "DataChangeListenerQueueMgr");
98 changePublisher = new InMemoryDOMStoreTreeChangePublisher(this.dataChangeListenerExecutor, maxDataChangeListenerQueueSize);
101 public void setCloseable(final AutoCloseable closeable) {
102 this.closeable = closeable;
105 public QueuedNotificationManager<?, ?> getDataChangeListenerNotificationManager() {
106 return dataChangeListenerNotificationManager;
110 public final String getIdentifier() {
115 public DOMStoreReadTransaction newReadOnlyTransaction() {
116 return SnapshotBackedTransactions.newReadTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot());
120 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
121 return SnapshotBackedTransactions.newReadWriteTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot(), this);
125 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
126 return SnapshotBackedTransactions.newWriteTransaction(nextIdentifier(), debugTransactions, dataTree.takeSnapshot(), this);
130 public DOMStoreTransactionChain createTransactionChain() {
131 return new DOMStoreTransactionChainImpl(this);
135 public synchronized void onGlobalContextUpdated(final SchemaContext ctx) {
136 dataTree.setSchemaContext(ctx);
140 public void close() {
141 ExecutorServiceUtil.tryGracefulShutdown(dataChangeListenerExecutor, 30, TimeUnit.SECONDS);
143 if(closeable != null) {
146 } catch(Exception e) {
147 LOG.debug("Error closing instance", e);
152 public final boolean getDebugTransactions() {
153 return debugTransactions;
156 final DataTreeSnapshot takeSnapshot() {
157 return dataTree.takeSnapshot();
161 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L> registerChangeListener(
162 final YangInstanceIdentifier path, final L listener, final DataChangeScope scope) {
165 * Make sure commit is not occurring right now. Listener has to be
166 * registered and its state capture enqueued at a consistent point.
168 * FIXME: improve this to read-write lock, such that multiple listener
169 * registrations can occur simultaneously
171 final DataChangeListenerRegistration<L> reg;
172 synchronized (this) {
173 LOG.debug("{}: Registering data change listener {} for {}", name, listener, path);
175 reg = listenerTree.registerDataChangeListener(path, listener, scope);
177 Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
178 if (currentState.isPresent()) {
179 final NormalizedNode<?, ?> data = currentState.get();
181 final DOMImmutableDataChangeEvent event = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE) //
183 .addCreated(path, data) //
186 dataChangeListenerNotificationManager.submitNotification(reg, event);
190 return new AbstractListenerRegistration<L>(listener) {
192 protected void removeRegistration() {
193 synchronized (InMemoryDOMDataStore.this) {
201 public synchronized <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(final YangInstanceIdentifier treeId, final L listener) {
203 * Make sure commit is not occurring right now. Listener has to be
204 * registered and its state capture enqueued at a consistent point.
206 return changePublisher.registerTreeChangeListener(treeId, listener, dataTree.takeSnapshot());
210 protected void transactionAborted(final SnapshotBackedWriteTransaction<String> tx) {
211 LOG.debug("Tx: {} is closed.", tx.getIdentifier());
215 protected DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction<String> tx, final DataTreeModification modification) {
216 LOG.debug("Tx: {} is submitted. Modifications: {}", tx.getIdentifier(), modification);
217 return new InMemoryDOMStoreThreePhaseCommitCohort(this, tx, modification);
220 String nextIdentifier() {
221 return name + "-" + txCounter.getAndIncrement();
224 void validate(final DataTreeModification modification) throws DataValidationFailedException {
225 dataTree.validate(modification);
228 DataTreeCandidate prepare(final DataTreeModification modification) {
229 return dataTree.prepare(modification);
232 synchronized void commit(final DataTreeCandidate candidate) {
233 dataTree.commit(candidate);
234 changePublisher.publishChange(candidate);
235 ResolveDataChangeEventsTask.create(candidate, listenerTree).resolve(dataChangeListenerNotificationManager);