xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 3174e4dfd3185c131a07371b4b5a5b40cf0e0bdb)
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     }
161*3174e4dfSEd Tanous     if (isIPv4)
1621f8c7b5dSJohnathan Mantey     {
1631f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1641f8c7b5dSJohnathan Mantey     }
165*3174e4dfSEd 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 
2034c9afe43SEd Tanous inline bool extractEthernetInterfaceData(const std::string& ethiface_id,
2044a0cb85cSEd Tanous                                          const GetManagedObjects& dbus_data,
2054a0cb85cSEd Tanous                                          EthernetInterfaceData& ethData)
2064a0cb85cSEd Tanous {
2074c9afe43SEd Tanous     bool idFound = false;
2084a0cb85cSEd Tanous     for (const auto& objpath : dbus_data)
2094a0cb85cSEd Tanous     {
2104a0cb85cSEd Tanous         for (const auto& ifacePair : objpath.second)
2114a0cb85cSEd Tanous         {
212029573d4SEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
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                             {
2930f6efdc1Smanojkiran.eda@gmail.com                                 ethData.nameServers = std::move(*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                             {
3030f6efdc1Smanojkiran.eda@gmail.com                                 ethData.staticNameServers =
3040f6efdc1Smanojkiran.eda@gmail.com                                     std::move(*staticNameServers);
3054a0cb85cSEd Tanous                             }
3064a0cb85cSEd Tanous                         }
3072a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3082a133282Smanojkiraneda                         {
3092c70f800SEd Tanous                             const std::string* dhcpEnabled =
3101f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3112c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3122a133282Smanojkiraneda                             {
3132c70f800SEd Tanous                                 ethData.DHCPEnabled = *dhcpEnabled;
3142a133282Smanojkiraneda                             }
3152a133282Smanojkiraneda                         }
316d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
317d24bfc7aSJennifer Lee                         {
318d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3198d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
320d24bfc7aSJennifer Lee                                     &propertyPair.second);
321d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
322d24bfc7aSJennifer Lee                             {
323d24bfc7aSJennifer Lee                                 ethData.domainnames = std::move(*domainNames);
324d24bfc7aSJennifer Lee                             }
325d24bfc7aSJennifer Lee                         }
326029573d4SEd Tanous                     }
327029573d4SEd Tanous                 }
328029573d4SEd Tanous             }
3291f8c7b5dSJohnathan Mantey 
3301f8c7b5dSJohnathan Mantey             if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
3311f8c7b5dSJohnathan Mantey             {
3321f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3331f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3341f8c7b5dSJohnathan Mantey                 {
3351f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3361f8c7b5dSJohnathan Mantey                     {
3371f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3381f8c7b5dSJohnathan Mantey                         {
3392c70f800SEd Tanous                             const bool* dnsEnabled =
3401f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3412c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3421f8c7b5dSJohnathan Mantey                             {
3432c70f800SEd Tanous                                 ethData.DNSEnabled = *dnsEnabled;
3441f8c7b5dSJohnathan Mantey                             }
3451f8c7b5dSJohnathan Mantey                         }
3461f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3471f8c7b5dSJohnathan Mantey                         {
3482c70f800SEd Tanous                             const bool* ntpEnabled =
3491f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3502c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3511f8c7b5dSJohnathan Mantey                             {
3522c70f800SEd Tanous                                 ethData.NTPEnabled = *ntpEnabled;
3531f8c7b5dSJohnathan Mantey                             }
3541f8c7b5dSJohnathan Mantey                         }
3551f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3561f8c7b5dSJohnathan Mantey                         {
3572c70f800SEd Tanous                             const bool* hostNameEnabled =
3581f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3592c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3601f8c7b5dSJohnathan Mantey                             {
3612c70f800SEd Tanous                                 ethData.HostNameEnabled = *hostNameEnabled;
3621f8c7b5dSJohnathan Mantey                             }
3631f8c7b5dSJohnathan Mantey                         }
3641f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "SendHostNameEnabled")
3651f8c7b5dSJohnathan Mantey                         {
3662c70f800SEd Tanous                             const bool* sendHostNameEnabled =
3671f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3682c70f800SEd Tanous                             if (sendHostNameEnabled != nullptr)
3691f8c7b5dSJohnathan Mantey                             {
3701f8c7b5dSJohnathan Mantey                                 ethData.SendHostNameEnabled =
3712c70f800SEd Tanous                                     *sendHostNameEnabled;
3721f8c7b5dSJohnathan Mantey                             }
3731f8c7b5dSJohnathan Mantey                         }
3741f8c7b5dSJohnathan Mantey                     }
3751f8c7b5dSJohnathan Mantey                 }
3761f8c7b5dSJohnathan Mantey             }
377029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
378029573d4SEd Tanous             // to check eth number
379029573d4SEd Tanous             if (ifacePair.first ==
3804a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
3814a0cb85cSEd Tanous             {
3824a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
3834a0cb85cSEd Tanous                 {
3844a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
3854a0cb85cSEd Tanous                     {
3864a0cb85cSEd Tanous                         const std::string* hostname =
3878d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
3884a0cb85cSEd Tanous                         if (hostname != nullptr)
3894a0cb85cSEd Tanous                         {
3904a0cb85cSEd Tanous                             ethData.hostname = *hostname;
3914a0cb85cSEd Tanous                         }
3924a0cb85cSEd Tanous                     }
3934a0cb85cSEd Tanous                     else if (propertyPair.first == "DefaultGateway")
3944a0cb85cSEd Tanous                     {
3954a0cb85cSEd Tanous                         const std::string* defaultGateway =
3968d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
3974a0cb85cSEd Tanous                         if (defaultGateway != nullptr)
3984a0cb85cSEd Tanous                         {
3994a0cb85cSEd Tanous                             ethData.default_gateway = *defaultGateway;
4004a0cb85cSEd Tanous                         }
4014a0cb85cSEd Tanous                     }
4029a6fc6feSRavi Teja                     else if (propertyPair.first == "DefaultGateway6")
4039a6fc6feSRavi Teja                     {
4049a6fc6feSRavi Teja                         const std::string* defaultGateway6 =
4058d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4069a6fc6feSRavi Teja                         if (defaultGateway6 != nullptr)
4079a6fc6feSRavi Teja                         {
4089a6fc6feSRavi Teja                             ethData.ipv6_default_gateway = *defaultGateway6;
4099a6fc6feSRavi Teja                         }
4109a6fc6feSRavi Teja                     }
4114a0cb85cSEd Tanous                 }
4124a0cb85cSEd Tanous             }
4134a0cb85cSEd Tanous         }
4144a0cb85cSEd Tanous     }
4154c9afe43SEd Tanous     return idFound;
4164a0cb85cSEd Tanous }
4174a0cb85cSEd Tanous 
418e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
41901784826SJohnathan Mantey inline void
42001784826SJohnathan Mantey     extractIPV6Data(const std::string& ethiface_id,
42101784826SJohnathan Mantey                     const GetManagedObjects& dbus_data,
42201784826SJohnathan Mantey                     boost::container::flat_set<IPv6AddressData>& ipv6_config)
423e48c0fc5SRavi Teja {
424e48c0fc5SRavi Teja     const std::string ipv6PathStart =
425e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ethiface_id + "/ipv6/";
426e48c0fc5SRavi Teja 
427e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
428e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
429e48c0fc5SRavi Teja     for (const auto& objpath : dbus_data)
430e48c0fc5SRavi Teja     {
431e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
432e48c0fc5SRavi Teja         if (boost::starts_with(objpath.first.str, ipv6PathStart))
433e48c0fc5SRavi Teja         {
434e48c0fc5SRavi Teja             for (auto& interface : objpath.second)
435e48c0fc5SRavi Teja             {
436e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
437e48c0fc5SRavi Teja                 {
438e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
439e48c0fc5SRavi Teja                     // appropriate
440e48c0fc5SRavi Teja                     std::pair<
441e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
442e48c0fc5SRavi Teja                         bool>
443271584abSEd Tanous                         it = ipv6_config.insert(IPv6AddressData{});
4442c70f800SEd Tanous                     IPv6AddressData& ipv6Address = *it.first;
4452c70f800SEd Tanous                     ipv6Address.id =
446271584abSEd Tanous                         objpath.first.str.substr(ipv6PathStart.size());
447e48c0fc5SRavi Teja                     for (auto& property : interface.second)
448e48c0fc5SRavi Teja                     {
449e48c0fc5SRavi Teja                         if (property.first == "Address")
450e48c0fc5SRavi Teja                         {
451e48c0fc5SRavi Teja                             const std::string* address =
452e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
453e48c0fc5SRavi Teja                             if (address != nullptr)
454e48c0fc5SRavi Teja                             {
4552c70f800SEd Tanous                                 ipv6Address.address = *address;
456e48c0fc5SRavi Teja                             }
457e48c0fc5SRavi Teja                         }
458e48c0fc5SRavi Teja                         else if (property.first == "Origin")
459e48c0fc5SRavi Teja                         {
460e48c0fc5SRavi Teja                             const std::string* origin =
461e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
462e48c0fc5SRavi Teja                             if (origin != nullptr)
463e48c0fc5SRavi Teja                             {
4642c70f800SEd Tanous                                 ipv6Address.origin =
465e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
466e48c0fc5SRavi Teja                                                                         false);
467e48c0fc5SRavi Teja                             }
468e48c0fc5SRavi Teja                         }
469e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
470e48c0fc5SRavi Teja                         {
471e48c0fc5SRavi Teja                             const uint8_t* prefix =
472e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
473e48c0fc5SRavi Teja                             if (prefix != nullptr)
474e48c0fc5SRavi Teja                             {
4752c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
476e48c0fc5SRavi Teja                             }
477e48c0fc5SRavi Teja                         }
478e48c0fc5SRavi Teja                         else
479e48c0fc5SRavi Teja                         {
480e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
481e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
482e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
483e48c0fc5SRavi Teja                         }
484e48c0fc5SRavi Teja                     }
485e48c0fc5SRavi Teja                 }
486e48c0fc5SRavi Teja             }
487e48c0fc5SRavi Teja         }
488e48c0fc5SRavi Teja     }
489e48c0fc5SRavi Teja }
490e48c0fc5SRavi Teja 
4914a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
49201784826SJohnathan Mantey inline void
49301784826SJohnathan Mantey     extractIPData(const std::string& ethiface_id,
49401784826SJohnathan Mantey                   const GetManagedObjects& dbus_data,
49501784826SJohnathan Mantey                   boost::container::flat_set<IPv4AddressData>& ipv4_config)
4964a0cb85cSEd Tanous {
4974a0cb85cSEd Tanous     const std::string ipv4PathStart =
4984a0cb85cSEd Tanous         "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
4994a0cb85cSEd Tanous 
5004a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5014a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
5024a0cb85cSEd Tanous     for (const auto& objpath : dbus_data)
5034a0cb85cSEd Tanous     {
5044a0cb85cSEd Tanous         // Check if proper pattern for object path appears
5054a0cb85cSEd Tanous         if (boost::starts_with(objpath.first.str, ipv4PathStart))
5064a0cb85cSEd Tanous         {
5074a0cb85cSEd Tanous             for (auto& interface : objpath.second)
5084a0cb85cSEd Tanous             {
5094a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5104a0cb85cSEd Tanous                 {
5114a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5124a0cb85cSEd Tanous                     // appropriate
5134a0cb85cSEd Tanous                     std::pair<
5144a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5154a0cb85cSEd Tanous                         bool>
516271584abSEd Tanous                         it = ipv4_config.insert(IPv4AddressData{});
5172c70f800SEd Tanous                     IPv4AddressData& ipv4Address = *it.first;
5182c70f800SEd Tanous                     ipv4Address.id =
519271584abSEd Tanous                         objpath.first.str.substr(ipv4PathStart.size());
5204a0cb85cSEd Tanous                     for (auto& property : interface.second)
5214a0cb85cSEd Tanous                     {
5224a0cb85cSEd Tanous                         if (property.first == "Address")
5234a0cb85cSEd Tanous                         {
5244a0cb85cSEd Tanous                             const std::string* address =
525abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5264a0cb85cSEd Tanous                             if (address != nullptr)
5274a0cb85cSEd Tanous                             {
5282c70f800SEd Tanous                                 ipv4Address.address = *address;
5294a0cb85cSEd Tanous                             }
5304a0cb85cSEd Tanous                         }
5314a0cb85cSEd Tanous                         else if (property.first == "Gateway")
5324a0cb85cSEd Tanous                         {
5334a0cb85cSEd Tanous                             const std::string* gateway =
534abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5354a0cb85cSEd Tanous                             if (gateway != nullptr)
5364a0cb85cSEd Tanous                             {
5372c70f800SEd Tanous                                 ipv4Address.gateway = *gateway;
5384a0cb85cSEd Tanous                             }
5394a0cb85cSEd Tanous                         }
5404a0cb85cSEd Tanous                         else if (property.first == "Origin")
5414a0cb85cSEd Tanous                         {
5424a0cb85cSEd Tanous                             const std::string* origin =
543abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5444a0cb85cSEd Tanous                             if (origin != nullptr)
5454a0cb85cSEd Tanous                             {
5462c70f800SEd Tanous                                 ipv4Address.origin =
5474a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5484a0cb85cSEd Tanous                                                                         true);
5494a0cb85cSEd Tanous                             }
5504a0cb85cSEd Tanous                         }
5514a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5524a0cb85cSEd Tanous                         {
5534a0cb85cSEd Tanous                             const uint8_t* mask =
554abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5554a0cb85cSEd Tanous                             if (mask != nullptr)
5564a0cb85cSEd Tanous                             {
5574a0cb85cSEd Tanous                                 // convert it to the string
5582c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
5594a0cb85cSEd Tanous                             }
5604a0cb85cSEd Tanous                         }
5614a0cb85cSEd Tanous                         else
5624a0cb85cSEd Tanous                         {
5634a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5644a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5654a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5664a0cb85cSEd Tanous                         }
5674a0cb85cSEd Tanous                     }
5684a0cb85cSEd Tanous                     // Check if given address is local, or global
5692c70f800SEd Tanous                     ipv4Address.linktype =
5702c70f800SEd Tanous                         boost::starts_with(ipv4Address.address, "169.254.")
57118659d10SJohnathan Mantey                             ? LinkType::Local
57218659d10SJohnathan Mantey                             : LinkType::Global;
5734a0cb85cSEd Tanous                 }
5744a0cb85cSEd Tanous             }
5754a0cb85cSEd Tanous         }
5764a0cb85cSEd Tanous     }
5774a0cb85cSEd Tanous }
578588c3f0dSKowalski, Kamil 
579588c3f0dSKowalski, Kamil /**
580588c3f0dSKowalski, Kamil  * @brief Sets given Id on the given VLAN interface through D-Bus
581588c3f0dSKowalski, Kamil  *
582588c3f0dSKowalski, Kamil  * @param[in] ifaceId       Id of VLAN interface that should be modified
583588c3f0dSKowalski, Kamil  * @param[in] inputVlanId   New ID of the VLAN
584588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
585588c3f0dSKowalski, Kamil  *
586588c3f0dSKowalski, Kamil  * @return None.
587588c3f0dSKowalski, Kamil  */
588588c3f0dSKowalski, Kamil template <typename CallbackFunc>
5894a0cb85cSEd Tanous void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
5901abe55efSEd Tanous                   CallbackFunc&& callback)
5911abe55efSEd Tanous {
59255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
593588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
594588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
595588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
596588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", "Id",
597abf2add6SEd Tanous         std::variant<uint32_t>(inputVlanId));
5984a0cb85cSEd Tanous }
599588c3f0dSKowalski, Kamil 
600588c3f0dSKowalski, Kamil /**
601179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
602179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
603179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
604179db1d7SKowalski, Kamil  *
605179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
606179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
607179db1d7SKowalski, Kamil  *
608179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
609179db1d7SKowalski, Kamil  */
6104a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
6111abe55efSEd Tanous                                        uint8_t* bits = nullptr)
6121abe55efSEd Tanous {
613179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
614179db1d7SKowalski, Kamil 
615179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
616179db1d7SKowalski, Kamil 
6174a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
6181abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
6191abe55efSEd Tanous     {
620179db1d7SKowalski, Kamil         return false;
621179db1d7SKowalski, Kamil     }
622179db1d7SKowalski, Kamil 
6231abe55efSEd Tanous     if (bits != nullptr)
6241abe55efSEd Tanous     {
625179db1d7SKowalski, Kamil         *bits = 0;
626179db1d7SKowalski, Kamil     }
627179db1d7SKowalski, Kamil 
628179db1d7SKowalski, Kamil     char* endPtr;
629179db1d7SKowalski, Kamil     long previousValue = 255;
630179db1d7SKowalski, Kamil     bool firstZeroInByteHit;
6311abe55efSEd Tanous     for (const std::string& byte : bytesInMask)
6321abe55efSEd Tanous     {
6331abe55efSEd Tanous         if (byte.empty())
6341abe55efSEd Tanous         {
6351db9ca37SKowalski, Kamil             return false;
6361db9ca37SKowalski, Kamil         }
6371db9ca37SKowalski, Kamil 
638179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
6391db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
640179db1d7SKowalski, Kamil 
6414a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
6424a0cb85cSEd Tanous         // is not 100% number
6431abe55efSEd Tanous         if (*endPtr != '\0')
6441abe55efSEd Tanous         {
645179db1d7SKowalski, Kamil             return false;
646179db1d7SKowalski, Kamil         }
647179db1d7SKowalski, Kamil 
648179db1d7SKowalski, Kamil         // Value should be contained in byte
6491abe55efSEd Tanous         if (value < 0 || value > 255)
6501abe55efSEd Tanous         {
651179db1d7SKowalski, Kamil             return false;
652179db1d7SKowalski, Kamil         }
653179db1d7SKowalski, Kamil 
6541abe55efSEd Tanous         if (bits != nullptr)
6551abe55efSEd Tanous         {
656179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
6571abe55efSEd Tanous             if (previousValue != 255 && value != 0)
6581abe55efSEd Tanous             {
659179db1d7SKowalski, Kamil                 return false;
660179db1d7SKowalski, Kamil             }
661179db1d7SKowalski, Kamil 
662179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
663179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
664179db1d7SKowalski, Kamil 
665179db1d7SKowalski, Kamil             // Count bits
66623a21a1cSEd Tanous             for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
6671abe55efSEd Tanous             {
66823a21a1cSEd Tanous                 if (value & (1L << bitIdx))
6691abe55efSEd Tanous                 {
6701abe55efSEd Tanous                     if (firstZeroInByteHit)
6711abe55efSEd Tanous                     {
672179db1d7SKowalski, Kamil                         // Continuity not preserved
673179db1d7SKowalski, Kamil                         return false;
6741abe55efSEd Tanous                     }
675179db1d7SKowalski, Kamil                     (*bits)++;
676179db1d7SKowalski, Kamil                 }
6771abe55efSEd Tanous                 else
6781abe55efSEd Tanous                 {
679179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
680179db1d7SKowalski, Kamil                 }
681179db1d7SKowalski, Kamil             }
682179db1d7SKowalski, Kamil         }
683179db1d7SKowalski, Kamil 
684179db1d7SKowalski, Kamil         previousValue = value;
685179db1d7SKowalski, Kamil     }
686179db1d7SKowalski, Kamil 
687179db1d7SKowalski, Kamil     return true;
688179db1d7SKowalski, Kamil }
689179db1d7SKowalski, Kamil 
690179db1d7SKowalski, Kamil /**
69101784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
692179db1d7SKowalski, Kamil  *
693179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
694179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
695179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
696179db1d7SKowalski, Kamil  *
697179db1d7SKowalski, Kamil  * @return None
698179db1d7SKowalski, Kamil  */
6994a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
7004a0cb85cSEd Tanous                        const std::shared_ptr<AsyncResp> asyncResp)
7011abe55efSEd Tanous {
70255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
703286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
7041abe55efSEd Tanous             if (ec)
7051abe55efSEd Tanous             {
706a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
7071abe55efSEd Tanous             }
708179db1d7SKowalski, Kamil         },
709179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
710179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
711179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
712179db1d7SKowalski, Kamil }
713179db1d7SKowalski, Kamil 
714179db1d7SKowalski, Kamil /**
71501784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
716179db1d7SKowalski, Kamil  *
71701784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
71801784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
71901784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
72001784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
721179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
722179db1d7SKowalski, Kamil  *
723179db1d7SKowalski, Kamil  * @return None
724179db1d7SKowalski, Kamil  */
725cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
726cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
7274a0cb85cSEd Tanous                        std::shared_ptr<AsyncResp> asyncResp)
7281abe55efSEd Tanous {
72901784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
73001784826SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
7311abe55efSEd Tanous             if (ec)
7321abe55efSEd Tanous             {
733a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
734179db1d7SKowalski, Kamil             }
73501784826SJohnathan Mantey         },
73601784826SJohnathan Mantey         "xyz.openbmc_project.Network",
737179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
738179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
73901784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
740179db1d7SKowalski, Kamil         gateway);
741179db1d7SKowalski, Kamil }
742e48c0fc5SRavi Teja 
743e48c0fc5SRavi Teja /**
74401784826SJohnathan Mantey  * @brief Deletes the IPv4 entry for this interface and creates a replacement
74501784826SJohnathan Mantey  * static IPv4 entry
74601784826SJohnathan Mantey  *
74701784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
74801784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
74901784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
75001784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
75101784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
75201784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
75301784826SJohnathan Mantey  *
75401784826SJohnathan Mantey  * @return None
75501784826SJohnathan Mantey  */
75601784826SJohnathan Mantey inline void deleteAndCreateIPv4(const std::string& ifaceId,
75701784826SJohnathan Mantey                                 const std::string& id, uint8_t prefixLength,
75801784826SJohnathan Mantey                                 const std::string& gateway,
75901784826SJohnathan Mantey                                 const std::string& address,
76001784826SJohnathan Mantey                                 std::shared_ptr<AsyncResp> asyncResp)
76101784826SJohnathan Mantey {
76201784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
76301784826SJohnathan Mantey         [asyncResp, ifaceId, address, prefixLength,
76401784826SJohnathan Mantey          gateway](const boost::system::error_code ec) {
76501784826SJohnathan Mantey             if (ec)
76601784826SJohnathan Mantey             {
76701784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
76801784826SJohnathan Mantey             }
76901784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
77023a21a1cSEd Tanous                 [asyncResp](const boost::system::error_code ec2) {
77123a21a1cSEd Tanous                     if (ec2)
77201784826SJohnathan Mantey                     {
77301784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
77401784826SJohnathan Mantey                     }
77501784826SJohnathan Mantey                 },
77601784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
77701784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
77801784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
77901784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
78001784826SJohnathan Mantey                 prefixLength, gateway);
78101784826SJohnathan Mantey         },
78201784826SJohnathan Mantey         "xyz.openbmc_project.Network",
78301784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
78401784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
78501784826SJohnathan Mantey }
78601784826SJohnathan Mantey 
78701784826SJohnathan Mantey /**
788e48c0fc5SRavi Teja  * @brief Deletes given IPv6
789e48c0fc5SRavi Teja  *
790e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
791e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
792e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
793e48c0fc5SRavi Teja  *
794e48c0fc5SRavi Teja  * @return None
795e48c0fc5SRavi Teja  */
796e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
797e48c0fc5SRavi Teja                        const std::shared_ptr<AsyncResp> asyncResp)
798e48c0fc5SRavi Teja {
799e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
800286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
801e48c0fc5SRavi Teja             if (ec)
802e48c0fc5SRavi Teja             {
803e48c0fc5SRavi Teja                 messages::internalError(asyncResp->res);
804e48c0fc5SRavi Teja             }
805e48c0fc5SRavi Teja         },
806e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
807e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
808e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
809e48c0fc5SRavi Teja }
810e48c0fc5SRavi Teja 
811e48c0fc5SRavi Teja /**
81201784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
81301784826SJohnathan Mantey  * static IPv6 entry
81401784826SJohnathan Mantey  *
81501784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
81601784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
81701784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
81801784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
81901784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
82001784826SJohnathan Mantey  *
82101784826SJohnathan Mantey  * @return None
82201784826SJohnathan Mantey  */
82301784826SJohnathan Mantey inline void deleteAndCreateIPv6(const std::string& ifaceId,
82401784826SJohnathan Mantey                                 const std::string& id, uint8_t prefixLength,
82501784826SJohnathan Mantey                                 const std::string& address,
82601784826SJohnathan Mantey                                 std::shared_ptr<AsyncResp> asyncResp)
82701784826SJohnathan Mantey {
82801784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
82901784826SJohnathan Mantey         [asyncResp, ifaceId, address,
83001784826SJohnathan Mantey          prefixLength](const boost::system::error_code ec) {
83101784826SJohnathan Mantey             if (ec)
83201784826SJohnathan Mantey             {
83301784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
83401784826SJohnathan Mantey             }
83501784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
83623a21a1cSEd Tanous                 [asyncResp](const boost::system::error_code ec2) {
83723a21a1cSEd Tanous                     if (ec2)
83801784826SJohnathan Mantey                     {
83901784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
84001784826SJohnathan Mantey                     }
84101784826SJohnathan Mantey                 },
84201784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
84301784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
84401784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
84501784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
84601784826SJohnathan Mantey                 prefixLength, "");
84701784826SJohnathan Mantey         },
84801784826SJohnathan Mantey         "xyz.openbmc_project.Network",
84901784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
85001784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
85101784826SJohnathan Mantey }
85201784826SJohnathan Mantey 
85301784826SJohnathan Mantey /**
854e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
855e48c0fc5SRavi Teja  *
856e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
857e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
858e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
859e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
860e48c0fc5SRavi Teja  *
861e48c0fc5SRavi Teja  * @return None
862e48c0fc5SRavi Teja  */
86301784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
86401784826SJohnathan Mantey                        const std::string& address,
865e48c0fc5SRavi Teja                        std::shared_ptr<AsyncResp> asyncResp)
866e48c0fc5SRavi Teja {
867e48c0fc5SRavi Teja     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
868e48c0fc5SRavi Teja         if (ec)
869e48c0fc5SRavi Teja         {
870e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
871e48c0fc5SRavi Teja         }
872e48c0fc5SRavi Teja     };
873e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
8744e0453b1SGunnar Mills     // does not have associated gateway property
875e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
876e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
877e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
878e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
879e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
880e48c0fc5SRavi Teja         "");
881e48c0fc5SRavi Teja }
882e48c0fc5SRavi Teja 
883179db1d7SKowalski, Kamil /**
884179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
885179db1d7SKowalski, Kamil  * Object
886179db1d7SKowalski, Kamil  * from EntityManager Network Manager
8874a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
888179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
889179db1d7SKowalski, Kamil  * into JSON
890179db1d7SKowalski, Kamil  */
891179db1d7SKowalski, Kamil template <typename CallbackFunc>
8924a0cb85cSEd Tanous void getEthernetIfaceData(const std::string& ethiface_id,
8931abe55efSEd Tanous                           CallbackFunc&& callback)
8941abe55efSEd Tanous {
89555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
8962c70f800SEd Tanous         [ethifaceId{std::string{ethiface_id}}, callback{std::move(callback)}](
8971abe55efSEd Tanous             const boost::system::error_code error_code,
8984a0cb85cSEd Tanous             const GetManagedObjects& resp) {
89955c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
9004a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
901e48c0fc5SRavi Teja             boost::container::flat_set<IPv6AddressData> ipv6Data;
902179db1d7SKowalski, Kamil 
9031abe55efSEd Tanous             if (error_code)
9041abe55efSEd Tanous             {
90501784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
906179db1d7SKowalski, Kamil                 return;
907179db1d7SKowalski, Kamil             }
908179db1d7SKowalski, Kamil 
9094c9afe43SEd Tanous             bool found =
9102c70f800SEd Tanous                 extractEthernetInterfaceData(ethifaceId, resp, ethData);
9114c9afe43SEd Tanous             if (!found)
9124c9afe43SEd Tanous             {
91301784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
9144c9afe43SEd Tanous                 return;
9154c9afe43SEd Tanous             }
9164c9afe43SEd Tanous 
9172c70f800SEd Tanous             extractIPData(ethifaceId, resp, ipv4Data);
918179db1d7SKowalski, Kamil             // Fix global GW
9191abe55efSEd Tanous             for (IPv4AddressData& ipv4 : ipv4Data)
9201abe55efSEd Tanous             {
921c619141bSRavi Teja                 if (((ipv4.linktype == LinkType::Global) &&
922c619141bSRavi Teja                      (ipv4.gateway == "0.0.0.0")) ||
923c619141bSRavi Teja                     (ipv4.origin == "DHCP"))
9241abe55efSEd Tanous                 {
9254a0cb85cSEd Tanous                     ipv4.gateway = ethData.default_gateway;
926179db1d7SKowalski, Kamil                 }
927179db1d7SKowalski, Kamil             }
928179db1d7SKowalski, Kamil 
9292c70f800SEd Tanous             extractIPV6Data(ethifaceId, resp, ipv6Data);
9304e0453b1SGunnar Mills             // Finally make a callback with useful data
93101784826SJohnathan Mantey             callback(true, ethData, ipv4Data, ipv6Data);
932179db1d7SKowalski, Kamil         },
933179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
934179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
935271584abSEd Tanous }
936179db1d7SKowalski, Kamil 
937179db1d7SKowalski, Kamil /**
9389391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
9399391bb9cSRapkiewicz, Pawel  * Manager
9401abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
9411abe55efSEd Tanous  * into JSON.
9429391bb9cSRapkiewicz, Pawel  */
9439391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
9441abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
9451abe55efSEd Tanous {
94655c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
9474a0cb85cSEd Tanous         [callback{std::move(callback)}](
9489391bb9cSRapkiewicz, Pawel             const boost::system::error_code error_code,
9494a0cb85cSEd Tanous             GetManagedObjects& resp) {
9501abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
9511abe55efSEd Tanous             // ethernet interfaces
9522c70f800SEd Tanous             boost::container::flat_set<std::string> ifaceList;
9532c70f800SEd Tanous             ifaceList.reserve(resp.size());
9541abe55efSEd Tanous             if (error_code)
9551abe55efSEd Tanous             {
9562c70f800SEd Tanous                 callback(false, ifaceList);
9579391bb9cSRapkiewicz, Pawel                 return;
9589391bb9cSRapkiewicz, Pawel             }
9599391bb9cSRapkiewicz, Pawel 
9609391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
9614a0cb85cSEd Tanous             for (const auto& objpath : resp)
9621abe55efSEd Tanous             {
9639391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
9644a0cb85cSEd Tanous                 for (const auto& interface : objpath.second)
9651abe55efSEd Tanous                 {
9661abe55efSEd Tanous                     // If interface is
9674a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
9684a0cb85cSEd Tanous                     // what we're looking for.
9699391bb9cSRapkiewicz, Pawel                     if (interface.first ==
9701abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
9711abe55efSEd Tanous                     {
9724e0453b1SGunnar Mills                         // Cut out everything until last "/", ...
9732c70f800SEd Tanous                         const std::string& ifaceId = objpath.first.str;
9742c70f800SEd Tanous                         std::size_t lastPos = ifaceId.rfind("/");
9752c70f800SEd Tanous                         if (lastPos != std::string::npos)
9761abe55efSEd Tanous                         {
9779391bb9cSRapkiewicz, Pawel                             // and put it into output vector.
9782c70f800SEd Tanous                             ifaceList.emplace(ifaceId.substr(lastPos + 1));
9799391bb9cSRapkiewicz, Pawel                         }
9809391bb9cSRapkiewicz, Pawel                     }
9819391bb9cSRapkiewicz, Pawel                 }
9829391bb9cSRapkiewicz, Pawel             }
983a434f2bdSEd Tanous             // Finally make a callback with useful data
9842c70f800SEd Tanous             callback(true, ifaceList);
9859391bb9cSRapkiewicz, Pawel         },
986aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
987aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
988271584abSEd Tanous }
9899391bb9cSRapkiewicz, Pawel 
9909391bb9cSRapkiewicz, Pawel /**
9919391bb9cSRapkiewicz, Pawel  * EthernetCollection derived class for delivering Ethernet Collection Schema
9929391bb9cSRapkiewicz, Pawel  */
9931abe55efSEd Tanous class EthernetCollection : public Node
9941abe55efSEd Tanous {
9959391bb9cSRapkiewicz, Pawel   public:
99652cc112dSEd Tanous     EthernetCollection(App& app) :
9974a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
9981abe55efSEd Tanous     {
999588c3f0dSKowalski, Kamil         entityPrivileges = {
1000588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1001e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1002e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1003e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1004e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1005e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
10069391bb9cSRapkiewicz, Pawel     }
10079391bb9cSRapkiewicz, Pawel 
10089391bb9cSRapkiewicz, Pawel   private:
10099391bb9cSRapkiewicz, Pawel     /**
10109391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
10119391bb9cSRapkiewicz, Pawel      */
1012cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
1013cb13a392SEd Tanous                const std::vector<std::string>&) override
10141abe55efSEd Tanous     {
10150f74e643SEd Tanous         res.jsonValue["@odata.type"] =
10160f74e643SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
10170f74e643SEd Tanous         res.jsonValue["@odata.id"] =
10180f74e643SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
10190f74e643SEd Tanous         res.jsonValue["Name"] = "Ethernet Network Interface Collection";
10200f74e643SEd Tanous         res.jsonValue["Description"] =
10210f74e643SEd Tanous             "Collection of EthernetInterfaces for this Manager";
10224c9afe43SEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
10234a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
10241abe55efSEd Tanous         // preparation
1025f12894f8SJason M. Bills         getEthernetIfaceList(
10264c9afe43SEd Tanous             [asyncResp](
10274c9afe43SEd Tanous                 const bool& success,
10284c9afe43SEd Tanous                 const boost::container::flat_set<std::string>& iface_list) {
10294a0cb85cSEd Tanous                 if (!success)
10301abe55efSEd Tanous                 {
10314c9afe43SEd Tanous                     messages::internalError(asyncResp->res);
10324a0cb85cSEd Tanous                     return;
10334a0cb85cSEd Tanous                 }
10344a0cb85cSEd Tanous 
10352c70f800SEd Tanous                 nlohmann::json& ifaceArray =
10364c9afe43SEd Tanous                     asyncResp->res.jsonValue["Members"];
10372c70f800SEd Tanous                 ifaceArray = nlohmann::json::array();
1038fda13ad2SSunitha Harish                 std::string tag = "_";
10392c70f800SEd Tanous                 for (const std::string& ifaceItem : iface_list)
10401abe55efSEd Tanous                 {
10412c70f800SEd Tanous                     std::size_t found = ifaceItem.find(tag);
1042fda13ad2SSunitha Harish                     if (found == std::string::npos)
1043fda13ad2SSunitha Harish                     {
10442c70f800SEd Tanous                         ifaceArray.push_back(
10454a0cb85cSEd Tanous                             {{"@odata.id",
10464a0cb85cSEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
10472c70f800SEd Tanous                                   ifaceItem}});
10489391bb9cSRapkiewicz, Pawel                     }
1049fda13ad2SSunitha Harish                 }
10504a0cb85cSEd Tanous 
10514c9afe43SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
10522c70f800SEd Tanous                     ifaceArray.size();
10534c9afe43SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
10544a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
10559391bb9cSRapkiewicz, Pawel             });
10569391bb9cSRapkiewicz, Pawel     }
10579391bb9cSRapkiewicz, Pawel };
10589391bb9cSRapkiewicz, Pawel 
10599391bb9cSRapkiewicz, Pawel /**
10609391bb9cSRapkiewicz, Pawel  * EthernetInterface derived class for delivering Ethernet Schema
10619391bb9cSRapkiewicz, Pawel  */
10621abe55efSEd Tanous class EthernetInterface : public Node
10631abe55efSEd Tanous {
10649391bb9cSRapkiewicz, Pawel   public:
10659391bb9cSRapkiewicz, Pawel     /*
10669391bb9cSRapkiewicz, Pawel      * Default Constructor
10679391bb9cSRapkiewicz, Pawel      */
106852cc112dSEd Tanous     EthernetInterface(App& app) :
10694a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
10701abe55efSEd Tanous              std::string())
10711abe55efSEd Tanous     {
1072588c3f0dSKowalski, Kamil         entityPrivileges = {
1073588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1074e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1075e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1076e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1077e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1078e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
10799391bb9cSRapkiewicz, Pawel     }
10809391bb9cSRapkiewicz, Pawel 
1081e439f0f8SKowalski, Kamil   private:
1082bc0bd6e0SEd Tanous     void handleHostnamePatch(const std::string& hostname,
10834a0cb85cSEd Tanous                              const std::shared_ptr<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 
1106ab6554f1SJoshi-Mansi     void handleDomainnamePatch(const std::string& ifaceId,
1107ab6554f1SJoshi-Mansi                                const std::string& domainname,
1108ab6554f1SJoshi-Mansi                                const std::shared_ptr<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,
1126ab6554f1SJoshi-Mansi                          const std::shared_ptr<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 
1184d577665bSRatan Gupta     void handleMACAddressPatch(const std::string& ifaceId,
1185d577665bSRatan Gupta                                const std::string& macAddress,
1186d577665bSRatan Gupta                                const std::shared_ptr<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,
1206da131a9aSJennifer Lee                         const std::shared_ptr<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                 }
1217da131a9aSJennifer Lee             },
1218da131a9aSJennifer Lee             "xyz.openbmc_project.Network",
1219da131a9aSJennifer Lee             "/xyz/openbmc_project/network/" + ifaceId,
1220da131a9aSJennifer Lee             "org.freedesktop.DBus.Properties", "Set",
1221da131a9aSJennifer Lee             "xyz.openbmc_project.Network.EthernetInterface", propertyName,
12221f8c7b5dSJohnathan Mantey             std::variant<std::string>{dhcp});
1223da131a9aSJennifer Lee     }
12241f8c7b5dSJohnathan Mantey 
1225eeedda23SJohnathan Mantey     void setEthernetInterfaceBoolProperty(
1226eeedda23SJohnathan Mantey         const std::string& ifaceId, const std::string& propertyName,
1227eeedda23SJohnathan Mantey         const bool& value, const std::shared_ptr<AsyncResp> asyncResp)
1228eeedda23SJohnathan Mantey     {
1229eeedda23SJohnathan Mantey         crow::connections::systemBus->async_method_call(
1230eeedda23SJohnathan Mantey             [asyncResp](const boost::system::error_code ec) {
1231eeedda23SJohnathan Mantey                 if (ec)
1232eeedda23SJohnathan Mantey                 {
1233eeedda23SJohnathan Mantey                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1234eeedda23SJohnathan Mantey                     messages::internalError(asyncResp->res);
1235eeedda23SJohnathan Mantey                     return;
1236eeedda23SJohnathan Mantey                 }
1237eeedda23SJohnathan Mantey             },
1238eeedda23SJohnathan Mantey             "xyz.openbmc_project.Network",
1239eeedda23SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
1240eeedda23SJohnathan Mantey             "org.freedesktop.DBus.Properties", "Set",
1241eeedda23SJohnathan Mantey             "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1242eeedda23SJohnathan Mantey             std::variant<bool>{value});
1243eeedda23SJohnathan Mantey     }
1244eeedda23SJohnathan Mantey 
1245da131a9aSJennifer Lee     void setDHCPv4Config(const std::string& propertyName, const bool& value,
1246da131a9aSJennifer Lee                          const std::shared_ptr<AsyncResp> asyncResp)
1247da131a9aSJennifer Lee     {
1248da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1249da131a9aSJennifer Lee         crow::connections::systemBus->async_method_call(
1250da131a9aSJennifer Lee             [asyncResp](const boost::system::error_code ec) {
1251da131a9aSJennifer Lee                 if (ec)
1252da131a9aSJennifer Lee                 {
1253da131a9aSJennifer Lee                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1254da131a9aSJennifer Lee                     messages::internalError(asyncResp->res);
1255da131a9aSJennifer Lee                     return;
1256da131a9aSJennifer Lee                 }
1257da131a9aSJennifer Lee             },
1258da131a9aSJennifer Lee             "xyz.openbmc_project.Network",
1259da131a9aSJennifer Lee             "/xyz/openbmc_project/network/config/dhcp",
1260da131a9aSJennifer Lee             "org.freedesktop.DBus.Properties", "Set",
1261da131a9aSJennifer Lee             "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1262da131a9aSJennifer Lee             std::variant<bool>{value});
1263da131a9aSJennifer Lee     }
1264d577665bSRatan Gupta 
12651f8c7b5dSJohnathan Mantey     void handleDHCPPatch(const std::string& ifaceId,
12661f8c7b5dSJohnathan Mantey                          const EthernetInterfaceData& ethData,
12671f8c7b5dSJohnathan Mantey                          DHCPParameters v4dhcpParms, DHCPParameters v6dhcpParms,
1268da131a9aSJennifer Lee                          const std::shared_ptr<AsyncResp> asyncResp)
1269da131a9aSJennifer Lee     {
12701f8c7b5dSJohnathan Mantey         bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
12711f8c7b5dSJohnathan Mantey         bool ipv6Active =
12721f8c7b5dSJohnathan Mantey             translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1273da131a9aSJennifer Lee 
12741f8c7b5dSJohnathan Mantey         bool nextv4DHCPState =
12751f8c7b5dSJohnathan Mantey             v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
12761f8c7b5dSJohnathan Mantey 
12771f8c7b5dSJohnathan Mantey         bool nextv6DHCPState{};
12781f8c7b5dSJohnathan Mantey         if (v6dhcpParms.dhcpv6OperatingMode)
1279da131a9aSJennifer Lee         {
12801f8c7b5dSJohnathan Mantey             if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
12811f8c7b5dSJohnathan Mantey                 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
12821f8c7b5dSJohnathan Mantey                 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
12831f8c7b5dSJohnathan Mantey             {
12841f8c7b5dSJohnathan Mantey                 messages::propertyValueFormatError(
12851f8c7b5dSJohnathan Mantey                     asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
12861f8c7b5dSJohnathan Mantey                     "OperatingMode");
1287da131a9aSJennifer Lee                 return;
1288da131a9aSJennifer Lee             }
12891f8c7b5dSJohnathan Mantey             nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
12901f8c7b5dSJohnathan Mantey         }
12911f8c7b5dSJohnathan Mantey         else
1292da131a9aSJennifer Lee         {
12931f8c7b5dSJohnathan Mantey             nextv6DHCPState = ipv6Active;
12941f8c7b5dSJohnathan Mantey         }
12951f8c7b5dSJohnathan Mantey 
12961f8c7b5dSJohnathan Mantey         bool nextDNS{};
12971f8c7b5dSJohnathan Mantey         if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
12981f8c7b5dSJohnathan Mantey         {
12991f8c7b5dSJohnathan Mantey             if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
13001f8c7b5dSJohnathan Mantey             {
13011f8c7b5dSJohnathan Mantey                 messages::generalError(asyncResp->res);
13021f8c7b5dSJohnathan Mantey                 return;
13031f8c7b5dSJohnathan Mantey             }
13041f8c7b5dSJohnathan Mantey             nextDNS = *v4dhcpParms.useDNSServers;
13051f8c7b5dSJohnathan Mantey         }
13061f8c7b5dSJohnathan Mantey         else if (v4dhcpParms.useDNSServers)
13071f8c7b5dSJohnathan Mantey         {
13081f8c7b5dSJohnathan Mantey             nextDNS = *v4dhcpParms.useDNSServers;
13091f8c7b5dSJohnathan Mantey         }
13101f8c7b5dSJohnathan Mantey         else if (v6dhcpParms.useDNSServers)
13111f8c7b5dSJohnathan Mantey         {
13121f8c7b5dSJohnathan Mantey             nextDNS = *v6dhcpParms.useDNSServers;
13131f8c7b5dSJohnathan Mantey         }
13141f8c7b5dSJohnathan Mantey         else
13151f8c7b5dSJohnathan Mantey         {
13161f8c7b5dSJohnathan Mantey             nextDNS = ethData.DNSEnabled;
13171f8c7b5dSJohnathan Mantey         }
13181f8c7b5dSJohnathan Mantey 
13191f8c7b5dSJohnathan Mantey         bool nextNTP{};
13201f8c7b5dSJohnathan Mantey         if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
13211f8c7b5dSJohnathan Mantey         {
13221f8c7b5dSJohnathan Mantey             if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
13231f8c7b5dSJohnathan Mantey             {
13241f8c7b5dSJohnathan Mantey                 messages::generalError(asyncResp->res);
13251f8c7b5dSJohnathan Mantey                 return;
13261f8c7b5dSJohnathan Mantey             }
13271f8c7b5dSJohnathan Mantey             nextNTP = *v4dhcpParms.useNTPServers;
13281f8c7b5dSJohnathan Mantey         }
13291f8c7b5dSJohnathan Mantey         else if (v4dhcpParms.useNTPServers)
13301f8c7b5dSJohnathan Mantey         {
13311f8c7b5dSJohnathan Mantey             nextNTP = *v4dhcpParms.useNTPServers;
13321f8c7b5dSJohnathan Mantey         }
13331f8c7b5dSJohnathan Mantey         else if (v6dhcpParms.useNTPServers)
13341f8c7b5dSJohnathan Mantey         {
13351f8c7b5dSJohnathan Mantey             nextNTP = *v6dhcpParms.useNTPServers;
13361f8c7b5dSJohnathan Mantey         }
13371f8c7b5dSJohnathan Mantey         else
13381f8c7b5dSJohnathan Mantey         {
13391f8c7b5dSJohnathan Mantey             nextNTP = ethData.NTPEnabled;
13401f8c7b5dSJohnathan Mantey         }
13411f8c7b5dSJohnathan Mantey 
13421f8c7b5dSJohnathan Mantey         bool nextUseDomain{};
13431f8c7b5dSJohnathan Mantey         if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
13441f8c7b5dSJohnathan Mantey         {
13451f8c7b5dSJohnathan Mantey             if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
13461f8c7b5dSJohnathan Mantey             {
13471f8c7b5dSJohnathan Mantey                 messages::generalError(asyncResp->res);
13481f8c7b5dSJohnathan Mantey                 return;
13491f8c7b5dSJohnathan Mantey             }
13501f8c7b5dSJohnathan Mantey             nextUseDomain = *v4dhcpParms.useUseDomainName;
13511f8c7b5dSJohnathan Mantey         }
13521f8c7b5dSJohnathan Mantey         else if (v4dhcpParms.useUseDomainName)
13531f8c7b5dSJohnathan Mantey         {
13541f8c7b5dSJohnathan Mantey             nextUseDomain = *v4dhcpParms.useUseDomainName;
13551f8c7b5dSJohnathan Mantey         }
13561f8c7b5dSJohnathan Mantey         else if (v6dhcpParms.useUseDomainName)
13571f8c7b5dSJohnathan Mantey         {
13581f8c7b5dSJohnathan Mantey             nextUseDomain = *v6dhcpParms.useUseDomainName;
13591f8c7b5dSJohnathan Mantey         }
13601f8c7b5dSJohnathan Mantey         else
13611f8c7b5dSJohnathan Mantey         {
13621f8c7b5dSJohnathan Mantey             nextUseDomain = ethData.HostNameEnabled;
13631f8c7b5dSJohnathan Mantey         }
13641f8c7b5dSJohnathan Mantey 
1365da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
13661f8c7b5dSJohnathan Mantey         setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
13671f8c7b5dSJohnathan Mantey                        asyncResp);
1368da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << "set DNSEnabled...";
13691f8c7b5dSJohnathan Mantey         setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1370da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << "set NTPEnabled...";
13711f8c7b5dSJohnathan Mantey         setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
13721f8c7b5dSJohnathan Mantey         BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
13731f8c7b5dSJohnathan Mantey         setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1374da131a9aSJennifer Lee     }
137501784826SJohnathan Mantey 
137601784826SJohnathan Mantey     boost::container::flat_set<IPv4AddressData>::const_iterator
13772c70f800SEd Tanous         getNextStaticIpEntry(
137801784826SJohnathan Mantey             boost::container::flat_set<IPv4AddressData>::const_iterator head,
137901784826SJohnathan Mantey             boost::container::flat_set<IPv4AddressData>::const_iterator end)
138001784826SJohnathan Mantey     {
138101784826SJohnathan Mantey         for (; head != end; head++)
138201784826SJohnathan Mantey         {
138301784826SJohnathan Mantey             if (head->origin == "Static")
138401784826SJohnathan Mantey             {
138501784826SJohnathan Mantey                 return head;
138601784826SJohnathan Mantey             }
138701784826SJohnathan Mantey         }
138801784826SJohnathan Mantey         return end;
138901784826SJohnathan Mantey     }
139001784826SJohnathan Mantey 
139101784826SJohnathan Mantey     boost::container::flat_set<IPv6AddressData>::const_iterator
13922c70f800SEd Tanous         getNextStaticIpEntry(
139301784826SJohnathan Mantey             boost::container::flat_set<IPv6AddressData>::const_iterator head,
139401784826SJohnathan Mantey             boost::container::flat_set<IPv6AddressData>::const_iterator end)
139501784826SJohnathan Mantey     {
139601784826SJohnathan Mantey         for (; head != end; head++)
139701784826SJohnathan Mantey         {
139801784826SJohnathan Mantey             if (head->origin == "Static")
139901784826SJohnathan Mantey             {
140001784826SJohnathan Mantey                 return head;
140101784826SJohnathan Mantey             }
140201784826SJohnathan Mantey         }
140301784826SJohnathan Mantey         return end;
140401784826SJohnathan Mantey     }
140501784826SJohnathan Mantey 
1406d1d50814SRavi Teja     void handleIPv4StaticPatch(
1407f476acbfSRatan Gupta         const std::string& ifaceId, nlohmann::json& input,
140801784826SJohnathan Mantey         const boost::container::flat_set<IPv4AddressData>& ipv4Data,
14094a0cb85cSEd Tanous         const std::shared_ptr<AsyncResp> asyncResp)
14101abe55efSEd Tanous     {
141101784826SJohnathan Mantey         if ((!input.is_array()) || input.empty())
1412f476acbfSRatan Gupta         {
1413f476acbfSRatan Gupta             messages::propertyValueTypeError(asyncResp->res, input.dump(),
1414d1d50814SRavi Teja                                              "IPv4StaticAddresses");
1415f476acbfSRatan Gupta             return;
1416f476acbfSRatan Gupta         }
1417f476acbfSRatan Gupta 
1418271584abSEd Tanous         unsigned entryIdx = 1;
141901784826SJohnathan Mantey         // Find the first static IP address currently active on the NIC and
142001784826SJohnathan Mantey         // match it to the first JSON element in the IPv4StaticAddresses array.
142101784826SJohnathan Mantey         // Match each subsequent JSON element to the next static IP programmed
142201784826SJohnathan Mantey         // into the NIC.
14232c70f800SEd Tanous         boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry =
14242c70f800SEd Tanous             getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
142501784826SJohnathan Mantey 
1426537174c4SEd Tanous         for (nlohmann::json& thisJson : input)
14271abe55efSEd Tanous         {
14284a0cb85cSEd Tanous             std::string pathString =
1429d1d50814SRavi Teja                 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1430179db1d7SKowalski, Kamil 
143101784826SJohnathan Mantey             if (!thisJson.is_null() && !thisJson.empty())
1432f476acbfSRatan Gupta             {
1433537174c4SEd Tanous                 std::optional<std::string> address;
1434537174c4SEd Tanous                 std::optional<std::string> subnetMask;
1435537174c4SEd Tanous                 std::optional<std::string> gateway;
1436537174c4SEd Tanous 
1437537174c4SEd Tanous                 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
14387e27d832SJohnathan Mantey                                          address, "SubnetMask", subnetMask,
14397e27d832SJohnathan Mantey                                          "Gateway", gateway))
1440537174c4SEd Tanous                 {
144101784826SJohnathan Mantey                     messages::propertyValueFormatError(
144201784826SJohnathan Mantey                         asyncResp->res, thisJson.dump(), pathString);
1443537174c4SEd Tanous                     return;
1444179db1d7SKowalski, Kamil                 }
1445179db1d7SKowalski, Kamil 
144601784826SJohnathan Mantey                 // Find the address/subnet/gateway values. Any values that are
144701784826SJohnathan Mantey                 // not explicitly provided are assumed to be unmodified from the
144801784826SJohnathan Mantey                 // current state of the interface. Merge existing state into the
144901784826SJohnathan Mantey                 // current request.
1450271584abSEd Tanous                 const std::string* addr = nullptr;
1451271584abSEd Tanous                 const std::string* gw = nullptr;
145201784826SJohnathan Mantey                 uint8_t prefixLength = 0;
145301784826SJohnathan Mantey                 bool errorInEntry = false;
1454537174c4SEd Tanous                 if (address)
14551abe55efSEd Tanous                 {
145601784826SJohnathan Mantey                     if (ipv4VerifyIpAndGetBitcount(*address))
14571abe55efSEd Tanous                     {
145801784826SJohnathan Mantey                         addr = &(*address);
14594a0cb85cSEd Tanous                     }
146001784826SJohnathan Mantey                     else
146101784826SJohnathan Mantey                     {
146201784826SJohnathan Mantey                         messages::propertyValueFormatError(
146301784826SJohnathan Mantey                             asyncResp->res, *address, pathString + "/Address");
146401784826SJohnathan Mantey                         errorInEntry = true;
146501784826SJohnathan Mantey                     }
146601784826SJohnathan Mantey                 }
14672c70f800SEd Tanous                 else if (niciPentry != ipv4Data.cend())
146801784826SJohnathan Mantey                 {
14692c70f800SEd Tanous                     addr = &(niciPentry->address);
147001784826SJohnathan Mantey                 }
147101784826SJohnathan Mantey                 else
147201784826SJohnathan Mantey                 {
147301784826SJohnathan Mantey                     messages::propertyMissing(asyncResp->res,
147401784826SJohnathan Mantey                                               pathString + "/Address");
147501784826SJohnathan Mantey                     errorInEntry = true;
14764a0cb85cSEd Tanous                 }
14774a0cb85cSEd Tanous 
1478537174c4SEd Tanous                 if (subnetMask)
14794a0cb85cSEd Tanous                 {
1480537174c4SEd Tanous                     if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
14814a0cb85cSEd Tanous                     {
1482f12894f8SJason M. Bills                         messages::propertyValueFormatError(
1483537174c4SEd Tanous                             asyncResp->res, *subnetMask,
14844a0cb85cSEd Tanous                             pathString + "/SubnetMask");
148501784826SJohnathan Mantey                         errorInEntry = true;
14864a0cb85cSEd Tanous                     }
14874a0cb85cSEd Tanous                 }
14882c70f800SEd Tanous                 else if (niciPentry != ipv4Data.cend())
14894a0cb85cSEd Tanous                 {
14902c70f800SEd Tanous                     if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask,
149101784826SJohnathan Mantey                                                     &prefixLength))
14924a0cb85cSEd Tanous                     {
149301784826SJohnathan Mantey                         messages::propertyValueFormatError(
14942c70f800SEd Tanous                             asyncResp->res, niciPentry->netmask,
149501784826SJohnathan Mantey                             pathString + "/SubnetMask");
149601784826SJohnathan Mantey                         errorInEntry = true;
14974a0cb85cSEd Tanous                     }
14984a0cb85cSEd Tanous                 }
14991abe55efSEd Tanous                 else
15001abe55efSEd Tanous                 {
150101784826SJohnathan Mantey                     messages::propertyMissing(asyncResp->res,
150201784826SJohnathan Mantey                                               pathString + "/SubnetMask");
150301784826SJohnathan Mantey                     errorInEntry = true;
150401784826SJohnathan Mantey                 }
150501784826SJohnathan Mantey 
150601784826SJohnathan Mantey                 if (gateway)
150701784826SJohnathan Mantey                 {
150801784826SJohnathan Mantey                     if (ipv4VerifyIpAndGetBitcount(*gateway))
150901784826SJohnathan Mantey                     {
151001784826SJohnathan Mantey                         gw = &(*gateway);
151101784826SJohnathan Mantey                     }
151201784826SJohnathan Mantey                     else
151301784826SJohnathan Mantey                     {
151401784826SJohnathan Mantey                         messages::propertyValueFormatError(
151501784826SJohnathan Mantey                             asyncResp->res, *gateway, pathString + "/Gateway");
151601784826SJohnathan Mantey                         errorInEntry = true;
151701784826SJohnathan Mantey                     }
151801784826SJohnathan Mantey                 }
15192c70f800SEd Tanous                 else if (niciPentry != ipv4Data.cend())
152001784826SJohnathan Mantey                 {
15212c70f800SEd Tanous                     gw = &niciPentry->gateway;
152201784826SJohnathan Mantey                 }
152301784826SJohnathan Mantey                 else
15241abe55efSEd Tanous                 {
1525a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
15264a0cb85cSEd Tanous                                               pathString + "/Gateway");
152701784826SJohnathan Mantey                     errorInEntry = true;
15284a0cb85cSEd Tanous                 }
15294a0cb85cSEd Tanous 
153001784826SJohnathan Mantey                 if (errorInEntry)
15311abe55efSEd Tanous                 {
153201784826SJohnathan Mantey                     return;
15334a0cb85cSEd Tanous                 }
15344a0cb85cSEd Tanous 
15352c70f800SEd Tanous                 if (niciPentry != ipv4Data.cend())
15361abe55efSEd Tanous                 {
15372c70f800SEd Tanous                     deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength,
153801784826SJohnathan Mantey                                         *gw, *addr, asyncResp);
15392c70f800SEd Tanous                     niciPentry =
15402c70f800SEd Tanous                         getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
1541588c3f0dSKowalski, Kamil                 }
154201784826SJohnathan Mantey                 else
154301784826SJohnathan Mantey                 {
1544cb13a392SEd Tanous                     createIPv4(ifaceId, prefixLength, *gateway, *address,
1545cb13a392SEd Tanous                                asyncResp);
15464a0cb85cSEd Tanous                 }
15474a0cb85cSEd Tanous                 entryIdx++;
15484a0cb85cSEd Tanous             }
154901784826SJohnathan Mantey             else
155001784826SJohnathan Mantey             {
15512c70f800SEd Tanous                 if (niciPentry == ipv4Data.cend())
155201784826SJohnathan Mantey                 {
155301784826SJohnathan Mantey                     // Requesting a DELETE/DO NOT MODIFY action for an item
155401784826SJohnathan Mantey                     // that isn't present on the eth(n) interface. Input JSON is
155501784826SJohnathan Mantey                     // in error, so bail out.
155601784826SJohnathan Mantey                     if (thisJson.is_null())
155701784826SJohnathan Mantey                     {
155801784826SJohnathan Mantey                         messages::resourceCannotBeDeleted(asyncResp->res);
155901784826SJohnathan Mantey                         return;
156001784826SJohnathan Mantey                     }
156101784826SJohnathan Mantey                     messages::propertyValueFormatError(
156201784826SJohnathan Mantey                         asyncResp->res, thisJson.dump(), pathString);
156301784826SJohnathan Mantey                     return;
156401784826SJohnathan Mantey                 }
156501784826SJohnathan Mantey 
156601784826SJohnathan Mantey                 if (thisJson.is_null())
156701784826SJohnathan Mantey                 {
15682c70f800SEd Tanous                     deleteIPv4(ifaceId, niciPentry->id, asyncResp);
156901784826SJohnathan Mantey                 }
15702c70f800SEd Tanous                 if (niciPentry != ipv4Data.cend())
157101784826SJohnathan Mantey                 {
15722c70f800SEd Tanous                     niciPentry =
15732c70f800SEd Tanous                         getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
157401784826SJohnathan Mantey                 }
157501784826SJohnathan Mantey                 entryIdx++;
157601784826SJohnathan Mantey             }
157701784826SJohnathan Mantey         }
15784a0cb85cSEd Tanous     }
15794a0cb85cSEd Tanous 
1580f85837bfSRAJESWARAN THILLAIGOVINDAN     void handleStaticNameServersPatch(
1581f85837bfSRAJESWARAN THILLAIGOVINDAN         const std::string& ifaceId,
1582f85837bfSRAJESWARAN THILLAIGOVINDAN         const std::vector<std::string>& updatedStaticNameServers,
1583f85837bfSRAJESWARAN THILLAIGOVINDAN         const std::shared_ptr<AsyncResp>& asyncResp)
1584f85837bfSRAJESWARAN THILLAIGOVINDAN     {
1585f85837bfSRAJESWARAN THILLAIGOVINDAN         crow::connections::systemBus->async_method_call(
1586286b9118SJohnathan Mantey             [asyncResp](const boost::system::error_code ec) {
1587f85837bfSRAJESWARAN THILLAIGOVINDAN                 if (ec)
1588f85837bfSRAJESWARAN THILLAIGOVINDAN                 {
1589f85837bfSRAJESWARAN THILLAIGOVINDAN                     messages::internalError(asyncResp->res);
1590f85837bfSRAJESWARAN THILLAIGOVINDAN                     return;
1591f85837bfSRAJESWARAN THILLAIGOVINDAN                 }
1592f85837bfSRAJESWARAN THILLAIGOVINDAN             },
1593f85837bfSRAJESWARAN THILLAIGOVINDAN             "xyz.openbmc_project.Network",
1594f85837bfSRAJESWARAN THILLAIGOVINDAN             "/xyz/openbmc_project/network/" + ifaceId,
1595f85837bfSRAJESWARAN THILLAIGOVINDAN             "org.freedesktop.DBus.Properties", "Set",
15960f6efdc1Smanojkiran.eda@gmail.com             "xyz.openbmc_project.Network.EthernetInterface",
15970f6efdc1Smanojkiran.eda@gmail.com             "StaticNameServers",
1598f85837bfSRAJESWARAN THILLAIGOVINDAN             std::variant<std::vector<std::string>>{updatedStaticNameServers});
1599f85837bfSRAJESWARAN THILLAIGOVINDAN     }
1600f85837bfSRAJESWARAN THILLAIGOVINDAN 
1601e48c0fc5SRavi Teja     void handleIPv6StaticAddressesPatch(
1602e48c0fc5SRavi Teja         const std::string& ifaceId, nlohmann::json& input,
160301784826SJohnathan Mantey         const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1604e48c0fc5SRavi Teja         const std::shared_ptr<AsyncResp> asyncResp)
1605e48c0fc5SRavi Teja     {
160601784826SJohnathan Mantey         if (!input.is_array() || input.empty())
1607e48c0fc5SRavi Teja         {
1608e48c0fc5SRavi Teja             messages::propertyValueTypeError(asyncResp->res, input.dump(),
1609e48c0fc5SRavi Teja                                              "IPv6StaticAddresses");
1610e48c0fc5SRavi Teja             return;
1611e48c0fc5SRavi Teja         }
1612271584abSEd Tanous         size_t entryIdx = 1;
16132c70f800SEd Tanous         boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry =
16142c70f800SEd Tanous             getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1615e48c0fc5SRavi Teja         for (nlohmann::json& thisJson : input)
1616e48c0fc5SRavi Teja         {
1617e48c0fc5SRavi Teja             std::string pathString =
1618e48c0fc5SRavi Teja                 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1619e48c0fc5SRavi Teja 
162001784826SJohnathan Mantey             if (!thisJson.is_null() && !thisJson.empty())
1621e48c0fc5SRavi Teja             {
1622e48c0fc5SRavi Teja                 std::optional<std::string> address;
1623e48c0fc5SRavi Teja                 std::optional<uint8_t> prefixLength;
1624e48c0fc5SRavi Teja 
1625e48c0fc5SRavi Teja                 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1626e48c0fc5SRavi Teja                                          address, "PrefixLength", prefixLength))
1627e48c0fc5SRavi Teja                 {
162801784826SJohnathan Mantey                     messages::propertyValueFormatError(
162901784826SJohnathan Mantey                         asyncResp->res, thisJson.dump(), pathString);
1630e48c0fc5SRavi Teja                     return;
1631e48c0fc5SRavi Teja                 }
1632e48c0fc5SRavi Teja 
163301784826SJohnathan Mantey                 const std::string* addr;
163401784826SJohnathan Mantey                 uint8_t prefix;
163501784826SJohnathan Mantey 
163601784826SJohnathan Mantey                 // Find the address and prefixLength values. Any values that are
163701784826SJohnathan Mantey                 // not explicitly provided are assumed to be unmodified from the
163801784826SJohnathan Mantey                 // current state of the interface. Merge existing state into the
163901784826SJohnathan Mantey                 // current request.
1640e48c0fc5SRavi Teja                 if (address)
1641e48c0fc5SRavi Teja                 {
164201784826SJohnathan Mantey                     addr = &(*address);
1643e48c0fc5SRavi Teja                 }
16442c70f800SEd Tanous                 else if (niciPentry != ipv6Data.end())
164501784826SJohnathan Mantey                 {
16462c70f800SEd Tanous                     addr = &(niciPentry->address);
164701784826SJohnathan Mantey                 }
164801784826SJohnathan Mantey                 else
164901784826SJohnathan Mantey                 {
165001784826SJohnathan Mantey                     messages::propertyMissing(asyncResp->res,
165101784826SJohnathan Mantey                                               pathString + "/Address");
165201784826SJohnathan Mantey                     return;
1653e48c0fc5SRavi Teja                 }
1654e48c0fc5SRavi Teja 
1655e48c0fc5SRavi Teja                 if (prefixLength)
1656e48c0fc5SRavi Teja                 {
165701784826SJohnathan Mantey                     prefix = *prefixLength;
165801784826SJohnathan Mantey                 }
16592c70f800SEd Tanous                 else if (niciPentry != ipv6Data.end())
1660e48c0fc5SRavi Teja                 {
16612c70f800SEd Tanous                     prefix = niciPentry->prefixLength;
1662e48c0fc5SRavi Teja                 }
1663e48c0fc5SRavi Teja                 else
1664e48c0fc5SRavi Teja                 {
1665e48c0fc5SRavi Teja                     messages::propertyMissing(asyncResp->res,
1666e48c0fc5SRavi Teja                                               pathString + "/PrefixLength");
166701784826SJohnathan Mantey                     return;
1668e48c0fc5SRavi Teja                 }
1669e48c0fc5SRavi Teja 
16702c70f800SEd Tanous                 if (niciPentry != ipv6Data.end())
1671e48c0fc5SRavi Teja                 {
16722c70f800SEd Tanous                     deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr,
1673e48c0fc5SRavi Teja                                         asyncResp);
16742c70f800SEd Tanous                     niciPentry =
16752c70f800SEd Tanous                         getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
167601784826SJohnathan Mantey                 }
167701784826SJohnathan Mantey                 else
167801784826SJohnathan Mantey                 {
167901784826SJohnathan Mantey                     createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1680e48c0fc5SRavi Teja                 }
1681e48c0fc5SRavi Teja                 entryIdx++;
1682e48c0fc5SRavi Teja             }
168301784826SJohnathan Mantey             else
168401784826SJohnathan Mantey             {
16852c70f800SEd Tanous                 if (niciPentry == ipv6Data.end())
168601784826SJohnathan Mantey                 {
168701784826SJohnathan Mantey                     // Requesting a DELETE/DO NOT MODIFY action for an item
168801784826SJohnathan Mantey                     // that isn't present on the eth(n) interface. Input JSON is
168901784826SJohnathan Mantey                     // in error, so bail out.
169001784826SJohnathan Mantey                     if (thisJson.is_null())
169101784826SJohnathan Mantey                     {
169201784826SJohnathan Mantey                         messages::resourceCannotBeDeleted(asyncResp->res);
169301784826SJohnathan Mantey                         return;
169401784826SJohnathan Mantey                     }
169501784826SJohnathan Mantey                     messages::propertyValueFormatError(
169601784826SJohnathan Mantey                         asyncResp->res, thisJson.dump(), pathString);
169701784826SJohnathan Mantey                     return;
169801784826SJohnathan Mantey                 }
169901784826SJohnathan Mantey 
170001784826SJohnathan Mantey                 if (thisJson.is_null())
170101784826SJohnathan Mantey                 {
17022c70f800SEd Tanous                     deleteIPv6(ifaceId, niciPentry->id, asyncResp);
170301784826SJohnathan Mantey                 }
17042c70f800SEd Tanous                 if (niciPentry != ipv6Data.cend())
170501784826SJohnathan Mantey                 {
17062c70f800SEd Tanous                     niciPentry =
17072c70f800SEd Tanous                         getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
170801784826SJohnathan Mantey                 }
170901784826SJohnathan Mantey                 entryIdx++;
171001784826SJohnathan Mantey             }
171101784826SJohnathan Mantey         }
1712e48c0fc5SRavi Teja     }
1713e48c0fc5SRavi Teja 
17140f74e643SEd Tanous     void parseInterfaceData(
1715eeedda23SJohnathan Mantey         std::shared_ptr<AsyncResp> asyncResp, const std::string& iface_id,
17160f74e643SEd Tanous         const EthernetInterfaceData& ethData,
1717e48c0fc5SRavi Teja         const boost::container::flat_set<IPv4AddressData>& ipv4Data,
171801784826SJohnathan Mantey         const boost::container::flat_set<IPv6AddressData>& ipv6Data)
17194a0cb85cSEd Tanous     {
1720eeedda23SJohnathan Mantey         constexpr const std::array<const char*, 1> inventoryForEthernet = {
1721eeedda23SJohnathan Mantey             "xyz.openbmc_project.Inventory.Item.Ethernet"};
1722eeedda23SJohnathan Mantey 
17232c70f800SEd Tanous         nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
17242c70f800SEd Tanous         jsonResponse["Id"] = iface_id;
17252c70f800SEd Tanous         jsonResponse["@odata.id"] =
17264a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
17272c70f800SEd Tanous         jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1728eeedda23SJohnathan Mantey 
1729eeedda23SJohnathan Mantey         auto health = std::make_shared<HealthPopulate>(asyncResp);
1730eeedda23SJohnathan Mantey 
1731eeedda23SJohnathan Mantey         crow::connections::systemBus->async_method_call(
1732eeedda23SJohnathan Mantey             [health](const boost::system::error_code ec,
1733eeedda23SJohnathan Mantey                      std::vector<std::string>& resp) {
1734eeedda23SJohnathan Mantey                 if (ec)
1735029573d4SEd Tanous                 {
1736eeedda23SJohnathan Mantey                     return;
1737eeedda23SJohnathan Mantey                 }
1738eeedda23SJohnathan Mantey 
1739eeedda23SJohnathan Mantey                 health->inventory = std::move(resp);
1740eeedda23SJohnathan Mantey             },
1741eeedda23SJohnathan Mantey             "xyz.openbmc_project.ObjectMapper",
1742eeedda23SJohnathan Mantey             "/xyz/openbmc_project/object_mapper",
1743eeedda23SJohnathan Mantey             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1744eeedda23SJohnathan Mantey             int32_t(0), inventoryForEthernet);
1745eeedda23SJohnathan Mantey 
1746eeedda23SJohnathan Mantey         health->populate();
1747eeedda23SJohnathan Mantey 
1748eeedda23SJohnathan Mantey         if (ethData.nicEnabled)
1749eeedda23SJohnathan Mantey         {
17502c70f800SEd Tanous             jsonResponse["LinkStatus"] = "LinkUp";
17512c70f800SEd Tanous             jsonResponse["Status"]["State"] = "Enabled";
1752029573d4SEd Tanous         }
1753029573d4SEd Tanous         else
1754029573d4SEd Tanous         {
17552c70f800SEd Tanous             jsonResponse["LinkStatus"] = "NoLink";
17562c70f800SEd Tanous             jsonResponse["Status"]["State"] = "Disabled";
1757029573d4SEd Tanous         }
1758aa05fb27SJohnathan Mantey 
17592c70f800SEd Tanous         jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
17602c70f800SEd Tanous         jsonResponse["SpeedMbps"] = ethData.speed;
17612c70f800SEd Tanous         jsonResponse["MACAddress"] = ethData.mac_address;
17622c70f800SEd Tanous         jsonResponse["DHCPv4"]["DHCPEnabled"] =
17631f8c7b5dSJohnathan Mantey             translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
17642c70f800SEd Tanous         jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
17652c70f800SEd Tanous         jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
17662c70f800SEd Tanous         jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
17671f8c7b5dSJohnathan Mantey 
17682c70f800SEd Tanous         jsonResponse["DHCPv6"]["OperatingMode"] =
17691f8c7b5dSJohnathan Mantey             translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
17701f8c7b5dSJohnathan Mantey                                                                    : "Disabled";
17712c70f800SEd Tanous         jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
17722c70f800SEd Tanous         jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
17732c70f800SEd Tanous         jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
17742a133282Smanojkiraneda 
17754a0cb85cSEd Tanous         if (!ethData.hostname.empty())
17764a0cb85cSEd Tanous         {
17772c70f800SEd Tanous             jsonResponse["HostName"] = ethData.hostname;
1778ab6554f1SJoshi-Mansi 
1779ab6554f1SJoshi-Mansi             // When domain name is empty then it means, that it is a network
1780ab6554f1SJoshi-Mansi             // without domain names, and the host name itself must be treated as
1781ab6554f1SJoshi-Mansi             // FQDN
17822c70f800SEd Tanous             std::string fqdn = std::move(ethData.hostname);
1783d24bfc7aSJennifer Lee             if (!ethData.domainnames.empty())
1784d24bfc7aSJennifer Lee             {
17852c70f800SEd Tanous                 fqdn += "." + ethData.domainnames[0];
1786d24bfc7aSJennifer Lee             }
17872c70f800SEd Tanous             jsonResponse["FQDN"] = fqdn;
17884a0cb85cSEd Tanous         }
17894a0cb85cSEd Tanous 
17902c70f800SEd Tanous         jsonResponse["VLANs"] = {
1791fda13ad2SSunitha Harish             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1792fda13ad2SSunitha Harish                               iface_id + "/VLANs"}};
1793fda13ad2SSunitha Harish 
17942c70f800SEd Tanous         jsonResponse["NameServers"] = ethData.nameServers;
17952c70f800SEd Tanous         jsonResponse["StaticNameServers"] = ethData.staticNameServers;
17964a0cb85cSEd Tanous 
17972c70f800SEd Tanous         nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
17982c70f800SEd Tanous         nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
17992c70f800SEd Tanous         ipv4Array = nlohmann::json::array();
18002c70f800SEd Tanous         ipv4StaticArray = nlohmann::json::array();
18012c70f800SEd Tanous         for (auto& ipv4Config : ipv4Data)
18024a0cb85cSEd Tanous         {
1803fa5053a6SGunnar Mills 
18042c70f800SEd Tanous             std::string gatewayStr = ipv4Config.gateway;
1805fa5053a6SGunnar Mills             if (gatewayStr.empty())
1806fa5053a6SGunnar Mills             {
1807fa5053a6SGunnar Mills                 gatewayStr = "0.0.0.0";
1808fa5053a6SGunnar Mills             }
1809fa5053a6SGunnar Mills 
18102c70f800SEd Tanous             ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
18112c70f800SEd Tanous                                  {"SubnetMask", ipv4Config.netmask},
18122c70f800SEd Tanous                                  {"Address", ipv4Config.address},
1813fa5053a6SGunnar Mills                                  {"Gateway", gatewayStr}});
18142c70f800SEd Tanous             if (ipv4Config.origin == "Static")
1815d1d50814SRavi Teja             {
18162c70f800SEd Tanous                 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
18172c70f800SEd Tanous                                            {"SubnetMask", ipv4Config.netmask},
18182c70f800SEd Tanous                                            {"Address", ipv4Config.address},
1819d1d50814SRavi Teja                                            {"Gateway", gatewayStr}});
1820d1d50814SRavi Teja             }
182101784826SJohnathan Mantey         }
1822d1d50814SRavi Teja 
18232c70f800SEd Tanous         jsonResponse["IPv6DefaultGateway"] = ethData.ipv6_default_gateway;
1824e48c0fc5SRavi Teja 
18252c70f800SEd Tanous         nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
18262c70f800SEd Tanous         nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
18272c70f800SEd Tanous         ipv6Array = nlohmann::json::array();
18282c70f800SEd Tanous         ipv6StaticArray = nlohmann::json::array();
18297f2e23e9SJohnathan Mantey         nlohmann::json& ipv6AddrPolicyTable =
18302c70f800SEd Tanous             jsonResponse["IPv6AddressPolicyTable"];
18317f2e23e9SJohnathan Mantey         ipv6AddrPolicyTable = nlohmann::json::array();
18322c70f800SEd Tanous         for (auto& ipv6Config : ipv6Data)
1833e48c0fc5SRavi Teja         {
18342c70f800SEd Tanous             ipv6Array.push_back({{"Address", ipv6Config.address},
18352c70f800SEd Tanous                                  {"PrefixLength", ipv6Config.prefixLength},
18362c70f800SEd Tanous                                  {"AddressOrigin", ipv6Config.origin},
18375fd16e4bSJohnathan Mantey                                  {"AddressState", nullptr}});
18382c70f800SEd Tanous             if (ipv6Config.origin == "Static")
1839e48c0fc5SRavi Teja             {
18402c70f800SEd Tanous                 ipv6StaticArray.push_back(
18412c70f800SEd Tanous                     {{"Address", ipv6Config.address},
18422c70f800SEd Tanous                      {"PrefixLength", ipv6Config.prefixLength},
18432c70f800SEd Tanous                      {"AddressOrigin", ipv6Config.origin},
18445fd16e4bSJohnathan Mantey                      {"AddressState", nullptr}});
184501784826SJohnathan Mantey             }
1846e48c0fc5SRavi Teja         }
1847588c3f0dSKowalski, Kamil     }
1848588c3f0dSKowalski, Kamil 
18499391bb9cSRapkiewicz, Pawel     /**
18509391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
18519391bb9cSRapkiewicz, Pawel      */
1852cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
18531abe55efSEd Tanous                const std::vector<std::string>& params) override
18541abe55efSEd Tanous     {
18554a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
18561abe55efSEd Tanous         if (params.size() != 1)
18571abe55efSEd Tanous         {
1858f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
18599391bb9cSRapkiewicz, Pawel             return;
18609391bb9cSRapkiewicz, Pawel         }
18619391bb9cSRapkiewicz, Pawel 
18624a0cb85cSEd Tanous         getEthernetIfaceData(
18634a0cb85cSEd Tanous             params[0],
18642c70f800SEd Tanous             [this, asyncResp, ifaceId{std::string(params[0])}](
18654a0cb85cSEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1866e48c0fc5SRavi Teja                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
186701784826SJohnathan Mantey                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
18684a0cb85cSEd Tanous                 if (!success)
18691abe55efSEd Tanous                 {
18701abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
18711abe55efSEd Tanous                     // object, and other errors
1872f12894f8SJason M. Bills                     messages::resourceNotFound(asyncResp->res,
18732c70f800SEd Tanous                                                "EthernetInterface", ifaceId);
18744a0cb85cSEd Tanous                     return;
18759391bb9cSRapkiewicz, Pawel                 }
18764c9afe43SEd Tanous 
18770f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
1878fda13ad2SSunitha Harish                     "#EthernetInterface.v1_4_1.EthernetInterface";
18790f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
18800f74e643SEd Tanous                 asyncResp->res.jsonValue["Description"] =
18810f74e643SEd Tanous                     "Management Network Interface";
18820f74e643SEd Tanous 
18832c70f800SEd Tanous                 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data,
1884eeedda23SJohnathan Mantey                                    ipv6Data);
18859391bb9cSRapkiewicz, Pawel             });
18869391bb9cSRapkiewicz, Pawel     }
18879391bb9cSRapkiewicz, Pawel 
188855c7b7a2SEd Tanous     void doPatch(crow::Response& res, const crow::Request& req,
18891abe55efSEd Tanous                  const std::vector<std::string>& params) override
18901abe55efSEd Tanous     {
18914a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
18921abe55efSEd Tanous         if (params.size() != 1)
18931abe55efSEd Tanous         {
1894f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1895588c3f0dSKowalski, Kamil             return;
1896588c3f0dSKowalski, Kamil         }
1897588c3f0dSKowalski, Kamil 
18982c70f800SEd Tanous         const std::string& ifaceId = params[0];
1899588c3f0dSKowalski, Kamil 
1900bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
1901ab6554f1SJoshi-Mansi         std::optional<std::string> fqdn;
1902d577665bSRatan Gupta         std::optional<std::string> macAddress;
19039a6fc6feSRavi Teja         std::optional<std::string> ipv6DefaultGateway;
1904d1d50814SRavi Teja         std::optional<nlohmann::json> ipv4StaticAddresses;
1905e48c0fc5SRavi Teja         std::optional<nlohmann::json> ipv6StaticAddresses;
1906f85837bfSRAJESWARAN THILLAIGOVINDAN         std::optional<std::vector<std::string>> staticNameServers;
1907da131a9aSJennifer Lee         std::optional<nlohmann::json> dhcpv4;
19081f8c7b5dSJohnathan Mantey         std::optional<nlohmann::json> dhcpv6;
1909eeedda23SJohnathan Mantey         std::optional<bool> interfaceEnabled;
19101f8c7b5dSJohnathan Mantey         DHCPParameters v4dhcpParms;
19111f8c7b5dSJohnathan Mantey         DHCPParameters v6dhcpParms;
19120627a2c7SEd Tanous 
19131f8c7b5dSJohnathan Mantey         if (!json_util::readJson(
1914ab6554f1SJoshi-Mansi                 req, res, "HostName", hostname, "FQDN", fqdn,
1915ab6554f1SJoshi-Mansi                 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1916ab6554f1SJoshi-Mansi                 macAddress, "StaticNameServers", staticNameServers,
1917ab6554f1SJoshi-Mansi                 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1918ab6554f1SJoshi-Mansi                 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1919ab6554f1SJoshi-Mansi                 "InterfaceEnabled", interfaceEnabled))
19201abe55efSEd Tanous         {
1921588c3f0dSKowalski, Kamil             return;
1922588c3f0dSKowalski, Kamil         }
1923da131a9aSJennifer Lee         if (dhcpv4)
1924da131a9aSJennifer Lee         {
19251f8c7b5dSJohnathan Mantey             if (!json_util::readJson(*dhcpv4, res, "DHCPEnabled",
19261f8c7b5dSJohnathan Mantey                                      v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
19271f8c7b5dSJohnathan Mantey                                      v4dhcpParms.useDNSServers, "UseNTPServers",
19281f8c7b5dSJohnathan Mantey                                      v4dhcpParms.useNTPServers, "UseDomainName",
19291f8c7b5dSJohnathan Mantey                                      v4dhcpParms.useUseDomainName))
19301f8c7b5dSJohnathan Mantey             {
19311f8c7b5dSJohnathan Mantey                 return;
19321f8c7b5dSJohnathan Mantey             }
19331f8c7b5dSJohnathan Mantey         }
19341f8c7b5dSJohnathan Mantey 
19351f8c7b5dSJohnathan Mantey         if (dhcpv6)
19361f8c7b5dSJohnathan Mantey         {
19371f8c7b5dSJohnathan Mantey             if (!json_util::readJson(*dhcpv6, res, "OperatingMode",
19381f8c7b5dSJohnathan Mantey                                      v6dhcpParms.dhcpv6OperatingMode,
19391f8c7b5dSJohnathan Mantey                                      "UseDNSServers", v6dhcpParms.useDNSServers,
19401f8c7b5dSJohnathan Mantey                                      "UseNTPServers", v6dhcpParms.useNTPServers,
19411f8c7b5dSJohnathan Mantey                                      "UseDomainName",
19421f8c7b5dSJohnathan Mantey                                      v6dhcpParms.useUseDomainName))
19431f8c7b5dSJohnathan Mantey             {
19441f8c7b5dSJohnathan Mantey                 return;
19451f8c7b5dSJohnathan Mantey             }
1946da131a9aSJennifer Lee         }
1947da131a9aSJennifer Lee 
194801784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
194901784826SJohnathan Mantey         // JSON preparation
19504a0cb85cSEd Tanous         getEthernetIfaceData(
19512c70f800SEd Tanous             ifaceId,
19522c70f800SEd Tanous             [this, asyncResp, ifaceId, hostname = std::move(hostname),
1953ab6554f1SJoshi-Mansi              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1954d1d50814SRavi Teja              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
19559a6fc6feSRavi Teja              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1956e48c0fc5SRavi Teja              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
19571f8c7b5dSJohnathan Mantey              staticNameServers = std::move(staticNameServers),
19581f8c7b5dSJohnathan Mantey              dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
19591f8c7b5dSJohnathan Mantey              v4dhcpParms = std::move(v4dhcpParms),
1960eeedda23SJohnathan Mantey              v6dhcpParms = std::move(v6dhcpParms),
1961eeedda23SJohnathan Mantey              interfaceEnabled = std::move(interfaceEnabled)](
19624a0cb85cSEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1963e48c0fc5SRavi Teja                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
196401784826SJohnathan Mantey                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
19651abe55efSEd Tanous                 if (!success)
19661abe55efSEd Tanous                 {
1967588c3f0dSKowalski, Kamil                     // ... otherwise return error
19681abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
19691abe55efSEd Tanous                     // object, and other errors
1970fda13ad2SSunitha Harish                     messages::resourceNotFound(asyncResp->res,
19712c70f800SEd Tanous                                                "Ethernet Interface", ifaceId);
1972588c3f0dSKowalski, Kamil                     return;
1973588c3f0dSKowalski, Kamil                 }
1974588c3f0dSKowalski, Kamil 
19751f8c7b5dSJohnathan Mantey                 if (dhcpv4 || dhcpv6)
19761f8c7b5dSJohnathan Mantey                 {
19772c70f800SEd Tanous                     handleDHCPPatch(ifaceId, ethData, std::move(v4dhcpParms),
19781f8c7b5dSJohnathan Mantey                                     std::move(v6dhcpParms), asyncResp);
19791f8c7b5dSJohnathan Mantey                 }
19801f8c7b5dSJohnathan Mantey 
19810627a2c7SEd Tanous                 if (hostname)
19821abe55efSEd Tanous                 {
19830627a2c7SEd Tanous                     handleHostnamePatch(*hostname, asyncResp);
19841abe55efSEd Tanous                 }
19850627a2c7SEd Tanous 
1986ab6554f1SJoshi-Mansi                 if (fqdn)
1987ab6554f1SJoshi-Mansi                 {
19882c70f800SEd Tanous                     handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1989ab6554f1SJoshi-Mansi                 }
1990ab6554f1SJoshi-Mansi 
1991d577665bSRatan Gupta                 if (macAddress)
1992d577665bSRatan Gupta                 {
19932c70f800SEd Tanous                     handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1994d577665bSRatan Gupta                 }
1995d577665bSRatan Gupta 
1996d1d50814SRavi Teja                 if (ipv4StaticAddresses)
1997d1d50814SRavi Teja                 {
1998537174c4SEd Tanous                     // TODO(ed) for some reason the capture of ipv4Addresses
199901784826SJohnathan Mantey                     // above is returning a const value, not a non-const
200001784826SJohnathan Mantey                     // value. This doesn't really work for us, as we need to
200101784826SJohnathan Mantey                     // be able to efficiently move out the intermedia
200201784826SJohnathan Mantey                     // nlohmann::json objects. This makes a copy of the
200301784826SJohnathan Mantey                     // structure, and operates on that, but could be done
200401784826SJohnathan Mantey                     // more efficiently
2005d1d50814SRavi Teja                     nlohmann::json ipv4Static = std::move(*ipv4StaticAddresses);
20062c70f800SEd Tanous                     handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2007d1d50814SRavi Teja                                           asyncResp);
20081abe55efSEd Tanous                 }
20090627a2c7SEd Tanous 
2010f85837bfSRAJESWARAN THILLAIGOVINDAN                 if (staticNameServers)
2011f85837bfSRAJESWARAN THILLAIGOVINDAN                 {
20122c70f800SEd Tanous                     handleStaticNameServersPatch(ifaceId, *staticNameServers,
2013f85837bfSRAJESWARAN THILLAIGOVINDAN                                                  asyncResp);
2014f85837bfSRAJESWARAN THILLAIGOVINDAN                 }
20159a6fc6feSRavi Teja 
20169a6fc6feSRavi Teja                 if (ipv6DefaultGateway)
20179a6fc6feSRavi Teja                 {
20189a6fc6feSRavi Teja                     messages::propertyNotWritable(asyncResp->res,
20199a6fc6feSRavi Teja                                                   "IPv6DefaultGateway");
20209a6fc6feSRavi Teja                 }
2021e48c0fc5SRavi Teja 
2022e48c0fc5SRavi Teja                 if (ipv6StaticAddresses)
2023e48c0fc5SRavi Teja                 {
2024e48c0fc5SRavi Teja                     nlohmann::json ipv6Static = std::move(*ipv6StaticAddresses);
20252c70f800SEd Tanous                     handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
202601784826SJohnathan Mantey                                                    ipv6Data, asyncResp);
2027e48c0fc5SRavi Teja                 }
2028eeedda23SJohnathan Mantey 
2029eeedda23SJohnathan Mantey                 if (interfaceEnabled)
2030eeedda23SJohnathan Mantey                 {
2031eeedda23SJohnathan Mantey                     setEthernetInterfaceBoolProperty(
20322c70f800SEd Tanous                         ifaceId, "NICEnabled", *interfaceEnabled, asyncResp);
2033eeedda23SJohnathan Mantey                 }
2034588c3f0dSKowalski, Kamil             });
2035588c3f0dSKowalski, Kamil     }
20369391bb9cSRapkiewicz, Pawel };
20379391bb9cSRapkiewicz, Pawel 
2038e439f0f8SKowalski, Kamil /**
20394a0cb85cSEd Tanous  * VlanNetworkInterface derived class for delivering VLANNetworkInterface
20404a0cb85cSEd Tanous  * Schema
2041e439f0f8SKowalski, Kamil  */
20421abe55efSEd Tanous class VlanNetworkInterface : public Node
20431abe55efSEd Tanous {
2044e439f0f8SKowalski, Kamil   public:
2045e439f0f8SKowalski, Kamil     /*
2046e439f0f8SKowalski, Kamil      * Default Constructor
2047e439f0f8SKowalski, Kamil      */
204852cc112dSEd Tanous     VlanNetworkInterface(App& app) :
20494a0cb85cSEd Tanous         Node(app,
20507af91514SGunnar Mills              "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/",
20511abe55efSEd Tanous              std::string(), std::string())
20521abe55efSEd Tanous     {
2053e439f0f8SKowalski, Kamil         entityPrivileges = {
2054e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
2055e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
2056e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2057e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2058e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2059e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2060e439f0f8SKowalski, Kamil     }
2061e439f0f8SKowalski, Kamil 
2062e439f0f8SKowalski, Kamil   private:
2063cb13a392SEd Tanous     void parseInterfaceData(nlohmann::json& json_response,
2064cb13a392SEd Tanous                             const std::string& parent_iface_id,
2065cb13a392SEd Tanous                             const std::string& iface_id,
2066cb13a392SEd Tanous                             const EthernetInterfaceData& ethData)
20671abe55efSEd Tanous     {
2068e439f0f8SKowalski, Kamil         // Fill out obvious data...
20694a0cb85cSEd Tanous         json_response["Id"] = iface_id;
20704a0cb85cSEd Tanous         json_response["@odata.id"] =
20714a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
20724a0cb85cSEd Tanous             "/VLANs/" + iface_id;
2073e439f0f8SKowalski, Kamil 
20744a0cb85cSEd Tanous         json_response["VLANEnable"] = true;
2075fda13ad2SSunitha Harish         if (!ethData.vlan_id.empty())
20764a0cb85cSEd Tanous         {
2077fda13ad2SSunitha Harish             json_response["VLANId"] = ethData.vlan_id.back();
20784a0cb85cSEd Tanous         }
2079e439f0f8SKowalski, Kamil     }
2080e439f0f8SKowalski, Kamil 
2081fda13ad2SSunitha Harish     bool verifyNames(const std::string& parent, const std::string& iface)
20821abe55efSEd Tanous     {
20831abe55efSEd Tanous         if (!boost::starts_with(iface, parent + "_"))
20841abe55efSEd Tanous         {
2085927a505aSKowalski, Kamil             return false;
20861abe55efSEd Tanous         }
2087927a505aSKowalski, Kamil         return true;
2088927a505aSKowalski, Kamil     }
2089927a505aSKowalski, Kamil 
2090e439f0f8SKowalski, Kamil     /**
2091e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
2092e439f0f8SKowalski, Kamil      */
2093cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
20941abe55efSEd Tanous                const std::vector<std::string>& params) override
20951abe55efSEd Tanous     {
20964a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
20974a0cb85cSEd Tanous         // TODO(Pawel) this shall be parameterized call (two params) to get
2098e439f0f8SKowalski, Kamil         // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
2099e439f0f8SKowalski, Kamil         // Check if there is required param, truly entering this shall be
2100e439f0f8SKowalski, Kamil         // impossible.
21011abe55efSEd Tanous         if (params.size() != 2)
21021abe55efSEd Tanous         {
2103f12894f8SJason M. Bills             messages::internalError(res);
2104e439f0f8SKowalski, Kamil             res.end();
2105e439f0f8SKowalski, Kamil             return;
2106e439f0f8SKowalski, Kamil         }
2107e439f0f8SKowalski, Kamil 
21082c70f800SEd Tanous         const std::string& parentIfaceId = params[0];
21092c70f800SEd Tanous         const std::string& ifaceId = params[1];
21100f74e643SEd Tanous         res.jsonValue["@odata.type"] =
21110f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
21120f74e643SEd Tanous         res.jsonValue["Name"] = "VLAN Network Interface";
2113e439f0f8SKowalski, Kamil 
21142c70f800SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
21151abe55efSEd Tanous         {
2116a434f2bdSEd Tanous             return;
2117a434f2bdSEd Tanous         }
2118a434f2bdSEd Tanous 
211901784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
212001784826SJohnathan Mantey         // JSON preparation
21214a0cb85cSEd Tanous         getEthernetIfaceData(
2122fda13ad2SSunitha Harish             params[1],
21232c70f800SEd Tanous             [this, asyncResp, parentIfaceId{std::string(params[0])},
21242c70f800SEd Tanous              ifaceId{std::string(params[1])}](
21254a0cb85cSEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
2126cb13a392SEd Tanous                 const boost::container::flat_set<IPv4AddressData>&,
2127cb13a392SEd Tanous                 const boost::container::flat_set<IPv6AddressData>&) {
2128fda13ad2SSunitha Harish                 if (success && ethData.vlan_id.size() != 0)
21291abe55efSEd Tanous                 {
21302c70f800SEd Tanous                     parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
21312c70f800SEd Tanous                                        ifaceId, ethData);
21321abe55efSEd Tanous                 }
21331abe55efSEd Tanous                 else
21341abe55efSEd Tanous                 {
2135e439f0f8SKowalski, Kamil                     // ... otherwise return error
21361abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
21371abe55efSEd Tanous                     // object, and other errors
2138f12894f8SJason M. Bills                     messages::resourceNotFound(
21392c70f800SEd Tanous                         asyncResp->res, "VLAN Network Interface", ifaceId);
2140e439f0f8SKowalski, Kamil                 }
2141e439f0f8SKowalski, Kamil             });
2142e439f0f8SKowalski, Kamil     }
2143e439f0f8SKowalski, Kamil 
214455c7b7a2SEd Tanous     void doPatch(crow::Response& res, const crow::Request& req,
21451abe55efSEd Tanous                  const std::vector<std::string>& params) override
21461abe55efSEd Tanous     {
21474a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
21481abe55efSEd Tanous         if (params.size() != 2)
21491abe55efSEd Tanous         {
2150f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2151e439f0f8SKowalski, Kamil             return;
2152e439f0f8SKowalski, Kamil         }
2153e439f0f8SKowalski, Kamil 
2154d76323e5SEd Tanous         const std::string& parentIfaceId = params[0];
215555c7b7a2SEd Tanous         const std::string& ifaceId = params[1];
2156927a505aSKowalski, Kamil 
2157fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
21581abe55efSEd Tanous         {
2159fda13ad2SSunitha Harish             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2160fda13ad2SSunitha Harish                                        ifaceId);
2161927a505aSKowalski, Kamil             return;
2162927a505aSKowalski, Kamil         }
2163927a505aSKowalski, Kamil 
21640627a2c7SEd Tanous         bool vlanEnable = false;
216538268fa8SAndrew Geissler         uint32_t vlanId = 0;
21660627a2c7SEd Tanous 
21670627a2c7SEd Tanous         if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
21680627a2c7SEd Tanous                                  vlanId))
21691abe55efSEd Tanous         {
2170927a505aSKowalski, Kamil             return;
2171927a505aSKowalski, Kamil         }
2172927a505aSKowalski, Kamil 
217301784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
217401784826SJohnathan Mantey         // JSON preparation
2175e48c0fc5SRavi Teja         getEthernetIfaceData(
2176e48c0fc5SRavi Teja             params[1],
2177271584abSEd Tanous             [asyncResp, parentIfaceId{std::string(params[0])},
2178cb13a392SEd Tanous              ifaceId{std::string(params[1])}, &vlanEnable,
2179cb13a392SEd Tanous              &vlanId](const bool& success, const EthernetInterfaceData& ethData,
2180cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
2181cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
218208244d02SSunitha Harish                 if (success && !ethData.vlan_id.empty())
218308244d02SSunitha Harish                 {
218408244d02SSunitha Harish                     auto callback =
218508244d02SSunitha Harish                         [asyncResp](const boost::system::error_code ec) {
218608244d02SSunitha Harish                             if (ec)
218708244d02SSunitha Harish                             {
218808244d02SSunitha Harish                                 messages::internalError(asyncResp->res);
218908244d02SSunitha Harish                             }
219008244d02SSunitha Harish                         };
219108244d02SSunitha Harish 
219208244d02SSunitha Harish                     if (vlanEnable == true)
219308244d02SSunitha Harish                     {
219408244d02SSunitha Harish                         crow::connections::systemBus->async_method_call(
219508244d02SSunitha Harish                             std::move(callback), "xyz.openbmc_project.Network",
219608244d02SSunitha Harish                             "/xyz/openbmc_project/network/" + ifaceId,
219708244d02SSunitha Harish                             "org.freedesktop.DBus.Properties", "Set",
219808244d02SSunitha Harish                             "xyz.openbmc_project.Network.VLAN", "Id",
219908244d02SSunitha Harish                             std::variant<uint32_t>(vlanId));
220008244d02SSunitha Harish                     }
220108244d02SSunitha Harish                     else
220208244d02SSunitha Harish                     {
2203e48c0fc5SRavi Teja                         BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2204e48c0fc5SRavi Teja                                             "vlan interface";
220508244d02SSunitha Harish                         crow::connections::systemBus->async_method_call(
220608244d02SSunitha Harish                             std::move(callback), "xyz.openbmc_project.Network",
2207e48c0fc5SRavi Teja                             std::string("/xyz/openbmc_project/network/") +
2208e48c0fc5SRavi Teja                                 ifaceId,
220908244d02SSunitha Harish                             "xyz.openbmc_project.Object.Delete", "Delete");
221008244d02SSunitha Harish                     }
221108244d02SSunitha Harish                 }
221208244d02SSunitha Harish                 else
22131abe55efSEd Tanous                 {
22141abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
22151abe55efSEd Tanous                     // object, and other errors
2216e48c0fc5SRavi Teja                     messages::resourceNotFound(
2217e48c0fc5SRavi Teja                         asyncResp->res, "VLAN Network Interface", ifaceId);
2218927a505aSKowalski, Kamil                     return;
2219927a505aSKowalski, Kamil                 }
2220927a505aSKowalski, Kamil             });
2221e439f0f8SKowalski, Kamil     }
2222e439f0f8SKowalski, Kamil 
2223cb13a392SEd Tanous     void doDelete(crow::Response& res, const crow::Request&,
22241abe55efSEd Tanous                   const std::vector<std::string>& params) override
22251abe55efSEd Tanous     {
22264a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
22271abe55efSEd Tanous         if (params.size() != 2)
22281abe55efSEd Tanous         {
2229f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2230e439f0f8SKowalski, Kamil             return;
2231e439f0f8SKowalski, Kamil         }
2232e439f0f8SKowalski, Kamil 
2233d76323e5SEd Tanous         const std::string& parentIfaceId = params[0];
223455c7b7a2SEd Tanous         const std::string& ifaceId = params[1];
2235927a505aSKowalski, Kamil 
2236fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
22371abe55efSEd Tanous         {
2238fda13ad2SSunitha Harish             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2239fda13ad2SSunitha Harish                                        ifaceId);
2240927a505aSKowalski, Kamil             return;
2241927a505aSKowalski, Kamil         }
2242927a505aSKowalski, Kamil 
224301784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
224401784826SJohnathan Mantey         // JSON preparation
2245f12894f8SJason M. Bills         getEthernetIfaceData(
2246fda13ad2SSunitha Harish             params[1],
2247271584abSEd Tanous             [asyncResp, parentIfaceId{std::string(params[0])},
2248fda13ad2SSunitha Harish              ifaceId{std::string(params[1])}](
2249f12894f8SJason M. Bills                 const bool& success, const EthernetInterfaceData& ethData,
2250cb13a392SEd Tanous                 const boost::container::flat_set<IPv4AddressData>&,
2251cb13a392SEd Tanous                 const boost::container::flat_set<IPv6AddressData>&) {
2252fda13ad2SSunitha Harish                 if (success && !ethData.vlan_id.empty())
22531abe55efSEd Tanous                 {
2254f12894f8SJason M. Bills                     auto callback =
2255f12894f8SJason M. Bills                         [asyncResp](const boost::system::error_code ec) {
22561abe55efSEd Tanous                             if (ec)
22571abe55efSEd Tanous                             {
2258f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
2259927a505aSKowalski, Kamil                             }
22604a0cb85cSEd Tanous                         };
22614a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
22624a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
22634a0cb85cSEd Tanous                         std::string("/xyz/openbmc_project/network/") + ifaceId,
22644a0cb85cSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
22651abe55efSEd Tanous                 }
22661abe55efSEd Tanous                 else
22671abe55efSEd Tanous                 {
2268927a505aSKowalski, Kamil                     // ... otherwise return error
2269f12894f8SJason M. Bills                     // TODO(Pawel)consider distinguish between non existing
2270f12894f8SJason M. Bills                     // object, and other errors
2271f12894f8SJason M. Bills                     messages::resourceNotFound(
2272f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", ifaceId);
2273927a505aSKowalski, Kamil                 }
2274927a505aSKowalski, Kamil             });
2275e439f0f8SKowalski, Kamil     }
2276e439f0f8SKowalski, Kamil };
2277e439f0f8SKowalski, Kamil 
2278e439f0f8SKowalski, Kamil /**
2279e439f0f8SKowalski, Kamil  * VlanNetworkInterfaceCollection derived class for delivering
2280e439f0f8SKowalski, Kamil  * VLANNetworkInterface Collection Schema
2281e439f0f8SKowalski, Kamil  */
22821abe55efSEd Tanous class VlanNetworkInterfaceCollection : public Node
22831abe55efSEd Tanous {
2284e439f0f8SKowalski, Kamil   public:
228552cc112dSEd Tanous     VlanNetworkInterfaceCollection(App& app) :
22864a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
22874a0cb85cSEd Tanous              std::string())
22881abe55efSEd Tanous     {
2289e439f0f8SKowalski, Kamil         entityPrivileges = {
2290e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
2291e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
2292e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2293e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2294e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2295e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2296e439f0f8SKowalski, Kamil     }
2297e439f0f8SKowalski, Kamil 
2298e439f0f8SKowalski, Kamil   private:
2299e439f0f8SKowalski, Kamil     /**
2300e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
2301e439f0f8SKowalski, Kamil      */
2302cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
23031abe55efSEd Tanous                const std::vector<std::string>& params) override
23041abe55efSEd Tanous     {
23054a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
23061abe55efSEd Tanous         if (params.size() != 1)
23071abe55efSEd Tanous         {
2308e439f0f8SKowalski, Kamil             // This means there is a problem with the router
2309f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2310e439f0f8SKowalski, Kamil             return;
2311e439f0f8SKowalski, Kamil         }
2312e439f0f8SKowalski, Kamil 
23134a0cb85cSEd Tanous         const std::string& rootInterfaceName = params[0];
2314e439f0f8SKowalski, Kamil 
23154a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
23161abe55efSEd Tanous         // preparation
2317f12894f8SJason M. Bills         getEthernetIfaceList(
231843b761d0SEd Tanous             [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
23191abe55efSEd Tanous                 const bool& success,
23204c9afe43SEd Tanous                 const boost::container::flat_set<std::string>& iface_list) {
23214a0cb85cSEd Tanous                 if (!success)
23221abe55efSEd Tanous                 {
2323f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
23244a0cb85cSEd Tanous                     return;
23251abe55efSEd Tanous                 }
23264c9afe43SEd Tanous 
23274c9afe43SEd Tanous                 if (iface_list.find(rootInterfaceName) == iface_list.end())
23284c9afe43SEd Tanous                 {
23294c9afe43SEd Tanous                     messages::resourceNotFound(asyncResp->res,
23304c9afe43SEd Tanous                                                "VLanNetworkInterfaceCollection",
23314c9afe43SEd Tanous                                                rootInterfaceName);
23324c9afe43SEd Tanous                     return;
23334c9afe43SEd Tanous                 }
23344c9afe43SEd Tanous 
23350f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
23360f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
23370f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
23380f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] =
23390f74e643SEd Tanous                     "VLAN Network Interface Collection";
23404a0cb85cSEd Tanous 
23412c70f800SEd Tanous                 nlohmann::json ifaceArray = nlohmann::json::array();
23424a0cb85cSEd Tanous 
23432c70f800SEd Tanous                 for (const std::string& ifaceItem : iface_list)
23441abe55efSEd Tanous                 {
23452c70f800SEd Tanous                     if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
23464a0cb85cSEd Tanous                     {
23472c70f800SEd Tanous                         ifaceArray.push_back(
23484a0cb85cSEd Tanous                             {{"@odata.id",
23494a0cb85cSEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
23502c70f800SEd Tanous                                   rootInterfaceName + "/VLANs/" + ifaceItem}});
2351e439f0f8SKowalski, Kamil                     }
2352e439f0f8SKowalski, Kamil                 }
2353e439f0f8SKowalski, Kamil 
23544a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
23552c70f800SEd Tanous                     ifaceArray.size();
23562c70f800SEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
23574a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
23584a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
23594a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
2360e439f0f8SKowalski, Kamil             });
2361e439f0f8SKowalski, Kamil     }
2362e439f0f8SKowalski, Kamil 
236355c7b7a2SEd Tanous     void doPost(crow::Response& res, const crow::Request& req,
23641abe55efSEd Tanous                 const std::vector<std::string>& params) override
23651abe55efSEd Tanous     {
23664a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
23671abe55efSEd Tanous         if (params.size() != 1)
23681abe55efSEd Tanous         {
2369f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2370e439f0f8SKowalski, Kamil             return;
2371e439f0f8SKowalski, Kamil         }
2372fda13ad2SSunitha Harish         bool vlanEnable = false;
23730627a2c7SEd Tanous         uint32_t vlanId = 0;
2374fda13ad2SSunitha Harish         if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable",
2375fda13ad2SSunitha Harish                                  vlanEnable))
23761abe55efSEd Tanous         {
23774a0cb85cSEd Tanous             return;
2378e439f0f8SKowalski, Kamil         }
2379fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
2380fda13ad2SSunitha Harish         if (!vlanId)
2381fda13ad2SSunitha Harish         {
2382fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
2383fda13ad2SSunitha Harish         }
2384fda13ad2SSunitha Harish         if (!vlanEnable)
2385fda13ad2SSunitha Harish         {
2386fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
2387fda13ad2SSunitha Harish         }
2388271584abSEd Tanous         if (static_cast<bool>(vlanId) ^ vlanEnable)
2389fda13ad2SSunitha Harish         {
2390fda13ad2SSunitha Harish             return;
2391fda13ad2SSunitha Harish         }
2392fda13ad2SSunitha Harish 
23934a0cb85cSEd Tanous         const std::string& rootInterfaceName = params[0];
23944a0cb85cSEd Tanous         auto callback = [asyncResp](const boost::system::error_code ec) {
23951abe55efSEd Tanous             if (ec)
23961abe55efSEd Tanous             {
23974a0cb85cSEd Tanous                 // TODO(ed) make more consistent error messages based on
23984a0cb85cSEd Tanous                 // phosphor-network responses
2399f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
24004a0cb85cSEd Tanous                 return;
24011abe55efSEd Tanous             }
2402f12894f8SJason M. Bills             messages::created(asyncResp->res);
2403e439f0f8SKowalski, Kamil         };
24044a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
24054a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
24064a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
24074a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
24080627a2c7SEd Tanous             rootInterfaceName, vlanId);
24094a0cb85cSEd Tanous     }
24104a0cb85cSEd Tanous };
24119391bb9cSRapkiewicz, Pawel } // namespace redfish
2412