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.Optional;
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.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
19 import org.opendaylight.controller.sal.core.compat.DataChangeListenerRegistration;
20 import org.opendaylight.controller.sal.core.compat.ListenerTree;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTreeChangePublisher;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
28 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedTransactions;
29 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
30 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
31 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
32 import org.opendaylight.yangtools.concepts.Identifiable;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.util.ExecutorServiceUtil;
35 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
36 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager.Invoker;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
40 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
42 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
43 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
44 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
45 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * In-memory DOM Data Store
55 * Implementation of {@link DOMStore} which uses {@link DataTree} and other
56 * classes such as {@link SnapshotBackedWriteTransaction}.
57 * {@link org.opendaylight.controller.sal.core.spi.data.SnapshotBackedReadTransaction} and
58 * {@link ResolveDataChangeEventsTask}
59 * to implement {@link DOMStore} contract.
62 public class InMemoryDOMDataStore extends TransactionReadyPrototype<String>
63 implements DOMStore, Identifiable<String>, SchemaContextListener, AutoCloseable, DOMStoreTreeChangePublisher {
64 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDOMDataStore.class);
66 private static final Invoker<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent>
67 DCL_NOTIFICATION_MGR_INVOKER =
68 (listener, notification) -> {
69 final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> inst =
70 listener.getInstance();
71 inst.onDataChanged(notification);
74 private final DataTree dataTree;
75 private final ListenerTree listenerTree = ListenerTree.create();
76 private final AtomicLong txCounter = new AtomicLong(0);
78 private final QueuedNotificationManager<DataChangeListenerRegistration<?>, DOMImmutableDataChangeEvent>
79 dataChangeListenerNotificationManager;
80 private final InMemoryDOMStoreTreeChangePublisher changePublisher;
81 private final ExecutorService dataChangeListenerExecutor;
82 private final boolean debugTransactions;
83 private final String name;
85 private volatile AutoCloseable closeable;
87 public InMemoryDOMDataStore(final String name, final ExecutorService dataChangeListenerExecutor) {
88 this(name, LogicalDatastoreType.OPERATIONAL, dataChangeListenerExecutor,
89 InMemoryDOMDataStoreConfigProperties.DEFAULT_MAX_DATA_CHANGE_LISTENER_QUEUE_SIZE, false);
92 public InMemoryDOMDataStore(final String name, final LogicalDatastoreType type,
93 final ExecutorService dataChangeListenerExecutor,
94 final int maxDataChangeListenerQueueSize, final boolean debugTransactions) {
95 this.name = Preconditions.checkNotNull(name);
96 this.dataChangeListenerExecutor = Preconditions.checkNotNull(dataChangeListenerExecutor);
97 this.debugTransactions = debugTransactions;
99 dataChangeListenerNotificationManager =
100 new QueuedNotificationManager<>(this.dataChangeListenerExecutor,
101 DCL_NOTIFICATION_MGR_INVOKER, maxDataChangeListenerQueueSize,
102 "DataChangeListenerQueueMgr");
103 changePublisher = new InMemoryDOMStoreTreeChangePublisher(this.dataChangeListenerExecutor,
104 maxDataChangeListenerQueueSize);
108 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION);
111 dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL);
114 throw new IllegalArgumentException("Data store " + type + " not supported");
118 public void setCloseable(final AutoCloseable closeable) {
119 this.closeable = closeable;
122 public QueuedNotificationManager<?, ?> getDataChangeListenerNotificationManager() {
123 return dataChangeListenerNotificationManager;
127 public final String getIdentifier() {
132 public DOMStoreReadTransaction newReadOnlyTransaction() {
133 return SnapshotBackedTransactions.newReadTransaction(nextIdentifier(), debugTransactions,
134 dataTree.takeSnapshot());
138 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
139 return SnapshotBackedTransactions.newReadWriteTransaction(nextIdentifier(), debugTransactions,
140 dataTree.takeSnapshot(), this);
144 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
145 return SnapshotBackedTransactions.newWriteTransaction(nextIdentifier(), debugTransactions,
146 dataTree.takeSnapshot(), this);
150 public DOMStoreTransactionChain createTransactionChain() {
151 return new DOMStoreTransactionChainImpl(this);
155 public synchronized void onGlobalContextUpdated(final SchemaContext ctx) {
156 dataTree.setSchemaContext(ctx);
160 @SuppressWarnings("checkstyle:IllegalCatch")
161 public void close() {
162 ExecutorServiceUtil.tryGracefulShutdown(dataChangeListenerExecutor, 30, TimeUnit.SECONDS);
164 if (closeable != null) {
167 } catch (Exception e) {
168 LOG.debug("Error closing instance", e);
173 public final boolean getDebugTransactions() {
174 return debugTransactions;
177 final DataTreeSnapshot takeSnapshot() {
178 return dataTree.takeSnapshot();
182 public <L extends AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>> ListenerRegistration<L>
183 registerChangeListener(final YangInstanceIdentifier path, final L listener, final DataChangeScope scope) {
186 * Make sure commit is not occurring right now. Listener has to be
187 * registered and its state capture enqueued at a consistent point.
189 * FIXME: improve this to read-write lock, such that multiple listener
190 * registrations can occur simultaneously
192 final DataChangeListenerRegistration<L> reg;
193 synchronized (this) {
194 LOG.debug("{}: Registering data change listener {} for {}", name, listener, path);
196 reg = listenerTree.registerDataChangeListener(path, listener, scope);
198 Optional<NormalizedNode<?, ?>> currentState = dataTree.takeSnapshot().readNode(path);
199 if (currentState.isPresent()) {
200 final NormalizedNode<?, ?> data = currentState.get();
202 final DOMImmutableDataChangeEvent event = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE) //
204 .addCreated(path, data) //
207 dataChangeListenerNotificationManager.submitNotification(reg, event);
211 return new AbstractListenerRegistration<L>(listener) {
213 protected void removeRegistration() {
214 synchronized (InMemoryDOMDataStore.this) {
222 public synchronized <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerTreeChangeListener(
223 final YangInstanceIdentifier treeId, final L listener) {
225 * Make sure commit is not occurring right now. Listener has to be
226 * registered and its state capture enqueued at a consistent point.
228 return changePublisher.registerTreeChangeListener(treeId, listener, dataTree.takeSnapshot());
232 protected void transactionAborted(final SnapshotBackedWriteTransaction<String> tx) {
233 LOG.debug("Tx: {} is closed.", tx.getIdentifier());
237 protected DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction<String> tx,
238 final DataTreeModification modification,
239 final Exception readyError) {
240 LOG.debug("Tx: {} is submitted. Modifications: {}", tx.getIdentifier(), modification);
241 return new InMemoryDOMStoreThreePhaseCommitCohort(this, tx, modification, readyError);
244 String nextIdentifier() {
245 return name + "-" + txCounter.getAndIncrement();
248 void validate(final DataTreeModification modification) throws DataValidationFailedException {
249 dataTree.validate(modification);
252 DataTreeCandidate prepare(final DataTreeModification modification) {
253 return dataTree.prepare(modification);
256 synchronized void commit(final DataTreeCandidate candidate) {
257 dataTree.commit(candidate);
258 changePublisher.publishChange(candidate);
259 ResolveDataChangeEventsTask.create(candidate, listenerTree).resolve(dataChangeListenerNotificationManager);