-
Notifications
You must be signed in to change notification settings - Fork 10
/
lemming.go
424 lines (364 loc) · 11.2 KB
/
lemming.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
// Copyright 2022 Google LLC
//
// 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 lemming provides reference device to be used with ondatra.
package lemming
import (
"context"
"fmt"
"net"
"runtime"
"sync"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/reflection"
"k8s.io/klog/v2"
gribis "github.com/openconfig/gribigo/server"
"github.com/openconfig/lemming/bgp"
"github.com/openconfig/lemming/dataplane"
"github.com/openconfig/lemming/dataplane/dplaneopts"
fgnmi "github.com/openconfig/lemming/gnmi"
"github.com/openconfig/lemming/gnmi/fakedevice"
"github.com/openconfig/lemming/gnmi/oc"
"github.com/openconfig/lemming/gnmi/reconciler"
fgnoi "github.com/openconfig/lemming/gnoi"
fgnsi "github.com/openconfig/lemming/gnsi"
fgribi "github.com/openconfig/lemming/gribi"
fp4rt "github.com/openconfig/lemming/p4rt"
"github.com/openconfig/lemming/sysrib"
log "github.com/golang/glog"
)
type gRPCService struct {
s *grpc.Server
lis net.Listener
stopped chan struct{}
}
// Device is the reference device implementation.
type Device struct {
gnmignoignsiService *gRPCService
gribiService *gRPCService
p4rtService *gRPCService
stop func()
gnmiServer *fgnmi.Server
gnoiServer *fgnoi.Server
gribiServer *fgribi.Server
gnsiServer *fgnsi.Server
p4rtServer *fp4rt.Server
dplaneServer *dataplane.Dataplane
// Stores the errors if the server fails will be returned on call to stop.
errsMu sync.Mutex
errs []error
stopped chan struct{}
}
// Option are device startup options for lemming.
type Option func(*opt)
type opt struct {
// deviceConfigJSON is the contents of the JSON document (prior to unmarshal).
deviceConfigJSON []byte
// tlsCredentials contains TLS credentials that can be used for a device.
tlsCredentials credentials.TransportCredentials
gribiAddr string
gnmiAddr string
p4rtAddr string
sysribAddr string
bgpPort uint16
dataplane bool
dataplaneOpts []dplaneopts.Option
gribiOpts []gribis.ServerOpt
}
// resolveOpts applies all the options and returns a struct containing the result.
func resolveOpts(opts []Option) *opt {
o := &opt{
sysribAddr: "/tmp/sysrib.api",
}
for _, opt := range opts {
opt(o)
}
return o
}
// WithInitialConfig sets the startup config of the device to c.
// Today we do not allow the configuration to be changed in flight, but this
// can be implemented in the future.
//
// This option is intended for standalone device testing.
func WithInitialConfig(c []byte) Option {
return func(o *opt) {
o.deviceConfigJSON = c
}
}
// WithGRIBIAddr is a device option that specifies that the gRIBI address.
func WithGRIBIAddr(addr string) Option {
return func(o *opt) {
o.gribiAddr = addr
}
}
// WithGNMIAddr is a device option that specifies that the gRIBI address.
func WithGNMIAddr(addr string) Option {
return func(o *opt) {
o.gnmiAddr = addr
}
}
// WithP4RTAddr is a device option that specifies that the P4RT address.
func WithP4RTAddr(addr string) Option {
return func(o *opt) {
o.p4rtAddr = addr
}
}
// WithBGPPort is a device option that specifies that the BGP port.
func WithBGPPort(port uint16) Option {
return func(o *opt) {
o.bgpPort = port
}
}
// WithTLSCredsFromFile loads the credentials from the specified cert and key file
// and returns them such that they can be used for the gNMI and gRIBI servers.
func WithTLSCredsFromFile(certFile, keyFile string) (Option, error) {
t, err := credentials.NewServerTLSFromFile(certFile, keyFile)
if err != nil {
return nil, err
}
return func(o *opt) {
o.tlsCredentials = t
}, nil
}
// WithTransportCreds returns a wrapper of TransportCredentials into a DevOpt.
func WithTransportCreds(c credentials.TransportCredentials) Option {
return func(o *opt) {
o.tlsCredentials = c
}
}
func WithDataplane(enable bool) Option {
return func(o *opt) {
o.dataplane = enable
}
}
func WithDataplaneOpts(opts ...dplaneopts.Option) Option {
return func(o *opt) {
o.dataplaneOpts = opts
}
}
// WithSysribAddr specifies a unix domain socket path for sysrib.
// Default: "/tmp/sysrib.api"
func WithSysribAddr(sysribAddr string) Option {
return func(o *opt) {
o.sysribAddr = sysribAddr
}
}
// WithGRIBIOpts specifies the set of gRIBI options that should be passed to
// the gRIBI server that lemming runs.
func WithGRIBIOpts(opts ...gribis.ServerOpt) Option {
return func(o *opt) {
o.gribiOpts = opts
}
}
// New returns a new initialized device.
func New(targetName, zapiURL string, opts ...Option) (*Device, error) {
var dplane *dataplane.Dataplane
var recs []reconciler.Reconciler
resolvedOpts := resolveOpts(opts)
if resolvedOpts.dataplane {
if runtime.GOOS != "linux" {
return nil, fmt.Errorf("dataplane only supported on linux, GOOS is %s", runtime.GOOS)
}
log.Info("enabling dataplane")
var err error
dplane, err = dataplane.New(context.Background(), resolvedOpts.dataplaneOpts...)
if err != nil {
return nil, err
}
recs = append(recs, dplane)
}
log.Info("starting gNMI")
root := &oc.Root{}
if jcfg := resolvedOpts.deviceConfigJSON; jcfg != nil {
if err := oc.Unmarshal(jcfg, root); err != nil {
return nil, fmt.Errorf("cannot unmarshal JSON configuration, %v", err)
}
} else {
// The initial config may specify a differently-named network
// instance, so only add when it is not present.
root.GetOrCreateNetworkInstance(fakedevice.DefaultNetworkInstance).Type = oc.NetworkInstanceTypes_NETWORK_INSTANCE_TYPE_DEFAULT_INSTANCE
}
var grpcOpts []grpc.ServerOption
creds := resolvedOpts.tlsCredentials
if creds != nil {
grpcOpts = append(grpcOpts, grpc.Creds(creds))
}
grpcOpts = append(grpcOpts, grpc.StreamInterceptor(fgnmi.NewSubscribeTargetUpdateInterceptor(targetName)))
s := grpc.NewServer(grpcOpts...)
recs = append(recs,
fakedevice.NewSystemBaseTask(),
fakedevice.NewBootTimeTask(),
fakedevice.NewCurrentTimeTask(),
bgp.NewGoBGPTask(targetName, zapiURL, resolvedOpts.bgpPort),
)
log.Info("starting gNSI")
gnsiServer := fgnsi.New(s)
gnmiServer, err := fgnmi.New(s, targetName, gnsiServer.GetPathZ(), recs...)
if err != nil {
return nil, err
}
cacheClient := gnmiServer.LocalClient()
log.Infof("starting sysrib")
sysribServer, err := sysrib.New(root)
if err != nil {
return nil, err
}
if err := sysribServer.Start(context.Background(), cacheClient, targetName, zapiURL, resolvedOpts.sysribAddr); err != nil {
return nil, fmt.Errorf("sysribServer failed to start: %v", err)
}
log.Info("starting gRIBI")
// TODO(wenbli): Use gRIBIs once we change lemming's KNE config to use different ports.
// gRIBIs := grpc.NewServer()
gribiServer, err := fgribi.New(s, cacheClient, targetName, root, fmt.Sprintf("unix:%s", resolvedOpts.sysribAddr), resolvedOpts.gribiOpts...)
if err != nil {
return nil, err
}
log.Info("starting P4RT (there is nothing here yet)")
P4RTs := grpc.NewServer()
log.Info("Create listeners")
lgnmi, err := net.Listen("tcp", resolvedOpts.gnmiAddr)
if err != nil {
return nil, fmt.Errorf("cannot create gRPC server for gNMI/gNOI/gNSI, %v", err)
}
lgribi, err := net.Listen("tcp", resolvedOpts.gribiAddr)
if err != nil {
return nil, fmt.Errorf("cannot create gRPC server for gRIBI, %v", err)
}
lp4rt, err := net.Listen("tcp", resolvedOpts.p4rtAddr)
if err != nil {
return nil, fmt.Errorf("cannot create gRPC server for P4RT, %v", err)
}
gnoiServer, err := fgnoi.New(s, cacheClient, targetName)
if err != nil {
return nil, err
}
d := &Device{
gnmignoignsiService: &gRPCService{
s: s,
lis: lgnmi,
stopped: make(chan struct{}),
},
gribiService: &gRPCService{
// TODO(wenbli): Change s to gRIBIs once we change lemming's KNE config to use different ports.
s: s,
lis: lgribi,
stopped: make(chan struct{}),
},
p4rtService: &gRPCService{
s: P4RTs,
lis: lp4rt,
stopped: make(chan struct{}),
},
gnmiServer: gnmiServer,
gnoiServer: gnoiServer,
gribiServer: gribiServer,
gnsiServer: gnsiServer,
p4rtServer: fp4rt.New(P4RTs),
dplaneServer: dplane,
}
reflection.Register(s)
d.startServer()
if err := gnmiServer.StartReconcilers(context.Background()); err != nil {
return nil, err
}
log.Info("lemming created")
return d, nil
}
// GRIBIAddr returns the address that the gRIBI server is listening on.
func (d *Device) GRIBIAddr() string {
return d.gribiService.lis.Addr().String()
}
// GNMIAddr returns the address that the gNMI/gNOI/gNSI server is listening on.
func (d *Device) GNMIAddr() string {
return d.gnmignoignsiService.lis.Addr().String()
}
// P4RTAddr returns the address that the P4RT server is listening on.
func (d *Device) P4RTAddr() string {
return d.p4rtService.lis.Addr().String()
}
// GRIBIListener returns the listener that the gRIBI server is listening on.
func (d *Device) GRIBIListener() net.Listener {
return d.gribiService.lis
}
// GNMIListener returns the listener that the gNMI/gNOI/gNSI server is listening on.
func (d *Device) GNMIListener() net.Listener {
return d.gnmignoignsiService.lis
}
// P4RTListener returns the listener that the P4RT server is listening on.
func (d *Device) P4RTListener() net.Listener {
return d.p4rtService.lis
}
// Stop stops the listening services.
// If error is not nil, it will contain why the server failed.
func (d *Device) Stop() error {
klog.Info("Stopping server")
select {
case <-d.stopped:
klog.Infof("Server already stopped: %v", d.errs)
default:
d.stop()
}
d.errsMu.Lock()
defer d.errsMu.Unlock()
if err := d.gnmiServer.StopReconcilers(context.Background()); err != nil {
d.errs = append(d.errs, err)
}
if len(d.errs) == 0 {
return nil
}
return fmt.Errorf("%v", d.errs)
}
// GNMI returns the gNMI server implementation.
func (d *Device) GNMI() *fgnmi.Server {
return d.gnmiServer
}
// GNSI returns the gNSI server implementation.
func (d *Device) GNSI() *fgnsi.Server {
return d.gnsiServer
}
// Dataplane returns the dataplane server implementation.
func (d *Device) Dataplane() *dataplane.Dataplane {
return d.dplaneServer
}
func (d *Device) startServer() {
d.stopped = make(chan struct{})
services := map[string]*gRPCService{
"gNMI/gNOI/gNSI": d.gnmignoignsiService,
"gRIBI": d.gribiService,
"P4RT": d.p4rtService,
}
for svcName, svc := range services {
// Capture loop variables by value instead of reference.
svcName, svc := svcName, svc
go func() {
if err := svc.s.Serve(svc.lis); err != nil {
d.errsMu.Lock()
d.errs = append(d.errs, err)
d.errsMu.Unlock()
}
d.errsMu.Lock()
klog.Infof("%s server stopped: %v", svcName, d.errs)
d.errsMu.Unlock()
close(svc.stopped)
}()
}
d.stop = func() {
for _, svc := range services {
svc.s.Stop()
}
for _, svc := range services {
<-svc.stopped
}
}
}