Tolerate failed redfish init for old bmc code

- Older bmc code does not support redfish and therefore robot code that
happens to use lib/bmc_redfish_resource.robot will get unsightly errors even if
these programs don't try to run actual redfish tests.

- Also, made correction to use of super() function in bmc_redfish.py login
method.

Change-Id: I25f9acac69a573a2d668d0fb97d84bce46545b4d
Signed-off-by: Michael Walsh <micwalsh@us.ibm.com>
diff --git a/lib/bmc_redfish.py b/lib/bmc_redfish.py
index 025059b..3baee67 100644
--- a/lib/bmc_redfish.py
+++ b/lib/bmc_redfish.py
@@ -4,10 +4,13 @@
 See class prolog below for details.
 """
 
+import sys
+import re
 from redfish_plus import redfish_plus
 from robot.libraries.BuiltIn import BuiltIn
 
 import func_args as fa
+import gen_print as gp
 
 
 class bmc_redfish(redfish_plus):
@@ -18,6 +21,34 @@
     See the prologs of the methods below for details.
     """
 
+    def __init__(self, *args, **kwargs):
+        r"""
+        Do BMC-related redfish initialization.
+
+        Presently, older versions of BMC code may not support redfish
+        requests.  This can lead to unsightly error text being printed out for
+        programs that may use lib/bmc_redfish_resource.robot even though they
+        don't necessarily intend to make redfish requests.
+
+        This class method will make an attempt to tolerate this situation.  At
+        some future point, when all BMCs can be expected to support redfish,
+        this class method may be considered for deletion.  If it is deleted,
+        the self.__inited__ test code in the login() class method below should
+        likewise be deleted.
+        """
+        self.__inited__ = False
+        try:
+            super(bmc_redfish, self).__init__(*args, **kwargs)
+            self.__inited__ = True
+        except ValueError as get_exception:
+            except_type, except_value, except_traceback = sys.exc_info()
+            regex = r"The HTTP status code was not valid:[\r\n]+status:[ ]+502"
+            result = re.match(regex, str(except_value), flags=re.MULTILINE)
+            if not result:
+                gp.lprint_var(except_type)
+                gp.lprint_varx("except_value", str(except_value))
+                raise(get_exception)
+
     def login(self, *args, **kwargs):
         r"""
         Assign BMC default values for username, password and auth arguments
@@ -28,6 +59,9 @@
         kwargs                      See parent class method prolog for details.
         """
 
+        if not self.__inited__:
+            message = "bmc_redfish.__init__() was never successfully run.\n"
+            raise ValueError(message)
         # Assign default values for username, password, auth where necessary.
         openbmc_username = BuiltIn().get_variable_value("${OPENBMC_USERNAME}")
         openbmc_password = BuiltIn().get_variable_value("${OPENBMC_PASSWORD}")
@@ -35,8 +69,8 @@
         password, args, kwargs = fa.pop_arg(openbmc_password, *args, **kwargs)
         auth, args, kwargs = fa.pop_arg('session', *args, **kwargs)
 
-        super(redfish_plus, self).login(username, password, auth,
-                                        *args, **kwargs)
+        super(bmc_redfish, self).login(username, password, auth,
+                                       *args, **kwargs)
 
     def get_properties(self, *args, **kwargs):
         r"""