2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.netconf.util;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter.UNKNOWN_SIZE;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.Iterables;
15 import java.io.IOException;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
20 import java.util.Map.Entry;
21 import java.util.Optional;
22 import javax.xml.transform.dom.DOMSource;
23 import org.opendaylight.yangtools.concepts.Identifiable;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
30 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
31 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
32 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
35 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
43 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
45 abstract class StreamingContext<T extends PathArgument> implements Identifiable<T> {
46 private final T identifier;
48 StreamingContext(final T identifier) {
49 this.identifier = identifier;
52 static StreamingContext<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) {
53 final Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child);
54 checkArgument(potential.isPresent(),
55 "Supplied QName %s is not valid according to schema %s, potential children nodes: %s", child, schema,
56 schema.getChildNodes());
58 final DataSchemaNode result = potential.get();
59 // We try to look up if this node was added by augmentation
60 if (schema instanceof DataSchemaNode && result.isAugmenting()) {
61 for (final AugmentationSchemaNode aug : ((AugmentationTarget)schema).getAvailableAugmentations()) {
62 final DataSchemaNode found = aug.getDataChildByName(result.getQName());
64 return new Augmentation(aug, schema);
68 return fromDataSchemaNode(result);
71 static StreamingContext<?> fromDataSchemaNode(final DataSchemaNode potential) {
72 if (potential instanceof ContainerSchemaNode) {
73 return new Container((ContainerSchemaNode) potential);
74 } else if (potential instanceof ListSchemaNode) {
75 return fromListSchemaNode((ListSchemaNode) potential);
76 } else if (potential instanceof LeafSchemaNode) {
77 return new Leaf((LeafSchemaNode) potential);
78 } else if (potential instanceof ChoiceSchemaNode) {
79 return new Choice((ChoiceSchemaNode) potential);
80 } else if (potential instanceof LeafListSchemaNode) {
81 return fromLeafListSchemaNode((LeafListSchemaNode) potential);
82 } else if (potential instanceof AnyxmlSchemaNode) {
83 return new AnyXml((AnyxmlSchemaNode) potential);
89 public final T getIdentifier() {
93 abstract StreamingContext<?> getChild(PathArgument child);
95 abstract void streamToWriter(NormalizedNodeStreamWriter writer, PathArgument first, Iterator<PathArgument> others)
98 abstract boolean isMixin();
100 private static Optional<DataSchemaNode> findChildSchemaNode(final DataNodeContainer parent, final QName child) {
101 DataSchemaNode potential = parent.getDataChildByName(child);
102 if (potential == null) {
103 potential = findChoice(Iterables.filter(parent.getChildNodes(), ChoiceSchemaNode.class), child);
105 return Optional.ofNullable(potential);
108 private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) {
109 for (final ChoiceSchemaNode choice : choices) {
110 for (final CaseSchemaNode caze : choice.getCases()) {
111 if (findChildSchemaNode(caze, child).isPresent()) {
119 private static StreamingContext<?> fromListSchemaNode(final ListSchemaNode potential) {
120 final List<QName> keyDefinition = potential.getKeyDefinition();
121 if (keyDefinition == null || keyDefinition.isEmpty()) {
122 return new UnkeyedListMixin(potential);
124 return potential.isUserOrdered() ? new OrderedMapMixin(potential)
125 : new UnorderedMapMixin(potential);
128 private static StreamingContext<?> fromLeafListSchemaNode(final LeafListSchemaNode potential) {
129 return potential.isUserOrdered() ? new OrderedLeafListMixin(potential)
130 : new UnorderedLeafListMixin(potential);
133 private abstract static class AbstractComposite<T extends PathArgument> extends StreamingContext<T> {
134 AbstractComposite(final T identifier) {
139 final void streamToWriter(final NormalizedNodeStreamWriter writer, final PathArgument first,
140 final Iterator<PathArgument> others) throws IOException {
142 final QName type = getIdentifier().getNodeType();
144 final QName firstType = first.getNodeType();
145 checkArgument(type.equals(firstType), "Node QName must be %s was %s", type, firstType);
149 emitElementStart(writer, first);
150 if (others.hasNext()) {
151 final PathArgument childPath = others.next();
152 final StreamingContext<?> childOp = getChildOperation(childPath);
153 childOp.streamToWriter(writer, childPath, others);
158 abstract void emitElementStart(NormalizedNodeStreamWriter writer, PathArgument arg) throws IOException;
160 @SuppressWarnings("checkstyle:illegalCatch")
161 private StreamingContext<?> getChildOperation(final PathArgument childPath) {
162 final StreamingContext<?> childOp;
164 childOp = getChild(childPath);
165 } catch (final RuntimeException e) {
166 throw new IllegalArgumentException(String.format("Failed to process child node %s", childPath), e);
168 checkArgument(childOp != null, "Node %s is not allowed inside %s", childPath, getIdentifier());
173 private abstract static class AbstractDataContainer<T extends PathArgument> extends AbstractComposite<T> {
174 private final Map<PathArgument, StreamingContext<?>> byArg = new HashMap<>();
175 private final DataNodeContainer schema;
177 AbstractDataContainer(final T identifier, final DataNodeContainer schema) {
179 this.schema = schema;
183 final StreamingContext<?> getChild(final PathArgument child) {
184 StreamingContext<?> potential = byArg.get(child);
185 if (potential != null) {
188 potential = fromLocalSchema(child);
189 if (potential != null) {
190 byArg.put(potential.getIdentifier(), potential);
195 private StreamingContext<?> fromLocalSchema(final PathArgument child) {
196 if (child instanceof AugmentationIdentifier) {
197 return fromSchemaAndQNameChecked(schema, ((AugmentationIdentifier) child).getPossibleChildNames()
200 return fromSchemaAndQNameChecked(schema, child.getNodeType());
204 private abstract static class AbstractMapMixin extends AbstractComposite<NodeIdentifier> {
205 private final ListEntry innerNode;
207 AbstractMapMixin(final ListSchemaNode list) {
208 super(NodeIdentifier.create(list.getQName()));
209 this.innerNode = new ListEntry(NodeIdentifierWithPredicates.of(list.getQName()), list);
213 final StreamingContext<?> getChild(final PathArgument child) {
214 return child.getNodeType().equals(getIdentifier().getNodeType()) ? innerNode : null;
218 final boolean isMixin() {
223 private abstract static class AbstractSimple<T extends PathArgument> extends StreamingContext<T> {
224 AbstractSimple(final T identifier) {
229 final StreamingContext<?> getChild(final PathArgument child) {
234 final boolean isMixin() {
239 private static final class AnyXml extends AbstractSimple<NodeIdentifier> {
240 AnyXml(final AnyxmlSchemaNode schema) {
241 super(NodeIdentifier.create(schema.getQName()));
245 void streamToWriter(final NormalizedNodeStreamWriter writer, final PathArgument first,
246 final Iterator<PathArgument> others) throws IOException {
247 writer.startAnyxmlNode(getIdentifier(), DOMSource.class);
248 // FIXME: why are we not emitting a value?
253 private static final class Choice extends AbstractComposite<NodeIdentifier> {
254 private final ImmutableMap<PathArgument, StreamingContext<?>> byArg;
256 Choice(final ChoiceSchemaNode schema) {
257 super(NodeIdentifier.create(schema.getQName()));
258 final ImmutableMap.Builder<PathArgument, StreamingContext<?>> byArgBuilder = ImmutableMap.builder();
260 for (final CaseSchemaNode caze : schema.getCases()) {
261 for (final DataSchemaNode cazeChild : caze.getChildNodes()) {
262 final StreamingContext<?> childOp = fromDataSchemaNode(cazeChild);
263 byArgBuilder.put(childOp.getIdentifier(), childOp);
266 byArg = byArgBuilder.build();
270 StreamingContext<?> getChild(final PathArgument child) {
271 return byArg.get(child);
280 void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
281 writer.startChoiceNode(getIdentifier(), UNKNOWN_SIZE);
285 private static final class Leaf extends AbstractSimple<NodeIdentifier> {
286 Leaf(final LeafSchemaNode potential) {
287 super(new NodeIdentifier(potential.getQName()));
291 void streamToWriter(final NormalizedNodeStreamWriter writer, final PathArgument first,
292 final Iterator<PathArgument> others) throws IOException {
293 writer.startLeafNode(getIdentifier());
294 // FIXME: why are we not emitting a value?
299 private static final class LeafListEntry extends AbstractSimple<NodeWithValue<?>> {
300 LeafListEntry(final LeafListSchemaNode potential) {
301 super(new NodeWithValue<>(potential.getQName(), null));
305 void streamToWriter(final NormalizedNodeStreamWriter writer, final PathArgument first,
306 final Iterator<PathArgument> others) throws IOException {
307 checkArgument(first instanceof NodeWithValue);
308 final NodeWithValue<?> identifier = (NodeWithValue<?>) first;
309 writer.startLeafSetEntryNode(identifier);
310 writer.scalarValue(identifier.getValue());
315 private static final class ListEntry extends AbstractDataContainer<NodeIdentifierWithPredicates> {
316 ListEntry(final NodeIdentifierWithPredicates identifier, final ListSchemaNode schema) {
317 super(identifier, schema);
326 void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
327 final NodeIdentifierWithPredicates identifier = (NodeIdentifierWithPredicates) arg;
328 writer.startMapEntryNode(identifier, UNKNOWN_SIZE);
330 for (Entry<QName, Object> entry : identifier.entrySet()) {
331 writer.startLeafNode(new NodeIdentifier(entry.getKey()));
332 writer.scalarValue(entry.getValue());
338 private static final class UnkeyedListItem extends AbstractDataContainer<NodeIdentifier> {
339 UnkeyedListItem(final ListSchemaNode schema) {
340 super(NodeIdentifier.create(schema.getQName()), schema);
349 void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
350 writer.startUnkeyedListItem(getIdentifier(), UNKNOWN_SIZE);
354 private static final class Container extends AbstractDataContainer<NodeIdentifier> {
355 Container(final ContainerSchemaNode schema) {
356 super(NodeIdentifier.create(schema.getQName()), schema);
365 void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
366 writer.startContainerNode(getIdentifier(), UNKNOWN_SIZE);
370 private abstract static class LeafListMixin extends AbstractComposite<NodeIdentifier> {
371 private final StreamingContext<?> innerOp;
373 LeafListMixin(final LeafListSchemaNode potential) {
374 super(NodeIdentifier.create(potential.getQName()));
375 innerOp = new LeafListEntry(potential);
379 final StreamingContext<?> getChild(final PathArgument child) {
380 return child instanceof NodeWithValue ? innerOp : null;
384 final boolean isMixin() {
389 private static final class OrderedLeafListMixin extends LeafListMixin {
390 OrderedLeafListMixin(final LeafListSchemaNode potential) {
395 void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
396 writer.startOrderedLeafSet(getIdentifier(), UNKNOWN_SIZE);
400 private static class UnorderedLeafListMixin extends LeafListMixin {
401 UnorderedLeafListMixin(final LeafListSchemaNode potential) {
406 void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
407 writer.startLeafSet(getIdentifier(), UNKNOWN_SIZE);
411 private static final class Augmentation extends AbstractDataContainer<AugmentationIdentifier> {
412 Augmentation(final AugmentationSchemaNode augmentation, final DataNodeContainer schema) {
413 super(DataSchemaContextNode.augmentationIdentifierFrom(augmentation),
414 EffectiveAugmentationSchema.create(augmentation, schema));
423 void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
424 writer.startAugmentationNode(getIdentifier());
428 private static final class UnorderedMapMixin extends AbstractMapMixin {
429 UnorderedMapMixin(final ListSchemaNode list) {
434 void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
435 writer.startMapNode(getIdentifier(), UNKNOWN_SIZE);
439 private static final class OrderedMapMixin extends AbstractMapMixin {
440 OrderedMapMixin(final ListSchemaNode list) {
445 void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
446 writer.startOrderedMapNode(getIdentifier(), UNKNOWN_SIZE);
450 private static final class UnkeyedListMixin extends AbstractComposite<NodeIdentifier> {
451 private final UnkeyedListItem innerNode;
453 UnkeyedListMixin(final ListSchemaNode list) {
454 super(NodeIdentifier.create(list.getQName()));
455 this.innerNode = new UnkeyedListItem(list);
459 StreamingContext<?> getChild(final PathArgument child) {
460 return child.getNodeType().equals(getIdentifier().getNodeType()) ? innerNode : null;
469 void emitElementStart(final NormalizedNodeStreamWriter writer, final PathArgument arg) throws IOException {
470 writer.startUnkeyedList(getIdentifier(), UNKNOWN_SIZE);