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.TipProducingDataTree;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Read-only snapshot of the data tree.
24 final class InMemoryDataTree extends AbstractDataTreeTip implements TipProducingDataTree {
25 private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
26 AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
27 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
30 * Current data store state generation.
32 private volatile DataTreeState state;
34 public InMemoryDataTree(final TreeNode rootNode, final SchemaContext schemaContext) {
35 state = DataTreeState.createInitial(rootNode);
36 if (schemaContext != null) {
37 setSchemaContext(schemaContext);
42 * This method is synchronized to guard against user attempting to install
43 * multiple contexts. Otherwise it runs in a lock-free manner.
46 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
47 Preconditions.checkNotNull(newSchemaContext);
49 LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
51 final SchemaAwareApplyOperation operation = SchemaAwareApplyOperation.from(newSchemaContext);
53 DataTreeState currentState, newState;
56 newState = currentState.withSchemaContext(newSchemaContext, operation);
57 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
61 public InMemoryDataTreeSnapshot takeSnapshot() {
62 return state.newSnapshot();
66 public void commit(final DataTreeCandidate candidate) {
67 if (candidate instanceof NoopDataTreeCandidate) {
71 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
72 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
74 if (LOG.isTraceEnabled()) {
75 LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
78 final TreeNode newRoot = c.getTipRoot();
79 DataTreeState currentState, newState;
82 final TreeNode currentRoot = currentState.getRoot();
83 LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
85 final TreeNode oldRoot = c.getBeforeRoot();
86 Preconditions.checkState(oldRoot == currentRoot, "Store tree %s and candidate base %s differ.", currentRoot, oldRoot);
88 newState = currentState.withRoot(newRoot);
89 LOG.trace("Updated state from {} to {}", currentState, newState);
90 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
94 public String toString() {
95 return MoreObjects.toStringHelper(this).add("object", super.toString()).add("state", state).toString();
99 protected TreeNode getTipRoot() {
100 return state.getRoot();