blob: a4ace763f1743401ff49f1846c3d8ac316042c19 [file] [log] [blame]
Deepak Kodihalli16e87542017-02-27 07:07:33 -06001#pragma once
2
Patrick Williams0bb89f82021-04-16 16:30:04 -05003#include "config.h"
4
Patrick Venturef18bf832018-10-26 18:14:00 -07005#include "callouts-gen.hpp"
6#include "elog_entry.hpp"
7
Deepak Kodihalli739e9252017-03-05 23:23:50 -06008#include <algorithm>
9#include <cstring>
Deepak Kodihalli16e87542017-02-27 07:07:33 -060010#include <phosphor-logging/elog-errors.hpp>
Patrick Venturef18bf832018-10-26 18:14:00 -070011#include <string>
12#include <tuple>
13#include <vector>
Deepak Kodihalli16e87542017-02-27 07:07:33 -060014
15namespace phosphor
16{
17namespace logging
18{
19namespace metadata
20{
21
22using Metadata = std::string;
23
24namespace associations
25{
26
Patrick Venturef18bf832018-10-26 18:14:00 -070027using Type = void(const std::string&, const std::vector<std::string>&,
Deepak Kodihalli16e87542017-02-27 07:07:33 -060028 AssociationList& list);
29
Deepak Kodihalli66c46eb2017-03-02 03:07:10 -060030/** @brief Pull out metadata name and value from the string
31 * <metadata name>=<metadata value>
32 * @param [in] data - metadata key=value entries
33 * @param [out] metadata - map of metadata name:value
34 */
35inline void parse(const std::vector<std::string>& data,
36 std::map<std::string, std::string>& metadata)
37{
38 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -070039 for (const auto& entryItem : data)
Deepak Kodihalli66c46eb2017-03-02 03:07:10 -060040 {
Patrick Venture34438962018-10-30 13:17:37 -070041 auto pos = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -070042 if (std::string::npos != pos)
Deepak Kodihalli66c46eb2017-03-02 03:07:10 -060043 {
Patrick Venture34438962018-10-30 13:17:37 -070044 auto key = entryItem.substr(0, entryItem.find(separator));
45 auto value = entryItem.substr(entryItem.find(separator) + 1);
Deepak Kodihalli66c46eb2017-03-02 03:07:10 -060046 metadata.emplace(std::move(key), std::move(value));
47 }
48 }
Patrick Williams44681ef2021-04-16 06:31:41 -050049}
Deepak Kodihalli66c46eb2017-03-02 03:07:10 -060050
Matt Spinler3fb83b32019-07-26 11:22:44 -050051/** @brief Combine the metadata keys and values from the map
52 * into a vector of strings that look like:
53 * "<metadata name>=<metadata value>"
54 * @param [in] data - metadata key:value map
55 * @param [out] metadata - vector of "key=value" strings
56 */
57inline void combine(const std::map<std::string, std::string>& data,
58 std::vector<std::string>& metadata)
59{
60 for (const auto& [key, value] : data)
61 {
62 std::string line{key};
63 line += "=" + value;
64 metadata.push_back(std::move(line));
65 }
66}
67
Deepak Kodihalli16e87542017-02-27 07:07:33 -060068/** @brief Build error associations specific to metadata. Specialize this
69 * template for handling a specific type of metadata.
70 * @tparam M - type of metadata
Deepak Kodihalli327e5802017-02-28 02:39:56 -060071 * @param [in] match - metadata to be handled
Deepak Kodihalli16e87542017-02-27 07:07:33 -060072 * @param [in] data - metadata key=value entries
73 * @param [out] list - list of error association objects
74 */
75template <typename M>
Patrick Venturef18bf832018-10-26 18:14:00 -070076void build(const std::string& match, const std::vector<std::string>& data,
Deepak Kodihalli16e87542017-02-27 07:07:33 -060077 AssociationList& list) = delete;
78
Deepak Kodihalli327e5802017-02-28 02:39:56 -060079// Example template specialization - we don't want to do anything
80// for this metadata.
81using namespace example::xyz::openbmc_project::Example::Elog;
82template <>
Patrick Williams86bb3fe2021-04-16 06:34:05 -050083inline void
84 build<TestErrorTwo::DEV_ID>(const std::string& /*match*/,
85 const std::vector<std::string>& /*data*/,
86 AssociationList& /*list*/)
Deepak Kodihalli327e5802017-02-28 02:39:56 -060087{
88}
89
Deepak Kodihalli739e9252017-03-05 23:23:50 -060090template <>
Patrick Venturef18bf832018-10-26 18:14:00 -070091inline void
92 build<example::xyz::openbmc_project::Example::Device::Callout::
93 CALLOUT_DEVICE_PATH_TEST>(const std::string& match,
94 const std::vector<std::string>& data,
95 AssociationList& list)
Deepak Kodihalli739e9252017-03-05 23:23:50 -060096{
Deepak Kodihalli739e9252017-03-05 23:23:50 -060097 std::map<std::string, std::string> metadata;
98 parse(data, metadata);
99 auto iter = metadata.find(match);
Patrick Venturef18bf832018-10-26 18:14:00 -0700100 if (metadata.end() != iter)
Deepak Kodihalli739e9252017-03-05 23:23:50 -0600101 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700102 auto comp = [](const auto& first, const auto& second) {
Patrick Venture30047bf2018-11-01 18:52:15 -0700103 return (std::strcmp(std::get<0>(first), second) < 0);
Deepak Kodihalli739e9252017-03-05 23:23:50 -0600104 };
Patrick Venturef18bf832018-10-26 18:14:00 -0700105 auto callout = std::lower_bound(callouts.begin(), callouts.end(),
106 (iter->second).c_str(), comp);
107 if ((callouts.end() != callout) &&
Patrick Venture30047bf2018-11-01 18:52:15 -0700108 !std::strcmp((iter->second).c_str(), std::get<0>(*callout)))
Deepak Kodihalli739e9252017-03-05 23:23:50 -0600109 {
Patrick Venturecaa73ad2018-10-30 13:13:06 -0700110 constexpr auto ROOT = "/xyz/openbmc_project/inventory";
111
Patrick Venturef18bf832018-10-26 18:14:00 -0700112 list.push_back(std::make_tuple(
113 "callout", "fault", std::string(ROOT) + std::get<1>(*callout)));
Deepak Kodihalli739e9252017-03-05 23:23:50 -0600114 }
115 }
116}
117
Deepak Kodihalli5edacf32017-03-16 07:04:07 -0500118// The PROCESS_META flag is needed to get out of tree builds working. Such
119// builds will have access only to internal error interfaces, hence handlers
120// for out dbus error interfaces won't compile. This flag is not set by default,
121// the phosphor-logging recipe enabled it.
Deepak Kodihalli682326a2017-03-06 05:26:53 -0600122#if defined PROCESS_META
123
124template <>
Patrick Venturef18bf832018-10-26 18:14:00 -0700125void build<xyz::openbmc_project::Common::Callout::Device::CALLOUT_DEVICE_PATH>(
126 const std::string& match, const std::vector<std::string>& data,
Deepak Kodihalli682326a2017-03-06 05:26:53 -0600127 AssociationList& list);
128
Tom Joseph213aaf62017-07-25 00:02:09 +0530129template <>
Patrick Venturef18bf832018-10-26 18:14:00 -0700130void build<
131 xyz::openbmc_project::Common::Callout::Inventory::CALLOUT_INVENTORY_PATH>(
132 const std::string& match, const std::vector<std::string>& data,
Tom Joseph213aaf62017-07-25 00:02:09 +0530133 AssociationList& list);
134
Deepak Kodihalli682326a2017-03-06 05:26:53 -0600135#endif // PROCESS_META
136
Deepak Kodihalli16e87542017-02-27 07:07:33 -0600137} // namespace associations
138} // namespace metadata
139} // namespace logging
140} // namespace phosphor