7db2161cc008d12421b6951566fe771277e5cef8
[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.OutputStreamWriter;
14 import java.nio.charset.StandardCharsets;
15 import java.util.Map.Entry;
16 import javax.ws.rs.core.Response;
17 import javax.ws.rs.core.UriInfo;
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
25  * <a 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>
30  * NOTE: These API's need to be synchronized due to bug 1198. Thread access to
31  * the SchemaContext is not synchronized properly and thus you can end up with
32  * missing definitions without this synchronization. There are likely otherways
33  * to work around this limitation, but given that this API is a dev only tool
34  * and not dependent UI, this was the fastest work around.
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     @Override
50     public synchronized Response getRootDoc(final UriInfo uriInfo) {
51         final ApiDocGenerator generator = ApiDocGenerator.getInstance();
52         if (isNew(uriInfo)) {
53             generator.setDraft(true);
54         } else {
55             generator.setDraft(false);
56         }
57         final ResourceList rootDoc = generator.getResourceListing(uriInfo);
58
59         return Response.ok(rootDoc).build();
60     }
61
62     /**
63      * Generates Swagger compliant document listing APIs for module.
64      */
65     @Override
66     public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
67         final ApiDocGenerator generator = ApiDocGenerator.getInstance();
68         if (isNew(uriInfo)) {
69             generator.setDraft(true);
70         } else {
71             generator.setDraft(false);
72         }
73         final ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
74         return Response.ok(doc).build();
75     }
76
77     /**
78      * Redirects to embedded swagger ui.
79      */
80     @Override
81     public synchronized Response getApiExplorer(final UriInfo uriInfo) {
82         return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
83     }
84
85     @SuppressWarnings("checkstyle:IllegalCatch")
86     @Override
87     public synchronized Response getListOfMounts(final UriInfo uriInfo) {
88         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
89         try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos, StandardCharsets.UTF_8)) {
90             JsonGenerator writer = new JsonFactory().createGenerator(streamWriter);
91             writer.writeStartArray();
92             for (final Entry<String, Long> entry : MountPointSwagger.getInstance().getInstanceIdentifiers()
93                     .entrySet()) {
94                 writer.writeStartObject();
95                 writer.writeObjectField("instance", entry.getKey());
96                 writer.writeObjectField("id", entry.getValue());
97                 writer.writeEndObject();
98             }
99             writer.writeEndArray();
100             writer.flush();
101         } catch (final Exception e) {
102             return Response.status(500).entity(e.getMessage()).build();
103         }
104         return Response.status(200).entity(baos.toString()).build();
105     }
106
107     @Override
108     public synchronized Response getMountRootDoc(final String instanceNum, final UriInfo uriInfo) {
109         final ResourceList resourceList;
110         if (isNew(uriInfo)) {
111             resourceList = MountPointSwagger.getInstanceDraft18().getResourceList(uriInfo, Long.parseLong(instanceNum));
112         } else {
113             resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo, Long.parseLong(instanceNum));
114         }
115         return Response.ok(resourceList).build();
116     }
117
118     @Override
119     public synchronized Response getMountDocByModule(final String instanceNum, final String module,
120             final String revision, final UriInfo uriInfo) {
121         final ApiDeclaration api;
122         if (isNew(uriInfo)) {
123             api = MountPointSwagger.getInstanceDraft18().getMountPointApi(uriInfo, Long.parseLong(instanceNum), module,
124                     revision);
125         } else {
126             api = MountPointSwagger.getInstance().getMountPointApi(uriInfo, Long.parseLong(instanceNum), module,
127                     revision);
128         }
129         return Response.ok(api).build();
130     }
131
132     private static boolean isNew(final UriInfo uriInfo) {
133         return uriInfo.getBaseUri().toString().contains("/18/");
134     }
135 }