blob: 4273a4ca5cb25996d6dcc9f459a7d8cd47955cab [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
Patrick Williams2544b412022-10-04 08:41:06 -05008#include <phosphor-logging/elog-errors.hpp>
9
Deepak Kodihalli739e9252017-03-05 23:23:50 -060010#include <algorithm>
11#include <cstring>
Patrick Venturef18bf832018-10-26 18:14:00 -070012#include <string>
13#include <tuple>
14#include <vector>
Deepak Kodihalli16e87542017-02-27 07:07:33 -060015
16namespace phosphor
17{
18namespace logging
19{
20namespace metadata
21{
22
23using Metadata = std::string;
24
25namespace associations
26{
27
Patrick Venturef18bf832018-10-26 18:14:00 -070028using Type = void(const std::string&, const std::vector<std::string>&,
Deepak Kodihalli16e87542017-02-27 07:07:33 -060029 AssociationList& list);
30
Deepak Kodihalli66c46eb2017-03-02 03:07:10 -060031/** @brief Pull out metadata name and value from the string
32 * <metadata name>=<metadata value>
33 * @param [in] data - metadata key=value entries
34 * @param [out] metadata - map of metadata name:value
35 */
36inline void parse(const std::vector<std::string>& data,
37 std::map<std::string, std::string>& metadata)
38{
39 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -070040 for (const auto& entryItem : data)
Deepak Kodihalli66c46eb2017-03-02 03:07:10 -060041 {
Patrick Venture34438962018-10-30 13:17:37 -070042 auto pos = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -070043 if (std::string::npos != pos)
Deepak Kodihalli66c46eb2017-03-02 03:07:10 -060044 {
Patrick Venture34438962018-10-30 13:17:37 -070045 auto key = entryItem.substr(0, entryItem.find(separator));
46 auto value = entryItem.substr(entryItem.find(separator) + 1);
Deepak Kodihalli66c46eb2017-03-02 03:07:10 -060047 metadata.emplace(std::move(key), std::move(value));
48 }
49 }
Patrick Williams44681ef2021-04-16 06:31:41 -050050}
Deepak Kodihalli66c46eb2017-03-02 03:07:10 -060051
Matt Spinler3fb83b32019-07-26 11:22:44 -050052/** @brief Combine the metadata keys and values from the map
53 * into a vector of strings that look like:
54 * "<metadata name>=<metadata value>"
55 * @param [in] data - metadata key:value map
56 * @param [out] metadata - vector of "key=value" strings
57 */
58inline void combine(const std::map<std::string, std::string>& data,
59 std::vector<std::string>& metadata)
60{
61 for (const auto& [key, value] : data)
62 {
63 std::string line{key};
64 line += "=" + value;
65 metadata.push_back(std::move(line));
66 }
67}
68
Deepak Kodihalli16e87542017-02-27 07:07:33 -060069/** @brief Build error associations specific to metadata. Specialize this
70 * template for handling a specific type of metadata.
71 * @tparam M - type of metadata
Deepak Kodihalli327e5802017-02-28 02:39:56 -060072 * @param [in] match - metadata to be handled
Deepak Kodihalli16e87542017-02-27 07:07:33 -060073 * @param [in] data - metadata key=value entries
74 * @param [out] list - list of error association objects
75 */
76template <typename M>
Patrick Venturef18bf832018-10-26 18:14:00 -070077void build(const std::string& match, const std::vector<std::string>& data,
Deepak Kodihalli16e87542017-02-27 07:07:33 -060078 AssociationList& list) = delete;
79
Deepak Kodihalli327e5802017-02-28 02:39:56 -060080// Example template specialization - we don't want to do anything
81// for this metadata.
Willy Tu6ddbf692023-09-05 10:54:16 -070082using namespace example::xyz::openbmc_project::example::elog;
Deepak Kodihalli327e5802017-02-28 02:39:56 -060083template <>
Patrick Williams075c7922024-08-16 15:19:49 -040084inline void build<TestErrorTwo::DEV_ID>(
85 const std::string& /*match*/, const std::vector<std::string>& /*data*/,
86 AssociationList& /*list*/)
Patrick Williams2544b412022-10-04 08:41:06 -050087{}
Deepak Kodihalli327e5802017-02-28 02:39:56 -060088
Deepak Kodihalli739e9252017-03-05 23:23:50 -060089template <>
Patrick Williams075c7922024-08-16 15:19:49 -040090inline void build<example::xyz::openbmc_project::example::device::Callout::
91 CALLOUT_DEVICE_PATH_TEST>(
92 const std::string& match, const std::vector<std::string>& data,
93 AssociationList& list)
Deepak Kodihalli739e9252017-03-05 23:23:50 -060094{
Deepak Kodihalli739e9252017-03-05 23:23:50 -060095 std::map<std::string, std::string> metadata;
96 parse(data, metadata);
97 auto iter = metadata.find(match);
Patrick Venturef18bf832018-10-26 18:14:00 -070098 if (metadata.end() != iter)
Deepak Kodihalli739e9252017-03-05 23:23:50 -060099 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700100 auto comp = [](const auto& first, const auto& second) {
Patrick Venture30047bf2018-11-01 18:52:15 -0700101 return (std::strcmp(std::get<0>(first), second) < 0);
Deepak Kodihalli739e9252017-03-05 23:23:50 -0600102 };
Patrick Venturef18bf832018-10-26 18:14:00 -0700103 auto callout = std::lower_bound(callouts.begin(), callouts.end(),
104 (iter->second).c_str(), comp);
105 if ((callouts.end() != callout) &&
Patrick Venture30047bf2018-11-01 18:52:15 -0700106 !std::strcmp((iter->second).c_str(), std::get<0>(*callout)))
Deepak Kodihalli739e9252017-03-05 23:23:50 -0600107 {
Patrick Venturecaa73ad2018-10-30 13:13:06 -0700108 constexpr auto ROOT = "/xyz/openbmc_project/inventory";
109
Patrick Venturef18bf832018-10-26 18:14:00 -0700110 list.push_back(std::make_tuple(
111 "callout", "fault", std::string(ROOT) + std::get<1>(*callout)));
Deepak Kodihalli739e9252017-03-05 23:23:50 -0600112 }
113 }
114}
115
Deepak Kodihalli5edacf32017-03-16 07:04:07 -0500116// The PROCESS_META flag is needed to get out of tree builds working. Such
117// builds will have access only to internal error interfaces, hence handlers
118// for out dbus error interfaces won't compile. This flag is not set by default,
119// the phosphor-logging recipe enabled it.
Deepak Kodihalli682326a2017-03-06 05:26:53 -0600120#if defined PROCESS_META
121
122template <>
Willy Tu6ddbf692023-09-05 10:54:16 -0700123void build<xyz::openbmc_project::common::callout::Device::CALLOUT_DEVICE_PATH>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700124 const std::string& match, const std::vector<std::string>& data,
Deepak Kodihalli682326a2017-03-06 05:26:53 -0600125 AssociationList& list);
126
Tom Joseph213aaf62017-07-25 00:02:09 +0530127template <>
Patrick Venturef18bf832018-10-26 18:14:00 -0700128void build<
Willy Tu6ddbf692023-09-05 10:54:16 -0700129 xyz::openbmc_project::common::callout::Inventory::CALLOUT_INVENTORY_PATH>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700130 const std::string& match, const std::vector<std::string>& data,
Tom Joseph213aaf62017-07-25 00:02:09 +0530131 AssociationList& list);
132
Deepak Kodihalli682326a2017-03-06 05:26:53 -0600133#endif // PROCESS_META
134
Deepak Kodihalli16e87542017-02-27 07:07:33 -0600135} // namespace associations
136} // namespace metadata
137} // namespace logging
138} // namespace phosphor