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