xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 7af9151495a18c805b45764b4bba6302ec214efb)
19391bb9cSRapkiewicz, Pawel /*
29391bb9cSRapkiewicz, Pawel // Copyright (c) 2018 Intel Corporation
39391bb9cSRapkiewicz, Pawel //
49391bb9cSRapkiewicz, Pawel // Licensed under the Apache License, Version 2.0 (the "License");
59391bb9cSRapkiewicz, Pawel // you may not use this file except in compliance with the License.
69391bb9cSRapkiewicz, Pawel // You may obtain a copy of the License at
79391bb9cSRapkiewicz, Pawel //
89391bb9cSRapkiewicz, Pawel //      http://www.apache.org/licenses/LICENSE-2.0
99391bb9cSRapkiewicz, Pawel //
109391bb9cSRapkiewicz, Pawel // Unless required by applicable law or agreed to in writing, software
119391bb9cSRapkiewicz, Pawel // distributed under the License is distributed on an "AS IS" BASIS,
129391bb9cSRapkiewicz, Pawel // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139391bb9cSRapkiewicz, Pawel // See the License for the specific language governing permissions and
149391bb9cSRapkiewicz, Pawel // limitations under the License.
159391bb9cSRapkiewicz, Pawel */
169391bb9cSRapkiewicz, Pawel #pragma once
179391bb9cSRapkiewicz, Pawel 
181abe55efSEd Tanous #include <boost/container/flat_map.hpp>
194a0cb85cSEd Tanous #include <boost/container/flat_set.hpp>
20179db1d7SKowalski, Kamil #include <dbus_singleton.hpp>
21588c3f0dSKowalski, Kamil #include <error_messages.hpp>
22179db1d7SKowalski, Kamil #include <node.hpp>
23a24526dcSEd Tanous #include <optional>
24ab6554f1SJoshi-Mansi #include <regex>
25588c3f0dSKowalski, Kamil #include <utils/json_utils.hpp>
26abf2add6SEd Tanous #include <variant>
279391bb9cSRapkiewicz, Pawel 
281abe55efSEd Tanous namespace redfish
291abe55efSEd Tanous {
309391bb9cSRapkiewicz, Pawel 
319391bb9cSRapkiewicz, Pawel /**
329391bb9cSRapkiewicz, Pawel  * DBus types primitives for several generic DBus interfaces
339391bb9cSRapkiewicz, Pawel  * TODO(Pawel) consider move this to separate file into boost::dbus
349391bb9cSRapkiewicz, Pawel  */
35aa2e59c1SEd Tanous using PropertiesMapType = boost::container::flat_map<
36abf2add6SEd Tanous     std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
37aa2e59c1SEd Tanous                               int32_t, uint32_t, int64_t, uint64_t, double>>;
389391bb9cSRapkiewicz, Pawel 
394a0cb85cSEd Tanous using GetManagedObjects = std::vector<std::pair<
40aa2e59c1SEd Tanous     sdbusplus::message::object_path,
414a0cb85cSEd Tanous     std::vector<std::pair<
42aa2e59c1SEd Tanous         std::string,
43aa2e59c1SEd Tanous         boost::container::flat_map<
44029573d4SEd Tanous             std::string, sdbusplus::message::variant<
45029573d4SEd Tanous                              std::string, bool, uint8_t, int16_t, uint16_t,
46029573d4SEd Tanous                              int32_t, uint32_t, int64_t, uint64_t, double,
47029573d4SEd Tanous                              std::vector<std::string>>>>>>>;
484a0cb85cSEd Tanous 
494a0cb85cSEd Tanous enum class LinkType
504a0cb85cSEd Tanous {
514a0cb85cSEd Tanous     Local,
524a0cb85cSEd Tanous     Global
534a0cb85cSEd Tanous };
549391bb9cSRapkiewicz, Pawel 
559391bb9cSRapkiewicz, Pawel /**
569391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
579391bb9cSRapkiewicz, Pawel  */
581abe55efSEd Tanous struct IPv4AddressData
591abe55efSEd Tanous {
60179db1d7SKowalski, Kamil     std::string id;
614a0cb85cSEd Tanous     std::string address;
624a0cb85cSEd Tanous     std::string domain;
634a0cb85cSEd Tanous     std::string gateway;
649391bb9cSRapkiewicz, Pawel     std::string netmask;
659391bb9cSRapkiewicz, Pawel     std::string origin;
664a0cb85cSEd Tanous     LinkType linktype;
674a0cb85cSEd Tanous 
681abe55efSEd Tanous     bool operator<(const IPv4AddressData &obj) const
691abe55efSEd Tanous     {
704a0cb85cSEd Tanous         return id < obj.id;
711abe55efSEd Tanous     }
729391bb9cSRapkiewicz, Pawel };
739391bb9cSRapkiewicz, Pawel 
749391bb9cSRapkiewicz, Pawel /**
75e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
76e48c0fc5SRavi Teja  */
77e48c0fc5SRavi Teja struct IPv6AddressData
78e48c0fc5SRavi Teja {
79e48c0fc5SRavi Teja     std::string id;
80e48c0fc5SRavi Teja     std::string address;
81e48c0fc5SRavi Teja     std::string origin;
82e48c0fc5SRavi Teja     uint8_t prefixLength;
83e48c0fc5SRavi Teja 
84e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData &obj) const
85e48c0fc5SRavi Teja     {
86e48c0fc5SRavi Teja         return id < obj.id;
87e48c0fc5SRavi Teja     }
88e48c0fc5SRavi Teja };
89e48c0fc5SRavi Teja /**
909391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
919391bb9cSRapkiewicz, Pawel  * available from DBus
929391bb9cSRapkiewicz, Pawel  */
931abe55efSEd Tanous struct EthernetInterfaceData
941abe55efSEd Tanous {
954a0cb85cSEd Tanous     uint32_t speed;
964a0cb85cSEd Tanous     bool auto_neg;
971f8c7b5dSJohnathan Mantey     bool DNSEnabled;
981f8c7b5dSJohnathan Mantey     bool NTPEnabled;
991f8c7b5dSJohnathan Mantey     bool HostNameEnabled;
1001f8c7b5dSJohnathan Mantey     bool SendHostNameEnabled;
101aa05fb27SJohnathan Mantey     bool linkUp;
102eeedda23SJohnathan Mantey     bool nicEnabled;
1031f8c7b5dSJohnathan Mantey     std::string DHCPEnabled;
1041f8c7b5dSJohnathan Mantey     std::string operatingMode;
1054a0cb85cSEd Tanous     std::string hostname;
1064a0cb85cSEd Tanous     std::string default_gateway;
1079a6fc6feSRavi Teja     std::string ipv6_default_gateway;
1084a0cb85cSEd Tanous     std::string mac_address;
109fda13ad2SSunitha Harish     std::vector<std::uint32_t> vlan_id;
1100f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
1110f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
112d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
1139391bb9cSRapkiewicz, Pawel };
1149391bb9cSRapkiewicz, Pawel 
1151f8c7b5dSJohnathan Mantey struct DHCPParameters
1161f8c7b5dSJohnathan Mantey {
1171f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
1181f8c7b5dSJohnathan Mantey     std::optional<bool> useDNSServers;
1191f8c7b5dSJohnathan Mantey     std::optional<bool> useNTPServers;
1201f8c7b5dSJohnathan Mantey     std::optional<bool> useUseDomainName;
1211f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1221f8c7b5dSJohnathan Mantey };
1231f8c7b5dSJohnathan Mantey 
1249391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1259391bb9cSRapkiewicz, Pawel // into full dot notation
1261abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1271abe55efSEd Tanous {
1289391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1299391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1309391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1319391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1329391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1339391bb9cSRapkiewicz, Pawel     return netmask;
1349391bb9cSRapkiewicz, Pawel }
1359391bb9cSRapkiewicz, Pawel 
1361f8c7b5dSJohnathan Mantey inline bool translateDHCPEnabledToBool(const std::string &inputDHCP,
1371f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1381f8c7b5dSJohnathan Mantey {
1391f8c7b5dSJohnathan Mantey     if (isIPv4)
1401f8c7b5dSJohnathan Mantey     {
1411f8c7b5dSJohnathan Mantey         return (
1421f8c7b5dSJohnathan Mantey             (inputDHCP ==
1431f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1441f8c7b5dSJohnathan Mantey             (inputDHCP ==
1451f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1461f8c7b5dSJohnathan Mantey     }
1471f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1481f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1491f8c7b5dSJohnathan Mantey             (inputDHCP ==
1501f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1511f8c7b5dSJohnathan Mantey }
1521f8c7b5dSJohnathan Mantey 
1531f8c7b5dSJohnathan Mantey inline std::string GetDHCPEnabledEnumeration(bool isIPv4, bool isIPv6)
1541f8c7b5dSJohnathan Mantey {
1551f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1561f8c7b5dSJohnathan Mantey     {
1571f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1581f8c7b5dSJohnathan Mantey     }
1591f8c7b5dSJohnathan Mantey     else if (isIPv4)
1601f8c7b5dSJohnathan Mantey     {
1611f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1621f8c7b5dSJohnathan Mantey     }
1631f8c7b5dSJohnathan Mantey     else if (isIPv6)
1641f8c7b5dSJohnathan Mantey     {
1651f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1661f8c7b5dSJohnathan Mantey     }
1671f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1681f8c7b5dSJohnathan Mantey }
1691f8c7b5dSJohnathan Mantey 
1704a0cb85cSEd Tanous inline std::string
1714a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string &inputOrigin,
1724a0cb85cSEd Tanous                                         bool isIPv4)
1731abe55efSEd Tanous {
1744a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1751abe55efSEd Tanous     {
1764a0cb85cSEd Tanous         return "Static";
1779391bb9cSRapkiewicz, Pawel     }
1784a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1791abe55efSEd Tanous     {
1804a0cb85cSEd Tanous         if (isIPv4)
1811abe55efSEd Tanous         {
1824a0cb85cSEd Tanous             return "IPv4LinkLocal";
1831abe55efSEd Tanous         }
1841abe55efSEd Tanous         else
1851abe55efSEd Tanous         {
1864a0cb85cSEd Tanous             return "LinkLocal";
1879391bb9cSRapkiewicz, Pawel         }
1889391bb9cSRapkiewicz, Pawel     }
1894a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1901abe55efSEd Tanous     {
1914a0cb85cSEd Tanous         if (isIPv4)
1924a0cb85cSEd Tanous         {
1934a0cb85cSEd Tanous             return "DHCP";
1944a0cb85cSEd Tanous         }
1954a0cb85cSEd Tanous         else
1964a0cb85cSEd Tanous         {
1974a0cb85cSEd Tanous             return "DHCPv6";
1984a0cb85cSEd Tanous         }
1994a0cb85cSEd Tanous     }
2004a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
2014a0cb85cSEd Tanous     {
2024a0cb85cSEd Tanous         return "SLAAC";
2034a0cb85cSEd Tanous     }
2044a0cb85cSEd Tanous     return "";
2054a0cb85cSEd Tanous }
2064a0cb85cSEd Tanous 
2074c9afe43SEd Tanous inline bool extractEthernetInterfaceData(const std::string &ethiface_id,
2084a0cb85cSEd Tanous                                          const GetManagedObjects &dbus_data,
2094a0cb85cSEd Tanous                                          EthernetInterfaceData &ethData)
2104a0cb85cSEd Tanous {
2114c9afe43SEd Tanous     bool idFound = false;
2124a0cb85cSEd Tanous     for (const auto &objpath : dbus_data)
2134a0cb85cSEd Tanous     {
2144a0cb85cSEd Tanous         for (const auto &ifacePair : objpath.second)
2154a0cb85cSEd Tanous         {
216029573d4SEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
217029573d4SEd Tanous             {
2184c9afe43SEd Tanous                 idFound = true;
2194a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2204a0cb85cSEd Tanous                 {
2214a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
2224a0cb85cSEd Tanous                     {
2234a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2244a0cb85cSEd Tanous                         {
2254a0cb85cSEd Tanous                             const std::string *mac =
226abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2274a0cb85cSEd Tanous                             if (mac != nullptr)
2284a0cb85cSEd Tanous                             {
2294a0cb85cSEd Tanous                                 ethData.mac_address = *mac;
2304a0cb85cSEd Tanous                             }
2314a0cb85cSEd Tanous                         }
2324a0cb85cSEd Tanous                     }
2334a0cb85cSEd Tanous                 }
2344a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2354a0cb85cSEd Tanous                 {
2364a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
2374a0cb85cSEd Tanous                     {
2384a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2394a0cb85cSEd Tanous                         {
2401b6b96c5SEd Tanous                             const uint32_t *id =
241abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2424a0cb85cSEd Tanous                             if (id != nullptr)
2434a0cb85cSEd Tanous                             {
244fda13ad2SSunitha Harish                                 ethData.vlan_id.push_back(*id);
2454a0cb85cSEd Tanous                             }
2464a0cb85cSEd Tanous                         }
2474a0cb85cSEd Tanous                     }
2484a0cb85cSEd Tanous                 }
2494a0cb85cSEd Tanous                 else if (ifacePair.first ==
2504a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2514a0cb85cSEd Tanous                 {
2524a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
2534a0cb85cSEd Tanous                     {
2544a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2554a0cb85cSEd Tanous                         {
2564a0cb85cSEd Tanous                             const bool *auto_neg =
257abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2584a0cb85cSEd Tanous                             if (auto_neg != nullptr)
2594a0cb85cSEd Tanous                             {
2604a0cb85cSEd Tanous                                 ethData.auto_neg = *auto_neg;
2614a0cb85cSEd Tanous                             }
2624a0cb85cSEd Tanous                         }
2634a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2644a0cb85cSEd Tanous                         {
2654a0cb85cSEd Tanous                             const uint32_t *speed =
266abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2674a0cb85cSEd Tanous                             if (speed != nullptr)
2684a0cb85cSEd Tanous                             {
2694a0cb85cSEd Tanous                                 ethData.speed = *speed;
2704a0cb85cSEd Tanous                             }
2714a0cb85cSEd Tanous                         }
272aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
273aa05fb27SJohnathan Mantey                         {
274aa05fb27SJohnathan Mantey                             const bool *linkUp =
275aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
276aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
277aa05fb27SJohnathan Mantey                             {
278aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
279aa05fb27SJohnathan Mantey                             }
280aa05fb27SJohnathan Mantey                         }
281eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
282eeedda23SJohnathan Mantey                         {
283eeedda23SJohnathan Mantey                             const bool *nicEnabled =
284eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
285eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
286eeedda23SJohnathan Mantey                             {
287eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
288eeedda23SJohnathan Mantey                             }
289eeedda23SJohnathan Mantey                         }
290f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
291029573d4SEd Tanous                         {
292029573d4SEd Tanous                             const std::vector<std::string> *nameservers =
293029573d4SEd Tanous                                 sdbusplus::message::variant_ns::get_if<
294029573d4SEd Tanous                                     std::vector<std::string>>(
295029573d4SEd Tanous                                     &propertyPair.second);
296029573d4SEd Tanous                             if (nameservers != nullptr)
297029573d4SEd Tanous                             {
2980f6efdc1Smanojkiran.eda@gmail.com                                 ethData.nameServers = std::move(*nameservers);
2990f6efdc1Smanojkiran.eda@gmail.com                             }
3000f6efdc1Smanojkiran.eda@gmail.com                         }
3010f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
3020f6efdc1Smanojkiran.eda@gmail.com                         {
3030f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string> *staticNameServers =
3040f6efdc1Smanojkiran.eda@gmail.com                                 sdbusplus::message::variant_ns::get_if<
3050f6efdc1Smanojkiran.eda@gmail.com                                     std::vector<std::string>>(
3060f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
3070f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
3080f6efdc1Smanojkiran.eda@gmail.com                             {
3090f6efdc1Smanojkiran.eda@gmail.com                                 ethData.staticNameServers =
3100f6efdc1Smanojkiran.eda@gmail.com                                     std::move(*staticNameServers);
3114a0cb85cSEd Tanous                             }
3124a0cb85cSEd Tanous                         }
3132a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3142a133282Smanojkiraneda                         {
3151f8c7b5dSJohnathan Mantey                             const std::string *DHCPEnabled =
3161f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3172a133282Smanojkiraneda                             if (DHCPEnabled != nullptr)
3182a133282Smanojkiraneda                             {
3192a133282Smanojkiraneda                                 ethData.DHCPEnabled = *DHCPEnabled;
3202a133282Smanojkiraneda                             }
3212a133282Smanojkiraneda                         }
322d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
323d24bfc7aSJennifer Lee                         {
324d24bfc7aSJennifer Lee                             const std::vector<std::string> *domainNames =
325d24bfc7aSJennifer Lee                                 sdbusplus::message::variant_ns::get_if<
326d24bfc7aSJennifer Lee                                     std::vector<std::string>>(
327d24bfc7aSJennifer Lee                                     &propertyPair.second);
328d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
329d24bfc7aSJennifer Lee                             {
330d24bfc7aSJennifer Lee                                 ethData.domainnames = std::move(*domainNames);
331d24bfc7aSJennifer Lee                             }
332d24bfc7aSJennifer Lee                         }
333029573d4SEd Tanous                     }
334029573d4SEd Tanous                 }
335029573d4SEd Tanous             }
3361f8c7b5dSJohnathan Mantey 
3371f8c7b5dSJohnathan Mantey             if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
3381f8c7b5dSJohnathan Mantey             {
3391f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3401f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3411f8c7b5dSJohnathan Mantey                 {
3421f8c7b5dSJohnathan Mantey                     for (const auto &propertyPair : ifacePair.second)
3431f8c7b5dSJohnathan Mantey                     {
3441f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3451f8c7b5dSJohnathan Mantey                         {
3461f8c7b5dSJohnathan Mantey                             const bool *DNSEnabled =
3471f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3481f8c7b5dSJohnathan Mantey                             if (DNSEnabled != nullptr)
3491f8c7b5dSJohnathan Mantey                             {
3501f8c7b5dSJohnathan Mantey                                 ethData.DNSEnabled = *DNSEnabled;
3511f8c7b5dSJohnathan Mantey                             }
3521f8c7b5dSJohnathan Mantey                         }
3531f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3541f8c7b5dSJohnathan Mantey                         {
3551f8c7b5dSJohnathan Mantey                             const bool *NTPEnabled =
3561f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3571f8c7b5dSJohnathan Mantey                             if (NTPEnabled != nullptr)
3581f8c7b5dSJohnathan Mantey                             {
3591f8c7b5dSJohnathan Mantey                                 ethData.NTPEnabled = *NTPEnabled;
3601f8c7b5dSJohnathan Mantey                             }
3611f8c7b5dSJohnathan Mantey                         }
3621f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3631f8c7b5dSJohnathan Mantey                         {
3641f8c7b5dSJohnathan Mantey                             const bool *HostNameEnabled =
3651f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3661f8c7b5dSJohnathan Mantey                             if (HostNameEnabled != nullptr)
3671f8c7b5dSJohnathan Mantey                             {
3681f8c7b5dSJohnathan Mantey                                 ethData.HostNameEnabled = *HostNameEnabled;
3691f8c7b5dSJohnathan Mantey                             }
3701f8c7b5dSJohnathan Mantey                         }
3711f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "SendHostNameEnabled")
3721f8c7b5dSJohnathan Mantey                         {
3731f8c7b5dSJohnathan Mantey                             const bool *SendHostNameEnabled =
3741f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3751f8c7b5dSJohnathan Mantey                             if (SendHostNameEnabled != nullptr)
3761f8c7b5dSJohnathan Mantey                             {
3771f8c7b5dSJohnathan Mantey                                 ethData.SendHostNameEnabled =
3781f8c7b5dSJohnathan Mantey                                     *SendHostNameEnabled;
3791f8c7b5dSJohnathan Mantey                             }
3801f8c7b5dSJohnathan Mantey                         }
3811f8c7b5dSJohnathan Mantey                     }
3821f8c7b5dSJohnathan Mantey                 }
3831f8c7b5dSJohnathan Mantey             }
384029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
385029573d4SEd Tanous             // to check eth number
386029573d4SEd Tanous             if (ifacePair.first ==
3874a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
3884a0cb85cSEd Tanous             {
3894a0cb85cSEd Tanous                 for (const auto &propertyPair : ifacePair.second)
3904a0cb85cSEd Tanous                 {
3914a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
3924a0cb85cSEd Tanous                     {
3934a0cb85cSEd Tanous                         const std::string *hostname =
394029573d4SEd Tanous                             sdbusplus::message::variant_ns::get_if<std::string>(
395029573d4SEd Tanous                                 &propertyPair.second);
3964a0cb85cSEd Tanous                         if (hostname != nullptr)
3974a0cb85cSEd Tanous                         {
3984a0cb85cSEd Tanous                             ethData.hostname = *hostname;
3994a0cb85cSEd Tanous                         }
4004a0cb85cSEd Tanous                     }
4014a0cb85cSEd Tanous                     else if (propertyPair.first == "DefaultGateway")
4024a0cb85cSEd Tanous                     {
4034a0cb85cSEd Tanous                         const std::string *defaultGateway =
404029573d4SEd Tanous                             sdbusplus::message::variant_ns::get_if<std::string>(
405029573d4SEd Tanous                                 &propertyPair.second);
4064a0cb85cSEd Tanous                         if (defaultGateway != nullptr)
4074a0cb85cSEd Tanous                         {
4084a0cb85cSEd Tanous                             ethData.default_gateway = *defaultGateway;
4094a0cb85cSEd Tanous                         }
4104a0cb85cSEd Tanous                     }
4119a6fc6feSRavi Teja                     else if (propertyPair.first == "DefaultGateway6")
4129a6fc6feSRavi Teja                     {
4139a6fc6feSRavi Teja                         const std::string *defaultGateway6 =
4149a6fc6feSRavi Teja                             sdbusplus::message::variant_ns::get_if<std::string>(
4159a6fc6feSRavi Teja                                 &propertyPair.second);
4169a6fc6feSRavi Teja                         if (defaultGateway6 != nullptr)
4179a6fc6feSRavi Teja                         {
4189a6fc6feSRavi Teja                             ethData.ipv6_default_gateway = *defaultGateway6;
4199a6fc6feSRavi Teja                         }
4209a6fc6feSRavi Teja                     }
4214a0cb85cSEd Tanous                 }
4224a0cb85cSEd Tanous             }
4234a0cb85cSEd Tanous         }
4244a0cb85cSEd Tanous     }
4254c9afe43SEd Tanous     return idFound;
4264a0cb85cSEd Tanous }
4274a0cb85cSEd Tanous 
428e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
42901784826SJohnathan Mantey inline void
43001784826SJohnathan Mantey     extractIPV6Data(const std::string &ethiface_id,
43101784826SJohnathan Mantey                     const GetManagedObjects &dbus_data,
43201784826SJohnathan Mantey                     boost::container::flat_set<IPv6AddressData> &ipv6_config)
433e48c0fc5SRavi Teja {
434e48c0fc5SRavi Teja     const std::string ipv6PathStart =
435e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ethiface_id + "/ipv6/";
436e48c0fc5SRavi Teja 
437e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
438e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
439e48c0fc5SRavi Teja     for (const auto &objpath : dbus_data)
440e48c0fc5SRavi Teja     {
441e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
442e48c0fc5SRavi Teja         if (boost::starts_with(objpath.first.str, ipv6PathStart))
443e48c0fc5SRavi Teja         {
444e48c0fc5SRavi Teja             for (auto &interface : objpath.second)
445e48c0fc5SRavi Teja             {
446e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
447e48c0fc5SRavi Teja                 {
448e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
449e48c0fc5SRavi Teja                     // appropriate
450e48c0fc5SRavi Teja                     std::pair<
451e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
452e48c0fc5SRavi Teja                         bool>
453271584abSEd Tanous                         it = ipv6_config.insert(IPv6AddressData{});
454e48c0fc5SRavi Teja                     IPv6AddressData &ipv6_address = *it.first;
455271584abSEd Tanous                     ipv6_address.id =
456271584abSEd Tanous                         objpath.first.str.substr(ipv6PathStart.size());
457e48c0fc5SRavi Teja                     for (auto &property : interface.second)
458e48c0fc5SRavi Teja                     {
459e48c0fc5SRavi Teja                         if (property.first == "Address")
460e48c0fc5SRavi Teja                         {
461e48c0fc5SRavi Teja                             const std::string *address =
462e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
463e48c0fc5SRavi Teja                             if (address != nullptr)
464e48c0fc5SRavi Teja                             {
465e48c0fc5SRavi Teja                                 ipv6_address.address = *address;
466e48c0fc5SRavi Teja                             }
467e48c0fc5SRavi Teja                         }
468e48c0fc5SRavi Teja                         else if (property.first == "Origin")
469e48c0fc5SRavi Teja                         {
470e48c0fc5SRavi Teja                             const std::string *origin =
471e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
472e48c0fc5SRavi Teja                             if (origin != nullptr)
473e48c0fc5SRavi Teja                             {
474e48c0fc5SRavi Teja                                 ipv6_address.origin =
475e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
476e48c0fc5SRavi Teja                                                                         false);
477e48c0fc5SRavi Teja                             }
478e48c0fc5SRavi Teja                         }
479e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
480e48c0fc5SRavi Teja                         {
481e48c0fc5SRavi Teja                             const uint8_t *prefix =
482e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
483e48c0fc5SRavi Teja                             if (prefix != nullptr)
484e48c0fc5SRavi Teja                             {
485e48c0fc5SRavi Teja                                 ipv6_address.prefixLength = *prefix;
486e48c0fc5SRavi Teja                             }
487e48c0fc5SRavi Teja                         }
488e48c0fc5SRavi Teja                         else
489e48c0fc5SRavi Teja                         {
490e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
491e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
492e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
493e48c0fc5SRavi Teja                         }
494e48c0fc5SRavi Teja                     }
495e48c0fc5SRavi Teja                 }
496e48c0fc5SRavi Teja             }
497e48c0fc5SRavi Teja         }
498e48c0fc5SRavi Teja     }
499e48c0fc5SRavi Teja }
500e48c0fc5SRavi Teja 
5014a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
50201784826SJohnathan Mantey inline void
50301784826SJohnathan Mantey     extractIPData(const std::string &ethiface_id,
50401784826SJohnathan Mantey                   const GetManagedObjects &dbus_data,
50501784826SJohnathan Mantey                   boost::container::flat_set<IPv4AddressData> &ipv4_config)
5064a0cb85cSEd Tanous {
5074a0cb85cSEd Tanous     const std::string ipv4PathStart =
5084a0cb85cSEd Tanous         "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
5094a0cb85cSEd Tanous 
5104a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5114a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
5124a0cb85cSEd Tanous     for (const auto &objpath : dbus_data)
5134a0cb85cSEd Tanous     {
5144a0cb85cSEd Tanous         // Check if proper pattern for object path appears
5154a0cb85cSEd Tanous         if (boost::starts_with(objpath.first.str, ipv4PathStart))
5164a0cb85cSEd Tanous         {
5174a0cb85cSEd Tanous             for (auto &interface : objpath.second)
5184a0cb85cSEd Tanous             {
5194a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5204a0cb85cSEd Tanous                 {
5214a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5224a0cb85cSEd Tanous                     // appropriate
5234a0cb85cSEd Tanous                     std::pair<
5244a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5254a0cb85cSEd Tanous                         bool>
526271584abSEd Tanous                         it = ipv4_config.insert(IPv4AddressData{});
5274a0cb85cSEd Tanous                     IPv4AddressData &ipv4_address = *it.first;
528271584abSEd Tanous                     ipv4_address.id =
529271584abSEd Tanous                         objpath.first.str.substr(ipv4PathStart.size());
5304a0cb85cSEd Tanous                     for (auto &property : interface.second)
5314a0cb85cSEd Tanous                     {
5324a0cb85cSEd Tanous                         if (property.first == "Address")
5334a0cb85cSEd Tanous                         {
5344a0cb85cSEd Tanous                             const std::string *address =
535abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5364a0cb85cSEd Tanous                             if (address != nullptr)
5374a0cb85cSEd Tanous                             {
5384a0cb85cSEd Tanous                                 ipv4_address.address = *address;
5394a0cb85cSEd Tanous                             }
5404a0cb85cSEd Tanous                         }
5414a0cb85cSEd Tanous                         else if (property.first == "Gateway")
5424a0cb85cSEd Tanous                         {
5434a0cb85cSEd Tanous                             const std::string *gateway =
544abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5454a0cb85cSEd Tanous                             if (gateway != nullptr)
5464a0cb85cSEd Tanous                             {
5474a0cb85cSEd Tanous                                 ipv4_address.gateway = *gateway;
5484a0cb85cSEd Tanous                             }
5494a0cb85cSEd Tanous                         }
5504a0cb85cSEd Tanous                         else if (property.first == "Origin")
5514a0cb85cSEd Tanous                         {
5524a0cb85cSEd Tanous                             const std::string *origin =
553abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5544a0cb85cSEd Tanous                             if (origin != nullptr)
5554a0cb85cSEd Tanous                             {
5564a0cb85cSEd Tanous                                 ipv4_address.origin =
5574a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5584a0cb85cSEd Tanous                                                                         true);
5594a0cb85cSEd Tanous                             }
5604a0cb85cSEd Tanous                         }
5614a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5624a0cb85cSEd Tanous                         {
5634a0cb85cSEd Tanous                             const uint8_t *mask =
564abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5654a0cb85cSEd Tanous                             if (mask != nullptr)
5664a0cb85cSEd Tanous                             {
5674a0cb85cSEd Tanous                                 // convert it to the string
5684a0cb85cSEd Tanous                                 ipv4_address.netmask = getNetmask(*mask);
5694a0cb85cSEd Tanous                             }
5704a0cb85cSEd Tanous                         }
5714a0cb85cSEd Tanous                         else
5724a0cb85cSEd Tanous                         {
5734a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5744a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5754a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5764a0cb85cSEd Tanous                         }
5774a0cb85cSEd Tanous                     }
5784a0cb85cSEd Tanous                     // Check if given address is local, or global
5794a0cb85cSEd Tanous                     ipv4_address.linktype =
5804a0cb85cSEd Tanous                         boost::starts_with(ipv4_address.address, "169.254.")
58118659d10SJohnathan Mantey                             ? LinkType::Local
58218659d10SJohnathan Mantey                             : LinkType::Global;
5834a0cb85cSEd Tanous                 }
5844a0cb85cSEd Tanous             }
5854a0cb85cSEd Tanous         }
5864a0cb85cSEd Tanous     }
5874a0cb85cSEd Tanous }
588588c3f0dSKowalski, Kamil 
589588c3f0dSKowalski, Kamil /**
590588c3f0dSKowalski, Kamil  * @brief Sets given Id on the given VLAN interface through D-Bus
591588c3f0dSKowalski, Kamil  *
592588c3f0dSKowalski, Kamil  * @param[in] ifaceId       Id of VLAN interface that should be modified
593588c3f0dSKowalski, Kamil  * @param[in] inputVlanId   New ID of the VLAN
594588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
595588c3f0dSKowalski, Kamil  *
596588c3f0dSKowalski, Kamil  * @return None.
597588c3f0dSKowalski, Kamil  */
598588c3f0dSKowalski, Kamil template <typename CallbackFunc>
5994a0cb85cSEd Tanous void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId,
6001abe55efSEd Tanous                   CallbackFunc &&callback)
6011abe55efSEd Tanous {
60255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
603588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
604588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
605588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
606588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", "Id",
607abf2add6SEd Tanous         std::variant<uint32_t>(inputVlanId));
6084a0cb85cSEd Tanous }
609588c3f0dSKowalski, Kamil 
610588c3f0dSKowalski, Kamil /**
611179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
612179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
613179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
614179db1d7SKowalski, Kamil  *
615179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
616179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
617179db1d7SKowalski, Kamil  *
618179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
619179db1d7SKowalski, Kamil  */
6204a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip,
6211abe55efSEd Tanous                                        uint8_t *bits = nullptr)
6221abe55efSEd Tanous {
623179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
624179db1d7SKowalski, Kamil 
625179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
626179db1d7SKowalski, Kamil 
6274a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
6281abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
6291abe55efSEd Tanous     {
630179db1d7SKowalski, Kamil         return false;
631179db1d7SKowalski, Kamil     }
632179db1d7SKowalski, Kamil 
6331abe55efSEd Tanous     if (bits != nullptr)
6341abe55efSEd Tanous     {
635179db1d7SKowalski, Kamil         *bits = 0;
636179db1d7SKowalski, Kamil     }
637179db1d7SKowalski, Kamil 
638179db1d7SKowalski, Kamil     char *endPtr;
639179db1d7SKowalski, Kamil     long previousValue = 255;
640179db1d7SKowalski, Kamil     bool firstZeroInByteHit;
6411abe55efSEd Tanous     for (const std::string &byte : bytesInMask)
6421abe55efSEd Tanous     {
6431abe55efSEd Tanous         if (byte.empty())
6441abe55efSEd Tanous         {
6451db9ca37SKowalski, Kamil             return false;
6461db9ca37SKowalski, Kamil         }
6471db9ca37SKowalski, Kamil 
648179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
6491db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
650179db1d7SKowalski, Kamil 
6514a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
6524a0cb85cSEd Tanous         // is not 100% number
6531abe55efSEd Tanous         if (*endPtr != '\0')
6541abe55efSEd Tanous         {
655179db1d7SKowalski, Kamil             return false;
656179db1d7SKowalski, Kamil         }
657179db1d7SKowalski, Kamil 
658179db1d7SKowalski, Kamil         // Value should be contained in byte
6591abe55efSEd Tanous         if (value < 0 || value > 255)
6601abe55efSEd Tanous         {
661179db1d7SKowalski, Kamil             return false;
662179db1d7SKowalski, Kamil         }
663179db1d7SKowalski, Kamil 
6641abe55efSEd Tanous         if (bits != nullptr)
6651abe55efSEd Tanous         {
666179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
6671abe55efSEd Tanous             if (previousValue != 255 && value != 0)
6681abe55efSEd Tanous             {
669179db1d7SKowalski, Kamil                 return false;
670179db1d7SKowalski, Kamil             }
671179db1d7SKowalski, Kamil 
672179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
673179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
674179db1d7SKowalski, Kamil 
675179db1d7SKowalski, Kamil             // Count bits
6761abe55efSEd Tanous             for (int bitIdx = 7; bitIdx >= 0; bitIdx--)
6771abe55efSEd Tanous             {
6781abe55efSEd Tanous                 if (value & (1 << bitIdx))
6791abe55efSEd Tanous                 {
6801abe55efSEd Tanous                     if (firstZeroInByteHit)
6811abe55efSEd Tanous                     {
682179db1d7SKowalski, Kamil                         // Continuity not preserved
683179db1d7SKowalski, Kamil                         return false;
6841abe55efSEd Tanous                     }
6851abe55efSEd Tanous                     else
6861abe55efSEd Tanous                     {
687179db1d7SKowalski, Kamil                         (*bits)++;
688179db1d7SKowalski, Kamil                     }
6891abe55efSEd Tanous                 }
6901abe55efSEd Tanous                 else
6911abe55efSEd Tanous                 {
692179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
693179db1d7SKowalski, Kamil                 }
694179db1d7SKowalski, Kamil             }
695179db1d7SKowalski, Kamil         }
696179db1d7SKowalski, Kamil 
697179db1d7SKowalski, Kamil         previousValue = value;
698179db1d7SKowalski, Kamil     }
699179db1d7SKowalski, Kamil 
700179db1d7SKowalski, Kamil     return true;
701179db1d7SKowalski, Kamil }
702179db1d7SKowalski, Kamil 
703179db1d7SKowalski, Kamil /**
70401784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
705179db1d7SKowalski, Kamil  *
706179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
707179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
708179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
709179db1d7SKowalski, Kamil  *
710179db1d7SKowalski, Kamil  * @return None
711179db1d7SKowalski, Kamil  */
7124a0cb85cSEd Tanous inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
7134a0cb85cSEd Tanous                        const std::shared_ptr<AsyncResp> asyncResp)
7141abe55efSEd Tanous {
71555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
716286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
7171abe55efSEd Tanous             if (ec)
7181abe55efSEd Tanous             {
719a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
7201abe55efSEd Tanous             }
721179db1d7SKowalski, Kamil         },
722179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
723179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
724179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
725179db1d7SKowalski, Kamil }
726179db1d7SKowalski, Kamil 
727179db1d7SKowalski, Kamil /**
72801784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
729179db1d7SKowalski, Kamil  *
73001784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
73101784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
73201784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
73301784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
734179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
735179db1d7SKowalski, Kamil  *
736179db1d7SKowalski, Kamil  * @return None
737179db1d7SKowalski, Kamil  */
738b01bf299SEd Tanous inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
73901784826SJohnathan Mantey                        uint8_t prefixLength, const std::string &gateway,
740b01bf299SEd Tanous                        const std::string &address,
7414a0cb85cSEd Tanous                        std::shared_ptr<AsyncResp> asyncResp)
7421abe55efSEd Tanous {
74301784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
74401784826SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
7451abe55efSEd Tanous             if (ec)
7461abe55efSEd Tanous             {
747a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
748179db1d7SKowalski, Kamil             }
74901784826SJohnathan Mantey         },
75001784826SJohnathan Mantey         "xyz.openbmc_project.Network",
751179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
752179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
75301784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
754179db1d7SKowalski, Kamil         gateway);
755179db1d7SKowalski, Kamil }
756e48c0fc5SRavi Teja 
757e48c0fc5SRavi Teja /**
75801784826SJohnathan Mantey  * @brief Deletes the IPv4 entry for this interface and creates a replacement
75901784826SJohnathan Mantey  * static IPv4 entry
76001784826SJohnathan Mantey  *
76101784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
76201784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
76301784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
76401784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
76501784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
76601784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
76701784826SJohnathan Mantey  *
76801784826SJohnathan Mantey  * @return None
76901784826SJohnathan Mantey  */
77001784826SJohnathan Mantey inline void deleteAndCreateIPv4(const std::string &ifaceId,
77101784826SJohnathan Mantey                                 const std::string &id, uint8_t prefixLength,
77201784826SJohnathan Mantey                                 const std::string &gateway,
77301784826SJohnathan Mantey                                 const std::string &address,
77401784826SJohnathan Mantey                                 std::shared_ptr<AsyncResp> asyncResp)
77501784826SJohnathan Mantey {
77601784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
77701784826SJohnathan Mantey         [asyncResp, ifaceId, address, prefixLength,
77801784826SJohnathan Mantey          gateway](const boost::system::error_code ec) {
77901784826SJohnathan Mantey             if (ec)
78001784826SJohnathan Mantey             {
78101784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
78201784826SJohnathan Mantey             }
78301784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
78401784826SJohnathan Mantey                 [asyncResp](const boost::system::error_code ec) {
78501784826SJohnathan Mantey                     if (ec)
78601784826SJohnathan Mantey                     {
78701784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
78801784826SJohnathan Mantey                     }
78901784826SJohnathan Mantey                 },
79001784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
79101784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
79201784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
79301784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
79401784826SJohnathan Mantey                 prefixLength, gateway);
79501784826SJohnathan Mantey         },
79601784826SJohnathan Mantey         "xyz.openbmc_project.Network",
79701784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
79801784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
79901784826SJohnathan Mantey }
80001784826SJohnathan Mantey 
80101784826SJohnathan Mantey /**
802e48c0fc5SRavi Teja  * @brief Deletes given IPv6
803e48c0fc5SRavi Teja  *
804e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
805e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
806e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
807e48c0fc5SRavi Teja  *
808e48c0fc5SRavi Teja  * @return None
809e48c0fc5SRavi Teja  */
810e48c0fc5SRavi Teja inline void deleteIPv6(const std::string &ifaceId, const std::string &ipHash,
811e48c0fc5SRavi Teja                        const std::shared_ptr<AsyncResp> asyncResp)
812e48c0fc5SRavi Teja {
813e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
814286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
815e48c0fc5SRavi Teja             if (ec)
816e48c0fc5SRavi Teja             {
817e48c0fc5SRavi Teja                 messages::internalError(asyncResp->res);
818e48c0fc5SRavi Teja             }
819e48c0fc5SRavi Teja         },
820e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
821e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
822e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
823e48c0fc5SRavi Teja }
824e48c0fc5SRavi Teja 
825e48c0fc5SRavi Teja /**
82601784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
82701784826SJohnathan Mantey  * static IPv6 entry
82801784826SJohnathan Mantey  *
82901784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
83001784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
83101784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
83201784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
83301784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
83401784826SJohnathan Mantey  *
83501784826SJohnathan Mantey  * @return None
83601784826SJohnathan Mantey  */
83701784826SJohnathan Mantey inline void deleteAndCreateIPv6(const std::string &ifaceId,
83801784826SJohnathan Mantey                                 const std::string &id, uint8_t prefixLength,
83901784826SJohnathan Mantey                                 const std::string &address,
84001784826SJohnathan Mantey                                 std::shared_ptr<AsyncResp> asyncResp)
84101784826SJohnathan Mantey {
84201784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
84301784826SJohnathan Mantey         [asyncResp, ifaceId, address,
84401784826SJohnathan Mantey          prefixLength](const boost::system::error_code ec) {
84501784826SJohnathan Mantey             if (ec)
84601784826SJohnathan Mantey             {
84701784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
84801784826SJohnathan Mantey             }
84901784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
85001784826SJohnathan Mantey                 [asyncResp](const boost::system::error_code ec) {
85101784826SJohnathan Mantey                     if (ec)
85201784826SJohnathan Mantey                     {
85301784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
85401784826SJohnathan Mantey                     }
85501784826SJohnathan Mantey                 },
85601784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
85701784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
85801784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
85901784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
86001784826SJohnathan Mantey                 prefixLength, "");
86101784826SJohnathan Mantey         },
86201784826SJohnathan Mantey         "xyz.openbmc_project.Network",
86301784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
86401784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
86501784826SJohnathan Mantey }
86601784826SJohnathan Mantey 
86701784826SJohnathan Mantey /**
868e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
869e48c0fc5SRavi Teja  *
870e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
871e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
872e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
873e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
874e48c0fc5SRavi Teja  *
875e48c0fc5SRavi Teja  * @return None
876e48c0fc5SRavi Teja  */
87701784826SJohnathan Mantey inline void createIPv6(const std::string &ifaceId, uint8_t prefixLength,
87801784826SJohnathan Mantey                        const std::string &address,
879e48c0fc5SRavi Teja                        std::shared_ptr<AsyncResp> asyncResp)
880e48c0fc5SRavi Teja {
881e48c0fc5SRavi Teja     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
882e48c0fc5SRavi Teja         if (ec)
883e48c0fc5SRavi Teja         {
884e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
885e48c0fc5SRavi Teja         }
886e48c0fc5SRavi Teja     };
887e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
888e48c0fc5SRavi Teja     // does not have assosiated gateway property
889e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
890e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
891e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
892e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
893e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
894e48c0fc5SRavi Teja         "");
895e48c0fc5SRavi Teja }
896e48c0fc5SRavi Teja 
897179db1d7SKowalski, Kamil /**
898179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
899179db1d7SKowalski, Kamil  * Object
900179db1d7SKowalski, Kamil  * from EntityManager Network Manager
9014a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
902179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
903179db1d7SKowalski, Kamil  * into JSON
904179db1d7SKowalski, Kamil  */
905179db1d7SKowalski, Kamil template <typename CallbackFunc>
9064a0cb85cSEd Tanous void getEthernetIfaceData(const std::string &ethiface_id,
9071abe55efSEd Tanous                           CallbackFunc &&callback)
9081abe55efSEd Tanous {
90955c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
9104a0cb85cSEd Tanous         [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
9111abe55efSEd Tanous             const boost::system::error_code error_code,
9124a0cb85cSEd Tanous             const GetManagedObjects &resp) {
91355c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
9144a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
915e48c0fc5SRavi Teja             boost::container::flat_set<IPv6AddressData> ipv6Data;
916179db1d7SKowalski, Kamil 
9171abe55efSEd Tanous             if (error_code)
9181abe55efSEd Tanous             {
91901784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
920179db1d7SKowalski, Kamil                 return;
921179db1d7SKowalski, Kamil             }
922179db1d7SKowalski, Kamil 
9234c9afe43SEd Tanous             bool found =
9244a0cb85cSEd Tanous                 extractEthernetInterfaceData(ethiface_id, resp, ethData);
9254c9afe43SEd Tanous             if (!found)
9264c9afe43SEd Tanous             {
92701784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
9284c9afe43SEd Tanous                 return;
9294c9afe43SEd Tanous             }
9304c9afe43SEd Tanous 
93101784826SJohnathan Mantey             extractIPData(ethiface_id, resp, ipv4Data);
932179db1d7SKowalski, Kamil             // Fix global GW
9331abe55efSEd Tanous             for (IPv4AddressData &ipv4 : ipv4Data)
9341abe55efSEd Tanous             {
935c619141bSRavi Teja                 if (((ipv4.linktype == LinkType::Global) &&
936c619141bSRavi Teja                      (ipv4.gateway == "0.0.0.0")) ||
937c619141bSRavi Teja                     (ipv4.origin == "DHCP"))
9381abe55efSEd Tanous                 {
9394a0cb85cSEd Tanous                     ipv4.gateway = ethData.default_gateway;
940179db1d7SKowalski, Kamil                 }
941179db1d7SKowalski, Kamil             }
942179db1d7SKowalski, Kamil 
94301784826SJohnathan Mantey             extractIPV6Data(ethiface_id, resp, ipv6Data);
9444a0cb85cSEd Tanous             // Finally make a callback with usefull data
94501784826SJohnathan Mantey             callback(true, ethData, ipv4Data, ipv6Data);
946179db1d7SKowalski, Kamil         },
947179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
948179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
949271584abSEd Tanous }
950179db1d7SKowalski, Kamil 
951179db1d7SKowalski, Kamil /**
9529391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
9539391bb9cSRapkiewicz, Pawel  * Manager
9541abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
9551abe55efSEd Tanous  * into JSON.
9569391bb9cSRapkiewicz, Pawel  */
9579391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
9581abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc &&callback)
9591abe55efSEd Tanous {
96055c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
9614a0cb85cSEd Tanous         [callback{std::move(callback)}](
9629391bb9cSRapkiewicz, Pawel             const boost::system::error_code error_code,
9634a0cb85cSEd Tanous             GetManagedObjects &resp) {
9641abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
9651abe55efSEd Tanous             // ethernet interfaces
9664c9afe43SEd Tanous             boost::container::flat_set<std::string> iface_list;
9674a0cb85cSEd Tanous             iface_list.reserve(resp.size());
9681abe55efSEd Tanous             if (error_code)
9691abe55efSEd Tanous             {
9704a0cb85cSEd Tanous                 callback(false, iface_list);
9719391bb9cSRapkiewicz, Pawel                 return;
9729391bb9cSRapkiewicz, Pawel             }
9739391bb9cSRapkiewicz, Pawel 
9749391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
9754a0cb85cSEd Tanous             for (const auto &objpath : resp)
9761abe55efSEd Tanous             {
9779391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
9784a0cb85cSEd Tanous                 for (const auto &interface : objpath.second)
9791abe55efSEd Tanous                 {
9801abe55efSEd Tanous                     // If interface is
9814a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
9824a0cb85cSEd Tanous                     // what we're looking for.
9839391bb9cSRapkiewicz, Pawel                     if (interface.first ==
9841abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
9851abe55efSEd Tanous                     {
9864a0cb85cSEd Tanous                         // Cut out everyting until last "/", ...
9874a0cb85cSEd Tanous                         const std::string &iface_id = objpath.first.str;
9884a0cb85cSEd Tanous                         std::size_t last_pos = iface_id.rfind("/");
9894a0cb85cSEd Tanous                         if (last_pos != std::string::npos)
9901abe55efSEd Tanous                         {
9919391bb9cSRapkiewicz, Pawel                             // and put it into output vector.
9924c9afe43SEd Tanous                             iface_list.emplace(iface_id.substr(last_pos + 1));
9939391bb9cSRapkiewicz, Pawel                         }
9949391bb9cSRapkiewicz, Pawel                     }
9959391bb9cSRapkiewicz, Pawel                 }
9969391bb9cSRapkiewicz, Pawel             }
997a434f2bdSEd Tanous             // Finally make a callback with useful data
9984a0cb85cSEd Tanous             callback(true, iface_list);
9999391bb9cSRapkiewicz, Pawel         },
1000aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1001aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1002271584abSEd Tanous }
10039391bb9cSRapkiewicz, Pawel 
10049391bb9cSRapkiewicz, Pawel /**
10059391bb9cSRapkiewicz, Pawel  * EthernetCollection derived class for delivering Ethernet Collection Schema
10069391bb9cSRapkiewicz, Pawel  */
10071abe55efSEd Tanous class EthernetCollection : public Node
10081abe55efSEd Tanous {
10099391bb9cSRapkiewicz, Pawel   public:
10104a0cb85cSEd Tanous     template <typename CrowApp>
10111abe55efSEd Tanous     EthernetCollection(CrowApp &app) :
10124a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
10131abe55efSEd Tanous     {
1014588c3f0dSKowalski, Kamil         entityPrivileges = {
1015588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1016e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1017e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1018e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1019e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1020e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
10219391bb9cSRapkiewicz, Pawel     }
10229391bb9cSRapkiewicz, Pawel 
10239391bb9cSRapkiewicz, Pawel   private:
10249391bb9cSRapkiewicz, Pawel     /**
10259391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
10269391bb9cSRapkiewicz, Pawel      */
102755c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
10281abe55efSEd Tanous                const std::vector<std::string> &params) override
10291abe55efSEd Tanous     {
10300f74e643SEd Tanous         res.jsonValue["@odata.type"] =
10310f74e643SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
10320f74e643SEd Tanous         res.jsonValue["@odata.id"] =
10330f74e643SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
10340f74e643SEd Tanous         res.jsonValue["Name"] = "Ethernet Network Interface Collection";
10350f74e643SEd Tanous         res.jsonValue["Description"] =
10360f74e643SEd Tanous             "Collection of EthernetInterfaces for this Manager";
10374c9afe43SEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
10384a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
10391abe55efSEd Tanous         // preparation
1040f12894f8SJason M. Bills         getEthernetIfaceList(
10414c9afe43SEd Tanous             [asyncResp](
10424c9afe43SEd Tanous                 const bool &success,
10434c9afe43SEd Tanous                 const boost::container::flat_set<std::string> &iface_list) {
10444a0cb85cSEd Tanous                 if (!success)
10451abe55efSEd Tanous                 {
10464c9afe43SEd Tanous                     messages::internalError(asyncResp->res);
10474a0cb85cSEd Tanous                     return;
10484a0cb85cSEd Tanous                 }
10494a0cb85cSEd Tanous 
10504c9afe43SEd Tanous                 nlohmann::json &iface_array =
10514c9afe43SEd Tanous                     asyncResp->res.jsonValue["Members"];
10524a0cb85cSEd Tanous                 iface_array = nlohmann::json::array();
1053fda13ad2SSunitha Harish                 std::string tag = "_";
10544a0cb85cSEd Tanous                 for (const std::string &iface_item : iface_list)
10551abe55efSEd Tanous                 {
1056fda13ad2SSunitha Harish                     std::size_t found = iface_item.find(tag);
1057fda13ad2SSunitha Harish                     if (found == std::string::npos)
1058fda13ad2SSunitha Harish                     {
10594a0cb85cSEd Tanous                         iface_array.push_back(
10604a0cb85cSEd Tanous                             {{"@odata.id",
10614a0cb85cSEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
10624a0cb85cSEd Tanous                                   iface_item}});
10639391bb9cSRapkiewicz, Pawel                     }
1064fda13ad2SSunitha Harish                 }
10654a0cb85cSEd Tanous 
10664c9afe43SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
10674c9afe43SEd Tanous                     iface_array.size();
10684c9afe43SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
10694a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
10709391bb9cSRapkiewicz, Pawel             });
10719391bb9cSRapkiewicz, Pawel     }
10729391bb9cSRapkiewicz, Pawel };
10739391bb9cSRapkiewicz, Pawel 
10749391bb9cSRapkiewicz, Pawel /**
10759391bb9cSRapkiewicz, Pawel  * EthernetInterface derived class for delivering Ethernet Schema
10769391bb9cSRapkiewicz, Pawel  */
10771abe55efSEd Tanous class EthernetInterface : public Node
10781abe55efSEd Tanous {
10799391bb9cSRapkiewicz, Pawel   public:
10809391bb9cSRapkiewicz, Pawel     /*
10819391bb9cSRapkiewicz, Pawel      * Default Constructor
10829391bb9cSRapkiewicz, Pawel      */
10834a0cb85cSEd Tanous     template <typename CrowApp>
10841abe55efSEd Tanous     EthernetInterface(CrowApp &app) :
10854a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
10861abe55efSEd Tanous              std::string())
10871abe55efSEd Tanous     {
1088588c3f0dSKowalski, Kamil         entityPrivileges = {
1089588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1090e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1091e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1092e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1093e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1094e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
10959391bb9cSRapkiewicz, Pawel     }
10969391bb9cSRapkiewicz, Pawel 
1097e439f0f8SKowalski, Kamil   private:
1098bc0bd6e0SEd Tanous     void handleHostnamePatch(const std::string &hostname,
10994a0cb85cSEd Tanous                              const std::shared_ptr<AsyncResp> asyncResp)
11001abe55efSEd Tanous     {
1101ab6554f1SJoshi-Mansi         // SHOULD handle host names of up to 255 characters(RFC 1123)
1102ab6554f1SJoshi-Mansi         if (hostname.length() > 255)
1103ab6554f1SJoshi-Mansi         {
1104ab6554f1SJoshi-Mansi             messages::propertyValueFormatError(asyncResp->res, hostname,
1105ab6554f1SJoshi-Mansi                                                "HostName");
1106ab6554f1SJoshi-Mansi             return;
1107ab6554f1SJoshi-Mansi         }
1108bc0bd6e0SEd Tanous         crow::connections::systemBus->async_method_call(
1109bc0bd6e0SEd Tanous             [asyncResp](const boost::system::error_code ec) {
11104a0cb85cSEd Tanous                 if (ec)
11114a0cb85cSEd Tanous                 {
1112a08b46ccSJason M. Bills                     messages::internalError(asyncResp->res);
11131abe55efSEd Tanous                 }
1114bc0bd6e0SEd Tanous             },
1115bc0bd6e0SEd Tanous             "xyz.openbmc_project.Network",
1116bc0bd6e0SEd Tanous             "/xyz/openbmc_project/network/config",
1117bc0bd6e0SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
1118bc0bd6e0SEd Tanous             "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1119abf2add6SEd Tanous             std::variant<std::string>(hostname));
1120588c3f0dSKowalski, Kamil     }
1121588c3f0dSKowalski, Kamil 
1122ab6554f1SJoshi-Mansi     void handleDomainnamePatch(const std::string &ifaceId,
1123ab6554f1SJoshi-Mansi                                const std::string &domainname,
1124ab6554f1SJoshi-Mansi                                const std::shared_ptr<AsyncResp> asyncResp)
1125ab6554f1SJoshi-Mansi     {
1126ab6554f1SJoshi-Mansi         std::vector<std::string> vectorDomainname = {domainname};
1127ab6554f1SJoshi-Mansi         crow::connections::systemBus->async_method_call(
1128ab6554f1SJoshi-Mansi             [asyncResp](const boost::system::error_code ec) {
1129ab6554f1SJoshi-Mansi                 if (ec)
1130ab6554f1SJoshi-Mansi                 {
1131ab6554f1SJoshi-Mansi                     messages::internalError(asyncResp->res);
1132ab6554f1SJoshi-Mansi                 }
1133ab6554f1SJoshi-Mansi             },
1134ab6554f1SJoshi-Mansi             "xyz.openbmc_project.Network",
1135ab6554f1SJoshi-Mansi             "/xyz/openbmc_project/network/" + ifaceId,
1136ab6554f1SJoshi-Mansi             "org.freedesktop.DBus.Properties", "Set",
1137ab6554f1SJoshi-Mansi             "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1138ab6554f1SJoshi-Mansi             std::variant<std::vector<std::string>>(vectorDomainname));
1139ab6554f1SJoshi-Mansi     }
1140ab6554f1SJoshi-Mansi 
1141ab6554f1SJoshi-Mansi     void handleFqdnPatch(const std::string &ifaceId, const std::string &fqdn,
1142ab6554f1SJoshi-Mansi                          const std::shared_ptr<AsyncResp> asyncResp)
1143ab6554f1SJoshi-Mansi     {
1144ab6554f1SJoshi-Mansi         // Total length of FQDN must not exceed 255 characters(RFC 1035)
1145ab6554f1SJoshi-Mansi         if (fqdn.length() > 255)
1146ab6554f1SJoshi-Mansi         {
1147ab6554f1SJoshi-Mansi             messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1148ab6554f1SJoshi-Mansi             return;
1149ab6554f1SJoshi-Mansi         }
1150ab6554f1SJoshi-Mansi 
1151ab6554f1SJoshi-Mansi         size_t pos = fqdn.find('.');
1152ab6554f1SJoshi-Mansi         if (pos == std::string::npos)
1153ab6554f1SJoshi-Mansi         {
1154ab6554f1SJoshi-Mansi             messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1155ab6554f1SJoshi-Mansi             return;
1156ab6554f1SJoshi-Mansi         }
1157ab6554f1SJoshi-Mansi 
1158ab6554f1SJoshi-Mansi         std::string hostname;
1159ab6554f1SJoshi-Mansi         std::string domainname;
1160ab6554f1SJoshi-Mansi         domainname = (fqdn).substr(pos + 1);
1161ab6554f1SJoshi-Mansi         hostname = (fqdn).substr(0, pos);
1162ab6554f1SJoshi-Mansi 
1163ab6554f1SJoshi-Mansi         if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1164ab6554f1SJoshi-Mansi         {
1165ab6554f1SJoshi-Mansi             messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1166ab6554f1SJoshi-Mansi             return;
1167ab6554f1SJoshi-Mansi         }
1168ab6554f1SJoshi-Mansi 
1169ab6554f1SJoshi-Mansi         handleHostnamePatch(hostname, asyncResp);
1170ab6554f1SJoshi-Mansi         handleDomainnamePatch(ifaceId, domainname, asyncResp);
1171ab6554f1SJoshi-Mansi     }
1172ab6554f1SJoshi-Mansi 
1173ab6554f1SJoshi-Mansi     bool isHostnameValid(const std::string &hostname)
1174ab6554f1SJoshi-Mansi     {
1175ab6554f1SJoshi-Mansi         // A valid host name can never have the dotted-decimal form (RFC 1123)
1176ab6554f1SJoshi-Mansi         if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1177ab6554f1SJoshi-Mansi         {
1178ab6554f1SJoshi-Mansi             return false;
1179ab6554f1SJoshi-Mansi         }
1180ab6554f1SJoshi-Mansi         // Each label(hostname/subdomains) within a valid FQDN
1181ab6554f1SJoshi-Mansi         // MUST handle host names of up to 63 characters (RFC 1123)
1182ab6554f1SJoshi-Mansi         // labels cannot start or end with hyphens (RFC 952)
1183ab6554f1SJoshi-Mansi         // labels can start with numbers (RFC 1123)
1184ab6554f1SJoshi-Mansi         const std::regex pattern(
1185ab6554f1SJoshi-Mansi             "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1186ab6554f1SJoshi-Mansi 
1187ab6554f1SJoshi-Mansi         return std::regex_match(hostname, pattern);
1188ab6554f1SJoshi-Mansi     }
1189ab6554f1SJoshi-Mansi 
1190ab6554f1SJoshi-Mansi     bool isDomainnameValid(const std::string &domainname)
1191ab6554f1SJoshi-Mansi     {
1192ab6554f1SJoshi-Mansi         // Can have multiple subdomains
1193ab6554f1SJoshi-Mansi         // Top Level Domain's min length is 2 character
1194ab6554f1SJoshi-Mansi         const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]"
1195ab6554f1SJoshi-Mansi                                  "{1,30}\\.)*[a-zA-Z]{2,}$");
1196ab6554f1SJoshi-Mansi 
1197ab6554f1SJoshi-Mansi         return std::regex_match(domainname, pattern);
1198ab6554f1SJoshi-Mansi     }
1199ab6554f1SJoshi-Mansi 
1200d577665bSRatan Gupta     void handleMACAddressPatch(const std::string &ifaceId,
1201d577665bSRatan Gupta                                const std::string &macAddress,
1202d577665bSRatan Gupta                                const std::shared_ptr<AsyncResp> &asyncResp)
1203d577665bSRatan Gupta     {
1204d577665bSRatan Gupta         crow::connections::systemBus->async_method_call(
1205d577665bSRatan Gupta             [asyncResp, macAddress](const boost::system::error_code ec) {
1206d577665bSRatan Gupta                 if (ec)
1207d577665bSRatan Gupta                 {
1208d577665bSRatan Gupta                     messages::internalError(asyncResp->res);
1209d577665bSRatan Gupta                     return;
1210d577665bSRatan Gupta                 }
1211d577665bSRatan Gupta             },
1212d577665bSRatan Gupta             "xyz.openbmc_project.Network",
1213d577665bSRatan Gupta             "/xyz/openbmc_project/network/" + ifaceId,
1214d577665bSRatan Gupta             "org.freedesktop.DBus.Properties", "Set",
1215d577665bSRatan Gupta             "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1216d577665bSRatan Gupta             std::variant<std::string>(macAddress));
1217d577665bSRatan Gupta     }
1218286b9118SJohnathan Mantey 
1219da131a9aSJennifer Lee     void setDHCPEnabled(const std::string &ifaceId,
12201f8c7b5dSJohnathan Mantey                         const std::string &propertyName, const bool v4Value,
12211f8c7b5dSJohnathan Mantey                         const bool v6Value,
1222da131a9aSJennifer Lee                         const std::shared_ptr<AsyncResp> asyncResp)
1223da131a9aSJennifer Lee     {
12241f8c7b5dSJohnathan Mantey         const std::string dhcp = GetDHCPEnabledEnumeration(v4Value, v6Value);
1225da131a9aSJennifer Lee         crow::connections::systemBus->async_method_call(
1226da131a9aSJennifer Lee             [asyncResp](const boost::system::error_code ec) {
1227da131a9aSJennifer Lee                 if (ec)
1228da131a9aSJennifer Lee                 {
1229da131a9aSJennifer Lee                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1230da131a9aSJennifer Lee                     messages::internalError(asyncResp->res);
1231da131a9aSJennifer Lee                     return;
1232da131a9aSJennifer Lee                 }
1233da131a9aSJennifer Lee             },
1234da131a9aSJennifer Lee             "xyz.openbmc_project.Network",
1235da131a9aSJennifer Lee             "/xyz/openbmc_project/network/" + ifaceId,
1236da131a9aSJennifer Lee             "org.freedesktop.DBus.Properties", "Set",
1237da131a9aSJennifer Lee             "xyz.openbmc_project.Network.EthernetInterface", propertyName,
12381f8c7b5dSJohnathan Mantey             std::variant<std::string>{dhcp});
1239da131a9aSJennifer Lee     }
12401f8c7b5dSJohnathan Mantey 
1241eeedda23SJohnathan Mantey     void setEthernetInterfaceBoolProperty(
1242eeedda23SJohnathan Mantey         const std::string &ifaceId, const std::string &propertyName,
1243eeedda23SJohnathan Mantey         const bool &value, const std::shared_ptr<AsyncResp> asyncResp)
1244eeedda23SJohnathan Mantey     {
1245eeedda23SJohnathan Mantey         crow::connections::systemBus->async_method_call(
1246eeedda23SJohnathan Mantey             [asyncResp](const boost::system::error_code ec) {
1247eeedda23SJohnathan Mantey                 if (ec)
1248eeedda23SJohnathan Mantey                 {
1249eeedda23SJohnathan Mantey                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1250eeedda23SJohnathan Mantey                     messages::internalError(asyncResp->res);
1251eeedda23SJohnathan Mantey                     return;
1252eeedda23SJohnathan Mantey                 }
1253eeedda23SJohnathan Mantey             },
1254eeedda23SJohnathan Mantey             "xyz.openbmc_project.Network",
1255eeedda23SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
1256eeedda23SJohnathan Mantey             "org.freedesktop.DBus.Properties", "Set",
1257eeedda23SJohnathan Mantey             "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1258eeedda23SJohnathan Mantey             std::variant<bool>{value});
1259eeedda23SJohnathan Mantey     }
1260eeedda23SJohnathan Mantey 
1261da131a9aSJennifer Lee     void setDHCPv4Config(const std::string &propertyName, const bool &value,
1262da131a9aSJennifer Lee                          const std::shared_ptr<AsyncResp> asyncResp)
1263da131a9aSJennifer Lee     {
1264da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1265da131a9aSJennifer Lee         crow::connections::systemBus->async_method_call(
1266da131a9aSJennifer Lee             [asyncResp](const boost::system::error_code ec) {
1267da131a9aSJennifer Lee                 if (ec)
1268da131a9aSJennifer Lee                 {
1269da131a9aSJennifer Lee                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1270da131a9aSJennifer Lee                     messages::internalError(asyncResp->res);
1271da131a9aSJennifer Lee                     return;
1272da131a9aSJennifer Lee                 }
1273da131a9aSJennifer Lee             },
1274da131a9aSJennifer Lee             "xyz.openbmc_project.Network",
1275da131a9aSJennifer Lee             "/xyz/openbmc_project/network/config/dhcp",
1276da131a9aSJennifer Lee             "org.freedesktop.DBus.Properties", "Set",
1277da131a9aSJennifer Lee             "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1278da131a9aSJennifer Lee             std::variant<bool>{value});
1279da131a9aSJennifer Lee     }
1280d577665bSRatan Gupta 
12811f8c7b5dSJohnathan Mantey     void handleDHCPPatch(const std::string &ifaceId,
12821f8c7b5dSJohnathan Mantey                          const EthernetInterfaceData &ethData,
12831f8c7b5dSJohnathan Mantey                          DHCPParameters v4dhcpParms, DHCPParameters v6dhcpParms,
1284da131a9aSJennifer Lee                          const std::shared_ptr<AsyncResp> asyncResp)
1285da131a9aSJennifer Lee     {
12861f8c7b5dSJohnathan Mantey         bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
12871f8c7b5dSJohnathan Mantey         bool ipv6Active =
12881f8c7b5dSJohnathan Mantey             translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1289da131a9aSJennifer Lee 
12901f8c7b5dSJohnathan Mantey         bool nextv4DHCPState =
12911f8c7b5dSJohnathan Mantey             v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
12921f8c7b5dSJohnathan Mantey 
12931f8c7b5dSJohnathan Mantey         bool nextv6DHCPState{};
12941f8c7b5dSJohnathan Mantey         if (v6dhcpParms.dhcpv6OperatingMode)
1295da131a9aSJennifer Lee         {
12961f8c7b5dSJohnathan Mantey             if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
12971f8c7b5dSJohnathan Mantey                 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
12981f8c7b5dSJohnathan Mantey                 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
12991f8c7b5dSJohnathan Mantey             {
13001f8c7b5dSJohnathan Mantey                 messages::propertyValueFormatError(
13011f8c7b5dSJohnathan Mantey                     asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
13021f8c7b5dSJohnathan Mantey                     "OperatingMode");
1303da131a9aSJennifer Lee                 return;
1304da131a9aSJennifer Lee             }
13051f8c7b5dSJohnathan Mantey             nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
13061f8c7b5dSJohnathan Mantey         }
13071f8c7b5dSJohnathan Mantey         else
1308da131a9aSJennifer Lee         {
13091f8c7b5dSJohnathan Mantey             nextv6DHCPState = ipv6Active;
13101f8c7b5dSJohnathan Mantey         }
13111f8c7b5dSJohnathan Mantey 
13121f8c7b5dSJohnathan Mantey         bool nextDNS{};
13131f8c7b5dSJohnathan Mantey         if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
13141f8c7b5dSJohnathan Mantey         {
13151f8c7b5dSJohnathan Mantey             if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
13161f8c7b5dSJohnathan Mantey             {
13171f8c7b5dSJohnathan Mantey                 messages::generalError(asyncResp->res);
13181f8c7b5dSJohnathan Mantey                 return;
13191f8c7b5dSJohnathan Mantey             }
13201f8c7b5dSJohnathan Mantey             nextDNS = *v4dhcpParms.useDNSServers;
13211f8c7b5dSJohnathan Mantey         }
13221f8c7b5dSJohnathan Mantey         else if (v4dhcpParms.useDNSServers)
13231f8c7b5dSJohnathan Mantey         {
13241f8c7b5dSJohnathan Mantey             nextDNS = *v4dhcpParms.useDNSServers;
13251f8c7b5dSJohnathan Mantey         }
13261f8c7b5dSJohnathan Mantey         else if (v6dhcpParms.useDNSServers)
13271f8c7b5dSJohnathan Mantey         {
13281f8c7b5dSJohnathan Mantey             nextDNS = *v6dhcpParms.useDNSServers;
13291f8c7b5dSJohnathan Mantey         }
13301f8c7b5dSJohnathan Mantey         else
13311f8c7b5dSJohnathan Mantey         {
13321f8c7b5dSJohnathan Mantey             nextDNS = ethData.DNSEnabled;
13331f8c7b5dSJohnathan Mantey         }
13341f8c7b5dSJohnathan Mantey 
13351f8c7b5dSJohnathan Mantey         bool nextNTP{};
13361f8c7b5dSJohnathan Mantey         if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
13371f8c7b5dSJohnathan Mantey         {
13381f8c7b5dSJohnathan Mantey             if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
13391f8c7b5dSJohnathan Mantey             {
13401f8c7b5dSJohnathan Mantey                 messages::generalError(asyncResp->res);
13411f8c7b5dSJohnathan Mantey                 return;
13421f8c7b5dSJohnathan Mantey             }
13431f8c7b5dSJohnathan Mantey             nextNTP = *v4dhcpParms.useNTPServers;
13441f8c7b5dSJohnathan Mantey         }
13451f8c7b5dSJohnathan Mantey         else if (v4dhcpParms.useNTPServers)
13461f8c7b5dSJohnathan Mantey         {
13471f8c7b5dSJohnathan Mantey             nextNTP = *v4dhcpParms.useNTPServers;
13481f8c7b5dSJohnathan Mantey         }
13491f8c7b5dSJohnathan Mantey         else if (v6dhcpParms.useNTPServers)
13501f8c7b5dSJohnathan Mantey         {
13511f8c7b5dSJohnathan Mantey             nextNTP = *v6dhcpParms.useNTPServers;
13521f8c7b5dSJohnathan Mantey         }
13531f8c7b5dSJohnathan Mantey         else
13541f8c7b5dSJohnathan Mantey         {
13551f8c7b5dSJohnathan Mantey             nextNTP = ethData.NTPEnabled;
13561f8c7b5dSJohnathan Mantey         }
13571f8c7b5dSJohnathan Mantey 
13581f8c7b5dSJohnathan Mantey         bool nextUseDomain{};
13591f8c7b5dSJohnathan Mantey         if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
13601f8c7b5dSJohnathan Mantey         {
13611f8c7b5dSJohnathan Mantey             if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
13621f8c7b5dSJohnathan Mantey             {
13631f8c7b5dSJohnathan Mantey                 messages::generalError(asyncResp->res);
13641f8c7b5dSJohnathan Mantey                 return;
13651f8c7b5dSJohnathan Mantey             }
13661f8c7b5dSJohnathan Mantey             nextUseDomain = *v4dhcpParms.useUseDomainName;
13671f8c7b5dSJohnathan Mantey         }
13681f8c7b5dSJohnathan Mantey         else if (v4dhcpParms.useUseDomainName)
13691f8c7b5dSJohnathan Mantey         {
13701f8c7b5dSJohnathan Mantey             nextUseDomain = *v4dhcpParms.useUseDomainName;
13711f8c7b5dSJohnathan Mantey         }
13721f8c7b5dSJohnathan Mantey         else if (v6dhcpParms.useUseDomainName)
13731f8c7b5dSJohnathan Mantey         {
13741f8c7b5dSJohnathan Mantey             nextUseDomain = *v6dhcpParms.useUseDomainName;
13751f8c7b5dSJohnathan Mantey         }
13761f8c7b5dSJohnathan Mantey         else
13771f8c7b5dSJohnathan Mantey         {
13781f8c7b5dSJohnathan Mantey             nextUseDomain = ethData.HostNameEnabled;
13791f8c7b5dSJohnathan Mantey         }
13801f8c7b5dSJohnathan Mantey 
1381da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
13821f8c7b5dSJohnathan Mantey         setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
13831f8c7b5dSJohnathan Mantey                        asyncResp);
1384da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << "set DNSEnabled...";
13851f8c7b5dSJohnathan Mantey         setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1386da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << "set NTPEnabled...";
13871f8c7b5dSJohnathan Mantey         setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
13881f8c7b5dSJohnathan Mantey         BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
13891f8c7b5dSJohnathan Mantey         setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1390da131a9aSJennifer Lee     }
139101784826SJohnathan Mantey 
139201784826SJohnathan Mantey     boost::container::flat_set<IPv4AddressData>::const_iterator
139301784826SJohnathan Mantey         GetNextStaticIPEntry(
139401784826SJohnathan Mantey             boost::container::flat_set<IPv4AddressData>::const_iterator head,
139501784826SJohnathan Mantey             boost::container::flat_set<IPv4AddressData>::const_iterator end)
139601784826SJohnathan Mantey     {
139701784826SJohnathan Mantey         for (; head != end; head++)
139801784826SJohnathan Mantey         {
139901784826SJohnathan Mantey             if (head->origin == "Static")
140001784826SJohnathan Mantey             {
140101784826SJohnathan Mantey                 return head;
140201784826SJohnathan Mantey             }
140301784826SJohnathan Mantey         }
140401784826SJohnathan Mantey         return end;
140501784826SJohnathan Mantey     }
140601784826SJohnathan Mantey 
140701784826SJohnathan Mantey     boost::container::flat_set<IPv6AddressData>::const_iterator
140801784826SJohnathan Mantey         GetNextStaticIPEntry(
140901784826SJohnathan Mantey             boost::container::flat_set<IPv6AddressData>::const_iterator head,
141001784826SJohnathan Mantey             boost::container::flat_set<IPv6AddressData>::const_iterator end)
141101784826SJohnathan Mantey     {
141201784826SJohnathan Mantey         for (; head != end; head++)
141301784826SJohnathan Mantey         {
141401784826SJohnathan Mantey             if (head->origin == "Static")
141501784826SJohnathan Mantey             {
141601784826SJohnathan Mantey                 return head;
141701784826SJohnathan Mantey             }
141801784826SJohnathan Mantey         }
141901784826SJohnathan Mantey         return end;
142001784826SJohnathan Mantey     }
142101784826SJohnathan Mantey 
1422d1d50814SRavi Teja     void handleIPv4StaticPatch(
1423f476acbfSRatan Gupta         const std::string &ifaceId, nlohmann::json &input,
142401784826SJohnathan Mantey         const boost::container::flat_set<IPv4AddressData> &ipv4Data,
14254a0cb85cSEd Tanous         const std::shared_ptr<AsyncResp> asyncResp)
14261abe55efSEd Tanous     {
142701784826SJohnathan Mantey         if ((!input.is_array()) || input.empty())
1428f476acbfSRatan Gupta         {
1429f476acbfSRatan Gupta             messages::propertyValueTypeError(asyncResp->res, input.dump(),
1430d1d50814SRavi Teja                                              "IPv4StaticAddresses");
1431f476acbfSRatan Gupta             return;
1432f476acbfSRatan Gupta         }
1433f476acbfSRatan Gupta 
1434271584abSEd Tanous         unsigned entryIdx = 1;
143501784826SJohnathan Mantey         // Find the first static IP address currently active on the NIC and
143601784826SJohnathan Mantey         // match it to the first JSON element in the IPv4StaticAddresses array.
143701784826SJohnathan Mantey         // Match each subsequent JSON element to the next static IP programmed
143801784826SJohnathan Mantey         // into the NIC.
143901784826SJohnathan Mantey         boost::container::flat_set<IPv4AddressData>::const_iterator NICIPentry =
144001784826SJohnathan Mantey             GetNextStaticIPEntry(ipv4Data.cbegin(), ipv4Data.cend());
144101784826SJohnathan Mantey 
1442537174c4SEd Tanous         for (nlohmann::json &thisJson : input)
14431abe55efSEd Tanous         {
14444a0cb85cSEd Tanous             std::string pathString =
1445d1d50814SRavi Teja                 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1446179db1d7SKowalski, Kamil 
144701784826SJohnathan Mantey             if (!thisJson.is_null() && !thisJson.empty())
1448f476acbfSRatan Gupta             {
1449537174c4SEd Tanous                 std::optional<std::string> address;
1450537174c4SEd Tanous                 std::optional<std::string> subnetMask;
1451537174c4SEd Tanous                 std::optional<std::string> gateway;
1452537174c4SEd Tanous 
1453537174c4SEd Tanous                 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
14547e27d832SJohnathan Mantey                                          address, "SubnetMask", subnetMask,
14557e27d832SJohnathan Mantey                                          "Gateway", gateway))
1456537174c4SEd Tanous                 {
145701784826SJohnathan Mantey                     messages::propertyValueFormatError(
145801784826SJohnathan Mantey                         asyncResp->res, thisJson.dump(), pathString);
1459537174c4SEd Tanous                     return;
1460179db1d7SKowalski, Kamil                 }
1461179db1d7SKowalski, Kamil 
146201784826SJohnathan Mantey                 // Find the address/subnet/gateway values. Any values that are
146301784826SJohnathan Mantey                 // not explicitly provided are assumed to be unmodified from the
146401784826SJohnathan Mantey                 // current state of the interface. Merge existing state into the
146501784826SJohnathan Mantey                 // current request.
1466271584abSEd Tanous                 const std::string *addr = nullptr;
1467271584abSEd Tanous                 const std::string *gw = nullptr;
146801784826SJohnathan Mantey                 uint8_t prefixLength = 0;
146901784826SJohnathan Mantey                 bool errorInEntry = false;
1470537174c4SEd Tanous                 if (address)
14711abe55efSEd Tanous                 {
147201784826SJohnathan Mantey                     if (ipv4VerifyIpAndGetBitcount(*address))
14731abe55efSEd Tanous                     {
147401784826SJohnathan Mantey                         addr = &(*address);
14754a0cb85cSEd Tanous                     }
147601784826SJohnathan Mantey                     else
147701784826SJohnathan Mantey                     {
147801784826SJohnathan Mantey                         messages::propertyValueFormatError(
147901784826SJohnathan Mantey                             asyncResp->res, *address, pathString + "/Address");
148001784826SJohnathan Mantey                         errorInEntry = true;
148101784826SJohnathan Mantey                     }
148201784826SJohnathan Mantey                 }
148301784826SJohnathan Mantey                 else if (NICIPentry != ipv4Data.cend())
148401784826SJohnathan Mantey                 {
148501784826SJohnathan Mantey                     addr = &(NICIPentry->address);
148601784826SJohnathan Mantey                 }
148701784826SJohnathan Mantey                 else
148801784826SJohnathan Mantey                 {
148901784826SJohnathan Mantey                     messages::propertyMissing(asyncResp->res,
149001784826SJohnathan Mantey                                               pathString + "/Address");
149101784826SJohnathan Mantey                     errorInEntry = true;
14924a0cb85cSEd Tanous                 }
14934a0cb85cSEd Tanous 
1494537174c4SEd Tanous                 if (subnetMask)
14954a0cb85cSEd Tanous                 {
1496537174c4SEd Tanous                     if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
14974a0cb85cSEd Tanous                     {
1498f12894f8SJason M. Bills                         messages::propertyValueFormatError(
1499537174c4SEd Tanous                             asyncResp->res, *subnetMask,
15004a0cb85cSEd Tanous                             pathString + "/SubnetMask");
150101784826SJohnathan Mantey                         errorInEntry = true;
15024a0cb85cSEd Tanous                     }
15034a0cb85cSEd Tanous                 }
150401784826SJohnathan Mantey                 else if (NICIPentry != ipv4Data.cend())
15054a0cb85cSEd Tanous                 {
150601784826SJohnathan Mantey                     if (!ipv4VerifyIpAndGetBitcount(NICIPentry->netmask,
150701784826SJohnathan Mantey                                                     &prefixLength))
15084a0cb85cSEd Tanous                     {
150901784826SJohnathan Mantey                         messages::propertyValueFormatError(
151001784826SJohnathan Mantey                             asyncResp->res, NICIPentry->netmask,
151101784826SJohnathan Mantey                             pathString + "/SubnetMask");
151201784826SJohnathan Mantey                         errorInEntry = true;
15134a0cb85cSEd Tanous                     }
15144a0cb85cSEd Tanous                 }
15151abe55efSEd Tanous                 else
15161abe55efSEd Tanous                 {
151701784826SJohnathan Mantey                     messages::propertyMissing(asyncResp->res,
151801784826SJohnathan Mantey                                               pathString + "/SubnetMask");
151901784826SJohnathan Mantey                     errorInEntry = true;
152001784826SJohnathan Mantey                 }
152101784826SJohnathan Mantey 
152201784826SJohnathan Mantey                 if (gateway)
152301784826SJohnathan Mantey                 {
152401784826SJohnathan Mantey                     if (ipv4VerifyIpAndGetBitcount(*gateway))
152501784826SJohnathan Mantey                     {
152601784826SJohnathan Mantey                         gw = &(*gateway);
152701784826SJohnathan Mantey                     }
152801784826SJohnathan Mantey                     else
152901784826SJohnathan Mantey                     {
153001784826SJohnathan Mantey                         messages::propertyValueFormatError(
153101784826SJohnathan Mantey                             asyncResp->res, *gateway, pathString + "/Gateway");
153201784826SJohnathan Mantey                         errorInEntry = true;
153301784826SJohnathan Mantey                     }
153401784826SJohnathan Mantey                 }
153501784826SJohnathan Mantey                 else if (NICIPentry != ipv4Data.cend())
153601784826SJohnathan Mantey                 {
153701784826SJohnathan Mantey                     gw = &NICIPentry->gateway;
153801784826SJohnathan Mantey                 }
153901784826SJohnathan Mantey                 else
15401abe55efSEd Tanous                 {
1541a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
15424a0cb85cSEd Tanous                                               pathString + "/Gateway");
154301784826SJohnathan Mantey                     errorInEntry = true;
15444a0cb85cSEd Tanous                 }
15454a0cb85cSEd Tanous 
154601784826SJohnathan Mantey                 if (errorInEntry)
15471abe55efSEd Tanous                 {
154801784826SJohnathan Mantey                     return;
15494a0cb85cSEd Tanous                 }
15504a0cb85cSEd Tanous 
155101784826SJohnathan Mantey                 if (NICIPentry != ipv4Data.cend())
15521abe55efSEd Tanous                 {
1553271584abSEd Tanous                     if (gw != nullptr || addr != nullptr)
1554271584abSEd Tanous                     {
1555271584abSEd Tanous                         // Shouldn't be possible based on errorInEntry, but
1556271584abSEd Tanous                         // it flags -wmaybe-uninitialized in the compiler,
1557271584abSEd Tanous                         // so defend against that
1558271584abSEd Tanous                         return;
1559271584abSEd Tanous                     }
156001784826SJohnathan Mantey                     deleteAndCreateIPv4(ifaceId, NICIPentry->id, prefixLength,
156101784826SJohnathan Mantey                                         *gw, *addr, asyncResp);
156201784826SJohnathan Mantey                     NICIPentry =
156301784826SJohnathan Mantey                         GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
1564588c3f0dSKowalski, Kamil                 }
156501784826SJohnathan Mantey                 else
156601784826SJohnathan Mantey                 {
156701784826SJohnathan Mantey                     createIPv4(ifaceId, entryIdx, prefixLength, *gateway,
156801784826SJohnathan Mantey                                *address, asyncResp);
15694a0cb85cSEd Tanous                 }
15704a0cb85cSEd Tanous                 entryIdx++;
15714a0cb85cSEd Tanous             }
157201784826SJohnathan Mantey             else
157301784826SJohnathan Mantey             {
157401784826SJohnathan Mantey                 if (NICIPentry == ipv4Data.cend())
157501784826SJohnathan Mantey                 {
157601784826SJohnathan Mantey                     // Requesting a DELETE/DO NOT MODIFY action for an item
157701784826SJohnathan Mantey                     // that isn't present on the eth(n) interface. Input JSON is
157801784826SJohnathan Mantey                     // in error, so bail out.
157901784826SJohnathan Mantey                     if (thisJson.is_null())
158001784826SJohnathan Mantey                     {
158101784826SJohnathan Mantey                         messages::resourceCannotBeDeleted(asyncResp->res);
158201784826SJohnathan Mantey                         return;
158301784826SJohnathan Mantey                     }
158401784826SJohnathan Mantey                     else
158501784826SJohnathan Mantey                     {
158601784826SJohnathan Mantey                         messages::propertyValueFormatError(
158701784826SJohnathan Mantey                             asyncResp->res, thisJson.dump(), pathString);
158801784826SJohnathan Mantey                         return;
158901784826SJohnathan Mantey                     }
159001784826SJohnathan Mantey                 }
159101784826SJohnathan Mantey 
159201784826SJohnathan Mantey                 if (thisJson.is_null())
159301784826SJohnathan Mantey                 {
159401784826SJohnathan Mantey                     deleteIPv4(ifaceId, NICIPentry->id, asyncResp);
159501784826SJohnathan Mantey                 }
159601784826SJohnathan Mantey                 if (NICIPentry != ipv4Data.cend())
159701784826SJohnathan Mantey                 {
159801784826SJohnathan Mantey                     NICIPentry =
159901784826SJohnathan Mantey                         GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
160001784826SJohnathan Mantey                 }
160101784826SJohnathan Mantey                 entryIdx++;
160201784826SJohnathan Mantey             }
160301784826SJohnathan Mantey         }
16044a0cb85cSEd Tanous     }
16054a0cb85cSEd Tanous 
1606f85837bfSRAJESWARAN THILLAIGOVINDAN     void handleStaticNameServersPatch(
1607f85837bfSRAJESWARAN THILLAIGOVINDAN         const std::string &ifaceId,
1608f85837bfSRAJESWARAN THILLAIGOVINDAN         const std::vector<std::string> &updatedStaticNameServers,
1609f85837bfSRAJESWARAN THILLAIGOVINDAN         const std::shared_ptr<AsyncResp> &asyncResp)
1610f85837bfSRAJESWARAN THILLAIGOVINDAN     {
1611f85837bfSRAJESWARAN THILLAIGOVINDAN         crow::connections::systemBus->async_method_call(
1612286b9118SJohnathan Mantey             [asyncResp](const boost::system::error_code ec) {
1613f85837bfSRAJESWARAN THILLAIGOVINDAN                 if (ec)
1614f85837bfSRAJESWARAN THILLAIGOVINDAN                 {
1615f85837bfSRAJESWARAN THILLAIGOVINDAN                     messages::internalError(asyncResp->res);
1616f85837bfSRAJESWARAN THILLAIGOVINDAN                     return;
1617f85837bfSRAJESWARAN THILLAIGOVINDAN                 }
1618f85837bfSRAJESWARAN THILLAIGOVINDAN             },
1619f85837bfSRAJESWARAN THILLAIGOVINDAN             "xyz.openbmc_project.Network",
1620f85837bfSRAJESWARAN THILLAIGOVINDAN             "/xyz/openbmc_project/network/" + ifaceId,
1621f85837bfSRAJESWARAN THILLAIGOVINDAN             "org.freedesktop.DBus.Properties", "Set",
16220f6efdc1Smanojkiran.eda@gmail.com             "xyz.openbmc_project.Network.EthernetInterface",
16230f6efdc1Smanojkiran.eda@gmail.com             "StaticNameServers",
1624f85837bfSRAJESWARAN THILLAIGOVINDAN             std::variant<std::vector<std::string>>{updatedStaticNameServers});
1625f85837bfSRAJESWARAN THILLAIGOVINDAN     }
1626f85837bfSRAJESWARAN THILLAIGOVINDAN 
1627e48c0fc5SRavi Teja     void handleIPv6StaticAddressesPatch(
1628e48c0fc5SRavi Teja         const std::string &ifaceId, nlohmann::json &input,
162901784826SJohnathan Mantey         const boost::container::flat_set<IPv6AddressData> &ipv6Data,
1630e48c0fc5SRavi Teja         const std::shared_ptr<AsyncResp> asyncResp)
1631e48c0fc5SRavi Teja     {
163201784826SJohnathan Mantey         if (!input.is_array() || input.empty())
1633e48c0fc5SRavi Teja         {
1634e48c0fc5SRavi Teja             messages::propertyValueTypeError(asyncResp->res, input.dump(),
1635e48c0fc5SRavi Teja                                              "IPv6StaticAddresses");
1636e48c0fc5SRavi Teja             return;
1637e48c0fc5SRavi Teja         }
1638271584abSEd Tanous         size_t entryIdx = 1;
163901784826SJohnathan Mantey         boost::container::flat_set<IPv6AddressData>::const_iterator NICIPentry =
164001784826SJohnathan Mantey             GetNextStaticIPEntry(ipv6Data.cbegin(), ipv6Data.cend());
1641e48c0fc5SRavi Teja         for (nlohmann::json &thisJson : input)
1642e48c0fc5SRavi Teja         {
1643e48c0fc5SRavi Teja             std::string pathString =
1644e48c0fc5SRavi Teja                 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1645e48c0fc5SRavi Teja 
164601784826SJohnathan Mantey             if (!thisJson.is_null() && !thisJson.empty())
1647e48c0fc5SRavi Teja             {
1648e48c0fc5SRavi Teja                 std::optional<std::string> address;
1649e48c0fc5SRavi Teja                 std::optional<uint8_t> prefixLength;
1650e48c0fc5SRavi Teja 
1651e48c0fc5SRavi Teja                 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1652e48c0fc5SRavi Teja                                          address, "PrefixLength", prefixLength))
1653e48c0fc5SRavi Teja                 {
165401784826SJohnathan Mantey                     messages::propertyValueFormatError(
165501784826SJohnathan Mantey                         asyncResp->res, thisJson.dump(), pathString);
1656e48c0fc5SRavi Teja                     return;
1657e48c0fc5SRavi Teja                 }
1658e48c0fc5SRavi Teja 
165901784826SJohnathan Mantey                 const std::string *addr;
166001784826SJohnathan Mantey                 uint8_t prefix;
166101784826SJohnathan Mantey 
166201784826SJohnathan Mantey                 // Find the address and prefixLength values. Any values that are
166301784826SJohnathan Mantey                 // not explicitly provided are assumed to be unmodified from the
166401784826SJohnathan Mantey                 // current state of the interface. Merge existing state into the
166501784826SJohnathan Mantey                 // current request.
1666e48c0fc5SRavi Teja                 if (address)
1667e48c0fc5SRavi Teja                 {
166801784826SJohnathan Mantey                     addr = &(*address);
1669e48c0fc5SRavi Teja                 }
167001784826SJohnathan Mantey                 else if (NICIPentry != ipv6Data.end())
167101784826SJohnathan Mantey                 {
167201784826SJohnathan Mantey                     addr = &(NICIPentry->address);
167301784826SJohnathan Mantey                 }
167401784826SJohnathan Mantey                 else
167501784826SJohnathan Mantey                 {
167601784826SJohnathan Mantey                     messages::propertyMissing(asyncResp->res,
167701784826SJohnathan Mantey                                               pathString + "/Address");
167801784826SJohnathan Mantey                     return;
1679e48c0fc5SRavi Teja                 }
1680e48c0fc5SRavi Teja 
1681e48c0fc5SRavi Teja                 if (prefixLength)
1682e48c0fc5SRavi Teja                 {
168301784826SJohnathan Mantey                     prefix = *prefixLength;
168401784826SJohnathan Mantey                 }
168501784826SJohnathan Mantey                 else if (NICIPentry != ipv6Data.end())
1686e48c0fc5SRavi Teja                 {
168701784826SJohnathan Mantey                     prefix = NICIPentry->prefixLength;
1688e48c0fc5SRavi Teja                 }
1689e48c0fc5SRavi Teja                 else
1690e48c0fc5SRavi Teja                 {
1691e48c0fc5SRavi Teja                     messages::propertyMissing(asyncResp->res,
1692e48c0fc5SRavi Teja                                               pathString + "/PrefixLength");
169301784826SJohnathan Mantey                     return;
1694e48c0fc5SRavi Teja                 }
1695e48c0fc5SRavi Teja 
169601784826SJohnathan Mantey                 if (NICIPentry != ipv6Data.end())
1697e48c0fc5SRavi Teja                 {
169801784826SJohnathan Mantey                     deleteAndCreateIPv6(ifaceId, NICIPentry->id, prefix, *addr,
1699e48c0fc5SRavi Teja                                         asyncResp);
170001784826SJohnathan Mantey                     NICIPentry =
170101784826SJohnathan Mantey                         GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
170201784826SJohnathan Mantey                 }
170301784826SJohnathan Mantey                 else
170401784826SJohnathan Mantey                 {
170501784826SJohnathan Mantey                     createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1706e48c0fc5SRavi Teja                 }
1707e48c0fc5SRavi Teja                 entryIdx++;
1708e48c0fc5SRavi Teja             }
170901784826SJohnathan Mantey             else
171001784826SJohnathan Mantey             {
171101784826SJohnathan Mantey                 if (NICIPentry == ipv6Data.end())
171201784826SJohnathan Mantey                 {
171301784826SJohnathan Mantey                     // Requesting a DELETE/DO NOT MODIFY action for an item
171401784826SJohnathan Mantey                     // that isn't present on the eth(n) interface. Input JSON is
171501784826SJohnathan Mantey                     // in error, so bail out.
171601784826SJohnathan Mantey                     if (thisJson.is_null())
171701784826SJohnathan Mantey                     {
171801784826SJohnathan Mantey                         messages::resourceCannotBeDeleted(asyncResp->res);
171901784826SJohnathan Mantey                         return;
172001784826SJohnathan Mantey                     }
172101784826SJohnathan Mantey                     else
172201784826SJohnathan Mantey                     {
172301784826SJohnathan Mantey                         messages::propertyValueFormatError(
172401784826SJohnathan Mantey                             asyncResp->res, thisJson.dump(), pathString);
172501784826SJohnathan Mantey                         return;
172601784826SJohnathan Mantey                     }
172701784826SJohnathan Mantey                 }
172801784826SJohnathan Mantey 
172901784826SJohnathan Mantey                 if (thisJson.is_null())
173001784826SJohnathan Mantey                 {
173101784826SJohnathan Mantey                     deleteIPv6(ifaceId, NICIPentry->id, asyncResp);
173201784826SJohnathan Mantey                 }
173301784826SJohnathan Mantey                 if (NICIPentry != ipv6Data.cend())
173401784826SJohnathan Mantey                 {
173501784826SJohnathan Mantey                     NICIPentry =
173601784826SJohnathan Mantey                         GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
173701784826SJohnathan Mantey                 }
173801784826SJohnathan Mantey                 entryIdx++;
173901784826SJohnathan Mantey             }
174001784826SJohnathan Mantey         }
1741e48c0fc5SRavi Teja     }
1742e48c0fc5SRavi Teja 
17430f74e643SEd Tanous     void parseInterfaceData(
1744eeedda23SJohnathan Mantey         std::shared_ptr<AsyncResp> asyncResp, const std::string &iface_id,
17450f74e643SEd Tanous         const EthernetInterfaceData &ethData,
1746e48c0fc5SRavi Teja         const boost::container::flat_set<IPv4AddressData> &ipv4Data,
174701784826SJohnathan Mantey         const boost::container::flat_set<IPv6AddressData> &ipv6Data)
17484a0cb85cSEd Tanous     {
1749eeedda23SJohnathan Mantey         constexpr const std::array<const char *, 1> inventoryForEthernet = {
1750eeedda23SJohnathan Mantey             "xyz.openbmc_project.Inventory.Item.Ethernet"};
1751eeedda23SJohnathan Mantey 
1752eeedda23SJohnathan Mantey         nlohmann::json &json_response = asyncResp->res.jsonValue;
17534a0cb85cSEd Tanous         json_response["Id"] = iface_id;
17544a0cb85cSEd Tanous         json_response["@odata.id"] =
17554a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
1756eeedda23SJohnathan Mantey         json_response["InterfaceEnabled"] = ethData.nicEnabled;
1757eeedda23SJohnathan Mantey 
1758eeedda23SJohnathan Mantey         auto health = std::make_shared<HealthPopulate>(asyncResp);
1759eeedda23SJohnathan Mantey 
1760eeedda23SJohnathan Mantey         crow::connections::systemBus->async_method_call(
1761eeedda23SJohnathan Mantey             [health](const boost::system::error_code ec,
1762eeedda23SJohnathan Mantey                      std::vector<std::string> &resp) {
1763eeedda23SJohnathan Mantey                 if (ec)
1764029573d4SEd Tanous                 {
1765eeedda23SJohnathan Mantey                     return;
1766eeedda23SJohnathan Mantey                 }
1767eeedda23SJohnathan Mantey 
1768eeedda23SJohnathan Mantey                 health->inventory = std::move(resp);
1769eeedda23SJohnathan Mantey             },
1770eeedda23SJohnathan Mantey             "xyz.openbmc_project.ObjectMapper",
1771eeedda23SJohnathan Mantey             "/xyz/openbmc_project/object_mapper",
1772eeedda23SJohnathan Mantey             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1773eeedda23SJohnathan Mantey             int32_t(0), inventoryForEthernet);
1774eeedda23SJohnathan Mantey 
1775eeedda23SJohnathan Mantey         health->populate();
1776eeedda23SJohnathan Mantey 
1777eeedda23SJohnathan Mantey         if (ethData.nicEnabled)
1778eeedda23SJohnathan Mantey         {
1779eeedda23SJohnathan Mantey             json_response["LinkStatus"] = "LinkUp";
1780eeedda23SJohnathan Mantey             json_response["Status"]["State"] = "Enabled";
1781029573d4SEd Tanous         }
1782029573d4SEd Tanous         else
1783029573d4SEd Tanous         {
1784eeedda23SJohnathan Mantey             json_response["LinkStatus"] = "NoLink";
1785eeedda23SJohnathan Mantey             json_response["Status"]["State"] = "Disabled";
1786029573d4SEd Tanous         }
1787aa05fb27SJohnathan Mantey 
1788aa05fb27SJohnathan Mantey         json_response["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
17894a0cb85cSEd Tanous         json_response["SpeedMbps"] = ethData.speed;
17904a0cb85cSEd Tanous         json_response["MACAddress"] = ethData.mac_address;
17911f8c7b5dSJohnathan Mantey         json_response["DHCPv4"]["DHCPEnabled"] =
17921f8c7b5dSJohnathan Mantey             translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
17931f8c7b5dSJohnathan Mantey         json_response["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
17941f8c7b5dSJohnathan Mantey         json_response["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
17951f8c7b5dSJohnathan Mantey         json_response["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
17961f8c7b5dSJohnathan Mantey 
17971f8c7b5dSJohnathan Mantey         json_response["DHCPv6"]["OperatingMode"] =
17981f8c7b5dSJohnathan Mantey             translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
17991f8c7b5dSJohnathan Mantey                                                                    : "Disabled";
18001f8c7b5dSJohnathan Mantey         json_response["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
18011f8c7b5dSJohnathan Mantey         json_response["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
18021f8c7b5dSJohnathan Mantey         json_response["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
18032a133282Smanojkiraneda 
18044a0cb85cSEd Tanous         if (!ethData.hostname.empty())
18054a0cb85cSEd Tanous         {
18064a0cb85cSEd Tanous             json_response["HostName"] = ethData.hostname;
1807ab6554f1SJoshi-Mansi 
1808ab6554f1SJoshi-Mansi             // When domain name is empty then it means, that it is a network
1809ab6554f1SJoshi-Mansi             // without domain names, and the host name itself must be treated as
1810ab6554f1SJoshi-Mansi             // FQDN
1811ab6554f1SJoshi-Mansi             std::string FQDN = std::move(ethData.hostname);
1812d24bfc7aSJennifer Lee             if (!ethData.domainnames.empty())
1813d24bfc7aSJennifer Lee             {
1814ab6554f1SJoshi-Mansi                 FQDN += "." + ethData.domainnames[0];
1815d24bfc7aSJennifer Lee             }
1816ab6554f1SJoshi-Mansi             json_response["FQDN"] = FQDN;
18174a0cb85cSEd Tanous         }
18184a0cb85cSEd Tanous 
1819fda13ad2SSunitha Harish         json_response["VLANs"] = {
1820fda13ad2SSunitha Harish             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1821fda13ad2SSunitha Harish                               iface_id + "/VLANs"}};
1822fda13ad2SSunitha Harish 
18230f6efdc1Smanojkiran.eda@gmail.com         json_response["NameServers"] = ethData.nameServers;
18240f6efdc1Smanojkiran.eda@gmail.com         json_response["StaticNameServers"] = ethData.staticNameServers;
18254a0cb85cSEd Tanous 
18264a0cb85cSEd Tanous         nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
182701784826SJohnathan Mantey         nlohmann::json &ipv4_static_array =
182801784826SJohnathan Mantey             json_response["IPv4StaticAddresses"];
18294a0cb85cSEd Tanous         ipv4_array = nlohmann::json::array();
183001784826SJohnathan Mantey         ipv4_static_array = nlohmann::json::array();
18314a0cb85cSEd Tanous         for (auto &ipv4_config : ipv4Data)
18324a0cb85cSEd Tanous         {
1833fa5053a6SGunnar Mills 
1834fa5053a6SGunnar Mills             std::string gatewayStr = ipv4_config.gateway;
1835fa5053a6SGunnar Mills             if (gatewayStr.empty())
1836fa5053a6SGunnar Mills             {
1837fa5053a6SGunnar Mills                 gatewayStr = "0.0.0.0";
1838fa5053a6SGunnar Mills             }
1839fa5053a6SGunnar Mills 
18404a0cb85cSEd Tanous             ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
18414a0cb85cSEd Tanous                                   {"SubnetMask", ipv4_config.netmask},
1842029573d4SEd Tanous                                   {"Address", ipv4_config.address},
1843fa5053a6SGunnar Mills                                   {"Gateway", gatewayStr}});
184401784826SJohnathan Mantey             if (ipv4_config.origin == "Static")
1845d1d50814SRavi Teja             {
1846d1d50814SRavi Teja                 ipv4_static_array.push_back(
184701784826SJohnathan Mantey                     {{"AddressOrigin", ipv4_config.origin},
184801784826SJohnathan Mantey                      {"SubnetMask", ipv4_config.netmask},
184901784826SJohnathan Mantey                      {"Address", ipv4_config.address},
1850d1d50814SRavi Teja                      {"Gateway", gatewayStr}});
1851d1d50814SRavi Teja             }
185201784826SJohnathan Mantey         }
1853d1d50814SRavi Teja 
18549a6fc6feSRavi Teja         json_response["IPv6DefaultGateway"] = ethData.ipv6_default_gateway;
1855e48c0fc5SRavi Teja 
1856e48c0fc5SRavi Teja         nlohmann::json &ipv6_array = json_response["IPv6Addresses"];
185701784826SJohnathan Mantey         nlohmann::json &ipv6_static_array =
185801784826SJohnathan Mantey             json_response["IPv6StaticAddresses"];
1859e48c0fc5SRavi Teja         ipv6_array = nlohmann::json::array();
186001784826SJohnathan Mantey         ipv6_static_array = nlohmann::json::array();
1861e48c0fc5SRavi Teja         for (auto &ipv6_config : ipv6Data)
1862e48c0fc5SRavi Teja         {
1863e48c0fc5SRavi Teja             ipv6_array.push_back({{"Address", ipv6_config.address},
1864e48c0fc5SRavi Teja                                   {"PrefixLength", ipv6_config.prefixLength},
1865e48c0fc5SRavi Teja                                   {"AddressOrigin", ipv6_config.origin}});
186601784826SJohnathan Mantey             if (ipv6_config.origin == "Static")
1867e48c0fc5SRavi Teja             {
1868e48c0fc5SRavi Teja                 ipv6_static_array.push_back(
186901784826SJohnathan Mantey                     {{"Address", ipv6_config.address},
187001784826SJohnathan Mantey                      {"PrefixLength", ipv6_config.prefixLength},
187101784826SJohnathan Mantey                      {"AddressOrigin", ipv6_config.origin}});
187201784826SJohnathan Mantey             }
1873e48c0fc5SRavi Teja         }
1874588c3f0dSKowalski, Kamil     }
1875588c3f0dSKowalski, Kamil 
18769391bb9cSRapkiewicz, Pawel     /**
18779391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
18789391bb9cSRapkiewicz, Pawel      */
187955c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
18801abe55efSEd Tanous                const std::vector<std::string> &params) override
18811abe55efSEd Tanous     {
18824a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
18831abe55efSEd Tanous         if (params.size() != 1)
18841abe55efSEd Tanous         {
1885f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
18869391bb9cSRapkiewicz, Pawel             return;
18879391bb9cSRapkiewicz, Pawel         }
18889391bb9cSRapkiewicz, Pawel 
18894a0cb85cSEd Tanous         getEthernetIfaceData(
18904a0cb85cSEd Tanous             params[0],
18914a0cb85cSEd Tanous             [this, asyncResp, iface_id{std::string(params[0])}](
18924a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
1893e48c0fc5SRavi Teja                 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
189401784826SJohnathan Mantey                 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
18954a0cb85cSEd Tanous                 if (!success)
18961abe55efSEd Tanous                 {
18971abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
18981abe55efSEd Tanous                     // object, and other errors
1899f12894f8SJason M. Bills                     messages::resourceNotFound(asyncResp->res,
1900f12894f8SJason M. Bills                                                "EthernetInterface", iface_id);
19014a0cb85cSEd Tanous                     return;
19029391bb9cSRapkiewicz, Pawel                 }
19034c9afe43SEd Tanous 
19040f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
1905fda13ad2SSunitha Harish                     "#EthernetInterface.v1_4_1.EthernetInterface";
19060f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
19070f74e643SEd Tanous                 asyncResp->res.jsonValue["Description"] =
19080f74e643SEd Tanous                     "Management Network Interface";
19090f74e643SEd Tanous 
1910eeedda23SJohnathan Mantey                 parseInterfaceData(asyncResp, iface_id, ethData, ipv4Data,
1911eeedda23SJohnathan Mantey                                    ipv6Data);
19129391bb9cSRapkiewicz, Pawel             });
19139391bb9cSRapkiewicz, Pawel     }
19149391bb9cSRapkiewicz, Pawel 
191555c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
19161abe55efSEd Tanous                  const std::vector<std::string> &params) override
19171abe55efSEd Tanous     {
19184a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
19191abe55efSEd Tanous         if (params.size() != 1)
19201abe55efSEd Tanous         {
1921f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1922588c3f0dSKowalski, Kamil             return;
1923588c3f0dSKowalski, Kamil         }
1924588c3f0dSKowalski, Kamil 
19254a0cb85cSEd Tanous         const std::string &iface_id = params[0];
1926588c3f0dSKowalski, Kamil 
1927bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
1928ab6554f1SJoshi-Mansi         std::optional<std::string> fqdn;
1929d577665bSRatan Gupta         std::optional<std::string> macAddress;
19309a6fc6feSRavi Teja         std::optional<std::string> ipv6DefaultGateway;
1931d1d50814SRavi Teja         std::optional<nlohmann::json> ipv4StaticAddresses;
1932e48c0fc5SRavi Teja         std::optional<nlohmann::json> ipv6StaticAddresses;
1933f85837bfSRAJESWARAN THILLAIGOVINDAN         std::optional<std::vector<std::string>> staticNameServers;
1934da131a9aSJennifer Lee         std::optional<nlohmann::json> dhcpv4;
19351f8c7b5dSJohnathan Mantey         std::optional<nlohmann::json> dhcpv6;
1936eeedda23SJohnathan Mantey         std::optional<bool> interfaceEnabled;
19371f8c7b5dSJohnathan Mantey         DHCPParameters v4dhcpParms;
19381f8c7b5dSJohnathan Mantey         DHCPParameters v6dhcpParms;
19390627a2c7SEd Tanous 
19401f8c7b5dSJohnathan Mantey         if (!json_util::readJson(
1941ab6554f1SJoshi-Mansi                 req, res, "HostName", hostname, "FQDN", fqdn,
1942ab6554f1SJoshi-Mansi                 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1943ab6554f1SJoshi-Mansi                 macAddress, "StaticNameServers", staticNameServers,
1944ab6554f1SJoshi-Mansi                 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1945ab6554f1SJoshi-Mansi                 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1946ab6554f1SJoshi-Mansi                 "InterfaceEnabled", interfaceEnabled))
19471abe55efSEd Tanous         {
1948588c3f0dSKowalski, Kamil             return;
1949588c3f0dSKowalski, Kamil         }
1950da131a9aSJennifer Lee         if (dhcpv4)
1951da131a9aSJennifer Lee         {
19521f8c7b5dSJohnathan Mantey             if (!json_util::readJson(*dhcpv4, res, "DHCPEnabled",
19531f8c7b5dSJohnathan Mantey                                      v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
19541f8c7b5dSJohnathan Mantey                                      v4dhcpParms.useDNSServers, "UseNTPServers",
19551f8c7b5dSJohnathan Mantey                                      v4dhcpParms.useNTPServers, "UseDomainName",
19561f8c7b5dSJohnathan Mantey                                      v4dhcpParms.useUseDomainName))
19571f8c7b5dSJohnathan Mantey             {
19581f8c7b5dSJohnathan Mantey                 return;
19591f8c7b5dSJohnathan Mantey             }
19601f8c7b5dSJohnathan Mantey         }
19611f8c7b5dSJohnathan Mantey 
19621f8c7b5dSJohnathan Mantey         if (dhcpv6)
19631f8c7b5dSJohnathan Mantey         {
19641f8c7b5dSJohnathan Mantey             if (!json_util::readJson(*dhcpv6, res, "OperatingMode",
19651f8c7b5dSJohnathan Mantey                                      v6dhcpParms.dhcpv6OperatingMode,
19661f8c7b5dSJohnathan Mantey                                      "UseDNSServers", v6dhcpParms.useDNSServers,
19671f8c7b5dSJohnathan Mantey                                      "UseNTPServers", v6dhcpParms.useNTPServers,
19681f8c7b5dSJohnathan Mantey                                      "UseDomainName",
19691f8c7b5dSJohnathan Mantey                                      v6dhcpParms.useUseDomainName))
19701f8c7b5dSJohnathan Mantey             {
19711f8c7b5dSJohnathan Mantey                 return;
19721f8c7b5dSJohnathan Mantey             }
1973da131a9aSJennifer Lee         }
1974da131a9aSJennifer Lee 
197501784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
197601784826SJohnathan Mantey         // JSON preparation
19774a0cb85cSEd Tanous         getEthernetIfaceData(
19784a0cb85cSEd Tanous             iface_id,
1979fda13ad2SSunitha Harish             [this, asyncResp, iface_id, hostname = std::move(hostname),
1980ab6554f1SJoshi-Mansi              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1981d1d50814SRavi Teja              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
19829a6fc6feSRavi Teja              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1983e48c0fc5SRavi Teja              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
19841f8c7b5dSJohnathan Mantey              staticNameServers = std::move(staticNameServers),
19851f8c7b5dSJohnathan Mantey              dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
19861f8c7b5dSJohnathan Mantey              v4dhcpParms = std::move(v4dhcpParms),
1987eeedda23SJohnathan Mantey              v6dhcpParms = std::move(v6dhcpParms),
1988eeedda23SJohnathan Mantey              interfaceEnabled = std::move(interfaceEnabled)](
19894a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
1990e48c0fc5SRavi Teja                 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
199101784826SJohnathan Mantey                 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
19921abe55efSEd Tanous                 if (!success)
19931abe55efSEd Tanous                 {
1994588c3f0dSKowalski, Kamil                     // ... otherwise return error
19951abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
19961abe55efSEd Tanous                     // object, and other errors
1997fda13ad2SSunitha Harish                     messages::resourceNotFound(asyncResp->res,
1998fda13ad2SSunitha Harish                                                "Ethernet Interface", iface_id);
1999588c3f0dSKowalski, Kamil                     return;
2000588c3f0dSKowalski, Kamil                 }
2001588c3f0dSKowalski, Kamil 
20021f8c7b5dSJohnathan Mantey                 if (dhcpv4 || dhcpv6)
20031f8c7b5dSJohnathan Mantey                 {
20041f8c7b5dSJohnathan Mantey                     handleDHCPPatch(iface_id, ethData, std::move(v4dhcpParms),
20051f8c7b5dSJohnathan Mantey                                     std::move(v6dhcpParms), asyncResp);
20061f8c7b5dSJohnathan Mantey                 }
20071f8c7b5dSJohnathan Mantey 
20080627a2c7SEd Tanous                 if (hostname)
20091abe55efSEd Tanous                 {
20100627a2c7SEd Tanous                     handleHostnamePatch(*hostname, asyncResp);
20111abe55efSEd Tanous                 }
20120627a2c7SEd Tanous 
2013ab6554f1SJoshi-Mansi                 if (fqdn)
2014ab6554f1SJoshi-Mansi                 {
2015ab6554f1SJoshi-Mansi                     handleFqdnPatch(iface_id, *fqdn, asyncResp);
2016ab6554f1SJoshi-Mansi                 }
2017ab6554f1SJoshi-Mansi 
2018d577665bSRatan Gupta                 if (macAddress)
2019d577665bSRatan Gupta                 {
2020d577665bSRatan Gupta                     handleMACAddressPatch(iface_id, *macAddress, asyncResp);
2021d577665bSRatan Gupta                 }
2022d577665bSRatan Gupta 
2023d1d50814SRavi Teja                 if (ipv4StaticAddresses)
2024d1d50814SRavi Teja                 {
2025537174c4SEd Tanous                     // TODO(ed) for some reason the capture of ipv4Addresses
202601784826SJohnathan Mantey                     // above is returning a const value, not a non-const
202701784826SJohnathan Mantey                     // value. This doesn't really work for us, as we need to
202801784826SJohnathan Mantey                     // be able to efficiently move out the intermedia
202901784826SJohnathan Mantey                     // nlohmann::json objects. This makes a copy of the
203001784826SJohnathan Mantey                     // structure, and operates on that, but could be done
203101784826SJohnathan Mantey                     // more efficiently
2032d1d50814SRavi Teja                     nlohmann::json ipv4Static = std::move(*ipv4StaticAddresses);
203301784826SJohnathan Mantey                     handleIPv4StaticPatch(iface_id, ipv4Static, ipv4Data,
2034d1d50814SRavi Teja                                           asyncResp);
20351abe55efSEd Tanous                 }
20360627a2c7SEd Tanous 
2037f85837bfSRAJESWARAN THILLAIGOVINDAN                 if (staticNameServers)
2038f85837bfSRAJESWARAN THILLAIGOVINDAN                 {
2039f85837bfSRAJESWARAN THILLAIGOVINDAN                     handleStaticNameServersPatch(iface_id, *staticNameServers,
2040f85837bfSRAJESWARAN THILLAIGOVINDAN                                                  asyncResp);
2041f85837bfSRAJESWARAN THILLAIGOVINDAN                 }
20429a6fc6feSRavi Teja 
20439a6fc6feSRavi Teja                 if (ipv6DefaultGateway)
20449a6fc6feSRavi Teja                 {
20459a6fc6feSRavi Teja                     messages::propertyNotWritable(asyncResp->res,
20469a6fc6feSRavi Teja                                                   "IPv6DefaultGateway");
20479a6fc6feSRavi Teja                 }
2048e48c0fc5SRavi Teja 
2049e48c0fc5SRavi Teja                 if (ipv6StaticAddresses)
2050e48c0fc5SRavi Teja                 {
2051e48c0fc5SRavi Teja                     nlohmann::json ipv6Static = std::move(*ipv6StaticAddresses);
2052e48c0fc5SRavi Teja                     handleIPv6StaticAddressesPatch(iface_id, ipv6Static,
205301784826SJohnathan Mantey                                                    ipv6Data, asyncResp);
2054e48c0fc5SRavi Teja                 }
2055eeedda23SJohnathan Mantey 
2056eeedda23SJohnathan Mantey                 if (interfaceEnabled)
2057eeedda23SJohnathan Mantey                 {
2058eeedda23SJohnathan Mantey                     setEthernetInterfaceBoolProperty(
2059eeedda23SJohnathan Mantey                         iface_id, "NICEnabled", *interfaceEnabled, asyncResp);
2060eeedda23SJohnathan Mantey                 }
2061588c3f0dSKowalski, Kamil             });
2062588c3f0dSKowalski, Kamil     }
20639391bb9cSRapkiewicz, Pawel };
20649391bb9cSRapkiewicz, Pawel 
2065e439f0f8SKowalski, Kamil /**
20664a0cb85cSEd Tanous  * VlanNetworkInterface derived class for delivering VLANNetworkInterface
20674a0cb85cSEd Tanous  * Schema
2068e439f0f8SKowalski, Kamil  */
20691abe55efSEd Tanous class VlanNetworkInterface : public Node
20701abe55efSEd Tanous {
2071e439f0f8SKowalski, Kamil   public:
2072e439f0f8SKowalski, Kamil     /*
2073e439f0f8SKowalski, Kamil      * Default Constructor
2074e439f0f8SKowalski, Kamil      */
2075e439f0f8SKowalski, Kamil     template <typename CrowApp>
20761abe55efSEd Tanous     VlanNetworkInterface(CrowApp &app) :
20774a0cb85cSEd Tanous         Node(app,
2078*7af91514SGunnar Mills              "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/",
20791abe55efSEd Tanous              std::string(), std::string())
20801abe55efSEd Tanous     {
2081e439f0f8SKowalski, Kamil         entityPrivileges = {
2082e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
2083e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
2084e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2085e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2086e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2087e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2088e439f0f8SKowalski, Kamil     }
2089e439f0f8SKowalski, Kamil 
2090e439f0f8SKowalski, Kamil   private:
20910f74e643SEd Tanous     void parseInterfaceData(
20920f74e643SEd Tanous         nlohmann::json &json_response, const std::string &parent_iface_id,
20930f74e643SEd Tanous         const std::string &iface_id, const EthernetInterfaceData &ethData,
2094e48c0fc5SRavi Teja         const boost::container::flat_set<IPv4AddressData> &ipv4Data,
209501784826SJohnathan Mantey         const boost::container::flat_set<IPv6AddressData> &ipv6Data)
20961abe55efSEd Tanous     {
2097e439f0f8SKowalski, Kamil         // Fill out obvious data...
20984a0cb85cSEd Tanous         json_response["Id"] = iface_id;
20994a0cb85cSEd Tanous         json_response["@odata.id"] =
21004a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
21014a0cb85cSEd Tanous             "/VLANs/" + iface_id;
2102e439f0f8SKowalski, Kamil 
21034a0cb85cSEd Tanous         json_response["VLANEnable"] = true;
2104fda13ad2SSunitha Harish         if (!ethData.vlan_id.empty())
21054a0cb85cSEd Tanous         {
2106fda13ad2SSunitha Harish             json_response["VLANId"] = ethData.vlan_id.back();
21074a0cb85cSEd Tanous         }
2108e439f0f8SKowalski, Kamil     }
2109e439f0f8SKowalski, Kamil 
2110fda13ad2SSunitha Harish     bool verifyNames(const std::string &parent, const std::string &iface)
21111abe55efSEd Tanous     {
21121abe55efSEd Tanous         if (!boost::starts_with(iface, parent + "_"))
21131abe55efSEd Tanous         {
2114927a505aSKowalski, Kamil             return false;
21151abe55efSEd Tanous         }
21161abe55efSEd Tanous         else
21171abe55efSEd Tanous         {
2118927a505aSKowalski, Kamil             return true;
2119927a505aSKowalski, Kamil         }
2120927a505aSKowalski, Kamil     }
2121927a505aSKowalski, Kamil 
2122e439f0f8SKowalski, Kamil     /**
2123e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
2124e439f0f8SKowalski, Kamil      */
212555c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
21261abe55efSEd Tanous                const std::vector<std::string> &params) override
21271abe55efSEd Tanous     {
21284a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
21294a0cb85cSEd Tanous         // TODO(Pawel) this shall be parameterized call (two params) to get
2130e439f0f8SKowalski, Kamil         // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
2131e439f0f8SKowalski, Kamil         // Check if there is required param, truly entering this shall be
2132e439f0f8SKowalski, Kamil         // impossible.
21331abe55efSEd Tanous         if (params.size() != 2)
21341abe55efSEd Tanous         {
2135f12894f8SJason M. Bills             messages::internalError(res);
2136e439f0f8SKowalski, Kamil             res.end();
2137e439f0f8SKowalski, Kamil             return;
2138e439f0f8SKowalski, Kamil         }
2139e439f0f8SKowalski, Kamil 
21404a0cb85cSEd Tanous         const std::string &parent_iface_id = params[0];
21414a0cb85cSEd Tanous         const std::string &iface_id = params[1];
21420f74e643SEd Tanous         res.jsonValue["@odata.type"] =
21430f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
21440f74e643SEd Tanous         res.jsonValue["Name"] = "VLAN Network Interface";
2145e439f0f8SKowalski, Kamil 
2146fda13ad2SSunitha Harish         if (!verifyNames(parent_iface_id, iface_id))
21471abe55efSEd Tanous         {
2148a434f2bdSEd Tanous             return;
2149a434f2bdSEd Tanous         }
2150a434f2bdSEd Tanous 
215101784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
215201784826SJohnathan Mantey         // JSON preparation
21534a0cb85cSEd Tanous         getEthernetIfaceData(
2154fda13ad2SSunitha Harish             params[1],
2155fda13ad2SSunitha Harish             [this, asyncResp, parent_iface_id{std::string(params[0])},
2156fda13ad2SSunitha Harish              iface_id{std::string(params[1])}](
21574a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
2158e48c0fc5SRavi Teja                 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
215901784826SJohnathan Mantey                 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
2160fda13ad2SSunitha Harish                 if (success && ethData.vlan_id.size() != 0)
21611abe55efSEd Tanous                 {
21620f74e643SEd Tanous                     parseInterfaceData(asyncResp->res.jsonValue,
21630f74e643SEd Tanous                                        parent_iface_id, iface_id, ethData,
216401784826SJohnathan Mantey                                        ipv4Data, ipv6Data);
21651abe55efSEd Tanous                 }
21661abe55efSEd Tanous                 else
21671abe55efSEd Tanous                 {
2168e439f0f8SKowalski, Kamil                     // ... otherwise return error
21691abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
21701abe55efSEd Tanous                     // object, and other errors
2171f12894f8SJason M. Bills                     messages::resourceNotFound(
2172f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", iface_id);
2173e439f0f8SKowalski, Kamil                 }
2174e439f0f8SKowalski, Kamil             });
2175e439f0f8SKowalski, Kamil     }
2176e439f0f8SKowalski, Kamil 
217755c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
21781abe55efSEd Tanous                  const std::vector<std::string> &params) override
21791abe55efSEd Tanous     {
21804a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
21811abe55efSEd Tanous         if (params.size() != 2)
21821abe55efSEd Tanous         {
2183f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2184e439f0f8SKowalski, Kamil             return;
2185e439f0f8SKowalski, Kamil         }
2186e439f0f8SKowalski, Kamil 
2187d76323e5SEd Tanous         const std::string &parentIfaceId = params[0];
218855c7b7a2SEd Tanous         const std::string &ifaceId = params[1];
2189927a505aSKowalski, Kamil 
2190fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
21911abe55efSEd Tanous         {
2192fda13ad2SSunitha Harish             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2193fda13ad2SSunitha Harish                                        ifaceId);
2194927a505aSKowalski, Kamil             return;
2195927a505aSKowalski, Kamil         }
2196927a505aSKowalski, Kamil 
21970627a2c7SEd Tanous         bool vlanEnable = false;
21980627a2c7SEd Tanous         uint64_t vlanId = 0;
21990627a2c7SEd Tanous 
22000627a2c7SEd Tanous         if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
22010627a2c7SEd Tanous                                  vlanId))
22021abe55efSEd Tanous         {
2203927a505aSKowalski, Kamil             return;
2204927a505aSKowalski, Kamil         }
2205927a505aSKowalski, Kamil 
220601784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
220701784826SJohnathan Mantey         // JSON preparation
2208e48c0fc5SRavi Teja         getEthernetIfaceData(
2209e48c0fc5SRavi Teja             params[1],
2210271584abSEd Tanous             [asyncResp, parentIfaceId{std::string(params[0])},
2211e48c0fc5SRavi Teja              ifaceId{std::string(params[1])}, &vlanEnable, &vlanId](
2212e48c0fc5SRavi Teja                 const bool &success, const EthernetInterfaceData &ethData,
2213e48c0fc5SRavi Teja                 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
221401784826SJohnathan Mantey                 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
221508244d02SSunitha Harish                 if (success && !ethData.vlan_id.empty())
221608244d02SSunitha Harish                 {
221708244d02SSunitha Harish                     auto callback =
221808244d02SSunitha Harish                         [asyncResp](const boost::system::error_code ec) {
221908244d02SSunitha Harish                             if (ec)
222008244d02SSunitha Harish                             {
222108244d02SSunitha Harish                                 messages::internalError(asyncResp->res);
222208244d02SSunitha Harish                             }
222308244d02SSunitha Harish                         };
222408244d02SSunitha Harish 
222508244d02SSunitha Harish                     if (vlanEnable == true)
222608244d02SSunitha Harish                     {
222708244d02SSunitha Harish                         crow::connections::systemBus->async_method_call(
222808244d02SSunitha Harish                             std::move(callback), "xyz.openbmc_project.Network",
222908244d02SSunitha Harish                             "/xyz/openbmc_project/network/" + ifaceId,
223008244d02SSunitha Harish                             "org.freedesktop.DBus.Properties", "Set",
223108244d02SSunitha Harish                             "xyz.openbmc_project.Network.VLAN", "Id",
223208244d02SSunitha Harish                             std::variant<uint32_t>(vlanId));
223308244d02SSunitha Harish                     }
223408244d02SSunitha Harish                     else
223508244d02SSunitha Harish                     {
2236e48c0fc5SRavi Teja                         BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2237e48c0fc5SRavi Teja                                             "vlan interface";
223808244d02SSunitha Harish                         crow::connections::systemBus->async_method_call(
223908244d02SSunitha Harish                             std::move(callback), "xyz.openbmc_project.Network",
2240e48c0fc5SRavi Teja                             std::string("/xyz/openbmc_project/network/") +
2241e48c0fc5SRavi Teja                                 ifaceId,
224208244d02SSunitha Harish                             "xyz.openbmc_project.Object.Delete", "Delete");
224308244d02SSunitha Harish                     }
224408244d02SSunitha Harish                 }
224508244d02SSunitha Harish                 else
22461abe55efSEd Tanous                 {
22471abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
22481abe55efSEd Tanous                     // object, and other errors
2249e48c0fc5SRavi Teja                     messages::resourceNotFound(
2250e48c0fc5SRavi Teja                         asyncResp->res, "VLAN Network Interface", ifaceId);
2251927a505aSKowalski, Kamil                     return;
2252927a505aSKowalski, Kamil                 }
2253927a505aSKowalski, Kamil             });
2254e439f0f8SKowalski, Kamil     }
2255e439f0f8SKowalski, Kamil 
225655c7b7a2SEd Tanous     void doDelete(crow::Response &res, const crow::Request &req,
22571abe55efSEd Tanous                   const std::vector<std::string> &params) override
22581abe55efSEd Tanous     {
22594a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
22601abe55efSEd Tanous         if (params.size() != 2)
22611abe55efSEd Tanous         {
2262f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2263e439f0f8SKowalski, Kamil             return;
2264e439f0f8SKowalski, Kamil         }
2265e439f0f8SKowalski, Kamil 
2266d76323e5SEd Tanous         const std::string &parentIfaceId = params[0];
226755c7b7a2SEd Tanous         const std::string &ifaceId = params[1];
2268927a505aSKowalski, Kamil 
2269fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
22701abe55efSEd Tanous         {
2271fda13ad2SSunitha Harish             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2272fda13ad2SSunitha Harish                                        ifaceId);
2273927a505aSKowalski, Kamil             return;
2274927a505aSKowalski, Kamil         }
2275927a505aSKowalski, Kamil 
227601784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
227701784826SJohnathan Mantey         // JSON preparation
2278f12894f8SJason M. Bills         getEthernetIfaceData(
2279fda13ad2SSunitha Harish             params[1],
2280271584abSEd Tanous             [asyncResp, parentIfaceId{std::string(params[0])},
2281fda13ad2SSunitha Harish              ifaceId{std::string(params[1])}](
2282f12894f8SJason M. Bills                 const bool &success, const EthernetInterfaceData &ethData,
2283e48c0fc5SRavi Teja                 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
228401784826SJohnathan Mantey                 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
2285fda13ad2SSunitha Harish                 if (success && !ethData.vlan_id.empty())
22861abe55efSEd Tanous                 {
2287f12894f8SJason M. Bills                     auto callback =
2288f12894f8SJason M. Bills                         [asyncResp](const boost::system::error_code ec) {
22891abe55efSEd Tanous                             if (ec)
22901abe55efSEd Tanous                             {
2291f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
2292927a505aSKowalski, Kamil                             }
22934a0cb85cSEd Tanous                         };
22944a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
22954a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
22964a0cb85cSEd Tanous                         std::string("/xyz/openbmc_project/network/") + ifaceId,
22974a0cb85cSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
22981abe55efSEd Tanous                 }
22991abe55efSEd Tanous                 else
23001abe55efSEd Tanous                 {
2301927a505aSKowalski, Kamil                     // ... otherwise return error
2302f12894f8SJason M. Bills                     // TODO(Pawel)consider distinguish between non existing
2303f12894f8SJason M. Bills                     // object, and other errors
2304f12894f8SJason M. Bills                     messages::resourceNotFound(
2305f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", ifaceId);
2306927a505aSKowalski, Kamil                 }
2307927a505aSKowalski, Kamil             });
2308e439f0f8SKowalski, Kamil     }
2309e439f0f8SKowalski, Kamil };
2310e439f0f8SKowalski, Kamil 
2311e439f0f8SKowalski, Kamil /**
2312e439f0f8SKowalski, Kamil  * VlanNetworkInterfaceCollection derived class for delivering
2313e439f0f8SKowalski, Kamil  * VLANNetworkInterface Collection Schema
2314e439f0f8SKowalski, Kamil  */
23151abe55efSEd Tanous class VlanNetworkInterfaceCollection : public Node
23161abe55efSEd Tanous {
2317e439f0f8SKowalski, Kamil   public:
2318e439f0f8SKowalski, Kamil     template <typename CrowApp>
23191abe55efSEd Tanous     VlanNetworkInterfaceCollection(CrowApp &app) :
23204a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
23214a0cb85cSEd Tanous              std::string())
23221abe55efSEd Tanous     {
2323e439f0f8SKowalski, Kamil         entityPrivileges = {
2324e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
2325e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
2326e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2327e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2328e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2329e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2330e439f0f8SKowalski, Kamil     }
2331e439f0f8SKowalski, Kamil 
2332e439f0f8SKowalski, Kamil   private:
2333e439f0f8SKowalski, Kamil     /**
2334e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
2335e439f0f8SKowalski, Kamil      */
233655c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
23371abe55efSEd Tanous                const std::vector<std::string> &params) override
23381abe55efSEd Tanous     {
23394a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
23401abe55efSEd Tanous         if (params.size() != 1)
23411abe55efSEd Tanous         {
2342e439f0f8SKowalski, Kamil             // This means there is a problem with the router
2343f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2344e439f0f8SKowalski, Kamil             return;
2345e439f0f8SKowalski, Kamil         }
2346e439f0f8SKowalski, Kamil 
23474a0cb85cSEd Tanous         const std::string &rootInterfaceName = params[0];
2348e439f0f8SKowalski, Kamil 
23494a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
23501abe55efSEd Tanous         // preparation
2351f12894f8SJason M. Bills         getEthernetIfaceList(
235243b761d0SEd Tanous             [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
23531abe55efSEd Tanous                 const bool &success,
23544c9afe43SEd Tanous                 const boost::container::flat_set<std::string> &iface_list) {
23554a0cb85cSEd Tanous                 if (!success)
23561abe55efSEd Tanous                 {
2357f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
23584a0cb85cSEd Tanous                     return;
23591abe55efSEd Tanous                 }
23604c9afe43SEd Tanous 
23614c9afe43SEd Tanous                 if (iface_list.find(rootInterfaceName) == iface_list.end())
23624c9afe43SEd Tanous                 {
23634c9afe43SEd Tanous                     messages::resourceNotFound(asyncResp->res,
23644c9afe43SEd Tanous                                                "VLanNetworkInterfaceCollection",
23654c9afe43SEd Tanous                                                rootInterfaceName);
23664c9afe43SEd Tanous                     return;
23674c9afe43SEd Tanous                 }
23684c9afe43SEd Tanous 
23690f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
23700f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
23710f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
23720f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] =
23730f74e643SEd Tanous                     "VLAN Network Interface Collection";
23744a0cb85cSEd Tanous 
23754a0cb85cSEd Tanous                 nlohmann::json iface_array = nlohmann::json::array();
23764a0cb85cSEd Tanous 
23774a0cb85cSEd Tanous                 for (const std::string &iface_item : iface_list)
23781abe55efSEd Tanous                 {
23794a0cb85cSEd Tanous                     if (boost::starts_with(iface_item, rootInterfaceName + "_"))
23804a0cb85cSEd Tanous                     {
23814a0cb85cSEd Tanous                         iface_array.push_back(
23824a0cb85cSEd Tanous                             {{"@odata.id",
23834a0cb85cSEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
23844a0cb85cSEd Tanous                                   rootInterfaceName + "/VLANs/" + iface_item}});
2385e439f0f8SKowalski, Kamil                     }
2386e439f0f8SKowalski, Kamil                 }
2387e439f0f8SKowalski, Kamil 
23884a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
23894a0cb85cSEd Tanous                     iface_array.size();
23904a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
23914a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
23924a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
23934a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
2394e439f0f8SKowalski, Kamil             });
2395e439f0f8SKowalski, Kamil     }
2396e439f0f8SKowalski, Kamil 
239755c7b7a2SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
23981abe55efSEd Tanous                 const std::vector<std::string> &params) override
23991abe55efSEd Tanous     {
24004a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
24011abe55efSEd Tanous         if (params.size() != 1)
24021abe55efSEd Tanous         {
2403f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2404e439f0f8SKowalski, Kamil             return;
2405e439f0f8SKowalski, Kamil         }
2406fda13ad2SSunitha Harish         bool vlanEnable = false;
24070627a2c7SEd Tanous         uint32_t vlanId = 0;
2408fda13ad2SSunitha Harish         if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable",
2409fda13ad2SSunitha Harish                                  vlanEnable))
24101abe55efSEd Tanous         {
24114a0cb85cSEd Tanous             return;
2412e439f0f8SKowalski, Kamil         }
2413fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
2414fda13ad2SSunitha Harish         if (!vlanId)
2415fda13ad2SSunitha Harish         {
2416fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
2417fda13ad2SSunitha Harish         }
2418fda13ad2SSunitha Harish         if (!vlanEnable)
2419fda13ad2SSunitha Harish         {
2420fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
2421fda13ad2SSunitha Harish         }
2422271584abSEd Tanous         if (static_cast<bool>(vlanId) ^ vlanEnable)
2423fda13ad2SSunitha Harish         {
2424fda13ad2SSunitha Harish             return;
2425fda13ad2SSunitha Harish         }
2426fda13ad2SSunitha Harish 
24274a0cb85cSEd Tanous         const std::string &rootInterfaceName = params[0];
24284a0cb85cSEd Tanous         auto callback = [asyncResp](const boost::system::error_code ec) {
24291abe55efSEd Tanous             if (ec)
24301abe55efSEd Tanous             {
24314a0cb85cSEd Tanous                 // TODO(ed) make more consistent error messages based on
24324a0cb85cSEd Tanous                 // phosphor-network responses
2433f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
24344a0cb85cSEd Tanous                 return;
24351abe55efSEd Tanous             }
2436f12894f8SJason M. Bills             messages::created(asyncResp->res);
2437e439f0f8SKowalski, Kamil         };
24384a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
24394a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
24404a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
24414a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
24420627a2c7SEd Tanous             rootInterfaceName, vlanId);
24434a0cb85cSEd Tanous     }
24444a0cb85cSEd Tanous };
24459391bb9cSRapkiewicz, Pawel } // namespace redfish
2446