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.ApiDocService;
23 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
24 import org.opendaylight.netconf.sal.rest.doc.swagger.CommonApiObject;
25 import org.opendaylight.netconf.sal.rest.doc.swagger.MountPointInstance;
26 import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
27 import org.osgi.service.component.annotations.Activate;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Reference;
33 * This service generates swagger (See
34 * <a href="https://helloreverb.com/developers/swagger"
35 * >https://helloreverb.com/developers/swagger</a>) compliant documentation for
36 * RESTCONF APIs. The output of this is used by embedded Swagger UI.
39 * NOTE: These API's need to be synchronized due to bug 1198. Thread access to
40 * the SchemaContext is not synchronized properly and thus you can end up with
41 * missing definitions without this synchronization. There are likely otherways
42 * to work around this limitation, but given that this API is a dev only tool
43 * and not dependent UI, this was the fastest work around.
47 public final class ApiDocServiceImpl implements ApiDocService {
48 // FIXME: make this configurable
49 public static final int DEFAULT_PAGESIZE = 20;
52 private static final String PAGE_NUM = "pageNum";
54 public enum OAversion { V2_0, V3_0 }
56 private final MountPointSwagger mountPointSwaggerRFC8040;
57 private final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040;
61 public ApiDocServiceImpl(final @Reference DOMSchemaService schemaService,
62 final @Reference DOMMountPointService mountPointService) {
63 this(new MountPointSwaggerGeneratorRFC8040(schemaService, mountPointService),
64 new ApiDocGeneratorRFC8040(schemaService));
68 ApiDocServiceImpl(final MountPointSwaggerGeneratorRFC8040 mountPointSwaggerGeneratorRFC8040,
69 final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040) {
70 mountPointSwaggerRFC8040 = requireNonNull(mountPointSwaggerGeneratorRFC8040).getMountPointSwagger();
71 this.apiDocGeneratorRFC8040 = requireNonNull(apiDocGeneratorRFC8040);
75 public synchronized Response getAllModulesDoc(final UriInfo uriInfo) {
76 final OAversion oaversion = identifyOpenApiVersion(uriInfo);
77 final DefinitionNames definitionNames = new DefinitionNames();
78 final SwaggerObject doc = apiDocGeneratorRFC8040.getAllModulesDoc(uriInfo, definitionNames, oaversion);
80 return Response.ok(BaseYangSwaggerGenerator.getAppropriateDoc(doc, oaversion)).build();
84 * Generates Swagger compliant document listing APIs for module.
87 public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
89 apiDocGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo, identifyOpenApiVersion(uriInfo)))
94 * Redirects to embedded swagger ui.
97 public synchronized Response getApiExplorer(final UriInfo uriInfo) {
98 return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
102 public synchronized Response getListOfMounts(final UriInfo uriInfo) {
103 final List<MountPointInstance> entity = mountPointSwaggerRFC8040
104 .getInstanceIdentifiers().entrySet().stream()
105 .map(MountPointInstance::new).collect(Collectors.toList());
106 return Response.ok(entity).build();
110 public synchronized Response getMountDocByModule(final String instanceNum, final String module,
111 final String revision, final UriInfo uriInfo) {
112 final OAversion oaversion = identifyOpenApiVersion(uriInfo);
113 final CommonApiObject api = mountPointSwaggerRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum),
114 module, revision, oaversion);
115 return Response.ok(api).build();
119 public synchronized Response getMountDoc(final String instanceNum, final UriInfo uriInfo) {
120 final CommonApiObject api;
121 final OAversion oaversion = identifyOpenApiVersion(uriInfo);
122 final String stringPageNum = uriInfo.getQueryParameters().getFirst(PAGE_NUM);
123 final Optional<Integer> pageNum = stringPageNum != null ? Optional.of(Integer.valueOf(stringPageNum))
125 api = mountPointSwaggerRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum), pageNum, oaversion);
126 return Response.ok(api).build();
129 private static OAversion identifyOpenApiVersion(final UriInfo uriInfo) {
130 if (uriInfo.getBaseUri().toString().contains("/swagger2/")) {
131 return OAversion.V2_0;
133 return OAversion.V3_0;