-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtypedef.tmpl
More file actions
100 lines (89 loc) · 4.24 KB
/
typedef.tmpl
File metadata and controls
100 lines (89 loc) · 4.24 KB
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
// SPDX-FileCopyrightText: 2022-present Intel Corporation
// SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
//
// SPDX-License-Identifier: Apache-2.0
{{range .Types}}{{$elevenchars := .TypeName}}{{if len .TypeName | lt 11 }}{{$elevenchars = slice .TypeName 0 11}}{{end}}
{{if printf "RequestBody" | eq $elevenchars}}//Ignoring {{.TypeName}}
{{else if printf "AdditionalP" | eq $elevenchars}}//Ignoring {{.TypeName}}
{{else if printf "EnterpriseI" | eq $elevenchars}}//Ignoring {{.TypeName}}
{{else if printf "LeafRefOpti" | eq $elevenchars}}//Ignoring {{.TypeName}}
{{else}}
// {{printf "GnmiDelete%s" .TypeName}} deletes an instance of {{.JsonName}}.
func (i *ServerImpl) {{printf "GnmiDelete%s" .TypeName}}(ctx context.Context,
openApiPath string, enterpriseId externalRef1.EnterpriseId, args ...string) (*string, error) {
// check to see if the item exists before deleting it
response, err := i.{{printf "GnmiGet%s" .TypeName}}(ctx, openApiPath, enterpriseId, args...)
if reflect.ValueOf(response).Kind() == reflect.Ptr && reflect.ValueOf(response).IsNil() {
log.Infof("Item at path %s with args %v not found", openApiPath, args)
return nil, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("item at path %s with args %v does not exists", openApiPath, args))
}
gnmiSet, err := utils.NewGnmiSetDeleteRequest(openApiPath, string(enterpriseId), args...)
if err != nil {
return nil, err
}
log.Infof("gnmiSetRequest %s", gnmiSet.String())
gnmiSetResponse, err := i.GnmiClient.Set(ctx, gnmiSet)
if err != nil {
return nil, err
}
return utils.ExtractResponseID(gnmiSetResponse)
}
// {{printf "GnmiGet%s" .TypeName}} returns an instance of {{.JsonName}}.
func (i *ServerImpl) {{printf "GnmiGet%s" .TypeName}}(ctx context.Context,
openApiPath string, enterpriseId externalRef1.EnterpriseId, args ...string) (*externalRef1.{{.TypeName}}, error) {
gnmiGet, err := utils.NewGnmiGetRequest(openApiPath, string(enterpriseId), args...)
if err != nil {
return nil, err
}
log.Infof("gnmiGetRequest %s", gnmiGet.String())
gnmiVal, err := utils.GetResponseUpdate(i.GnmiClient.Get(ctx, gnmiGet))
if err != nil {
return nil, err
}
if gnmiVal == nil {
return nil, nil
}
gnmiJsonVal, ok := gnmiVal.Value.(*gnmi.TypedValue_JsonVal)
if !ok {
return nil, fmt.Errorf("unexpected type of reply from server %v", gnmiVal.Value)
}
log.Debugf("gNMI Json %s", string(gnmiJsonVal.JsonVal))
var gnmiResponse externalRef0.Device
if err = externalRef0.Unmarshal(gnmiJsonVal.JsonVal, &gnmiResponse); err != nil {
return nil, fmt.Errorf("error unmarshalling gnmiResponse %v", err)
}
mpd := ModelPluginDevice{
device: gnmiResponse,
}
return mpd.{{printf "To%s" .TypeName}}(args...)
}
// {{printf "GnmiPost%s" .TypeName}} adds an instance of {{.JsonName}}.
func (i *ServerImpl) {{printf "GnmiPost%s" .TypeName}}(ctx context.Context, body []byte,
openApiPath string, enterpriseId externalRef1.EnterpriseId, args ...string) (*string, error) {
{{ if eq .JsonName "enterpriseId"}}return nil, fmt.Errorf("Not implemented")
{{else}}
jsonObj := new(externalRef1.{{camelCase .JsonName}})
if err := json.Unmarshal(body, jsonObj); err != nil {
return nil, fmt.Errorf("unable to unmarshal JSON as externalRef1.{{.JsonName}} %v", err)
}
gnmiUpdates, err := EncodeToGnmi{{camelCase .JsonName}}(jsonObj, false, false, enterpriseId, "", args...)
if err != nil {
return nil, fmt.Errorf("unable to convert externalRef1.{{.TypeName}} to gNMI %v", err)
}
gnmiSet, err := utils.NewGnmiSetUpdateRequestUpdates(openApiPath, string(enterpriseId), gnmiUpdates, args...)
if err != nil {
return nil, err
}
log.Infof("gnmiSetRequest %s", gnmiSet.String())
gnmiSetResponse, err := i.GnmiClient.Set(ctx, gnmiSet)
if err != nil {
return nil, err
}
return utils.ExtractResponseID(gnmiSetResponse){{end}}
}
{{end}}
{{end}}
type Translator interface {
{{range .Types}}{{$elevenchars := .TypeName}}{{if len .TypeName | lt 11 }}{{$elevenchars = slice .TypeName 0 11}}{{end}}{{if printf "RequestBody" | ne $elevenchars}}
{{printf "to%s" .TypeName}}(args ...string) ({{printf "*externalRef1.%s" .TypeName}}, error){{end}}{{end}}
}