Merge "Bug 1446: Changed QNM to toString listener for debug output"
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / CompositeNodeDataWithSchema.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.codec.gson;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ArrayListMultimap;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Multimap;
16
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Deque;
21 import java.util.List;
22 import java.util.Map.Entry;
23
24 import javax.annotation.Nonnull;
25
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
29 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
31 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
32 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
33 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
34 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
39
40 /**
41  * A node which is composed of multiple simpler nodes.
42  */
43 class CompositeNodeDataWithSchema extends AbstractNodeDataWithSchema {
44     private static final Function<DataSchemaNode, QName> QNAME_FUNCTION = new Function<DataSchemaNode, QName>() {
45         @Override
46         public QName apply(@Nonnull final DataSchemaNode input) {
47             return input.getQName();
48         }
49     };
50
51     /**
52      * nodes which were added to schema via augmentation and are present in data input
53      */
54     private final Multimap<AugmentationSchema, AbstractNodeDataWithSchema> augmentationsToChild = ArrayListMultimap.create();
55
56     /**
57      * remaining data nodes (which aren't added via augment). Every of one them should have the same QName.
58      */
59     private final List<AbstractNodeDataWithSchema> children = new ArrayList<>();
60
61     public CompositeNodeDataWithSchema(final DataSchemaNode schema) {
62         super(schema);
63     }
64
65     public AbstractNodeDataWithSchema addChild(final Deque<DataSchemaNode> schemas) {
66         Preconditions.checkArgument(!schemas.isEmpty(), "Expecting at least one schema");
67
68         // Pop the first node...
69         final DataSchemaNode schema = schemas.pop();
70         if (schemas.isEmpty()) {
71             // Simple, direct node
72             return addChild(schema);
73         }
74
75         // The choice/case mess, reuse what we already popped
76         final DataSchemaNode choiceCandidate = schema;
77         Preconditions.checkArgument(choiceCandidate instanceof ChoiceNode,
78             "Expected node of type ChoiceNode but was %s", choiceCandidate.getClass().getSimpleName());
79         final ChoiceNode choiceNode = (ChoiceNode) choiceCandidate;
80
81         final DataSchemaNode caseCandidate = schemas.pop();
82         Preconditions.checkArgument(caseCandidate instanceof ChoiceCaseNode,
83             "Expected node of type ChoiceCaseNode but was %s", caseCandidate.getClass().getSimpleName());
84         final ChoiceCaseNode caseNode = (ChoiceCaseNode) caseCandidate;
85
86         AugmentationSchema augSchema = null;
87         if (choiceCandidate.isAugmenting()) {
88             augSchema = findCorrespondingAugment(getSchema(), choiceCandidate);
89         }
90
91         // looking for existing choice
92         final Collection<AbstractNodeDataWithSchema> childNodes;
93         if (augSchema != null) {
94             childNodes = augmentationsToChild.get(augSchema);
95         } else {
96             childNodes = children;
97         }
98
99         CompositeNodeDataWithSchema caseNodeDataWithSchema = findChoice(childNodes, choiceCandidate, caseCandidate);
100         if (caseNodeDataWithSchema == null) {
101             ChoiceNodeDataWithSchema choiceNodeDataWithSchema = new ChoiceNodeDataWithSchema(choiceNode);
102             addChild(choiceNodeDataWithSchema);
103             caseNodeDataWithSchema = choiceNodeDataWithSchema.addCompositeChild(caseNode);
104         }
105
106         return caseNodeDataWithSchema.addChild(schemas);
107     }
108
109     private AbstractNodeDataWithSchema addSimpleChild(final DataSchemaNode schema) {
110         SimpleNodeDataWithSchema newChild = null;
111         if (schema instanceof LeafSchemaNode) {
112             newChild = new LeafNodeDataWithSchema(schema);
113         } else if (schema instanceof AnyXmlSchemaNode) {
114             newChild = new AnyXmlNodeDataWithSchema(schema);
115         } else {
116             return null;
117         }
118
119         AugmentationSchema augSchema = null;
120         if (schema.isAugmenting()) {
121             augSchema = findCorrespondingAugment(getSchema(), schema);
122         }
123         if (augSchema != null) {
124             augmentationsToChild.put(augSchema, newChild);
125         } else {
126             addChild(newChild);
127         }
128         return newChild;
129     }
130
131     private CaseNodeDataWithSchema findChoice(final Collection<AbstractNodeDataWithSchema> childNodes, final DataSchemaNode choiceCandidate,
132             final DataSchemaNode caseCandidate) {
133         if (childNodes != null) {
134             for (AbstractNodeDataWithSchema nodeDataWithSchema : childNodes) {
135                 if (nodeDataWithSchema instanceof ChoiceNodeDataWithSchema
136                         && nodeDataWithSchema.getSchema().getQName().equals(choiceCandidate.getQName())) {
137                     CaseNodeDataWithSchema casePrevious = ((ChoiceNodeDataWithSchema) nodeDataWithSchema).getCase();
138
139                     Preconditions.checkArgument(casePrevious.getSchema().getQName().equals(caseCandidate.getQName()),
140                         "Data from case %s are specified but other data from case %s were specified erlier. Data aren't from the same case.",
141                         caseCandidate.getQName(), casePrevious.getSchema().getQName());
142
143                     return casePrevious;
144                 }
145             }
146         }
147         return null;
148     }
149
150     AbstractNodeDataWithSchema addCompositeChild(final DataSchemaNode schema) {
151         CompositeNodeDataWithSchema newChild;
152         if (schema instanceof ListSchemaNode) {
153             newChild = new ListNodeDataWithSchema(schema);
154         } else if (schema instanceof LeafListSchemaNode) {
155             newChild = new LeafListNodeDataWithSchema(schema);
156         } else if (schema instanceof ContainerSchemaNode) {
157             newChild = new ContainerNodeDataWithSchema(schema);
158         } else {
159             newChild = new CompositeNodeDataWithSchema(schema);
160         }
161         addCompositeChild(newChild);
162         return newChild;
163     }
164
165     void addCompositeChild(final CompositeNodeDataWithSchema newChild) {
166         AugmentationSchema augSchema = findCorrespondingAugment(getSchema(), newChild.getSchema());
167         if (augSchema != null) {
168             augmentationsToChild.put(augSchema, newChild);
169         } else {
170             addChild(newChild);
171         }
172     }
173
174     private AbstractNodeDataWithSchema addChild(final DataSchemaNode schema) {
175         AbstractNodeDataWithSchema newChild = addSimpleChild(schema);
176         return newChild == null ? addCompositeChild(schema) : newChild;
177     }
178
179     public void addChild(final AbstractNodeDataWithSchema newChild) {
180         children.add(newChild);
181     }
182
183     /**
184      * Return a hint about how may children we are going to generate.
185      * @return Size of currently-present node list.
186      */
187     protected final int childSizeHint() {
188         return children.size();
189     }
190
191     /**
192      * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
193      * node is found then it is returned, else null.
194      */
195     AugmentationSchema findCorrespondingAugment(final DataSchemaNode parent, final DataSchemaNode child) {
196         if (parent instanceof AugmentationTarget) {
197             for (AugmentationSchema augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
198                 DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
199                 if (childInAugmentation != null) {
200                     return augmentation;
201                 }
202             }
203         }
204         return null;
205     }
206
207     @Override
208     public void write(final NormalizedNodeStreamWriter writer) throws IOException {
209         for (AbstractNodeDataWithSchema child : children) {
210             child.write(writer);
211         }
212         for (Entry<AugmentationSchema, Collection<AbstractNodeDataWithSchema>> augmentationToChild : augmentationsToChild.asMap().entrySet()) {
213             final Collection<AbstractNodeDataWithSchema> childsFromAgumentation = augmentationToChild.getValue();
214             if (!childsFromAgumentation.isEmpty()) {
215                 writer.startAugmentationNode(toAugmentationIdentifier(augmentationToChild.getKey()));
216
217                 for (AbstractNodeDataWithSchema nodeDataWithSchema : childsFromAgumentation) {
218                     nodeDataWithSchema.write(writer);
219                 }
220
221                 writer.endNode();
222             }
223         }
224     }
225
226     private static AugmentationIdentifier toAugmentationIdentifier(final AugmentationSchema schema) {
227         final Collection<QName> qnames = Collections2.transform(schema.getChildNodes(), QNAME_FUNCTION);
228         return new AugmentationIdentifier(ImmutableSet.copyOf(qnames));
229     }
230 }