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.Preconditions;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.TimeUnit;
13 import java.util.concurrent.atomic.AtomicLong;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
16 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTreeChangePublisher;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedTransactions;
24 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
25 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
26 import org.opendaylight.yangtools.concepts.Identifiable;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
29 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
37 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * In-memory DOM Data Store
47 * Implementation of {@link DOMStore} which uses {@link DataTree} and other
48 * classes such as {@link SnapshotBackedWriteTransaction}.
49 * {@link org.opendaylight.controller.sal.core.spi.data.SnapshotBackedReadTransaction} and
50 * to implement {@link DOMStore} contract.
53 public class InMemoryDOMDataStore extends TransactionReadyPrototype<String>
54 implements DOMStore, Identifiable<String>, SchemaContextListener, AutoCloseable, DOMStoreTreeChangePublisher {
55 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataStore.class);
57 private final DataTree dataTree;
58 private final AtomicLong txCounter = new AtomicLong(0);
60 private final InMemoryDOMStoreTreeChangePublisher changePublisher;
61 private final ExecutorService dataChangeListenerExecutor;
62 private final boolean debugTransactions;
63 private final String name;
65 private volatile AutoCloseable closeable;
67 public InMemoryDOMDataStore(final String name, final ExecutorService dataChangeListenerExecutor) {
68 this(name, LogicalDatastoreType.OPERATIONAL, dataChangeListenerExecutor,
69 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE, false);
72 public InMemoryDOMDataStore(final String name, final LogicalDatastoreType type,
73 final ExecutorService dataChangeListenerExecutor,
74 final int maxDataChangeListenerQueueSize, final boolean debugTransactions) {
75 this.name = Preconditions.checkNotNull(name);
76 this.dataChangeListenerExecutor = Preconditions.checkNotNull(dataChangeListenerExecutor);
77 this.debugTransactions = debugTransactions;
79 changePublisher = new InMemoryDOMStoreTreeChangePublisher(this.dataChangeListenerExecutor,
80 maxDataChangeListenerQueueSize);
84 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION);
87 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL);
90 throw new IllegalArgumentException("Data store " + type + " not supported");
94 public void setCloseable(final AutoCloseable closeable) {
95 this.closeable = closeable;
98 public QueuedNotificationManager<?, ?> getDataChangeListenerNotificationManager() {
99 return changePublisher.getNotificationManager();
103 public final String getIdentifier() {
108 public DOMStoreReadTransaction newReadOnlyTransaction() {
109 return SnapshotBackedTransactions.newReadTransaction(nextIdentifier(), debugTransactions,
110 dataTree.takeSnapshot());
114 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
115 return SnapshotBackedTransactions.newReadWriteTransaction(nextIdentifier(), debugTransactions,
116 dataTree.takeSnapshot(), this);
120 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
121 return SnapshotBackedTransactions.newWriteTransaction(nextIdentifier(), debugTransactions,
122 dataTree.takeSnapshot(), this);
126 public DOMStoreTransactionChain createTransactionChain() {
127 return new DOMStoreTransactionChainImpl(this);
131 public synchronized void onGlobalContextUpdated(final SchemaContext ctx) {
132 dataTree.setSchemaContext(ctx);
136 @SuppressWarnings("checkstyle:IllegalCatch")
137 public void close() {
138 ExecutorServiceUtil.tryGracefulShutdown(dataChangeListenerExecutor, 30, TimeUnit.SECONDS);
140 if (closeable != null) {
143 } catch (Exception e) {
144 LOG.debug("Error closing instance", e);
149 public final boolean getDebugTransactions() {
150 return debugTransactions;
153 final DataTreeSnapshot takeSnapshot() {
154 return dataTree.takeSnapshot();
158 public synchronized <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
159 final YangInstanceIdentifier treeId, final L listener) {
161 * Make sure commit is not occurring right now. Listener has to be
162 * registered and its state capture enqueued at a consistent point.
164 return changePublisher.registerTreeChangeListener(treeId, listener, dataTree.takeSnapshot());
168 protected void transactionAborted(final SnapshotBackedWriteTransaction<String> tx) {
169 LOG.debug("Tx: {} is closed.", tx.getIdentifier());
173 protected DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction<String> tx,
174 final DataTreeModification modification,
175 final Exception readyError) {
176 LOG.debug("Tx: {} is submitted. Modifications: {}", tx.getIdentifier(), modification);
177 return new InMemoryDOMStoreThreePhaseCommitCohort(this, tx, modification, readyError);
180 String nextIdentifier() {
181 return name + "-" + txCounter.getAndIncrement();
184 void validate(final DataTreeModification modification) throws DataValidationFailedException {
185 dataTree.validate(modification);
188 DataTreeCandidate prepare(final DataTreeModification modification) {
189 return dataTree.prepare(modification);
192 synchronized void commit(final DataTreeCandidate candidate) {
193 dataTree.commit(candidate);
194 changePublisher.publishChange(candidate);