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.DataTreeConfiguration;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
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 DataTreeConfiguration treeConfig;
38 private final boolean maskMandatory;
41 * Current data store state generation.
43 private volatile DataTreeState state;
45 InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
46 final SchemaContext schemaContext) {
47 this.treeConfig = Preconditions.checkNotNull(treeConfig, "treeConfig");
49 state = DataTreeState.createInitial(rootNode);
50 if (schemaContext != null) {
51 setSchemaContext(schemaContext);
55 InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig, final SchemaContext schemaContext,
56 final DataSchemaNode rootSchemaNode, final boolean maskMandatory) {
57 this.treeConfig = Preconditions.checkNotNull(treeConfig, "treeConfig");
58 this.maskMandatory = maskMandatory;
60 state = DataTreeState.createInitial(rootNode).withSchemaContext(schemaContext, getOperation(rootSchemaNode));
63 private ModificationApplyOperation getOperation(final DataSchemaNode rootSchemaNode) {
64 if (maskMandatory && rootSchemaNode instanceof ContainerSchemaNode) {
65 return new ContainerModificationStrategy((ContainerSchemaNode) rootSchemaNode, treeConfig);
68 return SchemaAwareApplyOperation.from(rootSchemaNode, treeConfig);
72 * This method is synchronized to guard against user attempting to install
73 * multiple contexts. Otherwise it runs in a lock-free manner.
76 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
77 Preconditions.checkNotNull(newSchemaContext);
79 LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
81 final DataSchemaContextTree contextTree = DataSchemaContextTree.from(newSchemaContext);
82 final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(getRootPath());
83 if (rootContextNode == null) {
84 LOG.warn("Could not find root {} in new schema context, not upgrading", getRootPath());
88 final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
89 if (!(rootSchemaNode instanceof DataNodeContainer)) {
90 LOG.warn("Root {} resolves to non-container type {}, not upgrading", getRootPath(), rootSchemaNode);
94 final ModificationApplyOperation rootNode = getOperation(rootSchemaNode);
95 DataTreeState currentState, newState;
98 newState = currentState.withSchemaContext(newSchemaContext, rootNode);
99 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
103 public InMemoryDataTreeSnapshot takeSnapshot() {
104 return state.newSnapshot();
108 public void commit(final DataTreeCandidate candidate) {
109 if (candidate instanceof NoopDataTreeCandidate) {
112 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
113 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
115 if (LOG.isTraceEnabled()) {
116 LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
119 final TreeNode newRoot = c.getTipRoot();
120 DataTreeState currentState, newState;
122 currentState = state;
123 final TreeNode currentRoot = currentState.getRoot();
124 LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
126 final TreeNode oldRoot = c.getBeforeRoot();
127 if (oldRoot != currentRoot) {
128 final String oldStr = simpleToString(oldRoot);
129 final String currentStr = simpleToString(currentRoot);
130 throw new IllegalStateException("Store tree " + currentStr + " and candidate base " + oldStr + " differ.");
133 newState = currentState.withRoot(newRoot);
134 LOG.trace("Updated state from {} to {}", currentState, newState);
135 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
138 private static String simpleToString(final Object o) {
139 return o.getClass().getName() + "@" + Integer.toHexString(o.hashCode());
143 public YangInstanceIdentifier getRootPath() {
144 return treeConfig.getRootPath();
148 public String toString() {
149 return MoreObjects.toStringHelper(this).
150 add("object", super.toString()).
151 add("config", treeConfig).
158 protected TreeNode getTipRoot() {
159 return state.getRoot();