-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.go
74 lines (66 loc) · 1.46 KB
/
get.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
package gnmi
import (
"context"
"time"
"github.com/freeconf/restconf/device"
"github.com/freeconf/yang/fc"
"github.com/freeconf/yang/meta"
"github.com/freeconf/yang/node"
"github.com/freeconf/yang/nodeutil"
pb_gnmi "github.com/openconfig/gnmi/proto/gnmi"
)
func get(d device.Device, ctx context.Context, req *pb_gnmi.GetRequest) (*pb_gnmi.GetResponse, error) {
now := time.Now().UnixNano()
resp := &pb_gnmi.Notification{
Timestamp: now,
}
prefix, err := selectPath(d, req.UseModels, req.Prefix)
if err != nil {
return nil, err
}
for _, p := range req.Path {
sel, err := advanceSelection(d, prefix, p)
if err != nil {
return nil, err
}
fc.Debug.Printf("get request %s", sel.Path)
if sel != nil {
val, err := getVal(sel)
if err != nil {
return nil, err
}
resp.Update = append(resp.Update, &pb_gnmi.Update{
Path: p,
Val: val,
})
}
}
return &pb_gnmi.GetResponse{
Notification: []*pb_gnmi.Notification{resp},
}, nil
}
func getVal(sel *node.Selection) (*pb_gnmi.TypedValue, error) {
var v *pb_gnmi.TypedValue
if meta.IsLeaf(sel.Path.Meta) {
val, err := sel.Get()
if err != nil {
return nil, err
}
v = &pb_gnmi.TypedValue{
Value: &pb_gnmi.TypedValue_JsonVal{
JsonVal: []byte(val.String()),
},
}
} else {
msg, err := nodeutil.WriteJSON(sel)
if err != nil {
return nil, err
}
v = &pb_gnmi.TypedValue{
Value: &pb_gnmi.TypedValue_JsonVal{
JsonVal: []byte(msg),
},
}
}
return v, nil
}