026296babad063363a3fac6c4215ede6468b6a2d
[netconf.git] / restconf / sal-rest-docgen / src / main / java / org / opendaylight / netconf / sal / rest / doc / impl / ApiDocServiceImpl.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.rest.doc.impl;
9
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;
21
22 /**
23  * This service generates swagger (See <a
24  * 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.
27  *
28  * <p>NOTE: These API's need to be synchronized due to bug 1198. Thread access to
29  * the SchemaContext is not synchronized properly and thus you can end up with
30  * missing definitions without this synchronization. There are likely otherways
31  * to work around this limitation, but given that this API is a dev only tool
32  * and not dependent UI, this was the fastest work around.
33  */
34 public class ApiDocServiceImpl implements ApiDocService {
35
36     private static final ApiDocService INSTANCE = new ApiDocServiceImpl();
37
38     public static ApiDocService getInstance() {
39         return INSTANCE;
40     }
41
42     /**
43      * Generates index document for Swagger UI. This document lists out all
44      * modules with link to get APIs for each module. The API for each module is
45      * served by <code> getDocByModule()</code> method.
46      */
47     @Override
48     public synchronized Response getRootDoc(final UriInfo uriInfo) {
49         final ApiDocGenerator generator = ApiDocGenerator.getInstance();
50         final ResourceList rootDoc = generator.getResourceListing(uriInfo);
51
52         return Response.ok(rootDoc).build();
53     }
54
55     /**
56      * Generates Swagger compliant document listing APIs for module.
57      */
58     @Override
59     public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
60         final ApiDocGenerator generator = ApiDocGenerator.getInstance();
61
62         final ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
63         return Response.ok(doc).build();
64     }
65
66     /**
67      * Redirects to embedded swagger ui.
68      */
69     @Override
70     public synchronized Response getApiExplorer(final UriInfo uriInfo) {
71         return Response
72                 .seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build())
73                 .build();
74     }
75
76     @Override
77     public synchronized Response getListOfMounts(final UriInfo uriInfo) {
78         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
79         try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos, StandardCharsets.UTF_8)) {
80             final JSONWriter writer = new JSONWriter(streamWriter);
81             writer.array();
82             for (final Entry<String, Long> entry : MountPointSwagger.getInstance()
83                     .getInstanceIdentifiers().entrySet()) {
84                 writer.object();
85                 writer.key("instance").value(entry.getKey());
86                 writer.key("id").value(entry.getValue());
87                 writer.endObject();
88             }
89             writer.endArray();
90         } catch (final Exception e) {
91             return Response.status(500).entity(e.getMessage()).build();
92         }
93         return Response.status(200).entity(baos.toString()).build();
94     }
95
96     @Override
97     public synchronized Response getMountRootDoc(final String instanceNum, final UriInfo uriInfo) {
98         final ResourceList resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo,
99                 Long.parseLong(instanceNum));
100         return Response.ok(resourceList).build();
101     }
102
103     @Override
104     public synchronized Response getMountDocByModule(final String instanceNum, final String module,
105                                                      final String revision, final UriInfo uriInfo) {
106         final ApiDeclaration api = MountPointSwagger.getInstance().getMountPointApi(uriInfo,
107                 Long.parseLong(instanceNum), module, revision);
108         return Response.ok(api).build();
109     }
110
111 }