-
Notifications
You must be signed in to change notification settings - Fork 1
/
hmstypes.go
518 lines (454 loc) · 16.6 KB
/
hmstypes.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// MIT License
//
// (C) Copyright [2018-2022] Hewlett Packard Enterprise Development LP
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
package base
import (
"encoding/json"
"strings"
)
// Use HMS-wrapped errors. Subsequent errors will be children of this one.
var e = NewHMSError("hms", "GenericError")
//
// State field used in component, set in response to events by state manager.
// 1.0.0
//
type HMSState string
// Valid state values for components - should refect hardware state
// Enabled/Disabled is a separate boolean field, as the component should
// still have it's actual physical state known and tracked at all times, so
// we know what it is when it is enabled. It also avoids the primary case
// where admins need to modify the state field manually.
//
// NOTE: there will be no state between on and ready. If the managed plane
// software does not have heartbeats, On is as high as it will ever get.
// So "active" is not useful. 'Paused' is not in scope now that the software
// status field exists.
const (
StateUnknown HMSState = "Unknown" // The State is unknown. Appears missing but has not been confirmed as empty.
StateEmpty HMSState = "Empty" // The location is not populated with a component
StatePopulated HMSState = "Populated" // Present (not empty), but no further track can or is being done.
StateOff HMSState = "Off" // Present but powered off
StateOn HMSState = "On" // Powered on. If no heartbeat mechanism is available, it's software state may be unknown.
StateStandby HMSState = "Standby" // No longer Ready and presumed dead. It typically means HB has been lost (w/alert).
StateHalt HMSState = "Halt" // No longer Ready and halted. OS has been gracefully shutdown or panicked (w/ alert).
StateReady HMSState = "Ready" // Both On and Ready to provide its expected services, i.e. used for jobs.
// Retired (actually never used) states:
// StateActive HMSState = "Active" // If level-2 systems without hb monitoring can make a distinction between on and booting/booted.
// StatePaused HMSState = "Paused" // Was in a Ready state, but is temporarily unavailable due to admin action or a transient issue.
)
var ErrHMSStateInvalid = e.NewChild("was not a valid HMS state")
var ErrHMSStateUnsupported = e.NewChild("HMSState value not supported for this operation")
var ErrHMSNeedForce = e.NewChild("operation not allowed and not forced.")
var ErrHMSTypeInvalid = e.NewChild("got HMSTypeInvalid instead of valid type")
var ErrHMSTypeUnsupported = e.NewChild("HMSType value not supported for this operation") // TODO should this be in base?
// For case-insensitive verification and normalization of state strings
var hmsStateMap = map[string]HMSState{
"unknown": StateUnknown,
"empty": StateEmpty,
"populated": StatePopulated,
"off": StateOff,
"on": StateOn,
"standby": StateStandby,
"halt": StateHalt,
"ready": StateReady,
}
func GetHMSStateList() []string {
hmsStateList := []string{}
for _, state := range hmsStateMap {
hmsStateList = append(hmsStateList, state.String())
}
return hmsStateList
}
// Returns the given state string (adjusting any capitalization differences),
// if a valid state is given. Else, return the empty string.
func VerifyNormalizeState(stateStr string) string {
stateLower := strings.ToLower(stateStr)
value, ok := hmsStateMap[stateLower]
if ok != true {
return ""
} else {
return value.String()
}
}
// Specifies valid STARTING states before changing to the indicated state,
// at least without forcing the change, which would normally be a bad idea.
// An empty array means "None without forcing.
var hmsValidStartStatesMap = map[string][]string{
"unknown": []string{}, // Force/HSM only
"empty": []string{}, // Force/HSM only
"populated": []string{}, // Force/HSM only
"off": []string{string(StateOff), string(StateOn), string(StateStandby), string(StateHalt), string(StateReady)},
"on": []string{string(StateOn), string(StateOff), string(StateStandby), string(StateHalt)},
"standby": []string{string(StateStandby), string(StateReady)},
"halt": []string{string(StateHalt), string(StateReady)},
"ready": []string{string(StateReady), string(StateOn), string(StateOff), string(StateStandby), string(StateHalt)}, // Last three are needed (for now) if RF events break.
}
// If ok == true, beforeStates contain valid current states a
// component can be in if it is being transitioned to afterState without
// being forced (either because it is a bad idea, or the state should
// only be set by HSM and not by other software). An empty array means 'None
// without force=true
//
// If ok == false, afterState matched no valid HMS State (case insensitive)
func GetValidStartStates(afterState string) (beforeStates []string, ok bool) {
stateLower := strings.ToLower(afterState)
beforeStates, ok = hmsValidStartStatesMap[stateLower]
return
}
// Same as above, but with force flag. If not found, returns
// ErrHMSStateInvalid. If can only be forced, and force = false,
// error will be ErrHMSNeedForce. Otherwise list of starting states.
// If force = true and no errors, an empty array means no restrictions.
func GetValidStartStateWForce(
afterState string,
force bool,
) (beforeStates []string, err error) {
beforeStates = []string{}
// See if transition is valid.
if force == false {
var ok bool
beforeStates, ok = GetValidStartStates(afterState)
if !ok {
err = ErrHMSStateInvalid
} else if len(beforeStates) == 0 {
err = ErrHMSNeedForce
}
}
return
}
// Check to see if the state is above on (on is the highest we will get
// from Redfish, so these are state set by higher software layers)
func IsPostBootState(stateStr string) bool {
stateLower := strings.ToLower(stateStr)
value, ok := hmsStateMap[stateLower]
if ok != true {
return false
} else {
switch value {
//case StateActive:
// fallthrough
case StateStandby:
fallthrough
case StateHalt:
fallthrough
case StateReady:
return true
//case StatePaused:
// return true
default:
return false
}
}
}
// Allow HMSState to be treated as a standard string type.
func (s HMSState) String() string { return string(s) }
//
// Flag field used in component, set in response to events by state manager.
// 1.0.0
//
type HMSFlag string
// Valid flag values.
const (
FlagUnknown HMSFlag = "Unknown"
FlagOK HMSFlag = "OK" // Functioning properly
FlagWarning HMSFlag = "Warning" // Continues to operate, but has an issue that may require attention.
FlagAlert HMSFlag = "Alert" // No longer operating as expected. The state may also have changed due to error.
FlagLocked HMSFlag = "Locked" // Another service has reserved this component.
)
// For case-insensitive verification and normalization of flag strings
var hmsFlagMap = map[string]HMSFlag{
"unknown": FlagUnknown,
"ok": FlagOK,
"warning": FlagWarning,
"warn": FlagWarning,
"alert": FlagAlert,
"locked": FlagLocked,
}
// Get a list of all valid HMS flags
func GetHMSFlagList() []string {
hmsFlagList := []string{}
for _, flag := range hmsFlagMap {
hmsFlagList = append(hmsFlagList, flag.String())
}
return hmsFlagList
}
// Returns the given flag string (adjusting any capitalization differences),
// if a valid flag was given. Else, return the empty string.
func VerifyNormalizeFlag(flagStr string) string {
flagLower := strings.ToLower(flagStr)
value, ok := hmsFlagMap[flagLower]
if ok != true {
return ""
} else {
return value.String()
}
}
// As above, but if flag is the empty string, then return FlagOK.
// If non-empty and invalid, return the empty string.
func VerifyNormalizeFlagOK(flag string) string {
if flag == "" {
return FlagOK.String()
}
return VerifyNormalizeFlag(flag)
}
// Allow HMSFlag to be treated as a standard string type.
func (f HMSFlag) String() string { return string(f) }
//
// Role of component
// 1.0.0
//
type HMSRole string
// Valid role values.
const (
RoleCompute HMSRole = "Compute"
RoleService HMSRole = "Service"
RoleSystem HMSRole = "System"
RoleApplication HMSRole = "Application"
RoleStorage HMSRole = "Storage"
RoleManagement HMSRole = "Management"
)
// For case-insensitive verification and normalization of role strings
var defaultHMSRoleMap = map[string]string{
"compute": RoleCompute.String(),
"service": RoleService.String(),
"system": RoleSystem.String(),
"application": RoleApplication.String(),
"storage": RoleStorage.String(),
"management": RoleManagement.String(),
}
var hmsRoleMap = defaultHMSRoleMap
// Get a list of all valid HMS roles
func GetHMSRoleList() []string {
hmsRoleList := []string{}
for _, role := range hmsRoleMap {
hmsRoleList = append(hmsRoleList, role)
}
return hmsRoleList
}
// Returns the given role string (adjusting any capitalization differences),
// if a valid role was given. Else, return the empty string.
func VerifyNormalizeRole(roleStr string) string {
roleLower := strings.ToLower(roleStr)
value, ok := hmsRoleMap[roleLower]
if ok != true {
return ""
} else {
return value
}
}
// Allow HMSRole to be treated as a standard string type.
func (r HMSRole) String() string { return string(r) }
//
// SubRole of component
// 1.0.0
//
type HMSSubRole string
// Valid SubRole values.
const (
SubRoleMaster HMSSubRole = "Master"
SubRoleWorker HMSSubRole = "Worker"
SubRoleStorage HMSSubRole = "Storage"
)
// For case-insensitive verification and normalization of SubRole strings
var defaultHMSSubRoleMap = map[string]string{
"master": SubRoleMaster.String(),
"worker": SubRoleWorker.String(),
"storage": SubRoleStorage.String(),
}
var hmsSubRoleMap = defaultHMSSubRoleMap
// Get a list of all valid HMS subroles
func GetHMSSubRoleList() []string {
hmsSubRoleList := []string{}
for _, subrole := range hmsSubRoleMap {
hmsSubRoleList = append(hmsSubRoleList, subrole)
}
return hmsSubRoleList
}
// Returns the given SubRole string (adjusting any capitalization differences),
// if a valid SubRole was given. Else, return the empty string.
func VerifyNormalizeSubRole(subRoleStr string) string {
subRoleLower := strings.ToLower(subRoleStr)
value, ok := hmsSubRoleMap[subRoleLower]
if ok != true {
return ""
} else {
return value
}
}
// Allow HMSSubRole to be treated as a standard string type.
func (r HMSSubRole) String() string { return string(r) }
//
// HMSNetType - type of high speed network
// 1.0.0
//
type HMSNetType string
const (
NetSling HMSNetType = "Sling"
NetInfiniband HMSNetType = "Infiniband"
NetEthernet HMSNetType = "Ethernet"
NetOEM HMSNetType = "OEM" // Placeholder for non-slingshot
NetNone HMSNetType = "None"
)
// For case-insensitive verification and normalization of HSN network types
var hmsNetTypeMap = map[string]HMSNetType{
"sling": NetSling,
"infiniband": NetInfiniband,
"ethernet": NetEthernet,
"oem": NetOEM,
"none": NetNone,
}
// Get a list of all valid HMS NetTypes
func GetHMSNetTypeList() []string {
hmsNetTypeList := []string{}
for _, netType := range hmsNetTypeMap {
hmsNetTypeList = append(hmsNetTypeList, netType.String())
}
return hmsNetTypeList
}
// Returns the given net type string (adjusting any capitalization differences),
// if a valid netType was given. Else, return the empty string.
func VerifyNormalizeNetType(netTypeStr string) string {
netTypeLower := strings.ToLower(netTypeStr)
value, ok := hmsNetTypeMap[netTypeLower]
if ok != true {
return ""
} else {
return value.String()
}
}
// Allow HMSNetType to be treated as a standard string type.
func (r HMSNetType) String() string { return string(r) }
//
// HMSArch - binary type needed for component
// 1.0.0
//
type HMSArch string
const (
ArchX86 HMSArch = "X86"
ArchARM HMSArch = "ARM"
ArchUnknown HMSArch = "UNKNOWN"
ArchOther HMSArch = "Other"
)
// For case-insensitive verification and normalization of HSN network types
var hmsArchMap = map[string]HMSArch{
"x86": ArchX86,
"arm": ArchARM,
"unknown": ArchUnknown,
"other": ArchOther,
}
// Get a list of all valid HMS Arch
func GetHMSArchList() []string {
hmsArchList := []string{}
for _, arch := range hmsArchMap {
hmsArchList = append(hmsArchList, arch.String())
}
return hmsArchList
}
// Returns the given arch string (adjusting any capitalization differences),
// if a valid arch was given. Else, return the empty string.
func VerifyNormalizeArch(archStr string) string {
archLower := strings.ToLower(archStr)
value, ok := hmsArchMap[archLower]
if ok != true {
return ""
} else {
return value.String()
}
}
// Allow HMSArch to be treated as a standard string type.
func (r HMSArch) String() string { return string(r) }
//
// HMSClass - Physical hardware profile
// 1.0.0
//
type HMSClass string
const (
ClassRiver HMSClass = "River"
ClassMountain HMSClass = "Mountain"
ClassHill HMSClass = "Hill"
)
// For case-insensitive verification and normalization of HMS Class
var hmsClassMap = map[string]HMSClass{
"river": ClassRiver,
"mountain": ClassMountain,
"hill": ClassHill,
}
// Get a list of all valid HMS Class
func GetHMSClassList() []string {
hmsClassList := []string{}
for _, class := range hmsClassMap {
hmsClassList = append(hmsClassList, class.String())
}
return hmsClassList
}
// Returns the given class string (adjusting any capitalization differences),
// if a valid class was given. Else, return the empty string.
func VerifyNormalizeClass(classStr string) string {
classLower := strings.ToLower(classStr)
value, ok := hmsClassMap[classLower]
if ok != true {
return ""
} else {
return value.String()
}
}
// Allow HMSClass to be treated as a standard string type.
func (r HMSClass) String() string { return string(r) }
//
// This is the equivalent to rs_node_t in Cascade. It is the minimal
// amount of of information for tracking component state and other vital
// info at an abstract level. The hwinv is for component-type specific
// fields and detailed HW attributes, i.e. just like XC.
//
// For most HMS operations (and non-inventory ones in the managed plane)
// this info should be sufficient. We want to keep it minimal for speed.
// Those fields that are not fixed at discovery should be those that can
// change outside of discovery in response to system activity, i.e. hwinv
// should contain only fields that are basically static between discoveries
// of the endpoint. Things like firmware versions might be an exception,
// but that would be a separate process SM would
//
// 1.0.0
//
type Component struct {
ID string `json:"ID"`
Type string `json:"Type"`
State string `json:"State,omitempty"`
Flag string `json:"Flag,omitempty"`
Enabled *bool `json:"Enabled,omitempty"`
SwStatus string `json:"SoftwareStatus,omitempty"`
Role string `json:"Role,omitempty"`
SubRole string `json:"SubRole,omitempty"`
NID json.Number `json:"NID,omitempty"`
Subtype string `json:"Subtype,omitempty"`
NetType string `json:"NetType,omitempty"`
Arch string `json:"Arch,omitempty"`
Class string `json:"Class,omitempty"`
ReservationDisabled bool `json:"ReservationDisabled,omitempty"`
Locked bool `json:"Locked,omitempty"`
}
// A collection of 0-n Components. It could just be an ordinary
// array but we want to save the option to have indentifying info, etc.
// packaged with it, e.g. the query parameters or options that produced it,
// especially if there are fewer fields than normal being included.
type ComponentArray struct {
Components []*Component `json:"Components"`
}