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