Clean up more Sonar warnings
[yangtools.git] / data / 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 com.google.common.base.VerifyException;
11 import java.io.IOException;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import org.eclipse.jdt.annotation.NonNull;
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.NormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter.MetadataExtension;
21 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
23
24 /**
25  * Utility class used for tracking parser state as needed by a StAX-like parser.
26  * This class is to be used only by respective XML and JSON parsers in yang-data-codec-xml and yang-data-codec-gson.
27  *
28  * <p>
29  * Represents a YANG list entry node.
30  */
31 public abstract sealed class ListEntryNodeDataWithSchema extends AbstractMountPointDataWithSchema<ListSchemaNode> {
32     private static final class Keyed extends ListEntryNodeDataWithSchema {
33         private final Map<QName, SimpleNodeDataWithSchema<?>> keyValues = new HashMap<>();
34         // This template results in Maps in schema definition order
35         private final ImmutableMapTemplate<QName> predicateTemplate;
36
37         Keyed(final ListSchemaNode schema, final List<QName> keyDef) {
38             super(schema);
39             predicateTemplate = ImmutableMapTemplate.ordered(keyDef);
40         }
41
42         @Override
43         void addChild(final AbstractNodeDataWithSchema<?> newChild) {
44             if (newChild.getSchema() instanceof LeafSchemaNode leaf) {
45                 final var childName = leaf.getQName();
46                 if (predicateTemplate.keySet().contains(childName)) {
47                     if (newChild instanceof SimpleNodeDataWithSchema<?> simpleChild) {
48                         keyValues.put(childName, simpleChild);
49                     } else {
50                         throw new VerifyException("Unexpected child " + newChild);
51                     }
52                 }
53             }
54             super.addChild(newChild);
55         }
56
57         @Override
58         public void write(final NormalizedNodeStreamWriter writer, final MetadataExtension metaWriter)
59                 throws IOException {
60             final var schema = getSchema();
61             writer.nextDataSchemaNode(schema);
62
63             final var nodeType = schema.getQName();
64             final Map<QName, Object> predicates;
65             try {
66                 predicates = predicateTemplate.instantiateTransformed(keyValues, (key, node) -> node.getValue());
67             } catch (IllegalArgumentException e) {
68                 final var present = keyValues.keySet();
69                 final var module = nodeType.getModule();
70                 final var missing = predicateTemplate.keySet().stream()
71                     .filter(key -> !present.contains(key))
72                     .map(key -> module.equals(key.getModule()) ? key.getLocalName() : key)
73                     .distinct()
74                     .toList();
75                 throw new IOException("List entry " + nodeType + " is missing leaf values for " + missing, e);
76             }
77
78             writer.startMapEntryNode(NodeIdentifierWithPredicates.of(nodeType, predicates), childSizeHint());
79             writeMetadata(metaWriter);
80             super.write(writer, metaWriter);
81             writer.endNode();
82         }
83     }
84
85     private static final class Unkeyed extends ListEntryNodeDataWithSchema {
86         Unkeyed(final ListSchemaNode schema) {
87             super(schema);
88         }
89
90         @Override
91         public void write(final NormalizedNodeStreamWriter writer, final MetadataExtension metaWriter)
92                 throws IOException {
93             writer.nextDataSchemaNode(getSchema());
94             writer.startUnkeyedListItem(provideNodeIdentifier(), childSizeHint());
95             super.write(writer, metaWriter);
96             writer.endNode();
97         }
98     }
99
100     ListEntryNodeDataWithSchema(final ListSchemaNode schema) {
101         super(schema);
102     }
103
104     static @NonNull ListEntryNodeDataWithSchema forSchema(final ListSchemaNode schema) {
105         final var keyDef = schema.getKeyDefinition();
106         return keyDef.isEmpty() ? new Unkeyed(schema) : new Keyed(schema, keyDef);
107     }
108 }