xref: /openbmc/ibm-logging/policy_find.cpp (revision 8e24dbc0)
1 /**
2  * Copyright © 2018 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <phosphor-logging/log.hpp>
17 #include "policy_find.hpp"
18 
19 namespace ibm
20 {
21 namespace logging
22 {
23 namespace policy
24 {
25 
26 namespace optional_ns = std::experimental;
27 
28 /**
29  * Returns a property value from a map of properties.
30  *
31  * @tparam - T the property data type
32  * @param[in] properties - the property map
33  * @param[in] name - the property name
34  *
35  * @return optional<T> - the property value
36  */
37 template<typename T>
38 optional_ns::optional<T> getProperty(
39         const DbusPropertyMap& properties,
40         const std::string& name)
41 {
42     auto prop = properties.find(name);
43 
44     if (prop != properties.end())
45     {
46         return prop->second.template get<T>();
47     }
48 
49     return {};
50 }
51 
52 /**
53  * Returns the search modifier to use.
54  *
55  * The modifier is used when the error name itself isn't granular
56  * enough to find a policy table entry.  The modifier is determined
57  * using rules provided by the IBM service team.
58  *
59  * Not all errors need a modifier, so this function isn't
60  * guaranteed to find one.
61  *
62  * @param[in] properties - the property map for the error
63  *
64  * @return string - the search modifier
65  *                  may be empty if none found
66  */
67 auto getSearchModifier(
68         const DbusPropertyMap& properties)
69 {
70     std::string modifier;
71 
72     auto data = getProperty<std::vector<std::string>>(
73             properties,
74             "AdditionalData");
75 
76     if (data)
77     {
78         //TODO
79     }
80 
81     return modifier;
82 }
83 
84 PolicyProps find(
85         const policy::Table& policy,
86         const DbusPropertyMap& errorLogProperties)
87 {
88     auto errorMsg = getProperty<std::string>(
89             errorLogProperties, "Message"); //e.g. xyz.X.Error.Y
90     if (errorMsg)
91     {
92         auto modifier = getSearchModifier(errorLogProperties);
93 
94         auto result = policy.find(*errorMsg, modifier);
95 
96         if (result)
97         {
98             return {(*result).get().ceid, (*result).get().msg};
99         }
100     }
101     else
102     {
103         using namespace phosphor::logging;
104         log<level::ERR>("No Message metadata found in an error");
105     }
106 
107     return {policy.defaultEID(), policy.defaultMsg()};
108 }
109 
110 }
111 }
112 }
113