aravind.est | 347f178 | 2023-07-14 09:53:41 +0100 | [diff] [blame] | 1 | // - |
| 2 | // ========================LICENSE_START================================= |
| 3 | // O-RAN-SC |
| 4 | // %% |
| 5 | // Copyright (C) 2023: Nordix Foundation |
aravind.est | bd79b24 | 2024-03-07 18:32:15 +0000 | [diff] [blame] | 6 | // Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved. |
aravind.est | 347f178 | 2023-07-14 09:53:41 +0100 | [diff] [blame] | 7 | // %% |
| 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 | |
| 22 | package main |
| 23 | |
aravind.est | 347f178 | 2023-07-14 09:53:41 +0100 | [diff] [blame] | 24 | import ( |
aravind.est | bd79b24 | 2024-03-07 18:32:15 +0000 | [diff] [blame] | 25 | "encoding/json" |
| 26 | "fmt" |
aravind.est | 347f178 | 2023-07-14 09:53:41 +0100 | [diff] [blame] | 27 | "github.com/getkin/kin-openapi/openapi3" |
| 28 | log "github.com/sirupsen/logrus" |
| 29 | "gopkg.in/yaml.v3" |
aravind.est | bd79b24 | 2024-03-07 18:32:15 +0000 | [diff] [blame] | 30 | "io/ioutil" |
| 31 | "os" |
aravind.est | 347f178 | 2023-07-14 09:53:41 +0100 | [diff] [blame] | 32 | |
aravind.est | bd79b24 | 2024-03-07 18:32:15 +0000 | [diff] [blame] | 33 | "oransc.org/nonrtric/capifcore/internal/common" |
| 34 | "oransc.org/nonrtric/capifcore/internal/common29122" |
| 35 | "oransc.org/nonrtric/capifcore/internal/common29571" |
aravind.est | 347f178 | 2023-07-14 09:53:41 +0100 | [diff] [blame] | 36 | "oransc.org/nonrtric/capifcore/internal/invokermanagementapi" |
| 37 | "oransc.org/nonrtric/capifcore/internal/providermanagementapi" |
| 38 | "oransc.org/nonrtric/capifcore/internal/publishserviceapi" |
aravind.est | 347f178 | 2023-07-14 09:53:41 +0100 | [diff] [blame] | 39 | ) |
| 40 | |
aravind.est | bd79b24 | 2024-03-07 18:32:15 +0000 | [diff] [blame] | 41 | type fn func() (swagger *openapi3.T, err error) |
| 42 | |
| 43 | var smeOpenApiFileLocation string = "../../openapi/sme/" |
| 44 | |
aravind.est | 347f178 | 2023-07-14 09:53:41 +0100 | [diff] [blame] | 45 | func main() { |
aravind.est | bd79b24 | 2024-03-07 18:32:15 +0000 | [diff] [blame] | 46 | |
aravind.est | 347f178 | 2023-07-14 09:53:41 +0100 | [diff] [blame] | 47 | var err error |
aravind.est | bd79b24 | 2024-03-07 18:32:15 +0000 | [diff] [blame] | 48 | 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.est | 347f178 | 2023-07-14 09:53:41 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | func generateSwaggerYaml(swagger *openapi3.T, filename string) { |
aravind.est | bd79b24 | 2024-03-07 18:32:15 +0000 | [diff] [blame] | 61 | jsondataarr, jsondataarrerr := json.Marshal(&swagger) |
| 62 | if jsondataarrerr != nil { |
| 63 | log.Fatalf("Error loading json data from swagger \n: %s", jsondataarrerr) |
| 64 | } |
aravind.est | 347f178 | 2023-07-14 09:53:41 +0100 | [diff] [blame] | 65 | |
aravind.est | bd79b24 | 2024-03-07 18:32:15 +0000 | [diff] [blame] | 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) |
| 69 | log.Fatal(err) |
| 70 | } |
aravind.est | 347f178 | 2023-07-14 09:53:41 +0100 | [diff] [blame] | 71 | |
aravind.est | bd79b24 | 2024-03-07 18:32:15 +0000 | [diff] [blame] | 72 | yamldataarr, yamldataarrerr := yaml.Marshal(&data) |
| 73 | if yamldataarrerr != nil { |
| 74 | log.Fatalf("Error loading json data map to array \n: %s", yamldataarrerr) |
| 75 | } |
aravind.est | 347f178 | 2023-07-14 09:53:41 +0100 | [diff] [blame] | 76 | |
aravind.est | bd79b24 | 2024-03-07 18:32:15 +0000 | [diff] [blame] | 77 | err2 := ioutil.WriteFile(filename, yamldataarr, 0755) |
| 78 | if err2 != nil { |
| 79 | log.Fatalf("Error writing provider yaml \n: %s", err2) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func 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 | |
| 95 | func 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 | } |