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/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