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.controller.sal.rest.doc.impl;
10 import java.io.ByteArrayOutputStream;
11 import java.io.OutputStreamWriter;
12 import java.util.Map.Entry;
14 import javax.ws.rs.core.Response;
15 import javax.ws.rs.core.UriInfo;
17 import org.json.JSONWriter;
18 import org.opendaylight.controller.sal.rest.doc.api.ApiDocService;
19 import org.opendaylight.controller.sal.rest.doc.mountpoints.MountPointSwagger;
20 import org.opendaylight.controller.sal.rest.doc.swagger.ApiDeclaration;
21 import org.opendaylight.controller.sal.rest.doc.swagger.ResourceList;
24 * This service generates swagger (See <a
25 * 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.
29 * NOTE: These API's need to be synchronized due to bug 1198. Thread access to
30 * the SchemaContext is not synchronized properly and thus you can end up with
31 * missing definitions without this synchronization. There are likely otherways
32 * to work around this limitation, but given that this API is a dev only tool
33 * and not dependent UI, this was the fastest work around.
36 public class ApiDocServiceImpl implements ApiDocService {
38 private static final ApiDocService INSTANCE = new ApiDocServiceImpl();
40 public static ApiDocService getInstance() {
45 * Generates index document for Swagger UI. This document lists out all
46 * modules with link to get APIs for each module. The API for each module is
47 * served by <code> getDocByModule()</code> method.
53 public synchronized Response getRootDoc(UriInfo uriInfo) {
54 ApiDocGenerator generator = ApiDocGenerator.getInstance();
55 ResourceList rootDoc = generator.getResourceListing(uriInfo);
57 return Response.ok(rootDoc).build();
61 * Generates Swagger compliant document listing APIs for module.
69 public synchronized Response getDocByModule(String module, String revision, UriInfo uriInfo) {
70 ApiDocGenerator generator = ApiDocGenerator.getInstance();
72 ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
73 return Response.ok(doc).build();
77 * Redirects to embedded swagger ui.
83 public synchronized Response getApiExplorer(UriInfo uriInfo) {
85 .seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build())
90 public synchronized Response getListOfMounts(UriInfo uriInfo) {
91 ByteArrayOutputStream baos = new ByteArrayOutputStream();
92 try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos)) {
93 JSONWriter writer = new JSONWriter(streamWriter);
95 for (Entry<String, Long> entry : MountPointSwagger.getInstance()
96 .getInstanceIdentifiers().entrySet()) {
98 writer.key("instance").value(entry.getKey());
99 writer.key("id").value(entry.getValue());
103 } catch (Exception e) {
104 return Response.status(500).entity(e.getMessage()).build();
106 return Response.status(200).entity(baos.toString()).build();
110 public synchronized Response getMountRootDoc(String instanceNum, UriInfo uriInfo) {
111 ResourceList resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo,
112 Long.parseLong(instanceNum));
113 return Response.ok(resourceList).build();
117 public synchronized Response getMountDocByModule(String instanceNum, String module,
118 String revision, UriInfo uriInfo) {
119 ApiDeclaration api = MountPointSwagger.getInstance().getMountPointApi(uriInfo,
120 Long.parseLong(instanceNum), module, revision);
121 return Response.ok(api).build();