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.tree.data;
10 import java.util.concurrent.locks.ReadWriteLock;
11 import java.util.concurrent.locks.ReentrantReadWriteLock;
13 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataPreconditionFailedException;
14 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataTree;
15 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataTreeCandidate;
16 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataTreeModification;
17 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreUtils;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import com.google.common.base.Optional;
24 import com.google.common.base.Preconditions;
27 * Read-only snapshot of the data tree.
29 final class InMemoryDataTree implements DataTree {
30 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
31 private static final InstanceIdentifier PUBLIC_ROOT_PATH = InstanceIdentifier.builder().build();
33 private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
34 private ModificationApplyOperation applyOper = new AlwaysFailOperation();
35 private SchemaContext currentSchemaContext;
36 private StoreMetadataNode rootNode;
38 public InMemoryDataTree(StoreMetadataNode rootNode, final SchemaContext schemaContext) {
39 this.rootNode = Preconditions.checkNotNull(rootNode);
41 if (schemaContext != null) {
42 // Also sets applyOper
43 setSchemaContext(schemaContext);
48 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
49 Preconditions.checkNotNull(newSchemaContext);
51 LOG.info("Attepting to install schema context {}", newSchemaContext);
54 * FIXME: we should walk the schema contexts, both current and new and see
55 * whether they are compatible here. Reject incompatible changes.
58 // Instantiate new apply operation, this still may fail
59 final ModificationApplyOperation newApplyOper = SchemaAwareApplyOperation.from(newSchemaContext);
61 // Ready to change the context now, make sure no operations are running
62 rwLock.writeLock().lock();
64 this.applyOper = newApplyOper;
65 this.currentSchemaContext = newSchemaContext;
67 rwLock.writeLock().unlock();
72 public InMemoryDataTreeSnapshot takeSnapshot() {
73 rwLock.readLock().lock();
75 return new InMemoryDataTreeSnapshot(currentSchemaContext, rootNode, applyOper);
77 rwLock.readLock().unlock();
82 public void validate(DataTreeModification modification) throws DataPreconditionFailedException {
83 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
85 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
86 m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.of(rootNode));
90 public synchronized DataTreeCandidate prepare(DataTreeModification modification) {
91 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
93 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
94 final NodeModification root = m.getRootModification();
96 if (root.getModificationType() == ModificationType.UNMODIFIED) {
97 return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
100 rwLock.writeLock().lock();
102 // FIXME: rootNode needs to be a read-write snapshot here...
103 final Optional<StoreMetadataNode> newRoot = m.getStrategy().apply(m.getRootModification(), Optional.of(rootNode), StoreUtils.increase(rootNode.getSubtreeVersion()));
104 Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node");
105 return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, rootNode, newRoot.get());
107 rwLock.writeLock().unlock();
112 public synchronized void commit(DataTreeCandidate candidate) {
113 if (candidate instanceof NoopDataTreeCandidate) {
117 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
118 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
120 LOG.debug("Updating Store snapshot version: {} with version:{}", rootNode.getSubtreeVersion(), c.getAfterRoot().getSubtreeVersion());
122 if (LOG.isTraceEnabled()) {
123 LOG.trace("Data Tree is {}", StoreUtils.toStringTree(c.getAfterRoot().getData()));
126 // Ready to change the context now, make sure no operations are running
127 rwLock.writeLock().lock();
129 Preconditions.checkState(c.getBeforeRoot() == rootNode,
130 String.format("Store snapshot %s and transaction snapshot %s differ.", rootNode, c.getBeforeRoot()));
131 this.rootNode = c.getAfterRoot();
133 rwLock.writeLock().unlock();