Add generated code
[nonrtric/plt/sme.git] / capifcore / internal / gentools / commoncollector / commoncollector.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package main
22
23 import (
24         "bufio"
25         "flag"
26         "io/ioutil"
27         "log"
28         "os"
29         "strings"
30
31         "gopkg.in/yaml.v2"
32 )
33
34 var apiDir *string
35 var common = map[interface{}]interface{}{
36         "openapi": "3.0.0",
37         "info": map[interface{}]interface{}{
38                 "title":   "Common",
39                 "version": "1.0.0",
40         },
41         "components": map[interface{}]interface{}{
42                 "schemas": map[interface{}]interface{}{},
43         },
44 }
45
46 func main() {
47         apiDir = flag.String("apidir", "", "Directory containing API definitions to fix")
48         flag.Parse()
49
50         file, err := os.Open("definitions.txt")
51         if err != nil {
52                 log.Fatalf("Error opening file: %v", err)
53         }
54         defer func(file *os.File) {
55                 _ = file.Close()
56         }(file)
57
58         scanner := bufio.NewScanner(file)
59         components := common["components"]
60         cMap := components.(map[interface{}]interface{})
61         schemas := cMap["schemas"].(map[interface{}]interface{})
62         for scanner.Scan() {
63                 name, data := getDependency(scanner.Text())
64                 if name == "EthFlowDescription" {
65                         changeToLocalReference("fDir", "FlowDirection", data)
66                 }
67                 if name == "ReportingInformation" {
68                         changeToLocalReference("notifMethod", "NotificationMethod", data)
69                 }
70                 if name == "RelativeCartesianLocation" {
71                         properties := data["properties"].(map[interface{}]interface{})
72                         delete(properties, true)
73                         data["required"] = remove(data["required"].([]interface{}), 1)
74                 }
75                 schemas[name] = data
76         }
77
78         if err := scanner.Err(); err != nil {
79                 log.Fatal(err)
80         }
81
82         modCommon, err := yaml.Marshal(common)
83         if err != nil {
84                 log.Fatalf("Marshal: #%v ", err)
85         }
86         err = ioutil.WriteFile(*apiDir+"/"+"CommonData.yaml", modCommon, 0644)
87         if err != nil {
88                 log.Fatalf("Error writing yamlFile. #%v ", err)
89         }
90 }
91
92 func changeToLocalReference(attrname, refName string, data map[interface{}]interface{}) {
93         properties := data["properties"].(map[interface{}]interface{})
94         ref := properties[attrname].(map[interface{}]interface{})
95         ref["$ref"] = "#/components/schemas/" + refName
96 }
97
98 func getDependency(s string) (string, map[interface{}]interface{}) {
99         info := strings.Split(s, "#")
100         yamlFile, err := ioutil.ReadFile(*apiDir + "/" + info[0])
101         if err != nil {
102                 log.Fatalf("Error reading yamlFile. #%v ", err)
103         }
104         m := make(map[string]interface{})
105         err = yaml.Unmarshal(yamlFile, m)
106         if err != nil {
107                 log.Fatalf("Unmarshal: %v", err)
108         }
109         components := m["components"]
110         cMap := components.(map[interface{}]interface{})
111         schemas := cMap["schemas"].(map[interface{}]interface{})
112         component := strings.Split(info[1], "/")
113         dep := schemas[component[3]].(map[interface{}]interface{})
114         return component[3], dep
115 }
116
117 func remove(slice []interface{}, s int) []interface{} {
118         return append(slice[:s], slice[s+1:]...)
119 }