Rename *ApiDoc* classess to *OpenApi* classes
[netconf.git] / restconf / sal-rest-docgen / src / main / java / org / opendaylight / netconf / sal / rest / doc / impl / OpenApiServiceImpl.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 com.google.common.annotations.VisibleForTesting;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.stream.Collectors;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import javax.ws.rs.core.Response;
19 import javax.ws.rs.core.UriInfo;
20 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
21 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
22 import org.opendaylight.netconf.sal.rest.doc.api.OpenApiService;
23 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointOpenApi;
24 import org.opendaylight.netconf.sal.rest.doc.openapi.MountPointInstance;
25 import org.opendaylight.netconf.sal.rest.doc.openapi.OpenApiObject;
26 import org.osgi.service.component.annotations.Activate;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Reference;
29
30
31 /**
32  * This service generates swagger (See
33  * <a href="https://helloreverb.com/developers/swagger"
34  * >https://helloreverb.com/developers/swagger</a>) compliant documentation for
35  * RESTCONF APIs. The output of this is used by embedded Swagger UI.
36  *
37  * <p>
38  * NOTE: These API's need to be synchronized due to bug 1198. Thread access to
39  * the SchemaContext is not synchronized properly and thus you can end up with
40  * missing definitions without this synchronization. There are likely otherways
41  * to work around this limitation, but given that this API is a dev only tool
42  * and not dependent UI, this was the fastest work around.
43  */
44 @Component
45 @Singleton
46 public final class OpenApiServiceImpl implements OpenApiService {
47     // FIXME: make this configurable
48     public static final int DEFAULT_PAGESIZE = 20;
49
50     // Query parameter
51     private static final String PAGE_NUM = "pageNum";
52
53     private final MountPointOpenApi mountPointOpenApiRFC8040;
54     private final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040;
55
56     @Inject
57     @Activate
58     public OpenApiServiceImpl(final @Reference DOMSchemaService schemaService,
59                              final @Reference DOMMountPointService mountPointService) {
60         this(new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService),
61             new OpenApiGeneratorRFC8040(schemaService));
62     }
63
64     public OpenApiServiceImpl(final DOMSchemaService schemaService,
65                              final DOMMountPointService mountPointService,
66                              final String basePath) {
67         this(new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService, basePath),
68             new OpenApiGeneratorRFC8040(schemaService, basePath));
69     }
70
71     @VisibleForTesting
72     OpenApiServiceImpl(final MountPointOpenApiGeneratorRFC8040 mountPointOpenApiGeneratorRFC8040,
73                        final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040) {
74         mountPointOpenApiRFC8040 = requireNonNull(mountPointOpenApiGeneratorRFC8040).getMountPointOpenApi();
75         this.openApiGeneratorRFC8040 = requireNonNull(openApiGeneratorRFC8040);
76     }
77
78     @Override
79     public synchronized Response getAllModulesDoc(final UriInfo uriInfo) {
80         final DefinitionNames definitionNames = new DefinitionNames();
81         final OpenApiObject doc = openApiGeneratorRFC8040.getAllModulesDoc(uriInfo, definitionNames);
82         return Response.ok(doc).build();
83     }
84
85     /**
86      * Generates Swagger compliant document listing APIs for module.
87      */
88     @Override
89     public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
90         return Response.ok(
91             openApiGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo))
92             .build();
93     }
94
95     /**
96      * Redirects to embedded swagger ui.
97      */
98     @Override
99     public synchronized Response getApiExplorer(final UriInfo uriInfo) {
100         return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
101     }
102
103     @Override
104     public synchronized Response getListOfMounts(final UriInfo uriInfo) {
105         final List<MountPointInstance> entity = mountPointOpenApiRFC8040
106                 .getInstanceIdentifiers().entrySet().stream()
107                 .map(MountPointInstance::new).collect(Collectors.toList());
108         return Response.ok(entity).build();
109     }
110
111     @Override
112     public synchronized Response getMountDocByModule(final String instanceNum, final String module,
113                                                      final String revision, final UriInfo uriInfo) {
114         final OpenApiObject api = mountPointOpenApiRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum),
115             module, revision);
116         return Response.ok(api).build();
117     }
118
119     @Override
120     public synchronized Response getMountDoc(final String instanceNum, final UriInfo uriInfo) {
121         final String stringPageNum = uriInfo.getQueryParameters().getFirst(PAGE_NUM);
122         final Optional<Integer> pageNum = stringPageNum != null ? Optional.of(Integer.valueOf(stringPageNum))
123                 : Optional.empty();
124         final OpenApiObject api = mountPointOpenApiRFC8040.getMountPointApi(uriInfo,
125                 Long.parseLong(instanceNum), pageNum);
126         return Response.ok(api).build();
127     }
128 }