-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathwrappers.tmpl
More file actions
70 lines (63 loc) · 2.4 KB
/
wrappers.tmpl
File metadata and controls
70 lines (63 loc) · 2.4 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
// SPDX-FileCopyrightText: 2022-present Intel Corporation
// SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
//
// SPDX-License-Identifier: Apache-2.0
const authorization = "Authorization"
// Implement the Server Interface for access to gNMI
var log = logging.GetLogger("model_0_0_0")
// ServerImpl -
type ServerImpl struct {
GnmiClient southbound.GnmiClient
GnmiTimeout time.Duration
}
{{- range .}}
{{- $opid := .OperationId}}
{{- $path := .Path}}
{{- $paramNames := genParamNames .PathParams}}
// {{$opid}} impl of gNMI access at {{$path}}
func (i *ServerImpl) {{.OperationId}} (ctx echo.Context{{genParamArgs .PathParams}}{{if .RequiresParamObject}}, params {{.OperationId}}Params{{end}}) error {
var response interface{}
var err error
gnmiCtx, cancel := utils.NewGnmiContext(ctx, i.GnmiTimeout)
defer cancel()
{{- range $resp := .Spec.Responses}}
// Response {{$resp.Value.Description}}
{{- $fourchars := slice $opid 0 4}}
{{- if eq $fourchars "Dele"}}
extension100, err := i.Gnmi{{$opid}}(gnmiCtx, "{{$path}}"{{$paramNames}})
if err == nil {
log.Infof("Delete succeded %s", *extension100)
return ctx.JSON(http.StatusOK, extension100)
}
{{- else if eq $fourchars "Post"}}
body, err := utils.ReadRequestBody(ctx.Request().Body)
if err != nil {
return err
}
extension100, err := i.Gnmi{{$opid}}(gnmiCtx, body, "{{$path}}"{{$paramNames}})
if err == nil {
log.Infof("Post succeded %s", *extension100)
return ctx.JSON(http.StatusCreated, extension100)
}
{{- else}} {{/* GET */}}
{{- $expand200 := printf "%200s" $opid}}
{{- $last13 := slice $expand200 187}}
{{- $isLeafSelection := eq "LeafSelection" $last13}}
{{- if eq "ValuesLeafref" $last13 | or $isLeafSelection}}
err = fmt.Errorf("{{$opid}} not implemented. %T", gnmiCtx)
{{- else}}
response, err = i.Gnmi{{$opid}}(gnmiCtx, "{{$path}}"{{$paramNames}})
{{- end}}
{{- end}}
if err != nil {
return utils.ConvertGrpcError(err)
}
// It's not enough to check if response==nil - see https://medium.com/@glucn/golang-an-interface-holding-a-nil-value-is-not-nil-bb151f472cc7
if (reflect.ValueOf(response).Kind() == reflect.Ptr && reflect.ValueOf(response).IsNil()) {
return ctx.NoContent(http.StatusNotFound)
}
{{- end}}
log.Infof("{{.OperationId}}")
return ctx.JSON(http.StatusOK, response)
}
{{- end}}