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.yangtools.yang.data.impl.schema.tree;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.locks.ReadWriteLock;
13 import java.util.concurrent.locks.ReentrantReadWriteLock;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.tree.RootModificationApplyOperation.LatestOperationHolder;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
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 YangInstanceIdentifier PUBLIC_ROOT_PATH = YangInstanceIdentifier.builder().build();
33 private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
34 private final LatestOperationHolder operationHolder = new LatestOperationHolder();
35 private SchemaContext currentSchemaContext;
36 private TreeNode rootNode;
38 public InMemoryDataTree(final TreeNode 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("Attempting to install schema contexts");
52 LOG.debug("Following schema contexts will be attempted {}",newSchemaContext);
55 * FIXME: we should walk the schema contexts, both current and new and see
56 * whether they are compatible here. Reject incompatible changes.
59 // Instantiate new apply operation, this still may fail
60 final ModificationApplyOperation newApplyOper = SchemaAwareApplyOperation.from(newSchemaContext);
62 // Ready to change the context now, make sure no operations are running
63 rwLock.writeLock().lock();
65 this.operationHolder.setCurrent(newApplyOper);
66 this.currentSchemaContext = newSchemaContext;
68 rwLock.writeLock().unlock();
73 public InMemoryDataTreeSnapshot takeSnapshot() {
74 rwLock.readLock().lock();
76 return new InMemoryDataTreeSnapshot(currentSchemaContext, rootNode, operationHolder.newSnapshot());
78 rwLock.readLock().unlock();
83 public void validate(final DataTreeModification modification) throws DataValidationFailedException {
84 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
86 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
87 m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.<TreeNode>of(rootNode));
91 public synchronized DataTreeCandidate prepare(final DataTreeModification modification) {
92 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
94 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
95 final ModifiedNode root = m.getRootModification();
97 if (root.getType() == ModificationType.UNMODIFIED) {
98 return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
101 rwLock.writeLock().lock();
103 final Optional<TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
104 Optional.<TreeNode>of(rootNode), m.getVersion());
105 Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node");
106 return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, rootNode, newRoot.get());
108 rwLock.writeLock().unlock();
113 public synchronized void commit(final DataTreeCandidate candidate) {
114 if (candidate instanceof NoopDataTreeCandidate) {
118 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
119 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
121 LOG.debug("Updating datastore from {} to {}", rootNode, c.getAfterRoot());
123 if (LOG.isTraceEnabled()) {
124 LOG.trace("Data Tree is {}", StoreUtils.toStringTree(c.getAfterRoot().getData()));
127 // Ready to change the context now, make sure no operations are running
128 rwLock.writeLock().lock();
130 Preconditions.checkState(c.getBeforeRoot() == rootNode,
131 "Store tree %s and candidate base %s differ.", rootNode, c.getBeforeRoot());
132 this.rootNode = c.getAfterRoot();
134 rwLock.writeLock().unlock();