Refactor SME swagger generation and remove OS profiles 00/12600/2
authoraravind.est <aravindhan.a@est.tech>
Thu, 7 Mar 2024 18:32:15 +0000 (18:32 +0000)
committeraravind.est <aravindhan.a@est.tech>
Fri, 8 Mar 2024 09:07:42 +0000 (09:07 +0000)
SME swagger generation logic moved to go. OS based files removed.
OS based profile handling in maven removed.

Issue-ID: NONRTRIC-986
Change-Id: Ic33050488843fbd5400cb754092e0630ba40dc03
Signed-off-by: aravind.est <aravindhan.a@est.tech>
pom.xml
rapp-manager-sme/pom.xml
scripts/init/getsmeswagger.go
scripts/init/init-sme-spec.bat [deleted file]
scripts/init/init-sme-spec.sh [deleted file]

diff --git a/pom.xml b/pom.xml
index 79d2934..f392133 100755 (executable)
--- a/pom.xml
+++ b/pom.xml
         <slf4j.version>2.0.12</slf4j.version>
         <apache.httpcore.version>4.4.16</apache.httpcore.version>
         <exec-maven-plugin.version>3.1.0</exec-maven-plugin.version>
+        <copy-resources-maven-plugin.version>3.3.1</copy-resources-maven-plugin.version>
         <apache.compress.version>1.25.0</apache.compress.version>
     </properties>
-    <profiles>
-        <profile>
-            <id>Windows</id>
-            <activation>
-                <os>
-                    <family>Windows</family>
-                </os>
-            </activation>
-            <properties>
-                <script.extension>.bat</script.extension>
-                <file.separator>\</file.separator>
-            </properties>
-        </profile>
-        <profile>
-            <id>unix</id>
-            <activation>
-                <os>
-                    <family>unix</family>
-                </os>
-            </activation>
-            <properties>
-                <script.extension>.sh</script.extension>
-                <file.separator>/</file.separator>
-            </properties>
-        </profile>
-    </profiles>
     <build>
         <plugins>
             <plugin>
index eb82cfa..dd877d2 100755 (executable)
                     </execution>
                     <execution>
                         <id>initialize-sme-openapi-specs</id>
-                        <phase>initialize</phase>
+                        <phase>generate-sources</phase>
                         <goals>
                             <goal>exec</goal>
                         </goals>
                         <configuration>
+                            <workingDirectory>
+                                ${project.parent.basedir}/sme/capifcore
+                            </workingDirectory>
                             <executable>
-                                ..${file.separator}scripts${file.separator}init${file.separator}init-sme-spec${script.extension}
+                                go
                             </executable>
+                            <arguments>
+                                <argument>run</argument>
+                                <argument>getsmeswagger.go</argument>
+                            </arguments>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <artifactId>maven-resources-plugin</artifactId>
+                <version>${copy-resources-maven-plugin.version}</version>
+                <executions>
+                    <execution>
+                        <id>copy-swagger-generator</id>
+                        <phase>initialize</phase>
+                        <goals>
+                            <goal>copy-resources</goal>
+                        </goals>
+                        <configuration>
+                            <outputDirectory>${project.parent.basedir}/sme/capifcore</outputDirectory>
+                            <resources>
+                                <resource>
+                                    <directory>${project.parent.basedir}/scripts/init/</directory>
+                                    <includes>
+                                        <include>getsmeswagger.go</include>
+                                    </includes>
+                                    <filtering>false</filtering>
+                                </resource>
+                            </resources>
                         </configuration>
                     </execution>
                 </executions>
index 65826a2..0e2c8ce 100755 (executable)
@@ -3,6 +3,7 @@
 //   O-RAN-SC
 //   %%
 //   Copyright (C) 2023: Nordix Foundation
+//   Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
 //   %%
 //   Licensed under the Apache License, Version 2.0 (the "License");
 //   you may not use this file except in compliance with the License.
 
 package main
 
-
 import (
+       "encoding/json"
+       "fmt"
        "github.com/getkin/kin-openapi/openapi3"
        log "github.com/sirupsen/logrus"
        "gopkg.in/yaml.v3"
-       "encoding/json"
-        "io/ioutil"
+       "io/ioutil"
+       "os"
 
+       "oransc.org/nonrtric/capifcore/internal/common"
+       "oransc.org/nonrtric/capifcore/internal/common29122"
+       "oransc.org/nonrtric/capifcore/internal/common29571"
        "oransc.org/nonrtric/capifcore/internal/invokermanagementapi"
        "oransc.org/nonrtric/capifcore/internal/providermanagementapi"
        "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
-    "oransc.org/nonrtric/capifcore/internal/common"
-    "oransc.org/nonrtric/capifcore/internal/common29122"
-    "oransc.org/nonrtric/capifcore/internal/common29571"
 )
 
-func main() {
-       var swagger *openapi3.T
-       var err error
-
-       swagger,err = providermanagementapi.GetSwagger()
-    if err == nil {
-        generateSwaggerYaml(swagger, "TS29222_CAPIF_API_Provider_Management_API.yaml")
-    }
-
-    swagger,err = publishserviceapi.GetSwagger()
-    if err == nil {
-        generateSwaggerYaml(swagger, "TS29222_CAPIF_Publish_Service_API.yaml")
-    }
-
-    swagger,err = invokermanagementapi.GetSwagger()
-    if err == nil {
-        generateSwaggerYaml(swagger, "TS29222_CAPIF_API_Invoker_Management_API.yaml")
-    }
+type fn func() (swagger *openapi3.T, err error)
 
-    swagger,err = common.GetSwagger()
-    if err == nil {
-        generateSwaggerYaml(swagger, "CommonData.yaml")
-    }
+var smeOpenApiFileLocation string = "../../openapi/sme/"
 
-    swagger,err = common29122.GetSwagger()
-    if err == nil {
-        generateSwaggerYaml(swagger, "TS29122_CommonData.yaml")
-    }
+func main() {
 
-    swagger,err = common29571.GetSwagger()
-    if err == nil {
-        generateSwaggerYaml(swagger, "TS29571_CommonData.yaml")
-    }
+       var err error
+       fmt.Println("Generating SME openapi spec...")
+       os.MkdirAll(smeOpenApiFileLocation, 0755)
+       if err == nil {
+               generateAndCopySwagger("TS29222_CAPIF_API_Provider_Management_API.yaml", providermanagementapi.GetSwagger)
+               generateAndCopySwagger("TS29222_CAPIF_Publish_Service_API.yaml", publishserviceapi.GetSwagger)
+               generateAndCopySwagger("TS29222_CAPIF_API_Invoker_Management_API.yaml", invokermanagementapi.GetSwagger)
+               generateAndCopySwagger("CommonData.yaml", common.GetSwagger)
+               generateAndCopySwagger("TS29122_CommonData.yaml", common29122.GetSwagger)
+               generateAndCopySwagger("TS29571_CommonData.yaml", common29571.GetSwagger)
+       }
 }
 
 func generateSwaggerYaml(swagger *openapi3.T, filename string) {
-    jsondataarr, jsondataarrerr := json.Marshal(&swagger)
-    if jsondataarrerr != nil {
-        log.Fatalf("Error loading json data from swagger \n: %s", jsondataarrerr)
-    }
+       jsondataarr, jsondataarrerr := json.Marshal(&swagger)
+       if jsondataarrerr != nil {
+               log.Fatalf("Error loading json data from swagger \n: %s", jsondataarrerr)
+       }
 
-    var data map[string]interface{}
-    if err := json.Unmarshal(jsondataarr, &data); err != nil {
-    log.Fatalf("Error loading json data to map \n: %s", jsondataarrerr)
-        log.Fatal(err)
-    }
+       var data map[string]interface{}
+       if err := json.Unmarshal(jsondataarr, &data); err != nil {
+               log.Fatalf("Error loading json data to map \n: %s", jsondataarrerr)
+               log.Fatal(err)
+       }
 
-    yamldataarr, yamldataarrerr := yaml.Marshal(&data)
-    if yamldataarrerr != nil {
-        log.Fatalf("Error loading json data map to array \n: %s", yamldataarrerr)
-    }
+       yamldataarr, yamldataarrerr := yaml.Marshal(&data)
+       if yamldataarrerr != nil {
+               log.Fatalf("Error loading json data map to array \n: %s", yamldataarrerr)
+       }
+
+       err2 := ioutil.WriteFile(filename, yamldataarr, 0755)
+       if err2 != nil {
+               log.Fatalf("Error writing provider yaml \n: %s", err2)
+       }
+}
 
-    err2 := ioutil.WriteFile(filename, yamldataarr, 0755)
-    if err2 != nil {
-        log.Fatalf("Error writing provider yaml \n: %s", err2)
-    }
-}
\ No newline at end of file
+func copy(srcFile string, targetFile string) error {
+       data, err := os.ReadFile(srcFile)
+       if err != nil {
+               return err
+       }
+       err = os.WriteFile(targetFile, data, 0644)
+       if err != nil {
+               return err
+       }
+       return nil
+}
+
+func generateAndCopySwagger(openApiFileName string, getSwagger fn) {
+       fmt.Printf("Generating %s...\n", openApiFileName)
+       swagger, err := getSwagger()
+       if err == nil {
+               generateSwaggerYaml(swagger, openApiFileName)
+               fmt.Printf("Copying %s to %s \n", openApiFileName, smeOpenApiFileLocation + openApiFileName)
+               copy(openApiFileName, smeOpenApiFileLocation +openApiFileName)
+       }
+}
diff --git a/scripts/init/init-sme-spec.bat b/scripts/init/init-sme-spec.bat
deleted file mode 100755 (executable)
index e9ee0b6..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-:: ============LICENSE_START===============================================
-::  Copyright (C) 2023 Nordix Foundation. All rights reserved.
-::  ========================================================================
-::  Licensed under the Apache License, Version 2.0 (the "License");
-::  you may not use this file except in compliance with the License.
-::  You may obtain a copy of the License at
-::
-::       http:\\www.apache.org\licenses\LICENSE-2.0
-::
-::  Unless required by applicable law or agreed to in writing, software
-::  distributed under the License is distributed on an "AS IS" BASIS,
-::  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-::  See the License for the specific language governing permissions and
-::  limitations under the License.
-::  ============LICENSE_END=================================================
-::
-
-@echo off
-set SME_LOCATION="..\sme\capifcore"
-set SME_OPENAPI_LOCATION="..\..\openapi\sme"
-cp ..\scripts\init\getsmeswagger.go %SME_LOCATION%
-cd %SME_LOCATION%
-
-echo Generating SME openapi spec...
-
-IF EXIST getsmeswagger.go (
-  echo Generating...
-  go run getsmeswagger.go
-
-  echo Copying generated specs...
-  mkdir %SME_OPENAPI_LOCATION%
-  echo Copying CommonData.yaml
-  mv CommonData.yaml %SME_OPENAPI_LOCATION%
-  echo Copying TS29122_CommonData.yaml
-  mv TS29122_CommonData.yaml %SME_OPENAPI_LOCATION%
-  echo Copying TS29571_CommonData.yaml
-  mv TS29571_CommonData.yaml %SME_OPENAPI_LOCATION%
-  echo Copying TS29222_CAPIF_API_Invoker_Management_API.yaml
-  mv TS29222_CAPIF_API_Invoker_Management_API.yaml %SME_OPENAPI_LOCATION%
-  echo Copying TS29222_CAPIF_API_Provider_Management_API.yaml
-  mv TS29222_CAPIF_API_Provider_Management_API.yaml %SME_OPENAPI_LOCATION%
-  echo Copying TS29222_CAPIF_Publish_Service_API.yaml
-  mv TS29222_CAPIF_Publish_Service_API.yaml %SME_OPENAPI_LOCATION%
-) ELSE (
-  echo Unable to find the openapi spec generator.
-)
diff --git a/scripts/init/init-sme-spec.sh b/scripts/init/init-sme-spec.sh
deleted file mode 100755 (executable)
index ee3e108..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-#!/bin/bash
-
-#  ============LICENSE_START===============================================
-#  Copyright (C) 2023 Nordix Foundation. All rights reserved.
-#  ========================================================================
-#  Licensed under the Apache License, Version 2.0 (the "License");
-#  you may not use this file except in compliance with the License.
-#  You may obtain a copy of the License at
-#
-#       http://www.apache.org/licenses/LICENSE-2.0
-#
-#  Unless required by applicable law or agreed to in writing, software
-#  distributed under the License is distributed on an "AS IS" BASIS,
-#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-#  See the License for the specific language governing permissions and
-#  limitations under the License.
-#  ============LICENSE_END=================================================
-#
-
-SME_LOCATION="../sme/capifcore"
-SME_OPENAPI_LOCATION="../../openapi/sme"
-cp ../scripts/init/getsmeswagger.go $SME_LOCATION
-cd $SME_LOCATION
-
-echo "Generating SME openapi spec..."
-
-if [ -f "getsmeswagger.go" ]; then
-  echo "Generating..."
-  go run getsmeswagger.go
-
-  echo "Copying generated specs..."
-  mkdir -p $SME_OPENAPI_LOCATION
-  echo "Copying CommonData.yaml"
-  mv CommonData.yaml $SME_OPENAPI_LOCATION
-  echo "Copying TS29122_CommonData.yaml"
-  mv TS29122_CommonData.yaml $SME_OPENAPI_LOCATION
-  echo "Copying TS29571_CommonData.yaml"
-  mv TS29571_CommonData.yaml $SME_OPENAPI_LOCATION
-  echo "Copying TS29222_CAPIF_API_Invoker_Management_API.yaml"
-  mv TS29222_CAPIF_API_Invoker_Management_API.yaml $SME_OPENAPI_LOCATION
-  echo "Copying TS29222_CAPIF_API_Provider_Management_API.yaml"
-  mv TS29222_CAPIF_API_Provider_Management_API.yaml $SME_OPENAPI_LOCATION
-  echo "Copying TS29222_CAPIF_Publish_Service_API.yaml"
-  mv TS29222_CAPIF_Publish_Service_API.yaml $SME_OPENAPI_LOCATION
-else
-  echo "Unable to find the openapi spec generator."
-fi