xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision e05aec50f10116e6dda7e377bc61799aa5b7c166)
19391bb9cSRapkiewicz, Pawel /*
29391bb9cSRapkiewicz, Pawel // Copyright (c) 2018 Intel Corporation
39391bb9cSRapkiewicz, Pawel //
49391bb9cSRapkiewicz, Pawel // Licensed under the Apache License, Version 2.0 (the "License");
59391bb9cSRapkiewicz, Pawel // you may not use this file except in compliance with the License.
69391bb9cSRapkiewicz, Pawel // You may obtain a copy of the License at
79391bb9cSRapkiewicz, Pawel //
89391bb9cSRapkiewicz, Pawel //      http://www.apache.org/licenses/LICENSE-2.0
99391bb9cSRapkiewicz, Pawel //
109391bb9cSRapkiewicz, Pawel // Unless required by applicable law or agreed to in writing, software
119391bb9cSRapkiewicz, Pawel // distributed under the License is distributed on an "AS IS" BASIS,
129391bb9cSRapkiewicz, Pawel // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139391bb9cSRapkiewicz, Pawel // See the License for the specific language governing permissions and
149391bb9cSRapkiewicz, Pawel // limitations under the License.
159391bb9cSRapkiewicz, Pawel */
169391bb9cSRapkiewicz, Pawel #pragma once
179391bb9cSRapkiewicz, Pawel 
187e860f15SJohn Edward Broadbent #include <app.hpp>
191abe55efSEd Tanous #include <boost/container/flat_map.hpp>
204a0cb85cSEd Tanous #include <boost/container/flat_set.hpp>
21179db1d7SKowalski, Kamil #include <dbus_singleton.hpp>
22168e20c1SEd Tanous #include <dbus_utility.hpp>
23588c3f0dSKowalski, Kamil #include <error_messages.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 
339391bb9cSRapkiewicz, Pawel /**
349391bb9cSRapkiewicz, Pawel  * DBus types primitives for several generic DBus interfaces
359391bb9cSRapkiewicz, Pawel  * TODO(Pawel) consider move this to separate file into boost::dbus
369391bb9cSRapkiewicz, Pawel  */
37168e20c1SEd Tanous using PropertiesMapType =
38168e20c1SEd Tanous     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>;
399391bb9cSRapkiewicz, Pawel 
404a0cb85cSEd Tanous enum class LinkType
414a0cb85cSEd Tanous {
424a0cb85cSEd Tanous     Local,
434a0cb85cSEd Tanous     Global
444a0cb85cSEd Tanous };
459391bb9cSRapkiewicz, Pawel 
469391bb9cSRapkiewicz, Pawel /**
479391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
489391bb9cSRapkiewicz, Pawel  */
491abe55efSEd Tanous struct IPv4AddressData
501abe55efSEd Tanous {
51179db1d7SKowalski, Kamil     std::string id;
524a0cb85cSEd Tanous     std::string address;
534a0cb85cSEd Tanous     std::string domain;
544a0cb85cSEd Tanous     std::string gateway;
559391bb9cSRapkiewicz, Pawel     std::string netmask;
569391bb9cSRapkiewicz, Pawel     std::string origin;
574a0cb85cSEd Tanous     LinkType linktype;
5801c6e858SSunitha Harish     bool isActive;
594a0cb85cSEd Tanous 
601abe55efSEd Tanous     bool operator<(const IPv4AddressData& obj) const
611abe55efSEd Tanous     {
624a0cb85cSEd Tanous         return id < obj.id;
631abe55efSEd Tanous     }
649391bb9cSRapkiewicz, Pawel };
659391bb9cSRapkiewicz, Pawel 
669391bb9cSRapkiewicz, Pawel /**
67e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
68e48c0fc5SRavi Teja  */
69e48c0fc5SRavi Teja struct IPv6AddressData
70e48c0fc5SRavi Teja {
71e48c0fc5SRavi Teja     std::string id;
72e48c0fc5SRavi Teja     std::string address;
73e48c0fc5SRavi Teja     std::string origin;
74e48c0fc5SRavi Teja     uint8_t prefixLength;
75e48c0fc5SRavi Teja 
76e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData& obj) const
77e48c0fc5SRavi Teja     {
78e48c0fc5SRavi Teja         return id < obj.id;
79e48c0fc5SRavi Teja     }
80e48c0fc5SRavi Teja };
81e48c0fc5SRavi Teja /**
829391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
839391bb9cSRapkiewicz, Pawel  * available from DBus
849391bb9cSRapkiewicz, Pawel  */
851abe55efSEd Tanous struct EthernetInterfaceData
861abe55efSEd Tanous {
874a0cb85cSEd Tanous     uint32_t speed;
8835fb5311STejas Patil     size_t mtuSize;
894a0cb85cSEd Tanous     bool auto_neg;
901f8c7b5dSJohnathan Mantey     bool DNSEnabled;
911f8c7b5dSJohnathan Mantey     bool NTPEnabled;
921f8c7b5dSJohnathan Mantey     bool HostNameEnabled;
931f8c7b5dSJohnathan Mantey     bool SendHostNameEnabled;
94aa05fb27SJohnathan Mantey     bool linkUp;
95eeedda23SJohnathan Mantey     bool nicEnabled;
961f8c7b5dSJohnathan Mantey     std::string DHCPEnabled;
971f8c7b5dSJohnathan Mantey     std::string operatingMode;
984a0cb85cSEd Tanous     std::string hostname;
994a0cb85cSEd Tanous     std::string default_gateway;
1009a6fc6feSRavi Teja     std::string ipv6_default_gateway;
1014a0cb85cSEd Tanous     std::string mac_address;
102fda13ad2SSunitha Harish     std::vector<std::uint32_t> vlan_id;
1030f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
1040f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
105d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
1069391bb9cSRapkiewicz, Pawel };
1079391bb9cSRapkiewicz, Pawel 
1081f8c7b5dSJohnathan Mantey struct DHCPParameters
1091f8c7b5dSJohnathan Mantey {
1101f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
1111f8c7b5dSJohnathan Mantey     std::optional<bool> useDNSServers;
1121f8c7b5dSJohnathan Mantey     std::optional<bool> useNTPServers;
1131f8c7b5dSJohnathan Mantey     std::optional<bool> useUseDomainName;
1141f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1151f8c7b5dSJohnathan Mantey };
1161f8c7b5dSJohnathan Mantey 
1179391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1189391bb9cSRapkiewicz, Pawel // into full dot notation
1191abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1201abe55efSEd Tanous {
1219391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1229391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1239391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1249391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1259391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1269391bb9cSRapkiewicz, Pawel     return netmask;
1279391bb9cSRapkiewicz, Pawel }
1289391bb9cSRapkiewicz, Pawel 
1291f8c7b5dSJohnathan Mantey inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
1301f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1311f8c7b5dSJohnathan Mantey {
1321f8c7b5dSJohnathan Mantey     if (isIPv4)
1331f8c7b5dSJohnathan Mantey     {
1341f8c7b5dSJohnathan Mantey         return (
1351f8c7b5dSJohnathan Mantey             (inputDHCP ==
1361f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1371f8c7b5dSJohnathan Mantey             (inputDHCP ==
1381f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1391f8c7b5dSJohnathan Mantey     }
1401f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1411f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1421f8c7b5dSJohnathan Mantey             (inputDHCP ==
1431f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1441f8c7b5dSJohnathan Mantey }
1451f8c7b5dSJohnathan Mantey 
1462c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1471f8c7b5dSJohnathan Mantey {
1481f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1491f8c7b5dSJohnathan Mantey     {
1501f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1511f8c7b5dSJohnathan Mantey     }
1523174e4dfSEd Tanous     if (isIPv4)
1531f8c7b5dSJohnathan Mantey     {
1541f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1551f8c7b5dSJohnathan Mantey     }
1563174e4dfSEd Tanous     if (isIPv6)
1571f8c7b5dSJohnathan Mantey     {
1581f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1591f8c7b5dSJohnathan Mantey     }
1601f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1611f8c7b5dSJohnathan Mantey }
1621f8c7b5dSJohnathan Mantey 
1634a0cb85cSEd Tanous inline std::string
1644a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1654a0cb85cSEd Tanous                                         bool isIPv4)
1661abe55efSEd Tanous {
1674a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1681abe55efSEd Tanous     {
1694a0cb85cSEd Tanous         return "Static";
1709391bb9cSRapkiewicz, Pawel     }
1714a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1721abe55efSEd Tanous     {
1734a0cb85cSEd Tanous         if (isIPv4)
1741abe55efSEd Tanous         {
1754a0cb85cSEd Tanous             return "IPv4LinkLocal";
1761abe55efSEd Tanous         }
1774a0cb85cSEd Tanous         return "LinkLocal";
1789391bb9cSRapkiewicz, Pawel     }
1794a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1801abe55efSEd Tanous     {
1814a0cb85cSEd Tanous         if (isIPv4)
1824a0cb85cSEd Tanous         {
1834a0cb85cSEd Tanous             return "DHCP";
1844a0cb85cSEd Tanous         }
1854a0cb85cSEd Tanous         return "DHCPv6";
1864a0cb85cSEd Tanous     }
1874a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1884a0cb85cSEd Tanous     {
1894a0cb85cSEd Tanous         return "SLAAC";
1904a0cb85cSEd Tanous     }
1914a0cb85cSEd Tanous     return "";
1924a0cb85cSEd Tanous }
1934a0cb85cSEd Tanous 
194711ac7a9SEd Tanous inline bool
195711ac7a9SEd Tanous     extractEthernetInterfaceData(const std::string& ethifaceId,
196711ac7a9SEd Tanous                                  dbus::utility::ManagedObjectType& dbusData,
1974a0cb85cSEd Tanous                                  EthernetInterfaceData& ethData)
1984a0cb85cSEd Tanous {
1994c9afe43SEd Tanous     bool idFound = false;
20081ce609eSEd Tanous     for (auto& objpath : dbusData)
2014a0cb85cSEd Tanous     {
202f23b7296SEd Tanous         for (auto& ifacePair : objpath.second)
2034a0cb85cSEd Tanous         {
20481ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
205029573d4SEd Tanous             {
2064c9afe43SEd Tanous                 idFound = true;
2074a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2084a0cb85cSEd Tanous                 {
2094a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2104a0cb85cSEd Tanous                     {
2114a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2124a0cb85cSEd Tanous                         {
2134a0cb85cSEd Tanous                             const std::string* mac =
214abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2154a0cb85cSEd Tanous                             if (mac != nullptr)
2164a0cb85cSEd Tanous                             {
2174a0cb85cSEd Tanous                                 ethData.mac_address = *mac;
2184a0cb85cSEd Tanous                             }
2194a0cb85cSEd Tanous                         }
2204a0cb85cSEd Tanous                     }
2214a0cb85cSEd Tanous                 }
2224a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2234a0cb85cSEd Tanous                 {
2244a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2254a0cb85cSEd Tanous                     {
2264a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2274a0cb85cSEd Tanous                         {
2281b6b96c5SEd Tanous                             const uint32_t* id =
229abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2304a0cb85cSEd Tanous                             if (id != nullptr)
2314a0cb85cSEd Tanous                             {
232fda13ad2SSunitha Harish                                 ethData.vlan_id.push_back(*id);
2334a0cb85cSEd Tanous                             }
2344a0cb85cSEd Tanous                         }
2354a0cb85cSEd Tanous                     }
2364a0cb85cSEd Tanous                 }
2374a0cb85cSEd Tanous                 else if (ifacePair.first ==
2384a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2394a0cb85cSEd Tanous                 {
2404a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2414a0cb85cSEd Tanous                     {
2424a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2434a0cb85cSEd Tanous                         {
2442c70f800SEd Tanous                             const bool* autoNeg =
245abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2462c70f800SEd Tanous                             if (autoNeg != nullptr)
2474a0cb85cSEd Tanous                             {
2482c70f800SEd Tanous                                 ethData.auto_neg = *autoNeg;
2494a0cb85cSEd Tanous                             }
2504a0cb85cSEd Tanous                         }
2514a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2524a0cb85cSEd Tanous                         {
2534a0cb85cSEd Tanous                             const uint32_t* speed =
254abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2554a0cb85cSEd Tanous                             if (speed != nullptr)
2564a0cb85cSEd Tanous                             {
2574a0cb85cSEd Tanous                                 ethData.speed = *speed;
2584a0cb85cSEd Tanous                             }
2594a0cb85cSEd Tanous                         }
26035fb5311STejas Patil                         else if (propertyPair.first == "MTU")
26135fb5311STejas Patil                         {
26235fb5311STejas Patil                             const uint32_t* mtuSize =
26335fb5311STejas Patil                                 std::get_if<uint32_t>(&propertyPair.second);
26435fb5311STejas Patil                             if (mtuSize != nullptr)
26535fb5311STejas Patil                             {
26635fb5311STejas Patil                                 ethData.mtuSize = *mtuSize;
26735fb5311STejas Patil                             }
26835fb5311STejas Patil                         }
269aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
270aa05fb27SJohnathan Mantey                         {
271aa05fb27SJohnathan Mantey                             const bool* linkUp =
272aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
273aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
274aa05fb27SJohnathan Mantey                             {
275aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
276aa05fb27SJohnathan Mantey                             }
277aa05fb27SJohnathan Mantey                         }
278eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
279eeedda23SJohnathan Mantey                         {
280eeedda23SJohnathan Mantey                             const bool* nicEnabled =
281eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
282eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
283eeedda23SJohnathan Mantey                             {
284eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
285eeedda23SJohnathan Mantey                             }
286eeedda23SJohnathan Mantey                         }
287f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
288029573d4SEd Tanous                         {
289029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2908d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
291029573d4SEd Tanous                                     &propertyPair.second);
292029573d4SEd Tanous                             if (nameservers != nullptr)
293029573d4SEd Tanous                             {
294f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2950f6efdc1Smanojkiran.eda@gmail.com                             }
2960f6efdc1Smanojkiran.eda@gmail.com                         }
2970f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2980f6efdc1Smanojkiran.eda@gmail.com                         {
2990f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
3008d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
3010f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
3020f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
3030f6efdc1Smanojkiran.eda@gmail.com                             {
304f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
3054a0cb85cSEd Tanous                             }
3064a0cb85cSEd Tanous                         }
3072a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3082a133282Smanojkiraneda                         {
3092c70f800SEd Tanous                             const std::string* dhcpEnabled =
3101f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3112c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3122a133282Smanojkiraneda                             {
3132c70f800SEd Tanous                                 ethData.DHCPEnabled = *dhcpEnabled;
3142a133282Smanojkiraneda                             }
3152a133282Smanojkiraneda                         }
316d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
317d24bfc7aSJennifer Lee                         {
318d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3198d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
320d24bfc7aSJennifer Lee                                     &propertyPair.second);
321d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
322d24bfc7aSJennifer Lee                             {
323f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
324d24bfc7aSJennifer Lee                             }
325d24bfc7aSJennifer Lee                         }
3269010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway")
3279010ec2eSRavi Teja                         {
3289010ec2eSRavi Teja                             const std::string* defaultGateway =
3299010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3309010ec2eSRavi Teja                             if (defaultGateway != nullptr)
3319010ec2eSRavi Teja                             {
3329010ec2eSRavi Teja                                 std::string defaultGatewayStr = *defaultGateway;
3339010ec2eSRavi Teja                                 if (defaultGatewayStr.empty())
3349010ec2eSRavi Teja                                 {
3359010ec2eSRavi Teja                                     ethData.default_gateway = "0.0.0.0";
3369010ec2eSRavi Teja                                 }
3379010ec2eSRavi Teja                                 else
3389010ec2eSRavi Teja                                 {
3399010ec2eSRavi Teja                                     ethData.default_gateway = defaultGatewayStr;
3409010ec2eSRavi Teja                                 }
3419010ec2eSRavi Teja                             }
3429010ec2eSRavi Teja                         }
3439010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway6")
3449010ec2eSRavi Teja                         {
3459010ec2eSRavi Teja                             const std::string* defaultGateway6 =
3469010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3479010ec2eSRavi Teja                             if (defaultGateway6 != nullptr)
3489010ec2eSRavi Teja                             {
3499010ec2eSRavi Teja                                 std::string defaultGateway6Str =
3509010ec2eSRavi Teja                                     *defaultGateway6;
3519010ec2eSRavi Teja                                 if (defaultGateway6Str.empty())
3529010ec2eSRavi Teja                                 {
3539010ec2eSRavi Teja                                     ethData.ipv6_default_gateway =
3549010ec2eSRavi Teja                                         "0:0:0:0:0:0:0:0";
3559010ec2eSRavi Teja                                 }
3569010ec2eSRavi Teja                                 else
3579010ec2eSRavi Teja                                 {
3589010ec2eSRavi Teja                                     ethData.ipv6_default_gateway =
3599010ec2eSRavi Teja                                         defaultGateway6Str;
3609010ec2eSRavi Teja                                 }
3619010ec2eSRavi Teja                             }
3629010ec2eSRavi Teja                         }
363029573d4SEd Tanous                     }
364029573d4SEd Tanous                 }
365029573d4SEd Tanous             }
3661f8c7b5dSJohnathan Mantey 
3671f8c7b5dSJohnathan Mantey             if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
3681f8c7b5dSJohnathan Mantey             {
3691f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3701f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3711f8c7b5dSJohnathan Mantey                 {
3721f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3731f8c7b5dSJohnathan Mantey                     {
3741f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3751f8c7b5dSJohnathan Mantey                         {
3762c70f800SEd Tanous                             const bool* dnsEnabled =
3771f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3782c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3791f8c7b5dSJohnathan Mantey                             {
3802c70f800SEd Tanous                                 ethData.DNSEnabled = *dnsEnabled;
3811f8c7b5dSJohnathan Mantey                             }
3821f8c7b5dSJohnathan Mantey                         }
3831f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3841f8c7b5dSJohnathan Mantey                         {
3852c70f800SEd Tanous                             const bool* ntpEnabled =
3861f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3872c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3881f8c7b5dSJohnathan Mantey                             {
3892c70f800SEd Tanous                                 ethData.NTPEnabled = *ntpEnabled;
3901f8c7b5dSJohnathan Mantey                             }
3911f8c7b5dSJohnathan Mantey                         }
3921f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3931f8c7b5dSJohnathan Mantey                         {
3942c70f800SEd Tanous                             const bool* hostNameEnabled =
3951f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3962c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3971f8c7b5dSJohnathan Mantey                             {
3982c70f800SEd Tanous                                 ethData.HostNameEnabled = *hostNameEnabled;
3991f8c7b5dSJohnathan Mantey                             }
4001f8c7b5dSJohnathan Mantey                         }
4011f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "SendHostNameEnabled")
4021f8c7b5dSJohnathan Mantey                         {
4032c70f800SEd Tanous                             const bool* sendHostNameEnabled =
4041f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
4052c70f800SEd Tanous                             if (sendHostNameEnabled != nullptr)
4061f8c7b5dSJohnathan Mantey                             {
4071f8c7b5dSJohnathan Mantey                                 ethData.SendHostNameEnabled =
4082c70f800SEd Tanous                                     *sendHostNameEnabled;
4091f8c7b5dSJohnathan Mantey                             }
4101f8c7b5dSJohnathan Mantey                         }
4111f8c7b5dSJohnathan Mantey                     }
4121f8c7b5dSJohnathan Mantey                 }
4131f8c7b5dSJohnathan Mantey             }
414029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
415029573d4SEd Tanous             // to check eth number
416029573d4SEd Tanous             if (ifacePair.first ==
4174a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
4184a0cb85cSEd Tanous             {
4194a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
4204a0cb85cSEd Tanous                 {
4214a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
4224a0cb85cSEd Tanous                     {
4234a0cb85cSEd Tanous                         const std::string* hostname =
4248d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4254a0cb85cSEd Tanous                         if (hostname != nullptr)
4264a0cb85cSEd Tanous                         {
4274a0cb85cSEd Tanous                             ethData.hostname = *hostname;
4284a0cb85cSEd Tanous                         }
4294a0cb85cSEd Tanous                     }
4304a0cb85cSEd Tanous                 }
4314a0cb85cSEd Tanous             }
4324a0cb85cSEd Tanous         }
4334a0cb85cSEd Tanous     }
4344c9afe43SEd Tanous     return idFound;
4354a0cb85cSEd Tanous }
4364a0cb85cSEd Tanous 
437e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
43801784826SJohnathan Mantey inline void
43981ce609eSEd Tanous     extractIPV6Data(const std::string& ethifaceId,
440711ac7a9SEd Tanous                     const dbus::utility::ManagedObjectType& dbusData,
44181ce609eSEd Tanous                     boost::container::flat_set<IPv6AddressData>& ipv6Config)
442e48c0fc5SRavi Teja {
443e48c0fc5SRavi Teja     const std::string ipv6PathStart =
44481ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
445e48c0fc5SRavi Teja 
446e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
447e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
44881ce609eSEd Tanous     for (const auto& objpath : dbusData)
449e48c0fc5SRavi Teja     {
450e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
451e48c0fc5SRavi Teja         if (boost::starts_with(objpath.first.str, ipv6PathStart))
452e48c0fc5SRavi Teja         {
4539eb808c1SEd Tanous             for (const auto& interface : objpath.second)
454e48c0fc5SRavi Teja             {
455e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
456e48c0fc5SRavi Teja                 {
457e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
458e48c0fc5SRavi Teja                     // appropriate
459e48c0fc5SRavi Teja                     std::pair<
460e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
461e48c0fc5SRavi Teja                         bool>
46281ce609eSEd Tanous                         it = ipv6Config.insert(IPv6AddressData{});
4632c70f800SEd Tanous                     IPv6AddressData& ipv6Address = *it.first;
4642c70f800SEd Tanous                     ipv6Address.id =
465271584abSEd Tanous                         objpath.first.str.substr(ipv6PathStart.size());
4669eb808c1SEd Tanous                     for (const auto& property : interface.second)
467e48c0fc5SRavi Teja                     {
468e48c0fc5SRavi Teja                         if (property.first == "Address")
469e48c0fc5SRavi Teja                         {
470e48c0fc5SRavi Teja                             const std::string* address =
471e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
472e48c0fc5SRavi Teja                             if (address != nullptr)
473e48c0fc5SRavi Teja                             {
4742c70f800SEd Tanous                                 ipv6Address.address = *address;
475e48c0fc5SRavi Teja                             }
476e48c0fc5SRavi Teja                         }
477e48c0fc5SRavi Teja                         else if (property.first == "Origin")
478e48c0fc5SRavi Teja                         {
479e48c0fc5SRavi Teja                             const std::string* origin =
480e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
481e48c0fc5SRavi Teja                             if (origin != nullptr)
482e48c0fc5SRavi Teja                             {
4832c70f800SEd Tanous                                 ipv6Address.origin =
484e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
485e48c0fc5SRavi Teja                                                                         false);
486e48c0fc5SRavi Teja                             }
487e48c0fc5SRavi Teja                         }
488e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
489e48c0fc5SRavi Teja                         {
490e48c0fc5SRavi Teja                             const uint8_t* prefix =
491e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
492e48c0fc5SRavi Teja                             if (prefix != nullptr)
493e48c0fc5SRavi Teja                             {
4942c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
495e48c0fc5SRavi Teja                             }
496e48c0fc5SRavi Teja                         }
497889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
498889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
499889ff694SAsmitha Karunanithi                         {
500889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
501889ff694SAsmitha Karunanithi                         }
502e48c0fc5SRavi Teja                         else
503e48c0fc5SRavi Teja                         {
504e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
505e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
506e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
507e48c0fc5SRavi Teja                         }
508e48c0fc5SRavi Teja                     }
509e48c0fc5SRavi Teja                 }
510e48c0fc5SRavi Teja             }
511e48c0fc5SRavi Teja         }
512e48c0fc5SRavi Teja     }
513e48c0fc5SRavi Teja }
514e48c0fc5SRavi Teja 
5154a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
51601784826SJohnathan Mantey inline void
51781ce609eSEd Tanous     extractIPData(const std::string& ethifaceId,
518711ac7a9SEd Tanous                   const dbus::utility::ManagedObjectType& dbusData,
51981ce609eSEd Tanous                   boost::container::flat_set<IPv4AddressData>& ipv4Config)
5204a0cb85cSEd Tanous {
5214a0cb85cSEd Tanous     const std::string ipv4PathStart =
52281ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
5234a0cb85cSEd Tanous 
5244a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5254a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
52681ce609eSEd Tanous     for (const auto& objpath : dbusData)
5274a0cb85cSEd Tanous     {
5284a0cb85cSEd Tanous         // Check if proper pattern for object path appears
5294a0cb85cSEd Tanous         if (boost::starts_with(objpath.first.str, ipv4PathStart))
5304a0cb85cSEd Tanous         {
5319eb808c1SEd Tanous             for (const auto& interface : objpath.second)
5324a0cb85cSEd Tanous             {
5334a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5344a0cb85cSEd Tanous                 {
5354a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5364a0cb85cSEd Tanous                     // appropriate
5374a0cb85cSEd Tanous                     std::pair<
5384a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5394a0cb85cSEd Tanous                         bool>
54081ce609eSEd Tanous                         it = ipv4Config.insert(IPv4AddressData{});
5412c70f800SEd Tanous                     IPv4AddressData& ipv4Address = *it.first;
5422c70f800SEd Tanous                     ipv4Address.id =
543271584abSEd Tanous                         objpath.first.str.substr(ipv4PathStart.size());
5449eb808c1SEd Tanous                     for (const auto& property : interface.second)
5454a0cb85cSEd Tanous                     {
5464a0cb85cSEd Tanous                         if (property.first == "Address")
5474a0cb85cSEd Tanous                         {
5484a0cb85cSEd Tanous                             const std::string* address =
549abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5504a0cb85cSEd Tanous                             if (address != nullptr)
5514a0cb85cSEd Tanous                             {
5522c70f800SEd Tanous                                 ipv4Address.address = *address;
5534a0cb85cSEd Tanous                             }
5544a0cb85cSEd Tanous                         }
5554a0cb85cSEd Tanous                         else if (property.first == "Origin")
5564a0cb85cSEd Tanous                         {
5574a0cb85cSEd Tanous                             const std::string* origin =
558abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5594a0cb85cSEd Tanous                             if (origin != nullptr)
5604a0cb85cSEd Tanous                             {
5612c70f800SEd Tanous                                 ipv4Address.origin =
5624a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5634a0cb85cSEd Tanous                                                                         true);
5644a0cb85cSEd Tanous                             }
5654a0cb85cSEd Tanous                         }
5664a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5674a0cb85cSEd Tanous                         {
5684a0cb85cSEd Tanous                             const uint8_t* mask =
569abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5704a0cb85cSEd Tanous                             if (mask != nullptr)
5714a0cb85cSEd Tanous                             {
5724a0cb85cSEd Tanous                                 // convert it to the string
5732c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
5744a0cb85cSEd Tanous                             }
5754a0cb85cSEd Tanous                         }
576889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
577889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
578889ff694SAsmitha Karunanithi                         {
579889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
580889ff694SAsmitha Karunanithi                         }
5814a0cb85cSEd Tanous                         else
5824a0cb85cSEd Tanous                         {
5834a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5844a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5854a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5864a0cb85cSEd Tanous                         }
5874a0cb85cSEd Tanous                     }
5884a0cb85cSEd Tanous                     // Check if given address is local, or global
5892c70f800SEd Tanous                     ipv4Address.linktype =
5902c70f800SEd Tanous                         boost::starts_with(ipv4Address.address, "169.254.")
59118659d10SJohnathan Mantey                             ? LinkType::Local
59218659d10SJohnathan Mantey                             : LinkType::Global;
5934a0cb85cSEd Tanous                 }
5944a0cb85cSEd Tanous             }
5954a0cb85cSEd Tanous         }
5964a0cb85cSEd Tanous     }
5974a0cb85cSEd Tanous }
598588c3f0dSKowalski, Kamil 
599588c3f0dSKowalski, Kamil /**
600588c3f0dSKowalski, Kamil  * @brief Sets given Id on the given VLAN interface through D-Bus
601588c3f0dSKowalski, Kamil  *
602588c3f0dSKowalski, Kamil  * @param[in] ifaceId       Id of VLAN interface that should be modified
603588c3f0dSKowalski, Kamil  * @param[in] inputVlanId   New ID of the VLAN
604588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
605588c3f0dSKowalski, Kamil  *
606588c3f0dSKowalski, Kamil  * @return None.
607588c3f0dSKowalski, Kamil  */
608588c3f0dSKowalski, Kamil template <typename CallbackFunc>
6094a0cb85cSEd Tanous void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
6101abe55efSEd Tanous                   CallbackFunc&& callback)
6111abe55efSEd Tanous {
61255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
613588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
614588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
615588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
616588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", "Id",
617168e20c1SEd Tanous         dbus::utility::DbusVariantType(inputVlanId));
6184a0cb85cSEd Tanous }
619588c3f0dSKowalski, Kamil 
620588c3f0dSKowalski, Kamil /**
621179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
622179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
623179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
624179db1d7SKowalski, Kamil  *
625179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
626179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
627179db1d7SKowalski, Kamil  *
628179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
629179db1d7SKowalski, Kamil  */
6304a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
6311abe55efSEd Tanous                                        uint8_t* bits = nullptr)
6321abe55efSEd Tanous {
633179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
634179db1d7SKowalski, Kamil 
635179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
636179db1d7SKowalski, Kamil 
6374a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
6381abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
6391abe55efSEd Tanous     {
640179db1d7SKowalski, Kamil         return false;
641179db1d7SKowalski, Kamil     }
642179db1d7SKowalski, Kamil 
6431abe55efSEd Tanous     if (bits != nullptr)
6441abe55efSEd Tanous     {
645179db1d7SKowalski, Kamil         *bits = 0;
646179db1d7SKowalski, Kamil     }
647179db1d7SKowalski, Kamil 
648543f4400SEd Tanous     char* endPtr = nullptr;
649179db1d7SKowalski, Kamil     long previousValue = 255;
650543f4400SEd Tanous     bool firstZeroInByteHit = false;
6511abe55efSEd Tanous     for (const std::string& byte : bytesInMask)
6521abe55efSEd Tanous     {
6531abe55efSEd Tanous         if (byte.empty())
6541abe55efSEd Tanous         {
6551db9ca37SKowalski, Kamil             return false;
6561db9ca37SKowalski, Kamil         }
6571db9ca37SKowalski, Kamil 
658179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
6591db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
660179db1d7SKowalski, Kamil 
6614a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
6624a0cb85cSEd Tanous         // is not 100% number
6631abe55efSEd Tanous         if (*endPtr != '\0')
6641abe55efSEd Tanous         {
665179db1d7SKowalski, Kamil             return false;
666179db1d7SKowalski, Kamil         }
667179db1d7SKowalski, Kamil 
668179db1d7SKowalski, Kamil         // Value should be contained in byte
6691abe55efSEd Tanous         if (value < 0 || value > 255)
6701abe55efSEd Tanous         {
671179db1d7SKowalski, Kamil             return false;
672179db1d7SKowalski, Kamil         }
673179db1d7SKowalski, Kamil 
6741abe55efSEd Tanous         if (bits != nullptr)
6751abe55efSEd Tanous         {
676179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
6771abe55efSEd Tanous             if (previousValue != 255 && value != 0)
6781abe55efSEd Tanous             {
679179db1d7SKowalski, Kamil                 return false;
680179db1d7SKowalski, Kamil             }
681179db1d7SKowalski, Kamil 
682179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
683179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
684179db1d7SKowalski, Kamil 
685179db1d7SKowalski, Kamil             // Count bits
68623a21a1cSEd Tanous             for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
6871abe55efSEd Tanous             {
68823a21a1cSEd Tanous                 if (value & (1L << bitIdx))
6891abe55efSEd Tanous                 {
6901abe55efSEd Tanous                     if (firstZeroInByteHit)
6911abe55efSEd Tanous                     {
692179db1d7SKowalski, Kamil                         // Continuity not preserved
693179db1d7SKowalski, Kamil                         return false;
6941abe55efSEd Tanous                     }
695179db1d7SKowalski, Kamil                     (*bits)++;
696179db1d7SKowalski, Kamil                 }
6971abe55efSEd Tanous                 else
6981abe55efSEd Tanous                 {
699179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
700179db1d7SKowalski, Kamil                 }
701179db1d7SKowalski, Kamil             }
702179db1d7SKowalski, Kamil         }
703179db1d7SKowalski, Kamil 
704179db1d7SKowalski, Kamil         previousValue = value;
705179db1d7SKowalski, Kamil     }
706179db1d7SKowalski, Kamil 
707179db1d7SKowalski, Kamil     return true;
708179db1d7SKowalski, Kamil }
709179db1d7SKowalski, Kamil 
710179db1d7SKowalski, Kamil /**
71101784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
712179db1d7SKowalski, Kamil  *
713179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
714179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
715179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
716179db1d7SKowalski, Kamil  *
717179db1d7SKowalski, Kamil  * @return None
718179db1d7SKowalski, Kamil  */
7194a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
7208d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7211abe55efSEd Tanous {
72255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
723286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
7241abe55efSEd Tanous             if (ec)
7251abe55efSEd Tanous             {
726a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
7271abe55efSEd Tanous             }
728179db1d7SKowalski, Kamil         },
729179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
730179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
731179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
732179db1d7SKowalski, Kamil }
733179db1d7SKowalski, Kamil 
734244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway(
735244b6d5bSGunnar Mills     const std::string& ifaceId, const std::string& gateway,
736244b6d5bSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7379010ec2eSRavi Teja {
7389010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7399010ec2eSRavi Teja         [asyncResp](const boost::system::error_code ec) {
7409010ec2eSRavi Teja             if (ec)
7419010ec2eSRavi Teja             {
7429010ec2eSRavi Teja                 messages::internalError(asyncResp->res);
7439010ec2eSRavi Teja                 return;
7449010ec2eSRavi Teja             }
7459010ec2eSRavi Teja             asyncResp->res.result(boost::beast::http::status::no_content);
7469010ec2eSRavi Teja         },
7479010ec2eSRavi Teja         "xyz.openbmc_project.Network",
7489010ec2eSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
7499010ec2eSRavi Teja         "org.freedesktop.DBus.Properties", "Set",
7509010ec2eSRavi Teja         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
751168e20c1SEd Tanous         dbus::utility::DbusVariantType(gateway));
7529010ec2eSRavi Teja }
753179db1d7SKowalski, Kamil /**
75401784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
755179db1d7SKowalski, Kamil  *
75601784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
75701784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
75801784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
75901784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
760179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
761179db1d7SKowalski, Kamil  *
762179db1d7SKowalski, Kamil  * @return None
763179db1d7SKowalski, Kamil  */
764cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
765cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
7668d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7671abe55efSEd Tanous {
7689010ec2eSRavi Teja     auto createIpHandler = [asyncResp, ifaceId,
7699010ec2eSRavi Teja                             gateway](const boost::system::error_code ec) {
7701abe55efSEd Tanous         if (ec)
7711abe55efSEd Tanous         {
772a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
7739010ec2eSRavi Teja             return;
774179db1d7SKowalski, Kamil         }
7759010ec2eSRavi Teja         updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
7769010ec2eSRavi Teja     };
7779010ec2eSRavi Teja 
7789010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7799010ec2eSRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
780179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
781179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
78201784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
783179db1d7SKowalski, Kamil         gateway);
784179db1d7SKowalski, Kamil }
785e48c0fc5SRavi Teja 
786e48c0fc5SRavi Teja /**
78701784826SJohnathan Mantey  * @brief Deletes the IPv4 entry for this interface and creates a replacement
78801784826SJohnathan Mantey  * static IPv4 entry
78901784826SJohnathan Mantey  *
79001784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
79101784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
79201784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
79301784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
79401784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
79501784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
79601784826SJohnathan Mantey  *
79701784826SJohnathan Mantey  * @return None
79801784826SJohnathan Mantey  */
7998d1b46d7Szhanghch05 inline void
8008d1b46d7Szhanghch05     deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
8018d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& gateway,
80201784826SJohnathan Mantey                         const std::string& address,
8038d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
80401784826SJohnathan Mantey {
80501784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
80601784826SJohnathan Mantey         [asyncResp, ifaceId, address, prefixLength,
80701784826SJohnathan Mantey          gateway](const boost::system::error_code ec) {
80801784826SJohnathan Mantey             if (ec)
80901784826SJohnathan Mantey             {
81001784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
8119010ec2eSRavi Teja                 return;
81201784826SJohnathan Mantey             }
8139010ec2eSRavi Teja 
81401784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
8159010ec2eSRavi Teja                 [asyncResp, ifaceId,
8169010ec2eSRavi Teja                  gateway](const boost::system::error_code ec2) {
81723a21a1cSEd Tanous                     if (ec2)
81801784826SJohnathan Mantey                     {
81901784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
8209010ec2eSRavi Teja                         return;
82101784826SJohnathan Mantey                     }
8229010ec2eSRavi Teja                     updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
82301784826SJohnathan Mantey                 },
82401784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
82501784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
82601784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
82701784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
82801784826SJohnathan Mantey                 prefixLength, gateway);
82901784826SJohnathan Mantey         },
83001784826SJohnathan Mantey         "xyz.openbmc_project.Network",
83101784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
83201784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
83301784826SJohnathan Mantey }
83401784826SJohnathan Mantey 
83501784826SJohnathan Mantey /**
836e48c0fc5SRavi Teja  * @brief Deletes given IPv6
837e48c0fc5SRavi Teja  *
838e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
839e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
840e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
841e48c0fc5SRavi Teja  *
842e48c0fc5SRavi Teja  * @return None
843e48c0fc5SRavi Teja  */
844e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
8458d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
846e48c0fc5SRavi Teja {
847e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
848286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
849e48c0fc5SRavi Teja             if (ec)
850e48c0fc5SRavi Teja             {
851e48c0fc5SRavi Teja                 messages::internalError(asyncResp->res);
852e48c0fc5SRavi Teja             }
853e48c0fc5SRavi Teja         },
854e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
855e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
856e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
857e48c0fc5SRavi Teja }
858e48c0fc5SRavi Teja 
859e48c0fc5SRavi Teja /**
86001784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
86101784826SJohnathan Mantey  * static IPv6 entry
86201784826SJohnathan Mantey  *
86301784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
86401784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
86501784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
86601784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
86701784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
86801784826SJohnathan Mantey  *
86901784826SJohnathan Mantey  * @return None
87001784826SJohnathan Mantey  */
8718d1b46d7Szhanghch05 inline void
8728d1b46d7Szhanghch05     deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
8738d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& address,
8748d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
87501784826SJohnathan Mantey {
87601784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
87701784826SJohnathan Mantey         [asyncResp, ifaceId, address,
87801784826SJohnathan Mantey          prefixLength](const boost::system::error_code ec) {
87901784826SJohnathan Mantey             if (ec)
88001784826SJohnathan Mantey             {
88101784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
88201784826SJohnathan Mantey             }
88301784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
88423a21a1cSEd Tanous                 [asyncResp](const boost::system::error_code ec2) {
88523a21a1cSEd Tanous                     if (ec2)
88601784826SJohnathan Mantey                     {
88701784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
88801784826SJohnathan Mantey                     }
88901784826SJohnathan Mantey                 },
89001784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
89101784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
89201784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
89301784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
89401784826SJohnathan Mantey                 prefixLength, "");
89501784826SJohnathan Mantey         },
89601784826SJohnathan Mantey         "xyz.openbmc_project.Network",
89701784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
89801784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
89901784826SJohnathan Mantey }
90001784826SJohnathan Mantey 
90101784826SJohnathan Mantey /**
902e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
903e48c0fc5SRavi Teja  *
904e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
905e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
906e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
907e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
908e48c0fc5SRavi Teja  *
909e48c0fc5SRavi Teja  * @return None
910e48c0fc5SRavi Teja  */
91101784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
91201784826SJohnathan Mantey                        const std::string& address,
9138d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
914e48c0fc5SRavi Teja {
915e48c0fc5SRavi Teja     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
916e48c0fc5SRavi Teja         if (ec)
917e48c0fc5SRavi Teja         {
918e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
919e48c0fc5SRavi Teja         }
920e48c0fc5SRavi Teja     };
921e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
9224e0453b1SGunnar Mills     // does not have associated gateway property
923e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
924e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
925e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
926e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
927e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
928e48c0fc5SRavi Teja         "");
929e48c0fc5SRavi Teja }
930e48c0fc5SRavi Teja 
931179db1d7SKowalski, Kamil /**
932179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
933179db1d7SKowalski, Kamil  * Object
934179db1d7SKowalski, Kamil  * from EntityManager Network Manager
9354a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
936179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
937179db1d7SKowalski, Kamil  * into JSON
938179db1d7SKowalski, Kamil  */
939179db1d7SKowalski, Kamil template <typename CallbackFunc>
94081ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
9411abe55efSEd Tanous                           CallbackFunc&& callback)
9421abe55efSEd Tanous {
94355c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
944f94c4ecfSEd Tanous         [ethifaceId{std::string{ethifaceId}},
945f94c4ecfSEd Tanous          callback{std::forward<CallbackFunc>(callback)}](
94681ce609eSEd Tanous             const boost::system::error_code errorCode,
947711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
94855c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
9494a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
950e48c0fc5SRavi Teja             boost::container::flat_set<IPv6AddressData> ipv6Data;
951179db1d7SKowalski, Kamil 
95281ce609eSEd Tanous             if (errorCode)
9531abe55efSEd Tanous             {
95401784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
955179db1d7SKowalski, Kamil                 return;
956179db1d7SKowalski, Kamil             }
957179db1d7SKowalski, Kamil 
9584c9afe43SEd Tanous             bool found =
9592c70f800SEd Tanous                 extractEthernetInterfaceData(ethifaceId, resp, ethData);
9604c9afe43SEd Tanous             if (!found)
9614c9afe43SEd Tanous             {
96201784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
9634c9afe43SEd Tanous                 return;
9644c9afe43SEd Tanous             }
9654c9afe43SEd Tanous 
9662c70f800SEd Tanous             extractIPData(ethifaceId, resp, ipv4Data);
967179db1d7SKowalski, Kamil             // Fix global GW
9681abe55efSEd Tanous             for (IPv4AddressData& ipv4 : ipv4Data)
9691abe55efSEd Tanous             {
970c619141bSRavi Teja                 if (((ipv4.linktype == LinkType::Global) &&
971c619141bSRavi Teja                      (ipv4.gateway == "0.0.0.0")) ||
9729010ec2eSRavi Teja                     (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
9731abe55efSEd Tanous                 {
9744a0cb85cSEd Tanous                     ipv4.gateway = ethData.default_gateway;
975179db1d7SKowalski, Kamil                 }
976179db1d7SKowalski, Kamil             }
977179db1d7SKowalski, Kamil 
9782c70f800SEd Tanous             extractIPV6Data(ethifaceId, resp, ipv6Data);
9794e0453b1SGunnar Mills             // Finally make a callback with useful data
98001784826SJohnathan Mantey             callback(true, ethData, ipv4Data, ipv6Data);
981179db1d7SKowalski, Kamil         },
982179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
983179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
984271584abSEd Tanous }
985179db1d7SKowalski, Kamil 
986179db1d7SKowalski, Kamil /**
9879391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
9889391bb9cSRapkiewicz, Pawel  * Manager
9891abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
9901abe55efSEd Tanous  * into JSON.
9919391bb9cSRapkiewicz, Pawel  */
9929391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
9931abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
9941abe55efSEd Tanous {
99555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
996f94c4ecfSEd Tanous         [callback{std::forward<CallbackFunc>(callback)}](
99781ce609eSEd Tanous             const boost::system::error_code errorCode,
998711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
9991abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
10001abe55efSEd Tanous             // ethernet interfaces
10012c70f800SEd Tanous             boost::container::flat_set<std::string> ifaceList;
10022c70f800SEd Tanous             ifaceList.reserve(resp.size());
100381ce609eSEd Tanous             if (errorCode)
10041abe55efSEd Tanous             {
10052c70f800SEd Tanous                 callback(false, ifaceList);
10069391bb9cSRapkiewicz, Pawel                 return;
10079391bb9cSRapkiewicz, Pawel             }
10089391bb9cSRapkiewicz, Pawel 
10099391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
10104a0cb85cSEd Tanous             for (const auto& objpath : resp)
10111abe55efSEd Tanous             {
10129391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
10134a0cb85cSEd Tanous                 for (const auto& interface : objpath.second)
10141abe55efSEd Tanous                 {
10151abe55efSEd Tanous                     // If interface is
10164a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
10174a0cb85cSEd Tanous                     // what we're looking for.
10189391bb9cSRapkiewicz, Pawel                     if (interface.first ==
10191abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
10201abe55efSEd Tanous                     {
10212dfd18efSEd Tanous                         std::string ifaceId = objpath.first.filename();
10222dfd18efSEd Tanous                         if (ifaceId.empty())
10231abe55efSEd Tanous                         {
10242dfd18efSEd Tanous                             continue;
10259391bb9cSRapkiewicz, Pawel                         }
10262dfd18efSEd Tanous                         // and put it into output vector.
10272dfd18efSEd Tanous                         ifaceList.emplace(ifaceId);
10289391bb9cSRapkiewicz, Pawel                     }
10299391bb9cSRapkiewicz, Pawel                 }
10309391bb9cSRapkiewicz, Pawel             }
1031a434f2bdSEd Tanous             // Finally make a callback with useful data
10322c70f800SEd Tanous             callback(true, ifaceList);
10339391bb9cSRapkiewicz, Pawel         },
1034aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1035aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1036271584abSEd Tanous }
10379391bb9cSRapkiewicz, Pawel 
10384f48d5f6SEd Tanous inline void
10394f48d5f6SEd Tanous     handleHostnamePatch(const std::string& hostname,
10408d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
10411abe55efSEd Tanous {
1042ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
1043ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
1044ab6554f1SJoshi-Mansi     {
1045ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
1046ab6554f1SJoshi-Mansi                                            "HostName");
1047ab6554f1SJoshi-Mansi         return;
1048ab6554f1SJoshi-Mansi     }
1049bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
1050bc0bd6e0SEd Tanous         [asyncResp](const boost::system::error_code ec) {
10514a0cb85cSEd Tanous             if (ec)
10524a0cb85cSEd Tanous             {
1053a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
10541abe55efSEd Tanous             }
1055bc0bd6e0SEd Tanous         },
1056bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1057bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
1058bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1059168e20c1SEd Tanous         dbus::utility::DbusVariantType(hostname));
1060588c3f0dSKowalski, Kamil }
1061588c3f0dSKowalski, Kamil 
10624f48d5f6SEd Tanous inline void
106335fb5311STejas Patil     handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
106435fb5311STejas Patil                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
106535fb5311STejas Patil {
106635fb5311STejas Patil     sdbusplus::message::object_path objPath =
106735fb5311STejas Patil         "/xyz/openbmc_project/network/" + ifaceId;
106835fb5311STejas Patil     crow::connections::systemBus->async_method_call(
106935fb5311STejas Patil         [asyncResp](const boost::system::error_code ec) {
107035fb5311STejas Patil             if (ec)
107135fb5311STejas Patil             {
107235fb5311STejas Patil                 messages::internalError(asyncResp->res);
107335fb5311STejas Patil             }
107435fb5311STejas Patil         },
107535fb5311STejas Patil         "xyz.openbmc_project.Network", objPath,
107635fb5311STejas Patil         "org.freedesktop.DBus.Properties", "Set",
107735fb5311STejas Patil         "xyz.openbmc_project.Network.EthernetInterface", "MTU",
107835fb5311STejas Patil         std::variant<size_t>(mtuSize));
107935fb5311STejas Patil }
108035fb5311STejas Patil 
108135fb5311STejas Patil inline void
10824f48d5f6SEd Tanous     handleDomainnamePatch(const std::string& ifaceId,
1083bf648f77SEd Tanous                           const std::string& domainname,
10848d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1085ab6554f1SJoshi-Mansi {
1086ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
1087ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
1088ab6554f1SJoshi-Mansi         [asyncResp](const boost::system::error_code ec) {
1089ab6554f1SJoshi-Mansi             if (ec)
1090ab6554f1SJoshi-Mansi             {
1091ab6554f1SJoshi-Mansi                 messages::internalError(asyncResp->res);
1092ab6554f1SJoshi-Mansi             }
1093ab6554f1SJoshi-Mansi         },
1094ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
1095ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
1096ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
1097ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1098168e20c1SEd Tanous         dbus::utility::DbusVariantType(vectorDomainname));
1099ab6554f1SJoshi-Mansi }
1100ab6554f1SJoshi-Mansi 
11014f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname)
1102bf648f77SEd Tanous {
1103bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
1104bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1105bf648f77SEd Tanous     {
1106bf648f77SEd Tanous         return false;
1107bf648f77SEd Tanous     }
1108bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
1109bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
1110bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
1111bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
1112bf648f77SEd Tanous     const std::regex pattern(
1113bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1114bf648f77SEd Tanous 
1115bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
1116bf648f77SEd Tanous }
1117bf648f77SEd Tanous 
11184f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname)
1119bf648f77SEd Tanous {
1120bf648f77SEd Tanous     // Can have multiple subdomains
1121bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
11220fda0f12SGeorge Liu     const std::regex pattern(
11230fda0f12SGeorge Liu         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
1124bf648f77SEd Tanous 
1125bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
1126bf648f77SEd Tanous }
1127bf648f77SEd Tanous 
11284f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
11298d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1130ab6554f1SJoshi-Mansi {
1131ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
1132ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
1133ab6554f1SJoshi-Mansi     {
1134ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1135ab6554f1SJoshi-Mansi         return;
1136ab6554f1SJoshi-Mansi     }
1137ab6554f1SJoshi-Mansi 
1138ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
1139ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
1140ab6554f1SJoshi-Mansi     {
1141ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1142ab6554f1SJoshi-Mansi         return;
1143ab6554f1SJoshi-Mansi     }
1144ab6554f1SJoshi-Mansi 
1145ab6554f1SJoshi-Mansi     std::string hostname;
1146ab6554f1SJoshi-Mansi     std::string domainname;
1147ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
1148ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
1149ab6554f1SJoshi-Mansi 
1150ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1151ab6554f1SJoshi-Mansi     {
1152ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1153ab6554f1SJoshi-Mansi         return;
1154ab6554f1SJoshi-Mansi     }
1155ab6554f1SJoshi-Mansi 
1156ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
1157ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
1158ab6554f1SJoshi-Mansi }
1159ab6554f1SJoshi-Mansi 
11604f48d5f6SEd Tanous inline void
11614f48d5f6SEd Tanous     handleMACAddressPatch(const std::string& ifaceId,
1162bf648f77SEd Tanous                           const std::string& macAddress,
11638d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1164d577665bSRatan Gupta {
1165d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
1166d577665bSRatan Gupta         [asyncResp, macAddress](const boost::system::error_code ec) {
1167d577665bSRatan Gupta             if (ec)
1168d577665bSRatan Gupta             {
1169d577665bSRatan Gupta                 messages::internalError(asyncResp->res);
1170d577665bSRatan Gupta                 return;
1171d577665bSRatan Gupta             }
1172d577665bSRatan Gupta         },
1173d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1174d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1175d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1176d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1177168e20c1SEd Tanous         dbus::utility::DbusVariantType(macAddress));
1178d577665bSRatan Gupta }
1179286b9118SJohnathan Mantey 
11804f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
11814f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
11824f48d5f6SEd Tanous                            const bool v6Value,
11838d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1184da131a9aSJennifer Lee {
11852c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1186da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1187da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1188da131a9aSJennifer Lee             if (ec)
1189da131a9aSJennifer Lee             {
1190da131a9aSJennifer Lee                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1191da131a9aSJennifer Lee                 messages::internalError(asyncResp->res);
1192da131a9aSJennifer Lee                 return;
1193da131a9aSJennifer Lee             }
11948f7e9c19SJayaprakash Mutyala             messages::success(asyncResp->res);
1195da131a9aSJennifer Lee         },
1196da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1197da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1198da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1199da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1200168e20c1SEd Tanous         dbus::utility::DbusVariantType{dhcp});
1201da131a9aSJennifer Lee }
12021f8c7b5dSJohnathan Mantey 
12034f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1204eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
12058d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1206eeedda23SJohnathan Mantey {
1207eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1208eeedda23SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1209eeedda23SJohnathan Mantey             if (ec)
1210eeedda23SJohnathan Mantey             {
1211eeedda23SJohnathan Mantey                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1212eeedda23SJohnathan Mantey                 messages::internalError(asyncResp->res);
1213eeedda23SJohnathan Mantey                 return;
1214eeedda23SJohnathan Mantey             }
1215eeedda23SJohnathan Mantey         },
1216eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1217eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1218eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1219eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1220168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1221eeedda23SJohnathan Mantey }
1222eeedda23SJohnathan Mantey 
12234f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
12248d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1225da131a9aSJennifer Lee {
1226da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1227da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1228da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1229da131a9aSJennifer Lee             if (ec)
1230da131a9aSJennifer Lee             {
1231da131a9aSJennifer Lee                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1232da131a9aSJennifer Lee                 messages::internalError(asyncResp->res);
1233da131a9aSJennifer Lee                 return;
1234da131a9aSJennifer Lee             }
1235da131a9aSJennifer Lee         },
1236da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1237da131a9aSJennifer Lee         "/xyz/openbmc_project/network/config/dhcp",
1238da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1239da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1240168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1241da131a9aSJennifer Lee }
1242d577665bSRatan Gupta 
12434f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
12441f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1245f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1246f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
12478d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1248da131a9aSJennifer Lee {
12491f8c7b5dSJohnathan Mantey     bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1250bf648f77SEd Tanous     bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1251da131a9aSJennifer Lee 
12521f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
12531f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
12541f8c7b5dSJohnathan Mantey 
12551f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
12561f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1257da131a9aSJennifer Lee     {
12581f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
12591f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
12601f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
12611f8c7b5dSJohnathan Mantey         {
1262bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1263bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
12641f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1265da131a9aSJennifer Lee             return;
1266da131a9aSJennifer Lee         }
12671f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
12681f8c7b5dSJohnathan Mantey     }
12691f8c7b5dSJohnathan Mantey     else
1270da131a9aSJennifer Lee     {
12711f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
12721f8c7b5dSJohnathan Mantey     }
12731f8c7b5dSJohnathan Mantey 
12741f8c7b5dSJohnathan Mantey     bool nextDNS{};
12751f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
12761f8c7b5dSJohnathan Mantey     {
12771f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
12781f8c7b5dSJohnathan Mantey         {
12791f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12801f8c7b5dSJohnathan Mantey             return;
12811f8c7b5dSJohnathan Mantey         }
12821f8c7b5dSJohnathan Mantey         nextDNS = *v4dhcpParms.useDNSServers;
12831f8c7b5dSJohnathan Mantey     }
12841f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useDNSServers)
12851f8c7b5dSJohnathan Mantey     {
12861f8c7b5dSJohnathan Mantey         nextDNS = *v4dhcpParms.useDNSServers;
12871f8c7b5dSJohnathan Mantey     }
12881f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useDNSServers)
12891f8c7b5dSJohnathan Mantey     {
12901f8c7b5dSJohnathan Mantey         nextDNS = *v6dhcpParms.useDNSServers;
12911f8c7b5dSJohnathan Mantey     }
12921f8c7b5dSJohnathan Mantey     else
12931f8c7b5dSJohnathan Mantey     {
12941f8c7b5dSJohnathan Mantey         nextDNS = ethData.DNSEnabled;
12951f8c7b5dSJohnathan Mantey     }
12961f8c7b5dSJohnathan Mantey 
12971f8c7b5dSJohnathan Mantey     bool nextNTP{};
12981f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
12991f8c7b5dSJohnathan Mantey     {
13001f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
13011f8c7b5dSJohnathan Mantey         {
13021f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
13031f8c7b5dSJohnathan Mantey             return;
13041f8c7b5dSJohnathan Mantey         }
13051f8c7b5dSJohnathan Mantey         nextNTP = *v4dhcpParms.useNTPServers;
13061f8c7b5dSJohnathan Mantey     }
13071f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useNTPServers)
13081f8c7b5dSJohnathan Mantey     {
13091f8c7b5dSJohnathan Mantey         nextNTP = *v4dhcpParms.useNTPServers;
13101f8c7b5dSJohnathan Mantey     }
13111f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useNTPServers)
13121f8c7b5dSJohnathan Mantey     {
13131f8c7b5dSJohnathan Mantey         nextNTP = *v6dhcpParms.useNTPServers;
13141f8c7b5dSJohnathan Mantey     }
13151f8c7b5dSJohnathan Mantey     else
13161f8c7b5dSJohnathan Mantey     {
13171f8c7b5dSJohnathan Mantey         nextNTP = ethData.NTPEnabled;
13181f8c7b5dSJohnathan Mantey     }
13191f8c7b5dSJohnathan Mantey 
13201f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
13211f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
13221f8c7b5dSJohnathan Mantey     {
13231f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
13241f8c7b5dSJohnathan Mantey         {
13251f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
13261f8c7b5dSJohnathan Mantey             return;
13271f8c7b5dSJohnathan Mantey         }
13281f8c7b5dSJohnathan Mantey         nextUseDomain = *v4dhcpParms.useUseDomainName;
13291f8c7b5dSJohnathan Mantey     }
13301f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useUseDomainName)
13311f8c7b5dSJohnathan Mantey     {
13321f8c7b5dSJohnathan Mantey         nextUseDomain = *v4dhcpParms.useUseDomainName;
13331f8c7b5dSJohnathan Mantey     }
13341f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useUseDomainName)
13351f8c7b5dSJohnathan Mantey     {
13361f8c7b5dSJohnathan Mantey         nextUseDomain = *v6dhcpParms.useUseDomainName;
13371f8c7b5dSJohnathan Mantey     }
13381f8c7b5dSJohnathan Mantey     else
13391f8c7b5dSJohnathan Mantey     {
13401f8c7b5dSJohnathan Mantey         nextUseDomain = ethData.HostNameEnabled;
13411f8c7b5dSJohnathan Mantey     }
13421f8c7b5dSJohnathan Mantey 
1343da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
13441f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
13451f8c7b5dSJohnathan Mantey                    asyncResp);
1346da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
13471f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1348da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
13491f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
13501f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
13511f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1352da131a9aSJennifer Lee }
135301784826SJohnathan Mantey 
13544f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator
13552c70f800SEd Tanous     getNextStaticIpEntry(
1356bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1357bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
135801784826SJohnathan Mantey {
135917a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
136017a897dfSManojkiran Eda         return value.origin == "Static";
136117a897dfSManojkiran Eda     });
136201784826SJohnathan Mantey }
136301784826SJohnathan Mantey 
13644f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator
13652c70f800SEd Tanous     getNextStaticIpEntry(
1366bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1367bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
136801784826SJohnathan Mantey {
136917a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
137017a897dfSManojkiran Eda         return value.origin == "Static";
137117a897dfSManojkiran Eda     });
137201784826SJohnathan Mantey }
137301784826SJohnathan Mantey 
13744f48d5f6SEd Tanous inline void handleIPv4StaticPatch(
1375f476acbfSRatan Gupta     const std::string& ifaceId, nlohmann::json& input,
137601784826SJohnathan Mantey     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
13778d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13781abe55efSEd Tanous {
137901784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1380f476acbfSRatan Gupta     {
138171f52d96SEd Tanous         messages::propertyValueTypeError(
138271f52d96SEd Tanous             asyncResp->res,
1383bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1384d1d50814SRavi Teja             "IPv4StaticAddresses");
1385f476acbfSRatan Gupta         return;
1386f476acbfSRatan Gupta     }
1387f476acbfSRatan Gupta 
1388271584abSEd Tanous     unsigned entryIdx = 1;
138901784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
139001784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
139101784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
139201784826SJohnathan Mantey     // into the NIC.
139385ffe86aSJiaqing Zhao     boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
13942c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
139501784826SJohnathan Mantey 
1396537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
13971abe55efSEd Tanous     {
13984a0cb85cSEd Tanous         std::string pathString =
1399d1d50814SRavi Teja             "IPv4StaticAddresses/" + std::to_string(entryIdx);
1400179db1d7SKowalski, Kamil 
140101784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1402f476acbfSRatan Gupta         {
1403537174c4SEd Tanous             std::optional<std::string> address;
1404537174c4SEd Tanous             std::optional<std::string> subnetMask;
1405537174c4SEd Tanous             std::optional<std::string> gateway;
1406537174c4SEd Tanous 
1407537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
14087e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
14097e27d832SJohnathan Mantey                                      "Gateway", gateway))
1410537174c4SEd Tanous             {
141101784826SJohnathan Mantey                 messages::propertyValueFormatError(
141271f52d96SEd Tanous                     asyncResp->res,
141371f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
141471f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
141571f52d96SEd Tanous                     pathString);
1416537174c4SEd Tanous                 return;
1417179db1d7SKowalski, Kamil             }
1418179db1d7SKowalski, Kamil 
141901784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
142001784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
142101784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
142201784826SJohnathan Mantey             // current request.
1423271584abSEd Tanous             const std::string* addr = nullptr;
1424271584abSEd Tanous             const std::string* gw = nullptr;
142501784826SJohnathan Mantey             uint8_t prefixLength = 0;
142601784826SJohnathan Mantey             bool errorInEntry = false;
1427537174c4SEd Tanous             if (address)
14281abe55efSEd Tanous             {
142901784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*address))
14301abe55efSEd Tanous                 {
143101784826SJohnathan Mantey                     addr = &(*address);
14324a0cb85cSEd Tanous                 }
143301784826SJohnathan Mantey                 else
143401784826SJohnathan Mantey                 {
1435bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1436bf648f77SEd Tanous                                                        pathString + "/Address");
143701784826SJohnathan Mantey                     errorInEntry = true;
143801784826SJohnathan Mantey                 }
143901784826SJohnathan Mantey             }
144085ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
144101784826SJohnathan Mantey             {
144285ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
144301784826SJohnathan Mantey             }
144401784826SJohnathan Mantey             else
144501784826SJohnathan Mantey             {
144601784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
144701784826SJohnathan Mantey                                           pathString + "/Address");
144801784826SJohnathan Mantey                 errorInEntry = true;
14494a0cb85cSEd Tanous             }
14504a0cb85cSEd Tanous 
1451537174c4SEd Tanous             if (subnetMask)
14524a0cb85cSEd Tanous             {
1453537174c4SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
14544a0cb85cSEd Tanous                 {
1455f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1456537174c4SEd Tanous                         asyncResp->res, *subnetMask,
14574a0cb85cSEd Tanous                         pathString + "/SubnetMask");
145801784826SJohnathan Mantey                     errorInEntry = true;
14594a0cb85cSEd Tanous                 }
14604a0cb85cSEd Tanous             }
146185ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
14624a0cb85cSEd Tanous             {
146385ffe86aSJiaqing Zhao                 if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
146401784826SJohnathan Mantey                                                 &prefixLength))
14654a0cb85cSEd Tanous                 {
146601784826SJohnathan Mantey                     messages::propertyValueFormatError(
146785ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
146801784826SJohnathan Mantey                         pathString + "/SubnetMask");
146901784826SJohnathan Mantey                     errorInEntry = true;
14704a0cb85cSEd Tanous                 }
14714a0cb85cSEd Tanous             }
14721abe55efSEd Tanous             else
14731abe55efSEd Tanous             {
147401784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
147501784826SJohnathan Mantey                                           pathString + "/SubnetMask");
147601784826SJohnathan Mantey                 errorInEntry = true;
147701784826SJohnathan Mantey             }
147801784826SJohnathan Mantey 
147901784826SJohnathan Mantey             if (gateway)
148001784826SJohnathan Mantey             {
148101784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*gateway))
148201784826SJohnathan Mantey                 {
148301784826SJohnathan Mantey                     gw = &(*gateway);
148401784826SJohnathan Mantey                 }
148501784826SJohnathan Mantey                 else
148601784826SJohnathan Mantey                 {
1487bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1488bf648f77SEd Tanous                                                        pathString + "/Gateway");
148901784826SJohnathan Mantey                     errorInEntry = true;
149001784826SJohnathan Mantey                 }
149101784826SJohnathan Mantey             }
149285ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
149301784826SJohnathan Mantey             {
149485ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
149501784826SJohnathan Mantey             }
149601784826SJohnathan Mantey             else
14971abe55efSEd Tanous             {
1498a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
14994a0cb85cSEd Tanous                                           pathString + "/Gateway");
150001784826SJohnathan Mantey                 errorInEntry = true;
15014a0cb85cSEd Tanous             }
15024a0cb85cSEd Tanous 
150301784826SJohnathan Mantey             if (errorInEntry)
15041abe55efSEd Tanous             {
150501784826SJohnathan Mantey                 return;
15064a0cb85cSEd Tanous             }
15074a0cb85cSEd Tanous 
150885ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
15091abe55efSEd Tanous             {
151085ffe86aSJiaqing Zhao                 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
1511bf648f77SEd Tanous                                     *addr, asyncResp);
151285ffe86aSJiaqing Zhao                 nicIpEntry =
151385ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
1514588c3f0dSKowalski, Kamil             }
151501784826SJohnathan Mantey             else
151601784826SJohnathan Mantey             {
1517cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1518cb13a392SEd Tanous                            asyncResp);
15194a0cb85cSEd Tanous             }
15204a0cb85cSEd Tanous             entryIdx++;
15214a0cb85cSEd Tanous         }
152201784826SJohnathan Mantey         else
152301784826SJohnathan Mantey         {
152485ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
152501784826SJohnathan Mantey             {
152601784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
152701784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
152801784826SJohnathan Mantey                 // in error, so bail out.
152901784826SJohnathan Mantey                 if (thisJson.is_null())
153001784826SJohnathan Mantey                 {
153101784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
153201784826SJohnathan Mantey                     return;
153301784826SJohnathan Mantey                 }
153401784826SJohnathan Mantey                 messages::propertyValueFormatError(
153571f52d96SEd Tanous                     asyncResp->res,
153671f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
153771f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
153871f52d96SEd Tanous                     pathString);
153901784826SJohnathan Mantey                 return;
154001784826SJohnathan Mantey             }
154101784826SJohnathan Mantey 
154201784826SJohnathan Mantey             if (thisJson.is_null())
154301784826SJohnathan Mantey             {
154485ffe86aSJiaqing Zhao                 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
154501784826SJohnathan Mantey             }
154685ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
154701784826SJohnathan Mantey             {
154885ffe86aSJiaqing Zhao                 nicIpEntry =
154985ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
155001784826SJohnathan Mantey             }
155101784826SJohnathan Mantey             entryIdx++;
155201784826SJohnathan Mantey         }
155301784826SJohnathan Mantey     }
15544a0cb85cSEd Tanous }
15554a0cb85cSEd Tanous 
15564f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1557f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1558f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
15598d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1560f85837bfSRAJESWARAN THILLAIGOVINDAN {
1561f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
1562286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1563f85837bfSRAJESWARAN THILLAIGOVINDAN             if (ec)
1564f85837bfSRAJESWARAN THILLAIGOVINDAN             {
1565f85837bfSRAJESWARAN THILLAIGOVINDAN                 messages::internalError(asyncResp->res);
1566f85837bfSRAJESWARAN THILLAIGOVINDAN                 return;
1567f85837bfSRAJESWARAN THILLAIGOVINDAN             }
1568f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1569f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1570f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1571f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1572bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1573168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1574f85837bfSRAJESWARAN THILLAIGOVINDAN }
1575f85837bfSRAJESWARAN THILLAIGOVINDAN 
15764f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1577f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
157801784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data,
15798d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1580e48c0fc5SRavi Teja {
158101784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1582e48c0fc5SRavi Teja     {
158371f52d96SEd Tanous         messages::propertyValueTypeError(
158471f52d96SEd Tanous             asyncResp->res,
1585bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1586e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1587e48c0fc5SRavi Teja         return;
1588e48c0fc5SRavi Teja     }
1589271584abSEd Tanous     size_t entryIdx = 1;
159085ffe86aSJiaqing Zhao     boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
15912c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1592f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1593e48c0fc5SRavi Teja     {
1594e48c0fc5SRavi Teja         std::string pathString =
1595e48c0fc5SRavi Teja             "IPv6StaticAddresses/" + std::to_string(entryIdx);
1596e48c0fc5SRavi Teja 
159701784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1598e48c0fc5SRavi Teja         {
1599e48c0fc5SRavi Teja             std::optional<std::string> address;
1600e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1601f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1602bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1603bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1604e48c0fc5SRavi Teja             {
160501784826SJohnathan Mantey                 messages::propertyValueFormatError(
160671f52d96SEd Tanous                     asyncResp->res,
160771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
160871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
160971f52d96SEd Tanous                     pathString);
1610e48c0fc5SRavi Teja                 return;
1611e48c0fc5SRavi Teja             }
1612e48c0fc5SRavi Teja 
1613543f4400SEd Tanous             const std::string* addr = nullptr;
1614543f4400SEd Tanous             uint8_t prefix = 0;
161501784826SJohnathan Mantey 
161601784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
161701784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
161801784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
161901784826SJohnathan Mantey             // current request.
1620e48c0fc5SRavi Teja             if (address)
1621e48c0fc5SRavi Teja             {
162201784826SJohnathan Mantey                 addr = &(*address);
1623e48c0fc5SRavi Teja             }
162485ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
162501784826SJohnathan Mantey             {
162685ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
162701784826SJohnathan Mantey             }
162801784826SJohnathan Mantey             else
162901784826SJohnathan Mantey             {
163001784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
163101784826SJohnathan Mantey                                           pathString + "/Address");
163201784826SJohnathan Mantey                 return;
1633e48c0fc5SRavi Teja             }
1634e48c0fc5SRavi Teja 
1635e48c0fc5SRavi Teja             if (prefixLength)
1636e48c0fc5SRavi Teja             {
163701784826SJohnathan Mantey                 prefix = *prefixLength;
163801784826SJohnathan Mantey             }
163985ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1640e48c0fc5SRavi Teja             {
164185ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1642e48c0fc5SRavi Teja             }
1643e48c0fc5SRavi Teja             else
1644e48c0fc5SRavi Teja             {
1645e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1646e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
164701784826SJohnathan Mantey                 return;
1648e48c0fc5SRavi Teja             }
1649e48c0fc5SRavi Teja 
165085ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1651e48c0fc5SRavi Teja             {
165285ffe86aSJiaqing Zhao                 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
1653e48c0fc5SRavi Teja                                     asyncResp);
165485ffe86aSJiaqing Zhao                 nicIpEntry =
165585ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
165601784826SJohnathan Mantey             }
165701784826SJohnathan Mantey             else
165801784826SJohnathan Mantey             {
165901784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1660e48c0fc5SRavi Teja             }
1661e48c0fc5SRavi Teja             entryIdx++;
1662e48c0fc5SRavi Teja         }
166301784826SJohnathan Mantey         else
166401784826SJohnathan Mantey         {
166585ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
166601784826SJohnathan Mantey             {
166701784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
166801784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
166901784826SJohnathan Mantey                 // in error, so bail out.
167001784826SJohnathan Mantey                 if (thisJson.is_null())
167101784826SJohnathan Mantey                 {
167201784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
167301784826SJohnathan Mantey                     return;
167401784826SJohnathan Mantey                 }
167501784826SJohnathan Mantey                 messages::propertyValueFormatError(
167671f52d96SEd Tanous                     asyncResp->res,
167771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
167871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
167971f52d96SEd Tanous                     pathString);
168001784826SJohnathan Mantey                 return;
168101784826SJohnathan Mantey             }
168201784826SJohnathan Mantey 
168301784826SJohnathan Mantey             if (thisJson.is_null())
168401784826SJohnathan Mantey             {
168585ffe86aSJiaqing Zhao                 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
168601784826SJohnathan Mantey             }
168785ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
168801784826SJohnathan Mantey             {
168985ffe86aSJiaqing Zhao                 nicIpEntry =
169085ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
169101784826SJohnathan Mantey             }
169201784826SJohnathan Mantey             entryIdx++;
169301784826SJohnathan Mantey         }
169401784826SJohnathan Mantey     }
1695e48c0fc5SRavi Teja }
1696e48c0fc5SRavi Teja 
16974f48d5f6SEd Tanous inline void parseInterfaceData(
16988d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16998d1b46d7Szhanghch05     const std::string& ifaceId, const EthernetInterfaceData& ethData,
1700e48c0fc5SRavi Teja     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
170101784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data)
17024a0cb85cSEd Tanous {
1703eeedda23SJohnathan Mantey     constexpr const std::array<const char*, 1> inventoryForEthernet = {
1704eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1705eeedda23SJohnathan Mantey 
17062c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
170781ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
17082c70f800SEd Tanous     jsonResponse["@odata.id"] =
170981ce609eSEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
17102c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1711eeedda23SJohnathan Mantey 
1712eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1713eeedda23SJohnathan Mantey 
1714eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1715eeedda23SJohnathan Mantey         [health](const boost::system::error_code ec,
1716914e2d5dSEd Tanous                  const std::vector<std::string>& resp) {
1717eeedda23SJohnathan Mantey             if (ec)
1718029573d4SEd Tanous             {
1719eeedda23SJohnathan Mantey                 return;
1720eeedda23SJohnathan Mantey             }
1721eeedda23SJohnathan Mantey 
1722914e2d5dSEd Tanous             health->inventory = resp;
1723eeedda23SJohnathan Mantey         },
1724eeedda23SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper",
1725eeedda23SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
1726bf648f77SEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1727bf648f77SEd Tanous         inventoryForEthernet);
1728eeedda23SJohnathan Mantey 
1729eeedda23SJohnathan Mantey     health->populate();
1730eeedda23SJohnathan Mantey 
1731eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1732eeedda23SJohnathan Mantey     {
17332c70f800SEd Tanous         jsonResponse["LinkStatus"] = "LinkUp";
17342c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1735029573d4SEd Tanous     }
1736029573d4SEd Tanous     else
1737029573d4SEd Tanous     {
17382c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
17392c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1740029573d4SEd Tanous     }
1741aa05fb27SJohnathan Mantey 
17422c70f800SEd Tanous     jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
17432c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
174435fb5311STejas Patil     jsonResponse["MTUSize"] = ethData.mtuSize;
17452c70f800SEd Tanous     jsonResponse["MACAddress"] = ethData.mac_address;
17462c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
17471f8c7b5dSJohnathan Mantey         translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
17482c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
17492c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
17502c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
17511f8c7b5dSJohnathan Mantey 
17522c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
17531f8c7b5dSJohnathan Mantey         translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
17541f8c7b5dSJohnathan Mantey                                                                : "Disabled";
17552c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
17562c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
17572c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
17582a133282Smanojkiraneda 
17594a0cb85cSEd Tanous     if (!ethData.hostname.empty())
17604a0cb85cSEd Tanous     {
17612c70f800SEd Tanous         jsonResponse["HostName"] = ethData.hostname;
1762ab6554f1SJoshi-Mansi 
1763ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1764ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1765ab6554f1SJoshi-Mansi         // FQDN
1766f23b7296SEd Tanous         std::string fqdn = ethData.hostname;
1767d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1768d24bfc7aSJennifer Lee         {
17692c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1770d24bfc7aSJennifer Lee         }
17712c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
17724a0cb85cSEd Tanous     }
17734a0cb85cSEd Tanous 
17742c70f800SEd Tanous     jsonResponse["VLANs"] = {
1775bf648f77SEd Tanous         {"@odata.id",
1776bf648f77SEd Tanous          "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1777fda13ad2SSunitha Harish 
17782c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
17792c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
17804a0cb85cSEd Tanous 
17812c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
17822c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
17832c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
17842c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
17859eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
17864a0cb85cSEd Tanous     {
1787fa5053a6SGunnar Mills 
17882c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1789fa5053a6SGunnar Mills         if (gatewayStr.empty())
1790fa5053a6SGunnar Mills         {
1791fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1792fa5053a6SGunnar Mills         }
1793fa5053a6SGunnar Mills 
17942c70f800SEd Tanous         ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
17952c70f800SEd Tanous                              {"SubnetMask", ipv4Config.netmask},
17962c70f800SEd Tanous                              {"Address", ipv4Config.address},
1797fa5053a6SGunnar Mills                              {"Gateway", gatewayStr}});
17982c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1799d1d50814SRavi Teja         {
18002c70f800SEd Tanous             ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
18012c70f800SEd Tanous                                        {"SubnetMask", ipv4Config.netmask},
18022c70f800SEd Tanous                                        {"Address", ipv4Config.address},
1803d1d50814SRavi Teja                                        {"Gateway", gatewayStr}});
1804d1d50814SRavi Teja         }
180501784826SJohnathan Mantey     }
1806d1d50814SRavi Teja 
18077ea79e5eSRavi Teja     std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
18087ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
18097ea79e5eSRavi Teja     {
18107ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
18117ea79e5eSRavi Teja     }
18127ea79e5eSRavi Teja 
18137ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1814e48c0fc5SRavi Teja 
18152c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
18162c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
18172c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
18182c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
18197f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
18202c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
18217f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
18229eb808c1SEd Tanous     for (const auto& ipv6Config : ipv6Data)
1823e48c0fc5SRavi Teja     {
18242c70f800SEd Tanous         ipv6Array.push_back({{"Address", ipv6Config.address},
18252c70f800SEd Tanous                              {"PrefixLength", ipv6Config.prefixLength},
18262c70f800SEd Tanous                              {"AddressOrigin", ipv6Config.origin},
18275fd16e4bSJohnathan Mantey                              {"AddressState", nullptr}});
18282c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1829e48c0fc5SRavi Teja         {
18302c70f800SEd Tanous             ipv6StaticArray.push_back(
18312c70f800SEd Tanous                 {{"Address", ipv6Config.address},
18327a474a5fSJiaqing Zhao                  {"PrefixLength", ipv6Config.prefixLength}});
183301784826SJohnathan Mantey         }
1834e48c0fc5SRavi Teja     }
1835588c3f0dSKowalski, Kamil }
1836588c3f0dSKowalski, Kamil 
18374f48d5f6SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse,
1838bf648f77SEd Tanous                                const std::string& parentIfaceId,
1839bf648f77SEd Tanous                                const std::string& ifaceId,
1840bf648f77SEd Tanous                                const EthernetInterfaceData& ethData)
18411abe55efSEd Tanous {
1842bf648f77SEd Tanous     // Fill out obvious data...
1843bf648f77SEd Tanous     jsonResponse["Id"] = ifaceId;
1844bf648f77SEd Tanous     jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1845bf648f77SEd Tanous                                 parentIfaceId + "/VLANs/" + ifaceId;
1846bf648f77SEd Tanous 
1847bf648f77SEd Tanous     jsonResponse["VLANEnable"] = true;
1848bf648f77SEd Tanous     if (!ethData.vlan_id.empty())
1849bf648f77SEd Tanous     {
1850bf648f77SEd Tanous         jsonResponse["VLANId"] = ethData.vlan_id.back();
1851bf648f77SEd Tanous     }
1852bf648f77SEd Tanous }
1853bf648f77SEd Tanous 
18544f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1855bf648f77SEd Tanous {
1856dcf2ebc0SEd Tanous     return boost::starts_with(iface, parent + "_");
1857bf648f77SEd Tanous }
1858bf648f77SEd Tanous 
1859bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1860bf648f77SEd Tanous {
1861bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1862ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
1863bf648f77SEd Tanous         .methods(
1864bf648f77SEd Tanous             boost::beast::http::verb::
1865bf648f77SEd Tanous                 get)([](const crow::Request&,
1866bf648f77SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1867bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1868bf648f77SEd Tanous                 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1869bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1870bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1871bf648f77SEd Tanous             asyncResp->res.jsonValue["Name"] =
1872bf648f77SEd Tanous                 "Ethernet Network Interface Collection";
1873bf648f77SEd Tanous             asyncResp->res.jsonValue["Description"] =
1874bf648f77SEd Tanous                 "Collection of EthernetInterfaces for this Manager";
1875bf648f77SEd Tanous 
1876bf648f77SEd Tanous             // Get eth interface list, and call the below callback for JSON
1877bf648f77SEd Tanous             // preparation
1878bf648f77SEd Tanous             getEthernetIfaceList([asyncResp](const bool& success,
1879bf648f77SEd Tanous                                              const boost::container::flat_set<
1880bf648f77SEd Tanous                                                  std::string>& ifaceList) {
1881bf648f77SEd Tanous                 if (!success)
18821abe55efSEd Tanous                 {
1883f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
18849391bb9cSRapkiewicz, Pawel                     return;
18859391bb9cSRapkiewicz, Pawel                 }
18869391bb9cSRapkiewicz, Pawel 
1887bf648f77SEd Tanous                 nlohmann::json& ifaceArray =
1888bf648f77SEd Tanous                     asyncResp->res.jsonValue["Members"];
1889bf648f77SEd Tanous                 ifaceArray = nlohmann::json::array();
1890bf648f77SEd Tanous                 std::string tag = "_";
1891bf648f77SEd Tanous                 for (const std::string& ifaceItem : ifaceList)
1892bf648f77SEd Tanous                 {
1893bf648f77SEd Tanous                     std::size_t found = ifaceItem.find(tag);
1894bf648f77SEd Tanous                     if (found == std::string::npos)
1895bf648f77SEd Tanous                     {
1896bf648f77SEd Tanous                         ifaceArray.push_back(
1897bf648f77SEd Tanous                             {{"@odata.id",
1898bf648f77SEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1899bf648f77SEd Tanous                                   ifaceItem}});
1900bf648f77SEd Tanous                     }
1901bf648f77SEd Tanous                 }
1902bf648f77SEd Tanous 
1903bf648f77SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
1904bf648f77SEd Tanous                     ifaceArray.size();
1905bf648f77SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
1906bf648f77SEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
1907bf648f77SEd Tanous             });
1908bf648f77SEd Tanous         });
1909bf648f77SEd Tanous 
1910bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1911ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1912bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
1913bf648f77SEd Tanous             [](const crow::Request&,
1914bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1915bf648f77SEd Tanous                const std::string& ifaceId) {
19164a0cb85cSEd Tanous                 getEthernetIfaceData(
1917bf648f77SEd Tanous                     ifaceId,
1918bf648f77SEd Tanous                     [asyncResp,
1919bf648f77SEd Tanous                      ifaceId](const bool& success,
1920bf648f77SEd Tanous                               const EthernetInterfaceData& ethData,
1921bf648f77SEd Tanous                               const boost::container::flat_set<IPv4AddressData>&
1922bf648f77SEd Tanous                                   ipv4Data,
1923bf648f77SEd Tanous                               const boost::container::flat_set<IPv6AddressData>&
1924bf648f77SEd Tanous                                   ipv6Data) {
19254a0cb85cSEd Tanous                         if (!success)
19261abe55efSEd Tanous                         {
1927bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
1928bf648f77SEd Tanous                             // existing object, and other errors
1929bf648f77SEd Tanous                             messages::resourceNotFound(
1930bf648f77SEd Tanous                                 asyncResp->res, "EthernetInterface", ifaceId);
19314a0cb85cSEd Tanous                             return;
19329391bb9cSRapkiewicz, Pawel                         }
19334c9afe43SEd Tanous 
19340f74e643SEd Tanous                         asyncResp->res.jsonValue["@odata.type"] =
1935fda13ad2SSunitha Harish                             "#EthernetInterface.v1_4_1.EthernetInterface";
1936bf648f77SEd Tanous                         asyncResp->res.jsonValue["Name"] =
1937bf648f77SEd Tanous                             "Manager Ethernet Interface";
19380f74e643SEd Tanous                         asyncResp->res.jsonValue["Description"] =
19390f74e643SEd Tanous                             "Management Network Interface";
19400f74e643SEd Tanous 
1941bf648f77SEd Tanous                         parseInterfaceData(asyncResp, ifaceId, ethData,
1942bf648f77SEd Tanous                                            ipv4Data, ipv6Data);
19439391bb9cSRapkiewicz, Pawel                     });
1944bf648f77SEd Tanous             });
19459391bb9cSRapkiewicz, Pawel 
1946bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1947ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1948ed398213SEd Tanous 
1949bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
1950bf648f77SEd Tanous             [](const crow::Request& req,
1951bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1952bf648f77SEd Tanous                const std::string& ifaceId) {
1953bc0bd6e0SEd Tanous                 std::optional<std::string> hostname;
1954ab6554f1SJoshi-Mansi                 std::optional<std::string> fqdn;
1955d577665bSRatan Gupta                 std::optional<std::string> macAddress;
19569a6fc6feSRavi Teja                 std::optional<std::string> ipv6DefaultGateway;
1957d1d50814SRavi Teja                 std::optional<nlohmann::json> ipv4StaticAddresses;
1958e48c0fc5SRavi Teja                 std::optional<nlohmann::json> ipv6StaticAddresses;
1959f85837bfSRAJESWARAN THILLAIGOVINDAN                 std::optional<std::vector<std::string>> staticNameServers;
1960da131a9aSJennifer Lee                 std::optional<nlohmann::json> dhcpv4;
19611f8c7b5dSJohnathan Mantey                 std::optional<nlohmann::json> dhcpv6;
1962eeedda23SJohnathan Mantey                 std::optional<bool> interfaceEnabled;
196335fb5311STejas Patil                 std::optional<size_t> mtuSize;
19641f8c7b5dSJohnathan Mantey                 DHCPParameters v4dhcpParms;
19651f8c7b5dSJohnathan Mantey                 DHCPParameters v6dhcpParms;
19660627a2c7SEd Tanous 
19671f8c7b5dSJohnathan Mantey                 if (!json_util::readJson(
19688d1b46d7Szhanghch05                         req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1969bf648f77SEd Tanous                         "IPv4StaticAddresses", ipv4StaticAddresses,
1970bf648f77SEd Tanous                         "MACAddress", macAddress, "StaticNameServers",
1971bf648f77SEd Tanous                         staticNameServers, "IPv6DefaultGateway",
1972bf648f77SEd Tanous                         ipv6DefaultGateway, "IPv6StaticAddresses",
1973ab6554f1SJoshi-Mansi                         ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
197435fb5311STejas Patil                         "MTUSize", mtuSize, "InterfaceEnabled",
197535fb5311STejas Patil                         interfaceEnabled))
19761abe55efSEd Tanous                 {
1977588c3f0dSKowalski, Kamil                     return;
1978588c3f0dSKowalski, Kamil                 }
1979da131a9aSJennifer Lee                 if (dhcpv4)
1980da131a9aSJennifer Lee                 {
1981bf648f77SEd Tanous                     if (!json_util::readJson(
1982bf648f77SEd Tanous                             *dhcpv4, asyncResp->res, "DHCPEnabled",
19831f8c7b5dSJohnathan Mantey                             v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
19841f8c7b5dSJohnathan Mantey                             v4dhcpParms.useDNSServers, "UseNTPServers",
19851f8c7b5dSJohnathan Mantey                             v4dhcpParms.useNTPServers, "UseDomainName",
19861f8c7b5dSJohnathan Mantey                             v4dhcpParms.useUseDomainName))
19871f8c7b5dSJohnathan Mantey                     {
19881f8c7b5dSJohnathan Mantey                         return;
19891f8c7b5dSJohnathan Mantey                     }
19901f8c7b5dSJohnathan Mantey                 }
19911f8c7b5dSJohnathan Mantey 
19921f8c7b5dSJohnathan Mantey                 if (dhcpv6)
19931f8c7b5dSJohnathan Mantey                 {
1994bf648f77SEd Tanous                     if (!json_util::readJson(
1995bf648f77SEd Tanous                             *dhcpv6, asyncResp->res, "OperatingMode",
1996bf648f77SEd Tanous                             v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers",
1997bf648f77SEd Tanous                             v6dhcpParms.useDNSServers, "UseNTPServers",
1998bf648f77SEd Tanous                             v6dhcpParms.useNTPServers, "UseDomainName",
19991f8c7b5dSJohnathan Mantey                             v6dhcpParms.useUseDomainName))
20001f8c7b5dSJohnathan Mantey                     {
20011f8c7b5dSJohnathan Mantey                         return;
20021f8c7b5dSJohnathan Mantey                     }
2003da131a9aSJennifer Lee                 }
2004da131a9aSJennifer Lee 
2005bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2006bf648f77SEd Tanous                 // for JSON preparation
20074a0cb85cSEd Tanous                 getEthernetIfaceData(
20082c70f800SEd Tanous                     ifaceId,
2009bf648f77SEd Tanous                     [asyncResp, ifaceId, hostname = std::move(hostname),
2010ab6554f1SJoshi-Mansi                      fqdn = std::move(fqdn), macAddress = std::move(macAddress),
2011d1d50814SRavi Teja                      ipv4StaticAddresses = std::move(ipv4StaticAddresses),
20129a6fc6feSRavi Teja                      ipv6DefaultGateway = std::move(ipv6DefaultGateway),
2013e48c0fc5SRavi Teja                      ipv6StaticAddresses = std::move(ipv6StaticAddresses),
20141f8c7b5dSJohnathan Mantey                      staticNameServers = std::move(staticNameServers),
20151f8c7b5dSJohnathan Mantey                      dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
201635fb5311STejas Patil                      mtuSize = mtuSize, v4dhcpParms = std::move(v4dhcpParms),
2017f23b7296SEd Tanous                      v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2018bf648f77SEd Tanous                         const bool& success,
2019bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2020bf648f77SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&
2021bf648f77SEd Tanous                             ipv4Data,
2022bf648f77SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&
2023bf648f77SEd Tanous                             ipv6Data) {
20241abe55efSEd Tanous                         if (!success)
20251abe55efSEd Tanous                         {
2026588c3f0dSKowalski, Kamil                             // ... otherwise return error
2027bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2028bf648f77SEd Tanous                             // existing object, and other errors
2029bf648f77SEd Tanous                             messages::resourceNotFound(
2030bf648f77SEd Tanous                                 asyncResp->res, "Ethernet Interface", ifaceId);
2031588c3f0dSKowalski, Kamil                             return;
2032588c3f0dSKowalski, Kamil                         }
2033588c3f0dSKowalski, Kamil 
20341f8c7b5dSJohnathan Mantey                         if (dhcpv4 || dhcpv6)
20351f8c7b5dSJohnathan Mantey                         {
2036bf648f77SEd Tanous                             handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2037bf648f77SEd Tanous                                             v6dhcpParms, asyncResp);
20381f8c7b5dSJohnathan Mantey                         }
20391f8c7b5dSJohnathan Mantey 
20400627a2c7SEd Tanous                         if (hostname)
20411abe55efSEd Tanous                         {
20420627a2c7SEd Tanous                             handleHostnamePatch(*hostname, asyncResp);
20431abe55efSEd Tanous                         }
20440627a2c7SEd Tanous 
2045ab6554f1SJoshi-Mansi                         if (fqdn)
2046ab6554f1SJoshi-Mansi                         {
20472c70f800SEd Tanous                             handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2048ab6554f1SJoshi-Mansi                         }
2049ab6554f1SJoshi-Mansi 
2050d577665bSRatan Gupta                         if (macAddress)
2051d577665bSRatan Gupta                         {
2052bf648f77SEd Tanous                             handleMACAddressPatch(ifaceId, *macAddress,
2053bf648f77SEd Tanous                                                   asyncResp);
2054d577665bSRatan Gupta                         }
2055d577665bSRatan Gupta 
2056d1d50814SRavi Teja                         if (ipv4StaticAddresses)
2057d1d50814SRavi Teja                         {
2058bf648f77SEd Tanous                             // TODO(ed) for some reason the capture of
2059bf648f77SEd Tanous                             // ipv4Addresses above is returning a const value,
2060bf648f77SEd Tanous                             // not a non-const value. This doesn't really work
2061bf648f77SEd Tanous                             // for us, as we need to be able to efficiently move
2062bf648f77SEd Tanous                             // out the intermedia nlohmann::json objects. This
2063bf648f77SEd Tanous                             // makes a copy of the structure, and operates on
2064bf648f77SEd Tanous                             // that, but could be done more efficiently
2065f23b7296SEd Tanous                             nlohmann::json ipv4Static = *ipv4StaticAddresses;
20662c70f800SEd Tanous                             handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2067d1d50814SRavi Teja                                                   asyncResp);
20681abe55efSEd Tanous                         }
20690627a2c7SEd Tanous 
2070f85837bfSRAJESWARAN THILLAIGOVINDAN                         if (staticNameServers)
2071f85837bfSRAJESWARAN THILLAIGOVINDAN                         {
2072bf648f77SEd Tanous                             handleStaticNameServersPatch(
2073bf648f77SEd Tanous                                 ifaceId, *staticNameServers, asyncResp);
2074f85837bfSRAJESWARAN THILLAIGOVINDAN                         }
20759a6fc6feSRavi Teja 
20769a6fc6feSRavi Teja                         if (ipv6DefaultGateway)
20779a6fc6feSRavi Teja                         {
20789a6fc6feSRavi Teja                             messages::propertyNotWritable(asyncResp->res,
20799a6fc6feSRavi Teja                                                           "IPv6DefaultGateway");
20809a6fc6feSRavi Teja                         }
2081e48c0fc5SRavi Teja 
2082e48c0fc5SRavi Teja                         if (ipv6StaticAddresses)
2083e48c0fc5SRavi Teja                         {
2084f23b7296SEd Tanous                             nlohmann::json ipv6Static = *ipv6StaticAddresses;
20852c70f800SEd Tanous                             handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
208601784826SJohnathan Mantey                                                            ipv6Data, asyncResp);
2087e48c0fc5SRavi Teja                         }
2088eeedda23SJohnathan Mantey 
2089eeedda23SJohnathan Mantey                         if (interfaceEnabled)
2090eeedda23SJohnathan Mantey                         {
2091eeedda23SJohnathan Mantey                             setEthernetInterfaceBoolProperty(
2092bf648f77SEd Tanous                                 ifaceId, "NICEnabled", *interfaceEnabled,
2093bf648f77SEd Tanous                                 asyncResp);
2094eeedda23SJohnathan Mantey                         }
209535fb5311STejas Patil 
209635fb5311STejas Patil                         if (mtuSize)
209735fb5311STejas Patil                         {
209835fb5311STejas Patil                             handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
209935fb5311STejas Patil                         }
2100588c3f0dSKowalski, Kamil                     });
2101bf648f77SEd Tanous             });
21029391bb9cSRapkiewicz, Pawel 
2103bf648f77SEd Tanous     BMCWEB_ROUTE(
2104bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2105ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
2106ed398213SEd Tanous 
2107bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
2108bf648f77SEd Tanous             [](const crow::Request& /* req */,
2109bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2110bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
21118d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
21120f74e643SEd Tanous                     "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
21138d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
2114e439f0f8SKowalski, Kamil 
21152c70f800SEd Tanous                 if (!verifyNames(parentIfaceId, ifaceId))
21161abe55efSEd Tanous                 {
2117a434f2bdSEd Tanous                     return;
2118a434f2bdSEd Tanous                 }
2119a434f2bdSEd Tanous 
2120bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2121bf648f77SEd Tanous                 // for JSON preparation
21224a0cb85cSEd Tanous                 getEthernetIfaceData(
2123bf648f77SEd Tanous                     ifaceId,
2124bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId](
2125bf648f77SEd Tanous                         const bool& success,
2126bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2127cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2128cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
212926f6976fSEd Tanous                         if (success && !ethData.vlan_id.empty())
21301abe55efSEd Tanous                         {
2131bf648f77SEd Tanous                             parseInterfaceData(asyncResp->res.jsonValue,
2132bf648f77SEd Tanous                                                parentIfaceId, ifaceId, ethData);
21331abe55efSEd Tanous                         }
21341abe55efSEd Tanous                         else
21351abe55efSEd Tanous                         {
2136e439f0f8SKowalski, Kamil                             // ... otherwise return error
2137bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2138bf648f77SEd Tanous                             // existing object, and other errors
2139bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2140bf648f77SEd Tanous                                                        "VLAN Network Interface",
2141bf648f77SEd Tanous                                                        ifaceId);
2142e439f0f8SKowalski, Kamil                         }
2143e439f0f8SKowalski, Kamil                     });
2144bf648f77SEd Tanous             });
2145e439f0f8SKowalski, Kamil 
2146bf648f77SEd Tanous     BMCWEB_ROUTE(
2147bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2148ed398213SEd Tanous         // This privilege is incorrect, it should be ConfigureManager
2149ed398213SEd Tanous         //.privileges(redfish::privileges::patchVLanNetworkInterface)
2150432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2151bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
2152bf648f77SEd Tanous             [](const crow::Request& req,
2153bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2154bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
2155fda13ad2SSunitha Harish                 if (!verifyNames(parentIfaceId, ifaceId))
21561abe55efSEd Tanous                 {
2157bf648f77SEd Tanous                     messages::resourceNotFound(
2158bf648f77SEd Tanous                         asyncResp->res, "VLAN Network Interface", ifaceId);
2159927a505aSKowalski, Kamil                     return;
2160927a505aSKowalski, Kamil                 }
2161927a505aSKowalski, Kamil 
21620627a2c7SEd Tanous                 bool vlanEnable = false;
216338268fa8SAndrew Geissler                 uint32_t vlanId = 0;
21640627a2c7SEd Tanous 
2165bf648f77SEd Tanous                 if (!json_util::readJson(req, asyncResp->res, "VLANEnable",
2166bf648f77SEd Tanous                                          vlanEnable, "VLANId", vlanId))
21671abe55efSEd Tanous                 {
2168927a505aSKowalski, Kamil                     return;
2169927a505aSKowalski, Kamil                 }
2170927a505aSKowalski, Kamil 
2171bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2172bf648f77SEd Tanous                 // for JSON preparation
2173e48c0fc5SRavi Teja                 getEthernetIfaceData(
2174bf648f77SEd Tanous                     ifaceId,
2175bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId](
2176bf648f77SEd Tanous                         const bool& success,
2177bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2178cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2179cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
218008244d02SSunitha Harish                         if (success && !ethData.vlan_id.empty())
218108244d02SSunitha Harish                         {
218208244d02SSunitha Harish                             auto callback =
2183bf648f77SEd Tanous                                 [asyncResp](
2184bf648f77SEd Tanous                                     const boost::system::error_code ec) {
218508244d02SSunitha Harish                                     if (ec)
218608244d02SSunitha Harish                                     {
218708244d02SSunitha Harish                                         messages::internalError(asyncResp->res);
218808244d02SSunitha Harish                                     }
218908244d02SSunitha Harish                                 };
219008244d02SSunitha Harish 
2191*e05aec50SEd Tanous                             if (vlanEnable)
219208244d02SSunitha Harish                             {
219308244d02SSunitha Harish                                 crow::connections::systemBus->async_method_call(
2194bf648f77SEd Tanous                                     std::move(callback),
2195bf648f77SEd Tanous                                     "xyz.openbmc_project.Network",
219608244d02SSunitha Harish                                     "/xyz/openbmc_project/network/" + ifaceId,
219708244d02SSunitha Harish                                     "org.freedesktop.DBus.Properties", "Set",
219808244d02SSunitha Harish                                     "xyz.openbmc_project.Network.VLAN", "Id",
2199168e20c1SEd Tanous                                     dbus::utility::DbusVariantType(vlanId));
220008244d02SSunitha Harish                             }
220108244d02SSunitha Harish                             else
220208244d02SSunitha Harish                             {
2203bf648f77SEd Tanous                                 BMCWEB_LOG_DEBUG
2204bf648f77SEd Tanous                                     << "vlanEnable is false. Deleting the "
2205e48c0fc5SRavi Teja                                        "vlan interface";
220608244d02SSunitha Harish                                 crow::connections::systemBus->async_method_call(
2207bf648f77SEd Tanous                                     std::move(callback),
2208bf648f77SEd Tanous                                     "xyz.openbmc_project.Network",
2209bf648f77SEd Tanous                                     std::string(
2210bf648f77SEd Tanous                                         "/xyz/openbmc_project/network/") +
2211e48c0fc5SRavi Teja                                         ifaceId,
2212bf648f77SEd Tanous                                     "xyz.openbmc_project.Object.Delete",
2213bf648f77SEd Tanous                                     "Delete");
221408244d02SSunitha Harish                             }
221508244d02SSunitha Harish                         }
221608244d02SSunitha Harish                         else
22171abe55efSEd Tanous                         {
2218bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2219bf648f77SEd Tanous                             // existing object, and other errors
2220bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2221bf648f77SEd Tanous                                                        "VLAN Network Interface",
2222bf648f77SEd Tanous                                                        ifaceId);
2223bf648f77SEd Tanous                             return;
2224bf648f77SEd Tanous                         }
2225bf648f77SEd Tanous                     });
2226bf648f77SEd Tanous             });
2227bf648f77SEd Tanous 
2228bf648f77SEd Tanous     BMCWEB_ROUTE(
2229bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2230ed398213SEd Tanous         // This privilege is incorrect, it should be ConfigureManager
2231ed398213SEd Tanous         //.privileges(redfish::privileges::deleteVLanNetworkInterface)
2232432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2233bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
2234bf648f77SEd Tanous             [](const crow::Request& /* req */,
2235bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2236bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
2237bf648f77SEd Tanous                 if (!verifyNames(parentIfaceId, ifaceId))
2238bf648f77SEd Tanous                 {
2239e48c0fc5SRavi Teja                     messages::resourceNotFound(
2240e48c0fc5SRavi Teja                         asyncResp->res, "VLAN Network Interface", ifaceId);
2241927a505aSKowalski, Kamil                     return;
2242927a505aSKowalski, Kamil                 }
2243e439f0f8SKowalski, Kamil 
2244bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2245bf648f77SEd Tanous                 // for JSON preparation
2246f12894f8SJason M. Bills                 getEthernetIfaceData(
2247bf648f77SEd Tanous                     ifaceId,
2248bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId](
2249bf648f77SEd Tanous                         const bool& success,
2250bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2251cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2252cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
2253fda13ad2SSunitha Harish                         if (success && !ethData.vlan_id.empty())
22541abe55efSEd Tanous                         {
2255f12894f8SJason M. Bills                             auto callback =
2256bf648f77SEd Tanous                                 [asyncResp](
2257bf648f77SEd Tanous                                     const boost::system::error_code ec) {
22581abe55efSEd Tanous                                     if (ec)
22591abe55efSEd Tanous                                     {
2260f12894f8SJason M. Bills                                         messages::internalError(asyncResp->res);
2261927a505aSKowalski, Kamil                                     }
22624a0cb85cSEd Tanous                                 };
22634a0cb85cSEd Tanous                             crow::connections::systemBus->async_method_call(
2264bf648f77SEd Tanous                                 std::move(callback),
2265bf648f77SEd Tanous                                 "xyz.openbmc_project.Network",
2266bf648f77SEd Tanous                                 std::string("/xyz/openbmc_project/network/") +
2267bf648f77SEd Tanous                                     ifaceId,
22684a0cb85cSEd Tanous                                 "xyz.openbmc_project.Object.Delete", "Delete");
22691abe55efSEd Tanous                         }
22701abe55efSEd Tanous                         else
22711abe55efSEd Tanous                         {
2272927a505aSKowalski, Kamil                             // ... otherwise return error
2273bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2274bf648f77SEd Tanous                             // existing object, and other errors
2275bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2276bf648f77SEd Tanous                                                        "VLAN Network Interface",
2277bf648f77SEd Tanous                                                        ifaceId);
2278927a505aSKowalski, Kamil                         }
2279927a505aSKowalski, Kamil                     });
2280bf648f77SEd Tanous             });
2281e439f0f8SKowalski, Kamil 
2282bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2283bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2284ed398213SEd Tanous 
2285ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
2286bf648f77SEd Tanous         .methods(
2287bf648f77SEd Tanous             boost::beast::http::verb::
2288bf648f77SEd Tanous                 get)([](const crow::Request& /* req */,
2289bf648f77SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2290bf648f77SEd Tanous                         const std::string& rootInterfaceName) {
22914a0cb85cSEd Tanous             // Get eth interface list, and call the below callback for JSON
22921abe55efSEd Tanous             // preparation
2293bf648f77SEd Tanous             getEthernetIfaceList([asyncResp, rootInterfaceName](
22941abe55efSEd Tanous                                      const bool& success,
2295bf648f77SEd Tanous                                      const boost::container::flat_set<
2296bf648f77SEd Tanous                                          std::string>& ifaceList) {
22974a0cb85cSEd Tanous                 if (!success)
22981abe55efSEd Tanous                 {
2299f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
23004a0cb85cSEd Tanous                     return;
23011abe55efSEd Tanous                 }
23024c9afe43SEd Tanous 
230381ce609eSEd Tanous                 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
23044c9afe43SEd Tanous                 {
23054c9afe43SEd Tanous                     messages::resourceNotFound(asyncResp->res,
23064c9afe43SEd Tanous                                                "VLanNetworkInterfaceCollection",
23074c9afe43SEd Tanous                                                rootInterfaceName);
23084c9afe43SEd Tanous                     return;
23094c9afe43SEd Tanous                 }
23104c9afe43SEd Tanous 
23110f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
23120f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
23130f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
23140f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] =
23150f74e643SEd Tanous                     "VLAN Network Interface Collection";
23164a0cb85cSEd Tanous 
23172c70f800SEd Tanous                 nlohmann::json ifaceArray = nlohmann::json::array();
23184a0cb85cSEd Tanous 
231981ce609eSEd Tanous                 for (const std::string& ifaceItem : ifaceList)
23201abe55efSEd Tanous                 {
23212c70f800SEd Tanous                     if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
23224a0cb85cSEd Tanous                     {
2323f23b7296SEd Tanous                         std::string path =
2324f23b7296SEd Tanous                             "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2325f23b7296SEd Tanous                         path += rootInterfaceName;
2326f23b7296SEd Tanous                         path += "/VLANs/";
2327f23b7296SEd Tanous                         path += ifaceItem;
2328f23b7296SEd Tanous                         ifaceArray.push_back({{"@odata.id", std::move(path)}});
2329e439f0f8SKowalski, Kamil                     }
2330e439f0f8SKowalski, Kamil                 }
2331e439f0f8SKowalski, Kamil 
23324a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
23332c70f800SEd Tanous                     ifaceArray.size();
23342c70f800SEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
23354a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
23364a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
23374a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
2338e439f0f8SKowalski, Kamil             });
2339bf648f77SEd Tanous         });
2340e439f0f8SKowalski, Kamil 
2341bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2342bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2343ed398213SEd Tanous         // This privilege is wrong, it should be ConfigureManager
2344ed398213SEd Tanous         //.privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2345432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2346bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
2347bf648f77SEd Tanous             [](const crow::Request& req,
2348bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2349bf648f77SEd Tanous                const std::string& rootInterfaceName) {
2350fda13ad2SSunitha Harish                 bool vlanEnable = false;
23510627a2c7SEd Tanous                 uint32_t vlanId = 0;
23528d1b46d7Szhanghch05                 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId,
23538d1b46d7Szhanghch05                                          "VLANEnable", vlanEnable))
23541abe55efSEd Tanous                 {
23554a0cb85cSEd Tanous                     return;
2356e439f0f8SKowalski, Kamil                 }
2357fda13ad2SSunitha Harish                 // Need both vlanId and vlanEnable to service this request
2358fda13ad2SSunitha Harish                 if (!vlanId)
2359fda13ad2SSunitha Harish                 {
2360fda13ad2SSunitha Harish                     messages::propertyMissing(asyncResp->res, "VLANId");
2361fda13ad2SSunitha Harish                 }
2362fda13ad2SSunitha Harish                 if (!vlanEnable)
2363fda13ad2SSunitha Harish                 {
2364fda13ad2SSunitha Harish                     messages::propertyMissing(asyncResp->res, "VLANEnable");
2365fda13ad2SSunitha Harish                 }
2366271584abSEd Tanous                 if (static_cast<bool>(vlanId) ^ vlanEnable)
2367fda13ad2SSunitha Harish                 {
2368fda13ad2SSunitha Harish                     return;
2369fda13ad2SSunitha Harish                 }
2370fda13ad2SSunitha Harish 
2371bf648f77SEd Tanous                 auto callback =
2372bf648f77SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
23731abe55efSEd Tanous                         if (ec)
23741abe55efSEd Tanous                         {
2375bf648f77SEd Tanous                             // TODO(ed) make more consistent error messages
2376bf648f77SEd Tanous                             // based on phosphor-network responses
2377f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
23784a0cb85cSEd Tanous                             return;
23791abe55efSEd Tanous                         }
2380f12894f8SJason M. Bills                         messages::created(asyncResp->res);
2381e439f0f8SKowalski, Kamil                     };
23824a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
23834a0cb85cSEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
23844a0cb85cSEd Tanous                     "/xyz/openbmc_project/network",
23854a0cb85cSEd Tanous                     "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
23860627a2c7SEd Tanous                     rootInterfaceName, vlanId);
2387bf648f77SEd Tanous             });
23884a0cb85cSEd Tanous }
2389bf648f77SEd Tanous 
23909391bb9cSRapkiewicz, Pawel } // namespace redfish
2391