-
Notifications
You must be signed in to change notification settings - Fork 52
/
subscribe.go
400 lines (369 loc) · 12.3 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// © 2022 Nokia.
//
// This code is a Contribution to the gNMIc project (“Work”) made under the Google Software Grant and Corporate Contributor License Agreement (“CLA”) and governed by the Apache License 2.0.
// No other rights or licenses in or to any of Nokia’s intellectual property are granted for any other purpose.
// This code is provided on an “as is” basis without any warranties of any kind.
//
// SPDX-License-Identifier: Apache-2.0
package app
import (
"context"
"errors"
"fmt"
"io"
"sort"
"strings"
"time"
"github.com/manifoldco/promptui"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/grpctunnel/tunnel"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/openconfig/gnmic/pkg/api/types"
"github.com/openconfig/gnmic/pkg/config"
"github.com/openconfig/gnmic/pkg/formatters"
)
const (
initLockerRetryTimer = 1 * time.Second
)
func (a *App) SubscribePreRunE(cmd *cobra.Command, args []string) error {
a.Config.SetLocalFlagsFromFile(cmd)
err := a.initPluginManager()
if err != nil {
return err
}
a.createCollectorDialOpts()
return nil
}
func (a *App) SubscribeRunE(cmd *cobra.Command, args []string) error {
defer a.InitSubscribeFlags(cmd)
// prompt mode
if a.PromptMode {
return a.SubscribeRunPrompt(cmd, args)
}
//
subCfg, err := a.Config.GetSubscriptions(cmd)
if err != nil {
return fmt.Errorf("failed reading subscriptions config: %v", err)
}
err = a.readConfigs()
if err != nil {
return err
}
err = a.Config.GetClustering()
if err != nil {
return err
}
err = a.Config.GetGNMIServer()
if err != nil {
return err
}
err = a.Config.GetAPIServer()
if err != nil {
return err
}
err = a.Config.GetLoader()
if err != nil {
return err
}
numInputs := len(a.Config.Inputs)
if len(subCfg) == 0 && numInputs == 0 {
return errors.New("no subscriptions or inputs configuration found")
}
// only once mode subscriptions requested
if allSubscriptionsModeOnce(subCfg) {
return a.SubscribeRunONCE(cmd, args)
}
// only poll mode subscriptions requested
if allSubscriptionsModePoll(subCfg) {
return a.SubscribeRunPoll(cmd, args)
}
// stream subscriptions
err = a.initTunnelServer(tunnel.ServerConfig{
AddTargetHandler: a.tunServerAddTargetSubscribeHandler,
DeleteTargetHandler: a.tunServerDeleteTargetHandler,
RegisterHandler: a.tunServerRegisterHandler,
Handler: a.tunServerHandler,
})
if err != nil {
return err
}
_, err = a.Config.GetTargets()
if errors.Is(err, config.ErrNoTargetsFound) {
if !a.Config.LocalFlags.SubscribeWatchConfig &&
len(a.Config.FileConfig.GetStringMap("loader")) == 0 &&
!a.Config.UseTunnelServer &&
numInputs == 0 {
return fmt.Errorf("failed reading targets config: %v", err)
}
} else if err != nil {
return fmt.Errorf("failed reading targets config: %v", err)
}
//
for {
err := a.InitLocker()
if err != nil {
a.Logger.Printf("failed to init locker: %v", err)
time.Sleep(initLockerRetryTimer)
continue
}
break
}
a.startAPIServer()
a.startGnmiServer()
go a.startCluster()
a.startIO()
if a.Config.LocalFlags.SubscribeWatchConfig {
go a.watchConfig()
}
for range a.ctx.Done() {
return a.ctx.Err()
}
return nil
}
func (a *App) subscribeStream(ctx context.Context, tc *types.TargetConfig) {
defer a.wg.Done()
a.TargetSubscribeStream(ctx, tc)
}
func (a *App) subscribeOnce(ctx context.Context, tc *types.TargetConfig) {
defer a.wg.Done()
err := a.TargetSubscribeOnce(ctx, tc)
if err != nil {
a.logError(err)
}
}
func (a *App) subscribePoll(ctx context.Context, tc *types.TargetConfig) {
defer a.wg.Done()
a.TargetSubscribePoll(ctx, tc)
}
// InitSubscribeFlags used to init or reset subscribeCmd flags for gnmic-prompt mode
func (a *App) InitSubscribeFlags(cmd *cobra.Command) {
cmd.ResetFlags()
cmd.Flags().StringVarP(&a.Config.LocalFlags.SubscribePrefix, "prefix", "", "", "subscribe request prefix")
cmd.Flags().StringArrayVarP(&a.Config.LocalFlags.SubscribePath, "path", "", []string{}, "subscribe request paths")
//cmd.MarkFlagRequired("path")
cmd.Flags().Uint32VarP(&a.Config.LocalFlags.SubscribeQos, "qos", "q", 0, "qos marking")
cmd.Flags().BoolVarP(&a.Config.LocalFlags.SubscribeUpdatesOnly, "updates-only", "", false, "only updates to current state should be sent")
cmd.Flags().StringVarP(&a.Config.LocalFlags.SubscribeMode, "mode", "", "stream", "one of: once, stream, poll")
cmd.Flags().StringVarP(&a.Config.LocalFlags.SubscribeStreamMode, "stream-mode", "", "target-defined", "one of: on-change, sample, target-defined")
cmd.Flags().DurationVarP(&a.Config.LocalFlags.SubscribeSampleInterval, "sample-interval", "i", 0,
"sample interval as a decimal number and a suffix unit, such as \"10s\" or \"1m30s\"")
cmd.Flags().BoolVarP(&a.Config.LocalFlags.SubscribeSuppressRedundant, "suppress-redundant", "", false, "suppress redundant update if the subscribed value didn't not change")
cmd.Flags().DurationVarP(&a.Config.LocalFlags.SubscribeHeartbeatInterval, "heartbeat-interval", "", 0, "heartbeat interval in case suppress-redundant is enabled")
cmd.Flags().StringSliceVarP(&a.Config.LocalFlags.SubscribeModel, "model", "", []string{}, "subscribe request used model(s)")
cmd.Flags().BoolVar(&a.Config.LocalFlags.SubscribeQuiet, "quiet", false, "suppress stdout printing")
cmd.Flags().StringVarP(&a.Config.LocalFlags.SubscribeTarget, "target", "", "", "subscribe request target")
cmd.Flags().BoolVarP(&a.Config.LocalFlags.SubscribeSetTarget, "set-target", "", false, "set target name in gNMI Path prefix")
cmd.Flags().StringSliceVarP(&a.Config.LocalFlags.SubscribeName, "name", "n", []string{}, "reference subscriptions by name, must be defined in gnmic config file")
cmd.Flags().StringSliceVarP(&a.Config.LocalFlags.SubscribeOutput, "output", "", []string{}, "reference to output groups by name, must be defined in gnmic config file")
cmd.Flags().BoolVarP(&a.Config.LocalFlags.SubscribeWatchConfig, "watch-config", "", false, "watch configuration changes, add or delete subscribe targets accordingly")
cmd.Flags().DurationVarP(&a.Config.LocalFlags.SubscribeBackoff, "backoff", "", 0, "backoff time between subscribe requests")
cmd.Flags().DurationVarP(&a.Config.LocalFlags.SubscribeLockRetry, "lock-retry", "", 5*time.Second, "time to wait between target lock attempts")
cmd.Flags().StringVarP(&a.Config.LocalFlags.SubscribeHistorySnapshot, "history-snapshot", "", "", "sets the snapshot time in a historical subscription, nanoseconds since Unix epoch or RFC3339 format")
cmd.Flags().StringVarP(&a.Config.LocalFlags.SubscribeHistoryStart, "history-start", "", "", "sets the start time in a historical range subscription, nanoseconds since Unix epoch or RFC3339 format")
cmd.Flags().StringVarP(&a.Config.LocalFlags.SubscribeHistoryEnd, "history-end", "", "", "sets the end time in a historical range subscription, nanoseconds since Unix epoch or RFC3339 format")
//
cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) {
a.Config.FileConfig.BindPFlag(fmt.Sprintf("%s-%s", cmd.Name(), flag.Name), flag)
})
}
func (a *App) readConfigs() error {
var err error
_, err = a.Config.GetOutputs()
if err != nil {
return fmt.Errorf("failed reading outputs config: %v", err)
}
_, err = a.Config.GetInputs()
if err != nil {
return fmt.Errorf("failed reading inputs config: %v", err)
}
_, err = a.Config.GetActions()
if err != nil {
return fmt.Errorf("failed reading actions config: %v", err)
}
_, err = a.Config.GetEventProcessors()
if err != nil {
return fmt.Errorf("failed reading event processors config: %v", err)
}
_, err = a.LoadProtoFiles()
if err != nil {
return fmt.Errorf("failed loading proto files: %v", err)
}
return nil
}
func (a *App) handlePolledSubscriptions() error {
polledTargetsSubscriptions := a.polledSubscriptionsTargets()
if len(polledTargetsSubscriptions) == 0 {
return nil
}
mo := &formatters.MarshalOptions{
Multiline: true,
Indent: " ",
Format: a.Config.Format,
CalculateLatency: a.Config.GlobalFlags.CalculateLatency,
}
// handle initial responses if updates-only is not set
if !a.Config.SubscribeUpdatesOnly {
for targetName := range polledTargetsSubscriptions {
a.operLock.RLock()
t, ok := a.Targets[targetName]
a.operLock.RUnlock()
if !ok {
return fmt.Errorf("unknown target name %q", targetName)
}
rspCh, errCh := t.ReadSubscriptions()
SUBS:
for {
select {
case rsp := <-rspCh:
b, err := mo.Marshal(rsp.Response, nil)
if err != nil {
return fmt.Errorf("target '%s', subscription '%s': poll response formatting error: %v", targetName, rsp.SubscriptionName, err)
}
fmt.Println(string(b))
switch rsp := rsp.Response.Response.(type) {
case *gnmi.SubscribeResponse_SyncResponse:
fmt.Printf("received sync response '%t' from '%s'\n", rsp.SyncResponse, targetName)
break SUBS // current target done sending initial updates
}
case tErr := <-errCh:
if tErr.Err != nil {
return fmt.Errorf("target '%s', subscription '%s': poll response error: %v", targetName, tErr.SubscriptionName, tErr.Err)
}
case <-a.ctx.Done():
return a.ctx.Err()
}
}
}
}
pollTargets := make([]string, 0, len(polledTargetsSubscriptions))
for t := range polledTargetsSubscriptions {
pollTargets = append(pollTargets, t)
}
sort.Slice(pollTargets, func(i, j int) bool {
return pollTargets[i] < pollTargets[j]
})
s := promptui.Select{
Label: "select target to poll",
Items: pollTargets,
HideSelected: true,
}
waitChan := make(chan struct{}, 1)
waitChan <- struct{}{}
OUTER:
for {
select {
case <-waitChan:
_, name, err := s.Run()
if err != nil {
fmt.Printf("failed selecting target to poll: %v\n", err)
continue
}
ss := promptui.Select{
Label: "select subscription to poll",
Items: polledTargetsSubscriptions[name],
HideSelected: true,
}
_, subName, err := ss.Run()
if err != nil {
fmt.Printf("failed selecting subscription to poll: %v\n", err)
continue
}
err = a.clientSubscribePoll(a.Context(), name, subName)
if err != nil && err != io.EOF {
fmt.Printf("target '%s', subscription '%s': poll response error:%v\n", name, subName, err)
continue
}
a.operLock.RLock()
t, ok := a.Targets[name]
a.operLock.RUnlock()
if !ok {
return fmt.Errorf("unknown target name %q", name)
}
rspCh, errCh := t.ReadSubscriptions()
for {
select {
case <-a.Context().Done():
return a.Context().Err()
case tErr := <-errCh:
if tErr.Err != nil {
fmt.Printf("received error from target '%s': %v\n", name, err)
waitChan <- struct{}{}
continue OUTER
}
case rsp, ok := <-rspCh:
if !ok {
waitChan <- struct{}{}
continue OUTER
}
if rsp == nil {
fmt.Printf("received empty response from target '%s'\n", name)
continue
}
switch rsp := rsp.Response.Response.(type) {
case *gnmi.SubscribeResponse_SyncResponse:
fmt.Printf("received sync response '%t' from '%s'\n", rsp.SyncResponse, name)
waitChan <- struct{}{}
continue OUTER
}
b, err := mo.Marshal(rsp.Response, nil)
if err != nil {
fmt.Printf("target '%s', subscription '%s': poll response formatting error:%v\n", name, subName, err)
fmt.Println(rsp.Response)
continue
}
fmt.Println(string(b))
}
}
case <-a.ctx.Done():
return a.Context().Err()
}
}
}
func (a *App) startIO() {
go a.StartCollector(a.ctx)
a.InitOutputs(a.ctx)
a.InitInputs(a.ctx)
if !a.inCluster() {
go a.startLoader(a.ctx)
var limiter *time.Ticker
if a.Config.LocalFlags.SubscribeBackoff > 0 {
limiter = time.NewTicker(a.Config.LocalFlags.SubscribeBackoff)
}
if !a.Config.UseTunnelServer {
for _, tc := range a.Config.Targets {
a.wg.Add(1)
go a.subscribeStream(a.ctx, tc)
if limiter != nil {
<-limiter.C
}
}
}
if limiter != nil {
limiter.Stop()
}
a.wg.Wait()
}
}
func allSubscriptionsModeOnce(subs map[string]*types.SubscriptionConfig) bool {
if len(subs) == 0 {
return false
}
for _, sub := range subs {
if strings.ToUpper(sub.Mode) != "ONCE" {
return false
}
}
return true
}
func allSubscriptionsModePoll(subs map[string]*types.SubscriptionConfig) bool {
if len(subs) == 0 {
return false
}
for _, sub := range subs {
if strings.ToUpper(sub.Mode) != "POLL" {
return false
}
}
return true
}