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.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
13 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Read-only snapshot of the data tree.
25 final class InMemoryDataTree extends AbstractDataTreeTip implements TipProducingDataTree {
26 private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
27 AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
28 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
31 * Current data store state generation.
33 private volatile DataTreeState state;
34 private final TreeType treeType;
36 public InMemoryDataTree(final TreeNode rootNode, final TreeType treeType, final SchemaContext schemaContext) {
37 this.treeType = Preconditions.checkNotNull(treeType,treeType);
38 state = DataTreeState.createInitial(rootNode);
39 if (schemaContext != null) {
40 setSchemaContext(schemaContext);
45 * This method is synchronized to guard against user attempting to install
46 * multiple contexts. Otherwise it runs in a lock-free manner.
49 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
50 Preconditions.checkNotNull(newSchemaContext);
52 LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
54 final SchemaAwareApplyOperation operation = SchemaAwareApplyOperation.from(newSchemaContext,treeType);
56 DataTreeState currentState, newState;
59 newState = currentState.withSchemaContext(newSchemaContext, operation);
60 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
64 public InMemoryDataTreeSnapshot takeSnapshot() {
65 return state.newSnapshot();
69 public void commit(final DataTreeCandidate candidate) {
70 if (candidate instanceof NoopDataTreeCandidate) {
73 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
74 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
76 if (LOG.isTraceEnabled()) {
77 LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
80 final TreeNode newRoot = c.getTipRoot();
81 DataTreeState currentState, newState;
84 final TreeNode currentRoot = currentState.getRoot();
85 LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
87 final TreeNode oldRoot = c.getBeforeRoot();
88 Preconditions.checkState(oldRoot == currentRoot, "Store tree %s and candidate base %s differ.", currentRoot, oldRoot);
90 newState = currentState.withRoot(newRoot);
91 LOG.trace("Updated state from {} to {}", currentState, newState);
92 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
96 public String toString() {
97 return MoreObjects.toStringHelper(this).add("object", super.toString()).add("state", state).toString();
101 protected TreeNode getTipRoot() {
102 return state.getRoot();