xref: /openbmc/entity-manager/src/utils.cpp (revision 6eb60972)
1 /*
2 // Copyright (c) 2017 Intel 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 /// \file utils.cpp
17 
18 #include "utils.hpp"
19 
20 #include "expression.hpp"
21 #include "variant_visitors.hpp"
22 
23 #include <boost/algorithm/string/classification.hpp>
24 #include <boost/algorithm/string/find.hpp>
25 #include <boost/algorithm/string/predicate.hpp>
26 #include <boost/algorithm/string/replace.hpp>
27 #include <boost/algorithm/string/split.hpp>
28 #include <boost/container/flat_map.hpp>
29 #include <boost/lexical_cast.hpp>
30 #include <sdbusplus/bus/match.hpp>
31 #include <valijson/adapters/nlohmann_json_adapter.hpp>
32 #include <valijson/schema.hpp>
33 #include <valijson/schema_parser.hpp>
34 #include <valijson/validator.hpp>
35 
36 #include <charconv>
37 #include <filesystem>
38 #include <fstream>
39 #include <map>
40 #include <regex>
41 
42 constexpr const char* templateChar = "$";
43 
44 namespace fs = std::filesystem;
45 static bool powerStatusOn = false;
46 static std::unique_ptr<sdbusplus::bus::match_t> powerMatch = nullptr;
47 
48 bool findFiles(const fs::path& dirPath, const std::string& matchString,
49                std::vector<fs::path>& foundPaths)
50 {
51     if (!fs::exists(dirPath))
52     {
53         return false;
54     }
55 
56     std::regex search(matchString);
57     std::smatch match;
58     for (const auto& p : fs::directory_iterator(dirPath))
59     {
60         std::string path = p.path().string();
61         if (std::regex_search(path, match, search))
62         {
63             foundPaths.emplace_back(p.path());
64         }
65     }
66     return true;
67 }
68 
69 bool findFiles(const std::vector<fs::path>&& dirPaths,
70                const std::string& matchString,
71                std::vector<fs::path>& foundPaths)
72 {
73     std::map<fs::path, fs::path> paths;
74     std::regex search(matchString);
75     std::smatch match;
76     for (const auto& dirPath : dirPaths)
77     {
78         if (!fs::exists(dirPath))
79         {
80             continue;
81         }
82 
83         for (const auto& p : fs::directory_iterator(dirPath))
84         {
85             std::string path = p.path().string();
86             if (std::regex_search(path, match, search))
87             {
88                 paths[p.path().filename()] = p.path();
89             }
90         }
91     }
92 
93     for (const auto& [key, value] : paths)
94     {
95         foundPaths.emplace_back(value);
96     }
97 
98     return !foundPaths.empty();
99 }
100 
101 bool getI2cDevicePaths(const fs::path& dirPath,
102                        boost::container::flat_map<size_t, fs::path>& busPaths)
103 {
104     if (!fs::exists(dirPath))
105     {
106         return false;
107     }
108 
109     // Regex for matching the path
110     std::regex searchPath(std::string(R"(i2c-\d+$)"));
111     // Regex for matching the bus numbers
112     std::regex searchBus(std::string(R"(\w[^-]*$)"));
113     std::smatch matchPath;
114     std::smatch matchBus;
115     for (const auto& p : fs::directory_iterator(dirPath))
116     {
117         std::string path = p.path().string();
118         if (std::regex_search(path, matchPath, searchPath))
119         {
120             if (std::regex_search(path, matchBus, searchBus))
121             {
122                 size_t bus = stoul(*matchBus.begin());
123                 busPaths.insert(std::pair<size_t, fs::path>(bus, p.path()));
124             }
125         }
126     }
127 
128     return true;
129 }
130 
131 bool validateJson(const nlohmann::json& schemaFile, const nlohmann::json& input)
132 {
133     valijson::Schema schema;
134     valijson::SchemaParser parser;
135     valijson::adapters::NlohmannJsonAdapter schemaAdapter(schemaFile);
136     parser.populateSchema(schemaAdapter, schema);
137     valijson::Validator validator;
138     valijson::adapters::NlohmannJsonAdapter targetAdapter(input);
139     return validator.validate(schema, targetAdapter, nullptr);
140 }
141 
142 bool isPowerOn(void)
143 {
144     if (!powerMatch)
145     {
146         throw std::runtime_error("Power Match Not Created");
147     }
148     return powerStatusOn;
149 }
150 
151 void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
152 {
153     powerMatch = std::make_unique<sdbusplus::bus::match_t>(
154         static_cast<sdbusplus::bus_t&>(*conn),
155         "type='signal',interface='" + std::string(properties::interface) +
156             "',path='" + std::string(power::path) + "',arg0='" +
157             std::string(power::interface) + "'",
158         [](sdbusplus::message_t& message) {
159         std::string objectName;
160         boost::container::flat_map<std::string, std::variant<std::string>>
161             values;
162         message.read(objectName, values);
163         auto findState = values.find(power::property);
164         if (findState != values.end())
165         {
166             powerStatusOn = boost::ends_with(
167                 std::get<std::string>(findState->second), "Running");
168         }
169         });
170 
171     conn->async_method_call(
172         [](boost::system::error_code ec,
173            const std::variant<std::string>& state) {
174         if (ec)
175         {
176             return;
177         }
178         powerStatusOn = boost::ends_with(std::get<std::string>(state),
179                                          "Running");
180         },
181         power::busname, power::path, properties::interface, properties::get,
182         power::interface, power::property);
183 }
184 
185 // Replaces the template character like the other version of this function,
186 // but checks all properties on all interfaces provided to do the substitution
187 // with.
188 std::optional<std::string>
189     templateCharReplace(nlohmann::json::iterator& keyPair,
190                         const DBusObject& object, const size_t index,
191                         const std::optional<std::string>& replaceStr)
192 {
193     for (const auto& [_, interface] : object)
194     {
195         auto ret = templateCharReplace(keyPair, interface, index, replaceStr);
196         if (ret)
197         {
198             return ret;
199         }
200     }
201     return std::nullopt;
202 }
203 
204 // finds the template character (currently set to $) and replaces the value with
205 // the field found in a dbus object i.e. $ADDRESS would get populated with the
206 // ADDRESS field from a object on dbus
207 std::optional<std::string>
208     templateCharReplace(nlohmann::json::iterator& keyPair,
209                         const DBusInterface& interface, const size_t index,
210                         const std::optional<std::string>& replaceStr)
211 {
212     std::optional<std::string> ret = std::nullopt;
213 
214     if (keyPair.value().type() == nlohmann::json::value_t::object ||
215         keyPair.value().type() == nlohmann::json::value_t::array)
216     {
217         for (auto nextLayer = keyPair.value().begin();
218              nextLayer != keyPair.value().end(); nextLayer++)
219         {
220             templateCharReplace(nextLayer, interface, index, replaceStr);
221         }
222         return ret;
223     }
224 
225     std::string* strPtr = keyPair.value().get_ptr<std::string*>();
226     if (strPtr == nullptr)
227     {
228         return ret;
229     }
230 
231     boost::replace_all(*strPtr, std::string(templateChar) + "index",
232                        std::to_string(index));
233     if (replaceStr)
234     {
235         boost::replace_all(*strPtr, *replaceStr, std::to_string(index));
236     }
237 
238     for (const auto& [propName, propValue] : interface)
239     {
240         std::string templateName = templateChar + propName;
241         boost::iterator_range<std::string::const_iterator> find =
242             boost::ifind_first(*strPtr, templateName);
243         if (!find)
244         {
245             continue;
246         }
247 
248         size_t start = find.begin() - strPtr->begin();
249 
250         // check for additional operations
251         if ((start == 0U) && find.end() == strPtr->end())
252         {
253             std::visit([&](auto&& val) { keyPair.value() = val; }, propValue);
254             return ret;
255         }
256 
257         constexpr const std::array<char, 5> mathChars = {'+', '-', '%', '*',
258                                                          '/'};
259         size_t nextItemIdx = start + templateName.size() + 1;
260 
261         if (nextItemIdx > strPtr->size() ||
262             std::find(mathChars.begin(), mathChars.end(),
263                       strPtr->at(nextItemIdx)) == mathChars.end())
264         {
265             std::string val = std::visit(VariantToStringVisitor(), propValue);
266             boost::ireplace_all(*strPtr, templateName, val);
267             continue;
268         }
269 
270         // save the prefix
271         std::string prefix = strPtr->substr(0, start);
272 
273         // operate on the rest
274         std::string end = strPtr->substr(nextItemIdx);
275 
276         std::vector<std::string> split;
277         boost::split(split, end, boost::is_any_of(" "));
278 
279         // need at least 1 operation and number
280         if (split.size() < 2)
281         {
282             std::cerr << "Syntax error on template replacement of " << *strPtr
283                       << "\n";
284             for (const std::string& data : split)
285             {
286                 std::cerr << data << " ";
287             }
288             std::cerr << "\n";
289             continue;
290         }
291 
292         // we assume that the replacement is a number, because we can
293         // only do math on numbers.. we might concatenate strings in the
294         // future, but thats later
295         int number = std::visit(VariantToIntVisitor(), propValue);
296         auto exprBegin = split.begin();
297         auto exprEnd = split.end();
298 
299         number = expression::evaluate(number, exprBegin, exprEnd);
300 
301         std::string replaced(find.begin(), find.end());
302         while (exprBegin != exprEnd)
303         {
304             replaced.append(" ").append(*exprBegin++);
305         }
306         ret = replaced;
307 
308         std::string result = prefix + std::to_string(number);
309         while (exprEnd != split.end())
310         {
311             result.append(" ").append(*exprEnd++);
312         }
313         keyPair.value() = result;
314 
315         // We probably just invalidated the pointer abovei,
316         // reset and continue to handle multiple templates
317         strPtr = keyPair.value().get_ptr<std::string*>();
318         if (strPtr == nullptr)
319         {
320             break;
321         }
322     }
323 
324     strPtr = keyPair.value().get_ptr<std::string*>();
325     if (strPtr == nullptr)
326     {
327         return ret;
328     }
329 
330     std::string_view strView = *strPtr;
331     int base = 10;
332     if (boost::starts_with(strView, "0x"))
333     {
334         strView.remove_prefix(2);
335         base = 16;
336     }
337 
338     uint64_t temp = 0;
339     const char* strDataEndPtr = strView.data() + strView.size();
340     const std::from_chars_result res =
341         std::from_chars(strView.data(), strDataEndPtr, temp, base);
342     if (res.ec == std::errc{} && res.ptr == strDataEndPtr)
343     {
344         keyPair.value() = temp;
345     }
346 
347     return ret;
348 }
349 
350 /// \brief JSON/DBus matching Callable for std::variant (visitor)
351 ///
352 /// Default match JSON/DBus match implementation
353 /// \tparam T The concrete DBus value type from DBusValueVariant
354 template <typename T>
355 struct MatchProbe
356 {
357     /// \param probe the probe statement to match against
358     /// \param value the property value being matched to a probe
359     /// \return true if the dbusValue matched the probe otherwise false
360     static bool match(const nlohmann::json& probe, const T& value)
361     {
362         return probe == value;
363     }
364 };
365 
366 /// \brief JSON/DBus matching Callable for std::variant (visitor)
367 ///
368 /// std::string specialization of MatchProbe enabling regex matching
369 template <>
370 struct MatchProbe<std::string>
371 {
372     /// \param probe the probe statement to match against
373     /// \param value the string value being matched to a probe
374     /// \return true if the dbusValue matched the probe otherwise false
375     static bool match(const nlohmann::json& probe, const std::string& value)
376     {
377         if (probe.is_string())
378         {
379             try
380             {
381                 std::regex search(probe);
382                 std::smatch regMatch;
383                 return std::regex_search(value, regMatch, search);
384             }
385             catch (const std::regex_error&)
386             {
387                 std::cerr << "Syntax error in regular expression: " << probe
388                           << " will never match";
389             }
390         }
391 
392         // Skip calling nlohmann here, since it will never match a non-string
393         // to a std::string
394         return false;
395     }
396 };
397 
398 /// \brief Forwarding JSON/DBus matching Callable for std::variant (visitor)
399 ///
400 /// Forward calls to the correct template instantiation of MatchProbe
401 struct MatchProbeForwarder
402 {
403     explicit MatchProbeForwarder(const nlohmann::json& probe) : probeRef(probe)
404     {}
405     const nlohmann::json& probeRef;
406 
407     template <typename T>
408     bool operator()(const T& dbusValue) const
409     {
410         return MatchProbe<T>::match(probeRef, dbusValue);
411     }
412 };
413 
414 bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue)
415 {
416     return std::visit(MatchProbeForwarder(probe), dbusValue);
417 }
418