/
import_init.go
977 lines (838 loc) · 37 KB
/
import_init.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
/*
*
* MIT License
*
* (C) Copyright 2023 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 csm
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"reflect"
"regexp"
"sort"
"strconv"
"time"
"github.com/Cray-HPE/cani/internal/inventory"
"github.com/Cray-HPE/cani/internal/provider/csm/sls"
"github.com/Cray-HPE/cani/pkg/hardwaretypes"
hsm_client "github.com/Cray-HPE/cani/pkg/hsm-client"
"github.com/Cray-HPE/cani/pkg/pointers"
sls_client "github.com/Cray-HPE/cani/pkg/sls-client"
"github.com/Cray-HPE/hms-xname/xnames"
"github.com/Cray-HPE/hms-xname/xnametypes"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
//
// Other thoughts
//
//
// 1. Create a top level system object in SLS, this will serve at the main place to store the CANI metadata
// - Last time was imported by CANI
// - Version of CANI when the last import occured
// - SLS CANI Schema Level
// 2. For missing hardware in SLS like Mountain Cabinet RouterBMCs, add them from HSM state
// 3. Each hardware object should have the UUID of the assoicated CANI Hardware UUID that it is assoicated to. Hopefullt this will be 1-to-1
// 4. If hardware is added to SLS without the special CANI metadata it can detected as being added outside the normal process
// 5. For hardware that doesn't exist in mountain cabinets (phantom nodes) either we mark thinks as absent as a CANI state (here is the logical data, but no physical data)
// or out right remove them, but that will break existing procedures.
func loadJSON(path string, dest interface{}) error {
raw, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return json.Unmarshal(raw, dest)
}
func (csm *CSM) ImportInit(cmd *cobra.Command, args []string, datastore inventory.Datastore) error {
// Check the schema version defined in the datastore
datastoreSchema, err := datastore.GetSchemaVersion()
if err != nil {
return err
}
currentSchema := string(datastoreSchema)
//
// Retrieve current state from the system
//
slsDumpstate, _, err := csm.slsClient.DumpstateApi.DumpstateGet(cmd.Context())
if err != nil {
return errors.Join(fmt.Errorf("failed to perform SLS dumpstate"), err)
}
hsmStateComponents, _, err := csm.hsmClient.ComponentApi.DoComponentsGet(cmd.Context(), nil)
if err != nil {
return errors.Join(fmt.Errorf("failed to retrieve HSM State Components"), err)
}
hsmHardwareInventory, _, err := csm.hsmClient.HWInventoryByLocationApi.DoHWInvByLocationGetAll(cmd.Context(), nil)
if err != nil {
return errors.Join(fmt.Errorf("failed to retrieve HSM State Components"), err)
}
//
// HSM lookup tables
//
hsmStateComponentsMap := map[string]hsm_client.Component100Component{}
for _, hsmComponent := range hsmStateComponents.Components {
hsmStateComponentsMap[hsmComponent.ID] = hsmComponent
}
hsmHardwareInventoryMap := map[string]hsm_client.HwInventory100HwInventoryByLocation{}
for _, hsmHardware := range hsmHardwareInventory {
hsmHardwareInventoryMap[hsmHardware.ID] = hsmHardware
}
tempDatastore, err := datastore.Clone()
if err != nil {
return errors.Join(fmt.Errorf("failed to clone datastore"), err)
}
// Prune non-mountain hardware
slsDumpstate.Hardware, _ = sls.FilterHardware(slsDumpstate.Hardware, func(hardware sls_client.Hardware) (bool, error) {
return hardware.Class != sls_client.HardwareClassRiver, nil
})
// Get the system UUID
cSystem, err := tempDatastore.GetSystemZero()
if err != nil {
return errors.Join(fmt.Errorf("failed to get system:0 ID"), err)
}
// Import related changes for SLS
// slsHardwareToAdd := map[string]sls_client.Hardware{}
slsHardwareToModify := map[string]sls_client.Hardware{}
// slsHardwareExists := map[string]sls_client.Hardware{}
// CANI Hardware changes
// TODO
// TODO Unable to POST this into SLS
// Check to see if a system object exists in the SLS dumpstate
// slsSystem, exists := slsDumpstate.Hardware["s0"]
// if !exists {
// log.Warn().Msgf("SLS does not contain a system object, creating one")
//
// slsSystem = sls.NewHardware(xnames.System{}, sls_client.HardwareClassRiver, sls_client.HardwareExtraPropertiesSystem{
// CaniId: cSystem.ID.String(),
// CaniSlsSchemaVersion: "v1alpha1", // TODO make this a enum
// CaniLastModified: time.Now().UTC().String(),
// })
// slsHardwareToAdd[slsSystem.Xname] = slsSystem
// }
// log.Info().Msgf("System: %v", slsSystem)
//
// Import Cabinets and Chassis
//
allCabinets := sls.FilterHardwareByType(slsDumpstate.Hardware, xnametypes.Cabinet)
allChassis := sls.FilterHardwareByType(slsDumpstate.Hardware, xnametypes.Chassis)
allChassisBMCs := sls.FilterHardwareByType(slsDumpstate.Hardware, xnametypes.ChassisBMC)
// Find all cabinets and what chassis they have
cabinetChassisCounts := map[string][]int{}
for _, chassis := range allChassis {
chassisXname := xnames.FromStringToStruct[xnames.Chassis](chassis.Xname)
cabinet := chassisXname.Parent()
cabinetChassisCounts[cabinet.String()] = append(cabinetChassisCounts[cabinet.String()], chassisXname.Chassis)
}
for cabinet, chassisOrdinals := range cabinetChassisCounts {
sort.Ints(chassisOrdinals)
log.Debug().Msgf("%s: %v - %v", cabinet, len(chassisOrdinals), chassisOrdinals)
}
// Find all cabinets and build up HMN VLAN Mappings
cabinetHMNVlans := map[string]int{}
cabinetSubnetRegex := regexp.MustCompile(`cabinet_(\d+)`)
// The networking data in SLS should be considered the source of truth for networking information
// instead of looking at the SLS hardware part of SLS
for _, networkName := range []string{"HMN_MTN", "HMN_RVR"} {
network, exists := slsDumpstate.Networks[networkName]
if !exists {
log.Warn().Msgf("SLS Network (%s) does not exist", networkName)
continue
}
for _, subnet := range network.ExtraProperties.Subnets {
matches := cabinetSubnetRegex.FindStringSubmatch(subnet.Name)
if len(matches) != 2 {
log.Warn().Msgf("Skipping subnet (%s) in network (%s) for cabinet HMN Vlan lookup", subnet.Name, networkName)
continue
}
cabinetXname := xnames.Cabinet{}
cabinetXname.Cabinet, err = strconv.Atoi(matches[1])
if err != nil {
return errors.Join(fmt.Errorf("failed to extract cabinet number from subnet (%s)", subnet.Name), err)
}
cabinetHMNVlans[cabinetXname.String()] = int(subnet.VlanID)
}
}
for cabinet, vlan := range cabinetHMNVlans {
log.Debug().Msgf("Cabinet (%s) has HMN VLAN (%d)", cabinet, vlan)
}
for _, slsCabinet := range allCabinets {
cabinetXname := xnames.FromStringToStruct[xnames.Cabinet](slsCabinet.Xname)
if cabinetXname == nil {
return fmt.Errorf("failed to parse cabinet xname (%s)", slsCabinet.Xname)
}
locationPath, err := FromXname(cabinetXname)
if err != nil {
return errors.Join(fmt.Errorf("failed to build location path for xname (%v)", cabinetXname), err)
}
//
// Stage 1: Determine of the cabinet is new or currently exists. If they don't exist push the required hardware data into the database
//
cCabinet, err := tempDatastore.GetAtLocation(locationPath)
if err == nil {
// Cabinet exists
log.Info().Msgf("Cabinet %s (%v) exists in datastore with ID (%s)", cabinetXname, locationPath, cCabinet.ID)
// TODO Build metadata from sls data
} else if errors.Is(err, inventory.ErrHardwareNotFound) {
// Cabinet does not exist, which means it needs to be added
// TODO When reconstituting the CANI inventory (say it was lost), should we reuse existing IDs?
log.Info().Msgf("Cabinet %s does not exist in datastore at %s", cabinetXname, locationPath)
deviceTypeSlug := ""
switch slsCabinet.Class {
case sls_client.HardwareClassRiver:
deviceTypeSlug = "hpe-eia-cabinet"
case sls_client.HardwareClassHill:
if reflect.DeepEqual(cabinetChassisCounts[cabinetXname.String()], []int{1, 3}) {
deviceTypeSlug = "hpe-ex2000"
} else if reflect.DeepEqual(cabinetChassisCounts[cabinetXname.String()], []int{0}) {
deviceTypeSlug = "hpe-ex2500-1-liquid-cooled-chassis"
} else if reflect.DeepEqual(cabinetChassisCounts[cabinetXname.String()], []int{0, 1}) {
deviceTypeSlug = "hpe-ex2500-2-liquid-cooled-chassis"
} else if reflect.DeepEqual(cabinetChassisCounts[cabinetXname.String()], []int{0, 1, 2}) {
deviceTypeSlug = "hpe-ex2500-3-liquid-cooled-chassis"
}
case sls_client.HardwareClassMountain:
if reflect.DeepEqual(cabinetChassisCounts[cabinetXname.String()], []int{0, 1, 2, 3, 4, 5, 6, 7}) {
deviceTypeSlug = "hpe-ex4000" // TODO This is ambiguous with the EX3000 cabinet, for right now assume
}
default:
return fmt.Errorf("cabinet (%s) has unknown class (%s)", cabinetXname, slsCabinet.Class)
}
if deviceTypeSlug == "" {
log.Warn().Msgf("Cabinet %s device type slug is unknown, ignoring", cabinetXname.String())
continue
} else {
log.Info().Msgf("Cabinet %s device type slug is %s", cabinetXname.String(), deviceTypeSlug)
}
// Now its time to build up what the hardware looks like
newHardware, err := csm.buildInventoryHardware(deviceTypeSlug, cabinetXname.Cabinet, cSystem, inventory.HardwareStatusProvisioned)
if err != nil {
return errors.Join(fmt.Errorf("failed to build hardware for cabinet (%s)", cabinetXname.String()), err)
}
// Push the new hardware into the datastore
for _, cHardware := range newHardware {
log.Info().Msgf("Hardware from cabinet %s: %s", cabinetXname.String(), cHardware.ID)
if err := tempDatastore.Add(&cHardware); err != nil {
return fmt.Errorf("failed to add hardware (%s) to in memory datastore", cHardware.ID)
}
}
// Set cabinet metadata
cabinetMetadata := CabinetMetadata{}
if vlan, exists := cabinetHMNVlans[slsCabinet.Xname]; exists {
cabinetMetadata.HMNVlan = pointers.IntPtr(vlan)
}
cCabinet, err = tempDatastore.GetAtLocation(locationPath)
if err != nil {
return errors.Join(fmt.Errorf("failed to query datastore for %s", locationPath), err)
}
// Encode cabinet metadata
metadataRaw, err := EncodeProviderMetadata(Metadata{Cabinet: &cabinetMetadata})
if err != nil {
return fmt.Errorf("failed to encode provider metadata for hardware (%s)", cCabinet.ID)
}
cCabinet.SetProviderMetadata(inventory.CSMProvider, metadataRaw)
// Push cabinet in datastore.
if err := tempDatastore.Update(&cCabinet); err != nil {
return fmt.Errorf("failed to update hardware (%s) in memory datastore", cCabinet.ID)
}
} else {
// Error occurred
return errors.Join(fmt.Errorf("failed to query datastore"), err)
}
// Update SLS metadata
slsCabinetEP, err := sls.DecodeExtraProperties[sls_client.HardwareExtraPropertiesCabinet](slsCabinet)
if err != nil {
return fmt.Errorf("failed to decode SLS hardware extra properties (%s)", slsCabinet.Xname)
}
// if a schema is present but does not match the current schema, warn but accept so the user can adjust as needed
if slsCabinetEP.CaniSlsSchemaVersion != "" && slsCabinetEP.CaniSlsSchemaVersion != currentSchema {
log.Warn().Msgf("Schema mismatch: expected %s but found %s for SLS %s %s (%s)", currentSchema, slsCabinetEP.CaniSlsSchemaVersion, cCabinet.Type, slsCabinet.Xname, cCabinet.ID)
}
if slsCabinetEP.CaniId != cCabinet.ID.String() {
if len(slsCabinetEP.CaniId) != 0 {
log.Warn().Msgf("Detected CANI hardware ID change from %s to %s for SLS Hardware %s", slsCabinetEP.CaniId, cCabinet.ID, slsCabinet.Xname)
}
// Add in CANI properties
slsCabinetEP.CaniId = cCabinet.ID.String()
slsCabinetEP.CaniSlsSchemaVersion = "v1alpha1" // TODO make this a enum
slsCabinetEP.CaniLastModified = time.Now().UTC().String()
slsCabinetEP.CaniStatus = (*sls_client.CaniStatus)(&cCabinet.Status)
log.Info().Msgf("SLS extra properties changed for %s", slsCabinet.Xname)
slsCabinet.ExtraProperties = slsCabinetEP
slsHardwareToModify[slsCabinet.Xname] = slsCabinet
}
}
//
// Fix up Chassis SLS metadata
//
for _, slsHardware := range allChassis {
xname := xnames.FromString(slsHardware.Xname)
if xname == nil {
return fmt.Errorf("failed to parse xname (%s)", slsHardware.Xname)
}
locationPath, err := FromXname(xname)
if err != nil {
return errors.Join(fmt.Errorf("failed to build location path for xname (%v)", xname), err)
}
cHardware, err := tempDatastore.GetAtLocation(locationPath)
if err != nil {
return errors.Join(fmt.Errorf("failed to query datastore for %s", locationPath), err)
}
// Update SLS metadata
slsEP, err := sls.DecodeExtraProperties[sls_client.HardwareExtraPropertiesChassis](slsHardware)
if err != nil {
return fmt.Errorf("failed to decode SLS hardware extra properties (%s)", slsHardware.Xname)
}
// if a schema is present but does not match the current schema, warn but accept so the user can adjust as needed
if slsEP.CaniSlsSchemaVersion != "" && slsEP.CaniSlsSchemaVersion != currentSchema {
log.Warn().Msgf("Schema mismatch: expected %s but found %s for SLS %s %s (%s)", currentSchema, slsEP.CaniSlsSchemaVersion, cHardware.Type, slsHardware.Xname, cHardware.ID)
}
if slsEP.CaniId != cHardware.ID.String() {
if len(slsEP.CaniId) != 0 {
log.Warn().Msgf("Detected CANI hardware ID change from %s to %s for SLS Hardware %s", slsEP.CaniId, cHardware.ID, slsHardware.Xname)
}
// Add in CANI properties
slsEP.CaniId = cHardware.ID.String()
slsEP.CaniSlsSchemaVersion = "v1alpha1" // TODO make this a enum
slsEP.CaniLastModified = time.Now().UTC().String()
slsEP.CaniStatus = (*sls_client.CaniStatus)(&cHardware.Status)
log.Info().Msgf("SLS extra properties changed for %s", slsHardware.Xname)
slsHardware.ExtraProperties = slsEP
slsHardwareToModify[slsHardware.Xname] = slsHardware
}
}
//
// Fix up ChassisBMC SLS Metadata
//
for _, slsHardware := range allChassisBMCs {
xname := xnames.FromString(slsHardware.Xname)
if xname == nil {
return fmt.Errorf("failed to parse xname (%s)", slsHardware.Xname)
}
locationPath, err := FromXname(xname)
if err != nil {
return errors.Join(fmt.Errorf("failed to build location path for xname (%v)", xname), err)
}
cHardware, err := tempDatastore.GetAtLocation(locationPath)
if err != nil {
return errors.Join(fmt.Errorf("failed to query datastore for %s", locationPath), err)
}
// Update SLS metadata
slsEP, err := sls.DecodeExtraProperties[sls_client.HardwareExtraPropertiesChassisBmc](slsHardware)
if err != nil {
return fmt.Errorf("failed to decode SLS hardware extra properties (%s)", slsHardware.Xname)
}
// if a schema is present but does not match the current schema, warn but accept so the user can adjust as needed
if slsEP.CaniSlsSchemaVersion != "" && slsEP.CaniSlsSchemaVersion != currentSchema {
log.Warn().Msgf("Schema mismatch: expected %s but found %s for SLS %s %s (%s)", currentSchema, slsEP.CaniSlsSchemaVersion, cHardware.Type, slsHardware.Xname, cHardware.ID)
}
if slsEP.CaniId != cHardware.ID.String() {
if len(slsEP.CaniId) != 0 {
log.Warn().Msgf("Detected CANI hardware ID change from %s to %s for SLS Hardware %s", slsEP.CaniId, cHardware.ID, slsHardware.Xname)
}
// Add in CANI properties
slsEP.CaniId = cHardware.ID.String()
slsEP.CaniSlsSchemaVersion = "v1alpha1" // TODO make this a enum
slsEP.CaniLastModified = time.Now().UTC().String()
slsEP.CaniStatus = (*sls_client.CaniStatus)(&cHardware.Status)
log.Info().Msgf("SLS extra properties changed for %s", slsHardware.Xname)
slsHardware.ExtraProperties = slsEP
slsHardwareToModify[slsHardware.Xname] = slsHardware
}
}
//
// Import Nodes
//
allNodes := sls.FilterHardwareByType(slsDumpstate.Hardware, xnametypes.Node)
// 1. Find all slots holding blades (either currently populated or could be populated) from SLS
slsNodeBladeXnames := []xnames.ComputeModule{}
slsNodeBladesFound := map[xnames.ComputeModule][]xnames.NodeBMC{}
slsNodeBMCFound := map[xnames.NodeBMC]bool{}
for _, slsNode := range allNodes {
nodeXname := xnames.FromStringToStruct[xnames.Node](slsNode.Xname)
if nodeXname == nil {
return fmt.Errorf("failed to parse node xname (%s)", slsNode.Xname)
}
// Node -> NodeBMC (Node Card) -> ComputeModule (Node Blade)
nodeBMCXname := nodeXname.Parent()
nodeBladeXname := nodeBMCXname.Parent()
if slsNodeBMCFound[nodeBMCXname] {
// We have already discovered this node BMC, and we don't need to add it again
continue
}
// Keep track that we have seem this BMC
slsNodeBMCFound[nodeBMCXname] = true
if _, exists := slsNodeBladesFound[nodeBladeXname]; !exists {
// This is the first time we have seem this blade, lets add it to our list of node blade xnames
slsNodeBladeXnames = append(slsNodeBladeXnames, nodeBladeXname)
}
// Keep track that we found this node BMC on this blade
slsNodeBladesFound[nodeBladeXname] = append(slsNodeBladesFound[nodeBladeXname], nodeBMCXname)
}
// 1.1 Sort the found node blade xnames, so the output is nice to look at
for _, nodeBMCs := range slsNodeBladesFound {
sort.Slice(nodeBMCs, func(i, j int) bool {
return nodeBMCs[i].String() < nodeBMCs[j].String()
})
}
sort.Slice(slsNodeBladeXnames, func(i, j int) bool {
return slsNodeBladeXnames[i].String() < slsNodeBladeXnames[j].String()
})
// 2. Find all slots holding blades from HSM, and identify hardware
nodeBladeDeviceSlugs := map[xnames.ComputeModule]string{}
for _, nodeBladeXname := range slsNodeBladeXnames {
hsmComponent, exists := hsmStateComponentsMap[nodeBladeXname.String()]
if !exists {
log.Debug().Msgf("%s exists in SLS, but not HSM", nodeBladeXname)
// This could be caused by the cabinet controller not been discovered yet
continue
}
if hsmComponent.State != nil {
log.Debug().Msgf("%s exists in HSM with state %s", nodeBladeXname, *hsmComponent.State)
}
for _, nodeBMCXname := range slsNodeBladesFound[nodeBladeXname] {
// Don't need to do this if we already identified the blade
if _, exists := nodeBladeDeviceSlugs[nodeBladeXname]; exists {
continue
}
// For every BMC in HSM there is a NodeEnclosure. The NodeEnclosure ordinal matches
// the BMC ordinal
nodeEnclosureXname := nodeBladeXname.NodeEnclosure(nodeBMCXname.NodeBMC)
nodeEnclosure, exists := hsmHardwareInventoryMap[nodeEnclosureXname.String()]
if !exists {
log.Warn().Msgf("%s is missing from HSM hardware inventory, possible phantom hardware", nodeEnclosureXname)
continue
}
if nodeEnclosure.PopulatedFRU == nil {
log.Warn().Msgf("%s is missing PopulatedFRU data", nodeEnclosureXname)
continue // TODO what should happen here?
}
if nodeEnclosure.PopulatedFRU.HMSNodeEnclosureFRUInfo == nil {
log.Warn().Msgf("%s is missing PopulatedFRU node enclosure data", nodeEnclosureXname)
continue // TODO what should happen here?
}
nodeEnclosureFru := nodeEnclosure.PopulatedFRU.HMSNodeEnclosureFRUInfo
log.Debug().Msgf("%s has manufacturer %s and model %s", nodeEnclosureXname, nodeEnclosureFru.Manufacturer, nodeEnclosureFru.Model)
bladeDeviceSlug, err := csm.identifyDeviceSlug(nodeEnclosureFru.Manufacturer, nodeEnclosureFru.Model, nodeEnclosureFru.PartNumber)
if err != nil {
log.Warn().Msgf("%s unable to determine blade device slug from Node Enclosure FRU data: %s", nodeEnclosureXname, err)
continue
}
nodeBladeDeviceSlugs[nodeBladeXname] = bladeDeviceSlug
log.Debug().Msgf("%s has blade device slug: %s", nodeBladeXname, bladeDeviceSlug)
}
}
// 3. Import node data for nodes that are physically present
for nodeBladeXname, deviceSlug := range nodeBladeDeviceSlugs {
// Check to see if the node blade exists
nodeBladeLocationPath, err := FromXname(nodeBladeXname)
if err != nil {
return errors.Join(fmt.Errorf("failed to build location path for xname (%v)", nodeBladeXname), err)
}
cNodeBlade, err := tempDatastore.GetAtLocation(nodeBladeLocationPath)
if err == nil {
// Blade currently exists
log.Debug().Msgf("Node blade %s (%v) exists in datastore with ID (%s)", nodeBladeXname, nodeBladeLocationPath, cNodeBlade.ID)
// TODO Build metadata from sls data for merging
} else if errors.Is(err, inventory.ErrHardwareNotFound) {
// Node blade does not exist
// Determine the chassis ID
chassisLocationPath, err := FromXname(nodeBladeXname.Parent())
if err != nil {
return errors.Join(fmt.Errorf("failed to build location path for xname (%v)", nodeBladeXname), err)
}
cChassis, err := tempDatastore.GetAtLocation(chassisLocationPath)
if err != nil {
return errors.Join(fmt.Errorf("failed to get datastore ID for %v", chassisLocationPath), err)
}
// Now its time to build up what the hardware looks like
newHardware, err := csm.buildInventoryHardware(deviceSlug, nodeBladeXname.ComputeModule, cChassis, inventory.HardwareStatusProvisioned)
if err != nil {
return errors.Join(fmt.Errorf("failed to build hardware for node blade (%s)", nodeBladeXname.String()), err)
}
// Push the new hardware into the datastore
for _, cHardware := range newHardware {
log.Debug().Msgf("Hardware from node blade %s: %s", nodeBladeXname.String(), cHardware.ID)
if err := tempDatastore.Add(&cHardware); err != nil {
return fmt.Errorf("failed to add hardware (%s) to in memory datastore", cHardware.ID)
}
}
} else {
// Error occurred
return errors.Join(fmt.Errorf("failed to query datastore"), err)
}
}
// 4. Handle phantom nodes
for _, slsNode := range allNodes {
nodeXname := xnames.FromStringToStruct[xnames.Node](slsNode.Xname)
if nodeXname == nil {
return fmt.Errorf("failed to parse node xname (%s)", slsNode.Xname)
}
if hsmComponent, exists := hsmHardwareInventoryMap[nodeXname.String()]; exists && hsmComponent.Status != "Empty" {
// HSM knows about the component and its not empty. This can't be a phantom node
continue
}
//
// This must be a phantom node!
//
nodeBMCXname := nodeXname.Parent()
nodeBladeXname := nodeBMCXname.Parent()
chassisXname := nodeBladeXname.Parent()
// Retrieve the chassis
chassisLP, err := FromXname(chassisXname)
if err != nil {
return errors.Join(fmt.Errorf("failed to determine location path for chassis %s", chassisXname), err)
}
cChassis, err := tempDatastore.GetAtLocation(chassisLP)
if errors.Is(err, inventory.ErrHardwareNotFound) {
return errors.Join(fmt.Errorf("%s %v does not exist in temporary datastore", hardwaretypes.Chassis, chassisLP), err)
} else if err != nil {
return errors.Join(fmt.Errorf("failed to retrieve %s %v from temporary datastore", hardwaretypes.Chassis, chassisLP), err)
}
// Retrieve the node blade
nodeBladeLP := append(chassisLP, inventory.LocationToken{
HardwareType: hardwaretypes.NodeBlade,
Ordinal: nodeBladeXname.ComputeModule,
})
cNodeBlade, err := tempDatastore.GetAtLocation(nodeBladeLP)
if errors.Is(err, inventory.ErrHardwareNotFound) {
// Create empty node blade in CANI
cNodeBlade = inventory.Hardware{
Parent: cChassis.ID,
Type: hardwaretypes.NodeBlade,
Status: inventory.HardwareStatusEmpty,
LocationOrdinal: pointers.IntPtr(nodeBladeXname.ComputeModule),
}
if err := tempDatastore.Add(&cNodeBlade); err != nil {
return errors.Join(fmt.Errorf("failed to create %s for %v in datastore", hardwaretypes.NodeBlade, nodeBladeXname), err)
}
} else if err != nil {
return errors.Join(fmt.Errorf("failed to retrieve %s %v from temporary datastore", hardwaretypes.NodeBlade, nodeBladeLP), err)
}
// Retrieve the node card
nodeCardLP := append(nodeBladeLP, inventory.LocationToken{
HardwareType: hardwaretypes.NodeCard,
Ordinal: nodeBMCXname.NodeBMC,
})
cNodeCard, err := tempDatastore.GetAtLocation(nodeCardLP)
if errors.Is(err, inventory.ErrHardwareNotFound) {
// Create empty node card in CANI
cNodeCard = inventory.Hardware{
Parent: cNodeBlade.ID,
Type: hardwaretypes.NodeCard,
Status: inventory.HardwareStatusEmpty,
LocationOrdinal: pointers.IntPtr(nodeBMCXname.NodeBMC),
}
if err := tempDatastore.Add(&cNodeCard); err != nil {
return errors.Join(fmt.Errorf("failed to create empty %s for %v in datastore", hardwaretypes.NodeCard, nodeBMCXname), err)
}
} else if err != nil {
return errors.Join(fmt.Errorf("failed to retrieve %s %v from temporary datastore", hardwaretypes.NodeCard, nodeCardLP), err)
}
// Retrieve the node
nodeLP := append(nodeCardLP, inventory.LocationToken{
HardwareType: hardwaretypes.Node,
Ordinal: nodeXname.Node,
})
cNode, err := tempDatastore.GetAtLocation(nodeLP)
if errors.Is(err, inventory.ErrHardwareNotFound) {
// Create empty node card in CANI
cNode = inventory.Hardware{
Parent: cNodeCard.ID,
Type: hardwaretypes.Node,
Status: inventory.HardwareStatusEmpty,
LocationOrdinal: pointers.IntPtr(nodeXname.Node),
}
if err := tempDatastore.Add(&cNode); err != nil {
return errors.Join(fmt.Errorf("failed to create empty %s for %v in datastore", hardwaretypes.Node, nodeXname), err)
}
} else if err != nil {
return errors.Join(fmt.Errorf("failed to retrieve %s %v from temporary datastore", hardwaretypes.Node, nodeLP), err)
}
}
// 5. Update node metadata in CANI and SLS
for _, slsNode := range allNodes {
nodeXname := xnames.FromString(slsNode.Xname)
if nodeXname == nil {
return fmt.Errorf("failed to parse node xname (%s)", slsNode.Xname)
}
nodeLocationPath, err := FromXname(nodeXname)
if err != nil {
return errors.Join(fmt.Errorf("failed to build location path for xname (%v)", nodeXname), err)
}
//
// Build up node extra properties for CANI
//
slsNodeEP, err := sls.DecodeExtraProperties[sls_client.HardwareExtraPropertiesNode](slsNode)
if err != nil {
return errors.Join(fmt.Errorf("failed to decode hardware extra properties for (%s)", slsNode.Xname), err)
}
nodeMetadata := NodeMetadata{}
if slsNodeEP.Role != "" {
nodeMetadata.Role = pointers.StringPtr(slsNodeEP.Role)
}
if slsNodeEP.SubRole != "" {
nodeMetadata.Role = pointers.StringPtr(slsNodeEP.SubRole)
}
if slsNodeEP.NID != 0 {
nodeMetadata.Nid = pointers.IntPtr(int(slsNodeEP.NID))
}
if len(slsNodeEP.Aliases) != 0 {
nodeMetadata.Alias = slsNodeEP.Aliases
}
cNode, err := tempDatastore.GetAtLocation(nodeLocationPath)
if errors.Is(err, inventory.ErrHardwareNotFound) {
log.Warn().Msgf("Hardware does not exist (possible phantom hardware): %s", nodeLocationPath)
log.Fatal().Msg("Panic!")
continue
} else if err != nil {
return errors.Join(fmt.Errorf("failed to query datastore for %s", nodeLocationPath), err)
}
// Set the node metadata
metadataRaw, err := EncodeProviderMetadata(Metadata{Node: &nodeMetadata})
if err != nil {
return fmt.Errorf("failed to encode provider metadata for hardware (%s)", cNode.ID)
}
cNode.SetProviderMetadata(inventory.CSMProvider, metadataRaw)
// Push updates into the datastore
if err := tempDatastore.Update(&cNode); err != nil {
return fmt.Errorf("failed to update hardware (%s) in memory datastore", cNode.ID)
}
// if a schema is present but does not match the current schema, warn but accept so the user can adjust as needed
if slsNodeEP.CaniSlsSchemaVersion != "" && slsNodeEP.CaniSlsSchemaVersion != currentSchema {
log.Warn().Msgf("Schema mismatch: expected %s but found %s for SLS %s %s (%s)", currentSchema, slsNodeEP.CaniSlsSchemaVersion, cNode.Type, slsNode.Xname, cNode.ID)
}
//
// Update SLS Extra Properties
//
if slsNodeEP.CaniId != cNode.ID.String() {
if len(slsNodeEP.CaniId) != 0 {
log.Warn().Msgf("Detected CANI hardware ID change from %s to %s for SLS Hardware %s", slsNodeEP.CaniId, cNode.ID, slsNode.Xname)
}
// Update it if it has changed
slsNodeEP.CaniId = cNode.ID.String()
slsNodeEP.CaniSlsSchemaVersion = "v1alpha1" // TODO make this a enum
slsNodeEP.CaniLastModified = time.Now().UTC().String()
slsNodeEP.CaniStatus = (*sls_client.CaniStatus)(&cNode.Status)
slsNode.ExtraProperties = slsNodeEP
slsHardwareToModify[slsNode.Xname] = slsNode
log.Debug().Msgf("SLS extra properties changed for %s", slsNode.Xname)
}
}
//
// Import Router BMCs
//
// TODO
//
// Push updates to SLS
//
if err := sls.HardwareUpdate(csm.slsClient, cmd.Context(), slsHardwareToModify, 10); err != nil {
return errors.Join(fmt.Errorf("failed to update hardware in SLS"), err)
}
// Commit changes!
if err := datastore.Merge(tempDatastore); err != nil {
return errors.Join(fmt.Errorf("failed to merge temporary datastore with actual datastore"), err)
}
return datastore.Flush()
}
func (csm *CSM) buildInventoryHardware(deviceTypeSlug string, ordinal int, cParent inventory.Hardware, status inventory.HardwareStatus) ([]inventory.Hardware, error) {
if csm.hardwareLibrary == nil {
panic("Hardware type library is nil")
}
// Build up the expected hardware
// Generate a hardware build out using the system as a parent
hardwareBuildOutItems, err := inventory.GenerateDefaultHardwareBuildOut(csm.hardwareLibrary, deviceTypeSlug, ordinal, cParent)
if err != nil {
return nil, errors.Join(
fmt.Errorf("unable to build default hardware build out for %s", deviceTypeSlug),
err,
)
}
var allHardware []inventory.Hardware
for _, hardwareBuildOut := range hardwareBuildOutItems {
hardware := inventory.NewHardwareFromBuildOut(hardwareBuildOut, inventory.HardwareStatusProvisioned)
allHardware = append(allHardware, hardware)
}
return allHardware, nil
}
func FromXname(xnameRaw xnames.Xname) (inventory.LocationPath, error) {
// TODO Look into go generating this
switch xname := xnameRaw.(type) {
// System
case xnames.System:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
}, nil
case *xnames.System:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
}, nil
// Cabinet
case xnames.Cabinet:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
}, nil
case *xnames.Cabinet:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
}, nil
// Chassis
case xnames.Chassis:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
{HardwareType: hardwaretypes.Chassis, Ordinal: xname.Chassis},
}, nil
case *xnames.Chassis:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
{HardwareType: hardwaretypes.Chassis, Ordinal: xname.Chassis},
}, nil
// Chassis BMC
case xnames.ChassisBMC:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
{HardwareType: hardwaretypes.Chassis, Ordinal: xname.Chassis},
{HardwareType: hardwaretypes.ChassisManagementModule, Ordinal: xname.ChassisBMC},
}, nil
case *xnames.ChassisBMC:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
{HardwareType: hardwaretypes.Chassis, Ordinal: xname.Chassis},
{HardwareType: hardwaretypes.ChassisManagementModule, Ordinal: xname.ChassisBMC},
}, nil
// Compute Module
case xnames.ComputeModule:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
{HardwareType: hardwaretypes.Chassis, Ordinal: xname.Chassis},
{HardwareType: hardwaretypes.NodeBlade, Ordinal: xname.ComputeModule},
}, nil
case *xnames.ComputeModule:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
{HardwareType: hardwaretypes.Chassis, Ordinal: xname.Chassis},
{HardwareType: hardwaretypes.NodeBlade, Ordinal: xname.ComputeModule},
}, nil
// Node BMC
case xnames.NodeBMC:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
{HardwareType: hardwaretypes.Chassis, Ordinal: xname.Chassis},
{HardwareType: hardwaretypes.NodeBlade, Ordinal: xname.ComputeModule},
{HardwareType: hardwaretypes.NodeCard, Ordinal: xname.NodeBMC},
{HardwareType: hardwaretypes.NodeController, Ordinal: 0}, // Assumes one Node BMC per node card, For all supported CSM hardware this is true
}, nil
case *xnames.NodeBMC:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
{HardwareType: hardwaretypes.Chassis, Ordinal: xname.Chassis},
{HardwareType: hardwaretypes.NodeBlade, Ordinal: xname.ComputeModule},
{HardwareType: hardwaretypes.NodeCard, Ordinal: xname.NodeBMC},
{HardwareType: hardwaretypes.NodeController, Ordinal: 0}, // Assumes one Node BMC per node card, For all supported CSM hardware this is true
}, nil
// Node
case xnames.Node:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
{HardwareType: hardwaretypes.Chassis, Ordinal: xname.Chassis},
{HardwareType: hardwaretypes.NodeBlade, Ordinal: xname.ComputeModule},
{HardwareType: hardwaretypes.NodeCard, Ordinal: xname.NodeBMC},
{HardwareType: hardwaretypes.Node, Ordinal: xname.Node},
}, nil
case *xnames.Node:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
{HardwareType: hardwaretypes.Chassis, Ordinal: xname.Chassis},
{HardwareType: hardwaretypes.NodeBlade, Ordinal: xname.ComputeModule},
{HardwareType: hardwaretypes.NodeCard, Ordinal: xname.NodeBMC},
{HardwareType: hardwaretypes.Node, Ordinal: xname.Node},
}, nil
// Router Module
case xnames.RouterModule:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
{HardwareType: hardwaretypes.Chassis, Ordinal: xname.Chassis},
{HardwareType: hardwaretypes.HighSpeedSwitchEnclosure, Ordinal: xname.RouterModule},
}, nil
case *xnames.RouterModule:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
{HardwareType: hardwaretypes.Chassis, Ordinal: xname.Chassis},
{HardwareType: hardwaretypes.HighSpeedSwitchEnclosure, Ordinal: xname.RouterModule},
}, nil
// Router BMC
case xnames.RouterBMC:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
{HardwareType: hardwaretypes.Chassis, Ordinal: xname.Chassis},
{HardwareType: hardwaretypes.HighSpeedSwitchEnclosure, Ordinal: xname.RouterModule},
{HardwareType: hardwaretypes.HighSpeedSwitchController, Ordinal: xname.RouterBMC},
}, nil
case *xnames.RouterBMC:
return inventory.LocationPath{
{HardwareType: hardwaretypes.System, Ordinal: 0},
{HardwareType: hardwaretypes.Cabinet, Ordinal: xname.Cabinet},
{HardwareType: hardwaretypes.Chassis, Ordinal: xname.Chassis},
{HardwareType: hardwaretypes.HighSpeedSwitchEnclosure, Ordinal: xname.RouterModule},
{HardwareType: hardwaretypes.HighSpeedSwitchController, Ordinal: xname.RouterBMC},
}, nil
}
return nil, fmt.Errorf("unable to convert xname type (%s)", xnameRaw.Type())
}
func (csm *CSM) identifyDeviceSlug(manufacturer, model, partNumber string) (string, error) {
for deviceSlug, deviceType := range csm.hardwareLibrary.DeviceTypes {
for _, identification := range deviceType.Identifications {
// log.Info().Msgf("Checking %v against [%s, %s]", identification, manufacturer, model)
// Check Manufacturer
if identification.Manufacturer != manufacturer {
continue
}
// Check Model
if identification.Model != model {
continue
}
// Check Part Number
if identification.PartNumber != nil && *identification.PartNumber != partNumber {
continue
}
// Found a match!
return deviceSlug, nil
}
}
return "", fmt.Errorf("unable to find corrensponding device slug for manufacturer (%s) and model (%s)", manufacturer, model)
}