New BMC redfish library integration

Changes:
   - Use redfish_plus.py when importing Redfish instances.
   - Update bmc_redfish.py.
   - Update bmc_redfish_utility.py.
   - Update object name 'redfish' to 'Redfish'.
   - Update test setup and teardown in suites.
   - Update response error checking using valid_status_codes.
   - Fix test cases required for migration.

Change-Id: Ida154aede649d9a2bbef66d16ccf725f5ea37ed0
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/lib/bmc_redfish.py b/lib/bmc_redfish.py
index eeab824..fad445e 100644
--- a/lib/bmc_redfish.py
+++ b/lib/bmc_redfish.py
@@ -1,152 +1,40 @@
 #!/usr/bin/env python
 
 r"""
-Using python based redfish library.
-Refer: https://github.com/DMTF/python-redfish-library
+See class prolog below for details.
 """
 
-import redfish
+from redfish_plus import redfish_plus
 from robot.libraries.BuiltIn import BuiltIn
 
 
-class HTTPSBadRequestError(Exception):
+class bmc_redfish(redfish_plus):
     r"""
-    BMC redfish generic raised method for error(s).
+    bmc_redfish is a child class of  redfish_plus that is designed to provide
+    benefits specifically for using redfish to communicate with an OpenBMC.
+
+    See the prologs of the methods below for details.
     """
-    pass
-
-
-class bmc_redfish(object):
-
-    ROBOT_LIBRARY_SCOPE = "TEST SUITE"
-    ROBOT_EXIT_ON_FAILURE = True
-
-    def __init__(self, hostname, username, password, *args, **kwargs):
-        r"""
-        Establish session connection to host.
-
-        Description of argument(s):
-        hostname       The host name or IP address of the server.
-        username       The username to be used to connect to the server.
-        password       The password to be used to connect to the server.
-        args/kwargs    Additional parms which are passed directly
-                       to the redfish_client function.
-        """
-        self._base_url_ = "https://" + hostname
-        self._username_ = username
-        self._password_ = password
-        self._default_prefix_ = "/redfish/v1"
-
-    def __enter__(self):
-        return self
-
-    def __del__(self):
-        del self
 
     def login(self, *args, **kwargs):
         r"""
-        Call the corresponding RestClientBase method and return the result.
+        Assign BMC default values for username, password and auth arguments
+        and call parent class login method.
 
         Description of argument(s):
-        args/kwargs     These are passed directly to the corresponding
-                        RestClientBase method.
+        args                        See parent class prolog for details.
+        kwargs                      See parent class prolog for details.
         """
 
-        for arg in args:
-            hostname = self._base_url_.strip("https://")
-            # Class object constructor reinitialized.
-            self.__init__(hostname=hostname,
-                          username=arg['username'],
-                          password=arg['password'])
+        args = list(args)
+        # Assign default values for username, password, auth where necessary.
+        username = args.pop(0) if args else \
+            kwargs.pop('username',
+                       BuiltIn().get_variable_value("${OPENBMC_USERNAME}"))
+        password = args.pop(0) if args else \
+            kwargs.pop('password',
+                       BuiltIn().get_variable_value("${OPENBMC_PASSWORD}"))
+        auth = args.pop(0) if args else kwargs.pop('auth', 'session')
 
-        self._robj_ = redfish.redfish_client(base_url=self._base_url_,
-                                             username=self._username_,
-                                             password=self._password_,
-                                             default_prefix=self._default_prefix_)
-        self._robj_.login(auth=redfish.AuthMethod.SESSION)
-        self._session_key_ = self._robj_.get_session_key()
-        self._session_location_ = self._robj_.get_session_location()
-
-    def set_session_key(self, session_key):
-        r"""
-        Update the session key instance.
-
-        session_key      Redfish valid session key.
-        """
-        self._robj_.set_session_key(session_key)
-
-    def set_session_location(self, session_location):
-        r"""
-        Update the session location instance.
-
-        session_location   Redfish valid session location.
-                           Example:
-                           /redfish/v1/SessionService/Sessions/j04tD83QQn
-        """
-        self._robj_.set_session_location(session_location)
-
-    def get(self, resource_path, *args, **kwargs):
-        r"""
-        Perform a GET request and return response.
-
-        Description of argument(s):
-        resource_path    URI resource absolute path (e.g. "/redfish/v1/Systems/1").
-        args/kwargs      These are passed directly to the corresponding
-                         RestClientBase method.
-        """
-        self._rest_response_ = self._robj_.get(resource_path, *args, **kwargs)
-        return self._rest_response_
-
-    def post(self, resource_path, *args, **kwargs):
-        r"""
-        Perform a POST request.
-
-        Description of argument(s):
-        resource_path    URI resource relative path
-                         (e.g. "Systems/1/Actions/ComputerSystem.Reset").
-        args/kwargs      These are passed directly to the corresponding
-                         RestClientBase method.
-        """
-        self._rest_response_ = self._robj_.post(resource_path, *args, **kwargs)
-        return self._rest_response_
-
-    def patch(self, resource_path, *args, **kwargs):
-        r"""
-        Perform a POST request.
-
-        Description of argument(s):
-        resource_path    URI resource relative path
-        args/kwargs      These are passed directly to the corresponding
-                         RestClientBase method.
-        """
-        self._rest_response_ = self._robj_.patch(resource_path, *args, **kwargs)
-        return self._rest_response_
-
-    def put(self, resource_path, actions, attr_data):
-        r"""
-        Perform a PUT request.
-
-        Description of argument(s):
-        resource_path    URI resource relative path.
-        args/kwargs      These are passed directly to the corresponding
-                         RestClientBase method.
-        """
-        self._rest_response_ = self._robj_.put(resource_path, *args, **kwargs)
-        return self._rest_response_
-
-    def delete(self, resource_path):
-        r"""
-        Perform a DELETE request.
-
-        Description of argument(s):
-        resource_path  URI resource absolute path
-                       (e.g. "/redfish/v1/SessionService/Sessions/8d1a9wiiNL").
-        """
-        self._rest_response_ = self._robj_.delete(resource_path)
-        return self._rest_response_
-
-    def logout(self):
-        r"""
-        Logout redfish connection session.
-        """
-        self._robj_.logout()
+        super(redfish_plus, self).login(username, password, auth,
+                                        *args, **kwargs)
diff --git a/lib/bmc_redfish_resource.robot b/lib/bmc_redfish_resource.robot
index ab0db3d..ef38830 100644
--- a/lib/bmc_redfish_resource.robot
+++ b/lib/bmc_redfish_resource.robot
@@ -3,9 +3,8 @@
 
 Resource        resource.robot
 Resource        rest_response_code.robot
-Library         bmc_redfish.py
-...             ${OPENBMC_HOST}  ${OPENBMC_USERNAME}  ${OPENBMC_PASSWORD}
-...             WITH NAME  redfish
+Library         bmc_redfish.py  https://${OPENBMC_HOST}  ${OPENBMC_USERNAME}
+...             ${OPENBMC_PASSWORD}  WITH NAME  Redfish
 Library         bmc_redfish_utils.py  WITH NAME  redfish_utils
 Library         disable_warning_urllib.py
 
diff --git a/lib/bmc_redfish_utils.py b/lib/bmc_redfish_utils.py
index 492a1a3..1bf53a7 100644
--- a/lib/bmc_redfish_utils.py
+++ b/lib/bmc_redfish_utils.py
@@ -27,8 +27,8 @@
         }
         """
         session_dict = {
-            "key": self._redfish_._session_key_,
-            "location": self._redfish_._session_location_
+            "key": self._redfish_.get_session_key(),
+            "location": self._redfish_.get_session_location()
         }
         return session_dict
 
@@ -111,8 +111,8 @@
 
         global resource_list
         resource_list = []
-
-        self._rest_response_ = self._redfish_.get(resource_path)
+        self._rest_response_ = \
+            self._redfish_.get(resource_path, valid_status_codes=[200, 404, 500])
 
         # Return empty list.
         if self._rest_response_.status != 200:
@@ -124,7 +124,8 @@
             return uri_path
 
         for resource in resource_list:
-            self._rest_response_ = self._redfish_.get(resource)
+            self._rest_response_ = \
+                self._redfish_.get(resource, valid_status_codes=[200, 404, 500])
             if self._rest_response_.status != 200:
                 continue
             self.walk_nested_dict(self._rest_response_.dict)
@@ -154,7 +155,8 @@
             # Example: '/redfish/v1/JsonSchemas/' and sub resources.
             if 'JsonSchemas' in resource:
                 continue
-            self._rest_response_ = self._redfish_.get(resource)
+            self._rest_response_ = \
+                self._redfish_.get(resource, valid_status_codes=[200, 404, 500])
             if self._rest_response_.status != 200:
                 continue
             resource_dict[resource] = self._rest_response_.dict
diff --git a/lib/openbmc_ffdc_methods.robot b/lib/openbmc_ffdc_methods.robot
index a436c63..cdd6778 100755
--- a/lib/openbmc_ffdc_methods.robot
+++ b/lib/openbmc_ffdc_methods.robot
@@ -478,7 +478,7 @@
 
     # Login is needed to fetch Redfish information.
     # If login fails, return from keyword.
-    ${status}=  Run Keyword And Return Status  redfish.Login
+    ${status}=  Run Keyword And Return Status  Redfish.Login
     Return From Keyword If   ${status} == ${False}
 
     # Get the Redfish resources and properties.