Convert sal-rest-docgen to OSGI DS 13/96713/14
authorTomas Cere <tomas.cere@pantheon.tech>
Tue, 22 Jun 2021 10:35:57 +0000 (12:35 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 13 Feb 2023 09:54:35 +0000 (09:54 +0000)
The generator is a few simple components. Rework their wiring to work
on top of OSGi DS.

JIRA: NETCONF-771
Change-Id: Iee36451c148c5fd414bda3c3ab43c7c8bac0b963
Signed-off-by: Tomas Cere <tomas.cere@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/sal-rest-docgen/pom.xml
restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/impl/AllModulesDocGenerator.java [deleted file]
restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/impl/ApiDocServiceImpl.java
restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/jaxrs/ApiDocApplication.java
restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/jaxrs/WebInitializer.java
restconf/sal-rest-docgen/src/main/resources/OSGI-INF/blueprint/blueprint.xml [deleted file]
restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/ApiDocServiceImplTest.java

index ff24f62a4313d0d854259abdf88b44a1fa020241..788b51da98e883e6b8ae13ccd6804223bb369a4f 100644 (file)
       <artifactId>guava</artifactId>
     </dependency>
 
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>javax.servlet-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>jakarta.ws.rs</groupId>
+      <artifactId>jakarta.ws.rs-api</artifactId>
+    </dependency>
+
     <dependency>
       <groupId>org.opendaylight.mdsal</groupId>
       <artifactId>mdsal-dom-api</artifactId>
       <artifactId>yang-model-util</artifactId>
     </dependency>
 
+    <dependency>
+      <groupId>com.guicedee.services</groupId>
+      <artifactId>javax.inject</artifactId>
+      <optional>true</optional>
+    </dependency>
+    <dependency>
+      <groupId>jakarta.annotation</groupId>
+      <artifactId>jakarta.annotation-api</artifactId>
+      <scope>provided</scope>
+      <optional>true</optional>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.service.component.annotations</artifactId>
+    </dependency>
+
     <dependency>
       <groupId>org.opendaylight.aaa.web</groupId>
       <artifactId>web-api</artifactId>
diff --git a/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/impl/AllModulesDocGenerator.java b/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/impl/AllModulesDocGenerator.java
deleted file mode 100644 (file)
index ee6e0ff..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.netconf.sal.rest.doc.impl;
-
-import static java.util.Objects.requireNonNull;
-
-import javax.ws.rs.core.UriInfo;
-import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.OAversion;
-import org.opendaylight.netconf.sal.rest.doc.swagger.CommonApiObject;
-import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
-
-public class AllModulesDocGenerator {
-    private final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040;
-
-    public AllModulesDocGenerator(final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040) {
-        this.apiDocGeneratorRFC8040 = requireNonNull(apiDocGeneratorRFC8040);
-    }
-
-    public CommonApiObject getAllModulesDoc(final UriInfo uriInfo, final OAversion oaversion) {
-        final DefinitionNames definitionNames = new DefinitionNames();
-        final SwaggerObject doc = apiDocGeneratorRFC8040.getAllModulesDoc(uriInfo, definitionNames, oaversion);
-
-        return BaseYangSwaggerGenerator.getAppropriateDoc(doc, oaversion);
-    }
-}
index 21dd225952bd73f8888c5584e6c27cf4e200cc32..06cb65fd9af64a0a1f4020db86819ac1dc20f61e 100644 (file)
@@ -9,15 +9,24 @@ package org.opendaylight.netconf.sal.rest.doc.impl;
 
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.annotations.VisibleForTesting;
 import java.util.List;
 import java.util.Optional;
 import java.util.stream.Collectors;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
+import org.opendaylight.mdsal.dom.api.DOMMountPointService;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.netconf.sal.rest.doc.api.ApiDocService;
 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
 import org.opendaylight.netconf.sal.rest.doc.swagger.CommonApiObject;
 import org.opendaylight.netconf.sal.rest.doc.swagger.MountPointInstance;
+import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 
 
 /**
@@ -33,9 +42,12 @@ import org.opendaylight.netconf.sal.rest.doc.swagger.MountPointInstance;
  * to work around this limitation, but given that this API is a dev only tool
  * and not dependent UI, this was the fastest work around.
  */
-public class ApiDocServiceImpl implements ApiDocService {
-
+@Component
+@Singleton
+public final class ApiDocServiceImpl implements ApiDocService {
+    // FIXME: make this configurable
     public static final int DEFAULT_PAGESIZE = 20;
+
     // Query parameter
     private static final String PAGE_NUM = "pageNum";
 
@@ -43,20 +55,29 @@ public class ApiDocServiceImpl implements ApiDocService {
 
     private final MountPointSwagger mountPointSwaggerRFC8040;
     private final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040;
-    private final AllModulesDocGenerator allModulesDocGenerator;
 
-    public ApiDocServiceImpl(final MountPointSwaggerGeneratorRFC8040 mountPointSwaggerGeneratorRFC8040,
-                             final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040,
-                             final AllModulesDocGenerator allModulesDocGenerator) {
-        mountPointSwaggerRFC8040 =
-                requireNonNull(mountPointSwaggerGeneratorRFC8040).getMountPointSwagger();
+    @Inject
+    @Activate
+    public ApiDocServiceImpl(final @Reference DOMSchemaService schemaService,
+                             final @Reference DOMMountPointService mountPointService) {
+        this(new MountPointSwaggerGeneratorRFC8040(schemaService, mountPointService),
+            new ApiDocGeneratorRFC8040(schemaService));
+    }
+
+    @VisibleForTesting
+    ApiDocServiceImpl(final MountPointSwaggerGeneratorRFC8040 mountPointSwaggerGeneratorRFC8040,
+                      final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040) {
+        mountPointSwaggerRFC8040 = requireNonNull(mountPointSwaggerGeneratorRFC8040).getMountPointSwagger();
         this.apiDocGeneratorRFC8040 = requireNonNull(apiDocGeneratorRFC8040);
-        this.allModulesDocGenerator = requireNonNull(allModulesDocGenerator);
     }
 
     @Override
     public synchronized Response getAllModulesDoc(final UriInfo uriInfo) {
-        return Response.ok(allModulesDocGenerator.getAllModulesDoc(uriInfo, identifyOpenApiVersion(uriInfo))).build();
+        final OAversion oaversion = identifyOpenApiVersion(uriInfo);
+        final DefinitionNames definitionNames = new DefinitionNames();
+        final SwaggerObject doc = apiDocGeneratorRFC8040.getAllModulesDoc(uriInfo, definitionNames, oaversion);
+
+        return Response.ok(BaseYangSwaggerGenerator.getAppropriateDoc(doc, oaversion)).build();
     }
 
     /**
index 2b9d6cdce00085ce42c5312b300d5629aa3a38ed..ce573abddde7aadf081cfcaf817947a9be53f1b7 100644 (file)
@@ -14,7 +14,8 @@ import java.util.Set;
 import javax.ws.rs.core.Application;
 import org.opendaylight.netconf.sal.rest.doc.api.ApiDocService;
 
-public class ApiDocApplication extends Application {
+// FIXME: hide this class
+public final class ApiDocApplication extends Application {
     private final ApiDocService apiDocService;
 
     public ApiDocApplication(final ApiDocService apiDocService) {
index dcf80277a7eeda099e4768533b16132788ebfeb1..457acdd95c59af8f2ae0d65f3ee7a82654b23ee2 100644 (file)
@@ -7,32 +7,44 @@
  */
 package org.opendaylight.netconf.sal.rest.doc.jaxrs;
 
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import javax.servlet.ServletException;
-import javax.ws.rs.core.Application;
 import org.opendaylight.aaa.web.ResourceDetails;
 import org.opendaylight.aaa.web.ServletDetails;
 import org.opendaylight.aaa.web.WebContext;
 import org.opendaylight.aaa.web.WebContextSecurer;
 import org.opendaylight.aaa.web.WebServer;
 import org.opendaylight.aaa.web.servlet.ServletSupport;
+import org.opendaylight.netconf.sal.rest.doc.api.ApiDocService;
 import org.opendaylight.yangtools.concepts.Registration;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
 
 /**
  * Initializes the wep app.
  *
  * @author Thomas Pantelis
  */
+@Singleton
+@Component(service = { })
 public final class WebInitializer implements AutoCloseable {
     private final Registration registration;
 
-    public WebInitializer(final WebServer webServer, final WebContextSecurer webContextSecurer,
-            final ServletSupport servletSupport, final Application webApp) throws ServletException {
-        var webContextBuilder = WebContext.builder()
+    @Inject
+    @Activate
+    public WebInitializer(@Reference final WebServer webServer, @Reference final WebContextSecurer webContextSecurer,
+            @Reference final ServletSupport servletSupport, @Reference final ApiDocService apiDocService)
+                throws ServletException {
+        final var webContextBuilder = WebContext.builder()
             .name("OpenAPI")
             .contextPath("/apidoc")
             .supportsSessions(true)
             .addServlet(ServletDetails.builder()
-                .servlet(servletSupport.createHttpServletBuilder(webApp).build())
+                .servlet(servletSupport.createHttpServletBuilder(new ApiDocApplication(apiDocService)).build())
                 .addUrlPattern("/swagger2/apis/*")
                 .addUrlPattern("/openapi3/apis/*")
                 .build())
@@ -44,6 +56,8 @@ public final class WebInitializer implements AutoCloseable {
     }
 
     @Override
+    @Deactivate
+    @PreDestroy
     public void close() {
         registration.close();
     }
diff --git a/restconf/sal-rest-docgen/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/restconf/sal-rest-docgen/src/main/resources/OSGI-INF/blueprint/blueprint.xml
deleted file mode 100644 (file)
index 439099b..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!-- vi: set et smarttab sw=4 tabstop=4: -->
-<!--
-Copyright © 2017 Inocybe Technologies and others. All rights reserved.
-
-This program and the accompanying materials are made available under the
-terms of the Eclipse Public License v1.0 which accompanies this distribution,
-and is available at http://www.eclipse.org/legal/epl-v10.html
--->
-<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
-    <reference id="schemaService" interface="org.opendaylight.mdsal.dom.api.DOMSchemaService"/>
-    <reference id="domMountPointService" interface="org.opendaylight.mdsal.dom.api.DOMMountPointService"/>
-    <reference id="webServer" interface="org.opendaylight.aaa.web.WebServer"/>
-    <reference id="webContextSecurer" interface="org.opendaylight.aaa.web.WebContextSecurer"/>
-    <reference id="servletSupport" interface="org.opendaylight.aaa.web.servlet.ServletSupport" />
-
-    <bean id="mountPointRFC8040" class="org.opendaylight.netconf.sal.rest.doc.impl.MountPointSwaggerGeneratorRFC8040"
-            destroy-method="close">
-        <argument ref="schemaService" />
-        <argument ref="domMountPointService" />
-    </bean>
-
-    <bean id="apiDocRFC8040" class="org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGeneratorRFC8040">
-        <argument ref="schemaService" />
-    </bean>
-
-    <bean id="allModulesDoc" class="org.opendaylight.netconf.sal.rest.doc.impl.AllModulesDocGenerator">
-        <argument ref="apiDocRFC8040" />
-    </bean>
-
-    <bean id="apiDocService" class="org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl">
-        <argument ref="mountPointRFC8040" />
-        <argument ref="apiDocRFC8040" />
-        <argument ref="allModulesDoc" />
-    </bean>
-
-    <bean id="apiDocApplication" class="org.opendaylight.netconf.sal.rest.doc.jaxrs.ApiDocApplication">
-        <argument ref="apiDocService" />
-    </bean>
-
-    <bean id="webInitializer" class="org.opendaylight.netconf.sal.rest.doc.jaxrs.WebInitializer"
-            destroy-method="close">
-        <argument ref="webServer"/>
-        <argument ref="webContextSecurer"/>
-        <argument ref="servletSupport"/>
-        <argument ref="apiDocApplication"/>
-    </bean>
-</blueprint>
index faf0b6bc20476fd593881190b4bf5b4855856ec3..04dd409d91db58c626d1b2775d6bc88d12949b01 100644 (file)
@@ -43,8 +43,7 @@ public final class ApiDocServiceImplTest extends AbstractApiDocTest {
                 new MountPointSwaggerGeneratorRFC8040(SCHEMA_SERVICE, service);
         final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040 = new ApiDocGeneratorRFC8040(SCHEMA_SERVICE);
         mountPointRFC8040.getMountPointSwagger().onMountPointCreated(INSTANCE_ID);
-        final AllModulesDocGenerator allModulesDocGenerator = new AllModulesDocGenerator(apiDocGeneratorRFC8040);
-        apiDocService = new ApiDocServiceImpl(mountPointRFC8040, apiDocGeneratorRFC8040, allModulesDocGenerator);
+        apiDocService = new ApiDocServiceImpl(mountPointRFC8040, apiDocGeneratorRFC8040);
     }
 
     @Test