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.mdsal.dom.store.inmemory;
10 import static java.util.Objects.requireNonNull;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.atomic.AtomicLong;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
17 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
18 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
19 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
20 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
21 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTreeChangePublisher;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
24 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedTransactions;
25 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction;
26 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
27 import org.opendaylight.yangtools.concepts.Identifiable;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
30 import org.opendaylight.yangtools.util.concurrent.EqualityQueuedNotificationManager;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
33 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
34 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
35 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
36 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
37 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
38 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
39 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
40 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * In-memory DOM Data Store. Implementation of {@link DOMStore} which uses {@link DataTree} and other classes such as
46 * {@link SnapshotBackedWriteTransaction}.
47 * {@link org.opendaylight.mdsal.dom.spi.store.SnapshotBackedReadTransaction} to implement {@link DOMStore} contract.
49 public class InMemoryDOMDataStore extends TransactionReadyPrototype<String> implements DOMStore,
50 Identifiable<String>, EffectiveModelContextListener, AutoCloseable, DOMStoreTreeChangePublisher {
51 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataStore.class);
53 private final AtomicLong txCounter = new AtomicLong(0);
54 private final DataTree dataTree;
56 private final InMemoryDOMStoreTreeChangePublisher changePublisher;
57 private final ExecutorService dataChangeListenerExecutor;
58 private final boolean debugTransactions;
59 private final String name;
61 private volatile AutoCloseable closeable;
63 public InMemoryDOMDataStore(final String name, final ExecutorService dataChangeListenerExecutor) {
64 this(name, dataChangeListenerExecutor,
65 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE, false);
68 public InMemoryDOMDataStore(final String name, final ExecutorService dataChangeListenerExecutor,
69 final int maxDataChangeListenerQueueSize, final boolean debugTransactions) {
70 this(name, LogicalDatastoreType.OPERATIONAL, dataChangeListenerExecutor, maxDataChangeListenerQueueSize,
74 public InMemoryDOMDataStore(final String name, final LogicalDatastoreType type,
75 final ExecutorService dataChangeListenerExecutor, final int maxDataChangeListenerQueueSize,
76 final boolean debugTransactions) {
77 this(name, defaultConfig(type), dataChangeListenerExecutor, maxDataChangeListenerQueueSize, debugTransactions);
80 public InMemoryDOMDataStore(final String name, final DataTreeConfiguration config,
81 final ExecutorService dataChangeListenerExecutor, final int maxDataChangeListenerQueueSize,
82 final boolean debugTransactions) {
83 this.name = requireNonNull(name);
84 this.dataChangeListenerExecutor = requireNonNull(dataChangeListenerExecutor);
85 this.debugTransactions = debugTransactions;
86 dataTree = new InMemoryDataTreeFactory().create(config);
87 changePublisher = new InMemoryDOMStoreTreeChangePublisher("name", this.dataChangeListenerExecutor,
88 maxDataChangeListenerQueueSize);
91 public EqualityQueuedNotificationManager<?, ?> getDataChangeListenerNotificationManager() {
92 return changePublisher.getNotificationManager();
95 public void setCloseable(final AutoCloseable closeable) {
96 this.closeable = closeable;
100 public final String getIdentifier() {
105 public DOMStoreReadTransaction newReadOnlyTransaction() {
106 return SnapshotBackedTransactions.newReadTransaction(nextIdentifier(), debugTransactions,
107 dataTree.takeSnapshot());
111 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
112 return SnapshotBackedTransactions.newReadWriteTransaction(nextIdentifier(), debugTransactions,
113 dataTree.takeSnapshot(), this);
117 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
118 return SnapshotBackedTransactions.newWriteTransaction(nextIdentifier(), debugTransactions,
119 dataTree.takeSnapshot(), this);
123 public DOMStoreTransactionChain createTransactionChain() {
124 return new DOMStoreTransactionChainImpl(this);
128 public synchronized void onModelContextUpdated(final EffectiveModelContext newModelContext) {
129 dataTree.setEffectiveModelContext(newModelContext);
132 @SuppressWarnings("checkstyle:IllegalCatch")
134 public void close() {
135 ExecutorServiceUtil.tryGracefulShutdown(dataChangeListenerExecutor, 30, TimeUnit.SECONDS);
137 if (closeable != null) {
140 } catch (Exception e) {
141 LOG.debug("Error closing instance", e);
146 public final boolean getDebugTransactions() {
147 return debugTransactions;
150 final DataTreeSnapshot takeSnapshot() {
151 return dataTree.takeSnapshot();
155 public synchronized <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
156 final YangInstanceIdentifier treeId, final L listener) {
158 * Make sure commit is not occurring right now. Listener has to be
159 * registered and its state capture enqueued at a consistent point.
161 return changePublisher.registerTreeChangeListener(treeId, listener, dataTree.takeSnapshot());
165 protected void transactionAborted(final SnapshotBackedWriteTransaction<String> tx) {
166 LOG.debug("Tx: {} is closed.", tx.getIdentifier());
170 protected DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction<String> tx,
171 final DataTreeModification modification, final Exception readyError) {
172 LOG.debug("Tx: {} is submitted. Modifications: {}", tx.getIdentifier(), modification);
173 return new InMemoryDOMStoreThreePhaseCommitCohort(this, tx, modification, readyError);
176 String nextIdentifier() {
177 return name + "-" + txCounter.getAndIncrement();
180 void validate(final DataTreeModification modification) throws DataValidationFailedException {
181 dataTree.validate(modification);
184 DataTreeCandidate prepare(final DataTreeModification modification) throws DataValidationFailedException {
185 return dataTree.prepare(modification);
188 synchronized void commit(final DataTreeCandidate candidate) {
189 dataTree.commit(candidate);
190 changePublisher.publishChange(candidate);
193 private static DataTreeConfiguration defaultConfig(final LogicalDatastoreType type) {
196 return DataTreeConfiguration.DEFAULT_CONFIGURATION;
198 return DataTreeConfiguration.DEFAULT_OPERATIONAL;
200 throw new IllegalArgumentException("Unhandled datastore type " + type);