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 rootNode = new PresenceContainerModificationStrategy((ContainerSchemaNode) rootSchemaNode, treeType);
80 rootNode = SchemaAwareApplyOperation.from(rootSchemaNode, treeType);
83 DataTreeState currentState, newState;
86 newState = currentState.withSchemaContext(newSchemaContext, rootNode);
87 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
91 public InMemoryDataTreeSnapshot takeSnapshot() {
92 return state.newSnapshot();
96 public void commit(final DataTreeCandidate candidate) {
97 if (candidate instanceof NoopDataTreeCandidate) {
100 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
101 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
103 if (LOG.isTraceEnabled()) {
104 LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
107 final TreeNode newRoot = c.getTipRoot();
108 DataTreeState currentState, newState;
110 currentState = state;
111 final TreeNode currentRoot = currentState.getRoot();
112 LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
114 final TreeNode oldRoot = c.getBeforeRoot();
115 Preconditions.checkState(oldRoot == currentRoot, "Store tree %s and candidate base %s differ.", currentRoot, oldRoot);
117 newState = currentState.withRoot(newRoot);
118 LOG.trace("Updated state from {} to {}", currentState, newState);
119 } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
123 public YangInstanceIdentifier getRootPath() {
128 public String toString() {
129 return MoreObjects.toStringHelper(this).
130 add("object", super.toString()).
131 add("rootPath", rootPath).
137 protected TreeNode getTipRoot() {
138 return state.getRoot();