xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 45ca1b868e47978a4d2e8ebb680cb384e804c97e)
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>
194a0cb85cSEd Tanous #include <boost/container/flat_set.hpp>
20179db1d7SKowalski, Kamil #include <dbus_singleton.hpp>
21168e20c1SEd Tanous #include <dbus_utility.hpp>
22588c3f0dSKowalski, Kamil #include <error_messages.hpp>
23*45ca1b86SEd Tanous #include <query.hpp>
24ed398213SEd Tanous #include <registries/privilege_registry.hpp>
251214b7e7SGunnar Mills #include <utils/json_utils.hpp>
261214b7e7SGunnar Mills 
27a24526dcSEd Tanous #include <optional>
28ab6554f1SJoshi-Mansi #include <regex>
299391bb9cSRapkiewicz, Pawel 
301abe55efSEd Tanous namespace redfish
311abe55efSEd Tanous {
329391bb9cSRapkiewicz, Pawel 
334a0cb85cSEd Tanous enum class LinkType
344a0cb85cSEd Tanous {
354a0cb85cSEd Tanous     Local,
364a0cb85cSEd Tanous     Global
374a0cb85cSEd Tanous };
389391bb9cSRapkiewicz, Pawel 
399391bb9cSRapkiewicz, Pawel /**
409391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
419391bb9cSRapkiewicz, Pawel  */
421abe55efSEd Tanous struct IPv4AddressData
431abe55efSEd Tanous {
44179db1d7SKowalski, Kamil     std::string id;
454a0cb85cSEd Tanous     std::string address;
464a0cb85cSEd Tanous     std::string domain;
474a0cb85cSEd Tanous     std::string gateway;
489391bb9cSRapkiewicz, Pawel     std::string netmask;
499391bb9cSRapkiewicz, Pawel     std::string origin;
504a0cb85cSEd Tanous     LinkType linktype;
5101c6e858SSunitha Harish     bool isActive;
524a0cb85cSEd Tanous 
531abe55efSEd Tanous     bool operator<(const IPv4AddressData& obj) const
541abe55efSEd Tanous     {
554a0cb85cSEd Tanous         return id < obj.id;
561abe55efSEd Tanous     }
579391bb9cSRapkiewicz, Pawel };
589391bb9cSRapkiewicz, Pawel 
599391bb9cSRapkiewicz, Pawel /**
60e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
61e48c0fc5SRavi Teja  */
62e48c0fc5SRavi Teja struct IPv6AddressData
63e48c0fc5SRavi Teja {
64e48c0fc5SRavi Teja     std::string id;
65e48c0fc5SRavi Teja     std::string address;
66e48c0fc5SRavi Teja     std::string origin;
67e48c0fc5SRavi Teja     uint8_t prefixLength;
68e48c0fc5SRavi Teja 
69e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData& obj) const
70e48c0fc5SRavi Teja     {
71e48c0fc5SRavi Teja         return id < obj.id;
72e48c0fc5SRavi Teja     }
73e48c0fc5SRavi Teja };
74e48c0fc5SRavi Teja /**
759391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
769391bb9cSRapkiewicz, Pawel  * available from DBus
779391bb9cSRapkiewicz, Pawel  */
781abe55efSEd Tanous struct EthernetInterfaceData
791abe55efSEd Tanous {
804a0cb85cSEd Tanous     uint32_t speed;
8135fb5311STejas Patil     size_t mtuSize;
824a0cb85cSEd Tanous     bool auto_neg;
831f8c7b5dSJohnathan Mantey     bool DNSEnabled;
841f8c7b5dSJohnathan Mantey     bool NTPEnabled;
851f8c7b5dSJohnathan Mantey     bool HostNameEnabled;
861f8c7b5dSJohnathan Mantey     bool SendHostNameEnabled;
87aa05fb27SJohnathan Mantey     bool linkUp;
88eeedda23SJohnathan Mantey     bool nicEnabled;
891f8c7b5dSJohnathan Mantey     std::string DHCPEnabled;
901f8c7b5dSJohnathan Mantey     std::string operatingMode;
914a0cb85cSEd Tanous     std::string hostname;
924a0cb85cSEd Tanous     std::string default_gateway;
939a6fc6feSRavi Teja     std::string ipv6_default_gateway;
944a0cb85cSEd Tanous     std::string mac_address;
95fda13ad2SSunitha Harish     std::vector<std::uint32_t> vlan_id;
960f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
970f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
98d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
999391bb9cSRapkiewicz, Pawel };
1009391bb9cSRapkiewicz, Pawel 
1011f8c7b5dSJohnathan Mantey struct DHCPParameters
1021f8c7b5dSJohnathan Mantey {
1031f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
1041f8c7b5dSJohnathan Mantey     std::optional<bool> useDNSServers;
1051f8c7b5dSJohnathan Mantey     std::optional<bool> useNTPServers;
1061f8c7b5dSJohnathan Mantey     std::optional<bool> useUseDomainName;
1071f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1081f8c7b5dSJohnathan Mantey };
1091f8c7b5dSJohnathan Mantey 
1109391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1119391bb9cSRapkiewicz, Pawel // into full dot notation
1121abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1131abe55efSEd Tanous {
1149391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1159391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1169391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1179391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1189391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1199391bb9cSRapkiewicz, Pawel     return netmask;
1209391bb9cSRapkiewicz, Pawel }
1219391bb9cSRapkiewicz, Pawel 
1221f8c7b5dSJohnathan Mantey inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
1231f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1241f8c7b5dSJohnathan Mantey {
1251f8c7b5dSJohnathan Mantey     if (isIPv4)
1261f8c7b5dSJohnathan Mantey     {
1271f8c7b5dSJohnathan Mantey         return (
1281f8c7b5dSJohnathan Mantey             (inputDHCP ==
1291f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1301f8c7b5dSJohnathan Mantey             (inputDHCP ==
1311f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1321f8c7b5dSJohnathan Mantey     }
1331f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1341f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1351f8c7b5dSJohnathan Mantey             (inputDHCP ==
1361f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1371f8c7b5dSJohnathan Mantey }
1381f8c7b5dSJohnathan Mantey 
1392c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1401f8c7b5dSJohnathan Mantey {
1411f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1421f8c7b5dSJohnathan Mantey     {
1431f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1441f8c7b5dSJohnathan Mantey     }
1453174e4dfSEd Tanous     if (isIPv4)
1461f8c7b5dSJohnathan Mantey     {
1471f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1481f8c7b5dSJohnathan Mantey     }
1493174e4dfSEd Tanous     if (isIPv6)
1501f8c7b5dSJohnathan Mantey     {
1511f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1521f8c7b5dSJohnathan Mantey     }
1531f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1541f8c7b5dSJohnathan Mantey }
1551f8c7b5dSJohnathan Mantey 
1564a0cb85cSEd Tanous inline std::string
1574a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1584a0cb85cSEd Tanous                                         bool isIPv4)
1591abe55efSEd Tanous {
1604a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1611abe55efSEd Tanous     {
1624a0cb85cSEd Tanous         return "Static";
1639391bb9cSRapkiewicz, Pawel     }
1644a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1651abe55efSEd Tanous     {
1664a0cb85cSEd Tanous         if (isIPv4)
1671abe55efSEd Tanous         {
1684a0cb85cSEd Tanous             return "IPv4LinkLocal";
1691abe55efSEd Tanous         }
1704a0cb85cSEd Tanous         return "LinkLocal";
1719391bb9cSRapkiewicz, Pawel     }
1724a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1731abe55efSEd Tanous     {
1744a0cb85cSEd Tanous         if (isIPv4)
1754a0cb85cSEd Tanous         {
1764a0cb85cSEd Tanous             return "DHCP";
1774a0cb85cSEd Tanous         }
1784a0cb85cSEd Tanous         return "DHCPv6";
1794a0cb85cSEd Tanous     }
1804a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1814a0cb85cSEd Tanous     {
1824a0cb85cSEd Tanous         return "SLAAC";
1834a0cb85cSEd Tanous     }
1844a0cb85cSEd Tanous     return "";
1854a0cb85cSEd Tanous }
1864a0cb85cSEd Tanous 
187711ac7a9SEd Tanous inline bool
188711ac7a9SEd Tanous     extractEthernetInterfaceData(const std::string& ethifaceId,
189711ac7a9SEd Tanous                                  dbus::utility::ManagedObjectType& dbusData,
1904a0cb85cSEd Tanous                                  EthernetInterfaceData& ethData)
1914a0cb85cSEd Tanous {
1924c9afe43SEd Tanous     bool idFound = false;
19381ce609eSEd Tanous     for (auto& objpath : dbusData)
1944a0cb85cSEd Tanous     {
195f23b7296SEd Tanous         for (auto& ifacePair : objpath.second)
1964a0cb85cSEd Tanous         {
19781ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
198029573d4SEd Tanous             {
1994c9afe43SEd Tanous                 idFound = true;
2004a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2014a0cb85cSEd Tanous                 {
2024a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2034a0cb85cSEd Tanous                     {
2044a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2054a0cb85cSEd Tanous                         {
2064a0cb85cSEd Tanous                             const std::string* mac =
207abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2084a0cb85cSEd Tanous                             if (mac != nullptr)
2094a0cb85cSEd Tanous                             {
2104a0cb85cSEd Tanous                                 ethData.mac_address = *mac;
2114a0cb85cSEd Tanous                             }
2124a0cb85cSEd Tanous                         }
2134a0cb85cSEd Tanous                     }
2144a0cb85cSEd Tanous                 }
2154a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2164a0cb85cSEd Tanous                 {
2174a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2184a0cb85cSEd Tanous                     {
2194a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2204a0cb85cSEd Tanous                         {
2211b6b96c5SEd Tanous                             const uint32_t* id =
222abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2234a0cb85cSEd Tanous                             if (id != nullptr)
2244a0cb85cSEd Tanous                             {
225fda13ad2SSunitha Harish                                 ethData.vlan_id.push_back(*id);
2264a0cb85cSEd Tanous                             }
2274a0cb85cSEd Tanous                         }
2284a0cb85cSEd Tanous                     }
2294a0cb85cSEd Tanous                 }
2304a0cb85cSEd Tanous                 else if (ifacePair.first ==
2314a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2324a0cb85cSEd Tanous                 {
2334a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2344a0cb85cSEd Tanous                     {
2354a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2364a0cb85cSEd Tanous                         {
2372c70f800SEd Tanous                             const bool* autoNeg =
238abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2392c70f800SEd Tanous                             if (autoNeg != nullptr)
2404a0cb85cSEd Tanous                             {
2412c70f800SEd Tanous                                 ethData.auto_neg = *autoNeg;
2424a0cb85cSEd Tanous                             }
2434a0cb85cSEd Tanous                         }
2444a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2454a0cb85cSEd Tanous                         {
2464a0cb85cSEd Tanous                             const uint32_t* speed =
247abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2484a0cb85cSEd Tanous                             if (speed != nullptr)
2494a0cb85cSEd Tanous                             {
2504a0cb85cSEd Tanous                                 ethData.speed = *speed;
2514a0cb85cSEd Tanous                             }
2524a0cb85cSEd Tanous                         }
25335fb5311STejas Patil                         else if (propertyPair.first == "MTU")
25435fb5311STejas Patil                         {
25535fb5311STejas Patil                             const uint32_t* mtuSize =
25635fb5311STejas Patil                                 std::get_if<uint32_t>(&propertyPair.second);
25735fb5311STejas Patil                             if (mtuSize != nullptr)
25835fb5311STejas Patil                             {
25935fb5311STejas Patil                                 ethData.mtuSize = *mtuSize;
26035fb5311STejas Patil                             }
26135fb5311STejas Patil                         }
262aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
263aa05fb27SJohnathan Mantey                         {
264aa05fb27SJohnathan Mantey                             const bool* linkUp =
265aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
266aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
267aa05fb27SJohnathan Mantey                             {
268aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
269aa05fb27SJohnathan Mantey                             }
270aa05fb27SJohnathan Mantey                         }
271eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
272eeedda23SJohnathan Mantey                         {
273eeedda23SJohnathan Mantey                             const bool* nicEnabled =
274eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
275eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
276eeedda23SJohnathan Mantey                             {
277eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
278eeedda23SJohnathan Mantey                             }
279eeedda23SJohnathan Mantey                         }
280f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
281029573d4SEd Tanous                         {
282029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2838d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
284029573d4SEd Tanous                                     &propertyPair.second);
285029573d4SEd Tanous                             if (nameservers != nullptr)
286029573d4SEd Tanous                             {
287f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2880f6efdc1Smanojkiran.eda@gmail.com                             }
2890f6efdc1Smanojkiran.eda@gmail.com                         }
2900f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2910f6efdc1Smanojkiran.eda@gmail.com                         {
2920f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
2938d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
2940f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
2950f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
2960f6efdc1Smanojkiran.eda@gmail.com                             {
297f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
2984a0cb85cSEd Tanous                             }
2994a0cb85cSEd Tanous                         }
3002a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3012a133282Smanojkiraneda                         {
3022c70f800SEd Tanous                             const std::string* dhcpEnabled =
3031f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3042c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3052a133282Smanojkiraneda                             {
3062c70f800SEd Tanous                                 ethData.DHCPEnabled = *dhcpEnabled;
3072a133282Smanojkiraneda                             }
3082a133282Smanojkiraneda                         }
309d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
310d24bfc7aSJennifer Lee                         {
311d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3128d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
313d24bfc7aSJennifer Lee                                     &propertyPair.second);
314d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
315d24bfc7aSJennifer Lee                             {
316f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
317d24bfc7aSJennifer Lee                             }
318d24bfc7aSJennifer Lee                         }
3199010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway")
3209010ec2eSRavi Teja                         {
3219010ec2eSRavi Teja                             const std::string* defaultGateway =
3229010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3239010ec2eSRavi Teja                             if (defaultGateway != nullptr)
3249010ec2eSRavi Teja                             {
3259010ec2eSRavi Teja                                 std::string defaultGatewayStr = *defaultGateway;
3269010ec2eSRavi Teja                                 if (defaultGatewayStr.empty())
3279010ec2eSRavi Teja                                 {
3289010ec2eSRavi Teja                                     ethData.default_gateway = "0.0.0.0";
3299010ec2eSRavi Teja                                 }
3309010ec2eSRavi Teja                                 else
3319010ec2eSRavi Teja                                 {
3329010ec2eSRavi Teja                                     ethData.default_gateway = defaultGatewayStr;
3339010ec2eSRavi Teja                                 }
3349010ec2eSRavi Teja                             }
3359010ec2eSRavi Teja                         }
3369010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway6")
3379010ec2eSRavi Teja                         {
3389010ec2eSRavi Teja                             const std::string* defaultGateway6 =
3399010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3409010ec2eSRavi Teja                             if (defaultGateway6 != nullptr)
3419010ec2eSRavi Teja                             {
3429010ec2eSRavi Teja                                 std::string defaultGateway6Str =
3439010ec2eSRavi Teja                                     *defaultGateway6;
3449010ec2eSRavi Teja                                 if (defaultGateway6Str.empty())
3459010ec2eSRavi Teja                                 {
3469010ec2eSRavi Teja                                     ethData.ipv6_default_gateway =
3479010ec2eSRavi Teja                                         "0:0:0:0:0:0:0:0";
3489010ec2eSRavi Teja                                 }
3499010ec2eSRavi Teja                                 else
3509010ec2eSRavi Teja                                 {
3519010ec2eSRavi Teja                                     ethData.ipv6_default_gateway =
3529010ec2eSRavi Teja                                         defaultGateway6Str;
3539010ec2eSRavi Teja                                 }
3549010ec2eSRavi Teja                             }
3559010ec2eSRavi Teja                         }
356029573d4SEd Tanous                     }
357029573d4SEd Tanous                 }
358029573d4SEd Tanous             }
3591f8c7b5dSJohnathan Mantey 
3601f8c7b5dSJohnathan Mantey             if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
3611f8c7b5dSJohnathan Mantey             {
3621f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3631f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3641f8c7b5dSJohnathan Mantey                 {
3651f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3661f8c7b5dSJohnathan Mantey                     {
3671f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3681f8c7b5dSJohnathan Mantey                         {
3692c70f800SEd Tanous                             const bool* dnsEnabled =
3701f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3712c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3721f8c7b5dSJohnathan Mantey                             {
3732c70f800SEd Tanous                                 ethData.DNSEnabled = *dnsEnabled;
3741f8c7b5dSJohnathan Mantey                             }
3751f8c7b5dSJohnathan Mantey                         }
3761f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3771f8c7b5dSJohnathan Mantey                         {
3782c70f800SEd Tanous                             const bool* ntpEnabled =
3791f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3802c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3811f8c7b5dSJohnathan Mantey                             {
3822c70f800SEd Tanous                                 ethData.NTPEnabled = *ntpEnabled;
3831f8c7b5dSJohnathan Mantey                             }
3841f8c7b5dSJohnathan Mantey                         }
3851f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3861f8c7b5dSJohnathan Mantey                         {
3872c70f800SEd Tanous                             const bool* hostNameEnabled =
3881f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3892c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3901f8c7b5dSJohnathan Mantey                             {
3912c70f800SEd Tanous                                 ethData.HostNameEnabled = *hostNameEnabled;
3921f8c7b5dSJohnathan Mantey                             }
3931f8c7b5dSJohnathan Mantey                         }
3941f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "SendHostNameEnabled")
3951f8c7b5dSJohnathan Mantey                         {
3962c70f800SEd Tanous                             const bool* sendHostNameEnabled =
3971f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3982c70f800SEd Tanous                             if (sendHostNameEnabled != nullptr)
3991f8c7b5dSJohnathan Mantey                             {
4001f8c7b5dSJohnathan Mantey                                 ethData.SendHostNameEnabled =
4012c70f800SEd Tanous                                     *sendHostNameEnabled;
4021f8c7b5dSJohnathan Mantey                             }
4031f8c7b5dSJohnathan Mantey                         }
4041f8c7b5dSJohnathan Mantey                     }
4051f8c7b5dSJohnathan Mantey                 }
4061f8c7b5dSJohnathan Mantey             }
407029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
408029573d4SEd Tanous             // to check eth number
409029573d4SEd Tanous             if (ifacePair.first ==
4104a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
4114a0cb85cSEd Tanous             {
4124a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
4134a0cb85cSEd Tanous                 {
4144a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
4154a0cb85cSEd Tanous                     {
4164a0cb85cSEd Tanous                         const std::string* hostname =
4178d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4184a0cb85cSEd Tanous                         if (hostname != nullptr)
4194a0cb85cSEd Tanous                         {
4204a0cb85cSEd Tanous                             ethData.hostname = *hostname;
4214a0cb85cSEd Tanous                         }
4224a0cb85cSEd Tanous                     }
4234a0cb85cSEd Tanous                 }
4244a0cb85cSEd Tanous             }
4254a0cb85cSEd Tanous         }
4264a0cb85cSEd Tanous     }
4274c9afe43SEd Tanous     return idFound;
4284a0cb85cSEd Tanous }
4294a0cb85cSEd Tanous 
430e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
43101784826SJohnathan Mantey inline void
43281ce609eSEd Tanous     extractIPV6Data(const std::string& ethifaceId,
433711ac7a9SEd Tanous                     const dbus::utility::ManagedObjectType& dbusData,
43481ce609eSEd Tanous                     boost::container::flat_set<IPv6AddressData>& ipv6Config)
435e48c0fc5SRavi Teja {
436e48c0fc5SRavi Teja     const std::string ipv6PathStart =
43781ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
438e48c0fc5SRavi Teja 
439e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
440e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
44181ce609eSEd Tanous     for (const auto& objpath : dbusData)
442e48c0fc5SRavi Teja     {
443e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
444e48c0fc5SRavi Teja         if (boost::starts_with(objpath.first.str, ipv6PathStart))
445e48c0fc5SRavi Teja         {
4469eb808c1SEd Tanous             for (const auto& interface : objpath.second)
447e48c0fc5SRavi Teja             {
448e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
449e48c0fc5SRavi Teja                 {
450e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
451e48c0fc5SRavi Teja                     // appropriate
452e48c0fc5SRavi Teja                     std::pair<
453e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
454e48c0fc5SRavi Teja                         bool>
45581ce609eSEd Tanous                         it = ipv6Config.insert(IPv6AddressData{});
4562c70f800SEd Tanous                     IPv6AddressData& ipv6Address = *it.first;
4572c70f800SEd Tanous                     ipv6Address.id =
458271584abSEd Tanous                         objpath.first.str.substr(ipv6PathStart.size());
4599eb808c1SEd Tanous                     for (const auto& property : interface.second)
460e48c0fc5SRavi Teja                     {
461e48c0fc5SRavi Teja                         if (property.first == "Address")
462e48c0fc5SRavi Teja                         {
463e48c0fc5SRavi Teja                             const std::string* address =
464e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
465e48c0fc5SRavi Teja                             if (address != nullptr)
466e48c0fc5SRavi Teja                             {
4672c70f800SEd Tanous                                 ipv6Address.address = *address;
468e48c0fc5SRavi Teja                             }
469e48c0fc5SRavi Teja                         }
470e48c0fc5SRavi Teja                         else if (property.first == "Origin")
471e48c0fc5SRavi Teja                         {
472e48c0fc5SRavi Teja                             const std::string* origin =
473e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
474e48c0fc5SRavi Teja                             if (origin != nullptr)
475e48c0fc5SRavi Teja                             {
4762c70f800SEd Tanous                                 ipv6Address.origin =
477e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
478e48c0fc5SRavi Teja                                                                         false);
479e48c0fc5SRavi Teja                             }
480e48c0fc5SRavi Teja                         }
481e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
482e48c0fc5SRavi Teja                         {
483e48c0fc5SRavi Teja                             const uint8_t* prefix =
484e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
485e48c0fc5SRavi Teja                             if (prefix != nullptr)
486e48c0fc5SRavi Teja                             {
4872c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
488e48c0fc5SRavi Teja                             }
489e48c0fc5SRavi Teja                         }
490889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
491889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
492889ff694SAsmitha Karunanithi                         {
493889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
494889ff694SAsmitha Karunanithi                         }
495e48c0fc5SRavi Teja                         else
496e48c0fc5SRavi Teja                         {
497e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
498e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
499e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
500e48c0fc5SRavi Teja                         }
501e48c0fc5SRavi Teja                     }
502e48c0fc5SRavi Teja                 }
503e48c0fc5SRavi Teja             }
504e48c0fc5SRavi Teja         }
505e48c0fc5SRavi Teja     }
506e48c0fc5SRavi Teja }
507e48c0fc5SRavi Teja 
5084a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
50901784826SJohnathan Mantey inline void
51081ce609eSEd Tanous     extractIPData(const std::string& ethifaceId,
511711ac7a9SEd Tanous                   const dbus::utility::ManagedObjectType& dbusData,
51281ce609eSEd Tanous                   boost::container::flat_set<IPv4AddressData>& ipv4Config)
5134a0cb85cSEd Tanous {
5144a0cb85cSEd Tanous     const std::string ipv4PathStart =
51581ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
5164a0cb85cSEd Tanous 
5174a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5184a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
51981ce609eSEd Tanous     for (const auto& objpath : dbusData)
5204a0cb85cSEd Tanous     {
5214a0cb85cSEd Tanous         // Check if proper pattern for object path appears
5224a0cb85cSEd Tanous         if (boost::starts_with(objpath.first.str, ipv4PathStart))
5234a0cb85cSEd Tanous         {
5249eb808c1SEd Tanous             for (const auto& interface : objpath.second)
5254a0cb85cSEd Tanous             {
5264a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5274a0cb85cSEd Tanous                 {
5284a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5294a0cb85cSEd Tanous                     // appropriate
5304a0cb85cSEd Tanous                     std::pair<
5314a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5324a0cb85cSEd Tanous                         bool>
53381ce609eSEd Tanous                         it = ipv4Config.insert(IPv4AddressData{});
5342c70f800SEd Tanous                     IPv4AddressData& ipv4Address = *it.first;
5352c70f800SEd Tanous                     ipv4Address.id =
536271584abSEd Tanous                         objpath.first.str.substr(ipv4PathStart.size());
5379eb808c1SEd Tanous                     for (const auto& property : interface.second)
5384a0cb85cSEd Tanous                     {
5394a0cb85cSEd Tanous                         if (property.first == "Address")
5404a0cb85cSEd Tanous                         {
5414a0cb85cSEd Tanous                             const std::string* address =
542abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5434a0cb85cSEd Tanous                             if (address != nullptr)
5444a0cb85cSEd Tanous                             {
5452c70f800SEd Tanous                                 ipv4Address.address = *address;
5464a0cb85cSEd Tanous                             }
5474a0cb85cSEd Tanous                         }
5484a0cb85cSEd Tanous                         else if (property.first == "Origin")
5494a0cb85cSEd Tanous                         {
5504a0cb85cSEd Tanous                             const std::string* origin =
551abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5524a0cb85cSEd Tanous                             if (origin != nullptr)
5534a0cb85cSEd Tanous                             {
5542c70f800SEd Tanous                                 ipv4Address.origin =
5554a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5564a0cb85cSEd Tanous                                                                         true);
5574a0cb85cSEd Tanous                             }
5584a0cb85cSEd Tanous                         }
5594a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5604a0cb85cSEd Tanous                         {
5614a0cb85cSEd Tanous                             const uint8_t* mask =
562abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5634a0cb85cSEd Tanous                             if (mask != nullptr)
5644a0cb85cSEd Tanous                             {
5654a0cb85cSEd Tanous                                 // convert it to the string
5662c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
5674a0cb85cSEd Tanous                             }
5684a0cb85cSEd Tanous                         }
569889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
570889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
571889ff694SAsmitha Karunanithi                         {
572889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
573889ff694SAsmitha Karunanithi                         }
5744a0cb85cSEd Tanous                         else
5754a0cb85cSEd Tanous                         {
5764a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5774a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5784a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5794a0cb85cSEd Tanous                         }
5804a0cb85cSEd Tanous                     }
5814a0cb85cSEd Tanous                     // Check if given address is local, or global
5822c70f800SEd Tanous                     ipv4Address.linktype =
5832c70f800SEd Tanous                         boost::starts_with(ipv4Address.address, "169.254.")
58418659d10SJohnathan Mantey                             ? LinkType::Local
58518659d10SJohnathan Mantey                             : LinkType::Global;
5864a0cb85cSEd Tanous                 }
5874a0cb85cSEd Tanous             }
5884a0cb85cSEd Tanous         }
5894a0cb85cSEd Tanous     }
5904a0cb85cSEd Tanous }
591588c3f0dSKowalski, Kamil 
592588c3f0dSKowalski, Kamil /**
593588c3f0dSKowalski, Kamil  * @brief Sets given Id on the given VLAN interface through D-Bus
594588c3f0dSKowalski, Kamil  *
595588c3f0dSKowalski, Kamil  * @param[in] ifaceId       Id of VLAN interface that should be modified
596588c3f0dSKowalski, Kamil  * @param[in] inputVlanId   New ID of the VLAN
597588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
598588c3f0dSKowalski, Kamil  *
599588c3f0dSKowalski, Kamil  * @return None.
600588c3f0dSKowalski, Kamil  */
601588c3f0dSKowalski, Kamil template <typename CallbackFunc>
6024a0cb85cSEd Tanous void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
6031abe55efSEd Tanous                   CallbackFunc&& callback)
6041abe55efSEd Tanous {
60555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
606588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
607588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
608588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
609588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", "Id",
610168e20c1SEd Tanous         dbus::utility::DbusVariantType(inputVlanId));
6114a0cb85cSEd Tanous }
612588c3f0dSKowalski, Kamil 
613588c3f0dSKowalski, Kamil /**
614179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
615179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
616179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
617179db1d7SKowalski, Kamil  *
618179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
619179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
620179db1d7SKowalski, Kamil  *
621179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
622179db1d7SKowalski, Kamil  */
6234a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
6241abe55efSEd Tanous                                        uint8_t* bits = nullptr)
6251abe55efSEd Tanous {
626179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
627179db1d7SKowalski, Kamil 
628179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
629179db1d7SKowalski, Kamil 
6304a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
6311abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
6321abe55efSEd Tanous     {
633179db1d7SKowalski, Kamil         return false;
634179db1d7SKowalski, Kamil     }
635179db1d7SKowalski, Kamil 
6361abe55efSEd Tanous     if (bits != nullptr)
6371abe55efSEd Tanous     {
638179db1d7SKowalski, Kamil         *bits = 0;
639179db1d7SKowalski, Kamil     }
640179db1d7SKowalski, Kamil 
641543f4400SEd Tanous     char* endPtr = nullptr;
642179db1d7SKowalski, Kamil     long previousValue = 255;
643543f4400SEd Tanous     bool firstZeroInByteHit = false;
6441abe55efSEd Tanous     for (const std::string& byte : bytesInMask)
6451abe55efSEd Tanous     {
6461abe55efSEd Tanous         if (byte.empty())
6471abe55efSEd Tanous         {
6481db9ca37SKowalski, Kamil             return false;
6491db9ca37SKowalski, Kamil         }
6501db9ca37SKowalski, Kamil 
651179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
6521db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
653179db1d7SKowalski, Kamil 
6544a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
6554a0cb85cSEd Tanous         // is not 100% number
6561abe55efSEd Tanous         if (*endPtr != '\0')
6571abe55efSEd Tanous         {
658179db1d7SKowalski, Kamil             return false;
659179db1d7SKowalski, Kamil         }
660179db1d7SKowalski, Kamil 
661179db1d7SKowalski, Kamil         // Value should be contained in byte
6621abe55efSEd Tanous         if (value < 0 || value > 255)
6631abe55efSEd Tanous         {
664179db1d7SKowalski, Kamil             return false;
665179db1d7SKowalski, Kamil         }
666179db1d7SKowalski, Kamil 
6671abe55efSEd Tanous         if (bits != nullptr)
6681abe55efSEd Tanous         {
669179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
6701abe55efSEd Tanous             if (previousValue != 255 && value != 0)
6711abe55efSEd Tanous             {
672179db1d7SKowalski, Kamil                 return false;
673179db1d7SKowalski, Kamil             }
674179db1d7SKowalski, Kamil 
675179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
676179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
677179db1d7SKowalski, Kamil 
678179db1d7SKowalski, Kamil             // Count bits
67923a21a1cSEd Tanous             for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
6801abe55efSEd Tanous             {
681e662eae8SEd Tanous                 if ((value & (1L << bitIdx)) != 0)
6821abe55efSEd Tanous                 {
6831abe55efSEd Tanous                     if (firstZeroInByteHit)
6841abe55efSEd Tanous                     {
685179db1d7SKowalski, Kamil                         // Continuity not preserved
686179db1d7SKowalski, Kamil                         return false;
6871abe55efSEd Tanous                     }
688179db1d7SKowalski, Kamil                     (*bits)++;
689179db1d7SKowalski, Kamil                 }
6901abe55efSEd Tanous                 else
6911abe55efSEd Tanous                 {
692179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
693179db1d7SKowalski, Kamil                 }
694179db1d7SKowalski, Kamil             }
695179db1d7SKowalski, Kamil         }
696179db1d7SKowalski, Kamil 
697179db1d7SKowalski, Kamil         previousValue = value;
698179db1d7SKowalski, Kamil     }
699179db1d7SKowalski, Kamil 
700179db1d7SKowalski, Kamil     return true;
701179db1d7SKowalski, Kamil }
702179db1d7SKowalski, Kamil 
703179db1d7SKowalski, Kamil /**
70401784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
705179db1d7SKowalski, Kamil  *
706179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
707179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
708179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
709179db1d7SKowalski, Kamil  *
710179db1d7SKowalski, Kamil  * @return None
711179db1d7SKowalski, Kamil  */
7124a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
7138d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7141abe55efSEd Tanous {
71555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
716286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
7171abe55efSEd Tanous             if (ec)
7181abe55efSEd Tanous             {
719a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
7201abe55efSEd Tanous             }
721179db1d7SKowalski, Kamil         },
722179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
723179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
724179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
725179db1d7SKowalski, Kamil }
726179db1d7SKowalski, Kamil 
727244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway(
728244b6d5bSGunnar Mills     const std::string& ifaceId, const std::string& gateway,
729244b6d5bSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7309010ec2eSRavi Teja {
7319010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7329010ec2eSRavi Teja         [asyncResp](const boost::system::error_code ec) {
7339010ec2eSRavi Teja             if (ec)
7349010ec2eSRavi Teja             {
7359010ec2eSRavi Teja                 messages::internalError(asyncResp->res);
7369010ec2eSRavi Teja                 return;
7379010ec2eSRavi Teja             }
7389010ec2eSRavi Teja             asyncResp->res.result(boost::beast::http::status::no_content);
7399010ec2eSRavi Teja         },
7409010ec2eSRavi Teja         "xyz.openbmc_project.Network",
7419010ec2eSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
7429010ec2eSRavi Teja         "org.freedesktop.DBus.Properties", "Set",
7439010ec2eSRavi Teja         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
744168e20c1SEd Tanous         dbus::utility::DbusVariantType(gateway));
7459010ec2eSRavi Teja }
746179db1d7SKowalski, Kamil /**
74701784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
748179db1d7SKowalski, Kamil  *
74901784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
75001784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
75101784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
75201784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
753179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
754179db1d7SKowalski, Kamil  *
755179db1d7SKowalski, Kamil  * @return None
756179db1d7SKowalski, Kamil  */
757cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
758cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
7598d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7601abe55efSEd Tanous {
7619010ec2eSRavi Teja     auto createIpHandler = [asyncResp, ifaceId,
7629010ec2eSRavi Teja                             gateway](const boost::system::error_code ec) {
7631abe55efSEd Tanous         if (ec)
7641abe55efSEd Tanous         {
765a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
7669010ec2eSRavi Teja             return;
767179db1d7SKowalski, Kamil         }
7689010ec2eSRavi Teja         updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
7699010ec2eSRavi Teja     };
7709010ec2eSRavi Teja 
7719010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7729010ec2eSRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
773179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
774179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
77501784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
776179db1d7SKowalski, Kamil         gateway);
777179db1d7SKowalski, Kamil }
778e48c0fc5SRavi Teja 
779e48c0fc5SRavi Teja /**
78001784826SJohnathan Mantey  * @brief Deletes the IPv4 entry for this interface and creates a replacement
78101784826SJohnathan Mantey  * static IPv4 entry
78201784826SJohnathan Mantey  *
78301784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
78401784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
78501784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
78601784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
78701784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
78801784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
78901784826SJohnathan Mantey  *
79001784826SJohnathan Mantey  * @return None
79101784826SJohnathan Mantey  */
7928d1b46d7Szhanghch05 inline void
7938d1b46d7Szhanghch05     deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
7948d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& gateway,
79501784826SJohnathan Mantey                         const std::string& address,
7968d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
79701784826SJohnathan Mantey {
79801784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
79901784826SJohnathan Mantey         [asyncResp, ifaceId, address, prefixLength,
80001784826SJohnathan Mantey          gateway](const boost::system::error_code ec) {
80101784826SJohnathan Mantey             if (ec)
80201784826SJohnathan Mantey             {
80301784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
8049010ec2eSRavi Teja                 return;
80501784826SJohnathan Mantey             }
8069010ec2eSRavi Teja 
80701784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
8089010ec2eSRavi Teja                 [asyncResp, ifaceId,
8099010ec2eSRavi Teja                  gateway](const boost::system::error_code ec2) {
81023a21a1cSEd Tanous                     if (ec2)
81101784826SJohnathan Mantey                     {
81201784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
8139010ec2eSRavi Teja                         return;
81401784826SJohnathan Mantey                     }
8159010ec2eSRavi Teja                     updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
81601784826SJohnathan Mantey                 },
81701784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
81801784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
81901784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
82001784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
82101784826SJohnathan Mantey                 prefixLength, gateway);
82201784826SJohnathan Mantey         },
82301784826SJohnathan Mantey         "xyz.openbmc_project.Network",
82401784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
82501784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
82601784826SJohnathan Mantey }
82701784826SJohnathan Mantey 
82801784826SJohnathan Mantey /**
829e48c0fc5SRavi Teja  * @brief Deletes given IPv6
830e48c0fc5SRavi Teja  *
831e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
832e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
833e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
834e48c0fc5SRavi Teja  *
835e48c0fc5SRavi Teja  * @return None
836e48c0fc5SRavi Teja  */
837e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
8388d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
839e48c0fc5SRavi Teja {
840e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
841286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
842e48c0fc5SRavi Teja             if (ec)
843e48c0fc5SRavi Teja             {
844e48c0fc5SRavi Teja                 messages::internalError(asyncResp->res);
845e48c0fc5SRavi Teja             }
846e48c0fc5SRavi Teja         },
847e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
848e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
849e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
850e48c0fc5SRavi Teja }
851e48c0fc5SRavi Teja 
852e48c0fc5SRavi Teja /**
85301784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
85401784826SJohnathan Mantey  * static IPv6 entry
85501784826SJohnathan Mantey  *
85601784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
85701784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
85801784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
85901784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
86001784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
86101784826SJohnathan Mantey  *
86201784826SJohnathan Mantey  * @return None
86301784826SJohnathan Mantey  */
8648d1b46d7Szhanghch05 inline void
8658d1b46d7Szhanghch05     deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
8668d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& address,
8678d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
86801784826SJohnathan Mantey {
86901784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
87001784826SJohnathan Mantey         [asyncResp, ifaceId, address,
87101784826SJohnathan Mantey          prefixLength](const boost::system::error_code ec) {
87201784826SJohnathan Mantey             if (ec)
87301784826SJohnathan Mantey             {
87401784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
87501784826SJohnathan Mantey             }
87601784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
87723a21a1cSEd Tanous                 [asyncResp](const boost::system::error_code ec2) {
87823a21a1cSEd Tanous                     if (ec2)
87901784826SJohnathan Mantey                     {
88001784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
88101784826SJohnathan Mantey                     }
88201784826SJohnathan Mantey                 },
88301784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
88401784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
88501784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
88601784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
88701784826SJohnathan Mantey                 prefixLength, "");
88801784826SJohnathan Mantey         },
88901784826SJohnathan Mantey         "xyz.openbmc_project.Network",
89001784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
89101784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
89201784826SJohnathan Mantey }
89301784826SJohnathan Mantey 
89401784826SJohnathan Mantey /**
895e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
896e48c0fc5SRavi Teja  *
897e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
898e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
899e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
900e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
901e48c0fc5SRavi Teja  *
902e48c0fc5SRavi Teja  * @return None
903e48c0fc5SRavi Teja  */
90401784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
90501784826SJohnathan Mantey                        const std::string& address,
9068d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
907e48c0fc5SRavi Teja {
908e48c0fc5SRavi Teja     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
909e48c0fc5SRavi Teja         if (ec)
910e48c0fc5SRavi Teja         {
911e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
912e48c0fc5SRavi Teja         }
913e48c0fc5SRavi Teja     };
914e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
9154e0453b1SGunnar Mills     // does not have associated gateway property
916e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
917e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
918e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
919e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
920e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
921e48c0fc5SRavi Teja         "");
922e48c0fc5SRavi Teja }
923e48c0fc5SRavi Teja 
924179db1d7SKowalski, Kamil /**
925179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
926179db1d7SKowalski, Kamil  * Object
927179db1d7SKowalski, Kamil  * from EntityManager Network Manager
9284a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
929179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
930179db1d7SKowalski, Kamil  * into JSON
931179db1d7SKowalski, Kamil  */
932179db1d7SKowalski, Kamil template <typename CallbackFunc>
93381ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
9341abe55efSEd Tanous                           CallbackFunc&& callback)
9351abe55efSEd Tanous {
93655c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
937f94c4ecfSEd Tanous         [ethifaceId{std::string{ethifaceId}},
938f94c4ecfSEd Tanous          callback{std::forward<CallbackFunc>(callback)}](
93981ce609eSEd Tanous             const boost::system::error_code errorCode,
940711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
94155c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
9424a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
943e48c0fc5SRavi Teja             boost::container::flat_set<IPv6AddressData> ipv6Data;
944179db1d7SKowalski, Kamil 
94581ce609eSEd Tanous             if (errorCode)
9461abe55efSEd Tanous             {
94701784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
948179db1d7SKowalski, Kamil                 return;
949179db1d7SKowalski, Kamil             }
950179db1d7SKowalski, Kamil 
9514c9afe43SEd Tanous             bool found =
9522c70f800SEd Tanous                 extractEthernetInterfaceData(ethifaceId, resp, ethData);
9534c9afe43SEd Tanous             if (!found)
9544c9afe43SEd Tanous             {
95501784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
9564c9afe43SEd Tanous                 return;
9574c9afe43SEd Tanous             }
9584c9afe43SEd Tanous 
9592c70f800SEd Tanous             extractIPData(ethifaceId, resp, ipv4Data);
960179db1d7SKowalski, Kamil             // Fix global GW
9611abe55efSEd Tanous             for (IPv4AddressData& ipv4 : ipv4Data)
9621abe55efSEd Tanous             {
963c619141bSRavi Teja                 if (((ipv4.linktype == LinkType::Global) &&
964c619141bSRavi Teja                      (ipv4.gateway == "0.0.0.0")) ||
9659010ec2eSRavi Teja                     (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
9661abe55efSEd Tanous                 {
9674a0cb85cSEd Tanous                     ipv4.gateway = ethData.default_gateway;
968179db1d7SKowalski, Kamil                 }
969179db1d7SKowalski, Kamil             }
970179db1d7SKowalski, Kamil 
9712c70f800SEd Tanous             extractIPV6Data(ethifaceId, resp, ipv6Data);
9724e0453b1SGunnar Mills             // Finally make a callback with useful data
97301784826SJohnathan Mantey             callback(true, ethData, ipv4Data, ipv6Data);
974179db1d7SKowalski, Kamil         },
975179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
976179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
977271584abSEd Tanous }
978179db1d7SKowalski, Kamil 
979179db1d7SKowalski, Kamil /**
9809391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
9819391bb9cSRapkiewicz, Pawel  * Manager
9821abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
9831abe55efSEd Tanous  * into JSON.
9849391bb9cSRapkiewicz, Pawel  */
9859391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
9861abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
9871abe55efSEd Tanous {
98855c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
989f94c4ecfSEd Tanous         [callback{std::forward<CallbackFunc>(callback)}](
99081ce609eSEd Tanous             const boost::system::error_code errorCode,
991711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
9921abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
9931abe55efSEd Tanous             // ethernet interfaces
9942c70f800SEd Tanous             boost::container::flat_set<std::string> ifaceList;
9952c70f800SEd Tanous             ifaceList.reserve(resp.size());
99681ce609eSEd Tanous             if (errorCode)
9971abe55efSEd Tanous             {
9982c70f800SEd Tanous                 callback(false, ifaceList);
9999391bb9cSRapkiewicz, Pawel                 return;
10009391bb9cSRapkiewicz, Pawel             }
10019391bb9cSRapkiewicz, Pawel 
10029391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
10034a0cb85cSEd Tanous             for (const auto& objpath : resp)
10041abe55efSEd Tanous             {
10059391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
10064a0cb85cSEd Tanous                 for (const auto& interface : objpath.second)
10071abe55efSEd Tanous                 {
10081abe55efSEd Tanous                     // If interface is
10094a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
10104a0cb85cSEd Tanous                     // what we're looking for.
10119391bb9cSRapkiewicz, Pawel                     if (interface.first ==
10121abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
10131abe55efSEd Tanous                     {
10142dfd18efSEd Tanous                         std::string ifaceId = objpath.first.filename();
10152dfd18efSEd Tanous                         if (ifaceId.empty())
10161abe55efSEd Tanous                         {
10172dfd18efSEd Tanous                             continue;
10189391bb9cSRapkiewicz, Pawel                         }
10192dfd18efSEd Tanous                         // and put it into output vector.
10202dfd18efSEd Tanous                         ifaceList.emplace(ifaceId);
10219391bb9cSRapkiewicz, Pawel                     }
10229391bb9cSRapkiewicz, Pawel                 }
10239391bb9cSRapkiewicz, Pawel             }
1024a434f2bdSEd Tanous             // Finally make a callback with useful data
10252c70f800SEd Tanous             callback(true, ifaceList);
10269391bb9cSRapkiewicz, Pawel         },
1027aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1028aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1029271584abSEd Tanous }
10309391bb9cSRapkiewicz, Pawel 
10314f48d5f6SEd Tanous inline void
10324f48d5f6SEd Tanous     handleHostnamePatch(const std::string& hostname,
10338d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
10341abe55efSEd Tanous {
1035ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
1036ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
1037ab6554f1SJoshi-Mansi     {
1038ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
1039ab6554f1SJoshi-Mansi                                            "HostName");
1040ab6554f1SJoshi-Mansi         return;
1041ab6554f1SJoshi-Mansi     }
1042bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
1043bc0bd6e0SEd Tanous         [asyncResp](const boost::system::error_code ec) {
10444a0cb85cSEd Tanous             if (ec)
10454a0cb85cSEd Tanous             {
1046a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
10471abe55efSEd Tanous             }
1048bc0bd6e0SEd Tanous         },
1049bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1050bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
1051bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1052168e20c1SEd Tanous         dbus::utility::DbusVariantType(hostname));
1053588c3f0dSKowalski, Kamil }
1054588c3f0dSKowalski, Kamil 
10554f48d5f6SEd Tanous inline void
105635fb5311STejas Patil     handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
105735fb5311STejas Patil                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
105835fb5311STejas Patil {
105935fb5311STejas Patil     sdbusplus::message::object_path objPath =
106035fb5311STejas Patil         "/xyz/openbmc_project/network/" + ifaceId;
106135fb5311STejas Patil     crow::connections::systemBus->async_method_call(
106235fb5311STejas Patil         [asyncResp](const boost::system::error_code ec) {
106335fb5311STejas Patil             if (ec)
106435fb5311STejas Patil             {
106535fb5311STejas Patil                 messages::internalError(asyncResp->res);
106635fb5311STejas Patil             }
106735fb5311STejas Patil         },
106835fb5311STejas Patil         "xyz.openbmc_project.Network", objPath,
106935fb5311STejas Patil         "org.freedesktop.DBus.Properties", "Set",
107035fb5311STejas Patil         "xyz.openbmc_project.Network.EthernetInterface", "MTU",
107135fb5311STejas Patil         std::variant<size_t>(mtuSize));
107235fb5311STejas Patil }
107335fb5311STejas Patil 
107435fb5311STejas Patil inline void
10754f48d5f6SEd Tanous     handleDomainnamePatch(const std::string& ifaceId,
1076bf648f77SEd Tanous                           const std::string& domainname,
10778d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1078ab6554f1SJoshi-Mansi {
1079ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
1080ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
1081ab6554f1SJoshi-Mansi         [asyncResp](const boost::system::error_code ec) {
1082ab6554f1SJoshi-Mansi             if (ec)
1083ab6554f1SJoshi-Mansi             {
1084ab6554f1SJoshi-Mansi                 messages::internalError(asyncResp->res);
1085ab6554f1SJoshi-Mansi             }
1086ab6554f1SJoshi-Mansi         },
1087ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
1088ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
1089ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
1090ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1091168e20c1SEd Tanous         dbus::utility::DbusVariantType(vectorDomainname));
1092ab6554f1SJoshi-Mansi }
1093ab6554f1SJoshi-Mansi 
10944f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname)
1095bf648f77SEd Tanous {
1096bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
1097bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1098bf648f77SEd Tanous     {
1099bf648f77SEd Tanous         return false;
1100bf648f77SEd Tanous     }
1101bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
1102bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
1103bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
1104bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
1105bf648f77SEd Tanous     const std::regex pattern(
1106bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1107bf648f77SEd Tanous 
1108bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
1109bf648f77SEd Tanous }
1110bf648f77SEd Tanous 
11114f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname)
1112bf648f77SEd Tanous {
1113bf648f77SEd Tanous     // Can have multiple subdomains
1114bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
11150fda0f12SGeorge Liu     const std::regex pattern(
11160fda0f12SGeorge Liu         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
1117bf648f77SEd Tanous 
1118bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
1119bf648f77SEd Tanous }
1120bf648f77SEd Tanous 
11214f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
11228d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1123ab6554f1SJoshi-Mansi {
1124ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
1125ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
1126ab6554f1SJoshi-Mansi     {
1127ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1128ab6554f1SJoshi-Mansi         return;
1129ab6554f1SJoshi-Mansi     }
1130ab6554f1SJoshi-Mansi 
1131ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
1132ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
1133ab6554f1SJoshi-Mansi     {
1134ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1135ab6554f1SJoshi-Mansi         return;
1136ab6554f1SJoshi-Mansi     }
1137ab6554f1SJoshi-Mansi 
1138ab6554f1SJoshi-Mansi     std::string hostname;
1139ab6554f1SJoshi-Mansi     std::string domainname;
1140ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
1141ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
1142ab6554f1SJoshi-Mansi 
1143ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1144ab6554f1SJoshi-Mansi     {
1145ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1146ab6554f1SJoshi-Mansi         return;
1147ab6554f1SJoshi-Mansi     }
1148ab6554f1SJoshi-Mansi 
1149ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
1150ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
1151ab6554f1SJoshi-Mansi }
1152ab6554f1SJoshi-Mansi 
11534f48d5f6SEd Tanous inline void
11544f48d5f6SEd Tanous     handleMACAddressPatch(const std::string& ifaceId,
1155bf648f77SEd Tanous                           const std::string& macAddress,
11568d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1157d577665bSRatan Gupta {
1158d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
1159d577665bSRatan Gupta         [asyncResp, macAddress](const boost::system::error_code ec) {
1160d577665bSRatan Gupta             if (ec)
1161d577665bSRatan Gupta             {
1162d577665bSRatan Gupta                 messages::internalError(asyncResp->res);
1163d577665bSRatan Gupta                 return;
1164d577665bSRatan Gupta             }
1165d577665bSRatan Gupta         },
1166d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1167d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1168d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1169d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1170168e20c1SEd Tanous         dbus::utility::DbusVariantType(macAddress));
1171d577665bSRatan Gupta }
1172286b9118SJohnathan Mantey 
11734f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
11744f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
11754f48d5f6SEd Tanous                            const bool v6Value,
11768d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1177da131a9aSJennifer Lee {
11782c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1179da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1180da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1181da131a9aSJennifer Lee             if (ec)
1182da131a9aSJennifer Lee             {
1183da131a9aSJennifer Lee                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1184da131a9aSJennifer Lee                 messages::internalError(asyncResp->res);
1185da131a9aSJennifer Lee                 return;
1186da131a9aSJennifer Lee             }
11878f7e9c19SJayaprakash Mutyala             messages::success(asyncResp->res);
1188da131a9aSJennifer Lee         },
1189da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1190da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1191da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1192da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1193168e20c1SEd Tanous         dbus::utility::DbusVariantType{dhcp});
1194da131a9aSJennifer Lee }
11951f8c7b5dSJohnathan Mantey 
11964f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1197eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
11988d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1199eeedda23SJohnathan Mantey {
1200eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1201eeedda23SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1202eeedda23SJohnathan Mantey             if (ec)
1203eeedda23SJohnathan Mantey             {
1204eeedda23SJohnathan Mantey                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1205eeedda23SJohnathan Mantey                 messages::internalError(asyncResp->res);
1206eeedda23SJohnathan Mantey                 return;
1207eeedda23SJohnathan Mantey             }
1208eeedda23SJohnathan Mantey         },
1209eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1210eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1211eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1212eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1213168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1214eeedda23SJohnathan Mantey }
1215eeedda23SJohnathan Mantey 
12164f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
12178d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1218da131a9aSJennifer Lee {
1219da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1220da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1221da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1222da131a9aSJennifer Lee             if (ec)
1223da131a9aSJennifer Lee             {
1224da131a9aSJennifer Lee                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1225da131a9aSJennifer Lee                 messages::internalError(asyncResp->res);
1226da131a9aSJennifer Lee                 return;
1227da131a9aSJennifer Lee             }
1228da131a9aSJennifer Lee         },
1229da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1230da131a9aSJennifer Lee         "/xyz/openbmc_project/network/config/dhcp",
1231da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1232da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1233168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1234da131a9aSJennifer Lee }
1235d577665bSRatan Gupta 
12364f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
12371f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1238f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1239f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
12408d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1241da131a9aSJennifer Lee {
12421f8c7b5dSJohnathan Mantey     bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1243bf648f77SEd Tanous     bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1244da131a9aSJennifer Lee 
12451f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
12461f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
12471f8c7b5dSJohnathan Mantey 
12481f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
12491f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1250da131a9aSJennifer Lee     {
12511f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
12521f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
12531f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
12541f8c7b5dSJohnathan Mantey         {
1255bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1256bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
12571f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1258da131a9aSJennifer Lee             return;
1259da131a9aSJennifer Lee         }
12601f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
12611f8c7b5dSJohnathan Mantey     }
12621f8c7b5dSJohnathan Mantey     else
1263da131a9aSJennifer Lee     {
12641f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
12651f8c7b5dSJohnathan Mantey     }
12661f8c7b5dSJohnathan Mantey 
12671f8c7b5dSJohnathan Mantey     bool nextDNS{};
12681f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
12691f8c7b5dSJohnathan Mantey     {
12701f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
12711f8c7b5dSJohnathan Mantey         {
12721f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12731f8c7b5dSJohnathan Mantey             return;
12741f8c7b5dSJohnathan Mantey         }
12751f8c7b5dSJohnathan Mantey         nextDNS = *v4dhcpParms.useDNSServers;
12761f8c7b5dSJohnathan Mantey     }
12771f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useDNSServers)
12781f8c7b5dSJohnathan Mantey     {
12791f8c7b5dSJohnathan Mantey         nextDNS = *v4dhcpParms.useDNSServers;
12801f8c7b5dSJohnathan Mantey     }
12811f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useDNSServers)
12821f8c7b5dSJohnathan Mantey     {
12831f8c7b5dSJohnathan Mantey         nextDNS = *v6dhcpParms.useDNSServers;
12841f8c7b5dSJohnathan Mantey     }
12851f8c7b5dSJohnathan Mantey     else
12861f8c7b5dSJohnathan Mantey     {
12871f8c7b5dSJohnathan Mantey         nextDNS = ethData.DNSEnabled;
12881f8c7b5dSJohnathan Mantey     }
12891f8c7b5dSJohnathan Mantey 
12901f8c7b5dSJohnathan Mantey     bool nextNTP{};
12911f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
12921f8c7b5dSJohnathan Mantey     {
12931f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
12941f8c7b5dSJohnathan Mantey         {
12951f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12961f8c7b5dSJohnathan Mantey             return;
12971f8c7b5dSJohnathan Mantey         }
12981f8c7b5dSJohnathan Mantey         nextNTP = *v4dhcpParms.useNTPServers;
12991f8c7b5dSJohnathan Mantey     }
13001f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useNTPServers)
13011f8c7b5dSJohnathan Mantey     {
13021f8c7b5dSJohnathan Mantey         nextNTP = *v4dhcpParms.useNTPServers;
13031f8c7b5dSJohnathan Mantey     }
13041f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useNTPServers)
13051f8c7b5dSJohnathan Mantey     {
13061f8c7b5dSJohnathan Mantey         nextNTP = *v6dhcpParms.useNTPServers;
13071f8c7b5dSJohnathan Mantey     }
13081f8c7b5dSJohnathan Mantey     else
13091f8c7b5dSJohnathan Mantey     {
13101f8c7b5dSJohnathan Mantey         nextNTP = ethData.NTPEnabled;
13111f8c7b5dSJohnathan Mantey     }
13121f8c7b5dSJohnathan Mantey 
13131f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
13141f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
13151f8c7b5dSJohnathan Mantey     {
13161f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
13171f8c7b5dSJohnathan Mantey         {
13181f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
13191f8c7b5dSJohnathan Mantey             return;
13201f8c7b5dSJohnathan Mantey         }
13211f8c7b5dSJohnathan Mantey         nextUseDomain = *v4dhcpParms.useUseDomainName;
13221f8c7b5dSJohnathan Mantey     }
13231f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useUseDomainName)
13241f8c7b5dSJohnathan Mantey     {
13251f8c7b5dSJohnathan Mantey         nextUseDomain = *v4dhcpParms.useUseDomainName;
13261f8c7b5dSJohnathan Mantey     }
13271f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useUseDomainName)
13281f8c7b5dSJohnathan Mantey     {
13291f8c7b5dSJohnathan Mantey         nextUseDomain = *v6dhcpParms.useUseDomainName;
13301f8c7b5dSJohnathan Mantey     }
13311f8c7b5dSJohnathan Mantey     else
13321f8c7b5dSJohnathan Mantey     {
13331f8c7b5dSJohnathan Mantey         nextUseDomain = ethData.HostNameEnabled;
13341f8c7b5dSJohnathan Mantey     }
13351f8c7b5dSJohnathan Mantey 
1336da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
13371f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
13381f8c7b5dSJohnathan Mantey                    asyncResp);
1339da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
13401f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1341da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
13421f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
13431f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
13441f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1345da131a9aSJennifer Lee }
134601784826SJohnathan Mantey 
13474f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator
13482c70f800SEd Tanous     getNextStaticIpEntry(
1349bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1350bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
135101784826SJohnathan Mantey {
135217a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
135317a897dfSManojkiran Eda         return value.origin == "Static";
135417a897dfSManojkiran Eda     });
135501784826SJohnathan Mantey }
135601784826SJohnathan Mantey 
13574f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator
13582c70f800SEd Tanous     getNextStaticIpEntry(
1359bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1360bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
136101784826SJohnathan Mantey {
136217a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
136317a897dfSManojkiran Eda         return value.origin == "Static";
136417a897dfSManojkiran Eda     });
136501784826SJohnathan Mantey }
136601784826SJohnathan Mantey 
13674f48d5f6SEd Tanous inline void handleIPv4StaticPatch(
1368f476acbfSRatan Gupta     const std::string& ifaceId, nlohmann::json& input,
136901784826SJohnathan Mantey     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
13708d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13711abe55efSEd Tanous {
137201784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1373f476acbfSRatan Gupta     {
137471f52d96SEd Tanous         messages::propertyValueTypeError(
137571f52d96SEd Tanous             asyncResp->res,
1376bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1377d1d50814SRavi Teja             "IPv4StaticAddresses");
1378f476acbfSRatan Gupta         return;
1379f476acbfSRatan Gupta     }
1380f476acbfSRatan Gupta 
1381271584abSEd Tanous     unsigned entryIdx = 1;
138201784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
138301784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
138401784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
138501784826SJohnathan Mantey     // into the NIC.
138685ffe86aSJiaqing Zhao     boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
13872c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
138801784826SJohnathan Mantey 
1389537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
13901abe55efSEd Tanous     {
13914a0cb85cSEd Tanous         std::string pathString =
1392d1d50814SRavi Teja             "IPv4StaticAddresses/" + std::to_string(entryIdx);
1393179db1d7SKowalski, Kamil 
139401784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1395f476acbfSRatan Gupta         {
1396537174c4SEd Tanous             std::optional<std::string> address;
1397537174c4SEd Tanous             std::optional<std::string> subnetMask;
1398537174c4SEd Tanous             std::optional<std::string> gateway;
1399537174c4SEd Tanous 
1400537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
14017e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
14027e27d832SJohnathan Mantey                                      "Gateway", gateway))
1403537174c4SEd Tanous             {
140401784826SJohnathan Mantey                 messages::propertyValueFormatError(
140571f52d96SEd Tanous                     asyncResp->res,
140671f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
140771f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
140871f52d96SEd Tanous                     pathString);
1409537174c4SEd Tanous                 return;
1410179db1d7SKowalski, Kamil             }
1411179db1d7SKowalski, Kamil 
141201784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
141301784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
141401784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
141501784826SJohnathan Mantey             // current request.
1416271584abSEd Tanous             const std::string* addr = nullptr;
1417271584abSEd Tanous             const std::string* gw = nullptr;
141801784826SJohnathan Mantey             uint8_t prefixLength = 0;
141901784826SJohnathan Mantey             bool errorInEntry = false;
1420537174c4SEd Tanous             if (address)
14211abe55efSEd Tanous             {
142201784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*address))
14231abe55efSEd Tanous                 {
142401784826SJohnathan Mantey                     addr = &(*address);
14254a0cb85cSEd Tanous                 }
142601784826SJohnathan Mantey                 else
142701784826SJohnathan Mantey                 {
1428bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1429bf648f77SEd Tanous                                                        pathString + "/Address");
143001784826SJohnathan Mantey                     errorInEntry = true;
143101784826SJohnathan Mantey                 }
143201784826SJohnathan Mantey             }
143385ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
143401784826SJohnathan Mantey             {
143585ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
143601784826SJohnathan Mantey             }
143701784826SJohnathan Mantey             else
143801784826SJohnathan Mantey             {
143901784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
144001784826SJohnathan Mantey                                           pathString + "/Address");
144101784826SJohnathan Mantey                 errorInEntry = true;
14424a0cb85cSEd Tanous             }
14434a0cb85cSEd Tanous 
1444537174c4SEd Tanous             if (subnetMask)
14454a0cb85cSEd Tanous             {
1446537174c4SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
14474a0cb85cSEd Tanous                 {
1448f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1449537174c4SEd Tanous                         asyncResp->res, *subnetMask,
14504a0cb85cSEd Tanous                         pathString + "/SubnetMask");
145101784826SJohnathan Mantey                     errorInEntry = true;
14524a0cb85cSEd Tanous                 }
14534a0cb85cSEd Tanous             }
145485ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
14554a0cb85cSEd Tanous             {
145685ffe86aSJiaqing Zhao                 if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
145701784826SJohnathan Mantey                                                 &prefixLength))
14584a0cb85cSEd Tanous                 {
145901784826SJohnathan Mantey                     messages::propertyValueFormatError(
146085ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
146101784826SJohnathan Mantey                         pathString + "/SubnetMask");
146201784826SJohnathan Mantey                     errorInEntry = true;
14634a0cb85cSEd Tanous                 }
14644a0cb85cSEd Tanous             }
14651abe55efSEd Tanous             else
14661abe55efSEd Tanous             {
146701784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
146801784826SJohnathan Mantey                                           pathString + "/SubnetMask");
146901784826SJohnathan Mantey                 errorInEntry = true;
147001784826SJohnathan Mantey             }
147101784826SJohnathan Mantey 
147201784826SJohnathan Mantey             if (gateway)
147301784826SJohnathan Mantey             {
147401784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*gateway))
147501784826SJohnathan Mantey                 {
147601784826SJohnathan Mantey                     gw = &(*gateway);
147701784826SJohnathan Mantey                 }
147801784826SJohnathan Mantey                 else
147901784826SJohnathan Mantey                 {
1480bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1481bf648f77SEd Tanous                                                        pathString + "/Gateway");
148201784826SJohnathan Mantey                     errorInEntry = true;
148301784826SJohnathan Mantey                 }
148401784826SJohnathan Mantey             }
148585ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
148601784826SJohnathan Mantey             {
148785ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
148801784826SJohnathan Mantey             }
148901784826SJohnathan Mantey             else
14901abe55efSEd Tanous             {
1491a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
14924a0cb85cSEd Tanous                                           pathString + "/Gateway");
149301784826SJohnathan Mantey                 errorInEntry = true;
14944a0cb85cSEd Tanous             }
14954a0cb85cSEd Tanous 
149601784826SJohnathan Mantey             if (errorInEntry)
14971abe55efSEd Tanous             {
149801784826SJohnathan Mantey                 return;
14994a0cb85cSEd Tanous             }
15004a0cb85cSEd Tanous 
150185ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
15021abe55efSEd Tanous             {
150385ffe86aSJiaqing Zhao                 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
1504bf648f77SEd Tanous                                     *addr, asyncResp);
150585ffe86aSJiaqing Zhao                 nicIpEntry =
150685ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
1507588c3f0dSKowalski, Kamil             }
150801784826SJohnathan Mantey             else
150901784826SJohnathan Mantey             {
1510cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1511cb13a392SEd Tanous                            asyncResp);
15124a0cb85cSEd Tanous             }
15134a0cb85cSEd Tanous             entryIdx++;
15144a0cb85cSEd Tanous         }
151501784826SJohnathan Mantey         else
151601784826SJohnathan Mantey         {
151785ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
151801784826SJohnathan Mantey             {
151901784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
152001784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
152101784826SJohnathan Mantey                 // in error, so bail out.
152201784826SJohnathan Mantey                 if (thisJson.is_null())
152301784826SJohnathan Mantey                 {
152401784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
152501784826SJohnathan Mantey                     return;
152601784826SJohnathan Mantey                 }
152701784826SJohnathan Mantey                 messages::propertyValueFormatError(
152871f52d96SEd Tanous                     asyncResp->res,
152971f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
153071f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
153171f52d96SEd Tanous                     pathString);
153201784826SJohnathan Mantey                 return;
153301784826SJohnathan Mantey             }
153401784826SJohnathan Mantey 
153501784826SJohnathan Mantey             if (thisJson.is_null())
153601784826SJohnathan Mantey             {
153785ffe86aSJiaqing Zhao                 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
153801784826SJohnathan Mantey             }
153985ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
154001784826SJohnathan Mantey             {
154185ffe86aSJiaqing Zhao                 nicIpEntry =
154285ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
154301784826SJohnathan Mantey             }
154401784826SJohnathan Mantey             entryIdx++;
154501784826SJohnathan Mantey         }
154601784826SJohnathan Mantey     }
15474a0cb85cSEd Tanous }
15484a0cb85cSEd Tanous 
15494f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1550f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1551f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
15528d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1553f85837bfSRAJESWARAN THILLAIGOVINDAN {
1554f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
1555286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1556f85837bfSRAJESWARAN THILLAIGOVINDAN             if (ec)
1557f85837bfSRAJESWARAN THILLAIGOVINDAN             {
1558f85837bfSRAJESWARAN THILLAIGOVINDAN                 messages::internalError(asyncResp->res);
1559f85837bfSRAJESWARAN THILLAIGOVINDAN                 return;
1560f85837bfSRAJESWARAN THILLAIGOVINDAN             }
1561f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1562f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1563f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1564f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1565bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1566168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1567f85837bfSRAJESWARAN THILLAIGOVINDAN }
1568f85837bfSRAJESWARAN THILLAIGOVINDAN 
15694f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1570f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
157101784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data,
15728d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1573e48c0fc5SRavi Teja {
157401784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1575e48c0fc5SRavi Teja     {
157671f52d96SEd Tanous         messages::propertyValueTypeError(
157771f52d96SEd Tanous             asyncResp->res,
1578bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1579e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1580e48c0fc5SRavi Teja         return;
1581e48c0fc5SRavi Teja     }
1582271584abSEd Tanous     size_t entryIdx = 1;
158385ffe86aSJiaqing Zhao     boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
15842c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1585f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1586e48c0fc5SRavi Teja     {
1587e48c0fc5SRavi Teja         std::string pathString =
1588e48c0fc5SRavi Teja             "IPv6StaticAddresses/" + std::to_string(entryIdx);
1589e48c0fc5SRavi Teja 
159001784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1591e48c0fc5SRavi Teja         {
1592e48c0fc5SRavi Teja             std::optional<std::string> address;
1593e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1594f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1595bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1596bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1597e48c0fc5SRavi Teja             {
159801784826SJohnathan Mantey                 messages::propertyValueFormatError(
159971f52d96SEd Tanous                     asyncResp->res,
160071f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
160171f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
160271f52d96SEd Tanous                     pathString);
1603e48c0fc5SRavi Teja                 return;
1604e48c0fc5SRavi Teja             }
1605e48c0fc5SRavi Teja 
1606543f4400SEd Tanous             const std::string* addr = nullptr;
1607543f4400SEd Tanous             uint8_t prefix = 0;
160801784826SJohnathan Mantey 
160901784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
161001784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
161101784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
161201784826SJohnathan Mantey             // current request.
1613e48c0fc5SRavi Teja             if (address)
1614e48c0fc5SRavi Teja             {
161501784826SJohnathan Mantey                 addr = &(*address);
1616e48c0fc5SRavi Teja             }
161785ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
161801784826SJohnathan Mantey             {
161985ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
162001784826SJohnathan Mantey             }
162101784826SJohnathan Mantey             else
162201784826SJohnathan Mantey             {
162301784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
162401784826SJohnathan Mantey                                           pathString + "/Address");
162501784826SJohnathan Mantey                 return;
1626e48c0fc5SRavi Teja             }
1627e48c0fc5SRavi Teja 
1628e48c0fc5SRavi Teja             if (prefixLength)
1629e48c0fc5SRavi Teja             {
163001784826SJohnathan Mantey                 prefix = *prefixLength;
163101784826SJohnathan Mantey             }
163285ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1633e48c0fc5SRavi Teja             {
163485ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1635e48c0fc5SRavi Teja             }
1636e48c0fc5SRavi Teja             else
1637e48c0fc5SRavi Teja             {
1638e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1639e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
164001784826SJohnathan Mantey                 return;
1641e48c0fc5SRavi Teja             }
1642e48c0fc5SRavi Teja 
164385ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1644e48c0fc5SRavi Teja             {
164585ffe86aSJiaqing Zhao                 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
1646e48c0fc5SRavi Teja                                     asyncResp);
164785ffe86aSJiaqing Zhao                 nicIpEntry =
164885ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
164901784826SJohnathan Mantey             }
165001784826SJohnathan Mantey             else
165101784826SJohnathan Mantey             {
165201784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1653e48c0fc5SRavi Teja             }
1654e48c0fc5SRavi Teja             entryIdx++;
1655e48c0fc5SRavi Teja         }
165601784826SJohnathan Mantey         else
165701784826SJohnathan Mantey         {
165885ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
165901784826SJohnathan Mantey             {
166001784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
166101784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
166201784826SJohnathan Mantey                 // in error, so bail out.
166301784826SJohnathan Mantey                 if (thisJson.is_null())
166401784826SJohnathan Mantey                 {
166501784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
166601784826SJohnathan Mantey                     return;
166701784826SJohnathan Mantey                 }
166801784826SJohnathan Mantey                 messages::propertyValueFormatError(
166971f52d96SEd Tanous                     asyncResp->res,
167071f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
167171f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
167271f52d96SEd Tanous                     pathString);
167301784826SJohnathan Mantey                 return;
167401784826SJohnathan Mantey             }
167501784826SJohnathan Mantey 
167601784826SJohnathan Mantey             if (thisJson.is_null())
167701784826SJohnathan Mantey             {
167885ffe86aSJiaqing Zhao                 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
167901784826SJohnathan Mantey             }
168085ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
168101784826SJohnathan Mantey             {
168285ffe86aSJiaqing Zhao                 nicIpEntry =
168385ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
168401784826SJohnathan Mantey             }
168501784826SJohnathan Mantey             entryIdx++;
168601784826SJohnathan Mantey         }
168701784826SJohnathan Mantey     }
1688e48c0fc5SRavi Teja }
1689e48c0fc5SRavi Teja 
16904f48d5f6SEd Tanous inline void parseInterfaceData(
16918d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16928d1b46d7Szhanghch05     const std::string& ifaceId, const EthernetInterfaceData& ethData,
1693e48c0fc5SRavi Teja     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
169401784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data)
16954a0cb85cSEd Tanous {
1696eeedda23SJohnathan Mantey     constexpr const std::array<const char*, 1> inventoryForEthernet = {
1697eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1698eeedda23SJohnathan Mantey 
16992c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
170081ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
17012c70f800SEd Tanous     jsonResponse["@odata.id"] =
170281ce609eSEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
17032c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1704eeedda23SJohnathan Mantey 
1705eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1706eeedda23SJohnathan Mantey 
1707eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1708eeedda23SJohnathan Mantey         [health](const boost::system::error_code ec,
1709b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1710eeedda23SJohnathan Mantey             if (ec)
1711029573d4SEd Tanous             {
1712eeedda23SJohnathan Mantey                 return;
1713eeedda23SJohnathan Mantey             }
1714eeedda23SJohnathan Mantey 
1715914e2d5dSEd Tanous             health->inventory = resp;
1716eeedda23SJohnathan Mantey         },
1717eeedda23SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper",
1718eeedda23SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
1719bf648f77SEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1720bf648f77SEd Tanous         inventoryForEthernet);
1721eeedda23SJohnathan Mantey 
1722eeedda23SJohnathan Mantey     health->populate();
1723eeedda23SJohnathan Mantey 
1724eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1725eeedda23SJohnathan Mantey     {
17262c70f800SEd Tanous         jsonResponse["LinkStatus"] = "LinkUp";
17272c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1728029573d4SEd Tanous     }
1729029573d4SEd Tanous     else
1730029573d4SEd Tanous     {
17312c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
17322c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1733029573d4SEd Tanous     }
1734aa05fb27SJohnathan Mantey 
17352c70f800SEd Tanous     jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
17362c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
173735fb5311STejas Patil     jsonResponse["MTUSize"] = ethData.mtuSize;
17382c70f800SEd Tanous     jsonResponse["MACAddress"] = ethData.mac_address;
17392c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
17401f8c7b5dSJohnathan Mantey         translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
17412c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
17422c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
17432c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
17441f8c7b5dSJohnathan Mantey 
17452c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
17461f8c7b5dSJohnathan Mantey         translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
17471f8c7b5dSJohnathan Mantey                                                                : "Disabled";
17482c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
17492c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
17502c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
17512a133282Smanojkiraneda 
17524a0cb85cSEd Tanous     if (!ethData.hostname.empty())
17534a0cb85cSEd Tanous     {
17542c70f800SEd Tanous         jsonResponse["HostName"] = ethData.hostname;
1755ab6554f1SJoshi-Mansi 
1756ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1757ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1758ab6554f1SJoshi-Mansi         // FQDN
1759f23b7296SEd Tanous         std::string fqdn = ethData.hostname;
1760d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1761d24bfc7aSJennifer Lee         {
17622c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1763d24bfc7aSJennifer Lee         }
17642c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
17654a0cb85cSEd Tanous     }
17664a0cb85cSEd Tanous 
17672c70f800SEd Tanous     jsonResponse["VLANs"] = {
1768bf648f77SEd Tanous         {"@odata.id",
1769bf648f77SEd Tanous          "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1770fda13ad2SSunitha Harish 
17712c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
17722c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
17734a0cb85cSEd Tanous 
17742c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
17752c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
17762c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
17772c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
17789eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
17794a0cb85cSEd Tanous     {
1780fa5053a6SGunnar Mills 
17812c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1782fa5053a6SGunnar Mills         if (gatewayStr.empty())
1783fa5053a6SGunnar Mills         {
1784fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1785fa5053a6SGunnar Mills         }
1786fa5053a6SGunnar Mills 
17872c70f800SEd Tanous         ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
17882c70f800SEd Tanous                              {"SubnetMask", ipv4Config.netmask},
17892c70f800SEd Tanous                              {"Address", ipv4Config.address},
1790fa5053a6SGunnar Mills                              {"Gateway", gatewayStr}});
17912c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1792d1d50814SRavi Teja         {
17932c70f800SEd Tanous             ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
17942c70f800SEd Tanous                                        {"SubnetMask", ipv4Config.netmask},
17952c70f800SEd Tanous                                        {"Address", ipv4Config.address},
1796d1d50814SRavi Teja                                        {"Gateway", gatewayStr}});
1797d1d50814SRavi Teja         }
179801784826SJohnathan Mantey     }
1799d1d50814SRavi Teja 
18007ea79e5eSRavi Teja     std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
18017ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
18027ea79e5eSRavi Teja     {
18037ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
18047ea79e5eSRavi Teja     }
18057ea79e5eSRavi Teja 
18067ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1807e48c0fc5SRavi Teja 
18082c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
18092c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
18102c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
18112c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
18127f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
18132c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
18147f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
18159eb808c1SEd Tanous     for (const auto& ipv6Config : ipv6Data)
1816e48c0fc5SRavi Teja     {
18172c70f800SEd Tanous         ipv6Array.push_back({{"Address", ipv6Config.address},
18182c70f800SEd Tanous                              {"PrefixLength", ipv6Config.prefixLength},
18192c70f800SEd Tanous                              {"AddressOrigin", ipv6Config.origin},
18205fd16e4bSJohnathan Mantey                              {"AddressState", nullptr}});
18212c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1822e48c0fc5SRavi Teja         {
18232c70f800SEd Tanous             ipv6StaticArray.push_back(
18242c70f800SEd Tanous                 {{"Address", ipv6Config.address},
18257a474a5fSJiaqing Zhao                  {"PrefixLength", ipv6Config.prefixLength}});
182601784826SJohnathan Mantey         }
1827e48c0fc5SRavi Teja     }
1828588c3f0dSKowalski, Kamil }
1829588c3f0dSKowalski, Kamil 
18304f48d5f6SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse,
1831bf648f77SEd Tanous                                const std::string& parentIfaceId,
1832bf648f77SEd Tanous                                const std::string& ifaceId,
1833bf648f77SEd Tanous                                const EthernetInterfaceData& ethData)
18341abe55efSEd Tanous {
1835bf648f77SEd Tanous     // Fill out obvious data...
1836bf648f77SEd Tanous     jsonResponse["Id"] = ifaceId;
1837bf648f77SEd Tanous     jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1838bf648f77SEd Tanous                                 parentIfaceId + "/VLANs/" + ifaceId;
1839bf648f77SEd Tanous 
1840bf648f77SEd Tanous     jsonResponse["VLANEnable"] = true;
1841bf648f77SEd Tanous     if (!ethData.vlan_id.empty())
1842bf648f77SEd Tanous     {
1843bf648f77SEd Tanous         jsonResponse["VLANId"] = ethData.vlan_id.back();
1844bf648f77SEd Tanous     }
1845bf648f77SEd Tanous }
1846bf648f77SEd Tanous 
18474f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1848bf648f77SEd Tanous {
1849dcf2ebc0SEd Tanous     return boost::starts_with(iface, parent + "_");
1850bf648f77SEd Tanous }
1851bf648f77SEd Tanous 
1852bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1853bf648f77SEd Tanous {
1854bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1855ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
1856*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
1857*45ca1b86SEd Tanous                                                        const std::shared_ptr<
1858*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
1859*45ca1b86SEd Tanous                                                            asyncResp) {
1860*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1861*45ca1b86SEd Tanous             {
1862*45ca1b86SEd Tanous                 return;
1863*45ca1b86SEd Tanous             }
1864*45ca1b86SEd Tanous 
1865bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1866bf648f77SEd Tanous                 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1867bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1868bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1869bf648f77SEd Tanous             asyncResp->res.jsonValue["Name"] =
1870bf648f77SEd Tanous                 "Ethernet Network Interface Collection";
1871bf648f77SEd Tanous             asyncResp->res.jsonValue["Description"] =
1872bf648f77SEd Tanous                 "Collection of EthernetInterfaces for this Manager";
1873bf648f77SEd Tanous 
1874bf648f77SEd Tanous             // Get eth interface list, and call the below callback for JSON
1875bf648f77SEd Tanous             // preparation
1876bf648f77SEd Tanous             getEthernetIfaceList([asyncResp](const bool& success,
1877bf648f77SEd Tanous                                              const boost::container::flat_set<
1878bf648f77SEd Tanous                                                  std::string>& ifaceList) {
1879bf648f77SEd Tanous                 if (!success)
18801abe55efSEd Tanous                 {
1881f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
18829391bb9cSRapkiewicz, Pawel                     return;
18839391bb9cSRapkiewicz, Pawel                 }
18849391bb9cSRapkiewicz, Pawel 
1885bf648f77SEd Tanous                 nlohmann::json& ifaceArray =
1886bf648f77SEd Tanous                     asyncResp->res.jsonValue["Members"];
1887bf648f77SEd Tanous                 ifaceArray = nlohmann::json::array();
1888bf648f77SEd Tanous                 std::string tag = "_";
1889bf648f77SEd Tanous                 for (const std::string& ifaceItem : ifaceList)
1890bf648f77SEd Tanous                 {
1891bf648f77SEd Tanous                     std::size_t found = ifaceItem.find(tag);
1892bf648f77SEd Tanous                     if (found == std::string::npos)
1893bf648f77SEd Tanous                     {
1894bf648f77SEd Tanous                         ifaceArray.push_back(
1895bf648f77SEd Tanous                             {{"@odata.id",
1896bf648f77SEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1897bf648f77SEd Tanous                                   ifaceItem}});
1898bf648f77SEd Tanous                     }
1899bf648f77SEd Tanous                 }
1900bf648f77SEd Tanous 
1901bf648f77SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
1902bf648f77SEd Tanous                     ifaceArray.size();
1903bf648f77SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
1904bf648f77SEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
1905bf648f77SEd Tanous             });
1906bf648f77SEd Tanous         });
1907bf648f77SEd Tanous 
1908bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1909ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1910bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
1911*45ca1b86SEd Tanous             [&app](const crow::Request& req,
1912bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1913bf648f77SEd Tanous                    const std::string& ifaceId) {
1914*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1915*45ca1b86SEd Tanous                 {
1916*45ca1b86SEd Tanous                     return;
1917*45ca1b86SEd Tanous                 }
19184a0cb85cSEd Tanous                 getEthernetIfaceData(
1919bf648f77SEd Tanous                     ifaceId,
1920bf648f77SEd Tanous                     [asyncResp,
1921bf648f77SEd Tanous                      ifaceId](const bool& success,
1922bf648f77SEd Tanous                               const EthernetInterfaceData& ethData,
1923bf648f77SEd Tanous                               const boost::container::flat_set<IPv4AddressData>&
1924bf648f77SEd Tanous                                   ipv4Data,
1925bf648f77SEd Tanous                               const boost::container::flat_set<IPv6AddressData>&
1926bf648f77SEd Tanous                                   ipv6Data) {
19274a0cb85cSEd Tanous                         if (!success)
19281abe55efSEd Tanous                         {
1929bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
1930bf648f77SEd Tanous                             // existing object, and other errors
1931bf648f77SEd Tanous                             messages::resourceNotFound(
1932bf648f77SEd Tanous                                 asyncResp->res, "EthernetInterface", ifaceId);
19334a0cb85cSEd Tanous                             return;
19349391bb9cSRapkiewicz, Pawel                         }
19354c9afe43SEd Tanous 
19360f74e643SEd Tanous                         asyncResp->res.jsonValue["@odata.type"] =
1937fda13ad2SSunitha Harish                             "#EthernetInterface.v1_4_1.EthernetInterface";
1938bf648f77SEd Tanous                         asyncResp->res.jsonValue["Name"] =
1939bf648f77SEd Tanous                             "Manager Ethernet Interface";
19400f74e643SEd Tanous                         asyncResp->res.jsonValue["Description"] =
19410f74e643SEd Tanous                             "Management Network Interface";
19420f74e643SEd Tanous 
1943bf648f77SEd Tanous                         parseInterfaceData(asyncResp, ifaceId, ethData,
1944bf648f77SEd Tanous                                            ipv4Data, ipv6Data);
19459391bb9cSRapkiewicz, Pawel                     });
1946bf648f77SEd Tanous             });
19479391bb9cSRapkiewicz, Pawel 
1948bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1949ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1950ed398213SEd Tanous 
1951bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
1952*45ca1b86SEd Tanous             [&app](const crow::Request& req,
1953bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1954bf648f77SEd Tanous                    const std::string& ifaceId) {
1955*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1956*45ca1b86SEd Tanous                 {
1957*45ca1b86SEd Tanous                     return;
1958*45ca1b86SEd Tanous                 }
1959bc0bd6e0SEd Tanous                 std::optional<std::string> hostname;
1960ab6554f1SJoshi-Mansi                 std::optional<std::string> fqdn;
1961d577665bSRatan Gupta                 std::optional<std::string> macAddress;
19629a6fc6feSRavi Teja                 std::optional<std::string> ipv6DefaultGateway;
1963d1d50814SRavi Teja                 std::optional<nlohmann::json> ipv4StaticAddresses;
1964e48c0fc5SRavi Teja                 std::optional<nlohmann::json> ipv6StaticAddresses;
1965f85837bfSRAJESWARAN THILLAIGOVINDAN                 std::optional<std::vector<std::string>> staticNameServers;
1966da131a9aSJennifer Lee                 std::optional<nlohmann::json> dhcpv4;
19671f8c7b5dSJohnathan Mantey                 std::optional<nlohmann::json> dhcpv6;
1968eeedda23SJohnathan Mantey                 std::optional<bool> interfaceEnabled;
196935fb5311STejas Patil                 std::optional<size_t> mtuSize;
19701f8c7b5dSJohnathan Mantey                 DHCPParameters v4dhcpParms;
19711f8c7b5dSJohnathan Mantey                 DHCPParameters v6dhcpParms;
19720627a2c7SEd Tanous 
197315ed6780SWilly Tu                 if (!json_util::readJsonPatch(
19748d1b46d7Szhanghch05                         req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1975bf648f77SEd Tanous                         "IPv4StaticAddresses", ipv4StaticAddresses,
1976bf648f77SEd Tanous                         "MACAddress", macAddress, "StaticNameServers",
1977bf648f77SEd Tanous                         staticNameServers, "IPv6DefaultGateway",
1978bf648f77SEd Tanous                         ipv6DefaultGateway, "IPv6StaticAddresses",
1979ab6554f1SJoshi-Mansi                         ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
198035fb5311STejas Patil                         "MTUSize", mtuSize, "InterfaceEnabled",
198135fb5311STejas Patil                         interfaceEnabled))
19821abe55efSEd Tanous                 {
1983588c3f0dSKowalski, Kamil                     return;
1984588c3f0dSKowalski, Kamil                 }
1985da131a9aSJennifer Lee                 if (dhcpv4)
1986da131a9aSJennifer Lee                 {
1987bf648f77SEd Tanous                     if (!json_util::readJson(
1988bf648f77SEd Tanous                             *dhcpv4, asyncResp->res, "DHCPEnabled",
19891f8c7b5dSJohnathan Mantey                             v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
19901f8c7b5dSJohnathan Mantey                             v4dhcpParms.useDNSServers, "UseNTPServers",
19911f8c7b5dSJohnathan Mantey                             v4dhcpParms.useNTPServers, "UseDomainName",
19921f8c7b5dSJohnathan Mantey                             v4dhcpParms.useUseDomainName))
19931f8c7b5dSJohnathan Mantey                     {
19941f8c7b5dSJohnathan Mantey                         return;
19951f8c7b5dSJohnathan Mantey                     }
19961f8c7b5dSJohnathan Mantey                 }
19971f8c7b5dSJohnathan Mantey 
19981f8c7b5dSJohnathan Mantey                 if (dhcpv6)
19991f8c7b5dSJohnathan Mantey                 {
2000bf648f77SEd Tanous                     if (!json_util::readJson(
2001bf648f77SEd Tanous                             *dhcpv6, asyncResp->res, "OperatingMode",
2002bf648f77SEd Tanous                             v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers",
2003bf648f77SEd Tanous                             v6dhcpParms.useDNSServers, "UseNTPServers",
2004bf648f77SEd Tanous                             v6dhcpParms.useNTPServers, "UseDomainName",
20051f8c7b5dSJohnathan Mantey                             v6dhcpParms.useUseDomainName))
20061f8c7b5dSJohnathan Mantey                     {
20071f8c7b5dSJohnathan Mantey                         return;
20081f8c7b5dSJohnathan Mantey                     }
2009da131a9aSJennifer Lee                 }
2010da131a9aSJennifer Lee 
2011bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2012bf648f77SEd Tanous                 // for JSON preparation
20134a0cb85cSEd Tanous                 getEthernetIfaceData(
20142c70f800SEd Tanous                     ifaceId,
2015bf648f77SEd Tanous                     [asyncResp, ifaceId, hostname = std::move(hostname),
2016ab6554f1SJoshi-Mansi                      fqdn = std::move(fqdn), macAddress = std::move(macAddress),
2017d1d50814SRavi Teja                      ipv4StaticAddresses = std::move(ipv4StaticAddresses),
20189a6fc6feSRavi Teja                      ipv6DefaultGateway = std::move(ipv6DefaultGateway),
2019e48c0fc5SRavi Teja                      ipv6StaticAddresses = std::move(ipv6StaticAddresses),
20201f8c7b5dSJohnathan Mantey                      staticNameServers = std::move(staticNameServers),
20211f8c7b5dSJohnathan Mantey                      dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
202235fb5311STejas Patil                      mtuSize = mtuSize, v4dhcpParms = std::move(v4dhcpParms),
2023f23b7296SEd Tanous                      v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2024bf648f77SEd Tanous                         const bool& success,
2025bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2026bf648f77SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&
2027bf648f77SEd Tanous                             ipv4Data,
2028bf648f77SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&
2029bf648f77SEd Tanous                             ipv6Data) {
20301abe55efSEd Tanous                         if (!success)
20311abe55efSEd Tanous                         {
2032588c3f0dSKowalski, Kamil                             // ... otherwise return error
2033bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2034bf648f77SEd Tanous                             // existing object, and other errors
2035bf648f77SEd Tanous                             messages::resourceNotFound(
2036bf648f77SEd Tanous                                 asyncResp->res, "Ethernet Interface", ifaceId);
2037588c3f0dSKowalski, Kamil                             return;
2038588c3f0dSKowalski, Kamil                         }
2039588c3f0dSKowalski, Kamil 
20401f8c7b5dSJohnathan Mantey                         if (dhcpv4 || dhcpv6)
20411f8c7b5dSJohnathan Mantey                         {
2042bf648f77SEd Tanous                             handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2043bf648f77SEd Tanous                                             v6dhcpParms, asyncResp);
20441f8c7b5dSJohnathan Mantey                         }
20451f8c7b5dSJohnathan Mantey 
20460627a2c7SEd Tanous                         if (hostname)
20471abe55efSEd Tanous                         {
20480627a2c7SEd Tanous                             handleHostnamePatch(*hostname, asyncResp);
20491abe55efSEd Tanous                         }
20500627a2c7SEd Tanous 
2051ab6554f1SJoshi-Mansi                         if (fqdn)
2052ab6554f1SJoshi-Mansi                         {
20532c70f800SEd Tanous                             handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2054ab6554f1SJoshi-Mansi                         }
2055ab6554f1SJoshi-Mansi 
2056d577665bSRatan Gupta                         if (macAddress)
2057d577665bSRatan Gupta                         {
2058bf648f77SEd Tanous                             handleMACAddressPatch(ifaceId, *macAddress,
2059bf648f77SEd Tanous                                                   asyncResp);
2060d577665bSRatan Gupta                         }
2061d577665bSRatan Gupta 
2062d1d50814SRavi Teja                         if (ipv4StaticAddresses)
2063d1d50814SRavi Teja                         {
2064bf648f77SEd Tanous                             // TODO(ed) for some reason the capture of
2065bf648f77SEd Tanous                             // ipv4Addresses above is returning a const value,
2066bf648f77SEd Tanous                             // not a non-const value. This doesn't really work
2067bf648f77SEd Tanous                             // for us, as we need to be able to efficiently move
2068bf648f77SEd Tanous                             // out the intermedia nlohmann::json objects. This
2069bf648f77SEd Tanous                             // makes a copy of the structure, and operates on
2070bf648f77SEd Tanous                             // that, but could be done more efficiently
2071f23b7296SEd Tanous                             nlohmann::json ipv4Static = *ipv4StaticAddresses;
20722c70f800SEd Tanous                             handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2073d1d50814SRavi Teja                                                   asyncResp);
20741abe55efSEd Tanous                         }
20750627a2c7SEd Tanous 
2076f85837bfSRAJESWARAN THILLAIGOVINDAN                         if (staticNameServers)
2077f85837bfSRAJESWARAN THILLAIGOVINDAN                         {
2078bf648f77SEd Tanous                             handleStaticNameServersPatch(
2079bf648f77SEd Tanous                                 ifaceId, *staticNameServers, asyncResp);
2080f85837bfSRAJESWARAN THILLAIGOVINDAN                         }
20819a6fc6feSRavi Teja 
20829a6fc6feSRavi Teja                         if (ipv6DefaultGateway)
20839a6fc6feSRavi Teja                         {
20849a6fc6feSRavi Teja                             messages::propertyNotWritable(asyncResp->res,
20859a6fc6feSRavi Teja                                                           "IPv6DefaultGateway");
20869a6fc6feSRavi Teja                         }
2087e48c0fc5SRavi Teja 
2088e48c0fc5SRavi Teja                         if (ipv6StaticAddresses)
2089e48c0fc5SRavi Teja                         {
2090f5b191a6SEd Tanous                             const nlohmann::json& ipv6Static =
2091f5b191a6SEd Tanous                                 *ipv6StaticAddresses;
20922c70f800SEd Tanous                             handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
209301784826SJohnathan Mantey                                                            ipv6Data, asyncResp);
2094e48c0fc5SRavi Teja                         }
2095eeedda23SJohnathan Mantey 
2096eeedda23SJohnathan Mantey                         if (interfaceEnabled)
2097eeedda23SJohnathan Mantey                         {
2098eeedda23SJohnathan Mantey                             setEthernetInterfaceBoolProperty(
2099bf648f77SEd Tanous                                 ifaceId, "NICEnabled", *interfaceEnabled,
2100bf648f77SEd Tanous                                 asyncResp);
2101eeedda23SJohnathan Mantey                         }
210235fb5311STejas Patil 
210335fb5311STejas Patil                         if (mtuSize)
210435fb5311STejas Patil                         {
210535fb5311STejas Patil                             handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
210635fb5311STejas Patil                         }
2107588c3f0dSKowalski, Kamil                     });
2108bf648f77SEd Tanous             });
21099391bb9cSRapkiewicz, Pawel 
2110bf648f77SEd Tanous     BMCWEB_ROUTE(
2111bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2112ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
2113bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
2114*45ca1b86SEd Tanous             [&app](const crow::Request& req,
2115bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2116*45ca1b86SEd Tanous                    const std::string& parentIfaceId,
2117*45ca1b86SEd Tanous                    const std::string& ifaceId) {
2118*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2119*45ca1b86SEd Tanous                 {
2120*45ca1b86SEd Tanous                     return;
2121*45ca1b86SEd Tanous                 }
21228d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
21230f74e643SEd Tanous                     "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
21248d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
2125e439f0f8SKowalski, Kamil 
21262c70f800SEd Tanous                 if (!verifyNames(parentIfaceId, ifaceId))
21271abe55efSEd Tanous                 {
2128a434f2bdSEd Tanous                     return;
2129a434f2bdSEd Tanous                 }
2130a434f2bdSEd Tanous 
2131bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2132bf648f77SEd Tanous                 // for JSON preparation
21334a0cb85cSEd Tanous                 getEthernetIfaceData(
2134bf648f77SEd Tanous                     ifaceId,
2135bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId](
2136bf648f77SEd Tanous                         const bool& success,
2137bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2138cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2139cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
214026f6976fSEd Tanous                         if (success && !ethData.vlan_id.empty())
21411abe55efSEd Tanous                         {
2142bf648f77SEd Tanous                             parseInterfaceData(asyncResp->res.jsonValue,
2143bf648f77SEd Tanous                                                parentIfaceId, ifaceId, ethData);
21441abe55efSEd Tanous                         }
21451abe55efSEd Tanous                         else
21461abe55efSEd Tanous                         {
2147e439f0f8SKowalski, Kamil                             // ... otherwise return error
2148bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2149bf648f77SEd Tanous                             // existing object, and other errors
2150bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2151bf648f77SEd Tanous                                                        "VLAN Network Interface",
2152bf648f77SEd Tanous                                                        ifaceId);
2153e439f0f8SKowalski, Kamil                         }
2154e439f0f8SKowalski, Kamil                     });
2155bf648f77SEd Tanous             });
2156e439f0f8SKowalski, Kamil 
2157bf648f77SEd Tanous     BMCWEB_ROUTE(
2158bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2159ed398213SEd Tanous         // This privilege is incorrect, it should be ConfigureManager
2160ed398213SEd Tanous         //.privileges(redfish::privileges::patchVLanNetworkInterface)
2161432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2162bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
2163*45ca1b86SEd Tanous             [&app](const crow::Request& req,
2164bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2165*45ca1b86SEd Tanous                    const std::string& parentIfaceId,
2166*45ca1b86SEd Tanous                    const std::string& ifaceId) {
2167*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2168*45ca1b86SEd Tanous                 {
2169*45ca1b86SEd Tanous                     return;
2170*45ca1b86SEd Tanous                 }
2171fda13ad2SSunitha Harish                 if (!verifyNames(parentIfaceId, ifaceId))
21721abe55efSEd Tanous                 {
2173bf648f77SEd Tanous                     messages::resourceNotFound(
2174bf648f77SEd Tanous                         asyncResp->res, "VLAN Network Interface", ifaceId);
2175927a505aSKowalski, Kamil                     return;
2176927a505aSKowalski, Kamil                 }
2177927a505aSKowalski, Kamil 
21780627a2c7SEd Tanous                 bool vlanEnable = false;
217938268fa8SAndrew Geissler                 uint32_t vlanId = 0;
21800627a2c7SEd Tanous 
218115ed6780SWilly Tu                 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2182bf648f77SEd Tanous                                               vlanEnable, "VLANId", vlanId))
21831abe55efSEd Tanous                 {
2184927a505aSKowalski, Kamil                     return;
2185927a505aSKowalski, Kamil                 }
2186927a505aSKowalski, Kamil 
2187bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2188bf648f77SEd Tanous                 // for JSON preparation
2189e48c0fc5SRavi Teja                 getEthernetIfaceData(
2190bf648f77SEd Tanous                     ifaceId,
2191bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId](
2192bf648f77SEd Tanous                         const bool& success,
2193bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2194cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2195cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
219608244d02SSunitha Harish                         if (success && !ethData.vlan_id.empty())
219708244d02SSunitha Harish                         {
219808244d02SSunitha Harish                             auto callback =
2199bf648f77SEd Tanous                                 [asyncResp](
2200bf648f77SEd Tanous                                     const boost::system::error_code ec) {
220108244d02SSunitha Harish                                     if (ec)
220208244d02SSunitha Harish                                     {
220308244d02SSunitha Harish                                         messages::internalError(asyncResp->res);
220408244d02SSunitha Harish                                     }
220508244d02SSunitha Harish                                 };
220608244d02SSunitha Harish 
2207e05aec50SEd Tanous                             if (vlanEnable)
220808244d02SSunitha Harish                             {
220908244d02SSunitha Harish                                 crow::connections::systemBus->async_method_call(
2210bf648f77SEd Tanous                                     std::move(callback),
2211bf648f77SEd Tanous                                     "xyz.openbmc_project.Network",
221208244d02SSunitha Harish                                     "/xyz/openbmc_project/network/" + ifaceId,
221308244d02SSunitha Harish                                     "org.freedesktop.DBus.Properties", "Set",
221408244d02SSunitha Harish                                     "xyz.openbmc_project.Network.VLAN", "Id",
2215168e20c1SEd Tanous                                     dbus::utility::DbusVariantType(vlanId));
221608244d02SSunitha Harish                             }
221708244d02SSunitha Harish                             else
221808244d02SSunitha Harish                             {
2219bf648f77SEd Tanous                                 BMCWEB_LOG_DEBUG
2220bf648f77SEd Tanous                                     << "vlanEnable is false. Deleting the "
2221e48c0fc5SRavi Teja                                        "vlan interface";
222208244d02SSunitha Harish                                 crow::connections::systemBus->async_method_call(
2223bf648f77SEd Tanous                                     std::move(callback),
2224bf648f77SEd Tanous                                     "xyz.openbmc_project.Network",
2225bf648f77SEd Tanous                                     std::string(
2226bf648f77SEd Tanous                                         "/xyz/openbmc_project/network/") +
2227e48c0fc5SRavi Teja                                         ifaceId,
2228bf648f77SEd Tanous                                     "xyz.openbmc_project.Object.Delete",
2229bf648f77SEd Tanous                                     "Delete");
223008244d02SSunitha Harish                             }
223108244d02SSunitha Harish                         }
223208244d02SSunitha Harish                         else
22331abe55efSEd Tanous                         {
2234bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2235bf648f77SEd Tanous                             // existing object, and other errors
2236bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2237bf648f77SEd Tanous                                                        "VLAN Network Interface",
2238bf648f77SEd Tanous                                                        ifaceId);
2239bf648f77SEd Tanous                             return;
2240bf648f77SEd Tanous                         }
2241bf648f77SEd Tanous                     });
2242bf648f77SEd Tanous             });
2243bf648f77SEd Tanous 
2244bf648f77SEd Tanous     BMCWEB_ROUTE(
2245bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2246ed398213SEd Tanous         // This privilege is incorrect, it should be ConfigureManager
2247ed398213SEd Tanous         //.privileges(redfish::privileges::deleteVLanNetworkInterface)
2248432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2249bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
2250*45ca1b86SEd Tanous             [&app](const crow::Request& req,
2251bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2252*45ca1b86SEd Tanous                    const std::string& parentIfaceId,
2253*45ca1b86SEd Tanous                    const std::string& ifaceId) {
2254*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2255*45ca1b86SEd Tanous                 {
2256*45ca1b86SEd Tanous                     return;
2257*45ca1b86SEd Tanous                 }
2258bf648f77SEd Tanous                 if (!verifyNames(parentIfaceId, ifaceId))
2259bf648f77SEd Tanous                 {
2260e48c0fc5SRavi Teja                     messages::resourceNotFound(
2261e48c0fc5SRavi Teja                         asyncResp->res, "VLAN Network Interface", ifaceId);
2262927a505aSKowalski, Kamil                     return;
2263927a505aSKowalski, Kamil                 }
2264e439f0f8SKowalski, Kamil 
2265bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2266bf648f77SEd Tanous                 // for JSON preparation
2267f12894f8SJason M. Bills                 getEthernetIfaceData(
2268bf648f77SEd Tanous                     ifaceId,
2269bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId](
2270bf648f77SEd Tanous                         const bool& success,
2271bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2272cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2273cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
2274fda13ad2SSunitha Harish                         if (success && !ethData.vlan_id.empty())
22751abe55efSEd Tanous                         {
2276f12894f8SJason M. Bills                             auto callback =
2277bf648f77SEd Tanous                                 [asyncResp](
2278bf648f77SEd Tanous                                     const boost::system::error_code ec) {
22791abe55efSEd Tanous                                     if (ec)
22801abe55efSEd Tanous                                     {
2281f12894f8SJason M. Bills                                         messages::internalError(asyncResp->res);
2282927a505aSKowalski, Kamil                                     }
22834a0cb85cSEd Tanous                                 };
22844a0cb85cSEd Tanous                             crow::connections::systemBus->async_method_call(
2285bf648f77SEd Tanous                                 std::move(callback),
2286bf648f77SEd Tanous                                 "xyz.openbmc_project.Network",
2287bf648f77SEd Tanous                                 std::string("/xyz/openbmc_project/network/") +
2288bf648f77SEd Tanous                                     ifaceId,
22894a0cb85cSEd Tanous                                 "xyz.openbmc_project.Object.Delete", "Delete");
22901abe55efSEd Tanous                         }
22911abe55efSEd Tanous                         else
22921abe55efSEd Tanous                         {
2293927a505aSKowalski, Kamil                             // ... otherwise return error
2294bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2295bf648f77SEd Tanous                             // existing object, and other errors
2296bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2297bf648f77SEd Tanous                                                        "VLAN Network Interface",
2298bf648f77SEd Tanous                                                        ifaceId);
2299927a505aSKowalski, Kamil                         }
2300927a505aSKowalski, Kamil                     });
2301bf648f77SEd Tanous             });
2302e439f0f8SKowalski, Kamil 
2303bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2304bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2305ed398213SEd Tanous 
2306ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
2307bf648f77SEd Tanous         .methods(
2308bf648f77SEd Tanous             boost::beast::http::verb::
2309*45ca1b86SEd Tanous                 get)([&app](const crow::Request& req,
2310bf648f77SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2311bf648f77SEd Tanous                             const std::string& rootInterfaceName) {
2312*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2313*45ca1b86SEd Tanous             {
2314*45ca1b86SEd Tanous                 return;
2315*45ca1b86SEd Tanous             }
23164a0cb85cSEd Tanous             // Get eth interface list, and call the below callback for JSON
23171abe55efSEd Tanous             // preparation
2318bf648f77SEd Tanous             getEthernetIfaceList([asyncResp, rootInterfaceName](
23191abe55efSEd Tanous                                      const bool& success,
2320bf648f77SEd Tanous                                      const boost::container::flat_set<
2321bf648f77SEd Tanous                                          std::string>& ifaceList) {
23224a0cb85cSEd Tanous                 if (!success)
23231abe55efSEd Tanous                 {
2324f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
23254a0cb85cSEd Tanous                     return;
23261abe55efSEd Tanous                 }
23274c9afe43SEd Tanous 
232881ce609eSEd Tanous                 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
23294c9afe43SEd Tanous                 {
23304c9afe43SEd Tanous                     messages::resourceNotFound(asyncResp->res,
23314c9afe43SEd Tanous                                                "VLanNetworkInterfaceCollection",
23324c9afe43SEd Tanous                                                rootInterfaceName);
23334c9afe43SEd Tanous                     return;
23344c9afe43SEd Tanous                 }
23354c9afe43SEd Tanous 
23360f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
23370f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
23380f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
23390f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] =
23400f74e643SEd Tanous                     "VLAN Network Interface Collection";
23414a0cb85cSEd Tanous 
23422c70f800SEd Tanous                 nlohmann::json ifaceArray = nlohmann::json::array();
23434a0cb85cSEd Tanous 
234481ce609eSEd Tanous                 for (const std::string& ifaceItem : ifaceList)
23451abe55efSEd Tanous                 {
23462c70f800SEd Tanous                     if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
23474a0cb85cSEd Tanous                     {
2348f23b7296SEd Tanous                         std::string path =
2349f23b7296SEd Tanous                             "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2350f23b7296SEd Tanous                         path += rootInterfaceName;
2351f23b7296SEd Tanous                         path += "/VLANs/";
2352f23b7296SEd Tanous                         path += ifaceItem;
2353f23b7296SEd Tanous                         ifaceArray.push_back({{"@odata.id", std::move(path)}});
2354e439f0f8SKowalski, Kamil                     }
2355e439f0f8SKowalski, Kamil                 }
2356e439f0f8SKowalski, Kamil 
23574a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
23582c70f800SEd Tanous                     ifaceArray.size();
23592c70f800SEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
23604a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
23614a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
23624a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
2363e439f0f8SKowalski, Kamil             });
2364bf648f77SEd Tanous         });
2365e439f0f8SKowalski, Kamil 
2366bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2367bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2368ed398213SEd Tanous         // This privilege is wrong, it should be ConfigureManager
2369ed398213SEd Tanous         //.privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2370432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2371bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
2372*45ca1b86SEd Tanous             [&app](const crow::Request& req,
2373bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2374bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
2375*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2376*45ca1b86SEd Tanous                 {
2377*45ca1b86SEd Tanous                     return;
2378*45ca1b86SEd Tanous                 }
2379fda13ad2SSunitha Harish                 bool vlanEnable = false;
23800627a2c7SEd Tanous                 uint32_t vlanId = 0;
238115ed6780SWilly Tu                 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId",
238215ed6780SWilly Tu                                               vlanId, "VLANEnable", vlanEnable))
23831abe55efSEd Tanous                 {
23844a0cb85cSEd Tanous                     return;
2385e439f0f8SKowalski, Kamil                 }
2386fda13ad2SSunitha Harish                 // Need both vlanId and vlanEnable to service this request
2387dbb59d4dSEd Tanous                 if (vlanId == 0U)
2388fda13ad2SSunitha Harish                 {
2389fda13ad2SSunitha Harish                     messages::propertyMissing(asyncResp->res, "VLANId");
2390fda13ad2SSunitha Harish                 }
2391fda13ad2SSunitha Harish                 if (!vlanEnable)
2392fda13ad2SSunitha Harish                 {
2393fda13ad2SSunitha Harish                     messages::propertyMissing(asyncResp->res, "VLANEnable");
2394fda13ad2SSunitha Harish                 }
2395271584abSEd Tanous                 if (static_cast<bool>(vlanId) ^ vlanEnable)
2396fda13ad2SSunitha Harish                 {
2397fda13ad2SSunitha Harish                     return;
2398fda13ad2SSunitha Harish                 }
2399fda13ad2SSunitha Harish 
2400bf648f77SEd Tanous                 auto callback =
2401bf648f77SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
24021abe55efSEd Tanous                         if (ec)
24031abe55efSEd Tanous                         {
2404bf648f77SEd Tanous                             // TODO(ed) make more consistent error messages
2405bf648f77SEd Tanous                             // based on phosphor-network responses
2406f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
24074a0cb85cSEd Tanous                             return;
24081abe55efSEd Tanous                         }
2409f12894f8SJason M. Bills                         messages::created(asyncResp->res);
2410e439f0f8SKowalski, Kamil                     };
24114a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
24124a0cb85cSEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
24134a0cb85cSEd Tanous                     "/xyz/openbmc_project/network",
24144a0cb85cSEd Tanous                     "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
24150627a2c7SEd Tanous                     rootInterfaceName, vlanId);
2416bf648f77SEd Tanous             });
24174a0cb85cSEd Tanous }
2418bf648f77SEd Tanous 
24199391bb9cSRapkiewicz, Pawel } // namespace redfish
2420