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