Remove Bierman02 support from sal-rest-docgen
[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 static java.util.Objects.requireNonNull;
11
12 import java.util.List;
13 import java.util.Optional;
14 import java.util.stream.Collectors;
15 import javax.ws.rs.core.Response;
16 import javax.ws.rs.core.UriInfo;
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.CommonApiObject;
20 import org.opendaylight.netconf.sal.rest.doc.swagger.MountPointInstance;
21
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 PAGE_NUM = "pageNum";
41
42     public enum OAversion { V2_0, V3_0 }
43
44     private final MountPointSwagger mountPointSwaggerRFC8040;
45     private final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040;
46     private final AllModulesDocGenerator allModulesDocGenerator;
47
48     public ApiDocServiceImpl(final MountPointSwaggerGeneratorRFC8040 mountPointSwaggerGeneratorRFC8040,
49                              final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040,
50                              final AllModulesDocGenerator allModulesDocGenerator) {
51         mountPointSwaggerRFC8040 =
52                 requireNonNull(mountPointSwaggerGeneratorRFC8040).getMountPointSwagger();
53         this.apiDocGeneratorRFC8040 = requireNonNull(apiDocGeneratorRFC8040);
54         this.allModulesDocGenerator = requireNonNull(allModulesDocGenerator);
55     }
56
57     @Override
58     public synchronized Response getAllModulesDoc(final UriInfo uriInfo) {
59         return Response.ok(allModulesDocGenerator.getAllModulesDoc(uriInfo, identifyOpenApiVersion(uriInfo))).build();
60     }
61
62     /**
63      * Generates Swagger compliant document listing APIs for module.
64      */
65     @Override
66     public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
67         return Response.ok(
68             apiDocGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo, identifyOpenApiVersion(uriInfo)))
69             .build();
70     }
71
72     /**
73      * Redirects to embedded swagger ui.
74      */
75     @Override
76     public synchronized Response getApiExplorer(final UriInfo uriInfo) {
77         return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
78     }
79
80     @Override
81     public synchronized Response getListOfMounts(final UriInfo uriInfo) {
82         final List<MountPointInstance> entity = mountPointSwaggerRFC8040
83                 .getInstanceIdentifiers().entrySet().stream()
84                 .map(MountPointInstance::new).collect(Collectors.toList());
85         return Response.ok(entity).build();
86     }
87
88     @Override
89     public synchronized Response getMountDocByModule(final String instanceNum, final String module,
90                                                      final String revision, final UriInfo uriInfo) {
91         final OAversion oaversion = identifyOpenApiVersion(uriInfo);
92         final CommonApiObject api = mountPointSwaggerRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum),
93             module, revision, oaversion);
94         return Response.ok(api).build();
95     }
96
97     @Override
98     public synchronized Response getMountDoc(final String instanceNum, final UriInfo uriInfo) {
99         final CommonApiObject api;
100         final OAversion oaversion = identifyOpenApiVersion(uriInfo);
101         final String stringPageNum = uriInfo.getQueryParameters().getFirst(PAGE_NUM);
102         final Optional<Integer> pageNum = stringPageNum != null ? Optional.of(Integer.valueOf(stringPageNum))
103                 : Optional.empty();
104         api = mountPointSwaggerRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum), pageNum, oaversion);
105         return Response.ok(api).build();
106     }
107
108     private static OAversion identifyOpenApiVersion(final UriInfo uriInfo) {
109         if (uriInfo.getBaseUri().toString().contains("/swagger2/")) {
110             return OAversion.V2_0;
111         }
112         return OAversion.V3_0;
113     }
114 }