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
24 * <a 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.
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.
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(final UriInfo uriInfo) {
50 final ApiDocGenerator generator = ApiDocGenerator.getInstance();
52 generator.setDraft(true);
54 generator.setDraft(false);
56 final ResourceList rootDoc = generator.getResourceListing(uriInfo);
58 return Response.ok(rootDoc).build();
62 * Generates Swagger compliant document listing APIs for module.
65 public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
66 final ApiDocGenerator generator = ApiDocGenerator.getInstance();
68 generator.setDraft(true);
70 generator.setDraft(false);
72 final ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
73 return Response.ok(doc).build();
77 * Redirects to embedded swagger ui.
80 public synchronized Response getApiExplorer(final UriInfo uriInfo) {
81 return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
85 public synchronized Response getListOfMounts(final UriInfo uriInfo) {
86 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
87 try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos, StandardCharsets.UTF_8)) {
88 final JSONWriter writer = new JSONWriter(streamWriter);
90 for (final Entry<String, Long> entry : MountPointSwagger.getInstance().getInstanceIdentifiers()
93 writer.key("instance").value(entry.getKey());
94 writer.key("id").value(entry.getValue());
98 } catch (final Exception e) {
99 return Response.status(500).entity(e.getMessage()).build();
101 return Response.status(200).entity(baos.toString()).build();
105 public synchronized Response getMountRootDoc(final String instanceNum, final UriInfo uriInfo) {
106 final ResourceList resourceList;
107 if (isNew(uriInfo)) {
108 resourceList = MountPointSwagger.getInstanceDraft18().getResourceList(uriInfo, Long.parseLong(instanceNum));
110 resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo, Long.parseLong(instanceNum));
112 return Response.ok(resourceList).build();
116 public synchronized Response getMountDocByModule(final String instanceNum, final String module,
117 final String revision, final UriInfo uriInfo) {
118 final ApiDeclaration api;
119 if (isNew(uriInfo)) {
120 api = MountPointSwagger.getInstanceDraft18().getMountPointApi(uriInfo, Long.parseLong(instanceNum), module,
123 api = MountPointSwagger.getInstance().getMountPointApi(uriInfo, Long.parseLong(instanceNum), module,
126 return Response.ok(api).build();
129 private static boolean isNew(final UriInfo uriInfo) {
130 return uriInfo.getBaseUri().toString().contains("/18/");