package org.opendaylight.netconf.util;
import static com.google.common.base.Preconditions.checkArgument;
-import static org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter.UNKNOWN_SIZE;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import java.io.IOException;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.stream.Collectors;
import javax.xml.transform.dom.DOMSource;
import org.opendaylight.yangtools.concepts.Identifiable;
+import org.opendaylight.yangtools.yang.common.Empty;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
// We try to look up if this node was added by augmentation
if (schema instanceof DataSchemaNode && result.isAugmenting()) {
for (final AugmentationSchemaNode aug : ((AugmentationTarget)schema).getAvailableAugmentations()) {
- if (aug.findDataChildByName(result.getQName()).isPresent()) {
+ if (aug.dataChildByName(result.getQName()) != null) {
return new Augmentation(aug, schema);
}
}
}
static StreamingContext<?> fromDataSchemaNode(final DataSchemaNode potential) {
- if (potential instanceof ContainerSchemaNode) {
- return new Container((ContainerSchemaNode) potential);
- } else if (potential instanceof ListSchemaNode) {
- return fromListSchemaNode((ListSchemaNode) potential);
- } else if (potential instanceof LeafSchemaNode) {
- return new Leaf((LeafSchemaNode) potential);
- } else if (potential instanceof ChoiceSchemaNode) {
- return new Choice((ChoiceSchemaNode) potential);
- } else if (potential instanceof LeafListSchemaNode) {
- return fromLeafListSchemaNode((LeafListSchemaNode) potential);
- } else if (potential instanceof AnyxmlSchemaNode) {
- return new AnyXml((AnyxmlSchemaNode) potential);
- }
+ if (potential instanceof ContainerSchemaNode container) {
+ return new Container(container);
+ } else if (potential instanceof ListSchemaNode list) {
+ return fromListSchemaNode(list);
+ } else if (potential instanceof LeafSchemaNode leaf) {
+ return new Leaf(leaf);
+ } else if (potential instanceof ChoiceSchemaNode choice) {
+ return new Choice(choice);
+ } else if (potential instanceof LeafListSchemaNode leafList) {
+ return fromLeafListSchemaNode(leafList);
+ } else if (potential instanceof AnyxmlSchemaNode anyxml) {
+ return new AnyXml(anyxml);
+ }
+ // FIXME: unhandled anydata!
return null;
}
final Iterator<PathArgument> others) throws IOException {
verifyActualPathArgument(first);
- emitElementStart(writer, first);
+ emitElementStart(writer, first, others.hasNext() ? 1 : 0);
if (others.hasNext()) {
final PathArgument childPath = others.next();
final StreamingContext<?> childOp = getChildOperation(childPath);
final PathNode subtree) throws IOException {
verifyActualPathArgument(first);
- emitElementStart(writer, first);
+ final Collection<PathNode> children = subtree.children();
+ emitElementStart(writer, first, children.size());
for (final PathNode node : subtree.children()) {
emitChildTreeNode(writer, node);
}
}
}
- abstract void emitElementStart(NormalizedNodeStreamWriter writer, PathArgument arg) throws IOException;
+ abstract void emitElementStart(NormalizedNodeStreamWriter writer, PathArgument arg,
+ int childSizeHint) throws IOException;
@SuppressWarnings("checkstyle:illegalCatch")
StreamingContext<?> getChildOperation(final PathArgument childPath) {
}
private StreamingContext<?> fromLocalSchema(final PathArgument child) {
- if (child instanceof AugmentationIdentifier) {
- return fromSchemaAndQNameChecked(schema, ((AugmentationIdentifier) child).getPossibleChildNames()
- .iterator().next());
+ if (child instanceof AugmentationIdentifier aid) {
+ return fromSchemaAndQNameChecked(schema, aid.getPossibleChildNames().iterator().next());
}
return fromSchemaAndQNameChecked(schema, child.getNodeType());
}
AbstractMapMixin(final ListSchemaNode list) {
super(NodeIdentifier.create(list.getQName()));
- this.innerNode = new ListEntry(NodeIdentifierWithPredicates.of(list.getQName()), list);
- this.keyLeaves = list.getKeyDefinition();
+ innerNode = new ListEntry(NodeIdentifierWithPredicates.of(list.getQName()), list);
+ keyLeaves = list.getKeyDefinition();
}
@Override
@Override
final void emitChildTreeNode(final NormalizedNodeStreamWriter writer, final PathNode node) throws IOException {
- final NodeIdentifierWithPredicates childPath = (NodeIdentifierWithPredicates) node.element();
+ final PathArgument element = node.element();
+ if (!(element instanceof NodeIdentifierWithPredicates childPath)) {
+ throw new IOException("Child identifier " + element + " is invalid in parent " + getIdentifier());
+ }
+
final StreamingContext<?> childOp = getChildOperation(childPath);
if (childPath.size() == 0 && node.isEmpty() || childPath.keySet().containsAll(keyLeaves)) {
// This is a query for the entire list, or the query specifies everything we need
}
@Override
- void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
- writer.startChoiceNode(getIdentifier(), UNKNOWN_SIZE);
+ void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg,
+ final int childSizeHint) throws IOException {
+ writer.startChoiceNode(getIdentifier(), childSizeHint);
}
}
private static final class LeafListEntry extends AbstractSimple<NodeWithValue<?>> {
LeafListEntry(final LeafListSchemaNode potential) {
- super(new NodeWithValue<>(potential.getQName(), null));
+ super(new NodeWithValue<>(potential.getQName(), Empty.value()));
}
@Override
}
@Override
- void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
+ void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg,
+ final int childSizeHint) throws IOException {
final NodeIdentifierWithPredicates identifier = (NodeIdentifierWithPredicates) arg;
- writer.startMapEntryNode(identifier, UNKNOWN_SIZE);
+ writer.startMapEntryNode(identifier, childSizeHint);
for (Entry<QName, Object> entry : identifier.entrySet()) {
writer.startLeafNode(new NodeIdentifier(entry.getKey()));
}
@Override
- void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
- writer.startUnkeyedListItem(getIdentifier(), UNKNOWN_SIZE);
+ void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg,
+ final int childSizeHint) throws IOException {
+ writer.startUnkeyedListItem(getIdentifier(), childSizeHint);
}
}
}
@Override
- void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
- writer.startContainerNode(getIdentifier(), UNKNOWN_SIZE);
+ void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg,
+ final int childSizeHint) throws IOException {
+ writer.startContainerNode(getIdentifier(), childSizeHint);
}
}
}
@Override
- void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
- writer.startOrderedLeafSet(getIdentifier(), UNKNOWN_SIZE);
+ void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg,
+ final int childSizeHint) throws IOException {
+ writer.startOrderedLeafSet(getIdentifier(), childSizeHint);
}
}
}
@Override
- void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
- writer.startLeafSet(getIdentifier(), UNKNOWN_SIZE);
+ void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg,
+ final int childSizeHint) throws IOException {
+ writer.startLeafSet(getIdentifier(), childSizeHint);
}
}
private static final class Augmentation extends AbstractDataContainer<AugmentationIdentifier> {
Augmentation(final AugmentationSchemaNode augmentation, final DataNodeContainer schema) {
super(DataSchemaContextNode.augmentationIdentifierFrom(augmentation),
- EffectiveAugmentationSchema.create(augmentation, schema));
+ new EffectiveAugmentationSchema(augmentation, schema));
}
@Override
}
@Override
- void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
+ void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg,
+ final int childSizeHint) throws IOException {
writer.startAugmentationNode(getIdentifier());
}
}
}
@Override
- void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
- writer.startMapNode(getIdentifier(), UNKNOWN_SIZE);
+ void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg,
+ final int childSizeHint) throws IOException {
+ writer.startMapNode(getIdentifier(), childSizeHint);
}
}
}
@Override
- void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
- writer.startOrderedMapNode(getIdentifier(), UNKNOWN_SIZE);
+ void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg,
+ final int childSizeHint) throws IOException {
+ writer.startOrderedMapNode(getIdentifier(), childSizeHint);
}
}
UnkeyedListMixin(final ListSchemaNode list) {
super(NodeIdentifier.create(list.getQName()));
- this.innerNode = new UnkeyedListItem(list);
+ innerNode = new UnkeyedListItem(list);
}
@Override
}
@Override
- void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
- writer.startUnkeyedList(getIdentifier(), UNKNOWN_SIZE);
+ void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg,
+ final int childSizeHint) throws IOException {
+ writer.startUnkeyedList(getIdentifier(), childSizeHint);
}
}
}