Make DOMMountPointService listenable
[controller.git] / opendaylight / md-sal / sal-rest-docgen / src / main / java / org / opendaylight / controller / 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.controller.sal.rest.doc.impl;
9
10 import java.io.ByteArrayOutputStream;
11 import java.io.OutputStreamWriter;
12 import java.util.Map.Entry;
13
14 import javax.ws.rs.core.Response;
15 import javax.ws.rs.core.UriInfo;
16
17 import org.json.JSONWriter;
18 import org.opendaylight.controller.sal.rest.doc.api.ApiDocService;
19 import org.opendaylight.controller.sal.rest.doc.mountpoints.MountPointSwagger;
20 import org.opendaylight.controller.sal.rest.doc.swagger.ApiDeclaration;
21 import org.opendaylight.controller.sal.rest.doc.swagger.ResourceList;
22
23 /**
24  * This service generates swagger (See <a
25  * 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.
28  *
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.
34  *
35  */
36 public class ApiDocServiceImpl implements ApiDocService {
37
38     private static final ApiDocService INSTANCE = new ApiDocServiceImpl();
39
40     public static ApiDocService getInstance() {
41         return INSTANCE;
42     }
43
44     /**
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.
48      *
49      * @param uriInfo
50      * @return
51      */
52     @Override
53     public synchronized Response getRootDoc(UriInfo uriInfo) {
54         ApiDocGenerator generator = ApiDocGenerator.getInstance();
55         ResourceList rootDoc = generator.getResourceListing(uriInfo);
56
57         return Response.ok(rootDoc).build();
58     }
59
60     /**
61      * Generates Swagger compliant document listing APIs for module.
62      *
63      * @param module
64      * @param revision
65      * @param uriInfo
66      * @return
67      */
68     @Override
69     public synchronized Response getDocByModule(String module, String revision, UriInfo uriInfo) {
70         ApiDocGenerator generator = ApiDocGenerator.getInstance();
71
72         ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
73         return Response.ok(doc).build();
74     }
75
76     /**
77      * Redirects to embedded swagger ui.
78      *
79      * @param uriInfo
80      * @return
81      */
82     @Override
83     public synchronized Response getApiExplorer(UriInfo uriInfo) {
84         return Response
85                 .seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build())
86                 .build();
87     }
88
89     @Override
90     public synchronized Response getListOfMounts(UriInfo uriInfo) {
91         ByteArrayOutputStream baos = new ByteArrayOutputStream();
92         try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos)) {
93             JSONWriter writer = new JSONWriter(streamWriter);
94             writer.array();
95             for (Entry<String, Long> entry : MountPointSwagger.getInstance()
96                     .getInstanceIdentifiers().entrySet()) {
97                 writer.object();
98                 writer.key("instance").value(entry.getKey());
99                 writer.key("id").value(entry.getValue());
100                 writer.endObject();
101             }
102             writer.endArray();
103         } catch (Exception e) {
104             return Response.status(500).entity(e.getMessage()).build();
105         }
106         return Response.status(200).entity(baos.toString()).build();
107     }
108
109     @Override
110     public synchronized Response getMountRootDoc(String instanceNum, UriInfo uriInfo) {
111         ResourceList resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo,
112                 Long.parseLong(instanceNum));
113         return Response.ok(resourceList).build();
114     }
115
116     @Override
117     public synchronized Response getMountDocByModule(String instanceNum, String module,
118             String revision, UriInfo uriInfo) {
119         ApiDeclaration api = MountPointSwagger.getInstance().getMountPointApi(uriInfo,
120                 Long.parseLong(instanceNum), module, revision);
121         return Response.ok(api).build();
122     }
123
124 }