Do not create OpenApiObject twice 21/105821/10
authorPeter Suna <[email protected]>
Wed, 3 May 2023 14:29:30 +0000 (16:29 +0200)
committerIvan Hrasko <[email protected]>
Fri, 12 May 2023 10:43:32 +0000 (12:43 +0200)
Refactor the getMountPointApi method to prevent the creation
of two OpenApiObject instances. Currently,
this duplication occurs because the method is responsible for both
creating the required paths and generating the OpenApiObject.

To improve the code, separate these two functions and use only
the method that creates the data-store paths. This change will make
the code cleaner and more efficient, and help to avoid any potential
issues caused by duplicate objects.

JIRA: NETCONF-1007
Change-Id: I5ff38b706ce34c270f43fd4cf00a98efcbdaf888
Signed-off-by: Peter Suna <[email protected]>
Signed-off-by: Ivan Hrasko <[email protected]>
restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/mountpoints/MountPointOpenApi.java

index 658b43b955d3b17882babe9d35bc2afb70e18bec..180821495e8575a28281dfb2ed74e7a37940d291 100644 (file)
@@ -17,7 +17,6 @@ import static org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuild
 import static org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.SUMMARY_SEPARATOR;
 import static org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.TAGS_KEY;
 import static org.opendaylight.netconf.sal.rest.doc.model.builder.OperationBuilder.buildTagsValue;
-import static org.opendaylight.netconf.sal.rest.doc.util.JsonUtil.addFields;
 
 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
 import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -178,25 +177,14 @@ public class MountPointOpenApi implements DOMMountPointListener, AutoCloseable {
             range = Optional.of(Range.closed(start, end));
         }
 
-        // FIXME get rid of this object and thus eliminate need to mutate fields
-        final OpenApiObject doc;
-
         final OpenApiObject openApiObject = openApiGenerator.getAllModulesDoc(uriInfo, range, context,
                 Optional.of(deviceName), urlPrefix, definitionNames);
-
         if (includeDataStore) {
-            doc = generateDataStoreApiDoc(uriInfo, urlPrefix, deviceName);
-            // Creating mutable copy of map
-            var paths = new HashMap<>(doc.getPaths());
-            paths.putAll(openApiObject.getPaths());
-            doc.setPaths(paths);
-            doc.getInfo().setTitle(openApiObject.getInfo().getTitle());
-            addFields(doc.getComponents().getSchemas(), openApiObject.getComponents().getSchemas().fields());
-        } else {
-            doc = openApiObject;
+            final var paths = new HashMap<>(openApiObject.getPaths());
+            paths.putAll(getDataStoreApiPaths(urlPrefix, deviceName));
+            openApiObject.setPaths(paths);
         }
-
-        return doc;
+        return openApiObject;
     }
 
     private static String extractDeviceName(final YangInstanceIdentifier iid) {
@@ -204,14 +192,14 @@ public class MountPointOpenApi implements DOMMountPointListener, AutoCloseable {
                 .values().getElement().toString();
     }
 
-    private OpenApiObject generateDataStoreApiDoc(final UriInfo uriInfo, final String context,
-            final String deviceName) {
-        final OpenApiObject declaration = openApiGenerator.createOpenApiObject(
-                openApiGenerator.createSchemaFromUriInfo(uriInfo),
-                openApiGenerator.createHostFromUriInfo(uriInfo),
-                BASE_PATH,
-                context);
+    private OpenApiObject generateDataStoreApiDoc(final UriInfo info, final String context, final String deviceName) {
+        final var openApiObject = openApiGenerator.createOpenApiObject(openApiGenerator.createSchemaFromUriInfo(info),
+                openApiGenerator.createHostFromUriInfo(info), BASE_PATH, context);
+        openApiObject.setPaths(getDataStoreApiPaths(context, deviceName));
+        return openApiObject;
+    }
 
+    private Map<String, Path> getDataStoreApiPaths(final String context, final String deviceName) {
         final var data = new Path();
         data.setGet(createGetPathItem("data",
                 "Queries the config (startup) datastore on the mounted hosted.", deviceName));
@@ -220,14 +208,12 @@ public class MountPointOpenApi implements DOMMountPointListener, AutoCloseable {
         operations.setGet(createGetPathItem("operations",
                 "Queries the available operations (RPC calls) on the mounted hosted.", deviceName));
 
-        final var paths = Map.of(openApiGenerator.getResourcePath("data", context), data,
-                openApiGenerator.getResourcePath("operations", context), operations);
-        declaration.setPaths(paths);
-
-        return declaration;
+        return Map.of(openApiGenerator.getResourcePath("data", context), data,
+            openApiGenerator.getResourcePath("operations", context), operations);
     }
 
-    private ObjectNode createGetPathItem(final String resourceType, final String description, final String deviceName) {
+    private static ObjectNode createGetPathItem(final String resourceType, final String description,
+            final String deviceName) {
         final ObjectNode operationObject = JsonNodeFactory.instance.objectNode();
         operationObject.put(DESCRIPTION_KEY, description);
         operationObject.put(SUMMARY_KEY, HttpMethod.GET + SUMMARY_SEPARATOR + deviceName + SUMMARY_SEPARATOR