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.IOException;
14 import java.io.OutputStreamWriter;
15 import java.io.UnsupportedEncodingException;
16 import java.nio.charset.StandardCharsets;
17 import java.util.Map.Entry;
18 import javax.ws.rs.core.Response;
19 import javax.ws.rs.core.UriInfo;
20 import org.opendaylight.netconf.sal.rest.doc.api.ApiDocService;
21 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
22 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
23 import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
26 * This service generates swagger (See
27 * <a href="https://helloreverb.com/developers/swagger"
28 * >https://helloreverb.com/developers/swagger</a>) compliant documentation for
29 * RESTCONF APIs. The output of this is used by embedded Swagger UI.
32 * NOTE: These API's need to be synchronized due to bug 1198. Thread access to
33 * the SchemaContext is not synchronized properly and thus you can end up with
34 * missing definitions without this synchronization. There are likely otherways
35 * to work around this limitation, but given that this API is a dev only tool
36 * and not dependent UI, this was the fastest work around.
38 public class ApiDocServiceImpl implements ApiDocService {
40 private static final ApiDocService INSTANCE = new ApiDocServiceImpl();
42 public static ApiDocService getInstance() {
47 * Generates index document for Swagger UI. This document lists out all
48 * modules with link to get APIs for each module. The API for each module is
49 * served by <code> getDocByModule()</code> method.
52 public synchronized Response getRootDoc(final UriInfo uriInfo) {
53 final ApiDocGenerator generator = ApiDocGenerator.getInstance();
55 generator.setDraft(true);
57 generator.setDraft(false);
59 final ResourceList rootDoc = generator.getResourceListing(uriInfo);
61 return Response.ok(rootDoc).build();
65 * Generates Swagger compliant document listing APIs for module.
68 public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
69 final ApiDocGenerator generator = ApiDocGenerator.getInstance();
71 generator.setDraft(true);
73 generator.setDraft(false);
75 final ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
76 return Response.ok(doc).build();
80 * Redirects to embedded swagger ui.
83 public synchronized Response getApiExplorer(final UriInfo uriInfo) {
84 return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
88 public synchronized Response getListOfMounts(final UriInfo uriInfo) {
89 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
90 try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos, StandardCharsets.UTF_8)) {
91 JsonGenerator writer = new JsonFactory().createGenerator(streamWriter);
92 writer.writeStartArray();
93 for (final Entry<String, Long> entry : MountPointSwagger.getInstance().getInstanceIdentifiers()
95 writer.writeStartObject();
96 writer.writeObjectField("instance", entry.getKey());
97 writer.writeObjectField("id", entry.getValue());
98 writer.writeEndObject();
100 writer.writeEndArray();
102 } catch (IOException e) {
103 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
107 String responseStr = baos.toString(StandardCharsets.UTF_8.name());
108 return Response.status(Response.Status.OK).entity(responseStr).build();
109 } catch (UnsupportedEncodingException e) {
110 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
115 public synchronized Response getMountRootDoc(final String instanceNum, final UriInfo uriInfo) {
116 final ResourceList resourceList;
117 if (isNew(uriInfo)) {
118 resourceList = MountPointSwagger.getInstanceDraft18().getResourceList(uriInfo, Long.parseLong(instanceNum));
120 resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo, Long.parseLong(instanceNum));
122 return Response.ok(resourceList).build();
126 public synchronized Response getMountDocByModule(final String instanceNum, final String module,
127 final String revision, final UriInfo uriInfo) {
128 final ApiDeclaration api;
129 if (isNew(uriInfo)) {
130 api = MountPointSwagger.getInstanceDraft18().getMountPointApi(uriInfo, Long.parseLong(instanceNum), module,
133 api = MountPointSwagger.getInstance().getMountPointApi(uriInfo, Long.parseLong(instanceNum), module,
136 return Response.ok(api).build();
139 private static boolean isNew(final UriInfo uriInfo) {
140 return uriInfo.getBaseUri().toString().contains("/18/");