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.util;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.annotations.Beta;
15 import com.google.common.collect.Iterables;
16 import java.io.IOException;
17 import java.util.ArrayDeque;
18 import java.util.Deque;
19 import java.util.Optional;
21 import java.util.stream.Collectors;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.yangtools.concepts.Mutable;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
31 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
35 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
38 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
41 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
42 import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
43 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
47 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
49 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
50 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
51 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
52 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement;
53 import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
54 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
55 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
56 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
57 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
62 * Utility class for tracking schema state underlying a {@link NormalizedNode} structure.
65 public final class NormalizedNodeInferenceStack implements Mutable {
66 private static final Logger LOG = LoggerFactory.getLogger(NormalizedNodeInferenceStack.class);
68 private final Deque<WithStatus> schemaStack = new ArrayDeque<>();
69 private final SchemaInferenceStack dataTree;
70 private final DataNodeContainer root;
72 private NormalizedNodeInferenceStack(final EffectiveModelContext context) {
73 root = requireNonNull(context);
74 dataTree = SchemaInferenceStack.of(context);
77 private NormalizedNodeInferenceStack(final SchemaInferenceStack dataTree) {
78 this.dataTree = requireNonNull(dataTree);
79 if (!dataTree.isEmpty()) {
80 final EffectiveStatement<?, ?> current = dataTree.currentStatement();
81 checkArgument(current instanceof DataNodeContainer, "Cannot instantiate on %s", current);
83 root = (DataNodeContainer) current;
85 root = dataTree.getEffectiveModelContext();
90 * Create a new writer with the specified inference state as its root.
92 * @param root Root inference state
93 * @return A new {@link NormalizedNodeStreamWriter}
94 * @throws NullPointerException if {@code root} is null
96 public static @NonNull NormalizedNodeInferenceStack of(final EffectiveStatementInference root) {
97 return new NormalizedNodeInferenceStack(SchemaInferenceStack.ofInference(root));
101 * Create a new writer with the specified inference state as its root.
103 * @param root Root inference state
104 * @return A new {@link NormalizedNodeStreamWriter}
105 * @throws NullPointerException if {@code root} is null
107 public static @NonNull NormalizedNodeInferenceStack of(final Inference root) {
108 return new NormalizedNodeInferenceStack(root.toSchemaInferenceStack());
112 * Create a new writer at the root of specified {@link EffectiveModelContext}.
114 * @param context effective model context
115 * @return A new {@link NormalizedNodeStreamWriter}
116 * @throws NullPointerException if {@code context} is null
118 public static @NonNull NormalizedNodeInferenceStack of(final EffectiveModelContext context) {
119 return new NormalizedNodeInferenceStack(context);
123 * Create a new writer with the specified context and rooted in the specified schema path.
125 * @param context Associated {@link EffectiveModelContext}
126 * @param path schema path
127 * @return A new {@link NormalizedNodeInferenceStack}
128 * @throws NullPointerException if any argument is null
129 * @throws IllegalArgumentException if {@code path} does not point to a valid root
131 public static @NonNull NormalizedNodeInferenceStack of(final EffectiveModelContext context, final Absolute path) {
132 return new NormalizedNodeInferenceStack(SchemaInferenceStack.of(context, path));
136 * Create a new writer with the specified context and rooted in the specified schema path.
138 * @param context Associated {@link EffectiveModelContext}
139 * @param path schema path
140 * @return A new {@link NormalizedNodeInferenceStack}
141 * @throws NullPointerException if any argument is null
142 * @throws IllegalArgumentException if {@code path} does not point to a valid root
145 public static @NonNull NormalizedNodeInferenceStack of(final EffectiveModelContext context, final SchemaPath path) {
146 return new NormalizedNodeInferenceStack(SchemaInferenceStack.ofInstantiatedPath(context, path));
150 * Create a new writer with the specified context and rooted in the specified schema path.
152 * @param context Associated {@link EffectiveModelContext}
153 * @param operation Operation schema path
154 * @param qname Input/Output container QName
155 * @return A new {@link NormalizedNodeStreamWriter}
156 * @throws NullPointerException if any argument is null
157 * @throws IllegalArgumentException if {@code operation} does not point to an actual operation or if {@code qname}
158 * does not identify a valid root underneath it.
160 public static @NonNull NormalizedNodeInferenceStack ofOperation(final EffectiveModelContext context,
161 final Absolute operation, final QName qname) {
162 final SchemaInferenceStack stack = SchemaInferenceStack.of(context, operation);
163 final EffectiveStatement<?, ?> current = stack.currentStatement();
164 checkArgument(current instanceof RpcEffectiveStatement || current instanceof ActionEffectiveStatement,
165 "Path %s resolved into non-operation %s", operation, current);
166 stack.enterSchemaTree(qname);
167 return new NormalizedNodeInferenceStack(stack);
171 * Return a copy of this tracker's state as an {@link SchemaInferenceStack}.
173 * @return A SchemaInferenceStack
175 public @NonNull SchemaInferenceStack toSchemaInferenceStack() {
176 return dataTree.copy();
179 public Object getParent() {
180 final WithStatus schema = schemaStack.peek();
181 return schema == null ? root : schema;
184 private SchemaNode enterDataTree(final PathArgument name) {
185 final QName qname = name.getNodeType();
186 final DataTreeEffectiveStatement<?> stmt = dataTree.enterDataTree(qname);
187 verify(stmt instanceof SchemaNode, "Unexpected result %s", stmt);
188 final SchemaNode ret = (SchemaNode) stmt;
189 final Object parent = getParent();
190 if (parent instanceof ChoiceSchemaNode) {
191 final DataSchemaNode check = ((ChoiceSchemaNode) parent).findDataSchemaChild(qname).orElse(null);
192 verify(check == ret, "Data tree result %s does not match choice result %s", ret, check);
197 public void startList(final PathArgument name) {
198 final SchemaNode schema = enterDataTree(name);
199 checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema);
200 schemaStack.push(schema);
203 public void startListItem(final PathArgument name) throws IOException {
204 final Object schema = getParent();
205 checkArgument(schema instanceof ListSchemaNode, "List item is not appropriate");
206 schemaStack.push((ListSchemaNode) schema);
209 public void startLeafNode(final NodeIdentifier name) throws IOException {
210 final SchemaNode schema = enterDataTree(name);
211 checkArgument(schema instanceof LeafSchemaNode, "Node %s is not a leaf", schema);
212 schemaStack.push(schema);
215 public LeafListSchemaNode startLeafSet(final NodeIdentifier name) {
216 final SchemaNode schema = enterDataTree(name);
217 checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema);
218 schemaStack.push(schema);
219 return (LeafListSchemaNode) schema;
222 public LeafListSchemaNode leafSetEntryNode(final QName qname) {
223 final Object parent = getParent();
224 if (parent instanceof LeafListSchemaNode) {
225 return (LeafListSchemaNode) parent;
227 checkArgument(parent instanceof DataNodeContainer, "Cannot lookup %s in parent %s", qname, parent);
228 final DataSchemaNode child = ((DataNodeContainer) parent).dataChildByName(qname);
229 checkArgument(child instanceof LeafListSchemaNode,
230 "Node %s is neither a leaf-list nor currently in a leaf-list", child);
231 return (LeafListSchemaNode) child;
234 public void startLeafSetEntryNode(final NodeWithValue<?> name) {
235 schemaStack.push(leafSetEntryNode(name.getNodeType()));
238 public ChoiceSchemaNode startChoiceNode(final NodeIdentifier name) {
239 LOG.debug("Enter choice {}", name);
240 final ChoiceEffectiveStatement stmt = dataTree.enterChoice(name.getNodeType());
241 verify(stmt instanceof ChoiceSchemaNode, "Node %s is not a choice", stmt);
242 final ChoiceSchemaNode ret = (ChoiceSchemaNode) stmt;
243 schemaStack.push(ret);
247 public SchemaNode startContainerNode(final NodeIdentifier name) {
248 LOG.debug("Enter container {}", name);
249 final SchemaNode schema = enterDataTree(name);
250 checkArgument(schema instanceof ContainerLike || schema instanceof NotificationDefinition,
251 "Node %s is not a container nor a notification", schema);
252 schemaStack.push(schema);
256 public void startAnyxmlNode(final NodeIdentifier name) {
257 final SchemaNode schema = enterDataTree(name);
258 checkArgument(schema instanceof AnyxmlSchemaNode, "Node %s is not anyxml", schema);
259 schemaStack.push(schema);
262 public void startAnydataNode(final NodeIdentifier name) {
263 final SchemaNode schema = enterDataTree(name);
264 checkArgument(schema instanceof AnydataSchemaNode, "Node %s is not anydata", schema);
265 schemaStack.push(schema);
268 public Object endNode() {
269 final Object ret = schemaStack.pop();
270 // If this is a data tree node, make sure it is updated. Before that, though, we need to check if this is not
271 // actually listEntry -> list or leafListEntry -> leafList exit.
272 if (!(ret instanceof AugmentationSchemaNode) && getParent() != ret) {
278 public AugmentationSchemaNode startAugmentationNode(final AugmentationIdentifier identifier) {
279 LOG.debug("Enter augmentation {}", identifier);
280 Object parent = getParent();
282 checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
283 if (parent instanceof ChoiceSchemaNode) {
284 final QName name = Iterables.get(identifier.getPossibleChildNames(), 0);
285 parent = findCaseByChild((ChoiceSchemaNode) parent, name);
287 checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer", parent);
288 final AugmentationSchemaNode schema = findSchemaForAugment((AugmentationTarget) parent,
289 identifier.getPossibleChildNames());
290 final AugmentationSchemaNode resolvedSchema = EffectiveAugmentationSchema.create(schema,
291 (DataNodeContainer) parent);
292 schemaStack.push(resolvedSchema);
293 return resolvedSchema;
296 // FIXME: 7.0.0: can we get rid of this?
297 private static @NonNull AugmentationSchemaNode findSchemaForAugment(final AugmentationTarget schema,
298 final Set<QName> qnames) {
299 for (final AugmentationSchemaNode augment : schema.getAvailableAugmentations()) {
300 if (qnames.equals(augment.getChildNodes().stream()
301 .map(DataSchemaNode::getQName)
302 .collect(Collectors.toUnmodifiableSet()))) {
307 throw new IllegalStateException(
308 "Unknown augmentation node detected, identified by: " + qnames + ", in: " + schema);
311 // FIXME: 7.0.0: can we get rid of this?
312 private static SchemaNode findCaseByChild(final ChoiceSchemaNode parent, final QName qname) {
313 for (final CaseSchemaNode caze : parent.getCases()) {
314 final Optional<DataSchemaNode> potential = caze.findDataChildByName(qname);
315 if (potential.isPresent()) {