blob: ca4653beca0577fb3435f14f8ac9856e469ebac1 [file] [log] [blame]
George Keishingf2613b72019-02-13 12:45:59 -06001#!/usr/bin/env python
2
3r"""
4BMC redfish utility functions.
5"""
6
7import json
8from robot.libraries.BuiltIn import BuiltIn
9
10
11class bmc_redfish_utils(object):
12
13 def __init__(self):
14 r"""
15 Initialize the bmc_redfish_utils object.
16 """
17 # Obtain a reference to the global redfish object.
18 self._redfish_ = BuiltIn().get_library_instance('redfish')
19
20 def get_attribute(self, resource_path, attribute):
21 r"""
22 Get resource attribute.
23
24 Description of argument(s):
25 resource_path URI resource absolute path (e.g. "/redfish/v1/Systems/1").
26 attribute Name of the attribute (e.g. 'PowerState').
27 """
28
29 resp = self._redfish_.get(resource_path)
30 if attribute in resp.dict:
31 return resp.dict[attribute]
32
33 return None
34
35 def list_request(self, resource_path):
36 r"""
37 Perform a GET list request and return available resource paths.
38
39 Description of argument(s):
40 resource_path URI resource absolute path
41 (e.g. "/redfish/v1/SessionService/Sessions").
42 """
43
44 global resource_list
45 resource_list = []
46
47 self._rest_response_ = self._redfish_.get(resource_path)
48
49 # Return empty list.
50 if self._rest_response_.status != 200:
51 return resource_list
52
53 self.walk_nested_dict(self._rest_response_.dict)
54
55 if not resource_list:
56 return uri_path
57
58 for resource in resource_list:
59 self._rest_response_ = self._redfish_.get(resource)
60 if self._rest_response_.status != 200:
61 continue
62 self.walk_nested_dict(self._rest_response_.dict)
63
64 resource_list.sort()
65 return resource_list
66
67 def enumerate_request(self, resource_path):
68 r"""
69 Perform a GET enumerate request and return available resource paths.
70
71 Description of argument(s):
72 resource_path URI resource absolute path
73 (e.g. "/redfish/v1/SessionService/Sessions").
74 """
75
76 url_list = self.list_request(resource_path)
77
78 resource_dict = {}
79
80 # Return empty dict.
81 if not url_list:
82 return resource_dict
83
84 for resource in url_list:
85 self._rest_response_ = self._redfish_.get(resource)
86 if self._rest_response_.status != 200:
87 continue
88 resource_dict[resource] = self._rest_response_.dict
89
90 return json.dumps(resource_dict, sort_keys=True,
91 indent=4, separators=(',', ': '))
92
93 def walk_nested_dict(self, data):
94 r"""
95 Parse through the nested dictionary and get the resource id paths.
96
97 Description of argument(s):
98 data Nested dictionary data from response message.
99 """
100
101 for key, value in data.items():
102 if isinstance(value, dict):
103 self.walk_nested_dict(value)
104 else:
105 if 'Members' == key:
106 if isinstance(value, list):
107 for index in value:
108 if index['@odata.id'] not in resource_list:
109 resource_list.append(index['@odata.id'])
110 if '@odata.id' == key:
111 if value not in resource_list and not value.endswith('/'):
112 resource_list.append(value)