xref: /openbmc/bmcweb/features/redfish/lib/network_protocol.hpp (revision 67a78d8758859a4e4477de932d24fac5fb287dad)
170141561SBorawski.Lukasz /*
270141561SBorawski.Lukasz // Copyright (c) 2018 Intel Corporation
370141561SBorawski.Lukasz //
470141561SBorawski.Lukasz // Licensed under the Apache License, Version 2.0 (the "License");
570141561SBorawski.Lukasz // you may not use this file except in compliance with the License.
670141561SBorawski.Lukasz // You may obtain a copy of the License at
770141561SBorawski.Lukasz //
870141561SBorawski.Lukasz //      http://www.apache.org/licenses/LICENSE-2.0
970141561SBorawski.Lukasz //
1070141561SBorawski.Lukasz // Unless required by applicable law or agreed to in writing, software
1170141561SBorawski.Lukasz // distributed under the License is distributed on an "AS IS" BASIS,
1270141561SBorawski.Lukasz // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1370141561SBorawski.Lukasz // See the License for the specific language governing permissions and
1470141561SBorawski.Lukasz // limitations under the License.
1570141561SBorawski.Lukasz */
1670141561SBorawski.Lukasz #pragma once
1770141561SBorawski.Lukasz 
183a8a0088SKowalski, Kamil #include "error_messages.hpp"
1970141561SBorawski.Lukasz #include "node.hpp"
20*67a78d87STom Joseph #include "openbmc_dbus_rest.hpp"
2170141561SBorawski.Lukasz 
2220e6ea5dSraviteja-b #include <utils/json_utils.hpp>
231214b7e7SGunnar Mills 
241214b7e7SGunnar Mills #include <optional>
25abf2add6SEd Tanous #include <variant>
261abe55efSEd Tanous namespace redfish
271abe55efSEd Tanous {
2870141561SBorawski.Lukasz 
291abe55efSEd Tanous enum NetworkProtocolUnitStructFields
301abe55efSEd Tanous {
313a8a0088SKowalski, Kamil     NET_PROTO_UNIT_NAME,
323a8a0088SKowalski, Kamil     NET_PROTO_UNIT_DESC,
333a8a0088SKowalski, Kamil     NET_PROTO_UNIT_LOAD_STATE,
343a8a0088SKowalski, Kamil     NET_PROTO_UNIT_ACTIVE_STATE,
353a8a0088SKowalski, Kamil     NET_PROTO_UNIT_SUB_STATE,
363a8a0088SKowalski, Kamil     NET_PROTO_UNIT_DEVICE,
373a8a0088SKowalski, Kamil     NET_PROTO_UNIT_OBJ_PATH,
383a8a0088SKowalski, Kamil     NET_PROTO_UNIT_ALWAYS_0,
393a8a0088SKowalski, Kamil     NET_PROTO_UNIT_ALWAYS_EMPTY,
403a8a0088SKowalski, Kamil     NET_PROTO_UNIT_ALWAYS_ROOT_PATH
413a8a0088SKowalski, Kamil };
423a8a0088SKowalski, Kamil 
431abe55efSEd Tanous enum NetworkProtocolListenResponseElements
441abe55efSEd Tanous {
453a8a0088SKowalski, Kamil     NET_PROTO_LISTEN_TYPE,
463a8a0088SKowalski, Kamil     NET_PROTO_LISTEN_STREAM
473a8a0088SKowalski, Kamil };
483a8a0088SKowalski, Kamil 
493a8a0088SKowalski, Kamil /**
503a8a0088SKowalski, Kamil  * @brief D-Bus Unit structure returned in array from ListUnits Method
513a8a0088SKowalski, Kamil  */
523a8a0088SKowalski, Kamil using UnitStruct =
533a8a0088SKowalski, Kamil     std::tuple<std::string, std::string, std::string, std::string, std::string,
543a8a0088SKowalski, Kamil                std::string, sdbusplus::message::object_path, uint32_t,
553a8a0088SKowalski, Kamil                std::string, sdbusplus::message::object_path>;
563a8a0088SKowalski, Kamil 
57ec4974ddSAppaRao Puli const static boost::container::flat_map<const char*, std::string>
58ec4974ddSAppaRao Puli     protocolToDBus{{"SSH", "dropbear"},
59ec4974ddSAppaRao Puli                    {"HTTPS", "bmcweb"},
60ec4974ddSAppaRao Puli                    {"IPMI", "phosphor-ipmi-net"}};
613a8a0088SKowalski, Kamil 
62d24bfc7aSJennifer Lee inline void
63d24bfc7aSJennifer Lee     extractNTPServersAndDomainNamesData(const GetManagedObjects& dbus_data,
64d24bfc7aSJennifer Lee                                         std::vector<std::string>& ntpData,
65d24bfc7aSJennifer Lee                                         std::vector<std::string>& dnData)
6620e6ea5dSraviteja-b {
6720e6ea5dSraviteja-b     for (const auto& obj : dbus_data)
6820e6ea5dSraviteja-b     {
6920e6ea5dSraviteja-b         for (const auto& ifacePair : obj.second)
7020e6ea5dSraviteja-b         {
7120e6ea5dSraviteja-b             if (obj.first == "/xyz/openbmc_project/network/eth0")
7220e6ea5dSraviteja-b             {
7320e6ea5dSraviteja-b                 if (ifacePair.first ==
7420e6ea5dSraviteja-b                     "xyz.openbmc_project.Network.EthernetInterface")
7520e6ea5dSraviteja-b                 {
7620e6ea5dSraviteja-b                     for (const auto& propertyPair : ifacePair.second)
7720e6ea5dSraviteja-b                     {
7820e6ea5dSraviteja-b                         if (propertyPair.first == "NTPServers")
7920e6ea5dSraviteja-b                         {
8020e6ea5dSraviteja-b                             const std::vector<std::string>* ntpServers =
818d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
8220e6ea5dSraviteja-b                                     &propertyPair.second);
8320e6ea5dSraviteja-b                             if (ntpServers != nullptr)
8420e6ea5dSraviteja-b                             {
8520e6ea5dSraviteja-b                                 ntpData = std::move(*ntpServers);
8620e6ea5dSraviteja-b                             }
8720e6ea5dSraviteja-b                         }
88d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
89d24bfc7aSJennifer Lee                         {
90d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
918d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
92d24bfc7aSJennifer Lee                                     &propertyPair.second);
93d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
94d24bfc7aSJennifer Lee                             {
95d24bfc7aSJennifer Lee                                 dnData = std::move(*domainNames);
96d24bfc7aSJennifer Lee                             }
97d24bfc7aSJennifer Lee                         }
9820e6ea5dSraviteja-b                     }
9920e6ea5dSraviteja-b                 }
10020e6ea5dSraviteja-b             }
10120e6ea5dSraviteja-b         }
10220e6ea5dSraviteja-b     }
10320e6ea5dSraviteja-b }
10420e6ea5dSraviteja-b 
10520e6ea5dSraviteja-b template <typename CallbackFunc>
10620e6ea5dSraviteja-b void getEthernetIfaceData(CallbackFunc&& callback)
10720e6ea5dSraviteja-b {
10820e6ea5dSraviteja-b     crow::connections::systemBus->async_method_call(
10920e6ea5dSraviteja-b         [callback{std::move(callback)}](
11020e6ea5dSraviteja-b             const boost::system::error_code error_code,
11120e6ea5dSraviteja-b             const GetManagedObjects& dbus_data) {
11220e6ea5dSraviteja-b             std::vector<std::string> ntpServers;
113d24bfc7aSJennifer Lee             std::vector<std::string> domainNames;
11420e6ea5dSraviteja-b 
11520e6ea5dSraviteja-b             if (error_code)
11620e6ea5dSraviteja-b             {
117d24bfc7aSJennifer Lee                 callback(false, ntpServers, domainNames);
11820e6ea5dSraviteja-b                 return;
11920e6ea5dSraviteja-b             }
12020e6ea5dSraviteja-b 
121d24bfc7aSJennifer Lee             extractNTPServersAndDomainNamesData(dbus_data, ntpServers,
122d24bfc7aSJennifer Lee                                                 domainNames);
12320e6ea5dSraviteja-b 
124d24bfc7aSJennifer Lee             callback(true, ntpServers, domainNames);
12520e6ea5dSraviteja-b         },
12620e6ea5dSraviteja-b         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
12720e6ea5dSraviteja-b         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
128271584abSEd Tanous }
12920e6ea5dSraviteja-b 
1301abe55efSEd Tanous class NetworkProtocol : public Node
1311abe55efSEd Tanous {
13270141561SBorawski.Lukasz   public:
1331abe55efSEd Tanous     NetworkProtocol(CrowApp& app) :
1347af91514SGunnar Mills         Node(app, "/redfish/v1/Managers/bmc/NetworkProtocol/")
1351abe55efSEd Tanous     {
1364b1b8683SBorawski.Lukasz         entityPrivileges = {
1374b1b8683SBorawski.Lukasz             {boost::beast::http::verb::get, {{"Login"}}},
138e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
139e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
140e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
141e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
142e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
14370141561SBorawski.Lukasz     }
14470141561SBorawski.Lukasz 
14570141561SBorawski.Lukasz   private:
14655c7b7a2SEd Tanous     void doGet(crow::Response& res, const crow::Request& req,
1471abe55efSEd Tanous                const std::vector<std::string>& params) override
1481abe55efSEd Tanous     {
1493a8a0088SKowalski, Kamil         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1503a8a0088SKowalski, Kamil 
1513a8a0088SKowalski, Kamil         getData(asyncResp);
15270141561SBorawski.Lukasz     }
15370141561SBorawski.Lukasz 
1541abe55efSEd Tanous     std::string getHostName() const
1551abe55efSEd Tanous     {
15670141561SBorawski.Lukasz         std::string hostName;
15770141561SBorawski.Lukasz 
15870141561SBorawski.Lukasz         std::array<char, HOST_NAME_MAX> hostNameCStr;
1591abe55efSEd Tanous         if (gethostname(hostNameCStr.data(), hostNameCStr.size()) == 0)
1601abe55efSEd Tanous         {
16170141561SBorawski.Lukasz             hostName = hostNameCStr.data();
16270141561SBorawski.Lukasz         }
16370141561SBorawski.Lukasz         return hostName;
16470141561SBorawski.Lukasz     }
16570141561SBorawski.Lukasz 
16620e6ea5dSraviteja-b     void getNTPProtocolEnabled(const std::shared_ptr<AsyncResp>& asyncResp)
16720e6ea5dSraviteja-b     {
16820e6ea5dSraviteja-b         crow::connections::systemBus->async_method_call(
16920e6ea5dSraviteja-b             [asyncResp](const boost::system::error_code error_code,
17020e6ea5dSraviteja-b                         const std::variant<std::string>& timeSyncMethod) {
17120e6ea5dSraviteja-b                 const std::string* s =
17220e6ea5dSraviteja-b                     std::get_if<std::string>(&timeSyncMethod);
17320e6ea5dSraviteja-b 
17420e6ea5dSraviteja-b                 if (*s == "xyz.openbmc_project.Time.Synchronization.Method.NTP")
17520e6ea5dSraviteja-b                 {
17620e6ea5dSraviteja-b                     asyncResp->res.jsonValue["NTP"]["ProtocolEnabled"] = true;
17720e6ea5dSraviteja-b                 }
17820e6ea5dSraviteja-b                 else if (*s == "xyz.openbmc_project.Time.Synchronization."
17920e6ea5dSraviteja-b                                "Method.Manual")
18020e6ea5dSraviteja-b                 {
18120e6ea5dSraviteja-b                     asyncResp->res.jsonValue["NTP"]["ProtocolEnabled"] = false;
18220e6ea5dSraviteja-b                 }
18320e6ea5dSraviteja-b             },
18420e6ea5dSraviteja-b             "xyz.openbmc_project.Settings",
18520e6ea5dSraviteja-b             "/xyz/openbmc_project/time/sync_method",
18620e6ea5dSraviteja-b             "org.freedesktop.DBus.Properties", "Get",
18720e6ea5dSraviteja-b             "xyz.openbmc_project.Time.Synchronization", "TimeSyncMethod");
18820e6ea5dSraviteja-b     }
18920e6ea5dSraviteja-b 
1901abe55efSEd Tanous     void getData(const std::shared_ptr<AsyncResp>& asyncResp)
1911abe55efSEd Tanous     {
1920f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
19361932318SXiaochao Ma             "#ManagerNetworkProtocol.v1_5_0.ManagerNetworkProtocol";
1940f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1950f74e643SEd Tanous             "/redfish/v1/Managers/bmc/NetworkProtocol";
1960f74e643SEd Tanous         asyncResp->res.jsonValue["Id"] = "NetworkProtocol";
1970f74e643SEd Tanous         asyncResp->res.jsonValue["Name"] = "Manager Network Protocol";
1980f74e643SEd Tanous         asyncResp->res.jsonValue["Description"] = "Manager Network Service";
1990f74e643SEd Tanous         asyncResp->res.jsonValue["Status"]["Health"] = "OK";
2000f74e643SEd Tanous         asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
2010f74e643SEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
20261932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["ProtocolEnabled"] = true;
20361932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["Port"] = 161;
20461932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["AuthenticationProtocol"] =
20561932318SXiaochao Ma             "CommunityString";
20661932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["CommunityAccessMode"] = "Full";
20761932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["HideCommunityStrings"] = true;
20861932318SXiaochao Ma         asyncResp->res
20961932318SXiaochao Ma             .jsonValue["SNMP"]["EngineId"]["EnterpriseSpecificMethod"] =
21061932318SXiaochao Ma             nullptr;
21161932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["EngineId"]["PrivateEnterpriseId"] =
21261932318SXiaochao Ma             nullptr;
21361932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["EnableSNMPv1"] = false;
21461932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["EnableSNMPv2c"] = true;
21561932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["EnableSNMPv3"] = false;
21661932318SXiaochao Ma         asyncResp->res.jsonValue["SNMP"]["EncryptionProtocol"] = "None";
21761932318SXiaochao Ma         nlohmann::json& memberArray =
21861932318SXiaochao Ma             asyncResp->res.jsonValue["SNMP"]["CommunityStrings"];
21961932318SXiaochao Ma         memberArray = nlohmann::json::array();
22061932318SXiaochao Ma         memberArray.push_back({{"AccessMode", "Full"}});
22161932318SXiaochao Ma         memberArray.push_back({{"CommunityString", ""}});
22261932318SXiaochao Ma         memberArray.push_back({{"Name", ""}});
2230f74e643SEd Tanous 
22461932318SXiaochao Ma         // HTTP is Mandatory attribute as per OCP Baseline Profile - v1.0.0,
225818ea7b8SJoshi-Mansi         // but from security perspective it is not recommended to use.
226818ea7b8SJoshi-Mansi         // Hence using protocolEnabled as false to make it OCP and security-wise
227818ea7b8SJoshi-Mansi         // compliant
228818ea7b8SJoshi-Mansi         asyncResp->res.jsonValue["HTTP"]["Port"] = 0;
229818ea7b8SJoshi-Mansi         asyncResp->res.jsonValue["HTTP"]["ProtocolEnabled"] = false;
230818ea7b8SJoshi-Mansi 
2310f74e643SEd Tanous         for (auto& protocol : protocolToDBus)
2320f74e643SEd Tanous         {
2330f74e643SEd Tanous             asyncResp->res.jsonValue[protocol.first]["ProtocolEnabled"] = false;
2340f74e643SEd Tanous         }
2350f74e643SEd Tanous 
236d24bfc7aSJennifer Lee         std::string hostName = getHostName();
237d24bfc7aSJennifer Lee 
238d24bfc7aSJennifer Lee         asyncResp->res.jsonValue["HostName"] = hostName;
2393a8a0088SKowalski, Kamil 
24020e6ea5dSraviteja-b         getNTPProtocolEnabled(asyncResp);
24120e6ea5dSraviteja-b 
24220e6ea5dSraviteja-b         // TODO Get eth0 interface data, and call the below callback for JSON
24320e6ea5dSraviteja-b         // preparation
244271584abSEd Tanous         getEthernetIfaceData(
245271584abSEd Tanous             [hostName, asyncResp](const bool& success,
246d24bfc7aSJennifer Lee                                   const std::vector<std::string>& ntpServers,
247d24bfc7aSJennifer Lee                                   const std::vector<std::string>& domainNames) {
24820e6ea5dSraviteja-b                 if (!success)
24920e6ea5dSraviteja-b                 {
250271584abSEd Tanous                     messages::resourceNotFound(asyncResp->res,
251271584abSEd Tanous                                                "EthernetInterface", "eth0");
25220e6ea5dSraviteja-b                     return;
25320e6ea5dSraviteja-b                 }
25420e6ea5dSraviteja-b                 asyncResp->res.jsonValue["NTP"]["NTPServers"] = ntpServers;
255d24bfc7aSJennifer Lee                 if (hostName.empty() == false)
256d24bfc7aSJennifer Lee                 {
257749dad7dSRatan Gupta                     std::string FQDN = std::move(hostName);
258d24bfc7aSJennifer Lee                     if (domainNames.empty() == false)
259d24bfc7aSJennifer Lee                     {
260749dad7dSRatan Gupta                         FQDN += "." + domainNames[0];
261d24bfc7aSJennifer Lee                     }
262749dad7dSRatan Gupta                     asyncResp->res.jsonValue["FQDN"] = std::move(FQDN);
263d24bfc7aSJennifer Lee                 }
26420e6ea5dSraviteja-b             });
26520e6ea5dSraviteja-b 
266865fbb75SEd Tanous         crow::connections::systemBus->async_method_call(
267271584abSEd Tanous             [asyncResp](const boost::system::error_code e,
268271584abSEd Tanous                         const std::vector<UnitStruct>& r) {
269271584abSEd Tanous                 if (e)
2701abe55efSEd Tanous                 {
2713a8a0088SKowalski, Kamil                     asyncResp->res.jsonValue = nlohmann::json::object();
272f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
273865fbb75SEd Tanous                     return;
2743a8a0088SKowalski, Kamil                 }
2755968caeeSMarri Devender Rao                 asyncResp->res.jsonValue["HTTPS"]["Certificates"] = {
2765968caeeSMarri Devender Rao                     {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol/"
277659dd62eSJason M. Bills                                   "HTTPS/Certificates"}};
2783a8a0088SKowalski, Kamil 
279271584abSEd Tanous                 for (auto& unit : r)
2801abe55efSEd Tanous                 {
281ec4974ddSAppaRao Puli                     /* Only traverse through <xyz>.socket units */
282ec4974ddSAppaRao Puli                     std::string unitName = std::get<NET_PROTO_UNIT_NAME>(unit);
283ec4974ddSAppaRao Puli                     if (!boost::ends_with(unitName, ".socket"))
2841abe55efSEd Tanous                     {
285865fbb75SEd Tanous                         continue;
28670141561SBorawski.Lukasz                     }
28770141561SBorawski.Lukasz 
288ec4974ddSAppaRao Puli                     for (auto& kv : protocolToDBus)
289ec4974ddSAppaRao Puli                     {
290ec4974ddSAppaRao Puli                         // We are interested in services, which starts with
291ec4974ddSAppaRao Puli                         // mapped service name
292ec4974ddSAppaRao Puli                         if (!boost::starts_with(unitName, kv.second))
293ec4974ddSAppaRao Puli                         {
294ec4974ddSAppaRao Puli                             continue;
295ec4974ddSAppaRao Puli                         }
296ec4974ddSAppaRao Puli                         const char* rfServiceKey = kv.first;
297ec4974ddSAppaRao Puli                         std::string socketPath =
298ec4974ddSAppaRao Puli                             std::get<NET_PROTO_UNIT_OBJ_PATH>(unit);
299ec4974ddSAppaRao Puli                         std::string unitState =
300ec4974ddSAppaRao Puli                             std::get<NET_PROTO_UNIT_SUB_STATE>(unit);
301ec4974ddSAppaRao Puli 
302ec4974ddSAppaRao Puli                         asyncResp->res
303ec4974ddSAppaRao Puli                             .jsonValue[rfServiceKey]["ProtocolEnabled"] =
304ec4974ddSAppaRao Puli                             (unitState == "running") ||
305ec4974ddSAppaRao Puli                             (unitState == "listening");
306ec4974ddSAppaRao Puli 
307865fbb75SEd Tanous                         crow::connections::systemBus->async_method_call(
308ec4974ddSAppaRao Puli                             [asyncResp,
309ec4974ddSAppaRao Puli                              rfServiceKey{std::string(rfServiceKey)}](
310865fbb75SEd Tanous                                 const boost::system::error_code ec,
311abf2add6SEd Tanous                                 const std::variant<std::vector<std::tuple<
312abf2add6SEd Tanous                                     std::string, std::string>>>& resp) {
3131abe55efSEd Tanous                                 if (ec)
3141abe55efSEd Tanous                                 {
315a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
316865fbb75SEd Tanous                                     return;
3173a8a0088SKowalski, Kamil                                 }
318abf2add6SEd Tanous                                 const std::vector<
319abf2add6SEd Tanous                                     std::tuple<std::string, std::string>>*
320abf2add6SEd Tanous                                     responsePtr = std::get_if<std::vector<
321abf2add6SEd Tanous                                         std::tuple<std::string, std::string>>>(
3221b6b96c5SEd Tanous                                         &resp);
3231abe55efSEd Tanous                                 if (responsePtr == nullptr ||
3241abe55efSEd Tanous                                     responsePtr->size() < 1)
3251abe55efSEd Tanous                                 {
326865fbb75SEd Tanous                                     return;
32770141561SBorawski.Lukasz                                 }
32870141561SBorawski.Lukasz 
329865fbb75SEd Tanous                                 const std::string& listenStream =
3301abe55efSEd Tanous                                     std::get<NET_PROTO_LISTEN_STREAM>(
3311abe55efSEd Tanous                                         (*responsePtr)[0]);
3321abe55efSEd Tanous                                 std::size_t lastColonPos =
3331abe55efSEd Tanous                                     listenStream.rfind(":");
3341abe55efSEd Tanous                                 if (lastColonPos == std::string::npos)
3351abe55efSEd Tanous                                 {
336865fbb75SEd Tanous                                     // Not a port
337865fbb75SEd Tanous                                     return;
338865fbb75SEd Tanous                                 }
3391abe55efSEd Tanous                                 std::string portStr =
3401abe55efSEd Tanous                                     listenStream.substr(lastColonPos + 1);
341ec4974ddSAppaRao Puli                                 if (portStr.empty())
342ec4974ddSAppaRao Puli                                 {
343ec4974ddSAppaRao Puli                                     return;
344ec4974ddSAppaRao Puli                                 }
345865fbb75SEd Tanous                                 char* endPtr = nullptr;
346ec4974ddSAppaRao Puli                                 errno = 0;
3471abe55efSEd Tanous                                 // Use strtol instead of stroi to avoid
3481abe55efSEd Tanous                                 // exceptions
3491abe55efSEd Tanous                                 long port =
3501abe55efSEd Tanous                                     std::strtol(portStr.c_str(), &endPtr, 10);
351ec4974ddSAppaRao Puli                                 if ((errno == 0) && (*endPtr == '\0'))
3521abe55efSEd Tanous                                 {
353ec4974ddSAppaRao Puli                                     asyncResp->res
354ec4974ddSAppaRao Puli                                         .jsonValue[rfServiceKey]["Port"] = port;
3551abe55efSEd Tanous                                 }
356ec4974ddSAppaRao Puli                                 return;
357865fbb75SEd Tanous                             },
358865fbb75SEd Tanous                             "org.freedesktop.systemd1", socketPath,
359865fbb75SEd Tanous                             "org.freedesktop.DBus.Properties", "Get",
360865fbb75SEd Tanous                             "org.freedesktop.systemd1.Socket", "Listen");
361ec4974ddSAppaRao Puli 
362ec4974ddSAppaRao Puli                         // We found service, break the inner loop.
363ec4974ddSAppaRao Puli                         break;
364865fbb75SEd Tanous                     }
365865fbb75SEd Tanous                 }
366865fbb75SEd Tanous             },
367865fbb75SEd Tanous             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
368865fbb75SEd Tanous             "org.freedesktop.systemd1.Manager", "ListUnits");
369865fbb75SEd Tanous     }
370501be32bSraviteja-b 
371501be32bSraviteja-b     void handleHostnamePatch(const std::string& hostName,
372501be32bSraviteja-b                              const std::shared_ptr<AsyncResp>& asyncResp)
373501be32bSraviteja-b     {
374501be32bSraviteja-b         crow::connections::systemBus->async_method_call(
375501be32bSraviteja-b             [asyncResp](const boost::system::error_code ec) {
376501be32bSraviteja-b                 if (ec)
377501be32bSraviteja-b                 {
378501be32bSraviteja-b                     messages::internalError(asyncResp->res);
379501be32bSraviteja-b                     return;
380501be32bSraviteja-b                 }
381501be32bSraviteja-b             },
382501be32bSraviteja-b             "xyz.openbmc_project.Network",
383501be32bSraviteja-b             "/xyz/openbmc_project/network/config",
384501be32bSraviteja-b             "org.freedesktop.DBus.Properties", "Set",
385501be32bSraviteja-b             "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
386501be32bSraviteja-b             std::variant<std::string>(hostName));
387501be32bSraviteja-b     }
388501be32bSraviteja-b 
38920e6ea5dSraviteja-b     void handleNTPProtocolEnabled(const bool& ntpEnabled,
39020e6ea5dSraviteja-b                                   const std::shared_ptr<AsyncResp>& asyncResp)
39120e6ea5dSraviteja-b     {
39220e6ea5dSraviteja-b         std::string timeSyncMethod;
39320e6ea5dSraviteja-b         if (ntpEnabled)
39420e6ea5dSraviteja-b         {
39520e6ea5dSraviteja-b             timeSyncMethod =
39620e6ea5dSraviteja-b                 "xyz.openbmc_project.Time.Synchronization.Method.NTP";
39720e6ea5dSraviteja-b         }
39820e6ea5dSraviteja-b         else
39920e6ea5dSraviteja-b         {
40020e6ea5dSraviteja-b             timeSyncMethod =
40120e6ea5dSraviteja-b                 "xyz.openbmc_project.Time.Synchronization.Method.Manual";
40220e6ea5dSraviteja-b         }
40320e6ea5dSraviteja-b 
40420e6ea5dSraviteja-b         crow::connections::systemBus->async_method_call(
405cf05f9dcSJohnathan Mantey             [asyncResp](const boost::system::error_code error_code) {},
40620e6ea5dSraviteja-b             "xyz.openbmc_project.Settings",
40720e6ea5dSraviteja-b             "/xyz/openbmc_project/time/sync_method",
40820e6ea5dSraviteja-b             "org.freedesktop.DBus.Properties", "Set",
40920e6ea5dSraviteja-b             "xyz.openbmc_project.Time.Synchronization", "TimeSyncMethod",
41020e6ea5dSraviteja-b             std::variant<std::string>{timeSyncMethod});
41120e6ea5dSraviteja-b     }
41220e6ea5dSraviteja-b 
41320e6ea5dSraviteja-b     void handleNTPServersPatch(const std::vector<std::string>& ntpServers,
41420e6ea5dSraviteja-b                                const std::shared_ptr<AsyncResp>& asyncResp)
41520e6ea5dSraviteja-b     {
41620e6ea5dSraviteja-b         crow::connections::systemBus->async_method_call(
417cf05f9dcSJohnathan Mantey             [asyncResp](const boost::system::error_code ec) {
41820e6ea5dSraviteja-b                 if (ec)
41920e6ea5dSraviteja-b                 {
42020e6ea5dSraviteja-b                     messages::internalError(asyncResp->res);
42120e6ea5dSraviteja-b                     return;
42220e6ea5dSraviteja-b                 }
42320e6ea5dSraviteja-b             },
42420e6ea5dSraviteja-b             "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/eth0",
42520e6ea5dSraviteja-b             "org.freedesktop.DBus.Properties", "Set",
42620e6ea5dSraviteja-b             "xyz.openbmc_project.Network.EthernetInterface", "NTPServers",
42720e6ea5dSraviteja-b             std::variant<std::vector<std::string>>{ntpServers});
42820e6ea5dSraviteja-b     }
42920e6ea5dSraviteja-b 
430*67a78d87STom Joseph     void handleIpmiProtocolEnabled(const bool ipmiProtocolEnabled,
431*67a78d87STom Joseph                                    const std::shared_ptr<AsyncResp>& asyncResp)
432*67a78d87STom Joseph     {
433*67a78d87STom Joseph         crow::connections::systemBus->async_method_call(
434*67a78d87STom Joseph             [ipmiProtocolEnabled,
435*67a78d87STom Joseph              asyncResp](const boost::system::error_code ec,
436*67a78d87STom Joseph                         const crow::openbmc_mapper::GetSubTreeType& subtree) {
437*67a78d87STom Joseph                 if (ec)
438*67a78d87STom Joseph                 {
439*67a78d87STom Joseph                     messages::internalError(asyncResp->res);
440*67a78d87STom Joseph                     return;
441*67a78d87STom Joseph                 }
442*67a78d87STom Joseph 
443*67a78d87STom Joseph                 constexpr char const* netipmidBasePath =
444*67a78d87STom Joseph                     "/xyz/openbmc_project/control/service/"
445*67a78d87STom Joseph                     "phosphor_2dipmi_2dnet_40";
446*67a78d87STom Joseph 
447*67a78d87STom Joseph                 for (const auto& entry : subtree)
448*67a78d87STom Joseph                 {
449*67a78d87STom Joseph                     if (boost::algorithm::starts_with(entry.first,
450*67a78d87STom Joseph                                                       netipmidBasePath))
451*67a78d87STom Joseph                     {
452*67a78d87STom Joseph                         crow::connections::systemBus->async_method_call(
453*67a78d87STom Joseph                             [ipmiProtocolEnabled,
454*67a78d87STom Joseph                              asyncResp](const boost::system::error_code ec) {
455*67a78d87STom Joseph                                 if (ec)
456*67a78d87STom Joseph                                 {
457*67a78d87STom Joseph                                     messages::internalError(asyncResp->res);
458*67a78d87STom Joseph                                     return;
459*67a78d87STom Joseph                                 }
460*67a78d87STom Joseph                             },
461*67a78d87STom Joseph                             entry.second.begin()->first, entry.first,
462*67a78d87STom Joseph                             "org.freedesktop.DBus.Properties", "Set",
463*67a78d87STom Joseph                             "xyz.openbmc_project.Control.Service.Attributes",
464*67a78d87STom Joseph                             "Running", std::variant<bool>{ipmiProtocolEnabled});
465*67a78d87STom Joseph 
466*67a78d87STom Joseph                         crow::connections::systemBus->async_method_call(
467*67a78d87STom Joseph                             [ipmiProtocolEnabled,
468*67a78d87STom Joseph                              asyncResp](const boost::system::error_code ec) {
469*67a78d87STom Joseph                                 if (ec)
470*67a78d87STom Joseph                                 {
471*67a78d87STom Joseph                                     messages::internalError(asyncResp->res);
472*67a78d87STom Joseph                                     return;
473*67a78d87STom Joseph                                 }
474*67a78d87STom Joseph                             },
475*67a78d87STom Joseph                             entry.second.begin()->first, entry.first,
476*67a78d87STom Joseph                             "org.freedesktop.DBus.Properties", "Set",
477*67a78d87STom Joseph                             "xyz.openbmc_project.Control.Service.Attributes",
478*67a78d87STom Joseph                             "Enabled", std::variant<bool>{ipmiProtocolEnabled});
479*67a78d87STom Joseph                     }
480*67a78d87STom Joseph                 }
481*67a78d87STom Joseph             },
482*67a78d87STom Joseph             "xyz.openbmc_project.ObjectMapper",
483*67a78d87STom Joseph             "/xyz/openbmc_project/object_mapper",
484*67a78d87STom Joseph             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
485*67a78d87STom Joseph             "/xyz/openbmc_project/control/service", 0,
486*67a78d87STom Joseph             std::array<const char*, 1>{
487*67a78d87STom Joseph                 "xyz.openbmc_project.Control.Service.Attributes"});
488*67a78d87STom Joseph     }
489*67a78d87STom Joseph 
490501be32bSraviteja-b     void doPatch(crow::Response& res, const crow::Request& req,
491501be32bSraviteja-b                  const std::vector<std::string>& params) override
492501be32bSraviteja-b     {
493501be32bSraviteja-b         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
494501be32bSraviteja-b         std::optional<std::string> newHostName;
495cf05f9dcSJohnathan Mantey         std::optional<nlohmann::json> ntp;
496*67a78d87STom Joseph         std::optional<nlohmann::json> ipmi;
497501be32bSraviteja-b 
498*67a78d87STom Joseph         if (!json_util::readJson(req, res, "HostName", newHostName, "NTP", ntp,
499*67a78d87STom Joseph                                  "IPMI", ipmi))
500501be32bSraviteja-b         {
501501be32bSraviteja-b             return;
502501be32bSraviteja-b         }
503cf05f9dcSJohnathan Mantey 
504cf05f9dcSJohnathan Mantey         res.result(boost::beast::http::status::no_content);
505501be32bSraviteja-b         if (newHostName)
506501be32bSraviteja-b         {
507501be32bSraviteja-b             handleHostnamePatch(*newHostName, asyncResp);
508cf05f9dcSJohnathan Mantey         }
509cf05f9dcSJohnathan Mantey 
510cf05f9dcSJohnathan Mantey         if (ntp)
511cf05f9dcSJohnathan Mantey         {
512cf05f9dcSJohnathan Mantey             std::optional<std::vector<std::string>> ntpServers;
513cf05f9dcSJohnathan Mantey             std::optional<bool> ntpEnabled;
514cf05f9dcSJohnathan Mantey             if (!json_util::readJson(*ntp, res, "NTPServers", ntpServers,
515cf05f9dcSJohnathan Mantey                                      "ProtocolEnabled", ntpEnabled))
516cf05f9dcSJohnathan Mantey             {
517501be32bSraviteja-b                 return;
518501be32bSraviteja-b             }
519cf05f9dcSJohnathan Mantey 
52020e6ea5dSraviteja-b             if (ntpEnabled)
52120e6ea5dSraviteja-b             {
52220e6ea5dSraviteja-b                 handleNTPProtocolEnabled(*ntpEnabled, asyncResp);
52320e6ea5dSraviteja-b             }
524cf05f9dcSJohnathan Mantey 
52520e6ea5dSraviteja-b             if (ntpServers)
52620e6ea5dSraviteja-b             {
527dc3fbbd0STony Lee                 std::sort((*ntpServers).begin(), (*ntpServers).end());
528dc3fbbd0STony Lee                 (*ntpServers)
529dc3fbbd0STony Lee                     .erase(
530dc3fbbd0STony Lee                         std::unique((*ntpServers).begin(), (*ntpServers).end()),
531dc3fbbd0STony Lee                         (*ntpServers).end());
53220e6ea5dSraviteja-b                 handleNTPServersPatch(*ntpServers, asyncResp);
53320e6ea5dSraviteja-b             }
534501be32bSraviteja-b         }
535*67a78d87STom Joseph 
536*67a78d87STom Joseph         if (ipmi)
537*67a78d87STom Joseph         {
538*67a78d87STom Joseph             std::optional<bool> ipmiProtocolEnabled;
539*67a78d87STom Joseph             if (!json_util::readJson(*ipmi, res, "ProtocolEnabled",
540*67a78d87STom Joseph                                      ipmiProtocolEnabled))
541*67a78d87STom Joseph             {
542*67a78d87STom Joseph                 return;
543*67a78d87STom Joseph             }
544*67a78d87STom Joseph 
545*67a78d87STom Joseph             if (ipmiProtocolEnabled)
546*67a78d87STom Joseph             {
547*67a78d87STom Joseph                 handleIpmiProtocolEnabled(*ipmiProtocolEnabled, asyncResp);
548*67a78d87STom Joseph             }
549*67a78d87STom Joseph         }
550cf05f9dcSJohnathan Mantey     }
55170141561SBorawski.Lukasz };
55270141561SBorawski.Lukasz 
55370141561SBorawski.Lukasz } // namespace redfish
554