Add pagination for mounted resources of apidocs
[netconf.git] / restconf / sal-rest-docgen / src / main / java / org / opendaylight / netconf / sal / rest / doc / impl / ApiDocServiceImpl.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.sal.rest.doc.impl;
9
10 import java.util.List;
11 import java.util.Objects;
12 import java.util.stream.Collectors;
13
14 import javax.ws.rs.core.Response;
15 import javax.ws.rs.core.UriInfo;
16
17 import org.opendaylight.netconf.sal.rest.doc.api.ApiDocService;
18 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
19 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
20 import org.opendaylight.netconf.sal.rest.doc.swagger.MountPointInstance;
21 import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
22
23 /**
24  * This service generates swagger (See
25  * <a href="https://helloreverb.com/developers/swagger"
26  * >https://helloreverb.com/developers/swagger</a>) compliant documentation for
27  * RESTCONF APIs. The output of this is used by embedded Swagger UI.
28  *
29  * <p>
30  * NOTE: These API's need to be synchronized due to bug 1198. Thread access to
31  * the SchemaContext is not synchronized properly and thus you can end up with
32  * missing definitions without this synchronization. There are likely otherways
33  * to work around this limitation, but given that this API is a dev only tool
34  * and not dependent UI, this was the fastest work around.
35  */
36 public class ApiDocServiceImpl implements ApiDocService {
37
38     public static final int DEFAULT_PAGESIZE = 20;
39     // Query parameter
40     private static final String TOTAL_PAGES = "totalPages";
41     private static final String PAGE_NUM = "pageNum";
42
43     private final MountPointSwagger mountPointSwaggerDraft02;
44     private final MountPointSwagger mountPointSwaggerRFC8040;
45     private final ApiDocGeneratorDraftO2 apiDocGeneratorDraft02;
46     private final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040;
47
48     public ApiDocServiceImpl(MountPointSwaggerGeneratorDraft02 mountPointSwaggerGeneratorDraft02,
49             MountPointSwaggerGeneratorRFC8040 mountPointSwaggerGeneratorRFC8040,
50             ApiDocGeneratorDraftO2 apiDocGeneratorDraft02, ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040) {
51         this.mountPointSwaggerDraft02 =
52                 Objects.requireNonNull(mountPointSwaggerGeneratorDraft02).getMountPointSwagger();
53         this.mountPointSwaggerRFC8040 =
54                 Objects.requireNonNull(mountPointSwaggerGeneratorRFC8040).getMountPointSwagger();
55         this.apiDocGeneratorDraft02 = Objects.requireNonNull(apiDocGeneratorDraft02);
56         this.apiDocGeneratorRFC8040 = Objects.requireNonNull(apiDocGeneratorRFC8040);
57     }
58
59     /**
60      * Generates index document for Swagger UI. This document lists out all
61      * modules with link to get APIs for each module. The API for each module is
62      * served by <code> getDocByModule()</code> method.
63      */
64     @Override
65     public synchronized Response getRootDoc(final UriInfo uriInfo) {
66         final ResourceList rootDoc;
67         if (isNew(uriInfo)) {
68             rootDoc = apiDocGeneratorRFC8040.getResourceListing(uriInfo);
69         } else {
70             rootDoc = apiDocGeneratorDraft02.getResourceListing(uriInfo);
71         }
72
73         return Response.ok(rootDoc).build();
74     }
75
76     /**
77      * Generates Swagger compliant document listing APIs for module.
78      */
79     @Override
80     public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
81         final ApiDeclaration doc;
82         if (isNew(uriInfo)) {
83             doc = apiDocGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo);
84         } else {
85             doc = apiDocGeneratorDraft02.getApiDeclaration(module, revision, uriInfo);
86         }
87
88         return Response.ok(doc).build();
89     }
90
91     /**
92      * Redirects to embedded swagger ui.
93      */
94     @Override
95     public synchronized Response getApiExplorer(final UriInfo uriInfo) {
96         return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
97     }
98
99     @Override
100     public synchronized Response getListOfMounts(final UriInfo uriInfo) {
101         final MountPointSwagger mountPointSwagger;
102         if (isNew(uriInfo)) {
103             mountPointSwagger = mountPointSwaggerRFC8040;
104         } else {
105             mountPointSwagger = mountPointSwaggerDraft02;
106         }
107         final List<MountPointInstance> entity = mountPointSwagger
108                 .getInstanceIdentifiers().entrySet().stream()
109                 .map(MountPointInstance::new).collect(Collectors.toList());
110         return Response.ok(entity).build();
111     }
112
113     @Override
114     public synchronized Response getMountRootDoc(final String instanceNum, final UriInfo uriInfo) {
115         final ResourceList resourceList;
116
117         if (uriInfo.getQueryParameters().getFirst(TOTAL_PAGES) != null) {
118             if (isNew(uriInfo)) {
119                 resourceList = mountPointSwaggerRFC8040.getResourceList(uriInfo, Long.parseLong(instanceNum));
120             } else {
121                 resourceList = mountPointSwaggerDraft02.getResourceList(uriInfo, Long.parseLong(instanceNum));
122             }
123             int size = resourceList.getApis().size();
124             return Response.ok(size % DEFAULT_PAGESIZE == 0 ? size / DEFAULT_PAGESIZE
125                     : size / DEFAULT_PAGESIZE + 1).build();
126         }
127
128         final int pageNum = Integer.parseInt(uriInfo.getQueryParameters().getFirst(PAGE_NUM));
129
130         if (isNew(uriInfo)) {
131             resourceList = mountPointSwaggerRFC8040.getResourceList(uriInfo, Long.parseLong(instanceNum), pageNum,
132                     false);
133         } else {
134             resourceList = mountPointSwaggerDraft02.getResourceList(uriInfo, Long.parseLong(instanceNum), pageNum,
135                     false);
136         }
137         return Response.ok(resourceList).build();
138     }
139
140     @Override
141     public synchronized Response getMountDocByModule(final String instanceNum, final String module,
142             final String revision, final UriInfo uriInfo) {
143         final ApiDeclaration api;
144         if (isNew(uriInfo)) {
145             api = mountPointSwaggerRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum), module, revision);
146         } else {
147             api = mountPointSwaggerDraft02.getMountPointApi(uriInfo, Long.parseLong(instanceNum), module, revision);
148         }
149         return Response.ok(api).build();
150     }
151
152     private static boolean isNew(final UriInfo uriInfo) {
153         return uriInfo.getBaseUri().toString().contains("/18/");
154     }
155 }