This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
gnmi_client.go
81 lines (75 loc) · 1.99 KB
/
gnmi_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
package app
import (
"context"
"fmt"
"github.com/karimra/gnmic/types"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/gnmi/proto/gnmi_ext"
)
func (a *App) ClientCapabilities(ctx context.Context, tc *types.TargetConfig, ext ...*gnmi_ext.Extension) (*gnmi.CapabilityResponse, error) {
// acquire writer lock
a.operLock.Lock()
t, err := a.initTarget(tc)
a.operLock.Unlock()
if err != nil {
return nil, err
}
// acquire reader lock
a.operLock.RLock()
err = a.CreateGNMIClient(ctx, t)
a.operLock.RUnlock()
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(ctx, t.Config.Timeout)
defer cancel()
capResponse, err := t.Capabilities(ctx, ext...)
if err != nil {
return nil, fmt.Errorf("%q CapabilitiesRequest failed: %v", t.Config.Address, err)
}
return capResponse, nil
}
func (a *App) ClientGet(ctx context.Context, tc *types.TargetConfig, req *gnmi.GetRequest) (*gnmi.GetResponse, error) {
a.operLock.Lock()
t, err := a.initTarget(tc)
a.operLock.Unlock()
if err != nil {
return nil, err
}
// acquire reader lock
a.operLock.RLock()
err = a.CreateGNMIClient(ctx, t)
a.operLock.RUnlock()
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(ctx, t.Config.Timeout)
defer cancel()
getResponse, err := t.Get(ctx, req)
if err != nil {
return nil, fmt.Errorf("%q GetRequest failed: %v", t.Config.Address, err)
}
return getResponse, nil
}
func (a *App) ClientSet(ctx context.Context, tc *types.TargetConfig, req *gnmi.SetRequest) (*gnmi.SetResponse, error) {
a.operLock.Lock()
t, err := a.initTarget(tc)
a.operLock.Unlock()
if err != nil {
return nil, err
}
// acquire reader lock
a.operLock.RLock()
err = a.CreateGNMIClient(ctx, t)
a.operLock.RUnlock()
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(ctx, t.Config.Timeout)
defer cancel()
setResponse, err := t.Set(ctx, req)
if err != nil {
return nil, fmt.Errorf("target %q SetRequest failed: %v", t.Config.Name, err)
}
return setResponse, nil
}