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.Collections;
13 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
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 YangInstanceIdentifier PUBLIC_ROOT_PATH = YangInstanceIdentifier.create(Collections.<PathArgument>emptyList());
31 private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
32 AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
33 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
36 * Current data store state generation.
38 private volatile DataTreeState state;
40 public InMemoryDataTree(final TreeNode rootNode, final SchemaContext schemaContext) {
41 state = DataTreeState.createInitial(rootNode);
42 if (schemaContext != null) {
43 setSchemaContext(schemaContext);
48 * This method is synchronized to guard against user attempting to install
49 * multiple contexts. Otherwise it runs in a lock-free manner.
52 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
53 Preconditions.checkNotNull(newSchemaContext);
55 LOG.info("Attempting to install schema contexts");
56 LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
58 final SchemaAwareApplyOperation operation = SchemaAwareApplyOperation.from(newSchemaContext);
60 DataTreeState currentState, newState;
63 newState = currentState.withSchemaContext(newSchemaContext, operation);
64 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
68 public InMemoryDataTreeSnapshot takeSnapshot() {
69 return state.newSnapshot();
73 public void validate(final DataTreeModification modification) throws DataValidationFailedException {
74 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
75 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
77 m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.<TreeNode>of(state.getRoot()));
81 public DataTreeCandidate prepare(final DataTreeModification modification) {
82 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
84 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
85 final ModifiedNode root = m.getRootModification();
87 if (root.getType() == ModificationType.UNMODIFIED) {
88 return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
91 final TreeNode currentRoot = state.getRoot();
92 final Optional<TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
93 Optional.<TreeNode>of(currentRoot), m.getVersion());
94 Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node");
95 return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, currentRoot, newRoot.get());
99 public void commit(final DataTreeCandidate candidate) {
100 if (candidate instanceof NoopDataTreeCandidate) {
104 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
105 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
107 if (LOG.isTraceEnabled()) {
108 LOG.trace("Data Tree is {}", StoreUtils.toStringTree(c.getAfterRoot().getData()));
111 final TreeNode newRoot = c.getAfterRoot();
112 DataTreeState currentState, newState;
114 currentState = state;
115 final TreeNode currentRoot = currentState.getRoot();
116 LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
118 final TreeNode oldRoot = c.getBeforeRoot();
119 Preconditions.checkState(oldRoot == currentRoot, "Store tree %s and candidate base %s differ.", currentRoot, oldRoot);
121 newState = currentState.withRoot(newRoot);
122 LOG.trace("Updated state from {} to {}", currentState, newState);
123 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));