2 * Copyright (c) 2016 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;
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ArrayListMultimap;
14 import com.google.common.collect.Multimap;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Deque;
19 import java.util.List;
20 import java.util.Map.Entry;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.yangtools.odlext.model.api.YangModeledAnyxmlSchemaNode;
23 import org.opendaylight.yangtools.rfc7952.data.api.StreamWriterMetadataExtension;
24 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
25 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
29 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
38 * Utility class used for tracking parser state as needed by a StAX-like parser.
39 * This class is to be used only by respective XML and JSON parsers in yang-data-codec-xml and yang-data-codec-gson.
42 * Represents a node which is composed of multiple simpler nodes.
44 public class CompositeNodeDataWithSchema<T extends DataSchemaNode> extends AbstractNodeDataWithSchema<T> {
46 * Policy on how child nodes should be treated when an attempt is made to add them multiple times.
49 public enum ChildReusePolicy {
51 * Do not consider any existing nodes at all, just perform a straight append. Multiple occurrences of a child
52 * will result in multiple children being emitted. This is almost certainly the wrong policy unless the caller
53 * prevents such a situation from arising via some different mechanism.
57 * Do not allow duplicate definition of a child node. This would typically be used when a child cannot be
58 * encountered multiple times, but the caller does not make any provision to detect such a conflict. If a child
59 * node would end up being defined a second time, {@link DuplicateChildNodeRejectedException} is reported.
63 AbstractNodeDataWithSchema<?> appendChild(final Collection<AbstractNodeDataWithSchema<?>> view,
64 final AbstractNodeDataWithSchema<?> newChild) {
65 final DataSchemaNode childSchema = newChild.getSchema();
66 final AbstractNodeDataWithSchema<?> existing = findExistingChild(view, childSchema);
67 if (existing != null) {
68 throw new DuplicateChildNodeRejectedException("Duplicate child " + childSchema.getQName());
70 return super.appendChild(view, newChild);
74 * Reuse previously-defined child node. This is most appropriate when a child may be visited multiple times
75 * and the intent is to append content of each visit. A typical usage is list elements with RFC7950 XML
76 * encoding, where there is no encapsulating element and hence list entries may be interleaved with other
81 AbstractNodeDataWithSchema<?> appendChild(final Collection<AbstractNodeDataWithSchema<?>> view,
82 final AbstractNodeDataWithSchema<?> newChild) {
83 final AbstractNodeDataWithSchema<?> existing = findExistingChild(view, newChild.getSchema());
84 return existing != null ? existing : super.appendChild(view, newChild);
88 AbstractNodeDataWithSchema<?> appendChild(final Collection<AbstractNodeDataWithSchema<?>> view,
89 final AbstractNodeDataWithSchema<?> newChild) {
94 static @Nullable AbstractNodeDataWithSchema<?> findExistingChild(
95 final Collection<AbstractNodeDataWithSchema<?>> view, final DataSchemaNode childSchema) {
96 for (AbstractNodeDataWithSchema<?> existing : view) {
97 if (childSchema.equals(existing.getSchema())) {
106 * nodes which were added to schema via augmentation and are present in data input.
108 private final Multimap<AugmentationSchemaNode, AbstractNodeDataWithSchema<?>> augmentationsToChild =
109 ArrayListMultimap.create();
112 * remaining data nodes (which aren't added via augment). Every of one them should have the same QName.
114 private final List<AbstractNodeDataWithSchema<?>> children = new ArrayList<>();
116 public CompositeNodeDataWithSchema(final T schema) {
120 void addChild(final AbstractNodeDataWithSchema<?> newChild) {
121 children.add(newChild);
124 public final AbstractNodeDataWithSchema<?> addChild(final Deque<DataSchemaNode> schemas,
125 final ChildReusePolicy policy) {
126 checkArgument(!schemas.isEmpty(), "Expecting at least one schema");
128 // Pop the first node...
129 final DataSchemaNode schema = schemas.pop();
130 if (schemas.isEmpty()) {
131 // Simple, direct node
132 return addChild(schema, policy);
135 // The choice/case mess, reuse what we already popped
136 final DataSchemaNode choiceCandidate = schema;
137 checkArgument(choiceCandidate instanceof ChoiceSchemaNode, "Expected node of type ChoiceNode but was %s",
138 choiceCandidate.getClass());
139 final ChoiceSchemaNode choiceNode = (ChoiceSchemaNode) choiceCandidate;
141 final DataSchemaNode caseCandidate = schemas.pop();
142 checkArgument(caseCandidate instanceof CaseSchemaNode, "Expected node of type ChoiceCaseNode but was %s",
143 caseCandidate.getClass());
144 final CaseSchemaNode caseNode = (CaseSchemaNode) caseCandidate;
146 final AugmentationSchemaNode augSchema;
147 if (choiceCandidate.isAugmenting()) {
148 augSchema = findCorrespondingAugment(getSchema(), choiceCandidate);
153 // looking for existing choice
154 final Collection<AbstractNodeDataWithSchema<?>> childNodes;
155 if (augSchema != null) {
156 childNodes = augmentationsToChild.get(augSchema);
158 childNodes = children;
161 CompositeNodeDataWithSchema<?> caseNodeDataWithSchema = findChoice(childNodes, choiceCandidate, caseCandidate);
162 if (caseNodeDataWithSchema == null) {
163 ChoiceNodeDataWithSchema choiceNodeDataWithSchema = new ChoiceNodeDataWithSchema(choiceNode);
164 childNodes.add(choiceNodeDataWithSchema);
165 caseNodeDataWithSchema = choiceNodeDataWithSchema.addCompositeChild(caseNode, ChildReusePolicy.NOOP);
168 return caseNodeDataWithSchema.addChild(schemas, policy);
171 private AbstractNodeDataWithSchema<?> addChild(final DataSchemaNode schema, final ChildReusePolicy policy) {
172 AbstractNodeDataWithSchema<?> newChild = addSimpleChild(schema, policy);
173 return newChild == null ? addCompositeChild(schema, policy) : newChild;
176 private AbstractNodeDataWithSchema<?> addSimpleChild(final DataSchemaNode schema, final ChildReusePolicy policy) {
177 final SimpleNodeDataWithSchema<?> newChild;
178 if (schema instanceof LeafSchemaNode) {
179 newChild = new LeafNodeDataWithSchema((LeafSchemaNode) schema);
180 } else if (schema instanceof AnyxmlSchemaNode) {
181 // YangModeledAnyxmlSchemaNode is handled by addCompositeChild method.
182 if (schema instanceof YangModeledAnyxmlSchemaNode) {
185 newChild = new AnyXmlNodeDataWithSchema((AnyxmlSchemaNode) schema);
186 } else if (schema instanceof AnydataSchemaNode) {
187 newChild = new AnydataNodeDataWithSchema((AnydataSchemaNode) schema);
192 final AugmentationSchemaNode augSchema;
193 if (schema.isAugmenting()) {
194 augSchema = findCorrespondingAugment(getSchema(), schema);
199 // FIXME: 6.0.0: use policy once we have removed addChild() visibility
201 if (augSchema != null) {
202 augmentationsToChild.put(augSchema, newChild);
209 private static CaseNodeDataWithSchema findChoice(final Collection<AbstractNodeDataWithSchema<?>> childNodes,
210 final DataSchemaNode choiceCandidate, final DataSchemaNode caseCandidate) {
211 if (childNodes != null) {
212 for (AbstractNodeDataWithSchema<?> nodeDataWithSchema : childNodes) {
213 if (nodeDataWithSchema instanceof ChoiceNodeDataWithSchema
214 && nodeDataWithSchema.getSchema().getQName().equals(choiceCandidate.getQName())) {
215 CaseNodeDataWithSchema casePrevious = ((ChoiceNodeDataWithSchema) nodeDataWithSchema).getCase();
217 checkArgument(casePrevious.getSchema().getQName().equals(caseCandidate.getQName()),
218 "Data from case %s are specified but other data from case %s were specified earlier."
219 + " Data aren't from the same case.", caseCandidate.getQName(),
220 casePrevious.getSchema().getQName());
229 AbstractNodeDataWithSchema<?> addCompositeChild(final DataSchemaNode schema, final ChildReusePolicy policy) {
230 final CompositeNodeDataWithSchema<?> newChild;
232 if (schema instanceof ListSchemaNode) {
233 newChild = new ListNodeDataWithSchema((ListSchemaNode) schema);
234 } else if (schema instanceof LeafListSchemaNode) {
235 newChild = new LeafListNodeDataWithSchema((LeafListSchemaNode) schema);
236 } else if (schema instanceof ContainerLike) {
237 newChild = new ContainerNodeDataWithSchema((ContainerLike) schema);
238 } else if (schema instanceof YangModeledAnyxmlSchemaNode) {
239 newChild = new YangModeledAnyXmlNodeDataWithSchema((YangModeledAnyxmlSchemaNode)schema);
241 newChild = new CompositeNodeDataWithSchema<>(schema);
244 return addCompositeChild(newChild, policy);
247 final AbstractNodeDataWithSchema<?> addCompositeChild(final CompositeNodeDataWithSchema<?> newChild,
248 final ChildReusePolicy policy) {
249 final AugmentationSchemaNode augSchema = findCorrespondingAugment(getSchema(), newChild.getSchema());
250 final Collection<AbstractNodeDataWithSchema<?>> view = augSchema == null ? children
251 : augmentationsToChild.get(augSchema);
253 return policy.appendChild(view, newChild);
257 * Return a hint about how may children we are going to generate.
258 * @return Size of currently-present node list.
260 protected final int childSizeHint() {
261 return children.size();
265 public void write(final NormalizedNodeStreamWriter writer, final StreamWriterMetadataExtension metaWriter)
267 for (AbstractNodeDataWithSchema<?> child : children) {
268 child.write(writer, metaWriter);
270 for (Entry<AugmentationSchemaNode, Collection<AbstractNodeDataWithSchema<?>>> augmentationToChild
271 : augmentationsToChild.asMap().entrySet()) {
272 final Collection<AbstractNodeDataWithSchema<?>> childsFromAgumentation = augmentationToChild.getValue();
273 if (!childsFromAgumentation.isEmpty()) {
274 // FIXME: can we get the augmentation schema?
275 writer.startAugmentationNode(DataSchemaContextNode.augmentationIdentifierFrom(
276 augmentationToChild.getKey()));
278 for (AbstractNodeDataWithSchema<?> nodeDataWithSchema : childsFromAgumentation) {
279 nodeDataWithSchema.write(writer, metaWriter);
288 * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
289 * node is found then it is returned, else null.
291 * @param parent parent node
292 * @param child child node
293 * @return augmentation schema
295 private static AugmentationSchemaNode findCorrespondingAugment(final DataSchemaNode parent,
296 final DataSchemaNode child) {
297 if (parent instanceof AugmentationTarget && !(parent instanceof ChoiceSchemaNode)) {
298 for (AugmentationSchemaNode augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
299 DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
300 if (childInAugmentation != null) {