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.netconf.sal.rest.doc.impl;
10 import static java.util.Objects.requireNonNull;
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.OpenApiService;
23 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointOpenApi;
24 import org.opendaylight.netconf.sal.rest.doc.openapi.MountPointInstance;
25 import org.opendaylight.netconf.sal.rest.doc.openapi.OpenApiObject;
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.
38 * NOTE: These API's need to be synchronized due to bug 1198. Thread access to
39 * the SchemaContext is not synchronized properly and thus you can end up with
40 * missing definitions without this synchronization. There are likely otherways
41 * to work around this limitation, but given that this API is a dev only tool
42 * and not dependent UI, this was the fastest work around.
46 public final class OpenApiServiceImpl implements OpenApiService {
47 // FIXME: make this configurable
48 public static final int DEFAULT_PAGESIZE = 20;
51 private static final String PAGE_NUM = "pageNum";
53 private final MountPointOpenApi mountPointOpenApiRFC8040;
54 private final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040;
58 public OpenApiServiceImpl(final @Reference DOMSchemaService schemaService,
59 final @Reference DOMMountPointService mountPointService) {
60 this(new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService),
61 new OpenApiGeneratorRFC8040(schemaService));
64 public OpenApiServiceImpl(final DOMSchemaService schemaService,
65 final DOMMountPointService mountPointService,
66 final String basePath) {
67 this(new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService, basePath),
68 new OpenApiGeneratorRFC8040(schemaService, basePath));
72 OpenApiServiceImpl(final MountPointOpenApiGeneratorRFC8040 mountPointOpenApiGeneratorRFC8040,
73 final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040) {
74 mountPointOpenApiRFC8040 = requireNonNull(mountPointOpenApiGeneratorRFC8040).getMountPointOpenApi();
75 this.openApiGeneratorRFC8040 = requireNonNull(openApiGeneratorRFC8040);
79 public synchronized Response getAllModulesDoc(final UriInfo uriInfo) {
80 final DefinitionNames definitionNames = new DefinitionNames();
81 final OpenApiObject doc = openApiGeneratorRFC8040.getAllModulesDoc(uriInfo, definitionNames);
82 return Response.ok(doc).build();
86 * Generates Swagger compliant document listing APIs for module.
89 public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
91 openApiGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo))
96 * Redirects to embedded swagger ui.
99 public synchronized Response getApiExplorer(final UriInfo uriInfo) {
100 return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
104 public synchronized Response getListOfMounts(final UriInfo uriInfo) {
105 final List<MountPointInstance> entity = mountPointOpenApiRFC8040
106 .getInstanceIdentifiers().entrySet().stream()
107 .map(MountPointInstance::new).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 Optional<Integer> pageNum = stringPageNum != null ? Optional.of(Integer.valueOf(stringPageNum))
124 final OpenApiObject api = mountPointOpenApiRFC8040.getMountPointApi(uriInfo,
125 Long.parseLong(instanceNum), pageNum);
126 return Response.ok(api).build();