xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 8d1b46d7f8d39db2ba048f9e9007106ca3a28c9b)
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>
231214b7e7SGunnar Mills #include <utils/json_utils.hpp>
241214b7e7SGunnar Mills 
25a24526dcSEd Tanous #include <optional>
26ab6554f1SJoshi-Mansi #include <regex>
27abf2add6SEd Tanous #include <variant>
289391bb9cSRapkiewicz, Pawel 
291abe55efSEd Tanous namespace redfish
301abe55efSEd Tanous {
319391bb9cSRapkiewicz, Pawel 
329391bb9cSRapkiewicz, Pawel /**
339391bb9cSRapkiewicz, Pawel  * DBus types primitives for several generic DBus interfaces
349391bb9cSRapkiewicz, Pawel  * TODO(Pawel) consider move this to separate file into boost::dbus
359391bb9cSRapkiewicz, Pawel  */
36aa2e59c1SEd Tanous using PropertiesMapType = boost::container::flat_map<
37abf2add6SEd Tanous     std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
38aa2e59c1SEd Tanous                               int32_t, uint32_t, int64_t, uint64_t, double>>;
399391bb9cSRapkiewicz, Pawel 
404a0cb85cSEd Tanous using GetManagedObjects = std::vector<std::pair<
41aa2e59c1SEd Tanous     sdbusplus::message::object_path,
424a0cb85cSEd Tanous     std::vector<std::pair<
43aa2e59c1SEd Tanous         std::string,
44aa2e59c1SEd Tanous         boost::container::flat_map<
4519bd78d9SPatrick Williams             std::string,
4619bd78d9SPatrick Williams             std::variant<std::string, bool, uint8_t, int16_t, uint16_t, int32_t,
4719bd78d9SPatrick Williams                          uint32_t, int64_t, uint64_t, double,
48029573d4SEd Tanous                          std::vector<std::string>>>>>>>;
494a0cb85cSEd Tanous 
504a0cb85cSEd Tanous enum class LinkType
514a0cb85cSEd Tanous {
524a0cb85cSEd Tanous     Local,
534a0cb85cSEd Tanous     Global
544a0cb85cSEd Tanous };
559391bb9cSRapkiewicz, Pawel 
569391bb9cSRapkiewicz, Pawel /**
579391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
589391bb9cSRapkiewicz, Pawel  */
591abe55efSEd Tanous struct IPv4AddressData
601abe55efSEd Tanous {
61179db1d7SKowalski, Kamil     std::string id;
624a0cb85cSEd Tanous     std::string address;
634a0cb85cSEd Tanous     std::string domain;
644a0cb85cSEd Tanous     std::string gateway;
659391bb9cSRapkiewicz, Pawel     std::string netmask;
669391bb9cSRapkiewicz, Pawel     std::string origin;
674a0cb85cSEd Tanous     LinkType linktype;
6801c6e858SSunitha Harish     bool isActive;
694a0cb85cSEd Tanous 
701abe55efSEd Tanous     bool operator<(const IPv4AddressData& obj) const
711abe55efSEd Tanous     {
724a0cb85cSEd Tanous         return id < obj.id;
731abe55efSEd Tanous     }
749391bb9cSRapkiewicz, Pawel };
759391bb9cSRapkiewicz, Pawel 
769391bb9cSRapkiewicz, Pawel /**
77e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
78e48c0fc5SRavi Teja  */
79e48c0fc5SRavi Teja struct IPv6AddressData
80e48c0fc5SRavi Teja {
81e48c0fc5SRavi Teja     std::string id;
82e48c0fc5SRavi Teja     std::string address;
83e48c0fc5SRavi Teja     std::string origin;
84e48c0fc5SRavi Teja     uint8_t prefixLength;
85e48c0fc5SRavi Teja 
86e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData& obj) const
87e48c0fc5SRavi Teja     {
88e48c0fc5SRavi Teja         return id < obj.id;
89e48c0fc5SRavi Teja     }
90e48c0fc5SRavi Teja };
91e48c0fc5SRavi Teja /**
929391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
939391bb9cSRapkiewicz, Pawel  * available from DBus
949391bb9cSRapkiewicz, Pawel  */
951abe55efSEd Tanous struct EthernetInterfaceData
961abe55efSEd Tanous {
974a0cb85cSEd Tanous     uint32_t speed;
984a0cb85cSEd Tanous     bool auto_neg;
991f8c7b5dSJohnathan Mantey     bool DNSEnabled;
1001f8c7b5dSJohnathan Mantey     bool NTPEnabled;
1011f8c7b5dSJohnathan Mantey     bool HostNameEnabled;
1021f8c7b5dSJohnathan Mantey     bool SendHostNameEnabled;
103aa05fb27SJohnathan Mantey     bool linkUp;
104eeedda23SJohnathan Mantey     bool nicEnabled;
1051f8c7b5dSJohnathan Mantey     std::string DHCPEnabled;
1061f8c7b5dSJohnathan Mantey     std::string operatingMode;
1074a0cb85cSEd Tanous     std::string hostname;
1084a0cb85cSEd Tanous     std::string default_gateway;
1099a6fc6feSRavi Teja     std::string ipv6_default_gateway;
1104a0cb85cSEd Tanous     std::string mac_address;
111fda13ad2SSunitha Harish     std::vector<std::uint32_t> vlan_id;
1120f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
1130f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
114d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
1159391bb9cSRapkiewicz, Pawel };
1169391bb9cSRapkiewicz, Pawel 
1171f8c7b5dSJohnathan Mantey struct DHCPParameters
1181f8c7b5dSJohnathan Mantey {
1191f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
1201f8c7b5dSJohnathan Mantey     std::optional<bool> useDNSServers;
1211f8c7b5dSJohnathan Mantey     std::optional<bool> useNTPServers;
1221f8c7b5dSJohnathan Mantey     std::optional<bool> useUseDomainName;
1231f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1241f8c7b5dSJohnathan Mantey };
1251f8c7b5dSJohnathan Mantey 
1269391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1279391bb9cSRapkiewicz, Pawel // into full dot notation
1281abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1291abe55efSEd Tanous {
1309391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1319391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1329391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1339391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1349391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1359391bb9cSRapkiewicz, Pawel     return netmask;
1369391bb9cSRapkiewicz, Pawel }
1379391bb9cSRapkiewicz, Pawel 
1381f8c7b5dSJohnathan Mantey inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
1391f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1401f8c7b5dSJohnathan Mantey {
1411f8c7b5dSJohnathan Mantey     if (isIPv4)
1421f8c7b5dSJohnathan Mantey     {
1431f8c7b5dSJohnathan Mantey         return (
1441f8c7b5dSJohnathan Mantey             (inputDHCP ==
1451f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1461f8c7b5dSJohnathan Mantey             (inputDHCP ==
1471f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1481f8c7b5dSJohnathan Mantey     }
1491f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1501f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1511f8c7b5dSJohnathan Mantey             (inputDHCP ==
1521f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1531f8c7b5dSJohnathan Mantey }
1541f8c7b5dSJohnathan Mantey 
1552c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1561f8c7b5dSJohnathan Mantey {
1571f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1581f8c7b5dSJohnathan Mantey     {
1591f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1601f8c7b5dSJohnathan Mantey     }
1613174e4dfSEd Tanous     if (isIPv4)
1621f8c7b5dSJohnathan Mantey     {
1631f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1641f8c7b5dSJohnathan Mantey     }
1653174e4dfSEd Tanous     if (isIPv6)
1661f8c7b5dSJohnathan Mantey     {
1671f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1681f8c7b5dSJohnathan Mantey     }
1691f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1701f8c7b5dSJohnathan Mantey }
1711f8c7b5dSJohnathan Mantey 
1724a0cb85cSEd Tanous inline std::string
1734a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1744a0cb85cSEd Tanous                                         bool isIPv4)
1751abe55efSEd Tanous {
1764a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1771abe55efSEd Tanous     {
1784a0cb85cSEd Tanous         return "Static";
1799391bb9cSRapkiewicz, Pawel     }
1804a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1811abe55efSEd Tanous     {
1824a0cb85cSEd Tanous         if (isIPv4)
1831abe55efSEd Tanous         {
1844a0cb85cSEd Tanous             return "IPv4LinkLocal";
1851abe55efSEd Tanous         }
1864a0cb85cSEd Tanous         return "LinkLocal";
1879391bb9cSRapkiewicz, Pawel     }
1884a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1891abe55efSEd Tanous     {
1904a0cb85cSEd Tanous         if (isIPv4)
1914a0cb85cSEd Tanous         {
1924a0cb85cSEd Tanous             return "DHCP";
1934a0cb85cSEd Tanous         }
1944a0cb85cSEd Tanous         return "DHCPv6";
1954a0cb85cSEd Tanous     }
1964a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1974a0cb85cSEd Tanous     {
1984a0cb85cSEd Tanous         return "SLAAC";
1994a0cb85cSEd Tanous     }
2004a0cb85cSEd Tanous     return "";
2014a0cb85cSEd Tanous }
2024a0cb85cSEd Tanous 
20381ce609eSEd Tanous inline bool extractEthernetInterfaceData(const std::string& ethifaceId,
20481ce609eSEd Tanous                                          GetManagedObjects& dbusData,
2054a0cb85cSEd Tanous                                          EthernetInterfaceData& ethData)
2064a0cb85cSEd Tanous {
2074c9afe43SEd Tanous     bool idFound = false;
20881ce609eSEd Tanous     for (auto& objpath : dbusData)
2094a0cb85cSEd Tanous     {
210f23b7296SEd Tanous         for (auto& ifacePair : objpath.second)
2114a0cb85cSEd Tanous         {
21281ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
213029573d4SEd Tanous             {
2144c9afe43SEd Tanous                 idFound = true;
2154a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2164a0cb85cSEd Tanous                 {
2174a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2184a0cb85cSEd Tanous                     {
2194a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2204a0cb85cSEd Tanous                         {
2214a0cb85cSEd Tanous                             const std::string* mac =
222abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2234a0cb85cSEd Tanous                             if (mac != nullptr)
2244a0cb85cSEd Tanous                             {
2254a0cb85cSEd Tanous                                 ethData.mac_address = *mac;
2264a0cb85cSEd Tanous                             }
2274a0cb85cSEd Tanous                         }
2284a0cb85cSEd Tanous                     }
2294a0cb85cSEd Tanous                 }
2304a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2314a0cb85cSEd Tanous                 {
2324a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2334a0cb85cSEd Tanous                     {
2344a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2354a0cb85cSEd Tanous                         {
2361b6b96c5SEd Tanous                             const uint32_t* id =
237abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2384a0cb85cSEd Tanous                             if (id != nullptr)
2394a0cb85cSEd Tanous                             {
240fda13ad2SSunitha Harish                                 ethData.vlan_id.push_back(*id);
2414a0cb85cSEd Tanous                             }
2424a0cb85cSEd Tanous                         }
2434a0cb85cSEd Tanous                     }
2444a0cb85cSEd Tanous                 }
2454a0cb85cSEd Tanous                 else if (ifacePair.first ==
2464a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2474a0cb85cSEd Tanous                 {
2484a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2494a0cb85cSEd Tanous                     {
2504a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2514a0cb85cSEd Tanous                         {
2522c70f800SEd Tanous                             const bool* autoNeg =
253abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2542c70f800SEd Tanous                             if (autoNeg != nullptr)
2554a0cb85cSEd Tanous                             {
2562c70f800SEd Tanous                                 ethData.auto_neg = *autoNeg;
2574a0cb85cSEd Tanous                             }
2584a0cb85cSEd Tanous                         }
2594a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2604a0cb85cSEd Tanous                         {
2614a0cb85cSEd Tanous                             const uint32_t* speed =
262abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2634a0cb85cSEd Tanous                             if (speed != nullptr)
2644a0cb85cSEd Tanous                             {
2654a0cb85cSEd Tanous                                 ethData.speed = *speed;
2664a0cb85cSEd Tanous                             }
2674a0cb85cSEd Tanous                         }
268aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
269aa05fb27SJohnathan Mantey                         {
270aa05fb27SJohnathan Mantey                             const bool* linkUp =
271aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
272aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
273aa05fb27SJohnathan Mantey                             {
274aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
275aa05fb27SJohnathan Mantey                             }
276aa05fb27SJohnathan Mantey                         }
277eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
278eeedda23SJohnathan Mantey                         {
279eeedda23SJohnathan Mantey                             const bool* nicEnabled =
280eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
281eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
282eeedda23SJohnathan Mantey                             {
283eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
284eeedda23SJohnathan Mantey                             }
285eeedda23SJohnathan Mantey                         }
286f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
287029573d4SEd Tanous                         {
288029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2898d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
290029573d4SEd Tanous                                     &propertyPair.second);
291029573d4SEd Tanous                             if (nameservers != nullptr)
292029573d4SEd Tanous                             {
293f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2940f6efdc1Smanojkiran.eda@gmail.com                             }
2950f6efdc1Smanojkiran.eda@gmail.com                         }
2960f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2970f6efdc1Smanojkiran.eda@gmail.com                         {
2980f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
2998d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
3000f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
3010f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
3020f6efdc1Smanojkiran.eda@gmail.com                             {
303f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
3044a0cb85cSEd Tanous                             }
3054a0cb85cSEd Tanous                         }
3062a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3072a133282Smanojkiraneda                         {
3082c70f800SEd Tanous                             const std::string* dhcpEnabled =
3091f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3102c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3112a133282Smanojkiraneda                             {
3122c70f800SEd Tanous                                 ethData.DHCPEnabled = *dhcpEnabled;
3132a133282Smanojkiraneda                             }
3142a133282Smanojkiraneda                         }
315d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
316d24bfc7aSJennifer Lee                         {
317d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3188d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
319d24bfc7aSJennifer Lee                                     &propertyPair.second);
320d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
321d24bfc7aSJennifer Lee                             {
322f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
323d24bfc7aSJennifer Lee                             }
324d24bfc7aSJennifer Lee                         }
325029573d4SEd Tanous                     }
326029573d4SEd Tanous                 }
327029573d4SEd Tanous             }
3281f8c7b5dSJohnathan Mantey 
3291f8c7b5dSJohnathan Mantey             if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
3301f8c7b5dSJohnathan Mantey             {
3311f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3321f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3331f8c7b5dSJohnathan Mantey                 {
3341f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3351f8c7b5dSJohnathan Mantey                     {
3361f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3371f8c7b5dSJohnathan Mantey                         {
3382c70f800SEd Tanous                             const bool* dnsEnabled =
3391f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3402c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3411f8c7b5dSJohnathan Mantey                             {
3422c70f800SEd Tanous                                 ethData.DNSEnabled = *dnsEnabled;
3431f8c7b5dSJohnathan Mantey                             }
3441f8c7b5dSJohnathan Mantey                         }
3451f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3461f8c7b5dSJohnathan Mantey                         {
3472c70f800SEd Tanous                             const bool* ntpEnabled =
3481f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3492c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3501f8c7b5dSJohnathan Mantey                             {
3512c70f800SEd Tanous                                 ethData.NTPEnabled = *ntpEnabled;
3521f8c7b5dSJohnathan Mantey                             }
3531f8c7b5dSJohnathan Mantey                         }
3541f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3551f8c7b5dSJohnathan Mantey                         {
3562c70f800SEd Tanous                             const bool* hostNameEnabled =
3571f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3582c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3591f8c7b5dSJohnathan Mantey                             {
3602c70f800SEd Tanous                                 ethData.HostNameEnabled = *hostNameEnabled;
3611f8c7b5dSJohnathan Mantey                             }
3621f8c7b5dSJohnathan Mantey                         }
3631f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "SendHostNameEnabled")
3641f8c7b5dSJohnathan Mantey                         {
3652c70f800SEd Tanous                             const bool* sendHostNameEnabled =
3661f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3672c70f800SEd Tanous                             if (sendHostNameEnabled != nullptr)
3681f8c7b5dSJohnathan Mantey                             {
3691f8c7b5dSJohnathan Mantey                                 ethData.SendHostNameEnabled =
3702c70f800SEd Tanous                                     *sendHostNameEnabled;
3711f8c7b5dSJohnathan Mantey                             }
3721f8c7b5dSJohnathan Mantey                         }
3731f8c7b5dSJohnathan Mantey                     }
3741f8c7b5dSJohnathan Mantey                 }
3751f8c7b5dSJohnathan Mantey             }
376029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
377029573d4SEd Tanous             // to check eth number
378029573d4SEd Tanous             if (ifacePair.first ==
3794a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
3804a0cb85cSEd Tanous             {
3814a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
3824a0cb85cSEd Tanous                 {
3834a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
3844a0cb85cSEd Tanous                     {
3854a0cb85cSEd Tanous                         const std::string* hostname =
3868d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
3874a0cb85cSEd Tanous                         if (hostname != nullptr)
3884a0cb85cSEd Tanous                         {
3894a0cb85cSEd Tanous                             ethData.hostname = *hostname;
3904a0cb85cSEd Tanous                         }
3914a0cb85cSEd Tanous                     }
3924a0cb85cSEd Tanous                     else if (propertyPair.first == "DefaultGateway")
3934a0cb85cSEd Tanous                     {
3944a0cb85cSEd Tanous                         const std::string* defaultGateway =
3958d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
3964a0cb85cSEd Tanous                         if (defaultGateway != nullptr)
3974a0cb85cSEd Tanous                         {
3984a0cb85cSEd Tanous                             ethData.default_gateway = *defaultGateway;
3994a0cb85cSEd Tanous                         }
4004a0cb85cSEd Tanous                     }
4019a6fc6feSRavi Teja                     else if (propertyPair.first == "DefaultGateway6")
4029a6fc6feSRavi Teja                     {
4039a6fc6feSRavi Teja                         const std::string* defaultGateway6 =
4048d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4059a6fc6feSRavi Teja                         if (defaultGateway6 != nullptr)
4069a6fc6feSRavi Teja                         {
4079a6fc6feSRavi Teja                             ethData.ipv6_default_gateway = *defaultGateway6;
4089a6fc6feSRavi Teja                         }
4099a6fc6feSRavi Teja                     }
4104a0cb85cSEd Tanous                 }
4114a0cb85cSEd Tanous             }
4124a0cb85cSEd Tanous         }
4134a0cb85cSEd Tanous     }
4144c9afe43SEd Tanous     return idFound;
4154a0cb85cSEd Tanous }
4164a0cb85cSEd Tanous 
417e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
41801784826SJohnathan Mantey inline void
41981ce609eSEd Tanous     extractIPV6Data(const std::string& ethifaceId,
42081ce609eSEd Tanous                     const GetManagedObjects& dbusData,
42181ce609eSEd Tanous                     boost::container::flat_set<IPv6AddressData>& ipv6Config)
422e48c0fc5SRavi Teja {
423e48c0fc5SRavi Teja     const std::string ipv6PathStart =
42481ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
425e48c0fc5SRavi Teja 
426e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
427e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
42881ce609eSEd Tanous     for (const auto& objpath : dbusData)
429e48c0fc5SRavi Teja     {
430e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
431e48c0fc5SRavi Teja         if (boost::starts_with(objpath.first.str, ipv6PathStart))
432e48c0fc5SRavi Teja         {
433e48c0fc5SRavi Teja             for (auto& interface : objpath.second)
434e48c0fc5SRavi Teja             {
435e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
436e48c0fc5SRavi Teja                 {
437e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
438e48c0fc5SRavi Teja                     // appropriate
439e48c0fc5SRavi Teja                     std::pair<
440e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
441e48c0fc5SRavi Teja                         bool>
44281ce609eSEd Tanous                         it = ipv6Config.insert(IPv6AddressData{});
4432c70f800SEd Tanous                     IPv6AddressData& ipv6Address = *it.first;
4442c70f800SEd Tanous                     ipv6Address.id =
445271584abSEd Tanous                         objpath.first.str.substr(ipv6PathStart.size());
446e48c0fc5SRavi Teja                     for (auto& property : interface.second)
447e48c0fc5SRavi Teja                     {
448e48c0fc5SRavi Teja                         if (property.first == "Address")
449e48c0fc5SRavi Teja                         {
450e48c0fc5SRavi Teja                             const std::string* address =
451e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
452e48c0fc5SRavi Teja                             if (address != nullptr)
453e48c0fc5SRavi Teja                             {
4542c70f800SEd Tanous                                 ipv6Address.address = *address;
455e48c0fc5SRavi Teja                             }
456e48c0fc5SRavi Teja                         }
457e48c0fc5SRavi Teja                         else if (property.first == "Origin")
458e48c0fc5SRavi Teja                         {
459e48c0fc5SRavi Teja                             const std::string* origin =
460e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
461e48c0fc5SRavi Teja                             if (origin != nullptr)
462e48c0fc5SRavi Teja                             {
4632c70f800SEd Tanous                                 ipv6Address.origin =
464e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
465e48c0fc5SRavi Teja                                                                         false);
466e48c0fc5SRavi Teja                             }
467e48c0fc5SRavi Teja                         }
468e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
469e48c0fc5SRavi Teja                         {
470e48c0fc5SRavi Teja                             const uint8_t* prefix =
471e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
472e48c0fc5SRavi Teja                             if (prefix != nullptr)
473e48c0fc5SRavi Teja                             {
4742c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
475e48c0fc5SRavi Teja                             }
476e48c0fc5SRavi Teja                         }
477e48c0fc5SRavi Teja                         else
478e48c0fc5SRavi Teja                         {
479e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
480e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
481e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
482e48c0fc5SRavi Teja                         }
483e48c0fc5SRavi Teja                     }
484e48c0fc5SRavi Teja                 }
485e48c0fc5SRavi Teja             }
486e48c0fc5SRavi Teja         }
487e48c0fc5SRavi Teja     }
488e48c0fc5SRavi Teja }
489e48c0fc5SRavi Teja 
4904a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
49101784826SJohnathan Mantey inline void
49281ce609eSEd Tanous     extractIPData(const std::string& ethifaceId,
49381ce609eSEd Tanous                   const GetManagedObjects& dbusData,
49481ce609eSEd Tanous                   boost::container::flat_set<IPv4AddressData>& ipv4Config)
4954a0cb85cSEd Tanous {
4964a0cb85cSEd Tanous     const std::string ipv4PathStart =
49781ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
4984a0cb85cSEd Tanous 
4994a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5004a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
50181ce609eSEd Tanous     for (const auto& objpath : dbusData)
5024a0cb85cSEd Tanous     {
5034a0cb85cSEd Tanous         // Check if proper pattern for object path appears
5044a0cb85cSEd Tanous         if (boost::starts_with(objpath.first.str, ipv4PathStart))
5054a0cb85cSEd Tanous         {
5064a0cb85cSEd Tanous             for (auto& interface : objpath.second)
5074a0cb85cSEd Tanous             {
5084a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5094a0cb85cSEd Tanous                 {
5104a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5114a0cb85cSEd Tanous                     // appropriate
5124a0cb85cSEd Tanous                     std::pair<
5134a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5144a0cb85cSEd Tanous                         bool>
51581ce609eSEd Tanous                         it = ipv4Config.insert(IPv4AddressData{});
5162c70f800SEd Tanous                     IPv4AddressData& ipv4Address = *it.first;
5172c70f800SEd Tanous                     ipv4Address.id =
518271584abSEd Tanous                         objpath.first.str.substr(ipv4PathStart.size());
5194a0cb85cSEd Tanous                     for (auto& property : interface.second)
5204a0cb85cSEd Tanous                     {
5214a0cb85cSEd Tanous                         if (property.first == "Address")
5224a0cb85cSEd Tanous                         {
5234a0cb85cSEd Tanous                             const std::string* address =
524abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5254a0cb85cSEd Tanous                             if (address != nullptr)
5264a0cb85cSEd Tanous                             {
5272c70f800SEd Tanous                                 ipv4Address.address = *address;
5284a0cb85cSEd Tanous                             }
5294a0cb85cSEd Tanous                         }
5304a0cb85cSEd Tanous                         else if (property.first == "Gateway")
5314a0cb85cSEd Tanous                         {
5324a0cb85cSEd Tanous                             const std::string* gateway =
533abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5344a0cb85cSEd Tanous                             if (gateway != nullptr)
5354a0cb85cSEd Tanous                             {
5362c70f800SEd Tanous                                 ipv4Address.gateway = *gateway;
5374a0cb85cSEd Tanous                             }
5384a0cb85cSEd Tanous                         }
5394a0cb85cSEd Tanous                         else if (property.first == "Origin")
5404a0cb85cSEd Tanous                         {
5414a0cb85cSEd Tanous                             const std::string* origin =
542abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5434a0cb85cSEd Tanous                             if (origin != nullptr)
5444a0cb85cSEd Tanous                             {
5452c70f800SEd Tanous                                 ipv4Address.origin =
5464a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5474a0cb85cSEd Tanous                                                                         true);
5484a0cb85cSEd Tanous                             }
5494a0cb85cSEd Tanous                         }
5504a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5514a0cb85cSEd Tanous                         {
5524a0cb85cSEd Tanous                             const uint8_t* mask =
553abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5544a0cb85cSEd Tanous                             if (mask != nullptr)
5554a0cb85cSEd Tanous                             {
5564a0cb85cSEd Tanous                                 // convert it to the string
5572c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
5584a0cb85cSEd Tanous                             }
5594a0cb85cSEd Tanous                         }
5604a0cb85cSEd Tanous                         else
5614a0cb85cSEd Tanous                         {
5624a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5634a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5644a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5654a0cb85cSEd Tanous                         }
5664a0cb85cSEd Tanous                     }
5674a0cb85cSEd Tanous                     // Check if given address is local, or global
5682c70f800SEd Tanous                     ipv4Address.linktype =
5692c70f800SEd Tanous                         boost::starts_with(ipv4Address.address, "169.254.")
57018659d10SJohnathan Mantey                             ? LinkType::Local
57118659d10SJohnathan Mantey                             : LinkType::Global;
5724a0cb85cSEd Tanous                 }
5734a0cb85cSEd Tanous             }
5744a0cb85cSEd Tanous         }
5754a0cb85cSEd Tanous     }
5764a0cb85cSEd Tanous }
577588c3f0dSKowalski, Kamil 
578588c3f0dSKowalski, Kamil /**
579588c3f0dSKowalski, Kamil  * @brief Sets given Id on the given VLAN interface through D-Bus
580588c3f0dSKowalski, Kamil  *
581588c3f0dSKowalski, Kamil  * @param[in] ifaceId       Id of VLAN interface that should be modified
582588c3f0dSKowalski, Kamil  * @param[in] inputVlanId   New ID of the VLAN
583588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
584588c3f0dSKowalski, Kamil  *
585588c3f0dSKowalski, Kamil  * @return None.
586588c3f0dSKowalski, Kamil  */
587588c3f0dSKowalski, Kamil template <typename CallbackFunc>
5884a0cb85cSEd Tanous void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
5891abe55efSEd Tanous                   CallbackFunc&& callback)
5901abe55efSEd Tanous {
59155c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
592588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
593588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
594588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
595588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", "Id",
596abf2add6SEd Tanous         std::variant<uint32_t>(inputVlanId));
5974a0cb85cSEd Tanous }
598588c3f0dSKowalski, Kamil 
599588c3f0dSKowalski, Kamil /**
600179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
601179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
602179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
603179db1d7SKowalski, Kamil  *
604179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
605179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
606179db1d7SKowalski, Kamil  *
607179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
608179db1d7SKowalski, Kamil  */
6094a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
6101abe55efSEd Tanous                                        uint8_t* bits = nullptr)
6111abe55efSEd Tanous {
612179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
613179db1d7SKowalski, Kamil 
614179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
615179db1d7SKowalski, Kamil 
6164a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
6171abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
6181abe55efSEd Tanous     {
619179db1d7SKowalski, Kamil         return false;
620179db1d7SKowalski, Kamil     }
621179db1d7SKowalski, Kamil 
6221abe55efSEd Tanous     if (bits != nullptr)
6231abe55efSEd Tanous     {
624179db1d7SKowalski, Kamil         *bits = 0;
625179db1d7SKowalski, Kamil     }
626179db1d7SKowalski, Kamil 
627179db1d7SKowalski, Kamil     char* endPtr;
628179db1d7SKowalski, Kamil     long previousValue = 255;
629179db1d7SKowalski, Kamil     bool firstZeroInByteHit;
6301abe55efSEd Tanous     for (const std::string& byte : bytesInMask)
6311abe55efSEd Tanous     {
6321abe55efSEd Tanous         if (byte.empty())
6331abe55efSEd Tanous         {
6341db9ca37SKowalski, Kamil             return false;
6351db9ca37SKowalski, Kamil         }
6361db9ca37SKowalski, Kamil 
637179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
6381db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
639179db1d7SKowalski, Kamil 
6404a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
6414a0cb85cSEd Tanous         // is not 100% number
6421abe55efSEd Tanous         if (*endPtr != '\0')
6431abe55efSEd Tanous         {
644179db1d7SKowalski, Kamil             return false;
645179db1d7SKowalski, Kamil         }
646179db1d7SKowalski, Kamil 
647179db1d7SKowalski, Kamil         // Value should be contained in byte
6481abe55efSEd Tanous         if (value < 0 || value > 255)
6491abe55efSEd Tanous         {
650179db1d7SKowalski, Kamil             return false;
651179db1d7SKowalski, Kamil         }
652179db1d7SKowalski, Kamil 
6531abe55efSEd Tanous         if (bits != nullptr)
6541abe55efSEd Tanous         {
655179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
6561abe55efSEd Tanous             if (previousValue != 255 && value != 0)
6571abe55efSEd Tanous             {
658179db1d7SKowalski, Kamil                 return false;
659179db1d7SKowalski, Kamil             }
660179db1d7SKowalski, Kamil 
661179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
662179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
663179db1d7SKowalski, Kamil 
664179db1d7SKowalski, Kamil             // Count bits
66523a21a1cSEd Tanous             for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
6661abe55efSEd Tanous             {
66723a21a1cSEd Tanous                 if (value & (1L << bitIdx))
6681abe55efSEd Tanous                 {
6691abe55efSEd Tanous                     if (firstZeroInByteHit)
6701abe55efSEd Tanous                     {
671179db1d7SKowalski, Kamil                         // Continuity not preserved
672179db1d7SKowalski, Kamil                         return false;
6731abe55efSEd Tanous                     }
674179db1d7SKowalski, Kamil                     (*bits)++;
675179db1d7SKowalski, Kamil                 }
6761abe55efSEd Tanous                 else
6771abe55efSEd Tanous                 {
678179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
679179db1d7SKowalski, Kamil                 }
680179db1d7SKowalski, Kamil             }
681179db1d7SKowalski, Kamil         }
682179db1d7SKowalski, Kamil 
683179db1d7SKowalski, Kamil         previousValue = value;
684179db1d7SKowalski, Kamil     }
685179db1d7SKowalski, Kamil 
686179db1d7SKowalski, Kamil     return true;
687179db1d7SKowalski, Kamil }
688179db1d7SKowalski, Kamil 
689179db1d7SKowalski, Kamil /**
69001784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
691179db1d7SKowalski, Kamil  *
692179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
693179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
694179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
695179db1d7SKowalski, Kamil  *
696179db1d7SKowalski, Kamil  * @return None
697179db1d7SKowalski, Kamil  */
6984a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
699*8d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7001abe55efSEd Tanous {
70155c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
702286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
7031abe55efSEd Tanous             if (ec)
7041abe55efSEd Tanous             {
705a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
7061abe55efSEd Tanous             }
707179db1d7SKowalski, Kamil         },
708179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
709179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
710179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
711179db1d7SKowalski, Kamil }
712179db1d7SKowalski, Kamil 
713179db1d7SKowalski, Kamil /**
71401784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
715179db1d7SKowalski, Kamil  *
71601784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
71701784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
71801784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
71901784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
720179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
721179db1d7SKowalski, Kamil  *
722179db1d7SKowalski, Kamil  * @return None
723179db1d7SKowalski, Kamil  */
724cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
725cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
726*8d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7271abe55efSEd Tanous {
72801784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
72901784826SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
7301abe55efSEd Tanous             if (ec)
7311abe55efSEd Tanous             {
732a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
733179db1d7SKowalski, Kamil             }
73401784826SJohnathan Mantey         },
73501784826SJohnathan Mantey         "xyz.openbmc_project.Network",
736179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
737179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
73801784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
739179db1d7SKowalski, Kamil         gateway);
740179db1d7SKowalski, Kamil }
741e48c0fc5SRavi Teja 
742e48c0fc5SRavi Teja /**
74301784826SJohnathan Mantey  * @brief Deletes the IPv4 entry for this interface and creates a replacement
74401784826SJohnathan Mantey  * static IPv4 entry
74501784826SJohnathan Mantey  *
74601784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
74701784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
74801784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
74901784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
75001784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
75101784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
75201784826SJohnathan Mantey  *
75301784826SJohnathan Mantey  * @return None
75401784826SJohnathan Mantey  */
755*8d1b46d7Szhanghch05 inline void
756*8d1b46d7Szhanghch05     deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
757*8d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& gateway,
75801784826SJohnathan Mantey                         const std::string& address,
759*8d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
76001784826SJohnathan Mantey {
76101784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
76201784826SJohnathan Mantey         [asyncResp, ifaceId, address, prefixLength,
76301784826SJohnathan Mantey          gateway](const boost::system::error_code ec) {
76401784826SJohnathan Mantey             if (ec)
76501784826SJohnathan Mantey             {
76601784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
76701784826SJohnathan Mantey             }
76801784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
76923a21a1cSEd Tanous                 [asyncResp](const boost::system::error_code ec2) {
77023a21a1cSEd Tanous                     if (ec2)
77101784826SJohnathan Mantey                     {
77201784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
77301784826SJohnathan Mantey                     }
77401784826SJohnathan Mantey                 },
77501784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
77601784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
77701784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
77801784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
77901784826SJohnathan Mantey                 prefixLength, gateway);
78001784826SJohnathan Mantey         },
78101784826SJohnathan Mantey         "xyz.openbmc_project.Network",
78201784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
78301784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
78401784826SJohnathan Mantey }
78501784826SJohnathan Mantey 
78601784826SJohnathan Mantey /**
787e48c0fc5SRavi Teja  * @brief Deletes given IPv6
788e48c0fc5SRavi Teja  *
789e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
790e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
791e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
792e48c0fc5SRavi Teja  *
793e48c0fc5SRavi Teja  * @return None
794e48c0fc5SRavi Teja  */
795e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
796*8d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
797e48c0fc5SRavi Teja {
798e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
799286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
800e48c0fc5SRavi Teja             if (ec)
801e48c0fc5SRavi Teja             {
802e48c0fc5SRavi Teja                 messages::internalError(asyncResp->res);
803e48c0fc5SRavi Teja             }
804e48c0fc5SRavi Teja         },
805e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
806e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
807e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
808e48c0fc5SRavi Teja }
809e48c0fc5SRavi Teja 
810e48c0fc5SRavi Teja /**
81101784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
81201784826SJohnathan Mantey  * static IPv6 entry
81301784826SJohnathan Mantey  *
81401784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
81501784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
81601784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
81701784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
81801784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
81901784826SJohnathan Mantey  *
82001784826SJohnathan Mantey  * @return None
82101784826SJohnathan Mantey  */
822*8d1b46d7Szhanghch05 inline void
823*8d1b46d7Szhanghch05     deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
824*8d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& address,
825*8d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
82601784826SJohnathan Mantey {
82701784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
82801784826SJohnathan Mantey         [asyncResp, ifaceId, address,
82901784826SJohnathan Mantey          prefixLength](const boost::system::error_code ec) {
83001784826SJohnathan Mantey             if (ec)
83101784826SJohnathan Mantey             {
83201784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
83301784826SJohnathan Mantey             }
83401784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
83523a21a1cSEd Tanous                 [asyncResp](const boost::system::error_code ec2) {
83623a21a1cSEd Tanous                     if (ec2)
83701784826SJohnathan Mantey                     {
83801784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
83901784826SJohnathan Mantey                     }
84001784826SJohnathan Mantey                 },
84101784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
84201784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
84301784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
84401784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
84501784826SJohnathan Mantey                 prefixLength, "");
84601784826SJohnathan Mantey         },
84701784826SJohnathan Mantey         "xyz.openbmc_project.Network",
84801784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
84901784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
85001784826SJohnathan Mantey }
85101784826SJohnathan Mantey 
85201784826SJohnathan Mantey /**
853e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
854e48c0fc5SRavi Teja  *
855e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
856e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
857e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
858e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
859e48c0fc5SRavi Teja  *
860e48c0fc5SRavi Teja  * @return None
861e48c0fc5SRavi Teja  */
86201784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
86301784826SJohnathan Mantey                        const std::string& address,
864*8d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
865e48c0fc5SRavi Teja {
866e48c0fc5SRavi Teja     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
867e48c0fc5SRavi Teja         if (ec)
868e48c0fc5SRavi Teja         {
869e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
870e48c0fc5SRavi Teja         }
871e48c0fc5SRavi Teja     };
872e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
8734e0453b1SGunnar Mills     // does not have associated gateway property
874e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
875e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
876e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
877e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
878e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
879e48c0fc5SRavi Teja         "");
880e48c0fc5SRavi Teja }
881e48c0fc5SRavi Teja 
882179db1d7SKowalski, Kamil /**
883179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
884179db1d7SKowalski, Kamil  * Object
885179db1d7SKowalski, Kamil  * from EntityManager Network Manager
8864a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
887179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
888179db1d7SKowalski, Kamil  * into JSON
889179db1d7SKowalski, Kamil  */
890179db1d7SKowalski, Kamil template <typename CallbackFunc>
89181ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
8921abe55efSEd Tanous                           CallbackFunc&& callback)
8931abe55efSEd Tanous {
89455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
89581ce609eSEd Tanous         [ethifaceId{std::string{ethifaceId}}, callback{std::move(callback)}](
89681ce609eSEd Tanous             const boost::system::error_code errorCode,
897f23b7296SEd Tanous             GetManagedObjects& resp) {
89855c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
8994a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
900e48c0fc5SRavi Teja             boost::container::flat_set<IPv6AddressData> ipv6Data;
901179db1d7SKowalski, Kamil 
90281ce609eSEd Tanous             if (errorCode)
9031abe55efSEd Tanous             {
90401784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
905179db1d7SKowalski, Kamil                 return;
906179db1d7SKowalski, Kamil             }
907179db1d7SKowalski, Kamil 
9084c9afe43SEd Tanous             bool found =
9092c70f800SEd Tanous                 extractEthernetInterfaceData(ethifaceId, resp, ethData);
9104c9afe43SEd Tanous             if (!found)
9114c9afe43SEd Tanous             {
91201784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
9134c9afe43SEd Tanous                 return;
9144c9afe43SEd Tanous             }
9154c9afe43SEd Tanous 
9162c70f800SEd Tanous             extractIPData(ethifaceId, resp, ipv4Data);
917179db1d7SKowalski, Kamil             // Fix global GW
9181abe55efSEd Tanous             for (IPv4AddressData& ipv4 : ipv4Data)
9191abe55efSEd Tanous             {
920c619141bSRavi Teja                 if (((ipv4.linktype == LinkType::Global) &&
921c619141bSRavi Teja                      (ipv4.gateway == "0.0.0.0")) ||
922c619141bSRavi Teja                     (ipv4.origin == "DHCP"))
9231abe55efSEd Tanous                 {
9244a0cb85cSEd Tanous                     ipv4.gateway = ethData.default_gateway;
925179db1d7SKowalski, Kamil                 }
926179db1d7SKowalski, Kamil             }
927179db1d7SKowalski, Kamil 
9282c70f800SEd Tanous             extractIPV6Data(ethifaceId, resp, ipv6Data);
9294e0453b1SGunnar Mills             // Finally make a callback with useful data
93001784826SJohnathan Mantey             callback(true, ethData, ipv4Data, ipv6Data);
931179db1d7SKowalski, Kamil         },
932179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
933179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
934271584abSEd Tanous }
935179db1d7SKowalski, Kamil 
936179db1d7SKowalski, Kamil /**
9379391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
9389391bb9cSRapkiewicz, Pawel  * Manager
9391abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
9401abe55efSEd Tanous  * into JSON.
9419391bb9cSRapkiewicz, Pawel  */
9429391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
9431abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
9441abe55efSEd Tanous {
94555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
9464a0cb85cSEd Tanous         [callback{std::move(callback)}](
94781ce609eSEd Tanous             const boost::system::error_code errorCode,
9484a0cb85cSEd Tanous             GetManagedObjects& resp) {
9491abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
9501abe55efSEd Tanous             // ethernet interfaces
9512c70f800SEd Tanous             boost::container::flat_set<std::string> ifaceList;
9522c70f800SEd Tanous             ifaceList.reserve(resp.size());
95381ce609eSEd Tanous             if (errorCode)
9541abe55efSEd Tanous             {
9552c70f800SEd Tanous                 callback(false, ifaceList);
9569391bb9cSRapkiewicz, Pawel                 return;
9579391bb9cSRapkiewicz, Pawel             }
9589391bb9cSRapkiewicz, Pawel 
9599391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
9604a0cb85cSEd Tanous             for (const auto& objpath : resp)
9611abe55efSEd Tanous             {
9629391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
9634a0cb85cSEd Tanous                 for (const auto& interface : objpath.second)
9641abe55efSEd Tanous                 {
9651abe55efSEd Tanous                     // If interface is
9664a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
9674a0cb85cSEd Tanous                     // what we're looking for.
9689391bb9cSRapkiewicz, Pawel                     if (interface.first ==
9691abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
9701abe55efSEd Tanous                     {
9712dfd18efSEd Tanous                         std::string ifaceId = objpath.first.filename();
9722dfd18efSEd Tanous                         if (ifaceId.empty())
9731abe55efSEd Tanous                         {
9742dfd18efSEd Tanous                             continue;
9759391bb9cSRapkiewicz, Pawel                         }
9762dfd18efSEd Tanous                         // and put it into output vector.
9772dfd18efSEd Tanous                         ifaceList.emplace(ifaceId);
9789391bb9cSRapkiewicz, Pawel                     }
9799391bb9cSRapkiewicz, Pawel                 }
9809391bb9cSRapkiewicz, Pawel             }
981a434f2bdSEd Tanous             // Finally make a callback with useful data
9822c70f800SEd Tanous             callback(true, ifaceList);
9839391bb9cSRapkiewicz, Pawel         },
984aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
985aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
986271584abSEd Tanous }
9879391bb9cSRapkiewicz, Pawel 
9889391bb9cSRapkiewicz, Pawel /**
9899391bb9cSRapkiewicz, Pawel  * EthernetCollection derived class for delivering Ethernet Collection Schema
9909391bb9cSRapkiewicz, Pawel  */
9911abe55efSEd Tanous class EthernetCollection : public Node
9921abe55efSEd Tanous {
9939391bb9cSRapkiewicz, Pawel   public:
99452cc112dSEd Tanous     EthernetCollection(App& app) :
9954a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
9961abe55efSEd Tanous     {
997588c3f0dSKowalski, Kamil         entityPrivileges = {
998588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
999e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1000e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1001e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1002e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1003e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
10049391bb9cSRapkiewicz, Pawel     }
10059391bb9cSRapkiewicz, Pawel 
10069391bb9cSRapkiewicz, Pawel   private:
10079391bb9cSRapkiewicz, Pawel     /**
10089391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
10099391bb9cSRapkiewicz, Pawel      */
1010*8d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1011*8d1b46d7Szhanghch05                const crow::Request&, const std::vector<std::string>&) override
10121abe55efSEd Tanous     {
1013*8d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
10140f74e643SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1015*8d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
10160f74e643SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
1017*8d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] =
1018*8d1b46d7Szhanghch05             "Ethernet Network Interface Collection";
1019*8d1b46d7Szhanghch05         asyncResp->res.jsonValue["Description"] =
10200f74e643SEd Tanous             "Collection of EthernetInterfaces for this Manager";
1021*8d1b46d7Szhanghch05 
10224a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
10231abe55efSEd Tanous         // preparation
1024f12894f8SJason M. Bills         getEthernetIfaceList(
10254c9afe43SEd Tanous             [asyncResp](
10264c9afe43SEd Tanous                 const bool& success,
102781ce609eSEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
10284a0cb85cSEd Tanous                 if (!success)
10291abe55efSEd Tanous                 {
10304c9afe43SEd Tanous                     messages::internalError(asyncResp->res);
10314a0cb85cSEd Tanous                     return;
10324a0cb85cSEd Tanous                 }
10334a0cb85cSEd Tanous 
10342c70f800SEd Tanous                 nlohmann::json& ifaceArray =
10354c9afe43SEd Tanous                     asyncResp->res.jsonValue["Members"];
10362c70f800SEd Tanous                 ifaceArray = nlohmann::json::array();
1037fda13ad2SSunitha Harish                 std::string tag = "_";
103881ce609eSEd Tanous                 for (const std::string& ifaceItem : ifaceList)
10391abe55efSEd Tanous                 {
10402c70f800SEd Tanous                     std::size_t found = ifaceItem.find(tag);
1041fda13ad2SSunitha Harish                     if (found == std::string::npos)
1042fda13ad2SSunitha Harish                     {
10432c70f800SEd Tanous                         ifaceArray.push_back(
10444a0cb85cSEd Tanous                             {{"@odata.id",
10454a0cb85cSEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
10462c70f800SEd Tanous                                   ifaceItem}});
10479391bb9cSRapkiewicz, Pawel                     }
1048fda13ad2SSunitha Harish                 }
10494a0cb85cSEd Tanous 
10504c9afe43SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
10512c70f800SEd Tanous                     ifaceArray.size();
10524c9afe43SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
10534a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
10549391bb9cSRapkiewicz, Pawel             });
10559391bb9cSRapkiewicz, Pawel     }
10569391bb9cSRapkiewicz, Pawel };
10579391bb9cSRapkiewicz, Pawel 
10589391bb9cSRapkiewicz, Pawel /**
10599391bb9cSRapkiewicz, Pawel  * EthernetInterface derived class for delivering Ethernet Schema
10609391bb9cSRapkiewicz, Pawel  */
10611abe55efSEd Tanous class EthernetInterface : public Node
10621abe55efSEd Tanous {
10639391bb9cSRapkiewicz, Pawel   public:
10649391bb9cSRapkiewicz, Pawel     /*
10659391bb9cSRapkiewicz, Pawel      * Default Constructor
10669391bb9cSRapkiewicz, Pawel      */
106752cc112dSEd Tanous     EthernetInterface(App& app) :
10684a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
10691abe55efSEd Tanous              std::string())
10701abe55efSEd Tanous     {
1071588c3f0dSKowalski, Kamil         entityPrivileges = {
1072588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1073e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1074e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1075e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1076e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1077e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
10789391bb9cSRapkiewicz, Pawel     }
10799391bb9cSRapkiewicz, Pawel 
1080e439f0f8SKowalski, Kamil   private:
1081*8d1b46d7Szhanghch05     void
1082*8d1b46d7Szhanghch05         handleHostnamePatch(const std::string& hostname,
1083*8d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
10841abe55efSEd Tanous     {
1085ab6554f1SJoshi-Mansi         // SHOULD handle host names of up to 255 characters(RFC 1123)
1086ab6554f1SJoshi-Mansi         if (hostname.length() > 255)
1087ab6554f1SJoshi-Mansi         {
1088ab6554f1SJoshi-Mansi             messages::propertyValueFormatError(asyncResp->res, hostname,
1089ab6554f1SJoshi-Mansi                                                "HostName");
1090ab6554f1SJoshi-Mansi             return;
1091ab6554f1SJoshi-Mansi         }
1092bc0bd6e0SEd Tanous         crow::connections::systemBus->async_method_call(
1093bc0bd6e0SEd Tanous             [asyncResp](const boost::system::error_code ec) {
10944a0cb85cSEd Tanous                 if (ec)
10954a0cb85cSEd Tanous                 {
1096a08b46ccSJason M. Bills                     messages::internalError(asyncResp->res);
10971abe55efSEd Tanous                 }
1098bc0bd6e0SEd Tanous             },
1099bc0bd6e0SEd Tanous             "xyz.openbmc_project.Network",
1100bc0bd6e0SEd Tanous             "/xyz/openbmc_project/network/config",
1101bc0bd6e0SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
1102bc0bd6e0SEd Tanous             "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1103abf2add6SEd Tanous             std::variant<std::string>(hostname));
1104588c3f0dSKowalski, Kamil     }
1105588c3f0dSKowalski, Kamil 
1106*8d1b46d7Szhanghch05     void handleDomainnamePatch(
1107*8d1b46d7Szhanghch05         const std::string& ifaceId, const std::string& domainname,
1108*8d1b46d7Szhanghch05         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1109ab6554f1SJoshi-Mansi     {
1110ab6554f1SJoshi-Mansi         std::vector<std::string> vectorDomainname = {domainname};
1111ab6554f1SJoshi-Mansi         crow::connections::systemBus->async_method_call(
1112ab6554f1SJoshi-Mansi             [asyncResp](const boost::system::error_code ec) {
1113ab6554f1SJoshi-Mansi                 if (ec)
1114ab6554f1SJoshi-Mansi                 {
1115ab6554f1SJoshi-Mansi                     messages::internalError(asyncResp->res);
1116ab6554f1SJoshi-Mansi                 }
1117ab6554f1SJoshi-Mansi             },
1118ab6554f1SJoshi-Mansi             "xyz.openbmc_project.Network",
1119ab6554f1SJoshi-Mansi             "/xyz/openbmc_project/network/" + ifaceId,
1120ab6554f1SJoshi-Mansi             "org.freedesktop.DBus.Properties", "Set",
1121ab6554f1SJoshi-Mansi             "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1122ab6554f1SJoshi-Mansi             std::variant<std::vector<std::string>>(vectorDomainname));
1123ab6554f1SJoshi-Mansi     }
1124ab6554f1SJoshi-Mansi 
1125ab6554f1SJoshi-Mansi     void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1126*8d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1127ab6554f1SJoshi-Mansi     {
1128ab6554f1SJoshi-Mansi         // Total length of FQDN must not exceed 255 characters(RFC 1035)
1129ab6554f1SJoshi-Mansi         if (fqdn.length() > 255)
1130ab6554f1SJoshi-Mansi         {
1131ab6554f1SJoshi-Mansi             messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1132ab6554f1SJoshi-Mansi             return;
1133ab6554f1SJoshi-Mansi         }
1134ab6554f1SJoshi-Mansi 
1135ab6554f1SJoshi-Mansi         size_t pos = fqdn.find('.');
1136ab6554f1SJoshi-Mansi         if (pos == std::string::npos)
1137ab6554f1SJoshi-Mansi         {
1138ab6554f1SJoshi-Mansi             messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1139ab6554f1SJoshi-Mansi             return;
1140ab6554f1SJoshi-Mansi         }
1141ab6554f1SJoshi-Mansi 
1142ab6554f1SJoshi-Mansi         std::string hostname;
1143ab6554f1SJoshi-Mansi         std::string domainname;
1144ab6554f1SJoshi-Mansi         domainname = (fqdn).substr(pos + 1);
1145ab6554f1SJoshi-Mansi         hostname = (fqdn).substr(0, pos);
1146ab6554f1SJoshi-Mansi 
1147ab6554f1SJoshi-Mansi         if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1148ab6554f1SJoshi-Mansi         {
1149ab6554f1SJoshi-Mansi             messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1150ab6554f1SJoshi-Mansi             return;
1151ab6554f1SJoshi-Mansi         }
1152ab6554f1SJoshi-Mansi 
1153ab6554f1SJoshi-Mansi         handleHostnamePatch(hostname, asyncResp);
1154ab6554f1SJoshi-Mansi         handleDomainnamePatch(ifaceId, domainname, asyncResp);
1155ab6554f1SJoshi-Mansi     }
1156ab6554f1SJoshi-Mansi 
1157ab6554f1SJoshi-Mansi     bool isHostnameValid(const std::string& hostname)
1158ab6554f1SJoshi-Mansi     {
1159ab6554f1SJoshi-Mansi         // A valid host name can never have the dotted-decimal form (RFC 1123)
1160ab6554f1SJoshi-Mansi         if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1161ab6554f1SJoshi-Mansi         {
1162ab6554f1SJoshi-Mansi             return false;
1163ab6554f1SJoshi-Mansi         }
1164ab6554f1SJoshi-Mansi         // Each label(hostname/subdomains) within a valid FQDN
1165ab6554f1SJoshi-Mansi         // MUST handle host names of up to 63 characters (RFC 1123)
1166ab6554f1SJoshi-Mansi         // labels cannot start or end with hyphens (RFC 952)
1167ab6554f1SJoshi-Mansi         // labels can start with numbers (RFC 1123)
1168ab6554f1SJoshi-Mansi         const std::regex pattern(
1169ab6554f1SJoshi-Mansi             "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1170ab6554f1SJoshi-Mansi 
1171ab6554f1SJoshi-Mansi         return std::regex_match(hostname, pattern);
1172ab6554f1SJoshi-Mansi     }
1173ab6554f1SJoshi-Mansi 
1174ab6554f1SJoshi-Mansi     bool isDomainnameValid(const std::string& domainname)
1175ab6554f1SJoshi-Mansi     {
1176ab6554f1SJoshi-Mansi         // Can have multiple subdomains
1177ab6554f1SJoshi-Mansi         // Top Level Domain's min length is 2 character
1178ab6554f1SJoshi-Mansi         const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]"
1179ab6554f1SJoshi-Mansi                                  "{1,30}\\.)*[a-zA-Z]{2,}$");
1180ab6554f1SJoshi-Mansi 
1181ab6554f1SJoshi-Mansi         return std::regex_match(domainname, pattern);
1182ab6554f1SJoshi-Mansi     }
1183ab6554f1SJoshi-Mansi 
1184*8d1b46d7Szhanghch05     void handleMACAddressPatch(
1185*8d1b46d7Szhanghch05         const std::string& ifaceId, const std::string& macAddress,
1186*8d1b46d7Szhanghch05         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1187d577665bSRatan Gupta     {
1188d577665bSRatan Gupta         crow::connections::systemBus->async_method_call(
1189d577665bSRatan Gupta             [asyncResp, macAddress](const boost::system::error_code ec) {
1190d577665bSRatan Gupta                 if (ec)
1191d577665bSRatan Gupta                 {
1192d577665bSRatan Gupta                     messages::internalError(asyncResp->res);
1193d577665bSRatan Gupta                     return;
1194d577665bSRatan Gupta                 }
1195d577665bSRatan Gupta             },
1196d577665bSRatan Gupta             "xyz.openbmc_project.Network",
1197d577665bSRatan Gupta             "/xyz/openbmc_project/network/" + ifaceId,
1198d577665bSRatan Gupta             "org.freedesktop.DBus.Properties", "Set",
1199d577665bSRatan Gupta             "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1200d577665bSRatan Gupta             std::variant<std::string>(macAddress));
1201d577665bSRatan Gupta     }
1202286b9118SJohnathan Mantey 
1203da131a9aSJennifer Lee     void setDHCPEnabled(const std::string& ifaceId,
12041f8c7b5dSJohnathan Mantey                         const std::string& propertyName, const bool v4Value,
12051f8c7b5dSJohnathan Mantey                         const bool v6Value,
1206*8d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1207da131a9aSJennifer Lee     {
12082c70f800SEd Tanous         const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1209da131a9aSJennifer Lee         crow::connections::systemBus->async_method_call(
1210da131a9aSJennifer Lee             [asyncResp](const boost::system::error_code ec) {
1211da131a9aSJennifer Lee                 if (ec)
1212da131a9aSJennifer Lee                 {
1213da131a9aSJennifer Lee                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1214da131a9aSJennifer Lee                     messages::internalError(asyncResp->res);
1215da131a9aSJennifer Lee                     return;
1216da131a9aSJennifer Lee                 }
12178f7e9c19SJayaprakash Mutyala                 messages::success(asyncResp->res);
1218da131a9aSJennifer Lee             },
1219da131a9aSJennifer Lee             "xyz.openbmc_project.Network",
1220da131a9aSJennifer Lee             "/xyz/openbmc_project/network/" + ifaceId,
1221da131a9aSJennifer Lee             "org.freedesktop.DBus.Properties", "Set",
1222da131a9aSJennifer Lee             "xyz.openbmc_project.Network.EthernetInterface", propertyName,
12231f8c7b5dSJohnathan Mantey             std::variant<std::string>{dhcp});
1224da131a9aSJennifer Lee     }
12251f8c7b5dSJohnathan Mantey 
1226eeedda23SJohnathan Mantey     void setEthernetInterfaceBoolProperty(
1227eeedda23SJohnathan Mantey         const std::string& ifaceId, const std::string& propertyName,
1228*8d1b46d7Szhanghch05         const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1229eeedda23SJohnathan Mantey     {
1230eeedda23SJohnathan Mantey         crow::connections::systemBus->async_method_call(
1231eeedda23SJohnathan Mantey             [asyncResp](const boost::system::error_code ec) {
1232eeedda23SJohnathan Mantey                 if (ec)
1233eeedda23SJohnathan Mantey                 {
1234eeedda23SJohnathan Mantey                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1235eeedda23SJohnathan Mantey                     messages::internalError(asyncResp->res);
1236eeedda23SJohnathan Mantey                     return;
1237eeedda23SJohnathan Mantey                 }
1238eeedda23SJohnathan Mantey             },
1239eeedda23SJohnathan Mantey             "xyz.openbmc_project.Network",
1240eeedda23SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
1241eeedda23SJohnathan Mantey             "org.freedesktop.DBus.Properties", "Set",
1242eeedda23SJohnathan Mantey             "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1243eeedda23SJohnathan Mantey             std::variant<bool>{value});
1244eeedda23SJohnathan Mantey     }
1245eeedda23SJohnathan Mantey 
1246da131a9aSJennifer Lee     void setDHCPv4Config(const std::string& propertyName, const bool& value,
1247*8d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1248da131a9aSJennifer Lee     {
1249da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1250da131a9aSJennifer Lee         crow::connections::systemBus->async_method_call(
1251da131a9aSJennifer Lee             [asyncResp](const boost::system::error_code ec) {
1252da131a9aSJennifer Lee                 if (ec)
1253da131a9aSJennifer Lee                 {
1254da131a9aSJennifer Lee                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1255da131a9aSJennifer Lee                     messages::internalError(asyncResp->res);
1256da131a9aSJennifer Lee                     return;
1257da131a9aSJennifer Lee                 }
1258da131a9aSJennifer Lee             },
1259da131a9aSJennifer Lee             "xyz.openbmc_project.Network",
1260da131a9aSJennifer Lee             "/xyz/openbmc_project/network/config/dhcp",
1261da131a9aSJennifer Lee             "org.freedesktop.DBus.Properties", "Set",
1262da131a9aSJennifer Lee             "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1263da131a9aSJennifer Lee             std::variant<bool>{value});
1264da131a9aSJennifer Lee     }
1265d577665bSRatan Gupta 
12661f8c7b5dSJohnathan Mantey     void handleDHCPPatch(const std::string& ifaceId,
12671f8c7b5dSJohnathan Mantey                          const EthernetInterfaceData& ethData,
1268f23b7296SEd Tanous                          const DHCPParameters& v4dhcpParms,
1269f23b7296SEd Tanous                          const DHCPParameters& v6dhcpParms,
1270*8d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1271da131a9aSJennifer Lee     {
12721f8c7b5dSJohnathan Mantey         bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
12731f8c7b5dSJohnathan Mantey         bool ipv6Active =
12741f8c7b5dSJohnathan Mantey             translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1275da131a9aSJennifer Lee 
12761f8c7b5dSJohnathan Mantey         bool nextv4DHCPState =
12771f8c7b5dSJohnathan Mantey             v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
12781f8c7b5dSJohnathan Mantey 
12791f8c7b5dSJohnathan Mantey         bool nextv6DHCPState{};
12801f8c7b5dSJohnathan Mantey         if (v6dhcpParms.dhcpv6OperatingMode)
1281da131a9aSJennifer Lee         {
12821f8c7b5dSJohnathan Mantey             if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
12831f8c7b5dSJohnathan Mantey                 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
12841f8c7b5dSJohnathan Mantey                 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
12851f8c7b5dSJohnathan Mantey             {
12861f8c7b5dSJohnathan Mantey                 messages::propertyValueFormatError(
12871f8c7b5dSJohnathan Mantey                     asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
12881f8c7b5dSJohnathan Mantey                     "OperatingMode");
1289da131a9aSJennifer Lee                 return;
1290da131a9aSJennifer Lee             }
12911f8c7b5dSJohnathan Mantey             nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
12921f8c7b5dSJohnathan Mantey         }
12931f8c7b5dSJohnathan Mantey         else
1294da131a9aSJennifer Lee         {
12951f8c7b5dSJohnathan Mantey             nextv6DHCPState = ipv6Active;
12961f8c7b5dSJohnathan Mantey         }
12971f8c7b5dSJohnathan Mantey 
12981f8c7b5dSJohnathan Mantey         bool nextDNS{};
12991f8c7b5dSJohnathan Mantey         if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
13001f8c7b5dSJohnathan Mantey         {
13011f8c7b5dSJohnathan Mantey             if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
13021f8c7b5dSJohnathan Mantey             {
13031f8c7b5dSJohnathan Mantey                 messages::generalError(asyncResp->res);
13041f8c7b5dSJohnathan Mantey                 return;
13051f8c7b5dSJohnathan Mantey             }
13061f8c7b5dSJohnathan Mantey             nextDNS = *v4dhcpParms.useDNSServers;
13071f8c7b5dSJohnathan Mantey         }
13081f8c7b5dSJohnathan Mantey         else if (v4dhcpParms.useDNSServers)
13091f8c7b5dSJohnathan Mantey         {
13101f8c7b5dSJohnathan Mantey             nextDNS = *v4dhcpParms.useDNSServers;
13111f8c7b5dSJohnathan Mantey         }
13121f8c7b5dSJohnathan Mantey         else if (v6dhcpParms.useDNSServers)
13131f8c7b5dSJohnathan Mantey         {
13141f8c7b5dSJohnathan Mantey             nextDNS = *v6dhcpParms.useDNSServers;
13151f8c7b5dSJohnathan Mantey         }
13161f8c7b5dSJohnathan Mantey         else
13171f8c7b5dSJohnathan Mantey         {
13181f8c7b5dSJohnathan Mantey             nextDNS = ethData.DNSEnabled;
13191f8c7b5dSJohnathan Mantey         }
13201f8c7b5dSJohnathan Mantey 
13211f8c7b5dSJohnathan Mantey         bool nextNTP{};
13221f8c7b5dSJohnathan Mantey         if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
13231f8c7b5dSJohnathan Mantey         {
13241f8c7b5dSJohnathan Mantey             if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
13251f8c7b5dSJohnathan Mantey             {
13261f8c7b5dSJohnathan Mantey                 messages::generalError(asyncResp->res);
13271f8c7b5dSJohnathan Mantey                 return;
13281f8c7b5dSJohnathan Mantey             }
13291f8c7b5dSJohnathan Mantey             nextNTP = *v4dhcpParms.useNTPServers;
13301f8c7b5dSJohnathan Mantey         }
13311f8c7b5dSJohnathan Mantey         else if (v4dhcpParms.useNTPServers)
13321f8c7b5dSJohnathan Mantey         {
13331f8c7b5dSJohnathan Mantey             nextNTP = *v4dhcpParms.useNTPServers;
13341f8c7b5dSJohnathan Mantey         }
13351f8c7b5dSJohnathan Mantey         else if (v6dhcpParms.useNTPServers)
13361f8c7b5dSJohnathan Mantey         {
13371f8c7b5dSJohnathan Mantey             nextNTP = *v6dhcpParms.useNTPServers;
13381f8c7b5dSJohnathan Mantey         }
13391f8c7b5dSJohnathan Mantey         else
13401f8c7b5dSJohnathan Mantey         {
13411f8c7b5dSJohnathan Mantey             nextNTP = ethData.NTPEnabled;
13421f8c7b5dSJohnathan Mantey         }
13431f8c7b5dSJohnathan Mantey 
13441f8c7b5dSJohnathan Mantey         bool nextUseDomain{};
13451f8c7b5dSJohnathan Mantey         if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
13461f8c7b5dSJohnathan Mantey         {
13471f8c7b5dSJohnathan Mantey             if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
13481f8c7b5dSJohnathan Mantey             {
13491f8c7b5dSJohnathan Mantey                 messages::generalError(asyncResp->res);
13501f8c7b5dSJohnathan Mantey                 return;
13511f8c7b5dSJohnathan Mantey             }
13521f8c7b5dSJohnathan Mantey             nextUseDomain = *v4dhcpParms.useUseDomainName;
13531f8c7b5dSJohnathan Mantey         }
13541f8c7b5dSJohnathan Mantey         else if (v4dhcpParms.useUseDomainName)
13551f8c7b5dSJohnathan Mantey         {
13561f8c7b5dSJohnathan Mantey             nextUseDomain = *v4dhcpParms.useUseDomainName;
13571f8c7b5dSJohnathan Mantey         }
13581f8c7b5dSJohnathan Mantey         else if (v6dhcpParms.useUseDomainName)
13591f8c7b5dSJohnathan Mantey         {
13601f8c7b5dSJohnathan Mantey             nextUseDomain = *v6dhcpParms.useUseDomainName;
13611f8c7b5dSJohnathan Mantey         }
13621f8c7b5dSJohnathan Mantey         else
13631f8c7b5dSJohnathan Mantey         {
13641f8c7b5dSJohnathan Mantey             nextUseDomain = ethData.HostNameEnabled;
13651f8c7b5dSJohnathan Mantey         }
13661f8c7b5dSJohnathan Mantey 
1367da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
13681f8c7b5dSJohnathan Mantey         setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
13691f8c7b5dSJohnathan Mantey                        asyncResp);
1370da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << "set DNSEnabled...";
13711f8c7b5dSJohnathan Mantey         setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1372da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << "set NTPEnabled...";
13731f8c7b5dSJohnathan Mantey         setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
13741f8c7b5dSJohnathan Mantey         BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
13751f8c7b5dSJohnathan Mantey         setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1376da131a9aSJennifer Lee     }
137701784826SJohnathan Mantey 
137801784826SJohnathan Mantey     boost::container::flat_set<IPv4AddressData>::const_iterator
13792c70f800SEd Tanous         getNextStaticIpEntry(
138017a897dfSManojkiran Eda             const boost::container::flat_set<IPv4AddressData>::const_iterator&
138117a897dfSManojkiran Eda                 head,
1382b5a76932SEd Tanous             const boost::container::flat_set<IPv4AddressData>::const_iterator&
1383b5a76932SEd Tanous                 end)
138401784826SJohnathan Mantey     {
138517a897dfSManojkiran Eda         return std::find_if(head, end, [](const IPv4AddressData& value) {
138617a897dfSManojkiran Eda             return value.origin == "Static";
138717a897dfSManojkiran Eda         });
138801784826SJohnathan Mantey     }
138901784826SJohnathan Mantey 
139001784826SJohnathan Mantey     boost::container::flat_set<IPv6AddressData>::const_iterator
13912c70f800SEd Tanous         getNextStaticIpEntry(
139217a897dfSManojkiran Eda             const boost::container::flat_set<IPv6AddressData>::const_iterator&
139317a897dfSManojkiran Eda                 head,
1394b5a76932SEd Tanous             const boost::container::flat_set<IPv6AddressData>::const_iterator&
1395b5a76932SEd Tanous                 end)
139601784826SJohnathan Mantey     {
139717a897dfSManojkiran Eda         return std::find_if(head, end, [](const IPv6AddressData& value) {
139817a897dfSManojkiran Eda             return value.origin == "Static";
139917a897dfSManojkiran Eda         });
140001784826SJohnathan Mantey     }
140101784826SJohnathan Mantey 
1402d1d50814SRavi Teja     void handleIPv4StaticPatch(
1403f476acbfSRatan Gupta         const std::string& ifaceId, nlohmann::json& input,
140401784826SJohnathan Mantey         const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1405*8d1b46d7Szhanghch05         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14061abe55efSEd Tanous     {
140701784826SJohnathan Mantey         if ((!input.is_array()) || input.empty())
1408f476acbfSRatan Gupta         {
140971f52d96SEd Tanous             messages::propertyValueTypeError(
141071f52d96SEd Tanous                 asyncResp->res,
141171f52d96SEd Tanous                 input.dump(2, ' ', true,
141271f52d96SEd Tanous                            nlohmann::json::error_handler_t::replace),
1413d1d50814SRavi Teja                 "IPv4StaticAddresses");
1414f476acbfSRatan Gupta             return;
1415f476acbfSRatan Gupta         }
1416f476acbfSRatan Gupta 
1417271584abSEd Tanous         unsigned entryIdx = 1;
141801784826SJohnathan Mantey         // Find the first static IP address currently active on the NIC and
141901784826SJohnathan Mantey         // match it to the first JSON element in the IPv4StaticAddresses array.
142001784826SJohnathan Mantey         // Match each subsequent JSON element to the next static IP programmed
142101784826SJohnathan Mantey         // into the NIC.
14222c70f800SEd Tanous         boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry =
14232c70f800SEd Tanous             getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
142401784826SJohnathan Mantey 
1425537174c4SEd Tanous         for (nlohmann::json& thisJson : input)
14261abe55efSEd Tanous         {
14274a0cb85cSEd Tanous             std::string pathString =
1428d1d50814SRavi Teja                 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1429179db1d7SKowalski, Kamil 
143001784826SJohnathan Mantey             if (!thisJson.is_null() && !thisJson.empty())
1431f476acbfSRatan Gupta             {
1432537174c4SEd Tanous                 std::optional<std::string> address;
1433537174c4SEd Tanous                 std::optional<std::string> subnetMask;
1434537174c4SEd Tanous                 std::optional<std::string> gateway;
1435537174c4SEd Tanous 
1436537174c4SEd Tanous                 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
14377e27d832SJohnathan Mantey                                          address, "SubnetMask", subnetMask,
14387e27d832SJohnathan Mantey                                          "Gateway", gateway))
1439537174c4SEd Tanous                 {
144001784826SJohnathan Mantey                     messages::propertyValueFormatError(
144171f52d96SEd Tanous                         asyncResp->res,
144271f52d96SEd Tanous                         thisJson.dump(2, ' ', true,
144371f52d96SEd Tanous                                       nlohmann::json::error_handler_t::replace),
144471f52d96SEd Tanous                         pathString);
1445537174c4SEd Tanous                     return;
1446179db1d7SKowalski, Kamil                 }
1447179db1d7SKowalski, Kamil 
144801784826SJohnathan Mantey                 // Find the address/subnet/gateway values. Any values that are
144901784826SJohnathan Mantey                 // not explicitly provided are assumed to be unmodified from the
145001784826SJohnathan Mantey                 // current state of the interface. Merge existing state into the
145101784826SJohnathan Mantey                 // current request.
1452271584abSEd Tanous                 const std::string* addr = nullptr;
1453271584abSEd Tanous                 const std::string* gw = nullptr;
145401784826SJohnathan Mantey                 uint8_t prefixLength = 0;
145501784826SJohnathan Mantey                 bool errorInEntry = false;
1456537174c4SEd Tanous                 if (address)
14571abe55efSEd Tanous                 {
145801784826SJohnathan Mantey                     if (ipv4VerifyIpAndGetBitcount(*address))
14591abe55efSEd Tanous                     {
146001784826SJohnathan Mantey                         addr = &(*address);
14614a0cb85cSEd Tanous                     }
146201784826SJohnathan Mantey                     else
146301784826SJohnathan Mantey                     {
146401784826SJohnathan Mantey                         messages::propertyValueFormatError(
146501784826SJohnathan Mantey                             asyncResp->res, *address, pathString + "/Address");
146601784826SJohnathan Mantey                         errorInEntry = true;
146701784826SJohnathan Mantey                     }
146801784826SJohnathan Mantey                 }
14692c70f800SEd Tanous                 else if (niciPentry != ipv4Data.cend())
147001784826SJohnathan Mantey                 {
14712c70f800SEd Tanous                     addr = &(niciPentry->address);
147201784826SJohnathan Mantey                 }
147301784826SJohnathan Mantey                 else
147401784826SJohnathan Mantey                 {
147501784826SJohnathan Mantey                     messages::propertyMissing(asyncResp->res,
147601784826SJohnathan Mantey                                               pathString + "/Address");
147701784826SJohnathan Mantey                     errorInEntry = true;
14784a0cb85cSEd Tanous                 }
14794a0cb85cSEd Tanous 
1480537174c4SEd Tanous                 if (subnetMask)
14814a0cb85cSEd Tanous                 {
1482537174c4SEd Tanous                     if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
14834a0cb85cSEd Tanous                     {
1484f12894f8SJason M. Bills                         messages::propertyValueFormatError(
1485537174c4SEd Tanous                             asyncResp->res, *subnetMask,
14864a0cb85cSEd Tanous                             pathString + "/SubnetMask");
148701784826SJohnathan Mantey                         errorInEntry = true;
14884a0cb85cSEd Tanous                     }
14894a0cb85cSEd Tanous                 }
14902c70f800SEd Tanous                 else if (niciPentry != ipv4Data.cend())
14914a0cb85cSEd Tanous                 {
14922c70f800SEd Tanous                     if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask,
149301784826SJohnathan Mantey                                                     &prefixLength))
14944a0cb85cSEd Tanous                     {
149501784826SJohnathan Mantey                         messages::propertyValueFormatError(
14962c70f800SEd Tanous                             asyncResp->res, niciPentry->netmask,
149701784826SJohnathan Mantey                             pathString + "/SubnetMask");
149801784826SJohnathan Mantey                         errorInEntry = true;
14994a0cb85cSEd Tanous                     }
15004a0cb85cSEd Tanous                 }
15011abe55efSEd Tanous                 else
15021abe55efSEd Tanous                 {
150301784826SJohnathan Mantey                     messages::propertyMissing(asyncResp->res,
150401784826SJohnathan Mantey                                               pathString + "/SubnetMask");
150501784826SJohnathan Mantey                     errorInEntry = true;
150601784826SJohnathan Mantey                 }
150701784826SJohnathan Mantey 
150801784826SJohnathan Mantey                 if (gateway)
150901784826SJohnathan Mantey                 {
151001784826SJohnathan Mantey                     if (ipv4VerifyIpAndGetBitcount(*gateway))
151101784826SJohnathan Mantey                     {
151201784826SJohnathan Mantey                         gw = &(*gateway);
151301784826SJohnathan Mantey                     }
151401784826SJohnathan Mantey                     else
151501784826SJohnathan Mantey                     {
151601784826SJohnathan Mantey                         messages::propertyValueFormatError(
151701784826SJohnathan Mantey                             asyncResp->res, *gateway, pathString + "/Gateway");
151801784826SJohnathan Mantey                         errorInEntry = true;
151901784826SJohnathan Mantey                     }
152001784826SJohnathan Mantey                 }
15212c70f800SEd Tanous                 else if (niciPentry != ipv4Data.cend())
152201784826SJohnathan Mantey                 {
15232c70f800SEd Tanous                     gw = &niciPentry->gateway;
152401784826SJohnathan Mantey                 }
152501784826SJohnathan Mantey                 else
15261abe55efSEd Tanous                 {
1527a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
15284a0cb85cSEd Tanous                                               pathString + "/Gateway");
152901784826SJohnathan Mantey                     errorInEntry = true;
15304a0cb85cSEd Tanous                 }
15314a0cb85cSEd Tanous 
153201784826SJohnathan Mantey                 if (errorInEntry)
15331abe55efSEd Tanous                 {
153401784826SJohnathan Mantey                     return;
15354a0cb85cSEd Tanous                 }
15364a0cb85cSEd Tanous 
15372c70f800SEd Tanous                 if (niciPentry != ipv4Data.cend())
15381abe55efSEd Tanous                 {
15392c70f800SEd Tanous                     deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength,
154001784826SJohnathan Mantey                                         *gw, *addr, asyncResp);
15412c70f800SEd Tanous                     niciPentry =
15422c70f800SEd Tanous                         getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
1543588c3f0dSKowalski, Kamil                 }
154401784826SJohnathan Mantey                 else
154501784826SJohnathan Mantey                 {
1546cb13a392SEd Tanous                     createIPv4(ifaceId, prefixLength, *gateway, *address,
1547cb13a392SEd Tanous                                asyncResp);
15484a0cb85cSEd Tanous                 }
15494a0cb85cSEd Tanous                 entryIdx++;
15504a0cb85cSEd Tanous             }
155101784826SJohnathan Mantey             else
155201784826SJohnathan Mantey             {
15532c70f800SEd Tanous                 if (niciPentry == ipv4Data.cend())
155401784826SJohnathan Mantey                 {
155501784826SJohnathan Mantey                     // Requesting a DELETE/DO NOT MODIFY action for an item
155601784826SJohnathan Mantey                     // that isn't present on the eth(n) interface. Input JSON is
155701784826SJohnathan Mantey                     // in error, so bail out.
155801784826SJohnathan Mantey                     if (thisJson.is_null())
155901784826SJohnathan Mantey                     {
156001784826SJohnathan Mantey                         messages::resourceCannotBeDeleted(asyncResp->res);
156101784826SJohnathan Mantey                         return;
156201784826SJohnathan Mantey                     }
156301784826SJohnathan Mantey                     messages::propertyValueFormatError(
156471f52d96SEd Tanous                         asyncResp->res,
156571f52d96SEd Tanous                         thisJson.dump(2, ' ', true,
156671f52d96SEd Tanous                                       nlohmann::json::error_handler_t::replace),
156771f52d96SEd Tanous                         pathString);
156801784826SJohnathan Mantey                     return;
156901784826SJohnathan Mantey                 }
157001784826SJohnathan Mantey 
157101784826SJohnathan Mantey                 if (thisJson.is_null())
157201784826SJohnathan Mantey                 {
15732c70f800SEd Tanous                     deleteIPv4(ifaceId, niciPentry->id, asyncResp);
157401784826SJohnathan Mantey                 }
15752c70f800SEd Tanous                 if (niciPentry != ipv4Data.cend())
157601784826SJohnathan Mantey                 {
15772c70f800SEd Tanous                     niciPentry =
15782c70f800SEd Tanous                         getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
157901784826SJohnathan Mantey                 }
158001784826SJohnathan Mantey                 entryIdx++;
158101784826SJohnathan Mantey             }
158201784826SJohnathan Mantey         }
15834a0cb85cSEd Tanous     }
15844a0cb85cSEd Tanous 
1585f85837bfSRAJESWARAN THILLAIGOVINDAN     void handleStaticNameServersPatch(
1586f85837bfSRAJESWARAN THILLAIGOVINDAN         const std::string& ifaceId,
1587f85837bfSRAJESWARAN THILLAIGOVINDAN         const std::vector<std::string>& updatedStaticNameServers,
1588*8d1b46d7Szhanghch05         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1589f85837bfSRAJESWARAN THILLAIGOVINDAN     {
1590f85837bfSRAJESWARAN THILLAIGOVINDAN         crow::connections::systemBus->async_method_call(
1591286b9118SJohnathan Mantey             [asyncResp](const boost::system::error_code ec) {
1592f85837bfSRAJESWARAN THILLAIGOVINDAN                 if (ec)
1593f85837bfSRAJESWARAN THILLAIGOVINDAN                 {
1594f85837bfSRAJESWARAN THILLAIGOVINDAN                     messages::internalError(asyncResp->res);
1595f85837bfSRAJESWARAN THILLAIGOVINDAN                     return;
1596f85837bfSRAJESWARAN THILLAIGOVINDAN                 }
1597f85837bfSRAJESWARAN THILLAIGOVINDAN             },
1598f85837bfSRAJESWARAN THILLAIGOVINDAN             "xyz.openbmc_project.Network",
1599f85837bfSRAJESWARAN THILLAIGOVINDAN             "/xyz/openbmc_project/network/" + ifaceId,
1600f85837bfSRAJESWARAN THILLAIGOVINDAN             "org.freedesktop.DBus.Properties", "Set",
16010f6efdc1Smanojkiran.eda@gmail.com             "xyz.openbmc_project.Network.EthernetInterface",
16020f6efdc1Smanojkiran.eda@gmail.com             "StaticNameServers",
1603f85837bfSRAJESWARAN THILLAIGOVINDAN             std::variant<std::vector<std::string>>{updatedStaticNameServers});
1604f85837bfSRAJESWARAN THILLAIGOVINDAN     }
1605f85837bfSRAJESWARAN THILLAIGOVINDAN 
1606e48c0fc5SRavi Teja     void handleIPv6StaticAddressesPatch(
1607f23b7296SEd Tanous         const std::string& ifaceId, const nlohmann::json& input,
160801784826SJohnathan Mantey         const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1609*8d1b46d7Szhanghch05         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1610e48c0fc5SRavi Teja     {
161101784826SJohnathan Mantey         if (!input.is_array() || input.empty())
1612e48c0fc5SRavi Teja         {
161371f52d96SEd Tanous             messages::propertyValueTypeError(
161471f52d96SEd Tanous                 asyncResp->res,
161571f52d96SEd Tanous                 input.dump(2, ' ', true,
161671f52d96SEd Tanous                            nlohmann::json::error_handler_t::replace),
1617e48c0fc5SRavi Teja                 "IPv6StaticAddresses");
1618e48c0fc5SRavi Teja             return;
1619e48c0fc5SRavi Teja         }
1620271584abSEd Tanous         size_t entryIdx = 1;
16212c70f800SEd Tanous         boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry =
16222c70f800SEd Tanous             getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1623f23b7296SEd Tanous         for (const nlohmann::json& thisJson : input)
1624e48c0fc5SRavi Teja         {
1625e48c0fc5SRavi Teja             std::string pathString =
1626e48c0fc5SRavi Teja                 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1627e48c0fc5SRavi Teja 
162801784826SJohnathan Mantey             if (!thisJson.is_null() && !thisJson.empty())
1629e48c0fc5SRavi Teja             {
1630e48c0fc5SRavi Teja                 std::optional<std::string> address;
1631e48c0fc5SRavi Teja                 std::optional<uint8_t> prefixLength;
1632f23b7296SEd Tanous                 nlohmann::json thisJsonCopy = thisJson;
1633f23b7296SEd Tanous                 if (!json_util::readJson(thisJsonCopy, asyncResp->res,
1634f23b7296SEd Tanous                                          "Address", address, "PrefixLength",
1635f23b7296SEd Tanous                                          prefixLength))
1636e48c0fc5SRavi Teja                 {
163701784826SJohnathan Mantey                     messages::propertyValueFormatError(
163871f52d96SEd Tanous                         asyncResp->res,
163971f52d96SEd Tanous                         thisJson.dump(2, ' ', true,
164071f52d96SEd Tanous                                       nlohmann::json::error_handler_t::replace),
164171f52d96SEd Tanous                         pathString);
1642e48c0fc5SRavi Teja                     return;
1643e48c0fc5SRavi Teja                 }
1644e48c0fc5SRavi Teja 
164501784826SJohnathan Mantey                 const std::string* addr;
164601784826SJohnathan Mantey                 uint8_t prefix;
164701784826SJohnathan Mantey 
164801784826SJohnathan Mantey                 // Find the address and prefixLength values. Any values that are
164901784826SJohnathan Mantey                 // not explicitly provided are assumed to be unmodified from the
165001784826SJohnathan Mantey                 // current state of the interface. Merge existing state into the
165101784826SJohnathan Mantey                 // current request.
1652e48c0fc5SRavi Teja                 if (address)
1653e48c0fc5SRavi Teja                 {
165401784826SJohnathan Mantey                     addr = &(*address);
1655e48c0fc5SRavi Teja                 }
16562c70f800SEd Tanous                 else if (niciPentry != ipv6Data.end())
165701784826SJohnathan Mantey                 {
16582c70f800SEd Tanous                     addr = &(niciPentry->address);
165901784826SJohnathan Mantey                 }
166001784826SJohnathan Mantey                 else
166101784826SJohnathan Mantey                 {
166201784826SJohnathan Mantey                     messages::propertyMissing(asyncResp->res,
166301784826SJohnathan Mantey                                               pathString + "/Address");
166401784826SJohnathan Mantey                     return;
1665e48c0fc5SRavi Teja                 }
1666e48c0fc5SRavi Teja 
1667e48c0fc5SRavi Teja                 if (prefixLength)
1668e48c0fc5SRavi Teja                 {
166901784826SJohnathan Mantey                     prefix = *prefixLength;
167001784826SJohnathan Mantey                 }
16712c70f800SEd Tanous                 else if (niciPentry != ipv6Data.end())
1672e48c0fc5SRavi Teja                 {
16732c70f800SEd Tanous                     prefix = niciPentry->prefixLength;
1674e48c0fc5SRavi Teja                 }
1675e48c0fc5SRavi Teja                 else
1676e48c0fc5SRavi Teja                 {
1677e48c0fc5SRavi Teja                     messages::propertyMissing(asyncResp->res,
1678e48c0fc5SRavi Teja                                               pathString + "/PrefixLength");
167901784826SJohnathan Mantey                     return;
1680e48c0fc5SRavi Teja                 }
1681e48c0fc5SRavi Teja 
16822c70f800SEd Tanous                 if (niciPentry != ipv6Data.end())
1683e48c0fc5SRavi Teja                 {
16842c70f800SEd Tanous                     deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr,
1685e48c0fc5SRavi Teja                                         asyncResp);
16862c70f800SEd Tanous                     niciPentry =
16872c70f800SEd Tanous                         getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
168801784826SJohnathan Mantey                 }
168901784826SJohnathan Mantey                 else
169001784826SJohnathan Mantey                 {
169101784826SJohnathan Mantey                     createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1692e48c0fc5SRavi Teja                 }
1693e48c0fc5SRavi Teja                 entryIdx++;
1694e48c0fc5SRavi Teja             }
169501784826SJohnathan Mantey             else
169601784826SJohnathan Mantey             {
16972c70f800SEd Tanous                 if (niciPentry == ipv6Data.end())
169801784826SJohnathan Mantey                 {
169901784826SJohnathan Mantey                     // Requesting a DELETE/DO NOT MODIFY action for an item
170001784826SJohnathan Mantey                     // that isn't present on the eth(n) interface. Input JSON is
170101784826SJohnathan Mantey                     // in error, so bail out.
170201784826SJohnathan Mantey                     if (thisJson.is_null())
170301784826SJohnathan Mantey                     {
170401784826SJohnathan Mantey                         messages::resourceCannotBeDeleted(asyncResp->res);
170501784826SJohnathan Mantey                         return;
170601784826SJohnathan Mantey                     }
170701784826SJohnathan Mantey                     messages::propertyValueFormatError(
170871f52d96SEd Tanous                         asyncResp->res,
170971f52d96SEd Tanous                         thisJson.dump(2, ' ', true,
171071f52d96SEd Tanous                                       nlohmann::json::error_handler_t::replace),
171171f52d96SEd Tanous                         pathString);
171201784826SJohnathan Mantey                     return;
171301784826SJohnathan Mantey                 }
171401784826SJohnathan Mantey 
171501784826SJohnathan Mantey                 if (thisJson.is_null())
171601784826SJohnathan Mantey                 {
17172c70f800SEd Tanous                     deleteIPv6(ifaceId, niciPentry->id, asyncResp);
171801784826SJohnathan Mantey                 }
17192c70f800SEd Tanous                 if (niciPentry != ipv6Data.cend())
172001784826SJohnathan Mantey                 {
17212c70f800SEd Tanous                     niciPentry =
17222c70f800SEd Tanous                         getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
172301784826SJohnathan Mantey                 }
172401784826SJohnathan Mantey                 entryIdx++;
172501784826SJohnathan Mantey             }
172601784826SJohnathan Mantey         }
1727e48c0fc5SRavi Teja     }
1728e48c0fc5SRavi Teja 
17290f74e643SEd Tanous     void parseInterfaceData(
1730*8d1b46d7Szhanghch05         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1731*8d1b46d7Szhanghch05         const std::string& ifaceId, const EthernetInterfaceData& ethData,
1732e48c0fc5SRavi Teja         const boost::container::flat_set<IPv4AddressData>& ipv4Data,
173301784826SJohnathan Mantey         const boost::container::flat_set<IPv6AddressData>& ipv6Data)
17344a0cb85cSEd Tanous     {
1735eeedda23SJohnathan Mantey         constexpr const std::array<const char*, 1> inventoryForEthernet = {
1736eeedda23SJohnathan Mantey             "xyz.openbmc_project.Inventory.Item.Ethernet"};
1737eeedda23SJohnathan Mantey 
17382c70f800SEd Tanous         nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
173981ce609eSEd Tanous         jsonResponse["Id"] = ifaceId;
17402c70f800SEd Tanous         jsonResponse["@odata.id"] =
174181ce609eSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
17422c70f800SEd Tanous         jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1743eeedda23SJohnathan Mantey 
1744eeedda23SJohnathan Mantey         auto health = std::make_shared<HealthPopulate>(asyncResp);
1745eeedda23SJohnathan Mantey 
1746eeedda23SJohnathan Mantey         crow::connections::systemBus->async_method_call(
1747eeedda23SJohnathan Mantey             [health](const boost::system::error_code ec,
1748eeedda23SJohnathan Mantey                      std::vector<std::string>& resp) {
1749eeedda23SJohnathan Mantey                 if (ec)
1750029573d4SEd Tanous                 {
1751eeedda23SJohnathan Mantey                     return;
1752eeedda23SJohnathan Mantey                 }
1753eeedda23SJohnathan Mantey 
1754eeedda23SJohnathan Mantey                 health->inventory = std::move(resp);
1755eeedda23SJohnathan Mantey             },
1756eeedda23SJohnathan Mantey             "xyz.openbmc_project.ObjectMapper",
1757eeedda23SJohnathan Mantey             "/xyz/openbmc_project/object_mapper",
1758eeedda23SJohnathan Mantey             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1759eeedda23SJohnathan Mantey             int32_t(0), inventoryForEthernet);
1760eeedda23SJohnathan Mantey 
1761eeedda23SJohnathan Mantey         health->populate();
1762eeedda23SJohnathan Mantey 
1763eeedda23SJohnathan Mantey         if (ethData.nicEnabled)
1764eeedda23SJohnathan Mantey         {
17652c70f800SEd Tanous             jsonResponse["LinkStatus"] = "LinkUp";
17662c70f800SEd Tanous             jsonResponse["Status"]["State"] = "Enabled";
1767029573d4SEd Tanous         }
1768029573d4SEd Tanous         else
1769029573d4SEd Tanous         {
17702c70f800SEd Tanous             jsonResponse["LinkStatus"] = "NoLink";
17712c70f800SEd Tanous             jsonResponse["Status"]["State"] = "Disabled";
1772029573d4SEd Tanous         }
1773aa05fb27SJohnathan Mantey 
17742c70f800SEd Tanous         jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
17752c70f800SEd Tanous         jsonResponse["SpeedMbps"] = ethData.speed;
17762c70f800SEd Tanous         jsonResponse["MACAddress"] = ethData.mac_address;
17772c70f800SEd Tanous         jsonResponse["DHCPv4"]["DHCPEnabled"] =
17781f8c7b5dSJohnathan Mantey             translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
17792c70f800SEd Tanous         jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
17802c70f800SEd Tanous         jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
17812c70f800SEd Tanous         jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
17821f8c7b5dSJohnathan Mantey 
17832c70f800SEd Tanous         jsonResponse["DHCPv6"]["OperatingMode"] =
17841f8c7b5dSJohnathan Mantey             translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
17851f8c7b5dSJohnathan Mantey                                                                    : "Disabled";
17862c70f800SEd Tanous         jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
17872c70f800SEd Tanous         jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
17882c70f800SEd Tanous         jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
17892a133282Smanojkiraneda 
17904a0cb85cSEd Tanous         if (!ethData.hostname.empty())
17914a0cb85cSEd Tanous         {
17922c70f800SEd Tanous             jsonResponse["HostName"] = ethData.hostname;
1793ab6554f1SJoshi-Mansi 
1794ab6554f1SJoshi-Mansi             // When domain name is empty then it means, that it is a network
1795ab6554f1SJoshi-Mansi             // without domain names, and the host name itself must be treated as
1796ab6554f1SJoshi-Mansi             // FQDN
1797f23b7296SEd Tanous             std::string fqdn = ethData.hostname;
1798d24bfc7aSJennifer Lee             if (!ethData.domainnames.empty())
1799d24bfc7aSJennifer Lee             {
18002c70f800SEd Tanous                 fqdn += "." + ethData.domainnames[0];
1801d24bfc7aSJennifer Lee             }
18022c70f800SEd Tanous             jsonResponse["FQDN"] = fqdn;
18034a0cb85cSEd Tanous         }
18044a0cb85cSEd Tanous 
18052c70f800SEd Tanous         jsonResponse["VLANs"] = {
1806fda13ad2SSunitha Harish             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
180781ce609eSEd Tanous                               ifaceId + "/VLANs"}};
1808fda13ad2SSunitha Harish 
18092c70f800SEd Tanous         jsonResponse["NameServers"] = ethData.nameServers;
18102c70f800SEd Tanous         jsonResponse["StaticNameServers"] = ethData.staticNameServers;
18114a0cb85cSEd Tanous 
18122c70f800SEd Tanous         nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
18132c70f800SEd Tanous         nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
18142c70f800SEd Tanous         ipv4Array = nlohmann::json::array();
18152c70f800SEd Tanous         ipv4StaticArray = nlohmann::json::array();
18162c70f800SEd Tanous         for (auto& ipv4Config : ipv4Data)
18174a0cb85cSEd Tanous         {
1818fa5053a6SGunnar Mills 
18192c70f800SEd Tanous             std::string gatewayStr = ipv4Config.gateway;
1820fa5053a6SGunnar Mills             if (gatewayStr.empty())
1821fa5053a6SGunnar Mills             {
1822fa5053a6SGunnar Mills                 gatewayStr = "0.0.0.0";
1823fa5053a6SGunnar Mills             }
1824fa5053a6SGunnar Mills 
18252c70f800SEd Tanous             ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
18262c70f800SEd Tanous                                  {"SubnetMask", ipv4Config.netmask},
18272c70f800SEd Tanous                                  {"Address", ipv4Config.address},
1828fa5053a6SGunnar Mills                                  {"Gateway", gatewayStr}});
18292c70f800SEd Tanous             if (ipv4Config.origin == "Static")
1830d1d50814SRavi Teja             {
18312c70f800SEd Tanous                 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
18322c70f800SEd Tanous                                            {"SubnetMask", ipv4Config.netmask},
18332c70f800SEd Tanous                                            {"Address", ipv4Config.address},
1834d1d50814SRavi Teja                                            {"Gateway", gatewayStr}});
1835d1d50814SRavi Teja             }
183601784826SJohnathan Mantey         }
1837d1d50814SRavi Teja 
18387ea79e5eSRavi Teja         std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
18397ea79e5eSRavi Teja         if (ipv6GatewayStr.empty())
18407ea79e5eSRavi Teja         {
18417ea79e5eSRavi Teja             ipv6GatewayStr = "0:0:0:0:0:0:0:0";
18427ea79e5eSRavi Teja         }
18437ea79e5eSRavi Teja 
18447ea79e5eSRavi Teja         jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1845e48c0fc5SRavi Teja 
18462c70f800SEd Tanous         nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
18472c70f800SEd Tanous         nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
18482c70f800SEd Tanous         ipv6Array = nlohmann::json::array();
18492c70f800SEd Tanous         ipv6StaticArray = nlohmann::json::array();
18507f2e23e9SJohnathan Mantey         nlohmann::json& ipv6AddrPolicyTable =
18512c70f800SEd Tanous             jsonResponse["IPv6AddressPolicyTable"];
18527f2e23e9SJohnathan Mantey         ipv6AddrPolicyTable = nlohmann::json::array();
18532c70f800SEd Tanous         for (auto& ipv6Config : ipv6Data)
1854e48c0fc5SRavi Teja         {
18552c70f800SEd Tanous             ipv6Array.push_back({{"Address", ipv6Config.address},
18562c70f800SEd Tanous                                  {"PrefixLength", ipv6Config.prefixLength},
18572c70f800SEd Tanous                                  {"AddressOrigin", ipv6Config.origin},
18585fd16e4bSJohnathan Mantey                                  {"AddressState", nullptr}});
18592c70f800SEd Tanous             if (ipv6Config.origin == "Static")
1860e48c0fc5SRavi Teja             {
18612c70f800SEd Tanous                 ipv6StaticArray.push_back(
18622c70f800SEd Tanous                     {{"Address", ipv6Config.address},
18632c70f800SEd Tanous                      {"PrefixLength", ipv6Config.prefixLength},
18642c70f800SEd Tanous                      {"AddressOrigin", ipv6Config.origin},
18655fd16e4bSJohnathan Mantey                      {"AddressState", nullptr}});
186601784826SJohnathan Mantey             }
1867e48c0fc5SRavi Teja         }
1868588c3f0dSKowalski, Kamil     }
1869588c3f0dSKowalski, Kamil 
18709391bb9cSRapkiewicz, Pawel     /**
18719391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
18729391bb9cSRapkiewicz, Pawel      */
1873*8d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1874*8d1b46d7Szhanghch05                const crow::Request&,
18751abe55efSEd Tanous                const std::vector<std::string>& params) override
18761abe55efSEd Tanous     {
18771abe55efSEd Tanous         if (params.size() != 1)
18781abe55efSEd Tanous         {
1879f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
18809391bb9cSRapkiewicz, Pawel             return;
18819391bb9cSRapkiewicz, Pawel         }
18829391bb9cSRapkiewicz, Pawel 
18834a0cb85cSEd Tanous         getEthernetIfaceData(
18844a0cb85cSEd Tanous             params[0],
18852c70f800SEd Tanous             [this, asyncResp, ifaceId{std::string(params[0])}](
18864a0cb85cSEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1887e48c0fc5SRavi Teja                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
188801784826SJohnathan Mantey                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
18894a0cb85cSEd Tanous                 if (!success)
18901abe55efSEd Tanous                 {
18911abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
18921abe55efSEd Tanous                     // object, and other errors
1893f12894f8SJason M. Bills                     messages::resourceNotFound(asyncResp->res,
18942c70f800SEd Tanous                                                "EthernetInterface", ifaceId);
18954a0cb85cSEd Tanous                     return;
18969391bb9cSRapkiewicz, Pawel                 }
18974c9afe43SEd Tanous 
18980f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
1899fda13ad2SSunitha Harish                     "#EthernetInterface.v1_4_1.EthernetInterface";
19000f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
19010f74e643SEd Tanous                 asyncResp->res.jsonValue["Description"] =
19020f74e643SEd Tanous                     "Management Network Interface";
19030f74e643SEd Tanous 
19042c70f800SEd Tanous                 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data,
1905eeedda23SJohnathan Mantey                                    ipv6Data);
19069391bb9cSRapkiewicz, Pawel             });
19079391bb9cSRapkiewicz, Pawel     }
19089391bb9cSRapkiewicz, Pawel 
1909*8d1b46d7Szhanghch05     void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1910*8d1b46d7Szhanghch05                  const crow::Request& req,
19111abe55efSEd Tanous                  const std::vector<std::string>& params) override
19121abe55efSEd Tanous     {
1913*8d1b46d7Szhanghch05 
19141abe55efSEd Tanous         if (params.size() != 1)
19151abe55efSEd Tanous         {
1916f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1917588c3f0dSKowalski, Kamil             return;
1918588c3f0dSKowalski, Kamil         }
1919588c3f0dSKowalski, Kamil 
19202c70f800SEd Tanous         const std::string& ifaceId = params[0];
1921588c3f0dSKowalski, Kamil 
1922bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
1923ab6554f1SJoshi-Mansi         std::optional<std::string> fqdn;
1924d577665bSRatan Gupta         std::optional<std::string> macAddress;
19259a6fc6feSRavi Teja         std::optional<std::string> ipv6DefaultGateway;
1926d1d50814SRavi Teja         std::optional<nlohmann::json> ipv4StaticAddresses;
1927e48c0fc5SRavi Teja         std::optional<nlohmann::json> ipv6StaticAddresses;
1928f85837bfSRAJESWARAN THILLAIGOVINDAN         std::optional<std::vector<std::string>> staticNameServers;
1929da131a9aSJennifer Lee         std::optional<nlohmann::json> dhcpv4;
19301f8c7b5dSJohnathan Mantey         std::optional<nlohmann::json> dhcpv6;
1931eeedda23SJohnathan Mantey         std::optional<bool> interfaceEnabled;
19321f8c7b5dSJohnathan Mantey         DHCPParameters v4dhcpParms;
19331f8c7b5dSJohnathan Mantey         DHCPParameters v6dhcpParms;
19340627a2c7SEd Tanous 
19351f8c7b5dSJohnathan Mantey         if (!json_util::readJson(
1936*8d1b46d7Szhanghch05                 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1937ab6554f1SJoshi-Mansi                 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1938ab6554f1SJoshi-Mansi                 macAddress, "StaticNameServers", staticNameServers,
1939ab6554f1SJoshi-Mansi                 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1940ab6554f1SJoshi-Mansi                 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1941ab6554f1SJoshi-Mansi                 "InterfaceEnabled", interfaceEnabled))
19421abe55efSEd Tanous         {
1943588c3f0dSKowalski, Kamil             return;
1944588c3f0dSKowalski, Kamil         }
1945da131a9aSJennifer Lee         if (dhcpv4)
1946da131a9aSJennifer Lee         {
1947*8d1b46d7Szhanghch05             if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
19481f8c7b5dSJohnathan Mantey                                      v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
19491f8c7b5dSJohnathan Mantey                                      v4dhcpParms.useDNSServers, "UseNTPServers",
19501f8c7b5dSJohnathan Mantey                                      v4dhcpParms.useNTPServers, "UseDomainName",
19511f8c7b5dSJohnathan Mantey                                      v4dhcpParms.useUseDomainName))
19521f8c7b5dSJohnathan Mantey             {
19531f8c7b5dSJohnathan Mantey                 return;
19541f8c7b5dSJohnathan Mantey             }
19551f8c7b5dSJohnathan Mantey         }
19561f8c7b5dSJohnathan Mantey 
19571f8c7b5dSJohnathan Mantey         if (dhcpv6)
19581f8c7b5dSJohnathan Mantey         {
1959*8d1b46d7Szhanghch05             if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
19601f8c7b5dSJohnathan Mantey                                      v6dhcpParms.dhcpv6OperatingMode,
19611f8c7b5dSJohnathan Mantey                                      "UseDNSServers", v6dhcpParms.useDNSServers,
19621f8c7b5dSJohnathan Mantey                                      "UseNTPServers", v6dhcpParms.useNTPServers,
19631f8c7b5dSJohnathan Mantey                                      "UseDomainName",
19641f8c7b5dSJohnathan Mantey                                      v6dhcpParms.useUseDomainName))
19651f8c7b5dSJohnathan Mantey             {
19661f8c7b5dSJohnathan Mantey                 return;
19671f8c7b5dSJohnathan Mantey             }
1968da131a9aSJennifer Lee         }
1969da131a9aSJennifer Lee 
197001784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
197101784826SJohnathan Mantey         // JSON preparation
19724a0cb85cSEd Tanous         getEthernetIfaceData(
19732c70f800SEd Tanous             ifaceId,
19742c70f800SEd Tanous             [this, asyncResp, ifaceId, hostname = std::move(hostname),
1975ab6554f1SJoshi-Mansi              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1976d1d50814SRavi Teja              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
19779a6fc6feSRavi Teja              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1978e48c0fc5SRavi Teja              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
19791f8c7b5dSJohnathan Mantey              staticNameServers = std::move(staticNameServers),
19801f8c7b5dSJohnathan Mantey              dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
19811f8c7b5dSJohnathan Mantey              v4dhcpParms = std::move(v4dhcpParms),
1982f23b7296SEd Tanous              v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
19834a0cb85cSEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1984e48c0fc5SRavi Teja                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
198501784826SJohnathan Mantey                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
19861abe55efSEd Tanous                 if (!success)
19871abe55efSEd Tanous                 {
1988588c3f0dSKowalski, Kamil                     // ... otherwise return error
19891abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
19901abe55efSEd Tanous                     // object, and other errors
1991fda13ad2SSunitha Harish                     messages::resourceNotFound(asyncResp->res,
19922c70f800SEd Tanous                                                "Ethernet Interface", ifaceId);
1993588c3f0dSKowalski, Kamil                     return;
1994588c3f0dSKowalski, Kamil                 }
1995588c3f0dSKowalski, Kamil 
19961f8c7b5dSJohnathan Mantey                 if (dhcpv4 || dhcpv6)
19971f8c7b5dSJohnathan Mantey                 {
1998f23b7296SEd Tanous                     handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
1999f23b7296SEd Tanous                                     asyncResp);
20001f8c7b5dSJohnathan Mantey                 }
20011f8c7b5dSJohnathan Mantey 
20020627a2c7SEd Tanous                 if (hostname)
20031abe55efSEd Tanous                 {
20040627a2c7SEd Tanous                     handleHostnamePatch(*hostname, asyncResp);
20051abe55efSEd Tanous                 }
20060627a2c7SEd Tanous 
2007ab6554f1SJoshi-Mansi                 if (fqdn)
2008ab6554f1SJoshi-Mansi                 {
20092c70f800SEd Tanous                     handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2010ab6554f1SJoshi-Mansi                 }
2011ab6554f1SJoshi-Mansi 
2012d577665bSRatan Gupta                 if (macAddress)
2013d577665bSRatan Gupta                 {
20142c70f800SEd Tanous                     handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
2015d577665bSRatan Gupta                 }
2016d577665bSRatan Gupta 
2017d1d50814SRavi Teja                 if (ipv4StaticAddresses)
2018d1d50814SRavi Teja                 {
2019537174c4SEd Tanous                     // TODO(ed) for some reason the capture of ipv4Addresses
202001784826SJohnathan Mantey                     // above is returning a const value, not a non-const
202101784826SJohnathan Mantey                     // value. This doesn't really work for us, as we need to
202201784826SJohnathan Mantey                     // be able to efficiently move out the intermedia
202301784826SJohnathan Mantey                     // nlohmann::json objects. This makes a copy of the
202401784826SJohnathan Mantey                     // structure, and operates on that, but could be done
202501784826SJohnathan Mantey                     // more efficiently
2026f23b7296SEd Tanous                     nlohmann::json ipv4Static = *ipv4StaticAddresses;
20272c70f800SEd Tanous                     handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2028d1d50814SRavi Teja                                           asyncResp);
20291abe55efSEd Tanous                 }
20300627a2c7SEd Tanous 
2031f85837bfSRAJESWARAN THILLAIGOVINDAN                 if (staticNameServers)
2032f85837bfSRAJESWARAN THILLAIGOVINDAN                 {
20332c70f800SEd Tanous                     handleStaticNameServersPatch(ifaceId, *staticNameServers,
2034f85837bfSRAJESWARAN THILLAIGOVINDAN                                                  asyncResp);
2035f85837bfSRAJESWARAN THILLAIGOVINDAN                 }
20369a6fc6feSRavi Teja 
20379a6fc6feSRavi Teja                 if (ipv6DefaultGateway)
20389a6fc6feSRavi Teja                 {
20399a6fc6feSRavi Teja                     messages::propertyNotWritable(asyncResp->res,
20409a6fc6feSRavi Teja                                                   "IPv6DefaultGateway");
20419a6fc6feSRavi Teja                 }
2042e48c0fc5SRavi Teja 
2043e48c0fc5SRavi Teja                 if (ipv6StaticAddresses)
2044e48c0fc5SRavi Teja                 {
2045f23b7296SEd Tanous                     nlohmann::json ipv6Static = *ipv6StaticAddresses;
20462c70f800SEd Tanous                     handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
204701784826SJohnathan Mantey                                                    ipv6Data, asyncResp);
2048e48c0fc5SRavi Teja                 }
2049eeedda23SJohnathan Mantey 
2050eeedda23SJohnathan Mantey                 if (interfaceEnabled)
2051eeedda23SJohnathan Mantey                 {
2052eeedda23SJohnathan Mantey                     setEthernetInterfaceBoolProperty(
20532c70f800SEd Tanous                         ifaceId, "NICEnabled", *interfaceEnabled, asyncResp);
2054eeedda23SJohnathan Mantey                 }
2055588c3f0dSKowalski, Kamil             });
2056588c3f0dSKowalski, Kamil     }
20579391bb9cSRapkiewicz, Pawel };
20589391bb9cSRapkiewicz, Pawel 
2059e439f0f8SKowalski, Kamil /**
20604a0cb85cSEd Tanous  * VlanNetworkInterface derived class for delivering VLANNetworkInterface
20614a0cb85cSEd Tanous  * Schema
2062e439f0f8SKowalski, Kamil  */
20631abe55efSEd Tanous class VlanNetworkInterface : public Node
20641abe55efSEd Tanous {
2065e439f0f8SKowalski, Kamil   public:
2066e439f0f8SKowalski, Kamil     /*
2067e439f0f8SKowalski, Kamil      * Default Constructor
2068e439f0f8SKowalski, Kamil      */
206952cc112dSEd Tanous     VlanNetworkInterface(App& app) :
20704a0cb85cSEd Tanous         Node(app,
20717af91514SGunnar Mills              "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/",
20721abe55efSEd Tanous              std::string(), std::string())
20731abe55efSEd Tanous     {
2074e439f0f8SKowalski, Kamil         entityPrivileges = {
2075e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
2076e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
2077e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2078e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2079e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2080e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2081e439f0f8SKowalski, Kamil     }
2082e439f0f8SKowalski, Kamil 
2083e439f0f8SKowalski, Kamil   private:
208481ce609eSEd Tanous     void parseInterfaceData(nlohmann::json& jsonResponse,
208581ce609eSEd Tanous                             const std::string& parentIfaceId,
208681ce609eSEd Tanous                             const std::string& ifaceId,
2087cb13a392SEd Tanous                             const EthernetInterfaceData& ethData)
20881abe55efSEd Tanous     {
2089e439f0f8SKowalski, Kamil         // Fill out obvious data...
209081ce609eSEd Tanous         jsonResponse["Id"] = ifaceId;
209181ce609eSEd Tanous         jsonResponse["@odata.id"] =
209281ce609eSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parentIfaceId +
209381ce609eSEd Tanous             "/VLANs/" + ifaceId;
2094e439f0f8SKowalski, Kamil 
209581ce609eSEd Tanous         jsonResponse["VLANEnable"] = true;
2096fda13ad2SSunitha Harish         if (!ethData.vlan_id.empty())
20974a0cb85cSEd Tanous         {
209881ce609eSEd Tanous             jsonResponse["VLANId"] = ethData.vlan_id.back();
20994a0cb85cSEd Tanous         }
2100e439f0f8SKowalski, Kamil     }
2101e439f0f8SKowalski, Kamil 
2102fda13ad2SSunitha Harish     bool verifyNames(const std::string& parent, const std::string& iface)
21031abe55efSEd Tanous     {
21041abe55efSEd Tanous         if (!boost::starts_with(iface, parent + "_"))
21051abe55efSEd Tanous         {
2106927a505aSKowalski, Kamil             return false;
21071abe55efSEd Tanous         }
2108927a505aSKowalski, Kamil         return true;
2109927a505aSKowalski, Kamil     }
2110927a505aSKowalski, Kamil 
2111e439f0f8SKowalski, Kamil     /**
2112e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
2113e439f0f8SKowalski, Kamil      */
2114*8d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2115*8d1b46d7Szhanghch05                const crow::Request&,
21161abe55efSEd Tanous                const std::vector<std::string>& params) override
21171abe55efSEd Tanous     {
21184a0cb85cSEd Tanous         // TODO(Pawel) this shall be parameterized call (two params) to get
2119e439f0f8SKowalski, Kamil         // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
2120e439f0f8SKowalski, Kamil         // Check if there is required param, truly entering this shall be
2121e439f0f8SKowalski, Kamil         // impossible.
21221abe55efSEd Tanous         if (params.size() != 2)
21231abe55efSEd Tanous         {
2124*8d1b46d7Szhanghch05             messages::internalError(asyncResp->res);
2125e439f0f8SKowalski, Kamil             return;
2126e439f0f8SKowalski, Kamil         }
2127e439f0f8SKowalski, Kamil 
21282c70f800SEd Tanous         const std::string& parentIfaceId = params[0];
21292c70f800SEd Tanous         const std::string& ifaceId = params[1];
2130*8d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
21310f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
2132*8d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
2133e439f0f8SKowalski, Kamil 
21342c70f800SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
21351abe55efSEd Tanous         {
2136a434f2bdSEd Tanous             return;
2137a434f2bdSEd Tanous         }
2138a434f2bdSEd Tanous 
213901784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
214001784826SJohnathan Mantey         // JSON preparation
21414a0cb85cSEd Tanous         getEthernetIfaceData(
2142fda13ad2SSunitha Harish             params[1],
21432c70f800SEd Tanous             [this, asyncResp, parentIfaceId{std::string(params[0])},
21442c70f800SEd Tanous              ifaceId{std::string(params[1])}](
21454a0cb85cSEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
2146cb13a392SEd Tanous                 const boost::container::flat_set<IPv4AddressData>&,
2147cb13a392SEd Tanous                 const boost::container::flat_set<IPv6AddressData>&) {
2148fda13ad2SSunitha Harish                 if (success && ethData.vlan_id.size() != 0)
21491abe55efSEd Tanous                 {
21502c70f800SEd Tanous                     parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
21512c70f800SEd Tanous                                        ifaceId, ethData);
21521abe55efSEd Tanous                 }
21531abe55efSEd Tanous                 else
21541abe55efSEd Tanous                 {
2155e439f0f8SKowalski, Kamil                     // ... otherwise return error
21561abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
21571abe55efSEd Tanous                     // object, and other errors
2158f12894f8SJason M. Bills                     messages::resourceNotFound(
21592c70f800SEd Tanous                         asyncResp->res, "VLAN Network Interface", ifaceId);
2160e439f0f8SKowalski, Kamil                 }
2161e439f0f8SKowalski, Kamil             });
2162e439f0f8SKowalski, Kamil     }
2163e439f0f8SKowalski, Kamil 
2164*8d1b46d7Szhanghch05     void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2165*8d1b46d7Szhanghch05                  const crow::Request& req,
21661abe55efSEd Tanous                  const std::vector<std::string>& params) override
21671abe55efSEd Tanous     {
2168*8d1b46d7Szhanghch05 
21691abe55efSEd Tanous         if (params.size() != 2)
21701abe55efSEd Tanous         {
2171f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2172e439f0f8SKowalski, Kamil             return;
2173e439f0f8SKowalski, Kamil         }
2174e439f0f8SKowalski, Kamil 
2175d76323e5SEd Tanous         const std::string& parentIfaceId = params[0];
217655c7b7a2SEd Tanous         const std::string& ifaceId = params[1];
2177927a505aSKowalski, Kamil 
2178fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
21791abe55efSEd Tanous         {
2180fda13ad2SSunitha Harish             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2181fda13ad2SSunitha Harish                                        ifaceId);
2182927a505aSKowalski, Kamil             return;
2183927a505aSKowalski, Kamil         }
2184927a505aSKowalski, Kamil 
21850627a2c7SEd Tanous         bool vlanEnable = false;
218638268fa8SAndrew Geissler         uint32_t vlanId = 0;
21870627a2c7SEd Tanous 
2188*8d1b46d7Szhanghch05         if (!json_util::readJson(req, asyncResp->res, "VLANEnable", vlanEnable,
2189*8d1b46d7Szhanghch05                                  "VLANId", vlanId))
21901abe55efSEd Tanous         {
2191927a505aSKowalski, Kamil             return;
2192927a505aSKowalski, Kamil         }
2193927a505aSKowalski, Kamil 
219401784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
219501784826SJohnathan Mantey         // JSON preparation
2196e48c0fc5SRavi Teja         getEthernetIfaceData(
2197e48c0fc5SRavi Teja             params[1],
2198271584abSEd Tanous             [asyncResp, parentIfaceId{std::string(params[0])},
2199cb13a392SEd Tanous              ifaceId{std::string(params[1])}, &vlanEnable,
2200cb13a392SEd Tanous              &vlanId](const bool& success, const EthernetInterfaceData& ethData,
2201cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
2202cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
220308244d02SSunitha Harish                 if (success && !ethData.vlan_id.empty())
220408244d02SSunitha Harish                 {
220508244d02SSunitha Harish                     auto callback =
220608244d02SSunitha Harish                         [asyncResp](const boost::system::error_code ec) {
220708244d02SSunitha Harish                             if (ec)
220808244d02SSunitha Harish                             {
220908244d02SSunitha Harish                                 messages::internalError(asyncResp->res);
221008244d02SSunitha Harish                             }
221108244d02SSunitha Harish                         };
221208244d02SSunitha Harish 
221308244d02SSunitha Harish                     if (vlanEnable == true)
221408244d02SSunitha Harish                     {
221508244d02SSunitha Harish                         crow::connections::systemBus->async_method_call(
221608244d02SSunitha Harish                             std::move(callback), "xyz.openbmc_project.Network",
221708244d02SSunitha Harish                             "/xyz/openbmc_project/network/" + ifaceId,
221808244d02SSunitha Harish                             "org.freedesktop.DBus.Properties", "Set",
221908244d02SSunitha Harish                             "xyz.openbmc_project.Network.VLAN", "Id",
222008244d02SSunitha Harish                             std::variant<uint32_t>(vlanId));
222108244d02SSunitha Harish                     }
222208244d02SSunitha Harish                     else
222308244d02SSunitha Harish                     {
2224e48c0fc5SRavi Teja                         BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2225e48c0fc5SRavi Teja                                             "vlan interface";
222608244d02SSunitha Harish                         crow::connections::systemBus->async_method_call(
222708244d02SSunitha Harish                             std::move(callback), "xyz.openbmc_project.Network",
2228e48c0fc5SRavi Teja                             std::string("/xyz/openbmc_project/network/") +
2229e48c0fc5SRavi Teja                                 ifaceId,
223008244d02SSunitha Harish                             "xyz.openbmc_project.Object.Delete", "Delete");
223108244d02SSunitha Harish                     }
223208244d02SSunitha Harish                 }
223308244d02SSunitha Harish                 else
22341abe55efSEd Tanous                 {
22351abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
22361abe55efSEd Tanous                     // object, and other errors
2237e48c0fc5SRavi Teja                     messages::resourceNotFound(
2238e48c0fc5SRavi Teja                         asyncResp->res, "VLAN Network Interface", ifaceId);
2239927a505aSKowalski, Kamil                     return;
2240927a505aSKowalski, Kamil                 }
2241927a505aSKowalski, Kamil             });
2242e439f0f8SKowalski, Kamil     }
2243e439f0f8SKowalski, Kamil 
2244*8d1b46d7Szhanghch05     void doDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2245*8d1b46d7Szhanghch05                   const crow::Request&,
22461abe55efSEd Tanous                   const std::vector<std::string>& params) override
22471abe55efSEd Tanous     {
22481abe55efSEd Tanous         if (params.size() != 2)
22491abe55efSEd Tanous         {
2250f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2251e439f0f8SKowalski, Kamil             return;
2252e439f0f8SKowalski, Kamil         }
2253e439f0f8SKowalski, Kamil 
2254d76323e5SEd Tanous         const std::string& parentIfaceId = params[0];
225555c7b7a2SEd Tanous         const std::string& ifaceId = params[1];
2256927a505aSKowalski, Kamil 
2257fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
22581abe55efSEd Tanous         {
2259fda13ad2SSunitha Harish             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2260fda13ad2SSunitha Harish                                        ifaceId);
2261927a505aSKowalski, Kamil             return;
2262927a505aSKowalski, Kamil         }
2263927a505aSKowalski, Kamil 
226401784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
226501784826SJohnathan Mantey         // JSON preparation
2266f12894f8SJason M. Bills         getEthernetIfaceData(
2267fda13ad2SSunitha Harish             params[1],
2268271584abSEd Tanous             [asyncResp, parentIfaceId{std::string(params[0])},
2269fda13ad2SSunitha Harish              ifaceId{std::string(params[1])}](
2270f12894f8SJason M. Bills                 const bool& success, const EthernetInterfaceData& ethData,
2271cb13a392SEd Tanous                 const boost::container::flat_set<IPv4AddressData>&,
2272cb13a392SEd Tanous                 const boost::container::flat_set<IPv6AddressData>&) {
2273fda13ad2SSunitha Harish                 if (success && !ethData.vlan_id.empty())
22741abe55efSEd Tanous                 {
2275f12894f8SJason M. Bills                     auto callback =
2276f12894f8SJason M. Bills                         [asyncResp](const boost::system::error_code ec) {
22771abe55efSEd Tanous                             if (ec)
22781abe55efSEd Tanous                             {
2279f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
2280927a505aSKowalski, Kamil                             }
22814a0cb85cSEd Tanous                         };
22824a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
22834a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
22844a0cb85cSEd Tanous                         std::string("/xyz/openbmc_project/network/") + ifaceId,
22854a0cb85cSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
22861abe55efSEd Tanous                 }
22871abe55efSEd Tanous                 else
22881abe55efSEd Tanous                 {
2289927a505aSKowalski, Kamil                     // ... otherwise return error
2290f12894f8SJason M. Bills                     // TODO(Pawel)consider distinguish between non existing
2291f12894f8SJason M. Bills                     // object, and other errors
2292f12894f8SJason M. Bills                     messages::resourceNotFound(
2293f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", ifaceId);
2294927a505aSKowalski, Kamil                 }
2295927a505aSKowalski, Kamil             });
2296e439f0f8SKowalski, Kamil     }
2297e439f0f8SKowalski, Kamil };
2298e439f0f8SKowalski, Kamil 
2299e439f0f8SKowalski, Kamil /**
2300e439f0f8SKowalski, Kamil  * VlanNetworkInterfaceCollection derived class for delivering
2301e439f0f8SKowalski, Kamil  * VLANNetworkInterface Collection Schema
2302e439f0f8SKowalski, Kamil  */
23031abe55efSEd Tanous class VlanNetworkInterfaceCollection : public Node
23041abe55efSEd Tanous {
2305e439f0f8SKowalski, Kamil   public:
230652cc112dSEd Tanous     VlanNetworkInterfaceCollection(App& app) :
23074a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
23084a0cb85cSEd Tanous              std::string())
23091abe55efSEd Tanous     {
2310e439f0f8SKowalski, Kamil         entityPrivileges = {
2311e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
2312e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
2313e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2314e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2315e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2316e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2317e439f0f8SKowalski, Kamil     }
2318e439f0f8SKowalski, Kamil 
2319e439f0f8SKowalski, Kamil   private:
2320e439f0f8SKowalski, Kamil     /**
2321e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
2322e439f0f8SKowalski, Kamil      */
2323*8d1b46d7Szhanghch05     void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2324*8d1b46d7Szhanghch05                const crow::Request&,
23251abe55efSEd Tanous                const std::vector<std::string>& params) override
23261abe55efSEd Tanous     {
23271abe55efSEd Tanous         if (params.size() != 1)
23281abe55efSEd Tanous         {
2329e439f0f8SKowalski, Kamil             // This means there is a problem with the router
2330f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2331e439f0f8SKowalski, Kamil             return;
2332e439f0f8SKowalski, Kamil         }
2333e439f0f8SKowalski, Kamil 
23344a0cb85cSEd Tanous         const std::string& rootInterfaceName = params[0];
2335e439f0f8SKowalski, Kamil 
23364a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
23371abe55efSEd Tanous         // preparation
2338f12894f8SJason M. Bills         getEthernetIfaceList(
233943b761d0SEd Tanous             [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
23401abe55efSEd Tanous                 const bool& success,
234181ce609eSEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
23424a0cb85cSEd Tanous                 if (!success)
23431abe55efSEd Tanous                 {
2344f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
23454a0cb85cSEd Tanous                     return;
23461abe55efSEd Tanous                 }
23474c9afe43SEd Tanous 
234881ce609eSEd Tanous                 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
23494c9afe43SEd Tanous                 {
23504c9afe43SEd Tanous                     messages::resourceNotFound(asyncResp->res,
23514c9afe43SEd Tanous                                                "VLanNetworkInterfaceCollection",
23524c9afe43SEd Tanous                                                rootInterfaceName);
23534c9afe43SEd Tanous                     return;
23544c9afe43SEd Tanous                 }
23554c9afe43SEd Tanous 
23560f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
23570f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
23580f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
23590f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] =
23600f74e643SEd Tanous                     "VLAN Network Interface Collection";
23614a0cb85cSEd Tanous 
23622c70f800SEd Tanous                 nlohmann::json ifaceArray = nlohmann::json::array();
23634a0cb85cSEd Tanous 
236481ce609eSEd Tanous                 for (const std::string& ifaceItem : ifaceList)
23651abe55efSEd Tanous                 {
23662c70f800SEd Tanous                     if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
23674a0cb85cSEd Tanous                     {
2368f23b7296SEd Tanous                         std::string path =
2369f23b7296SEd Tanous                             "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2370f23b7296SEd Tanous                         path += rootInterfaceName;
2371f23b7296SEd Tanous                         path += "/VLANs/";
2372f23b7296SEd Tanous                         path += ifaceItem;
2373f23b7296SEd Tanous                         ifaceArray.push_back({{"@odata.id", std::move(path)}});
2374e439f0f8SKowalski, Kamil                     }
2375e439f0f8SKowalski, Kamil                 }
2376e439f0f8SKowalski, Kamil 
23774a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
23782c70f800SEd Tanous                     ifaceArray.size();
23792c70f800SEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
23804a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
23814a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
23824a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
2383e439f0f8SKowalski, Kamil             });
2384e439f0f8SKowalski, Kamil     }
2385e439f0f8SKowalski, Kamil 
2386*8d1b46d7Szhanghch05     void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2387*8d1b46d7Szhanghch05                 const crow::Request& req,
23881abe55efSEd Tanous                 const std::vector<std::string>& params) override
23891abe55efSEd Tanous     {
23901abe55efSEd Tanous         if (params.size() != 1)
23911abe55efSEd Tanous         {
2392f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2393e439f0f8SKowalski, Kamil             return;
2394e439f0f8SKowalski, Kamil         }
2395fda13ad2SSunitha Harish         bool vlanEnable = false;
23960627a2c7SEd Tanous         uint32_t vlanId = 0;
2397*8d1b46d7Szhanghch05         if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId,
2398*8d1b46d7Szhanghch05                                  "VLANEnable", vlanEnable))
23991abe55efSEd Tanous         {
24004a0cb85cSEd Tanous             return;
2401e439f0f8SKowalski, Kamil         }
2402fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
2403fda13ad2SSunitha Harish         if (!vlanId)
2404fda13ad2SSunitha Harish         {
2405fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
2406fda13ad2SSunitha Harish         }
2407fda13ad2SSunitha Harish         if (!vlanEnable)
2408fda13ad2SSunitha Harish         {
2409fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
2410fda13ad2SSunitha Harish         }
2411271584abSEd Tanous         if (static_cast<bool>(vlanId) ^ vlanEnable)
2412fda13ad2SSunitha Harish         {
2413fda13ad2SSunitha Harish             return;
2414fda13ad2SSunitha Harish         }
2415fda13ad2SSunitha Harish 
24164a0cb85cSEd Tanous         const std::string& rootInterfaceName = params[0];
24174a0cb85cSEd Tanous         auto callback = [asyncResp](const boost::system::error_code ec) {
24181abe55efSEd Tanous             if (ec)
24191abe55efSEd Tanous             {
24204a0cb85cSEd Tanous                 // TODO(ed) make more consistent error messages based on
24214a0cb85cSEd Tanous                 // phosphor-network responses
2422f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
24234a0cb85cSEd Tanous                 return;
24241abe55efSEd Tanous             }
2425f12894f8SJason M. Bills             messages::created(asyncResp->res);
2426e439f0f8SKowalski, Kamil         };
24274a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
24284a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
24294a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
24304a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
24310627a2c7SEd Tanous             rootInterfaceName, vlanId);
24324a0cb85cSEd Tanous     }
24334a0cb85cSEd Tanous };
24349391bb9cSRapkiewicz, Pawel } // namespace redfish
2435