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;
40 * Current data store state generation.
42 private volatile DataTreeState state;
44 InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
45 final SchemaContext schemaContext) {
46 this.treeConfig = Preconditions.checkNotNull(treeConfig, "treeConfig");
47 state = DataTreeState.createInitial(rootNode);
48 if (schemaContext != null) {
49 setSchemaContext(schemaContext);
54 * This method is synchronized to guard against user attempting to install
55 * multiple contexts. Otherwise it runs in a lock-free manner.
58 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
59 Preconditions.checkNotNull(newSchemaContext);
61 LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
63 final DataSchemaContextTree contextTree = DataSchemaContextTree.from(newSchemaContext);
64 final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(getRootPath());
65 if (rootContextNode == null) {
66 LOG.warn("Could not find root {} in new schema context, not upgrading", getRootPath());
70 final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
71 if (!(rootSchemaNode instanceof DataNodeContainer)) {
72 LOG.warn("Root {} resolves to non-container type {}, not upgrading", getRootPath(), rootSchemaNode);
76 final ModificationApplyOperation rootNode;
77 if (rootSchemaNode instanceof ContainerSchemaNode) {
78 // FIXME: real root needs to enfore presence, but that require pre-population
79 rootNode = new ContainerModificationStrategy((ContainerSchemaNode) rootSchemaNode, treeConfig);
81 rootNode = SchemaAwareApplyOperation.from(rootSchemaNode, treeConfig);
84 DataTreeState currentState, newState;
87 newState = currentState.withSchemaContext(newSchemaContext, rootNode);
88 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
92 public InMemoryDataTreeSnapshot takeSnapshot() {
93 return state.newSnapshot();
97 public void commit(final DataTreeCandidate candidate) {
98 if (candidate instanceof NoopDataTreeCandidate) {
101 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
102 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
104 if (LOG.isTraceEnabled()) {
105 LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
108 final TreeNode newRoot = c.getTipRoot();
109 DataTreeState currentState, newState;
111 currentState = state;
112 final TreeNode currentRoot = currentState.getRoot();
113 LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
115 final TreeNode oldRoot = c.getBeforeRoot();
116 if (oldRoot != currentRoot) {
117 final String oldStr = simpleToString(oldRoot);
118 final String currentStr = simpleToString(currentRoot);
119 throw new IllegalStateException("Store tree " + currentStr + " and candidate base " + oldStr + " differ.");
122 newState = currentState.withRoot(newRoot);
123 LOG.trace("Updated state from {} to {}", currentState, newState);
124 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
127 private static String simpleToString(final Object o) {
128 return o.getClass().getName() + "@" + Integer.toHexString(o.hashCode());
132 public YangInstanceIdentifier getRootPath() {
133 return treeConfig.getRootPath();
137 public String toString() {
138 return MoreObjects.toStringHelper(this).
139 add("object", super.toString()).
140 add("config", treeConfig).
147 protected TreeNode getTipRoot() {
148 return state.getRoot();