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.openapi.CommonApiObject;
25 import org.opendaylight.netconf.sal.rest.doc.openapi.MountPointInstance;
26 import org.opendaylight.netconf.sal.rest.doc.openapi.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 private final MountPointSwagger mountPointSwaggerRFC8040;
55 private final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040;
59 public ApiDocServiceImpl(final @Reference DOMSchemaService schemaService,
60 final @Reference DOMMountPointService mountPointService) {
61 this(new MountPointSwaggerGeneratorRFC8040(schemaService, mountPointService),
62 new ApiDocGeneratorRFC8040(schemaService));
66 ApiDocServiceImpl(final MountPointSwaggerGeneratorRFC8040 mountPointSwaggerGeneratorRFC8040,
67 final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040) {
68 mountPointSwaggerRFC8040 = requireNonNull(mountPointSwaggerGeneratorRFC8040).getMountPointSwagger();
69 this.apiDocGeneratorRFC8040 = requireNonNull(apiDocGeneratorRFC8040);
73 public synchronized Response getAllModulesDoc(final UriInfo uriInfo) {
74 final DefinitionNames definitionNames = new DefinitionNames();
75 final SwaggerObject doc = apiDocGeneratorRFC8040.getAllModulesDoc(uriInfo, definitionNames);
77 return Response.ok(BaseYangSwaggerGenerator.convertToOpenApi(doc)).build();
81 * Generates Swagger compliant document listing APIs for module.
84 public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
86 apiDocGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo))
91 * Redirects to embedded swagger ui.
94 public synchronized Response getApiExplorer(final UriInfo uriInfo) {
95 return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
99 public synchronized Response getListOfMounts(final UriInfo uriInfo) {
100 final List<MountPointInstance> entity = mountPointSwaggerRFC8040
101 .getInstanceIdentifiers().entrySet().stream()
102 .map(MountPointInstance::new).collect(Collectors.toList());
103 return Response.ok(entity).build();
107 public synchronized Response getMountDocByModule(final String instanceNum, final String module,
108 final String revision, final UriInfo uriInfo) {
109 final CommonApiObject api = mountPointSwaggerRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum),
111 return Response.ok(api).build();
115 public synchronized Response getMountDoc(final String instanceNum, final UriInfo uriInfo) {
116 final CommonApiObject api;
117 final String stringPageNum = uriInfo.getQueryParameters().getFirst(PAGE_NUM);
118 final Optional<Integer> pageNum = stringPageNum != null ? Optional.of(Integer.valueOf(stringPageNum))
120 api = mountPointSwaggerRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum), pageNum);
121 return Response.ok(api).build();