Set default for boolean type
[netconf.git] / restconf / restconf-openapi / src / main / java / org / opendaylight / restconf / openapi / impl / OpenApiServiceImpl.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.restconf.openapi.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import java.util.List;
14 import java.util.stream.Collectors;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import javax.ws.rs.core.Response;
18 import javax.ws.rs.core.UriInfo;
19 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
20 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
21 import org.opendaylight.restconf.openapi.api.OpenApiService;
22 import org.opendaylight.restconf.openapi.model.MountPointInstance;
23 import org.opendaylight.restconf.openapi.model.OpenApiObject;
24 import org.opendaylight.restconf.openapi.mountpoints.MountPointOpenApi;
25 import org.osgi.service.component.annotations.Activate;
26 import org.osgi.service.component.annotations.Component;
27 import org.osgi.service.component.annotations.Reference;
28
29
30 /**
31  * This service generates swagger (See
32  * <a href="https://helloreverb.com/developers/swagger"
33  * >https://helloreverb.com/developers/swagger</a>) compliant documentation for
34  * RESTCONF APIs. The output of this is used by embedded Swagger UI.
35  *
36  * <p>
37  * NOTE: These API's need to be synchronized due to bug 1198. Thread access to
38  * the SchemaContext is not synchronized properly and thus you can end up with
39  * missing definitions without this synchronization. There are likely otherways
40  * to work around this limitation, but given that this API is a dev only tool
41  * and not dependent UI, this was the fastest work around.
42  */
43 @Component
44 @Singleton
45 public final class OpenApiServiceImpl implements OpenApiService {
46     // FIXME: make this configurable
47     public static final int DEFAULT_PAGESIZE = 20;
48
49     // Query parameter
50     private static final String PAGE_NUM = "pageNum";
51
52     private final MountPointOpenApi mountPointOpenApiRFC8040;
53     private final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040;
54
55     @Inject
56     @Activate
57     public OpenApiServiceImpl(final @Reference DOMSchemaService schemaService,
58                              final @Reference DOMMountPointService mountPointService) {
59         this(new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService),
60             new OpenApiGeneratorRFC8040(schemaService));
61     }
62
63     public OpenApiServiceImpl(final DOMSchemaService schemaService,
64                              final DOMMountPointService mountPointService,
65                              final String basePath) {
66         this(new MountPointOpenApiGeneratorRFC8040(schemaService, mountPointService, basePath),
67             new OpenApiGeneratorRFC8040(schemaService, basePath));
68     }
69
70     @VisibleForTesting
71     OpenApiServiceImpl(final MountPointOpenApiGeneratorRFC8040 mountPointOpenApiGeneratorRFC8040,
72                        final OpenApiGeneratorRFC8040 openApiGeneratorRFC8040) {
73         mountPointOpenApiRFC8040 = requireNonNull(mountPointOpenApiGeneratorRFC8040).getMountPointOpenApi();
74         this.openApiGeneratorRFC8040 = requireNonNull(openApiGeneratorRFC8040);
75     }
76
77     @Override
78     public synchronized Response getAllModulesDoc(final UriInfo uriInfo) {
79         final DefinitionNames definitionNames = new DefinitionNames();
80         final OpenApiObject doc = openApiGeneratorRFC8040.getAllModulesDoc(uriInfo, definitionNames);
81         return Response.ok(doc).build();
82     }
83
84     /**
85      * Generates Swagger compliant document listing APIs for module.
86      */
87     @Override
88     public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
89         return Response.ok(
90             openApiGeneratorRFC8040.getApiDeclaration(module, revision, uriInfo))
91             .build();
92     }
93
94     /**
95      * Redirects to embedded swagger ui.
96      */
97     @Override
98     public synchronized Response getApiExplorer(final UriInfo uriInfo) {
99         return Response.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build()).build();
100     }
101
102     @Override
103     public synchronized Response getListOfMounts(final UriInfo uriInfo) {
104         final List<MountPointInstance> entity = mountPointOpenApiRFC8040
105                 .getInstanceIdentifiers().entrySet().stream()
106                 .map(entry -> new MountPointInstance(entry.getKey(), entry.getValue()))
107                 .collect(Collectors.toList());
108         return Response.ok(entity).build();
109     }
110
111     @Override
112     public synchronized Response getMountDocByModule(final String instanceNum, final String module,
113                                                      final String revision, final UriInfo uriInfo) {
114         final OpenApiObject api = mountPointOpenApiRFC8040.getMountPointApi(uriInfo, Long.parseLong(instanceNum),
115             module, revision);
116         return Response.ok(api).build();
117     }
118
119     @Override
120     public synchronized Response getMountDoc(final String instanceNum, final UriInfo uriInfo) {
121         final String stringPageNum = uriInfo.getQueryParameters().getFirst(PAGE_NUM);
122         final OpenApiObject api = mountPointOpenApiRFC8040.getMountPointApi(uriInfo,
123                 Long.parseLong(instanceNum), stringPageNum);
124         return Response.ok(api).build();
125     }
126 }