Newer
Older
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
"path/filepath"
"strings"
spb "code.fbi.h-da.de/danet/api/go/gosdn/southbound"
"github.com/openconfig/ygot/genutil"
"github.com/openconfig/ygot/ygen"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// write takes a ygen.Generatedcode struct and writes the Go code
// snippets contained within it to the io.Writer, w, provided as an argument.
// The output includes a package header which is generated.
func write(code *ygen.GeneratedGoCode, path string, sbiType spb.Type) error {
if err := os.Mkdir(path, 0755); err != nil {
if err.(*fs.PathError).Err.Error() != "file exists" {
return err
}
}
switch sbiType {
case spb.Type_PLUGIN:
return writePlugin(code, path)
case spb.Type_CONTAINERISED:
return writeCsbi(code, path)
default:
return fmt.Errorf("invalid sbi type provided")
}
}
func writeCsbi(code *ygen.GeneratedGoCode, path string) error {
controller := viper.GetString("controller-address")
target := viper.GetString("target-address")
writerViper := viper.New()
writerViper.Set("uuid", path)
writerViper.Set("controller", controller+":55055")
writerViper.Set("target", target)
if err := writerViper.WriteConfigAs(filepath.Join(path, ".csbi.toml")); err != nil {
return err
}
if err := copyFile(path, "csbi.go"); err != nil {
return err
}
if err := copyFile(path, "go.mod"); err != nil {
return err
}
if err := copyFile(path, "go.sum"); err != nil {
return err
}
}
func writePlugin(code *ygen.GeneratedGoCode, path string) error {
err := writeCode(path, code)
if err != nil {
return err
}
return writeGoMod(path)
}
func copyFile(path, filename string) error {
dstFile := filepath.Join(path, filename)
cmd := exec.Command("cp", srcFile, dstFile)
cmd.Stderr = &stderr
err := cmd.Run()
return err
}
log.WithFields(log.Fields{
"source file": srcFile,
"dst file": dstFile,
return nil
}
func writeCode(path string, code *ygen.GeneratedGoCode) error {
code.CommonHeader = strings.TrimSuffix(code.CommonHeader, ")\n")
code.CommonHeader = code.CommonHeader + pluginImportAmendmend
code.Structs = append(code.Structs, pluginStruct)
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
137
138
139
140
141
142
143
144
145
146
147
file := filepath.Join(path, "gostructs.go")
generatedCode := genutil.OpenFile(file)
defer genutil.SyncFile(generatedCode)
// Write the package header to the supplier writer.
fmt.Fprint(generatedCode, code.CommonHeader)
fmt.Fprint(generatedCode, code.OneOffHeader)
// Write the returned Go code out. First the Structs - which is the struct
// definitions for the generated YANG entity, followed by the enumerations.
for _, snippet := range code.Structs {
fmt.Fprintln(generatedCode, snippet)
}
for _, snippet := range code.Enums {
fmt.Fprintln(generatedCode, snippet)
}
// Write the generated enumeration map out.
fmt.Fprintln(generatedCode, code.EnumMap)
// Write the schema out if it was received.
if len(code.JSONSchemaCode) > 0 {
fmt.Fprintln(generatedCode, code.JSONSchemaCode)
}
if len(code.EnumTypeMap) > 0 {
fmt.Fprintln(generatedCode, code.EnumTypeMap)
}
return nil
}
func writeDockerfile(path string, buffer []string) error {
file := filepath.Join(path, "Dockerfile")
dockerfile, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY, 0755)
defer dockerfile.Sync()
if err != nil {
return err
}
for _, line := range buffer {
fmt.Fprintln(dockerfile, line)
}
return nil
}
func writeGoMod(path string) error {
// Read dependencies from JSON file
deps, err := os.ReadFile(filepath.Join("resources", "plugin_deps.json"))
if err != nil {
return err
}
module := GoMod{}
if err := json.Unmarshal(deps, &module); err != nil {
return err
}
// Create go.mod in destination directory and write template
file := filepath.Join(path, "go.mod")
goMod, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
return err
}
defer goMod.Sync()
templater := template.New("goMod").Delims("<<", ">>")
_, err = templater.Parse(pluginGoModTemplate)