Merge branch 'master' of ../controller
[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.rfc7952.data.api.StreamWriterMetadataExtension;
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;
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 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;
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, final StreamWriterMetadataExtension metaWriter)
58                 throws IOException {
59             writer.nextDataSchemaNode(getSchema());
60             final NodeIdentifierWithPredicates identifier = NodeIdentifierWithPredicates.of(getSchema().getQName(),
61                 predicateTemplate.instantiateTransformed(keyValues, (key, node) -> node.getValue()));
62
63             writer.startMapEntryNode(identifier, childSizeHint());
64             writeMetadata(metaWriter);
65             super.write(writer, metaWriter);
66             writer.endNode();
67         }
68     }
69
70     private static final class Unkeyed extends ListEntryNodeDataWithSchema {
71         Unkeyed(final ListSchemaNode schema) {
72             super(schema);
73         }
74
75         @Override
76         public void write(final NormalizedNodeStreamWriter writer, final StreamWriterMetadataExtension metaWriter)
77                 throws IOException {
78             writer.nextDataSchemaNode(getSchema());
79             writer.startUnkeyedListItem(provideNodeIdentifier(), childSizeHint());
80             super.write(writer, metaWriter);
81             writer.endNode();
82         }
83     }
84
85     ListEntryNodeDataWithSchema(final ListSchemaNode schema) {
86         super(schema);
87     }
88
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);
92     }
93 }