Add peltool APIs for RAS SRC verification

Change-Id: I80b5dacb89a1fd87995cd73086d483840e19d363
Signed-off-by: Sridevi Ramesh <sridevra@in.ibm.com>
diff --git a/lib/pel_utils.py b/lib/pel_utils.py
index d12abd7..eb74ef5 100644
--- a/lib/pel_utils.py
+++ b/lib/pel_utils.py
@@ -7,6 +7,7 @@
 import func_args as fa
 import bmc_ssh_utils as bsu
 import json
+import pel_variables
 
 
 class peltool_exception(Exception):
@@ -51,77 +52,113 @@
     if parse_json:
         try:
             return json.loads(out_buf)
-        except json.JSONDecodeError:
+        except ValueError:
             return {}
     return out_buf
 
 
-def fetch_pelId_For_SRC(src_id):
+def fetch_all_pel_ids_for_src(src_id, severity):
     r"""
-    Fetch PEL ID for the input SRC ID
+    Fetch all PEL IDs for the input SRC ID based on the severity type
+    in the list format.
 
     Description of arguments:
-    src_id  SRC ID (e.g. BC20E504)
+    src_id      SRC ID (e.g. BC20E504).
+    severity    PEL severity (e.g. "Predictive Error"
+                                   "Recovered Error").
     """
 
-    src_pel_id = []
     try:
+        src_pel_ids = []
         pel_data = peltool(" -l")
         pel_id_list = pel_data.keys()
-        for i in pel_id_list:
-            if pel_data[i]["SRC"] == src_id:
-                src_pel_id.append(i)
+        for pel_id in pel_id_list:
+            # Check if required SRC ID with severity is present
+            if ((pel_data[pel_id]["SRC"] == src_id) and (pel_data[pel_id]["Sev"] == severity)):
+                src_pel_ids.append(pel_id)
+
+        if not src_pel_ids:
+            raise peltool_exception(src_id + " with severity " + severity + " not present")
     except Exception as e:
         raise peltool_exception("Failed to fetch PEL ID for required SRC : " + str(e))
-    return src_pel_id
+    return src_pel_ids
 
 
-def verify_SRC_details(pel_id, attn_type, target_type, signature_desc,
-                       threshold_limit):
+def verify_src_signature_and_threshold(pel_id, attn_type, signature_desc, th_limit):
     r"""
-    Verify SRC details for the given PEL ID based on the required
-    target type, attention type, signature description, threshold limit.
+    Verifies SRC details for the given PEL ID based on the required
+    attention type, signature description, threshold limits.
 
     Description of arguments:
-    pel_id          PEL ID for the required SRC details to verify
-    target_type     Target type (e.g. TYPE_OMIC)
-    attn_type       Attention type (e.g. RECCOVERABLE)
-    signature_desc  Signature description of the error inject
-    threshold_limit Threshold limit (e.g. 1, 5, 32)
+    pel_id          PEL ID for the required SRC details to verify.
+    attn_type       Attention type (e.g. RE, CS, UNIT_CS).
+    signature_desc  Signature description of the error inject.
+    th_limit        Threshold limit (e.g. 1, 5, 32).
     """
 
     try:
         pel_cmd = " -i " + pel_id
         src_data = peltool(pel_cmd)
         src_dict = src_data["Primary SRC"]["SRC Details"]
-        usr_data = src_data["User Data 4"]
-        if (src_dict["Attention Type"] != attn_type):
+        usr_data = src_data["User Data 1"]
+
+        # Example for signature in recoverable error
+        #
+        # "SRC Details": {
+        # "Attention Type": "RECOVERABLE",
+        # "Node": 0,
+        # "Target Type": "TYPE_OMIC",
+        # "Target Instance": 0,
+        # "Signature": "MC_OMI_DL_FIR[1]: OMI-DL0 UE on data flit"
+        # }
+        if (attn_type == "RE"):
+            if (src_dict["Attention Type"] != "RECOVERABLE"):
+                raise peltool_exception("Required Attention type " + attn_type + " not found")
+
+        # Example for signature in system checkstop error
+        #
+        # "SRC Details": {
+        # "Primary Attention": "system checkstop",
+        # "Signature Description": {
+        #    "Chip Desc": "node 0 proc 0 (P10 2.0)",
+        #    "Signature": "EQ_L2_FIR(0)[7] L2 directory read UE",
+        #    "Attn Type": "checkstop"
+        # }
+
+        elif (attn_type == "CS"):
+            if (src_dict["Primary Attention"] != "system checkstop"):
+                raise peltool_exception("Required Attention type " + attn_type + " not found")
+
+        elif (attn_type == "UNIT_CS"):
+            if (src_dict["Attention Type"] != "UNIT_CS"):
+                raise peltool_exception("Required Attention type " + attn_type + " not found")
+        else:
             raise peltool_exception("Required Attention type " + attn_type + " not found")
-        if target_type not in src_dict["Target Type"]:
-            raise peltool_exception("Required Target type " + target_type + " not found")
+
         if signature_desc not in src_dict["Signature"]:
             raise peltool_exception("Required Signature " + signature_desc + " not found")
 
-        if (int(threshold_limit) != usr_data["Error Threshold"]):
-            raise peltool_exception("Required Threshold limit " + threshold_limit + " not found")
+        if (int(th_limit) != usr_data["Error Count"]):
+            raise peltool_exception("Required Threshold limit " + th_limit + " not found")
 
     except Exception as e:
         raise peltool_exception("Failed to verify SRC details : " + str(e))
-
     return True
 
 
-def fetch_All_SRC():
+def fetch_all_src():
     r"""
-    Fetch all list of SRC IDs from peltool
+    Fetch all SRC IDs from peltool in the list format.
     """
-
-    src_id = []
     try:
+        src_id = []
         pel_data = peltool(" -l")
-        pel_id_list = pel_data.keys()
-        for i in pel_id_list:
-            src_id.append(pel_data[i]["SRC"])
+        if pel_data:
+            pel_id_list = pel_data.keys()
+            for pel_id in pel_id_list:
+                src_id.append(pel_data[pel_id]["SRC"])
+        else:
+            raise peltool_exception("No PEL entry found ..")
     except Exception as e:
         raise peltool_exception("Failed to fetch all SRCs : " + str(e))
     return src_id