043cdc2ba1b2f640183de7ea0a289ca73d18a8f9
[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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.MoreObjects;
14 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
22 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
23 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
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.SchemaContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Read-only snapshot of the data tree.
33  */
34 final class InMemoryDataTree extends AbstractDataTreeTip implements DataTree {
35     private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
36             AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
37     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
38
39     private final DataTreeConfiguration treeConfig;
40     private final boolean maskMandatory;
41
42     /**
43      * Current data store state generation.
44      */
45     private volatile DataTreeState state;
46
47     InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
48         final SchemaContext schemaContext) {
49         this.treeConfig = requireNonNull(treeConfig, "treeConfig");
50         maskMandatory = true;
51         state = DataTreeState.createInitial(rootNode);
52         if (schemaContext != null) {
53             setSchemaContext(schemaContext);
54         }
55     }
56
57     InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
58             final SchemaContext schemaContext, final DataSchemaNode rootSchemaNode, final boolean maskMandatory) {
59         this.treeConfig = requireNonNull(treeConfig, "treeConfig");
60         this.maskMandatory = maskMandatory;
61
62         state = DataTreeState.createInitial(rootNode).withSchemaContext(schemaContext, getOperation(rootSchemaNode));
63     }
64
65     private ModificationApplyOperation getOperation(final DataSchemaNode rootSchemaNode) {
66         if (maskMandatory && rootSchemaNode instanceof ContainerSchemaNode) {
67             return new ContainerModificationStrategy((ContainerSchemaNode) rootSchemaNode, treeConfig);
68         }
69
70         return SchemaAwareApplyOperation.from(rootSchemaNode, treeConfig);
71     }
72
73     /*
74      * This method is synchronized to guard against user attempting to install
75      * multiple contexts. Otherwise it runs in a lock-free manner.
76      */
77     @Override
78     public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
79         requireNonNull(newSchemaContext);
80
81         LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
82
83         final DataSchemaContextTree contextTree = DataSchemaContextTree.from(newSchemaContext);
84         final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(getRootPath());
85         if (rootContextNode == null) {
86             LOG.warn("Could not find root {} in new schema context, not upgrading", getRootPath());
87             return;
88         }
89
90         final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
91         if (!(rootSchemaNode instanceof DataNodeContainer)) {
92             LOG.warn("Root {} resolves to non-container type {}, not upgrading", getRootPath(), rootSchemaNode);
93             return;
94         }
95
96         final ModificationApplyOperation rootNode = getOperation(rootSchemaNode);
97         DataTreeState currentState;
98         DataTreeState newState;
99         do {
100             currentState = state;
101             newState = currentState.withSchemaContext(newSchemaContext, rootNode);
102         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
103     }
104
105     @Override
106     public InMemoryDataTreeSnapshot takeSnapshot() {
107         return state.newSnapshot();
108     }
109
110     @Override
111     public void commit(final DataTreeCandidate candidate) {
112         if (candidate instanceof NoopDataTreeCandidate) {
113             return;
114         }
115         checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s",
116             candidate.getClass());
117         final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
118
119         if (LOG.isTraceEnabled()) {
120             LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
121         }
122
123         final TreeNode newRoot = c.getTipRoot();
124         DataTreeState currentState;
125         DataTreeState newState;
126         do {
127             currentState = state;
128             final TreeNode currentRoot = currentState.getRoot();
129             LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
130
131             final TreeNode oldRoot = c.getBeforeRoot();
132             if (oldRoot != currentRoot) {
133                 final String oldStr = simpleToString(oldRoot);
134                 final String currentStr = simpleToString(currentRoot);
135                 throw new IllegalStateException("Store tree " + currentStr + " and candidate base " + oldStr
136                     + " differ.");
137             }
138
139             newState = currentState.withRoot(newRoot);
140             LOG.trace("Updated state from {} to {}", currentState, newState);
141         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
142     }
143
144     private static String simpleToString(final Object obj) {
145         return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode());
146     }
147
148     @Override
149     public YangInstanceIdentifier getRootPath() {
150         return treeConfig.getRootPath();
151     }
152
153     @Override
154     public String toString() {
155         return MoreObjects.toStringHelper(this)
156                 .add("object", super.toString())
157                 .add("config", treeConfig)
158                 .add("state", state)
159                 .toString();
160     }
161
162     @Override
163     @Nonnull
164     protected TreeNode getTipRoot() {
165         return state.getRoot();
166     }
167 }