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.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;
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.
38 public final class OpenApiServiceImpl implements OpenApiService {
39 // FIXME: make this configurable
40 public static final int DEFAULT_PAGESIZE = 20;
43 private static final String PAGE_NUM = "pageNum";
45 private final MountPointOpenApi mountPointOpenApiRFC8040;
46 private final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040;
50 public OpenApiServiceImpl(final @Reference DOMSchemaService schemaService,
51 final @Reference DOMMountPointService mountPointService) {
52 this(new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService),
53 new OpenApiGeneratorRFC8040(schemaService));
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));
64 OpenApiServiceImpl(final MountPointOpenApiGeneratorRFC8040 mountPointOpenApiGeneratorRFC8040,
65 final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040) {
66 mountPointOpenApiRFC8040 = requireNonNull(mountPointOpenApiGeneratorRFC8040).getMountPointOpenApi();
67 this.openApiGeneratorRFC8040 = requireNonNull(openApiGeneratorRFC8040);
71 public Response getAllModulesDoc(final UriInfo uriInfo) throws IOException {
72 final OpenApiInputStream stream = openApiGeneratorRFC8040.getControllerModulesDoc(uriInfo);
73 return Response.ok(stream).build();
77 * Generates Swagger compliant document listing APIs for module.
80 public Response getDocByModule(final String module, final String revision, final UriInfo uriInfo)
82 final OpenApiInputStream stream = openApiGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo);
83 return Response.ok(stream).build();
87 * Redirects to embedded swagger ui.
90 public Response getApiExplorer(final UriInfo uriInfo) {
91 return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
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();
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();
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();