forked from openconfig/gnmi
/
client.go
334 lines (298 loc) · 9.37 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
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package client contains transport implementation for the parent client
// library using gnmi.proto.
//
// Note: this package should not be used directly. Use
// github.com/openconfig/gnmi/client instead.
package client
import (
"context"
"encoding/json"
"fmt"
"math"
"net"
"strings"
"time"
log "github.com/golang/glog"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc"
"github.com/golang/protobuf/proto"
"github.com/openconfig/ygot/ygot"
"github.com/openconfig/gnmi/client"
"github.com/openconfig/gnmi/client/grpcutil"
"github.com/openconfig/gnmi/path"
"github.com/openconfig/gnmi/value"
gpb "github.com/openconfig/gnmi/proto/gnmi"
)
// ToSubscribeRequest is the function used to convert a client.Query into
// a *gpb.SubscribeRequest. It can be stubbed for unit test.
var ToSubscribeRequest = subscribe
// Type defines the name resolution for this client type.
const Type = "gnmi"
// Client handles execution of the query and caching of its results.
type Client struct {
conn *grpc.ClientConn
client gpb.GNMIClient
sub gpb.GNMI_SubscribeClient
query client.Query
recv client.ProtoHandler
handler client.NotificationHandler
connected bool
}
// New returns a new initialized client. If error is nil, returned Client has
// established a connection to d. Close needs to be called for cleanup.
func New(ctx context.Context, d client.Destination) (client.Impl, error) {
if d.TunnelConn == nil {
if len(d.Addrs) != 1 {
return nil, fmt.Errorf("d.Addrs must only contain one entry: %v", d.Addrs)
}
}
opts := []grpc.DialOption{
grpc.WithBlock(),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32)),
}
switch d.TLS {
case nil:
opts = append(opts, grpc.WithInsecure())
default:
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(d.TLS)))
}
if d.Credentials != nil {
secure := true
if d.TLS == nil {
secure = false
}
pc := newPassCred(d.Credentials.Username, d.Credentials.Password, secure)
opts = append(opts, grpc.WithPerRPCCredentials(pc))
}
gCtx, cancel := context.WithTimeout(ctx, d.Timeout)
defer cancel()
if d.TunnelConn != nil {
withContextDialer := grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return d.TunnelConn, nil
})
opts = append(opts, withContextDialer)
}
addr := ""
if len(d.Addrs) != 0 {
addr = d.Addrs[0]
}
conn, err := grpc.DialContext(gCtx, addr, opts...)
if err != nil {
return nil, fmt.Errorf("Dialer(%s, %v): %v", addr, d.Timeout, err)
}
return NewFromConn(ctx, conn, d)
}
// NewFromConn creates and returns the client based on the provided transport.
func NewFromConn(ctx context.Context, conn *grpc.ClientConn, d client.Destination) (*Client, error) {
ok, err := grpcutil.Lookup(ctx, conn, "gnmi.gNMI")
if err != nil {
log.V(1).Infof("gRPC reflection lookup on %q for service gnmi.gNMI failed: %v", d.Addrs, err)
// This check is disabled for now. Reflection will become part of gNMI
// specification in the near future, so we can't enforce it yet.
}
if !ok {
// This check is disabled for now. Reflection will become part of gNMI
// specification in the near future, so we can't enforce it yet.
}
cl := gpb.NewGNMIClient(conn)
return &Client{
conn: conn,
client: cl,
}, nil
}
// Subscribe sends the gNMI Subscribe RPC to the server.
func (c *Client) Subscribe(ctx context.Context, q client.Query) error {
sub, err := c.client.Subscribe(ctx)
if err != nil {
return fmt.Errorf("gpb.GNMIClient.Subscribe(%v) failed to initialize Subscribe RPC: %v", q, err)
}
sr := q.SubReq
if sr == nil {
sr, err = ToSubscribeRequest(q)
if err != nil {
return fmt.Errorf("generating SubscribeRequest proto: %v", err)
}
}
if err := sub.Send(sr); err != nil {
return fmt.Errorf("client.Send(%+v): %v", sr, err)
}
c.sub = sub
c.query = q
if q.ProtoHandler == nil {
c.recv = c.defaultRecv
c.handler = q.NotificationHandler
} else {
c.recv = q.ProtoHandler
}
return nil
}
// Poll will send a single gNMI poll request to the server.
func (c *Client) Poll() error {
if err := c.sub.Send(&gpb.SubscribeRequest{Request: &gpb.SubscribeRequest_Poll{Poll: &gpb.Poll{}}}); err != nil {
return fmt.Errorf("client.Poll(): %v", err)
}
return nil
}
// Peer returns the peer of the current stream. If the client is not created or
// if the peer is not valid nil is returned.
func (c *Client) Peer() string {
return c.query.Addrs[0]
}
// Close forcefully closes the underlying connection, terminating the query
// right away. It's safe to call Close multiple times.
func (c *Client) Close() error {
return c.conn.Close()
}
// Recv will recieve a single message from the server and process it based on
// the provided handlers (Proto or Notification).
func (c *Client) Recv() error {
n, err := c.sub.Recv()
if err != nil {
return err
}
return c.recv(n)
}
// defaultRecv is the default implementation of recv provided by the client.
// This function will be replaced by the ProtoHandler member of the Query
// struct passed to New(), if it is set.
func (c *Client) defaultRecv(msg proto.Message) error {
if !c.connected {
c.handler(client.Connected{})
c.connected = true
}
resp, ok := msg.(*gpb.SubscribeResponse)
if !ok {
return fmt.Errorf("failed to type assert message %#v", msg)
}
log.V(1).Info(resp)
switch v := resp.Response.(type) {
default:
return fmt.Errorf("unknown response %T: %s", v, v)
case *gpb.SubscribeResponse_Error:
return fmt.Errorf("error in response: %s", v)
case *gpb.SubscribeResponse_SyncResponse:
c.handler(client.Sync{})
if c.query.Type == client.Poll || c.query.Type == client.Once {
return client.ErrStopReading
}
case *gpb.SubscribeResponse_Update:
n := v.Update
p := path.ToStrings(n.Prefix, true)
ts := time.Unix(0, n.Timestamp)
for _, u := range n.Update {
if u.Path == nil {
return fmt.Errorf("invalid nil path in update: %v", u)
}
u, err := noti(p, u.Path, ts, u)
if err != nil {
return err
}
c.handler(u)
}
for _, d := range n.Delete {
u, err := noti(p, d, ts, nil)
if err != nil {
return err
}
c.handler(u)
}
}
return nil
}
// Capabilities calls the gNMI Capabilities RPC.
func (c *Client) Capabilities(ctx context.Context, r *gpb.CapabilityRequest) (*gpb.CapabilityResponse, error) {
return c.client.Capabilities(ctx, r)
}
// Get calls the gNMI Get RPC.
func (c *Client) Get(ctx context.Context, r *gpb.GetRequest) (*gpb.GetResponse, error) {
return c.client.Get(ctx, r)
}
// Set calls the gNMI Set RPC.
func (c *Client) Set(ctx context.Context, r *gpb.SetRequest) (*gpb.SetResponse, error) {
return c.client.Set(ctx, r)
}
func getType(t client.Type) gpb.SubscriptionList_Mode {
switch t {
case client.Once:
return gpb.SubscriptionList_ONCE
case client.Stream:
return gpb.SubscriptionList_STREAM
case client.Poll:
return gpb.SubscriptionList_POLL
}
return gpb.SubscriptionList_ONCE
}
func subscribe(q client.Query) (*gpb.SubscribeRequest, error) {
s := &gpb.SubscribeRequest_Subscribe{
Subscribe: &gpb.SubscriptionList{
Mode: getType(q.Type),
Prefix: &gpb.Path{Target: q.Target},
},
}
if q.UpdatesOnly {
s.Subscribe.UpdatesOnly = true
}
for _, qq := range q.Queries {
pp, err := ygot.StringToPath(pathToString(qq), ygot.StructuredPath, ygot.StringSlicePath)
if err != nil {
return nil, fmt.Errorf("invalid query path %q: %v", qq, err)
}
s.Subscribe.Subscription = append(s.Subscribe.Subscription, &gpb.Subscription{Path: pp})
}
return &gpb.SubscribeRequest{Request: s}, nil
}
func noti(prefix []string, pp *gpb.Path, ts time.Time, u *gpb.Update) (client.Notification, error) {
sp := path.ToStrings(pp, false)
// Make a full new copy of prefix + u.Path to avoid any reuse of underlying
// slice arrays.
p := make([]string, 0, len(prefix)+len(sp))
p = append(p, prefix...)
p = append(p, sp...)
if u == nil {
return client.Delete{Path: p, TS: ts}, nil
}
if u.Val != nil {
val, err := value.ToScalar(u.Val)
if err != nil {
return nil, fmt.Errorf("failed to decode %s: %v", p, err)
}
return client.Update{Path: p, TS: ts, Val: val, Dups: u.Duplicates}, nil
}
switch v := u.Value; v.Type {
case gpb.Encoding_BYTES:
return client.Update{Path: p, TS: ts, Val: v.Value, Dups: u.Duplicates}, nil
case gpb.Encoding_JSON, gpb.Encoding_JSON_IETF:
var val interface{}
if err := json.Unmarshal(v.Value, &val); err != nil {
return nil, fmt.Errorf("json.Unmarshal(%q, val): %v", v, err)
}
return client.Update{Path: p, TS: ts, Val: val, Dups: u.Duplicates}, nil
default:
return nil, fmt.Errorf("Unsupported value type: %v", v.Type)
}
}
func init() {
client.Register(Type, New)
}
func pathToString(q client.Path) string {
qq := make(client.Path, len(q))
copy(qq, q)
// Escape all slashes within a path element. ygot.StringToPath will handle
// these escapes.
for i, e := range qq {
qq[i] = strings.Replace(e, "/", "\\/", -1)
}
return strings.Join(qq, "/")
}