-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathxport_gnmi.go
508 lines (430 loc) · 12.3 KB
/
xport_gnmi.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//
// Copyright (c) 2018 Cisco Systems
//
// Author: Steven Barth <[email protected]>
//
//
package main
import (
"encoding/json"
"regexp"
"strconv"
"strings"
"time"
"github.com/golang/protobuf/jsonpb"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
const (
gnmiWaitToRedial = 10
gnmiTimeout = 10
)
type gnmiClient struct {
name string
codec codecGNMI
// Log data into debug log
logData bool
// Credentials
tls bool
tlsPEM string
tlsKey string
tlsHostname string
// Control channel used to control the gNMI client
ctrlChan <-chan *ctrlMsg
// Data channels fed by the server
dataChans []chan<- dataMsg
// Use to signal that client has shutdown.
clientDone chan struct{}
// Setup a consistent log entry to build logging from
logctx *logrus.Entry
// Server name
server string
paths []string
selectors map[string]struct{}
heartbeatInterval float64
// Authentication
auth userPasswordCollector
// Compatibility mode for uint64 marshaling
jsonUInt64Compat bool
}
func (client *gnmiClient) String() string {
return client.server
}
func (client *gnmiClient) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
username, password, err := client.auth.getUP()
return map[string]string{
"username": username,
"password": password,
}, err
}
func (client *gnmiClient) RequireTransportSecurity() bool {
return false
}
func parseGnmiSensorPath(sensorPath string) (string, []*gnmi.PathElem, float64) {
var interval float64 = -1
var err error
sensorParts := strings.SplitN(sensorPath, ":", 2)
origin := ""
path := sensorParts[0]
if len(sensorParts) > 1 {
origin = sensorParts[0]
path = sensorParts[1]
}
if len(path) > 0 && path[0] == '/' {
path = path[1:]
}
pathInterval := strings.SplitN(path, "@", 2)
path = pathInterval[0]
if len(pathInterval) > 1 {
if pathInterval[1] == "change" {
interval = 0
} else {
interval, err = strconv.ParseFloat(pathInterval[1], 64)
if err != nil {
interval = -1
}
}
}
pathElems := make([]*gnmi.PathElem, strings.Count(path, "/")+1)
filterRegex := regexp.MustCompile(`^([^=]+)='([^']+)'$`)
for i, elem := range strings.Split(path, "/") {
pathElems[i] = &gnmi.PathElem{}
for j, entry := range strings.Split(elem, "[") {
if j == 0 {
pathElems[i].Name = entry
} else if j >= 1 {
filter := filterRegex.FindStringSubmatch(entry)
pathElems[i].Key[filter[1]] = filter[2]
}
}
}
return origin, pathElems, interval
}
//
// Sticky loop when we are handling a remote server, involves trying
// to stay connected and pulling streams for all the subscriptions.
func (client *gnmiClient) loop(ctx context.Context) {
var err error
logctx := client.logctx
defer close(client.clientDone)
//
// Prepare dial options (TLS, user/password, timeout...)
dialOptions := []grpc.DialOption{
grpc.WithBlock(),
grpc.WithPerRPCCredentials(client),
grpc.WithTimeout(time.Second * time.Duration(gnmiTimeout)),
}
if client.tls {
var creds credentials.TransportCredentials
if client.tlsPEM != "" {
creds, err = credentials.NewClientTLSFromFile(client.tlsPEM, client.tlsHostname)
if err != nil {
logctx.WithError(err).Error("gnmi: failed to load TLS credentials!")
return
}
} else {
creds = credentials.NewClientTLSFromCert(nil, client.tlsHostname)
}
dialOptions = append(dialOptions, grpc.WithTransportCredentials(creds))
} else {
dialOptions = append(dialOptions, grpc.WithInsecure())
}
conn, err := grpc.DialContext(ctx, client.server, dialOptions...)
if err != nil {
logctx.WithError(err).Error("gnmi: dial failed, retrying")
return
}
defer conn.Close()
logctx.Info("gnmi: Connected")
// Setup client on connection
gnmiClient := gnmi.NewGNMIClient(conn)
subscribeClient, err := gnmiClient.Subscribe(ctx)
if err != nil {
logctx.WithError(err).Error("gnmi: subscription setup failed")
return
}
defer subscribeClient.CloseSend()
var heartbeatInterval uint64
var suppressRedundant bool
if client.heartbeatInterval >= 0 {
suppressRedundant = true
heartbeatInterval = uint64(1000000000 * client.heartbeatInterval)
}
// Create subscription objects
subscriptions := make([]*gnmi.Subscription, len(client.paths))
for i, path := range client.paths {
origin, elems, interval := parseGnmiSensorPath(path)
subscriptions[i] = &gnmi.Subscription{
Path: &gnmi.Path{
Origin: origin,
Elem: elems,
},
SuppressRedundant: suppressRedundant,
HeartbeatInterval: heartbeatInterval,
}
if interval == 0 {
subscriptions[i].Mode = gnmi.SubscriptionMode_ON_CHANGE
} else if interval > 0 {
subscriptions[i].Mode = gnmi.SubscriptionMode_SAMPLE
subscriptions[i].SampleInterval = uint64(1000000000 * interval)
} else {
subscriptions[i].Mode = gnmi.SubscriptionMode_TARGET_DEFINED
}
}
// Construct subscribe request
request := &gnmi.SubscribeRequest{
Request: &gnmi.SubscribeRequest_Subscribe{
Subscribe: &gnmi.SubscriptionList{
Prefix: &gnmi.Path{Target: client.name},
Mode: gnmi.SubscriptionList_STREAM,
Encoding: gnmi.Encoding_PROTO,
Subscription: subscriptions,
},
},
}
err = subscribeClient.Send(request)
if err != nil {
logctx.WithError(err).Error("gnmi: subscription failed")
return
}
// Subscription setup, kick off go routine to handle
// the stream. Add child to wait group such that this
// routine can wait for all its children on the way
// out.
marshaler := jsonpb.Marshaler{Indent: " "}
logctx.Info("gnmi: SubscribeClient running")
for ctx.Err() == nil {
reply, err := subscribeClient.Recv()
select {
case <-ctx.Done():
count := 0
for {
if _, err := subscribeClient.Recv(); err != nil {
logctx.WithFields(logrus.Fields{"count": count}).Info("gnmi: drained on cancellation")
break
}
count++
}
return
default:
if err != nil {
logctx.WithError(err).Error("gnmi: server terminated sub")
return
}
if client.logData {
json, err := marshaler.MarshalToString(reply)
if err == nil {
logctx.WithFields(logrus.Fields{"msg": json}).Debug("gnmi: server logdata")
}
}
updateResponse, ok := reply.Response.(*gnmi.SubscribeResponse_Update)
if !ok {
continue
}
notification := updateResponse.Update
// Work around XR not honoring target field
notification.Prefix.Target = client.name
// Prefilter using selectors
if len(client.selectors) > 0 {
prefix := decodeGNMIPath(notification.Prefix)
k := 0
for _, update := range notification.Update {
_, present := client.selectors[prefix+decodeGNMIPath(update.Path)]
if present {
notification.Update[k] = update
k++
}
}
notification.Update = notification.Update[:k]
}
dMs, err := client.codec.notificationToDataMsgs(client, notification)
if err != nil {
logctx.WithError(err).Error("gnmi: extracting msg from stream failed")
return
}
for _, dM := range dMs {
//
// Push data onto channel.
for _, dataChan := range client.dataChans {
//
// Make sure that if
// we are blocked on
// consumer, we still
// handle cancel.
select {
case <-ctx.Done():
return
case dataChan <- dM:
continue
}
}
}
}
}
logctx.Info("gnmi: all subscriptions closed")
return
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
/////// C O M M O N ///////
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//
// Run the client/server until shutdown. The role of this outer
// handler is to receive control plane messages and convey them to
// workers, as well as to retry sticky loops if we bail.
// Note: this loop handle dialout and dialin roles.
func (client *gnmiClient) run() {
var stats msgStats
var cancel context.CancelFunc
var ctx context.Context
username, _, _ := client.auth.getUP()
logctx := logger.WithFields(logrus.Fields{
"name": client.name,
"type": "gnmi",
"server": client.server,
"username": username,
})
client.logctx = logctx
go func() {
// Prime the pump, and kick off the first retry.
close(client.clientDone)
}()
logctx.Info("gNMI starting block")
for {
select {
case <-client.clientDone:
//
// If we receive clientDone signal here, we need to retry.
// Start by making a new channel.
ctx, cancel = context.WithCancel(context.Background())
client.clientDone = make(chan struct{})
go client.loop(ctx)
//
// Ensure that if we bailed out right away we
// do not reschedule immediately.
time.Sleep(gnmiWaitToRedial * time.Second)
case msg := <-client.ctrlChan:
switch msg.id {
case REPORT:
content, _ := json.Marshal(stats)
resp := &ctrlMsg{
id: ACK,
content: content,
respChan: nil,
}
msg.respChan <- resp
case SHUTDOWN:
logctx.WithFields(logrus.Fields{"ctrl_msg_id": msg.id}).
Info("gnmi: client loop, rxed SHUTDOWN, closing connections")
//
// Flag cancellation of binding and
// its connections and wait for
// cancellation to complete
// synchronously.
if cancel != nil {
logctx.Debug("gnmi: waiting for children")
cancel()
} else {
logctx.Debug("gnmi: NOT waiting for children")
}
logctx.Debug("gnmi: server notify conductor binding is closed")
resp := &ctrlMsg{
id: ACK,
respChan: nil,
}
msg.respChan <- resp
return
default:
logctx.Error("gnmi: block loop, unknown ctrl message")
}
}
}
}
// Module implement inputNodeModule interface
type gnmiInputModule struct {
}
func gnmiInputModuleNew() inputNodeModule {
return &gnmiInputModule{}
}
//
// Read the module configuration, and set us up to handle control
// requests, receive telemetry and feed the data channels passed in.
func (module *gnmiInputModule) configure(
name string,
nc nodeConfig,
dataChans []chan<- dataMsg) (error, chan<- *ctrlMsg) {
var err error
//
// If not set, will default to false
logData, _ := nc.config.GetBool(name, "logdata")
//
// If not set, will default to false
tls, _ := nc.config.GetBool(name, "tls")
tlsPEM, _ := nc.config.GetString(name, "tls_pem")
tlsHostname, _ := nc.config.GetString(name, "tls_hostname")
tlsKey, _ := nc.config.GetString(name, "tls_key")
server, _ := nc.config.GetString(name, "server")
jsonUInt64Compat, _ := nc.config.GetBool(name, "json_uint64_compat")
heartbeatInterval, err := nc.config.GetFloat64(name, "heartbeat_interval")
if err != nil {
heartbeatInterval = -1
}
paths := make([]string, 0)
selectors := make(map[string]struct{}, 0)
options, _ := nc.config.GetOptions(name)
for _, key := range options {
if strings.HasPrefix(key, "path") {
path, _ := nc.config.GetString(name, key)
paths = append(paths, path)
} else if strings.HasPrefix(key, "select") {
selector, _ := nc.config.GetString(name, key)
selectors[selector] = struct{}{}
}
}
//
// Handle user/password
authCollect := gnmiUPCollectorFactory()
err = authCollect.handleConfig(nc, name, server)
if err != nil {
return err, nil
}
//
// Create a control channel which will be used to control us,
// and kick off the server which will accept connections and
// listen for control requests.
ctrlChan := make(chan *ctrlMsg)
_, codec := getNewCodecGNMI(name)
client := &gnmiClient{
name: name,
codec: codec,
auth: authCollect,
server: server,
logData: logData,
ctrlChan: ctrlChan,
dataChans: dataChans,
clientDone: make(chan struct{}),
tls: tls,
tlsPEM: tlsPEM,
tlsHostname: tlsHostname,
tlsKey: tlsKey,
heartbeatInterval: heartbeatInterval,
paths: paths,
selectors: selectors,
jsonUInt64Compat: jsonUInt64Compat,
}
go client.run()
return nil, ctrlChan
}
var gnmiUPCollectorFactory userPasswordCollectorFactory
//
// We use init to setup the default user/password collection
// factory. This can be overwritten by test.
func init() {
gnmiUPCollectorFactory = func() userPasswordCollector {
return &cryptUserPasswordCollector{}
}
}