From f2d036b03196cdc2b3ff07224a4845b178ae8695 Mon Sep 17 00:00:00 2001 From: Peter Suna Date: Wed, 3 May 2023 16:29:30 +0200 Subject: [PATCH] Do not create OpenApiObject twice 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 Signed-off-by: Ivan Hrasko --- .../doc/mountpoints/MountPointOpenApi.java | 44 +++++++------------ 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/mountpoints/MountPointOpenApi.java b/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/mountpoints/MountPointOpenApi.java index 658b43b955..180821495e 100644 --- a/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/mountpoints/MountPointOpenApi.java +++ b/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/mountpoints/MountPointOpenApi.java @@ -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 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 -- 2.36.6