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