Delete all always null returning methods
[netconf.git] / restconf / restconf-openapi / src / main / java / org / opendaylight / restconf / openapi / model / OperationEntity.java
1 /*
2  * Copyright (c) 2023 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 java.util.Objects.requireNonNull;
11 import static javax.ws.rs.core.Response.Status.OK;
12
13 import com.fasterxml.jackson.core.JsonGenerator;
14 import java.io.IOException;
15 import java.util.List;
16 import javax.ws.rs.HttpMethod;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
20
21 /**
22  * Archetype for an Operation.
23  */
24 public abstract sealed class OperationEntity extends OpenApiEntity permits DeleteEntity, GetEntity, PatchEntity,
25         PostEntity, PutEntity {
26     protected static final String SCHEMA = "schema";
27     protected static final String SUMMARY_TEMPLATE = "%s - %s - %s - %s";
28     protected static final String RESPONSES = "responses";
29     protected static final String DESCRIPTION = "description";
30     protected static final String OBJECT = "object";
31     protected static final String CONTENT = "content";
32     protected static final String COMPONENTS_PREFIX = "#/components/schemas/";
33     protected static final String PROPERTIES = "properties";
34     protected static final String TYPE = "type";
35     protected static final String ARRAY = "array";
36     protected static final String ITEMS = "items";
37     protected static final String REF = "$ref";
38     protected static final String PARAMETERS = "parameters";
39     protected static final String SUMMARY = "summary";
40     protected static final String NAME = "name";
41     protected static final String IN = "in";
42     protected static final String REQUIRED = "required";
43     protected static final String REQUEST_BODY = "requestBody";
44
45     private final @Nullable SchemaNode schema;
46     private final @NonNull String deviceName;
47     private final @NonNull String moduleName;
48     private final @Nullable String refPath;
49     private final @Nullable List<ParameterEntity> parameters;
50
51     protected @Nullable SchemaNode schema() {
52         return schema;
53     }
54
55     protected @NonNull String deviceName() {
56         return deviceName;
57     }
58
59     protected @NonNull String moduleName() {
60         return moduleName;
61     }
62
63     protected @Nullable List<ParameterEntity> parameters() {
64         return parameters;
65     }
66
67     protected @Nullable String refPath() {
68         return refPath;
69     }
70
71     public OperationEntity(final @Nullable SchemaNode schema, final @NonNull String deviceName,
72             final @NonNull String moduleName, final @Nullable List<ParameterEntity> parameters,
73             final @Nullable String refPath) {
74         this.schema = schema;
75         this.deviceName = requireNonNull(deviceName);
76         this.moduleName = requireNonNull(moduleName);
77         this.parameters = parameters;
78         this.refPath = refPath;
79     }
80
81     @Override
82     public void generate(@NonNull JsonGenerator generator) throws IOException {
83         if (schema() == null) {
84             generateGetRoot(generator, moduleName());
85         } else {
86             generator.writeObjectFieldStart(operation());
87             generateBasics(generator);
88             generateRequestBody(generator);
89             generateResponses(generator);
90             generateTags(generator);
91             generateParams(generator);
92             generator.writeEndObject();
93         }
94     }
95
96     public void generateBasics(@NonNull JsonGenerator generator) throws IOException {
97         generator.writeStringField(DESCRIPTION, description());
98         generator.writeStringField(SUMMARY, summary());
99     }
100
101     protected @NonNull abstract String operation();
102
103     @NonNull String description() {
104         return schema == null ? "" : schema.getDescription().orElse("");
105     }
106
107     @Nullable String nodeName() {
108         return schema == null ? null : schema.getQName().getLocalName();
109     }
110
111     @NonNull abstract String summary();
112
113     void generateRequestBody(final @NonNull JsonGenerator generator) throws IOException {
114         // No-op
115     }
116
117     void generateResponses(final @NonNull JsonGenerator generator) throws IOException {
118         // No-op
119     }
120
121     void generateTags(final @NonNull JsonGenerator generator) throws IOException {
122         generator.writeArrayFieldStart("tags");
123         generator.writeString(deviceName + " " + moduleName);
124         generator.writeEndArray();
125     }
126
127     void generateParams(final @NonNull JsonGenerator generator) throws IOException {
128         generator.writeArrayFieldStart(PARAMETERS);
129         final var parametersList = requireNonNull(parameters());
130         if (!parametersList.isEmpty()) {
131             for (final var parameter : parametersList) {
132                 generator.writeStartObject();
133                 generator.writeStringField(NAME, parameter.name());
134                 generator.writeStringField(IN, parameter.in());
135                 generator.writeBooleanField(REQUIRED, parameter.required());
136                 generator.writeObjectFieldStart(SCHEMA);
137                 if (parameter.schema() != null) {
138                     generator.writeStringField(TYPE, parameter.schema().type());
139                 }
140                 generator.writeEndObject(); //end of schema
141                 if (parameter.description() != null) {
142                     generator.writeStringField(DESCRIPTION, parameter.description());
143                 }
144                 generator.writeEndObject(); //end of parameter
145             }
146         }
147         generator.writeEndArray(); //end of params
148     }
149
150
151     protected static void generateMediaTypeSchemaRef(final @NonNull JsonGenerator generator,
152             final @NonNull String mediaType, final @NonNull String ref) throws IOException {
153         generator.writeObjectFieldStart(mediaType);
154         generator.writeObjectFieldStart(SCHEMA);
155         generator.writeStringField(REF, ref);
156         generator.writeEndObject();
157         generator.writeEndObject();
158     }
159
160     void generateGetRoot(final @NonNull JsonGenerator generator, final @NonNull String resourceType)
161             throws IOException {
162         generator.writeObjectFieldStart("get");
163         if (resourceType.equals("data")) {
164             generator.writeStringField(DESCRIPTION, "Queries the config (startup) datastore on the mounted hosted.");
165         } else if (resourceType.equals("operations")) {
166             generator.writeStringField(DESCRIPTION,
167                 "Queries the available operations (RPC calls) on the mounted hosted.");
168         }
169         generator.writeObjectFieldStart(RESPONSES);
170         generator.writeObjectFieldStart(String.valueOf(OK.getStatusCode()));
171         generator.writeStringField(DESCRIPTION, "OK");
172         generator.writeEndObject(); //end of 200
173         generator.writeEndObject(); // end of responses
174         final var summary = HttpMethod.GET + " - " + deviceName() + " - datastore - " + resourceType;
175         generator.writeStringField(SUMMARY, summary);
176         generator.writeArrayFieldStart("tags");
177         generator.writeString(deviceName + " GET root");
178         generator.writeEndArray(); //end of tags
179         generator.writeEndObject(); //end of get
180     }
181 }