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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
19 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
20 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Read-only snapshot of the data tree.
31 final class InMemoryDataTree extends AbstractDataTreeTip implements TipProducingDataTree {
32 private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
33 AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
34 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
36 private final YangInstanceIdentifier rootPath;
37 private final TreeType treeType;
40 * Current data store state generation.
42 private volatile DataTreeState state;
44 public InMemoryDataTree(final TreeNode rootNode, final TreeType treeType, final YangInstanceIdentifier rootPath, final SchemaContext schemaContext) {
45 this.treeType = Preconditions.checkNotNull(treeType, "treeType");
46 this.rootPath = Preconditions.checkNotNull(rootPath, "rootPath");
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(rootPath);
65 if (rootContextNode == null) {
66 LOG.debug("Could not find root {} in new schema context, not upgrading", rootPath);
70 final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
71 if (!(rootSchemaNode instanceof DataNodeContainer)) {
72 LOG.warn("Root {} resolves to non-container type {}, not upgrading", rootPath, 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, treeType);
81 rootNode = SchemaAwareApplyOperation.from(rootSchemaNode, treeType);
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 Preconditions.checkState(oldRoot == currentRoot, "Store tree %s and candidate base %s differ.", currentRoot, oldRoot);
118 newState = currentState.withRoot(newRoot);
119 LOG.trace("Updated state from {} to {}", currentState, newState);
120 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
124 public YangInstanceIdentifier getRootPath() {
129 public String toString() {
130 return MoreObjects.toStringHelper(this).
131 add("object", super.toString()).
132 add("rootPath", rootPath).
138 protected TreeNode getTipRoot() {
139 return state.getRoot();