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 com.fasterxml.jackson.core.JsonFactory;
11 import com.fasterxml.jackson.core.JsonGenerator;
12 import java.io.ByteArrayOutputStream;
13 import java.io.OutputStreamWriter;
14 import java.nio.charset.StandardCharsets;
15 import java.util.Map.Entry;
16 import javax.ws.rs.core.Response;
17 import javax.ws.rs.core.UriInfo;
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
25 * <a 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.
30 * NOTE: These API's need to be synchronized due to bug 1198. Thread access to
31 * the SchemaContext is not synchronized properly and thus you can end up with
32 * missing definitions without this synchronization. There are likely otherways
33 * to work around this limitation, but given that this API is a dev only tool
34 * 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.
50 public synchronized Response getRootDoc(final UriInfo uriInfo) {
51 final ApiDocGenerator generator = ApiDocGenerator.getInstance();
53 generator.setDraft(true);
55 generator.setDraft(false);
57 final ResourceList rootDoc = generator.getResourceListing(uriInfo);
59 return Response.ok(rootDoc).build();
63 * Generates Swagger compliant document listing APIs for module.
66 public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
67 final ApiDocGenerator generator = ApiDocGenerator.getInstance();
69 generator.setDraft(true);
71 generator.setDraft(false);
73 final ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
74 return Response.ok(doc).build();
78 * Redirects to embedded swagger ui.
81 public synchronized Response getApiExplorer(final UriInfo uriInfo) {
82 return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
85 @SuppressWarnings("checkstyle:IllegalCatch")
87 public synchronized Response getListOfMounts(final UriInfo uriInfo) {
88 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
89 try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos, StandardCharsets.UTF_8)) {
90 JsonGenerator writer = new JsonFactory().createGenerator(streamWriter);
91 writer.writeStartArray();
92 for (final Entry<String, Long> entry : MountPointSwagger.getInstance().getInstanceIdentifiers()
94 writer.writeStartObject();
95 writer.writeObjectField("instance", entry.getKey());
96 writer.writeObjectField("id", entry.getValue());
97 writer.writeEndObject();
99 writer.writeEndArray();
100 } catch (final Exception e) {
101 return Response.status(500).entity(e.getMessage()).build();
103 return Response.status(200).entity(baos.toString()).build();
107 public synchronized Response getMountRootDoc(final String instanceNum, final UriInfo uriInfo) {
108 final ResourceList resourceList;
109 if (isNew(uriInfo)) {
110 resourceList = MountPointSwagger.getInstanceDraft18().getResourceList(uriInfo, Long.parseLong(instanceNum));
112 resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo, Long.parseLong(instanceNum));
114 return Response.ok(resourceList).build();
118 public synchronized Response getMountDocByModule(final String instanceNum, final String module,
119 final String revision, final UriInfo uriInfo) {
120 final ApiDeclaration api;
121 if (isNew(uriInfo)) {
122 api = MountPointSwagger.getInstanceDraft18().getMountPointApi(uriInfo, Long.parseLong(instanceNum), module,
125 api = MountPointSwagger.getInstance().getMountPointApi(uriInfo, Long.parseLong(instanceNum), module,
128 return Response.ok(api).build();
131 private static boolean isNew(final UriInfo uriInfo) {
132 return uriInfo.getBaseUri().toString().contains("/18/");