89eedd623826cfa32fcc258f836c1649c43a3186
[netconf.git] / restconf / restconf-openapi / src / main / java / org / opendaylight / restconf / openapi / 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.restconf.openapi.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import java.io.IOException;
14 import java.util.List;
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.restconf.openapi.api.OpenApiService;
23 import org.opendaylight.restconf.openapi.model.MountPointInstance;
24 import org.opendaylight.restconf.openapi.mountpoints.MountPointOpenApi;
25 import org.osgi.service.component.annotations.Activate;
26 import org.osgi.service.component.annotations.Component;
27 import org.osgi.service.component.annotations.Reference;
28
29
30 /**
31  * This service generates swagger (See
32  * <a href="https://helloreverb.com/developers/swagger"
33  * >https://helloreverb.com/developers/swagger</a>) compliant documentation for
34  * RESTCONF APIs. The output of this is used by embedded Swagger UI.
35  */
36 @Component
37 @Singleton
38 public final class OpenApiServiceImpl implements OpenApiService {
39     // FIXME: make this configurable
40     public static final int DEFAULT_PAGESIZE = 20;
41
42     // Query parameter
43     private static final String PAGE_NUM = "pageNum";
44
45     private final MountPointOpenApi mountPointOpenApiRFC8040;
46     private final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040;
47
48     @Inject
49     @Activate
50     public OpenApiServiceImpl(final @Reference DOMSchemaService schemaService,
51                              final @Reference DOMMountPointService mountPointService) {
52         this(new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService),
53             new OpenApiGeneratorRFC8040(schemaService));
54     }
55
56     public OpenApiServiceImpl(final DOMSchemaService schemaService,
57                              final DOMMountPointService mountPointService,
58                              final String basePath) {
59         this(new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService, basePath),
60             new OpenApiGeneratorRFC8040(schemaService, basePath));
61     }
62
63     @VisibleForTesting
64     OpenApiServiceImpl(final MountPointOpenApiGeneratorRFC8040 mountPointOpenApiGeneratorRFC8040,
65                        final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040) {
66         mountPointOpenApiRFC8040 = requireNonNull(mountPointOpenApiGeneratorRFC8040).getMountPointOpenApi();
67         this.openApiGeneratorRFC8040 = requireNonNull(openApiGeneratorRFC8040);
68     }
69
70     @Override
71     public Response getAllModulesDoc(final UriInfo uriInfo) throws IOException {
72         final OpenApiInputStream stream = openApiGeneratorRFC8040.getControllerModulesDoc(uriInfo);
73         return Response.ok(stream).build();
74     }
75
76     /**
77      * Generates Swagger compliant document listing APIs for module.
78      */
79     @Override
80     public Response getDocByModule(final String module, final String revision, final UriInfo uriInfo)
81             throws IOException {
82         final OpenApiInputStream stream = openApiGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo);
83         return Response.ok(stream).build();
84     }
85
86     /**
87      * Redirects to embedded swagger ui.
88      */
89     @Override
90     public Response getApiExplorer(final UriInfo uriInfo) {
91         return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
92     }
93
94     @Override
95     public Response getListOfMounts(final UriInfo uriInfo) {
96         final List<MountPointInstance> entity = mountPointOpenApiRFC8040
97                 .getInstanceIdentifiers().entrySet().stream()
98                 .map(entry -> new MountPointInstance(entry.getKey(), entry.getValue()))
99                 .collect(Collectors.toList());
100         return Response.ok(entity).build();
101     }
102
103     @Override
104     public Response getMountDocByModule(final String instanceNum, final String module,
105             final String revision, final UriInfo uriInfo) throws IOException {
106         final OpenApiInputStream stream =
107             mountPointOpenApiRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum), module, revision);
108         return Response.ok(stream).build();
109     }
110
111     @Override
112     public Response getMountDoc(final String instanceNum, final UriInfo uriInfo) throws IOException {
113         final String stringPageNum = uriInfo.getQueryParameters().getFirst(PAGE_NUM);
114         final OpenApiInputStream stream =
115             mountPointOpenApiRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum), stringPageNum);
116         return Response.ok(stream).build();
117     }
118 }