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 java.util.List;
13 import java.util.Optional;
14 import java.util.stream.Collectors;
15 import javax.ws.rs.core.Response;
16 import javax.ws.rs.core.UriInfo;
17 import org.opendaylight.netconf.sal.rest.doc.api.ApiDocService;
18 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
19 import org.opendaylight.netconf.sal.rest.doc.swagger.CommonApiObject;
20 import org.opendaylight.netconf.sal.rest.doc.swagger.MountPointInstance;
24 * This service generates swagger (See
25 * <a href="https://helloreverb.com/developers/swagger"
26 * >https://helloreverb.com/developers/swagger</a>) compliant documentation for
27 * RESTCONF APIs. The output of this is used by embedded Swagger UI.
30 * NOTE: These API's need to be synchronized due to bug 1198. Thread access to
31 * the SchemaContext is not synchronized properly and thus you can end up with
32 * missing definitions without this synchronization. There are likely otherways
33 * to work around this limitation, but given that this API is a dev only tool
34 * and not dependent UI, this was the fastest work around.
36 public class ApiDocServiceImpl implements ApiDocService {
38 public static final int DEFAULT_PAGESIZE = 20;
40 private static final String PAGE_NUM = "pageNum";
42 public enum OAversion { V2_0, V3_0 }
44 private final MountPointSwagger mountPointSwaggerRFC8040;
45 private final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040;
46 private final AllModulesDocGenerator allModulesDocGenerator;
48 public ApiDocServiceImpl(final MountPointSwaggerGeneratorRFC8040 mountPointSwaggerGeneratorRFC8040,
49 final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040,
50 final AllModulesDocGenerator allModulesDocGenerator) {
51 mountPointSwaggerRFC8040 =
52 requireNonNull(mountPointSwaggerGeneratorRFC8040).getMountPointSwagger();
53 this.apiDocGeneratorRFC8040 = requireNonNull(apiDocGeneratorRFC8040);
54 this.allModulesDocGenerator = requireNonNull(allModulesDocGenerator);
58 public synchronized Response getAllModulesDoc(final UriInfo uriInfo) {
59 return Response.ok(allModulesDocGenerator.getAllModulesDoc(uriInfo, identifyOpenApiVersion(uriInfo))).build();
63 * Generates Swagger compliant document listing APIs for module.
66 public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
68 apiDocGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo, identifyOpenApiVersion(uriInfo)))
73 * Redirects to embedded swagger ui.
76 public synchronized Response getApiExplorer(final UriInfo uriInfo) {
77 return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
81 public synchronized Response getListOfMounts(final UriInfo uriInfo) {
82 final List<MountPointInstance> entity = mountPointSwaggerRFC8040
83 .getInstanceIdentifiers().entrySet().stream()
84 .map(MountPointInstance::new).collect(Collectors.toList());
85 return Response.ok(entity).build();
89 public synchronized Response getMountDocByModule(final String instanceNum, final String module,
90 final String revision, final UriInfo uriInfo) {
91 final OAversion oaversion = identifyOpenApiVersion(uriInfo);
92 final CommonApiObject api = mountPointSwaggerRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum),
93 module, revision, oaversion);
94 return Response.ok(api).build();
98 public synchronized Response getMountDoc(final String instanceNum, final UriInfo uriInfo) {
99 final CommonApiObject api;
100 final OAversion oaversion = identifyOpenApiVersion(uriInfo);
101 final String stringPageNum = uriInfo.getQueryParameters().getFirst(PAGE_NUM);
102 final Optional<Integer> pageNum = stringPageNum != null ? Optional.of(Integer.valueOf(stringPageNum))
104 api = mountPointSwaggerRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum), pageNum, oaversion);
105 return Response.ok(api).build();
108 private static OAversion identifyOpenApiVersion(final UriInfo uriInfo) {
109 if (uriInfo.getBaseUri().toString().contains("/swagger2/")) {
110 return OAversion.V2_0;
112 return OAversion.V3_0;