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.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
18 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
19 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
20 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
21 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTreeChangePublisher;
24 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
25 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedTransactions;
26 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction;
27 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
28 import org.opendaylight.yangtools.concepts.Identifiable;
29 import org.opendaylight.yangtools.concepts.Registration;
30 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
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 @NonNull 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 void setCloseable(final AutoCloseable closeable) {
92 this.closeable = closeable;
96 public final String getIdentifier() {
101 public DOMStoreReadTransaction newReadOnlyTransaction() {
102 return SnapshotBackedTransactions.newReadTransaction(nextIdentifier(), debugTransactions,
103 dataTree.takeSnapshot());
107 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
108 return SnapshotBackedTransactions.newReadWriteTransaction(nextIdentifier(), debugTransactions,
109 dataTree.takeSnapshot(), this);
113 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
114 return SnapshotBackedTransactions.newWriteTransaction(nextIdentifier(), debugTransactions,
115 dataTree.takeSnapshot(), this);
119 public DOMStoreTransactionChain createTransactionChain() {
120 return new DOMStoreTransactionChainImpl(this);
124 public synchronized void onModelContextUpdated(final EffectiveModelContext newModelContext) {
125 dataTree.setEffectiveModelContext(newModelContext);
128 @SuppressWarnings("checkstyle:IllegalCatch")
130 public void close() {
131 ExecutorServiceUtil.tryGracefulShutdown(dataChangeListenerExecutor, 30, TimeUnit.SECONDS);
133 if (closeable != null) {
136 } catch (Exception e) {
137 LOG.debug("Error closing instance", e);
142 public final boolean getDebugTransactions() {
143 return debugTransactions;
146 final DataTreeSnapshot takeSnapshot() {
147 return dataTree.takeSnapshot();
151 public synchronized Registration registerTreeChangeListener(final YangInstanceIdentifier treeId,
152 final DOMDataTreeChangeListener listener) {
153 // Make sure commit is not occurring right now. Listener has to be registered and its state capture enqueued at
154 // a consistent point.
155 return changePublisher.registerTreeChangeListener(treeId, listener, dataTree.takeSnapshot());
159 protected void transactionAborted(final SnapshotBackedWriteTransaction<String> tx) {
160 LOG.debug("Tx: {} is closed.", tx.getIdentifier());
164 protected DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction<String> tx,
165 final DataTreeModification modification, final Exception readyError) {
166 LOG.debug("Tx: {} is submitted. Modifications: {}", tx.getIdentifier(), modification);
167 return new InMemoryDOMStoreThreePhaseCommitCohort(this, tx, modification, readyError);
170 String nextIdentifier() {
171 return name + "-" + txCounter.getAndIncrement();
174 void validate(final DataTreeModification modification) throws DataValidationFailedException {
175 dataTree.validate(modification);
178 DataTreeCandidate prepare(final DataTreeModification modification) throws DataValidationFailedException {
179 return dataTree.prepare(modification);
182 synchronized void commit(final DataTreeCandidate candidate) {
183 dataTree.commit(candidate);
184 changePublisher.publishChange(candidate);
187 private static DataTreeConfiguration defaultConfig(final LogicalDatastoreType type) {
188 return switch (type) {
189 case CONFIGURATION -> DataTreeConfiguration.DEFAULT_CONFIGURATION;
190 case OPERATIONAL -> DataTreeConfiguration.DEFAULT_OPERATIONAL;