/
spec_parser.go
136 lines (118 loc) · 3.55 KB
/
spec_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package controller
import (
"fmt"
"os"
apiv1 "github.com/appnet-org/appnet/api/v1"
"gopkg.in/yaml.v2"
)
type AppManifest struct {
AppName string `yaml:"app_name"`
AppManifestFile string `yaml:"app_manifest"`
AppStructure []string `yaml:"app_structure"`
Edge map[string][]EdgeElementItem `yaml:"edge"`
Link map[string][]PairElementItem `yaml:"link"`
}
type EdgeElementItem struct {
Method string `yaml:"method"`
Name string `yaml:"name"`
Path string `yaml:"path"`
Position string `yaml:"position"`
Proto string `yaml:"proto"`
}
type PairElementItem struct {
Method string `yaml:"method"`
Name1 string `yaml:"name1"`
Name2 string `yaml:"name2"`
Position string `yaml:"position"`
Proto string `yaml:"proto"`
}
func ConvertToAppNetSpec(appName, appManifestFile, clientService, serverService, method, proto, fileName string, clientChain, serverChain, anyChain, pairChain []apiv1.ChainElement) error {
clientServerTag := fmt.Sprintf("%s->%s", clientService, serverService)
appManifest := AppManifest{
AppName: appName,
AppManifestFile: appManifestFile,
AppStructure: []string{
clientServerTag,
},
Edge: make(map[string][]EdgeElementItem),
Link: make(map[string][]PairElementItem),
}
if len(clientChain) > 0 {
for _, element := range clientChain {
appManifest.Edge[clientServerTag] = append(appManifest.Edge[clientServerTag], EdgeElementItem{
Method: method,
Name: element.Name,
Position: "C",
Proto: proto,
Path: element.File,
})
}
}
if len(serverChain) > 0 {
for _, element := range serverChain {
appManifest.Edge[clientServerTag] = append(appManifest.Edge[clientServerTag], EdgeElementItem{
Method: method,
Name: element.Name,
Position: "S",
Proto: proto,
Path: element.File,
})
}
}
if len(anyChain) > 0 {
for _, element := range anyChain {
appManifest.Edge[clientServerTag] = append(appManifest.Edge[clientServerTag], EdgeElementItem{
Method: method,
Name: element.Name,
Position: "C/S",
Proto: proto,
Path: element.File,
})
}
}
// // TODO: Update pairChain parsing logic
// if len(pairChain) > 0 {
// pair_elements := strings.Split(pairChain, "->")
// for _, element := range pair_elements {
// // Modify as needed to specify different elements for Name1 and Name2
// appManifest.Link[clientServerTag] = append(appManifest.Link[clientServerTag], PairElementItem{
// Method: method,
// Name1: removeParentheses(element),
// Name2: removeParentheses(element), // Modify as needed
// Proto: proto,
// })
// }
// }
yamlBytes, err := yaml.Marshal(&appManifest)
if err != nil {
return err
}
file, err := os.Create(fileName)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(yamlBytes)
if err != nil {
return err
}
return nil
}
// func main() {
// clientService := "frontend"
// serverService := "ping"
// name := "ping"
// method := "PingEcho"
// clientChain := "rate()->fault()"
// serverChain := "admission()->loadbalance()"
// anyChain := "metrics()->tracing()"
// pairChain := "encrypt()->decrypt()"
// proto := "examples/proto/ping.proto"
// fileName := "output.yaml"
// err := GenerateAndWriteYAMLToFile(clientService, serverService, clientChain, serverChain, anyChain, pairChain, name, method, proto, fileName)
// if err != nil {
// fmt.Printf("Error: %v\n", err)
// return
// }
// fmt.Printf("YAML written to %s\n", fileName)
// }