-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
482 lines (442 loc) · 15.3 KB
/
config.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
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
package main
import (
"context"
"encoding/json"
"fmt"
"sync"
"github.com/nokia/srlinux-ndk-go/ndk"
log "github.com/sirupsen/logrus"
)
const (
operUp = "OPER_STATE_up"
operDown = "OPER_STATE_down"
operStarting = "OPER_STATE_starting"
operFailed = "OPER_STATE_failed"
//
adminEnable = "ADMIN_STATE_enable"
adminDisable = "ADMIN_STATE_disable"
// app configuration paths
grpcTunnelPath = ".system.grpc_tunnel"
destinationPath = ".system.grpc_tunnel.destination"
tunnelPath = ".system.grpc_tunnel.tunnel"
tunnelDestinationPath = ".system.grpc_tunnel.tunnel.destination"
tunnelTargetPath = ".system.grpc_tunnel.tunnel.target"
)
type config struct {
m *sync.Mutex
trx []*ndk.ConfigNotification
//
app *appConfig
//
sysInfo systemInfo
username string
password string
}
type appConfig struct {
AdminState string `json:"admin_state,omitempty"`
OperState string `json:"oper_state,omitempty"`
//
Destination map[string]*destination `json:"-"`
Tunnel map[string]*tunnelCfg `json:"-"`
}
type destination struct {
Destination struct {
Address stringValue `json:"address,omitempty"`
Port stringValue `json:"port,omitempty"`
Description stringValue `json:"description,omitempty"`
NoTLS boolValue `json:"no_tls,omitempty"`
TLSProfile stringValue `json:"tls_profile,omitempty"`
NetworkInstance stringValue `json:"network_instance,omitempty"`
} `json:"destination,omitempty"`
}
type tunnelCfg struct {
Tunnel struct {
AdminState string `json:"admin_state,omitempty"`
OperState string `json:"oper_state,omitempty"`
OperStateDownReason stringValue `json:"oper_state_down_reason,omitempty"`
Description stringValue `json:"description,omitempty"`
Target map[string]*target `json:"-"`
Destination map[string]*destinationState `json:"-"`
} `json:"tunnel,omitempty"`
}
type destinationState struct {
OperState string `json:"oper_state,omitempty"`
OperStateDownReason stringValue `json:"oper_state_down_reason,omitempty"`
Target map[string]*targetState `json:"-"`
}
type targetState struct {
Target struct {
OperState string `json:"oper_state,omitempty"`
OperStateDownReason stringValue `json:"oper_state_down_reason,omitempty"`
} `json:"target,omitempty"`
}
type target struct {
Target struct {
LocalAddress stringValue `json:"local_address,omitempty"`
ID struct {
NodeName *boolValue `json:"node_name,omitempty"`
UserAgent *boolValue `json:"user_agent,omitempty"`
MacAddress *boolValue `json:"mac_address,omitempty"`
Custom *stringValue `json:"custom,omitempty"`
} `json:"id,omitempty"`
Type struct {
GrpcServer *boolValue `json:"grpc_server,omitempty"`
SSHServer *boolValue `json:"ssh_server,omitempty"`
Custom *stringValue `json:"custom,omitempty"`
} `json:"type,omitempty"`
} `json:"target,omitempty"`
}
type stringValue struct {
Value string `json:"value,omitempty"`
}
type boolValue struct {
Value bool `json:"value,omitempty"`
}
func newConfig() *config {
return &config{
m: new(sync.Mutex),
trx: make([]*ndk.ConfigNotification, 0),
app: &appConfig{
Destination: make(map[string]*destination),
Tunnel: make(map[string]*tunnelCfg),
},
}
}
func (a *app) handleConfigEvent(ctx context.Context, cfg *ndk.ConfigNotification) {
jsPath := cfg.GetKey().GetJsPath()
// collect non commit.end config notifications
if jsPath != ".commit.end" && cfg != nil {
a.config.trx = append(a.config.trx, cfg)
return
}
a.config.m.Lock()
defer a.config.m.Unlock()
// when path is ".commit.end", handle the stored config notifications
for _, txCfg := range a.config.trx {
switch txCfg.GetKey().GetJsPath() {
case grpcTunnelPath:
a.handleGrpcTunnel(ctx, txCfg)
case destinationPath:
a.handleDestination(ctx, txCfg)
case tunnelPath:
a.handleTunnel(ctx, txCfg)
case tunnelTargetPath:
a.handleTunnelTarget(ctx, txCfg)
case tunnelDestinationPath:
a.handleTunnelDestination(ctx, txCfg)
default:
log.Errorf("received unexpected config path %q", txCfg.GetKey().GetJsPath())
}
}
// reset transaction array
a.config.trx = make([]*ndk.ConfigNotification, 0)
}
func (a *app) handleNwInstCfg(ctx context.Context, cfg *ndk.NetworkInstanceNotification) {
//log.Debugf("received network instance notification: %+v", cfg)
}
// ".system.grpc_tunnel" handlers
func (a *app) handleGrpcTunnel(ctx context.Context, txCfg *ndk.ConfigNotification) {
switch txCfg.GetOp() {
case ndk.SdkMgrOperation_Create:
log.Infof("Create: .system.grpc_tunnel: %+v", txCfg)
a.handleGrpcTunnelCreate(ctx, txCfg.GetData())
case ndk.SdkMgrOperation_Update:
log.Infof("Update: .system.grpc_tunnel: %+v", txCfg)
a.handleGrpcTunnelChange(ctx, txCfg.GetData())
case ndk.SdkMgrOperation_Delete:
log.Infof("Delete: .system.grpc_tunnel: %+v", txCfg)
a.handleGrpcTunnelDelete(ctx)
}
}
func (a *app) handleGrpcTunnelCreate(ctx context.Context, cfgData *ndk.ConfigData) {
newAppCfg := new(appConfig)
err := json.Unmarshal([]byte(cfgData.GetJson()), newAppCfg)
if err != nil {
log.Errorf("failed to unmarshal path %q config %+v", grpcTunnelPath, cfgData)
return
}
a.config.app = newAppCfg
a.config.app.OperState = operDown
a.updateRootLevelTelemetry(a.config.app)
}
func (a *app) handleGrpcTunnelChange(ctx context.Context, cfgData *ndk.ConfigData) {
// unmarshal changed config
newAppCfg := new(appConfig)
err := json.Unmarshal([]byte(cfgData.GetJson()), newAppCfg)
if err != nil {
log.Errorf("failed to unmarshal path %q config %+v", grpcTunnelPath, cfgData)
return
}
a.config.app.AdminState = newAppCfg.AdminState
// apply state change
switch {
case a.config.app.AdminState == adminDisable && a.config.app.OperState == operUp:
// stop all tunnels
a.stopAll(ctx)
a.config.app.OperState = operDown
case a.config.app.AdminState == adminEnable && a.config.app.OperState != operUp:
// start all tunnels
a.startAll(ctx)
a.config.app.OperState = operUp
}
// update telemetry
a.updateRootLevelTelemetry(a.config.app)
}
func (a *app) handleGrpcTunnelDelete(ctx context.Context) {
a.stopAll(ctx)
a.config.app = &appConfig{
AdminState: adminDisable,
OperState: operDown,
Destination: make(map[string]*destination),
Tunnel: make(map[string]*tunnelCfg),
}
a.updateRootLevelTelemetry(a.config.app)
a.m.Lock()
a.tunnelClients = make(map[string]map[string]*tunnelDestinationClient)
a.m.Unlock()
}
// ".system.grpc_tunnel.destination" handlers
func (a *app) handleDestination(ctx context.Context, txCfg *ndk.ConfigNotification) {
keys := txCfg.GetKey().GetKeys()
if len(keys) != 1 {
log.Errorf("unexpected number of keys in path %q: %v: %+v", destinationPath, keys, txCfg)
return
}
dName := keys[0]
switch txCfg.GetOp() {
case ndk.SdkMgrOperation_Create:
a.handleDestinationCreate(ctx, dName, txCfg.GetData())
case ndk.SdkMgrOperation_Update:
a.handleDestinationChange(ctx, dName, txCfg.GetData())
case ndk.SdkMgrOperation_Delete:
a.handleDestinationDelete(ctx, dName)
}
}
func (a *app) handleDestinationCreate(ctx context.Context, dName string, cfgData *ndk.ConfigData) {
newDG := new(destination)
err := json.Unmarshal([]byte(cfgData.GetJson()), newDG)
if err != nil {
log.Errorf("failed to unmarshal path %q config %+v", destinationPath, cfgData)
return
}
if a.config.app.Destination == nil {
a.config.app.Destination = make(map[string]*destination)
}
a.config.app.Destination[dName] = newDG
a.updateDestinationTelemetry(dName, newDG)
}
func (a *app) handleDestinationChange(ctx context.Context, dName string, cfgData *ndk.ConfigData) {
newDest := new(destination)
err := json.Unmarshal([]byte(cfgData.GetJson()), newDest)
if err != nil {
log.Errorf("failed to unmarshal path %q config %+v", destinationPath, cfgData)
return
}
if a.config.app.Destination == nil {
a.config.app.Destination = make(map[string]*destination)
}
a.config.app.Destination[dName] = newDest
a.updateDestinationTelemetry(dName, newDest)
}
func (a *app) handleDestinationDelete(ctx context.Context, dName string) {
delete(a.config.app.Destination, dName)
a.deleteDestinationTelemetry(ctx, dName)
}
// ".system.grpc_tunnel.tunnel" handlers
func (a *app) handleTunnel(ctx context.Context, txCfg *ndk.ConfigNotification) {
keys := txCfg.GetKey().GetKeys()
if len(keys) != 1 {
log.Errorf("unexpected number of keys in path %q: %v: %+v", destinationPath, keys, txCfg)
return
}
tn := keys[0]
switch txCfg.GetOp() {
case ndk.SdkMgrOperation_Create:
a.handleTunnelCreate(ctx, tn, txCfg.GetData())
case ndk.SdkMgrOperation_Update:
a.handleTunnelChange(ctx, tn, txCfg.GetData())
case ndk.SdkMgrOperation_Delete:
a.handleTunnelDelete(ctx, tn)
}
}
func (a *app) handleTunnelCreate(ctx context.Context, tn string, cfgData *ndk.ConfigData) {
newTunnel := new(tunnelCfg)
err := json.Unmarshal([]byte(cfgData.GetJson()), newTunnel)
if err != nil {
log.Errorf("failed to unmarshal path %q config %+v", tunnelPath, cfgData)
return
}
if a.config.app.Tunnel == nil {
a.config.app.Tunnel = make(map[string]*tunnelCfg)
}
newTunnel.Tunnel.OperState = operUp
if newTunnel.Tunnel.AdminState == adminDisable {
newTunnel.Tunnel.OperState = operDown
}
a.config.app.Tunnel[tn] = newTunnel
a.updateTunnelTelemetry(tn, newTunnel)
}
func (a *app) handleTunnelChange(ctx context.Context, tn string, cfgData *ndk.ConfigData) {
newTunnel := new(tunnelCfg)
err := json.Unmarshal([]byte(cfgData.GetJson()), newTunnel)
if err != nil {
log.Errorf("failed to unmarshal path %q config %+v", tunnelPath, cfgData)
return
}
if a.config.app.Tunnel == nil {
a.config.app.Tunnel = make(map[string]*tunnelCfg)
}
if tun, ok := a.config.app.Tunnel[tn]; ok {
newTunnel.Tunnel.Target = tun.Tunnel.Target
newTunnel.Tunnel.Destination = tun.Tunnel.Destination
}
log.Infof("tunnel %s, new admin-state=%s, oper-state=%s", tn, newTunnel.Tunnel.AdminState, a.config.app.Tunnel[tn].Tunnel.OperState)
switch {
case newTunnel.Tunnel.AdminState == adminEnable && a.config.app.Tunnel[tn].Tunnel.OperState != operUp:
err := a.startTunnel(ctx, tn, newTunnel)
if err != nil {
log.Errorf("failed to start tunnel %s: %v", tn, err)
}
newTunnel.Tunnel.OperState = operUp
newTunnel.Tunnel.OperStateDownReason.Value = ""
case newTunnel.Tunnel.AdminState == adminDisable && a.config.app.Tunnel[tn].Tunnel.OperState != operDown:
a.stopTunnel(ctx, tn)
newTunnel.Tunnel.OperState = operDown
newTunnel.Tunnel.OperStateDownReason = stringValue{
Value: "admin down",
}
}
a.config.app.Tunnel[tn] = newTunnel
a.updateTunnelTelemetry(tn, newTunnel)
}
func (a *app) handleTunnelDelete(ctx context.Context, tn string) {
// stop all destinations of tunnel
a.stopTunnel(ctx, tn)
delete(a.config.app.Tunnel, tn)
a.deleteTunnelTelemetry(ctx, tn)
}
// ".system.grpc_tunnel.tunnel.destination" handlers
func (a *app) handleTunnelDestination(ctx context.Context, txCfg *ndk.ConfigNotification) {
keys := txCfg.GetKey().GetKeys()
if len(keys) != 2 {
log.Errorf("unexpected number of keys in path %q: %v: %+v", tunnelDestinationPath, keys, txCfg)
return
}
tn := keys[0]
dn := keys[1]
switch txCfg.GetOp() {
case ndk.SdkMgrOperation_Create:
a.handleTunnelDestinationCreate(ctx, tn, dn, txCfg.GetData())
case ndk.SdkMgrOperation_Update:
a.handleTunnelDestinationChange(ctx, tn, dn, txCfg.GetData())
case ndk.SdkMgrOperation_Delete:
a.handleTunnelDestinationDelete(ctx, tn, dn)
}
}
func (a *app) handleTunnelDestinationCreate(ctx context.Context, tn, dn string, cfgData *ndk.ConfigData) {
newDstState := new(destinationState)
err := json.Unmarshal([]byte(cfgData.GetJson()), newDstState)
if err != nil {
log.Errorf("failed to unmarshal path %q config %+v", tunnelDestinationPath, cfgData)
return
}
if _, ok := a.config.app.Tunnel[tn]; !ok {
a.config.app.Tunnel[tn] = new(tunnelCfg)
}
if a.config.app.Tunnel[tn].Tunnel.Destination == nil {
a.config.app.Tunnel[tn].Tunnel.Destination = make(map[string]*destinationState)
}
tun := a.config.app.Tunnel[tn]
tun.Tunnel.Destination[dn] = newDstState
dest := a.config.app.Destination[dn]
a.updateTunnelDestinationTelemetry(tn, dn, newDstState)
if tun.Tunnel.AdminState == adminEnable {
go a.startTunnelDestination(ctx, tn, dn, tun, dest, newDstState)
}
}
// won't happen until there is config under .grpc_tunnel.tunnel.destination
func (a *app) handleTunnelDestinationChange(ctx context.Context, tn, dn string, cfgData *ndk.ConfigData) {
}
func (a *app) handleTunnelDestinationDelete(ctx context.Context, tn, dn string) {
a.stopTunnelDestination(ctx, tn, dn)
delete(a.config.app.Tunnel[tn].Tunnel.Destination, dn)
a.deleteTunnelDestinationTelemetry(tn, dn)
}
// ".system.grpc_tunnel.tunnel.target" handlers
func (a *app) handleTunnelTarget(ctx context.Context, txCfg *ndk.ConfigNotification) {
fmt.Printf("handler config: %v\n", txCfg.GetOp())
keys := txCfg.GetKey().GetKeys()
if len(keys) != 2 {
log.Errorf("unexpected number of keys in path %q: %v: %+v", destinationPath, keys, txCfg)
return
}
tn := keys[0]
tg := keys[1]
switch txCfg.GetOp() {
case ndk.SdkMgrOperation_Create:
a.handleTunnelTargetCreate(ctx, tn, tg, txCfg.GetData())
case ndk.SdkMgrOperation_Update:
a.handleTunnelTargetChange(ctx, tn, tg, txCfg.GetData())
case ndk.SdkMgrOperation_Delete:
a.handleTunnelTargetDelete(ctx, tn, tg)
}
}
func (a *app) handleTunnelTargetCreate(ctx context.Context, tn, tg string, cfgData *ndk.ConfigData) {
newTarget := new(target)
err := json.Unmarshal([]byte(cfgData.GetJson()), newTarget)
if err != nil {
log.Errorf("failed to unmarshal path %q config %+v", tunnelTargetPath, cfgData)
return
}
if _, ok := a.config.app.Tunnel[tn]; !ok {
a.config.app.Tunnel[tn] = new(tunnelCfg)
}
if a.config.app.Tunnel[tn].Tunnel.Target == nil {
a.config.app.Tunnel[tn].Tunnel.Target = make(map[string]*target)
}
if _, ok := a.config.app.Tunnel[tn]; ok {
a.config.app.Tunnel[tn].Tunnel.Target[tg] = newTarget
for dn, dest := range a.config.app.Tunnel[tn].Tunnel.Destination {
if ttd, ok := a.tunnelClients[tn][dn]; ok {
a.startTunnelHandlerDestination(ctx, tn, tg, newTarget, dn, dest, ttd.client)
}
}
}
a.updateTunnelTargetTelemetry(tn, tg, newTarget)
}
func (a *app) handleTunnelTargetChange(ctx context.Context, tn, tg string, cfgData *ndk.ConfigData) {
newTarget := new(target)
err := json.Unmarshal([]byte(cfgData.GetJson()), newTarget)
if err != nil {
log.Errorf("failed to unmarshal path %q config %+v", tunnelTargetPath, cfgData)
return
}
if a.config.app.Tunnel[tn].Tunnel.Target == nil {
a.config.app.Tunnel[tn].Tunnel.Target = make(map[string]*target)
}
if tun, ok := a.config.app.Tunnel[tn]; ok {
tun.Tunnel.Target[tg] = newTarget
for dn, dest := range tun.Tunnel.Destination {
if tdc, ok := a.tunnelClients[tn][dn]; ok {
// stop target
a.stopTunnelHandlerDestination(ctx, tn, tg, dn, dest, tdc)
delete(tdc.targets, tg)
// start again
a.startTunnelHandlerDestination(ctx, tn, tg, newTarget, dn, dest, tdc.client)
}
}
}
a.updateTunnelTargetTelemetry(tn, tg, newTarget)
}
func (a *app) handleTunnelTargetDelete(ctx context.Context, tn, tg string) {
if tun, ok := a.config.app.Tunnel[tn]; ok {
for dn, dest := range tun.Tunnel.Destination {
if tdc, ok := a.tunnelClients[tn][dn]; ok {
a.stopTunnelHandlerDestination(ctx, tn, tg, dn, dest, tdc)
delete(tdc.targets, tg)
}
}
}
delete(a.config.app.Tunnel[tn].Tunnel.Target, tg)
a.deleteTunnelTargetTelemetry(tn, tg)
}