xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 432a890cfca335e565b770b1604ed4e547c5a732)
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 
187e860f15SJohn Edward Broadbent #include <app.hpp>
191abe55efSEd Tanous #include <boost/container/flat_map.hpp>
204a0cb85cSEd Tanous #include <boost/container/flat_set.hpp>
21179db1d7SKowalski, Kamil #include <dbus_singleton.hpp>
22588c3f0dSKowalski, Kamil #include <error_messages.hpp>
231214b7e7SGunnar Mills #include <utils/json_utils.hpp>
241214b7e7SGunnar Mills 
25a24526dcSEd Tanous #include <optional>
26ab6554f1SJoshi-Mansi #include <regex>
27abf2add6SEd Tanous #include <variant>
289391bb9cSRapkiewicz, Pawel 
291abe55efSEd Tanous namespace redfish
301abe55efSEd Tanous {
319391bb9cSRapkiewicz, Pawel 
329391bb9cSRapkiewicz, Pawel /**
339391bb9cSRapkiewicz, Pawel  * DBus types primitives for several generic DBus interfaces
349391bb9cSRapkiewicz, Pawel  * TODO(Pawel) consider move this to separate file into boost::dbus
359391bb9cSRapkiewicz, Pawel  */
36aa2e59c1SEd Tanous using PropertiesMapType = boost::container::flat_map<
37abf2add6SEd Tanous     std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
38aa2e59c1SEd Tanous                               int32_t, uint32_t, int64_t, uint64_t, double>>;
399391bb9cSRapkiewicz, Pawel 
404a0cb85cSEd Tanous using GetManagedObjects = std::vector<std::pair<
41aa2e59c1SEd Tanous     sdbusplus::message::object_path,
424a0cb85cSEd Tanous     std::vector<std::pair<
43aa2e59c1SEd Tanous         std::string,
44aa2e59c1SEd Tanous         boost::container::flat_map<
4519bd78d9SPatrick Williams             std::string,
4619bd78d9SPatrick Williams             std::variant<std::string, bool, uint8_t, int16_t, uint16_t, int32_t,
4719bd78d9SPatrick Williams                          uint32_t, int64_t, uint64_t, double,
48029573d4SEd Tanous                          std::vector<std::string>>>>>>>;
494a0cb85cSEd Tanous 
504a0cb85cSEd Tanous enum class LinkType
514a0cb85cSEd Tanous {
524a0cb85cSEd Tanous     Local,
534a0cb85cSEd Tanous     Global
544a0cb85cSEd Tanous };
559391bb9cSRapkiewicz, Pawel 
569391bb9cSRapkiewicz, Pawel /**
579391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
589391bb9cSRapkiewicz, Pawel  */
591abe55efSEd Tanous struct IPv4AddressData
601abe55efSEd Tanous {
61179db1d7SKowalski, Kamil     std::string id;
624a0cb85cSEd Tanous     std::string address;
634a0cb85cSEd Tanous     std::string domain;
644a0cb85cSEd Tanous     std::string gateway;
659391bb9cSRapkiewicz, Pawel     std::string netmask;
669391bb9cSRapkiewicz, Pawel     std::string origin;
674a0cb85cSEd Tanous     LinkType linktype;
6801c6e858SSunitha Harish     bool isActive;
694a0cb85cSEd Tanous 
701abe55efSEd Tanous     bool operator<(const IPv4AddressData& obj) const
711abe55efSEd Tanous     {
724a0cb85cSEd Tanous         return id < obj.id;
731abe55efSEd Tanous     }
749391bb9cSRapkiewicz, Pawel };
759391bb9cSRapkiewicz, Pawel 
769391bb9cSRapkiewicz, Pawel /**
77e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
78e48c0fc5SRavi Teja  */
79e48c0fc5SRavi Teja struct IPv6AddressData
80e48c0fc5SRavi Teja {
81e48c0fc5SRavi Teja     std::string id;
82e48c0fc5SRavi Teja     std::string address;
83e48c0fc5SRavi Teja     std::string origin;
84e48c0fc5SRavi Teja     uint8_t prefixLength;
85e48c0fc5SRavi Teja 
86e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData& obj) const
87e48c0fc5SRavi Teja     {
88e48c0fc5SRavi Teja         return id < obj.id;
89e48c0fc5SRavi Teja     }
90e48c0fc5SRavi Teja };
91e48c0fc5SRavi Teja /**
929391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
939391bb9cSRapkiewicz, Pawel  * available from DBus
949391bb9cSRapkiewicz, Pawel  */
951abe55efSEd Tanous struct EthernetInterfaceData
961abe55efSEd Tanous {
974a0cb85cSEd Tanous     uint32_t speed;
984a0cb85cSEd Tanous     bool auto_neg;
991f8c7b5dSJohnathan Mantey     bool DNSEnabled;
1001f8c7b5dSJohnathan Mantey     bool NTPEnabled;
1011f8c7b5dSJohnathan Mantey     bool HostNameEnabled;
1021f8c7b5dSJohnathan Mantey     bool SendHostNameEnabled;
103aa05fb27SJohnathan Mantey     bool linkUp;
104eeedda23SJohnathan Mantey     bool nicEnabled;
1051f8c7b5dSJohnathan Mantey     std::string DHCPEnabled;
1061f8c7b5dSJohnathan Mantey     std::string operatingMode;
1074a0cb85cSEd Tanous     std::string hostname;
1084a0cb85cSEd Tanous     std::string default_gateway;
1099a6fc6feSRavi Teja     std::string ipv6_default_gateway;
1104a0cb85cSEd Tanous     std::string mac_address;
111fda13ad2SSunitha Harish     std::vector<std::uint32_t> vlan_id;
1120f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
1130f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
114d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
1159391bb9cSRapkiewicz, Pawel };
1169391bb9cSRapkiewicz, Pawel 
1171f8c7b5dSJohnathan Mantey struct DHCPParameters
1181f8c7b5dSJohnathan Mantey {
1191f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
1201f8c7b5dSJohnathan Mantey     std::optional<bool> useDNSServers;
1211f8c7b5dSJohnathan Mantey     std::optional<bool> useNTPServers;
1221f8c7b5dSJohnathan Mantey     std::optional<bool> useUseDomainName;
1231f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1241f8c7b5dSJohnathan Mantey };
1251f8c7b5dSJohnathan Mantey 
1269391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1279391bb9cSRapkiewicz, Pawel // into full dot notation
1281abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1291abe55efSEd Tanous {
1309391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1319391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1329391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1339391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1349391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1359391bb9cSRapkiewicz, Pawel     return netmask;
1369391bb9cSRapkiewicz, Pawel }
1379391bb9cSRapkiewicz, Pawel 
1381f8c7b5dSJohnathan Mantey inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
1391f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1401f8c7b5dSJohnathan Mantey {
1411f8c7b5dSJohnathan Mantey     if (isIPv4)
1421f8c7b5dSJohnathan Mantey     {
1431f8c7b5dSJohnathan Mantey         return (
1441f8c7b5dSJohnathan Mantey             (inputDHCP ==
1451f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1461f8c7b5dSJohnathan Mantey             (inputDHCP ==
1471f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1481f8c7b5dSJohnathan Mantey     }
1491f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1501f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1511f8c7b5dSJohnathan Mantey             (inputDHCP ==
1521f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1531f8c7b5dSJohnathan Mantey }
1541f8c7b5dSJohnathan Mantey 
1552c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1561f8c7b5dSJohnathan Mantey {
1571f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1581f8c7b5dSJohnathan Mantey     {
1591f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1601f8c7b5dSJohnathan Mantey     }
1613174e4dfSEd Tanous     if (isIPv4)
1621f8c7b5dSJohnathan Mantey     {
1631f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1641f8c7b5dSJohnathan Mantey     }
1653174e4dfSEd Tanous     if (isIPv6)
1661f8c7b5dSJohnathan Mantey     {
1671f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1681f8c7b5dSJohnathan Mantey     }
1691f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1701f8c7b5dSJohnathan Mantey }
1711f8c7b5dSJohnathan Mantey 
1724a0cb85cSEd Tanous inline std::string
1734a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1744a0cb85cSEd Tanous                                         bool isIPv4)
1751abe55efSEd Tanous {
1764a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1771abe55efSEd Tanous     {
1784a0cb85cSEd Tanous         return "Static";
1799391bb9cSRapkiewicz, Pawel     }
1804a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1811abe55efSEd Tanous     {
1824a0cb85cSEd Tanous         if (isIPv4)
1831abe55efSEd Tanous         {
1844a0cb85cSEd Tanous             return "IPv4LinkLocal";
1851abe55efSEd Tanous         }
1864a0cb85cSEd Tanous         return "LinkLocal";
1879391bb9cSRapkiewicz, Pawel     }
1884a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1891abe55efSEd Tanous     {
1904a0cb85cSEd Tanous         if (isIPv4)
1914a0cb85cSEd Tanous         {
1924a0cb85cSEd Tanous             return "DHCP";
1934a0cb85cSEd Tanous         }
1944a0cb85cSEd Tanous         return "DHCPv6";
1954a0cb85cSEd Tanous     }
1964a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1974a0cb85cSEd Tanous     {
1984a0cb85cSEd Tanous         return "SLAAC";
1994a0cb85cSEd Tanous     }
2004a0cb85cSEd Tanous     return "";
2014a0cb85cSEd Tanous }
2024a0cb85cSEd Tanous 
20381ce609eSEd Tanous inline bool extractEthernetInterfaceData(const std::string& ethifaceId,
20481ce609eSEd Tanous                                          GetManagedObjects& dbusData,
2054a0cb85cSEd Tanous                                          EthernetInterfaceData& ethData)
2064a0cb85cSEd Tanous {
2074c9afe43SEd Tanous     bool idFound = false;
20881ce609eSEd Tanous     for (auto& objpath : dbusData)
2094a0cb85cSEd Tanous     {
210f23b7296SEd Tanous         for (auto& ifacePair : objpath.second)
2114a0cb85cSEd Tanous         {
21281ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
213029573d4SEd Tanous             {
2144c9afe43SEd Tanous                 idFound = true;
2154a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2164a0cb85cSEd Tanous                 {
2174a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2184a0cb85cSEd Tanous                     {
2194a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2204a0cb85cSEd Tanous                         {
2214a0cb85cSEd Tanous                             const std::string* mac =
222abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2234a0cb85cSEd Tanous                             if (mac != nullptr)
2244a0cb85cSEd Tanous                             {
2254a0cb85cSEd Tanous                                 ethData.mac_address = *mac;
2264a0cb85cSEd Tanous                             }
2274a0cb85cSEd Tanous                         }
2284a0cb85cSEd Tanous                     }
2294a0cb85cSEd Tanous                 }
2304a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2314a0cb85cSEd Tanous                 {
2324a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2334a0cb85cSEd Tanous                     {
2344a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2354a0cb85cSEd Tanous                         {
2361b6b96c5SEd Tanous                             const uint32_t* id =
237abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2384a0cb85cSEd Tanous                             if (id != nullptr)
2394a0cb85cSEd Tanous                             {
240fda13ad2SSunitha Harish                                 ethData.vlan_id.push_back(*id);
2414a0cb85cSEd Tanous                             }
2424a0cb85cSEd Tanous                         }
2434a0cb85cSEd Tanous                     }
2444a0cb85cSEd Tanous                 }
2454a0cb85cSEd Tanous                 else if (ifacePair.first ==
2464a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2474a0cb85cSEd Tanous                 {
2484a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2494a0cb85cSEd Tanous                     {
2504a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2514a0cb85cSEd Tanous                         {
2522c70f800SEd Tanous                             const bool* autoNeg =
253abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2542c70f800SEd Tanous                             if (autoNeg != nullptr)
2554a0cb85cSEd Tanous                             {
2562c70f800SEd Tanous                                 ethData.auto_neg = *autoNeg;
2574a0cb85cSEd Tanous                             }
2584a0cb85cSEd Tanous                         }
2594a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2604a0cb85cSEd Tanous                         {
2614a0cb85cSEd Tanous                             const uint32_t* speed =
262abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2634a0cb85cSEd Tanous                             if (speed != nullptr)
2644a0cb85cSEd Tanous                             {
2654a0cb85cSEd Tanous                                 ethData.speed = *speed;
2664a0cb85cSEd Tanous                             }
2674a0cb85cSEd Tanous                         }
268aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
269aa05fb27SJohnathan Mantey                         {
270aa05fb27SJohnathan Mantey                             const bool* linkUp =
271aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
272aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
273aa05fb27SJohnathan Mantey                             {
274aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
275aa05fb27SJohnathan Mantey                             }
276aa05fb27SJohnathan Mantey                         }
277eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
278eeedda23SJohnathan Mantey                         {
279eeedda23SJohnathan Mantey                             const bool* nicEnabled =
280eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
281eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
282eeedda23SJohnathan Mantey                             {
283eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
284eeedda23SJohnathan Mantey                             }
285eeedda23SJohnathan Mantey                         }
286f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
287029573d4SEd Tanous                         {
288029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2898d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
290029573d4SEd Tanous                                     &propertyPair.second);
291029573d4SEd Tanous                             if (nameservers != nullptr)
292029573d4SEd Tanous                             {
293f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2940f6efdc1Smanojkiran.eda@gmail.com                             }
2950f6efdc1Smanojkiran.eda@gmail.com                         }
2960f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2970f6efdc1Smanojkiran.eda@gmail.com                         {
2980f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
2998d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
3000f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
3010f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
3020f6efdc1Smanojkiran.eda@gmail.com                             {
303f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
3044a0cb85cSEd Tanous                             }
3054a0cb85cSEd Tanous                         }
3062a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3072a133282Smanojkiraneda                         {
3082c70f800SEd Tanous                             const std::string* dhcpEnabled =
3091f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3102c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3112a133282Smanojkiraneda                             {
3122c70f800SEd Tanous                                 ethData.DHCPEnabled = *dhcpEnabled;
3132a133282Smanojkiraneda                             }
3142a133282Smanojkiraneda                         }
315d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
316d24bfc7aSJennifer Lee                         {
317d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3188d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
319d24bfc7aSJennifer Lee                                     &propertyPair.second);
320d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
321d24bfc7aSJennifer Lee                             {
322f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
323d24bfc7aSJennifer Lee                             }
324d24bfc7aSJennifer Lee                         }
3259010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway")
3269010ec2eSRavi Teja                         {
3279010ec2eSRavi Teja                             const std::string* defaultGateway =
3289010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3299010ec2eSRavi Teja                             if (defaultGateway != nullptr)
3309010ec2eSRavi Teja                             {
3319010ec2eSRavi Teja                                 std::string defaultGatewayStr = *defaultGateway;
3329010ec2eSRavi Teja                                 if (defaultGatewayStr.empty())
3339010ec2eSRavi Teja                                 {
3349010ec2eSRavi Teja                                     ethData.default_gateway = "0.0.0.0";
3359010ec2eSRavi Teja                                 }
3369010ec2eSRavi Teja                                 else
3379010ec2eSRavi Teja                                 {
3389010ec2eSRavi Teja                                     ethData.default_gateway = defaultGatewayStr;
3399010ec2eSRavi Teja                                 }
3409010ec2eSRavi Teja                             }
3419010ec2eSRavi Teja                         }
3429010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway6")
3439010ec2eSRavi Teja                         {
3449010ec2eSRavi Teja                             const std::string* defaultGateway6 =
3459010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3469010ec2eSRavi Teja                             if (defaultGateway6 != nullptr)
3479010ec2eSRavi Teja                             {
3489010ec2eSRavi Teja                                 std::string defaultGateway6Str =
3499010ec2eSRavi Teja                                     *defaultGateway6;
3509010ec2eSRavi Teja                                 if (defaultGateway6Str.empty())
3519010ec2eSRavi Teja                                 {
3529010ec2eSRavi Teja                                     ethData.ipv6_default_gateway =
3539010ec2eSRavi Teja                                         "0:0:0:0:0:0:0:0";
3549010ec2eSRavi Teja                                 }
3559010ec2eSRavi Teja                                 else
3569010ec2eSRavi Teja                                 {
3579010ec2eSRavi Teja                                     ethData.ipv6_default_gateway =
3589010ec2eSRavi Teja                                         defaultGateway6Str;
3599010ec2eSRavi Teja                                 }
3609010ec2eSRavi Teja                             }
3619010ec2eSRavi Teja                         }
362029573d4SEd Tanous                     }
363029573d4SEd Tanous                 }
364029573d4SEd Tanous             }
3651f8c7b5dSJohnathan Mantey 
3661f8c7b5dSJohnathan Mantey             if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
3671f8c7b5dSJohnathan Mantey             {
3681f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3691f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3701f8c7b5dSJohnathan Mantey                 {
3711f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3721f8c7b5dSJohnathan Mantey                     {
3731f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3741f8c7b5dSJohnathan Mantey                         {
3752c70f800SEd Tanous                             const bool* dnsEnabled =
3761f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3772c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3781f8c7b5dSJohnathan Mantey                             {
3792c70f800SEd Tanous                                 ethData.DNSEnabled = *dnsEnabled;
3801f8c7b5dSJohnathan Mantey                             }
3811f8c7b5dSJohnathan Mantey                         }
3821f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3831f8c7b5dSJohnathan Mantey                         {
3842c70f800SEd Tanous                             const bool* ntpEnabled =
3851f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3862c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3871f8c7b5dSJohnathan Mantey                             {
3882c70f800SEd Tanous                                 ethData.NTPEnabled = *ntpEnabled;
3891f8c7b5dSJohnathan Mantey                             }
3901f8c7b5dSJohnathan Mantey                         }
3911f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3921f8c7b5dSJohnathan Mantey                         {
3932c70f800SEd Tanous                             const bool* hostNameEnabled =
3941f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3952c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3961f8c7b5dSJohnathan Mantey                             {
3972c70f800SEd Tanous                                 ethData.HostNameEnabled = *hostNameEnabled;
3981f8c7b5dSJohnathan Mantey                             }
3991f8c7b5dSJohnathan Mantey                         }
4001f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "SendHostNameEnabled")
4011f8c7b5dSJohnathan Mantey                         {
4022c70f800SEd Tanous                             const bool* sendHostNameEnabled =
4031f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
4042c70f800SEd Tanous                             if (sendHostNameEnabled != nullptr)
4051f8c7b5dSJohnathan Mantey                             {
4061f8c7b5dSJohnathan Mantey                                 ethData.SendHostNameEnabled =
4072c70f800SEd Tanous                                     *sendHostNameEnabled;
4081f8c7b5dSJohnathan Mantey                             }
4091f8c7b5dSJohnathan Mantey                         }
4101f8c7b5dSJohnathan Mantey                     }
4111f8c7b5dSJohnathan Mantey                 }
4121f8c7b5dSJohnathan Mantey             }
413029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
414029573d4SEd Tanous             // to check eth number
415029573d4SEd Tanous             if (ifacePair.first ==
4164a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
4174a0cb85cSEd Tanous             {
4184a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
4194a0cb85cSEd Tanous                 {
4204a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
4214a0cb85cSEd Tanous                     {
4224a0cb85cSEd Tanous                         const std::string* hostname =
4238d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4244a0cb85cSEd Tanous                         if (hostname != nullptr)
4254a0cb85cSEd Tanous                         {
4264a0cb85cSEd Tanous                             ethData.hostname = *hostname;
4274a0cb85cSEd Tanous                         }
4284a0cb85cSEd Tanous                     }
4294a0cb85cSEd Tanous                 }
4304a0cb85cSEd Tanous             }
4314a0cb85cSEd Tanous         }
4324a0cb85cSEd Tanous     }
4334c9afe43SEd Tanous     return idFound;
4344a0cb85cSEd Tanous }
4354a0cb85cSEd Tanous 
436e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
43701784826SJohnathan Mantey inline void
43881ce609eSEd Tanous     extractIPV6Data(const std::string& ethifaceId,
43981ce609eSEd Tanous                     const GetManagedObjects& dbusData,
44081ce609eSEd Tanous                     boost::container::flat_set<IPv6AddressData>& ipv6Config)
441e48c0fc5SRavi Teja {
442e48c0fc5SRavi Teja     const std::string ipv6PathStart =
44381ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
444e48c0fc5SRavi Teja 
445e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
446e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
44781ce609eSEd Tanous     for (const auto& objpath : dbusData)
448e48c0fc5SRavi Teja     {
449e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
450e48c0fc5SRavi Teja         if (boost::starts_with(objpath.first.str, ipv6PathStart))
451e48c0fc5SRavi Teja         {
452e48c0fc5SRavi Teja             for (auto& interface : objpath.second)
453e48c0fc5SRavi Teja             {
454e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
455e48c0fc5SRavi Teja                 {
456e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
457e48c0fc5SRavi Teja                     // appropriate
458e48c0fc5SRavi Teja                     std::pair<
459e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
460e48c0fc5SRavi Teja                         bool>
46181ce609eSEd Tanous                         it = ipv6Config.insert(IPv6AddressData{});
4622c70f800SEd Tanous                     IPv6AddressData& ipv6Address = *it.first;
4632c70f800SEd Tanous                     ipv6Address.id =
464271584abSEd Tanous                         objpath.first.str.substr(ipv6PathStart.size());
465e48c0fc5SRavi Teja                     for (auto& property : interface.second)
466e48c0fc5SRavi Teja                     {
467e48c0fc5SRavi Teja                         if (property.first == "Address")
468e48c0fc5SRavi Teja                         {
469e48c0fc5SRavi Teja                             const std::string* address =
470e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
471e48c0fc5SRavi Teja                             if (address != nullptr)
472e48c0fc5SRavi Teja                             {
4732c70f800SEd Tanous                                 ipv6Address.address = *address;
474e48c0fc5SRavi Teja                             }
475e48c0fc5SRavi Teja                         }
476e48c0fc5SRavi Teja                         else if (property.first == "Origin")
477e48c0fc5SRavi Teja                         {
478e48c0fc5SRavi Teja                             const std::string* origin =
479e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
480e48c0fc5SRavi Teja                             if (origin != nullptr)
481e48c0fc5SRavi Teja                             {
4822c70f800SEd Tanous                                 ipv6Address.origin =
483e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
484e48c0fc5SRavi Teja                                                                         false);
485e48c0fc5SRavi Teja                             }
486e48c0fc5SRavi Teja                         }
487e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
488e48c0fc5SRavi Teja                         {
489e48c0fc5SRavi Teja                             const uint8_t* prefix =
490e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
491e48c0fc5SRavi Teja                             if (prefix != nullptr)
492e48c0fc5SRavi Teja                             {
4932c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
494e48c0fc5SRavi Teja                             }
495e48c0fc5SRavi Teja                         }
496e48c0fc5SRavi Teja                         else
497e48c0fc5SRavi Teja                         {
498e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
499e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
500e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
501e48c0fc5SRavi Teja                         }
502e48c0fc5SRavi Teja                     }
503e48c0fc5SRavi Teja                 }
504e48c0fc5SRavi Teja             }
505e48c0fc5SRavi Teja         }
506e48c0fc5SRavi Teja     }
507e48c0fc5SRavi Teja }
508e48c0fc5SRavi Teja 
5094a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
51001784826SJohnathan Mantey inline void
51181ce609eSEd Tanous     extractIPData(const std::string& ethifaceId,
51281ce609eSEd Tanous                   const GetManagedObjects& dbusData,
51381ce609eSEd Tanous                   boost::container::flat_set<IPv4AddressData>& ipv4Config)
5144a0cb85cSEd Tanous {
5154a0cb85cSEd Tanous     const std::string ipv4PathStart =
51681ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
5174a0cb85cSEd Tanous 
5184a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5194a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
52081ce609eSEd Tanous     for (const auto& objpath : dbusData)
5214a0cb85cSEd Tanous     {
5224a0cb85cSEd Tanous         // Check if proper pattern for object path appears
5234a0cb85cSEd Tanous         if (boost::starts_with(objpath.first.str, ipv4PathStart))
5244a0cb85cSEd Tanous         {
5254a0cb85cSEd Tanous             for (auto& interface : objpath.second)
5264a0cb85cSEd Tanous             {
5274a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5284a0cb85cSEd Tanous                 {
5294a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5304a0cb85cSEd Tanous                     // appropriate
5314a0cb85cSEd Tanous                     std::pair<
5324a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5334a0cb85cSEd Tanous                         bool>
53481ce609eSEd Tanous                         it = ipv4Config.insert(IPv4AddressData{});
5352c70f800SEd Tanous                     IPv4AddressData& ipv4Address = *it.first;
5362c70f800SEd Tanous                     ipv4Address.id =
537271584abSEd Tanous                         objpath.first.str.substr(ipv4PathStart.size());
5384a0cb85cSEd Tanous                     for (auto& property : interface.second)
5394a0cb85cSEd Tanous                     {
5404a0cb85cSEd Tanous                         if (property.first == "Address")
5414a0cb85cSEd Tanous                         {
5424a0cb85cSEd Tanous                             const std::string* address =
543abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5444a0cb85cSEd Tanous                             if (address != nullptr)
5454a0cb85cSEd Tanous                             {
5462c70f800SEd Tanous                                 ipv4Address.address = *address;
5474a0cb85cSEd Tanous                             }
5484a0cb85cSEd Tanous                         }
5494a0cb85cSEd Tanous                         else if (property.first == "Origin")
5504a0cb85cSEd Tanous                         {
5514a0cb85cSEd Tanous                             const std::string* origin =
552abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5534a0cb85cSEd Tanous                             if (origin != nullptr)
5544a0cb85cSEd Tanous                             {
5552c70f800SEd Tanous                                 ipv4Address.origin =
5564a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5574a0cb85cSEd Tanous                                                                         true);
5584a0cb85cSEd Tanous                             }
5594a0cb85cSEd Tanous                         }
5604a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5614a0cb85cSEd Tanous                         {
5624a0cb85cSEd Tanous                             const uint8_t* mask =
563abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5644a0cb85cSEd Tanous                             if (mask != nullptr)
5654a0cb85cSEd Tanous                             {
5664a0cb85cSEd Tanous                                 // convert it to the string
5672c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
5684a0cb85cSEd Tanous                             }
5694a0cb85cSEd Tanous                         }
5704a0cb85cSEd Tanous                         else
5714a0cb85cSEd Tanous                         {
5724a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5734a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5744a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5754a0cb85cSEd Tanous                         }
5764a0cb85cSEd Tanous                     }
5774a0cb85cSEd Tanous                     // Check if given address is local, or global
5782c70f800SEd Tanous                     ipv4Address.linktype =
5792c70f800SEd Tanous                         boost::starts_with(ipv4Address.address, "169.254.")
58018659d10SJohnathan Mantey                             ? LinkType::Local
58118659d10SJohnathan Mantey                             : LinkType::Global;
5824a0cb85cSEd Tanous                 }
5834a0cb85cSEd Tanous             }
5844a0cb85cSEd Tanous         }
5854a0cb85cSEd Tanous     }
5864a0cb85cSEd Tanous }
587588c3f0dSKowalski, Kamil 
588588c3f0dSKowalski, Kamil /**
589588c3f0dSKowalski, Kamil  * @brief Sets given Id on the given VLAN interface through D-Bus
590588c3f0dSKowalski, Kamil  *
591588c3f0dSKowalski, Kamil  * @param[in] ifaceId       Id of VLAN interface that should be modified
592588c3f0dSKowalski, Kamil  * @param[in] inputVlanId   New ID of the VLAN
593588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
594588c3f0dSKowalski, Kamil  *
595588c3f0dSKowalski, Kamil  * @return None.
596588c3f0dSKowalski, Kamil  */
597588c3f0dSKowalski, Kamil template <typename CallbackFunc>
5984a0cb85cSEd Tanous void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
5991abe55efSEd Tanous                   CallbackFunc&& callback)
6001abe55efSEd Tanous {
60155c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
602588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
603588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
604588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
605588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", "Id",
606abf2add6SEd Tanous         std::variant<uint32_t>(inputVlanId));
6074a0cb85cSEd Tanous }
608588c3f0dSKowalski, Kamil 
609588c3f0dSKowalski, Kamil /**
610179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
611179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
612179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
613179db1d7SKowalski, Kamil  *
614179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
615179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
616179db1d7SKowalski, Kamil  *
617179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
618179db1d7SKowalski, Kamil  */
6194a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
6201abe55efSEd Tanous                                        uint8_t* bits = nullptr)
6211abe55efSEd Tanous {
622179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
623179db1d7SKowalski, Kamil 
624179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
625179db1d7SKowalski, Kamil 
6264a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
6271abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
6281abe55efSEd Tanous     {
629179db1d7SKowalski, Kamil         return false;
630179db1d7SKowalski, Kamil     }
631179db1d7SKowalski, Kamil 
6321abe55efSEd Tanous     if (bits != nullptr)
6331abe55efSEd Tanous     {
634179db1d7SKowalski, Kamil         *bits = 0;
635179db1d7SKowalski, Kamil     }
636179db1d7SKowalski, Kamil 
637179db1d7SKowalski, Kamil     char* endPtr;
638179db1d7SKowalski, Kamil     long previousValue = 255;
639179db1d7SKowalski, Kamil     bool firstZeroInByteHit;
6401abe55efSEd Tanous     for (const std::string& byte : bytesInMask)
6411abe55efSEd Tanous     {
6421abe55efSEd Tanous         if (byte.empty())
6431abe55efSEd Tanous         {
6441db9ca37SKowalski, Kamil             return false;
6451db9ca37SKowalski, Kamil         }
6461db9ca37SKowalski, Kamil 
647179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
6481db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
649179db1d7SKowalski, Kamil 
6504a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
6514a0cb85cSEd Tanous         // is not 100% number
6521abe55efSEd Tanous         if (*endPtr != '\0')
6531abe55efSEd Tanous         {
654179db1d7SKowalski, Kamil             return false;
655179db1d7SKowalski, Kamil         }
656179db1d7SKowalski, Kamil 
657179db1d7SKowalski, Kamil         // Value should be contained in byte
6581abe55efSEd Tanous         if (value < 0 || value > 255)
6591abe55efSEd Tanous         {
660179db1d7SKowalski, Kamil             return false;
661179db1d7SKowalski, Kamil         }
662179db1d7SKowalski, Kamil 
6631abe55efSEd Tanous         if (bits != nullptr)
6641abe55efSEd Tanous         {
665179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
6661abe55efSEd Tanous             if (previousValue != 255 && value != 0)
6671abe55efSEd Tanous             {
668179db1d7SKowalski, Kamil                 return false;
669179db1d7SKowalski, Kamil             }
670179db1d7SKowalski, Kamil 
671179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
672179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
673179db1d7SKowalski, Kamil 
674179db1d7SKowalski, Kamil             // Count bits
67523a21a1cSEd Tanous             for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
6761abe55efSEd Tanous             {
67723a21a1cSEd Tanous                 if (value & (1L << bitIdx))
6781abe55efSEd Tanous                 {
6791abe55efSEd Tanous                     if (firstZeroInByteHit)
6801abe55efSEd Tanous                     {
681179db1d7SKowalski, Kamil                         // Continuity not preserved
682179db1d7SKowalski, Kamil                         return false;
6831abe55efSEd Tanous                     }
684179db1d7SKowalski, Kamil                     (*bits)++;
685179db1d7SKowalski, Kamil                 }
6861abe55efSEd Tanous                 else
6871abe55efSEd Tanous                 {
688179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
689179db1d7SKowalski, Kamil                 }
690179db1d7SKowalski, Kamil             }
691179db1d7SKowalski, Kamil         }
692179db1d7SKowalski, Kamil 
693179db1d7SKowalski, Kamil         previousValue = value;
694179db1d7SKowalski, Kamil     }
695179db1d7SKowalski, Kamil 
696179db1d7SKowalski, Kamil     return true;
697179db1d7SKowalski, Kamil }
698179db1d7SKowalski, Kamil 
699179db1d7SKowalski, Kamil /**
70001784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
701179db1d7SKowalski, Kamil  *
702179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
703179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
704179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
705179db1d7SKowalski, Kamil  *
706179db1d7SKowalski, Kamil  * @return None
707179db1d7SKowalski, Kamil  */
7084a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
7098d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7101abe55efSEd Tanous {
71155c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
712286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
7131abe55efSEd Tanous             if (ec)
7141abe55efSEd Tanous             {
715a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
7161abe55efSEd Tanous             }
717179db1d7SKowalski, Kamil         },
718179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
719179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
720179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
721179db1d7SKowalski, Kamil }
722179db1d7SKowalski, Kamil 
723244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway(
724244b6d5bSGunnar Mills     const std::string& ifaceId, const std::string& gateway,
725244b6d5bSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7269010ec2eSRavi Teja {
7279010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7289010ec2eSRavi Teja         [asyncResp](const boost::system::error_code ec) {
7299010ec2eSRavi Teja             if (ec)
7309010ec2eSRavi Teja             {
7319010ec2eSRavi Teja                 messages::internalError(asyncResp->res);
7329010ec2eSRavi Teja                 return;
7339010ec2eSRavi Teja             }
7349010ec2eSRavi Teja             asyncResp->res.result(boost::beast::http::status::no_content);
7359010ec2eSRavi Teja         },
7369010ec2eSRavi Teja         "xyz.openbmc_project.Network",
7379010ec2eSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
7389010ec2eSRavi Teja         "org.freedesktop.DBus.Properties", "Set",
7399010ec2eSRavi Teja         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
7409010ec2eSRavi Teja         std::variant<std::string>(gateway));
7419010ec2eSRavi Teja }
742179db1d7SKowalski, Kamil /**
74301784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
744179db1d7SKowalski, Kamil  *
74501784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
74601784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
74701784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
74801784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
749179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
750179db1d7SKowalski, Kamil  *
751179db1d7SKowalski, Kamil  * @return None
752179db1d7SKowalski, Kamil  */
753cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
754cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
7558d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7561abe55efSEd Tanous {
7579010ec2eSRavi Teja     auto createIpHandler = [asyncResp, ifaceId,
7589010ec2eSRavi Teja                             gateway](const boost::system::error_code ec) {
7591abe55efSEd Tanous         if (ec)
7601abe55efSEd Tanous         {
761a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
7629010ec2eSRavi Teja             return;
763179db1d7SKowalski, Kamil         }
7649010ec2eSRavi Teja         updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
7659010ec2eSRavi Teja     };
7669010ec2eSRavi Teja 
7679010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7689010ec2eSRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
769179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
770179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
77101784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
772179db1d7SKowalski, Kamil         gateway);
773179db1d7SKowalski, Kamil }
774e48c0fc5SRavi Teja 
775e48c0fc5SRavi Teja /**
77601784826SJohnathan Mantey  * @brief Deletes the IPv4 entry for this interface and creates a replacement
77701784826SJohnathan Mantey  * static IPv4 entry
77801784826SJohnathan Mantey  *
77901784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
78001784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
78101784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
78201784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
78301784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
78401784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
78501784826SJohnathan Mantey  *
78601784826SJohnathan Mantey  * @return None
78701784826SJohnathan Mantey  */
7888d1b46d7Szhanghch05 inline void
7898d1b46d7Szhanghch05     deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
7908d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& gateway,
79101784826SJohnathan Mantey                         const std::string& address,
7928d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
79301784826SJohnathan Mantey {
79401784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
79501784826SJohnathan Mantey         [asyncResp, ifaceId, address, prefixLength,
79601784826SJohnathan Mantey          gateway](const boost::system::error_code ec) {
79701784826SJohnathan Mantey             if (ec)
79801784826SJohnathan Mantey             {
79901784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
8009010ec2eSRavi Teja                 return;
80101784826SJohnathan Mantey             }
8029010ec2eSRavi Teja 
80301784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
8049010ec2eSRavi Teja                 [asyncResp, ifaceId,
8059010ec2eSRavi Teja                  gateway](const boost::system::error_code ec2) {
80623a21a1cSEd Tanous                     if (ec2)
80701784826SJohnathan Mantey                     {
80801784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
8099010ec2eSRavi Teja                         return;
81001784826SJohnathan Mantey                     }
8119010ec2eSRavi Teja                     updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
81201784826SJohnathan Mantey                 },
81301784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
81401784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
81501784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
81601784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
81701784826SJohnathan Mantey                 prefixLength, gateway);
81801784826SJohnathan Mantey         },
81901784826SJohnathan Mantey         "xyz.openbmc_project.Network",
82001784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
82101784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
82201784826SJohnathan Mantey }
82301784826SJohnathan Mantey 
82401784826SJohnathan Mantey /**
825e48c0fc5SRavi Teja  * @brief Deletes given IPv6
826e48c0fc5SRavi Teja  *
827e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
828e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
829e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
830e48c0fc5SRavi Teja  *
831e48c0fc5SRavi Teja  * @return None
832e48c0fc5SRavi Teja  */
833e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
8348d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
835e48c0fc5SRavi Teja {
836e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
837286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
838e48c0fc5SRavi Teja             if (ec)
839e48c0fc5SRavi Teja             {
840e48c0fc5SRavi Teja                 messages::internalError(asyncResp->res);
841e48c0fc5SRavi Teja             }
842e48c0fc5SRavi Teja         },
843e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
844e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
845e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
846e48c0fc5SRavi Teja }
847e48c0fc5SRavi Teja 
848e48c0fc5SRavi Teja /**
84901784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
85001784826SJohnathan Mantey  * static IPv6 entry
85101784826SJohnathan Mantey  *
85201784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
85301784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
85401784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
85501784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
85601784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
85701784826SJohnathan Mantey  *
85801784826SJohnathan Mantey  * @return None
85901784826SJohnathan Mantey  */
8608d1b46d7Szhanghch05 inline void
8618d1b46d7Szhanghch05     deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
8628d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& address,
8638d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
86401784826SJohnathan Mantey {
86501784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
86601784826SJohnathan Mantey         [asyncResp, ifaceId, address,
86701784826SJohnathan Mantey          prefixLength](const boost::system::error_code ec) {
86801784826SJohnathan Mantey             if (ec)
86901784826SJohnathan Mantey             {
87001784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
87101784826SJohnathan Mantey             }
87201784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
87323a21a1cSEd Tanous                 [asyncResp](const boost::system::error_code ec2) {
87423a21a1cSEd Tanous                     if (ec2)
87501784826SJohnathan Mantey                     {
87601784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
87701784826SJohnathan Mantey                     }
87801784826SJohnathan Mantey                 },
87901784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
88001784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
88101784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
88201784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
88301784826SJohnathan Mantey                 prefixLength, "");
88401784826SJohnathan Mantey         },
88501784826SJohnathan Mantey         "xyz.openbmc_project.Network",
88601784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
88701784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
88801784826SJohnathan Mantey }
88901784826SJohnathan Mantey 
89001784826SJohnathan Mantey /**
891e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
892e48c0fc5SRavi Teja  *
893e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
894e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
895e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
896e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
897e48c0fc5SRavi Teja  *
898e48c0fc5SRavi Teja  * @return None
899e48c0fc5SRavi Teja  */
90001784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
90101784826SJohnathan Mantey                        const std::string& address,
9028d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
903e48c0fc5SRavi Teja {
904e48c0fc5SRavi Teja     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
905e48c0fc5SRavi Teja         if (ec)
906e48c0fc5SRavi Teja         {
907e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
908e48c0fc5SRavi Teja         }
909e48c0fc5SRavi Teja     };
910e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
9114e0453b1SGunnar Mills     // does not have associated gateway property
912e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
913e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
914e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
915e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
916e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
917e48c0fc5SRavi Teja         "");
918e48c0fc5SRavi Teja }
919e48c0fc5SRavi Teja 
920179db1d7SKowalski, Kamil /**
921179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
922179db1d7SKowalski, Kamil  * Object
923179db1d7SKowalski, Kamil  * from EntityManager Network Manager
9244a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
925179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
926179db1d7SKowalski, Kamil  * into JSON
927179db1d7SKowalski, Kamil  */
928179db1d7SKowalski, Kamil template <typename CallbackFunc>
92981ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
9301abe55efSEd Tanous                           CallbackFunc&& callback)
9311abe55efSEd Tanous {
93255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
93381ce609eSEd Tanous         [ethifaceId{std::string{ethifaceId}}, callback{std::move(callback)}](
93481ce609eSEd Tanous             const boost::system::error_code errorCode,
935f23b7296SEd Tanous             GetManagedObjects& resp) {
93655c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
9374a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
938e48c0fc5SRavi Teja             boost::container::flat_set<IPv6AddressData> ipv6Data;
939179db1d7SKowalski, Kamil 
94081ce609eSEd Tanous             if (errorCode)
9411abe55efSEd Tanous             {
94201784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
943179db1d7SKowalski, Kamil                 return;
944179db1d7SKowalski, Kamil             }
945179db1d7SKowalski, Kamil 
9464c9afe43SEd Tanous             bool found =
9472c70f800SEd Tanous                 extractEthernetInterfaceData(ethifaceId, resp, ethData);
9484c9afe43SEd Tanous             if (!found)
9494c9afe43SEd Tanous             {
95001784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
9514c9afe43SEd Tanous                 return;
9524c9afe43SEd Tanous             }
9534c9afe43SEd Tanous 
9542c70f800SEd Tanous             extractIPData(ethifaceId, resp, ipv4Data);
955179db1d7SKowalski, Kamil             // Fix global GW
9561abe55efSEd Tanous             for (IPv4AddressData& ipv4 : ipv4Data)
9571abe55efSEd Tanous             {
958c619141bSRavi Teja                 if (((ipv4.linktype == LinkType::Global) &&
959c619141bSRavi Teja                      (ipv4.gateway == "0.0.0.0")) ||
9609010ec2eSRavi Teja                     (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
9611abe55efSEd Tanous                 {
9624a0cb85cSEd Tanous                     ipv4.gateway = ethData.default_gateway;
963179db1d7SKowalski, Kamil                 }
964179db1d7SKowalski, Kamil             }
965179db1d7SKowalski, Kamil 
9662c70f800SEd Tanous             extractIPV6Data(ethifaceId, resp, ipv6Data);
9674e0453b1SGunnar Mills             // Finally make a callback with useful data
96801784826SJohnathan Mantey             callback(true, ethData, ipv4Data, ipv6Data);
969179db1d7SKowalski, Kamil         },
970179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
971179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
972271584abSEd Tanous }
973179db1d7SKowalski, Kamil 
974179db1d7SKowalski, Kamil /**
9759391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
9769391bb9cSRapkiewicz, Pawel  * Manager
9771abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
9781abe55efSEd Tanous  * into JSON.
9799391bb9cSRapkiewicz, Pawel  */
9809391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
9811abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
9821abe55efSEd Tanous {
98355c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
9844a0cb85cSEd Tanous         [callback{std::move(callback)}](
98581ce609eSEd Tanous             const boost::system::error_code errorCode,
9864a0cb85cSEd Tanous             GetManagedObjects& resp) {
9871abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
9881abe55efSEd Tanous             // ethernet interfaces
9892c70f800SEd Tanous             boost::container::flat_set<std::string> ifaceList;
9902c70f800SEd Tanous             ifaceList.reserve(resp.size());
99181ce609eSEd Tanous             if (errorCode)
9921abe55efSEd Tanous             {
9932c70f800SEd Tanous                 callback(false, ifaceList);
9949391bb9cSRapkiewicz, Pawel                 return;
9959391bb9cSRapkiewicz, Pawel             }
9969391bb9cSRapkiewicz, Pawel 
9979391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
9984a0cb85cSEd Tanous             for (const auto& objpath : resp)
9991abe55efSEd Tanous             {
10009391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
10014a0cb85cSEd Tanous                 for (const auto& interface : objpath.second)
10021abe55efSEd Tanous                 {
10031abe55efSEd Tanous                     // If interface is
10044a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
10054a0cb85cSEd Tanous                     // what we're looking for.
10069391bb9cSRapkiewicz, Pawel                     if (interface.first ==
10071abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
10081abe55efSEd Tanous                     {
10092dfd18efSEd Tanous                         std::string ifaceId = objpath.first.filename();
10102dfd18efSEd Tanous                         if (ifaceId.empty())
10111abe55efSEd Tanous                         {
10122dfd18efSEd Tanous                             continue;
10139391bb9cSRapkiewicz, Pawel                         }
10142dfd18efSEd Tanous                         // and put it into output vector.
10152dfd18efSEd Tanous                         ifaceList.emplace(ifaceId);
10169391bb9cSRapkiewicz, Pawel                     }
10179391bb9cSRapkiewicz, Pawel                 }
10189391bb9cSRapkiewicz, Pawel             }
1019a434f2bdSEd Tanous             // Finally make a callback with useful data
10202c70f800SEd Tanous             callback(true, ifaceList);
10219391bb9cSRapkiewicz, Pawel         },
1022aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1023aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1024271584abSEd Tanous }
10259391bb9cSRapkiewicz, Pawel 
1026bf648f77SEd Tanous void handleHostnamePatch(const std::string& hostname,
10278d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
10281abe55efSEd Tanous {
1029ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
1030ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
1031ab6554f1SJoshi-Mansi     {
1032ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
1033ab6554f1SJoshi-Mansi                                            "HostName");
1034ab6554f1SJoshi-Mansi         return;
1035ab6554f1SJoshi-Mansi     }
1036bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
1037bc0bd6e0SEd Tanous         [asyncResp](const boost::system::error_code ec) {
10384a0cb85cSEd Tanous             if (ec)
10394a0cb85cSEd Tanous             {
1040a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
10411abe55efSEd Tanous             }
1042bc0bd6e0SEd Tanous         },
1043bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1044bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
1045bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1046abf2add6SEd Tanous         std::variant<std::string>(hostname));
1047588c3f0dSKowalski, Kamil }
1048588c3f0dSKowalski, Kamil 
1049bf648f77SEd Tanous void handleDomainnamePatch(const std::string& ifaceId,
1050bf648f77SEd Tanous                            const std::string& domainname,
10518d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1052ab6554f1SJoshi-Mansi {
1053ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
1054ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
1055ab6554f1SJoshi-Mansi         [asyncResp](const boost::system::error_code ec) {
1056ab6554f1SJoshi-Mansi             if (ec)
1057ab6554f1SJoshi-Mansi             {
1058ab6554f1SJoshi-Mansi                 messages::internalError(asyncResp->res);
1059ab6554f1SJoshi-Mansi             }
1060ab6554f1SJoshi-Mansi         },
1061ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
1062ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
1063ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
1064ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1065ab6554f1SJoshi-Mansi         std::variant<std::vector<std::string>>(vectorDomainname));
1066ab6554f1SJoshi-Mansi }
1067ab6554f1SJoshi-Mansi 
1068bf648f77SEd Tanous bool isHostnameValid(const std::string& hostname)
1069bf648f77SEd Tanous {
1070bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
1071bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1072bf648f77SEd Tanous     {
1073bf648f77SEd Tanous         return false;
1074bf648f77SEd Tanous     }
1075bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
1076bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
1077bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
1078bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
1079bf648f77SEd Tanous     const std::regex pattern(
1080bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1081bf648f77SEd Tanous 
1082bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
1083bf648f77SEd Tanous }
1084bf648f77SEd Tanous 
1085bf648f77SEd Tanous bool isDomainnameValid(const std::string& domainname)
1086bf648f77SEd Tanous {
1087bf648f77SEd Tanous     // Can have multiple subdomains
1088bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
1089bf648f77SEd Tanous     const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]"
1090bf648f77SEd Tanous                              "{1,30}\\.)*[a-zA-Z]{2,}$");
1091bf648f77SEd Tanous 
1092bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
1093bf648f77SEd Tanous }
1094bf648f77SEd Tanous 
1095ab6554f1SJoshi-Mansi void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
10968d1b46d7Szhanghch05                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1097ab6554f1SJoshi-Mansi {
1098ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
1099ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
1100ab6554f1SJoshi-Mansi     {
1101ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1102ab6554f1SJoshi-Mansi         return;
1103ab6554f1SJoshi-Mansi     }
1104ab6554f1SJoshi-Mansi 
1105ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
1106ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
1107ab6554f1SJoshi-Mansi     {
1108ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1109ab6554f1SJoshi-Mansi         return;
1110ab6554f1SJoshi-Mansi     }
1111ab6554f1SJoshi-Mansi 
1112ab6554f1SJoshi-Mansi     std::string hostname;
1113ab6554f1SJoshi-Mansi     std::string domainname;
1114ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
1115ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
1116ab6554f1SJoshi-Mansi 
1117ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1118ab6554f1SJoshi-Mansi     {
1119ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1120ab6554f1SJoshi-Mansi         return;
1121ab6554f1SJoshi-Mansi     }
1122ab6554f1SJoshi-Mansi 
1123ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
1124ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
1125ab6554f1SJoshi-Mansi }
1126ab6554f1SJoshi-Mansi 
1127bf648f77SEd Tanous void handleMACAddressPatch(const std::string& ifaceId,
1128bf648f77SEd Tanous                            const std::string& macAddress,
11298d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1130d577665bSRatan Gupta {
1131d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
1132d577665bSRatan Gupta         [asyncResp, macAddress](const boost::system::error_code ec) {
1133d577665bSRatan Gupta             if (ec)
1134d577665bSRatan Gupta             {
1135d577665bSRatan Gupta                 messages::internalError(asyncResp->res);
1136d577665bSRatan Gupta                 return;
1137d577665bSRatan Gupta             }
1138d577665bSRatan Gupta         },
1139d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1140d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1141d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1142d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1143d577665bSRatan Gupta         std::variant<std::string>(macAddress));
1144d577665bSRatan Gupta }
1145286b9118SJohnathan Mantey 
1146bf648f77SEd Tanous void setDHCPEnabled(const std::string& ifaceId, const std::string& propertyName,
1147bf648f77SEd Tanous                     const bool v4Value, const bool v6Value,
11488d1b46d7Szhanghch05                     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1149da131a9aSJennifer Lee {
11502c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1151da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1152da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1153da131a9aSJennifer Lee             if (ec)
1154da131a9aSJennifer Lee             {
1155da131a9aSJennifer Lee                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1156da131a9aSJennifer Lee                 messages::internalError(asyncResp->res);
1157da131a9aSJennifer Lee                 return;
1158da131a9aSJennifer Lee             }
11598f7e9c19SJayaprakash Mutyala             messages::success(asyncResp->res);
1160da131a9aSJennifer Lee         },
1161da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1162da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1163da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1164da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
11651f8c7b5dSJohnathan Mantey         std::variant<std::string>{dhcp});
1166da131a9aSJennifer Lee }
11671f8c7b5dSJohnathan Mantey 
1168eeedda23SJohnathan Mantey void setEthernetInterfaceBoolProperty(
1169eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
11708d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1171eeedda23SJohnathan Mantey {
1172eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1173eeedda23SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1174eeedda23SJohnathan Mantey             if (ec)
1175eeedda23SJohnathan Mantey             {
1176eeedda23SJohnathan Mantey                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1177eeedda23SJohnathan Mantey                 messages::internalError(asyncResp->res);
1178eeedda23SJohnathan Mantey                 return;
1179eeedda23SJohnathan Mantey             }
1180eeedda23SJohnathan Mantey         },
1181eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1182eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1183eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1184eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1185eeedda23SJohnathan Mantey         std::variant<bool>{value});
1186eeedda23SJohnathan Mantey }
1187eeedda23SJohnathan Mantey 
1188da131a9aSJennifer Lee void setDHCPv4Config(const std::string& propertyName, const bool& value,
11898d1b46d7Szhanghch05                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1190da131a9aSJennifer Lee {
1191da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1192da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1193da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1194da131a9aSJennifer Lee             if (ec)
1195da131a9aSJennifer Lee             {
1196da131a9aSJennifer Lee                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1197da131a9aSJennifer Lee                 messages::internalError(asyncResp->res);
1198da131a9aSJennifer Lee                 return;
1199da131a9aSJennifer Lee             }
1200da131a9aSJennifer Lee         },
1201da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1202da131a9aSJennifer Lee         "/xyz/openbmc_project/network/config/dhcp",
1203da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1204da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1205da131a9aSJennifer Lee         std::variant<bool>{value});
1206da131a9aSJennifer Lee }
1207d577665bSRatan Gupta 
12081f8c7b5dSJohnathan Mantey void handleDHCPPatch(const std::string& ifaceId,
12091f8c7b5dSJohnathan Mantey                      const EthernetInterfaceData& ethData,
1210f23b7296SEd Tanous                      const DHCPParameters& v4dhcpParms,
1211f23b7296SEd Tanous                      const DHCPParameters& v6dhcpParms,
12128d1b46d7Szhanghch05                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1213da131a9aSJennifer Lee {
12141f8c7b5dSJohnathan Mantey     bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1215bf648f77SEd Tanous     bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1216da131a9aSJennifer Lee 
12171f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
12181f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
12191f8c7b5dSJohnathan Mantey 
12201f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
12211f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1222da131a9aSJennifer Lee     {
12231f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
12241f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
12251f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
12261f8c7b5dSJohnathan Mantey         {
1227bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1228bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
12291f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1230da131a9aSJennifer Lee             return;
1231da131a9aSJennifer Lee         }
12321f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
12331f8c7b5dSJohnathan Mantey     }
12341f8c7b5dSJohnathan Mantey     else
1235da131a9aSJennifer Lee     {
12361f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
12371f8c7b5dSJohnathan Mantey     }
12381f8c7b5dSJohnathan Mantey 
12391f8c7b5dSJohnathan Mantey     bool nextDNS{};
12401f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
12411f8c7b5dSJohnathan Mantey     {
12421f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
12431f8c7b5dSJohnathan Mantey         {
12441f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12451f8c7b5dSJohnathan Mantey             return;
12461f8c7b5dSJohnathan Mantey         }
12471f8c7b5dSJohnathan Mantey         nextDNS = *v4dhcpParms.useDNSServers;
12481f8c7b5dSJohnathan Mantey     }
12491f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useDNSServers)
12501f8c7b5dSJohnathan Mantey     {
12511f8c7b5dSJohnathan Mantey         nextDNS = *v4dhcpParms.useDNSServers;
12521f8c7b5dSJohnathan Mantey     }
12531f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useDNSServers)
12541f8c7b5dSJohnathan Mantey     {
12551f8c7b5dSJohnathan Mantey         nextDNS = *v6dhcpParms.useDNSServers;
12561f8c7b5dSJohnathan Mantey     }
12571f8c7b5dSJohnathan Mantey     else
12581f8c7b5dSJohnathan Mantey     {
12591f8c7b5dSJohnathan Mantey         nextDNS = ethData.DNSEnabled;
12601f8c7b5dSJohnathan Mantey     }
12611f8c7b5dSJohnathan Mantey 
12621f8c7b5dSJohnathan Mantey     bool nextNTP{};
12631f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
12641f8c7b5dSJohnathan Mantey     {
12651f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
12661f8c7b5dSJohnathan Mantey         {
12671f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12681f8c7b5dSJohnathan Mantey             return;
12691f8c7b5dSJohnathan Mantey         }
12701f8c7b5dSJohnathan Mantey         nextNTP = *v4dhcpParms.useNTPServers;
12711f8c7b5dSJohnathan Mantey     }
12721f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useNTPServers)
12731f8c7b5dSJohnathan Mantey     {
12741f8c7b5dSJohnathan Mantey         nextNTP = *v4dhcpParms.useNTPServers;
12751f8c7b5dSJohnathan Mantey     }
12761f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useNTPServers)
12771f8c7b5dSJohnathan Mantey     {
12781f8c7b5dSJohnathan Mantey         nextNTP = *v6dhcpParms.useNTPServers;
12791f8c7b5dSJohnathan Mantey     }
12801f8c7b5dSJohnathan Mantey     else
12811f8c7b5dSJohnathan Mantey     {
12821f8c7b5dSJohnathan Mantey         nextNTP = ethData.NTPEnabled;
12831f8c7b5dSJohnathan Mantey     }
12841f8c7b5dSJohnathan Mantey 
12851f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
12861f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
12871f8c7b5dSJohnathan Mantey     {
12881f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
12891f8c7b5dSJohnathan Mantey         {
12901f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12911f8c7b5dSJohnathan Mantey             return;
12921f8c7b5dSJohnathan Mantey         }
12931f8c7b5dSJohnathan Mantey         nextUseDomain = *v4dhcpParms.useUseDomainName;
12941f8c7b5dSJohnathan Mantey     }
12951f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useUseDomainName)
12961f8c7b5dSJohnathan Mantey     {
12971f8c7b5dSJohnathan Mantey         nextUseDomain = *v4dhcpParms.useUseDomainName;
12981f8c7b5dSJohnathan Mantey     }
12991f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useUseDomainName)
13001f8c7b5dSJohnathan Mantey     {
13011f8c7b5dSJohnathan Mantey         nextUseDomain = *v6dhcpParms.useUseDomainName;
13021f8c7b5dSJohnathan Mantey     }
13031f8c7b5dSJohnathan Mantey     else
13041f8c7b5dSJohnathan Mantey     {
13051f8c7b5dSJohnathan Mantey         nextUseDomain = ethData.HostNameEnabled;
13061f8c7b5dSJohnathan Mantey     }
13071f8c7b5dSJohnathan Mantey 
1308da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
13091f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
13101f8c7b5dSJohnathan Mantey                    asyncResp);
1311da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
13121f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1313da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
13141f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
13151f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
13161f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1317da131a9aSJennifer Lee }
131801784826SJohnathan Mantey 
131901784826SJohnathan Mantey boost::container::flat_set<IPv4AddressData>::const_iterator
13202c70f800SEd Tanous     getNextStaticIpEntry(
1321bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1322bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
132301784826SJohnathan Mantey {
132417a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
132517a897dfSManojkiran Eda         return value.origin == "Static";
132617a897dfSManojkiran Eda     });
132701784826SJohnathan Mantey }
132801784826SJohnathan Mantey 
132901784826SJohnathan Mantey boost::container::flat_set<IPv6AddressData>::const_iterator
13302c70f800SEd Tanous     getNextStaticIpEntry(
1331bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1332bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
133301784826SJohnathan Mantey {
133417a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
133517a897dfSManojkiran Eda         return value.origin == "Static";
133617a897dfSManojkiran Eda     });
133701784826SJohnathan Mantey }
133801784826SJohnathan Mantey 
1339d1d50814SRavi Teja void handleIPv4StaticPatch(
1340f476acbfSRatan Gupta     const std::string& ifaceId, nlohmann::json& input,
134101784826SJohnathan Mantey     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
13428d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13431abe55efSEd Tanous {
134401784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1345f476acbfSRatan Gupta     {
134671f52d96SEd Tanous         messages::propertyValueTypeError(
134771f52d96SEd Tanous             asyncResp->res,
1348bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1349d1d50814SRavi Teja             "IPv4StaticAddresses");
1350f476acbfSRatan Gupta         return;
1351f476acbfSRatan Gupta     }
1352f476acbfSRatan Gupta 
1353271584abSEd Tanous     unsigned entryIdx = 1;
135401784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
135501784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
135601784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
135701784826SJohnathan Mantey     // into the NIC.
13582c70f800SEd Tanous     boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry =
13592c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
136001784826SJohnathan Mantey 
1361537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
13621abe55efSEd Tanous     {
13634a0cb85cSEd Tanous         std::string pathString =
1364d1d50814SRavi Teja             "IPv4StaticAddresses/" + std::to_string(entryIdx);
1365179db1d7SKowalski, Kamil 
136601784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1367f476acbfSRatan Gupta         {
1368537174c4SEd Tanous             std::optional<std::string> address;
1369537174c4SEd Tanous             std::optional<std::string> subnetMask;
1370537174c4SEd Tanous             std::optional<std::string> gateway;
1371537174c4SEd Tanous 
1372537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
13737e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
13747e27d832SJohnathan Mantey                                      "Gateway", gateway))
1375537174c4SEd Tanous             {
137601784826SJohnathan Mantey                 messages::propertyValueFormatError(
137771f52d96SEd Tanous                     asyncResp->res,
137871f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
137971f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
138071f52d96SEd Tanous                     pathString);
1381537174c4SEd Tanous                 return;
1382179db1d7SKowalski, Kamil             }
1383179db1d7SKowalski, Kamil 
138401784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
138501784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
138601784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
138701784826SJohnathan Mantey             // current request.
1388271584abSEd Tanous             const std::string* addr = nullptr;
1389271584abSEd Tanous             const std::string* gw = nullptr;
139001784826SJohnathan Mantey             uint8_t prefixLength = 0;
139101784826SJohnathan Mantey             bool errorInEntry = false;
1392537174c4SEd Tanous             if (address)
13931abe55efSEd Tanous             {
139401784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*address))
13951abe55efSEd Tanous                 {
139601784826SJohnathan Mantey                     addr = &(*address);
13974a0cb85cSEd Tanous                 }
139801784826SJohnathan Mantey                 else
139901784826SJohnathan Mantey                 {
1400bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1401bf648f77SEd Tanous                                                        pathString + "/Address");
140201784826SJohnathan Mantey                     errorInEntry = true;
140301784826SJohnathan Mantey                 }
140401784826SJohnathan Mantey             }
14052c70f800SEd Tanous             else if (niciPentry != ipv4Data.cend())
140601784826SJohnathan Mantey             {
14072c70f800SEd Tanous                 addr = &(niciPentry->address);
140801784826SJohnathan Mantey             }
140901784826SJohnathan Mantey             else
141001784826SJohnathan Mantey             {
141101784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
141201784826SJohnathan Mantey                                           pathString + "/Address");
141301784826SJohnathan Mantey                 errorInEntry = true;
14144a0cb85cSEd Tanous             }
14154a0cb85cSEd Tanous 
1416537174c4SEd Tanous             if (subnetMask)
14174a0cb85cSEd Tanous             {
1418537174c4SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
14194a0cb85cSEd Tanous                 {
1420f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1421537174c4SEd Tanous                         asyncResp->res, *subnetMask,
14224a0cb85cSEd Tanous                         pathString + "/SubnetMask");
142301784826SJohnathan Mantey                     errorInEntry = true;
14244a0cb85cSEd Tanous                 }
14254a0cb85cSEd Tanous             }
14262c70f800SEd Tanous             else if (niciPentry != ipv4Data.cend())
14274a0cb85cSEd Tanous             {
14282c70f800SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask,
142901784826SJohnathan Mantey                                                 &prefixLength))
14304a0cb85cSEd Tanous                 {
143101784826SJohnathan Mantey                     messages::propertyValueFormatError(
14322c70f800SEd Tanous                         asyncResp->res, niciPentry->netmask,
143301784826SJohnathan Mantey                         pathString + "/SubnetMask");
143401784826SJohnathan Mantey                     errorInEntry = true;
14354a0cb85cSEd Tanous                 }
14364a0cb85cSEd Tanous             }
14371abe55efSEd Tanous             else
14381abe55efSEd Tanous             {
143901784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
144001784826SJohnathan Mantey                                           pathString + "/SubnetMask");
144101784826SJohnathan Mantey                 errorInEntry = true;
144201784826SJohnathan Mantey             }
144301784826SJohnathan Mantey 
144401784826SJohnathan Mantey             if (gateway)
144501784826SJohnathan Mantey             {
144601784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*gateway))
144701784826SJohnathan Mantey                 {
144801784826SJohnathan Mantey                     gw = &(*gateway);
144901784826SJohnathan Mantey                 }
145001784826SJohnathan Mantey                 else
145101784826SJohnathan Mantey                 {
1452bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1453bf648f77SEd Tanous                                                        pathString + "/Gateway");
145401784826SJohnathan Mantey                     errorInEntry = true;
145501784826SJohnathan Mantey                 }
145601784826SJohnathan Mantey             }
14572c70f800SEd Tanous             else if (niciPentry != ipv4Data.cend())
145801784826SJohnathan Mantey             {
14592c70f800SEd Tanous                 gw = &niciPentry->gateway;
146001784826SJohnathan Mantey             }
146101784826SJohnathan Mantey             else
14621abe55efSEd Tanous             {
1463a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
14644a0cb85cSEd Tanous                                           pathString + "/Gateway");
146501784826SJohnathan Mantey                 errorInEntry = true;
14664a0cb85cSEd Tanous             }
14674a0cb85cSEd Tanous 
146801784826SJohnathan Mantey             if (errorInEntry)
14691abe55efSEd Tanous             {
147001784826SJohnathan Mantey                 return;
14714a0cb85cSEd Tanous             }
14724a0cb85cSEd Tanous 
14732c70f800SEd Tanous             if (niciPentry != ipv4Data.cend())
14741abe55efSEd Tanous             {
1475bf648f77SEd Tanous                 deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength, *gw,
1476bf648f77SEd Tanous                                     *addr, asyncResp);
14772c70f800SEd Tanous                 niciPentry =
14782c70f800SEd Tanous                     getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
1479588c3f0dSKowalski, Kamil             }
148001784826SJohnathan Mantey             else
148101784826SJohnathan Mantey             {
1482cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1483cb13a392SEd Tanous                            asyncResp);
14844a0cb85cSEd Tanous             }
14854a0cb85cSEd Tanous             entryIdx++;
14864a0cb85cSEd Tanous         }
148701784826SJohnathan Mantey         else
148801784826SJohnathan Mantey         {
14892c70f800SEd Tanous             if (niciPentry == ipv4Data.cend())
149001784826SJohnathan Mantey             {
149101784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
149201784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
149301784826SJohnathan Mantey                 // in error, so bail out.
149401784826SJohnathan Mantey                 if (thisJson.is_null())
149501784826SJohnathan Mantey                 {
149601784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
149701784826SJohnathan Mantey                     return;
149801784826SJohnathan Mantey                 }
149901784826SJohnathan Mantey                 messages::propertyValueFormatError(
150071f52d96SEd Tanous                     asyncResp->res,
150171f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
150271f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
150371f52d96SEd Tanous                     pathString);
150401784826SJohnathan Mantey                 return;
150501784826SJohnathan Mantey             }
150601784826SJohnathan Mantey 
150701784826SJohnathan Mantey             if (thisJson.is_null())
150801784826SJohnathan Mantey             {
15092c70f800SEd Tanous                 deleteIPv4(ifaceId, niciPentry->id, asyncResp);
151001784826SJohnathan Mantey             }
15112c70f800SEd Tanous             if (niciPentry != ipv4Data.cend())
151201784826SJohnathan Mantey             {
15132c70f800SEd Tanous                 niciPentry =
15142c70f800SEd Tanous                     getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
151501784826SJohnathan Mantey             }
151601784826SJohnathan Mantey             entryIdx++;
151701784826SJohnathan Mantey         }
151801784826SJohnathan Mantey     }
15194a0cb85cSEd Tanous }
15204a0cb85cSEd Tanous 
1521f85837bfSRAJESWARAN THILLAIGOVINDAN void handleStaticNameServersPatch(
1522f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1523f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
15248d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1525f85837bfSRAJESWARAN THILLAIGOVINDAN {
1526f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
1527286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1528f85837bfSRAJESWARAN THILLAIGOVINDAN             if (ec)
1529f85837bfSRAJESWARAN THILLAIGOVINDAN             {
1530f85837bfSRAJESWARAN THILLAIGOVINDAN                 messages::internalError(asyncResp->res);
1531f85837bfSRAJESWARAN THILLAIGOVINDAN                 return;
1532f85837bfSRAJESWARAN THILLAIGOVINDAN             }
1533f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1534f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1535f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1536f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1537bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1538f85837bfSRAJESWARAN THILLAIGOVINDAN         std::variant<std::vector<std::string>>{updatedStaticNameServers});
1539f85837bfSRAJESWARAN THILLAIGOVINDAN }
1540f85837bfSRAJESWARAN THILLAIGOVINDAN 
1541e48c0fc5SRavi Teja void handleIPv6StaticAddressesPatch(
1542f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
154301784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data,
15448d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1545e48c0fc5SRavi Teja {
154601784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1547e48c0fc5SRavi Teja     {
154871f52d96SEd Tanous         messages::propertyValueTypeError(
154971f52d96SEd Tanous             asyncResp->res,
1550bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1551e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1552e48c0fc5SRavi Teja         return;
1553e48c0fc5SRavi Teja     }
1554271584abSEd Tanous     size_t entryIdx = 1;
15552c70f800SEd Tanous     boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry =
15562c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1557f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1558e48c0fc5SRavi Teja     {
1559e48c0fc5SRavi Teja         std::string pathString =
1560e48c0fc5SRavi Teja             "IPv6StaticAddresses/" + std::to_string(entryIdx);
1561e48c0fc5SRavi Teja 
156201784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1563e48c0fc5SRavi Teja         {
1564e48c0fc5SRavi Teja             std::optional<std::string> address;
1565e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1566f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1567bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1568bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1569e48c0fc5SRavi Teja             {
157001784826SJohnathan Mantey                 messages::propertyValueFormatError(
157171f52d96SEd Tanous                     asyncResp->res,
157271f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
157371f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
157471f52d96SEd Tanous                     pathString);
1575e48c0fc5SRavi Teja                 return;
1576e48c0fc5SRavi Teja             }
1577e48c0fc5SRavi Teja 
157801784826SJohnathan Mantey             const std::string* addr;
157901784826SJohnathan Mantey             uint8_t prefix;
158001784826SJohnathan Mantey 
158101784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
158201784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
158301784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
158401784826SJohnathan Mantey             // current request.
1585e48c0fc5SRavi Teja             if (address)
1586e48c0fc5SRavi Teja             {
158701784826SJohnathan Mantey                 addr = &(*address);
1588e48c0fc5SRavi Teja             }
15892c70f800SEd Tanous             else if (niciPentry != ipv6Data.end())
159001784826SJohnathan Mantey             {
15912c70f800SEd Tanous                 addr = &(niciPentry->address);
159201784826SJohnathan Mantey             }
159301784826SJohnathan Mantey             else
159401784826SJohnathan Mantey             {
159501784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
159601784826SJohnathan Mantey                                           pathString + "/Address");
159701784826SJohnathan Mantey                 return;
1598e48c0fc5SRavi Teja             }
1599e48c0fc5SRavi Teja 
1600e48c0fc5SRavi Teja             if (prefixLength)
1601e48c0fc5SRavi Teja             {
160201784826SJohnathan Mantey                 prefix = *prefixLength;
160301784826SJohnathan Mantey             }
16042c70f800SEd Tanous             else if (niciPentry != ipv6Data.end())
1605e48c0fc5SRavi Teja             {
16062c70f800SEd Tanous                 prefix = niciPentry->prefixLength;
1607e48c0fc5SRavi Teja             }
1608e48c0fc5SRavi Teja             else
1609e48c0fc5SRavi Teja             {
1610e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1611e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
161201784826SJohnathan Mantey                 return;
1613e48c0fc5SRavi Teja             }
1614e48c0fc5SRavi Teja 
16152c70f800SEd Tanous             if (niciPentry != ipv6Data.end())
1616e48c0fc5SRavi Teja             {
16172c70f800SEd Tanous                 deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr,
1618e48c0fc5SRavi Teja                                     asyncResp);
16192c70f800SEd Tanous                 niciPentry =
16202c70f800SEd Tanous                     getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
162101784826SJohnathan Mantey             }
162201784826SJohnathan Mantey             else
162301784826SJohnathan Mantey             {
162401784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1625e48c0fc5SRavi Teja             }
1626e48c0fc5SRavi Teja             entryIdx++;
1627e48c0fc5SRavi Teja         }
162801784826SJohnathan Mantey         else
162901784826SJohnathan Mantey         {
16302c70f800SEd Tanous             if (niciPentry == ipv6Data.end())
163101784826SJohnathan Mantey             {
163201784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
163301784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
163401784826SJohnathan Mantey                 // in error, so bail out.
163501784826SJohnathan Mantey                 if (thisJson.is_null())
163601784826SJohnathan Mantey                 {
163701784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
163801784826SJohnathan Mantey                     return;
163901784826SJohnathan Mantey                 }
164001784826SJohnathan Mantey                 messages::propertyValueFormatError(
164171f52d96SEd Tanous                     asyncResp->res,
164271f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
164371f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
164471f52d96SEd Tanous                     pathString);
164501784826SJohnathan Mantey                 return;
164601784826SJohnathan Mantey             }
164701784826SJohnathan Mantey 
164801784826SJohnathan Mantey             if (thisJson.is_null())
164901784826SJohnathan Mantey             {
16502c70f800SEd Tanous                 deleteIPv6(ifaceId, niciPentry->id, asyncResp);
165101784826SJohnathan Mantey             }
16522c70f800SEd Tanous             if (niciPentry != ipv6Data.cend())
165301784826SJohnathan Mantey             {
16542c70f800SEd Tanous                 niciPentry =
16552c70f800SEd Tanous                     getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
165601784826SJohnathan Mantey             }
165701784826SJohnathan Mantey             entryIdx++;
165801784826SJohnathan Mantey         }
165901784826SJohnathan Mantey     }
1660e48c0fc5SRavi Teja }
1661e48c0fc5SRavi Teja 
16620f74e643SEd Tanous void parseInterfaceData(
16638d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16648d1b46d7Szhanghch05     const std::string& ifaceId, const EthernetInterfaceData& ethData,
1665e48c0fc5SRavi Teja     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
166601784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data)
16674a0cb85cSEd Tanous {
1668eeedda23SJohnathan Mantey     constexpr const std::array<const char*, 1> inventoryForEthernet = {
1669eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1670eeedda23SJohnathan Mantey 
16712c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
167281ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
16732c70f800SEd Tanous     jsonResponse["@odata.id"] =
167481ce609eSEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
16752c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1676eeedda23SJohnathan Mantey 
1677eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1678eeedda23SJohnathan Mantey 
1679eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1680eeedda23SJohnathan Mantey         [health](const boost::system::error_code ec,
1681eeedda23SJohnathan Mantey                  std::vector<std::string>& resp) {
1682eeedda23SJohnathan Mantey             if (ec)
1683029573d4SEd Tanous             {
1684eeedda23SJohnathan Mantey                 return;
1685eeedda23SJohnathan Mantey             }
1686eeedda23SJohnathan Mantey 
1687eeedda23SJohnathan Mantey             health->inventory = std::move(resp);
1688eeedda23SJohnathan Mantey         },
1689eeedda23SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper",
1690eeedda23SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
1691bf648f77SEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1692bf648f77SEd Tanous         inventoryForEthernet);
1693eeedda23SJohnathan Mantey 
1694eeedda23SJohnathan Mantey     health->populate();
1695eeedda23SJohnathan Mantey 
1696eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1697eeedda23SJohnathan Mantey     {
16982c70f800SEd Tanous         jsonResponse["LinkStatus"] = "LinkUp";
16992c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1700029573d4SEd Tanous     }
1701029573d4SEd Tanous     else
1702029573d4SEd Tanous     {
17032c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
17042c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1705029573d4SEd Tanous     }
1706aa05fb27SJohnathan Mantey 
17072c70f800SEd Tanous     jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
17082c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
17092c70f800SEd Tanous     jsonResponse["MACAddress"] = ethData.mac_address;
17102c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
17111f8c7b5dSJohnathan Mantey         translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
17122c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
17132c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
17142c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
17151f8c7b5dSJohnathan Mantey 
17162c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
17171f8c7b5dSJohnathan Mantey         translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
17181f8c7b5dSJohnathan Mantey                                                                : "Disabled";
17192c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
17202c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
17212c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
17222a133282Smanojkiraneda 
17234a0cb85cSEd Tanous     if (!ethData.hostname.empty())
17244a0cb85cSEd Tanous     {
17252c70f800SEd Tanous         jsonResponse["HostName"] = ethData.hostname;
1726ab6554f1SJoshi-Mansi 
1727ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1728ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1729ab6554f1SJoshi-Mansi         // FQDN
1730f23b7296SEd Tanous         std::string fqdn = ethData.hostname;
1731d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1732d24bfc7aSJennifer Lee         {
17332c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1734d24bfc7aSJennifer Lee         }
17352c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
17364a0cb85cSEd Tanous     }
17374a0cb85cSEd Tanous 
17382c70f800SEd Tanous     jsonResponse["VLANs"] = {
1739bf648f77SEd Tanous         {"@odata.id",
1740bf648f77SEd Tanous          "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1741fda13ad2SSunitha Harish 
17422c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
17432c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
17444a0cb85cSEd Tanous 
17452c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
17462c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
17472c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
17482c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
17492c70f800SEd Tanous     for (auto& ipv4Config : ipv4Data)
17504a0cb85cSEd Tanous     {
1751fa5053a6SGunnar Mills 
17522c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1753fa5053a6SGunnar Mills         if (gatewayStr.empty())
1754fa5053a6SGunnar Mills         {
1755fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1756fa5053a6SGunnar Mills         }
1757fa5053a6SGunnar Mills 
17582c70f800SEd Tanous         ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
17592c70f800SEd Tanous                              {"SubnetMask", ipv4Config.netmask},
17602c70f800SEd Tanous                              {"Address", ipv4Config.address},
1761fa5053a6SGunnar Mills                              {"Gateway", gatewayStr}});
17622c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1763d1d50814SRavi Teja         {
17642c70f800SEd Tanous             ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
17652c70f800SEd Tanous                                        {"SubnetMask", ipv4Config.netmask},
17662c70f800SEd Tanous                                        {"Address", ipv4Config.address},
1767d1d50814SRavi Teja                                        {"Gateway", gatewayStr}});
1768d1d50814SRavi Teja         }
176901784826SJohnathan Mantey     }
1770d1d50814SRavi Teja 
17717ea79e5eSRavi Teja     std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
17727ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
17737ea79e5eSRavi Teja     {
17747ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
17757ea79e5eSRavi Teja     }
17767ea79e5eSRavi Teja 
17777ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1778e48c0fc5SRavi Teja 
17792c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
17802c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
17812c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
17822c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
17837f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
17842c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
17857f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
17862c70f800SEd Tanous     for (auto& ipv6Config : ipv6Data)
1787e48c0fc5SRavi Teja     {
17882c70f800SEd Tanous         ipv6Array.push_back({{"Address", ipv6Config.address},
17892c70f800SEd Tanous                              {"PrefixLength", ipv6Config.prefixLength},
17902c70f800SEd Tanous                              {"AddressOrigin", ipv6Config.origin},
17915fd16e4bSJohnathan Mantey                              {"AddressState", nullptr}});
17922c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1793e48c0fc5SRavi Teja         {
17942c70f800SEd Tanous             ipv6StaticArray.push_back(
17952c70f800SEd Tanous                 {{"Address", ipv6Config.address},
17962c70f800SEd Tanous                  {"PrefixLength", ipv6Config.prefixLength},
17972c70f800SEd Tanous                  {"AddressOrigin", ipv6Config.origin},
17985fd16e4bSJohnathan Mantey                  {"AddressState", nullptr}});
179901784826SJohnathan Mantey         }
1800e48c0fc5SRavi Teja     }
1801588c3f0dSKowalski, Kamil }
1802588c3f0dSKowalski, Kamil 
1803bf648f77SEd Tanous void parseInterfaceData(nlohmann::json& jsonResponse,
1804bf648f77SEd Tanous                         const std::string& parentIfaceId,
1805bf648f77SEd Tanous                         const std::string& ifaceId,
1806bf648f77SEd Tanous                         const EthernetInterfaceData& ethData)
18071abe55efSEd Tanous {
1808bf648f77SEd Tanous     // Fill out obvious data...
1809bf648f77SEd Tanous     jsonResponse["Id"] = ifaceId;
1810bf648f77SEd Tanous     jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1811bf648f77SEd Tanous                                 parentIfaceId + "/VLANs/" + ifaceId;
1812bf648f77SEd Tanous 
1813bf648f77SEd Tanous     jsonResponse["VLANEnable"] = true;
1814bf648f77SEd Tanous     if (!ethData.vlan_id.empty())
1815bf648f77SEd Tanous     {
1816bf648f77SEd Tanous         jsonResponse["VLANId"] = ethData.vlan_id.back();
1817bf648f77SEd Tanous     }
1818bf648f77SEd Tanous }
1819bf648f77SEd Tanous 
1820bf648f77SEd Tanous bool verifyNames(const std::string& parent, const std::string& iface)
1821bf648f77SEd Tanous {
1822bf648f77SEd Tanous     if (!boost::starts_with(iface, parent + "_"))
1823bf648f77SEd Tanous     {
1824bf648f77SEd Tanous         return false;
1825bf648f77SEd Tanous     }
1826bf648f77SEd Tanous     return true;
1827bf648f77SEd Tanous }
1828bf648f77SEd Tanous 
1829bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1830bf648f77SEd Tanous {
1831bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1832*432a890cSEd Tanous         .privileges({{"Login"}})
1833bf648f77SEd Tanous         .methods(
1834bf648f77SEd Tanous             boost::beast::http::verb::
1835bf648f77SEd Tanous                 get)([](const crow::Request&,
1836bf648f77SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1837bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1838bf648f77SEd Tanous                 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1839bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1840bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1841bf648f77SEd Tanous             asyncResp->res.jsonValue["Name"] =
1842bf648f77SEd Tanous                 "Ethernet Network Interface Collection";
1843bf648f77SEd Tanous             asyncResp->res.jsonValue["Description"] =
1844bf648f77SEd Tanous                 "Collection of EthernetInterfaces for this Manager";
1845bf648f77SEd Tanous 
1846bf648f77SEd Tanous             // Get eth interface list, and call the below callback for JSON
1847bf648f77SEd Tanous             // preparation
1848bf648f77SEd Tanous             getEthernetIfaceList([asyncResp](const bool& success,
1849bf648f77SEd Tanous                                              const boost::container::flat_set<
1850bf648f77SEd Tanous                                                  std::string>& ifaceList) {
1851bf648f77SEd Tanous                 if (!success)
18521abe55efSEd Tanous                 {
1853f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
18549391bb9cSRapkiewicz, Pawel                     return;
18559391bb9cSRapkiewicz, Pawel                 }
18569391bb9cSRapkiewicz, Pawel 
1857bf648f77SEd Tanous                 nlohmann::json& ifaceArray =
1858bf648f77SEd Tanous                     asyncResp->res.jsonValue["Members"];
1859bf648f77SEd Tanous                 ifaceArray = nlohmann::json::array();
1860bf648f77SEd Tanous                 std::string tag = "_";
1861bf648f77SEd Tanous                 for (const std::string& ifaceItem : ifaceList)
1862bf648f77SEd Tanous                 {
1863bf648f77SEd Tanous                     std::size_t found = ifaceItem.find(tag);
1864bf648f77SEd Tanous                     if (found == std::string::npos)
1865bf648f77SEd Tanous                     {
1866bf648f77SEd Tanous                         ifaceArray.push_back(
1867bf648f77SEd Tanous                             {{"@odata.id",
1868bf648f77SEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1869bf648f77SEd Tanous                                   ifaceItem}});
1870bf648f77SEd Tanous                     }
1871bf648f77SEd Tanous                 }
1872bf648f77SEd Tanous 
1873bf648f77SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
1874bf648f77SEd Tanous                     ifaceArray.size();
1875bf648f77SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
1876bf648f77SEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
1877bf648f77SEd Tanous             });
1878bf648f77SEd Tanous         });
1879bf648f77SEd Tanous 
1880bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1881*432a890cSEd Tanous         .privileges({{"Login"}})
1882bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
1883bf648f77SEd Tanous             [](const crow::Request&,
1884bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1885bf648f77SEd Tanous                const std::string& ifaceId) {
18864a0cb85cSEd Tanous                 getEthernetIfaceData(
1887bf648f77SEd Tanous                     ifaceId,
1888bf648f77SEd Tanous                     [asyncResp,
1889bf648f77SEd Tanous                      ifaceId](const bool& success,
1890bf648f77SEd Tanous                               const EthernetInterfaceData& ethData,
1891bf648f77SEd Tanous                               const boost::container::flat_set<IPv4AddressData>&
1892bf648f77SEd Tanous                                   ipv4Data,
1893bf648f77SEd Tanous                               const boost::container::flat_set<IPv6AddressData>&
1894bf648f77SEd Tanous                                   ipv6Data) {
18954a0cb85cSEd Tanous                         if (!success)
18961abe55efSEd Tanous                         {
1897bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
1898bf648f77SEd Tanous                             // existing object, and other errors
1899bf648f77SEd Tanous                             messages::resourceNotFound(
1900bf648f77SEd Tanous                                 asyncResp->res, "EthernetInterface", ifaceId);
19014a0cb85cSEd Tanous                             return;
19029391bb9cSRapkiewicz, Pawel                         }
19034c9afe43SEd Tanous 
19040f74e643SEd Tanous                         asyncResp->res.jsonValue["@odata.type"] =
1905fda13ad2SSunitha Harish                             "#EthernetInterface.v1_4_1.EthernetInterface";
1906bf648f77SEd Tanous                         asyncResp->res.jsonValue["Name"] =
1907bf648f77SEd Tanous                             "Manager Ethernet Interface";
19080f74e643SEd Tanous                         asyncResp->res.jsonValue["Description"] =
19090f74e643SEd Tanous                             "Management Network Interface";
19100f74e643SEd Tanous 
1911bf648f77SEd Tanous                         parseInterfaceData(asyncResp, ifaceId, ethData,
1912bf648f77SEd Tanous                                            ipv4Data, ipv6Data);
19139391bb9cSRapkiewicz, Pawel                     });
1914bf648f77SEd Tanous             });
19159391bb9cSRapkiewicz, Pawel 
1916bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1917*432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
1918bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
1919bf648f77SEd Tanous             [](const crow::Request& req,
1920bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1921bf648f77SEd Tanous                const std::string& ifaceId) {
1922bc0bd6e0SEd Tanous                 std::optional<std::string> hostname;
1923ab6554f1SJoshi-Mansi                 std::optional<std::string> fqdn;
1924d577665bSRatan Gupta                 std::optional<std::string> macAddress;
19259a6fc6feSRavi Teja                 std::optional<std::string> ipv6DefaultGateway;
1926d1d50814SRavi Teja                 std::optional<nlohmann::json> ipv4StaticAddresses;
1927e48c0fc5SRavi Teja                 std::optional<nlohmann::json> ipv6StaticAddresses;
1928f85837bfSRAJESWARAN THILLAIGOVINDAN                 std::optional<std::vector<std::string>> staticNameServers;
1929da131a9aSJennifer Lee                 std::optional<nlohmann::json> dhcpv4;
19301f8c7b5dSJohnathan Mantey                 std::optional<nlohmann::json> dhcpv6;
1931eeedda23SJohnathan Mantey                 std::optional<bool> interfaceEnabled;
19321f8c7b5dSJohnathan Mantey                 DHCPParameters v4dhcpParms;
19331f8c7b5dSJohnathan Mantey                 DHCPParameters v6dhcpParms;
19340627a2c7SEd Tanous 
19351f8c7b5dSJohnathan Mantey                 if (!json_util::readJson(
19368d1b46d7Szhanghch05                         req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1937bf648f77SEd Tanous                         "IPv4StaticAddresses", ipv4StaticAddresses,
1938bf648f77SEd Tanous                         "MACAddress", macAddress, "StaticNameServers",
1939bf648f77SEd Tanous                         staticNameServers, "IPv6DefaultGateway",
1940bf648f77SEd Tanous                         ipv6DefaultGateway, "IPv6StaticAddresses",
1941ab6554f1SJoshi-Mansi                         ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1942ab6554f1SJoshi-Mansi                         "InterfaceEnabled", interfaceEnabled))
19431abe55efSEd Tanous                 {
1944588c3f0dSKowalski, Kamil                     return;
1945588c3f0dSKowalski, Kamil                 }
1946da131a9aSJennifer Lee                 if (dhcpv4)
1947da131a9aSJennifer Lee                 {
1948bf648f77SEd Tanous                     if (!json_util::readJson(
1949bf648f77SEd Tanous                             *dhcpv4, asyncResp->res, "DHCPEnabled",
19501f8c7b5dSJohnathan Mantey                             v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
19511f8c7b5dSJohnathan Mantey                             v4dhcpParms.useDNSServers, "UseNTPServers",
19521f8c7b5dSJohnathan Mantey                             v4dhcpParms.useNTPServers, "UseDomainName",
19531f8c7b5dSJohnathan Mantey                             v4dhcpParms.useUseDomainName))
19541f8c7b5dSJohnathan Mantey                     {
19551f8c7b5dSJohnathan Mantey                         return;
19561f8c7b5dSJohnathan Mantey                     }
19571f8c7b5dSJohnathan Mantey                 }
19581f8c7b5dSJohnathan Mantey 
19591f8c7b5dSJohnathan Mantey                 if (dhcpv6)
19601f8c7b5dSJohnathan Mantey                 {
1961bf648f77SEd Tanous                     if (!json_util::readJson(
1962bf648f77SEd Tanous                             *dhcpv6, asyncResp->res, "OperatingMode",
1963bf648f77SEd Tanous                             v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers",
1964bf648f77SEd Tanous                             v6dhcpParms.useDNSServers, "UseNTPServers",
1965bf648f77SEd Tanous                             v6dhcpParms.useNTPServers, "UseDomainName",
19661f8c7b5dSJohnathan Mantey                             v6dhcpParms.useUseDomainName))
19671f8c7b5dSJohnathan Mantey                     {
19681f8c7b5dSJohnathan Mantey                         return;
19691f8c7b5dSJohnathan Mantey                     }
1970da131a9aSJennifer Lee                 }
1971da131a9aSJennifer Lee 
1972bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
1973bf648f77SEd Tanous                 // for JSON preparation
19744a0cb85cSEd Tanous                 getEthernetIfaceData(
19752c70f800SEd Tanous                     ifaceId,
1976bf648f77SEd Tanous                     [asyncResp, ifaceId, hostname = std::move(hostname),
1977ab6554f1SJoshi-Mansi                      fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1978d1d50814SRavi Teja                      ipv4StaticAddresses = std::move(ipv4StaticAddresses),
19799a6fc6feSRavi Teja                      ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1980e48c0fc5SRavi Teja                      ipv6StaticAddresses = std::move(ipv6StaticAddresses),
19811f8c7b5dSJohnathan Mantey                      staticNameServers = std::move(staticNameServers),
19821f8c7b5dSJohnathan Mantey                      dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
19831f8c7b5dSJohnathan Mantey                      v4dhcpParms = std::move(v4dhcpParms),
1984f23b7296SEd Tanous                      v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1985bf648f77SEd Tanous                         const bool& success,
1986bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
1987bf648f77SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&
1988bf648f77SEd Tanous                             ipv4Data,
1989bf648f77SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&
1990bf648f77SEd Tanous                             ipv6Data) {
19911abe55efSEd Tanous                         if (!success)
19921abe55efSEd Tanous                         {
1993588c3f0dSKowalski, Kamil                             // ... otherwise return error
1994bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
1995bf648f77SEd Tanous                             // existing object, and other errors
1996bf648f77SEd Tanous                             messages::resourceNotFound(
1997bf648f77SEd Tanous                                 asyncResp->res, "Ethernet Interface", ifaceId);
1998588c3f0dSKowalski, Kamil                             return;
1999588c3f0dSKowalski, Kamil                         }
2000588c3f0dSKowalski, Kamil 
20011f8c7b5dSJohnathan Mantey                         if (dhcpv4 || dhcpv6)
20021f8c7b5dSJohnathan Mantey                         {
2003bf648f77SEd Tanous                             handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2004bf648f77SEd Tanous                                             v6dhcpParms, asyncResp);
20051f8c7b5dSJohnathan Mantey                         }
20061f8c7b5dSJohnathan Mantey 
20070627a2c7SEd Tanous                         if (hostname)
20081abe55efSEd Tanous                         {
20090627a2c7SEd Tanous                             handleHostnamePatch(*hostname, asyncResp);
20101abe55efSEd Tanous                         }
20110627a2c7SEd Tanous 
2012ab6554f1SJoshi-Mansi                         if (fqdn)
2013ab6554f1SJoshi-Mansi                         {
20142c70f800SEd Tanous                             handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2015ab6554f1SJoshi-Mansi                         }
2016ab6554f1SJoshi-Mansi 
2017d577665bSRatan Gupta                         if (macAddress)
2018d577665bSRatan Gupta                         {
2019bf648f77SEd Tanous                             handleMACAddressPatch(ifaceId, *macAddress,
2020bf648f77SEd Tanous                                                   asyncResp);
2021d577665bSRatan Gupta                         }
2022d577665bSRatan Gupta 
2023d1d50814SRavi Teja                         if (ipv4StaticAddresses)
2024d1d50814SRavi Teja                         {
2025bf648f77SEd Tanous                             // TODO(ed) for some reason the capture of
2026bf648f77SEd Tanous                             // ipv4Addresses above is returning a const value,
2027bf648f77SEd Tanous                             // not a non-const value. This doesn't really work
2028bf648f77SEd Tanous                             // for us, as we need to be able to efficiently move
2029bf648f77SEd Tanous                             // out the intermedia nlohmann::json objects. This
2030bf648f77SEd Tanous                             // makes a copy of the structure, and operates on
2031bf648f77SEd Tanous                             // that, but could be done more efficiently
2032f23b7296SEd Tanous                             nlohmann::json ipv4Static = *ipv4StaticAddresses;
20332c70f800SEd Tanous                             handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2034d1d50814SRavi Teja                                                   asyncResp);
20351abe55efSEd Tanous                         }
20360627a2c7SEd Tanous 
2037f85837bfSRAJESWARAN THILLAIGOVINDAN                         if (staticNameServers)
2038f85837bfSRAJESWARAN THILLAIGOVINDAN                         {
2039bf648f77SEd Tanous                             handleStaticNameServersPatch(
2040bf648f77SEd Tanous                                 ifaceId, *staticNameServers, asyncResp);
2041f85837bfSRAJESWARAN THILLAIGOVINDAN                         }
20429a6fc6feSRavi Teja 
20439a6fc6feSRavi Teja                         if (ipv6DefaultGateway)
20449a6fc6feSRavi Teja                         {
20459a6fc6feSRavi Teja                             messages::propertyNotWritable(asyncResp->res,
20469a6fc6feSRavi Teja                                                           "IPv6DefaultGateway");
20479a6fc6feSRavi Teja                         }
2048e48c0fc5SRavi Teja 
2049e48c0fc5SRavi Teja                         if (ipv6StaticAddresses)
2050e48c0fc5SRavi Teja                         {
2051f23b7296SEd Tanous                             nlohmann::json ipv6Static = *ipv6StaticAddresses;
20522c70f800SEd Tanous                             handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
205301784826SJohnathan Mantey                                                            ipv6Data, asyncResp);
2054e48c0fc5SRavi Teja                         }
2055eeedda23SJohnathan Mantey 
2056eeedda23SJohnathan Mantey                         if (interfaceEnabled)
2057eeedda23SJohnathan Mantey                         {
2058eeedda23SJohnathan Mantey                             setEthernetInterfaceBoolProperty(
2059bf648f77SEd Tanous                                 ifaceId, "NICEnabled", *interfaceEnabled,
2060bf648f77SEd Tanous                                 asyncResp);
2061eeedda23SJohnathan Mantey                         }
2062588c3f0dSKowalski, Kamil                     });
2063bf648f77SEd Tanous             });
20649391bb9cSRapkiewicz, Pawel 
2065bf648f77SEd Tanous     BMCWEB_ROUTE(
2066bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2067*432a890cSEd Tanous         .privileges({{"Login"}})
2068bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
2069bf648f77SEd Tanous             [](const crow::Request& /* req */,
2070bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2071bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
20728d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
20730f74e643SEd Tanous                     "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
20748d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
2075e439f0f8SKowalski, Kamil 
20762c70f800SEd Tanous                 if (!verifyNames(parentIfaceId, ifaceId))
20771abe55efSEd Tanous                 {
2078a434f2bdSEd Tanous                     return;
2079a434f2bdSEd Tanous                 }
2080a434f2bdSEd Tanous 
2081bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2082bf648f77SEd Tanous                 // for JSON preparation
20834a0cb85cSEd Tanous                 getEthernetIfaceData(
2084bf648f77SEd Tanous                     ifaceId,
2085bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId](
2086bf648f77SEd Tanous                         const bool& success,
2087bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2088cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2089cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
2090fda13ad2SSunitha Harish                         if (success && ethData.vlan_id.size() != 0)
20911abe55efSEd Tanous                         {
2092bf648f77SEd Tanous                             parseInterfaceData(asyncResp->res.jsonValue,
2093bf648f77SEd Tanous                                                parentIfaceId, ifaceId, ethData);
20941abe55efSEd Tanous                         }
20951abe55efSEd Tanous                         else
20961abe55efSEd Tanous                         {
2097e439f0f8SKowalski, Kamil                             // ... otherwise return error
2098bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2099bf648f77SEd Tanous                             // existing object, and other errors
2100bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2101bf648f77SEd Tanous                                                        "VLAN Network Interface",
2102bf648f77SEd Tanous                                                        ifaceId);
2103e439f0f8SKowalski, Kamil                         }
2104e439f0f8SKowalski, Kamil                     });
2105bf648f77SEd Tanous             });
2106e439f0f8SKowalski, Kamil 
2107bf648f77SEd Tanous     BMCWEB_ROUTE(
2108bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2109*432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2110bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
2111bf648f77SEd Tanous             [](const crow::Request& req,
2112bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2113bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
2114fda13ad2SSunitha Harish                 if (!verifyNames(parentIfaceId, ifaceId))
21151abe55efSEd Tanous                 {
2116bf648f77SEd Tanous                     messages::resourceNotFound(
2117bf648f77SEd Tanous                         asyncResp->res, "VLAN Network Interface", ifaceId);
2118927a505aSKowalski, Kamil                     return;
2119927a505aSKowalski, Kamil                 }
2120927a505aSKowalski, Kamil 
21210627a2c7SEd Tanous                 bool vlanEnable = false;
212238268fa8SAndrew Geissler                 uint32_t vlanId = 0;
21230627a2c7SEd Tanous 
2124bf648f77SEd Tanous                 if (!json_util::readJson(req, asyncResp->res, "VLANEnable",
2125bf648f77SEd Tanous                                          vlanEnable, "VLANId", vlanId))
21261abe55efSEd Tanous                 {
2127927a505aSKowalski, Kamil                     return;
2128927a505aSKowalski, Kamil                 }
2129927a505aSKowalski, Kamil 
2130bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2131bf648f77SEd Tanous                 // for JSON preparation
2132e48c0fc5SRavi Teja                 getEthernetIfaceData(
2133bf648f77SEd Tanous                     ifaceId,
2134bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId](
2135bf648f77SEd Tanous                         const bool& success,
2136bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2137cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2138cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
213908244d02SSunitha Harish                         if (success && !ethData.vlan_id.empty())
214008244d02SSunitha Harish                         {
214108244d02SSunitha Harish                             auto callback =
2142bf648f77SEd Tanous                                 [asyncResp](
2143bf648f77SEd Tanous                                     const boost::system::error_code ec) {
214408244d02SSunitha Harish                                     if (ec)
214508244d02SSunitha Harish                                     {
214608244d02SSunitha Harish                                         messages::internalError(asyncResp->res);
214708244d02SSunitha Harish                                     }
214808244d02SSunitha Harish                                 };
214908244d02SSunitha Harish 
215008244d02SSunitha Harish                             if (vlanEnable == true)
215108244d02SSunitha Harish                             {
215208244d02SSunitha Harish                                 crow::connections::systemBus->async_method_call(
2153bf648f77SEd Tanous                                     std::move(callback),
2154bf648f77SEd Tanous                                     "xyz.openbmc_project.Network",
215508244d02SSunitha Harish                                     "/xyz/openbmc_project/network/" + ifaceId,
215608244d02SSunitha Harish                                     "org.freedesktop.DBus.Properties", "Set",
215708244d02SSunitha Harish                                     "xyz.openbmc_project.Network.VLAN", "Id",
215808244d02SSunitha Harish                                     std::variant<uint32_t>(vlanId));
215908244d02SSunitha Harish                             }
216008244d02SSunitha Harish                             else
216108244d02SSunitha Harish                             {
2162bf648f77SEd Tanous                                 BMCWEB_LOG_DEBUG
2163bf648f77SEd Tanous                                     << "vlanEnable is false. Deleting the "
2164e48c0fc5SRavi Teja                                        "vlan interface";
216508244d02SSunitha Harish                                 crow::connections::systemBus->async_method_call(
2166bf648f77SEd Tanous                                     std::move(callback),
2167bf648f77SEd Tanous                                     "xyz.openbmc_project.Network",
2168bf648f77SEd Tanous                                     std::string(
2169bf648f77SEd Tanous                                         "/xyz/openbmc_project/network/") +
2170e48c0fc5SRavi Teja                                         ifaceId,
2171bf648f77SEd Tanous                                     "xyz.openbmc_project.Object.Delete",
2172bf648f77SEd Tanous                                     "Delete");
217308244d02SSunitha Harish                             }
217408244d02SSunitha Harish                         }
217508244d02SSunitha Harish                         else
21761abe55efSEd Tanous                         {
2177bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2178bf648f77SEd Tanous                             // existing object, and other errors
2179bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2180bf648f77SEd Tanous                                                        "VLAN Network Interface",
2181bf648f77SEd Tanous                                                        ifaceId);
2182bf648f77SEd Tanous                             return;
2183bf648f77SEd Tanous                         }
2184bf648f77SEd Tanous                     });
2185bf648f77SEd Tanous             });
2186bf648f77SEd Tanous 
2187bf648f77SEd Tanous     BMCWEB_ROUTE(
2188bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2189*432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2190bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
2191bf648f77SEd Tanous             [](const crow::Request& /* req */,
2192bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2193bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
2194bf648f77SEd Tanous                 if (!verifyNames(parentIfaceId, ifaceId))
2195bf648f77SEd Tanous                 {
2196e48c0fc5SRavi Teja                     messages::resourceNotFound(
2197e48c0fc5SRavi Teja                         asyncResp->res, "VLAN Network Interface", ifaceId);
2198927a505aSKowalski, Kamil                     return;
2199927a505aSKowalski, Kamil                 }
2200e439f0f8SKowalski, Kamil 
2201bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2202bf648f77SEd Tanous                 // for JSON preparation
2203f12894f8SJason M. Bills                 getEthernetIfaceData(
2204bf648f77SEd Tanous                     ifaceId,
2205bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId](
2206bf648f77SEd Tanous                         const bool& success,
2207bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2208cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2209cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
2210fda13ad2SSunitha Harish                         if (success && !ethData.vlan_id.empty())
22111abe55efSEd Tanous                         {
2212f12894f8SJason M. Bills                             auto callback =
2213bf648f77SEd Tanous                                 [asyncResp](
2214bf648f77SEd Tanous                                     const boost::system::error_code ec) {
22151abe55efSEd Tanous                                     if (ec)
22161abe55efSEd Tanous                                     {
2217f12894f8SJason M. Bills                                         messages::internalError(asyncResp->res);
2218927a505aSKowalski, Kamil                                     }
22194a0cb85cSEd Tanous                                 };
22204a0cb85cSEd Tanous                             crow::connections::systemBus->async_method_call(
2221bf648f77SEd Tanous                                 std::move(callback),
2222bf648f77SEd Tanous                                 "xyz.openbmc_project.Network",
2223bf648f77SEd Tanous                                 std::string("/xyz/openbmc_project/network/") +
2224bf648f77SEd Tanous                                     ifaceId,
22254a0cb85cSEd Tanous                                 "xyz.openbmc_project.Object.Delete", "Delete");
22261abe55efSEd Tanous                         }
22271abe55efSEd Tanous                         else
22281abe55efSEd Tanous                         {
2229927a505aSKowalski, Kamil                             // ... otherwise return error
2230bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2231bf648f77SEd Tanous                             // existing object, and other errors
2232bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2233bf648f77SEd Tanous                                                        "VLAN Network Interface",
2234bf648f77SEd Tanous                                                        ifaceId);
2235927a505aSKowalski, Kamil                         }
2236927a505aSKowalski, Kamil                     });
2237bf648f77SEd Tanous             });
2238e439f0f8SKowalski, Kamil 
2239bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2240bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2241*432a890cSEd Tanous         .privileges({{"Login"}})
2242bf648f77SEd Tanous         .methods(
2243bf648f77SEd Tanous             boost::beast::http::verb::
2244bf648f77SEd Tanous                 get)([](const crow::Request& /* req */,
2245bf648f77SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2246bf648f77SEd Tanous                         const std::string& rootInterfaceName) {
22474a0cb85cSEd Tanous             // Get eth interface list, and call the below callback for JSON
22481abe55efSEd Tanous             // preparation
2249bf648f77SEd Tanous             getEthernetIfaceList([asyncResp, rootInterfaceName](
22501abe55efSEd Tanous                                      const bool& success,
2251bf648f77SEd Tanous                                      const boost::container::flat_set<
2252bf648f77SEd Tanous                                          std::string>& ifaceList) {
22534a0cb85cSEd Tanous                 if (!success)
22541abe55efSEd Tanous                 {
2255f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
22564a0cb85cSEd Tanous                     return;
22571abe55efSEd Tanous                 }
22584c9afe43SEd Tanous 
225981ce609eSEd Tanous                 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
22604c9afe43SEd Tanous                 {
22614c9afe43SEd Tanous                     messages::resourceNotFound(asyncResp->res,
22624c9afe43SEd Tanous                                                "VLanNetworkInterfaceCollection",
22634c9afe43SEd Tanous                                                rootInterfaceName);
22644c9afe43SEd Tanous                     return;
22654c9afe43SEd Tanous                 }
22664c9afe43SEd Tanous 
22670f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
22680f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
22690f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
22700f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] =
22710f74e643SEd Tanous                     "VLAN Network Interface Collection";
22724a0cb85cSEd Tanous 
22732c70f800SEd Tanous                 nlohmann::json ifaceArray = nlohmann::json::array();
22744a0cb85cSEd Tanous 
227581ce609eSEd Tanous                 for (const std::string& ifaceItem : ifaceList)
22761abe55efSEd Tanous                 {
22772c70f800SEd Tanous                     if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
22784a0cb85cSEd Tanous                     {
2279f23b7296SEd Tanous                         std::string path =
2280f23b7296SEd Tanous                             "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2281f23b7296SEd Tanous                         path += rootInterfaceName;
2282f23b7296SEd Tanous                         path += "/VLANs/";
2283f23b7296SEd Tanous                         path += ifaceItem;
2284f23b7296SEd Tanous                         ifaceArray.push_back({{"@odata.id", std::move(path)}});
2285e439f0f8SKowalski, Kamil                     }
2286e439f0f8SKowalski, Kamil                 }
2287e439f0f8SKowalski, Kamil 
22884a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
22892c70f800SEd Tanous                     ifaceArray.size();
22902c70f800SEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
22914a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
22924a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
22934a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
2294e439f0f8SKowalski, Kamil             });
2295bf648f77SEd Tanous         });
2296e439f0f8SKowalski, Kamil 
2297bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2298bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2299*432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2300bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
2301bf648f77SEd Tanous             [](const crow::Request& req,
2302bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2303bf648f77SEd Tanous                const std::string& rootInterfaceName) {
2304fda13ad2SSunitha Harish                 bool vlanEnable = false;
23050627a2c7SEd Tanous                 uint32_t vlanId = 0;
23068d1b46d7Szhanghch05                 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId,
23078d1b46d7Szhanghch05                                          "VLANEnable", vlanEnable))
23081abe55efSEd Tanous                 {
23094a0cb85cSEd Tanous                     return;
2310e439f0f8SKowalski, Kamil                 }
2311fda13ad2SSunitha Harish                 // Need both vlanId and vlanEnable to service this request
2312fda13ad2SSunitha Harish                 if (!vlanId)
2313fda13ad2SSunitha Harish                 {
2314fda13ad2SSunitha Harish                     messages::propertyMissing(asyncResp->res, "VLANId");
2315fda13ad2SSunitha Harish                 }
2316fda13ad2SSunitha Harish                 if (!vlanEnable)
2317fda13ad2SSunitha Harish                 {
2318fda13ad2SSunitha Harish                     messages::propertyMissing(asyncResp->res, "VLANEnable");
2319fda13ad2SSunitha Harish                 }
2320271584abSEd Tanous                 if (static_cast<bool>(vlanId) ^ vlanEnable)
2321fda13ad2SSunitha Harish                 {
2322fda13ad2SSunitha Harish                     return;
2323fda13ad2SSunitha Harish                 }
2324fda13ad2SSunitha Harish 
2325bf648f77SEd Tanous                 auto callback =
2326bf648f77SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
23271abe55efSEd Tanous                         if (ec)
23281abe55efSEd Tanous                         {
2329bf648f77SEd Tanous                             // TODO(ed) make more consistent error messages
2330bf648f77SEd Tanous                             // based on phosphor-network responses
2331f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
23324a0cb85cSEd Tanous                             return;
23331abe55efSEd Tanous                         }
2334f12894f8SJason M. Bills                         messages::created(asyncResp->res);
2335e439f0f8SKowalski, Kamil                     };
23364a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
23374a0cb85cSEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
23384a0cb85cSEd Tanous                     "/xyz/openbmc_project/network",
23394a0cb85cSEd Tanous                     "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
23400627a2c7SEd Tanous                     rootInterfaceName, vlanId);
2341bf648f77SEd Tanous             });
23424a0cb85cSEd Tanous }
2343bf648f77SEd Tanous 
23449391bb9cSRapkiewicz, Pawel } // namespace redfish
2345