Generate error map as part of dump types

An error type is a specific type of BMC dump that is generated
when a particular error is logged in the BMC. Previously,
dump types and error types were managed independently, with
error types being defined in separate files, namely 'error_map.hpp'
and its associated mako template 'error_map.mako.cpp'. However, this
practice led to inconsistencies, as the dreport script, which is used
for handling dump collection and packaging, was interpreting error
type as another dump type rather than as additional parameters.

The separate error_map.hpp file and its associated mako template
(error_map.mako.cpp) have been removed. Instead, error types are now
incorporated directly into the dump types along with an error type map
for validating the error names. This integration aligns the code with
the existing behaviour of the dreport script, allowing for more
consistent processing of dump types

As a result of this integration, the error map generation is now part
of the  dump_types_mako.cpp file. To support these changes, a second
input YAML file has been added to the map_gen.py script to generate
the dump types and error types together.

Tests:
Create InternalFailure
> busctl call xyz.openbmc_project.Logging  /xyz/openbmc_project/logging \
  xyz.openbmc_project.Logging.Create Create ssa{ss}  \
  xyz.openbmc_project.Common.Error.InternalFailure \
  xyz.openbmc_project.Logging.Entry.Level.Error 0
> dump created
  /var/lib/phosphor-debug-collector/dumps/1/\
  BMCDUMP.XXXXXXXX.0000001.20230723084043

Create Checkstop
> busctl call xyz.openbmc_project.Logging  /xyz/openbmc_project/logging \
  xyz.openbmc_project.Logging.Create Create ssa{ss}  \
  org.open_power.Host.Boot.Error.Checkstop \
  xyz.openbmc_project.Logging.Entry.Level.Error 0
> /var/lib/phosphor-debug-collector/dumps/4/\
  BMCDUMP.XXXXXXXX.0000004.20230723094150

Change-Id: Iac483f4993b59cf0fc063ed9584785f1a1fa04a8
Signed-off-by: Dhruvaraj Subhashchandran <dhruvaraj@in.ibm.com>
diff --git a/dump_types.mako.cpp b/dump_types.mako.cpp
index 251599f..969bfff 100644
--- a/dump_types.mako.cpp
+++ b/dump_types.mako.cpp
@@ -1,6 +1,6 @@
-## This file is a template.  The comment below is emitted
+## This file is a template.The comment below is emitted
 ## into the rendered file; feel free to edit this file.
-// !!! WARNING: This is a GENERATED Code..Please do NOT Edit !!!
+//  !!! WARNING: This is a GENERATED Code..Please do NOT Edit !!!
 #include "dump_types.hpp"
 
 #include <phosphor-logging/elog-errors.hpp>
@@ -21,12 +21,34 @@
 % endfor
 };
 
+<%
+map_keys = set()
+%>
 DUMP_TYPE_TO_STRING_MAP dumpTypeToStringMap = {
 % for item in DUMP_TYPE_TABLE:
   % for key, values in item.items():
-    {DumpTypes::${values[0].upper()}, "${values[0]}"},
+    % if values[0].upper() not in map_keys:
+        {DumpTypes::${values[0].upper()}, "${values[0]}"},
+        <% map_keys.add(values[0].upper()) %>
+    % endif
   % endfor
 % endfor
+% for key, values in ERROR_TYPE_DICT.items():
+    % if key.upper() not in map_keys:
+        {DumpTypes::${key.upper()}, "${key}"},
+        <% map_keys.add(key.upper()) %>
+    % endif
+% endfor
+};
+
+const ErrorMap errorMap = {
+% for key, errors in ERROR_TYPE_DICT.items():
+    {"${key}", {
+    % for error in errors:
+        "${error}",
+    % endfor
+    }},
+% endfor
 };
 
 std::optional<std::string> dumpTypeToString(const DumpTypes& dumpType)
@@ -85,5 +107,24 @@
     return dumpType;
 }
 
+bool isErrorTypeValid(const std::string& errorType)
+{
+    const auto it = errorMap.find(errorType);
+    return it != errorMap.end();
+}
+
+std::optional<ErrorType> findErrorType(const std::string& errString)
+{
+    for (const auto& [type, errorList] : errorMap)
+    {
+        auto error = std::find(errorList.begin(), errorList.end(), errString);
+        if (error != errorList.end())
+        {
+            return type;
+        }
+    }
+    return std::nullopt;
+}
+
 } // namespace dump
 } // namespace phosphor
diff --git a/dump_types.mako.hpp b/dump_types.mako.hpp
index c46bcdc..28f052c 100644
--- a/dump_types.mako.hpp
+++ b/dump_types.mako.hpp
@@ -7,6 +7,7 @@
 #include <ranges>
 #include <string>
 #include <unordered_map>
+#include <vector>
 
 namespace phosphor
 {
@@ -21,13 +22,28 @@
 // Dump collection indicator
 using DUMP_COLLECTION_TYPE = std::string;
 
+using ErrorType = std::string;
+using Error = std::string;
+using ErrorList = std::vector<Error>;
+using ErrorMap = std::unordered_map<ErrorType, ErrorList>;
+
 // Dump types
+<% enum_values = set() %>
 enum class DumpTypes {
 % for item in DUMP_TYPE_TABLE:
   % for key, values in item.items():
-    ${values[0].upper()},
+    % if values[0].upper() not in enum_values:
+        ${values[0].upper()},
+        <% enum_values.add(values[0].upper()) %>
+    % endif
   % endfor
 % endfor
+% for key in ERROR_TYPE_DICT:
+    % if key.upper() not in enum_values:
+        ${key.upper()},
+        <% enum_values.add(key.upper()) %>
+    % endif
+% endfor
 };
 
 // A table of dump types
@@ -76,5 +92,32 @@
 DumpTypes validateDumpType(const std::string& type,
                            const std::string& category);
 
+/**
+ * @brief Checks if the provided error type is valid.
+ *
+ * This function verifies the validity of the error type by checking if it
+ * exists in the error map.
+ *
+ * @param[in] errorType - The string representation of the error type to
+ * validate.
+ *
+ * @return True if the error type exists in the error map, False otherwise.
+ */
+bool isErrorTypeValid(const std::string& errorType);
+
+/**
+ * @brief Finds the error type based on the provided error string.
+ *
+ * This function searches the error map for the provided error string.
+ * If it finds the string in the list of errors for a specific error type,
+ * it returns that error type.
+ *
+ * @param[in] errString - The string representation of the error to search for.
+ *
+ * @return An optional containing the error type if found. If the error string
+ * is not found in the map, returns an empty optional.
+ */
+std::optional<ErrorType> findErrorType(const std::string& errString);
+
 } // namespace dump
 } // namespace phosphor
diff --git a/dump_utils.hpp b/dump_utils.hpp
index 01e4166..7bf8a7b 100644
--- a/dump_utils.hpp
+++ b/dump_utils.hpp
@@ -335,5 +335,46 @@
     return T{};
 }
 
+/**
+ * @brief This function fetches the dump type associated with a particular
+ * error.
+ *
+ * @param[in] params The map of parameters passed as input.
+ *
+ * @return The dump type associated with the error.
+ *
+ * @throw std::invalid_argument If the dump type associated with the error
+ * type is not found in the map.
+ */
+inline DumpTypes getErrorDumpType(phosphor::dump::DumpCreateParams& params)
+{
+    using CreateParameters =
+        sdbusplus::xyz::openbmc_project::Dump::server::Create::CreateParameters;
+    using DumpIntr = sdbusplus::common::xyz::openbmc_project::dump::Create;
+    using InvalidArgument =
+        sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
+    using Argument = xyz::openbmc_project::Common::InvalidArgument;
+
+    std::string errorType = extractParameter<std::string>(
+        DumpIntr::convertCreateParametersToString(CreateParameters::ErrorType),
+        params);
+    if (!isErrorTypeValid(errorType))
+    {
+        lg2::error("An invalid error type passed type: {ERROR_TYPE}",
+                   "ERROR_TYPE", errorType);
+        elog<InvalidArgument>(Argument::ARGUMENT_NAME("ERROR_TYPE"),
+                              Argument::ARGUMENT_VALUE(errorType.c_str()));
+    }
+    auto type = stringToDumpType(errorType);
+    if (type.has_value())
+    {
+        return type.value();
+    }
+
+    // Ideally this should never happen, because if the error type is valid
+    // it should be present in the dumpTypeToStringMap
+    throw std::invalid_argument{"Dump type not found"};
+}
+
 } // namespace dump
 } // namespace phosphor
diff --git a/elog_watch.cpp b/elog_watch.cpp
index 84eaff8..93c73e9 100644
--- a/elog_watch.cpp
+++ b/elog_watch.cpp
@@ -4,7 +4,7 @@
 
 #include "dump_internal.hpp"
 #include "dump_serialize.hpp"
-#include "errors_map.hpp"
+#include "dump_types.hpp"
 #include "xyz/openbmc_project/Dump/Create/error.hpp"
 
 #include <cereal/cereal.hpp>
@@ -109,23 +109,15 @@
         return;
     }
 
-    EType errorType;
-    for (const auto& [type, errorList] : errorMap)
+    auto etype = findErrorType(data);
+    if (!etype.has_value())
     {
-        auto error = std::find(errorList.begin(), errorList.end(), data);
-        if (error != errorList.end())
-        {
-            errorType = type;
-            break;
-        }
-    }
-
-    // error not supported in the configuration
-    if (errorType.empty())
-    {
+        // error not supported in the configuration
         return;
     }
 
+    auto errorType = etype.value();
+
     std::vector<std::string> fullPaths;
     fullPaths.push_back(objectPath);
 
diff --git a/errors_map.hpp b/errors_map.hpp
deleted file mode 100644
index 1389509..0000000
--- a/errors_map.hpp
+++ /dev/null
@@ -1,12 +0,0 @@
-#pragma once
-
-#include <map>
-#include <string>
-#include <vector>
-
-using EType = std::string;
-using Error = std::string;
-using ErrorList = std::vector<Error>;
-using ErrorMap = std::map<EType, ErrorList>;
-
-extern const ErrorMap errorMap;
diff --git a/errors_map.mako.cpp b/errors_map.mako.cpp
deleted file mode 100644
index b8246cf..0000000
--- a/errors_map.mako.cpp
+++ /dev/null
@@ -1,14 +0,0 @@
-## This file is a template.  The comment below is emitted
-## into the rendered file; feel free to edit this file.
-// !!! WARNING: This is a GENERATED Code..Please do NOT Edit !!!
-#include "errors_map.hpp"
-
-const ErrorMap errorMap = {
-% for key, errors in errDict.items():
-    {"${key}", {
-    % for error in errors:
-        "${error}",
-    % endfor
-    }},
-% endfor
-};
diff --git a/map_gen.py b/map_gen.py
index 39215d1..367587c 100755
--- a/map_gen.py
+++ b/map_gen.py
@@ -14,10 +14,18 @@
 
     parser.add_argument(
         "-i",
-        "--input_yaml",
-        dest="input_yaml",
-        default="example.yaml",
-        help="input yaml file to parse",
+        "--input_dump_type_yaml",
+        dest="input_dump_type_yaml",
+        default="example_dump_types.yaml",
+        help="input dump type yaml file to parse",
+    )
+
+    parser.add_argument(
+        "-j",
+        "--input_error_type_yaml",
+        dest="input_error_type_yaml",
+        default="example_errors_watch.yaml",
+        help="input error type yaml file to parse",
     )
 
     parser.add_argument(
@@ -36,26 +44,20 @@
         help="output cpp file",
     )
 
-    parser.add_argument(
-        "-v",
-        "--var_name",
-        dest="var_name",
-        default="mapping",
-        help="variable name to use in the template",
-    )
-
     args = parser.parse_args()
 
-    with open(os.path.join(script_dir, args.input_yaml), "r") as fd:
-        yaml_dict = yaml.safe_load(fd)
+    with open(os.path.join(script_dir, args.input_dump_type_yaml), "r") as fd:
+        yaml_dict1 = yaml.safe_load(fd)
+
+    with open(os.path.join(script_dir, args.input_error_type_yaml), "r") as fd:
+        yaml_dict2 = yaml.safe_load(fd)
 
     template = os.path.join(script_dir, args.template)
     t = Template(filename=template)
     with open(args.output_file, "w") as fd:
-        if args.var_name == "errDict":
-            fd.write(t.render(errDict=yaml_dict))
-        else:
-            fd.write(t.render(DUMP_TYPE_TABLE=yaml_dict))
+        fd.write(
+            t.render(DUMP_TYPE_TABLE=yaml_dict1, ERROR_TYPE_DICT=yaml_dict2)
+        )
 
 
 if __name__ == "__main__":
diff --git a/meson.build b/meson.build
index 54a233c..f7acc64 100644
--- a/meson.build
+++ b/meson.build
@@ -155,16 +155,17 @@
                         map_gen_file_loc,
                         '-i',
                         meson.build_root() + '/' +  combined_yaml_file,
+                        '-j',
+                        get_option('ERROR_MAP_YAML'),
                         '-t',
                         'dump_types.mako.hpp',
-                        '-v',
-                        'DUMP_TYPE_TABLE',
                         '-o',
                         'dump_types.hpp'
                     ],
                     depend_files : [ 'dump_types.mako.hpp',
                                      'map_gen.py',
-                                     meson.build_root() + '/' +  combined_yaml_file
+                                     meson.build_root() + '/' +  combined_yaml_file,
+                                     get_option('ERROR_MAP_YAML')
                                    ],
                     output : 'dump_types.hpp'
                  )
@@ -176,39 +177,19 @@
                         map_gen_file_loc,
                         '-i',
                         meson.build_root() + '/' +  combined_yaml_file,
+                        '-j',
+                        get_option('ERROR_MAP_YAML'),
                         '-t',
                         'dump_types.mako.cpp',
-                        '-v',
-                        'DUMP_TYPE_TABLE',
                         '-o',
                         'dump_types.cpp'
                     ],
                     depend_files : [ 'dump_types.mako.cpp',
                                      'map_gen.py',
-                                     meson.build_root() + '/' +  combined_yaml_file
-                                   ],
-                    output : 'dump_types.cpp'
-                 )
-
-errors_map_cpp = custom_target(
-                    'errors_map.cpp',
-                    command : [
-                        python,
-                        map_gen_file_loc,
-                        '-i',
-                        get_option('ERROR_MAP_YAML'),
-                        '-t',
-                        'errors_map.mako.cpp',
-                        '-v',
-                        'errDict',
-                        '-o',
-                        'errors_map.cpp'
-                    ],
-                    depend_files : [ 'errors_map.mako.cpp',
-                                     'map_gen.py',
+                                     meson.build_root() + '/' +  combined_yaml_file,
                                      get_option('ERROR_MAP_YAML')
                                    ],
-                    output : 'errors_map.cpp'
+                    output : 'dump_types.cpp'
                  )
 
 phosphor_dump_manager_sources = [
@@ -218,7 +199,6 @@
         'dump_manager_main.cpp',
         'dump_serialize.cpp',
         'elog_watch.cpp',
-        errors_map_cpp,
         common_hpp,
         server_hpp,
         server_cpp,