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 java.util.concurrent.locks.ReadWriteLock;
11 import java.util.concurrent.locks.ReentrantReadWriteLock;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
20 import org.opendaylight.yangtools.yang.data.impl.schema.tree.RootModificationApplyOperation.LatestOperationHolder;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.common.base.Optional;
26 import com.google.common.base.Preconditions;
29 * Read-only snapshot of the data tree.
31 final class InMemoryDataTree implements DataTree {
32 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
33 private static final YangInstanceIdentifier PUBLIC_ROOT_PATH = YangInstanceIdentifier.builder().build();
35 private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
36 private final LatestOperationHolder operationHolder = new LatestOperationHolder();
37 private SchemaContext currentSchemaContext;
38 private TreeNode rootNode;
40 public InMemoryDataTree(final TreeNode rootNode, final SchemaContext schemaContext) {
41 this.rootNode = Preconditions.checkNotNull(rootNode);
43 if (schemaContext != null) {
44 // Also sets applyOper
45 setSchemaContext(schemaContext);
50 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
51 Preconditions.checkNotNull(newSchemaContext);
53 LOG.info("Attempting to install schema contexts");
54 LOG.debug("Following schema contexts will be attempted {}",newSchemaContext);
57 * FIXME: we should walk the schema contexts, both current and new and see
58 * whether they are compatible here. Reject incompatible changes.
61 // Instantiate new apply operation, this still may fail
62 final ModificationApplyOperation newApplyOper = SchemaAwareApplyOperation.from(newSchemaContext);
64 // Ready to change the context now, make sure no operations are running
65 rwLock.writeLock().lock();
67 this.operationHolder.setCurrent(newApplyOper);
68 this.currentSchemaContext = newSchemaContext;
70 rwLock.writeLock().unlock();
75 public InMemoryDataTreeSnapshot takeSnapshot() {
76 rwLock.readLock().lock();
78 return new InMemoryDataTreeSnapshot(currentSchemaContext, rootNode, operationHolder.newSnapshot());
80 rwLock.readLock().unlock();
85 public void validate(final DataTreeModification modification) throws DataValidationFailedException {
86 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
88 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
89 m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.<TreeNode>of(rootNode));
93 public synchronized DataTreeCandidate prepare(final DataTreeModification modification) {
94 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
96 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
97 final ModifiedNode root = m.getRootModification();
99 if (root.getType() == ModificationType.UNMODIFIED) {
100 return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
103 rwLock.writeLock().lock();
105 final Optional<TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
106 Optional.<TreeNode>of(rootNode), m.getVersion());
107 Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node");
108 return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, rootNode, newRoot.get());
110 rwLock.writeLock().unlock();
115 public synchronized void commit(final DataTreeCandidate candidate) {
116 if (candidate instanceof NoopDataTreeCandidate) {
120 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
121 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
123 LOG.debug("Updating datastore from {} to {}", rootNode, c.getAfterRoot());
125 if (LOG.isTraceEnabled()) {
126 LOG.trace("Data Tree is {}", StoreUtils.toStringTree(c.getAfterRoot().getData()));
129 // Ready to change the context now, make sure no operations are running
130 rwLock.writeLock().lock();
132 Preconditions.checkState(c.getBeforeRoot() == rootNode,
133 String.format("Store tree %s and candidate base %s differ.", rootNode, c.getBeforeRoot()));
134 this.rootNode = c.getAfterRoot();
136 rwLock.writeLock().unlock();