*/
package org.opendaylight.yangtools.yang.data.codec.gson;
-import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.Collections2;
-import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Map.Entry;
-import javax.annotation.Nonnull;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
-import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
-import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
* A node which is composed of multiple simpler nodes.
*/
class CompositeNodeDataWithSchema extends AbstractNodeDataWithSchema {
- private static final Function<DataSchemaNode, QName> QNAME_FUNCTION = new Function<DataSchemaNode, QName>() {
- @Override
- public QName apply(@Nonnull final DataSchemaNode input) {
- return input.getQName();
- }
- };
/**
* nodes which were added to schema via augmentation and are present in data input
// The choice/case mess, reuse what we already popped
final DataSchemaNode choiceCandidate = schema;
- Preconditions.checkArgument(choiceCandidate instanceof ChoiceNode,
+ Preconditions.checkArgument(choiceCandidate instanceof ChoiceSchemaNode,
"Expected node of type ChoiceNode but was %s", choiceCandidate.getClass().getSimpleName());
- final ChoiceNode choiceNode = (ChoiceNode) choiceCandidate;
+ final ChoiceSchemaNode choiceNode = (ChoiceSchemaNode) choiceCandidate;
final DataSchemaNode caseCandidate = schemas.pop();
Preconditions.checkArgument(caseCandidate instanceof ChoiceCaseNode,
AugmentationSchema augSchema = null;
if (choiceCandidate.isAugmenting()) {
- augSchema = findCorrespondingAugment(getSchema(), choiceCandidate);
+ augSchema = SchemaUtils.findCorrespondingAugment(getSchema(), choiceCandidate);
}
// looking for existing choice
CompositeNodeDataWithSchema caseNodeDataWithSchema = findChoice(childNodes, choiceCandidate, caseCandidate);
if (caseNodeDataWithSchema == null) {
ChoiceNodeDataWithSchema choiceNodeDataWithSchema = new ChoiceNodeDataWithSchema(choiceNode);
- addChild(choiceNodeDataWithSchema);
+ childNodes.add(choiceNodeDataWithSchema);
caseNodeDataWithSchema = choiceNodeDataWithSchema.addCompositeChild(caseNode);
}
AugmentationSchema augSchema = null;
if (schema.isAugmenting()) {
- augSchema = findCorrespondingAugment(getSchema(), schema);
+ augSchema = SchemaUtils.findCorrespondingAugment(getSchema(), schema);
}
if (augSchema != null) {
augmentationsToChild.put(augSchema, newChild);
}
void addCompositeChild(final CompositeNodeDataWithSchema newChild) {
- AugmentationSchema augSchema = findCorrespondingAugment(getSchema(), newChild.getSchema());
+ AugmentationSchema augSchema = SchemaUtils.findCorrespondingAugment(getSchema(), newChild.getSchema());
if (augSchema != null) {
augmentationsToChild.put(augSchema, newChild);
} else {
return children.size();
}
- /**
- * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
- * node is found then it is returned, else null.
- */
- AugmentationSchema findCorrespondingAugment(final DataSchemaNode parent, final DataSchemaNode child) {
- if (parent instanceof AugmentationTarget && !((parent instanceof ChoiceCaseNode) || (parent instanceof ChoiceNode))) {
- for (AugmentationSchema augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
- DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
- if (childInAugmentation != null) {
- return augmentation;
- }
- }
- }
- return null;
- }
-
@Override
public void write(final NormalizedNodeStreamWriter writer) throws IOException {
for (AbstractNodeDataWithSchema child : children) {
for (Entry<AugmentationSchema, Collection<AbstractNodeDataWithSchema>> augmentationToChild : augmentationsToChild.asMap().entrySet()) {
final Collection<AbstractNodeDataWithSchema> childsFromAgumentation = augmentationToChild.getValue();
if (!childsFromAgumentation.isEmpty()) {
- writer.startAugmentationNode(toAugmentationIdentifier(augmentationToChild.getKey()));
+ writer.startAugmentationNode(SchemaUtils.getNodeIdentifierForAugmentation(augmentationToChild.getKey()));
for (AbstractNodeDataWithSchema nodeDataWithSchema : childsFromAgumentation) {
nodeDataWithSchema.write(writer);
}
}
}
-
- private static AugmentationIdentifier toAugmentationIdentifier(final AugmentationSchema schema) {
- final Collection<QName> qnames = Collections2.transform(schema.getChildNodes(), QNAME_FUNCTION);
- return new AugmentationIdentifier(ImmutableSet.copyOf(qnames));
- }
}