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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
23 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
24 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Read-only snapshot of the data tree.
36 final class InMemoryDataTree extends AbstractDataTreeTip implements DataTree {
37 private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
38 AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
39 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
41 private final DataTreeConfiguration treeConfig;
42 private final boolean maskMandatory;
45 * Current data store state generation.
47 private volatile DataTreeState state;
49 InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
50 final SchemaContext schemaContext) {
51 this.treeConfig = requireNonNull(treeConfig, "treeConfig");
53 state = DataTreeState.createInitial(rootNode);
54 if (schemaContext != null) {
55 setSchemaContext(schemaContext);
59 InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
60 final SchemaContext schemaContext, final DataSchemaNode rootSchemaNode, final boolean maskMandatory) {
61 this.treeConfig = requireNonNull(treeConfig, "treeConfig");
62 this.maskMandatory = maskMandatory;
64 state = DataTreeState.createInitial(rootNode).withSchemaContext(schemaContext, getOperation(rootSchemaNode));
67 private ModificationApplyOperation getOperation(final DataSchemaNode rootSchemaNode) {
68 if (rootSchemaNode instanceof ContainerSchemaNode && maskMandatory) {
69 return new ContainerModificationStrategy((ContainerSchemaNode) rootSchemaNode, treeConfig);
71 if (rootSchemaNode instanceof ListSchemaNode) {
72 final PathArgument arg = treeConfig.getRootPath().getLastPathArgument();
73 if (arg instanceof NodeIdentifierWithPredicates) {
74 return maskMandatory ? new ListEntryModificationStrategy((ListSchemaNode) rootSchemaNode, treeConfig)
75 : ListEntryModificationStrategy.of((ListSchemaNode) rootSchemaNode, treeConfig);
79 return SchemaAwareApplyOperation.from(rootSchemaNode, treeConfig);
83 * This method is synchronized to guard against user attempting to install
84 * multiple contexts. Otherwise it runs in a lock-free manner.
87 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
88 requireNonNull(newSchemaContext);
90 LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
92 final DataSchemaContextTree contextTree = DataSchemaContextTree.from(newSchemaContext);
93 final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(getRootPath());
94 if (rootContextNode == null) {
95 LOG.warn("Could not find root {} in new schema context, not upgrading", getRootPath());
99 final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
100 if (!(rootSchemaNode instanceof DataNodeContainer)) {
101 LOG.warn("Root {} resolves to non-container type {}, not upgrading", getRootPath(), rootSchemaNode);
105 final ModificationApplyOperation rootNode = getOperation(rootSchemaNode);
106 DataTreeState currentState;
107 DataTreeState newState;
109 currentState = state;
110 newState = currentState.withSchemaContext(newSchemaContext, rootNode);
111 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
115 public InMemoryDataTreeSnapshot takeSnapshot() {
116 return state.newSnapshot();
120 public void commit(final DataTreeCandidate candidate) {
121 if (candidate instanceof NoopDataTreeCandidate) {
124 checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s",
125 candidate.getClass());
126 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
128 if (LOG.isTraceEnabled()) {
129 LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
132 final TreeNode newRoot = c.getTipRoot();
133 DataTreeState currentState;
134 DataTreeState newState;
136 currentState = state;
137 final TreeNode currentRoot = currentState.getRoot();
138 LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
140 final TreeNode oldRoot = c.getBeforeRoot();
141 if (oldRoot != currentRoot) {
142 final String oldStr = simpleToString(oldRoot);
143 final String currentStr = simpleToString(currentRoot);
144 throw new IllegalStateException("Store tree " + currentStr + " and candidate base " + oldStr
148 newState = currentState.withRoot(newRoot);
149 LOG.trace("Updated state from {} to {}", currentState, newState);
150 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
153 private static String simpleToString(final Object obj) {
154 return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode());
158 public YangInstanceIdentifier getRootPath() {
159 return treeConfig.getRootPath();
163 public String toString() {
164 return MoreObjects.toStringHelper(this)
165 .add("object", super.toString())
166 .add("config", treeConfig)
172 protected TreeNode getTipRoot() {
173 return state.getRoot();