Merge "Bump ietf-yang-library module to newest draft"
[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
14 import javax.ws.rs.core.Response;
15 import javax.ws.rs.core.UriInfo;
16
17 import org.json.JSONWriter;
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;
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  * <p>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 public class ApiDocServiceImpl implements ApiDocService {
36
37     private static final ApiDocService INSTANCE = new ApiDocServiceImpl();
38
39     public static ApiDocService getInstance() {
40         return INSTANCE;
41     }
42
43     /**
44      * Generates index document for Swagger UI. This document lists out all
45      * modules with link to get APIs for each module. The API for each module is
46      * served by <code> getDocByModule()</code> method.
47      */
48     @Override
49     public synchronized Response getRootDoc(UriInfo uriInfo) {
50         ApiDocGenerator generator = ApiDocGenerator.getInstance();
51         ResourceList rootDoc = generator.getResourceListing(uriInfo);
52
53         return Response.ok(rootDoc).build();
54     }
55
56     /**
57      * Generates Swagger compliant document listing APIs for module.
58      */
59     @Override
60     public synchronized Response getDocByModule(String module, String revision, UriInfo uriInfo) {
61         ApiDocGenerator generator = ApiDocGenerator.getInstance();
62
63         ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
64         return Response.ok(doc).build();
65     }
66
67     /**
68      * Redirects to embedded swagger ui.
69      */
70     @Override
71     public synchronized Response getApiExplorer(UriInfo uriInfo) {
72         return Response
73                 .seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build())
74                 .build();
75     }
76
77     @Override
78     public synchronized Response getListOfMounts(UriInfo uriInfo) {
79         ByteArrayOutputStream baos = new ByteArrayOutputStream();
80         try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos)) {
81             JSONWriter writer = new JSONWriter(streamWriter);
82             writer.array();
83             for (Entry<String, Long> entry : MountPointSwagger.getInstance()
84                     .getInstanceIdentifiers().entrySet()) {
85                 writer.object();
86                 writer.key("instance").value(entry.getKey());
87                 writer.key("id").value(entry.getValue());
88                 writer.endObject();
89             }
90             writer.endArray();
91         } catch (Exception e) {
92             return Response.status(500).entity(e.getMessage()).build();
93         }
94         return Response.status(200).entity(baos.toString()).build();
95     }
96
97     @Override
98     public synchronized Response getMountRootDoc(String instanceNum, UriInfo uriInfo) {
99         ResourceList resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo,
100                 Long.parseLong(instanceNum));
101         return Response.ok(resourceList).build();
102     }
103
104     @Override
105     public synchronized Response getMountDocByModule(String instanceNum, String module,
106             String revision, UriInfo uriInfo) {
107         ApiDeclaration api = MountPointSwagger.getInstance().getMountPointApi(uriInfo,
108                 Long.parseLong(instanceNum), module, revision);
109         return Response.ok(api).build();
110     }
111
112 }