blob: 6783a325e4f47fd336e3918662b144b74174749a [file] [log] [blame]
Deepak Kodihalli16e87542017-02-27 07:07:33 -06001#pragma once
2
Patrick Venturef18bf832018-10-26 18:14:00 -07003#include "callouts-gen.hpp"
4#include "elog_entry.hpp"
5
Deepak Kodihalli739e9252017-03-05 23:23:50 -06006#include <algorithm>
7#include <cstring>
Deepak Kodihalli16e87542017-02-27 07:07:33 -06008#include <phosphor-logging/elog-errors.hpp>
Patrick Venturef18bf832018-10-26 18:14:00 -07009#include <string>
10#include <tuple>
11#include <vector>
Deepak Kodihalli16e87542017-02-27 07:07:33 -060012
13namespace phosphor
14{
15namespace logging
16{
17namespace metadata
18{
19
20using Metadata = std::string;
21
22namespace associations
23{
24
Patrick Venturef18bf832018-10-26 18:14:00 -070025using Type = void(const std::string&, const std::vector<std::string>&,
Deepak Kodihalli16e87542017-02-27 07:07:33 -060026 AssociationList& list);
27
Deepak Kodihalli66c46eb2017-03-02 03:07:10 -060028/** @brief Pull out metadata name and value from the string
29 * <metadata name>=<metadata value>
30 * @param [in] data - metadata key=value entries
31 * @param [out] metadata - map of metadata name:value
32 */
33inline void parse(const std::vector<std::string>& data,
34 std::map<std::string, std::string>& metadata)
35{
36 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -070037 for (const auto& entryItem : data)
Deepak Kodihalli66c46eb2017-03-02 03:07:10 -060038 {
Patrick Venture34438962018-10-30 13:17:37 -070039 auto pos = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -070040 if (std::string::npos != pos)
Deepak Kodihalli66c46eb2017-03-02 03:07:10 -060041 {
Patrick Venture34438962018-10-30 13:17:37 -070042 auto key = entryItem.substr(0, entryItem.find(separator));
43 auto value = entryItem.substr(entryItem.find(separator) + 1);
Deepak Kodihalli66c46eb2017-03-02 03:07:10 -060044 metadata.emplace(std::move(key), std::move(value));
45 }
46 }
47};
48
Matt Spinler3fb83b32019-07-26 11:22:44 -050049/** @brief Combine the metadata keys and values from the map
50 * into a vector of strings that look like:
51 * "<metadata name>=<metadata value>"
52 * @param [in] data - metadata key:value map
53 * @param [out] metadata - vector of "key=value" strings
54 */
55inline void combine(const std::map<std::string, std::string>& data,
56 std::vector<std::string>& metadata)
57{
58 for (const auto& [key, value] : data)
59 {
60 std::string line{key};
61 line += "=" + value;
62 metadata.push_back(std::move(line));
63 }
64}
65
Deepak Kodihalli16e87542017-02-27 07:07:33 -060066/** @brief Build error associations specific to metadata. Specialize this
67 * template for handling a specific type of metadata.
68 * @tparam M - type of metadata
Deepak Kodihalli327e5802017-02-28 02:39:56 -060069 * @param [in] match - metadata to be handled
Deepak Kodihalli16e87542017-02-27 07:07:33 -060070 * @param [in] data - metadata key=value entries
71 * @param [out] list - list of error association objects
72 */
73template <typename M>
Patrick Venturef18bf832018-10-26 18:14:00 -070074void build(const std::string& match, const std::vector<std::string>& data,
Deepak Kodihalli16e87542017-02-27 07:07:33 -060075 AssociationList& list) = delete;
76
Deepak Kodihalli327e5802017-02-28 02:39:56 -060077// Example template specialization - we don't want to do anything
78// for this metadata.
79using namespace example::xyz::openbmc_project::Example::Elog;
80template <>
81inline void build<TestErrorTwo::DEV_ID>(const std::string& match,
82 const std::vector<std::string>& data,
83 AssociationList& list)
84{
85}
86
Deepak Kodihalli739e9252017-03-05 23:23:50 -060087template <>
Patrick Venturef18bf832018-10-26 18:14:00 -070088inline void
89 build<example::xyz::openbmc_project::Example::Device::Callout::
90 CALLOUT_DEVICE_PATH_TEST>(const std::string& match,
91 const std::vector<std::string>& data,
92 AssociationList& list)
Deepak Kodihalli739e9252017-03-05 23:23:50 -060093{
Deepak Kodihalli739e9252017-03-05 23:23:50 -060094 std::map<std::string, std::string> metadata;
95 parse(data, metadata);
96 auto iter = metadata.find(match);
Patrick Venturef18bf832018-10-26 18:14:00 -070097 if (metadata.end() != iter)
Deepak Kodihalli739e9252017-03-05 23:23:50 -060098 {
Patrick Venturef18bf832018-10-26 18:14:00 -070099 auto comp = [](const auto& first, const auto& second) {
Patrick Venture30047bf2018-11-01 18:52:15 -0700100 return (std::strcmp(std::get<0>(first), second) < 0);
Deepak Kodihalli739e9252017-03-05 23:23:50 -0600101 };
Patrick Venturef18bf832018-10-26 18:14:00 -0700102 auto callout = std::lower_bound(callouts.begin(), callouts.end(),
103 (iter->second).c_str(), comp);
104 if ((callouts.end() != callout) &&
Patrick Venture30047bf2018-11-01 18:52:15 -0700105 !std::strcmp((iter->second).c_str(), std::get<0>(*callout)))
Deepak Kodihalli739e9252017-03-05 23:23:50 -0600106 {
Patrick Venturecaa73ad2018-10-30 13:13:06 -0700107 constexpr auto ROOT = "/xyz/openbmc_project/inventory";
108
Patrick Venturef18bf832018-10-26 18:14:00 -0700109 list.push_back(std::make_tuple(
110 "callout", "fault", std::string(ROOT) + std::get<1>(*callout)));
Deepak Kodihalli739e9252017-03-05 23:23:50 -0600111 }
112 }
113}
114
Deepak Kodihalli5edacf32017-03-16 07:04:07 -0500115// The PROCESS_META flag is needed to get out of tree builds working. Such
116// builds will have access only to internal error interfaces, hence handlers
117// for out dbus error interfaces won't compile. This flag is not set by default,
118// the phosphor-logging recipe enabled it.
Deepak Kodihalli682326a2017-03-06 05:26:53 -0600119#if defined PROCESS_META
120
121template <>
Patrick Venturef18bf832018-10-26 18:14:00 -0700122void build<xyz::openbmc_project::Common::Callout::Device::CALLOUT_DEVICE_PATH>(
123 const std::string& match, const std::vector<std::string>& data,
Deepak Kodihalli682326a2017-03-06 05:26:53 -0600124 AssociationList& list);
125
Tom Joseph213aaf62017-07-25 00:02:09 +0530126template <>
Patrick Venturef18bf832018-10-26 18:14:00 -0700127void build<
128 xyz::openbmc_project::Common::Callout::Inventory::CALLOUT_INVENTORY_PATH>(
129 const std::string& match, const std::vector<std::string>& data,
Tom Joseph213aaf62017-07-25 00:02:09 +0530130 AssociationList& list);
131
Deepak Kodihalli682326a2017-03-06 05:26:53 -0600132#endif // PROCESS_META
133
Deepak Kodihalli16e87542017-02-27 07:07:33 -0600134} // namespace associations
135} // namespace metadata
136} // namespace logging
137} // namespace phosphor