bfac921a818d633fcf80b88448614e17df810845
[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     public enum URIType { RFC8040, DRAFT02 }
44
45     private final MountPointSwagger mountPointSwaggerDraft02;
46     private final MountPointSwagger mountPointSwaggerRFC8040;
47     private final ApiDocGeneratorDraftO2 apiDocGeneratorDraft02;
48     private final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040;
49
50     public ApiDocServiceImpl(MountPointSwaggerGeneratorDraft02 mountPointSwaggerGeneratorDraft02,
51             MountPointSwaggerGeneratorRFC8040 mountPointSwaggerGeneratorRFC8040,
52             ApiDocGeneratorDraftO2 apiDocGeneratorDraft02, ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040) {
53         this.mountPointSwaggerDraft02 =
54                 Objects.requireNonNull(mountPointSwaggerGeneratorDraft02).getMountPointSwagger();
55         this.mountPointSwaggerRFC8040 =
56                 Objects.requireNonNull(mountPointSwaggerGeneratorRFC8040).getMountPointSwagger();
57         this.apiDocGeneratorDraft02 = Objects.requireNonNull(apiDocGeneratorDraft02);
58         this.apiDocGeneratorRFC8040 = Objects.requireNonNull(apiDocGeneratorRFC8040);
59     }
60
61     /**
62      * Generates index document for Swagger UI. This document lists out all
63      * modules with link to get APIs for each module. The API for each module is
64      * served by <code> getDocByModule()</code> method.
65      */
66     @Override
67     public synchronized Response getRootDoc(final UriInfo uriInfo) {
68         final ResourceList rootDoc;
69         if (isNew(uriInfo).equals(URIType.RFC8040)) {
70             rootDoc = apiDocGeneratorRFC8040.getResourceListing(uriInfo, URIType.RFC8040);
71         } else {
72             rootDoc = apiDocGeneratorDraft02.getResourceListing(uriInfo, URIType.DRAFT02);
73         }
74
75         return Response.ok(rootDoc).build();
76     }
77
78     /**
79      * Generates Swagger compliant document listing APIs for module.
80      */
81     @Override
82     public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
83         final ApiDeclaration doc;
84         if (isNew(uriInfo).equals(URIType.RFC8040)) {
85             doc = apiDocGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo, URIType.RFC8040);
86         } else {
87             doc = apiDocGeneratorDraft02.getApiDeclaration(module, revision, uriInfo, URIType.DRAFT02);
88         }
89
90         return Response.ok(doc).build();
91     }
92
93     /**
94      * Redirects to embedded swagger ui.
95      */
96     @Override
97     public synchronized Response getApiExplorer(final UriInfo uriInfo) {
98         return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
99     }
100
101     @Override
102     public synchronized Response getListOfMounts(final UriInfo uriInfo) {
103         final MountPointSwagger mountPointSwagger;
104         if (isNew(uriInfo).equals(URIType.RFC8040)) {
105             mountPointSwagger = mountPointSwaggerRFC8040;
106         } else {
107             mountPointSwagger = mountPointSwaggerDraft02;
108         }
109         final List<MountPointInstance> entity = mountPointSwagger
110                 .getInstanceIdentifiers().entrySet().stream()
111                 .map(MountPointInstance::new).collect(Collectors.toList());
112         return Response.ok(entity).build();
113     }
114
115     @Override
116     public synchronized Response getMountRootDoc(final String instanceNum, final UriInfo uriInfo) {
117         final ResourceList resourceList;
118
119         if (uriInfo.getQueryParameters().getFirst(TOTAL_PAGES) != null) {
120             if (isNew(uriInfo).equals(URIType.RFC8040)) {
121                 resourceList = mountPointSwaggerRFC8040.getResourceList(uriInfo, Long.parseLong(instanceNum),
122                     URIType.RFC8040);
123             } else {
124                 resourceList = mountPointSwaggerDraft02.getResourceList(uriInfo, Long.parseLong(instanceNum),
125                     URIType.DRAFT02);
126             }
127             int size = resourceList.getApis().size();
128             return Response.ok(size % DEFAULT_PAGESIZE == 0 ? size / DEFAULT_PAGESIZE
129                     : size / DEFAULT_PAGESIZE + 1).build();
130         }
131
132         final int pageNum = Integer.parseInt(uriInfo.getQueryParameters().getFirst(PAGE_NUM));
133
134         if (isNew(uriInfo).equals(URIType.RFC8040)) {
135             resourceList = mountPointSwaggerRFC8040.getResourceList(uriInfo, Long.parseLong(instanceNum), pageNum,
136                 false, URIType.RFC8040);
137         } else {
138             resourceList = mountPointSwaggerDraft02.getResourceList(uriInfo, Long.parseLong(instanceNum), pageNum,
139                 false, URIType.DRAFT02);
140         }
141         return Response.ok(resourceList).build();
142     }
143
144     @Override
145     public synchronized Response getMountDocByModule(final String instanceNum, final String module,
146             final String revision, final UriInfo uriInfo) {
147         final ApiDeclaration api;
148         if (isNew(uriInfo).equals(URIType.RFC8040)) {
149             api = mountPointSwaggerRFC8040
150                 .getMountPointApi(uriInfo, Long.parseLong(instanceNum), module, revision, URIType.RFC8040);
151         } else {
152             api = mountPointSwaggerDraft02
153                 .getMountPointApi(uriInfo, Long.parseLong(instanceNum), module, revision, URIType.DRAFT02);
154         }
155         return Response.ok(api).build();
156     }
157
158     private static URIType isNew(final UriInfo uriInfo) {
159         if (uriInfo.getBaseUri().toString().contains("/18/")) {
160             return URIType.RFC8040;
161         }
162         return URIType.DRAFT02;
163     }
164 }