OpenAPI: Add possibility to change base path
[netconf.git] / restconf / sal-rest-docgen / src / main / java / org / opendaylight / netconf / sal / rest / doc / impl / ApiDocServiceImpl.java
index bfac921a818d633fcf80b88448614e17df810845..3149551c50fb58e410fcb743b6591705ef90d7a8 100644 (file)
@@ -7,18 +7,26 @@
  */
 package org.opendaylight.netconf.sal.rest.doc.impl;
 
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.VisibleForTesting;
 import java.util.List;
-import java.util.Objects;
+import java.util.Optional;
 import java.util.stream.Collectors;
-
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
-
+import org.opendaylight.mdsal.dom.api.DOMMountPointService;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.netconf.sal.rest.doc.api.ApiDocService;
-import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
-import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
-import org.opendaylight.netconf.sal.rest.doc.swagger.MountPointInstance;
-import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
+import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointOpenApi;
+import org.opendaylight.netconf.sal.rest.doc.openapi.MountPointInstance;
+import org.opendaylight.netconf.sal.rest.doc.openapi.OpenApiObject;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+
 
 /**
  * This service generates swagger (See
@@ -33,46 +41,45 @@ import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
  * to work around this limitation, but given that this API is a dev only tool
  * and not dependent UI, this was the fastest work around.
  */
-public class ApiDocServiceImpl implements ApiDocService {
-
+@Component
+@Singleton
+public final class ApiDocServiceImpl implements ApiDocService {
+    // FIXME: make this configurable
     public static final int DEFAULT_PAGESIZE = 20;
+
     // Query parameter
-    private static final String TOTAL_PAGES = "totalPages";
     private static final String PAGE_NUM = "pageNum";
 
-    public enum URIType { RFC8040, DRAFT02 }
-
-    private final MountPointSwagger mountPointSwaggerDraft02;
-    private final MountPointSwagger mountPointSwaggerRFC8040;
-    private final ApiDocGeneratorDraftO2 apiDocGeneratorDraft02;
+    private final MountPointOpenApi mountPointOpenApiRFC8040;
     private final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040;
 
-    public ApiDocServiceImpl(MountPointSwaggerGeneratorDraft02 mountPointSwaggerGeneratorDraft02,
-            MountPointSwaggerGeneratorRFC8040 mountPointSwaggerGeneratorRFC8040,
-            ApiDocGeneratorDraftO2 apiDocGeneratorDraft02, ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040) {
-        this.mountPointSwaggerDraft02 =
-                Objects.requireNonNull(mountPointSwaggerGeneratorDraft02).getMountPointSwagger();
-        this.mountPointSwaggerRFC8040 =
-                Objects.requireNonNull(mountPointSwaggerGeneratorRFC8040).getMountPointSwagger();
-        this.apiDocGeneratorDraft02 = Objects.requireNonNull(apiDocGeneratorDraft02);
-        this.apiDocGeneratorRFC8040 = Objects.requireNonNull(apiDocGeneratorRFC8040);
+    @Inject
+    @Activate
+    public ApiDocServiceImpl(final @Reference DOMSchemaService schemaService,
+                             final @Reference DOMMountPointService mountPointService) {
+        this(new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService),
+            new ApiDocGeneratorRFC8040(schemaService));
     }
 
-    /**
-     * Generates index document for Swagger UI. This document lists out all
-     * modules with link to get APIs for each module. The API for each module is
-     * served by <code> getDocByModule()</code> method.
-     */
-    @Override
-    public synchronized Response getRootDoc(final UriInfo uriInfo) {
-        final ResourceList rootDoc;
-        if (isNew(uriInfo).equals(URIType.RFC8040)) {
-            rootDoc = apiDocGeneratorRFC8040.getResourceListing(uriInfo, URIType.RFC8040);
-        } else {
-            rootDoc = apiDocGeneratorDraft02.getResourceListing(uriInfo, URIType.DRAFT02);
-        }
+    public ApiDocServiceImpl(final DOMSchemaService schemaService,
+                             final DOMMountPointService mountPointService,
+                             final String basePath) {
+        this(new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService, basePath),
+            new ApiDocGeneratorRFC8040(schemaService, basePath));
+    }
 
-        return Response.ok(rootDoc).build();
+    @VisibleForTesting
+    ApiDocServiceImpl(final MountPointOpenApiGeneratorRFC8040 mountPointOpenApiGeneratorRFC8040,
+                      final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040) {
+        mountPointOpenApiRFC8040 = requireNonNull(mountPointOpenApiGeneratorRFC8040).getMountPointOpenApi();
+        this.apiDocGeneratorRFC8040 = requireNonNull(apiDocGeneratorRFC8040);
+    }
+
+    @Override
+    public synchronized Response getAllModulesDoc(final UriInfo uriInfo) {
+        final DefinitionNames definitionNames = new DefinitionNames();
+        final OpenApiObject doc = apiDocGeneratorRFC8040.getAllModulesDoc(uriInfo, definitionNames);
+        return Response.ok(doc).build();
     }
 
     /**
@@ -80,14 +87,9 @@ public class ApiDocServiceImpl implements ApiDocService {
      */
     @Override
     public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
-        final ApiDeclaration doc;
-        if (isNew(uriInfo).equals(URIType.RFC8040)) {
-            doc = apiDocGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo, URIType.RFC8040);
-        } else {
-            doc = apiDocGeneratorDraft02.getApiDeclaration(module, revision, uriInfo, URIType.DRAFT02);
-        }
-
-        return Response.ok(doc).build();
+        return Response.ok(
+            apiDocGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo))
+            .build();
     }
 
     /**
@@ -100,65 +102,27 @@ public class ApiDocServiceImpl implements ApiDocService {
 
     @Override
     public synchronized Response getListOfMounts(final UriInfo uriInfo) {
-        final MountPointSwagger mountPointSwagger;
-        if (isNew(uriInfo).equals(URIType.RFC8040)) {
-            mountPointSwagger = mountPointSwaggerRFC8040;
-        } else {
-            mountPointSwagger = mountPointSwaggerDraft02;
-        }
-        final List<MountPointInstance> entity = mountPointSwagger
+        final List<MountPointInstance> entity = mountPointOpenApiRFC8040
                 .getInstanceIdentifiers().entrySet().stream()
                 .map(MountPointInstance::new).collect(Collectors.toList());
         return Response.ok(entity).build();
     }
 
-    @Override
-    public synchronized Response getMountRootDoc(final String instanceNum, final UriInfo uriInfo) {
-        final ResourceList resourceList;
-
-        if (uriInfo.getQueryParameters().getFirst(TOTAL_PAGES) != null) {
-            if (isNew(uriInfo).equals(URIType.RFC8040)) {
-                resourceList = mountPointSwaggerRFC8040.getResourceList(uriInfo, Long.parseLong(instanceNum),
-                    URIType.RFC8040);
-            } else {
-                resourceList = mountPointSwaggerDraft02.getResourceList(uriInfo, Long.parseLong(instanceNum),
-                    URIType.DRAFT02);
-            }
-            int size = resourceList.getApis().size();
-            return Response.ok(size % DEFAULT_PAGESIZE == 0 ? size / DEFAULT_PAGESIZE
-                    : size / DEFAULT_PAGESIZE + 1).build();
-        }
-
-        final int pageNum = Integer.parseInt(uriInfo.getQueryParameters().getFirst(PAGE_NUM));
-
-        if (isNew(uriInfo).equals(URIType.RFC8040)) {
-            resourceList = mountPointSwaggerRFC8040.getResourceList(uriInfo, Long.parseLong(instanceNum), pageNum,
-                false, URIType.RFC8040);
-        } else {
-            resourceList = mountPointSwaggerDraft02.getResourceList(uriInfo, Long.parseLong(instanceNum), pageNum,
-                false, URIType.DRAFT02);
-        }
-        return Response.ok(resourceList).build();
-    }
-
     @Override
     public synchronized Response getMountDocByModule(final String instanceNum, final String module,
-            final String revision, final UriInfo uriInfo) {
-        final ApiDeclaration api;
-        if (isNew(uriInfo).equals(URIType.RFC8040)) {
-            api = mountPointSwaggerRFC8040
-                .getMountPointApi(uriInfo, Long.parseLong(instanceNum), module, revision, URIType.RFC8040);
-        } else {
-            api = mountPointSwaggerDraft02
-                .getMountPointApi(uriInfo, Long.parseLong(instanceNum), module, revision, URIType.DRAFT02);
-        }
+                                                     final String revision, final UriInfo uriInfo) {
+        final OpenApiObject api = mountPointOpenApiRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum),
+            module, revision);
         return Response.ok(api).build();
     }
 
-    private static URIType isNew(final UriInfo uriInfo) {
-        if (uriInfo.getBaseUri().toString().contains("/18/")) {
-            return URIType.RFC8040;
-        }
-        return URIType.DRAFT02;
+    @Override
+    public synchronized Response getMountDoc(final String instanceNum, final UriInfo uriInfo) {
+        final String stringPageNum = uriInfo.getQueryParameters().getFirst(PAGE_NUM);
+        final Optional<Integer> pageNum = stringPageNum != null ? Optional.of(Integer.valueOf(stringPageNum))
+                : Optional.empty();
+        final OpenApiObject api = mountPointOpenApiRFC8040.getMountPointApi(uriInfo,
+                Long.parseLong(instanceNum), pageNum);
+        return Response.ok(api).build();
     }
 }