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.codec.gson;
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;
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;
24 import javax.annotation.Nonnull;
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;
41 * A node which is composed of multiple simpler nodes.
43 class CompositeNodeDataWithSchema extends AbstractNodeDataWithSchema {
44 private static final Function<DataSchemaNode, QName> QNAME_FUNCTION = new Function<DataSchemaNode, QName>() {
46 public QName apply(@Nonnull final DataSchemaNode input) {
47 return input.getQName();
52 * nodes which were added to schema via augmentation and are present in data input
54 private final Multimap<AugmentationSchema, AbstractNodeDataWithSchema> augmentationsToChild = ArrayListMultimap.create();
57 * remaining data nodes (which aren't added via augment). Every of one them should have the same QName.
59 private final List<AbstractNodeDataWithSchema> children = new ArrayList<>();
61 public CompositeNodeDataWithSchema(final DataSchemaNode schema) {
65 public AbstractNodeDataWithSchema addChild(final Deque<DataSchemaNode> schemas) {
66 Preconditions.checkArgument(!schemas.isEmpty(), "Expecting at least one schema");
68 // Pop the first node...
69 final DataSchemaNode schema = schemas.pop();
70 if (schemas.isEmpty()) {
71 // Simple, direct node
72 return addChild(schema);
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;
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;
86 AugmentationSchema augSchema = null;
87 if (choiceCandidate.isAugmenting()) {
88 augSchema = findCorrespondingAugment(getSchema(), choiceCandidate);
91 // looking for existing choice
92 final Collection<AbstractNodeDataWithSchema> childNodes;
93 if (augSchema != null) {
94 childNodes = augmentationsToChild.get(augSchema);
96 childNodes = children;
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);
106 return caseNodeDataWithSchema.addChild(schemas);
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);
117 if (newChild != null) {
119 AugmentationSchema augSchema = null;
120 if (schema.isAugmenting()) {
121 augSchema = findCorrespondingAugment(getSchema(), schema);
123 if (augSchema != null) {
124 augmentationsToChild.put(augSchema, newChild);
133 private CaseNodeDataWithSchema findChoice(final Collection<AbstractNodeDataWithSchema> childNodes, final DataSchemaNode choiceCandidate,
134 final DataSchemaNode caseCandidate) {
135 if (childNodes != null) {
136 for (AbstractNodeDataWithSchema nodeDataWithSchema : childNodes) {
137 if (nodeDataWithSchema instanceof ChoiceNodeDataWithSchema
138 && nodeDataWithSchema.getSchema().getQName().equals(choiceCandidate.getQName())) {
139 CaseNodeDataWithSchema casePrevious = ((ChoiceNodeDataWithSchema) nodeDataWithSchema).getCase();
141 Preconditions.checkArgument(casePrevious.getSchema().getQName().equals(caseCandidate.getQName()),
142 "Data from case %s are specified but other data from case %s were specified erlier. Data aren't from the same case.",
143 caseCandidate.getQName(), casePrevious.getSchema().getQName());
152 AbstractNodeDataWithSchema addCompositeChild(final DataSchemaNode schema) {
153 CompositeNodeDataWithSchema newChild;
154 if (schema instanceof ListSchemaNode) {
155 newChild = new ListNodeDataWithSchema(schema);
156 } else if (schema instanceof LeafListSchemaNode) {
157 newChild = new LeafListNodeDataWithSchema(schema);
158 } else if (schema instanceof ContainerSchemaNode) {
159 newChild = new ContainerNodeDataWithSchema(schema);
161 newChild = new CompositeNodeDataWithSchema(schema);
163 addCompositeChild(newChild);
167 void addCompositeChild(final CompositeNodeDataWithSchema newChild) {
168 AugmentationSchema augSchema = findCorrespondingAugment(getSchema(), newChild.getSchema());
169 if (augSchema != null) {
170 augmentationsToChild.put(augSchema, newChild);
176 private AbstractNodeDataWithSchema addChild(final DataSchemaNode schema) {
177 AbstractNodeDataWithSchema newChild = addSimpleChild(schema);
178 return newChild == null ? addCompositeChild(schema) : newChild;
181 public void addChild(final AbstractNodeDataWithSchema newChild) {
182 children.add(newChild);
186 * Return a hint about how may children we are going to generate.
187 * @return Size of currently-present node list.
189 protected final int childSizeHint() {
190 return children.size();
194 * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
195 * node is found then it is returned, else null.
197 AugmentationSchema findCorrespondingAugment(final DataSchemaNode parent, final DataSchemaNode child) {
198 if (parent instanceof AugmentationTarget) {
199 for (AugmentationSchema augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
200 DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
201 if (childInAugmentation != null) {
210 public void write(final NormalizedNodeStreamWriter writer) throws IOException {
211 for (AbstractNodeDataWithSchema child : children) {
214 for (Entry<AugmentationSchema, Collection<AbstractNodeDataWithSchema>> augmentationToChild : augmentationsToChild.asMap().entrySet()) {
215 final Collection<AbstractNodeDataWithSchema> childsFromAgumentation = augmentationToChild.getValue();
216 if (!childsFromAgumentation.isEmpty()) {
217 writer.startAugmentationNode(toAugmentationIdentifier(augmentationToChild.getKey()));
219 for (AbstractNodeDataWithSchema nodeDataWithSchema : childsFromAgumentation) {
220 nodeDataWithSchema.write(writer);
228 private static AugmentationIdentifier toAugmentationIdentifier(final AugmentationSchema schema) {
229 final Collection<QName> qnames = Collections2.transform(schema.getChildNodes(), QNAME_FUNCTION);
230 return new AugmentationIdentifier(ImmutableSet.copyOf(qnames));