xref: /openbmc/bmcweb/features/redfish/lib/network_protocol.hpp (revision 52cc112d962920b035c870127784bcbd98948fad)
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"
2067a78d87STom 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:
133*52cc112dSEd Tanous     NetworkProtocol(App& 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         {
2330870f8c7SJayaprakash Mutyala             asyncResp->res.jsonValue[protocol.first]["Port"] =
2340870f8c7SJayaprakash Mutyala                 nlohmann::detail::value_t::null;
2350f74e643SEd Tanous             asyncResp->res.jsonValue[protocol.first]["ProtocolEnabled"] = false;
2360f74e643SEd Tanous         }
2370f74e643SEd Tanous 
238d24bfc7aSJennifer Lee         std::string hostName = getHostName();
239d24bfc7aSJennifer Lee 
240d24bfc7aSJennifer Lee         asyncResp->res.jsonValue["HostName"] = hostName;
2413a8a0088SKowalski, Kamil 
24220e6ea5dSraviteja-b         getNTPProtocolEnabled(asyncResp);
24320e6ea5dSraviteja-b 
24420e6ea5dSraviteja-b         // TODO Get eth0 interface data, and call the below callback for JSON
24520e6ea5dSraviteja-b         // preparation
246271584abSEd Tanous         getEthernetIfaceData(
247271584abSEd Tanous             [hostName, asyncResp](const bool& success,
248d24bfc7aSJennifer Lee                                   const std::vector<std::string>& ntpServers,
249d24bfc7aSJennifer Lee                                   const std::vector<std::string>& domainNames) {
25020e6ea5dSraviteja-b                 if (!success)
25120e6ea5dSraviteja-b                 {
252271584abSEd Tanous                     messages::resourceNotFound(asyncResp->res,
253271584abSEd Tanous                                                "EthernetInterface", "eth0");
25420e6ea5dSraviteja-b                     return;
25520e6ea5dSraviteja-b                 }
25620e6ea5dSraviteja-b                 asyncResp->res.jsonValue["NTP"]["NTPServers"] = ntpServers;
257d24bfc7aSJennifer Lee                 if (hostName.empty() == false)
258d24bfc7aSJennifer Lee                 {
259749dad7dSRatan Gupta                     std::string FQDN = std::move(hostName);
260d24bfc7aSJennifer Lee                     if (domainNames.empty() == false)
261d24bfc7aSJennifer Lee                     {
262749dad7dSRatan Gupta                         FQDN += "." + domainNames[0];
263d24bfc7aSJennifer Lee                     }
264749dad7dSRatan Gupta                     asyncResp->res.jsonValue["FQDN"] = std::move(FQDN);
265d24bfc7aSJennifer Lee                 }
26620e6ea5dSraviteja-b             });
26720e6ea5dSraviteja-b 
268865fbb75SEd Tanous         crow::connections::systemBus->async_method_call(
269271584abSEd Tanous             [asyncResp](const boost::system::error_code e,
270271584abSEd Tanous                         const std::vector<UnitStruct>& r) {
271271584abSEd Tanous                 if (e)
2721abe55efSEd Tanous                 {
2733a8a0088SKowalski, Kamil                     asyncResp->res.jsonValue = nlohmann::json::object();
274f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
275865fbb75SEd Tanous                     return;
2763a8a0088SKowalski, Kamil                 }
2775968caeeSMarri Devender Rao                 asyncResp->res.jsonValue["HTTPS"]["Certificates"] = {
2785968caeeSMarri Devender Rao                     {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol/"
279659dd62eSJason M. Bills                                   "HTTPS/Certificates"}};
2803a8a0088SKowalski, Kamil 
281271584abSEd Tanous                 for (auto& unit : r)
2821abe55efSEd Tanous                 {
283ec4974ddSAppaRao Puli                     /* Only traverse through <xyz>.socket units */
284ec4974ddSAppaRao Puli                     std::string unitName = std::get<NET_PROTO_UNIT_NAME>(unit);
285ec4974ddSAppaRao Puli                     if (!boost::ends_with(unitName, ".socket"))
2861abe55efSEd Tanous                     {
287865fbb75SEd Tanous                         continue;
28870141561SBorawski.Lukasz                     }
28970141561SBorawski.Lukasz 
290ec4974ddSAppaRao Puli                     for (auto& kv : protocolToDBus)
291ec4974ddSAppaRao Puli                     {
292ec4974ddSAppaRao Puli                         // We are interested in services, which starts with
293ec4974ddSAppaRao Puli                         // mapped service name
294ec4974ddSAppaRao Puli                         if (!boost::starts_with(unitName, kv.second))
295ec4974ddSAppaRao Puli                         {
296ec4974ddSAppaRao Puli                             continue;
297ec4974ddSAppaRao Puli                         }
298ec4974ddSAppaRao Puli                         const char* rfServiceKey = kv.first;
299ec4974ddSAppaRao Puli                         std::string socketPath =
300ec4974ddSAppaRao Puli                             std::get<NET_PROTO_UNIT_OBJ_PATH>(unit);
301ec4974ddSAppaRao Puli                         std::string unitState =
302ec4974ddSAppaRao Puli                             std::get<NET_PROTO_UNIT_SUB_STATE>(unit);
303ec4974ddSAppaRao Puli 
304ec4974ddSAppaRao Puli                         asyncResp->res
305ec4974ddSAppaRao Puli                             .jsonValue[rfServiceKey]["ProtocolEnabled"] =
306ec4974ddSAppaRao Puli                             (unitState == "running") ||
307ec4974ddSAppaRao Puli                             (unitState == "listening");
308ec4974ddSAppaRao Puli 
309865fbb75SEd Tanous                         crow::connections::systemBus->async_method_call(
310ec4974ddSAppaRao Puli                             [asyncResp,
311ec4974ddSAppaRao Puli                              rfServiceKey{std::string(rfServiceKey)}](
312865fbb75SEd Tanous                                 const boost::system::error_code ec,
313abf2add6SEd Tanous                                 const std::variant<std::vector<std::tuple<
314abf2add6SEd Tanous                                     std::string, std::string>>>& resp) {
3151abe55efSEd Tanous                                 if (ec)
3161abe55efSEd Tanous                                 {
317a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
318865fbb75SEd Tanous                                     return;
3193a8a0088SKowalski, Kamil                                 }
320abf2add6SEd Tanous                                 const std::vector<
321abf2add6SEd Tanous                                     std::tuple<std::string, std::string>>*
322abf2add6SEd Tanous                                     responsePtr = std::get_if<std::vector<
323abf2add6SEd Tanous                                         std::tuple<std::string, std::string>>>(
3241b6b96c5SEd Tanous                                         &resp);
3251abe55efSEd Tanous                                 if (responsePtr == nullptr ||
3261abe55efSEd Tanous                                     responsePtr->size() < 1)
3271abe55efSEd Tanous                                 {
328865fbb75SEd Tanous                                     return;
32970141561SBorawski.Lukasz                                 }
33070141561SBorawski.Lukasz 
331865fbb75SEd Tanous                                 const std::string& listenStream =
3321abe55efSEd Tanous                                     std::get<NET_PROTO_LISTEN_STREAM>(
3331abe55efSEd Tanous                                         (*responsePtr)[0]);
3341abe55efSEd Tanous                                 std::size_t lastColonPos =
3351abe55efSEd Tanous                                     listenStream.rfind(":");
3361abe55efSEd Tanous                                 if (lastColonPos == std::string::npos)
3371abe55efSEd Tanous                                 {
338865fbb75SEd Tanous                                     // Not a port
339865fbb75SEd Tanous                                     return;
340865fbb75SEd Tanous                                 }
3411abe55efSEd Tanous                                 std::string portStr =
3421abe55efSEd Tanous                                     listenStream.substr(lastColonPos + 1);
343ec4974ddSAppaRao Puli                                 if (portStr.empty())
344ec4974ddSAppaRao Puli                                 {
345ec4974ddSAppaRao Puli                                     return;
346ec4974ddSAppaRao Puli                                 }
347865fbb75SEd Tanous                                 char* endPtr = nullptr;
348ec4974ddSAppaRao Puli                                 errno = 0;
3491abe55efSEd Tanous                                 // Use strtol instead of stroi to avoid
3501abe55efSEd Tanous                                 // exceptions
3511abe55efSEd Tanous                                 long port =
3521abe55efSEd Tanous                                     std::strtol(portStr.c_str(), &endPtr, 10);
353ec4974ddSAppaRao Puli                                 if ((errno == 0) && (*endPtr == '\0'))
3541abe55efSEd Tanous                                 {
355ec4974ddSAppaRao Puli                                     asyncResp->res
356ec4974ddSAppaRao Puli                                         .jsonValue[rfServiceKey]["Port"] = port;
3571abe55efSEd Tanous                                 }
358ec4974ddSAppaRao Puli                                 return;
359865fbb75SEd Tanous                             },
360865fbb75SEd Tanous                             "org.freedesktop.systemd1", socketPath,
361865fbb75SEd Tanous                             "org.freedesktop.DBus.Properties", "Get",
362865fbb75SEd Tanous                             "org.freedesktop.systemd1.Socket", "Listen");
363ec4974ddSAppaRao Puli 
364ec4974ddSAppaRao Puli                         // We found service, break the inner loop.
365ec4974ddSAppaRao Puli                         break;
366865fbb75SEd Tanous                     }
367865fbb75SEd Tanous                 }
368865fbb75SEd Tanous             },
369865fbb75SEd Tanous             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
370865fbb75SEd Tanous             "org.freedesktop.systemd1.Manager", "ListUnits");
371865fbb75SEd Tanous     }
372501be32bSraviteja-b 
373501be32bSraviteja-b     void handleHostnamePatch(const std::string& hostName,
374501be32bSraviteja-b                              const std::shared_ptr<AsyncResp>& asyncResp)
375501be32bSraviteja-b     {
376501be32bSraviteja-b         crow::connections::systemBus->async_method_call(
377501be32bSraviteja-b             [asyncResp](const boost::system::error_code ec) {
378501be32bSraviteja-b                 if (ec)
379501be32bSraviteja-b                 {
380501be32bSraviteja-b                     messages::internalError(asyncResp->res);
381501be32bSraviteja-b                     return;
382501be32bSraviteja-b                 }
383501be32bSraviteja-b             },
384501be32bSraviteja-b             "xyz.openbmc_project.Network",
385501be32bSraviteja-b             "/xyz/openbmc_project/network/config",
386501be32bSraviteja-b             "org.freedesktop.DBus.Properties", "Set",
387501be32bSraviteja-b             "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
388501be32bSraviteja-b             std::variant<std::string>(hostName));
389501be32bSraviteja-b     }
390501be32bSraviteja-b 
39120e6ea5dSraviteja-b     void handleNTPProtocolEnabled(const bool& ntpEnabled,
39220e6ea5dSraviteja-b                                   const std::shared_ptr<AsyncResp>& asyncResp)
39320e6ea5dSraviteja-b     {
39420e6ea5dSraviteja-b         std::string timeSyncMethod;
39520e6ea5dSraviteja-b         if (ntpEnabled)
39620e6ea5dSraviteja-b         {
39720e6ea5dSraviteja-b             timeSyncMethod =
39820e6ea5dSraviteja-b                 "xyz.openbmc_project.Time.Synchronization.Method.NTP";
39920e6ea5dSraviteja-b         }
40020e6ea5dSraviteja-b         else
40120e6ea5dSraviteja-b         {
40220e6ea5dSraviteja-b             timeSyncMethod =
40320e6ea5dSraviteja-b                 "xyz.openbmc_project.Time.Synchronization.Method.Manual";
40420e6ea5dSraviteja-b         }
40520e6ea5dSraviteja-b 
40620e6ea5dSraviteja-b         crow::connections::systemBus->async_method_call(
407cf05f9dcSJohnathan Mantey             [asyncResp](const boost::system::error_code error_code) {},
40820e6ea5dSraviteja-b             "xyz.openbmc_project.Settings",
40920e6ea5dSraviteja-b             "/xyz/openbmc_project/time/sync_method",
41020e6ea5dSraviteja-b             "org.freedesktop.DBus.Properties", "Set",
41120e6ea5dSraviteja-b             "xyz.openbmc_project.Time.Synchronization", "TimeSyncMethod",
41220e6ea5dSraviteja-b             std::variant<std::string>{timeSyncMethod});
41320e6ea5dSraviteja-b     }
41420e6ea5dSraviteja-b 
41520e6ea5dSraviteja-b     void handleNTPServersPatch(const std::vector<std::string>& ntpServers,
41620e6ea5dSraviteja-b                                const std::shared_ptr<AsyncResp>& asyncResp)
41720e6ea5dSraviteja-b     {
41820e6ea5dSraviteja-b         crow::connections::systemBus->async_method_call(
419cf05f9dcSJohnathan Mantey             [asyncResp](const boost::system::error_code ec) {
42020e6ea5dSraviteja-b                 if (ec)
42120e6ea5dSraviteja-b                 {
42220e6ea5dSraviteja-b                     messages::internalError(asyncResp->res);
42320e6ea5dSraviteja-b                     return;
42420e6ea5dSraviteja-b                 }
42520e6ea5dSraviteja-b             },
42620e6ea5dSraviteja-b             "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/eth0",
42720e6ea5dSraviteja-b             "org.freedesktop.DBus.Properties", "Set",
42820e6ea5dSraviteja-b             "xyz.openbmc_project.Network.EthernetInterface", "NTPServers",
42920e6ea5dSraviteja-b             std::variant<std::vector<std::string>>{ntpServers});
43020e6ea5dSraviteja-b     }
43120e6ea5dSraviteja-b 
43267a78d87STom Joseph     void handleIpmiProtocolEnabled(const bool ipmiProtocolEnabled,
43367a78d87STom Joseph                                    const std::shared_ptr<AsyncResp>& asyncResp)
43467a78d87STom Joseph     {
43567a78d87STom Joseph         crow::connections::systemBus->async_method_call(
43667a78d87STom Joseph             [ipmiProtocolEnabled,
43767a78d87STom Joseph              asyncResp](const boost::system::error_code ec,
43867a78d87STom Joseph                         const crow::openbmc_mapper::GetSubTreeType& subtree) {
43967a78d87STom Joseph                 if (ec)
44067a78d87STom Joseph                 {
44167a78d87STom Joseph                     messages::internalError(asyncResp->res);
44267a78d87STom Joseph                     return;
44367a78d87STom Joseph                 }
44467a78d87STom Joseph 
44567a78d87STom Joseph                 constexpr char const* netipmidBasePath =
44667a78d87STom Joseph                     "/xyz/openbmc_project/control/service/"
44767a78d87STom Joseph                     "phosphor_2dipmi_2dnet_40";
44867a78d87STom Joseph 
44967a78d87STom Joseph                 for (const auto& entry : subtree)
45067a78d87STom Joseph                 {
45167a78d87STom Joseph                     if (boost::algorithm::starts_with(entry.first,
45267a78d87STom Joseph                                                       netipmidBasePath))
45367a78d87STom Joseph                     {
45467a78d87STom Joseph                         crow::connections::systemBus->async_method_call(
45567a78d87STom Joseph                             [ipmiProtocolEnabled,
45667a78d87STom Joseph                              asyncResp](const boost::system::error_code ec) {
45767a78d87STom Joseph                                 if (ec)
45867a78d87STom Joseph                                 {
45967a78d87STom Joseph                                     messages::internalError(asyncResp->res);
46067a78d87STom Joseph                                     return;
46167a78d87STom Joseph                                 }
46267a78d87STom Joseph                             },
46367a78d87STom Joseph                             entry.second.begin()->first, entry.first,
46467a78d87STom Joseph                             "org.freedesktop.DBus.Properties", "Set",
46567a78d87STom Joseph                             "xyz.openbmc_project.Control.Service.Attributes",
46667a78d87STom Joseph                             "Running", std::variant<bool>{ipmiProtocolEnabled});
46767a78d87STom Joseph 
46867a78d87STom Joseph                         crow::connections::systemBus->async_method_call(
46967a78d87STom Joseph                             [ipmiProtocolEnabled,
47067a78d87STom Joseph                              asyncResp](const boost::system::error_code ec) {
47167a78d87STom Joseph                                 if (ec)
47267a78d87STom Joseph                                 {
47367a78d87STom Joseph                                     messages::internalError(asyncResp->res);
47467a78d87STom Joseph                                     return;
47567a78d87STom Joseph                                 }
47667a78d87STom Joseph                             },
47767a78d87STom Joseph                             entry.second.begin()->first, entry.first,
47867a78d87STom Joseph                             "org.freedesktop.DBus.Properties", "Set",
47967a78d87STom Joseph                             "xyz.openbmc_project.Control.Service.Attributes",
48067a78d87STom Joseph                             "Enabled", std::variant<bool>{ipmiProtocolEnabled});
48167a78d87STom Joseph                     }
48267a78d87STom Joseph                 }
48367a78d87STom Joseph             },
48467a78d87STom Joseph             "xyz.openbmc_project.ObjectMapper",
48567a78d87STom Joseph             "/xyz/openbmc_project/object_mapper",
48667a78d87STom Joseph             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
48767a78d87STom Joseph             "/xyz/openbmc_project/control/service", 0,
48867a78d87STom Joseph             std::array<const char*, 1>{
48967a78d87STom Joseph                 "xyz.openbmc_project.Control.Service.Attributes"});
49067a78d87STom Joseph     }
49167a78d87STom Joseph 
492501be32bSraviteja-b     void doPatch(crow::Response& res, const crow::Request& req,
493501be32bSraviteja-b                  const std::vector<std::string>& params) override
494501be32bSraviteja-b     {
495501be32bSraviteja-b         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
496501be32bSraviteja-b         std::optional<std::string> newHostName;
497cf05f9dcSJohnathan Mantey         std::optional<nlohmann::json> ntp;
49867a78d87STom Joseph         std::optional<nlohmann::json> ipmi;
499501be32bSraviteja-b 
50067a78d87STom Joseph         if (!json_util::readJson(req, res, "HostName", newHostName, "NTP", ntp,
50167a78d87STom Joseph                                  "IPMI", ipmi))
502501be32bSraviteja-b         {
503501be32bSraviteja-b             return;
504501be32bSraviteja-b         }
505cf05f9dcSJohnathan Mantey 
506cf05f9dcSJohnathan Mantey         res.result(boost::beast::http::status::no_content);
507501be32bSraviteja-b         if (newHostName)
508501be32bSraviteja-b         {
509501be32bSraviteja-b             handleHostnamePatch(*newHostName, asyncResp);
510cf05f9dcSJohnathan Mantey         }
511cf05f9dcSJohnathan Mantey 
512cf05f9dcSJohnathan Mantey         if (ntp)
513cf05f9dcSJohnathan Mantey         {
514cf05f9dcSJohnathan Mantey             std::optional<std::vector<std::string>> ntpServers;
515cf05f9dcSJohnathan Mantey             std::optional<bool> ntpEnabled;
516cf05f9dcSJohnathan Mantey             if (!json_util::readJson(*ntp, res, "NTPServers", ntpServers,
517cf05f9dcSJohnathan Mantey                                      "ProtocolEnabled", ntpEnabled))
518cf05f9dcSJohnathan Mantey             {
519501be32bSraviteja-b                 return;
520501be32bSraviteja-b             }
521cf05f9dcSJohnathan Mantey 
52220e6ea5dSraviteja-b             if (ntpEnabled)
52320e6ea5dSraviteja-b             {
52420e6ea5dSraviteja-b                 handleNTPProtocolEnabled(*ntpEnabled, asyncResp);
52520e6ea5dSraviteja-b             }
526cf05f9dcSJohnathan Mantey 
52720e6ea5dSraviteja-b             if (ntpServers)
52820e6ea5dSraviteja-b             {
529dc3fbbd0STony Lee                 std::sort((*ntpServers).begin(), (*ntpServers).end());
530dc3fbbd0STony Lee                 (*ntpServers)
531dc3fbbd0STony Lee                     .erase(
532dc3fbbd0STony Lee                         std::unique((*ntpServers).begin(), (*ntpServers).end()),
533dc3fbbd0STony Lee                         (*ntpServers).end());
53420e6ea5dSraviteja-b                 handleNTPServersPatch(*ntpServers, asyncResp);
53520e6ea5dSraviteja-b             }
536501be32bSraviteja-b         }
53767a78d87STom Joseph 
53867a78d87STom Joseph         if (ipmi)
53967a78d87STom Joseph         {
54067a78d87STom Joseph             std::optional<bool> ipmiProtocolEnabled;
54167a78d87STom Joseph             if (!json_util::readJson(*ipmi, res, "ProtocolEnabled",
54267a78d87STom Joseph                                      ipmiProtocolEnabled))
54367a78d87STom Joseph             {
54467a78d87STom Joseph                 return;
54567a78d87STom Joseph             }
54667a78d87STom Joseph 
54767a78d87STom Joseph             if (ipmiProtocolEnabled)
54867a78d87STom Joseph             {
54967a78d87STom Joseph                 handleIpmiProtocolEnabled(*ipmiProtocolEnabled, asyncResp);
55067a78d87STom Joseph             }
55167a78d87STom Joseph         }
552cf05f9dcSJohnathan Mantey     }
55370141561SBorawski.Lukasz };
55470141561SBorawski.Lukasz 
55570141561SBorawski.Lukasz } // namespace redfish
556