-
Notifications
You must be signed in to change notification settings - Fork 62
/
client.go
435 lines (404 loc) · 12.7 KB
/
client.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright (c) 2017 Arista Networks, Inc.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package client
import (
"context"
"flag"
"fmt"
"os"
"strconv"
"strings"
"sync/atomic"
"time"
aflag "github.com/aristanetworks/goarista/flag"
"github.com/aristanetworks/goarista/gnmi"
"github.com/aristanetworks/glog"
pb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/gnmi/proto/gnmi_ext"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
)
// TODO: Make this more clear
var help = `Usage of gnmi:
gnmi -addr [<VRF-NAME>/]ADDRESS:PORT [options...]
capabilities
get ((origin=ORIGIN) (target=TARGET) PATH+)+
subscribe ((origin=ORIGIN) (target=TARGET) PATH+)+
((update|replace (origin=ORIGIN) (target=TARGET) PATH JSON|FILE) |
(delete (origin=ORIGIN) (target=TARGET) PATH))+
`
type reqParams struct {
origin string
target string
paths []string
}
func usageAndExit(s string) {
flag.Usage()
if s != "" {
fmt.Fprintln(os.Stderr, s)
}
os.Exit(1)
}
// Main initializes the gNMI client.
func Main() {
cfg := &gnmi.Config{}
flag.StringVar(&cfg.Addr, "addr", "", "Address of gNMI gRPC server with optional VRF name")
flag.StringVar(&cfg.CAFile, "cafile", "", "Path to server TLS certificate file")
flag.StringVar(&cfg.CertFile, "certfile", "", "Path to client TLS certificate file")
flag.StringVar(&cfg.KeyFile, "keyfile", "", "Path to client TLS private key file")
flag.StringVar(&cfg.Password, "password", "", "Password to authenticate with")
flag.StringVar(&cfg.Username, "username", "", "Username to authenticate with")
flag.StringVar(&cfg.Compression, "compression", "", "Compression method. "+
`Supported options: "" and "gzip"`)
flag.BoolVar(&cfg.TLS, "tls", false, "Enable TLS")
flag.BoolVar(&cfg.BDP, "bdp", true,
"Enable Bandwidth Delay Product (BDP) estimation and dynamic flow control window")
subscribeOptions := &gnmi.SubscribeOptions{}
flag.StringVar(&subscribeOptions.Prefix, "prefix", "", "Subscribe prefix path")
flag.BoolVar(&subscribeOptions.UpdatesOnly, "updates_only", false,
"Subscribe to updates only (false | true)")
flag.StringVar(&subscribeOptions.Mode, "mode", "stream",
"Subscribe mode (stream | once | poll)")
flag.StringVar(&subscribeOptions.StreamMode, "stream_mode", "target_defined",
"Subscribe stream mode, only applies for stream subscriptions "+
"(target_defined | on_change | sample)")
sampleIntervalStr := flag.String("sample_interval", "0", "Subscribe sample interval, "+
"only applies for sample subscriptions (400ms, 2.5s, 1m, etc.)")
heartbeatIntervalStr := flag.String("heartbeat_interval", "0", "Subscribe heartbeat "+
"interval, only applies for on-change subscriptions (400ms, 2.5s, 1m, etc.)")
arbitrationStr := flag.String("arbitration", "", "master arbitration identifier "+
"([<role_id>:]<election_id>)")
historyStartStr := flag.String("history_start", "", "Historical data subscription "+
"start time (nanoseconds since Unix epoch or RFC3339 format with nanosecond "+
"precision, e.g., 2006-01-02T15:04:05.999999999+07:00)")
historyEndStr := flag.String("history_end", "", "Historical data subscription "+
"end time (nanoseconds since Unix epoch or RFC3339 format with nanosecond "+
"precision, e.g., 2006-01-02T15:04:05.999999999+07:00)")
historySnapshotStr := flag.String("history_snapshot", "", "Historical data subscription "+
"snapshot time (nanoseconds since Unix epoch or RFC3339 format with nanosecond "+
"precision, e.g., 2006-01-02T15:04:05.999999999+07:00)")
dataTypeStr := flag.String("data_type", "all",
"Get data type (all | config | state | operational)")
flag.StringVar(&cfg.Token, "token", "", "Authentication token")
grpcMetadata := aflag.Map{}
flag.Var(grpcMetadata, "grpcmetadata",
"key=value gRPC metadata fields, can be used repeatedly")
debug := flag.String("debug", "", "Enable a debug mode:\n"+
" 'proto' : print SubscribeResponses in protobuf text format\n"+
" 'latency' : print timing numbers to help debug latency\n"+
" 'throughput' : print number of notifications sent in a second\n"+
" 'clog' : start a subscribe and then don't read any of the responses")
flag.Usage = func() {
fmt.Fprintln(os.Stderr, help)
flag.PrintDefaults()
}
flag.Parse()
if cfg.Addr == "" {
usageAndExit("error: address not specified")
}
cfg.GRPCMetadata = grpcMetadata
var sampleInterval, heartbeatInterval time.Duration
var err error
if sampleInterval, err = time.ParseDuration(*sampleIntervalStr); err != nil {
usageAndExit(fmt.Sprintf("error: sample interval (%s) invalid", *sampleIntervalStr))
}
subscribeOptions.SampleInterval = uint64(sampleInterval)
if heartbeatInterval, err = time.ParseDuration(*heartbeatIntervalStr); err != nil {
usageAndExit(fmt.Sprintf("error: heartbeat interval (%s) invalid", *heartbeatIntervalStr))
}
subscribeOptions.HeartbeatInterval = uint64(heartbeatInterval)
var histExt *gnmi_ext.Extension_History
if *historyStartStr != "" || *historyEndStr != "" || *historySnapshotStr != "" {
if *historySnapshotStr != "" {
if *historyStartStr != "" || *historyEndStr != "" {
usageAndExit("error: specified history start/end and snapshot time")
}
t, err := parseTime(*historySnapshotStr)
if err != nil {
usageAndExit(fmt.Sprintf("error: invalid snapshot time (%s): %s",
*historySnapshotStr, err))
}
histExt = gnmi.HistorySnapshotExtension(t.UnixNano())
} else {
var s, e int64
if *historyStartStr != "" {
st, err := parseTime(*historyStartStr)
if err != nil {
usageAndExit(fmt.Sprintf("error: invalid start time (%s): %s",
*historyStartStr, err))
}
s = st.UnixNano()
}
if *historyEndStr != "" {
et, err := parseTime(*historyEndStr)
if err != nil {
usageAndExit(fmt.Sprintf("error: invalid end time (%s): %s",
*historyEndStr, err))
}
e = et.UnixNano()
}
histExt = gnmi.HistoryRangeExtension(s, e)
}
}
args := flag.Args()
ctx := gnmi.NewContext(context.Background(), cfg)
client, err := gnmi.Dial(cfg)
if err != nil {
glog.Fatal(err)
}
var setOps []*gnmi.Operation
for i := 0; i < len(args); i++ {
op := args[i]
switch op {
case "capabilities":
if len(setOps) != 0 {
usageAndExit("error: 'capabilities' not allowed after 'merge|replace|delete'")
}
err := gnmi.Capabilities(ctx, client)
if err != nil {
glog.Fatal(err)
}
return
case "get":
if len(setOps) != 0 {
usageAndExit("error: 'get' not allowed after 'merge|replace|delete'")
}
pathParams, _ := parsereqParams(args[1:], false)
for _, pathParam := range pathParams {
origin := pathParam.origin
target := pathParam.target
paths := pathParam.paths
req, err := gnmi.NewGetRequest(gnmi.SplitPaths(paths), origin)
if err != nil {
glog.Fatal(err)
}
if target != "" {
if req.Prefix == nil {
req.Prefix = &pb.Path{}
}
req.Prefix.Target = target
}
switch strings.ToLower(*dataTypeStr) {
case "", "all":
req.Type = pb.GetRequest_ALL
case "config":
req.Type = pb.GetRequest_CONFIG
case "state":
req.Type = pb.GetRequest_STATE
case "operational":
req.Type = pb.GetRequest_OPERATIONAL
default:
usageAndExit(fmt.Sprintf("error: invalid data type (%s)", *dataTypeStr))
}
err = gnmi.GetWithRequest(ctx, client, req)
if err != nil {
glog.Fatal(err)
}
}
return
case "subscribe":
if len(setOps) != 0 {
usageAndExit("error: 'subscribe' not allowed after 'merge|replace|delete'")
}
var g errgroup.Group
pathParams, _ := parsereqParams(args[1:], false)
for _, pathParam := range pathParams {
origin := pathParam.origin
target := pathParam.target
paths := pathParam.paths
respChan := make(chan *pb.SubscribeResponse)
subOptions := new(gnmi.SubscribeOptions)
*subOptions = *subscribeOptions
subOptions.Origin = origin
subOptions.Target = target
subOptions.Paths = gnmi.SplitPaths(paths)
if histExt != nil {
subOptions.Extensions = []*gnmi_ext.Extension{{
Ext: histExt,
}}
}
g.Go(func() error {
return gnmi.SubscribeErr(ctx, client, subOptions, respChan)
})
switch *debug {
case "proto":
for resp := range respChan {
fmt.Println(resp)
}
case "latency":
for resp := range respChan {
printLatencyStats(resp)
}
case "throughput":
handleThroughput(respChan)
case "clog":
// Don't read any subscription updates
g.Wait()
case "":
go processSubscribeResponses(origin, respChan)
default:
usageAndExit(fmt.Sprintf("unknown debug option: %q", *debug))
}
}
if err := g.Wait(); err != nil {
glog.Fatal(err)
}
return
case "update", "replace", "delete":
// ok if no args, if arbitration was specified
if len(args) == i+1 && *arbitrationStr == "" {
usageAndExit("error: missing path")
}
op := &gnmi.Operation{
Type: args[i],
}
i++
if len(args) <= i {
break
}
pathParams, argsParsed := parsereqParams(args[i:], true)
i += argsParsed
op.Path = gnmi.SplitPath(pathParams[0].paths[0])
op.Origin = pathParams[0].origin
op.Target = pathParams[0].target
if op.Type != "delete" {
if len(args) == i {
usageAndExit("error: missing JSON or FILEPATH to data")
}
op.Val = args[i]
}
setOps = append(setOps, op)
default:
usageAndExit(fmt.Sprintf("error: unknown operation %q", args[i]))
}
}
arb, err := gnmi.ArbitrationExt(*arbitrationStr)
if err != nil {
glog.Fatal(err)
}
var exts []*gnmi_ext.Extension
if arb != nil {
exts = append(exts, arb)
}
err = gnmi.Set(ctx, client, setOps, exts...)
if err != nil {
glog.Fatal(err)
}
}
func processSubscribeResponses(origin string, respChan chan *pb.SubscribeResponse) {
for resp := range respChan {
if err := gnmi.LogSubscribeResponse(resp); err != nil {
glog.Fatal(err)
}
}
}
// Parse string timestamp, first trying for ns since epoch, and then
// for RFC3339.
func parseTime(ts string) (time.Time, error) {
if ti, err := strconv.ParseInt(ts, 10, 64); err == nil {
return time.Unix(0, ti), nil
}
return time.Parse(time.RFC3339Nano, ts)
}
func parseStringOpt(s, prefix string) (string, bool) {
if strings.HasPrefix(s, prefix+"=") {
return strings.TrimPrefix(s, prefix+"="), true
}
return "", false
}
func parseOrigin(s string) (string, bool) {
return parseStringOpt(s, "origin")
}
func parseTarget(s string) (string, bool) {
return parseStringOpt(s, "target")
}
func parsereqParams(args []string, maxOnePath bool) (pathParams []reqParams,
argsParsed int) {
var pP *reqParams = nil
//
for _, arg := range args {
argsParsed++
if o, ok := parseOrigin(arg); ok {
// Either this is the first Origin-Target-Path or a subsequent one
if pP == nil {
pP = new(reqParams)
} else {
// Subsequent one, save the last set
pathParams = append(pathParams, *pP)
pP = new(reqParams)
}
pP.origin = o
} else if t, ok := parseTarget(arg); ok { // if this is target , assume origin has been set
if pP == nil {
pP = new(reqParams)
}
pP.target = t
} else {
// Its the path, origin may or may not be set
if pP == nil {
pP = new(reqParams)
}
pP.paths = append(pP.paths, arg)
if maxOnePath {
pathParams = append(pathParams, *pP)
return
}
}
}
pathParams = append(pathParams, *pP)
return
}
func printLatencyStats(s *pb.SubscribeResponse) {
switch resp := s.Response.(type) {
case *pb.SubscribeResponse_SyncResponse:
fmt.Printf("now=%d sync_response=%t\n",
time.Now().UnixNano(), resp.SyncResponse)
case *pb.SubscribeResponse_Update:
notif := resp.Update
now := time.Now().UnixNano()
fmt.Printf("now=%d timestamp=%d latency=%s prefix=%s size=%d updates=%d deletes=%d\n",
now,
notif.Timestamp,
time.Duration(now-notif.Timestamp),
gnmi.StrPath(notif.Prefix),
proto.Size(s),
len(notif.Update),
len(notif.Delete),
)
}
}
func handleThroughput(respChan <-chan *pb.SubscribeResponse) {
var notifs uint64
var updates uint64
go func() {
var (
lastNotifs uint64
lastUpdates uint64
lastTime = time.Now()
)
ticker := time.NewTicker(10 * time.Second)
for t := range ticker.C {
newNotifs := atomic.LoadUint64(¬ifs)
newUpdates := atomic.LoadUint64(&updates)
dNotifs := newNotifs - lastNotifs
dUpdates := newUpdates - lastUpdates
since := t.Sub(lastTime).Seconds()
lastNotifs = newNotifs
lastUpdates = newUpdates
lastTime = t
fmt.Printf("%s: %f notifs/s %f updates/s\n",
t, float64(dNotifs)/since, float64(dUpdates)/since)
}
}()
for resp := range respChan {
r, ok := resp.Response.(*pb.SubscribeResponse_Update)
if !ok {
continue
}
notif := r.Update
atomic.AddUint64(¬ifs, 1)
atomic.AddUint64(&updates, uint64(len(notif.Update)+len(notif.Delete)))
}
return
}