2 // ========================LICENSE_START=================================
5 // Copyright (C) 2023: Nordix Foundation
6 // Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 // ========================LICENSE_END===================================
27 "github.com/getkin/kin-openapi/openapi3"
28 log "github.com/sirupsen/logrus"
33 "oransc.org/nonrtric/capifcore/internal/common"
34 "oransc.org/nonrtric/capifcore/internal/common29122"
35 "oransc.org/nonrtric/capifcore/internal/common29571"
36 "oransc.org/nonrtric/capifcore/internal/invokermanagementapi"
37 "oransc.org/nonrtric/capifcore/internal/providermanagementapi"
38 "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
41 type fn func() (swagger *openapi3.T, err error)
43 var smeOpenApiFileLocation string = "../../openapi/sme/"
48 fmt.Println("Generating SME openapi spec...")
49 os.MkdirAll(smeOpenApiFileLocation, 0755)
51 generateAndCopySwagger("TS29222_CAPIF_API_Provider_Management_API.yaml", providermanagementapi.GetSwagger)
52 generateAndCopySwagger("TS29222_CAPIF_Publish_Service_API.yaml", publishserviceapi.GetSwagger)
53 generateAndCopySwagger("TS29222_CAPIF_API_Invoker_Management_API.yaml", invokermanagementapi.GetSwagger)
54 generateAndCopySwagger("CommonData.yaml", common.GetSwagger)
55 generateAndCopySwagger("TS29122_CommonData.yaml", common29122.GetSwagger)
56 generateAndCopySwagger("TS29571_CommonData.yaml", common29571.GetSwagger)
60 func generateSwaggerYaml(swagger *openapi3.T, filename string) {
61 jsondataarr, jsondataarrerr := json.Marshal(&swagger)
62 if jsondataarrerr != nil {
63 log.Fatalf("Error loading json data from swagger \n: %s", jsondataarrerr)
66 var data map[string]interface{}
67 if err := json.Unmarshal(jsondataarr, &data); err != nil {
68 log.Fatalf("Error loading json data to map \n: %s", jsondataarrerr)
72 yamldataarr, yamldataarrerr := yaml.Marshal(&data)
73 if yamldataarrerr != nil {
74 log.Fatalf("Error loading json data map to array \n: %s", yamldataarrerr)
77 err2 := ioutil.WriteFile(filename, yamldataarr, 0755)
79 log.Fatalf("Error writing provider yaml \n: %s", err2)
83 func copy(srcFile string, targetFile string) error {
84 data, err := os.ReadFile(srcFile)
88 err = os.WriteFile(targetFile, data, 0644)
95 func generateAndCopySwagger(openApiFileName string, getSwagger fn) {
96 fmt.Printf("Generating %s...\n", openApiFileName)
97 swagger, err := getSwagger()
99 generateSwaggerYaml(swagger, openApiFileName)
100 fmt.Printf("Copying %s to %s \n", openApiFileName, smeOpenApiFileLocation + openApiFileName)
101 copy(openApiFileName, smeOpenApiFileLocation +openApiFileName)