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.NormalizedNodes;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Read-only snapshot of the data tree.
31 final class InMemoryDataTree implements DataTree {
32 private static final YangInstanceIdentifier PUBLIC_ROOT_PATH = YangInstanceIdentifier.create(Collections.<PathArgument>emptyList());
33 private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
34 AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
35 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
38 * Current data store state generation.
40 private volatile DataTreeState state;
42 public InMemoryDataTree(final TreeNode rootNode, final SchemaContext schemaContext) {
43 state = DataTreeState.createInitial(rootNode);
44 if (schemaContext != null) {
45 setSchemaContext(schemaContext);
50 * This method is synchronized to guard against user attempting to install
51 * multiple contexts. Otherwise it runs in a lock-free manner.
54 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
55 Preconditions.checkNotNull(newSchemaContext);
57 LOG.info("Attempting to install schema contexts");
58 LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
60 final SchemaAwareApplyOperation operation = SchemaAwareApplyOperation.from(newSchemaContext);
62 DataTreeState currentState, newState;
65 newState = currentState.withSchemaContext(newSchemaContext, operation);
66 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
70 public InMemoryDataTreeSnapshot takeSnapshot() {
71 return state.newSnapshot();
75 public void validate(final DataTreeModification modification) throws DataValidationFailedException {
76 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
77 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
79 m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.<TreeNode>of(state.getRoot()));
83 public DataTreeCandidate prepare(final DataTreeModification modification) {
84 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
86 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
87 final ModifiedNode root = m.getRootModification();
89 if (root.getType() == ModificationType.UNMODIFIED) {
90 return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
93 final TreeNode currentRoot = state.getRoot();
94 final Optional<TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
95 Optional.<TreeNode>of(currentRoot), m.getVersion());
96 Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node");
97 return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, currentRoot, newRoot.get());
101 public void commit(final DataTreeCandidate candidate) {
102 if (candidate instanceof NoopDataTreeCandidate) {
106 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
107 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
109 if (LOG.isTraceEnabled()) {
110 LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getAfterRoot().getData()));
113 final TreeNode newRoot = c.getAfterRoot();
114 DataTreeState currentState, newState;
116 currentState = state;
117 final TreeNode currentRoot = currentState.getRoot();
118 LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
120 final TreeNode oldRoot = c.getBeforeRoot();
121 Preconditions.checkState(oldRoot == currentRoot, "Store tree %s and candidate base %s differ.", currentRoot, oldRoot);
123 newState = currentState.withRoot(newRoot);
124 LOG.trace("Updated state from {} to {}", currentState, newState);
125 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
129 public String toString() {
130 return Objects.toStringHelper(this).add("object", super.toString()).add("state", state).toString();