Bug 6382 - add apidoc for latest restconf 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.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 <a
24  * 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>NOTE: These API's need to be synchronized due to bug 1198. Thread access to
29  * the SchemaContext is not synchronized properly and thus you can end up with
30  * missing definitions without this synchronization. There are likely otherways
31  * to work around this limitation, but given that this API is a dev only tool
32  * and not dependent UI, this was the fastest work around.
33  */
34 public class ApiDocServiceImpl implements ApiDocService {
35
36     private static final ApiDocService INSTANCE = new ApiDocServiceImpl();
37
38     public static ApiDocService getInstance() {
39         return INSTANCE;
40     }
41
42     /**
43      * Generates index document for Swagger UI. This document lists out all
44      * modules with link to get APIs for each module. The API for each module is
45      * served by <code> getDocByModule()</code> method.
46      */
47     @Override
48     public synchronized Response getRootDoc(final UriInfo uriInfo) {
49         final ApiDocGenerator generator = ApiDocGenerator.getInstance();
50         if (isNew(uriInfo)) {
51             generator.setDraft(true);
52         } else {
53             generator.setDraft(false);
54         }
55         final 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     @Override
64     public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
65         final ApiDocGenerator generator = ApiDocGenerator.getInstance();
66         if (isNew(uriInfo)) {
67             generator.setDraft(true);
68         } else {
69             generator.setDraft(false);
70         }
71         final ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
72         return Response.ok(doc).build();
73     }
74
75     /**
76      * Redirects to embedded swagger ui.
77      */
78     @Override
79     public synchronized Response getApiExplorer(final UriInfo uriInfo) {
80         return Response
81                 .seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build())
82                 .build();
83     }
84
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()
92                     .getInstanceIdentifiers().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.getInstanceDraft17().getResourceList(uriInfo,
110                     Long.parseLong(instanceNum));
111         } else {
112             resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo,
113                     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.getInstanceDraft17().getMountPointApi(uriInfo,
124                     Long.parseLong(instanceNum), module, revision);
125         } else {
126             api = MountPointSwagger.getInstance().getMountPointApi(uriInfo,
127                     Long.parseLong(instanceNum), module, revision);
128         }
129         return Response.ok(api).build();
130     }
131
132     private static boolean isNew(final UriInfo uriInfo) {
133         return uriInfo.getBaseUri().toString().contains("/17/");
134     }
135 }