/
inventory.go
370 lines (326 loc) · 9.48 KB
/
inventory.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
// Copyright (c) 2019 Arista Networks, Inc.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package device
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/aristanetworks/cloudvision-go/device/cvclient"
"github.com/aristanetworks/cloudvision-go/log"
"github.com/aristanetworks/cloudvision-go/provider"
"github.com/openconfig/gnmi/proto/gnmi"
"google.golang.org/grpc"
)
const heartbeatInterval = 10 * time.Second
// An Inventory maintains a set of devices.
type Inventory interface {
Add(deviceInfo *Info) error
Delete(key string) error
Get(key string) (*Info, error)
List() []*Info
SetStatus(key string, status ManagedDeviceStatus) error
}
// InventoryOption configures how we create the Inventory.
type InventoryOption func(*inventory)
// deviceConn contains a device and its gNMI/gRPC connections.
type deviceConn struct {
info *Info
ctx context.Context
cancel context.CancelFunc
cvClient cvclient.CVClient
grpcConn *grpc.ClientConn
group sync.WaitGroup
}
// inventory implements the Inventory interface.
type inventory struct {
ctx context.Context
rawGNMIClient gnmi.GNMIClient
grpcConn *grpc.ClientConn
grpcServerAddr string
grpcConnector GRPCConnector // Connector to get gRPC connection
standalone bool
devices map[string]*deviceConn
lock sync.Mutex
clientFactory func(gnmi.GNMIClient, *Info) cvclient.CVClient
}
func (dc *deviceConn) sendPeriodicUpdates() error {
ticker := time.NewTicker(heartbeatInterval)
defer ticker.Stop()
did, _ := dc.info.Device.DeviceID(dc.ctx)
logger := log.Log(dc.info.Device)
wasFailing := false // used to only log once when device is unhealthy and back alive
for {
select {
case <-dc.ctx.Done():
return nil
case <-ticker.C:
alive, err := dc.info.Device.Alive(dc.ctx)
if err == nil && alive {
if wasFailing {
logger.Infof("Device %s is back alive", did)
wasFailing = false
}
if err := dc.cvClient.SendHeartbeat(dc.ctx, alive); err != nil {
// Don't give up if an update fails for some reason.
logger.Infof("Error sending periodic update for device %v: %v",
did, err)
}
} else {
if !wasFailing {
logger.Infof("Device %s is not alive, err: %v", did, err)
wasFailing = true
}
}
}
}
}
func (i *inventory) newDeviceConn(info *Info) (*deviceConn, error) {
dc := &deviceConn{
cvClient: i.clientFactory(i.rawGNMIClient, info),
info: info,
}
// Take any metadata associated with the device context.
if info.Context != nil {
dc.ctx, dc.cancel = context.WithCancel(info.Context)
} else {
dc.ctx, dc.cancel = context.WithCancel(i.ctx)
}
// i.grpcConnector is set,
// only if grpcServerAddr is provided
if i.grpcConnector != nil {
cc := GRPCConnectorConfig{
dc.info.ID,
i.standalone,
}
conn, err := i.grpcConnector.Connect(dc.ctx, i.grpcConn, i.grpcServerAddr, cc)
if err != nil {
return nil, fmt.Errorf("gRPC connection to device %v failed: %w", cc.DeviceID, err)
}
dc.grpcConn = conn
}
return dc, nil
}
func (dc *deviceConn) runProviders() error {
providers, err := dc.info.Device.Providers()
if err != nil {
return err
}
logFileName := dc.info.ID + ".log"
err = log.InitLogging(logFileName, dc.info.Device)
if err != nil {
return fmt.Errorf("Error setting up logging for device %s: %v", dc.info.ID, err)
}
for _, p := range providers {
err = log.InitLogging(logFileName, p)
if err != nil {
return fmt.Errorf("Error setting up logging for provider %#v: %v", p, err)
}
switch pt := p.(type) {
case provider.GNMIProvider:
pt.InitGNMI(dc.cvClient.ForProvider(pt))
case provider.GRPCProvider:
pt.InitGRPC(dc.grpcConn)
default:
return fmt.Errorf("unexpected provider type %T", p)
}
// Start the providers.
dc.group.Add(1)
go func(p provider.Provider) {
err := p.Run(dc.ctx)
if err != nil {
log.Log(p).Errorf("Provider exiting with error %v", err)
}
dc.group.Done()
}(p)
}
return nil
}
// Add adds a device to the inventory, opens up any gNMI connections
// required by the device's providers, and then starts its providers.
func (i *inventory) Add(info *Info) error {
i.lock.Lock()
defer i.lock.Unlock()
if info.ID == "" {
return errors.New("ID in device.Info cannot be empty")
} else if info.Config == nil {
return errors.New("Config in device.Info cannot be empty")
}
if dev, ok := i.devices[info.ID]; ok {
log.Log(info.Device).Debugf("Replacing device %s (type %s)",
info.ID, info.Config.Device)
dev.cancel()
delete(i.devices, info.ID)
}
// Create device connection object.
dc, err := i.newDeviceConn(info)
if err != nil {
return err
}
// Register the device before starting providers. If we can't reach
// the device right now, we should return an error rather than
// considering it added.
if err := dc.cvClient.SendDeviceMetadata(dc.ctx); err != nil {
return fmt.Errorf("Error sending device metadata for device "+
"%q (%s): %w", info.ID, info.Config.Device, err)
}
// Start providers.
if err := dc.runProviders(); err != nil {
return fmt.Errorf("Error starting providers for device %q (%s): %w",
info.ID, info.Config.Device, err)
}
// We're connected to the device, have told CloudVision about the
// device, and are streaming the device's data now, so add the
// device to the inventory.
i.devices[info.ID] = dc
// Send periodic updates of device-level metadata.
if !info.Config.NoStream {
dc.group.Add(1)
go func() {
err := dc.sendPeriodicUpdates()
if err != nil {
log.Log(info.Device).Errorf("Error updating device metadata: %v", err)
}
dc.group.Done()
}()
}
if manager, ok := info.Device.(Manager); ok {
dc.group.Add(1)
go func() {
err := manager.Manage(dc.ctx, i)
if err != nil {
log.Log(info.Device).Errorf("Error in manager.Manage: %v", err)
}
dc.group.Done()
}()
}
log.Log(info.Device).Infof("Added device %q (%s)", info.ID,
info.Config.Device)
return nil
}
func (i *inventory) Delete(key string) error {
i.lock.Lock()
defer i.lock.Unlock()
if key == "" {
return fmt.Errorf("key in inventory.Delete cannot be empty")
}
dc, ok := i.devices[key]
if !ok {
return nil
}
// Cancel the device context and delete the device from the device
// map. We need to make sure this device's providers are finished
// before deleting the device. We also need to make sure Manager device
// has manage go routine closed too.
dc.cancel()
dc.group.Wait()
ctx := i.ctx
if dc.info.Context != nil {
ctx = dc.info.Context
}
// send metadata update for device as removed
// so that it can be removed from CloudVision.
dc.info.Status = StatusRemoved
if err := dc.cvClient.SendDeviceMetadata(ctx); err != nil {
return fmt.Errorf("Error sending device metadata for device "+
"%q (%s): %w", key, dc.info.Status, err)
}
delete(i.devices, key)
log.Log(dc.info.Device).Infof("Deleted device %s", key)
return nil
}
func (i *inventory) Get(key string) (*Info, error) {
i.lock.Lock()
defer i.lock.Unlock()
if key == "" {
return nil, fmt.Errorf("key in inventory.Get cannot be empty")
}
d, ok := i.devices[key]
if !ok {
return nil, fmt.Errorf("Device %s not found", key)
}
return d.info, nil
}
func (i *inventory) List() []*Info {
var ret []*Info
for _, conn := range i.devices {
ret = append(ret, conn.info)
}
return ret
}
func (i *inventory) SetStatus(key string, status ManagedDeviceStatus) error {
i.lock.Lock()
defer i.lock.Unlock()
if key == "" {
return fmt.Errorf("key in inventory.Update cannot be empty")
}
dc, ok := i.devices[key]
if !ok {
return fmt.Errorf("device %s is not present in inventory", key)
}
dc.info.Status = status
if err := dc.cvClient.SendDeviceMetadata(dc.ctx); err != nil {
return fmt.Errorf("error sending device metadata for device "+
"%s: %w", key, err)
}
return nil
}
// WithGNMIClient sets a gNMI client on the Inventory.
func WithGNMIClient(c gnmi.GNMIClient) InventoryOption {
return func(i *inventory) {
i.rawGNMIClient = c
}
}
// WithGRPCConn sets a gRPC connection on the Inventory.
func WithGRPCConn(c *grpc.ClientConn) InventoryOption {
return func(i *inventory) {
i.grpcConn = c
}
}
// WithGRPCServerAddr sets an grpc server address on the Inventory.
func WithGRPCServerAddr(addr string) InventoryOption {
return func(i *inventory) {
i.grpcServerAddr = addr
}
}
// WithGRPCConnector sets a gRPC connector on the Inventory.
func WithGRPCConnector(c GRPCConnector) InventoryOption {
return func(i *inventory) {
i.grpcConnector = c
}
}
// WithStandaloneStatus sets flag to identify if its standalone sensor
func WithStandaloneStatus(standalone bool) InventoryOption {
return func(i *inventory) {
i.standalone = standalone
}
}
// WithClientFactory sets a client factory on the Inventory.
func WithClientFactory(
f func(gnmi.GNMIClient, *Info) cvclient.CVClient) InventoryOption {
return func(i *inventory) {
i.clientFactory = f
}
}
// NewInventoryWithOptions creates an Inventory with the supplied options.
func NewInventoryWithOptions(ctx context.Context,
options ...InventoryOption) Inventory {
inv := &inventory{
ctx: ctx,
devices: make(map[string]*deviceConn),
}
for _, opt := range options {
opt(inv)
}
return inv
}
// NewInventory creates an Inventory.
// Deprecated: Use NewInventoryWithOptions instead.
func NewInventory(ctx context.Context, gnmiClient gnmi.GNMIClient,
clientFactory func(gnmi.GNMIClient, *Info) cvclient.CVClient) Inventory {
return NewInventoryWithOptions(ctx,
WithGNMIClient(gnmiClient),
WithClientFactory(clientFactory))
}