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 javax.annotation.Nonnull;
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.TipProducingDataTree;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
20 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
21 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Read-only snapshot of the data tree.
32 final class InMemoryDataTree extends AbstractDataTreeTip implements TipProducingDataTree {
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);
37 private final YangInstanceIdentifier rootPath;
38 private final TreeType treeType;
41 * Current data store state generation.
43 private volatile DataTreeState state;
45 public InMemoryDataTree(final TreeNode rootNode, final TreeType treeType, final YangInstanceIdentifier rootPath, final SchemaContext schemaContext) {
46 this.treeType = Preconditions.checkNotNull(treeType, "treeType");
47 this.rootPath = Preconditions.checkNotNull(rootPath, "rootPath");
48 state = DataTreeState.createInitial(rootNode);
49 if (schemaContext != null) {
50 setSchemaContext(schemaContext);
55 * This method is synchronized to guard against user attempting to install
56 * multiple contexts. Otherwise it runs in a lock-free manner.
59 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
60 Preconditions.checkNotNull(newSchemaContext);
62 LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
64 final DataSchemaContextTree contextTree = DataSchemaContextTree.from(newSchemaContext);
65 final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(rootPath);
66 if (rootContextNode == null) {
67 LOG.debug("Could not find root {} in new schema context, not upgrading", rootPath);
71 final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
72 if (!(rootSchemaNode instanceof DataNodeContainer)) {
73 LOG.warn("Root {} resolves to non-container type {}, not upgrading", rootPath, rootSchemaNode);
77 final ModificationApplyOperation rootNode;
78 if (rootSchemaNode instanceof ContainerSchemaNode) {
79 // FIXME: real root needs to enfore presence, but that require pre-population
80 rootNode = new ContainerModificationStrategy((ContainerSchemaNode) rootSchemaNode, treeType);
82 rootNode = SchemaAwareApplyOperation.from(rootSchemaNode, treeType);
85 DataTreeState currentState, newState;
88 newState = currentState.withSchemaContext(newSchemaContext, rootNode);
89 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
93 public InMemoryDataTreeSnapshot takeSnapshot() {
94 return state.newSnapshot();
98 public void commit(final DataTreeCandidate candidate) {
99 if (candidate instanceof NoopDataTreeCandidate) {
102 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
103 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
105 if (LOG.isTraceEnabled()) {
106 LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
109 final TreeNode newRoot = c.getTipRoot();
110 DataTreeState currentState, newState;
112 currentState = state;
113 final TreeNode currentRoot = currentState.getRoot();
114 LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
116 final TreeNode oldRoot = c.getBeforeRoot();
117 if (oldRoot != currentRoot) {
118 final String oldStr = simpleToString(oldRoot);
119 final String currentStr = simpleToString(currentRoot);
120 throw new IllegalStateException("Store tree " + currentStr + " and candidate base " + oldStr + " differ.");
123 newState = currentState.withRoot(newRoot);
124 LOG.trace("Updated state from {} to {}", currentState, newState);
125 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
128 private static String simpleToString(final Object o) {
129 return o.getClass().getName() + "@" + Integer.toHexString(o.hashCode());
133 public YangInstanceIdentifier getRootPath() {
138 public String toString() {
139 return MoreObjects.toStringHelper(this).
140 add("object", super.toString()).
141 add("rootPath", rootPath).
148 protected TreeNode getTipRoot() {
149 return state.getRoot();