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.Preconditions.checkState;
12 import java.io.IOException;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.LinkedHashMap;
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.NormalizedNodeStreamAttributeWriter;
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 class ListEntryNodeDataWithSchema extends CompositeNodeDataWithSchema {
34 private final Map<QName, SimpleNodeDataWithSchema> qnameToKeys = new HashMap<>();
36 public ListEntryNodeDataWithSchema(final DataSchemaNode schema) {
41 public void addChild(final AbstractNodeDataWithSchema newChild) {
42 final DataSchemaNode childSchema = newChild.getSchema();
43 if (childSchema instanceof LeafSchemaNode && isPartOfKey((LeafSchemaNode) childSchema)) {
44 qnameToKeys.put(childSchema.getQName(), (SimpleNodeDataWithSchema)newChild);
46 super.addChild(newChild);
49 private boolean isPartOfKey(final LeafSchemaNode potentialKey) {
50 for (QName qname : ((ListSchemaNode) getSchema()).getKeyDefinition()) {
51 if (qname.equals(potentialKey.getQName())) {
59 public void write(final NormalizedNodeStreamWriter writer) throws IOException {
60 final Collection<QName> keyDef = ((ListSchemaNode) getSchema()).getKeyDefinition();
61 if (keyDef.isEmpty()) {
62 writer.nextDataSchemaNode(getSchema());
63 writer.startUnkeyedListItem(provideNodeIdentifier(), childSizeHint());
69 checkState(keyDef.size() == qnameToKeys.size(), "Input is missing some of the keys of %s",
70 getSchema().getQName());
72 // Need to restore schema order...
73 final Map<QName, Object> predicates = new LinkedHashMap<>();
74 for (QName qname : keyDef) {
75 predicates.put(qname, qnameToKeys.get(qname).getValue());
78 writer.nextDataSchemaNode(getSchema());
80 if (writer instanceof NormalizedNodeStreamAttributeWriter && getAttributes() != null) {
81 ((NormalizedNodeStreamAttributeWriter) writer).startMapEntryNode(
82 new NodeIdentifierWithPredicates(getSchema().getQName(), predicates), childSizeHint(),
85 writer.startMapEntryNode(new NodeIdentifierWithPredicates(getSchema().getQName(), predicates),