Use Java Path object instead of JsonNode
[netconf.git] / restconf / sal-rest-docgen / src / main / java / org / opendaylight / netconf / sal / rest / doc / mountpoints / MountPointOpenApi.java
index 053fca881e838c1044c08dde681116aee1bde845..658b43b955d3b17882babe9d35bc2afb70e18bec 100644 (file)
@@ -37,6 +37,7 @@ import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.netconf.sal.rest.doc.impl.BaseYangOpenApiGenerator;
 import org.opendaylight.netconf.sal.rest.doc.impl.DefinitionNames;
 import org.opendaylight.netconf.sal.rest.doc.openapi.OpenApiObject;
+import org.opendaylight.netconf.sal.rest.doc.openapi.Path;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
@@ -185,9 +186,12 @@ public class MountPointOpenApi implements DOMMountPointListener, AutoCloseable {
 
         if (includeDataStore) {
             doc = generateDataStoreApiDoc(uriInfo, urlPrefix, deviceName);
-            addFields(doc.getPaths() ,openApiObject.getPaths().fields());
-            addFields(doc.getComponents().getSchemas(), openApiObject.getComponents().getSchemas().fields());
+            // 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;
         }
@@ -208,22 +212,23 @@ public class MountPointOpenApi implements DOMMountPointListener, AutoCloseable {
                 BASE_PATH,
                 context);
 
-        final ObjectNode pathsObject = JsonNodeFactory.instance.objectNode();
-        createGetPathItem("data", "Queries the config (startup) datastore on the mounted hosted.",
-                context, deviceName, pathsObject);
-        createGetPathItem("operations", "Queries the available operations (RPC calls) on the mounted hosted.",
-                context, deviceName, pathsObject);
+        final var data = new Path();
+        data.setGet(createGetPathItem("data",
+                "Queries the config (startup) datastore on the mounted hosted.", deviceName));
+
+        final var operations = new Path();
+        operations.setGet(createGetPathItem("operations",
+                "Queries the available operations (RPC calls) on the mounted hosted.", deviceName));
 
-        declaration.setPaths(pathsObject);
+        final var paths = Map.of(openApiGenerator.getResourcePath("data", context), data,
+                openApiGenerator.getResourcePath("operations", context), operations);
+        declaration.setPaths(paths);
 
         return declaration;
     }
 
-    private void createGetPathItem(final String resourceType, final String description, final String context,
-            final String deviceName, final ObjectNode pathsObject) {
-        final ObjectNode pathItem = JsonNodeFactory.instance.objectNode();
+    private ObjectNode createGetPathItem(final String resourceType, final String description, final String deviceName) {
         final ObjectNode operationObject = JsonNodeFactory.instance.objectNode();
-        pathItem.set("get", operationObject);
         operationObject.put(DESCRIPTION_KEY, description);
         operationObject.put(SUMMARY_KEY, HttpMethod.GET + SUMMARY_SEPARATOR + deviceName + SUMMARY_SEPARATOR
                 + resourceType);
@@ -233,7 +238,7 @@ public class MountPointOpenApi implements DOMMountPointListener, AutoCloseable {
         final ObjectNode responses = JsonNodeFactory.instance.objectNode();
         responses.set(String.valueOf(Response.Status.OK.getStatusCode()), okResponse);
         operationObject.set(RESPONSES_KEY, responses);
-        pathsObject.set(openApiGenerator.getResourcePath(resourceType, context), pathItem);
+        return operationObject;
     }
 
     @Override