2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
35 var common = map[interface{}]interface{}{
37 "info": map[interface{}]interface{}{
41 "components": map[interface{}]interface{}{
42 "schemas": map[interface{}]interface{}{},
47 apiDir = flag.String("apidir", "", "Directory containing API definitions to fix")
50 file, err := os.Open("definitions.txt")
52 log.Fatalf("Error opening file: %v", err)
54 defer func(file *os.File) {
58 scanner := bufio.NewScanner(file)
59 components := common["components"]
60 cMap := components.(map[interface{}]interface{})
61 schemas := cMap["schemas"].(map[interface{}]interface{})
63 name, data := getDependency(scanner.Text())
64 if name == "EthFlowDescription" {
65 changeToLocalReference("fDir", "FlowDirection", data)
67 if name == "ReportingInformation" {
68 changeToLocalReference("notifMethod", "NotificationMethod", data)
70 if name == "RelativeCartesianLocation" {
71 properties := data["properties"].(map[interface{}]interface{})
72 delete(properties, true)
73 data["required"] = remove(data["required"].([]interface{}), 1)
78 if err := scanner.Err(); err != nil {
82 modCommon, err := yaml.Marshal(common)
84 log.Fatalf("Marshal: #%v ", err)
86 err = ioutil.WriteFile(*apiDir+"/"+"CommonData.yaml", modCommon, 0644)
88 log.Fatalf("Error writing yamlFile. #%v ", err)
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
98 func getDependency(s string) (string, map[interface{}]interface{}) {
99 info := strings.Split(s, "#")
100 yamlFile, err := ioutil.ReadFile(*apiDir + "/" + info[0])
102 log.Fatalf("Error reading yamlFile. #%v ", err)
104 m := make(map[string]interface{})
105 err = yaml.Unmarshal(yamlFile, m)
107 log.Fatalf("Unmarshal: %v", err)
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
117 func remove(slice []interface{}, s int) []interface{} {
118 return append(slice[:s], slice[s+1:]...)