George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | BMC redfish utility functions. |
| 5 | """ |
| 6 | |
| 7 | import json |
| 8 | from robot.libraries.BuiltIn import BuiltIn |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 9 | import gen_print as gp |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 10 | |
| 11 | |
| 12 | class bmc_redfish_utils(object): |
| 13 | |
George Keishing | eb1fe35 | 2020-06-19 03:02:22 -0500 | [diff] [blame] | 14 | ROBOT_LIBRARY_SCOPE = 'TEST SUITE' |
| 15 | |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 16 | def __init__(self): |
| 17 | r""" |
| 18 | Initialize the bmc_redfish_utils object. |
| 19 | """ |
| 20 | # Obtain a reference to the global redfish object. |
George Keishing | eb1fe35 | 2020-06-19 03:02:22 -0500 | [diff] [blame] | 21 | self.__inited__ = False |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 22 | self._redfish_ = BuiltIn().get_library_instance('redfish') |
| 23 | |
George Keishing | eb1fe35 | 2020-06-19 03:02:22 -0500 | [diff] [blame] | 24 | # There is a possibility that a given driver support both redfish and |
| 25 | # legacy REST. |
| 26 | self._redfish_.login() |
| 27 | self._rest_response_ = \ |
| 28 | self._redfish_.get("/xyz/openbmc_project/", valid_status_codes=[200, 404]) |
| 29 | |
| 30 | # If REST URL /xyz/openbmc_project/ is supported. |
| 31 | if self._rest_response_.status == 200: |
| 32 | self.__inited__ = True |
| 33 | |
| 34 | BuiltIn().set_global_variable("${REDFISH_REST_SUPPORTED}", self.__inited__) |
| 35 | |
George Keishing | 374e684 | 2019-02-20 08:57:18 -0600 | [diff] [blame] | 36 | def get_redfish_session_info(self): |
| 37 | r""" |
| 38 | Returns redfish sessions info dictionary. |
| 39 | |
| 40 | { |
| 41 | 'key': 'yLXotJnrh5nDhXj5lLiH' , |
| 42 | 'location': '/redfish/v1/SessionService/Sessions/nblYY4wlz0' |
| 43 | } |
| 44 | """ |
| 45 | session_dict = { |
George Keishing | 97c9394 | 2019-03-04 12:45:07 -0600 | [diff] [blame] | 46 | "key": self._redfish_.get_session_key(), |
| 47 | "location": self._redfish_.get_session_location() |
George Keishing | 374e684 | 2019-02-20 08:57:18 -0600 | [diff] [blame] | 48 | } |
| 49 | return session_dict |
| 50 | |
Sandhya Somashekar | 37122b6 | 2019-06-18 06:02:02 -0500 | [diff] [blame] | 51 | def get_attribute(self, resource_path, attribute, verify=None): |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 52 | r""" |
| 53 | Get resource attribute. |
| 54 | |
| 55 | Description of argument(s): |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 56 | resource_path URI resource absolute path (e.g. |
| 57 | "/redfish/v1/Systems/1"). |
| 58 | attribute Name of the attribute (e.g. 'PowerState'). |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 59 | """ |
| 60 | |
| 61 | resp = self._redfish_.get(resource_path) |
Sandhya Somashekar | 37122b6 | 2019-06-18 06:02:02 -0500 | [diff] [blame] | 62 | |
| 63 | if verify: |
| 64 | if resp.dict[attribute] == verify: |
| 65 | return resp.dict[attribute] |
| 66 | else: |
| 67 | raise ValueError("Attribute value is not equal") |
| 68 | elif attribute in resp.dict: |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 69 | return resp.dict[attribute] |
| 70 | |
| 71 | return None |
| 72 | |
George Keishing | c3c05c2 | 2019-02-19 01:04:54 -0600 | [diff] [blame] | 73 | def get_properties(self, resource_path): |
| 74 | r""" |
| 75 | Returns dictionary of attributes for the resource. |
| 76 | |
| 77 | Description of argument(s): |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 78 | resource_path URI resource absolute path (e.g. |
Sandhya Somashekar | 37122b6 | 2019-06-18 06:02:02 -0500 | [diff] [blame] | 79 | /redfish/v1/Systems/1"). |
George Keishing | c3c05c2 | 2019-02-19 01:04:54 -0600 | [diff] [blame] | 80 | """ |
| 81 | |
| 82 | resp = self._redfish_.get(resource_path) |
| 83 | return resp.dict |
| 84 | |
George Keishing | 7ec4593 | 2019-02-27 14:02:16 -0600 | [diff] [blame] | 85 | def get_target_actions(self, resource_path, target_attribute): |
| 86 | r""" |
| 87 | Returns resource target entry of the searched target attribute. |
| 88 | |
| 89 | Description of argument(s): |
| 90 | resource_path URI resource absolute path |
George Keishing | f8acde9 | 2019-04-19 19:46:48 +0000 | [diff] [blame] | 91 | (e.g. "/redfish/v1/Systems/system"). |
George Keishing | 7ec4593 | 2019-02-27 14:02:16 -0600 | [diff] [blame] | 92 | target_attribute Name of the attribute (e.g. 'ComputerSystem.Reset'). |
| 93 | |
| 94 | Example: |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 95 | "Actions": { |
| 96 | "#ComputerSystem.Reset": { |
| 97 | "ResetType@Redfish.AllowableValues": [ |
George Keishing | 7ec4593 | 2019-02-27 14:02:16 -0600 | [diff] [blame] | 98 | "On", |
| 99 | "ForceOff", |
| 100 | "GracefulRestart", |
| 101 | "GracefulShutdown" |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 102 | ], |
| 103 | "target": "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset" |
| 104 | } |
| 105 | } |
George Keishing | 7ec4593 | 2019-02-27 14:02:16 -0600 | [diff] [blame] | 106 | """ |
| 107 | |
| 108 | global target_list |
| 109 | target_list = [] |
| 110 | |
| 111 | resp_dict = self.get_attribute(resource_path, "Actions") |
| 112 | if resp_dict is None: |
| 113 | return None |
| 114 | |
| 115 | # Recursively search the "target" key in the nested dictionary. |
| 116 | # Populate the target_list of target entries. |
| 117 | self.get_key_value_nested_dict(resp_dict, "target") |
George Keishing | 7ec4593 | 2019-02-27 14:02:16 -0600 | [diff] [blame] | 118 | # Return the matching target URL entry. |
| 119 | for target in target_list: |
| 120 | # target "/redfish/v1/Systems/system/Actions/ComputerSystem.Reset" |
| 121 | if target_attribute in target: |
| 122 | return target |
| 123 | |
| 124 | return None |
| 125 | |
George Keishing | dabf38f | 2019-03-10 09:52:40 -0500 | [diff] [blame] | 126 | def get_member_list(self, resource_path): |
| 127 | r""" |
| 128 | Perform a GET list request and return available members entries. |
| 129 | |
| 130 | Description of argument(s): |
| 131 | resource_path URI resource absolute path |
George Keishing | f8acde9 | 2019-04-19 19:46:48 +0000 | [diff] [blame] | 132 | (e.g. "/redfish/v1/SessionService/Sessions"). |
George Keishing | dabf38f | 2019-03-10 09:52:40 -0500 | [diff] [blame] | 133 | |
| 134 | "Members": [ |
| 135 | { |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 136 | "@odata.id": "/redfish/v1/SessionService/Sessions/Z5HummWPZ7" |
George Keishing | dabf38f | 2019-03-10 09:52:40 -0500 | [diff] [blame] | 137 | } |
| 138 | { |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 139 | "@odata.id": "/redfish/v1/SessionService/Sessions/46CmQmEL7H" |
George Keishing | dabf38f | 2019-03-10 09:52:40 -0500 | [diff] [blame] | 140 | } |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 141 | ], |
George Keishing | dabf38f | 2019-03-10 09:52:40 -0500 | [diff] [blame] | 142 | """ |
| 143 | |
| 144 | member_list = [] |
| 145 | resp_list_dict = self.get_attribute(resource_path, "Members") |
| 146 | if resp_list_dict is None: |
| 147 | return member_list |
| 148 | |
| 149 | for member_id in range(0, len(resp_list_dict)): |
| 150 | member_list.append(resp_list_dict[member_id]["@odata.id"]) |
| 151 | |
| 152 | return member_list |
| 153 | |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 154 | def list_request(self, resource_path): |
| 155 | r""" |
| 156 | Perform a GET list request and return available resource paths. |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 157 | Description of argument(s): |
| 158 | resource_path URI resource absolute path |
George Keishing | f8acde9 | 2019-04-19 19:46:48 +0000 | [diff] [blame] | 159 | (e.g. "/redfish/v1/SessionService/Sessions"). |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 160 | """ |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 161 | gp.qprint_executing(style=gp.func_line_style_short) |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 162 | # Set quiet variable to keep subordinate get() calls quiet. |
| 163 | quiet = 1 |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 164 | self.__pending_enumeration = set() |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 165 | self._rest_response_ = \ |
George Keishing | f8acde9 | 2019-04-19 19:46:48 +0000 | [diff] [blame] | 166 | self._redfish_.get(resource_path, |
| 167 | valid_status_codes=[200, 404, 500]) |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 168 | |
| 169 | # Return empty list. |
| 170 | if self._rest_response_.status != 200: |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 171 | return self.__pending_enumeration |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 172 | self.walk_nested_dict(self._rest_response_.dict) |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 173 | if not self.__pending_enumeration: |
| 174 | return resource_path |
| 175 | for resource in self.__pending_enumeration.copy(): |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 176 | self._rest_response_ = \ |
George Keishing | f8acde9 | 2019-04-19 19:46:48 +0000 | [diff] [blame] | 177 | self._redfish_.get(resource, |
| 178 | valid_status_codes=[200, 404, 500]) |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 179 | |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 180 | if self._rest_response_.status != 200: |
| 181 | continue |
| 182 | self.walk_nested_dict(self._rest_response_.dict) |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 183 | return list(sorted(self.__pending_enumeration)) |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 184 | |
Anusha Dathatri | 3e7930d | 2019-11-06 03:55:35 -0600 | [diff] [blame] | 185 | def enumerate_request(self, resource_path, return_json=1, |
| 186 | include_dead_resources=False): |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 187 | r""" |
| 188 | Perform a GET enumerate request and return available resource paths. |
| 189 | |
| 190 | Description of argument(s): |
Michael Walsh | 37e028f | 2019-05-22 16:16:32 -0500 | [diff] [blame] | 191 | resource_path URI resource absolute path (e.g. |
| 192 | "/redfish/v1/SessionService/Sessions"). |
| 193 | return_json Indicates whether the result should be |
| 194 | returned as a json string or as a |
| 195 | dictionary. |
Anusha Dathatri | 3e7930d | 2019-11-06 03:55:35 -0600 | [diff] [blame] | 196 | include_dead_resources Check and return a list of dead/broken URI |
| 197 | resources. |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 198 | """ |
| 199 | |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 200 | gp.qprint_executing(style=gp.func_line_style_short) |
| 201 | |
Michael Walsh | 37e028f | 2019-05-22 16:16:32 -0500 | [diff] [blame] | 202 | return_json = int(return_json) |
| 203 | |
Michael Walsh | c86a2f7 | 2019-03-19 13:24:37 -0500 | [diff] [blame] | 204 | # Set quiet variable to keep subordinate get() calls quiet. |
| 205 | quiet = 1 |
| 206 | |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 207 | # Variable to hold enumerated data. |
| 208 | self.__result = {} |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 209 | |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 210 | # Variable to hold the pending list of resources for which enumeration. |
| 211 | # is yet to be obtained. |
| 212 | self.__pending_enumeration = set() |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 213 | |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 214 | self.__pending_enumeration.add(resource_path) |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 215 | |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 216 | # Variable having resources for which enumeration is completed. |
| 217 | enumerated_resources = set() |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 218 | |
Anusha Dathatri | 3e7930d | 2019-11-06 03:55:35 -0600 | [diff] [blame] | 219 | if include_dead_resources: |
| 220 | dead_resources = {} |
| 221 | |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 222 | resources_to_be_enumerated = (resource_path,) |
| 223 | |
| 224 | while resources_to_be_enumerated: |
| 225 | for resource in resources_to_be_enumerated: |
Anusha Dathatri | 6d2d42f | 2019-11-20 06:17:51 -0600 | [diff] [blame] | 226 | # JsonSchemas, SessionService or URLs containing # are not |
| 227 | # required in enumeration. |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 228 | # Example: '/redfish/v1/JsonSchemas/' and sub resources. |
Anusha Dathatri | cdb77db | 2019-09-10 08:10:29 -0500 | [diff] [blame] | 229 | # '/redfish/v1/SessionService' |
Anusha Dathatri | 6d2d42f | 2019-11-20 06:17:51 -0600 | [diff] [blame] | 230 | # '/redfish/v1/Managers/bmc#/Oem' |
| 231 | if ('JsonSchemas' in resource) or ('SessionService' in resource)\ |
| 232 | or ('#' in resource): |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 233 | continue |
| 234 | |
| 235 | self._rest_response_ = \ |
| 236 | self._redfish_.get(resource, valid_status_codes=[200, 404, 500]) |
| 237 | # Enumeration is done for available resources ignoring the |
| 238 | # ones for which response is not obtained. |
| 239 | if self._rest_response_.status != 200: |
Anusha Dathatri | 3e7930d | 2019-11-06 03:55:35 -0600 | [diff] [blame] | 240 | if include_dead_resources: |
| 241 | try: |
| 242 | dead_resources[self._rest_response_.status].append( |
| 243 | resource) |
| 244 | except KeyError: |
| 245 | dead_resources[self._rest_response_.status] = \ |
| 246 | [resource] |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 247 | continue |
| 248 | |
| 249 | self.walk_nested_dict(self._rest_response_.dict, url=resource) |
| 250 | |
| 251 | enumerated_resources.update(set(resources_to_be_enumerated)) |
| 252 | resources_to_be_enumerated = \ |
| 253 | tuple(self.__pending_enumeration - enumerated_resources) |
| 254 | |
Michael Walsh | 37e028f | 2019-05-22 16:16:32 -0500 | [diff] [blame] | 255 | if return_json: |
Anusha Dathatri | 3e7930d | 2019-11-06 03:55:35 -0600 | [diff] [blame] | 256 | if include_dead_resources: |
| 257 | return json.dumps(self.__result, sort_keys=True, |
| 258 | indent=4, separators=(',', ': ')), dead_resources |
| 259 | else: |
| 260 | return json.dumps(self.__result, sort_keys=True, |
| 261 | indent=4, separators=(',', ': ')) |
Michael Walsh | 37e028f | 2019-05-22 16:16:32 -0500 | [diff] [blame] | 262 | else: |
Anusha Dathatri | 3e7930d | 2019-11-06 03:55:35 -0600 | [diff] [blame] | 263 | if include_dead_resources: |
| 264 | return self.__result, dead_resources |
| 265 | else: |
| 266 | return self.__result |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 267 | |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 268 | def walk_nested_dict(self, data, url=''): |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 269 | r""" |
| 270 | Parse through the nested dictionary and get the resource id paths. |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 271 | Description of argument(s): |
| 272 | data Nested dictionary data from response message. |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 273 | url Resource for which the response is obtained in data. |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 274 | """ |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 275 | url = url.rstrip('/') |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 276 | |
| 277 | for key, value in data.items(): |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 278 | |
| 279 | # Recursion if nested dictionary found. |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 280 | if isinstance(value, dict): |
| 281 | self.walk_nested_dict(value) |
| 282 | else: |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 283 | # Value contains a list of dictionaries having member data. |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 284 | if 'Members' == key: |
| 285 | if isinstance(value, list): |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 286 | for memberDict in value: |
| 287 | self.__pending_enumeration.add(memberDict['@odata.id']) |
George Keishing | f2613b7 | 2019-02-13 12:45:59 -0600 | [diff] [blame] | 288 | if '@odata.id' == key: |
Anusha Dathatri | 62dfb86 | 2019-04-23 06:52:16 -0500 | [diff] [blame] | 289 | value = value.rstrip('/') |
| 290 | # Data for the given url. |
| 291 | if value == url: |
| 292 | self.__result[url] = data |
| 293 | # Data still needs to be looked up, |
| 294 | else: |
| 295 | self.__pending_enumeration.add(value) |
George Keishing | 7ec4593 | 2019-02-27 14:02:16 -0600 | [diff] [blame] | 296 | |
| 297 | def get_key_value_nested_dict(self, data, key): |
| 298 | r""" |
| 299 | Parse through the nested dictionary and get the searched key value. |
| 300 | |
| 301 | Description of argument(s): |
| 302 | data Nested dictionary data from response message. |
| 303 | key Search dictionary key element. |
| 304 | """ |
| 305 | |
| 306 | for k, v in data.items(): |
| 307 | if isinstance(v, dict): |
| 308 | self.get_key_value_nested_dict(v, key) |
| 309 | |
| 310 | if k == key: |
| 311 | target_list.append(v) |