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