xref: /openbmc/bmcweb/features/redfish/lib/network_protocol.hpp (revision 44fad2aa3ba9e28bb52c0ff21ba2cdbd2812b199)
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"
1967a78d87STom Joseph #include "openbmc_dbus_rest.hpp"
20b4bec66bSAbhishek Patel #include "redfish_util.hpp"
2170141561SBorawski.Lukasz 
227e860f15SJohn Edward Broadbent #include <app.hpp>
23ed398213SEd Tanous #include <registries/privilege_registry.hpp>
2420e6ea5dSraviteja-b #include <utils/json_utils.hpp>
251214b7e7SGunnar Mills 
261214b7e7SGunnar Mills #include <optional>
27abf2add6SEd Tanous #include <variant>
281abe55efSEd Tanous namespace redfish
291abe55efSEd Tanous {
3070141561SBorawski.Lukasz 
317e860f15SJohn Edward Broadbent void getNTPProtocolEnabled(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp);
327e860f15SJohn Edward Broadbent std::string getHostName();
337e860f15SJohn Edward Broadbent 
34b4bec66bSAbhishek Patel const static std::array<std::pair<std::string, std::string>, 3> protocolToDBus{
35b0972a63SEd Tanous     {{"SSH", "dropbear"}, {"HTTPS", "bmcweb"}, {"IPMI", "phosphor-ipmi-net"}}};
363a8a0088SKowalski, Kamil 
37d24bfc7aSJennifer Lee inline void
3881ce609eSEd Tanous     extractNTPServersAndDomainNamesData(const GetManagedObjects& dbusData,
39d24bfc7aSJennifer Lee                                         std::vector<std::string>& ntpData,
40d24bfc7aSJennifer Lee                                         std::vector<std::string>& dnData)
4120e6ea5dSraviteja-b {
4281ce609eSEd Tanous     for (const auto& obj : dbusData)
4320e6ea5dSraviteja-b     {
4420e6ea5dSraviteja-b         for (const auto& ifacePair : obj.second)
4520e6ea5dSraviteja-b         {
4620e6ea5dSraviteja-b             if (obj.first == "/xyz/openbmc_project/network/eth0")
4720e6ea5dSraviteja-b             {
4820e6ea5dSraviteja-b                 if (ifacePair.first ==
4920e6ea5dSraviteja-b                     "xyz.openbmc_project.Network.EthernetInterface")
5020e6ea5dSraviteja-b                 {
5120e6ea5dSraviteja-b                     for (const auto& propertyPair : ifacePair.second)
5220e6ea5dSraviteja-b                     {
5320e6ea5dSraviteja-b                         if (propertyPair.first == "NTPServers")
5420e6ea5dSraviteja-b                         {
5520e6ea5dSraviteja-b                             const std::vector<std::string>* ntpServers =
568d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
5720e6ea5dSraviteja-b                                     &propertyPair.second);
5820e6ea5dSraviteja-b                             if (ntpServers != nullptr)
5920e6ea5dSraviteja-b                             {
60f23b7296SEd Tanous                                 ntpData = *ntpServers;
6120e6ea5dSraviteja-b                             }
6220e6ea5dSraviteja-b                         }
63d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
64d24bfc7aSJennifer Lee                         {
65d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
668d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
67d24bfc7aSJennifer Lee                                     &propertyPair.second);
68d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
69d24bfc7aSJennifer Lee                             {
70f23b7296SEd Tanous                                 dnData = *domainNames;
71d24bfc7aSJennifer Lee                             }
72d24bfc7aSJennifer Lee                         }
7320e6ea5dSraviteja-b                     }
7420e6ea5dSraviteja-b                 }
7520e6ea5dSraviteja-b             }
7620e6ea5dSraviteja-b         }
7720e6ea5dSraviteja-b     }
7820e6ea5dSraviteja-b }
7920e6ea5dSraviteja-b 
8020e6ea5dSraviteja-b template <typename CallbackFunc>
8120e6ea5dSraviteja-b void getEthernetIfaceData(CallbackFunc&& callback)
8220e6ea5dSraviteja-b {
8320e6ea5dSraviteja-b     crow::connections::systemBus->async_method_call(
8420e6ea5dSraviteja-b         [callback{std::move(callback)}](
8581ce609eSEd Tanous             const boost::system::error_code errorCode,
8681ce609eSEd Tanous             const GetManagedObjects& dbusData) {
8720e6ea5dSraviteja-b             std::vector<std::string> ntpServers;
88d24bfc7aSJennifer Lee             std::vector<std::string> domainNames;
8920e6ea5dSraviteja-b 
9081ce609eSEd Tanous             if (errorCode)
9120e6ea5dSraviteja-b             {
92d24bfc7aSJennifer Lee                 callback(false, ntpServers, domainNames);
9320e6ea5dSraviteja-b                 return;
9420e6ea5dSraviteja-b             }
9520e6ea5dSraviteja-b 
9681ce609eSEd Tanous             extractNTPServersAndDomainNamesData(dbusData, ntpServers,
97d24bfc7aSJennifer Lee                                                 domainNames);
9820e6ea5dSraviteja-b 
99d24bfc7aSJennifer Lee             callback(true, ntpServers, domainNames);
10020e6ea5dSraviteja-b         },
10120e6ea5dSraviteja-b         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
10220e6ea5dSraviteja-b         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
103271584abSEd Tanous }
10420e6ea5dSraviteja-b 
1054f48d5f6SEd Tanous inline void getNetworkData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10672048780SAbhishek Patel                            const crow::Request& req)
1071abe55efSEd Tanous {
1080f74e643SEd Tanous     asyncResp->res.jsonValue["@odata.type"] =
10961932318SXiaochao Ma         "#ManagerNetworkProtocol.v1_5_0.ManagerNetworkProtocol";
1100f74e643SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
1110f74e643SEd Tanous         "/redfish/v1/Managers/bmc/NetworkProtocol";
1120f74e643SEd Tanous     asyncResp->res.jsonValue["Id"] = "NetworkProtocol";
1130f74e643SEd Tanous     asyncResp->res.jsonValue["Name"] = "Manager Network Protocol";
1140f74e643SEd Tanous     asyncResp->res.jsonValue["Description"] = "Manager Network Service";
1150f74e643SEd Tanous     asyncResp->res.jsonValue["Status"]["Health"] = "OK";
1160f74e643SEd Tanous     asyncResp->res.jsonValue["Status"]["HealthRollup"] = "OK";
1170f74e643SEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
1180f74e643SEd Tanous 
11961932318SXiaochao Ma     // HTTP is Mandatory attribute as per OCP Baseline Profile - v1.0.0,
120818ea7b8SJoshi-Mansi     // but from security perspective it is not recommended to use.
121818ea7b8SJoshi-Mansi     // Hence using protocolEnabled as false to make it OCP and security-wise
122818ea7b8SJoshi-Mansi     // compliant
123818ea7b8SJoshi-Mansi     asyncResp->res.jsonValue["HTTP"]["Port"] = 0;
124818ea7b8SJoshi-Mansi     asyncResp->res.jsonValue["HTTP"]["ProtocolEnabled"] = false;
125818ea7b8SJoshi-Mansi 
126d24bfc7aSJennifer Lee     std::string hostName = getHostName();
127d24bfc7aSJennifer Lee 
128d24bfc7aSJennifer Lee     asyncResp->res.jsonValue["HostName"] = hostName;
1293a8a0088SKowalski, Kamil 
13020e6ea5dSraviteja-b     getNTPProtocolEnabled(asyncResp);
13120e6ea5dSraviteja-b 
13220e6ea5dSraviteja-b     // TODO Get eth0 interface data, and call the below callback for JSON
13320e6ea5dSraviteja-b     // preparation
134271584abSEd Tanous     getEthernetIfaceData(
135271584abSEd Tanous         [hostName, asyncResp](const bool& success,
136d24bfc7aSJennifer Lee                               const std::vector<std::string>& ntpServers,
137d24bfc7aSJennifer Lee                               const std::vector<std::string>& domainNames) {
13820e6ea5dSraviteja-b             if (!success)
13920e6ea5dSraviteja-b             {
1407e860f15SJohn Edward Broadbent                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1417e860f15SJohn Edward Broadbent                                            "eth0");
14220e6ea5dSraviteja-b                 return;
14320e6ea5dSraviteja-b             }
14420e6ea5dSraviteja-b             asyncResp->res.jsonValue["NTP"]["NTPServers"] = ntpServers;
145d24bfc7aSJennifer Lee             if (hostName.empty() == false)
146d24bfc7aSJennifer Lee             {
147f23b7296SEd Tanous                 std::string fqdn = hostName;
148d24bfc7aSJennifer Lee                 if (domainNames.empty() == false)
149d24bfc7aSJennifer Lee                 {
150f23b7296SEd Tanous                     fqdn += ".";
151f23b7296SEd Tanous                     fqdn += domainNames[0];
152d24bfc7aSJennifer Lee                 }
1532c70f800SEd Tanous                 asyncResp->res.jsonValue["FQDN"] = std::move(fqdn);
154d24bfc7aSJennifer Lee             }
15520e6ea5dSraviteja-b         });
15620e6ea5dSraviteja-b 
15772048780SAbhishek Patel     Privileges effectiveUserPrivileges =
15872048780SAbhishek Patel         redfish::getUserPrivileges(req.userRole);
15972048780SAbhishek Patel 
16072048780SAbhishek Patel     // /redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates is
16172048780SAbhishek Patel     // something only ConfigureManager can access then only display when
16272048780SAbhishek Patel     // the user has permissions ConfigureManager
16372048780SAbhishek Patel     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
16472048780SAbhishek Patel                                          effectiveUserPrivileges))
16572048780SAbhishek Patel     {
1665968caeeSMarri Devender Rao         asyncResp->res.jsonValue["HTTPS"]["Certificates"] = {
167b4bec66bSAbhishek Patel             {"@odata.id",
168b4bec66bSAbhishek Patel              "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates"}};
16970141561SBorawski.Lukasz     }
17070141561SBorawski.Lukasz 
171b4bec66bSAbhishek Patel     for (const auto& protocol : protocolToDBus)
172ec4974ddSAppaRao Puli     {
173b4bec66bSAbhishek Patel         const std::string& protocolName = protocol.first;
174b4bec66bSAbhishek Patel         const std::string& serviceName = protocol.second;
175b4bec66bSAbhishek Patel         getPortStatusAndPath(
176b4bec66bSAbhishek Patel             serviceName,
177b4bec66bSAbhishek Patel             [asyncResp, protocolName](const boost::system::error_code ec,
178b4bec66bSAbhishek Patel                                       const std::string& socketPath,
179b4bec66bSAbhishek Patel                                       bool isProtocolEnabled) {
1804d875bd8SEd Tanous                 // If the service is not installed, that is not an error
1814d875bd8SEd Tanous                 if (ec == boost::system::errc::no_such_process)
1824d875bd8SEd Tanous                 {
1834d875bd8SEd Tanous                     asyncResp->res.jsonValue[protocolName]["Port"] =
1844d875bd8SEd Tanous                         nlohmann::detail::value_t::null;
1854d875bd8SEd Tanous                     asyncResp->res.jsonValue[protocolName]["ProtocolEnabled"] =
1864d875bd8SEd Tanous                         false;
1874d875bd8SEd Tanous                     return;
1884d875bd8SEd Tanous                 }
1891abe55efSEd Tanous                 if (ec)
1901abe55efSEd Tanous                 {
191a08b46ccSJason M. Bills                     messages::internalError(asyncResp->res);
192865fbb75SEd Tanous                     return;
1933a8a0088SKowalski, Kamil                 }
194b4bec66bSAbhishek Patel                 asyncResp->res.jsonValue[protocolName]["ProtocolEnabled"] =
195b4bec66bSAbhishek Patel                     isProtocolEnabled;
196b4bec66bSAbhishek Patel                 getPortNumber(
197b4bec66bSAbhishek Patel                     socketPath,
198b4bec66bSAbhishek Patel                     [asyncResp, protocolName](
199b4bec66bSAbhishek Patel                         const boost::system::error_code ec, int portNumber) {
200b4bec66bSAbhishek Patel                         if (ec)
2011abe55efSEd Tanous                         {
202b4bec66bSAbhishek Patel                             messages::internalError(asyncResp->res);
203865fbb75SEd Tanous                             return;
20470141561SBorawski.Lukasz                         }
205b4bec66bSAbhishek Patel                         asyncResp->res.jsonValue[protocolName]["Port"] =
206b4bec66bSAbhishek Patel                             portNumber;
207b4bec66bSAbhishek Patel                     });
208b4bec66bSAbhishek Patel             });
209865fbb75SEd Tanous     }
210b4bec66bSAbhishek Patel } // namespace redfish
211501be32bSraviteja-b 
2124f48d5f6SEd Tanous inline void handleNTPProtocolEnabled(
2137e860f15SJohn Edward Broadbent     const bool& ntpEnabled, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
21420e6ea5dSraviteja-b {
21520e6ea5dSraviteja-b     std::string timeSyncMethod;
21620e6ea5dSraviteja-b     if (ntpEnabled)
21720e6ea5dSraviteja-b     {
2187e860f15SJohn Edward Broadbent         timeSyncMethod = "xyz.openbmc_project.Time.Synchronization.Method.NTP";
21920e6ea5dSraviteja-b     }
22020e6ea5dSraviteja-b     else
22120e6ea5dSraviteja-b     {
22220e6ea5dSraviteja-b         timeSyncMethod =
22320e6ea5dSraviteja-b             "xyz.openbmc_project.Time.Synchronization.Method.Manual";
22420e6ea5dSraviteja-b     }
22520e6ea5dSraviteja-b 
22620e6ea5dSraviteja-b     crow::connections::systemBus->async_method_call(
22781ce609eSEd Tanous         [asyncResp](const boost::system::error_code errorCode) {
22881ce609eSEd Tanous             if (errorCode)
229cb13a392SEd Tanous             {
230cb13a392SEd Tanous                 messages::internalError(asyncResp->res);
231cb13a392SEd Tanous             }
232cb13a392SEd Tanous         },
2337e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Settings", "/xyz/openbmc_project/time/sync_method",
23420e6ea5dSraviteja-b         "org.freedesktop.DBus.Properties", "Set",
23520e6ea5dSraviteja-b         "xyz.openbmc_project.Time.Synchronization", "TimeSyncMethod",
23620e6ea5dSraviteja-b         std::variant<std::string>{timeSyncMethod});
23720e6ea5dSraviteja-b }
23820e6ea5dSraviteja-b 
2394f48d5f6SEd Tanous inline void
2404f48d5f6SEd Tanous     handleNTPServersPatch(const std::vector<std::string>& ntpServers,
2418d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
24220e6ea5dSraviteja-b {
24320e6ea5dSraviteja-b     crow::connections::systemBus->async_method_call(
244cf05f9dcSJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
24520e6ea5dSraviteja-b             if (ec)
24620e6ea5dSraviteja-b             {
24720e6ea5dSraviteja-b                 messages::internalError(asyncResp->res);
24820e6ea5dSraviteja-b                 return;
24920e6ea5dSraviteja-b             }
25020e6ea5dSraviteja-b         },
25120e6ea5dSraviteja-b         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/eth0",
25220e6ea5dSraviteja-b         "org.freedesktop.DBus.Properties", "Set",
25320e6ea5dSraviteja-b         "xyz.openbmc_project.Network.EthernetInterface", "NTPServers",
25420e6ea5dSraviteja-b         std::variant<std::vector<std::string>>{ntpServers});
25520e6ea5dSraviteja-b }
25620e6ea5dSraviteja-b 
2574f48d5f6SEd Tanous inline void
2584f48d5f6SEd Tanous     handleProtocolEnabled(const bool protocolEnabled,
259e5a99777SAlbert Zhang                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
260e5a99777SAlbert Zhang                           const std::string_view netBasePath)
26167a78d87STom Joseph {
26267a78d87STom Joseph     crow::connections::systemBus->async_method_call(
263e5a99777SAlbert Zhang         [protocolEnabled, asyncResp,
264e5a99777SAlbert Zhang          netBasePath](const boost::system::error_code ec,
26567a78d87STom Joseph                       const crow::openbmc_mapper::GetSubTreeType& subtree) {
26667a78d87STom Joseph             if (ec)
26767a78d87STom Joseph             {
26867a78d87STom Joseph                 messages::internalError(asyncResp->res);
26967a78d87STom Joseph                 return;
27067a78d87STom Joseph             }
27167a78d87STom Joseph 
27267a78d87STom Joseph             for (const auto& entry : subtree)
27367a78d87STom Joseph             {
274e5a99777SAlbert Zhang                 if (boost::algorithm::starts_with(entry.first, netBasePath))
27567a78d87STom Joseph                 {
27667a78d87STom Joseph                     crow::connections::systemBus->async_method_call(
27723a21a1cSEd Tanous                         [asyncResp](const boost::system::error_code ec2) {
27823a21a1cSEd Tanous                             if (ec2)
27967a78d87STom Joseph                             {
28067a78d87STom Joseph                                 messages::internalError(asyncResp->res);
28167a78d87STom Joseph                                 return;
28267a78d87STom Joseph                             }
28367a78d87STom Joseph                         },
28467a78d87STom Joseph                         entry.second.begin()->first, entry.first,
28567a78d87STom Joseph                         "org.freedesktop.DBus.Properties", "Set",
28667a78d87STom Joseph                         "xyz.openbmc_project.Control.Service.Attributes",
287e5a99777SAlbert Zhang                         "Running", std::variant<bool>{protocolEnabled});
28867a78d87STom Joseph 
28967a78d87STom Joseph                     crow::connections::systemBus->async_method_call(
29023a21a1cSEd Tanous                         [asyncResp](const boost::system::error_code ec2) {
29123a21a1cSEd Tanous                             if (ec2)
29267a78d87STom Joseph                             {
29367a78d87STom Joseph                                 messages::internalError(asyncResp->res);
29467a78d87STom Joseph                                 return;
29567a78d87STom Joseph                             }
29667a78d87STom Joseph                         },
29767a78d87STom Joseph                         entry.second.begin()->first, entry.first,
29867a78d87STom Joseph                         "org.freedesktop.DBus.Properties", "Set",
29967a78d87STom Joseph                         "xyz.openbmc_project.Control.Service.Attributes",
300e5a99777SAlbert Zhang                         "Enabled", std::variant<bool>{protocolEnabled});
30167a78d87STom Joseph                 }
30267a78d87STom Joseph             }
30367a78d87STom Joseph         },
30467a78d87STom Joseph         "xyz.openbmc_project.ObjectMapper",
30567a78d87STom Joseph         "/xyz/openbmc_project/object_mapper",
30667a78d87STom Joseph         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
30767a78d87STom Joseph         "/xyz/openbmc_project/control/service", 0,
30867a78d87STom Joseph         std::array<const char*, 1>{
30967a78d87STom Joseph             "xyz.openbmc_project.Control.Service.Attributes"});
31067a78d87STom Joseph }
31167a78d87STom Joseph 
3124f48d5f6SEd Tanous inline std::string getHostName()
313501be32bSraviteja-b {
3147e860f15SJohn Edward Broadbent     std::string hostName;
3158d1b46d7Szhanghch05 
3167e860f15SJohn Edward Broadbent     std::array<char, HOST_NAME_MAX> hostNameCStr;
3177e860f15SJohn Edward Broadbent     if (gethostname(hostNameCStr.data(), hostNameCStr.size()) == 0)
3187e860f15SJohn Edward Broadbent     {
3197e860f15SJohn Edward Broadbent         hostName = hostNameCStr.data();
3207e860f15SJohn Edward Broadbent     }
3217e860f15SJohn Edward Broadbent     return hostName;
3227e860f15SJohn Edward Broadbent }
3237e860f15SJohn Edward Broadbent 
3244f48d5f6SEd Tanous inline void
3254f48d5f6SEd Tanous     getNTPProtocolEnabled(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3267e860f15SJohn Edward Broadbent {
3277e860f15SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
3287e860f15SJohn Edward Broadbent         [asyncResp](const boost::system::error_code errorCode,
3297e860f15SJohn Edward Broadbent                     const std::variant<std::string>& timeSyncMethod) {
3307e860f15SJohn Edward Broadbent             if (errorCode)
3317e860f15SJohn Edward Broadbent             {
3327e860f15SJohn Edward Broadbent                 return;
3337e860f15SJohn Edward Broadbent             }
3347e860f15SJohn Edward Broadbent 
3357e860f15SJohn Edward Broadbent             const std::string* s = std::get_if<std::string>(&timeSyncMethod);
3367e860f15SJohn Edward Broadbent 
3377e860f15SJohn Edward Broadbent             if (*s == "xyz.openbmc_project.Time.Synchronization.Method.NTP")
3387e860f15SJohn Edward Broadbent             {
3397e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["NTP"]["ProtocolEnabled"] = true;
3407e860f15SJohn Edward Broadbent             }
3417e860f15SJohn Edward Broadbent             else if (*s == "xyz.openbmc_project.Time.Synchronization."
3427e860f15SJohn Edward Broadbent                            "Method.Manual")
3437e860f15SJohn Edward Broadbent             {
3447e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["NTP"]["ProtocolEnabled"] = false;
3457e860f15SJohn Edward Broadbent             }
3467e860f15SJohn Edward Broadbent         },
3477e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Settings", "/xyz/openbmc_project/time/sync_method",
3487e860f15SJohn Edward Broadbent         "org.freedesktop.DBus.Properties", "Get",
3497e860f15SJohn Edward Broadbent         "xyz.openbmc_project.Time.Synchronization", "TimeSyncMethod");
3507e860f15SJohn Edward Broadbent }
3517e860f15SJohn Edward Broadbent 
3527e860f15SJohn Edward Broadbent inline void requestRoutesNetworkProtocol(App& app)
3537e860f15SJohn Edward Broadbent {
3547e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/NetworkProtocol/")
355ed398213SEd Tanous         .privileges(redfish::privileges::patchManagerNetworkProtocol)
3567e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
3577e860f15SJohn Edward Broadbent             [](const crow::Request& req,
3587e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
359501be32bSraviteja-b                 std::optional<std::string> newHostName;
360cf05f9dcSJohnathan Mantey                 std::optional<nlohmann::json> ntp;
36167a78d87STom Joseph                 std::optional<nlohmann::json> ipmi;
362e5a99777SAlbert Zhang                 std::optional<nlohmann::json> ssh;
363501be32bSraviteja-b 
3647e860f15SJohn Edward Broadbent                 if (!json_util::readJson(req, asyncResp->res, "NTP", ntp,
365e5a99777SAlbert Zhang                                          "HostName", newHostName, "IPMI", ipmi,
366e5a99777SAlbert Zhang                                          "SSH", ssh))
367501be32bSraviteja-b                 {
368501be32bSraviteja-b                     return;
369501be32bSraviteja-b                 }
370cf05f9dcSJohnathan Mantey 
3718d1b46d7Szhanghch05                 asyncResp->res.result(boost::beast::http::status::no_content);
372501be32bSraviteja-b                 if (newHostName)
373501be32bSraviteja-b                 {
3742db77d34SJohnathan Mantey                     messages::propertyNotWritable(asyncResp->res, "HostName");
375*44fad2aaSEd Tanous                     return;
376cf05f9dcSJohnathan Mantey                 }
377cf05f9dcSJohnathan Mantey 
378cf05f9dcSJohnathan Mantey                 if (ntp)
379cf05f9dcSJohnathan Mantey                 {
380cf05f9dcSJohnathan Mantey                     std::optional<std::vector<std::string>> ntpServers;
381cf05f9dcSJohnathan Mantey                     std::optional<bool> ntpEnabled;
3828d1b46d7Szhanghch05                     if (!json_util::readJson(*ntp, asyncResp->res, "NTPServers",
3837e860f15SJohn Edward Broadbent                                              ntpServers, "ProtocolEnabled",
3847e860f15SJohn Edward Broadbent                                              ntpEnabled))
385cf05f9dcSJohnathan Mantey                     {
386501be32bSraviteja-b                         return;
387501be32bSraviteja-b                     }
388cf05f9dcSJohnathan Mantey 
38920e6ea5dSraviteja-b                     if (ntpEnabled)
39020e6ea5dSraviteja-b                     {
39120e6ea5dSraviteja-b                         handleNTPProtocolEnabled(*ntpEnabled, asyncResp);
39220e6ea5dSraviteja-b                     }
393cf05f9dcSJohnathan Mantey 
39420e6ea5dSraviteja-b                     if (ntpServers)
39520e6ea5dSraviteja-b                     {
396dc3fbbd0STony Lee                         std::sort((*ntpServers).begin(), (*ntpServers).end());
397dc3fbbd0STony Lee                         (*ntpServers)
3987e860f15SJohn Edward Broadbent                             .erase(std::unique((*ntpServers).begin(),
3997e860f15SJohn Edward Broadbent                                                (*ntpServers).end()),
400dc3fbbd0STony Lee                                    (*ntpServers).end());
40120e6ea5dSraviteja-b                         handleNTPServersPatch(*ntpServers, asyncResp);
40220e6ea5dSraviteja-b                     }
403501be32bSraviteja-b                 }
40467a78d87STom Joseph 
40567a78d87STom Joseph                 if (ipmi)
40667a78d87STom Joseph                 {
40767a78d87STom Joseph                     std::optional<bool> ipmiProtocolEnabled;
4087e860f15SJohn Edward Broadbent                     if (!json_util::readJson(*ipmi, asyncResp->res,
4097e860f15SJohn Edward Broadbent                                              "ProtocolEnabled",
41067a78d87STom Joseph                                              ipmiProtocolEnabled))
41167a78d87STom Joseph                     {
41267a78d87STom Joseph                         return;
41367a78d87STom Joseph                     }
41467a78d87STom Joseph 
41567a78d87STom Joseph                     if (ipmiProtocolEnabled)
41667a78d87STom Joseph                     {
417e5a99777SAlbert Zhang                         handleProtocolEnabled(
418e5a99777SAlbert Zhang                             *ipmiProtocolEnabled, asyncResp,
419e5a99777SAlbert Zhang                             "/xyz/openbmc_project/control/service/"
420e5a99777SAlbert Zhang                             "phosphor_2dipmi_2dnet_40");
421e5a99777SAlbert Zhang                     }
422e5a99777SAlbert Zhang                 }
423e5a99777SAlbert Zhang 
424e5a99777SAlbert Zhang                 if (ssh)
425e5a99777SAlbert Zhang                 {
426e5a99777SAlbert Zhang                     std::optional<bool> sshProtocolEnabled;
427e5a99777SAlbert Zhang                     if (!json_util::readJson(*ssh, asyncResp->res,
428e5a99777SAlbert Zhang                                              "ProtocolEnabled",
429e5a99777SAlbert Zhang                                              sshProtocolEnabled))
430e5a99777SAlbert Zhang                     {
431e5a99777SAlbert Zhang                         return;
432e5a99777SAlbert Zhang                     }
433e5a99777SAlbert Zhang 
434e5a99777SAlbert Zhang                     if (sshProtocolEnabled)
435e5a99777SAlbert Zhang                     {
436e5a99777SAlbert Zhang                         handleProtocolEnabled(
437e5a99777SAlbert Zhang                             *sshProtocolEnabled, asyncResp,
438e5a99777SAlbert Zhang                             "/xyz/openbmc_project/control/service/dropbear");
43967a78d87STom Joseph                     }
44067a78d87STom Joseph                 }
4417e860f15SJohn Edward Broadbent             });
4427e860f15SJohn Edward Broadbent 
4437e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/NetworkProtocol/")
444ed398213SEd Tanous         .privileges(redfish::privileges::getManagerNetworkProtocol)
4457e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
44672048780SAbhishek Patel             [](const crow::Request& req,
4477e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
44872048780SAbhishek Patel                 getNetworkData(asyncResp, req);
4497e860f15SJohn Edward Broadbent             });
450cf05f9dcSJohnathan Mantey }
45170141561SBorawski.Lukasz 
45270141561SBorawski.Lukasz } // namespace redfish
453