George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
George Keishing | 97c9394 | 2019-03-04 12:45:07 -0600 | [diff] [blame] | 4 | See class prolog below for details. |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 5 | """ |
| 6 | |
Michael Walsh | ce7c4b5 | 2019-03-20 17:33:15 -0500 | [diff] [blame] | 7 | import sys |
| 8 | import re |
Michael Walsh | 1a611fb | 2020-01-14 17:22:07 -0600 | [diff] [blame] | 9 | import json |
George Keishing | 97c9394 | 2019-03-04 12:45:07 -0600 | [diff] [blame] | 10 | from redfish_plus import redfish_plus |
George Keishing | 2296e8c | 2019-02-01 05:49:58 -0600 | [diff] [blame] | 11 | from robot.libraries.BuiltIn import BuiltIn |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 12 | |
Michael Walsh | 5cc8919 | 2019-03-12 16:43:38 -0500 | [diff] [blame] | 13 | import func_args as fa |
Michael Walsh | ce7c4b5 | 2019-03-20 17:33:15 -0500 | [diff] [blame] | 14 | import gen_print as gp |
Michael Walsh | 5cc8919 | 2019-03-12 16:43:38 -0500 | [diff] [blame] | 15 | |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 16 | |
Tony Lee | 05aa70b | 2021-01-28 19:18:27 +0800 | [diff] [blame] | 17 | MTLS_ENABLED = BuiltIn().get_variable_value("${MTLS_ENABLED}") |
| 18 | |
| 19 | |
George Keishing | 97c9394 | 2019-03-04 12:45:07 -0600 | [diff] [blame] | 20 | class bmc_redfish(redfish_plus): |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 21 | r""" |
Michael Walsh | 5cc8919 | 2019-03-12 16:43:38 -0500 | [diff] [blame] | 22 | bmc_redfish is a child class of redfish_plus that is designed to provide |
George Keishing | 97c9394 | 2019-03-04 12:45:07 -0600 | [diff] [blame] | 23 | benefits specifically for using redfish to communicate with an OpenBMC. |
| 24 | |
| 25 | See the prologs of the methods below for details. |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 26 | """ |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 27 | |
Michael Walsh | ce7c4b5 | 2019-03-20 17:33:15 -0500 | [diff] [blame] | 28 | def __init__(self, *args, **kwargs): |
| 29 | r""" |
| 30 | Do BMC-related redfish initialization. |
| 31 | |
| 32 | Presently, older versions of BMC code may not support redfish |
| 33 | requests. This can lead to unsightly error text being printed out for |
| 34 | programs that may use lib/bmc_redfish_resource.robot even though they |
| 35 | don't necessarily intend to make redfish requests. |
| 36 | |
| 37 | This class method will make an attempt to tolerate this situation. At |
| 38 | some future point, when all BMCs can be expected to support redfish, |
| 39 | this class method may be considered for deletion. If it is deleted, |
| 40 | the self.__inited__ test code in the login() class method below should |
| 41 | likewise be deleted. |
| 42 | """ |
| 43 | self.__inited__ = False |
| 44 | try: |
Tony Lee | 05aa70b | 2021-01-28 19:18:27 +0800 | [diff] [blame] | 45 | if MTLS_ENABLED == 'True': |
| 46 | self.__inited__ = True |
| 47 | else: |
| 48 | super(bmc_redfish, self).__init__(*args, **kwargs) |
| 49 | self.__inited__ = True |
Michael Walsh | ce7c4b5 | 2019-03-20 17:33:15 -0500 | [diff] [blame] | 50 | except ValueError as get_exception: |
| 51 | except_type, except_value, except_traceback = sys.exc_info() |
| 52 | regex = r"The HTTP status code was not valid:[\r\n]+status:[ ]+502" |
| 53 | result = re.match(regex, str(except_value), flags=re.MULTILINE) |
| 54 | if not result: |
| 55 | gp.lprint_var(except_type) |
| 56 | gp.lprint_varx("except_value", str(except_value)) |
| 57 | raise(get_exception) |
Michael Walsh | e58df1c | 2019-08-07 09:57:43 -0500 | [diff] [blame] | 58 | BuiltIn().set_global_variable("${REDFISH_SUPPORTED}", self.__inited__) |
George Keishing | b131ff3 | 2020-10-13 09:18:24 -0500 | [diff] [blame] | 59 | BuiltIn().set_global_variable("${REDFISH_REST_SUPPORTED}", True) |
Michael Walsh | ce7c4b5 | 2019-03-20 17:33:15 -0500 | [diff] [blame] | 60 | |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 61 | def login(self, *args, **kwargs): |
| 62 | r""" |
George Keishing | 97c9394 | 2019-03-04 12:45:07 -0600 | [diff] [blame] | 63 | Assign BMC default values for username, password and auth arguments |
| 64 | and call parent class login method. |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 65 | |
| 66 | Description of argument(s): |
Michael Walsh | 5cc8919 | 2019-03-12 16:43:38 -0500 | [diff] [blame] | 67 | args See parent class method prolog for details. |
| 68 | kwargs See parent class method prolog for details. |
George Keishing | e62d8b0 | 2018-11-29 12:01:56 -0600 | [diff] [blame] | 69 | """ |
George Keishing | 4c39401 | 2019-02-01 06:03:02 -0600 | [diff] [blame] | 70 | |
Tony Lee | 05aa70b | 2021-01-28 19:18:27 +0800 | [diff] [blame] | 71 | if MTLS_ENABLED == 'True': |
| 72 | return None |
Michael Walsh | ce7c4b5 | 2019-03-20 17:33:15 -0500 | [diff] [blame] | 73 | if not self.__inited__: |
Michael Walsh | 707ed0e | 2019-05-17 15:27:25 -0500 | [diff] [blame] | 74 | message = "bmc_redfish.__init__() was never successfully run. It " |
| 75 | message += "is likely that the target BMC firmware code level " |
| 76 | message += "does not support redfish.\n" |
Michael Walsh | ce7c4b5 | 2019-03-20 17:33:15 -0500 | [diff] [blame] | 77 | raise ValueError(message) |
George Keishing | 97c9394 | 2019-03-04 12:45:07 -0600 | [diff] [blame] | 78 | # Assign default values for username, password, auth where necessary. |
Michael Walsh | 5cc8919 | 2019-03-12 16:43:38 -0500 | [diff] [blame] | 79 | openbmc_username = BuiltIn().get_variable_value("${OPENBMC_USERNAME}") |
| 80 | openbmc_password = BuiltIn().get_variable_value("${OPENBMC_PASSWORD}") |
| 81 | username, args, kwargs = fa.pop_arg(openbmc_username, *args, **kwargs) |
| 82 | password, args, kwargs = fa.pop_arg(openbmc_password, *args, **kwargs) |
| 83 | auth, args, kwargs = fa.pop_arg('session', *args, **kwargs) |
George Keishing | 4c39401 | 2019-02-01 06:03:02 -0600 | [diff] [blame] | 84 | |
Michael Walsh | ce7c4b5 | 2019-03-20 17:33:15 -0500 | [diff] [blame] | 85 | super(bmc_redfish, self).login(username, password, auth, |
| 86 | *args, **kwargs) |
Michael Walsh | 5cc8919 | 2019-03-12 16:43:38 -0500 | [diff] [blame] | 87 | |
Tony Lee | 05aa70b | 2021-01-28 19:18:27 +0800 | [diff] [blame] | 88 | def logout(self): |
| 89 | |
| 90 | if MTLS_ENABLED == 'True': |
| 91 | return None |
| 92 | else: |
| 93 | super(bmc_redfish, self).logout() |
| 94 | |
Michael Walsh | 5cc8919 | 2019-03-12 16:43:38 -0500 | [diff] [blame] | 95 | def get_properties(self, *args, **kwargs): |
| 96 | r""" |
| 97 | Return dictionary of attributes for a given path. |
| 98 | |
| 99 | The difference between calling this function and calling get() |
| 100 | directly is that this function returns ONLY the dictionary portion of |
| 101 | the response object. |
| 102 | |
| 103 | Example robot code: |
| 104 | |
| 105 | ${properties}= Get Properties /redfish/v1/Systems/system/ |
Michael Walsh | 39c0051 | 2019-07-17 10:54:06 -0500 | [diff] [blame] | 106 | Rprint Vars properties |
Michael Walsh | 5cc8919 | 2019-03-12 16:43:38 -0500 | [diff] [blame] | 107 | |
| 108 | Output: |
| 109 | |
| 110 | properties: |
| 111 | [PowerState]: Off |
| 112 | [Processors]: |
| 113 | [@odata.id]: /redfish/v1/Systems/system/Processors |
| 114 | [SerialNumber]: 1234567 |
| 115 | ... |
| 116 | |
| 117 | Description of argument(s): |
| 118 | args See parent class get() prolog for details. |
| 119 | kwargs See parent class get() prolog for details. |
| 120 | """ |
| 121 | |
| 122 | resp = self.get(*args, **kwargs) |
| 123 | return resp.dict if hasattr(resp, 'dict') else {} |
| 124 | |
| 125 | def get_attribute(self, path, attribute, default=None, *args, **kwargs): |
| 126 | r""" |
| 127 | Get and return the named attribute from the properties for a given |
| 128 | path. |
| 129 | |
| 130 | This method has the following advantages over calling get_properties |
| 131 | directly: |
| 132 | - The caller can specify a default value to be returned if the |
| 133 | attribute does not exist. |
| 134 | |
| 135 | Example robot code: |
| 136 | |
| 137 | ${attribute}= Get Attribute /redfish/v1/AccountService |
| 138 | ... MaxPasswordLength default=600 |
| 139 | Rprint Vars attribute |
| 140 | |
| 141 | Output: |
| 142 | |
| 143 | attribute: 31 |
| 144 | |
| 145 | Description of argument(s): |
| 146 | path The path (e.g. |
| 147 | "/redfish/v1/AccountService"). |
| 148 | attribute The name of the attribute to be retrieved |
| 149 | (e.g. "MaxPasswordLength"). |
| 150 | default The default value to be returned if the |
| 151 | attribute does not exist (e.g. ""). |
| 152 | args See parent class get() prolog for details. |
| 153 | kwargs See parent class get() prolog for details. |
| 154 | """ |
| 155 | |
| 156 | return self.get_properties(path, *args, **kwargs).get(attribute, |
| 157 | default) |
| 158 | |
| 159 | def get_session_info(self): |
| 160 | r""" |
| 161 | Get and return session info as a tuple consisting of session_key and |
| 162 | session_location. |
| 163 | """ |
| 164 | |
| 165 | return self.get_session_key(), self.get_session_location() |
Michael Walsh | 1a611fb | 2020-01-14 17:22:07 -0600 | [diff] [blame] | 166 | |
| 167 | def enumerate(self, resource_path, return_json=1, include_dead_resources=False): |
| 168 | r""" |
| 169 | Perform a GET enumerate request and return available resource paths. |
| 170 | |
| 171 | Description of argument(s): |
| 172 | resource_path URI resource absolute path (e.g. "/redfish/v1/SessionService/Sessions"). |
| 173 | return_json Indicates whether the result should be returned as a json string or as a |
| 174 | dictionary. |
| 175 | include_dead_resources Check and return a list of dead/broken URI resources. |
| 176 | """ |
| 177 | |
| 178 | gp.qprint_executing(style=gp.func_line_style_short) |
| 179 | # Set quiet variable to keep subordinate get() calls quiet. |
| 180 | quiet = 1 |
| 181 | |
| 182 | self.__result = {} |
| 183 | # Variable to hold the pending list of resources for which enumeration is yet to be obtained. |
| 184 | self.__pending_enumeration = set() |
| 185 | self.__pending_enumeration.add(resource_path) |
| 186 | |
| 187 | # Variable having resources for which enumeration is completed. |
| 188 | enumerated_resources = set() |
| 189 | dead_resources = {} |
| 190 | resources_to_be_enumerated = (resource_path,) |
| 191 | while resources_to_be_enumerated: |
| 192 | for resource in resources_to_be_enumerated: |
| 193 | # JsonSchemas, SessionService or URLs containing # are not required in enumeration. |
| 194 | # Example: '/redfish/v1/JsonSchemas/' and sub resources. |
| 195 | # '/redfish/v1/SessionService' |
| 196 | # '/redfish/v1/Managers/bmc#/Oem' |
| 197 | if ('JsonSchemas' in resource) or ('SessionService' in resource) or ('#' in resource): |
| 198 | continue |
| 199 | |
| 200 | self._rest_response_ = self.get(resource, valid_status_codes=[200, 404, 500]) |
| 201 | # Enumeration is done for available resources ignoring the ones for which response is not |
| 202 | # obtained. |
| 203 | if self._rest_response_.status != 200: |
| 204 | if include_dead_resources: |
| 205 | try: |
| 206 | dead_resources[self._rest_response_.status].append(resource) |
| 207 | except KeyError: |
| 208 | dead_resources[self._rest_response_.status] = [resource] |
| 209 | continue |
| 210 | self.walk_nested_dict(self._rest_response_.dict, url=resource) |
| 211 | |
| 212 | enumerated_resources.update(set(resources_to_be_enumerated)) |
| 213 | resources_to_be_enumerated = tuple(self.__pending_enumeration - enumerated_resources) |
| 214 | |
| 215 | if return_json: |
| 216 | if include_dead_resources: |
| 217 | return json.dumps(self.__result, sort_keys=True, |
| 218 | indent=4, separators=(',', ': ')), dead_resources |
| 219 | else: |
| 220 | return json.dumps(self.__result, sort_keys=True, |
| 221 | indent=4, separators=(',', ': ')) |
| 222 | else: |
| 223 | if include_dead_resources: |
| 224 | return self.__result, dead_resources |
| 225 | else: |
| 226 | return self.__result |
| 227 | |
| 228 | def walk_nested_dict(self, data, url=''): |
| 229 | r""" |
| 230 | Parse through the nested dictionary and get the resource id paths. |
| 231 | |
| 232 | Description of argument(s): |
| 233 | data Nested dictionary data from response message. |
| 234 | url Resource for which the response is obtained in data. |
| 235 | """ |
| 236 | url = url.rstrip('/') |
| 237 | |
| 238 | for key, value in data.items(): |
| 239 | |
| 240 | # Recursion if nested dictionary found. |
| 241 | if isinstance(value, dict): |
| 242 | self.walk_nested_dict(value) |
| 243 | else: |
| 244 | # Value contains a list of dictionaries having member data. |
| 245 | if 'Members' == key: |
| 246 | if isinstance(value, list): |
| 247 | for memberDict in value: |
| 248 | self.__pending_enumeration.add(memberDict['@odata.id']) |
| 249 | if '@odata.id' == key: |
| 250 | value = value.rstrip('/') |
| 251 | # Data for the given url. |
| 252 | if value == url: |
| 253 | self.__result[url] = data |
| 254 | # Data still needs to be looked up, |
| 255 | else: |
| 256 | self.__pending_enumeration.add(value) |