Implement depth parameter
[netconf.git] / restconf / restconf-openapi / src / main / java / org / opendaylight / restconf / openapi / model / NodeSchemaEntity.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. 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.restconf.openapi.model;
9
10 import static org.opendaylight.restconf.openapi.model.PropertyEntity.isSchemaNodeMandatory;
11 import static org.opendaylight.restconf.openapi.util.RestDocgenUtil.widthList;
12
13 import com.fasterxml.jackson.core.JsonGenerator;
14 import java.io.IOException;
15 import java.util.HashMap;
16 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.restconf.openapi.impl.DefinitionNames;
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
24 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
25
26 public final class NodeSchemaEntity extends SchemaEntity {
27
28     public NodeSchemaEntity(final @NonNull SchemaNode value, final @NonNull String title,
29             final @Nullable String discriminator, final @NonNull String type,
30             final @NonNull SchemaInferenceStack context, final @NonNull String parentName, final boolean isParentConfig,
31             final @NonNull DefinitionNames definitionNames, final @NonNull Integer width,
32             final @NonNull Integer depth, final @NonNull Integer nodeDepth) {
33         super(value, title, discriminator, type, context, parentName, isParentConfig, definitionNames, width, depth,
34             nodeDepth);
35     }
36
37     @Override
38     void generateProperties(final @NonNull JsonGenerator generator, final @NonNull List<String> required)
39             throws IOException {
40         if (depth > 0 && nodeDepth + 1 > depth) {
41             return;
42         }
43         final var childNodes = new HashMap<String, DataSchemaNode>();
44         final var dataSchemaNodes = widthList((DataNodeContainer) value(), width);
45         for (final var childNode : dataSchemaNodes) {
46             childNodes.put(childNode.getQName().getLocalName(), childNode);
47         }
48         final boolean isValueConfig = ((DataSchemaNode) value()).isConfiguration();
49
50         for (final var childNode : childNodes.values()) {
51             if (shouldBeAddedAsProperty(childNode, isValueConfig)) {
52                 new PropertyEntity(childNode, generator, stack(), required, parentName() + "_"
53                     + value().getQName().getLocalName(), isValueConfig, definitionNames(), width, depth, nodeDepth);
54             }
55         }
56     }
57
58     private static boolean shouldBeAddedAsProperty(final DataSchemaNode childNode, final boolean isValueConfig) {
59         final boolean isChildNodeConfig = childNode.isConfiguration();
60         if (childNode instanceof ContainerSchemaNode) {
61             return isChildNodeConfig || isSchemaNodeMandatory(childNode) || !isValueConfig;
62         }
63         return isChildNodeConfig || !isValueConfig;
64     }
65 }