Fix findbugs violations in 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 com.fasterxml.jackson.core.JsonFactory;
11 import com.fasterxml.jackson.core.JsonGenerator;
12 import java.io.ByteArrayOutputStream;
13 import java.io.IOException;
14 import java.io.OutputStreamWriter;
15 import java.io.UnsupportedEncodingException;
16 import java.nio.charset.StandardCharsets;
17 import java.util.Map.Entry;
18 import javax.ws.rs.core.Response;
19 import javax.ws.rs.core.UriInfo;
20 import org.opendaylight.netconf.sal.rest.doc.api.ApiDocService;
21 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
22 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
23 import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
24
25 /**
26  * This service generates swagger (See
27  * <a href="https://helloreverb.com/developers/swagger"
28  * >https://helloreverb.com/developers/swagger</a>) compliant documentation for
29  * RESTCONF APIs. The output of this is used by embedded Swagger UI.
30  *
31  * <p>
32  * NOTE: These API's need to be synchronized due to bug 1198. Thread access to
33  * the SchemaContext is not synchronized properly and thus you can end up with
34  * missing definitions without this synchronization. There are likely otherways
35  * to work around this limitation, but given that this API is a dev only tool
36  * and not dependent UI, this was the fastest work around.
37  */
38 public class ApiDocServiceImpl implements ApiDocService {
39
40     private static final ApiDocService INSTANCE = new ApiDocServiceImpl();
41
42     public static ApiDocService getInstance() {
43         return INSTANCE;
44     }
45
46     /**
47      * Generates index document for Swagger UI. This document lists out all
48      * modules with link to get APIs for each module. The API for each module is
49      * served by <code> getDocByModule()</code> method.
50      */
51     @Override
52     public synchronized Response getRootDoc(final UriInfo uriInfo) {
53         final ApiDocGenerator generator = ApiDocGenerator.getInstance();
54         if (isNew(uriInfo)) {
55             generator.setDraft(true);
56         } else {
57             generator.setDraft(false);
58         }
59         final ResourceList rootDoc = generator.getResourceListing(uriInfo);
60
61         return Response.ok(rootDoc).build();
62     }
63
64     /**
65      * Generates Swagger compliant document listing APIs for module.
66      */
67     @Override
68     public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
69         final ApiDocGenerator generator = ApiDocGenerator.getInstance();
70         if (isNew(uriInfo)) {
71             generator.setDraft(true);
72         } else {
73             generator.setDraft(false);
74         }
75         final ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
76         return Response.ok(doc).build();
77     }
78
79     /**
80      * Redirects to embedded swagger ui.
81      */
82     @Override
83     public synchronized Response getApiExplorer(final UriInfo uriInfo) {
84         return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
85     }
86
87     @Override
88     public synchronized Response getListOfMounts(final UriInfo uriInfo) {
89         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
90         try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos, StandardCharsets.UTF_8)) {
91             JsonGenerator writer = new JsonFactory().createGenerator(streamWriter);
92             writer.writeStartArray();
93             for (final Entry<String, Long> entry : MountPointSwagger.getInstance().getInstanceIdentifiers()
94                     .entrySet()) {
95                 writer.writeStartObject();
96                 writer.writeObjectField("instance", entry.getKey());
97                 writer.writeObjectField("id", entry.getValue());
98                 writer.writeEndObject();
99             }
100             writer.writeEndArray();
101             writer.flush();
102         } catch (IOException e) {
103             return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
104         }
105
106         try {
107             String responseStr = baos.toString(StandardCharsets.UTF_8.name());
108             return Response.status(Response.Status.OK).entity(responseStr).build();
109         } catch (UnsupportedEncodingException e) {
110             return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
111         }
112     }
113
114     @Override
115     public synchronized Response getMountRootDoc(final String instanceNum, final UriInfo uriInfo) {
116         final ResourceList resourceList;
117         if (isNew(uriInfo)) {
118             resourceList = MountPointSwagger.getInstanceDraft18().getResourceList(uriInfo, Long.parseLong(instanceNum));
119         } else {
120             resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo, Long.parseLong(instanceNum));
121         }
122         return Response.ok(resourceList).build();
123     }
124
125     @Override
126     public synchronized Response getMountDocByModule(final String instanceNum, final String module,
127             final String revision, final UriInfo uriInfo) {
128         final ApiDeclaration api;
129         if (isNew(uriInfo)) {
130             api = MountPointSwagger.getInstanceDraft18().getMountPointApi(uriInfo, Long.parseLong(instanceNum), module,
131                     revision);
132         } else {
133             api = MountPointSwagger.getInstance().getMountPointApi(uriInfo, Long.parseLong(instanceNum), module,
134                     revision);
135         }
136         return Response.ok(api).build();
137     }
138
139     private static boolean isNew(final UriInfo uriInfo) {
140         return uriInfo.getBaseUri().toString().contains("/18/");
141     }
142 }