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.collect.ArrayListMultimap;
13 import com.google.common.collect.Multimap;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Deque;
18 import java.util.List;
19 import java.util.Map.Entry;
20 import org.opendaylight.yangtools.odlext.model.api.YangModeledAnyxmlSchemaNode;
21 import org.opendaylight.yangtools.rfc7952.data.api.StreamWriterMetadataExtension;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
23 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
27 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
36 * Utility class used for tracking parser state as needed by a StAX-like parser.
37 * This class is to be used only by respective XML and JSON parsers in yang-data-codec-xml and yang-data-codec-gson.
40 * Represents a node which is composed of multiple simpler nodes.
42 public class CompositeNodeDataWithSchema<T extends DataSchemaNode> extends AbstractNodeDataWithSchema<T> {
44 * nodes which were added to schema via augmentation and are present in data input.
46 private final Multimap<AugmentationSchemaNode, AbstractNodeDataWithSchema<?>> augmentationsToChild =
47 ArrayListMultimap.create();
50 * remaining data nodes (which aren't added via augment). Every of one them should have the same QName.
52 private final List<AbstractNodeDataWithSchema<?>> children = new ArrayList<>();
54 public CompositeNodeDataWithSchema(final T schema) {
58 private AbstractNodeDataWithSchema<?> addChild(final DataSchemaNode schema) {
59 AbstractNodeDataWithSchema<?> newChild = addSimpleChild(schema);
60 return newChild == null ? addCompositeChild(schema) : newChild;
64 public void addChild(final AbstractNodeDataWithSchema<?> newChild) {
65 children.add(newChild);
68 public AbstractNodeDataWithSchema<?> addChild(final Deque<DataSchemaNode> schemas) {
69 checkArgument(!schemas.isEmpty(), "Expecting at least one schema");
71 // Pop the first node...
72 final DataSchemaNode schema = schemas.pop();
73 if (schemas.isEmpty()) {
74 // Simple, direct node
75 return addChild(schema);
78 // The choice/case mess, reuse what we already popped
79 final DataSchemaNode choiceCandidate = schema;
80 checkArgument(choiceCandidate instanceof ChoiceSchemaNode, "Expected node of type ChoiceNode but was %s",
81 choiceCandidate.getClass());
82 final ChoiceSchemaNode choiceNode = (ChoiceSchemaNode) choiceCandidate;
84 final DataSchemaNode caseCandidate = schemas.pop();
85 checkArgument(caseCandidate instanceof CaseSchemaNode, "Expected node of type ChoiceCaseNode but was %s",
86 caseCandidate.getClass());
87 final CaseSchemaNode caseNode = (CaseSchemaNode) caseCandidate;
89 final AugmentationSchemaNode augSchema;
90 if (choiceCandidate.isAugmenting()) {
91 augSchema = findCorrespondingAugment(getSchema(), choiceCandidate);
96 // looking for existing choice
97 final Collection<AbstractNodeDataWithSchema<?>> childNodes;
98 if (augSchema != null) {
99 childNodes = augmentationsToChild.get(augSchema);
101 childNodes = children;
104 CompositeNodeDataWithSchema<?> caseNodeDataWithSchema = findChoice(childNodes, choiceCandidate, caseCandidate);
105 if (caseNodeDataWithSchema == null) {
106 ChoiceNodeDataWithSchema choiceNodeDataWithSchema = new ChoiceNodeDataWithSchema(choiceNode);
107 childNodes.add(choiceNodeDataWithSchema);
108 caseNodeDataWithSchema = choiceNodeDataWithSchema.addCompositeChild(caseNode);
111 return caseNodeDataWithSchema.addChild(schemas);
114 private AbstractNodeDataWithSchema<?> addSimpleChild(final DataSchemaNode schema) {
115 final SimpleNodeDataWithSchema<?> newChild;
116 if (schema instanceof LeafSchemaNode) {
117 newChild = new LeafNodeDataWithSchema((LeafSchemaNode) schema);
118 } else if (schema instanceof AnyxmlSchemaNode) {
119 // YangModeledAnyxmlSchemaNode is handled by addCompositeChild method.
120 if (schema instanceof YangModeledAnyxmlSchemaNode) {
123 newChild = new AnyXmlNodeDataWithSchema((AnyxmlSchemaNode) schema);
124 } else if (schema instanceof AnydataSchemaNode) {
125 newChild = new AnydataNodeDataWithSchema((AnydataSchemaNode) schema);
130 final AugmentationSchemaNode augSchema;
131 if (schema.isAugmenting()) {
132 augSchema = findCorrespondingAugment(getSchema(), schema);
136 if (augSchema != null) {
137 augmentationsToChild.put(augSchema, newChild);
144 private static CaseNodeDataWithSchema findChoice(final Collection<AbstractNodeDataWithSchema<?>> childNodes,
145 final DataSchemaNode choiceCandidate, final DataSchemaNode caseCandidate) {
146 if (childNodes != null) {
147 for (AbstractNodeDataWithSchema<?> nodeDataWithSchema : childNodes) {
148 if (nodeDataWithSchema instanceof ChoiceNodeDataWithSchema
149 && nodeDataWithSchema.getSchema().getQName().equals(choiceCandidate.getQName())) {
150 CaseNodeDataWithSchema casePrevious = ((ChoiceNodeDataWithSchema) nodeDataWithSchema).getCase();
152 checkArgument(casePrevious.getSchema().getQName().equals(caseCandidate.getQName()),
153 "Data from case %s are specified but other data from case %s were specified earlier."
154 + " Data aren't from the same case.", caseCandidate.getQName(),
155 casePrevious.getSchema().getQName());
164 AbstractNodeDataWithSchema<?> addCompositeChild(final DataSchemaNode schema) {
165 final CompositeNodeDataWithSchema<?> newChild;
167 if (schema instanceof ListSchemaNode) {
168 newChild = new ListNodeDataWithSchema((ListSchemaNode) schema);
169 } else if (schema instanceof LeafListSchemaNode) {
170 newChild = new LeafListNodeDataWithSchema((LeafListSchemaNode) schema);
171 } else if (schema instanceof ContainerSchemaNode) {
172 newChild = new ContainerNodeDataWithSchema((ContainerSchemaNode) schema);
173 } else if (schema instanceof YangModeledAnyxmlSchemaNode) {
174 newChild = new YangModeledAnyXmlNodeDataWithSchema((YangModeledAnyxmlSchemaNode)schema);
176 newChild = new CompositeNodeDataWithSchema<>(schema);
179 addCompositeChild(newChild);
183 final void addCompositeChild(final CompositeNodeDataWithSchema<?> newChild) {
184 final AugmentationSchemaNode augSchema = findCorrespondingAugment(getSchema(), newChild.getSchema());
185 if (augSchema != null) {
186 augmentationsToChild.put(augSchema, newChild);
193 * Return a hint about how may children we are going to generate.
194 * @return Size of currently-present node list.
196 protected final int childSizeHint() {
197 return children.size();
201 public void write(final NormalizedNodeStreamWriter writer, final StreamWriterMetadataExtension metaWriter)
203 for (AbstractNodeDataWithSchema<?> child : children) {
204 child.write(writer, metaWriter);
206 for (Entry<AugmentationSchemaNode, Collection<AbstractNodeDataWithSchema<?>>> augmentationToChild
207 : augmentationsToChild.asMap().entrySet()) {
208 final Collection<AbstractNodeDataWithSchema<?>> childsFromAgumentation = augmentationToChild.getValue();
209 if (!childsFromAgumentation.isEmpty()) {
210 // FIXME: can we get the augmentation schema?
211 writer.startAugmentationNode(DataSchemaContextNode.augmentationIdentifierFrom(
212 augmentationToChild.getKey()));
214 for (AbstractNodeDataWithSchema<?> nodeDataWithSchema : childsFromAgumentation) {
215 nodeDataWithSchema.write(writer, metaWriter);
224 * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
225 * node is found then it is returned, else null.
227 * @param parent parent node
228 * @param child child node
229 * @return augmentation schema
231 private static AugmentationSchemaNode findCorrespondingAugment(final DataSchemaNode parent,
232 final DataSchemaNode child) {
233 if (parent instanceof AugmentationTarget && !(parent instanceof ChoiceSchemaNode)) {
234 for (AugmentationSchemaNode augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
235 DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
236 if (childInAugmentation != null) {