Bug 8351: Enforce check-style rules for restconf - sal-rest-docgen
[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
24  * <a 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>
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 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(final UriInfo uriInfo) {
50         final ApiDocGenerator generator = ApiDocGenerator.getInstance();
51         if (isNew(uriInfo)) {
52             generator.setDraft(true);
53         } else {
54             generator.setDraft(false);
55         }
56         final ResourceList rootDoc = generator.getResourceListing(uriInfo);
57
58         return Response.ok(rootDoc).build();
59     }
60
61     /**
62      * Generates Swagger compliant document listing APIs for module.
63      */
64     @Override
65     public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
66         final ApiDocGenerator generator = ApiDocGenerator.getInstance();
67         if (isNew(uriInfo)) {
68             generator.setDraft(true);
69         } else {
70             generator.setDraft(false);
71         }
72         final ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
73         return Response.ok(doc).build();
74     }
75
76     /**
77      * Redirects to embedded swagger ui.
78      */
79     @Override
80     public synchronized Response getApiExplorer(final UriInfo uriInfo) {
81         return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
82     }
83
84     @SuppressWarnings("checkstyle:IllegalCatch")
85     @Override
86     public synchronized Response getListOfMounts(final UriInfo uriInfo) {
87         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
88         try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos, StandardCharsets.UTF_8)) {
89             final JSONWriter writer = new JSONWriter(streamWriter);
90             writer.array();
91             for (final Entry<String, Long> entry : MountPointSwagger.getInstance().getInstanceIdentifiers()
92                     .entrySet()) {
93                 writer.object();
94                 writer.key("instance").value(entry.getKey());
95                 writer.key("id").value(entry.getValue());
96                 writer.endObject();
97             }
98             writer.endArray();
99         } catch (final Exception e) {
100             return Response.status(500).entity(e.getMessage()).build();
101         }
102         return Response.status(200).entity(baos.toString()).build();
103     }
104
105     @Override
106     public synchronized Response getMountRootDoc(final String instanceNum, final UriInfo uriInfo) {
107         final ResourceList resourceList;
108         if (isNew(uriInfo)) {
109             resourceList = MountPointSwagger.getInstanceDraft18().getResourceList(uriInfo, Long.parseLong(instanceNum));
110         } else {
111             resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo, Long.parseLong(instanceNum));
112         }
113         return Response.ok(resourceList).build();
114     }
115
116     @Override
117     public synchronized Response getMountDocByModule(final String instanceNum, final String module,
118             final String revision, final UriInfo uriInfo) {
119         final ApiDeclaration api;
120         if (isNew(uriInfo)) {
121             api = MountPointSwagger.getInstanceDraft18().getMountPointApi(uriInfo, Long.parseLong(instanceNum), module,
122                     revision);
123         } else {
124             api = MountPointSwagger.getInstance().getMountPointApi(uriInfo, Long.parseLong(instanceNum), module,
125                     revision);
126         }
127         return Response.ok(api).build();
128     }
129
130     private static boolean isNew(final UriInfo uriInfo) {
131         return uriInfo.getBaseUri().toString().contains("/18/");
132     }
133 }