51c12bc4a7394cfc807d4e5642d9ffea61677583
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / InMemoryDataTree.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
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;
28
29 /**
30  * Read-only snapshot of the data tree.
31  */
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);
36
37     private final DataTreeConfiguration treeConfig;
38
39     /**
40      * Current data store state generation.
41      */
42     private volatile DataTreeState state;
43
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);
50         }
51     }
52
53     /*
54      * This method is synchronized to guard against user attempting to install
55      * multiple contexts. Otherwise it runs in a lock-free manner.
56      */
57     @Override
58     public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
59         Preconditions.checkNotNull(newSchemaContext);
60
61         LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
62
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());
67             return;
68         }
69
70         final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
71         if (!(rootSchemaNode instanceof DataNodeContainer)) {
72             LOG.warn("Root {} resolves to non-container type {}, not upgrading", getRootPath(), rootSchemaNode);
73             return;
74         }
75
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);
80         } else {
81             rootNode = SchemaAwareApplyOperation.from(rootSchemaNode, treeConfig);
82         }
83
84         DataTreeState currentState, newState;
85         do {
86             currentState = state;
87             newState = currentState.withSchemaContext(newSchemaContext, rootNode);
88         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
89     }
90
91     @Override
92     public InMemoryDataTreeSnapshot takeSnapshot() {
93         return state.newSnapshot();
94     }
95
96     @Override
97     public void commit(final DataTreeCandidate candidate) {
98         if (candidate instanceof NoopDataTreeCandidate) {
99             return;
100         }
101         Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
102         final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
103
104         if (LOG.isTraceEnabled()) {
105             LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
106         }
107
108         final TreeNode newRoot = c.getTipRoot();
109         DataTreeState currentState, newState;
110         do {
111             currentState = state;
112             final TreeNode currentRoot = currentState.getRoot();
113             LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
114
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.");
120             }
121
122             newState = currentState.withRoot(newRoot);
123             LOG.trace("Updated state from {} to {}", currentState, newState);
124         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
125     }
126
127     private static String simpleToString(final Object o) {
128         return o.getClass().getName() + "@" + Integer.toHexString(o.hashCode());
129     }
130
131     @Override
132     public YangInstanceIdentifier getRootPath() {
133         return treeConfig.getRootPath();
134     }
135
136     @Override
137     public String toString() {
138         return MoreObjects.toStringHelper(this).
139                 add("object", super.toString()).
140                 add("config", treeConfig).
141                 add("state", state).
142                 toString();
143     }
144
145     @Override
146     @Nonnull
147     protected TreeNode getTipRoot() {
148         return state.getRoot();
149     }
150 }