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