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;
14 import javax.ws.rs.core.Response;
15 import javax.ws.rs.core.UriInfo;
17 import org.json.JSONWriter;
18 import org.opendaylight.netconf.sal.rest.doc.api.ApiDocService;
19 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
20 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
21 import org.opendaylight.netconf.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 * <p>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.
35 public class ApiDocServiceImpl implements ApiDocService {
37 private static final ApiDocService INSTANCE = new ApiDocServiceImpl();
39 public static ApiDocService getInstance() {
44 * Generates index document for Swagger UI. This document lists out all
45 * modules with link to get APIs for each module. The API for each module is
46 * served by <code> getDocByModule()</code> method.
49 public synchronized Response getRootDoc(UriInfo uriInfo) {
50 ApiDocGenerator generator = ApiDocGenerator.getInstance();
51 ResourceList rootDoc = generator.getResourceListing(uriInfo);
53 return Response.ok(rootDoc).build();
57 * Generates Swagger compliant document listing APIs for module.
60 public synchronized Response getDocByModule(String module, String revision, UriInfo uriInfo) {
61 ApiDocGenerator generator = ApiDocGenerator.getInstance();
63 ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
64 return Response.ok(doc).build();
68 * Redirects to embedded swagger ui.
71 public synchronized Response getApiExplorer(UriInfo uriInfo) {
73 .seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build())
78 public synchronized Response getListOfMounts(UriInfo uriInfo) {
79 ByteArrayOutputStream baos = new ByteArrayOutputStream();
80 try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos)) {
81 JSONWriter writer = new JSONWriter(streamWriter);
83 for (Entry<String, Long> entry : MountPointSwagger.getInstance()
84 .getInstanceIdentifiers().entrySet()) {
86 writer.key("instance").value(entry.getKey());
87 writer.key("id").value(entry.getValue());
91 } catch (Exception e) {
92 return Response.status(500).entity(e.getMessage()).build();
94 return Response.status(200).entity(baos.toString()).build();
98 public synchronized Response getMountRootDoc(String instanceNum, UriInfo uriInfo) {
99 ResourceList resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo,
100 Long.parseLong(instanceNum));
101 return Response.ok(resourceList).build();
105 public synchronized Response getMountDocByModule(String instanceNum, String module,
106 String revision, UriInfo uriInfo) {
107 ApiDeclaration api = MountPointSwagger.getInstance().getMountPointApi(uriInfo,
108 Long.parseLong(instanceNum), module, revision);
109 return Response.ok(api).build();