8b79165cf9afe5f028def1aa8824b545c13258f4
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / CompositeNodeDataWithSchema.java
1 /*
2  * Copyright (c) 2016 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.util;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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;
34
35 /**
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.
38  *
39  * <p>
40  * Represents a node which is composed of multiple simpler nodes.
41  */
42 public class CompositeNodeDataWithSchema<T extends DataSchemaNode> extends AbstractNodeDataWithSchema<T> {
43     /**
44      * nodes which were added to schema via augmentation and are present in data input.
45      */
46     private final Multimap<AugmentationSchemaNode, AbstractNodeDataWithSchema<?>> augmentationsToChild =
47         ArrayListMultimap.create();
48
49     /**
50      * remaining data nodes (which aren't added via augment). Every of one them should have the same QName.
51      */
52     private final List<AbstractNodeDataWithSchema<?>> children = new ArrayList<>();
53
54     public CompositeNodeDataWithSchema(final T schema) {
55         super(schema);
56     }
57
58     private AbstractNodeDataWithSchema<?> addChild(final DataSchemaNode schema) {
59         AbstractNodeDataWithSchema<?> newChild = addSimpleChild(schema);
60         return newChild == null ? addCompositeChild(schema) : newChild;
61     }
62
63     @Deprecated
64     public void addChild(final AbstractNodeDataWithSchema<?> newChild) {
65         children.add(newChild);
66     }
67
68     public AbstractNodeDataWithSchema<?> addChild(final Deque<DataSchemaNode> schemas) {
69         checkArgument(!schemas.isEmpty(), "Expecting at least one schema");
70
71         // Pop the first node...
72         final DataSchemaNode schema = schemas.pop();
73         if (schemas.isEmpty()) {
74             // Simple, direct node
75             return addChild(schema);
76         }
77
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;
83
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;
88
89         AugmentationSchemaNode augSchema = null;
90         if (choiceCandidate.isAugmenting()) {
91             augSchema = findCorrespondingAugment(getSchema(), choiceCandidate);
92         }
93
94         // looking for existing choice
95         final Collection<AbstractNodeDataWithSchema<?>> childNodes;
96         if (augSchema != null) {
97             childNodes = augmentationsToChild.get(augSchema);
98         } else {
99             childNodes = children;
100         }
101
102         CompositeNodeDataWithSchema<?> caseNodeDataWithSchema = findChoice(childNodes, choiceCandidate, caseCandidate);
103         if (caseNodeDataWithSchema == null) {
104             ChoiceNodeDataWithSchema choiceNodeDataWithSchema = new ChoiceNodeDataWithSchema(choiceNode);
105             childNodes.add(choiceNodeDataWithSchema);
106             caseNodeDataWithSchema = choiceNodeDataWithSchema.addCompositeChild(caseNode);
107         }
108
109         return caseNodeDataWithSchema.addChild(schemas);
110     }
111
112     private AbstractNodeDataWithSchema<?> addSimpleChild(final DataSchemaNode schema) {
113         SimpleNodeDataWithSchema<?> newChild = null;
114         if (schema instanceof LeafSchemaNode) {
115             newChild = new LeafNodeDataWithSchema((LeafSchemaNode) schema);
116         } else if (schema instanceof AnyxmlSchemaNode) {
117             // YangModeledAnyxmlSchemaNode is handled by addCompositeChild method.
118             if (schema instanceof YangModeledAnyxmlSchemaNode) {
119                 return null;
120             }
121             newChild = new AnyXmlNodeDataWithSchema((AnyxmlSchemaNode) schema);
122         } else if (schema instanceof AnydataSchemaNode) {
123             newChild = new AnydataNodeDataWithSchema((AnydataSchemaNode) schema);
124         } else {
125             return null;
126         }
127
128         AugmentationSchemaNode augSchema = null;
129         if (schema.isAugmenting()) {
130             augSchema = findCorrespondingAugment(getSchema(), schema);
131         }
132         if (augSchema != null) {
133             augmentationsToChild.put(augSchema, newChild);
134         } else {
135             addChild(newChild);
136         }
137         return newChild;
138     }
139
140     private static CaseNodeDataWithSchema findChoice(final Collection<AbstractNodeDataWithSchema<?>> childNodes,
141             final DataSchemaNode choiceCandidate, final DataSchemaNode caseCandidate) {
142         if (childNodes != null) {
143             for (AbstractNodeDataWithSchema<?> nodeDataWithSchema : childNodes) {
144                 if (nodeDataWithSchema instanceof ChoiceNodeDataWithSchema
145                         && nodeDataWithSchema.getSchema().getQName().equals(choiceCandidate.getQName())) {
146                     CaseNodeDataWithSchema casePrevious = ((ChoiceNodeDataWithSchema) nodeDataWithSchema).getCase();
147
148                     checkArgument(casePrevious.getSchema().getQName().equals(caseCandidate.getQName()),
149                         "Data from case %s are specified but other data from case %s were specified earlier."
150                         + " Data aren't from the same case.", caseCandidate.getQName(),
151                         casePrevious.getSchema().getQName());
152
153                     return casePrevious;
154                 }
155             }
156         }
157         return null;
158     }
159
160     AbstractNodeDataWithSchema<?> addCompositeChild(final DataSchemaNode schema) {
161         final CompositeNodeDataWithSchema<?> newChild;
162
163         if (schema instanceof ListSchemaNode) {
164             newChild = new ListNodeDataWithSchema((ListSchemaNode) schema);
165         } else if (schema instanceof LeafListSchemaNode) {
166             newChild = new LeafListNodeDataWithSchema((LeafListSchemaNode) schema);
167         } else if (schema instanceof ContainerSchemaNode) {
168             newChild = new ContainerNodeDataWithSchema((ContainerSchemaNode) schema);
169         } else if (schema instanceof YangModeledAnyxmlSchemaNode) {
170             newChild = new YangModeledAnyXmlNodeDataWithSchema((YangModeledAnyxmlSchemaNode)schema);
171         } else {
172             newChild = new CompositeNodeDataWithSchema<>(schema);
173         }
174
175         addCompositeChild(newChild);
176         return newChild;
177     }
178
179     void addCompositeChild(final CompositeNodeDataWithSchema<?> newChild) {
180         AugmentationSchemaNode augSchema = findCorrespondingAugment(getSchema(), newChild.getSchema());
181         if (augSchema != null) {
182             augmentationsToChild.put(augSchema, newChild);
183         } else {
184             addChild(newChild);
185         }
186     }
187
188     /**
189      * Return a hint about how may children we are going to generate.
190      * @return Size of currently-present node list.
191      */
192     protected final int childSizeHint() {
193         return children.size();
194     }
195
196     @Override
197     public void write(final NormalizedNodeStreamWriter writer, final StreamWriterMetadataExtension metaWriter)
198             throws IOException {
199         for (AbstractNodeDataWithSchema<?> child : children) {
200             child.write(writer, metaWriter);
201         }
202         for (Entry<AugmentationSchemaNode, Collection<AbstractNodeDataWithSchema<?>>> augmentationToChild
203                 : augmentationsToChild.asMap().entrySet()) {
204             final Collection<AbstractNodeDataWithSchema<?>> childsFromAgumentation = augmentationToChild.getValue();
205             if (!childsFromAgumentation.isEmpty()) {
206                 // FIXME: can we get the augmentation schema?
207                 writer.startAugmentationNode(DataSchemaContextNode.augmentationIdentifierFrom(
208                     augmentationToChild.getKey()));
209
210                 for (AbstractNodeDataWithSchema<?> nodeDataWithSchema : childsFromAgumentation) {
211                     nodeDataWithSchema.write(writer, metaWriter);
212                 }
213
214                 writer.endNode();
215             }
216         }
217     }
218
219     /**
220      * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
221      * node is found then it is returned, else null.
222      *
223      * @param parent parent node
224      * @param child child node
225      * @return augmentation schema
226      */
227     private static AugmentationSchemaNode findCorrespondingAugment(final DataSchemaNode parent,
228             final DataSchemaNode child) {
229         if (parent instanceof AugmentationTarget && !(parent instanceof ChoiceSchemaNode)) {
230             for (AugmentationSchemaNode augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
231                 DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
232                 if (childInAugmentation != null) {
233                     return augmentation;
234                 }
235             }
236         }
237         return null;
238     }
239 }