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 java.io.ByteArrayOutputStream;
11 import java.io.OutputStreamWriter;
12 import java.nio.charset.StandardCharsets;
13 import java.util.Map.Entry;
14 import javax.ws.rs.core.Response;
15 import javax.ws.rs.core.UriInfo;
16 import org.json.JSONWriter;
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.ApiDeclaration;
20 import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
23 * This service generates swagger (See <a
24 * href="https://helloreverb.com/developers/swagger"
25 * >https://helloreverb.com/developers/swagger</a>) compliant documentation for
26 * RESTCONF APIs. The output of this is used by embedded Swagger UI.
28 * <p>NOTE: These API's need to be synchronized due to bug 1198. Thread access to
29 * the SchemaContext is not synchronized properly and thus you can end up with
30 * missing definitions without this synchronization. There are likely otherways
31 * to work around this limitation, but given that this API is a dev only tool
32 * and not dependent UI, this was the fastest work around.
34 public class ApiDocServiceImpl implements ApiDocService {
36 private static final ApiDocService INSTANCE = new ApiDocServiceImpl();
38 public static ApiDocService getInstance() {
43 * Generates index document for Swagger UI. This document lists out all
44 * modules with link to get APIs for each module. The API for each module is
45 * served by <code> getDocByModule()</code> method.
48 public synchronized Response getRootDoc(final UriInfo uriInfo) {
49 final ApiDocGenerator generator = ApiDocGenerator.getInstance();
50 final ResourceList rootDoc = generator.getResourceListing(uriInfo);
52 return Response.ok(rootDoc).build();
56 * Generates Swagger compliant document listing APIs for module.
59 public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
60 final ApiDocGenerator generator = ApiDocGenerator.getInstance();
62 final ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
63 return Response.ok(doc).build();
67 * Redirects to embedded swagger ui.
70 public synchronized Response getApiExplorer(final UriInfo uriInfo) {
72 .seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build())
77 public synchronized Response getListOfMounts(final UriInfo uriInfo) {
78 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
79 try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos, StandardCharsets.UTF_8)) {
80 final JSONWriter writer = new JSONWriter(streamWriter);
82 for (final Entry<String, Long> entry : MountPointSwagger.getInstance()
83 .getInstanceIdentifiers().entrySet()) {
85 writer.key("instance").value(entry.getKey());
86 writer.key("id").value(entry.getValue());
90 } catch (final Exception e) {
91 return Response.status(500).entity(e.getMessage()).build();
93 return Response.status(200).entity(baos.toString()).build();
97 public synchronized Response getMountRootDoc(final String instanceNum, final UriInfo uriInfo) {
98 final ResourceList resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo,
99 Long.parseLong(instanceNum));
100 return Response.ok(resourceList).build();
104 public synchronized Response getMountDocByModule(final String instanceNum, final String module,
105 final String revision, final UriInfo uriInfo) {
106 final ApiDeclaration api = MountPointSwagger.getInstance().getMountPointApi(uriInfo,
107 Long.parseLong(instanceNum), module, revision);
108 return Response.ok(api).build();