Update MRI projects for Aluminium
[netconf.git] / restconf / sal-rest-docgen / src / main / java / org / opendaylight / netconf / sal / rest / doc / impl / ApiDocServiceImpl.java
index f3294ec3d0665c53a0f90c9e9442ac242d34d0a0..377059638658c421454b4f34298b42b0cc8096bb 100644 (file)
@@ -7,26 +7,25 @@
  */
 package org.opendaylight.netconf.sal.rest.doc.impl;
 
-import java.io.ByteArrayOutputStream;
-import java.io.OutputStreamWriter;
-import java.util.Map.Entry;
-
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
-
-import org.json.JSONWriter;
 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;
 
 /**
- * This service generates swagger (See <a
- * href="https://helloreverb.com/developers/swagger"
+ * This service generates swagger (See
+ * <a href="https://helloreverb.com/developers/swagger"
  * >https://helloreverb.com/developers/swagger</a>) compliant documentation for
  * RESTCONF APIs. The output of this is used by embedded Swagger UI.
  *
- * <p>NOTE: These API's need to be synchronized due to bug 1198. Thread access to
+ * <p>
+ * NOTE: These API's need to be synchronized due to bug 1198. Thread access to
  * the SchemaContext is not synchronized properly and thus you can end up with
  * missing definitions without this synchronization. There are likely otherways
  * to work around this limitation, but given that this API is a dev only tool
@@ -34,10 +33,27 @@ import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
  */
 public class ApiDocServiceImpl implements ApiDocService {
 
-    private static final ApiDocService INSTANCE = new ApiDocServiceImpl();
+    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 ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040;
 
-    public static ApiDocService getInstance() {
-        return INSTANCE;
+    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);
     }
 
     /**
@@ -46,9 +62,13 @@ public class ApiDocServiceImpl implements ApiDocService {
      * served by <code> getDocByModule()</code> method.
      */
     @Override
-    public synchronized Response getRootDoc(UriInfo uriInfo) {
-        ApiDocGenerator generator = ApiDocGenerator.getInstance();
-        ResourceList rootDoc = generator.getResourceListing(uriInfo);
+    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);
+        }
 
         return Response.ok(rootDoc).build();
     }
@@ -57,10 +77,14 @@ public class ApiDocServiceImpl implements ApiDocService {
      * Generates Swagger compliant document listing APIs for module.
      */
     @Override
-    public synchronized Response getDocByModule(String module, String revision, UriInfo uriInfo) {
-        ApiDocGenerator generator = ApiDocGenerator.getInstance();
+    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);
+        }
 
-        ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
         return Response.ok(doc).build();
     }
 
@@ -68,45 +92,71 @@ public class ApiDocServiceImpl implements ApiDocService {
      * Redirects to embedded swagger ui.
      */
     @Override
-    public synchronized Response getApiExplorer(UriInfo uriInfo) {
-        return Response
-                .seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build())
-                .build();
+    public synchronized Response getApiExplorer(final UriInfo uriInfo) {
+        return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
     }
 
     @Override
-    public synchronized Response getListOfMounts(UriInfo uriInfo) {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos)) {
-            JSONWriter writer = new JSONWriter(streamWriter);
-            writer.array();
-            for (Entry<String, Long> entry : MountPointSwagger.getInstance()
-                    .getInstanceIdentifiers().entrySet()) {
-                writer.object();
-                writer.key("instance").value(entry.getKey());
-                writer.key("id").value(entry.getValue());
-                writer.endObject();
-            }
-            writer.endArray();
-        } catch (Exception e) {
-            return Response.status(500).entity(e.getMessage()).build();
+    public synchronized Response getListOfMounts(final UriInfo uriInfo) {
+        final MountPointSwagger mountPointSwagger;
+        if (isNew(uriInfo).equals(URIType.RFC8040)) {
+            mountPointSwagger = mountPointSwaggerRFC8040;
+        } else {
+            mountPointSwagger = mountPointSwaggerDraft02;
         }
-        return Response.status(200).entity(baos.toString()).build();
+        final List<MountPointInstance> entity = mountPointSwagger
+                .getInstanceIdentifiers().entrySet().stream()
+                .map(MountPointInstance::new).collect(Collectors.toList());
+        return Response.ok(entity).build();
     }
 
     @Override
-    public synchronized Response getMountRootDoc(String instanceNum, UriInfo uriInfo) {
-        ResourceList resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo,
-                Long.parseLong(instanceNum));
+    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(String instanceNum, String module,
-            String revision, UriInfo uriInfo) {
-        ApiDeclaration api = MountPointSwagger.getInstance().getMountPointApi(uriInfo,
-                Long.parseLong(instanceNum), module, revision);
+    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);
+        }
         return Response.ok(api).build();
     }
 
+    private static URIType isNew(final UriInfo uriInfo) {
+        if (uriInfo.getBaseUri().toString().contains("/18/")) {
+            return URIType.RFC8040;
+        }
+        return URIType.DRAFT02;
+    }
 }