2 * Copyright (c) 2016 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.util;
10 import com.google.common.base.VerifyException;
11 import java.io.IOException;
12 import java.util.HashMap;
13 import java.util.List;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.util.ImmutableMapTemplate;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter.MetadataExtension;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
25 * Utility class used for tracking parser state as needed by a StAX-like parser.
26 * This class is to be used only by respective XML and JSON parsers in yang-data-codec-xml and yang-data-codec-gson.
29 * Represents a YANG list entry node.
31 public abstract sealed class ListEntryNodeDataWithSchema extends AbstractMountPointDataWithSchema<ListSchemaNode> {
32 private static final class Keyed extends ListEntryNodeDataWithSchema {
33 private final Map<QName, SimpleNodeDataWithSchema<?>> keyValues = new HashMap<>();
34 // This template results in Maps in schema definition order
35 private final ImmutableMapTemplate<QName> predicateTemplate;
37 Keyed(final ListSchemaNode schema, final List<QName> keyDef) {
39 predicateTemplate = ImmutableMapTemplate.ordered(keyDef);
43 void addChild(final AbstractNodeDataWithSchema<?> newChild) {
44 if (newChild.getSchema() instanceof LeafSchemaNode leaf) {
45 final var childName = leaf.getQName();
46 if (predicateTemplate.keySet().contains(childName)) {
47 if (newChild instanceof SimpleNodeDataWithSchema<?> simpleChild) {
48 keyValues.put(childName, simpleChild);
50 throw new VerifyException("Unexpected child " + newChild);
54 super.addChild(newChild);
58 public void write(final NormalizedNodeStreamWriter writer, final MetadataExtension metaWriter)
60 final var schema = getSchema();
61 writer.nextDataSchemaNode(schema);
63 final var nodeType = schema.getQName();
64 final Map<QName, Object> predicates;
66 predicates = predicateTemplate.instantiateTransformed(keyValues, (key, node) -> node.getValue());
67 } catch (IllegalArgumentException e) {
68 final var present = keyValues.keySet();
69 final var module = nodeType.getModule();
70 final var missing = predicateTemplate.keySet().stream()
71 .filter(key -> !present.contains(key))
72 .map(key -> module.equals(key.getModule()) ? key.getLocalName() : key)
75 throw new IOException("List entry " + nodeType + " is missing leaf values for " + missing, e);
78 writer.startMapEntryNode(NodeIdentifierWithPredicates.of(nodeType, predicates), childSizeHint());
79 writeMetadata(metaWriter);
80 super.write(writer, metaWriter);
85 private static final class Unkeyed extends ListEntryNodeDataWithSchema {
86 Unkeyed(final ListSchemaNode schema) {
91 public void write(final NormalizedNodeStreamWriter writer, final MetadataExtension metaWriter)
93 writer.nextDataSchemaNode(getSchema());
94 writer.startUnkeyedListItem(provideNodeIdentifier(), childSizeHint());
95 super.write(writer, metaWriter);
100 ListEntryNodeDataWithSchema(final ListSchemaNode schema) {
104 static @NonNull ListEntryNodeDataWithSchema forSchema(final ListSchemaNode schema) {
105 final var keyDef = schema.getKeyDefinition();
106 return keyDef.isEmpty() ? new Unkeyed(schema) : new Keyed(schema, keyDef);