2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.rest.doc.impl;
10 import java.util.List;
11 import java.util.Objects;
12 import java.util.stream.Collectors;
14 import javax.ws.rs.core.Response;
15 import javax.ws.rs.core.UriInfo;
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.MountPointInstance;
21 import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
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.
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.
36 public class ApiDocServiceImpl implements ApiDocService {
38 private final MountPointSwagger mountPointSwaggerDraft02;
39 private final MountPointSwagger mountPointSwaggerRFC8040;
40 private final ApiDocGeneratorDraftO2 apiDocGeneratorDraft02;
41 private final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040;
43 public ApiDocServiceImpl(MountPointSwaggerGeneratorDraft02 mountPointSwaggerGeneratorDraft02,
44 MountPointSwaggerGeneratorRFC8040 mountPointSwaggerGeneratorRFC8040,
45 ApiDocGeneratorDraftO2 apiDocGeneratorDraft02, ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040) {
46 this.mountPointSwaggerDraft02 =
47 Objects.requireNonNull(mountPointSwaggerGeneratorDraft02).getMountPointSwagger();
48 this.mountPointSwaggerRFC8040 =
49 Objects.requireNonNull(mountPointSwaggerGeneratorRFC8040).getMountPointSwagger();
50 this.apiDocGeneratorDraft02 = Objects.requireNonNull(apiDocGeneratorDraft02);
51 this.apiDocGeneratorRFC8040 = Objects.requireNonNull(apiDocGeneratorRFC8040);
55 * Generates index document for Swagger UI. This document lists out all
56 * modules with link to get APIs for each module. The API for each module is
57 * served by <code> getDocByModule()</code> method.
60 public synchronized Response getRootDoc(final UriInfo uriInfo) {
61 final ResourceList rootDoc;
63 rootDoc = apiDocGeneratorRFC8040.getResourceListing(uriInfo);
65 rootDoc = apiDocGeneratorDraft02.getResourceListing(uriInfo);
68 return Response.ok(rootDoc).build();
72 * Generates Swagger compliant document listing APIs for module.
75 public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
76 final ApiDeclaration doc;
78 doc = apiDocGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo);
80 doc = apiDocGeneratorDraft02.getApiDeclaration(module, revision, uriInfo);
83 return Response.ok(doc).build();
87 * Redirects to embedded swagger ui.
90 public synchronized Response getApiExplorer(final UriInfo uriInfo) {
91 return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
95 public synchronized Response getListOfMounts(final UriInfo uriInfo) {
96 final MountPointSwagger mountPointSwagger;
98 mountPointSwagger = mountPointSwaggerRFC8040;
100 mountPointSwagger = mountPointSwaggerDraft02;
102 final List<MountPointInstance> entity = mountPointSwagger
103 .getInstanceIdentifiers().entrySet().stream()
104 .map(MountPointInstance::new).collect(Collectors.toList());
105 return Response.ok(entity).build();
109 public synchronized Response getMountRootDoc(final String instanceNum, final UriInfo uriInfo) {
110 final ResourceList resourceList;
111 if (isNew(uriInfo)) {
112 resourceList = mountPointSwaggerRFC8040.getResourceList(uriInfo, Long.parseLong(instanceNum));
114 resourceList = mountPointSwaggerDraft02.getResourceList(uriInfo, Long.parseLong(instanceNum));
116 return Response.ok(resourceList).build();
120 public synchronized Response getMountDocByModule(final String instanceNum, final String module,
121 final String revision, final UriInfo uriInfo) {
122 final ApiDeclaration api;
123 if (isNew(uriInfo)) {
124 api = mountPointSwaggerRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum), module, revision);
126 api = mountPointSwaggerDraft02.getMountPointApi(uriInfo, Long.parseLong(instanceNum), module, revision);
128 return Response.ok(api).build();
131 private static boolean isNew(final UriInfo uriInfo) {
132 return uriInfo.getBaseUri().toString().contains("/18/");