-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathtypes.go
474 lines (425 loc) · 12.7 KB
/
types.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
// Copyright (c) 2017 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package types
import (
"fmt"
"time"
"github.com/google/go-cmp/cmp"
"github.com/lf-edge/eve-api/go/info"
"github.com/lf-edge/eve/pkg/pillar/base"
"github.com/lf-edge/eve/pkg/pillar/objtonum"
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus" // OK for logrus.Fatal
)
// SwState started with enum names from OMA-TS-LWM2M_SwMgmt-V1_0-20151201-C
// but now has many additions.
// They are in order of progression
// We map this to info.ZSwState
type SwState uint8
const (
// INITIAL is 100 to be able to tell any confusion with ZSwState
INITIAL SwState = iota + 100 // Initial value
RESOLVING_TAG // Resolving an image tag
RESOLVED_TAG // Tag has been resolved or resolution failed
DOWNLOADING
DOWNLOADED
VERIFYING
VERIFIED
LOADING
LOADED
CREATING_VOLUME // Volume create in progress
CREATED_VOLUME // Volume create done or failed
INSTALLED // Available to be activated
AWAITNETWORKINSTANCE
START_DELAYED // Domain start is delayed to due to StartDelay being honored.
BOOTING
RUNNING
PAUSING
PAUSED
HALTING // being halted
HALTED
BROKEN // BROKEN means domain is still alive, but its device model has failed
UNKNOWN // UNKNOWN means state of the domain can't be determined
// PENDING to start
PENDING
// SCHEDULING waiting to be scheduled
SCHEDULING
// FAILED to start
FAILED
MAXSTATE
)
// String returns the string name
func (state SwState) String() string {
switch state {
case INITIAL:
return "INITIAL"
case RESOLVING_TAG:
return "RESOLVING_TAG"
case RESOLVED_TAG:
return "RESOLVED_TAG"
case DOWNLOADING:
return "DOWNLOADING"
case DOWNLOADED:
return "DOWNLOADED"
case VERIFYING:
return "VERIFYING"
case VERIFIED:
return "VERIFIED"
case LOADING:
return "LOADING"
case LOADED:
return "LOADED"
case CREATING_VOLUME:
return "CREATING_VOLUME"
case CREATED_VOLUME:
return "CREATED_VOLUME"
case INSTALLED:
return "INSTALLED"
case AWAITNETWORKINSTANCE:
return "AWAITNETWORKINSTANCE"
case BOOTING:
return "BOOTING"
case RUNNING:
return "RUNNING"
case PAUSING:
return "PAUSING"
case PAUSED:
return "PAUSED"
case HALTING:
return "HALTING"
case HALTED:
return "HALTED"
case PENDING:
return "PENDING"
case FAILED:
return "FAILED"
case SCHEDULING:
return "SCHEDULING"
case BROKEN:
return "BROKEN"
case START_DELAYED:
return "START_DELAYED"
case UNKNOWN:
return "UNKNOWN"
default:
return fmt.Sprintf("Unknown state %d", state)
}
}
// ZSwState returns different numbers and in some cases mapped many to one
func (state SwState) ZSwState() info.ZSwState {
switch state {
case 0:
return 0
case INITIAL:
return info.ZSwState_INITIAL
case RESOLVING_TAG:
return info.ZSwState_RESOLVING_TAG
case RESOLVED_TAG:
return info.ZSwState_RESOLVED_TAG
case DOWNLOADING:
return info.ZSwState_DOWNLOAD_STARTED
case DOWNLOADED:
return info.ZSwState_DOWNLOADED
case VERIFYING:
return info.ZSwState_VERIFYING
case VERIFIED:
return info.ZSwState_VERIFIED
case LOADING:
return info.ZSwState_LOADING
case LOADED:
// TBD return info.ZSwState_LOADED
return info.ZSwState_DELIVERED
case CREATING_VOLUME:
return info.ZSwState_CREATING_VOLUME
case CREATED_VOLUME:
return info.ZSwState_CREATED_VOLUME
case INSTALLED:
return info.ZSwState_INSTALLED
case AWAITNETWORKINSTANCE:
return info.ZSwState_AWAITNETWORKINSTANCE
case BOOTING:
return info.ZSwState_BOOTING
case RUNNING:
return info.ZSwState_RUNNING
// for now we're treating PAUSING as a subset of RUNNING
// simply because controllers don't support resumable
// paused tasks yet
case PAUSING:
return info.ZSwState_RUNNING
// for now we're treating PAUSED as a subset
// of INSTALLED simply because controllers don't
// support resumable paused tasks just yet (see
// how PAUSING maps to RUNNING below)
case PAUSED:
return info.ZSwState_INSTALLED
case HALTING:
return info.ZSwState_HALTING
case HALTED:
return info.ZSwState_HALTED
// we map BROKEN to HALTING to indicate that EVE has an active
// role in reaping BROKEN domains and transitioning them to
// a final HALTED state
case BROKEN:
return info.ZSwState_HALTING
case START_DELAYED:
return info.ZSwState_START_DELAYED
case FAILED:
return info.ZSwState_ERROR
case PENDING:
return info.ZSwState_PENDING
case SCHEDULING:
return info.ZSwState_SCHEDULING
// If we ever see UNKNOWN we return RUNNING assuming the state will change to something
// known soon.
case UNKNOWN:
return info.ZSwState_RUNNING
default:
logrus.Fatalf("Unknown state %d", state)
}
return info.ZSwState_INITIAL
}
// UuidToNumKey is used as a reference to an object with an allocated number stored
// inside UuidToNum.
type UuidToNumKey struct {
UUID uuid.UUID
}
// Key returns string representation of the key.
func (k UuidToNumKey) Key() string {
return k.UUID.String()
}
// UuidToNum is used to store number allocated for an object with UUID.
// Currently used for appNum and bridgeNum (number types).
type UuidToNum struct {
UuidToNumKey
Number int
NumType string
CreateTime time.Time
LastUseTime time.Time
InUse bool
}
// New is used by objtonum.ObjNumPublisher.
func (info *UuidToNum) New(objKey objtonum.ObjKey) objtonum.ObjNumContainer {
key, ok := objKey.(UuidToNumKey)
if !ok {
panic(fmt.Sprintf("invalid key type: %T vs. expected UuidToNumKey", objKey))
}
return &UuidToNum{
UuidToNumKey: key,
LastUseTime: time.Now(),
CreateTime: time.Now(),
}
}
// GetKey is used by objtonum.ObjNumPublisher.
func (info *UuidToNum) GetKey() objtonum.ObjKey {
return info.UuidToNumKey
}
// SetNumber is used by objtonum.ObjNumPublisher.
func (info *UuidToNum) SetNumber(number int, numberType string) {
info.Number = number
info.NumType = numberType
info.LastUseTime = time.Now()
}
// GetNumber is used by objtonum.ObjNumPublisher.
func (info *UuidToNum) GetNumber() (number int, numberType string) {
return info.Number, info.NumType
}
// GetTimestamps is used by objtonum.ObjNumPublisher.
func (info *UuidToNum) GetTimestamps() (createdAt time.Time, lastUpdatedAt time.Time) {
return info.CreateTime, info.LastUseTime
}
// SetReservedOnly is used by objtonum.ObjNumPublisher.
func (info *UuidToNum) SetReservedOnly(reservedOnly bool) {
info.InUse = !reservedOnly
}
// IsReservedOnly is used by objtonum.ObjNumPublisher.
func (info *UuidToNum) IsReservedOnly() bool {
return !info.InUse
}
// LogCreate :
func (info UuidToNum) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.UUIDToNumLogType, "",
info.UUID, info.LogKey())
if logObject == nil {
return
}
logObject.Noticef("UuidToNum info create")
}
// LogModify :
func (info UuidToNum) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.EnsureLogObject(logBase, base.UUIDToNumLogType, "",
info.UUID, info.LogKey())
oldInfo, ok := old.(UuidToNum)
if !ok {
logObject.Clone().Fatalf("LogModify: Old object interface passed is not of UuidToNum type")
}
// XXX remove?
logObject.CloneAndAddField("diff", cmp.Diff(oldInfo, info)).
Noticef("UuidToNum info modify")
}
// LogDelete :
func (info UuidToNum) LogDelete(logBase *base.LogObject) {
logObject := base.EnsureLogObject(logBase, base.UUIDToNumLogType, "",
info.UUID, info.LogKey())
logObject.Noticef("UuidToNum info delete")
base.DeleteLogObject(logBase, info.LogKey())
}
// LogKey :
func (info UuidToNum) LogKey() string {
return string(base.UUIDToNumLogType) + "-" + info.Key()
}
// Use this for booleans which have a none/dontcare/notset value
type TriState uint8
const (
TS_NONE TriState = iota
TS_DISABLED
TS_ENABLED
)
func ParseTriState(value string) (TriState, error) {
var ts TriState
switch value {
case "none":
ts = TS_NONE
case "enabled", "enable", "on":
ts = TS_ENABLED
case "disabled", "disable", "off":
ts = TS_DISABLED
default:
err := fmt.Errorf("bad value: %s", value)
return ts, err
}
return ts, nil
}
// FormatTriState - return string format of TriState
func FormatTriState(state TriState) string {
switch state {
case TS_NONE:
return "none"
case TS_ENABLED:
return "enabled"
case TS_DISABLED:
return "disabled"
default:
logrus.Fatalf("Invalid TriState Value: %v", state)
}
return ""
}
// UEvent stores information about uevent comes from kernel
type UEvent struct {
Action string
Obj string
Env map[string]string
}
// AppInterfaceKey uniquely references application interface.
type AppInterfaceKey struct {
// NetInstID : UUID of the network instance to which the interface is connected.
// Previously it was called BaseID, which was rather unspecific and a bit confusing.
// In order to not break EVE upgrades, the json tag is set to use this old field name.
NetInstID uuid.UUID `json:"BaseID"`
// AppID : UUID of the application
AppID uuid.UUID
// IfIdx : interface index among interfaces of this app on this network instance.
// First has index 0, next 1, etc.
IfIdx uint32
}
// Key returns string representation of the key.
func (info AppInterfaceKey) Key() string {
return fmt.Sprintf("%s-%s-%d",
info.NetInstID.String(), info.AppID.String(), info.IfIdx)
}
// AppInterfaceToNum is used to store number assigned to an application interface.
// The number is unique only within the network instance where the interface
// is connected to. It is used to generate an IP address for the interface.
type AppInterfaceToNum = UUIDPairAndIfIdxToNum
// UUIDPairAndIfIdxToNum is a legacy name for a structure holding number allocated
// to an application interface. Because this name is rather unspecific and quite
// clumsy, we now use the AppInterfaceToNum alias instead.
// However, in order to not break EVE upgrades (by changing pubsub topic name),
// we continue using this name for the underlying structure.
// Do NOT use this type name anywhere else in pillar anymore, use AppInterfaceToNum
// instead.
type UUIDPairAndIfIdxToNum struct {
AppInterfaceKey
Number int
NumType string
CreateTime time.Time
LastUseTime time.Time
InUse bool
}
// New is used by objtonum.ObjNumPublisher.
func (info *AppInterfaceToNum) New(objKey objtonum.ObjKey) objtonum.ObjNumContainer {
key, ok := objKey.(AppInterfaceKey)
if !ok {
panic(fmt.Sprintf("invalid key type: %T vs. expected AppInterfaceKey", objKey))
}
return &AppInterfaceToNum{
AppInterfaceKey: key,
LastUseTime: time.Now(),
CreateTime: time.Now(),
}
}
// GetKey is used by objtonum.ObjNumPublisher.
func (info *AppInterfaceToNum) GetKey() objtonum.ObjKey {
return info.AppInterfaceKey
}
// SetNumber is used by objtonum.ObjNumPublisher.
func (info *AppInterfaceToNum) SetNumber(number int, numberType string) {
info.Number = number
info.NumType = numberType
info.LastUseTime = time.Now()
}
// GetNumber is used by objtonum.ObjNumPublisher.
func (info *AppInterfaceToNum) GetNumber() (number int, numberType string) {
return info.Number, info.NumType
}
// GetTimestamps is used by objtonum.ObjNumPublisher.
func (info *AppInterfaceToNum) GetTimestamps() (createdAt time.Time, lastUpdatedAt time.Time) {
return info.CreateTime, info.LastUseTime
}
// SetReservedOnly is used by objtonum.ObjNumPublisher.
func (info *AppInterfaceToNum) SetReservedOnly(reservedOnly bool) {
info.InUse = !reservedOnly
}
// IsReservedOnly is used by objtonum.ObjNumPublisher.
func (info *AppInterfaceToNum) IsReservedOnly() bool {
return !info.InUse
}
// LogCreate :
func (info AppInterfaceToNum) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.AppInterfaceToNumLogType, "",
info.NetInstID, info.LogKey())
if logObject == nil {
return
}
logObject.Noticef("AppInterfaceToNum info create")
}
// LogModify :
func (info AppInterfaceToNum) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.EnsureLogObject(logBase, base.AppInterfaceToNumLogType, "",
info.NetInstID, info.LogKey())
oldInfo, ok := old.(AppInterfaceToNum)
if !ok {
logObject.Clone().Fatalf("LogModify: Old object interface passed is not of AppInterfaceToNum type")
}
// XXX remove?
logObject.CloneAndAddField("diff", cmp.Diff(oldInfo, info)).
Noticef("AppInterfaceToNum info modify")
}
// LogDelete :
func (info AppInterfaceToNum) LogDelete(logBase *base.LogObject) {
logObject := base.EnsureLogObject(logBase, base.AppInterfaceToNumLogType, "",
info.NetInstID, info.LogKey())
logObject.Noticef("AppInterfaceToNum info delete")
base.DeleteLogObject(logBase, info.LogKey())
}
// LogKey :
func (info AppInterfaceToNum) LogKey() string {
return string(base.AppInterfaceToNumLogType) + "-" + info.Key()
}
// UuidsToStrings converts list of uuids to a list of strings
func UuidsToStrings(uuids []uuid.UUID) []string {
list := make([]string, len(uuids))
for i, uuid := range uuids {
list[i] = uuid.String()
}
return list
}