-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathdb_client.go
1305 lines (1155 loc) · 36.6 KB
/
db_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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package client provides a generic access layer for data available in system
package client
import (
"bytes"
"encoding/json"
"fmt"
"net"
"reflect"
"strconv"
"strings"
"sync"
"time"
log "github.com/golang/glog"
spb "github.com/sonic-net/sonic-gnmi/proto"
sdcfg "github.com/sonic-net/sonic-gnmi/sonic_db_config"
"github.com/Workiva/go-datastructures/queue"
"github.com/go-redis/redis"
gnmipb "github.com/openconfig/gnmi/proto/gnmi"
)
const (
// indentString represents the default indentation string used for
// JSON. Two spaces are used here.
indentString string = " "
)
// Client defines a set of methods which every client must implement.
// This package provides one implmentation for now: the DbClient
//
type Client interface {
// StreamRun will start watching service on data source
// and enqueue data change to the priority queue.
// It stops all activities upon receiving signal on stop channel
// It should run as a go routine
StreamRun(q *queue.PriorityQueue, stop chan struct{}, w *sync.WaitGroup, subscribe *gnmipb.SubscriptionList)
// Poll will start service to respond poll signal received on poll channel.
// data read from data source will be enqueued on to the priority queue
// The service will stop upon detection of poll channel closing.
// It should run as a go routine
PollRun(q *queue.PriorityQueue, poll chan struct{}, w *sync.WaitGroup, subscribe *gnmipb.SubscriptionList)
OnceRun(q *queue.PriorityQueue, once chan struct{}, w *sync.WaitGroup, subscribe *gnmipb.SubscriptionList)
// Get return data from the data source in format of *spb.Value
Get(w *sync.WaitGroup) ([]*spb.Value, error)
// Set data based on path and value
Set(delete []*gnmipb.Path, replace []*gnmipb.Update, update []*gnmipb.Update) error
// Capabilities of the switch
Capabilities() []gnmipb.ModelData
// Close provides implemenation for explicit cleanup of Client
Close() error
// callbacks on send failed
FailedSend()
// callback on sent
SentOne(*Value)
}
type Stream interface {
Send(m *gnmipb.SubscribeResponse) error
}
// Let it be variable visible to other packages for now.
// May add an interface function for it.
var UseRedisLocalTcpPort bool = false
// redis client connected to each DB
var Target2RedisDb = make(map[string]map[string]*redis.Client)
// MinSampleInterval is the lowest sampling interval for streaming subscriptions.
// Any non-zero value that less than this threshold is considered invalid argument.
var MinSampleInterval = time.Second
// IntervalTicker is a factory method to implement interval ticking.
// Exposed for UT purposes.
var IntervalTicker = func(interval time.Duration) <-chan time.Time {
return time.After(interval)
}
var NeedMock bool = false
var intervalTickerMutex sync.Mutex
// Define a new function to set the IntervalTicker variable
func SetIntervalTicker(f func(interval time.Duration) <-chan time.Time) {
if NeedMock == true {
intervalTickerMutex.Lock()
defer intervalTickerMutex.Unlock()
IntervalTicker = f
} else {
IntervalTicker = f
}
}
// Define a new function to get the IntervalTicker variable
func GetIntervalTicker() func(interval time.Duration) <-chan time.Time {
if NeedMock == true {
intervalTickerMutex.Lock()
defer intervalTickerMutex.Unlock()
return IntervalTicker
} else {
return IntervalTicker
}
}
type tablePath struct {
dbNamespace string
dbName string
tableName string
tableKey string
delimitor string
field string
value string
index int
operation int
// path name to be used in json data which may be different
// from the real data path. Ex. in Counters table, real tableKey
// is oid:0x####, while key name like Ethernet## may be put
// in json data. They are to be filled in populateDbtablePath()
jsonTableName string
jsonTableKey string
jsonDelimitor string
jsonField string
}
type Value struct {
*spb.Value
}
// Implement Compare method for priority queue
func (val Value) Compare(other queue.Item) int {
oval := other.(Value)
if val.GetTimestamp() > oval.GetTimestamp() {
return 1
} else if val.GetTimestamp() == oval.GetTimestamp() {
return 0
}
return -1
}
func (val Value) GetTimestamp() int64 {
if n := val.GetNotification(); n != nil {
return n.GetTimestamp()
}
return val.Value.GetTimestamp()
}
type DbClient struct {
prefix *gnmipb.Path
pathG2S map[*gnmipb.Path][]tablePath
q *queue.PriorityQueue
channel chan struct{}
synced sync.WaitGroup // Control when to send gNMI sync_response
w *sync.WaitGroup // wait for all sub go routines to finish
mu sync.RWMutex // Mutex for data protection among routines for DbClient
sendMsg int64
recvMsg int64
errors int64
}
func NewDbClient(paths []*gnmipb.Path, prefix *gnmipb.Path) (Client, error) {
var client DbClient
var err error
// Testing program may ask to use redis local tcp connection
if UseRedisLocalTcpPort {
useRedisTcpClient()
}
client.prefix = prefix
client.pathG2S = make(map[*gnmipb.Path][]tablePath)
err = populateAllDbtablePath(prefix, paths, &client.pathG2S)
if err != nil {
return nil, err
} else {
return &client, nil
}
}
// String returns the target the client is querying.
func (c *DbClient) String() string {
// TODO: print gnmiPaths of this DbClient
return fmt.Sprintf("DbClient Prefix %v sendMsg %v, recvMsg %v",
c.prefix.GetTarget(), c.sendMsg, c.recvMsg)
}
func (c *DbClient) StreamRun(q *queue.PriorityQueue, stop chan struct{}, w *sync.WaitGroup, subscribe *gnmipb.SubscriptionList) {
c.w = w
defer c.w.Done()
c.q = q
c.channel = stop
if subscribe.GetSubscription() == nil {
log.V(2).Infof("No incoming subscription, it is considered a dialout connection.")
// NOTE: per https://github.com/sonic-net/sonic-gnmi/blob/master/doc/dialout.md#dialout_client_cli-and-dialout_server_cli
// TELEMETRY_CLIENT subscription doesn't specificy type of the stream.
// Handling it as a ON_CHANGE stream for backward compatibility.
for gnmiPath := range c.pathG2S {
c.w.Add(1)
c.synced.Add(1)
go streamOnChangeSubscription(c, gnmiPath)
}
} else {
log.V(2).Infof("Stream subscription request received, mode: %v, subscription count: %v",
subscribe.GetMode(),
len(subscribe.GetSubscription()))
for _, sub := range subscribe.GetSubscription() {
log.V(2).Infof("Sub mode: %v, path: %v", sub.GetMode(), sub.GetPath())
subMode := sub.GetMode()
if subMode == gnmipb.SubscriptionMode_SAMPLE {
c.w.Add(1) // wait group to indicate the streaming session is complete.
c.synced.Add(1) // wait group to indicate whether sync_response is sent.
go streamSampleSubscription(c, sub, subscribe.GetUpdatesOnly())
} else if subMode == gnmipb.SubscriptionMode_ON_CHANGE {
c.w.Add(1)
c.synced.Add(1)
go streamOnChangeSubscription(c, sub.GetPath())
} else {
enqueueFatalMsg(c, fmt.Sprintf("unsupported subscription mode, %v", subMode))
return
}
}
}
// Wait until all data values corresponding to the path(s) specified
// in the SubscriptionList has been transmitted at least once
c.synced.Wait()
// Inject sync message
c.q.Put(Value{
&spb.Value{
Timestamp: time.Now().UnixNano(),
SyncResponse: true,
},
})
log.V(2).Infof("%v Synced", c.pathG2S)
<-c.channel
log.V(1).Infof("Exiting StreamRun routine for Client %v", c)
}
// streamOnChangeSubscription implements Subscription "ON_CHANGE STREAM" mode
func streamOnChangeSubscription(c *DbClient, gnmiPath *gnmipb.Path) {
tblPaths := c.pathG2S[gnmiPath]
log.V(2).Infof("streamOnChangeSubscription gnmiPath: %v", gnmiPath)
if tblPaths[0].field != "" {
if len(tblPaths) > 1 {
go dbFieldMultiSubscribe(c, gnmiPath, true, time.Millisecond*200, false)
} else {
go dbFieldSubscribe(c, gnmiPath, true, time.Millisecond*200)
}
} else {
// sample interval and update only parameters are not applicable
go dbTableKeySubscribe(c, gnmiPath, 0, true)
}
}
// streamSampleSubscription implements Subscription "SAMPLE STREAM" mode
func streamSampleSubscription(c *DbClient, sub *gnmipb.Subscription, updateOnly bool) {
samplingInterval, err := validateSampleInterval(sub)
if err != nil {
enqueueFatalMsg(c, err.Error())
c.synced.Done()
c.w.Done()
return
}
gnmiPath := sub.GetPath()
tblPaths := c.pathG2S[gnmiPath]
log.V(2).Infof("streamSampleSubscription gnmiPath: %v", gnmiPath)
if tblPaths[0].field != "" {
if len(tblPaths) > 1 {
dbFieldMultiSubscribe(c, gnmiPath, false, samplingInterval, updateOnly)
} else {
dbFieldSubscribe(c, gnmiPath, false, samplingInterval)
}
} else {
dbTableKeySubscribe(c, gnmiPath, samplingInterval, updateOnly)
}
}
func (c *DbClient) PollRun(q *queue.PriorityQueue, poll chan struct{}, w *sync.WaitGroup, subscribe *gnmipb.SubscriptionList) {
c.w = w
defer c.w.Done()
c.q = q
c.channel = poll
for {
_, more := <-c.channel
if !more {
log.V(1).Infof("%v poll channel closed, exiting pollDb routine", c)
return
}
t1 := time.Now()
for gnmiPath, tblPaths := range c.pathG2S {
val, err := tableData2TypedValue(tblPaths, nil)
if err != nil {
return
}
spbv := &spb.Value{
Prefix: c.prefix,
Path: gnmiPath,
Timestamp: time.Now().UnixNano(),
SyncResponse: false,
Val: val,
}
c.q.Put(Value{spbv})
log.V(6).Infof("Added spbv #%v", spbv)
}
c.q.Put(Value{
&spb.Value{
Timestamp: time.Now().UnixNano(),
SyncResponse: true,
},
})
log.V(4).Infof("Sync done, poll time taken: %v ms", int64(time.Since(t1)/time.Millisecond))
}
}
func (c *DbClient) OnceRun(q *queue.PriorityQueue, once chan struct{}, w *sync.WaitGroup, subscribe *gnmipb.SubscriptionList) {
return
}
func (c *DbClient) Get(w *sync.WaitGroup) ([]*spb.Value, error) {
// wait sync for Get, not used for now
c.w = w
var values []*spb.Value
ts := time.Now()
for gnmiPath, tblPaths := range c.pathG2S {
val, err := tableData2TypedValue(tblPaths, nil)
if err != nil {
return nil, err
}
values = append(values, &spb.Value{
Prefix: c.prefix,
Path: gnmiPath,
Timestamp: ts.UnixNano(),
Val: val,
})
}
log.V(6).Infof("Getting #%v", values)
log.V(4).Infof("Get done, total time taken: %v ms", int64(time.Since(ts)/time.Millisecond))
return values, nil
}
// TODO: Log data related to this session
func (c *DbClient) Close() error {
return nil
}
// Convert from SONiC Value to its corresponding gNMI proto stream
// response type.
func ValToResp(val Value) (*gnmipb.SubscribeResponse, error) {
switch val.GetSyncResponse() {
case true:
return &gnmipb.SubscribeResponse{
Response: &gnmipb.SubscribeResponse_SyncResponse{
SyncResponse: true,
},
}, nil
default:
// In case the subscribe/poll routines encountered fatal error
if fatal := val.GetFatal(); fatal != "" {
return nil, fmt.Errorf("%s", fatal)
}
// In case the client returned a full gnmipb.Notification object
if n := val.GetNotification(); n != nil {
return &gnmipb.SubscribeResponse{
Response: &gnmipb.SubscribeResponse_Update{Update: n}}, nil
}
return &gnmipb.SubscribeResponse{
Response: &gnmipb.SubscribeResponse_Update{
Update: &gnmipb.Notification{
Timestamp: val.GetTimestamp(),
Prefix: val.GetPrefix(),
Update: []*gnmipb.Update{
{
Path: val.GetPath(),
Val: val.GetVal(),
},
},
},
},
}, nil
}
}
func GetTableKeySeparator(target string, ns string) (string, error) {
_, ok := spb.Target_value[target]
if !ok {
log.V(1).Infof(" %v not a valid path target", target)
return "", fmt.Errorf("%v not a valid path target", target)
}
var separator string = sdcfg.GetDbSeparator(target, ns)
return separator, nil
}
func GetRedisClientsForDb(target string) map[string]*redis.Client {
redis_client_map := make(map[string]*redis.Client)
if sdcfg.CheckDbMultiNamespace() {
ns_list := sdcfg.GetDbNonDefaultNamespaces()
for _, ns := range ns_list {
redis_client_map[ns] = Target2RedisDb[ns][target]
}
} else {
ns := sdcfg.GetDbDefaultNamespace()
redis_client_map[ns] = Target2RedisDb[ns][target]
}
return redis_client_map
}
// This function get target present in GNMI Request and
// returns: 1. DbName (string) 2. Is DbName valid (bool)
// 3. DbNamespace (string) 4. Is DbNamespace present in Target (bool)
func IsTargetDb(target string) (string, bool, string, bool) {
targetname := strings.Split(target, "/")
dbName := targetname[0]
dbNameSpaceExist := false
dbNamespace := sdcfg.GetDbDefaultNamespace()
if len(targetname) > 2 {
log.V(1).Infof("target format is not correct")
return dbName, false, dbNamespace, dbNameSpaceExist
}
if len(targetname) > 1 {
dbNamespace = targetname[1]
dbNameSpaceExist = true
}
for name, _ := range spb.Target_value {
if name == dbName {
return dbName, true, dbNamespace, dbNameSpaceExist
}
}
return dbName, false, dbNamespace, dbNameSpaceExist
}
// For testing only
func useRedisTcpClient() {
if !UseRedisLocalTcpPort {
return
}
for _, dbNamespace := range sdcfg.GetDbAllNamespaces() {
Target2RedisDb[dbNamespace] = make(map[string]*redis.Client)
for dbName, dbn := range spb.Target_value {
if dbName != "OTHERS" {
// DB connector for direct redis operation
redisDb := redis.NewClient(&redis.Options{
Network: "tcp",
Addr: sdcfg.GetDbTcpAddr(dbName, dbNamespace),
Password: "", // no password set
DB: int(dbn),
DialTimeout: 0,
})
Target2RedisDb[dbNamespace][dbName] = redisDb
}
}
}
}
// Client package prepare redis clients to all DBs automatically
func init() {
for _, dbNamespace := range sdcfg.GetDbAllNamespaces() {
Target2RedisDb[dbNamespace] = make(map[string]*redis.Client)
for dbName, dbn := range spb.Target_value {
if dbName != "OTHERS" {
// DB connector for direct redis operation
redisDb := redis.NewClient(&redis.Options{
Network: "unix",
Addr: sdcfg.GetDbSock(dbName, dbNamespace),
Password: "", // no password set
DB: int(dbn),
DialTimeout: 0,
})
Target2RedisDb[dbNamespace][dbName] = redisDb
}
}
}
}
// gnmiFullPath builds the full path from the prefix and path.
func gnmiFullPath(prefix, path *gnmipb.Path) *gnmipb.Path {
fullPath := &gnmipb.Path{Origin: path.Origin}
if path.GetElement() != nil {
fullPath.Element = append(prefix.GetElement(), path.GetElement()...)
}
if path.GetElem() != nil {
fullPath.Elem = append(prefix.GetElem(), path.GetElem()...)
}
return fullPath
}
func populateAllDbtablePath(prefix *gnmipb.Path, paths []*gnmipb.Path, pathG2S *map[*gnmipb.Path][]tablePath) error {
for _, path := range paths {
err := populateDbtablePath(prefix, path, pathG2S)
if err != nil {
return err
}
}
return nil
}
// Populate table path in DB from gnmi path
func populateDbtablePath(prefix, path *gnmipb.Path, pathG2S *map[*gnmipb.Path][]tablePath) error {
var buffer bytes.Buffer
var dbPath string
var tblPath tablePath
target := prefix.GetTarget()
targetDbName, targetDbNameValid, targetDbNameSpace, targetDbNameSpaceExist := IsTargetDb(target)
// Verify it is a valid db name
if !targetDbNameValid {
return fmt.Errorf("Invalid target dbName %v", targetDbName)
}
// Verify Namespace is valid
dbNamespace, ok := sdcfg.GetDbNamespaceFromTarget(targetDbNameSpace)
if !ok {
return fmt.Errorf("Invalid target dbNameSpace %v", targetDbNameSpace)
}
if targetDbName == "COUNTERS_DB" {
err := initCountersPortNameMap()
if err != nil {
return err
}
err = initCountersQueueNameMap()
if err != nil {
return err
}
err = initAliasMap()
if err != nil {
return err
}
err = initCountersPfcwdNameMap()
if err != nil {
return err
}
}
fullPath := path
if prefix != nil {
fullPath = gnmiFullPath(prefix, path)
}
stringSlice := []string{targetDbName}
separator, _ := GetTableKeySeparator(targetDbName, dbNamespace)
elems := fullPath.GetElem()
if elems != nil {
for i, elem := range elems {
// TODO: Usage of key field
log.V(6).Infof("index %d elem : %#v %#v", i, elem.GetName(), elem.GetKey())
if i != 0 {
buffer.WriteString(separator)
}
buffer.WriteString(elem.GetName())
stringSlice = append(stringSlice, elem.GetName())
}
dbPath = buffer.String()
}
// First lookup the Virtual path to Real path mapping tree
// The path from gNMI might not be real db path
if tblPaths, err := lookupV2R(stringSlice); err == nil {
if targetDbNameSpaceExist {
return fmt.Errorf("Target having %v namespace is not supported for V2R Dataset", dbNamespace)
}
(*pathG2S)[path] = tblPaths
log.V(5).Infof("v2r from %v to %+v ", stringSlice, tblPaths)
return nil
} else {
log.V(5).Infof("v2r lookup failed for %v %v", stringSlice, err)
}
tblPath.dbNamespace = dbNamespace
tblPath.dbName = targetDbName
if len(stringSlice) > 1 {
tblPath.tableName = stringSlice[1]
}
tblPath.delimitor = separator
var mappedKey string
if len(stringSlice) > 2 { // tmp, to remove mappedKey
mappedKey = stringSlice[2]
}
redisDb, ok := Target2RedisDb[tblPath.dbNamespace][tblPath.dbName]
if !ok {
return fmt.Errorf("Redis Client not present for dbName %v dbNamespace %v", targetDbName, dbNamespace)
}
// The expect real db path could be in one of the formats:
// <1> DB Table
// <2> DB Table Key
// <3> DB Table Field
// <4> DB Table Key Field
// <5> DB Table Key Key Field
switch len(stringSlice) {
case 2: // only table name provided
res, err := redisDb.Keys(tblPath.tableName + "*").Result()
if err != nil || len(res) < 1 {
log.V(2).Infof("Invalid db table Path %v %v", target, dbPath)
return fmt.Errorf("Failed to find %v %v %v %v", target, dbPath, err, res)
}
tblPath.tableKey = ""
case 3: // Third element could be table key; or field name in which case table name itself is the key too
n, err := redisDb.Exists(tblPath.tableName + tblPath.delimitor + mappedKey).Result()
if err != nil {
return fmt.Errorf("redis Exists op failed for %v", dbPath)
}
if n == 1 {
tblPath.tableKey = mappedKey
} else {
tblPath.field = mappedKey
}
case 4: // Fourth element could part of the table key or field name
tblPath.tableKey = mappedKey + tblPath.delimitor + stringSlice[3]
// verify whether this key exists
key := tblPath.tableName + tblPath.delimitor + tblPath.tableKey
n, err := redisDb.Exists(key).Result()
if err != nil {
return fmt.Errorf("redis Exists op failed for %v", dbPath)
}
if n != 1 { // Looks like the Fourth slice is not part of the key
tblPath.tableKey = mappedKey
tblPath.field = stringSlice[3]
}
case 5: // both third and fourth element are part of table key, fourth element must be field name
tblPath.tableKey = mappedKey + tblPath.delimitor + stringSlice[3]
tblPath.field = stringSlice[4]
default:
log.V(2).Infof("Invalid db table Path %v", dbPath)
return fmt.Errorf("Invalid db table Path %v", dbPath)
}
var key string
if tblPath.tableKey != "" {
key = tblPath.tableName + tblPath.delimitor + tblPath.tableKey
n, _ := redisDb.Exists(key).Result()
if n != 1 {
log.V(2).Infof("No valid entry found on %v with key %v", dbPath, key)
return fmt.Errorf("No valid entry found on %v with key %v", dbPath, key)
}
}
(*pathG2S)[path] = []tablePath{tblPath}
log.V(5).Infof("tablePath %+v", tblPath)
return nil
}
// makeJSON renders the database Key op value_pairs to map[string]interface{} for JSON marshall.
func makeJSON_redis(msi *map[string]interface{}, key *string, op *string, mfv map[string]string) error {
if key == nil && op == nil {
for f, v := range mfv {
(*msi)[f] = v
}
return nil
}
fp := map[string]interface{}{}
for f, v := range mfv {
fp[f] = v
}
if key == nil {
(*msi)[*op] = fp
} else if op == nil {
(*msi)[*key] = fp
} else {
// Also have operation layer
of := map[string]interface{}{}
of[*op] = fp
(*msi)[*key] = of
}
return nil
}
// emitJSON marshalls map[string]interface{} to JSON byte stream.
func emitJSON(v *map[string]interface{}) ([]byte, error) {
//j, err := json.MarshalIndent(*v, "", indentString)
j, err := json.Marshal(*v)
if err != nil {
return nil, fmt.Errorf("JSON marshalling error: %v", err)
}
return j, nil
}
// tableData2Msi renders the redis DB data to map[string]interface{}
// which may be marshaled to JSON format
// If only table name provided in the tablePath, find all keys in the table, otherwise
// Use tableName + tableKey as key to get all field value paires
func tableData2Msi(tblPath *tablePath, useKey bool, op *string, msi *map[string]interface{}) error {
redisDb := Target2RedisDb[tblPath.dbNamespace][tblPath.dbName]
var pattern string
var dbkeys []string
var err error
var fv map[string]string
//Only table name provided
if tblPath.tableKey == "" {
// tables in COUNTERS_DB other than COUNTERS table doesn't have keys
if tblPath.dbName == "COUNTERS_DB" && tblPath.tableName != "COUNTERS" {
pattern = tblPath.tableName
} else {
pattern = tblPath.tableName + tblPath.delimitor + "*"
}
dbkeys, err = redisDb.Keys(pattern).Result()
if err != nil {
log.V(2).Infof("redis Keys failed for %v, pattern %s", tblPath, pattern)
return fmt.Errorf("redis Keys failed for %v, pattern %s %v", tblPath, pattern, err)
}
} else {
// both table name and key provided
dbkeys = []string{tblPath.tableName + tblPath.delimitor + tblPath.tableKey}
}
// Asked to use jsonField and jsonTableKey in the final json value
if tblPath.jsonField != "" && tblPath.jsonTableKey != "" {
val, err := redisDb.HGet(dbkeys[0], tblPath.field).Result()
if err != nil {
log.V(3).Infof("redis HGet failed for %v %v", tblPath, err)
// ignore non-existing field which was derived from virtual path
return nil
}
fv = map[string]string{tblPath.jsonField: val}
makeJSON_redis(msi, &tblPath.jsonTableKey, op, fv)
log.V(6).Infof("Added json key %v fv %v ", tblPath.jsonTableKey, fv)
return nil
}
for idx, dbkey := range dbkeys {
fv, err = redisDb.HGetAll(dbkey).Result()
if err != nil {
log.V(2).Infof("redis HGetAll failed for %v, dbkey %s", tblPath, dbkey)
return err
}
if tblPath.jsonTableKey != "" { // If jsonTableKey was prepared, use it
err = makeJSON_redis(msi, &tblPath.jsonTableKey, op, fv)
} else if (tblPath.tableKey != "" && !useKey) || tblPath.tableName == dbkey {
err = makeJSON_redis(msi, nil, op, fv)
} else {
var key string
// Split dbkey string into two parts and second part is key in table
keys := strings.SplitN(dbkey, tblPath.delimitor, 2)
if len(keys) < 2 {
return fmt.Errorf("dbkey: %s, failed split from delimitor %v", dbkey, tblPath.delimitor)
}
key = keys[1]
err = makeJSON_redis(msi, &key, op, fv)
}
if err != nil {
log.V(2).Infof("makeJSON err %s for fv %v", err, fv)
return err
}
log.V(6).Infof("Added idex %v fv %v ", idx, fv)
}
return nil
}
func msi2TypedValue(msi map[string]interface{}) (*gnmipb.TypedValue, error) {
jv, err := emitJSON(&msi)
if err != nil {
log.V(2).Infof("emitJSON err %s for %v", err, msi)
return nil, fmt.Errorf("emitJSON err %s for %v", err, msi)
}
return &gnmipb.TypedValue{
Value: &gnmipb.TypedValue_JsonIetfVal{
JsonIetfVal: jv,
}}, nil
}
func tableData2TypedValue(tblPaths []tablePath, op *string) (*gnmipb.TypedValue, error) {
var useKey bool
msi := make(map[string]interface{})
for _, tblPath := range tblPaths {
redisDb := Target2RedisDb[tblPath.dbNamespace][tblPath.dbName]
if tblPath.jsonField == "" { // Not asked to include field in json value, which means not wildcard query
// table path includes table, key and field
if tblPath.field != "" {
if len(tblPaths) != 1 {
log.V(2).Infof("WARNING: more than one path exists for field granularity query: %v", tblPaths)
}
var key string
if tblPath.tableKey != "" {
key = tblPath.tableName + tblPath.delimitor + tblPath.tableKey
} else {
key = tblPath.tableName
}
val, err := redisDb.HGet(key, tblPath.field).Result()
if err != nil {
log.V(2).Infof("redis HGet failed for %v", tblPath)
return nil, err
}
// TODO: support multiple table paths
return &gnmipb.TypedValue{
Value: &gnmipb.TypedValue_StringVal{
StringVal: val,
}}, nil
}
}
err := tableData2Msi(&tblPath, useKey, nil, &msi)
if err != nil {
return nil, err
}
}
return msi2TypedValue(msi)
}
func enqueueFatalMsg(c *DbClient, msg string) {
putFatalMsg(c.q, msg)
}
func putFatalMsg(q *queue.PriorityQueue, msg string) {
q.Put(Value{
&spb.Value{
Timestamp: time.Now().UnixNano(),
Fatal: msg,
},
})
}
// dbFieldMultiSubscribe would read a field from multiple tables and put to output queue.
// It handles queries like "COUNTERS/Ethernet*/xyz" where the path translates to a field in multiple tables.
// For SAMPLE mode, it would send periodically regardless of change.
// However, if `updateOnly` is true, the payload would include only the changed fields.
// For ON_CHANGE mode, it would send only if the value has changed since the last update.
func dbFieldMultiSubscribe(c *DbClient, gnmiPath *gnmipb.Path, onChange bool, interval time.Duration, updateOnly bool) {
defer c.w.Done()
tblPaths := c.pathG2S[gnmiPath]
// Init the path to value map, it saves the previous value
path2ValueMap := make(map[tablePath]string)
readVal := func() map[string]interface{} {
msi := make(map[string]interface{})
for _, tblPath := range tblPaths {
var key string
if tblPath.tableKey != "" {
key = tblPath.tableName + tblPath.delimitor + tblPath.tableKey
} else {
key = tblPath.tableName
}
// run redis get directly for field value
redisDb := Target2RedisDb[tblPath.dbNamespace][tblPath.dbName]
val, err := redisDb.HGet(key, tblPath.field).Result()
if err == redis.Nil {
if tblPath.jsonField != "" {
// ignore non-existing field which was derived from virtual path
continue
}
log.V(2).Infof("%v doesn't exist with key %v in db", tblPath.field, key)
val = ""
} else if err != nil {
log.V(1).Infof(" redis HGet error on %v with key %v", tblPath.field, key)
val = ""
}
// This value was saved before and it hasn't changed since then
_, valueMapped := path2ValueMap[tblPath]
if (onChange || updateOnly) && valueMapped && val == path2ValueMap[tblPath] {
continue
}
path2ValueMap[tblPath] = val
fv := map[string]string{tblPath.jsonField: val}
msi[tblPath.jsonTableKey] = fv
log.V(6).Infof("new value %v for %v", val, tblPath)
}
return msi
}
sendVal := func(msi map[string]interface{}) error {
val, err := msi2TypedValue(msi)
if err != nil {
enqueueFatalMsg(c, err.Error())
return err
}
spbv := &spb.Value{
Prefix: c.prefix,
Path: gnmiPath,
Timestamp: time.Now().UnixNano(),
Val: val,
}
if err = c.q.Put(Value{spbv}); err != nil {
log.V(1).Infof("Queue error: %v", err)
return err
}
return nil
}
msi := readVal()
if err := sendVal(msi); err != nil {
c.synced.Done()
return
}
c.synced.Done()
intervalTicker := GetIntervalTicker()(interval)
for {
select {
case <-c.channel:
log.V(1).Infof("Stopping dbFieldMultiSubscribe routine for Client %s ", c)
return
case <-intervalTicker:
msi := readVal()
if onChange == false || len(msi) != 0 {
if err := sendVal(msi); err != nil {
log.Errorf("Queue error: %v", err)
return
}
}
}
intervalTicker = GetIntervalTicker()(interval)
}
}
// dbFieldSubscribe would read a field from a single table and put to output queue.
// Handles queries like "COUNTERS/Ethernet0/xyz" where the path translates to a field in a table.
// For SAMPLE mode, it would send periodically regardless of change.
// For ON_CHANGE mode, it would send only if the value has changed since the last update.
func dbFieldSubscribe(c *DbClient, gnmiPath *gnmipb.Path, onChange bool, interval time.Duration) {
defer c.w.Done()
tblPaths := c.pathG2S[gnmiPath]
tblPath := tblPaths[0]
// run redis get directly for field value
redisDb := Target2RedisDb[tblPath.dbNamespace][tblPath.dbName]
var key string
if tblPath.tableKey != "" {
key = tblPath.tableName + tblPath.delimitor + tblPath.tableKey
} else {
key = tblPath.tableName
}
readVal := func() string {
newVal, err := redisDb.HGet(key, tblPath.field).Result()
if err == redis.Nil {
log.V(2).Infof("%v doesn't exist with key %v in db", tblPath.field, key)
newVal = ""
} else if err != nil {
log.V(1).Infof(" redis HGet error on %v with key %v", tblPath.field, key)
newVal = ""
}
return newVal
}
sendVal := func(newVal string) error {
spbv := &spb.Value{
Prefix: c.prefix,
Path: gnmiPath,
Timestamp: time.Now().UnixNano(),
Val: &gnmipb.TypedValue{
Value: &gnmipb.TypedValue_StringVal{
StringVal: newVal,
},
},
}
if err := c.q.Put(Value{spbv}); err != nil {
log.V(1).Infof("Queue error: %v", err)
return err
}
return nil
}
// Read the initial value and signal sync after sending it
val := readVal()
err := sendVal(val)
if err != nil {
putFatalMsg(c.q, err.Error())
c.synced.Done()