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 static com.google.common.base.Verify.verify;
12 import java.io.IOException;
13 import java.util.HashMap;
14 import java.util.List;
16 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadataStreamWriter;
17 import org.opendaylight.yangtools.util.ImmutableMapTemplate;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
26 * Utility class used for tracking parser state as needed by a StAX-like parser.
27 * This class is to be used only by respective XML and JSON parsers in yang-data-codec-xml and yang-data-codec-gson.
30 * Represents a YANG list entry node.
32 public abstract class ListEntryNodeDataWithSchema extends AbstractMountPointDataWithSchema<ListSchemaNode> {
33 private static final class Keyed extends ListEntryNodeDataWithSchema {
34 private final Map<QName, SimpleNodeDataWithSchema<?>> keyValues = new HashMap<>();
35 // This template results in Maps in schema definition order
36 private final ImmutableMapTemplate<QName> predicateTemplate;
38 Keyed(final ListSchemaNode schema, final List<QName> keyDef) {
40 predicateTemplate = ImmutableMapTemplate.ordered(keyDef);
44 public void addChild(final AbstractNodeDataWithSchema<?> newChild) {
45 final DataSchemaNode childSchema = newChild.getSchema();
46 if (childSchema instanceof LeafSchemaNode) {
47 final QName childName = childSchema.getQName();
48 if (predicateTemplate.keySet().contains(childName)) {
49 verify(newChild instanceof SimpleNodeDataWithSchema);
50 keyValues.put(childName, (SimpleNodeDataWithSchema<?>)newChild);
53 super.addChild(newChild);
57 public void write(final NormalizedNodeStreamWriter writer, final NormalizedMetadataStreamWriter metaWriter)
59 writer.nextDataSchemaNode(getSchema());
60 final NodeIdentifierWithPredicates identifier = NodeIdentifierWithPredicates.of(getSchema().getQName(),
61 predicateTemplate.instantiateTransformed(keyValues, (key, node) -> node.getValue()));
63 writer.startMapEntryNode(identifier, childSizeHint());
64 writeMetadata(metaWriter);
65 super.write(writer, metaWriter);
70 private static final class Unkeyed extends ListEntryNodeDataWithSchema {
71 Unkeyed(final ListSchemaNode schema) {
76 public void write(final NormalizedNodeStreamWriter writer, final NormalizedMetadataStreamWriter metaWriter)
78 writer.nextDataSchemaNode(getSchema());
79 writer.startUnkeyedListItem(provideNodeIdentifier(), childSizeHint());
80 super.write(writer, metaWriter);
85 ListEntryNodeDataWithSchema(final ListSchemaNode schema) {
89 public static ListEntryNodeDataWithSchema forSchema(final ListSchemaNode schema) {
90 final List<QName> keyDef = schema.getKeyDefinition();
91 return keyDef.isEmpty() ? new Unkeyed(schema) : new Keyed(schema, keyDef);