Prevent duplicate entries in callout list

If duplicates exist, only the highest priority callout will remain in
the list.

Signed-off-by: Zane Shelley <zshelle@us.ibm.com>
Change-Id: Ibec768b22c944965460010be8867749a8c0f9044
diff --git a/analyzer/meson.build b/analyzer/meson.build
index fe2858d..18fb57c 100644
--- a/analyzer/meson.build
+++ b/analyzer/meson.build
@@ -7,6 +7,7 @@
     'initialize_isolator.cpp',
     'ras-data/ras-data-parser.cpp',
     'resolution.cpp',
+    'service_data.cpp',
 )
 
 # Library dependencies.
diff --git a/analyzer/ras-data/ras-data-definition.md b/analyzer/ras-data/ras-data-definition.md
index b7846d7..2df571b 100644
--- a/analyzer/ras-data/ras-data-definition.md
+++ b/analyzer/ras-data/ras-data-definition.md
@@ -92,6 +92,9 @@
 | `MED_C`  | Same as `MED` except all in group C replaced at the same time.    |
 | `LOW`    | Same as `MED*`, but only if higher priority service does not work.|
 
+NOTE: If a part is called out more than once, only the highest priority callout
+will be used.
+
 Actions with a `guard` keyword can only use the following values (boolean):
 
 | Guard | Description                                                          |
diff --git a/analyzer/service_data.cpp b/analyzer/service_data.cpp
new file mode 100644
index 0000000..c569739
--- /dev/null
+++ b/analyzer/service_data.cpp
@@ -0,0 +1,54 @@
+#include <analyzer/service_data.hpp>
+
+namespace analyzer
+{
+
+void ServiceData::addCallout(const nlohmann::json& i_callout)
+{
+    // The new callout is either a hardware callout with a location code or a
+    // procedure callout.
+
+    std::string type{};
+    if (i_callout.contains("LocationCode"))
+    {
+        type = "LocationCode";
+    }
+    else if (i_callout.contains("Procedure"))
+    {
+        type = "Procedure";
+    }
+    else
+    {
+        throw std::logic_error("Unsupported callout: " + i_callout.dump());
+    }
+
+    // A map to determine the priority order. All of the medium priorities,
+    // including the medium group priorities, are all the same level.
+    static const std::map<std::string, unsigned int> m = {
+        {"H", 3}, {"M", 2}, {"A", 2}, {"B", 2}, {"C", 2}, {"L", 1},
+    };
+
+    bool addCallout = true;
+
+    for (auto& c : iv_calloutList)
+    {
+        if (c.contains(type) && (c.at(type) == i_callout.at(type)))
+        {
+            // The new callout already exists. Don't add a new callout.
+            addCallout = false;
+
+            if (m.at(c.at("Priority")) < m.at(i_callout.at("Priority")))
+            {
+                // The new callout has a higher priority, update it.
+                c["Priority"] = i_callout.at("Priority");
+            }
+        }
+    }
+
+    if (addCallout)
+    {
+        iv_calloutList.push_back(i_callout);
+    }
+}
+
+} // namespace analyzer
diff --git a/analyzer/service_data.hpp b/analyzer/service_data.hpp
index ce9993e..ac90707 100644
--- a/analyzer/service_data.hpp
+++ b/analyzer/service_data.hpp
@@ -72,10 +72,7 @@
      * @brief Add callout information to the callout list.
      * @param The JSON object for this callout.
      */
-    void addCallout(const nlohmann::json& i_callout)
-    {
-        iv_calloutList.push_back(i_callout);
-    }
+    void addCallout(const nlohmann::json& i_callout);
 
     /**
      * @brief Add FFDC for a callout that would otherwise not be available in
diff --git a/test/meson.build b/test/meson.build
index ea40e4c..dc374fb 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -11,11 +11,15 @@
   'resolution_test',
 ]
 
+analyzer_src = files(
+  '../analyzer/service_data.cpp',
+)
+
 gtest = dependency('gtest', main : true, required : false, method : 'system')
 
 if gtest.found()
     foreach t : tests
-        test(t, executable(t.underscorify(), t + '.cpp',
+        test(t, executable(t.underscorify(), [ t + '.cpp', analyzer_src ],
                            link_with : [ util_lib ],
                            dependencies : [ libhei_dep, gtest ],
                            cpp_args : test_arg,
diff --git a/test/resolution_test.cpp b/test/resolution_test.cpp
index ddca989..d50213d 100644
--- a/test/resolution_test.cpp
+++ b/test/resolution_test.cpp
@@ -264,10 +264,6 @@
         "Priority": "H"
     },
     {
-        "LocationCode": "/proc0",
-        "Priority": "A"
-    },
-    {
         "LocationCode": "P0",
         "Priority": "L"
     }
@@ -282,17 +278,9 @@
     },
     {
         "LocationCode": "/proc0",
-        "Priority": "M"
-    },
-    {
-        "LocationCode": "/proc0",
         "Priority": "H"
     },
     {
-        "LocationCode": "/proc0",
-        "Priority": "A"
-    },
-    {
         "LocationCode": "P0",
         "Priority": "L"
     }