blob: 2272cf507325a5d250f29a68f287d0c72af33996 [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.
82using namespace example::xyz::openbmc_project::Example::Elog;
83template <>
Patrick Williams86bb3fe2021-04-16 06:34:05 -050084inline void
85 build<TestErrorTwo::DEV_ID>(const std::string& /*match*/,
86 const std::vector<std::string>& /*data*/,
87 AssociationList& /*list*/)
Patrick Williams2544b412022-10-04 08:41:06 -050088{}
Deepak Kodihalli327e5802017-02-28 02:39:56 -060089
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