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.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNodes;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Read-only snapshot of the data tree.
28 final class InMemoryDataTree extends AbstractDataTreeTip implements TipProducingDataTree {
29 private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
30 AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
31 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
33 private final YangInstanceIdentifier rootPath;
34 private final TreeType treeType;
37 * Current data store state generation.
39 private volatile DataTreeState state;
41 public InMemoryDataTree(final TreeNode rootNode, final TreeType treeType, final YangInstanceIdentifier rootPath, final SchemaContext schemaContext) {
42 this.treeType = Preconditions.checkNotNull(treeType, "treeType");
43 this.rootPath = Preconditions.checkNotNull(rootPath, "rootPath");
44 state = DataTreeState.createInitial(rootNode);
45 if (schemaContext != null) {
46 setSchemaContext(schemaContext);
51 * This method is synchronized to guard against user attempting to install
52 * multiple contexts. Otherwise it runs in a lock-free manner.
55 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
56 Preconditions.checkNotNull(newSchemaContext);
58 LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
60 final ModificationApplyOperation op = SchemaAwareApplyOperation.from(newSchemaContext, treeType);
61 final Optional<ModificationApplyOperation> maybeRootNode = StoreTreeNodes.findNode(op, rootPath);
62 if (!maybeRootNode.isPresent()) {
63 LOG.debug("Could not find root {} in new schema context, not upgrading", rootPath);
67 final ModificationApplyOperation rootNode = maybeRootNode.get();
68 if (!(rootNode instanceof AbstractNodeContainerModificationStrategy)) {
69 LOG.warn("Root {} resolves to non-container type {}, not upgrading", rootPath, rootNode);
73 DataTreeState currentState, newState;
76 newState = currentState.withSchemaContext(newSchemaContext, (SchemaAwareApplyOperation) rootNode);
77 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
81 public InMemoryDataTreeSnapshot takeSnapshot() {
82 return state.newSnapshot();
86 public void commit(final DataTreeCandidate candidate) {
87 if (candidate instanceof NoopDataTreeCandidate) {
90 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
91 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
93 if (LOG.isTraceEnabled()) {
94 LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
97 final TreeNode newRoot = c.getTipRoot();
98 DataTreeState currentState, newState;
100 currentState = state;
101 final TreeNode currentRoot = currentState.getRoot();
102 LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
104 final TreeNode oldRoot = c.getBeforeRoot();
105 Preconditions.checkState(oldRoot == currentRoot, "Store tree %s and candidate base %s differ.", currentRoot, oldRoot);
107 newState = currentState.withRoot(newRoot);
108 LOG.trace("Updated state from {} to {}", currentState, newState);
109 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
113 public YangInstanceIdentifier getRootPath() {
118 public String toString() {
119 return MoreObjects.toStringHelper(this).
120 add("object", super.toString()).
121 add("rootPath", rootPath).
127 protected TreeNode getTipRoot() {
128 return state.getRoot();