xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 889ff6943d62762eeaf58824b651d2edaf940d1d)
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>
23ed398213SEd Tanous #include <registries/privilege_registry.hpp>
241214b7e7SGunnar Mills #include <utils/json_utils.hpp>
251214b7e7SGunnar Mills 
26a24526dcSEd Tanous #include <optional>
27ab6554f1SJoshi-Mansi #include <regex>
28abf2add6SEd Tanous #include <variant>
299391bb9cSRapkiewicz, Pawel 
301abe55efSEd Tanous namespace redfish
311abe55efSEd Tanous {
329391bb9cSRapkiewicz, Pawel 
339391bb9cSRapkiewicz, Pawel /**
349391bb9cSRapkiewicz, Pawel  * DBus types primitives for several generic DBus interfaces
359391bb9cSRapkiewicz, Pawel  * TODO(Pawel) consider move this to separate file into boost::dbus
369391bb9cSRapkiewicz, Pawel  */
37aa2e59c1SEd Tanous using PropertiesMapType = boost::container::flat_map<
38abf2add6SEd Tanous     std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
39aa2e59c1SEd Tanous                               int32_t, uint32_t, int64_t, uint64_t, double>>;
409391bb9cSRapkiewicz, Pawel 
414a0cb85cSEd Tanous using GetManagedObjects = std::vector<std::pair<
42aa2e59c1SEd Tanous     sdbusplus::message::object_path,
434a0cb85cSEd Tanous     std::vector<std::pair<
44aa2e59c1SEd Tanous         std::string,
45aa2e59c1SEd Tanous         boost::container::flat_map<
4619bd78d9SPatrick Williams             std::string,
4719bd78d9SPatrick Williams             std::variant<std::string, bool, uint8_t, int16_t, uint16_t, int32_t,
4819bd78d9SPatrick Williams                          uint32_t, int64_t, uint64_t, double,
49029573d4SEd Tanous                          std::vector<std::string>>>>>>>;
504a0cb85cSEd Tanous 
514a0cb85cSEd Tanous enum class LinkType
524a0cb85cSEd Tanous {
534a0cb85cSEd Tanous     Local,
544a0cb85cSEd Tanous     Global
554a0cb85cSEd Tanous };
569391bb9cSRapkiewicz, Pawel 
579391bb9cSRapkiewicz, Pawel /**
589391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
599391bb9cSRapkiewicz, Pawel  */
601abe55efSEd Tanous struct IPv4AddressData
611abe55efSEd Tanous {
62179db1d7SKowalski, Kamil     std::string id;
634a0cb85cSEd Tanous     std::string address;
644a0cb85cSEd Tanous     std::string domain;
654a0cb85cSEd Tanous     std::string gateway;
669391bb9cSRapkiewicz, Pawel     std::string netmask;
679391bb9cSRapkiewicz, Pawel     std::string origin;
684a0cb85cSEd Tanous     LinkType linktype;
6901c6e858SSunitha Harish     bool isActive;
704a0cb85cSEd Tanous 
711abe55efSEd Tanous     bool operator<(const IPv4AddressData& obj) const
721abe55efSEd Tanous     {
734a0cb85cSEd Tanous         return id < obj.id;
741abe55efSEd Tanous     }
759391bb9cSRapkiewicz, Pawel };
769391bb9cSRapkiewicz, Pawel 
779391bb9cSRapkiewicz, Pawel /**
78e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
79e48c0fc5SRavi Teja  */
80e48c0fc5SRavi Teja struct IPv6AddressData
81e48c0fc5SRavi Teja {
82e48c0fc5SRavi Teja     std::string id;
83e48c0fc5SRavi Teja     std::string address;
84e48c0fc5SRavi Teja     std::string origin;
85e48c0fc5SRavi Teja     uint8_t prefixLength;
86e48c0fc5SRavi Teja 
87e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData& obj) const
88e48c0fc5SRavi Teja     {
89e48c0fc5SRavi Teja         return id < obj.id;
90e48c0fc5SRavi Teja     }
91e48c0fc5SRavi Teja };
92e48c0fc5SRavi Teja /**
939391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
949391bb9cSRapkiewicz, Pawel  * available from DBus
959391bb9cSRapkiewicz, Pawel  */
961abe55efSEd Tanous struct EthernetInterfaceData
971abe55efSEd Tanous {
984a0cb85cSEd Tanous     uint32_t speed;
994a0cb85cSEd Tanous     bool auto_neg;
1001f8c7b5dSJohnathan Mantey     bool DNSEnabled;
1011f8c7b5dSJohnathan Mantey     bool NTPEnabled;
1021f8c7b5dSJohnathan Mantey     bool HostNameEnabled;
1031f8c7b5dSJohnathan Mantey     bool SendHostNameEnabled;
104aa05fb27SJohnathan Mantey     bool linkUp;
105eeedda23SJohnathan Mantey     bool nicEnabled;
1061f8c7b5dSJohnathan Mantey     std::string DHCPEnabled;
1071f8c7b5dSJohnathan Mantey     std::string operatingMode;
1084a0cb85cSEd Tanous     std::string hostname;
1094a0cb85cSEd Tanous     std::string default_gateway;
1109a6fc6feSRavi Teja     std::string ipv6_default_gateway;
1114a0cb85cSEd Tanous     std::string mac_address;
112fda13ad2SSunitha Harish     std::vector<std::uint32_t> vlan_id;
1130f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
1140f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
115d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
1169391bb9cSRapkiewicz, Pawel };
1179391bb9cSRapkiewicz, Pawel 
1181f8c7b5dSJohnathan Mantey struct DHCPParameters
1191f8c7b5dSJohnathan Mantey {
1201f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
1211f8c7b5dSJohnathan Mantey     std::optional<bool> useDNSServers;
1221f8c7b5dSJohnathan Mantey     std::optional<bool> useNTPServers;
1231f8c7b5dSJohnathan Mantey     std::optional<bool> useUseDomainName;
1241f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1251f8c7b5dSJohnathan Mantey };
1261f8c7b5dSJohnathan Mantey 
1279391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1289391bb9cSRapkiewicz, Pawel // into full dot notation
1291abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1301abe55efSEd Tanous {
1319391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1329391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1339391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1349391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1359391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1369391bb9cSRapkiewicz, Pawel     return netmask;
1379391bb9cSRapkiewicz, Pawel }
1389391bb9cSRapkiewicz, Pawel 
1391f8c7b5dSJohnathan Mantey inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
1401f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1411f8c7b5dSJohnathan Mantey {
1421f8c7b5dSJohnathan Mantey     if (isIPv4)
1431f8c7b5dSJohnathan Mantey     {
1441f8c7b5dSJohnathan Mantey         return (
1451f8c7b5dSJohnathan Mantey             (inputDHCP ==
1461f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1471f8c7b5dSJohnathan Mantey             (inputDHCP ==
1481f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1491f8c7b5dSJohnathan Mantey     }
1501f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1511f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1521f8c7b5dSJohnathan Mantey             (inputDHCP ==
1531f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1541f8c7b5dSJohnathan Mantey }
1551f8c7b5dSJohnathan Mantey 
1562c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1571f8c7b5dSJohnathan Mantey {
1581f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1591f8c7b5dSJohnathan Mantey     {
1601f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1611f8c7b5dSJohnathan Mantey     }
1623174e4dfSEd Tanous     if (isIPv4)
1631f8c7b5dSJohnathan Mantey     {
1641f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1651f8c7b5dSJohnathan Mantey     }
1663174e4dfSEd Tanous     if (isIPv6)
1671f8c7b5dSJohnathan Mantey     {
1681f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1691f8c7b5dSJohnathan Mantey     }
1701f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1711f8c7b5dSJohnathan Mantey }
1721f8c7b5dSJohnathan Mantey 
1734a0cb85cSEd Tanous inline std::string
1744a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1754a0cb85cSEd Tanous                                         bool isIPv4)
1761abe55efSEd Tanous {
1774a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1781abe55efSEd Tanous     {
1794a0cb85cSEd Tanous         return "Static";
1809391bb9cSRapkiewicz, Pawel     }
1814a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1821abe55efSEd Tanous     {
1834a0cb85cSEd Tanous         if (isIPv4)
1841abe55efSEd Tanous         {
1854a0cb85cSEd Tanous             return "IPv4LinkLocal";
1861abe55efSEd Tanous         }
1874a0cb85cSEd Tanous         return "LinkLocal";
1889391bb9cSRapkiewicz, Pawel     }
1894a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1901abe55efSEd Tanous     {
1914a0cb85cSEd Tanous         if (isIPv4)
1924a0cb85cSEd Tanous         {
1934a0cb85cSEd Tanous             return "DHCP";
1944a0cb85cSEd Tanous         }
1954a0cb85cSEd Tanous         return "DHCPv6";
1964a0cb85cSEd Tanous     }
1974a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1984a0cb85cSEd Tanous     {
1994a0cb85cSEd Tanous         return "SLAAC";
2004a0cb85cSEd Tanous     }
2014a0cb85cSEd Tanous     return "";
2024a0cb85cSEd Tanous }
2034a0cb85cSEd Tanous 
20481ce609eSEd Tanous inline bool extractEthernetInterfaceData(const std::string& ethifaceId,
20581ce609eSEd Tanous                                          GetManagedObjects& dbusData,
2064a0cb85cSEd Tanous                                          EthernetInterfaceData& ethData)
2074a0cb85cSEd Tanous {
2084c9afe43SEd Tanous     bool idFound = false;
20981ce609eSEd Tanous     for (auto& objpath : dbusData)
2104a0cb85cSEd Tanous     {
211f23b7296SEd Tanous         for (auto& ifacePair : objpath.second)
2124a0cb85cSEd Tanous         {
21381ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
214029573d4SEd Tanous             {
2154c9afe43SEd Tanous                 idFound = true;
2164a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2174a0cb85cSEd Tanous                 {
2184a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2194a0cb85cSEd Tanous                     {
2204a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2214a0cb85cSEd Tanous                         {
2224a0cb85cSEd Tanous                             const std::string* mac =
223abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2244a0cb85cSEd Tanous                             if (mac != nullptr)
2254a0cb85cSEd Tanous                             {
2264a0cb85cSEd Tanous                                 ethData.mac_address = *mac;
2274a0cb85cSEd Tanous                             }
2284a0cb85cSEd Tanous                         }
2294a0cb85cSEd Tanous                     }
2304a0cb85cSEd Tanous                 }
2314a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2324a0cb85cSEd Tanous                 {
2334a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2344a0cb85cSEd Tanous                     {
2354a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2364a0cb85cSEd Tanous                         {
2371b6b96c5SEd Tanous                             const uint32_t* id =
238abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2394a0cb85cSEd Tanous                             if (id != nullptr)
2404a0cb85cSEd Tanous                             {
241fda13ad2SSunitha Harish                                 ethData.vlan_id.push_back(*id);
2424a0cb85cSEd Tanous                             }
2434a0cb85cSEd Tanous                         }
2444a0cb85cSEd Tanous                     }
2454a0cb85cSEd Tanous                 }
2464a0cb85cSEd Tanous                 else if (ifacePair.first ==
2474a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2484a0cb85cSEd Tanous                 {
2494a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2504a0cb85cSEd Tanous                     {
2514a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2524a0cb85cSEd Tanous                         {
2532c70f800SEd Tanous                             const bool* autoNeg =
254abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2552c70f800SEd Tanous                             if (autoNeg != nullptr)
2564a0cb85cSEd Tanous                             {
2572c70f800SEd Tanous                                 ethData.auto_neg = *autoNeg;
2584a0cb85cSEd Tanous                             }
2594a0cb85cSEd Tanous                         }
2604a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2614a0cb85cSEd Tanous                         {
2624a0cb85cSEd Tanous                             const uint32_t* speed =
263abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2644a0cb85cSEd Tanous                             if (speed != nullptr)
2654a0cb85cSEd Tanous                             {
2664a0cb85cSEd Tanous                                 ethData.speed = *speed;
2674a0cb85cSEd Tanous                             }
2684a0cb85cSEd Tanous                         }
269aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
270aa05fb27SJohnathan Mantey                         {
271aa05fb27SJohnathan Mantey                             const bool* linkUp =
272aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
273aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
274aa05fb27SJohnathan Mantey                             {
275aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
276aa05fb27SJohnathan Mantey                             }
277aa05fb27SJohnathan Mantey                         }
278eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
279eeedda23SJohnathan Mantey                         {
280eeedda23SJohnathan Mantey                             const bool* nicEnabled =
281eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
282eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
283eeedda23SJohnathan Mantey                             {
284eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
285eeedda23SJohnathan Mantey                             }
286eeedda23SJohnathan Mantey                         }
287f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
288029573d4SEd Tanous                         {
289029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2908d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
291029573d4SEd Tanous                                     &propertyPair.second);
292029573d4SEd Tanous                             if (nameservers != nullptr)
293029573d4SEd Tanous                             {
294f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2950f6efdc1Smanojkiran.eda@gmail.com                             }
2960f6efdc1Smanojkiran.eda@gmail.com                         }
2970f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2980f6efdc1Smanojkiran.eda@gmail.com                         {
2990f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
3008d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
3010f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
3020f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
3030f6efdc1Smanojkiran.eda@gmail.com                             {
304f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
3054a0cb85cSEd Tanous                             }
3064a0cb85cSEd Tanous                         }
3072a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3082a133282Smanojkiraneda                         {
3092c70f800SEd Tanous                             const std::string* dhcpEnabled =
3101f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3112c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3122a133282Smanojkiraneda                             {
3132c70f800SEd Tanous                                 ethData.DHCPEnabled = *dhcpEnabled;
3142a133282Smanojkiraneda                             }
3152a133282Smanojkiraneda                         }
316d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
317d24bfc7aSJennifer Lee                         {
318d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3198d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
320d24bfc7aSJennifer Lee                                     &propertyPair.second);
321d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
322d24bfc7aSJennifer Lee                             {
323f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
324d24bfc7aSJennifer Lee                             }
325d24bfc7aSJennifer Lee                         }
3269010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway")
3279010ec2eSRavi Teja                         {
3289010ec2eSRavi Teja                             const std::string* defaultGateway =
3299010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3309010ec2eSRavi Teja                             if (defaultGateway != nullptr)
3319010ec2eSRavi Teja                             {
3329010ec2eSRavi Teja                                 std::string defaultGatewayStr = *defaultGateway;
3339010ec2eSRavi Teja                                 if (defaultGatewayStr.empty())
3349010ec2eSRavi Teja                                 {
3359010ec2eSRavi Teja                                     ethData.default_gateway = "0.0.0.0";
3369010ec2eSRavi Teja                                 }
3379010ec2eSRavi Teja                                 else
3389010ec2eSRavi Teja                                 {
3399010ec2eSRavi Teja                                     ethData.default_gateway = defaultGatewayStr;
3409010ec2eSRavi Teja                                 }
3419010ec2eSRavi Teja                             }
3429010ec2eSRavi Teja                         }
3439010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway6")
3449010ec2eSRavi Teja                         {
3459010ec2eSRavi Teja                             const std::string* defaultGateway6 =
3469010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3479010ec2eSRavi Teja                             if (defaultGateway6 != nullptr)
3489010ec2eSRavi Teja                             {
3499010ec2eSRavi Teja                                 std::string defaultGateway6Str =
3509010ec2eSRavi Teja                                     *defaultGateway6;
3519010ec2eSRavi Teja                                 if (defaultGateway6Str.empty())
3529010ec2eSRavi Teja                                 {
3539010ec2eSRavi Teja                                     ethData.ipv6_default_gateway =
3549010ec2eSRavi Teja                                         "0:0:0:0:0:0:0:0";
3559010ec2eSRavi Teja                                 }
3569010ec2eSRavi Teja                                 else
3579010ec2eSRavi Teja                                 {
3589010ec2eSRavi Teja                                     ethData.ipv6_default_gateway =
3599010ec2eSRavi Teja                                         defaultGateway6Str;
3609010ec2eSRavi Teja                                 }
3619010ec2eSRavi Teja                             }
3629010ec2eSRavi Teja                         }
363029573d4SEd Tanous                     }
364029573d4SEd Tanous                 }
365029573d4SEd Tanous             }
3661f8c7b5dSJohnathan Mantey 
3671f8c7b5dSJohnathan Mantey             if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
3681f8c7b5dSJohnathan Mantey             {
3691f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3701f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3711f8c7b5dSJohnathan Mantey                 {
3721f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3731f8c7b5dSJohnathan Mantey                     {
3741f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3751f8c7b5dSJohnathan Mantey                         {
3762c70f800SEd Tanous                             const bool* dnsEnabled =
3771f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3782c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3791f8c7b5dSJohnathan Mantey                             {
3802c70f800SEd Tanous                                 ethData.DNSEnabled = *dnsEnabled;
3811f8c7b5dSJohnathan Mantey                             }
3821f8c7b5dSJohnathan Mantey                         }
3831f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3841f8c7b5dSJohnathan Mantey                         {
3852c70f800SEd Tanous                             const bool* ntpEnabled =
3861f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3872c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3881f8c7b5dSJohnathan Mantey                             {
3892c70f800SEd Tanous                                 ethData.NTPEnabled = *ntpEnabled;
3901f8c7b5dSJohnathan Mantey                             }
3911f8c7b5dSJohnathan Mantey                         }
3921f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3931f8c7b5dSJohnathan Mantey                         {
3942c70f800SEd Tanous                             const bool* hostNameEnabled =
3951f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3962c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3971f8c7b5dSJohnathan Mantey                             {
3982c70f800SEd Tanous                                 ethData.HostNameEnabled = *hostNameEnabled;
3991f8c7b5dSJohnathan Mantey                             }
4001f8c7b5dSJohnathan Mantey                         }
4011f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "SendHostNameEnabled")
4021f8c7b5dSJohnathan Mantey                         {
4032c70f800SEd Tanous                             const bool* sendHostNameEnabled =
4041f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
4052c70f800SEd Tanous                             if (sendHostNameEnabled != nullptr)
4061f8c7b5dSJohnathan Mantey                             {
4071f8c7b5dSJohnathan Mantey                                 ethData.SendHostNameEnabled =
4082c70f800SEd Tanous                                     *sendHostNameEnabled;
4091f8c7b5dSJohnathan Mantey                             }
4101f8c7b5dSJohnathan Mantey                         }
4111f8c7b5dSJohnathan Mantey                     }
4121f8c7b5dSJohnathan Mantey                 }
4131f8c7b5dSJohnathan Mantey             }
414029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
415029573d4SEd Tanous             // to check eth number
416029573d4SEd Tanous             if (ifacePair.first ==
4174a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
4184a0cb85cSEd Tanous             {
4194a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
4204a0cb85cSEd Tanous                 {
4214a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
4224a0cb85cSEd Tanous                     {
4234a0cb85cSEd Tanous                         const std::string* hostname =
4248d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4254a0cb85cSEd Tanous                         if (hostname != nullptr)
4264a0cb85cSEd Tanous                         {
4274a0cb85cSEd Tanous                             ethData.hostname = *hostname;
4284a0cb85cSEd Tanous                         }
4294a0cb85cSEd Tanous                     }
4304a0cb85cSEd Tanous                 }
4314a0cb85cSEd Tanous             }
4324a0cb85cSEd Tanous         }
4334a0cb85cSEd Tanous     }
4344c9afe43SEd Tanous     return idFound;
4354a0cb85cSEd Tanous }
4364a0cb85cSEd Tanous 
437e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
43801784826SJohnathan Mantey inline void
43981ce609eSEd Tanous     extractIPV6Data(const std::string& ethifaceId,
44081ce609eSEd Tanous                     const GetManagedObjects& dbusData,
44181ce609eSEd Tanous                     boost::container::flat_set<IPv6AddressData>& ipv6Config)
442e48c0fc5SRavi Teja {
443e48c0fc5SRavi Teja     const std::string ipv6PathStart =
44481ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
445e48c0fc5SRavi Teja 
446e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
447e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
44881ce609eSEd Tanous     for (const auto& objpath : dbusData)
449e48c0fc5SRavi Teja     {
450e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
451e48c0fc5SRavi Teja         if (boost::starts_with(objpath.first.str, ipv6PathStart))
452e48c0fc5SRavi Teja         {
453e48c0fc5SRavi Teja             for (auto& interface : objpath.second)
454e48c0fc5SRavi Teja             {
455e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
456e48c0fc5SRavi Teja                 {
457e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
458e48c0fc5SRavi Teja                     // appropriate
459e48c0fc5SRavi Teja                     std::pair<
460e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
461e48c0fc5SRavi Teja                         bool>
46281ce609eSEd Tanous                         it = ipv6Config.insert(IPv6AddressData{});
4632c70f800SEd Tanous                     IPv6AddressData& ipv6Address = *it.first;
4642c70f800SEd Tanous                     ipv6Address.id =
465271584abSEd Tanous                         objpath.first.str.substr(ipv6PathStart.size());
466e48c0fc5SRavi Teja                     for (auto& property : interface.second)
467e48c0fc5SRavi Teja                     {
468e48c0fc5SRavi Teja                         if (property.first == "Address")
469e48c0fc5SRavi Teja                         {
470e48c0fc5SRavi Teja                             const std::string* address =
471e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
472e48c0fc5SRavi Teja                             if (address != nullptr)
473e48c0fc5SRavi Teja                             {
4742c70f800SEd Tanous                                 ipv6Address.address = *address;
475e48c0fc5SRavi Teja                             }
476e48c0fc5SRavi Teja                         }
477e48c0fc5SRavi Teja                         else if (property.first == "Origin")
478e48c0fc5SRavi Teja                         {
479e48c0fc5SRavi Teja                             const std::string* origin =
480e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
481e48c0fc5SRavi Teja                             if (origin != nullptr)
482e48c0fc5SRavi Teja                             {
4832c70f800SEd Tanous                                 ipv6Address.origin =
484e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
485e48c0fc5SRavi Teja                                                                         false);
486e48c0fc5SRavi Teja                             }
487e48c0fc5SRavi Teja                         }
488e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
489e48c0fc5SRavi Teja                         {
490e48c0fc5SRavi Teja                             const uint8_t* prefix =
491e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
492e48c0fc5SRavi Teja                             if (prefix != nullptr)
493e48c0fc5SRavi Teja                             {
4942c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
495e48c0fc5SRavi Teja                             }
496e48c0fc5SRavi Teja                         }
497*889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
498*889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
499*889ff694SAsmitha Karunanithi                         {
500*889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
501*889ff694SAsmitha Karunanithi                         }
502e48c0fc5SRavi Teja                         else
503e48c0fc5SRavi Teja                         {
504e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
505e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
506e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
507e48c0fc5SRavi Teja                         }
508e48c0fc5SRavi Teja                     }
509e48c0fc5SRavi Teja                 }
510e48c0fc5SRavi Teja             }
511e48c0fc5SRavi Teja         }
512e48c0fc5SRavi Teja     }
513e48c0fc5SRavi Teja }
514e48c0fc5SRavi Teja 
5154a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
51601784826SJohnathan Mantey inline void
51781ce609eSEd Tanous     extractIPData(const std::string& ethifaceId,
51881ce609eSEd Tanous                   const GetManagedObjects& dbusData,
51981ce609eSEd Tanous                   boost::container::flat_set<IPv4AddressData>& ipv4Config)
5204a0cb85cSEd Tanous {
5214a0cb85cSEd Tanous     const std::string ipv4PathStart =
52281ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
5234a0cb85cSEd Tanous 
5244a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5254a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
52681ce609eSEd Tanous     for (const auto& objpath : dbusData)
5274a0cb85cSEd Tanous     {
5284a0cb85cSEd Tanous         // Check if proper pattern for object path appears
5294a0cb85cSEd Tanous         if (boost::starts_with(objpath.first.str, ipv4PathStart))
5304a0cb85cSEd Tanous         {
5314a0cb85cSEd Tanous             for (auto& interface : objpath.second)
5324a0cb85cSEd Tanous             {
5334a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5344a0cb85cSEd Tanous                 {
5354a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5364a0cb85cSEd Tanous                     // appropriate
5374a0cb85cSEd Tanous                     std::pair<
5384a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5394a0cb85cSEd Tanous                         bool>
54081ce609eSEd Tanous                         it = ipv4Config.insert(IPv4AddressData{});
5412c70f800SEd Tanous                     IPv4AddressData& ipv4Address = *it.first;
5422c70f800SEd Tanous                     ipv4Address.id =
543271584abSEd Tanous                         objpath.first.str.substr(ipv4PathStart.size());
5444a0cb85cSEd Tanous                     for (auto& property : interface.second)
5454a0cb85cSEd Tanous                     {
5464a0cb85cSEd Tanous                         if (property.first == "Address")
5474a0cb85cSEd Tanous                         {
5484a0cb85cSEd Tanous                             const std::string* address =
549abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5504a0cb85cSEd Tanous                             if (address != nullptr)
5514a0cb85cSEd Tanous                             {
5522c70f800SEd Tanous                                 ipv4Address.address = *address;
5534a0cb85cSEd Tanous                             }
5544a0cb85cSEd Tanous                         }
5554a0cb85cSEd Tanous                         else if (property.first == "Origin")
5564a0cb85cSEd Tanous                         {
5574a0cb85cSEd Tanous                             const std::string* origin =
558abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5594a0cb85cSEd Tanous                             if (origin != nullptr)
5604a0cb85cSEd Tanous                             {
5612c70f800SEd Tanous                                 ipv4Address.origin =
5624a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5634a0cb85cSEd Tanous                                                                         true);
5644a0cb85cSEd Tanous                             }
5654a0cb85cSEd Tanous                         }
5664a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5674a0cb85cSEd Tanous                         {
5684a0cb85cSEd Tanous                             const uint8_t* mask =
569abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5704a0cb85cSEd Tanous                             if (mask != nullptr)
5714a0cb85cSEd Tanous                             {
5724a0cb85cSEd Tanous                                 // convert it to the string
5732c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
5744a0cb85cSEd Tanous                             }
5754a0cb85cSEd Tanous                         }
576*889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
577*889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
578*889ff694SAsmitha Karunanithi                         {
579*889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
580*889ff694SAsmitha Karunanithi                         }
5814a0cb85cSEd Tanous                         else
5824a0cb85cSEd Tanous                         {
5834a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5844a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5854a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5864a0cb85cSEd Tanous                         }
5874a0cb85cSEd Tanous                     }
5884a0cb85cSEd Tanous                     // Check if given address is local, or global
5892c70f800SEd Tanous                     ipv4Address.linktype =
5902c70f800SEd Tanous                         boost::starts_with(ipv4Address.address, "169.254.")
59118659d10SJohnathan Mantey                             ? LinkType::Local
59218659d10SJohnathan Mantey                             : LinkType::Global;
5934a0cb85cSEd Tanous                 }
5944a0cb85cSEd Tanous             }
5954a0cb85cSEd Tanous         }
5964a0cb85cSEd Tanous     }
5974a0cb85cSEd Tanous }
598588c3f0dSKowalski, Kamil 
599588c3f0dSKowalski, Kamil /**
600588c3f0dSKowalski, Kamil  * @brief Sets given Id on the given VLAN interface through D-Bus
601588c3f0dSKowalski, Kamil  *
602588c3f0dSKowalski, Kamil  * @param[in] ifaceId       Id of VLAN interface that should be modified
603588c3f0dSKowalski, Kamil  * @param[in] inputVlanId   New ID of the VLAN
604588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
605588c3f0dSKowalski, Kamil  *
606588c3f0dSKowalski, Kamil  * @return None.
607588c3f0dSKowalski, Kamil  */
608588c3f0dSKowalski, Kamil template <typename CallbackFunc>
6094a0cb85cSEd Tanous void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
6101abe55efSEd Tanous                   CallbackFunc&& callback)
6111abe55efSEd Tanous {
61255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
613588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
614588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
615588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
616588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", "Id",
617abf2add6SEd Tanous         std::variant<uint32_t>(inputVlanId));
6184a0cb85cSEd Tanous }
619588c3f0dSKowalski, Kamil 
620588c3f0dSKowalski, Kamil /**
621179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
622179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
623179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
624179db1d7SKowalski, Kamil  *
625179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
626179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
627179db1d7SKowalski, Kamil  *
628179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
629179db1d7SKowalski, Kamil  */
6304a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
6311abe55efSEd Tanous                                        uint8_t* bits = nullptr)
6321abe55efSEd Tanous {
633179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
634179db1d7SKowalski, Kamil 
635179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
636179db1d7SKowalski, Kamil 
6374a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
6381abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
6391abe55efSEd Tanous     {
640179db1d7SKowalski, Kamil         return false;
641179db1d7SKowalski, Kamil     }
642179db1d7SKowalski, Kamil 
6431abe55efSEd Tanous     if (bits != nullptr)
6441abe55efSEd Tanous     {
645179db1d7SKowalski, Kamil         *bits = 0;
646179db1d7SKowalski, Kamil     }
647179db1d7SKowalski, Kamil 
648179db1d7SKowalski, Kamil     char* endPtr;
649179db1d7SKowalski, Kamil     long previousValue = 255;
650179db1d7SKowalski, Kamil     bool firstZeroInByteHit;
6511abe55efSEd Tanous     for (const std::string& byte : bytesInMask)
6521abe55efSEd Tanous     {
6531abe55efSEd Tanous         if (byte.empty())
6541abe55efSEd Tanous         {
6551db9ca37SKowalski, Kamil             return false;
6561db9ca37SKowalski, Kamil         }
6571db9ca37SKowalski, Kamil 
658179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
6591db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
660179db1d7SKowalski, Kamil 
6614a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
6624a0cb85cSEd Tanous         // is not 100% number
6631abe55efSEd Tanous         if (*endPtr != '\0')
6641abe55efSEd Tanous         {
665179db1d7SKowalski, Kamil             return false;
666179db1d7SKowalski, Kamil         }
667179db1d7SKowalski, Kamil 
668179db1d7SKowalski, Kamil         // Value should be contained in byte
6691abe55efSEd Tanous         if (value < 0 || value > 255)
6701abe55efSEd Tanous         {
671179db1d7SKowalski, Kamil             return false;
672179db1d7SKowalski, Kamil         }
673179db1d7SKowalski, Kamil 
6741abe55efSEd Tanous         if (bits != nullptr)
6751abe55efSEd Tanous         {
676179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
6771abe55efSEd Tanous             if (previousValue != 255 && value != 0)
6781abe55efSEd Tanous             {
679179db1d7SKowalski, Kamil                 return false;
680179db1d7SKowalski, Kamil             }
681179db1d7SKowalski, Kamil 
682179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
683179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
684179db1d7SKowalski, Kamil 
685179db1d7SKowalski, Kamil             // Count bits
68623a21a1cSEd Tanous             for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
6871abe55efSEd Tanous             {
68823a21a1cSEd Tanous                 if (value & (1L << bitIdx))
6891abe55efSEd Tanous                 {
6901abe55efSEd Tanous                     if (firstZeroInByteHit)
6911abe55efSEd Tanous                     {
692179db1d7SKowalski, Kamil                         // Continuity not preserved
693179db1d7SKowalski, Kamil                         return false;
6941abe55efSEd Tanous                     }
695179db1d7SKowalski, Kamil                     (*bits)++;
696179db1d7SKowalski, Kamil                 }
6971abe55efSEd Tanous                 else
6981abe55efSEd Tanous                 {
699179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
700179db1d7SKowalski, Kamil                 }
701179db1d7SKowalski, Kamil             }
702179db1d7SKowalski, Kamil         }
703179db1d7SKowalski, Kamil 
704179db1d7SKowalski, Kamil         previousValue = value;
705179db1d7SKowalski, Kamil     }
706179db1d7SKowalski, Kamil 
707179db1d7SKowalski, Kamil     return true;
708179db1d7SKowalski, Kamil }
709179db1d7SKowalski, Kamil 
710179db1d7SKowalski, Kamil /**
71101784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
712179db1d7SKowalski, Kamil  *
713179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
714179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
715179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
716179db1d7SKowalski, Kamil  *
717179db1d7SKowalski, Kamil  * @return None
718179db1d7SKowalski, Kamil  */
7194a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
7208d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7211abe55efSEd Tanous {
72255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
723286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
7241abe55efSEd Tanous             if (ec)
7251abe55efSEd Tanous             {
726a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
7271abe55efSEd Tanous             }
728179db1d7SKowalski, Kamil         },
729179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
730179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
731179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
732179db1d7SKowalski, Kamil }
733179db1d7SKowalski, Kamil 
734244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway(
735244b6d5bSGunnar Mills     const std::string& ifaceId, const std::string& gateway,
736244b6d5bSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7379010ec2eSRavi Teja {
7389010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7399010ec2eSRavi Teja         [asyncResp](const boost::system::error_code ec) {
7409010ec2eSRavi Teja             if (ec)
7419010ec2eSRavi Teja             {
7429010ec2eSRavi Teja                 messages::internalError(asyncResp->res);
7439010ec2eSRavi Teja                 return;
7449010ec2eSRavi Teja             }
7459010ec2eSRavi Teja             asyncResp->res.result(boost::beast::http::status::no_content);
7469010ec2eSRavi Teja         },
7479010ec2eSRavi Teja         "xyz.openbmc_project.Network",
7489010ec2eSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
7499010ec2eSRavi Teja         "org.freedesktop.DBus.Properties", "Set",
7509010ec2eSRavi Teja         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
7519010ec2eSRavi Teja         std::variant<std::string>(gateway));
7529010ec2eSRavi Teja }
753179db1d7SKowalski, Kamil /**
75401784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
755179db1d7SKowalski, Kamil  *
75601784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
75701784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
75801784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
75901784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
760179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
761179db1d7SKowalski, Kamil  *
762179db1d7SKowalski, Kamil  * @return None
763179db1d7SKowalski, Kamil  */
764cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
765cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
7668d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7671abe55efSEd Tanous {
7689010ec2eSRavi Teja     auto createIpHandler = [asyncResp, ifaceId,
7699010ec2eSRavi Teja                             gateway](const boost::system::error_code ec) {
7701abe55efSEd Tanous         if (ec)
7711abe55efSEd Tanous         {
772a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
7739010ec2eSRavi Teja             return;
774179db1d7SKowalski, Kamil         }
7759010ec2eSRavi Teja         updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
7769010ec2eSRavi Teja     };
7779010ec2eSRavi Teja 
7789010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7799010ec2eSRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
780179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
781179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
78201784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
783179db1d7SKowalski, Kamil         gateway);
784179db1d7SKowalski, Kamil }
785e48c0fc5SRavi Teja 
786e48c0fc5SRavi Teja /**
78701784826SJohnathan Mantey  * @brief Deletes the IPv4 entry for this interface and creates a replacement
78801784826SJohnathan Mantey  * static IPv4 entry
78901784826SJohnathan Mantey  *
79001784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
79101784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
79201784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
79301784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
79401784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
79501784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
79601784826SJohnathan Mantey  *
79701784826SJohnathan Mantey  * @return None
79801784826SJohnathan Mantey  */
7998d1b46d7Szhanghch05 inline void
8008d1b46d7Szhanghch05     deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
8018d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& gateway,
80201784826SJohnathan Mantey                         const std::string& address,
8038d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
80401784826SJohnathan Mantey {
80501784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
80601784826SJohnathan Mantey         [asyncResp, ifaceId, address, prefixLength,
80701784826SJohnathan Mantey          gateway](const boost::system::error_code ec) {
80801784826SJohnathan Mantey             if (ec)
80901784826SJohnathan Mantey             {
81001784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
8119010ec2eSRavi Teja                 return;
81201784826SJohnathan Mantey             }
8139010ec2eSRavi Teja 
81401784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
8159010ec2eSRavi Teja                 [asyncResp, ifaceId,
8169010ec2eSRavi Teja                  gateway](const boost::system::error_code ec2) {
81723a21a1cSEd Tanous                     if (ec2)
81801784826SJohnathan Mantey                     {
81901784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
8209010ec2eSRavi Teja                         return;
82101784826SJohnathan Mantey                     }
8229010ec2eSRavi Teja                     updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
82301784826SJohnathan Mantey                 },
82401784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
82501784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
82601784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
82701784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
82801784826SJohnathan Mantey                 prefixLength, gateway);
82901784826SJohnathan Mantey         },
83001784826SJohnathan Mantey         "xyz.openbmc_project.Network",
83101784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
83201784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
83301784826SJohnathan Mantey }
83401784826SJohnathan Mantey 
83501784826SJohnathan Mantey /**
836e48c0fc5SRavi Teja  * @brief Deletes given IPv6
837e48c0fc5SRavi Teja  *
838e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
839e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
840e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
841e48c0fc5SRavi Teja  *
842e48c0fc5SRavi Teja  * @return None
843e48c0fc5SRavi Teja  */
844e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
8458d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
846e48c0fc5SRavi Teja {
847e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
848286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
849e48c0fc5SRavi Teja             if (ec)
850e48c0fc5SRavi Teja             {
851e48c0fc5SRavi Teja                 messages::internalError(asyncResp->res);
852e48c0fc5SRavi Teja             }
853e48c0fc5SRavi Teja         },
854e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
855e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
856e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
857e48c0fc5SRavi Teja }
858e48c0fc5SRavi Teja 
859e48c0fc5SRavi Teja /**
86001784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
86101784826SJohnathan Mantey  * static IPv6 entry
86201784826SJohnathan Mantey  *
86301784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
86401784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
86501784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
86601784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
86701784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
86801784826SJohnathan Mantey  *
86901784826SJohnathan Mantey  * @return None
87001784826SJohnathan Mantey  */
8718d1b46d7Szhanghch05 inline void
8728d1b46d7Szhanghch05     deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
8738d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& address,
8748d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
87501784826SJohnathan Mantey {
87601784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
87701784826SJohnathan Mantey         [asyncResp, ifaceId, address,
87801784826SJohnathan Mantey          prefixLength](const boost::system::error_code ec) {
87901784826SJohnathan Mantey             if (ec)
88001784826SJohnathan Mantey             {
88101784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
88201784826SJohnathan Mantey             }
88301784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
88423a21a1cSEd Tanous                 [asyncResp](const boost::system::error_code ec2) {
88523a21a1cSEd Tanous                     if (ec2)
88601784826SJohnathan Mantey                     {
88701784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
88801784826SJohnathan Mantey                     }
88901784826SJohnathan Mantey                 },
89001784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
89101784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
89201784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
89301784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
89401784826SJohnathan Mantey                 prefixLength, "");
89501784826SJohnathan Mantey         },
89601784826SJohnathan Mantey         "xyz.openbmc_project.Network",
89701784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
89801784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
89901784826SJohnathan Mantey }
90001784826SJohnathan Mantey 
90101784826SJohnathan Mantey /**
902e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
903e48c0fc5SRavi Teja  *
904e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
905e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
906e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
907e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
908e48c0fc5SRavi Teja  *
909e48c0fc5SRavi Teja  * @return None
910e48c0fc5SRavi Teja  */
91101784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
91201784826SJohnathan Mantey                        const std::string& address,
9138d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
914e48c0fc5SRavi Teja {
915e48c0fc5SRavi Teja     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
916e48c0fc5SRavi Teja         if (ec)
917e48c0fc5SRavi Teja         {
918e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
919e48c0fc5SRavi Teja         }
920e48c0fc5SRavi Teja     };
921e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
9224e0453b1SGunnar Mills     // does not have associated gateway property
923e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
924e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
925e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
926e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
927e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
928e48c0fc5SRavi Teja         "");
929e48c0fc5SRavi Teja }
930e48c0fc5SRavi Teja 
931179db1d7SKowalski, Kamil /**
932179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
933179db1d7SKowalski, Kamil  * Object
934179db1d7SKowalski, Kamil  * from EntityManager Network Manager
9354a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
936179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
937179db1d7SKowalski, Kamil  * into JSON
938179db1d7SKowalski, Kamil  */
939179db1d7SKowalski, Kamil template <typename CallbackFunc>
94081ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
9411abe55efSEd Tanous                           CallbackFunc&& callback)
9421abe55efSEd Tanous {
94355c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
94481ce609eSEd Tanous         [ethifaceId{std::string{ethifaceId}}, callback{std::move(callback)}](
94581ce609eSEd Tanous             const boost::system::error_code errorCode,
946f23b7296SEd Tanous             GetManagedObjects& resp) {
94755c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
9484a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
949e48c0fc5SRavi Teja             boost::container::flat_set<IPv6AddressData> ipv6Data;
950179db1d7SKowalski, Kamil 
95181ce609eSEd Tanous             if (errorCode)
9521abe55efSEd Tanous             {
95301784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
954179db1d7SKowalski, Kamil                 return;
955179db1d7SKowalski, Kamil             }
956179db1d7SKowalski, Kamil 
9574c9afe43SEd Tanous             bool found =
9582c70f800SEd Tanous                 extractEthernetInterfaceData(ethifaceId, resp, ethData);
9594c9afe43SEd Tanous             if (!found)
9604c9afe43SEd Tanous             {
96101784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
9624c9afe43SEd Tanous                 return;
9634c9afe43SEd Tanous             }
9644c9afe43SEd Tanous 
9652c70f800SEd Tanous             extractIPData(ethifaceId, resp, ipv4Data);
966179db1d7SKowalski, Kamil             // Fix global GW
9671abe55efSEd Tanous             for (IPv4AddressData& ipv4 : ipv4Data)
9681abe55efSEd Tanous             {
969c619141bSRavi Teja                 if (((ipv4.linktype == LinkType::Global) &&
970c619141bSRavi Teja                      (ipv4.gateway == "0.0.0.0")) ||
9719010ec2eSRavi Teja                     (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
9721abe55efSEd Tanous                 {
9734a0cb85cSEd Tanous                     ipv4.gateway = ethData.default_gateway;
974179db1d7SKowalski, Kamil                 }
975179db1d7SKowalski, Kamil             }
976179db1d7SKowalski, Kamil 
9772c70f800SEd Tanous             extractIPV6Data(ethifaceId, resp, ipv6Data);
9784e0453b1SGunnar Mills             // Finally make a callback with useful data
97901784826SJohnathan Mantey             callback(true, ethData, ipv4Data, ipv6Data);
980179db1d7SKowalski, Kamil         },
981179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
982179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
983271584abSEd Tanous }
984179db1d7SKowalski, Kamil 
985179db1d7SKowalski, Kamil /**
9869391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
9879391bb9cSRapkiewicz, Pawel  * Manager
9881abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
9891abe55efSEd Tanous  * into JSON.
9909391bb9cSRapkiewicz, Pawel  */
9919391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
9921abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
9931abe55efSEd Tanous {
99455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
9954a0cb85cSEd Tanous         [callback{std::move(callback)}](
99681ce609eSEd Tanous             const boost::system::error_code errorCode,
9974a0cb85cSEd Tanous             GetManagedObjects& resp) {
9981abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
9991abe55efSEd Tanous             // ethernet interfaces
10002c70f800SEd Tanous             boost::container::flat_set<std::string> ifaceList;
10012c70f800SEd Tanous             ifaceList.reserve(resp.size());
100281ce609eSEd Tanous             if (errorCode)
10031abe55efSEd Tanous             {
10042c70f800SEd Tanous                 callback(false, ifaceList);
10059391bb9cSRapkiewicz, Pawel                 return;
10069391bb9cSRapkiewicz, Pawel             }
10079391bb9cSRapkiewicz, Pawel 
10089391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
10094a0cb85cSEd Tanous             for (const auto& objpath : resp)
10101abe55efSEd Tanous             {
10119391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
10124a0cb85cSEd Tanous                 for (const auto& interface : objpath.second)
10131abe55efSEd Tanous                 {
10141abe55efSEd Tanous                     // If interface is
10154a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
10164a0cb85cSEd Tanous                     // what we're looking for.
10179391bb9cSRapkiewicz, Pawel                     if (interface.first ==
10181abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
10191abe55efSEd Tanous                     {
10202dfd18efSEd Tanous                         std::string ifaceId = objpath.first.filename();
10212dfd18efSEd Tanous                         if (ifaceId.empty())
10221abe55efSEd Tanous                         {
10232dfd18efSEd Tanous                             continue;
10249391bb9cSRapkiewicz, Pawel                         }
10252dfd18efSEd Tanous                         // and put it into output vector.
10262dfd18efSEd Tanous                         ifaceList.emplace(ifaceId);
10279391bb9cSRapkiewicz, Pawel                     }
10289391bb9cSRapkiewicz, Pawel                 }
10299391bb9cSRapkiewicz, Pawel             }
1030a434f2bdSEd Tanous             // Finally make a callback with useful data
10312c70f800SEd Tanous             callback(true, ifaceList);
10329391bb9cSRapkiewicz, Pawel         },
1033aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1034aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1035271584abSEd Tanous }
10369391bb9cSRapkiewicz, Pawel 
10374f48d5f6SEd Tanous inline void
10384f48d5f6SEd Tanous     handleHostnamePatch(const std::string& hostname,
10398d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
10401abe55efSEd Tanous {
1041ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
1042ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
1043ab6554f1SJoshi-Mansi     {
1044ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
1045ab6554f1SJoshi-Mansi                                            "HostName");
1046ab6554f1SJoshi-Mansi         return;
1047ab6554f1SJoshi-Mansi     }
1048bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
1049bc0bd6e0SEd Tanous         [asyncResp](const boost::system::error_code ec) {
10504a0cb85cSEd Tanous             if (ec)
10514a0cb85cSEd Tanous             {
1052a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
10531abe55efSEd Tanous             }
1054bc0bd6e0SEd Tanous         },
1055bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1056bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
1057bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1058abf2add6SEd Tanous         std::variant<std::string>(hostname));
1059588c3f0dSKowalski, Kamil }
1060588c3f0dSKowalski, Kamil 
10614f48d5f6SEd Tanous inline void
10624f48d5f6SEd Tanous     handleDomainnamePatch(const std::string& ifaceId,
1063bf648f77SEd Tanous                           const std::string& domainname,
10648d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1065ab6554f1SJoshi-Mansi {
1066ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
1067ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
1068ab6554f1SJoshi-Mansi         [asyncResp](const boost::system::error_code ec) {
1069ab6554f1SJoshi-Mansi             if (ec)
1070ab6554f1SJoshi-Mansi             {
1071ab6554f1SJoshi-Mansi                 messages::internalError(asyncResp->res);
1072ab6554f1SJoshi-Mansi             }
1073ab6554f1SJoshi-Mansi         },
1074ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
1075ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
1076ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
1077ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1078ab6554f1SJoshi-Mansi         std::variant<std::vector<std::string>>(vectorDomainname));
1079ab6554f1SJoshi-Mansi }
1080ab6554f1SJoshi-Mansi 
10814f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname)
1082bf648f77SEd Tanous {
1083bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
1084bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1085bf648f77SEd Tanous     {
1086bf648f77SEd Tanous         return false;
1087bf648f77SEd Tanous     }
1088bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
1089bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
1090bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
1091bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
1092bf648f77SEd Tanous     const std::regex pattern(
1093bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1094bf648f77SEd Tanous 
1095bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
1096bf648f77SEd Tanous }
1097bf648f77SEd Tanous 
10984f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname)
1099bf648f77SEd Tanous {
1100bf648f77SEd Tanous     // Can have multiple subdomains
1101bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
11020fda0f12SGeorge Liu     const std::regex pattern(
11030fda0f12SGeorge Liu         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
1104bf648f77SEd Tanous 
1105bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
1106bf648f77SEd Tanous }
1107bf648f77SEd Tanous 
11084f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
11098d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1110ab6554f1SJoshi-Mansi {
1111ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
1112ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
1113ab6554f1SJoshi-Mansi     {
1114ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1115ab6554f1SJoshi-Mansi         return;
1116ab6554f1SJoshi-Mansi     }
1117ab6554f1SJoshi-Mansi 
1118ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
1119ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
1120ab6554f1SJoshi-Mansi     {
1121ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1122ab6554f1SJoshi-Mansi         return;
1123ab6554f1SJoshi-Mansi     }
1124ab6554f1SJoshi-Mansi 
1125ab6554f1SJoshi-Mansi     std::string hostname;
1126ab6554f1SJoshi-Mansi     std::string domainname;
1127ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
1128ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
1129ab6554f1SJoshi-Mansi 
1130ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1131ab6554f1SJoshi-Mansi     {
1132ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1133ab6554f1SJoshi-Mansi         return;
1134ab6554f1SJoshi-Mansi     }
1135ab6554f1SJoshi-Mansi 
1136ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
1137ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
1138ab6554f1SJoshi-Mansi }
1139ab6554f1SJoshi-Mansi 
11404f48d5f6SEd Tanous inline void
11414f48d5f6SEd Tanous     handleMACAddressPatch(const std::string& ifaceId,
1142bf648f77SEd Tanous                           const std::string& macAddress,
11438d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1144d577665bSRatan Gupta {
1145d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
1146d577665bSRatan Gupta         [asyncResp, macAddress](const boost::system::error_code ec) {
1147d577665bSRatan Gupta             if (ec)
1148d577665bSRatan Gupta             {
1149d577665bSRatan Gupta                 messages::internalError(asyncResp->res);
1150d577665bSRatan Gupta                 return;
1151d577665bSRatan Gupta             }
1152d577665bSRatan Gupta         },
1153d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1154d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1155d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1156d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1157d577665bSRatan Gupta         std::variant<std::string>(macAddress));
1158d577665bSRatan Gupta }
1159286b9118SJohnathan Mantey 
11604f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
11614f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
11624f48d5f6SEd Tanous                            const bool v6Value,
11638d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1164da131a9aSJennifer Lee {
11652c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1166da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1167da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1168da131a9aSJennifer Lee             if (ec)
1169da131a9aSJennifer Lee             {
1170da131a9aSJennifer Lee                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1171da131a9aSJennifer Lee                 messages::internalError(asyncResp->res);
1172da131a9aSJennifer Lee                 return;
1173da131a9aSJennifer Lee             }
11748f7e9c19SJayaprakash Mutyala             messages::success(asyncResp->res);
1175da131a9aSJennifer Lee         },
1176da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1177da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1178da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1179da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
11801f8c7b5dSJohnathan Mantey         std::variant<std::string>{dhcp});
1181da131a9aSJennifer Lee }
11821f8c7b5dSJohnathan Mantey 
11834f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1184eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
11858d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1186eeedda23SJohnathan Mantey {
1187eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1188eeedda23SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1189eeedda23SJohnathan Mantey             if (ec)
1190eeedda23SJohnathan Mantey             {
1191eeedda23SJohnathan Mantey                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1192eeedda23SJohnathan Mantey                 messages::internalError(asyncResp->res);
1193eeedda23SJohnathan Mantey                 return;
1194eeedda23SJohnathan Mantey             }
1195eeedda23SJohnathan Mantey         },
1196eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1197eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1198eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1199eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1200eeedda23SJohnathan Mantey         std::variant<bool>{value});
1201eeedda23SJohnathan Mantey }
1202eeedda23SJohnathan Mantey 
12034f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
12048d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1205da131a9aSJennifer Lee {
1206da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1207da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1208da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1209da131a9aSJennifer Lee             if (ec)
1210da131a9aSJennifer Lee             {
1211da131a9aSJennifer Lee                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1212da131a9aSJennifer Lee                 messages::internalError(asyncResp->res);
1213da131a9aSJennifer Lee                 return;
1214da131a9aSJennifer Lee             }
1215da131a9aSJennifer Lee         },
1216da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1217da131a9aSJennifer Lee         "/xyz/openbmc_project/network/config/dhcp",
1218da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1219da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1220da131a9aSJennifer Lee         std::variant<bool>{value});
1221da131a9aSJennifer Lee }
1222d577665bSRatan Gupta 
12234f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
12241f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1225f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1226f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
12278d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1228da131a9aSJennifer Lee {
12291f8c7b5dSJohnathan Mantey     bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1230bf648f77SEd Tanous     bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1231da131a9aSJennifer Lee 
12321f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
12331f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
12341f8c7b5dSJohnathan Mantey 
12351f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
12361f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1237da131a9aSJennifer Lee     {
12381f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
12391f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
12401f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
12411f8c7b5dSJohnathan Mantey         {
1242bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1243bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
12441f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1245da131a9aSJennifer Lee             return;
1246da131a9aSJennifer Lee         }
12471f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
12481f8c7b5dSJohnathan Mantey     }
12491f8c7b5dSJohnathan Mantey     else
1250da131a9aSJennifer Lee     {
12511f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
12521f8c7b5dSJohnathan Mantey     }
12531f8c7b5dSJohnathan Mantey 
12541f8c7b5dSJohnathan Mantey     bool nextDNS{};
12551f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
12561f8c7b5dSJohnathan Mantey     {
12571f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
12581f8c7b5dSJohnathan Mantey         {
12591f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12601f8c7b5dSJohnathan Mantey             return;
12611f8c7b5dSJohnathan Mantey         }
12621f8c7b5dSJohnathan Mantey         nextDNS = *v4dhcpParms.useDNSServers;
12631f8c7b5dSJohnathan Mantey     }
12641f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useDNSServers)
12651f8c7b5dSJohnathan Mantey     {
12661f8c7b5dSJohnathan Mantey         nextDNS = *v4dhcpParms.useDNSServers;
12671f8c7b5dSJohnathan Mantey     }
12681f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useDNSServers)
12691f8c7b5dSJohnathan Mantey     {
12701f8c7b5dSJohnathan Mantey         nextDNS = *v6dhcpParms.useDNSServers;
12711f8c7b5dSJohnathan Mantey     }
12721f8c7b5dSJohnathan Mantey     else
12731f8c7b5dSJohnathan Mantey     {
12741f8c7b5dSJohnathan Mantey         nextDNS = ethData.DNSEnabled;
12751f8c7b5dSJohnathan Mantey     }
12761f8c7b5dSJohnathan Mantey 
12771f8c7b5dSJohnathan Mantey     bool nextNTP{};
12781f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
12791f8c7b5dSJohnathan Mantey     {
12801f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
12811f8c7b5dSJohnathan Mantey         {
12821f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12831f8c7b5dSJohnathan Mantey             return;
12841f8c7b5dSJohnathan Mantey         }
12851f8c7b5dSJohnathan Mantey         nextNTP = *v4dhcpParms.useNTPServers;
12861f8c7b5dSJohnathan Mantey     }
12871f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useNTPServers)
12881f8c7b5dSJohnathan Mantey     {
12891f8c7b5dSJohnathan Mantey         nextNTP = *v4dhcpParms.useNTPServers;
12901f8c7b5dSJohnathan Mantey     }
12911f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useNTPServers)
12921f8c7b5dSJohnathan Mantey     {
12931f8c7b5dSJohnathan Mantey         nextNTP = *v6dhcpParms.useNTPServers;
12941f8c7b5dSJohnathan Mantey     }
12951f8c7b5dSJohnathan Mantey     else
12961f8c7b5dSJohnathan Mantey     {
12971f8c7b5dSJohnathan Mantey         nextNTP = ethData.NTPEnabled;
12981f8c7b5dSJohnathan Mantey     }
12991f8c7b5dSJohnathan Mantey 
13001f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
13011f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
13021f8c7b5dSJohnathan Mantey     {
13031f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
13041f8c7b5dSJohnathan Mantey         {
13051f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
13061f8c7b5dSJohnathan Mantey             return;
13071f8c7b5dSJohnathan Mantey         }
13081f8c7b5dSJohnathan Mantey         nextUseDomain = *v4dhcpParms.useUseDomainName;
13091f8c7b5dSJohnathan Mantey     }
13101f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useUseDomainName)
13111f8c7b5dSJohnathan Mantey     {
13121f8c7b5dSJohnathan Mantey         nextUseDomain = *v4dhcpParms.useUseDomainName;
13131f8c7b5dSJohnathan Mantey     }
13141f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useUseDomainName)
13151f8c7b5dSJohnathan Mantey     {
13161f8c7b5dSJohnathan Mantey         nextUseDomain = *v6dhcpParms.useUseDomainName;
13171f8c7b5dSJohnathan Mantey     }
13181f8c7b5dSJohnathan Mantey     else
13191f8c7b5dSJohnathan Mantey     {
13201f8c7b5dSJohnathan Mantey         nextUseDomain = ethData.HostNameEnabled;
13211f8c7b5dSJohnathan Mantey     }
13221f8c7b5dSJohnathan Mantey 
1323da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
13241f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
13251f8c7b5dSJohnathan Mantey                    asyncResp);
1326da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
13271f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1328da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
13291f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
13301f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
13311f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1332da131a9aSJennifer Lee }
133301784826SJohnathan Mantey 
13344f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator
13352c70f800SEd Tanous     getNextStaticIpEntry(
1336bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1337bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
133801784826SJohnathan Mantey {
133917a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
134017a897dfSManojkiran Eda         return value.origin == "Static";
134117a897dfSManojkiran Eda     });
134201784826SJohnathan Mantey }
134301784826SJohnathan Mantey 
13444f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator
13452c70f800SEd Tanous     getNextStaticIpEntry(
1346bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1347bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
134801784826SJohnathan Mantey {
134917a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
135017a897dfSManojkiran Eda         return value.origin == "Static";
135117a897dfSManojkiran Eda     });
135201784826SJohnathan Mantey }
135301784826SJohnathan Mantey 
13544f48d5f6SEd Tanous inline void handleIPv4StaticPatch(
1355f476acbfSRatan Gupta     const std::string& ifaceId, nlohmann::json& input,
135601784826SJohnathan Mantey     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
13578d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13581abe55efSEd Tanous {
135901784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1360f476acbfSRatan Gupta     {
136171f52d96SEd Tanous         messages::propertyValueTypeError(
136271f52d96SEd Tanous             asyncResp->res,
1363bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1364d1d50814SRavi Teja             "IPv4StaticAddresses");
1365f476acbfSRatan Gupta         return;
1366f476acbfSRatan Gupta     }
1367f476acbfSRatan Gupta 
1368271584abSEd Tanous     unsigned entryIdx = 1;
136901784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
137001784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
137101784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
137201784826SJohnathan Mantey     // into the NIC.
13732c70f800SEd Tanous     boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry =
13742c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
137501784826SJohnathan Mantey 
1376537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
13771abe55efSEd Tanous     {
13784a0cb85cSEd Tanous         std::string pathString =
1379d1d50814SRavi Teja             "IPv4StaticAddresses/" + std::to_string(entryIdx);
1380179db1d7SKowalski, Kamil 
138101784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1382f476acbfSRatan Gupta         {
1383537174c4SEd Tanous             std::optional<std::string> address;
1384537174c4SEd Tanous             std::optional<std::string> subnetMask;
1385537174c4SEd Tanous             std::optional<std::string> gateway;
1386537174c4SEd Tanous 
1387537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
13887e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
13897e27d832SJohnathan Mantey                                      "Gateway", gateway))
1390537174c4SEd Tanous             {
139101784826SJohnathan Mantey                 messages::propertyValueFormatError(
139271f52d96SEd Tanous                     asyncResp->res,
139371f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
139471f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
139571f52d96SEd Tanous                     pathString);
1396537174c4SEd Tanous                 return;
1397179db1d7SKowalski, Kamil             }
1398179db1d7SKowalski, Kamil 
139901784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
140001784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
140101784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
140201784826SJohnathan Mantey             // current request.
1403271584abSEd Tanous             const std::string* addr = nullptr;
1404271584abSEd Tanous             const std::string* gw = nullptr;
140501784826SJohnathan Mantey             uint8_t prefixLength = 0;
140601784826SJohnathan Mantey             bool errorInEntry = false;
1407537174c4SEd Tanous             if (address)
14081abe55efSEd Tanous             {
140901784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*address))
14101abe55efSEd Tanous                 {
141101784826SJohnathan Mantey                     addr = &(*address);
14124a0cb85cSEd Tanous                 }
141301784826SJohnathan Mantey                 else
141401784826SJohnathan Mantey                 {
1415bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1416bf648f77SEd Tanous                                                        pathString + "/Address");
141701784826SJohnathan Mantey                     errorInEntry = true;
141801784826SJohnathan Mantey                 }
141901784826SJohnathan Mantey             }
14202c70f800SEd Tanous             else if (niciPentry != ipv4Data.cend())
142101784826SJohnathan Mantey             {
14222c70f800SEd Tanous                 addr = &(niciPentry->address);
142301784826SJohnathan Mantey             }
142401784826SJohnathan Mantey             else
142501784826SJohnathan Mantey             {
142601784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
142701784826SJohnathan Mantey                                           pathString + "/Address");
142801784826SJohnathan Mantey                 errorInEntry = true;
14294a0cb85cSEd Tanous             }
14304a0cb85cSEd Tanous 
1431537174c4SEd Tanous             if (subnetMask)
14324a0cb85cSEd Tanous             {
1433537174c4SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
14344a0cb85cSEd Tanous                 {
1435f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1436537174c4SEd Tanous                         asyncResp->res, *subnetMask,
14374a0cb85cSEd Tanous                         pathString + "/SubnetMask");
143801784826SJohnathan Mantey                     errorInEntry = true;
14394a0cb85cSEd Tanous                 }
14404a0cb85cSEd Tanous             }
14412c70f800SEd Tanous             else if (niciPentry != ipv4Data.cend())
14424a0cb85cSEd Tanous             {
14432c70f800SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask,
144401784826SJohnathan Mantey                                                 &prefixLength))
14454a0cb85cSEd Tanous                 {
144601784826SJohnathan Mantey                     messages::propertyValueFormatError(
14472c70f800SEd Tanous                         asyncResp->res, niciPentry->netmask,
144801784826SJohnathan Mantey                         pathString + "/SubnetMask");
144901784826SJohnathan Mantey                     errorInEntry = true;
14504a0cb85cSEd Tanous                 }
14514a0cb85cSEd Tanous             }
14521abe55efSEd Tanous             else
14531abe55efSEd Tanous             {
145401784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
145501784826SJohnathan Mantey                                           pathString + "/SubnetMask");
145601784826SJohnathan Mantey                 errorInEntry = true;
145701784826SJohnathan Mantey             }
145801784826SJohnathan Mantey 
145901784826SJohnathan Mantey             if (gateway)
146001784826SJohnathan Mantey             {
146101784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*gateway))
146201784826SJohnathan Mantey                 {
146301784826SJohnathan Mantey                     gw = &(*gateway);
146401784826SJohnathan Mantey                 }
146501784826SJohnathan Mantey                 else
146601784826SJohnathan Mantey                 {
1467bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1468bf648f77SEd Tanous                                                        pathString + "/Gateway");
146901784826SJohnathan Mantey                     errorInEntry = true;
147001784826SJohnathan Mantey                 }
147101784826SJohnathan Mantey             }
14722c70f800SEd Tanous             else if (niciPentry != ipv4Data.cend())
147301784826SJohnathan Mantey             {
14742c70f800SEd Tanous                 gw = &niciPentry->gateway;
147501784826SJohnathan Mantey             }
147601784826SJohnathan Mantey             else
14771abe55efSEd Tanous             {
1478a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
14794a0cb85cSEd Tanous                                           pathString + "/Gateway");
148001784826SJohnathan Mantey                 errorInEntry = true;
14814a0cb85cSEd Tanous             }
14824a0cb85cSEd Tanous 
148301784826SJohnathan Mantey             if (errorInEntry)
14841abe55efSEd Tanous             {
148501784826SJohnathan Mantey                 return;
14864a0cb85cSEd Tanous             }
14874a0cb85cSEd Tanous 
14882c70f800SEd Tanous             if (niciPentry != ipv4Data.cend())
14891abe55efSEd Tanous             {
1490bf648f77SEd Tanous                 deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength, *gw,
1491bf648f77SEd Tanous                                     *addr, asyncResp);
14922c70f800SEd Tanous                 niciPentry =
14932c70f800SEd Tanous                     getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
1494588c3f0dSKowalski, Kamil             }
149501784826SJohnathan Mantey             else
149601784826SJohnathan Mantey             {
1497cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1498cb13a392SEd Tanous                            asyncResp);
14994a0cb85cSEd Tanous             }
15004a0cb85cSEd Tanous             entryIdx++;
15014a0cb85cSEd Tanous         }
150201784826SJohnathan Mantey         else
150301784826SJohnathan Mantey         {
15042c70f800SEd Tanous             if (niciPentry == ipv4Data.cend())
150501784826SJohnathan Mantey             {
150601784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
150701784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
150801784826SJohnathan Mantey                 // in error, so bail out.
150901784826SJohnathan Mantey                 if (thisJson.is_null())
151001784826SJohnathan Mantey                 {
151101784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
151201784826SJohnathan Mantey                     return;
151301784826SJohnathan Mantey                 }
151401784826SJohnathan Mantey                 messages::propertyValueFormatError(
151571f52d96SEd Tanous                     asyncResp->res,
151671f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
151771f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
151871f52d96SEd Tanous                     pathString);
151901784826SJohnathan Mantey                 return;
152001784826SJohnathan Mantey             }
152101784826SJohnathan Mantey 
152201784826SJohnathan Mantey             if (thisJson.is_null())
152301784826SJohnathan Mantey             {
15242c70f800SEd Tanous                 deleteIPv4(ifaceId, niciPentry->id, asyncResp);
152501784826SJohnathan Mantey             }
15262c70f800SEd Tanous             if (niciPentry != ipv4Data.cend())
152701784826SJohnathan Mantey             {
15282c70f800SEd Tanous                 niciPentry =
15292c70f800SEd Tanous                     getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
153001784826SJohnathan Mantey             }
153101784826SJohnathan Mantey             entryIdx++;
153201784826SJohnathan Mantey         }
153301784826SJohnathan Mantey     }
15344a0cb85cSEd Tanous }
15354a0cb85cSEd Tanous 
15364f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1537f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1538f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
15398d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1540f85837bfSRAJESWARAN THILLAIGOVINDAN {
1541f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
1542286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1543f85837bfSRAJESWARAN THILLAIGOVINDAN             if (ec)
1544f85837bfSRAJESWARAN THILLAIGOVINDAN             {
1545f85837bfSRAJESWARAN THILLAIGOVINDAN                 messages::internalError(asyncResp->res);
1546f85837bfSRAJESWARAN THILLAIGOVINDAN                 return;
1547f85837bfSRAJESWARAN THILLAIGOVINDAN             }
1548f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1549f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1550f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1551f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1552bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1553f85837bfSRAJESWARAN THILLAIGOVINDAN         std::variant<std::vector<std::string>>{updatedStaticNameServers});
1554f85837bfSRAJESWARAN THILLAIGOVINDAN }
1555f85837bfSRAJESWARAN THILLAIGOVINDAN 
15564f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1557f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
155801784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data,
15598d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1560e48c0fc5SRavi Teja {
156101784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1562e48c0fc5SRavi Teja     {
156371f52d96SEd Tanous         messages::propertyValueTypeError(
156471f52d96SEd Tanous             asyncResp->res,
1565bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1566e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1567e48c0fc5SRavi Teja         return;
1568e48c0fc5SRavi Teja     }
1569271584abSEd Tanous     size_t entryIdx = 1;
15702c70f800SEd Tanous     boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry =
15712c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1572f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1573e48c0fc5SRavi Teja     {
1574e48c0fc5SRavi Teja         std::string pathString =
1575e48c0fc5SRavi Teja             "IPv6StaticAddresses/" + std::to_string(entryIdx);
1576e48c0fc5SRavi Teja 
157701784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1578e48c0fc5SRavi Teja         {
1579e48c0fc5SRavi Teja             std::optional<std::string> address;
1580e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1581f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1582bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1583bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1584e48c0fc5SRavi Teja             {
158501784826SJohnathan Mantey                 messages::propertyValueFormatError(
158671f52d96SEd Tanous                     asyncResp->res,
158771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
158871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
158971f52d96SEd Tanous                     pathString);
1590e48c0fc5SRavi Teja                 return;
1591e48c0fc5SRavi Teja             }
1592e48c0fc5SRavi Teja 
159301784826SJohnathan Mantey             const std::string* addr;
159401784826SJohnathan Mantey             uint8_t prefix;
159501784826SJohnathan Mantey 
159601784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
159701784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
159801784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
159901784826SJohnathan Mantey             // current request.
1600e48c0fc5SRavi Teja             if (address)
1601e48c0fc5SRavi Teja             {
160201784826SJohnathan Mantey                 addr = &(*address);
1603e48c0fc5SRavi Teja             }
16042c70f800SEd Tanous             else if (niciPentry != ipv6Data.end())
160501784826SJohnathan Mantey             {
16062c70f800SEd Tanous                 addr = &(niciPentry->address);
160701784826SJohnathan Mantey             }
160801784826SJohnathan Mantey             else
160901784826SJohnathan Mantey             {
161001784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
161101784826SJohnathan Mantey                                           pathString + "/Address");
161201784826SJohnathan Mantey                 return;
1613e48c0fc5SRavi Teja             }
1614e48c0fc5SRavi Teja 
1615e48c0fc5SRavi Teja             if (prefixLength)
1616e48c0fc5SRavi Teja             {
161701784826SJohnathan Mantey                 prefix = *prefixLength;
161801784826SJohnathan Mantey             }
16192c70f800SEd Tanous             else if (niciPentry != ipv6Data.end())
1620e48c0fc5SRavi Teja             {
16212c70f800SEd Tanous                 prefix = niciPentry->prefixLength;
1622e48c0fc5SRavi Teja             }
1623e48c0fc5SRavi Teja             else
1624e48c0fc5SRavi Teja             {
1625e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1626e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
162701784826SJohnathan Mantey                 return;
1628e48c0fc5SRavi Teja             }
1629e48c0fc5SRavi Teja 
16302c70f800SEd Tanous             if (niciPentry != ipv6Data.end())
1631e48c0fc5SRavi Teja             {
16322c70f800SEd Tanous                 deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr,
1633e48c0fc5SRavi Teja                                     asyncResp);
16342c70f800SEd Tanous                 niciPentry =
16352c70f800SEd Tanous                     getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
163601784826SJohnathan Mantey             }
163701784826SJohnathan Mantey             else
163801784826SJohnathan Mantey             {
163901784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1640e48c0fc5SRavi Teja             }
1641e48c0fc5SRavi Teja             entryIdx++;
1642e48c0fc5SRavi Teja         }
164301784826SJohnathan Mantey         else
164401784826SJohnathan Mantey         {
16452c70f800SEd Tanous             if (niciPentry == ipv6Data.end())
164601784826SJohnathan Mantey             {
164701784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
164801784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
164901784826SJohnathan Mantey                 // in error, so bail out.
165001784826SJohnathan Mantey                 if (thisJson.is_null())
165101784826SJohnathan Mantey                 {
165201784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
165301784826SJohnathan Mantey                     return;
165401784826SJohnathan Mantey                 }
165501784826SJohnathan Mantey                 messages::propertyValueFormatError(
165671f52d96SEd Tanous                     asyncResp->res,
165771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
165871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
165971f52d96SEd Tanous                     pathString);
166001784826SJohnathan Mantey                 return;
166101784826SJohnathan Mantey             }
166201784826SJohnathan Mantey 
166301784826SJohnathan Mantey             if (thisJson.is_null())
166401784826SJohnathan Mantey             {
16652c70f800SEd Tanous                 deleteIPv6(ifaceId, niciPentry->id, asyncResp);
166601784826SJohnathan Mantey             }
16672c70f800SEd Tanous             if (niciPentry != ipv6Data.cend())
166801784826SJohnathan Mantey             {
16692c70f800SEd Tanous                 niciPentry =
16702c70f800SEd Tanous                     getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
167101784826SJohnathan Mantey             }
167201784826SJohnathan Mantey             entryIdx++;
167301784826SJohnathan Mantey         }
167401784826SJohnathan Mantey     }
1675e48c0fc5SRavi Teja }
1676e48c0fc5SRavi Teja 
16774f48d5f6SEd Tanous inline void parseInterfaceData(
16788d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16798d1b46d7Szhanghch05     const std::string& ifaceId, const EthernetInterfaceData& ethData,
1680e48c0fc5SRavi Teja     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
168101784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data)
16824a0cb85cSEd Tanous {
1683eeedda23SJohnathan Mantey     constexpr const std::array<const char*, 1> inventoryForEthernet = {
1684eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1685eeedda23SJohnathan Mantey 
16862c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
168781ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
16882c70f800SEd Tanous     jsonResponse["@odata.id"] =
168981ce609eSEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
16902c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1691eeedda23SJohnathan Mantey 
1692eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1693eeedda23SJohnathan Mantey 
1694eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1695eeedda23SJohnathan Mantey         [health](const boost::system::error_code ec,
1696eeedda23SJohnathan Mantey                  std::vector<std::string>& resp) {
1697eeedda23SJohnathan Mantey             if (ec)
1698029573d4SEd Tanous             {
1699eeedda23SJohnathan Mantey                 return;
1700eeedda23SJohnathan Mantey             }
1701eeedda23SJohnathan Mantey 
1702eeedda23SJohnathan Mantey             health->inventory = std::move(resp);
1703eeedda23SJohnathan Mantey         },
1704eeedda23SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper",
1705eeedda23SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
1706bf648f77SEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1707bf648f77SEd Tanous         inventoryForEthernet);
1708eeedda23SJohnathan Mantey 
1709eeedda23SJohnathan Mantey     health->populate();
1710eeedda23SJohnathan Mantey 
1711eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1712eeedda23SJohnathan Mantey     {
17132c70f800SEd Tanous         jsonResponse["LinkStatus"] = "LinkUp";
17142c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1715029573d4SEd Tanous     }
1716029573d4SEd Tanous     else
1717029573d4SEd Tanous     {
17182c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
17192c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1720029573d4SEd Tanous     }
1721aa05fb27SJohnathan Mantey 
17222c70f800SEd Tanous     jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
17232c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
17242c70f800SEd Tanous     jsonResponse["MACAddress"] = ethData.mac_address;
17252c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
17261f8c7b5dSJohnathan Mantey         translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
17272c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
17282c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
17292c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
17301f8c7b5dSJohnathan Mantey 
17312c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
17321f8c7b5dSJohnathan Mantey         translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
17331f8c7b5dSJohnathan Mantey                                                                : "Disabled";
17342c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
17352c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
17362c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
17372a133282Smanojkiraneda 
17384a0cb85cSEd Tanous     if (!ethData.hostname.empty())
17394a0cb85cSEd Tanous     {
17402c70f800SEd Tanous         jsonResponse["HostName"] = ethData.hostname;
1741ab6554f1SJoshi-Mansi 
1742ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1743ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1744ab6554f1SJoshi-Mansi         // FQDN
1745f23b7296SEd Tanous         std::string fqdn = ethData.hostname;
1746d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1747d24bfc7aSJennifer Lee         {
17482c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1749d24bfc7aSJennifer Lee         }
17502c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
17514a0cb85cSEd Tanous     }
17524a0cb85cSEd Tanous 
17532c70f800SEd Tanous     jsonResponse["VLANs"] = {
1754bf648f77SEd Tanous         {"@odata.id",
1755bf648f77SEd Tanous          "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1756fda13ad2SSunitha Harish 
17572c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
17582c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
17594a0cb85cSEd Tanous 
17602c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
17612c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
17622c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
17632c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
17642c70f800SEd Tanous     for (auto& ipv4Config : ipv4Data)
17654a0cb85cSEd Tanous     {
1766fa5053a6SGunnar Mills 
17672c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1768fa5053a6SGunnar Mills         if (gatewayStr.empty())
1769fa5053a6SGunnar Mills         {
1770fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1771fa5053a6SGunnar Mills         }
1772fa5053a6SGunnar Mills 
17732c70f800SEd Tanous         ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
17742c70f800SEd Tanous                              {"SubnetMask", ipv4Config.netmask},
17752c70f800SEd Tanous                              {"Address", ipv4Config.address},
1776fa5053a6SGunnar Mills                              {"Gateway", gatewayStr}});
17772c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1778d1d50814SRavi Teja         {
17792c70f800SEd Tanous             ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
17802c70f800SEd Tanous                                        {"SubnetMask", ipv4Config.netmask},
17812c70f800SEd Tanous                                        {"Address", ipv4Config.address},
1782d1d50814SRavi Teja                                        {"Gateway", gatewayStr}});
1783d1d50814SRavi Teja         }
178401784826SJohnathan Mantey     }
1785d1d50814SRavi Teja 
17867ea79e5eSRavi Teja     std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
17877ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
17887ea79e5eSRavi Teja     {
17897ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
17907ea79e5eSRavi Teja     }
17917ea79e5eSRavi Teja 
17927ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1793e48c0fc5SRavi Teja 
17942c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
17952c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
17962c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
17972c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
17987f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
17992c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
18007f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
18012c70f800SEd Tanous     for (auto& ipv6Config : ipv6Data)
1802e48c0fc5SRavi Teja     {
18032c70f800SEd Tanous         ipv6Array.push_back({{"Address", ipv6Config.address},
18042c70f800SEd Tanous                              {"PrefixLength", ipv6Config.prefixLength},
18052c70f800SEd Tanous                              {"AddressOrigin", ipv6Config.origin},
18065fd16e4bSJohnathan Mantey                              {"AddressState", nullptr}});
18072c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1808e48c0fc5SRavi Teja         {
18092c70f800SEd Tanous             ipv6StaticArray.push_back(
18102c70f800SEd Tanous                 {{"Address", ipv6Config.address},
18112c70f800SEd Tanous                  {"PrefixLength", ipv6Config.prefixLength},
18122c70f800SEd Tanous                  {"AddressOrigin", ipv6Config.origin},
18135fd16e4bSJohnathan Mantey                  {"AddressState", nullptr}});
181401784826SJohnathan Mantey         }
1815e48c0fc5SRavi Teja     }
1816588c3f0dSKowalski, Kamil }
1817588c3f0dSKowalski, Kamil 
18184f48d5f6SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse,
1819bf648f77SEd Tanous                                const std::string& parentIfaceId,
1820bf648f77SEd Tanous                                const std::string& ifaceId,
1821bf648f77SEd Tanous                                const EthernetInterfaceData& ethData)
18221abe55efSEd Tanous {
1823bf648f77SEd Tanous     // Fill out obvious data...
1824bf648f77SEd Tanous     jsonResponse["Id"] = ifaceId;
1825bf648f77SEd Tanous     jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1826bf648f77SEd Tanous                                 parentIfaceId + "/VLANs/" + ifaceId;
1827bf648f77SEd Tanous 
1828bf648f77SEd Tanous     jsonResponse["VLANEnable"] = true;
1829bf648f77SEd Tanous     if (!ethData.vlan_id.empty())
1830bf648f77SEd Tanous     {
1831bf648f77SEd Tanous         jsonResponse["VLANId"] = ethData.vlan_id.back();
1832bf648f77SEd Tanous     }
1833bf648f77SEd Tanous }
1834bf648f77SEd Tanous 
18354f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1836bf648f77SEd Tanous {
1837bf648f77SEd Tanous     if (!boost::starts_with(iface, parent + "_"))
1838bf648f77SEd Tanous     {
1839bf648f77SEd Tanous         return false;
1840bf648f77SEd Tanous     }
1841bf648f77SEd Tanous     return true;
1842bf648f77SEd Tanous }
1843bf648f77SEd Tanous 
1844bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1845bf648f77SEd Tanous {
1846bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1847ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
1848bf648f77SEd Tanous         .methods(
1849bf648f77SEd Tanous             boost::beast::http::verb::
1850bf648f77SEd Tanous                 get)([](const crow::Request&,
1851bf648f77SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1852bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1853bf648f77SEd Tanous                 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1854bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1855bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1856bf648f77SEd Tanous             asyncResp->res.jsonValue["Name"] =
1857bf648f77SEd Tanous                 "Ethernet Network Interface Collection";
1858bf648f77SEd Tanous             asyncResp->res.jsonValue["Description"] =
1859bf648f77SEd Tanous                 "Collection of EthernetInterfaces for this Manager";
1860bf648f77SEd Tanous 
1861bf648f77SEd Tanous             // Get eth interface list, and call the below callback for JSON
1862bf648f77SEd Tanous             // preparation
1863bf648f77SEd Tanous             getEthernetIfaceList([asyncResp](const bool& success,
1864bf648f77SEd Tanous                                              const boost::container::flat_set<
1865bf648f77SEd Tanous                                                  std::string>& ifaceList) {
1866bf648f77SEd Tanous                 if (!success)
18671abe55efSEd Tanous                 {
1868f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
18699391bb9cSRapkiewicz, Pawel                     return;
18709391bb9cSRapkiewicz, Pawel                 }
18719391bb9cSRapkiewicz, Pawel 
1872bf648f77SEd Tanous                 nlohmann::json& ifaceArray =
1873bf648f77SEd Tanous                     asyncResp->res.jsonValue["Members"];
1874bf648f77SEd Tanous                 ifaceArray = nlohmann::json::array();
1875bf648f77SEd Tanous                 std::string tag = "_";
1876bf648f77SEd Tanous                 for (const std::string& ifaceItem : ifaceList)
1877bf648f77SEd Tanous                 {
1878bf648f77SEd Tanous                     std::size_t found = ifaceItem.find(tag);
1879bf648f77SEd Tanous                     if (found == std::string::npos)
1880bf648f77SEd Tanous                     {
1881bf648f77SEd Tanous                         ifaceArray.push_back(
1882bf648f77SEd Tanous                             {{"@odata.id",
1883bf648f77SEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1884bf648f77SEd Tanous                                   ifaceItem}});
1885bf648f77SEd Tanous                     }
1886bf648f77SEd Tanous                 }
1887bf648f77SEd Tanous 
1888bf648f77SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
1889bf648f77SEd Tanous                     ifaceArray.size();
1890bf648f77SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
1891bf648f77SEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
1892bf648f77SEd Tanous             });
1893bf648f77SEd Tanous         });
1894bf648f77SEd Tanous 
1895bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1896ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1897bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
1898bf648f77SEd Tanous             [](const crow::Request&,
1899bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1900bf648f77SEd Tanous                const std::string& ifaceId) {
19014a0cb85cSEd Tanous                 getEthernetIfaceData(
1902bf648f77SEd Tanous                     ifaceId,
1903bf648f77SEd Tanous                     [asyncResp,
1904bf648f77SEd Tanous                      ifaceId](const bool& success,
1905bf648f77SEd Tanous                               const EthernetInterfaceData& ethData,
1906bf648f77SEd Tanous                               const boost::container::flat_set<IPv4AddressData>&
1907bf648f77SEd Tanous                                   ipv4Data,
1908bf648f77SEd Tanous                               const boost::container::flat_set<IPv6AddressData>&
1909bf648f77SEd Tanous                                   ipv6Data) {
19104a0cb85cSEd Tanous                         if (!success)
19111abe55efSEd Tanous                         {
1912bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
1913bf648f77SEd Tanous                             // existing object, and other errors
1914bf648f77SEd Tanous                             messages::resourceNotFound(
1915bf648f77SEd Tanous                                 asyncResp->res, "EthernetInterface", ifaceId);
19164a0cb85cSEd Tanous                             return;
19179391bb9cSRapkiewicz, Pawel                         }
19184c9afe43SEd Tanous 
19190f74e643SEd Tanous                         asyncResp->res.jsonValue["@odata.type"] =
1920fda13ad2SSunitha Harish                             "#EthernetInterface.v1_4_1.EthernetInterface";
1921bf648f77SEd Tanous                         asyncResp->res.jsonValue["Name"] =
1922bf648f77SEd Tanous                             "Manager Ethernet Interface";
19230f74e643SEd Tanous                         asyncResp->res.jsonValue["Description"] =
19240f74e643SEd Tanous                             "Management Network Interface";
19250f74e643SEd Tanous 
1926bf648f77SEd Tanous                         parseInterfaceData(asyncResp, ifaceId, ethData,
1927bf648f77SEd Tanous                                            ipv4Data, ipv6Data);
19289391bb9cSRapkiewicz, Pawel                     });
1929bf648f77SEd Tanous             });
19309391bb9cSRapkiewicz, Pawel 
1931bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1932ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1933ed398213SEd Tanous 
1934bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
1935bf648f77SEd Tanous             [](const crow::Request& req,
1936bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1937bf648f77SEd Tanous                const std::string& ifaceId) {
1938bc0bd6e0SEd Tanous                 std::optional<std::string> hostname;
1939ab6554f1SJoshi-Mansi                 std::optional<std::string> fqdn;
1940d577665bSRatan Gupta                 std::optional<std::string> macAddress;
19419a6fc6feSRavi Teja                 std::optional<std::string> ipv6DefaultGateway;
1942d1d50814SRavi Teja                 std::optional<nlohmann::json> ipv4StaticAddresses;
1943e48c0fc5SRavi Teja                 std::optional<nlohmann::json> ipv6StaticAddresses;
1944f85837bfSRAJESWARAN THILLAIGOVINDAN                 std::optional<std::vector<std::string>> staticNameServers;
1945da131a9aSJennifer Lee                 std::optional<nlohmann::json> dhcpv4;
19461f8c7b5dSJohnathan Mantey                 std::optional<nlohmann::json> dhcpv6;
1947eeedda23SJohnathan Mantey                 std::optional<bool> interfaceEnabled;
19481f8c7b5dSJohnathan Mantey                 DHCPParameters v4dhcpParms;
19491f8c7b5dSJohnathan Mantey                 DHCPParameters v6dhcpParms;
19500627a2c7SEd Tanous 
19511f8c7b5dSJohnathan Mantey                 if (!json_util::readJson(
19528d1b46d7Szhanghch05                         req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1953bf648f77SEd Tanous                         "IPv4StaticAddresses", ipv4StaticAddresses,
1954bf648f77SEd Tanous                         "MACAddress", macAddress, "StaticNameServers",
1955bf648f77SEd Tanous                         staticNameServers, "IPv6DefaultGateway",
1956bf648f77SEd Tanous                         ipv6DefaultGateway, "IPv6StaticAddresses",
1957ab6554f1SJoshi-Mansi                         ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1958ab6554f1SJoshi-Mansi                         "InterfaceEnabled", interfaceEnabled))
19591abe55efSEd Tanous                 {
1960588c3f0dSKowalski, Kamil                     return;
1961588c3f0dSKowalski, Kamil                 }
1962da131a9aSJennifer Lee                 if (dhcpv4)
1963da131a9aSJennifer Lee                 {
1964bf648f77SEd Tanous                     if (!json_util::readJson(
1965bf648f77SEd Tanous                             *dhcpv4, asyncResp->res, "DHCPEnabled",
19661f8c7b5dSJohnathan Mantey                             v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
19671f8c7b5dSJohnathan Mantey                             v4dhcpParms.useDNSServers, "UseNTPServers",
19681f8c7b5dSJohnathan Mantey                             v4dhcpParms.useNTPServers, "UseDomainName",
19691f8c7b5dSJohnathan Mantey                             v4dhcpParms.useUseDomainName))
19701f8c7b5dSJohnathan Mantey                     {
19711f8c7b5dSJohnathan Mantey                         return;
19721f8c7b5dSJohnathan Mantey                     }
19731f8c7b5dSJohnathan Mantey                 }
19741f8c7b5dSJohnathan Mantey 
19751f8c7b5dSJohnathan Mantey                 if (dhcpv6)
19761f8c7b5dSJohnathan Mantey                 {
1977bf648f77SEd Tanous                     if (!json_util::readJson(
1978bf648f77SEd Tanous                             *dhcpv6, asyncResp->res, "OperatingMode",
1979bf648f77SEd Tanous                             v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers",
1980bf648f77SEd Tanous                             v6dhcpParms.useDNSServers, "UseNTPServers",
1981bf648f77SEd Tanous                             v6dhcpParms.useNTPServers, "UseDomainName",
19821f8c7b5dSJohnathan Mantey                             v6dhcpParms.useUseDomainName))
19831f8c7b5dSJohnathan Mantey                     {
19841f8c7b5dSJohnathan Mantey                         return;
19851f8c7b5dSJohnathan Mantey                     }
1986da131a9aSJennifer Lee                 }
1987da131a9aSJennifer Lee 
1988bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
1989bf648f77SEd Tanous                 // for JSON preparation
19904a0cb85cSEd Tanous                 getEthernetIfaceData(
19912c70f800SEd Tanous                     ifaceId,
1992bf648f77SEd Tanous                     [asyncResp, ifaceId, hostname = std::move(hostname),
1993ab6554f1SJoshi-Mansi                      fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1994d1d50814SRavi Teja                      ipv4StaticAddresses = std::move(ipv4StaticAddresses),
19959a6fc6feSRavi Teja                      ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1996e48c0fc5SRavi Teja                      ipv6StaticAddresses = std::move(ipv6StaticAddresses),
19971f8c7b5dSJohnathan Mantey                      staticNameServers = std::move(staticNameServers),
19981f8c7b5dSJohnathan Mantey                      dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
19991f8c7b5dSJohnathan Mantey                      v4dhcpParms = std::move(v4dhcpParms),
2000f23b7296SEd Tanous                      v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2001bf648f77SEd Tanous                         const bool& success,
2002bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2003bf648f77SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&
2004bf648f77SEd Tanous                             ipv4Data,
2005bf648f77SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&
2006bf648f77SEd Tanous                             ipv6Data) {
20071abe55efSEd Tanous                         if (!success)
20081abe55efSEd Tanous                         {
2009588c3f0dSKowalski, Kamil                             // ... otherwise return error
2010bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2011bf648f77SEd Tanous                             // existing object, and other errors
2012bf648f77SEd Tanous                             messages::resourceNotFound(
2013bf648f77SEd Tanous                                 asyncResp->res, "Ethernet Interface", ifaceId);
2014588c3f0dSKowalski, Kamil                             return;
2015588c3f0dSKowalski, Kamil                         }
2016588c3f0dSKowalski, Kamil 
20171f8c7b5dSJohnathan Mantey                         if (dhcpv4 || dhcpv6)
20181f8c7b5dSJohnathan Mantey                         {
2019bf648f77SEd Tanous                             handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2020bf648f77SEd Tanous                                             v6dhcpParms, asyncResp);
20211f8c7b5dSJohnathan Mantey                         }
20221f8c7b5dSJohnathan Mantey 
20230627a2c7SEd Tanous                         if (hostname)
20241abe55efSEd Tanous                         {
20250627a2c7SEd Tanous                             handleHostnamePatch(*hostname, asyncResp);
20261abe55efSEd Tanous                         }
20270627a2c7SEd Tanous 
2028ab6554f1SJoshi-Mansi                         if (fqdn)
2029ab6554f1SJoshi-Mansi                         {
20302c70f800SEd Tanous                             handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2031ab6554f1SJoshi-Mansi                         }
2032ab6554f1SJoshi-Mansi 
2033d577665bSRatan Gupta                         if (macAddress)
2034d577665bSRatan Gupta                         {
2035bf648f77SEd Tanous                             handleMACAddressPatch(ifaceId, *macAddress,
2036bf648f77SEd Tanous                                                   asyncResp);
2037d577665bSRatan Gupta                         }
2038d577665bSRatan Gupta 
2039d1d50814SRavi Teja                         if (ipv4StaticAddresses)
2040d1d50814SRavi Teja                         {
2041bf648f77SEd Tanous                             // TODO(ed) for some reason the capture of
2042bf648f77SEd Tanous                             // ipv4Addresses above is returning a const value,
2043bf648f77SEd Tanous                             // not a non-const value. This doesn't really work
2044bf648f77SEd Tanous                             // for us, as we need to be able to efficiently move
2045bf648f77SEd Tanous                             // out the intermedia nlohmann::json objects. This
2046bf648f77SEd Tanous                             // makes a copy of the structure, and operates on
2047bf648f77SEd Tanous                             // that, but could be done more efficiently
2048f23b7296SEd Tanous                             nlohmann::json ipv4Static = *ipv4StaticAddresses;
20492c70f800SEd Tanous                             handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2050d1d50814SRavi Teja                                                   asyncResp);
20511abe55efSEd Tanous                         }
20520627a2c7SEd Tanous 
2053f85837bfSRAJESWARAN THILLAIGOVINDAN                         if (staticNameServers)
2054f85837bfSRAJESWARAN THILLAIGOVINDAN                         {
2055bf648f77SEd Tanous                             handleStaticNameServersPatch(
2056bf648f77SEd Tanous                                 ifaceId, *staticNameServers, asyncResp);
2057f85837bfSRAJESWARAN THILLAIGOVINDAN                         }
20589a6fc6feSRavi Teja 
20599a6fc6feSRavi Teja                         if (ipv6DefaultGateway)
20609a6fc6feSRavi Teja                         {
20619a6fc6feSRavi Teja                             messages::propertyNotWritable(asyncResp->res,
20629a6fc6feSRavi Teja                                                           "IPv6DefaultGateway");
20639a6fc6feSRavi Teja                         }
2064e48c0fc5SRavi Teja 
2065e48c0fc5SRavi Teja                         if (ipv6StaticAddresses)
2066e48c0fc5SRavi Teja                         {
2067f23b7296SEd Tanous                             nlohmann::json ipv6Static = *ipv6StaticAddresses;
20682c70f800SEd Tanous                             handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
206901784826SJohnathan Mantey                                                            ipv6Data, asyncResp);
2070e48c0fc5SRavi Teja                         }
2071eeedda23SJohnathan Mantey 
2072eeedda23SJohnathan Mantey                         if (interfaceEnabled)
2073eeedda23SJohnathan Mantey                         {
2074eeedda23SJohnathan Mantey                             setEthernetInterfaceBoolProperty(
2075bf648f77SEd Tanous                                 ifaceId, "NICEnabled", *interfaceEnabled,
2076bf648f77SEd Tanous                                 asyncResp);
2077eeedda23SJohnathan Mantey                         }
2078588c3f0dSKowalski, Kamil                     });
2079bf648f77SEd Tanous             });
20809391bb9cSRapkiewicz, Pawel 
2081bf648f77SEd Tanous     BMCWEB_ROUTE(
2082bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2083ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
2084ed398213SEd Tanous 
2085bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
2086bf648f77SEd Tanous             [](const crow::Request& /* req */,
2087bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2088bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
20898d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
20900f74e643SEd Tanous                     "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
20918d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
2092e439f0f8SKowalski, Kamil 
20932c70f800SEd Tanous                 if (!verifyNames(parentIfaceId, ifaceId))
20941abe55efSEd Tanous                 {
2095a434f2bdSEd Tanous                     return;
2096a434f2bdSEd Tanous                 }
2097a434f2bdSEd Tanous 
2098bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2099bf648f77SEd Tanous                 // for JSON preparation
21004a0cb85cSEd Tanous                 getEthernetIfaceData(
2101bf648f77SEd Tanous                     ifaceId,
2102bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId](
2103bf648f77SEd Tanous                         const bool& success,
2104bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2105cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2106cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
2107fda13ad2SSunitha Harish                         if (success && ethData.vlan_id.size() != 0)
21081abe55efSEd Tanous                         {
2109bf648f77SEd Tanous                             parseInterfaceData(asyncResp->res.jsonValue,
2110bf648f77SEd Tanous                                                parentIfaceId, ifaceId, ethData);
21111abe55efSEd Tanous                         }
21121abe55efSEd Tanous                         else
21131abe55efSEd Tanous                         {
2114e439f0f8SKowalski, Kamil                             // ... otherwise return error
2115bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2116bf648f77SEd Tanous                             // existing object, and other errors
2117bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2118bf648f77SEd Tanous                                                        "VLAN Network Interface",
2119bf648f77SEd Tanous                                                        ifaceId);
2120e439f0f8SKowalski, Kamil                         }
2121e439f0f8SKowalski, Kamil                     });
2122bf648f77SEd Tanous             });
2123e439f0f8SKowalski, Kamil 
2124bf648f77SEd Tanous     BMCWEB_ROUTE(
2125bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2126ed398213SEd Tanous         // This privilege is incorrect, it should be ConfigureManager
2127ed398213SEd Tanous         //.privileges(redfish::privileges::patchVLanNetworkInterface)
2128432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2129bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
2130bf648f77SEd Tanous             [](const crow::Request& req,
2131bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2132bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
2133fda13ad2SSunitha Harish                 if (!verifyNames(parentIfaceId, ifaceId))
21341abe55efSEd Tanous                 {
2135bf648f77SEd Tanous                     messages::resourceNotFound(
2136bf648f77SEd Tanous                         asyncResp->res, "VLAN Network Interface", ifaceId);
2137927a505aSKowalski, Kamil                     return;
2138927a505aSKowalski, Kamil                 }
2139927a505aSKowalski, Kamil 
21400627a2c7SEd Tanous                 bool vlanEnable = false;
214138268fa8SAndrew Geissler                 uint32_t vlanId = 0;
21420627a2c7SEd Tanous 
2143bf648f77SEd Tanous                 if (!json_util::readJson(req, asyncResp->res, "VLANEnable",
2144bf648f77SEd Tanous                                          vlanEnable, "VLANId", vlanId))
21451abe55efSEd Tanous                 {
2146927a505aSKowalski, Kamil                     return;
2147927a505aSKowalski, Kamil                 }
2148927a505aSKowalski, Kamil 
2149bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2150bf648f77SEd Tanous                 // for JSON preparation
2151e48c0fc5SRavi Teja                 getEthernetIfaceData(
2152bf648f77SEd Tanous                     ifaceId,
2153bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId](
2154bf648f77SEd Tanous                         const bool& success,
2155bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2156cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2157cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
215808244d02SSunitha Harish                         if (success && !ethData.vlan_id.empty())
215908244d02SSunitha Harish                         {
216008244d02SSunitha Harish                             auto callback =
2161bf648f77SEd Tanous                                 [asyncResp](
2162bf648f77SEd Tanous                                     const boost::system::error_code ec) {
216308244d02SSunitha Harish                                     if (ec)
216408244d02SSunitha Harish                                     {
216508244d02SSunitha Harish                                         messages::internalError(asyncResp->res);
216608244d02SSunitha Harish                                     }
216708244d02SSunitha Harish                                 };
216808244d02SSunitha Harish 
216908244d02SSunitha Harish                             if (vlanEnable == true)
217008244d02SSunitha Harish                             {
217108244d02SSunitha Harish                                 crow::connections::systemBus->async_method_call(
2172bf648f77SEd Tanous                                     std::move(callback),
2173bf648f77SEd Tanous                                     "xyz.openbmc_project.Network",
217408244d02SSunitha Harish                                     "/xyz/openbmc_project/network/" + ifaceId,
217508244d02SSunitha Harish                                     "org.freedesktop.DBus.Properties", "Set",
217608244d02SSunitha Harish                                     "xyz.openbmc_project.Network.VLAN", "Id",
217708244d02SSunitha Harish                                     std::variant<uint32_t>(vlanId));
217808244d02SSunitha Harish                             }
217908244d02SSunitha Harish                             else
218008244d02SSunitha Harish                             {
2181bf648f77SEd Tanous                                 BMCWEB_LOG_DEBUG
2182bf648f77SEd Tanous                                     << "vlanEnable is false. Deleting the "
2183e48c0fc5SRavi Teja                                        "vlan interface";
218408244d02SSunitha Harish                                 crow::connections::systemBus->async_method_call(
2185bf648f77SEd Tanous                                     std::move(callback),
2186bf648f77SEd Tanous                                     "xyz.openbmc_project.Network",
2187bf648f77SEd Tanous                                     std::string(
2188bf648f77SEd Tanous                                         "/xyz/openbmc_project/network/") +
2189e48c0fc5SRavi Teja                                         ifaceId,
2190bf648f77SEd Tanous                                     "xyz.openbmc_project.Object.Delete",
2191bf648f77SEd Tanous                                     "Delete");
219208244d02SSunitha Harish                             }
219308244d02SSunitha Harish                         }
219408244d02SSunitha Harish                         else
21951abe55efSEd Tanous                         {
2196bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2197bf648f77SEd Tanous                             // existing object, and other errors
2198bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2199bf648f77SEd Tanous                                                        "VLAN Network Interface",
2200bf648f77SEd Tanous                                                        ifaceId);
2201bf648f77SEd Tanous                             return;
2202bf648f77SEd Tanous                         }
2203bf648f77SEd Tanous                     });
2204bf648f77SEd Tanous             });
2205bf648f77SEd Tanous 
2206bf648f77SEd Tanous     BMCWEB_ROUTE(
2207bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2208ed398213SEd Tanous         // This privilege is incorrect, it should be ConfigureManager
2209ed398213SEd Tanous         //.privileges(redfish::privileges::deleteVLanNetworkInterface)
2210432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2211bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
2212bf648f77SEd Tanous             [](const crow::Request& /* req */,
2213bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2214bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
2215bf648f77SEd Tanous                 if (!verifyNames(parentIfaceId, ifaceId))
2216bf648f77SEd Tanous                 {
2217e48c0fc5SRavi Teja                     messages::resourceNotFound(
2218e48c0fc5SRavi Teja                         asyncResp->res, "VLAN Network Interface", ifaceId);
2219927a505aSKowalski, Kamil                     return;
2220927a505aSKowalski, Kamil                 }
2221e439f0f8SKowalski, Kamil 
2222bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2223bf648f77SEd Tanous                 // for JSON preparation
2224f12894f8SJason M. Bills                 getEthernetIfaceData(
2225bf648f77SEd Tanous                     ifaceId,
2226bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId](
2227bf648f77SEd Tanous                         const bool& success,
2228bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2229cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2230cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
2231fda13ad2SSunitha Harish                         if (success && !ethData.vlan_id.empty())
22321abe55efSEd Tanous                         {
2233f12894f8SJason M. Bills                             auto callback =
2234bf648f77SEd Tanous                                 [asyncResp](
2235bf648f77SEd Tanous                                     const boost::system::error_code ec) {
22361abe55efSEd Tanous                                     if (ec)
22371abe55efSEd Tanous                                     {
2238f12894f8SJason M. Bills                                         messages::internalError(asyncResp->res);
2239927a505aSKowalski, Kamil                                     }
22404a0cb85cSEd Tanous                                 };
22414a0cb85cSEd Tanous                             crow::connections::systemBus->async_method_call(
2242bf648f77SEd Tanous                                 std::move(callback),
2243bf648f77SEd Tanous                                 "xyz.openbmc_project.Network",
2244bf648f77SEd Tanous                                 std::string("/xyz/openbmc_project/network/") +
2245bf648f77SEd Tanous                                     ifaceId,
22464a0cb85cSEd Tanous                                 "xyz.openbmc_project.Object.Delete", "Delete");
22471abe55efSEd Tanous                         }
22481abe55efSEd Tanous                         else
22491abe55efSEd Tanous                         {
2250927a505aSKowalski, Kamil                             // ... otherwise return error
2251bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2252bf648f77SEd Tanous                             // existing object, and other errors
2253bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2254bf648f77SEd Tanous                                                        "VLAN Network Interface",
2255bf648f77SEd Tanous                                                        ifaceId);
2256927a505aSKowalski, Kamil                         }
2257927a505aSKowalski, Kamil                     });
2258bf648f77SEd Tanous             });
2259e439f0f8SKowalski, Kamil 
2260bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2261bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2262ed398213SEd Tanous 
2263ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
2264bf648f77SEd Tanous         .methods(
2265bf648f77SEd Tanous             boost::beast::http::verb::
2266bf648f77SEd Tanous                 get)([](const crow::Request& /* req */,
2267bf648f77SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2268bf648f77SEd Tanous                         const std::string& rootInterfaceName) {
22694a0cb85cSEd Tanous             // Get eth interface list, and call the below callback for JSON
22701abe55efSEd Tanous             // preparation
2271bf648f77SEd Tanous             getEthernetIfaceList([asyncResp, rootInterfaceName](
22721abe55efSEd Tanous                                      const bool& success,
2273bf648f77SEd Tanous                                      const boost::container::flat_set<
2274bf648f77SEd Tanous                                          std::string>& ifaceList) {
22754a0cb85cSEd Tanous                 if (!success)
22761abe55efSEd Tanous                 {
2277f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
22784a0cb85cSEd Tanous                     return;
22791abe55efSEd Tanous                 }
22804c9afe43SEd Tanous 
228181ce609eSEd Tanous                 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
22824c9afe43SEd Tanous                 {
22834c9afe43SEd Tanous                     messages::resourceNotFound(asyncResp->res,
22844c9afe43SEd Tanous                                                "VLanNetworkInterfaceCollection",
22854c9afe43SEd Tanous                                                rootInterfaceName);
22864c9afe43SEd Tanous                     return;
22874c9afe43SEd Tanous                 }
22884c9afe43SEd Tanous 
22890f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
22900f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
22910f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
22920f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] =
22930f74e643SEd Tanous                     "VLAN Network Interface Collection";
22944a0cb85cSEd Tanous 
22952c70f800SEd Tanous                 nlohmann::json ifaceArray = nlohmann::json::array();
22964a0cb85cSEd Tanous 
229781ce609eSEd Tanous                 for (const std::string& ifaceItem : ifaceList)
22981abe55efSEd Tanous                 {
22992c70f800SEd Tanous                     if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
23004a0cb85cSEd Tanous                     {
2301f23b7296SEd Tanous                         std::string path =
2302f23b7296SEd Tanous                             "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2303f23b7296SEd Tanous                         path += rootInterfaceName;
2304f23b7296SEd Tanous                         path += "/VLANs/";
2305f23b7296SEd Tanous                         path += ifaceItem;
2306f23b7296SEd Tanous                         ifaceArray.push_back({{"@odata.id", std::move(path)}});
2307e439f0f8SKowalski, Kamil                     }
2308e439f0f8SKowalski, Kamil                 }
2309e439f0f8SKowalski, Kamil 
23104a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
23112c70f800SEd Tanous                     ifaceArray.size();
23122c70f800SEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
23134a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
23144a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
23154a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
2316e439f0f8SKowalski, Kamil             });
2317bf648f77SEd Tanous         });
2318e439f0f8SKowalski, Kamil 
2319bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2320bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2321ed398213SEd Tanous         // This privilege is wrong, it should be ConfigureManager
2322ed398213SEd Tanous         //.privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2323432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2324bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
2325bf648f77SEd Tanous             [](const crow::Request& req,
2326bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2327bf648f77SEd Tanous                const std::string& rootInterfaceName) {
2328fda13ad2SSunitha Harish                 bool vlanEnable = false;
23290627a2c7SEd Tanous                 uint32_t vlanId = 0;
23308d1b46d7Szhanghch05                 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId,
23318d1b46d7Szhanghch05                                          "VLANEnable", vlanEnable))
23321abe55efSEd Tanous                 {
23334a0cb85cSEd Tanous                     return;
2334e439f0f8SKowalski, Kamil                 }
2335fda13ad2SSunitha Harish                 // Need both vlanId and vlanEnable to service this request
2336fda13ad2SSunitha Harish                 if (!vlanId)
2337fda13ad2SSunitha Harish                 {
2338fda13ad2SSunitha Harish                     messages::propertyMissing(asyncResp->res, "VLANId");
2339fda13ad2SSunitha Harish                 }
2340fda13ad2SSunitha Harish                 if (!vlanEnable)
2341fda13ad2SSunitha Harish                 {
2342fda13ad2SSunitha Harish                     messages::propertyMissing(asyncResp->res, "VLANEnable");
2343fda13ad2SSunitha Harish                 }
2344271584abSEd Tanous                 if (static_cast<bool>(vlanId) ^ vlanEnable)
2345fda13ad2SSunitha Harish                 {
2346fda13ad2SSunitha Harish                     return;
2347fda13ad2SSunitha Harish                 }
2348fda13ad2SSunitha Harish 
2349bf648f77SEd Tanous                 auto callback =
2350bf648f77SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
23511abe55efSEd Tanous                         if (ec)
23521abe55efSEd Tanous                         {
2353bf648f77SEd Tanous                             // TODO(ed) make more consistent error messages
2354bf648f77SEd Tanous                             // based on phosphor-network responses
2355f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
23564a0cb85cSEd Tanous                             return;
23571abe55efSEd Tanous                         }
2358f12894f8SJason M. Bills                         messages::created(asyncResp->res);
2359e439f0f8SKowalski, Kamil                     };
23604a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
23614a0cb85cSEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
23624a0cb85cSEd Tanous                     "/xyz/openbmc_project/network",
23634a0cb85cSEd Tanous                     "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
23640627a2c7SEd Tanous                     rootInterfaceName, vlanId);
2365bf648f77SEd Tanous             });
23664a0cb85cSEd Tanous }
2367bf648f77SEd Tanous 
23689391bb9cSRapkiewicz, Pawel } // namespace redfish
2369