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.Objects;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.Collections;
14 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Read-only snapshot of the data tree.
30 final class InMemoryDataTree implements DataTree {
31 private static final YangInstanceIdentifier PUBLIC_ROOT_PATH = YangInstanceIdentifier.create(Collections.<PathArgument>emptyList());
32 private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
33 AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
34 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
37 * Current data store state generation.
39 private volatile DataTreeState state;
41 public InMemoryDataTree(final TreeNode rootNode, final SchemaContext schemaContext) {
42 state = DataTreeState.createInitial(rootNode);
43 if (schemaContext != null) {
44 setSchemaContext(schemaContext);
49 * This method is synchronized to guard against user attempting to install
50 * multiple contexts. Otherwise it runs in a lock-free manner.
53 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
54 Preconditions.checkNotNull(newSchemaContext);
56 LOG.info("Attempting to install schema contexts");
57 LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
59 final SchemaAwareApplyOperation operation = SchemaAwareApplyOperation.from(newSchemaContext);
61 DataTreeState currentState, newState;
64 newState = currentState.withSchemaContext(newSchemaContext, operation);
65 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
69 public InMemoryDataTreeSnapshot takeSnapshot() {
70 return state.newSnapshot();
74 public void validate(final DataTreeModification modification) throws DataValidationFailedException {
75 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
76 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
78 m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.<TreeNode>of(state.getRoot()));
82 public DataTreeCandidate prepare(final DataTreeModification modification) {
83 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
85 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
86 final ModifiedNode root = m.getRootModification();
88 if (root.getType() == ModificationType.UNMODIFIED) {
89 return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
92 final TreeNode currentRoot = state.getRoot();
93 final Optional<TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
94 Optional.<TreeNode>of(currentRoot), m.getVersion());
95 Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node");
96 return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, currentRoot, newRoot.get());
100 public void commit(final DataTreeCandidate candidate) {
101 if (candidate instanceof NoopDataTreeCandidate) {
105 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
106 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
108 if (LOG.isTraceEnabled()) {
109 LOG.trace("Data Tree is {}", StoreUtils.toStringTree(c.getAfterRoot().getData()));
112 final TreeNode newRoot = c.getAfterRoot();
113 DataTreeState currentState, newState;
115 currentState = state;
116 final TreeNode currentRoot = currentState.getRoot();
117 LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
119 final TreeNode oldRoot = c.getBeforeRoot();
120 Preconditions.checkState(oldRoot == currentRoot, "Store tree %s and candidate base %s differ.", currentRoot, oldRoot);
122 newState = currentState.withRoot(newRoot);
123 LOG.trace("Updated state from {} to {}", currentState, newState);
124 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
128 public String toString() {
129 return Objects.toStringHelper(this).add("object", super.toString()).add("state", state).toString();