Redfish python utility
Changes:
- New redfish utility library code.
- Move generic utility functions to new library.
- Update test cases with new utility object acces.
Change-Id: Ia2bb18cb46ca93503853bbfd1773d3e4447339c9
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/lib/bmc_redfish.py b/lib/bmc_redfish.py
index 79d2aaa..92001c9 100644
--- a/lib/bmc_redfish.py
+++ b/lib/bmc_redfish.py
@@ -6,7 +6,6 @@
"""
import redfish
-import json
from robot.libraries.BuiltIn import BuiltIn
@@ -135,98 +134,3 @@
Logout redfish connection session.
"""
self._robj_.logout()
-
- def get_attribute(self, resource_path, attribute):
- r"""
- Perform a GET request and return attribute value.
-
- Description of argument(s):
- resource_path URI resource absolute path (e.g. "/redfish/v1/Systems/1").
- attribute Property name (e.g. "PowerState").
- """
-
- resp = self._robj_.get(resource_path)
-
- if attribute in resp.dict:
- return resp.dict[attribute]
-
- return None
-
- def list_request(self, resource_path):
- r"""
- Perform a GET list request and return available resource paths.
-
- Description of argument(s):
- resource_path URI resource absolute path
- (e.g. "/redfish/v1/SessionService/Sessions").
- """
-
- global resource_list
- resource_list = []
-
- self._rest_response_ = self._robj_.get(resource_path)
-
- # Return empty list.
- if self._rest_response_.status != 200:
- return resource_list
-
- self.walk_nested_dict(self._rest_response_.dict)
-
- if not resource_list:
- return uri_path
-
- for resource in resource_list:
- self._rest_response_ = self._robj_.get(resource)
- if self._rest_response_.status != 200:
- continue
- self.walk_nested_dict(self._rest_response_.dict)
-
- resource_list.sort()
- return resource_list
-
- def enumerate_request(self, resource_path):
- r"""
- Perform a GET enumerate request and return available resource paths.
-
- Description of argument(s):
- resource_path URI resource absolute path
- (e.g. "/redfish/v1/SessionService/Sessions").
- """
-
- url_list = self.list_request(resource_path)
-
- resource_dict = {}
-
- # Return empty dict.
- if not url_list:
- return resource_dict
-
- for resource in url_list:
- self._rest_response_ = self._robj_.get(resource)
- if self._rest_response_.status != 200:
- continue
- resource_dict[resource] = self._rest_response_.dict
-
- return json.dumps(resource_dict, sort_keys=True,
- indent=4, separators=(',', ': '))
-
- def walk_nested_dict(self, data):
- r"""
- Parse through the nested dictionary and get the resource id paths.
-
- Description of argument(s):
- data Nested dictionary data from response message.
- """
-
- for key, value in data.items():
- if isinstance(value, dict):
- self.walk_nested_dict(value)
- else:
- if 'Members' == key:
- if isinstance(value, list):
- for index in value:
- if index['@odata.id'] not in resource_list:
- resource_list.append(index['@odata.id'])
- if '@odata.id' == key:
- if value not in resource_list and not value.endswith('/'):
- resource_list.append(value)
diff --git a/lib/bmc_redfish_resource.robot b/lib/bmc_redfish_resource.robot
index 9ed49c7..ab0db3d 100644
--- a/lib/bmc_redfish_resource.robot
+++ b/lib/bmc_redfish_resource.robot
@@ -6,6 +6,7 @@
Library bmc_redfish.py
... ${OPENBMC_HOST} ${OPENBMC_USERNAME} ${OPENBMC_PASSWORD}
... WITH NAME redfish
+Library bmc_redfish_utils.py WITH NAME redfish_utils
Library disable_warning_urllib.py
*** Keywords ***
diff --git a/lib/bmc_redfish_utils.py b/lib/bmc_redfish_utils.py
new file mode 100644
index 0000000..ca4653b
--- /dev/null
+++ b/lib/bmc_redfish_utils.py
@@ -0,0 +1,112 @@
+#!/usr/bin/env python
+
+r"""
+BMC redfish utility functions.
+"""
+
+import json
+from robot.libraries.BuiltIn import BuiltIn
+
+
+class bmc_redfish_utils(object):
+
+ def __init__(self):
+ r"""
+ Initialize the bmc_redfish_utils object.
+ """
+ # Obtain a reference to the global redfish object.
+ self._redfish_ = BuiltIn().get_library_instance('redfish')
+
+ def get_attribute(self, resource_path, attribute):
+ r"""
+ Get resource attribute.
+
+ Description of argument(s):
+ resource_path URI resource absolute path (e.g. "/redfish/v1/Systems/1").
+ attribute Name of the attribute (e.g. 'PowerState').
+ """
+
+ resp = self._redfish_.get(resource_path)
+ if attribute in resp.dict:
+ return resp.dict[attribute]
+
+ return None
+
+ def list_request(self, resource_path):
+ r"""
+ Perform a GET list request and return available resource paths.
+
+ Description of argument(s):
+ resource_path URI resource absolute path
+ (e.g. "/redfish/v1/SessionService/Sessions").
+ """
+
+ global resource_list
+ resource_list = []
+
+ self._rest_response_ = self._redfish_.get(resource_path)
+
+ # Return empty list.
+ if self._rest_response_.status != 200:
+ return resource_list
+
+ self.walk_nested_dict(self._rest_response_.dict)
+
+ if not resource_list:
+ return uri_path
+
+ for resource in resource_list:
+ self._rest_response_ = self._redfish_.get(resource)
+ if self._rest_response_.status != 200:
+ continue
+ self.walk_nested_dict(self._rest_response_.dict)
+
+ resource_list.sort()
+ return resource_list
+
+ def enumerate_request(self, resource_path):
+ r"""
+ Perform a GET enumerate request and return available resource paths.
+
+ Description of argument(s):
+ resource_path URI resource absolute path
+ (e.g. "/redfish/v1/SessionService/Sessions").
+ """
+
+ url_list = self.list_request(resource_path)
+
+ resource_dict = {}
+
+ # Return empty dict.
+ if not url_list:
+ return resource_dict
+
+ for resource in url_list:
+ self._rest_response_ = self._redfish_.get(resource)
+ if self._rest_response_.status != 200:
+ continue
+ resource_dict[resource] = self._rest_response_.dict
+
+ return json.dumps(resource_dict, sort_keys=True,
+ indent=4, separators=(',', ': '))
+
+ def walk_nested_dict(self, data):
+ r"""
+ Parse through the nested dictionary and get the resource id paths.
+
+ Description of argument(s):
+ data Nested dictionary data from response message.
+ """
+
+ for key, value in data.items():
+ if isinstance(value, dict):
+ self.walk_nested_dict(value)
+ else:
+ if 'Members' == key:
+ if isinstance(value, list):
+ for index in value:
+ if index['@odata.id'] not in resource_list:
+ resource_list.append(index['@odata.id'])
+ if '@odata.id' == key:
+ if value not in resource_list and not value.endswith('/'):
+ resource_list.append(value)
diff --git a/lib/openbmc_ffdc_methods.robot b/lib/openbmc_ffdc_methods.robot
index 97ddcab..a436c63 100755
--- a/lib/openbmc_ffdc_methods.robot
+++ b/lib/openbmc_ffdc_methods.robot
@@ -482,7 +482,7 @@
Return From Keyword If ${status} == ${False}
# Get the Redfish resources and properties.
- ${json_data}= redfish.Enumerate Request /redfish/v1
+ ${json_data}= redfish_utils.Enumerate Request /redfish/v1
# Typical output:
# {
# "@odata.id": "/redfish/v1",
diff --git a/redfish/service_root/test_service_root.robot b/redfish/service_root/test_service_root.robot
index e60793e..23a2ea6 100644
--- a/redfish/service_root/test_service_root.robot
+++ b/redfish/service_root/test_service_root.robot
@@ -69,10 +69,10 @@
# Example o/p:
# [{'@odata.id': '/redfish/v1/SessionService/Sessions/bOol3WlCI8'},
# {'@odata.id': '/redfish/v1/SessionService/Sessions/Yu3xFqjZr1'}]
- ${resp_list}= redfish.List Request /redfish/v1/SessionService/Sessions
+ ${resp_list}= redfish_utils.List Request /redfish/v1/SessionService/Sessions
redfish.Delete ${resp_list[1]}
- ${resp}= redfish.List Request /redfish/v1/SessionService/Sessions
+ ${resp}= redfish_utils.List Request /redfish/v1/SessionService/Sessions
List Should Not Contain Value ${resp} ${resp_list[1]}