Refactor ListEntryNodeDataWithSchema
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / ListEntryNodeDataWithSchema.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.util;
9
10 import static com.google.common.base.Verify.verify;
11
12 import java.io.IOException;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
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.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;
24
25 /**
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.
28  *
29  * <p>
30  * Represents a YANG list entry node.
31  */
32 public abstract class ListEntryNodeDataWithSchema extends CompositeNodeDataWithSchema<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;
37
38         Keyed(final ListSchemaNode schema, final List<QName> keyDef) {
39             super(schema);
40             predicateTemplate = ImmutableMapTemplate.ordered(keyDef);
41         }
42
43         @Override
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);
51                 }
52             }
53             super.addChild(newChild);
54         }
55
56         @Override
57         public void write(final NormalizedNodeStreamWriter writer) throws IOException {
58             writer.nextDataSchemaNode(getSchema());
59             final NodeIdentifierWithPredicates identifier = new NodeIdentifierWithPredicates(getSchema().getQName(),
60                 predicateTemplate.instantiateTransformed(keyValues, (key, node) -> node.getValue()));
61
62             if (writer instanceof NormalizedNodeStreamAttributeWriter && getAttributes() != null) {
63                 ((NormalizedNodeStreamAttributeWriter) writer).startMapEntryNode(identifier, childSizeHint(),
64                     getAttributes());
65             } else {
66                 writer.startMapEntryNode(identifier, childSizeHint());
67             }
68
69             super.write(writer);
70             writer.endNode();
71         }
72     }
73
74     private static final class Unkeyed extends ListEntryNodeDataWithSchema {
75         Unkeyed(final ListSchemaNode schema) {
76             super(schema);
77         }
78
79         @Override
80         public void write(final NormalizedNodeStreamWriter writer) throws IOException {
81             writer.nextDataSchemaNode(getSchema());
82             writer.startUnkeyedListItem(provideNodeIdentifier(), childSizeHint());
83             super.write(writer);
84             writer.endNode();
85         }
86     }
87
88     ListEntryNodeDataWithSchema(final ListSchemaNode schema) {
89         super(schema);
90     }
91
92     public static ListEntryNodeDataWithSchema forSchema(final ListSchemaNode schema) {
93         final List<QName> keyDef = schema.getKeyDefinition();
94         return keyDef.isEmpty() ? new Unkeyed(schema) :  new Keyed(schema, keyDef);
95     }
96 }