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.tree.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.VarHandle;
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.tree.api.DataTree;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
22 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
23 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
24 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
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.EffectiveModelContext;
28 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Read-only snapshot of the data tree.
35 public final class InMemoryDataTree extends AbstractDataTreeTip implements DataTree {
36 private static final VarHandle STATE;
40 STATE = MethodHandles.lookup().findVarHandle(InMemoryDataTree.class, "state", DataTreeState.class);
41 } catch (NoSuchFieldException | IllegalAccessException e) {
42 throw new ExceptionInInitializerError(e);
46 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
48 private final DataTreeConfiguration treeConfig;
49 private final boolean maskMandatory;
52 * Current data store state generation. All accesses need to go through {@link #STATE}
54 @SuppressWarnings("unused")
55 private volatile DataTreeState state;
57 public InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
58 final EffectiveModelContext schemaContext) {
59 this.treeConfig = requireNonNull(treeConfig, "treeConfig");
61 state = DataTreeState.createInitial(rootNode);
62 if (schemaContext != null) {
63 setEffectiveModelContext(schemaContext);
67 public InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
68 final EffectiveModelContext schemaContext, final DataSchemaNode rootSchemaNode,
69 final boolean maskMandatory) {
70 this.treeConfig = requireNonNull(treeConfig, "treeConfig");
71 this.maskMandatory = maskMandatory;
73 state = DataTreeState.createInitial(rootNode).withSchemaContext(schemaContext, getOperation(rootSchemaNode));
76 private ModificationApplyOperation getOperation(final DataSchemaNode rootSchemaNode) {
77 if (rootSchemaNode instanceof ContainerLike rootContainerLike && maskMandatory) {
78 return new ContainerModificationStrategy(rootContainerLike, treeConfig);
80 if (rootSchemaNode instanceof ListSchemaNode rootList) {
81 final PathArgument arg = treeConfig.getRootPath().getLastPathArgument();
82 if (arg instanceof NodeIdentifierWithPredicates) {
83 return maskMandatory ? new MapEntryModificationStrategy(rootList, treeConfig)
84 : MapEntryModificationStrategy.of(rootList, treeConfig);
89 return SchemaAwareApplyOperation.from(rootSchemaNode, treeConfig);
90 } catch (ExcludedDataSchemaNodeException e) {
91 throw new IllegalArgumentException("Root node does not belong current data tree", e);
96 public void setEffectiveModelContext(final EffectiveModelContext newModelContext) {
97 internalSetSchemaContext(newModelContext);
101 * This method is synchronized to guard against user attempting to install
102 * multiple contexts. Otherwise it runs in a lock-free manner.
104 private synchronized void internalSetSchemaContext(final EffectiveModelContext newSchemaContext) {
105 requireNonNull(newSchemaContext);
107 LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
109 final var contextTree = DataSchemaContextTree.from(newSchemaContext);
110 final var rootContextNode = contextTree.childByPath(getRootPath());
111 if (rootContextNode == null) {
112 LOG.warn("Could not find root {} in new schema context, not upgrading", getRootPath());
116 final var rootSchemaNode = rootContextNode.dataSchemaNode();
117 if (!(rootSchemaNode instanceof DataNodeContainer)) {
118 LOG.warn("Root {} resolves to non-container type {}, not upgrading", getRootPath(), rootSchemaNode);
122 final var rootNode = getOperation(rootSchemaNode);
123 DataTreeState currentState;
124 DataTreeState newState;
126 currentState = currentState();
127 newState = currentState.withSchemaContext(newSchemaContext, rootNode);
128 // TODO: can we lower this to compareAndSwapRelease?
129 } while (!STATE.compareAndSet(this, currentState, newState));
133 public InMemoryDataTreeSnapshot takeSnapshot() {
134 return currentState().newSnapshot();
138 public void commit(final DataTreeCandidate candidate) {
139 if (candidate instanceof NoopDataTreeCandidate) {
142 if (!(candidate instanceof InMemoryDataTreeCandidate c)) {
143 throw new IllegalArgumentException("Invalid candidate class " + candidate.getClass());
146 if (LOG.isTraceEnabled()) {
147 LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
150 final TreeNode newRoot = c.getTipRoot();
151 DataTreeState currentState;
152 DataTreeState newState;
154 currentState = currentState();
155 final TreeNode currentRoot = currentState.getRoot();
156 LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
158 final TreeNode oldRoot = c.getBeforeRoot();
159 if (oldRoot != currentRoot) {
160 final String oldStr = simpleToString(oldRoot);
161 final String currentStr = simpleToString(currentRoot);
162 throw new IllegalStateException("Store tree " + currentStr + " and candidate base " + oldStr
166 newState = currentState.withRoot(newRoot);
167 LOG.trace("Updated state from {} to {}", currentState, newState);
168 // TODO: can we lower this to compareAndSwapRelease?
169 } while (!STATE.compareAndSet(this, currentState, newState));
172 private static String simpleToString(final Object obj) {
173 return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode());
176 private DataTreeState currentState() {
177 return (DataTreeState) STATE.getAcquire(this);
181 public YangInstanceIdentifier getRootPath() {
182 return treeConfig.getRootPath();
186 public String toString() {
187 return MoreObjects.toStringHelper(this)
188 .add("object", super.toString())
189 .add("config", treeConfig)
190 .add("state", currentState())
195 protected TreeNode getTipRoot() {
196 return currentState().getRoot();