xref: /openbmc/bmcweb/redfish-core/include/utils/manager_utils.hpp (revision 0fa3418803cb0af347903ae0b7d741f63d49cd32)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include "async_resp.hpp"
6 #include "error_messages.hpp"
7 #include "persistent_data.hpp"
8 
9 #include <nlohmann/json.hpp>
10 
11 #include <cstddef>
12 #include <memory>
13 #include <string_view>
14 
15 namespace redfish
16 {
17 
18 namespace manager_utils
19 {
20 
setServiceIdentification(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,std::string_view serviceIdentification)21 inline void setServiceIdentification(
22     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23     std::string_view serviceIdentification)
24 {
25     constexpr const size_t maxStrSize = 99;
26     constexpr const char* allowedChars =
27         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 _-";
28     if (serviceIdentification.size() > maxStrSize)
29     {
30         messages::stringValueTooLong(asyncResp->res, "ServiceIdentification",
31                                      maxStrSize);
32         return;
33     }
34     if (serviceIdentification.find_first_not_of(allowedChars) !=
35         std::string_view::npos)
36     {
37         messages::propertyValueError(asyncResp->res, "ServiceIdentification");
38         return;
39     }
40 
41     persistent_data::ConfigFile& config = persistent_data::getConfig();
42     config.serviceIdentification = serviceIdentification;
43     config.writeData();
44     messages::success(asyncResp->res);
45 }
46 
getServiceIdentification(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const bool isServiceRoot)47 inline void getServiceIdentification(
48     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
49     const bool isServiceRoot)
50 {
51     std::string_view serviceIdentification =
52         persistent_data::getConfig().serviceIdentification;
53 
54     // This property shall not be present if its value is an empty string or
55     // null: Redfish Data Model Specification 6.125.3
56     if (isServiceRoot && serviceIdentification.empty())
57     {
58         return;
59     }
60     asyncResp->res.jsonValue["ServiceIdentification"] = serviceIdentification;
61 }
62 
63 } // namespace manager_utils
64 
65 } // namespace redfish
66