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