xref: /openbmc/openbmc-test-automation/bin/generate_inventory (revision 5731818de0ce446ceaafc7e75ae39da1b69942ae)
1e7e9171eSGeorge Keishing#!/usr/bin/env python3
2ae7c820fSGeorge Keishingr"""
3ae7c820fSGeorge KeishingGenerate an inventory variable file containing a list of properties
4ae7c820fSGeorge Keishingfields from the YAML phosphor-dbus-interfaces repository.
5ae7c820fSGeorge Keishing"""
647375aa7SSridevi Ramesh
7ae7c820fSGeorge Keishingimport json
8*5731818dSPatrick Williamsimport os
9*5731818dSPatrick Williamsimport sys
10*5731818dSPatrick Williams
11*5731818dSPatrick Williamsimport yaml
12*5731818dSPatrick Williamsfrom gen_print import *
13ae7c820fSGeorge Keishing
14ae7c820fSGeorge Keishinglib_path = sys.path[0] + "/../lib"
15ae7c820fSGeorge Keishingsys.path.insert(0, lib_path)
16ae7c820fSGeorge Keishing
17ae7c820fSGeorge Keishing# This list will be longer when more development codes are available.
18*5731818dSPatrick Williamsinventory_items = ["fru", "core", "fan", "fan_wc", "gpu"]
19ae7c820fSGeorge Keishingprint_var(inventory_items)
20*5731818dSPatrick Williamsfru_inventory_file_path = "inventory.py"
21ae7c820fSGeorge Keishingprint_var(fru_inventory_file_path)
22ae7c820fSGeorge Keishing
231eab8838SGeorge Keishing# Properties inventory list
241eab8838SGeorge Keishingyaml_inventory_list = []
25ae7c820fSGeorge Keishing
26ae7c820fSGeorge Keishing# Clone the phosphor-dbus-interfaces repository
27*5731818dSPatrick Williamscmd_buf = "git clone https://github.com/openbmc/phosphor-dbus-interfaces"
28ae7c820fSGeorge Keishingos.system(cmd_buf)
29ae7c820fSGeorge Keishing
30*5731818dSPatrick Williamsrepo_subdir_path = "/phosphor-dbus-interfaces/xyz/openbmc_project/"
31ae7c820fSGeorge Keishingbase_dir_path = os.getcwd() + repo_subdir_path
32ae7c820fSGeorge Keishing
339ef1fd47SGeorge Keishing# yaml file paths for FRU
34*5731818dSPatrick Williamsyaml_fru_list = [
35*5731818dSPatrick Williams    "Inventory/Item.interface.yaml",
36*5731818dSPatrick Williams    "Inventory/Decorator/Asset.interface.yaml",
37*5731818dSPatrick Williams    "Inventory/Decorator/Revision.interface.yaml",
38*5731818dSPatrick Williams    "Inventory/Decorator/Replaceable.interface.yaml",
39*5731818dSPatrick Williams    "Inventory/Decorator/Cacheable.interface.yaml",
40*5731818dSPatrick Williams    "State/Decorator/OperationalStatus.interface.yaml",
41*5731818dSPatrick Williams]
42ae7c820fSGeorge Keishing
431da4c24dSSweta Potthuri# yaml file paths for CORE.
44*5731818dSPatrick Williamsyaml_core_list = [
45*5731818dSPatrick Williams    "Inventory/Item.interface.yaml",
46*5731818dSPatrick Williams    "State/Decorator/OperationalStatus.interface.yaml",
47*5731818dSPatrick Williams]
48dc68eff6SGeorge Keishing
49dc68eff6SGeorge Keishing# yaml file paths for fan.
50*5731818dSPatrick Williamsyaml_fan_list = [
51*5731818dSPatrick Williams    "Inventory/Item.interface.yaml",
52*5731818dSPatrick Williams    "Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml",
53*5731818dSPatrick Williams    "State/Decorator/OperationalStatus.interface.yaml",
54*5731818dSPatrick Williams]
555c006d39SSteven Sombar
565c006d39SSteven Sombar# yaml file paths for fan_wc (fans in water-cooled system).
57*5731818dSPatrick Williamsyaml_fan_wc_list = [
58*5731818dSPatrick Williams    "Inventory/Item.interface.yaml",
59*5731818dSPatrick Williams    "Inventory/Decorator/MeetsMinimumShipLevel.interface.yaml",
60*5731818dSPatrick Williams]
615c006d39SSteven Sombar
6219305205SGeorge Keishing# yaml file paths for GPU.
63*5731818dSPatrick Williamsyaml_gpu_list = [
64*5731818dSPatrick Williams    "Inventory/Item.interface.yaml",
65*5731818dSPatrick Williams    "Inventory/Decorator/Replaceable.interface.yaml",
66*5731818dSPatrick Williams    "State/Decorator/OperationalStatus.interface.yaml",
67*5731818dSPatrick Williams]
685c006d39SSteven Sombar
691eab8838SGeorge Keishing# Append to inventory list
701eab8838SGeorge Keishingyaml_inventory_list.append(yaml_fru_list)
711eab8838SGeorge Keishingyaml_inventory_list.append(yaml_core_list)
721eab8838SGeorge Keishingyaml_inventory_list.append(yaml_fan_list)
731eab8838SGeorge Keishingyaml_inventory_list.append(yaml_fan_wc_list)
741eab8838SGeorge Keishingyaml_inventory_list.append(yaml_gpu_list)
75ae7c820fSGeorge Keishing
761eab8838SGeorge Keishingprint_var(yaml_inventory_list)
779ef1fd47SGeorge Keishing
78ae7c820fSGeorge Keishing# Populate Inventory data
79ae7c820fSGeorge Keishinginventory_dict = {}
80ae7c820fSGeorge Keishing
811eab8838SGeorge Keishingfor inv_index in range(len(yaml_inventory_list)):
821eab8838SGeorge Keishing    print_var(inv_index)
831eab8838SGeorge Keishing    inventory_dict[str(inventory_items[inv_index])] = []
841eab8838SGeorge Keishing    for rel_yaml_file_path in yaml_inventory_list[inv_index]:
859ef1fd47SGeorge Keishing        yaml_file_path = base_dir_path + rel_yaml_file_path
86ae7c820fSGeorge Keishing
87ae7c820fSGeorge Keishing        # Get the yaml dictionary data
889ef1fd47SGeorge Keishing        print_timen("Loading " + yaml_file_path)
899ef1fd47SGeorge Keishing        f = open(yaml_file_path)
90f87cc0afSYunyun Lin        yaml_data = yaml.safe_load(f)
91ae7c820fSGeorge Keishing        f.close()
92*5731818dSPatrick Williams        for item in range(0, len(yaml_data["properties"])):
93*5731818dSPatrick Williams            tmp_data = yaml_data["properties"][item]["name"]
941eab8838SGeorge Keishing            inventory_dict[str(inventory_items[inv_index])].append(tmp_data)
95ae7c820fSGeorge Keishing
96ae7c820fSGeorge Keishing# Pretty print json formatter
97*5731818dSPatrick Williamsdata = json.dumps(
98*5731818dSPatrick Williams    inventory_dict,
999ef1fd47SGeorge Keishing    indent=4,
1009ef1fd47SGeorge Keishing    sort_keys=True,
1019ef1fd47SGeorge Keishing    default=str,
102*5731818dSPatrick Williams    separators=(",", ":"),
103*5731818dSPatrick Williams)
104ae7c820fSGeorge Keishing
105ae7c820fSGeorge Keishing# Check if there is mismatch in data vs expect list
106ae7c820fSGeorge Keishingif len(inventory_dict) != len(inventory_items):
107178d9bf2SGeorge Keishing    print_error("The generated list doesn't match Inventory List.\n")
10832c4c9e4SGeorge Keishing    print(data)
109ae7c820fSGeorge Keishing    print_var(inventory_items)
110ae7c820fSGeorge Keishing    sys.exit()
111ae7c820fSGeorge Keishing
112ae7c820fSGeorge Keishing# Write dictionary data to inventory file
11332c4c9e4SGeorge Keishingprint("\nGenerated Inventory item json format\n")
11432c4c9e4SGeorge Keishingprint(data)
115*5731818dSPatrick Williamsout = open(fru_inventory_file_path, "w")
116*5731818dSPatrick Williamsout.write("inventory_dict = ")
117ae7c820fSGeorge Keishingout.write(data)
118ae7c820fSGeorge Keishing
119ae7c820fSGeorge Keishingout.close()
12032c4c9e4SGeorge Keishingprint("\nGenerated Inventory File: %s " % fru_inventory_file_path)
121