-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoperations.go
More file actions
1094 lines (966 loc) · 31.9 KB
/
Copy pathoperations.go
File metadata and controls
1094 lines (966 loc) · 31.9 KB
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
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2025 Daniel Schmidt
package gnmi
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
gnmipb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/gnmic/pkg/api"
"google.golang.org/grpc/status"
)
// Input validation constants
const (
// MaxValueSize is the maximum size for a single value in bytes (10MB)
MaxValueSize = 10 * 1024 * 1024
// MaxPathLength is the maximum length for a gNMI path (1024 characters)
MaxPathLength = 1024
)
// Input validation functions
// validatePaths validates a slice of gNMI paths
//
// Checks:
// - Paths slice is not empty
// - Each path starts with "/"
// - Each path length does not exceed MaxPathLength
// - Each path does not contain malicious patterns (null bytes, path traversal)
//
// Returns an error if any path is invalid with a descriptive message.
func validatePaths(paths []string) error {
if len(paths) == 0 {
return fmt.Errorf("paths cannot be empty")
}
for i, path := range paths {
if path == "" {
return fmt.Errorf("path cannot be empty (at index %d)", i)
}
if len(path) > MaxPathLength {
return fmt.Errorf("path at index %d exceeds maximum length of %d characters: %s", i, MaxPathLength, truncatePath(path))
}
// gNMI paths can be in two formats:
// 1. Absolute: /interfaces/interface[name=eth0]
// 2. Module-qualified: openconfig-interfaces:/interfaces/interface[name=eth0]
// or: Cisco-IOS-XR-um-banner-cfg:/banners/banner[banner-type=login]
// Check if path is valid (starts with / or is module-qualified)
if !isValidGNMIPath(path) {
return fmt.Errorf("path at index %d must start with '/' or be module-qualified (module:path): %s", i, path)
}
// Check for malicious patterns
if err := checkPathSecurity(path); err != nil {
return fmt.Errorf("path at index %d is invalid: %w", i, err)
}
}
return nil
}
// validateEncoding validates a gNMI encoding string
//
// Valid encodings: json, json_ietf, proto, ascii, bytes
// Empty string is valid (will default to json_ietf)
//
// Returns an error if the encoding is not supported.
func validateEncoding(encoding string) error {
if encoding == "" {
// Empty encoding is valid (will default to json_ietf)
return nil
}
return ValidateEncoding(encoding)
}
// validateValue validates a value string for gNMI operations
//
// Checks:
// - Value size does not exceed MaxValueSize (10MB)
// - For json/json_ietf encodings: basic JSON syntax validation
//
// Returns an error if the value is invalid with a descriptive message.
func validateValue(value string, encoding string) error {
valueSize := len(value)
if valueSize > MaxValueSize {
return fmt.Errorf("value size exceeds maximum of %d bytes (got %d bytes)", MaxValueSize, valueSize)
}
// Validate JSON syntax for json/json_ietf encodings
if encoding == EncodingJSON || encoding == EncodingJSONIETF || encoding == "" {
if err := validateJSONSyntax(value); err != nil {
return fmt.Errorf("invalid JSON syntax: %w", err)
}
}
return nil
}
// validateSetOperations validates a slice of SetOperation structs
//
// Checks:
// - Operations slice is not empty
// - Each operation has a valid path
// - Each operation has a valid encoding
// - Each operation has a valid value (for Update/Replace operations)
// - Each operation has a valid operation type
//
// Returns an error if any operation is invalid with a descriptive message.
func validateSetOperations(ops []SetOperation) error {
if len(ops) == 0 {
return fmt.Errorf("operations cannot be empty")
}
for i, op := range ops {
// Validate operation type
if op.OperationType == "" {
return fmt.Errorf("operation type cannot be empty (at index %d)", i)
}
if op.OperationType != OperationUpdate &&
op.OperationType != OperationReplace &&
op.OperationType != OperationDelete {
return fmt.Errorf("operation type invalid: %s (must be 'update', 'replace', or 'delete', at index %d)", op.OperationType, i)
}
// Validate path
if err := validatePaths([]string{op.Path}); err != nil {
return fmt.Errorf("operation at index %d: %w", i, err)
}
// Validate encoding
encoding := op.Encoding
if encoding == "" {
encoding = EncodingJSONIETF
}
if err := validateEncoding(encoding); err != nil {
return fmt.Errorf("operation at index %d: %w", i, err)
}
// Validate value for Update/Replace operations
if op.OperationType == OperationUpdate || op.OperationType == OperationReplace {
if err := validateValue(op.Value, encoding); err != nil {
return fmt.Errorf("operation at index %d: %w", i, err)
}
}
}
return nil
}
// Security validation helpers
// checkPathSecurity checks a path for malicious patterns
//
// Checks for:
// - Null bytes (path injection)
// - Path traversal patterns (..)
//
// Returns an error if malicious patterns are detected.
func checkPathSecurity(path string) error {
// Check for null bytes
for i := 0; i < len(path); i++ {
if path[i] == 0 {
return fmt.Errorf("path contains null byte at position %d", i)
}
}
// Check for path traversal patterns
// Note: ".." is valid in gNMI paths (e.g., "../config"), but we check for
// suspicious patterns like "/../" which could indicate path traversal attacks
if len(path) >= 4 {
for i := 0; i < len(path)-3; i++ {
if path[i] == '/' && path[i+1] == '.' && path[i+2] == '.' && path[i+3] == '/' {
return fmt.Errorf("path contains suspicious traversal pattern '/../' at position %d", i)
}
}
}
return nil
}
// validateJSONSyntax performs basic JSON syntax validation
//
// This is a lightweight check to catch obvious JSON errors without
// fully parsing the JSON (which would be expensive for large values).
//
// Checks:
// - Empty values are allowed (for some operations)
// - JSON must start with '{', '[', '"', or be a JSON literal (true/false/null/number)
// - Balanced braces/brackets (basic check)
//
// Returns an error if JSON syntax appears invalid.
//
//nolint:gocyclo // Validation logic naturally has high complexity
func validateJSONSyntax(value string) error {
if value == "" {
// Empty value is valid for some operations
return nil
}
// Trim whitespace
trimmed := value
for len(trimmed) > 0 && (trimmed[0] == ' ' || trimmed[0] == '\t' || trimmed[0] == '\n' || trimmed[0] == '\r') {
trimmed = trimmed[1:]
}
for len(trimmed) > 0 && (trimmed[len(trimmed)-1] == ' ' || trimmed[len(trimmed)-1] == '\t' || trimmed[len(trimmed)-1] == '\n' || trimmed[len(trimmed)-1] == '\r') {
trimmed = trimmed[:len(trimmed)-1]
}
if len(trimmed) == 0 {
return nil
}
// Check first character for valid JSON start
firstChar := trimmed[0]
if firstChar != '{' && firstChar != '[' && firstChar != '"' &&
firstChar != 't' && firstChar != 'f' && firstChar != 'n' &&
(firstChar < '0' || firstChar > '9') && firstChar != '-' {
return fmt.Errorf("JSON must start with '{', '[', '\"', or a JSON literal")
}
// Basic brace/bracket balancing check
braceCount := 0
bracketCount := 0
inString := false
escaped := false
for i := 0; i < len(trimmed); i++ {
ch := trimmed[i]
if escaped {
escaped = false
continue
}
if ch == '\\' {
escaped = true
continue
}
if ch == '"' {
inString = !inString
continue
}
if !inString {
switch ch {
case '{':
braceCount++
case '}':
braceCount--
if braceCount < 0 {
return fmt.Errorf("unbalanced braces: too many '}'")
}
case '[':
bracketCount++
case ']':
bracketCount--
if bracketCount < 0 {
return fmt.Errorf("unbalanced brackets: too many ']'")
}
}
}
}
if braceCount != 0 {
return fmt.Errorf("unbalanced braces: %d unclosed '{'", braceCount)
}
if bracketCount != 0 {
return fmt.Errorf("unbalanced brackets: %d unclosed '['", bracketCount)
}
if inString {
return fmt.Errorf("unterminated string")
}
return nil
}
// truncatePath truncates a path for error messages
//
// Returns the first 100 characters of the path followed by "..." if longer.
func truncatePath(path string) string {
if len(path) <= 100 {
return path
}
return path[:100] + "..."
}
// Get performs a gNMI Get operation to retrieve data from the device
//
// Get supports querying multiple paths in a single request. The paths parameter
// must be a non-empty slice of gNMI path strings. The encoding can be specified
// via request modifiers, defaulting to json_ietf.
//
// The operation uses RLock for concurrent read access, allowing multiple Get
// operations to run in parallel. Context timeout follows priority:
// 1. Request-specific timeout (via Timeout modifier)
// 2. Context deadline (if already set)
// 3. Client.OperationTimeout (fallback default)
//
// Example:
//
// ctx := context.Background()
// paths := []string{
// "/interfaces/interface[name=GigabitEthernet0/0/0/0]/state",
// "/system/config/hostname",
// }
// res, err := client.Get(ctx, paths)
// if err != nil {
// log.Fatal(err)
// }
// for _, notif := range res.Notifications {
// fmt.Printf("Path: %s\n", notif.Prefix)
// }
//
// Returns GetRes with notifications, timestamp, OK status, and any errors.
func (c *Client) Get(ctx context.Context, paths []string, mods ...func(*Req)) (GetRes, error) {
// Validate paths (before acquiring lock)
if err := validatePaths(paths); err != nil {
return GetRes{
OK: false,
Errors: []ErrorModel{{Message: err.Error()}},
}, fmt.Errorf("get: %w", err)
}
// Build request with default encoding
req := &Req{
Encoding: EncodingJSONIETF,
}
// Apply modifiers
for _, mod := range mods {
mod(req)
}
// Validate encoding (before acquiring lock)
if err := validateEncoding(req.Encoding); err != nil {
return GetRes{
OK: false,
Errors: []ErrorModel{{Message: err.Error()}},
}, fmt.Errorf("get: %w", err)
}
// Check context cancellation first (before acquiring lock)
if err := checkContextCancellation(ctx); err != nil {
return GetRes{
OK: false,
Errors: []ErrorModel{{Message: err.Error()}},
}, err
}
// Ensure connection is established (lazy connection)
if err := c.ensureConnected(ctx); err != nil {
return GetRes{
OK: false,
Errors: []ErrorModel{{Message: err.Error()}},
}, fmt.Errorf("get: connection failed: %w", err)
}
// Acquire lock after validation
c.mu.RLock()
defer c.mu.RUnlock()
// Check target exists
if c.target == nil {
return GetRes{
OK: false,
Errors: []ErrorModel{{Message: "client not connected"}},
}, fmt.Errorf("get: client not connected")
}
// Calculate total timeout budget to prevent unbounded accumulation
// Total timeout = OperationTimeout + sum of actual backoff delays
// This accurately reflects the maximum time needed for all retry attempts
totalTimeout := c.calculateTotalTimeout()
c.logger.Debug(ctx, "applying total timeout budget",
"totalTimeout", totalTimeout.String(),
"operationTimeout", c.OperationTimeout.String(),
"maxRetries", c.MaxRetries,
"target", c.Target)
// Apply parent context timeout for total budget
ctx, parentCancel := context.WithTimeout(ctx, totalTimeout)
defer parentCancel()
// NOTE: Full integration tests with mock gnmic target are documented in operations_test.go.
// Current tests focus on input validation, error handling, and API structure correctness.
// Build gnmic GetRequest
gnmicOpts := []api.GNMIOption{
api.Encoding(req.Encoding),
}
for _, path := range paths {
gnmicOpts = append(gnmicOpts, api.Path(path))
}
getReq, err := api.NewGetRequest(gnmicOpts...)
if err != nil {
c.logger.Error(ctx, "gNMI Get request creation failed",
"target", c.Target,
"error", err.Error())
return GetRes{
OK: false,
Errors: []ErrorModel{{Message: err.Error()}},
}, fmt.Errorf("get: failed to create request: %w", err)
}
// Log request
c.logger.Debug(ctx, "gNMI Get request",
"target", c.Target,
"paths", len(paths),
"encoding", req.Encoding)
// Log each path (at Debug level)
for i, path := range paths {
c.logger.Debug(ctx, "gNMI Get path",
"index", i,
"path", path)
}
// Execute request with retry logic
var getResp *gnmipb.GetResponse
var lastErr error
//nolint:dupl // Get and Set retry logic are similar but have different error handling
for attempt := 0; attempt <= c.MaxRetries; attempt++ {
// Check parent context cancellation before attempt
if err := checkContextCancellation(ctx); err != nil {
c.logger.Debug(ctx, "get operation canceled",
"operation", "get",
"attempt", attempt,
"error", err.Error())
return GetRes{
OK: false,
Errors: []ErrorModel{{Message: fmt.Sprintf("context canceled: %s", err.Error())}},
}, fmt.Errorf("get: %w", err)
}
// Create attempt-specific context with timeout
attemptCtx, attemptCancel := c.createAttemptContext(ctx, req)
// Execute Get request with attempt context
resp, err := c.target.Get(attemptCtx, getReq)
// Clean up attempt context immediately to prevent goroutine leak
attemptCancel()
if err == nil {
// Success
getResp = resp
lastErr = nil
break
}
// Store error
lastErr = err
// Extract error details for transient checking
errors := c.extractErrorDetails(err)
// Check if error is transient and retries remain
if c.checkTransientErrorModels(errors) && attempt < c.MaxRetries {
// Check for transport errors requiring reconnection
if c.isTransportError(lastErr) {
// Upgrade to write lock for reconnection
// First, release read lock
c.mu.RUnlock()
c.mu.Lock()
// Attempt to reconnect
if reconnectErr := c.reconnect(ctx); reconnectErr != nil {
// Reconnection failed, downgrade to read lock and return error
c.mu.Unlock()
c.mu.RLock()
c.logger.Error(ctx, "gNMI reconnection failed",
"operation", "get",
"error", reconnectErr.Error())
return GetRes{
OK: false,
Errors: []ErrorModel{{Message: fmt.Sprintf("operation failed and reconnection failed: %s", reconnectErr.Error())}},
}, fmt.Errorf("get: reconnection failed: %w", reconnectErr)
}
// Reconnection succeeded, downgrade to read lock and continue retry
c.mu.Unlock()
c.mu.RLock()
}
backoff := c.Backoff(attempt)
c.logger.Warn(ctx, "transient error, retrying",
"operation", "get",
"attempt", attempt+1,
"max_retries", c.MaxRetries,
"backoff", backoff,
"error", err.Error())
// Sleep with context cancellation awareness (uses ctx)
select {
case <-time.After(backoff):
// Backoff complete, continue to next attempt
continue
case <-ctx.Done():
// Context canceled during backoff
c.logger.Debug(ctx, "get operation canceled during backoff",
"operation", "get",
"attempt", attempt+1)
return GetRes{
OK: false,
Errors: []ErrorModel{{Message: fmt.Sprintf("context canceled during backoff: %s", ctx.Err().Error())}},
}, fmt.Errorf("get: context canceled during backoff: %w", ctx.Err())
}
} else {
// Non-transient error or no retries remaining
break
}
}
// Check if all retries failed
if lastErr != nil {
c.logger.Error(ctx, "gNMI Get failed",
"target", c.Target,
"error", lastErr.Error())
// Extract gRPC error details
errors := c.extractErrorDetails(lastErr)
return GetRes{
OK: false,
Errors: errors,
}, fmt.Errorf("get: request failed: %w", lastErr)
}
// Log response
c.logger.Debug(ctx, "gNMI Get response",
"target", c.Target,
"notifications", len(getResp.Notification))
// Log each notification with redacted JSON values (at Debug level)
for i, notif := range getResp.Notification {
// Convert notification to JSON and redact sensitive data
if notifJSON, err := json.Marshal(notif); err == nil {
sanitizedNotif := c.prepareJSONForLogging(string(notifJSON))
c.logger.Debug(ctx, "gNMI Get notification",
"index", i,
"timestamp", notif.Timestamp,
"updates", len(notif.Update),
"deletes", len(notif.Delete),
"notification", sanitizedNotif)
}
}
// Parse response
timestamp := time.Now().UnixNano()
return GetRes{
Notifications: getResp.Notification,
Timestamp: timestamp,
OK: true,
}, nil
}
// Set performs a gNMI Set operation to configure the device
//
// Set supports multiple update, replace, and delete operations in a single request.
// The ops parameter must be a non-empty slice of SetOperation structs created via
// the Update(), Replace(), or Delete() helper functions.
//
// The operation uses Lock for exclusive write access, serializing all Set operations
// to prevent concurrent writes. Context timeout follows priority:
// 1. Request-specific timeout (via Timeout modifier)
// 2. Context deadline (if already set)
// 3. Client.OperationTimeout (fallback default)
//
// Example:
//
// ctx := context.Background()
// ops := []gnmi.SetOperation{
// gnmi.Update("/interfaces/interface[name=Gi0/0/0/0]/config/description",
// `{"description": "WAN Interface"}`),
// gnmi.Replace("/interfaces/interface[name=Gi0/0/0/0]/config/enabled",
// `{"enabled": true}`),
// gnmi.Delete("/interfaces/interface[name=Gi0/0/0/1]/config"),
// }
// res, err := client.Set(ctx, ops)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Printf("Set operation successful: %v\n", res.OK)
//
// Returns SetRes with response, timestamp, OK status, and any errors.
func (c *Client) Set(ctx context.Context, ops []SetOperation, mods ...func(*Req)) (SetRes, error) {
// Validate operations (before acquiring lock for better performance)
if err := validateSetOperations(ops); err != nil {
return SetRes{
OK: false,
Errors: []ErrorModel{{Message: err.Error()}},
}, fmt.Errorf("set: %w", err)
}
// Check context cancellation first (before acquiring lock)
if err := checkContextCancellation(ctx); err != nil {
return SetRes{
OK: false,
Errors: []ErrorModel{{Message: err.Error()}},
}, err
}
// Ensure connection is established (lazy connection)
if err := c.ensureConnected(ctx); err != nil {
return SetRes{
OK: false,
Errors: []ErrorModel{{Message: err.Error()}},
}, fmt.Errorf("set: connection failed: %w", err)
}
// Acquire lock after validation and connection
c.mu.Lock()
defer c.mu.Unlock()
// Build request for modifiers
req := &Req{}
// Apply modifiers
for _, mod := range mods {
mod(req)
}
// Calculate total timeout budget to prevent unbounded accumulation
// Total timeout = OperationTimeout + sum of actual backoff delays
// This accurately reflects the maximum time needed for all retry attempts
totalTimeout := c.calculateTotalTimeout()
c.logger.Debug(ctx, "applying total timeout budget",
"totalTimeout", totalTimeout.String(),
"operationTimeout", c.OperationTimeout.String(),
"maxRetries", c.MaxRetries,
"target", c.Target)
// Apply parent context timeout for total budget
ctx, parentCancel := context.WithTimeout(ctx, totalTimeout)
defer parentCancel()
// NOTE: Full integration tests with mock gnmic target are documented in operations_test.go.
// Current tests focus on input validation, operation helpers, and error handling correctness.
// Build gnmic SetRequest based on operation types
gnmicOpts := []api.GNMIOption{}
for _, op := range ops {
encoding := op.Encoding
if encoding == "" {
encoding = EncodingJSONIETF
}
switch op.OperationType {
case OperationUpdate:
gnmicOpts = append(gnmicOpts, api.Update(api.Path(op.Path), api.Value(op.Value, encoding)))
case OperationReplace:
gnmicOpts = append(gnmicOpts, api.Replace(api.Path(op.Path), api.Value(op.Value, encoding)))
case OperationDelete:
gnmicOpts = append(gnmicOpts, api.Delete(op.Path))
default:
// Invalid operation type
return SetRes{
OK: false,
Errors: []ErrorModel{{
Message: fmt.Sprintf("invalid operation type: %s", op.OperationType),
}},
}, fmt.Errorf("set: invalid operation type: %s", op.OperationType)
}
}
setReq, err := api.NewSetRequest(gnmicOpts...)
if err != nil {
c.logger.Error(ctx, "gNMI Set request creation failed",
"target", c.Target,
"error", err.Error())
return SetRes{
OK: false,
Errors: []ErrorModel{{Message: err.Error()}},
}, fmt.Errorf("set: failed to create request: %w", err)
}
// Log request with sanitized operation details
c.logger.Debug(ctx, "gNMI Set request",
"target", c.Target,
"operations", len(ops))
// Log each operation with redacted JSON values (at Debug level)
for i, op := range ops {
// Prepare JSON for logging (redacts sensitive data)
sanitizedValue := c.prepareJSONForLogging(op.Value)
c.logger.Debug(ctx, "gNMI Set operation",
"index", i,
"type", op.OperationType,
"path", op.Path,
"encoding", op.Encoding,
"value", sanitizedValue)
}
// Execute request with retry logic
var setResp *gnmipb.SetResponse
var lastErr error
//nolint:dupl // Get and Set retry logic are similar but have different error handling
for attempt := 0; attempt <= c.MaxRetries; attempt++ {
// Check parent context cancellation before attempt
if err := checkContextCancellation(ctx); err != nil {
c.logger.Debug(ctx, "set operation canceled",
"operation", "set",
"attempt", attempt,
"error", err.Error())
return SetRes{
OK: false,
Errors: []ErrorModel{{Message: fmt.Sprintf("context canceled: %s", err.Error())}},
}, fmt.Errorf("set: %w", err)
}
// Create attempt-specific context with timeout
attemptCtx, attemptCancel := c.createAttemptContext(ctx, req)
// Execute Set request with attempt context
resp, err := c.target.Set(attemptCtx, setReq)
// Clean up attempt context immediately to prevent goroutine leak
attemptCancel()
if err == nil {
// Success
setResp = resp
lastErr = nil
break
}
// Store error
lastErr = err
// Extract error details for transient checking
errors := c.extractErrorDetails(err)
// Check if error is transient and retries remain
if c.checkTransientErrorModels(errors) && attempt < c.MaxRetries {
// Check for transport errors requiring reconnection
// Note: Set operation already holds write lock (c.mu.Lock), so no lock upgrade needed
if c.isTransportError(lastErr) {
// Attempt to reconnect (already holding write lock)
if reconnectErr := c.reconnect(ctx); reconnectErr != nil {
// Reconnection failed, return error
c.logger.Error(ctx, "gNMI reconnection failed",
"operation", "set",
"error", reconnectErr.Error())
return SetRes{
OK: false,
Errors: []ErrorModel{{Message: fmt.Sprintf("operation failed and reconnection failed: %s", reconnectErr.Error())}},
}, fmt.Errorf("set: reconnection failed: %w", reconnectErr)
}
// Reconnection succeeded, continue to retry
}
backoff := c.Backoff(attempt)
c.logger.Warn(ctx, "transient error, retrying",
"operation", "set",
"attempt", attempt+1,
"max_retries", c.MaxRetries,
"backoff", backoff,
"error", err.Error())
// Sleep with context cancellation awareness (uses ctx)
select {
case <-time.After(backoff):
// Backoff complete, continue to next attempt
continue
case <-ctx.Done():
// Context canceled during backoff
c.logger.Debug(ctx, "set operation canceled during backoff",
"operation", "set",
"attempt", attempt+1)
return SetRes{
OK: false,
Errors: []ErrorModel{{Message: fmt.Sprintf("context canceled during backoff: %s", ctx.Err().Error())}},
}, fmt.Errorf("set: context canceled during backoff: %w", ctx.Err())
}
} else {
// Non-transient error or no retries remaining
break
}
}
// Check if all retries failed
if lastErr != nil {
c.logger.Error(ctx, "gNMI Set failed",
"target", c.Target,
"error", lastErr.Error())
// Extract gRPC error details
errors := c.extractErrorDetails(lastErr)
return SetRes{
OK: false,
Errors: errors,
}, fmt.Errorf("set: request failed: %w", lastErr)
}
// Log response
c.logger.Debug(ctx, "gNMI Set response",
"target", c.Target,
"results", len(setResp.Response))
// Parse response
timestamp := time.Now().UnixNano()
return SetRes{
Response: setResp,
Timestamp: timestamp,
OK: true,
}, nil
}
// Update creates a SetOperation for updating a path with a value
//
// Update operations modify existing configuration, creating it if it doesn't exist.
// This is the most common Set operation type.
//
// The encoding defaults to json_ietf. Use the SetEncoding() modifier to specify
// a different encoding (json, proto, ascii, bytes).
//
// Parameters:
// - path: gNMI path string (e.g., "/interfaces/interface[name=Gi0/0/0/0]/config")
// - value: JSON-encoded value string
// - opts: optional modifiers (SetEncoding, etc.)
//
// Example:
//
// // Default encoding (json_ietf)
// op := gnmi.Update("/system/config/hostname", `{"hostname": "router1"}`)
//
// // Explicit encoding
// op := gnmi.Update("/interfaces/interface[name=Gi0]/config", protoBytes,
// gnmi.SetEncoding("proto"))
func Update(path, value string, opts ...func(*SetOperation)) SetOperation {
op := SetOperation{
OperationType: OperationUpdate,
Path: path,
Value: value,
Encoding: EncodingJSONIETF, // default
}
// Apply functional options
for _, opt := range opts {
opt(&op)
}
return op
}
// Replace creates a SetOperation for replacing a path with a value
//
// Replace operations remove existing configuration at the path before applying
// the new value. Use Replace when you need to ensure no old config remains.
//
// The encoding defaults to json_ietf. Use the SetEncoding() modifier to specify
// a different encoding (json, proto, ascii, bytes).
//
// Parameters:
// - path: gNMI path string
// - value: JSON-encoded value string
// - opts: optional modifiers (SetEncoding, etc.)
//
// Example:
//
// // Default encoding (json_ietf)
// op := gnmi.Replace("/interfaces/interface[name=Gi0/0/0/0]/config",
// `{"mtu": 9000}`)
//
// // Explicit encoding
// op := gnmi.Replace("/system/config", jsonData,
// gnmi.SetEncoding("json"))
func Replace(path, value string, opts ...func(*SetOperation)) SetOperation {
op := SetOperation{
OperationType: OperationReplace,
Path: path,
Value: value,
Encoding: EncodingJSONIETF, // default
}
// Apply functional options
for _, opt := range opts {
opt(&op)
}
return op
}
// Delete creates a SetOperation for deleting a path
//
// Delete operations remove configuration at the specified path.
//
// Parameters:
// - path: gNMI path string
//
// Example:
//
// op := gnmi.Delete("/interfaces/interface[name=Gi0/0/0/1]/config")
func Delete(path string) SetOperation {
return SetOperation{
OperationType: OperationDelete,
Path: path,
Value: "", // Empty value for delete
}
}
// Internal helper methods
// calculateTotalTimeout calculates the total timeout for all retry attempts
//
// This method calculates the actual total timeout based on exponential backoff
// by summing the backoff delays for all attempts. This provides an accurate
// timeout budget instead of assuming maximum backoff for all retries.
//
// Formula: OperationTimeout + sum(Backoff(0), Backoff(1), ..., Backoff(MaxRetries))
//
// Example:
//
// OperationTimeout = 15s
// MaxRetries = 3
// BackoffMinDelay = 1s
// BackoffDelayFactor = 2.0
//
// Backoff delays:
// Attempt 0: 1s
// Attempt 1: 2s
// Attempt 2: 4s
//
// Total timeout = 15s + 1s + 2s + 4s = 22s
//
// This is much more accurate than the old formula which would calculate:
//
// 15s + (3+1) × 60s = 255s (10x too long!)
//
// Returns the total timeout duration for all retry attempts.
func (c *Client) calculateTotalTimeout() time.Duration {
totalBackoff := time.Duration(0)
for attempt := 0; attempt <= c.MaxRetries; attempt++ {
backoff := c.Backoff(attempt)
totalBackoff += backoff
}
return c.OperationTimeout + totalBackoff
}
// checkContextCancellation checks if context is canceled or deadline exceeded
//
// This is a non-blocking check that immediately returns if the context is canceled
// or deadline has exceeded. Used before retry attempts to avoid wasted work.
//
// Returns context.Canceled if context is canceled, context.DeadlineExceeded if
// deadline exceeded, or nil if context is still valid.
func checkContextCancellation(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err() // context.Canceled or context.DeadlineExceeded
default:
return nil
}
}
// createAttemptContext creates a new context for a single retry attempt with timeout
//
// Timeout priority model:
// 1. Request-specific timeout (req.Timeout > 0) - highest priority
// 2. Existing context deadline (ctx.Deadline() set) - medium priority
// 3. Client default timeout (c.OperationTimeout) - fallback
//
// This model allows:
// - Per-request timeout overrides: client.Get(ctx, paths, gnmi.Timeout(5*time.Second))
// - Context deadline propagation: ctx, cancel := context.WithTimeout(parent, 30*time.Second)
// - Sensible defaults: No timeout specified uses client.OperationTimeout
//
// CRITICAL: Caller MUST call the returned cancel function after operation completes
// to prevent goroutine leaks. Failure to call cancel will leak resources.
//
// Example usage:
//
// attemptCtx, attemptCancel := c.createAttemptContext(ctx, req)
// resp, err := c.target.Get(attemptCtx, getReq)
// attemptCancel() // CRITICAL: Must clean up immediately
//
// Warnings are logged for extreme timeouts:
// - Very short timeouts (<1s) may not allow operations to complete
// - Very long timeouts (>5min) may delay error detection
//
// Returns a context with timeout applied and its cancel function.
func (c *Client) createAttemptContext(ctx context.Context, req *Req) (context.Context, context.CancelFunc) {
// Priority 1: Request-specific timeout (highest)
if req.Timeout > 0 {
// Warn about extreme timeouts