Merge "Added tests for yang.model.util"
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / ListEntryNodeDataWithSchema.java
1 /*
2  * Copyright (c) 2014 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.codec.gson;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
13
14 import java.io.IOException;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
19 import javax.annotation.Nonnull;
20
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
23 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
27
28 class ListEntryNodeDataWithSchema extends CompositeNodeDataWithSchema {
29     private static final Function<SimpleNodeDataWithSchema, Object> VALUE_FUNCTION = new Function<SimpleNodeDataWithSchema, Object>() {
30         @Override
31         public Object apply(@Nonnull final SimpleNodeDataWithSchema input) {
32             return input.getValue();
33         }
34     };
35
36     private final Map<QName, SimpleNodeDataWithSchema> qNameToKeys = new HashMap<>();
37
38     public ListEntryNodeDataWithSchema(final DataSchemaNode schema) {
39         super(schema);
40     }
41
42     @Override
43     public void addChild(final AbstractNodeDataWithSchema newChild) {
44         final DataSchemaNode childSchema = newChild.getSchema();
45         if (childSchema instanceof LeafSchemaNode && isPartOfKey((LeafSchemaNode) childSchema)) {
46             qNameToKeys.put(childSchema.getQName(), (SimpleNodeDataWithSchema)newChild);
47         }
48         super.addChild(newChild);
49     }
50
51     private boolean isPartOfKey(final LeafSchemaNode potentialKey) {
52         List<QName> keys = ((ListSchemaNode) getSchema()).getKeyDefinition();
53         for (QName qName : keys) {
54             if (qName.equals(potentialKey.getQName())) {
55                 return true;
56             }
57         }
58         return false;
59     }
60
61     @Override
62     public void write(final NormalizedNodeStreamWriter writer) throws IOException {
63         final int keyCount = ((ListSchemaNode) getSchema()).getKeyDefinition().size();
64         if (keyCount == 0) {
65             writer.startUnkeyedListItem(provideNodeIdentifier(), childSizeHint());
66             super.write(writer);
67             writer.endNode();
68             return;
69         }
70
71         Preconditions.checkState(keyCount == qNameToKeys.size(), "Input is missing some of the keys of %s", getSchema().getQName());
72         writer.startMapEntryNode(
73             new NodeIdentifierWithPredicates(getSchema().getQName(), Maps.transformValues(qNameToKeys, VALUE_FUNCTION)),
74             childSizeHint());
75         super.write(writer);
76         writer.endNode();
77     }
78 }