-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.go
188 lines (166 loc) · 4.25 KB
/
subscribe.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package gnmi
import (
"bytes"
"context"
"errors"
"reflect"
"time"
"github.com/freeconf/restconf/device"
"github.com/freeconf/yang/fc"
"github.com/freeconf/yang/node"
pb_gnmi "github.com/openconfig/gnmi/proto/gnmi"
)
type subscriptionSink func(*pb_gnmi.SubscribeResponse) error
type subscriptionManager struct {
subs []reoccurringSubscription
}
type reoccurringSubscription interface {
execute() error
getSampleInterval() time.Duration
}
var errNoSampleInterval = errors.New("no sample interval given")
type subService struct {
subMgr subscriptionManager
}
func (s *subService) subscribe(d device.Device, server pb_gnmi.GNMI_SubscribeServer) error {
for {
req, err := server.Recv()
if err != nil && req != nil {
return err
}
if req != nil {
if err = s.handleSubscribeList(d, server.Context(), req, server.Send); err != nil {
return err
}
}
}
}
// according to gNMI spec, this is for config or metrics only, not YANG notifications!
func (s *subService) handleSubscribeList(d device.Device, ctx context.Context, req *pb_gnmi.SubscribeRequest, sink subscriptionSink) error {
list := req.GetSubscribe()
prefix, err := selectPath(d, list.UseModels, list.Prefix)
if err != nil {
return err
}
for _, subReq := range list.Subscription {
fc.Debug.Printf("new sub mode = %d", list.Mode)
sub := newSubscription(d, prefix, subReq, sink)
// execute once sychronously avoids kicking off threads and runs thru
// sub to validate paths
if err := sub.execute(); err != nil {
return err
}
if list.Mode != pb_gnmi.SubscriptionList_ONCE {
if err := s.subMgr.add(ctx, sub); err != nil {
return err
}
}
}
return nil
}
func (mgr *subscriptionManager) add(ctx context.Context, sub reoccurringSubscription) error {
fc.Debug.Printf("starting ticker with sample rate %s", sub.getSampleInterval())
sample := sub.getSampleInterval()
if sample == 0 {
return errNoSampleInterval
}
mgr.subs = append(mgr.subs, sub)
t := time.NewTicker(sample)
go func() {
defer t.Stop()
for {
select {
case <-t.C:
fc.Debug.Printf("ticker fired")
if err := sub.execute(); err != nil {
// unclear how i should handle this
fc.Err.Printf("cannot get sub %s", err)
}
case <-ctx.Done():
return
}
}
}()
return nil
}
type subscription struct {
device device.Device
prefix *node.Selection
sink subscriptionSink
opts *pb_gnmi.Subscription
previousValue *pb_gnmi.TypedValue
previousTime time.Time
}
func (s *subscription) getHeartbeatInterval() time.Duration {
return time.Duration(s.opts.HeartbeatInterval) * time.Nanosecond
}
func (s *subscription) getSampleInterval() time.Duration {
if s.opts.SampleInterval == 0 {
return s.getHeartbeatInterval()
}
return time.Duration(s.opts.SampleInterval) * time.Nanosecond
}
func newSubscription(d device.Device, prefix *node.Selection, opts *pb_gnmi.Subscription, sink subscriptionSink) *subscription {
return &subscription{
device: d,
prefix: prefix,
opts: opts,
sink: sink,
}
}
func (s *subscription) execute() error {
sel, err := advanceSelection(s.device, s.prefix, s.opts.Path)
fc.Debug.Printf("sub request %s", sel.Path)
if err != nil {
return err
}
val, err := getVal(sel)
if err != nil {
return err
}
now := time.Now()
if s.previousValue != nil {
if now.Sub(s.previousTime) < s.getHeartbeatInterval() {
if s.opts.Mode == pb_gnmi.SubscriptionMode_ON_CHANGE {
if isEqualValues(s.previousValue, val) {
return nil
}
}
}
}
update := &pb_gnmi.Update{
Path: s.opts.Path,
Val: val,
}
resp := &pb_gnmi.SubscribeResponse{
Response: &pb_gnmi.SubscribeResponse_Update{
Update: &pb_gnmi.Notification{
Timestamp: now.UnixNano(),
Update: []*pb_gnmi.Update{update},
},
},
}
s.sink(resp)
s.previousValue = val
s.previousTime = now
return nil
}
func isEqualValues(a, b *pb_gnmi.TypedValue) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
t := reflect.TypeOf(a.Value)
if t != reflect.TypeOf(b.Value) {
return false
}
switch a.Value.(type) {
case *pb_gnmi.TypedValue_JsonVal:
return bytes.Equal(a.GetJsonVal(), b.GetJsonVal())
case *pb_gnmi.TypedValue_JsonIetfVal:
return bytes.Equal(a.GetJsonIetfVal(), b.GetJsonIetfVal())
}
return false
}