blob: 0e2c8ce27fd6062b97aac58db038c25288775958 [file] [log] [blame]
aravind.est347f1782023-07-14 09:53:41 +01001// -
2// ========================LICENSE_START=================================
3// O-RAN-SC
4// %%
5// Copyright (C) 2023: Nordix Foundation
aravind.estbd79b242024-03-07 18:32:15 +00006// Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
aravind.est347f1782023-07-14 09:53:41 +01007// %%
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
11//
12// http://www.apache.org/licenses/LICENSE-2.0
13//
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===================================
20//
21
22package main
23
aravind.est347f1782023-07-14 09:53:41 +010024import (
aravind.estbd79b242024-03-07 18:32:15 +000025 "encoding/json"
26 "fmt"
aravind.est347f1782023-07-14 09:53:41 +010027 "github.com/getkin/kin-openapi/openapi3"
28 log "github.com/sirupsen/logrus"
29 "gopkg.in/yaml.v3"
aravind.estbd79b242024-03-07 18:32:15 +000030 "io/ioutil"
31 "os"
aravind.est347f1782023-07-14 09:53:41 +010032
aravind.estbd79b242024-03-07 18:32:15 +000033 "oransc.org/nonrtric/capifcore/internal/common"
34 "oransc.org/nonrtric/capifcore/internal/common29122"
35 "oransc.org/nonrtric/capifcore/internal/common29571"
aravind.est347f1782023-07-14 09:53:41 +010036 "oransc.org/nonrtric/capifcore/internal/invokermanagementapi"
37 "oransc.org/nonrtric/capifcore/internal/providermanagementapi"
38 "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
aravind.est347f1782023-07-14 09:53:41 +010039)
40
aravind.estbd79b242024-03-07 18:32:15 +000041type fn func() (swagger *openapi3.T, err error)
42
43var smeOpenApiFileLocation string = "../../openapi/sme/"
44
aravind.est347f1782023-07-14 09:53:41 +010045func main() {
aravind.estbd79b242024-03-07 18:32:15 +000046
aravind.est347f1782023-07-14 09:53:41 +010047 var err error
aravind.estbd79b242024-03-07 18:32:15 +000048 fmt.Println("Generating SME openapi spec...")
49 os.MkdirAll(smeOpenApiFileLocation, 0755)
50 if err == nil {
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)
57 }
aravind.est347f1782023-07-14 09:53:41 +010058}
59
60func generateSwaggerYaml(swagger *openapi3.T, filename string) {
aravind.estbd79b242024-03-07 18:32:15 +000061 jsondataarr, jsondataarrerr := json.Marshal(&swagger)
62 if jsondataarrerr != nil {
63 log.Fatalf("Error loading json data from swagger \n: %s", jsondataarrerr)
64 }
aravind.est347f1782023-07-14 09:53:41 +010065
aravind.estbd79b242024-03-07 18:32:15 +000066 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)
69 log.Fatal(err)
70 }
aravind.est347f1782023-07-14 09:53:41 +010071
aravind.estbd79b242024-03-07 18:32:15 +000072 yamldataarr, yamldataarrerr := yaml.Marshal(&data)
73 if yamldataarrerr != nil {
74 log.Fatalf("Error loading json data map to array \n: %s", yamldataarrerr)
75 }
aravind.est347f1782023-07-14 09:53:41 +010076
aravind.estbd79b242024-03-07 18:32:15 +000077 err2 := ioutil.WriteFile(filename, yamldataarr, 0755)
78 if err2 != nil {
79 log.Fatalf("Error writing provider yaml \n: %s", err2)
80 }
81}
82
83func copy(srcFile string, targetFile string) error {
84 data, err := os.ReadFile(srcFile)
85 if err != nil {
86 return err
87 }
88 err = os.WriteFile(targetFile, data, 0644)
89 if err != nil {
90 return err
91 }
92 return nil
93}
94
95func generateAndCopySwagger(openApiFileName string, getSwagger fn) {
96 fmt.Printf("Generating %s...\n", openApiFileName)
97 swagger, err := getSwagger()
98 if err == nil {
99 generateSwaggerYaml(swagger, openApiFileName)
100 fmt.Printf("Copying %s to %s \n", openApiFileName, smeOpenApiFileLocation + openApiFileName)
101 copy(openApiFileName, smeOpenApiFileLocation +openApiFileName)
102 }
103}