2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.openapi.impl;
10 import static java.util.Objects.requireNonNull;
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.nb.rfc8040.streams.RestconfStreamServletFactory;
23 import org.opendaylight.restconf.openapi.api.OpenApiService;
24 import org.opendaylight.restconf.openapi.model.MountPointInstance;
25 import org.opendaylight.restconf.openapi.mountpoints.MountPointOpenApi;
26 import org.osgi.service.component.annotations.Activate;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Reference;
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.
39 public final class OpenApiServiceImpl implements OpenApiService {
40 // FIXME: make this configurable
41 public static final int DEFAULT_PAGESIZE = 20;
44 private static final String PAGE_NUM = "pageNum";
46 private final MountPointOpenApi mountPointOpenApiRFC8040;
47 private final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040;
51 public OpenApiServiceImpl(final @Reference DOMSchemaService schemaService,
52 final @Reference DOMMountPointService mountPointService,
53 final @Reference RestconfStreamServletFactory context) {
54 this(new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService, context.restconf()),
55 new OpenApiGeneratorRFC8040(schemaService, context.restconf()));
59 OpenApiServiceImpl(final MountPointOpenApiGeneratorRFC8040 mountPointOpenApiGeneratorRFC8040,
60 final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040) {
61 mountPointOpenApiRFC8040 = requireNonNull(mountPointOpenApiGeneratorRFC8040).getMountPointOpenApi();
62 this.openApiGeneratorRFC8040 = requireNonNull(openApiGeneratorRFC8040);
66 public Response getAllModulesDoc(final UriInfo uriInfo) throws IOException {
67 final OpenApiInputStream stream = openApiGeneratorRFC8040.getControllerModulesDoc(uriInfo);
68 return Response.ok(stream).build();
72 * Generates Swagger compliant document listing APIs for module.
75 public Response getDocByModule(final String module, final String revision, final UriInfo uriInfo)
77 final OpenApiInputStream stream = openApiGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo);
78 return Response.ok(stream).build();
82 * Redirects to embedded swagger ui.
85 public Response getApiExplorer(final UriInfo uriInfo) {
86 return Response.seeOther(uriInfo.getBaseUriBuilder().path("../../explorer/index.html").build()).build();
90 public Response getListOfMounts(final UriInfo uriInfo) {
91 final List<MountPointInstance> entity = mountPointOpenApiRFC8040
92 .getInstanceIdentifiers().entrySet().stream()
93 .map(entry -> new MountPointInstance(entry.getKey(), entry.getValue()))
94 .collect(Collectors.toList());
95 return Response.ok(entity).build();
99 public Response getMountDocByModule(final String instanceNum, final String module,
100 final String revision, final UriInfo uriInfo) throws IOException {
101 final OpenApiInputStream stream =
102 mountPointOpenApiRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum), module, revision);
103 return Response.ok(stream).build();
107 public Response getMountDoc(final String instanceNum, final UriInfo uriInfo) throws IOException {
108 final String stringPageNum = uriInfo.getQueryParameters().getFirst(PAGE_NUM);
109 final OpenApiInputStream stream =
110 mountPointOpenApiRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum), stringPageNum);
111 return Response.ok(stream).build();