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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.MoreObjects;
14 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.DataTreeConfiguration;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
22 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
23 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Read-only snapshot of the data tree.
34 final class InMemoryDataTree extends AbstractDataTreeTip implements DataTree {
35 private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
36 AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
37 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
39 private final DataTreeConfiguration treeConfig;
40 private final boolean maskMandatory;
43 * Current data store state generation.
45 private volatile DataTreeState state;
47 InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
48 final SchemaContext schemaContext) {
49 this.treeConfig = requireNonNull(treeConfig, "treeConfig");
51 state = DataTreeState.createInitial(rootNode);
52 if (schemaContext != null) {
53 setSchemaContext(schemaContext);
57 InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
58 final SchemaContext schemaContext, final DataSchemaNode rootSchemaNode, final boolean maskMandatory) {
59 this.treeConfig = requireNonNull(treeConfig, "treeConfig");
60 this.maskMandatory = maskMandatory;
62 state = DataTreeState.createInitial(rootNode).withSchemaContext(schemaContext, getOperation(rootSchemaNode));
65 private ModificationApplyOperation getOperation(final DataSchemaNode rootSchemaNode) {
66 if (maskMandatory && rootSchemaNode instanceof ContainerSchemaNode) {
67 return new ContainerModificationStrategy((ContainerSchemaNode) rootSchemaNode, treeConfig);
70 return SchemaAwareApplyOperation.from(rootSchemaNode, treeConfig);
74 * This method is synchronized to guard against user attempting to install
75 * multiple contexts. Otherwise it runs in a lock-free manner.
78 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
79 requireNonNull(newSchemaContext);
81 LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
83 final DataSchemaContextTree contextTree = DataSchemaContextTree.from(newSchemaContext);
84 final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(getRootPath());
85 if (rootContextNode == null) {
86 LOG.warn("Could not find root {} in new schema context, not upgrading", getRootPath());
90 final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
91 if (!(rootSchemaNode instanceof DataNodeContainer)) {
92 LOG.warn("Root {} resolves to non-container type {}, not upgrading", getRootPath(), rootSchemaNode);
96 final ModificationApplyOperation rootNode = getOperation(rootSchemaNode);
97 DataTreeState currentState;
98 DataTreeState newState;
100 currentState = state;
101 newState = currentState.withSchemaContext(newSchemaContext, rootNode);
102 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
106 public InMemoryDataTreeSnapshot takeSnapshot() {
107 return state.newSnapshot();
111 public void commit(final DataTreeCandidate candidate) {
112 if (candidate instanceof NoopDataTreeCandidate) {
115 checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s",
116 candidate.getClass());
117 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
119 if (LOG.isTraceEnabled()) {
120 LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
123 final TreeNode newRoot = c.getTipRoot();
124 DataTreeState currentState;
125 DataTreeState newState;
127 currentState = state;
128 final TreeNode currentRoot = currentState.getRoot();
129 LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
131 final TreeNode oldRoot = c.getBeforeRoot();
132 if (oldRoot != currentRoot) {
133 final String oldStr = simpleToString(oldRoot);
134 final String currentStr = simpleToString(currentRoot);
135 throw new IllegalStateException("Store tree " + currentStr + " and candidate base " + oldStr
139 newState = currentState.withRoot(newRoot);
140 LOG.trace("Updated state from {} to {}", currentState, newState);
141 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
144 private static String simpleToString(final Object obj) {
145 return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode());
149 public YangInstanceIdentifier getRootPath() {
150 return treeConfig.getRootPath();
154 public String toString() {
155 return MoreObjects.toStringHelper(this)
156 .add("object", super.toString())
157 .add("config", treeConfig)
164 protected TreeNode getTipRoot() {
165 return state.getRoot();