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.util.List;
14 import java.util.stream.Collectors;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import javax.ws.rs.core.Response;
18 import javax.ws.rs.core.UriInfo;
19 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
20 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
21 import org.opendaylight.restconf.openapi.api.OpenApiService;
22 import org.opendaylight.restconf.openapi.model.MountPointInstance;
23 import org.opendaylight.restconf.openapi.model.OpenApiObject;
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.
37 * NOTE: These API's need to be synchronized due to bug 1198. Thread access to
38 * the SchemaContext is not synchronized properly and thus you can end up with
39 * missing definitions without this synchronization. There are likely otherways
40 * to work around this limitation, but given that this API is a dev only tool
41 * and not dependent UI, this was the fastest work around.
45 public final class OpenApiServiceImpl implements OpenApiService {
46 // FIXME: make this configurable
47 public static final int DEFAULT_PAGESIZE = 20;
50 private static final String PAGE_NUM = "pageNum";
52 private final MountPointOpenApi mountPointOpenApiRFC8040;
53 private final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040;
57 public OpenApiServiceImpl(final @Reference DOMSchemaService schemaService,
58 final @Reference DOMMountPointService mountPointService) {
59 this(new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService),
60 new OpenApiGeneratorRFC8040(schemaService));
63 public OpenApiServiceImpl(final DOMSchemaService schemaService,
64 final DOMMountPointService mountPointService,
65 final String basePath) {
66 this(new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService, basePath),
67 new OpenApiGeneratorRFC8040(schemaService, basePath));
71 OpenApiServiceImpl(final MountPointOpenApiGeneratorRFC8040 mountPointOpenApiGeneratorRFC8040,
72 final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040) {
73 mountPointOpenApiRFC8040 = requireNonNull(mountPointOpenApiGeneratorRFC8040).getMountPointOpenApi();
74 this.openApiGeneratorRFC8040 = requireNonNull(openApiGeneratorRFC8040);
78 public synchronized Response getAllModulesDoc(final UriInfo uriInfo) {
79 final DefinitionNames definitionNames = new DefinitionNames();
80 final OpenApiObject doc = openApiGeneratorRFC8040.getAllModulesDoc(uriInfo, definitionNames);
81 return Response.ok(doc).build();
85 * Generates Swagger compliant document listing APIs for module.
88 public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
90 openApiGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo))
95 * Redirects to embedded swagger ui.
98 public synchronized Response getApiExplorer(final UriInfo uriInfo) {
99 return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
103 public synchronized Response getListOfMounts(final UriInfo uriInfo) {
104 final List<MountPointInstance> entity = mountPointOpenApiRFC8040
105 .getInstanceIdentifiers().entrySet().stream()
106 .map(entry -> new MountPointInstance(entry.getKey(), entry.getValue()))
107 .collect(Collectors.toList());
108 return Response.ok(entity).build();
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),
116 return Response.ok(api).build();
120 public synchronized Response getMountDoc(final String instanceNum, final UriInfo uriInfo) {
121 final String stringPageNum = uriInfo.getQueryParameters().getFirst(PAGE_NUM);
122 final OpenApiObject api = mountPointOpenApiRFC8040.getMountPointApi(uriInfo,
123 Long.parseLong(instanceNum), stringPageNum);
124 return Response.ok(api).build();