19391bb9cSRapkiewicz, Pawel /* 29391bb9cSRapkiewicz, Pawel // Copyright (c) 2018 Intel Corporation 39391bb9cSRapkiewicz, Pawel // 49391bb9cSRapkiewicz, Pawel // Licensed under the Apache License, Version 2.0 (the "License"); 59391bb9cSRapkiewicz, Pawel // you may not use this file except in compliance with the License. 69391bb9cSRapkiewicz, Pawel // You may obtain a copy of the License at 79391bb9cSRapkiewicz, Pawel // 89391bb9cSRapkiewicz, Pawel // http://www.apache.org/licenses/LICENSE-2.0 99391bb9cSRapkiewicz, Pawel // 109391bb9cSRapkiewicz, Pawel // Unless required by applicable law or agreed to in writing, software 119391bb9cSRapkiewicz, Pawel // distributed under the License is distributed on an "AS IS" BASIS, 129391bb9cSRapkiewicz, Pawel // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139391bb9cSRapkiewicz, Pawel // See the License for the specific language governing permissions and 149391bb9cSRapkiewicz, Pawel // limitations under the License. 159391bb9cSRapkiewicz, Pawel */ 169391bb9cSRapkiewicz, Pawel #pragma once 179391bb9cSRapkiewicz, Pawel 181abe55efSEd Tanous #include <boost/container/flat_map.hpp> 194a0cb85cSEd Tanous #include <boost/container/flat_set.hpp> 20179db1d7SKowalski, Kamil #include <dbus_singleton.hpp> 21588c3f0dSKowalski, Kamil #include <error_messages.hpp> 22179db1d7SKowalski, Kamil #include <node.hpp> 231214b7e7SGunnar Mills #include <utils/json_utils.hpp> 241214b7e7SGunnar Mills 25a24526dcSEd Tanous #include <optional> 26ab6554f1SJoshi-Mansi #include <regex> 27abf2add6SEd Tanous #include <variant> 289391bb9cSRapkiewicz, Pawel 291abe55efSEd Tanous namespace redfish 301abe55efSEd Tanous { 319391bb9cSRapkiewicz, Pawel 329391bb9cSRapkiewicz, Pawel /** 339391bb9cSRapkiewicz, Pawel * DBus types primitives for several generic DBus interfaces 349391bb9cSRapkiewicz, Pawel * TODO(Pawel) consider move this to separate file into boost::dbus 359391bb9cSRapkiewicz, Pawel */ 36aa2e59c1SEd Tanous using PropertiesMapType = boost::container::flat_map< 37abf2add6SEd Tanous std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t, 38aa2e59c1SEd Tanous int32_t, uint32_t, int64_t, uint64_t, double>>; 399391bb9cSRapkiewicz, Pawel 404a0cb85cSEd Tanous using GetManagedObjects = std::vector<std::pair< 41aa2e59c1SEd Tanous sdbusplus::message::object_path, 424a0cb85cSEd Tanous std::vector<std::pair< 43aa2e59c1SEd Tanous std::string, 44aa2e59c1SEd Tanous boost::container::flat_map< 4519bd78d9SPatrick Williams std::string, 4619bd78d9SPatrick Williams std::variant<std::string, bool, uint8_t, int16_t, uint16_t, int32_t, 4719bd78d9SPatrick Williams uint32_t, int64_t, uint64_t, double, 48029573d4SEd Tanous std::vector<std::string>>>>>>>; 494a0cb85cSEd Tanous 504a0cb85cSEd Tanous enum class LinkType 514a0cb85cSEd Tanous { 524a0cb85cSEd Tanous Local, 534a0cb85cSEd Tanous Global 544a0cb85cSEd Tanous }; 559391bb9cSRapkiewicz, Pawel 569391bb9cSRapkiewicz, Pawel /** 579391bb9cSRapkiewicz, Pawel * Structure for keeping IPv4 data required by Redfish 589391bb9cSRapkiewicz, Pawel */ 591abe55efSEd Tanous struct IPv4AddressData 601abe55efSEd Tanous { 61179db1d7SKowalski, Kamil std::string id; 624a0cb85cSEd Tanous std::string address; 634a0cb85cSEd Tanous std::string domain; 644a0cb85cSEd Tanous std::string gateway; 659391bb9cSRapkiewicz, Pawel std::string netmask; 669391bb9cSRapkiewicz, Pawel std::string origin; 674a0cb85cSEd Tanous LinkType linktype; 6801c6e858SSunitha Harish bool isActive; 694a0cb85cSEd Tanous 701abe55efSEd Tanous bool operator<(const IPv4AddressData& obj) const 711abe55efSEd Tanous { 724a0cb85cSEd Tanous return id < obj.id; 731abe55efSEd Tanous } 749391bb9cSRapkiewicz, Pawel }; 759391bb9cSRapkiewicz, Pawel 769391bb9cSRapkiewicz, Pawel /** 77e48c0fc5SRavi Teja * Structure for keeping IPv6 data required by Redfish 78e48c0fc5SRavi Teja */ 79e48c0fc5SRavi Teja struct IPv6AddressData 80e48c0fc5SRavi Teja { 81e48c0fc5SRavi Teja std::string id; 82e48c0fc5SRavi Teja std::string address; 83e48c0fc5SRavi Teja std::string origin; 84e48c0fc5SRavi Teja uint8_t prefixLength; 85e48c0fc5SRavi Teja 86e48c0fc5SRavi Teja bool operator<(const IPv6AddressData& obj) const 87e48c0fc5SRavi Teja { 88e48c0fc5SRavi Teja return id < obj.id; 89e48c0fc5SRavi Teja } 90e48c0fc5SRavi Teja }; 91e48c0fc5SRavi Teja /** 929391bb9cSRapkiewicz, Pawel * Structure for keeping basic single Ethernet Interface information 939391bb9cSRapkiewicz, Pawel * available from DBus 949391bb9cSRapkiewicz, Pawel */ 951abe55efSEd Tanous struct EthernetInterfaceData 961abe55efSEd Tanous { 974a0cb85cSEd Tanous uint32_t speed; 984a0cb85cSEd Tanous bool auto_neg; 991f8c7b5dSJohnathan Mantey bool DNSEnabled; 1001f8c7b5dSJohnathan Mantey bool NTPEnabled; 1011f8c7b5dSJohnathan Mantey bool HostNameEnabled; 1021f8c7b5dSJohnathan Mantey bool SendHostNameEnabled; 103aa05fb27SJohnathan Mantey bool linkUp; 104eeedda23SJohnathan Mantey bool nicEnabled; 1051f8c7b5dSJohnathan Mantey std::string DHCPEnabled; 1061f8c7b5dSJohnathan Mantey std::string operatingMode; 1074a0cb85cSEd Tanous std::string hostname; 1084a0cb85cSEd Tanous std::string default_gateway; 1099a6fc6feSRavi Teja std::string ipv6_default_gateway; 1104a0cb85cSEd Tanous std::string mac_address; 111fda13ad2SSunitha Harish std::vector<std::uint32_t> vlan_id; 1120f6efdc1Smanojkiran.eda@gmail.com std::vector<std::string> nameServers; 1130f6efdc1Smanojkiran.eda@gmail.com std::vector<std::string> staticNameServers; 114d24bfc7aSJennifer Lee std::vector<std::string> domainnames; 1159391bb9cSRapkiewicz, Pawel }; 1169391bb9cSRapkiewicz, Pawel 1171f8c7b5dSJohnathan Mantey struct DHCPParameters 1181f8c7b5dSJohnathan Mantey { 1191f8c7b5dSJohnathan Mantey std::optional<bool> dhcpv4Enabled; 1201f8c7b5dSJohnathan Mantey std::optional<bool> useDNSServers; 1211f8c7b5dSJohnathan Mantey std::optional<bool> useNTPServers; 1221f8c7b5dSJohnathan Mantey std::optional<bool> useUseDomainName; 1231f8c7b5dSJohnathan Mantey std::optional<std::string> dhcpv6OperatingMode; 1241f8c7b5dSJohnathan Mantey }; 1251f8c7b5dSJohnathan Mantey 1269391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24) 1279391bb9cSRapkiewicz, Pawel // into full dot notation 1281abe55efSEd Tanous inline std::string getNetmask(unsigned int bits) 1291abe55efSEd Tanous { 1309391bb9cSRapkiewicz, Pawel uint32_t value = 0xffffffff << (32 - bits); 1319391bb9cSRapkiewicz, Pawel std::string netmask = std::to_string((value >> 24) & 0xff) + "." + 1329391bb9cSRapkiewicz, Pawel std::to_string((value >> 16) & 0xff) + "." + 1339391bb9cSRapkiewicz, Pawel std::to_string((value >> 8) & 0xff) + "." + 1349391bb9cSRapkiewicz, Pawel std::to_string(value & 0xff); 1359391bb9cSRapkiewicz, Pawel return netmask; 1369391bb9cSRapkiewicz, Pawel } 1379391bb9cSRapkiewicz, Pawel 1381f8c7b5dSJohnathan Mantey inline bool translateDHCPEnabledToBool(const std::string& inputDHCP, 1391f8c7b5dSJohnathan Mantey bool isIPv4) 1401f8c7b5dSJohnathan Mantey { 1411f8c7b5dSJohnathan Mantey if (isIPv4) 1421f8c7b5dSJohnathan Mantey { 1431f8c7b5dSJohnathan Mantey return ( 1441f8c7b5dSJohnathan Mantey (inputDHCP == 1451f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") || 1461f8c7b5dSJohnathan Mantey (inputDHCP == 1471f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); 1481f8c7b5dSJohnathan Mantey } 1491f8c7b5dSJohnathan Mantey return ((inputDHCP == 1501f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") || 1511f8c7b5dSJohnathan Mantey (inputDHCP == 1521f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); 1531f8c7b5dSJohnathan Mantey } 1541f8c7b5dSJohnathan Mantey 155*2c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6) 1561f8c7b5dSJohnathan Mantey { 1571f8c7b5dSJohnathan Mantey if (isIPv4 && isIPv6) 1581f8c7b5dSJohnathan Mantey { 1591f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"; 1601f8c7b5dSJohnathan Mantey } 1611f8c7b5dSJohnathan Mantey else if (isIPv4) 1621f8c7b5dSJohnathan Mantey { 1631f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4"; 1641f8c7b5dSJohnathan Mantey } 1651f8c7b5dSJohnathan Mantey else if (isIPv6) 1661f8c7b5dSJohnathan Mantey { 1671f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6"; 1681f8c7b5dSJohnathan Mantey } 1691f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none"; 1701f8c7b5dSJohnathan Mantey } 1711f8c7b5dSJohnathan Mantey 1724a0cb85cSEd Tanous inline std::string 1734a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(const std::string& inputOrigin, 1744a0cb85cSEd Tanous bool isIPv4) 1751abe55efSEd Tanous { 1764a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static") 1771abe55efSEd Tanous { 1784a0cb85cSEd Tanous return "Static"; 1799391bb9cSRapkiewicz, Pawel } 1804a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal") 1811abe55efSEd Tanous { 1824a0cb85cSEd Tanous if (isIPv4) 1831abe55efSEd Tanous { 1844a0cb85cSEd Tanous return "IPv4LinkLocal"; 1851abe55efSEd Tanous } 1861abe55efSEd Tanous else 1871abe55efSEd Tanous { 1884a0cb85cSEd Tanous return "LinkLocal"; 1899391bb9cSRapkiewicz, Pawel } 1909391bb9cSRapkiewicz, Pawel } 1914a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP") 1921abe55efSEd Tanous { 1934a0cb85cSEd Tanous if (isIPv4) 1944a0cb85cSEd Tanous { 1954a0cb85cSEd Tanous return "DHCP"; 1964a0cb85cSEd Tanous } 1974a0cb85cSEd Tanous else 1984a0cb85cSEd Tanous { 1994a0cb85cSEd Tanous return "DHCPv6"; 2004a0cb85cSEd Tanous } 2014a0cb85cSEd Tanous } 2024a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC") 2034a0cb85cSEd Tanous { 2044a0cb85cSEd Tanous return "SLAAC"; 2054a0cb85cSEd Tanous } 2064a0cb85cSEd Tanous return ""; 2074a0cb85cSEd Tanous } 2084a0cb85cSEd Tanous 2094c9afe43SEd Tanous inline bool extractEthernetInterfaceData(const std::string& ethiface_id, 2104a0cb85cSEd Tanous const GetManagedObjects& dbus_data, 2114a0cb85cSEd Tanous EthernetInterfaceData& ethData) 2124a0cb85cSEd Tanous { 2134c9afe43SEd Tanous bool idFound = false; 2144a0cb85cSEd Tanous for (const auto& objpath : dbus_data) 2154a0cb85cSEd Tanous { 2164a0cb85cSEd Tanous for (const auto& ifacePair : objpath.second) 2174a0cb85cSEd Tanous { 218029573d4SEd Tanous if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id) 219029573d4SEd Tanous { 2204c9afe43SEd Tanous idFound = true; 2214a0cb85cSEd Tanous if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") 2224a0cb85cSEd Tanous { 2234a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2244a0cb85cSEd Tanous { 2254a0cb85cSEd Tanous if (propertyPair.first == "MACAddress") 2264a0cb85cSEd Tanous { 2274a0cb85cSEd Tanous const std::string* mac = 228abf2add6SEd Tanous std::get_if<std::string>(&propertyPair.second); 2294a0cb85cSEd Tanous if (mac != nullptr) 2304a0cb85cSEd Tanous { 2314a0cb85cSEd Tanous ethData.mac_address = *mac; 2324a0cb85cSEd Tanous } 2334a0cb85cSEd Tanous } 2344a0cb85cSEd Tanous } 2354a0cb85cSEd Tanous } 2364a0cb85cSEd Tanous else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN") 2374a0cb85cSEd Tanous { 2384a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2394a0cb85cSEd Tanous { 2404a0cb85cSEd Tanous if (propertyPair.first == "Id") 2414a0cb85cSEd Tanous { 2421b6b96c5SEd Tanous const uint32_t* id = 243abf2add6SEd Tanous std::get_if<uint32_t>(&propertyPair.second); 2444a0cb85cSEd Tanous if (id != nullptr) 2454a0cb85cSEd Tanous { 246fda13ad2SSunitha Harish ethData.vlan_id.push_back(*id); 2474a0cb85cSEd Tanous } 2484a0cb85cSEd Tanous } 2494a0cb85cSEd Tanous } 2504a0cb85cSEd Tanous } 2514a0cb85cSEd Tanous else if (ifacePair.first == 2524a0cb85cSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 2534a0cb85cSEd Tanous { 2544a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2554a0cb85cSEd Tanous { 2564a0cb85cSEd Tanous if (propertyPair.first == "AutoNeg") 2574a0cb85cSEd Tanous { 258*2c70f800SEd Tanous const bool* autoNeg = 259abf2add6SEd Tanous std::get_if<bool>(&propertyPair.second); 260*2c70f800SEd Tanous if (autoNeg != nullptr) 2614a0cb85cSEd Tanous { 262*2c70f800SEd Tanous ethData.auto_neg = *autoNeg; 2634a0cb85cSEd Tanous } 2644a0cb85cSEd Tanous } 2654a0cb85cSEd Tanous else if (propertyPair.first == "Speed") 2664a0cb85cSEd Tanous { 2674a0cb85cSEd Tanous const uint32_t* speed = 268abf2add6SEd Tanous std::get_if<uint32_t>(&propertyPair.second); 2694a0cb85cSEd Tanous if (speed != nullptr) 2704a0cb85cSEd Tanous { 2714a0cb85cSEd Tanous ethData.speed = *speed; 2724a0cb85cSEd Tanous } 2734a0cb85cSEd Tanous } 274aa05fb27SJohnathan Mantey else if (propertyPair.first == "LinkUp") 275aa05fb27SJohnathan Mantey { 276aa05fb27SJohnathan Mantey const bool* linkUp = 277aa05fb27SJohnathan Mantey std::get_if<bool>(&propertyPair.second); 278aa05fb27SJohnathan Mantey if (linkUp != nullptr) 279aa05fb27SJohnathan Mantey { 280aa05fb27SJohnathan Mantey ethData.linkUp = *linkUp; 281aa05fb27SJohnathan Mantey } 282aa05fb27SJohnathan Mantey } 283eeedda23SJohnathan Mantey else if (propertyPair.first == "NICEnabled") 284eeedda23SJohnathan Mantey { 285eeedda23SJohnathan Mantey const bool* nicEnabled = 286eeedda23SJohnathan Mantey std::get_if<bool>(&propertyPair.second); 287eeedda23SJohnathan Mantey if (nicEnabled != nullptr) 288eeedda23SJohnathan Mantey { 289eeedda23SJohnathan Mantey ethData.nicEnabled = *nicEnabled; 290eeedda23SJohnathan Mantey } 291eeedda23SJohnathan Mantey } 292f85837bfSRAJESWARAN THILLAIGOVINDAN else if (propertyPair.first == "Nameservers") 293029573d4SEd Tanous { 294029573d4SEd Tanous const std::vector<std::string>* nameservers = 2958d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 296029573d4SEd Tanous &propertyPair.second); 297029573d4SEd Tanous if (nameservers != nullptr) 298029573d4SEd Tanous { 2990f6efdc1Smanojkiran.eda@gmail.com ethData.nameServers = std::move(*nameservers); 3000f6efdc1Smanojkiran.eda@gmail.com } 3010f6efdc1Smanojkiran.eda@gmail.com } 3020f6efdc1Smanojkiran.eda@gmail.com else if (propertyPair.first == "StaticNameServers") 3030f6efdc1Smanojkiran.eda@gmail.com { 3040f6efdc1Smanojkiran.eda@gmail.com const std::vector<std::string>* staticNameServers = 3058d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 3060f6efdc1Smanojkiran.eda@gmail.com &propertyPair.second); 3070f6efdc1Smanojkiran.eda@gmail.com if (staticNameServers != nullptr) 3080f6efdc1Smanojkiran.eda@gmail.com { 3090f6efdc1Smanojkiran.eda@gmail.com ethData.staticNameServers = 3100f6efdc1Smanojkiran.eda@gmail.com std::move(*staticNameServers); 3114a0cb85cSEd Tanous } 3124a0cb85cSEd Tanous } 3132a133282Smanojkiraneda else if (propertyPair.first == "DHCPEnabled") 3142a133282Smanojkiraneda { 315*2c70f800SEd Tanous const std::string* dhcpEnabled = 3161f8c7b5dSJohnathan Mantey std::get_if<std::string>(&propertyPair.second); 317*2c70f800SEd Tanous if (dhcpEnabled != nullptr) 3182a133282Smanojkiraneda { 319*2c70f800SEd Tanous ethData.DHCPEnabled = *dhcpEnabled; 3202a133282Smanojkiraneda } 3212a133282Smanojkiraneda } 322d24bfc7aSJennifer Lee else if (propertyPair.first == "DomainName") 323d24bfc7aSJennifer Lee { 324d24bfc7aSJennifer Lee const std::vector<std::string>* domainNames = 3258d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 326d24bfc7aSJennifer Lee &propertyPair.second); 327d24bfc7aSJennifer Lee if (domainNames != nullptr) 328d24bfc7aSJennifer Lee { 329d24bfc7aSJennifer Lee ethData.domainnames = std::move(*domainNames); 330d24bfc7aSJennifer Lee } 331d24bfc7aSJennifer Lee } 332029573d4SEd Tanous } 333029573d4SEd Tanous } 334029573d4SEd Tanous } 3351f8c7b5dSJohnathan Mantey 3361f8c7b5dSJohnathan Mantey if (objpath.first == "/xyz/openbmc_project/network/config/dhcp") 3371f8c7b5dSJohnathan Mantey { 3381f8c7b5dSJohnathan Mantey if (ifacePair.first == 3391f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.DHCPConfiguration") 3401f8c7b5dSJohnathan Mantey { 3411f8c7b5dSJohnathan Mantey for (const auto& propertyPair : ifacePair.second) 3421f8c7b5dSJohnathan Mantey { 3431f8c7b5dSJohnathan Mantey if (propertyPair.first == "DNSEnabled") 3441f8c7b5dSJohnathan Mantey { 345*2c70f800SEd Tanous const bool* dnsEnabled = 3461f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 347*2c70f800SEd Tanous if (dnsEnabled != nullptr) 3481f8c7b5dSJohnathan Mantey { 349*2c70f800SEd Tanous ethData.DNSEnabled = *dnsEnabled; 3501f8c7b5dSJohnathan Mantey } 3511f8c7b5dSJohnathan Mantey } 3521f8c7b5dSJohnathan Mantey else if (propertyPair.first == "NTPEnabled") 3531f8c7b5dSJohnathan Mantey { 354*2c70f800SEd Tanous const bool* ntpEnabled = 3551f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 356*2c70f800SEd Tanous if (ntpEnabled != nullptr) 3571f8c7b5dSJohnathan Mantey { 358*2c70f800SEd Tanous ethData.NTPEnabled = *ntpEnabled; 3591f8c7b5dSJohnathan Mantey } 3601f8c7b5dSJohnathan Mantey } 3611f8c7b5dSJohnathan Mantey else if (propertyPair.first == "HostNameEnabled") 3621f8c7b5dSJohnathan Mantey { 363*2c70f800SEd Tanous const bool* hostNameEnabled = 3641f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 365*2c70f800SEd Tanous if (hostNameEnabled != nullptr) 3661f8c7b5dSJohnathan Mantey { 367*2c70f800SEd Tanous ethData.HostNameEnabled = *hostNameEnabled; 3681f8c7b5dSJohnathan Mantey } 3691f8c7b5dSJohnathan Mantey } 3701f8c7b5dSJohnathan Mantey else if (propertyPair.first == "SendHostNameEnabled") 3711f8c7b5dSJohnathan Mantey { 372*2c70f800SEd Tanous const bool* sendHostNameEnabled = 3731f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 374*2c70f800SEd Tanous if (sendHostNameEnabled != nullptr) 3751f8c7b5dSJohnathan Mantey { 3761f8c7b5dSJohnathan Mantey ethData.SendHostNameEnabled = 377*2c70f800SEd Tanous *sendHostNameEnabled; 3781f8c7b5dSJohnathan Mantey } 3791f8c7b5dSJohnathan Mantey } 3801f8c7b5dSJohnathan Mantey } 3811f8c7b5dSJohnathan Mantey } 3821f8c7b5dSJohnathan Mantey } 383029573d4SEd Tanous // System configuration shows up in the global namespace, so no need 384029573d4SEd Tanous // to check eth number 385029573d4SEd Tanous if (ifacePair.first == 3864a0cb85cSEd Tanous "xyz.openbmc_project.Network.SystemConfiguration") 3874a0cb85cSEd Tanous { 3884a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 3894a0cb85cSEd Tanous { 3904a0cb85cSEd Tanous if (propertyPair.first == "HostName") 3914a0cb85cSEd Tanous { 3924a0cb85cSEd Tanous const std::string* hostname = 3938d78b7a9SPatrick Williams std::get_if<std::string>(&propertyPair.second); 3944a0cb85cSEd Tanous if (hostname != nullptr) 3954a0cb85cSEd Tanous { 3964a0cb85cSEd Tanous ethData.hostname = *hostname; 3974a0cb85cSEd Tanous } 3984a0cb85cSEd Tanous } 3994a0cb85cSEd Tanous else if (propertyPair.first == "DefaultGateway") 4004a0cb85cSEd Tanous { 4014a0cb85cSEd Tanous const std::string* defaultGateway = 4028d78b7a9SPatrick Williams std::get_if<std::string>(&propertyPair.second); 4034a0cb85cSEd Tanous if (defaultGateway != nullptr) 4044a0cb85cSEd Tanous { 4054a0cb85cSEd Tanous ethData.default_gateway = *defaultGateway; 4064a0cb85cSEd Tanous } 4074a0cb85cSEd Tanous } 4089a6fc6feSRavi Teja else if (propertyPair.first == "DefaultGateway6") 4099a6fc6feSRavi Teja { 4109a6fc6feSRavi Teja const std::string* defaultGateway6 = 4118d78b7a9SPatrick Williams std::get_if<std::string>(&propertyPair.second); 4129a6fc6feSRavi Teja if (defaultGateway6 != nullptr) 4139a6fc6feSRavi Teja { 4149a6fc6feSRavi Teja ethData.ipv6_default_gateway = *defaultGateway6; 4159a6fc6feSRavi Teja } 4169a6fc6feSRavi Teja } 4174a0cb85cSEd Tanous } 4184a0cb85cSEd Tanous } 4194a0cb85cSEd Tanous } 4204a0cb85cSEd Tanous } 4214c9afe43SEd Tanous return idFound; 4224a0cb85cSEd Tanous } 4234a0cb85cSEd Tanous 424e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address 42501784826SJohnathan Mantey inline void 42601784826SJohnathan Mantey extractIPV6Data(const std::string& ethiface_id, 42701784826SJohnathan Mantey const GetManagedObjects& dbus_data, 42801784826SJohnathan Mantey boost::container::flat_set<IPv6AddressData>& ipv6_config) 429e48c0fc5SRavi Teja { 430e48c0fc5SRavi Teja const std::string ipv6PathStart = 431e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ethiface_id + "/ipv6/"; 432e48c0fc5SRavi Teja 433e48c0fc5SRavi Teja // Since there might be several IPv6 configurations aligned with 434e48c0fc5SRavi Teja // single ethernet interface, loop over all of them 435e48c0fc5SRavi Teja for (const auto& objpath : dbus_data) 436e48c0fc5SRavi Teja { 437e48c0fc5SRavi Teja // Check if proper pattern for object path appears 438e48c0fc5SRavi Teja if (boost::starts_with(objpath.first.str, ipv6PathStart)) 439e48c0fc5SRavi Teja { 440e48c0fc5SRavi Teja for (auto& interface : objpath.second) 441e48c0fc5SRavi Teja { 442e48c0fc5SRavi Teja if (interface.first == "xyz.openbmc_project.Network.IP") 443e48c0fc5SRavi Teja { 444e48c0fc5SRavi Teja // Instance IPv6AddressData structure, and set as 445e48c0fc5SRavi Teja // appropriate 446e48c0fc5SRavi Teja std::pair< 447e48c0fc5SRavi Teja boost::container::flat_set<IPv6AddressData>::iterator, 448e48c0fc5SRavi Teja bool> 449271584abSEd Tanous it = ipv6_config.insert(IPv6AddressData{}); 450*2c70f800SEd Tanous IPv6AddressData& ipv6Address = *it.first; 451*2c70f800SEd Tanous ipv6Address.id = 452271584abSEd Tanous objpath.first.str.substr(ipv6PathStart.size()); 453e48c0fc5SRavi Teja for (auto& property : interface.second) 454e48c0fc5SRavi Teja { 455e48c0fc5SRavi Teja if (property.first == "Address") 456e48c0fc5SRavi Teja { 457e48c0fc5SRavi Teja const std::string* address = 458e48c0fc5SRavi Teja std::get_if<std::string>(&property.second); 459e48c0fc5SRavi Teja if (address != nullptr) 460e48c0fc5SRavi Teja { 461*2c70f800SEd Tanous ipv6Address.address = *address; 462e48c0fc5SRavi Teja } 463e48c0fc5SRavi Teja } 464e48c0fc5SRavi Teja else if (property.first == "Origin") 465e48c0fc5SRavi Teja { 466e48c0fc5SRavi Teja const std::string* origin = 467e48c0fc5SRavi Teja std::get_if<std::string>(&property.second); 468e48c0fc5SRavi Teja if (origin != nullptr) 469e48c0fc5SRavi Teja { 470*2c70f800SEd Tanous ipv6Address.origin = 471e48c0fc5SRavi Teja translateAddressOriginDbusToRedfish(*origin, 472e48c0fc5SRavi Teja false); 473e48c0fc5SRavi Teja } 474e48c0fc5SRavi Teja } 475e48c0fc5SRavi Teja else if (property.first == "PrefixLength") 476e48c0fc5SRavi Teja { 477e48c0fc5SRavi Teja const uint8_t* prefix = 478e48c0fc5SRavi Teja std::get_if<uint8_t>(&property.second); 479e48c0fc5SRavi Teja if (prefix != nullptr) 480e48c0fc5SRavi Teja { 481*2c70f800SEd Tanous ipv6Address.prefixLength = *prefix; 482e48c0fc5SRavi Teja } 483e48c0fc5SRavi Teja } 484e48c0fc5SRavi Teja else 485e48c0fc5SRavi Teja { 486e48c0fc5SRavi Teja BMCWEB_LOG_ERROR 487e48c0fc5SRavi Teja << "Got extra property: " << property.first 488e48c0fc5SRavi Teja << " on the " << objpath.first.str << " object"; 489e48c0fc5SRavi Teja } 490e48c0fc5SRavi Teja } 491e48c0fc5SRavi Teja } 492e48c0fc5SRavi Teja } 493e48c0fc5SRavi Teja } 494e48c0fc5SRavi Teja } 495e48c0fc5SRavi Teja } 496e48c0fc5SRavi Teja 4974a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address 49801784826SJohnathan Mantey inline void 49901784826SJohnathan Mantey extractIPData(const std::string& ethiface_id, 50001784826SJohnathan Mantey const GetManagedObjects& dbus_data, 50101784826SJohnathan Mantey boost::container::flat_set<IPv4AddressData>& ipv4_config) 5024a0cb85cSEd Tanous { 5034a0cb85cSEd Tanous const std::string ipv4PathStart = 5044a0cb85cSEd Tanous "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/"; 5054a0cb85cSEd Tanous 5064a0cb85cSEd Tanous // Since there might be several IPv4 configurations aligned with 5074a0cb85cSEd Tanous // single ethernet interface, loop over all of them 5084a0cb85cSEd Tanous for (const auto& objpath : dbus_data) 5094a0cb85cSEd Tanous { 5104a0cb85cSEd Tanous // Check if proper pattern for object path appears 5114a0cb85cSEd Tanous if (boost::starts_with(objpath.first.str, ipv4PathStart)) 5124a0cb85cSEd Tanous { 5134a0cb85cSEd Tanous for (auto& interface : objpath.second) 5144a0cb85cSEd Tanous { 5154a0cb85cSEd Tanous if (interface.first == "xyz.openbmc_project.Network.IP") 5164a0cb85cSEd Tanous { 5174a0cb85cSEd Tanous // Instance IPv4AddressData structure, and set as 5184a0cb85cSEd Tanous // appropriate 5194a0cb85cSEd Tanous std::pair< 5204a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData>::iterator, 5214a0cb85cSEd Tanous bool> 522271584abSEd Tanous it = ipv4_config.insert(IPv4AddressData{}); 523*2c70f800SEd Tanous IPv4AddressData& ipv4Address = *it.first; 524*2c70f800SEd Tanous ipv4Address.id = 525271584abSEd Tanous objpath.first.str.substr(ipv4PathStart.size()); 5264a0cb85cSEd Tanous for (auto& property : interface.second) 5274a0cb85cSEd Tanous { 5284a0cb85cSEd Tanous if (property.first == "Address") 5294a0cb85cSEd Tanous { 5304a0cb85cSEd Tanous const std::string* address = 531abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5324a0cb85cSEd Tanous if (address != nullptr) 5334a0cb85cSEd Tanous { 534*2c70f800SEd Tanous ipv4Address.address = *address; 5354a0cb85cSEd Tanous } 5364a0cb85cSEd Tanous } 5374a0cb85cSEd Tanous else if (property.first == "Gateway") 5384a0cb85cSEd Tanous { 5394a0cb85cSEd Tanous const std::string* gateway = 540abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5414a0cb85cSEd Tanous if (gateway != nullptr) 5424a0cb85cSEd Tanous { 543*2c70f800SEd Tanous ipv4Address.gateway = *gateway; 5444a0cb85cSEd Tanous } 5454a0cb85cSEd Tanous } 5464a0cb85cSEd Tanous else if (property.first == "Origin") 5474a0cb85cSEd Tanous { 5484a0cb85cSEd Tanous const std::string* origin = 549abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5504a0cb85cSEd Tanous if (origin != nullptr) 5514a0cb85cSEd Tanous { 552*2c70f800SEd Tanous ipv4Address.origin = 5534a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(*origin, 5544a0cb85cSEd Tanous true); 5554a0cb85cSEd Tanous } 5564a0cb85cSEd Tanous } 5574a0cb85cSEd Tanous else if (property.first == "PrefixLength") 5584a0cb85cSEd Tanous { 5594a0cb85cSEd Tanous const uint8_t* mask = 560abf2add6SEd Tanous std::get_if<uint8_t>(&property.second); 5614a0cb85cSEd Tanous if (mask != nullptr) 5624a0cb85cSEd Tanous { 5634a0cb85cSEd Tanous // convert it to the string 564*2c70f800SEd Tanous ipv4Address.netmask = getNetmask(*mask); 5654a0cb85cSEd Tanous } 5664a0cb85cSEd Tanous } 5674a0cb85cSEd Tanous else 5684a0cb85cSEd Tanous { 5694a0cb85cSEd Tanous BMCWEB_LOG_ERROR 5704a0cb85cSEd Tanous << "Got extra property: " << property.first 5714a0cb85cSEd Tanous << " on the " << objpath.first.str << " object"; 5724a0cb85cSEd Tanous } 5734a0cb85cSEd Tanous } 5744a0cb85cSEd Tanous // Check if given address is local, or global 575*2c70f800SEd Tanous ipv4Address.linktype = 576*2c70f800SEd Tanous boost::starts_with(ipv4Address.address, "169.254.") 57718659d10SJohnathan Mantey ? LinkType::Local 57818659d10SJohnathan Mantey : LinkType::Global; 5794a0cb85cSEd Tanous } 5804a0cb85cSEd Tanous } 5814a0cb85cSEd Tanous } 5824a0cb85cSEd Tanous } 5834a0cb85cSEd Tanous } 584588c3f0dSKowalski, Kamil 585588c3f0dSKowalski, Kamil /** 586588c3f0dSKowalski, Kamil * @brief Sets given Id on the given VLAN interface through D-Bus 587588c3f0dSKowalski, Kamil * 588588c3f0dSKowalski, Kamil * @param[in] ifaceId Id of VLAN interface that should be modified 589588c3f0dSKowalski, Kamil * @param[in] inputVlanId New ID of the VLAN 590588c3f0dSKowalski, Kamil * @param[in] callback Function that will be called after the operation 591588c3f0dSKowalski, Kamil * 592588c3f0dSKowalski, Kamil * @return None. 593588c3f0dSKowalski, Kamil */ 594588c3f0dSKowalski, Kamil template <typename CallbackFunc> 5954a0cb85cSEd Tanous void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId, 5961abe55efSEd Tanous CallbackFunc&& callback) 5971abe55efSEd Tanous { 59855c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 599588c3f0dSKowalski, Kamil callback, "xyz.openbmc_project.Network", 600588c3f0dSKowalski, Kamil std::string("/xyz/openbmc_project/network/") + ifaceId, 601588c3f0dSKowalski, Kamil "org.freedesktop.DBus.Properties", "Set", 602588c3f0dSKowalski, Kamil "xyz.openbmc_project.Network.VLAN", "Id", 603abf2add6SEd Tanous std::variant<uint32_t>(inputVlanId)); 6044a0cb85cSEd Tanous } 605588c3f0dSKowalski, Kamil 606588c3f0dSKowalski, Kamil /** 607179db1d7SKowalski, Kamil * @brief Helper function that verifies IP address to check if it is in 608179db1d7SKowalski, Kamil * proper format. If bits pointer is provided, also calculates active 609179db1d7SKowalski, Kamil * bit count for Subnet Mask. 610179db1d7SKowalski, Kamil * 611179db1d7SKowalski, Kamil * @param[in] ip IP that will be verified 612179db1d7SKowalski, Kamil * @param[out] bits Calculated mask in bits notation 613179db1d7SKowalski, Kamil * 614179db1d7SKowalski, Kamil * @return true in case of success, false otherwise 615179db1d7SKowalski, Kamil */ 6164a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip, 6171abe55efSEd Tanous uint8_t* bits = nullptr) 6181abe55efSEd Tanous { 619179db1d7SKowalski, Kamil std::vector<std::string> bytesInMask; 620179db1d7SKowalski, Kamil 621179db1d7SKowalski, Kamil boost::split(bytesInMask, ip, boost::is_any_of(".")); 622179db1d7SKowalski, Kamil 6234a0cb85cSEd Tanous static const constexpr int ipV4AddressSectionsCount = 4; 6241abe55efSEd Tanous if (bytesInMask.size() != ipV4AddressSectionsCount) 6251abe55efSEd Tanous { 626179db1d7SKowalski, Kamil return false; 627179db1d7SKowalski, Kamil } 628179db1d7SKowalski, Kamil 6291abe55efSEd Tanous if (bits != nullptr) 6301abe55efSEd Tanous { 631179db1d7SKowalski, Kamil *bits = 0; 632179db1d7SKowalski, Kamil } 633179db1d7SKowalski, Kamil 634179db1d7SKowalski, Kamil char* endPtr; 635179db1d7SKowalski, Kamil long previousValue = 255; 636179db1d7SKowalski, Kamil bool firstZeroInByteHit; 6371abe55efSEd Tanous for (const std::string& byte : bytesInMask) 6381abe55efSEd Tanous { 6391abe55efSEd Tanous if (byte.empty()) 6401abe55efSEd Tanous { 6411db9ca37SKowalski, Kamil return false; 6421db9ca37SKowalski, Kamil } 6431db9ca37SKowalski, Kamil 644179db1d7SKowalski, Kamil // Use strtol instead of stroi to avoid exceptions 6451db9ca37SKowalski, Kamil long value = std::strtol(byte.c_str(), &endPtr, 10); 646179db1d7SKowalski, Kamil 6474a0cb85cSEd Tanous // endPtr should point to the end of the string, otherwise given string 6484a0cb85cSEd Tanous // is not 100% number 6491abe55efSEd Tanous if (*endPtr != '\0') 6501abe55efSEd Tanous { 651179db1d7SKowalski, Kamil return false; 652179db1d7SKowalski, Kamil } 653179db1d7SKowalski, Kamil 654179db1d7SKowalski, Kamil // Value should be contained in byte 6551abe55efSEd Tanous if (value < 0 || value > 255) 6561abe55efSEd Tanous { 657179db1d7SKowalski, Kamil return false; 658179db1d7SKowalski, Kamil } 659179db1d7SKowalski, Kamil 6601abe55efSEd Tanous if (bits != nullptr) 6611abe55efSEd Tanous { 662179db1d7SKowalski, Kamil // Mask has to be continuous between bytes 6631abe55efSEd Tanous if (previousValue != 255 && value != 0) 6641abe55efSEd Tanous { 665179db1d7SKowalski, Kamil return false; 666179db1d7SKowalski, Kamil } 667179db1d7SKowalski, Kamil 668179db1d7SKowalski, Kamil // Mask has to be continuous inside bytes 669179db1d7SKowalski, Kamil firstZeroInByteHit = false; 670179db1d7SKowalski, Kamil 671179db1d7SKowalski, Kamil // Count bits 67223a21a1cSEd Tanous for (long bitIdx = 7; bitIdx >= 0; bitIdx--) 6731abe55efSEd Tanous { 67423a21a1cSEd Tanous if (value & (1L << bitIdx)) 6751abe55efSEd Tanous { 6761abe55efSEd Tanous if (firstZeroInByteHit) 6771abe55efSEd Tanous { 678179db1d7SKowalski, Kamil // Continuity not preserved 679179db1d7SKowalski, Kamil return false; 6801abe55efSEd Tanous } 6811abe55efSEd Tanous else 6821abe55efSEd Tanous { 683179db1d7SKowalski, Kamil (*bits)++; 684179db1d7SKowalski, Kamil } 6851abe55efSEd Tanous } 6861abe55efSEd Tanous else 6871abe55efSEd Tanous { 688179db1d7SKowalski, Kamil firstZeroInByteHit = true; 689179db1d7SKowalski, Kamil } 690179db1d7SKowalski, Kamil } 691179db1d7SKowalski, Kamil } 692179db1d7SKowalski, Kamil 693179db1d7SKowalski, Kamil previousValue = value; 694179db1d7SKowalski, Kamil } 695179db1d7SKowalski, Kamil 696179db1d7SKowalski, Kamil return true; 697179db1d7SKowalski, Kamil } 698179db1d7SKowalski, Kamil 699179db1d7SKowalski, Kamil /** 70001784826SJohnathan Mantey * @brief Deletes given IPv4 interface 701179db1d7SKowalski, Kamil * 702179db1d7SKowalski, Kamil * @param[in] ifaceId Id of interface whose IP should be deleted 703179db1d7SKowalski, Kamil * @param[in] ipHash DBus Hash id of IP that should be deleted 704179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 705179db1d7SKowalski, Kamil * 706179db1d7SKowalski, Kamil * @return None 707179db1d7SKowalski, Kamil */ 7084a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash, 7094a0cb85cSEd Tanous const std::shared_ptr<AsyncResp> asyncResp) 7101abe55efSEd Tanous { 71155c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 712286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 7131abe55efSEd Tanous if (ec) 7141abe55efSEd Tanous { 715a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 7161abe55efSEd Tanous } 717179db1d7SKowalski, Kamil }, 718179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", 719179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 720179db1d7SKowalski, Kamil "xyz.openbmc_project.Object.Delete", "Delete"); 721179db1d7SKowalski, Kamil } 722179db1d7SKowalski, Kamil 723179db1d7SKowalski, Kamil /** 72401784826SJohnathan Mantey * @brief Creates a static IPv4 entry 725179db1d7SKowalski, Kamil * 72601784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 72701784826SJohnathan Mantey * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 72801784826SJohnathan Mantey * @param[in] gateway IPv4 address of this interfaces gateway 72901784826SJohnathan Mantey * @param[in] address IPv4 address to assign to this interface 730179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 731179db1d7SKowalski, Kamil * 732179db1d7SKowalski, Kamil * @return None 733179db1d7SKowalski, Kamil */ 734cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength, 735cb13a392SEd Tanous const std::string& gateway, const std::string& address, 7364a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp) 7371abe55efSEd Tanous { 73801784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 73901784826SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 7401abe55efSEd Tanous if (ec) 7411abe55efSEd Tanous { 742a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 743179db1d7SKowalski, Kamil } 74401784826SJohnathan Mantey }, 74501784826SJohnathan Mantey "xyz.openbmc_project.Network", 746179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId, 747179db1d7SKowalski, Kamil "xyz.openbmc_project.Network.IP.Create", "IP", 74801784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength, 749179db1d7SKowalski, Kamil gateway); 750179db1d7SKowalski, Kamil } 751e48c0fc5SRavi Teja 752e48c0fc5SRavi Teja /** 75301784826SJohnathan Mantey * @brief Deletes the IPv4 entry for this interface and creates a replacement 75401784826SJohnathan Mantey * static IPv4 entry 75501784826SJohnathan Mantey * 75601784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 75701784826SJohnathan Mantey * @param[in] id The unique hash entry identifying the DBus entry 75801784826SJohnathan Mantey * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 75901784826SJohnathan Mantey * @param[in] gateway IPv4 address of this interfaces gateway 76001784826SJohnathan Mantey * @param[in] address IPv4 address to assign to this interface 76101784826SJohnathan Mantey * @param[io] asyncResp Response object that will be returned to client 76201784826SJohnathan Mantey * 76301784826SJohnathan Mantey * @return None 76401784826SJohnathan Mantey */ 76501784826SJohnathan Mantey inline void deleteAndCreateIPv4(const std::string& ifaceId, 76601784826SJohnathan Mantey const std::string& id, uint8_t prefixLength, 76701784826SJohnathan Mantey const std::string& gateway, 76801784826SJohnathan Mantey const std::string& address, 76901784826SJohnathan Mantey std::shared_ptr<AsyncResp> asyncResp) 77001784826SJohnathan Mantey { 77101784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 77201784826SJohnathan Mantey [asyncResp, ifaceId, address, prefixLength, 77301784826SJohnathan Mantey gateway](const boost::system::error_code ec) { 77401784826SJohnathan Mantey if (ec) 77501784826SJohnathan Mantey { 77601784826SJohnathan Mantey messages::internalError(asyncResp->res); 77701784826SJohnathan Mantey } 77801784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 77923a21a1cSEd Tanous [asyncResp](const boost::system::error_code ec2) { 78023a21a1cSEd Tanous if (ec2) 78101784826SJohnathan Mantey { 78201784826SJohnathan Mantey messages::internalError(asyncResp->res); 78301784826SJohnathan Mantey } 78401784826SJohnathan Mantey }, 78501784826SJohnathan Mantey "xyz.openbmc_project.Network", 78601784826SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 78701784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Create", "IP", 78801784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, 78901784826SJohnathan Mantey prefixLength, gateway); 79001784826SJohnathan Mantey }, 79101784826SJohnathan Mantey "xyz.openbmc_project.Network", 79201784826SJohnathan Mantey +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id, 79301784826SJohnathan Mantey "xyz.openbmc_project.Object.Delete", "Delete"); 79401784826SJohnathan Mantey } 79501784826SJohnathan Mantey 79601784826SJohnathan Mantey /** 797e48c0fc5SRavi Teja * @brief Deletes given IPv6 798e48c0fc5SRavi Teja * 799e48c0fc5SRavi Teja * @param[in] ifaceId Id of interface whose IP should be deleted 800e48c0fc5SRavi Teja * @param[in] ipHash DBus Hash id of IP that should be deleted 801e48c0fc5SRavi Teja * @param[io] asyncResp Response object that will be returned to client 802e48c0fc5SRavi Teja * 803e48c0fc5SRavi Teja * @return None 804e48c0fc5SRavi Teja */ 805e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash, 806e48c0fc5SRavi Teja const std::shared_ptr<AsyncResp> asyncResp) 807e48c0fc5SRavi Teja { 808e48c0fc5SRavi Teja crow::connections::systemBus->async_method_call( 809286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 810e48c0fc5SRavi Teja if (ec) 811e48c0fc5SRavi Teja { 812e48c0fc5SRavi Teja messages::internalError(asyncResp->res); 813e48c0fc5SRavi Teja } 814e48c0fc5SRavi Teja }, 815e48c0fc5SRavi Teja "xyz.openbmc_project.Network", 816e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash, 817e48c0fc5SRavi Teja "xyz.openbmc_project.Object.Delete", "Delete"); 818e48c0fc5SRavi Teja } 819e48c0fc5SRavi Teja 820e48c0fc5SRavi Teja /** 82101784826SJohnathan Mantey * @brief Deletes the IPv6 entry for this interface and creates a replacement 82201784826SJohnathan Mantey * static IPv6 entry 82301784826SJohnathan Mantey * 82401784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv6 entry 82501784826SJohnathan Mantey * @param[in] id The unique hash entry identifying the DBus entry 82601784826SJohnathan Mantey * @param[in] prefixLength IPv6 prefix syntax for the subnet mask 82701784826SJohnathan Mantey * @param[in] address IPv6 address to assign to this interface 82801784826SJohnathan Mantey * @param[io] asyncResp Response object that will be returned to client 82901784826SJohnathan Mantey * 83001784826SJohnathan Mantey * @return None 83101784826SJohnathan Mantey */ 83201784826SJohnathan Mantey inline void deleteAndCreateIPv6(const std::string& ifaceId, 83301784826SJohnathan Mantey const std::string& id, uint8_t prefixLength, 83401784826SJohnathan Mantey const std::string& address, 83501784826SJohnathan Mantey std::shared_ptr<AsyncResp> asyncResp) 83601784826SJohnathan Mantey { 83701784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 83801784826SJohnathan Mantey [asyncResp, ifaceId, address, 83901784826SJohnathan Mantey prefixLength](const boost::system::error_code ec) { 84001784826SJohnathan Mantey if (ec) 84101784826SJohnathan Mantey { 84201784826SJohnathan Mantey messages::internalError(asyncResp->res); 84301784826SJohnathan Mantey } 84401784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 84523a21a1cSEd Tanous [asyncResp](const boost::system::error_code ec2) { 84623a21a1cSEd Tanous if (ec2) 84701784826SJohnathan Mantey { 84801784826SJohnathan Mantey messages::internalError(asyncResp->res); 84901784826SJohnathan Mantey } 85001784826SJohnathan Mantey }, 85101784826SJohnathan Mantey "xyz.openbmc_project.Network", 85201784826SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 85301784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Create", "IP", 85401784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, 85501784826SJohnathan Mantey prefixLength, ""); 85601784826SJohnathan Mantey }, 85701784826SJohnathan Mantey "xyz.openbmc_project.Network", 85801784826SJohnathan Mantey +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id, 85901784826SJohnathan Mantey "xyz.openbmc_project.Object.Delete", "Delete"); 86001784826SJohnathan Mantey } 86101784826SJohnathan Mantey 86201784826SJohnathan Mantey /** 863e48c0fc5SRavi Teja * @brief Creates IPv6 with given data 864e48c0fc5SRavi Teja * 865e48c0fc5SRavi Teja * @param[in] ifaceId Id of interface whose IP should be added 866e48c0fc5SRavi Teja * @param[in] prefixLength Prefix length that needs to be added 867e48c0fc5SRavi Teja * @param[in] address IP address that needs to be added 868e48c0fc5SRavi Teja * @param[io] asyncResp Response object that will be returned to client 869e48c0fc5SRavi Teja * 870e48c0fc5SRavi Teja * @return None 871e48c0fc5SRavi Teja */ 87201784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength, 87301784826SJohnathan Mantey const std::string& address, 874e48c0fc5SRavi Teja std::shared_ptr<AsyncResp> asyncResp) 875e48c0fc5SRavi Teja { 876e48c0fc5SRavi Teja auto createIpHandler = [asyncResp](const boost::system::error_code ec) { 877e48c0fc5SRavi Teja if (ec) 878e48c0fc5SRavi Teja { 879e48c0fc5SRavi Teja messages::internalError(asyncResp->res); 880e48c0fc5SRavi Teja } 881e48c0fc5SRavi Teja }; 882e48c0fc5SRavi Teja // Passing null for gateway, as per redfish spec IPv6StaticAddresses object 8834e0453b1SGunnar Mills // does not have associated gateway property 884e48c0fc5SRavi Teja crow::connections::systemBus->async_method_call( 885e48c0fc5SRavi Teja std::move(createIpHandler), "xyz.openbmc_project.Network", 886e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ifaceId, 887e48c0fc5SRavi Teja "xyz.openbmc_project.Network.IP.Create", "IP", 888e48c0fc5SRavi Teja "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength, 889e48c0fc5SRavi Teja ""); 890e48c0fc5SRavi Teja } 891e48c0fc5SRavi Teja 892179db1d7SKowalski, Kamil /** 893179db1d7SKowalski, Kamil * Function that retrieves all properties for given Ethernet Interface 894179db1d7SKowalski, Kamil * Object 895179db1d7SKowalski, Kamil * from EntityManager Network Manager 8964a0cb85cSEd Tanous * @param ethiface_id a eth interface id to query on DBus 897179db1d7SKowalski, Kamil * @param callback a function that shall be called to convert Dbus output 898179db1d7SKowalski, Kamil * into JSON 899179db1d7SKowalski, Kamil */ 900179db1d7SKowalski, Kamil template <typename CallbackFunc> 9014a0cb85cSEd Tanous void getEthernetIfaceData(const std::string& ethiface_id, 9021abe55efSEd Tanous CallbackFunc&& callback) 9031abe55efSEd Tanous { 90455c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 905*2c70f800SEd Tanous [ethifaceId{std::string{ethiface_id}}, callback{std::move(callback)}]( 9061abe55efSEd Tanous const boost::system::error_code error_code, 9074a0cb85cSEd Tanous const GetManagedObjects& resp) { 90855c7b7a2SEd Tanous EthernetInterfaceData ethData{}; 9094a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData> ipv4Data; 910e48c0fc5SRavi Teja boost::container::flat_set<IPv6AddressData> ipv6Data; 911179db1d7SKowalski, Kamil 9121abe55efSEd Tanous if (error_code) 9131abe55efSEd Tanous { 91401784826SJohnathan Mantey callback(false, ethData, ipv4Data, ipv6Data); 915179db1d7SKowalski, Kamil return; 916179db1d7SKowalski, Kamil } 917179db1d7SKowalski, Kamil 9184c9afe43SEd Tanous bool found = 919*2c70f800SEd Tanous extractEthernetInterfaceData(ethifaceId, resp, ethData); 9204c9afe43SEd Tanous if (!found) 9214c9afe43SEd Tanous { 92201784826SJohnathan Mantey callback(false, ethData, ipv4Data, ipv6Data); 9234c9afe43SEd Tanous return; 9244c9afe43SEd Tanous } 9254c9afe43SEd Tanous 926*2c70f800SEd Tanous extractIPData(ethifaceId, resp, ipv4Data); 927179db1d7SKowalski, Kamil // Fix global GW 9281abe55efSEd Tanous for (IPv4AddressData& ipv4 : ipv4Data) 9291abe55efSEd Tanous { 930c619141bSRavi Teja if (((ipv4.linktype == LinkType::Global) && 931c619141bSRavi Teja (ipv4.gateway == "0.0.0.0")) || 932c619141bSRavi Teja (ipv4.origin == "DHCP")) 9331abe55efSEd Tanous { 9344a0cb85cSEd Tanous ipv4.gateway = ethData.default_gateway; 935179db1d7SKowalski, Kamil } 936179db1d7SKowalski, Kamil } 937179db1d7SKowalski, Kamil 938*2c70f800SEd Tanous extractIPV6Data(ethifaceId, resp, ipv6Data); 9394e0453b1SGunnar Mills // Finally make a callback with useful data 94001784826SJohnathan Mantey callback(true, ethData, ipv4Data, ipv6Data); 941179db1d7SKowalski, Kamil }, 942179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 943179db1d7SKowalski, Kamil "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 944271584abSEd Tanous } 945179db1d7SKowalski, Kamil 946179db1d7SKowalski, Kamil /** 9479391bb9cSRapkiewicz, Pawel * Function that retrieves all Ethernet Interfaces available through Network 9489391bb9cSRapkiewicz, Pawel * Manager 9491abe55efSEd Tanous * @param callback a function that shall be called to convert Dbus output 9501abe55efSEd Tanous * into JSON. 9519391bb9cSRapkiewicz, Pawel */ 9529391bb9cSRapkiewicz, Pawel template <typename CallbackFunc> 9531abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback) 9541abe55efSEd Tanous { 95555c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 9564a0cb85cSEd Tanous [callback{std::move(callback)}]( 9579391bb9cSRapkiewicz, Pawel const boost::system::error_code error_code, 9584a0cb85cSEd Tanous GetManagedObjects& resp) { 9591abe55efSEd Tanous // Callback requires vector<string> to retrieve all available 9601abe55efSEd Tanous // ethernet interfaces 961*2c70f800SEd Tanous boost::container::flat_set<std::string> ifaceList; 962*2c70f800SEd Tanous ifaceList.reserve(resp.size()); 9631abe55efSEd Tanous if (error_code) 9641abe55efSEd Tanous { 965*2c70f800SEd Tanous callback(false, ifaceList); 9669391bb9cSRapkiewicz, Pawel return; 9679391bb9cSRapkiewicz, Pawel } 9689391bb9cSRapkiewicz, Pawel 9699391bb9cSRapkiewicz, Pawel // Iterate over all retrieved ObjectPaths. 9704a0cb85cSEd Tanous for (const auto& objpath : resp) 9711abe55efSEd Tanous { 9729391bb9cSRapkiewicz, Pawel // And all interfaces available for certain ObjectPath. 9734a0cb85cSEd Tanous for (const auto& interface : objpath.second) 9741abe55efSEd Tanous { 9751abe55efSEd Tanous // If interface is 9764a0cb85cSEd Tanous // xyz.openbmc_project.Network.EthernetInterface, this is 9774a0cb85cSEd Tanous // what we're looking for. 9789391bb9cSRapkiewicz, Pawel if (interface.first == 9791abe55efSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 9801abe55efSEd Tanous { 9814e0453b1SGunnar Mills // Cut out everything until last "/", ... 982*2c70f800SEd Tanous const std::string& ifaceId = objpath.first.str; 983*2c70f800SEd Tanous std::size_t lastPos = ifaceId.rfind("/"); 984*2c70f800SEd Tanous if (lastPos != std::string::npos) 9851abe55efSEd Tanous { 9869391bb9cSRapkiewicz, Pawel // and put it into output vector. 987*2c70f800SEd Tanous ifaceList.emplace(ifaceId.substr(lastPos + 1)); 9889391bb9cSRapkiewicz, Pawel } 9899391bb9cSRapkiewicz, Pawel } 9909391bb9cSRapkiewicz, Pawel } 9919391bb9cSRapkiewicz, Pawel } 992a434f2bdSEd Tanous // Finally make a callback with useful data 993*2c70f800SEd Tanous callback(true, ifaceList); 9949391bb9cSRapkiewicz, Pawel }, 995aa2e59c1SEd Tanous "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 996aa2e59c1SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 997271584abSEd Tanous } 9989391bb9cSRapkiewicz, Pawel 9999391bb9cSRapkiewicz, Pawel /** 10009391bb9cSRapkiewicz, Pawel * EthernetCollection derived class for delivering Ethernet Collection Schema 10019391bb9cSRapkiewicz, Pawel */ 10021abe55efSEd Tanous class EthernetCollection : public Node 10031abe55efSEd Tanous { 10049391bb9cSRapkiewicz, Pawel public: 100552cc112dSEd Tanous EthernetCollection(App& app) : 10064a0cb85cSEd Tanous Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") 10071abe55efSEd Tanous { 1008588c3f0dSKowalski, Kamil entityPrivileges = { 1009588c3f0dSKowalski, Kamil {boost::beast::http::verb::get, {{"Login"}}}, 1010e0d918bcSEd Tanous {boost::beast::http::verb::head, {{"Login"}}}, 1011e0d918bcSEd Tanous {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 1012e0d918bcSEd Tanous {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 1013e0d918bcSEd Tanous {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 1014e0d918bcSEd Tanous {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 10159391bb9cSRapkiewicz, Pawel } 10169391bb9cSRapkiewicz, Pawel 10179391bb9cSRapkiewicz, Pawel private: 10189391bb9cSRapkiewicz, Pawel /** 10199391bb9cSRapkiewicz, Pawel * Functions triggers appropriate requests on DBus 10209391bb9cSRapkiewicz, Pawel */ 1021cb13a392SEd Tanous void doGet(crow::Response& res, const crow::Request&, 1022cb13a392SEd Tanous const std::vector<std::string>&) override 10231abe55efSEd Tanous { 10240f74e643SEd Tanous res.jsonValue["@odata.type"] = 10250f74e643SEd Tanous "#EthernetInterfaceCollection.EthernetInterfaceCollection"; 10260f74e643SEd Tanous res.jsonValue["@odata.id"] = 10270f74e643SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 10280f74e643SEd Tanous res.jsonValue["Name"] = "Ethernet Network Interface Collection"; 10290f74e643SEd Tanous res.jsonValue["Description"] = 10300f74e643SEd Tanous "Collection of EthernetInterfaces for this Manager"; 10314c9afe43SEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 10324a0cb85cSEd Tanous // Get eth interface list, and call the below callback for JSON 10331abe55efSEd Tanous // preparation 1034f12894f8SJason M. Bills getEthernetIfaceList( 10354c9afe43SEd Tanous [asyncResp]( 10364c9afe43SEd Tanous const bool& success, 10374c9afe43SEd Tanous const boost::container::flat_set<std::string>& iface_list) { 10384a0cb85cSEd Tanous if (!success) 10391abe55efSEd Tanous { 10404c9afe43SEd Tanous messages::internalError(asyncResp->res); 10414a0cb85cSEd Tanous return; 10424a0cb85cSEd Tanous } 10434a0cb85cSEd Tanous 1044*2c70f800SEd Tanous nlohmann::json& ifaceArray = 10454c9afe43SEd Tanous asyncResp->res.jsonValue["Members"]; 1046*2c70f800SEd Tanous ifaceArray = nlohmann::json::array(); 1047fda13ad2SSunitha Harish std::string tag = "_"; 1048*2c70f800SEd Tanous for (const std::string& ifaceItem : iface_list) 10491abe55efSEd Tanous { 1050*2c70f800SEd Tanous std::size_t found = ifaceItem.find(tag); 1051fda13ad2SSunitha Harish if (found == std::string::npos) 1052fda13ad2SSunitha Harish { 1053*2c70f800SEd Tanous ifaceArray.push_back( 10544a0cb85cSEd Tanous {{"@odata.id", 10554a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1056*2c70f800SEd Tanous ifaceItem}}); 10579391bb9cSRapkiewicz, Pawel } 1058fda13ad2SSunitha Harish } 10594a0cb85cSEd Tanous 10604c9afe43SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 1061*2c70f800SEd Tanous ifaceArray.size(); 10624c9afe43SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 10634a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 10649391bb9cSRapkiewicz, Pawel }); 10659391bb9cSRapkiewicz, Pawel } 10669391bb9cSRapkiewicz, Pawel }; 10679391bb9cSRapkiewicz, Pawel 10689391bb9cSRapkiewicz, Pawel /** 10699391bb9cSRapkiewicz, Pawel * EthernetInterface derived class for delivering Ethernet Schema 10709391bb9cSRapkiewicz, Pawel */ 10711abe55efSEd Tanous class EthernetInterface : public Node 10721abe55efSEd Tanous { 10739391bb9cSRapkiewicz, Pawel public: 10749391bb9cSRapkiewicz, Pawel /* 10759391bb9cSRapkiewicz, Pawel * Default Constructor 10769391bb9cSRapkiewicz, Pawel */ 107752cc112dSEd Tanous EthernetInterface(App& app) : 10784a0cb85cSEd Tanous Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/", 10791abe55efSEd Tanous std::string()) 10801abe55efSEd Tanous { 1081588c3f0dSKowalski, Kamil entityPrivileges = { 1082588c3f0dSKowalski, Kamil {boost::beast::http::verb::get, {{"Login"}}}, 1083e0d918bcSEd Tanous {boost::beast::http::verb::head, {{"Login"}}}, 1084e0d918bcSEd Tanous {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 1085e0d918bcSEd Tanous {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 1086e0d918bcSEd Tanous {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 1087e0d918bcSEd Tanous {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 10889391bb9cSRapkiewicz, Pawel } 10899391bb9cSRapkiewicz, Pawel 1090e439f0f8SKowalski, Kamil private: 1091bc0bd6e0SEd Tanous void handleHostnamePatch(const std::string& hostname, 10924a0cb85cSEd Tanous const std::shared_ptr<AsyncResp> asyncResp) 10931abe55efSEd Tanous { 1094ab6554f1SJoshi-Mansi // SHOULD handle host names of up to 255 characters(RFC 1123) 1095ab6554f1SJoshi-Mansi if (hostname.length() > 255) 1096ab6554f1SJoshi-Mansi { 1097ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, hostname, 1098ab6554f1SJoshi-Mansi "HostName"); 1099ab6554f1SJoshi-Mansi return; 1100ab6554f1SJoshi-Mansi } 1101bc0bd6e0SEd Tanous crow::connections::systemBus->async_method_call( 1102bc0bd6e0SEd Tanous [asyncResp](const boost::system::error_code ec) { 11034a0cb85cSEd Tanous if (ec) 11044a0cb85cSEd Tanous { 1105a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 11061abe55efSEd Tanous } 1107bc0bd6e0SEd Tanous }, 1108bc0bd6e0SEd Tanous "xyz.openbmc_project.Network", 1109bc0bd6e0SEd Tanous "/xyz/openbmc_project/network/config", 1110bc0bd6e0SEd Tanous "org.freedesktop.DBus.Properties", "Set", 1111bc0bd6e0SEd Tanous "xyz.openbmc_project.Network.SystemConfiguration", "HostName", 1112abf2add6SEd Tanous std::variant<std::string>(hostname)); 1113588c3f0dSKowalski, Kamil } 1114588c3f0dSKowalski, Kamil 1115ab6554f1SJoshi-Mansi void handleDomainnamePatch(const std::string& ifaceId, 1116ab6554f1SJoshi-Mansi const std::string& domainname, 1117ab6554f1SJoshi-Mansi const std::shared_ptr<AsyncResp> asyncResp) 1118ab6554f1SJoshi-Mansi { 1119ab6554f1SJoshi-Mansi std::vector<std::string> vectorDomainname = {domainname}; 1120ab6554f1SJoshi-Mansi crow::connections::systemBus->async_method_call( 1121ab6554f1SJoshi-Mansi [asyncResp](const boost::system::error_code ec) { 1122ab6554f1SJoshi-Mansi if (ec) 1123ab6554f1SJoshi-Mansi { 1124ab6554f1SJoshi-Mansi messages::internalError(asyncResp->res); 1125ab6554f1SJoshi-Mansi } 1126ab6554f1SJoshi-Mansi }, 1127ab6554f1SJoshi-Mansi "xyz.openbmc_project.Network", 1128ab6554f1SJoshi-Mansi "/xyz/openbmc_project/network/" + ifaceId, 1129ab6554f1SJoshi-Mansi "org.freedesktop.DBus.Properties", "Set", 1130ab6554f1SJoshi-Mansi "xyz.openbmc_project.Network.EthernetInterface", "DomainName", 1131ab6554f1SJoshi-Mansi std::variant<std::vector<std::string>>(vectorDomainname)); 1132ab6554f1SJoshi-Mansi } 1133ab6554f1SJoshi-Mansi 1134ab6554f1SJoshi-Mansi void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn, 1135ab6554f1SJoshi-Mansi const std::shared_ptr<AsyncResp> asyncResp) 1136ab6554f1SJoshi-Mansi { 1137ab6554f1SJoshi-Mansi // Total length of FQDN must not exceed 255 characters(RFC 1035) 1138ab6554f1SJoshi-Mansi if (fqdn.length() > 255) 1139ab6554f1SJoshi-Mansi { 1140ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1141ab6554f1SJoshi-Mansi return; 1142ab6554f1SJoshi-Mansi } 1143ab6554f1SJoshi-Mansi 1144ab6554f1SJoshi-Mansi size_t pos = fqdn.find('.'); 1145ab6554f1SJoshi-Mansi if (pos == std::string::npos) 1146ab6554f1SJoshi-Mansi { 1147ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1148ab6554f1SJoshi-Mansi return; 1149ab6554f1SJoshi-Mansi } 1150ab6554f1SJoshi-Mansi 1151ab6554f1SJoshi-Mansi std::string hostname; 1152ab6554f1SJoshi-Mansi std::string domainname; 1153ab6554f1SJoshi-Mansi domainname = (fqdn).substr(pos + 1); 1154ab6554f1SJoshi-Mansi hostname = (fqdn).substr(0, pos); 1155ab6554f1SJoshi-Mansi 1156ab6554f1SJoshi-Mansi if (!isHostnameValid(hostname) || !isDomainnameValid(domainname)) 1157ab6554f1SJoshi-Mansi { 1158ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1159ab6554f1SJoshi-Mansi return; 1160ab6554f1SJoshi-Mansi } 1161ab6554f1SJoshi-Mansi 1162ab6554f1SJoshi-Mansi handleHostnamePatch(hostname, asyncResp); 1163ab6554f1SJoshi-Mansi handleDomainnamePatch(ifaceId, domainname, asyncResp); 1164ab6554f1SJoshi-Mansi } 1165ab6554f1SJoshi-Mansi 1166ab6554f1SJoshi-Mansi bool isHostnameValid(const std::string& hostname) 1167ab6554f1SJoshi-Mansi { 1168ab6554f1SJoshi-Mansi // A valid host name can never have the dotted-decimal form (RFC 1123) 1169ab6554f1SJoshi-Mansi if (std::all_of(hostname.begin(), hostname.end(), ::isdigit)) 1170ab6554f1SJoshi-Mansi { 1171ab6554f1SJoshi-Mansi return false; 1172ab6554f1SJoshi-Mansi } 1173ab6554f1SJoshi-Mansi // Each label(hostname/subdomains) within a valid FQDN 1174ab6554f1SJoshi-Mansi // MUST handle host names of up to 63 characters (RFC 1123) 1175ab6554f1SJoshi-Mansi // labels cannot start or end with hyphens (RFC 952) 1176ab6554f1SJoshi-Mansi // labels can start with numbers (RFC 1123) 1177ab6554f1SJoshi-Mansi const std::regex pattern( 1178ab6554f1SJoshi-Mansi "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$"); 1179ab6554f1SJoshi-Mansi 1180ab6554f1SJoshi-Mansi return std::regex_match(hostname, pattern); 1181ab6554f1SJoshi-Mansi } 1182ab6554f1SJoshi-Mansi 1183ab6554f1SJoshi-Mansi bool isDomainnameValid(const std::string& domainname) 1184ab6554f1SJoshi-Mansi { 1185ab6554f1SJoshi-Mansi // Can have multiple subdomains 1186ab6554f1SJoshi-Mansi // Top Level Domain's min length is 2 character 1187ab6554f1SJoshi-Mansi const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]" 1188ab6554f1SJoshi-Mansi "{1,30}\\.)*[a-zA-Z]{2,}$"); 1189ab6554f1SJoshi-Mansi 1190ab6554f1SJoshi-Mansi return std::regex_match(domainname, pattern); 1191ab6554f1SJoshi-Mansi } 1192ab6554f1SJoshi-Mansi 1193d577665bSRatan Gupta void handleMACAddressPatch(const std::string& ifaceId, 1194d577665bSRatan Gupta const std::string& macAddress, 1195d577665bSRatan Gupta const std::shared_ptr<AsyncResp>& asyncResp) 1196d577665bSRatan Gupta { 1197d577665bSRatan Gupta crow::connections::systemBus->async_method_call( 1198d577665bSRatan Gupta [asyncResp, macAddress](const boost::system::error_code ec) { 1199d577665bSRatan Gupta if (ec) 1200d577665bSRatan Gupta { 1201d577665bSRatan Gupta messages::internalError(asyncResp->res); 1202d577665bSRatan Gupta return; 1203d577665bSRatan Gupta } 1204d577665bSRatan Gupta }, 1205d577665bSRatan Gupta "xyz.openbmc_project.Network", 1206d577665bSRatan Gupta "/xyz/openbmc_project/network/" + ifaceId, 1207d577665bSRatan Gupta "org.freedesktop.DBus.Properties", "Set", 1208d577665bSRatan Gupta "xyz.openbmc_project.Network.MACAddress", "MACAddress", 1209d577665bSRatan Gupta std::variant<std::string>(macAddress)); 1210d577665bSRatan Gupta } 1211286b9118SJohnathan Mantey 1212da131a9aSJennifer Lee void setDHCPEnabled(const std::string& ifaceId, 12131f8c7b5dSJohnathan Mantey const std::string& propertyName, const bool v4Value, 12141f8c7b5dSJohnathan Mantey const bool v6Value, 1215da131a9aSJennifer Lee const std::shared_ptr<AsyncResp> asyncResp) 1216da131a9aSJennifer Lee { 1217*2c70f800SEd Tanous const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value); 1218da131a9aSJennifer Lee crow::connections::systemBus->async_method_call( 1219da131a9aSJennifer Lee [asyncResp](const boost::system::error_code ec) { 1220da131a9aSJennifer Lee if (ec) 1221da131a9aSJennifer Lee { 1222da131a9aSJennifer Lee BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1223da131a9aSJennifer Lee messages::internalError(asyncResp->res); 1224da131a9aSJennifer Lee return; 1225da131a9aSJennifer Lee } 1226da131a9aSJennifer Lee }, 1227da131a9aSJennifer Lee "xyz.openbmc_project.Network", 1228da131a9aSJennifer Lee "/xyz/openbmc_project/network/" + ifaceId, 1229da131a9aSJennifer Lee "org.freedesktop.DBus.Properties", "Set", 1230da131a9aSJennifer Lee "xyz.openbmc_project.Network.EthernetInterface", propertyName, 12311f8c7b5dSJohnathan Mantey std::variant<std::string>{dhcp}); 1232da131a9aSJennifer Lee } 12331f8c7b5dSJohnathan Mantey 1234eeedda23SJohnathan Mantey void setEthernetInterfaceBoolProperty( 1235eeedda23SJohnathan Mantey const std::string& ifaceId, const std::string& propertyName, 1236eeedda23SJohnathan Mantey const bool& value, const std::shared_ptr<AsyncResp> asyncResp) 1237eeedda23SJohnathan Mantey { 1238eeedda23SJohnathan Mantey crow::connections::systemBus->async_method_call( 1239eeedda23SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 1240eeedda23SJohnathan Mantey if (ec) 1241eeedda23SJohnathan Mantey { 1242eeedda23SJohnathan Mantey BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1243eeedda23SJohnathan Mantey messages::internalError(asyncResp->res); 1244eeedda23SJohnathan Mantey return; 1245eeedda23SJohnathan Mantey } 1246eeedda23SJohnathan Mantey }, 1247eeedda23SJohnathan Mantey "xyz.openbmc_project.Network", 1248eeedda23SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 1249eeedda23SJohnathan Mantey "org.freedesktop.DBus.Properties", "Set", 1250eeedda23SJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1251eeedda23SJohnathan Mantey std::variant<bool>{value}); 1252eeedda23SJohnathan Mantey } 1253eeedda23SJohnathan Mantey 1254da131a9aSJennifer Lee void setDHCPv4Config(const std::string& propertyName, const bool& value, 1255da131a9aSJennifer Lee const std::shared_ptr<AsyncResp> asyncResp) 1256da131a9aSJennifer Lee { 1257da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << propertyName << " = " << value; 1258da131a9aSJennifer Lee crow::connections::systemBus->async_method_call( 1259da131a9aSJennifer Lee [asyncResp](const boost::system::error_code ec) { 1260da131a9aSJennifer Lee if (ec) 1261da131a9aSJennifer Lee { 1262da131a9aSJennifer Lee BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1263da131a9aSJennifer Lee messages::internalError(asyncResp->res); 1264da131a9aSJennifer Lee return; 1265da131a9aSJennifer Lee } 1266da131a9aSJennifer Lee }, 1267da131a9aSJennifer Lee "xyz.openbmc_project.Network", 1268da131a9aSJennifer Lee "/xyz/openbmc_project/network/config/dhcp", 1269da131a9aSJennifer Lee "org.freedesktop.DBus.Properties", "Set", 1270da131a9aSJennifer Lee "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, 1271da131a9aSJennifer Lee std::variant<bool>{value}); 1272da131a9aSJennifer Lee } 1273d577665bSRatan Gupta 12741f8c7b5dSJohnathan Mantey void handleDHCPPatch(const std::string& ifaceId, 12751f8c7b5dSJohnathan Mantey const EthernetInterfaceData& ethData, 12761f8c7b5dSJohnathan Mantey DHCPParameters v4dhcpParms, DHCPParameters v6dhcpParms, 1277da131a9aSJennifer Lee const std::shared_ptr<AsyncResp> asyncResp) 1278da131a9aSJennifer Lee { 12791f8c7b5dSJohnathan Mantey bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true); 12801f8c7b5dSJohnathan Mantey bool ipv6Active = 12811f8c7b5dSJohnathan Mantey translateDHCPEnabledToBool(ethData.DHCPEnabled, false); 1282da131a9aSJennifer Lee 12831f8c7b5dSJohnathan Mantey bool nextv4DHCPState = 12841f8c7b5dSJohnathan Mantey v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active; 12851f8c7b5dSJohnathan Mantey 12861f8c7b5dSJohnathan Mantey bool nextv6DHCPState{}; 12871f8c7b5dSJohnathan Mantey if (v6dhcpParms.dhcpv6OperatingMode) 1288da131a9aSJennifer Lee { 12891f8c7b5dSJohnathan Mantey if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") && 12901f8c7b5dSJohnathan Mantey (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") && 12911f8c7b5dSJohnathan Mantey (*v6dhcpParms.dhcpv6OperatingMode != "Disabled")) 12921f8c7b5dSJohnathan Mantey { 12931f8c7b5dSJohnathan Mantey messages::propertyValueFormatError( 12941f8c7b5dSJohnathan Mantey asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode, 12951f8c7b5dSJohnathan Mantey "OperatingMode"); 1296da131a9aSJennifer Lee return; 1297da131a9aSJennifer Lee } 12981f8c7b5dSJohnathan Mantey nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful"); 12991f8c7b5dSJohnathan Mantey } 13001f8c7b5dSJohnathan Mantey else 1301da131a9aSJennifer Lee { 13021f8c7b5dSJohnathan Mantey nextv6DHCPState = ipv6Active; 13031f8c7b5dSJohnathan Mantey } 13041f8c7b5dSJohnathan Mantey 13051f8c7b5dSJohnathan Mantey bool nextDNS{}; 13061f8c7b5dSJohnathan Mantey if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers) 13071f8c7b5dSJohnathan Mantey { 13081f8c7b5dSJohnathan Mantey if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers) 13091f8c7b5dSJohnathan Mantey { 13101f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 13111f8c7b5dSJohnathan Mantey return; 13121f8c7b5dSJohnathan Mantey } 13131f8c7b5dSJohnathan Mantey nextDNS = *v4dhcpParms.useDNSServers; 13141f8c7b5dSJohnathan Mantey } 13151f8c7b5dSJohnathan Mantey else if (v4dhcpParms.useDNSServers) 13161f8c7b5dSJohnathan Mantey { 13171f8c7b5dSJohnathan Mantey nextDNS = *v4dhcpParms.useDNSServers; 13181f8c7b5dSJohnathan Mantey } 13191f8c7b5dSJohnathan Mantey else if (v6dhcpParms.useDNSServers) 13201f8c7b5dSJohnathan Mantey { 13211f8c7b5dSJohnathan Mantey nextDNS = *v6dhcpParms.useDNSServers; 13221f8c7b5dSJohnathan Mantey } 13231f8c7b5dSJohnathan Mantey else 13241f8c7b5dSJohnathan Mantey { 13251f8c7b5dSJohnathan Mantey nextDNS = ethData.DNSEnabled; 13261f8c7b5dSJohnathan Mantey } 13271f8c7b5dSJohnathan Mantey 13281f8c7b5dSJohnathan Mantey bool nextNTP{}; 13291f8c7b5dSJohnathan Mantey if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers) 13301f8c7b5dSJohnathan Mantey { 13311f8c7b5dSJohnathan Mantey if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers) 13321f8c7b5dSJohnathan Mantey { 13331f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 13341f8c7b5dSJohnathan Mantey return; 13351f8c7b5dSJohnathan Mantey } 13361f8c7b5dSJohnathan Mantey nextNTP = *v4dhcpParms.useNTPServers; 13371f8c7b5dSJohnathan Mantey } 13381f8c7b5dSJohnathan Mantey else if (v4dhcpParms.useNTPServers) 13391f8c7b5dSJohnathan Mantey { 13401f8c7b5dSJohnathan Mantey nextNTP = *v4dhcpParms.useNTPServers; 13411f8c7b5dSJohnathan Mantey } 13421f8c7b5dSJohnathan Mantey else if (v6dhcpParms.useNTPServers) 13431f8c7b5dSJohnathan Mantey { 13441f8c7b5dSJohnathan Mantey nextNTP = *v6dhcpParms.useNTPServers; 13451f8c7b5dSJohnathan Mantey } 13461f8c7b5dSJohnathan Mantey else 13471f8c7b5dSJohnathan Mantey { 13481f8c7b5dSJohnathan Mantey nextNTP = ethData.NTPEnabled; 13491f8c7b5dSJohnathan Mantey } 13501f8c7b5dSJohnathan Mantey 13511f8c7b5dSJohnathan Mantey bool nextUseDomain{}; 13521f8c7b5dSJohnathan Mantey if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName) 13531f8c7b5dSJohnathan Mantey { 13541f8c7b5dSJohnathan Mantey if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName) 13551f8c7b5dSJohnathan Mantey { 13561f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 13571f8c7b5dSJohnathan Mantey return; 13581f8c7b5dSJohnathan Mantey } 13591f8c7b5dSJohnathan Mantey nextUseDomain = *v4dhcpParms.useUseDomainName; 13601f8c7b5dSJohnathan Mantey } 13611f8c7b5dSJohnathan Mantey else if (v4dhcpParms.useUseDomainName) 13621f8c7b5dSJohnathan Mantey { 13631f8c7b5dSJohnathan Mantey nextUseDomain = *v4dhcpParms.useUseDomainName; 13641f8c7b5dSJohnathan Mantey } 13651f8c7b5dSJohnathan Mantey else if (v6dhcpParms.useUseDomainName) 13661f8c7b5dSJohnathan Mantey { 13671f8c7b5dSJohnathan Mantey nextUseDomain = *v6dhcpParms.useUseDomainName; 13681f8c7b5dSJohnathan Mantey } 13691f8c7b5dSJohnathan Mantey else 13701f8c7b5dSJohnathan Mantey { 13711f8c7b5dSJohnathan Mantey nextUseDomain = ethData.HostNameEnabled; 13721f8c7b5dSJohnathan Mantey } 13731f8c7b5dSJohnathan Mantey 1374da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set DHCPEnabled..."; 13751f8c7b5dSJohnathan Mantey setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState, 13761f8c7b5dSJohnathan Mantey asyncResp); 1377da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set DNSEnabled..."; 13781f8c7b5dSJohnathan Mantey setDHCPv4Config("DNSEnabled", nextDNS, asyncResp); 1379da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set NTPEnabled..."; 13801f8c7b5dSJohnathan Mantey setDHCPv4Config("NTPEnabled", nextNTP, asyncResp); 13811f8c7b5dSJohnathan Mantey BMCWEB_LOG_DEBUG << "set HostNameEnabled..."; 13821f8c7b5dSJohnathan Mantey setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp); 1383da131a9aSJennifer Lee } 138401784826SJohnathan Mantey 138501784826SJohnathan Mantey boost::container::flat_set<IPv4AddressData>::const_iterator 1386*2c70f800SEd Tanous getNextStaticIpEntry( 138701784826SJohnathan Mantey boost::container::flat_set<IPv4AddressData>::const_iterator head, 138801784826SJohnathan Mantey boost::container::flat_set<IPv4AddressData>::const_iterator end) 138901784826SJohnathan Mantey { 139001784826SJohnathan Mantey for (; head != end; head++) 139101784826SJohnathan Mantey { 139201784826SJohnathan Mantey if (head->origin == "Static") 139301784826SJohnathan Mantey { 139401784826SJohnathan Mantey return head; 139501784826SJohnathan Mantey } 139601784826SJohnathan Mantey } 139701784826SJohnathan Mantey return end; 139801784826SJohnathan Mantey } 139901784826SJohnathan Mantey 140001784826SJohnathan Mantey boost::container::flat_set<IPv6AddressData>::const_iterator 1401*2c70f800SEd Tanous getNextStaticIpEntry( 140201784826SJohnathan Mantey boost::container::flat_set<IPv6AddressData>::const_iterator head, 140301784826SJohnathan Mantey boost::container::flat_set<IPv6AddressData>::const_iterator end) 140401784826SJohnathan Mantey { 140501784826SJohnathan Mantey for (; head != end; head++) 140601784826SJohnathan Mantey { 140701784826SJohnathan Mantey if (head->origin == "Static") 140801784826SJohnathan Mantey { 140901784826SJohnathan Mantey return head; 141001784826SJohnathan Mantey } 141101784826SJohnathan Mantey } 141201784826SJohnathan Mantey return end; 141301784826SJohnathan Mantey } 141401784826SJohnathan Mantey 1415d1d50814SRavi Teja void handleIPv4StaticPatch( 1416f476acbfSRatan Gupta const std::string& ifaceId, nlohmann::json& input, 141701784826SJohnathan Mantey const boost::container::flat_set<IPv4AddressData>& ipv4Data, 14184a0cb85cSEd Tanous const std::shared_ptr<AsyncResp> asyncResp) 14191abe55efSEd Tanous { 142001784826SJohnathan Mantey if ((!input.is_array()) || input.empty()) 1421f476acbfSRatan Gupta { 1422f476acbfSRatan Gupta messages::propertyValueTypeError(asyncResp->res, input.dump(), 1423d1d50814SRavi Teja "IPv4StaticAddresses"); 1424f476acbfSRatan Gupta return; 1425f476acbfSRatan Gupta } 1426f476acbfSRatan Gupta 1427271584abSEd Tanous unsigned entryIdx = 1; 142801784826SJohnathan Mantey // Find the first static IP address currently active on the NIC and 142901784826SJohnathan Mantey // match it to the first JSON element in the IPv4StaticAddresses array. 143001784826SJohnathan Mantey // Match each subsequent JSON element to the next static IP programmed 143101784826SJohnathan Mantey // into the NIC. 1432*2c70f800SEd Tanous boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry = 1433*2c70f800SEd Tanous getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend()); 143401784826SJohnathan Mantey 1435537174c4SEd Tanous for (nlohmann::json& thisJson : input) 14361abe55efSEd Tanous { 14374a0cb85cSEd Tanous std::string pathString = 1438d1d50814SRavi Teja "IPv4StaticAddresses/" + std::to_string(entryIdx); 1439179db1d7SKowalski, Kamil 144001784826SJohnathan Mantey if (!thisJson.is_null() && !thisJson.empty()) 1441f476acbfSRatan Gupta { 1442537174c4SEd Tanous std::optional<std::string> address; 1443537174c4SEd Tanous std::optional<std::string> subnetMask; 1444537174c4SEd Tanous std::optional<std::string> gateway; 1445537174c4SEd Tanous 1446537174c4SEd Tanous if (!json_util::readJson(thisJson, asyncResp->res, "Address", 14477e27d832SJohnathan Mantey address, "SubnetMask", subnetMask, 14487e27d832SJohnathan Mantey "Gateway", gateway)) 1449537174c4SEd Tanous { 145001784826SJohnathan Mantey messages::propertyValueFormatError( 145101784826SJohnathan Mantey asyncResp->res, thisJson.dump(), pathString); 1452537174c4SEd Tanous return; 1453179db1d7SKowalski, Kamil } 1454179db1d7SKowalski, Kamil 145501784826SJohnathan Mantey // Find the address/subnet/gateway values. Any values that are 145601784826SJohnathan Mantey // not explicitly provided are assumed to be unmodified from the 145701784826SJohnathan Mantey // current state of the interface. Merge existing state into the 145801784826SJohnathan Mantey // current request. 1459271584abSEd Tanous const std::string* addr = nullptr; 1460271584abSEd Tanous const std::string* gw = nullptr; 146101784826SJohnathan Mantey uint8_t prefixLength = 0; 146201784826SJohnathan Mantey bool errorInEntry = false; 1463537174c4SEd Tanous if (address) 14641abe55efSEd Tanous { 146501784826SJohnathan Mantey if (ipv4VerifyIpAndGetBitcount(*address)) 14661abe55efSEd Tanous { 146701784826SJohnathan Mantey addr = &(*address); 14684a0cb85cSEd Tanous } 146901784826SJohnathan Mantey else 147001784826SJohnathan Mantey { 147101784826SJohnathan Mantey messages::propertyValueFormatError( 147201784826SJohnathan Mantey asyncResp->res, *address, pathString + "/Address"); 147301784826SJohnathan Mantey errorInEntry = true; 147401784826SJohnathan Mantey } 147501784826SJohnathan Mantey } 1476*2c70f800SEd Tanous else if (niciPentry != ipv4Data.cend()) 147701784826SJohnathan Mantey { 1478*2c70f800SEd Tanous addr = &(niciPentry->address); 147901784826SJohnathan Mantey } 148001784826SJohnathan Mantey else 148101784826SJohnathan Mantey { 148201784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 148301784826SJohnathan Mantey pathString + "/Address"); 148401784826SJohnathan Mantey errorInEntry = true; 14854a0cb85cSEd Tanous } 14864a0cb85cSEd Tanous 1487537174c4SEd Tanous if (subnetMask) 14884a0cb85cSEd Tanous { 1489537174c4SEd Tanous if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength)) 14904a0cb85cSEd Tanous { 1491f12894f8SJason M. Bills messages::propertyValueFormatError( 1492537174c4SEd Tanous asyncResp->res, *subnetMask, 14934a0cb85cSEd Tanous pathString + "/SubnetMask"); 149401784826SJohnathan Mantey errorInEntry = true; 14954a0cb85cSEd Tanous } 14964a0cb85cSEd Tanous } 1497*2c70f800SEd Tanous else if (niciPentry != ipv4Data.cend()) 14984a0cb85cSEd Tanous { 1499*2c70f800SEd Tanous if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask, 150001784826SJohnathan Mantey &prefixLength)) 15014a0cb85cSEd Tanous { 150201784826SJohnathan Mantey messages::propertyValueFormatError( 1503*2c70f800SEd Tanous asyncResp->res, niciPentry->netmask, 150401784826SJohnathan Mantey pathString + "/SubnetMask"); 150501784826SJohnathan Mantey errorInEntry = true; 15064a0cb85cSEd Tanous } 15074a0cb85cSEd Tanous } 15081abe55efSEd Tanous else 15091abe55efSEd Tanous { 151001784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 151101784826SJohnathan Mantey pathString + "/SubnetMask"); 151201784826SJohnathan Mantey errorInEntry = true; 151301784826SJohnathan Mantey } 151401784826SJohnathan Mantey 151501784826SJohnathan Mantey if (gateway) 151601784826SJohnathan Mantey { 151701784826SJohnathan Mantey if (ipv4VerifyIpAndGetBitcount(*gateway)) 151801784826SJohnathan Mantey { 151901784826SJohnathan Mantey gw = &(*gateway); 152001784826SJohnathan Mantey } 152101784826SJohnathan Mantey else 152201784826SJohnathan Mantey { 152301784826SJohnathan Mantey messages::propertyValueFormatError( 152401784826SJohnathan Mantey asyncResp->res, *gateway, pathString + "/Gateway"); 152501784826SJohnathan Mantey errorInEntry = true; 152601784826SJohnathan Mantey } 152701784826SJohnathan Mantey } 1528*2c70f800SEd Tanous else if (niciPentry != ipv4Data.cend()) 152901784826SJohnathan Mantey { 1530*2c70f800SEd Tanous gw = &niciPentry->gateway; 153101784826SJohnathan Mantey } 153201784826SJohnathan Mantey else 15331abe55efSEd Tanous { 1534a08b46ccSJason M. Bills messages::propertyMissing(asyncResp->res, 15354a0cb85cSEd Tanous pathString + "/Gateway"); 153601784826SJohnathan Mantey errorInEntry = true; 15374a0cb85cSEd Tanous } 15384a0cb85cSEd Tanous 153901784826SJohnathan Mantey if (errorInEntry) 15401abe55efSEd Tanous { 154101784826SJohnathan Mantey return; 15424a0cb85cSEd Tanous } 15434a0cb85cSEd Tanous 1544*2c70f800SEd Tanous if (niciPentry != ipv4Data.cend()) 15451abe55efSEd Tanous { 1546*2c70f800SEd Tanous deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength, 154701784826SJohnathan Mantey *gw, *addr, asyncResp); 1548*2c70f800SEd Tanous niciPentry = 1549*2c70f800SEd Tanous getNextStaticIpEntry(++niciPentry, ipv4Data.cend()); 1550588c3f0dSKowalski, Kamil } 155101784826SJohnathan Mantey else 155201784826SJohnathan Mantey { 1553cb13a392SEd Tanous createIPv4(ifaceId, prefixLength, *gateway, *address, 1554cb13a392SEd Tanous asyncResp); 15554a0cb85cSEd Tanous } 15564a0cb85cSEd Tanous entryIdx++; 15574a0cb85cSEd Tanous } 155801784826SJohnathan Mantey else 155901784826SJohnathan Mantey { 1560*2c70f800SEd Tanous if (niciPentry == ipv4Data.cend()) 156101784826SJohnathan Mantey { 156201784826SJohnathan Mantey // Requesting a DELETE/DO NOT MODIFY action for an item 156301784826SJohnathan Mantey // that isn't present on the eth(n) interface. Input JSON is 156401784826SJohnathan Mantey // in error, so bail out. 156501784826SJohnathan Mantey if (thisJson.is_null()) 156601784826SJohnathan Mantey { 156701784826SJohnathan Mantey messages::resourceCannotBeDeleted(asyncResp->res); 156801784826SJohnathan Mantey return; 156901784826SJohnathan Mantey } 157001784826SJohnathan Mantey else 157101784826SJohnathan Mantey { 157201784826SJohnathan Mantey messages::propertyValueFormatError( 157301784826SJohnathan Mantey asyncResp->res, thisJson.dump(), pathString); 157401784826SJohnathan Mantey return; 157501784826SJohnathan Mantey } 157601784826SJohnathan Mantey } 157701784826SJohnathan Mantey 157801784826SJohnathan Mantey if (thisJson.is_null()) 157901784826SJohnathan Mantey { 1580*2c70f800SEd Tanous deleteIPv4(ifaceId, niciPentry->id, asyncResp); 158101784826SJohnathan Mantey } 1582*2c70f800SEd Tanous if (niciPentry != ipv4Data.cend()) 158301784826SJohnathan Mantey { 1584*2c70f800SEd Tanous niciPentry = 1585*2c70f800SEd Tanous getNextStaticIpEntry(++niciPentry, ipv4Data.cend()); 158601784826SJohnathan Mantey } 158701784826SJohnathan Mantey entryIdx++; 158801784826SJohnathan Mantey } 158901784826SJohnathan Mantey } 15904a0cb85cSEd Tanous } 15914a0cb85cSEd Tanous 1592f85837bfSRAJESWARAN THILLAIGOVINDAN void handleStaticNameServersPatch( 1593f85837bfSRAJESWARAN THILLAIGOVINDAN const std::string& ifaceId, 1594f85837bfSRAJESWARAN THILLAIGOVINDAN const std::vector<std::string>& updatedStaticNameServers, 1595f85837bfSRAJESWARAN THILLAIGOVINDAN const std::shared_ptr<AsyncResp>& asyncResp) 1596f85837bfSRAJESWARAN THILLAIGOVINDAN { 1597f85837bfSRAJESWARAN THILLAIGOVINDAN crow::connections::systemBus->async_method_call( 1598286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 1599f85837bfSRAJESWARAN THILLAIGOVINDAN if (ec) 1600f85837bfSRAJESWARAN THILLAIGOVINDAN { 1601f85837bfSRAJESWARAN THILLAIGOVINDAN messages::internalError(asyncResp->res); 1602f85837bfSRAJESWARAN THILLAIGOVINDAN return; 1603f85837bfSRAJESWARAN THILLAIGOVINDAN } 1604f85837bfSRAJESWARAN THILLAIGOVINDAN }, 1605f85837bfSRAJESWARAN THILLAIGOVINDAN "xyz.openbmc_project.Network", 1606f85837bfSRAJESWARAN THILLAIGOVINDAN "/xyz/openbmc_project/network/" + ifaceId, 1607f85837bfSRAJESWARAN THILLAIGOVINDAN "org.freedesktop.DBus.Properties", "Set", 16080f6efdc1Smanojkiran.eda@gmail.com "xyz.openbmc_project.Network.EthernetInterface", 16090f6efdc1Smanojkiran.eda@gmail.com "StaticNameServers", 1610f85837bfSRAJESWARAN THILLAIGOVINDAN std::variant<std::vector<std::string>>{updatedStaticNameServers}); 1611f85837bfSRAJESWARAN THILLAIGOVINDAN } 1612f85837bfSRAJESWARAN THILLAIGOVINDAN 1613e48c0fc5SRavi Teja void handleIPv6StaticAddressesPatch( 1614e48c0fc5SRavi Teja const std::string& ifaceId, nlohmann::json& input, 161501784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data, 1616e48c0fc5SRavi Teja const std::shared_ptr<AsyncResp> asyncResp) 1617e48c0fc5SRavi Teja { 161801784826SJohnathan Mantey if (!input.is_array() || input.empty()) 1619e48c0fc5SRavi Teja { 1620e48c0fc5SRavi Teja messages::propertyValueTypeError(asyncResp->res, input.dump(), 1621e48c0fc5SRavi Teja "IPv6StaticAddresses"); 1622e48c0fc5SRavi Teja return; 1623e48c0fc5SRavi Teja } 1624271584abSEd Tanous size_t entryIdx = 1; 1625*2c70f800SEd Tanous boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry = 1626*2c70f800SEd Tanous getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend()); 1627e48c0fc5SRavi Teja for (nlohmann::json& thisJson : input) 1628e48c0fc5SRavi Teja { 1629e48c0fc5SRavi Teja std::string pathString = 1630e48c0fc5SRavi Teja "IPv6StaticAddresses/" + std::to_string(entryIdx); 1631e48c0fc5SRavi Teja 163201784826SJohnathan Mantey if (!thisJson.is_null() && !thisJson.empty()) 1633e48c0fc5SRavi Teja { 1634e48c0fc5SRavi Teja std::optional<std::string> address; 1635e48c0fc5SRavi Teja std::optional<uint8_t> prefixLength; 1636e48c0fc5SRavi Teja 1637e48c0fc5SRavi Teja if (!json_util::readJson(thisJson, asyncResp->res, "Address", 1638e48c0fc5SRavi Teja address, "PrefixLength", prefixLength)) 1639e48c0fc5SRavi Teja { 164001784826SJohnathan Mantey messages::propertyValueFormatError( 164101784826SJohnathan Mantey asyncResp->res, thisJson.dump(), pathString); 1642e48c0fc5SRavi Teja return; 1643e48c0fc5SRavi Teja } 1644e48c0fc5SRavi Teja 164501784826SJohnathan Mantey const std::string* addr; 164601784826SJohnathan Mantey uint8_t prefix; 164701784826SJohnathan Mantey 164801784826SJohnathan Mantey // Find the address and prefixLength values. Any values that are 164901784826SJohnathan Mantey // not explicitly provided are assumed to be unmodified from the 165001784826SJohnathan Mantey // current state of the interface. Merge existing state into the 165101784826SJohnathan Mantey // current request. 1652e48c0fc5SRavi Teja if (address) 1653e48c0fc5SRavi Teja { 165401784826SJohnathan Mantey addr = &(*address); 1655e48c0fc5SRavi Teja } 1656*2c70f800SEd Tanous else if (niciPentry != ipv6Data.end()) 165701784826SJohnathan Mantey { 1658*2c70f800SEd Tanous addr = &(niciPentry->address); 165901784826SJohnathan Mantey } 166001784826SJohnathan Mantey else 166101784826SJohnathan Mantey { 166201784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 166301784826SJohnathan Mantey pathString + "/Address"); 166401784826SJohnathan Mantey return; 1665e48c0fc5SRavi Teja } 1666e48c0fc5SRavi Teja 1667e48c0fc5SRavi Teja if (prefixLength) 1668e48c0fc5SRavi Teja { 166901784826SJohnathan Mantey prefix = *prefixLength; 167001784826SJohnathan Mantey } 1671*2c70f800SEd Tanous else if (niciPentry != ipv6Data.end()) 1672e48c0fc5SRavi Teja { 1673*2c70f800SEd Tanous prefix = niciPentry->prefixLength; 1674e48c0fc5SRavi Teja } 1675e48c0fc5SRavi Teja else 1676e48c0fc5SRavi Teja { 1677e48c0fc5SRavi Teja messages::propertyMissing(asyncResp->res, 1678e48c0fc5SRavi Teja pathString + "/PrefixLength"); 167901784826SJohnathan Mantey return; 1680e48c0fc5SRavi Teja } 1681e48c0fc5SRavi Teja 1682*2c70f800SEd Tanous if (niciPentry != ipv6Data.end()) 1683e48c0fc5SRavi Teja { 1684*2c70f800SEd Tanous deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr, 1685e48c0fc5SRavi Teja asyncResp); 1686*2c70f800SEd Tanous niciPentry = 1687*2c70f800SEd Tanous getNextStaticIpEntry(++niciPentry, ipv6Data.cend()); 168801784826SJohnathan Mantey } 168901784826SJohnathan Mantey else 169001784826SJohnathan Mantey { 169101784826SJohnathan Mantey createIPv6(ifaceId, *prefixLength, *addr, asyncResp); 1692e48c0fc5SRavi Teja } 1693e48c0fc5SRavi Teja entryIdx++; 1694e48c0fc5SRavi Teja } 169501784826SJohnathan Mantey else 169601784826SJohnathan Mantey { 1697*2c70f800SEd Tanous if (niciPentry == ipv6Data.end()) 169801784826SJohnathan Mantey { 169901784826SJohnathan Mantey // Requesting a DELETE/DO NOT MODIFY action for an item 170001784826SJohnathan Mantey // that isn't present on the eth(n) interface. Input JSON is 170101784826SJohnathan Mantey // in error, so bail out. 170201784826SJohnathan Mantey if (thisJson.is_null()) 170301784826SJohnathan Mantey { 170401784826SJohnathan Mantey messages::resourceCannotBeDeleted(asyncResp->res); 170501784826SJohnathan Mantey return; 170601784826SJohnathan Mantey } 170701784826SJohnathan Mantey else 170801784826SJohnathan Mantey { 170901784826SJohnathan Mantey messages::propertyValueFormatError( 171001784826SJohnathan Mantey asyncResp->res, thisJson.dump(), pathString); 171101784826SJohnathan Mantey return; 171201784826SJohnathan Mantey } 171301784826SJohnathan Mantey } 171401784826SJohnathan Mantey 171501784826SJohnathan Mantey if (thisJson.is_null()) 171601784826SJohnathan Mantey { 1717*2c70f800SEd Tanous deleteIPv6(ifaceId, niciPentry->id, asyncResp); 171801784826SJohnathan Mantey } 1719*2c70f800SEd Tanous if (niciPentry != ipv6Data.cend()) 172001784826SJohnathan Mantey { 1721*2c70f800SEd Tanous niciPentry = 1722*2c70f800SEd Tanous getNextStaticIpEntry(++niciPentry, ipv6Data.cend()); 172301784826SJohnathan Mantey } 172401784826SJohnathan Mantey entryIdx++; 172501784826SJohnathan Mantey } 172601784826SJohnathan Mantey } 1727e48c0fc5SRavi Teja } 1728e48c0fc5SRavi Teja 17290f74e643SEd Tanous void parseInterfaceData( 1730eeedda23SJohnathan Mantey std::shared_ptr<AsyncResp> asyncResp, const std::string& iface_id, 17310f74e643SEd Tanous const EthernetInterfaceData& ethData, 1732e48c0fc5SRavi Teja const boost::container::flat_set<IPv4AddressData>& ipv4Data, 173301784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data) 17344a0cb85cSEd Tanous { 1735eeedda23SJohnathan Mantey constexpr const std::array<const char*, 1> inventoryForEthernet = { 1736eeedda23SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Ethernet"}; 1737eeedda23SJohnathan Mantey 1738*2c70f800SEd Tanous nlohmann::json& jsonResponse = asyncResp->res.jsonValue; 1739*2c70f800SEd Tanous jsonResponse["Id"] = iface_id; 1740*2c70f800SEd Tanous jsonResponse["@odata.id"] = 17414a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id; 1742*2c70f800SEd Tanous jsonResponse["InterfaceEnabled"] = ethData.nicEnabled; 1743eeedda23SJohnathan Mantey 1744eeedda23SJohnathan Mantey auto health = std::make_shared<HealthPopulate>(asyncResp); 1745eeedda23SJohnathan Mantey 1746eeedda23SJohnathan Mantey crow::connections::systemBus->async_method_call( 1747eeedda23SJohnathan Mantey [health](const boost::system::error_code ec, 1748eeedda23SJohnathan Mantey std::vector<std::string>& resp) { 1749eeedda23SJohnathan Mantey if (ec) 1750029573d4SEd Tanous { 1751eeedda23SJohnathan Mantey return; 1752eeedda23SJohnathan Mantey } 1753eeedda23SJohnathan Mantey 1754eeedda23SJohnathan Mantey health->inventory = std::move(resp); 1755eeedda23SJohnathan Mantey }, 1756eeedda23SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", 1757eeedda23SJohnathan Mantey "/xyz/openbmc_project/object_mapper", 1758eeedda23SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 1759eeedda23SJohnathan Mantey int32_t(0), inventoryForEthernet); 1760eeedda23SJohnathan Mantey 1761eeedda23SJohnathan Mantey health->populate(); 1762eeedda23SJohnathan Mantey 1763eeedda23SJohnathan Mantey if (ethData.nicEnabled) 1764eeedda23SJohnathan Mantey { 1765*2c70f800SEd Tanous jsonResponse["LinkStatus"] = "LinkUp"; 1766*2c70f800SEd Tanous jsonResponse["Status"]["State"] = "Enabled"; 1767029573d4SEd Tanous } 1768029573d4SEd Tanous else 1769029573d4SEd Tanous { 1770*2c70f800SEd Tanous jsonResponse["LinkStatus"] = "NoLink"; 1771*2c70f800SEd Tanous jsonResponse["Status"]["State"] = "Disabled"; 1772029573d4SEd Tanous } 1773aa05fb27SJohnathan Mantey 1774*2c70f800SEd Tanous jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown"; 1775*2c70f800SEd Tanous jsonResponse["SpeedMbps"] = ethData.speed; 1776*2c70f800SEd Tanous jsonResponse["MACAddress"] = ethData.mac_address; 1777*2c70f800SEd Tanous jsonResponse["DHCPv4"]["DHCPEnabled"] = 17781f8c7b5dSJohnathan Mantey translateDHCPEnabledToBool(ethData.DHCPEnabled, true); 1779*2c70f800SEd Tanous jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled; 1780*2c70f800SEd Tanous jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled; 1781*2c70f800SEd Tanous jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled; 17821f8c7b5dSJohnathan Mantey 1783*2c70f800SEd Tanous jsonResponse["DHCPv6"]["OperatingMode"] = 17841f8c7b5dSJohnathan Mantey translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful" 17851f8c7b5dSJohnathan Mantey : "Disabled"; 1786*2c70f800SEd Tanous jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled; 1787*2c70f800SEd Tanous jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled; 1788*2c70f800SEd Tanous jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled; 17892a133282Smanojkiraneda 17904a0cb85cSEd Tanous if (!ethData.hostname.empty()) 17914a0cb85cSEd Tanous { 1792*2c70f800SEd Tanous jsonResponse["HostName"] = ethData.hostname; 1793ab6554f1SJoshi-Mansi 1794ab6554f1SJoshi-Mansi // When domain name is empty then it means, that it is a network 1795ab6554f1SJoshi-Mansi // without domain names, and the host name itself must be treated as 1796ab6554f1SJoshi-Mansi // FQDN 1797*2c70f800SEd Tanous std::string fqdn = std::move(ethData.hostname); 1798d24bfc7aSJennifer Lee if (!ethData.domainnames.empty()) 1799d24bfc7aSJennifer Lee { 1800*2c70f800SEd Tanous fqdn += "." + ethData.domainnames[0]; 1801d24bfc7aSJennifer Lee } 1802*2c70f800SEd Tanous jsonResponse["FQDN"] = fqdn; 18034a0cb85cSEd Tanous } 18044a0cb85cSEd Tanous 1805*2c70f800SEd Tanous jsonResponse["VLANs"] = { 1806fda13ad2SSunitha Harish {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1807fda13ad2SSunitha Harish iface_id + "/VLANs"}}; 1808fda13ad2SSunitha Harish 1809*2c70f800SEd Tanous jsonResponse["NameServers"] = ethData.nameServers; 1810*2c70f800SEd Tanous jsonResponse["StaticNameServers"] = ethData.staticNameServers; 18114a0cb85cSEd Tanous 1812*2c70f800SEd Tanous nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; 1813*2c70f800SEd Tanous nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; 1814*2c70f800SEd Tanous ipv4Array = nlohmann::json::array(); 1815*2c70f800SEd Tanous ipv4StaticArray = nlohmann::json::array(); 1816*2c70f800SEd Tanous for (auto& ipv4Config : ipv4Data) 18174a0cb85cSEd Tanous { 1818fa5053a6SGunnar Mills 1819*2c70f800SEd Tanous std::string gatewayStr = ipv4Config.gateway; 1820fa5053a6SGunnar Mills if (gatewayStr.empty()) 1821fa5053a6SGunnar Mills { 1822fa5053a6SGunnar Mills gatewayStr = "0.0.0.0"; 1823fa5053a6SGunnar Mills } 1824fa5053a6SGunnar Mills 1825*2c70f800SEd Tanous ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin}, 1826*2c70f800SEd Tanous {"SubnetMask", ipv4Config.netmask}, 1827*2c70f800SEd Tanous {"Address", ipv4Config.address}, 1828fa5053a6SGunnar Mills {"Gateway", gatewayStr}}); 1829*2c70f800SEd Tanous if (ipv4Config.origin == "Static") 1830d1d50814SRavi Teja { 1831*2c70f800SEd Tanous ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin}, 1832*2c70f800SEd Tanous {"SubnetMask", ipv4Config.netmask}, 1833*2c70f800SEd Tanous {"Address", ipv4Config.address}, 1834d1d50814SRavi Teja {"Gateway", gatewayStr}}); 1835d1d50814SRavi Teja } 183601784826SJohnathan Mantey } 1837d1d50814SRavi Teja 1838*2c70f800SEd Tanous jsonResponse["IPv6DefaultGateway"] = ethData.ipv6_default_gateway; 1839e48c0fc5SRavi Teja 1840*2c70f800SEd Tanous nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"]; 1841*2c70f800SEd Tanous nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"]; 1842*2c70f800SEd Tanous ipv6Array = nlohmann::json::array(); 1843*2c70f800SEd Tanous ipv6StaticArray = nlohmann::json::array(); 18447f2e23e9SJohnathan Mantey nlohmann::json& ipv6AddrPolicyTable = 1845*2c70f800SEd Tanous jsonResponse["IPv6AddressPolicyTable"]; 18467f2e23e9SJohnathan Mantey ipv6AddrPolicyTable = nlohmann::json::array(); 1847*2c70f800SEd Tanous for (auto& ipv6Config : ipv6Data) 1848e48c0fc5SRavi Teja { 1849*2c70f800SEd Tanous ipv6Array.push_back({{"Address", ipv6Config.address}, 1850*2c70f800SEd Tanous {"PrefixLength", ipv6Config.prefixLength}, 1851*2c70f800SEd Tanous {"AddressOrigin", ipv6Config.origin}, 18525fd16e4bSJohnathan Mantey {"AddressState", nullptr}}); 1853*2c70f800SEd Tanous if (ipv6Config.origin == "Static") 1854e48c0fc5SRavi Teja { 1855*2c70f800SEd Tanous ipv6StaticArray.push_back( 1856*2c70f800SEd Tanous {{"Address", ipv6Config.address}, 1857*2c70f800SEd Tanous {"PrefixLength", ipv6Config.prefixLength}, 1858*2c70f800SEd Tanous {"AddressOrigin", ipv6Config.origin}, 18595fd16e4bSJohnathan Mantey {"AddressState", nullptr}}); 186001784826SJohnathan Mantey } 1861e48c0fc5SRavi Teja } 1862588c3f0dSKowalski, Kamil } 1863588c3f0dSKowalski, Kamil 18649391bb9cSRapkiewicz, Pawel /** 18659391bb9cSRapkiewicz, Pawel * Functions triggers appropriate requests on DBus 18669391bb9cSRapkiewicz, Pawel */ 1867cb13a392SEd Tanous void doGet(crow::Response& res, const crow::Request&, 18681abe55efSEd Tanous const std::vector<std::string>& params) override 18691abe55efSEd Tanous { 18704a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 18711abe55efSEd Tanous if (params.size() != 1) 18721abe55efSEd Tanous { 1873f12894f8SJason M. Bills messages::internalError(asyncResp->res); 18749391bb9cSRapkiewicz, Pawel return; 18759391bb9cSRapkiewicz, Pawel } 18769391bb9cSRapkiewicz, Pawel 18774a0cb85cSEd Tanous getEthernetIfaceData( 18784a0cb85cSEd Tanous params[0], 1879*2c70f800SEd Tanous [this, asyncResp, ifaceId{std::string(params[0])}]( 18804a0cb85cSEd Tanous const bool& success, const EthernetInterfaceData& ethData, 1881e48c0fc5SRavi Teja const boost::container::flat_set<IPv4AddressData>& ipv4Data, 188201784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data) { 18834a0cb85cSEd Tanous if (!success) 18841abe55efSEd Tanous { 18851abe55efSEd Tanous // TODO(Pawel)consider distinguish between non existing 18861abe55efSEd Tanous // object, and other errors 1887f12894f8SJason M. Bills messages::resourceNotFound(asyncResp->res, 1888*2c70f800SEd Tanous "EthernetInterface", ifaceId); 18894a0cb85cSEd Tanous return; 18909391bb9cSRapkiewicz, Pawel } 18914c9afe43SEd Tanous 18920f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1893fda13ad2SSunitha Harish "#EthernetInterface.v1_4_1.EthernetInterface"; 18940f74e643SEd Tanous asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface"; 18950f74e643SEd Tanous asyncResp->res.jsonValue["Description"] = 18960f74e643SEd Tanous "Management Network Interface"; 18970f74e643SEd Tanous 1898*2c70f800SEd Tanous parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, 1899eeedda23SJohnathan Mantey ipv6Data); 19009391bb9cSRapkiewicz, Pawel }); 19019391bb9cSRapkiewicz, Pawel } 19029391bb9cSRapkiewicz, Pawel 190355c7b7a2SEd Tanous void doPatch(crow::Response& res, const crow::Request& req, 19041abe55efSEd Tanous const std::vector<std::string>& params) override 19051abe55efSEd Tanous { 19064a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 19071abe55efSEd Tanous if (params.size() != 1) 19081abe55efSEd Tanous { 1909f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1910588c3f0dSKowalski, Kamil return; 1911588c3f0dSKowalski, Kamil } 1912588c3f0dSKowalski, Kamil 1913*2c70f800SEd Tanous const std::string& ifaceId = params[0]; 1914588c3f0dSKowalski, Kamil 1915bc0bd6e0SEd Tanous std::optional<std::string> hostname; 1916ab6554f1SJoshi-Mansi std::optional<std::string> fqdn; 1917d577665bSRatan Gupta std::optional<std::string> macAddress; 19189a6fc6feSRavi Teja std::optional<std::string> ipv6DefaultGateway; 1919d1d50814SRavi Teja std::optional<nlohmann::json> ipv4StaticAddresses; 1920e48c0fc5SRavi Teja std::optional<nlohmann::json> ipv6StaticAddresses; 1921f85837bfSRAJESWARAN THILLAIGOVINDAN std::optional<std::vector<std::string>> staticNameServers; 1922da131a9aSJennifer Lee std::optional<nlohmann::json> dhcpv4; 19231f8c7b5dSJohnathan Mantey std::optional<nlohmann::json> dhcpv6; 1924eeedda23SJohnathan Mantey std::optional<bool> interfaceEnabled; 19251f8c7b5dSJohnathan Mantey DHCPParameters v4dhcpParms; 19261f8c7b5dSJohnathan Mantey DHCPParameters v6dhcpParms; 19270627a2c7SEd Tanous 19281f8c7b5dSJohnathan Mantey if (!json_util::readJson( 1929ab6554f1SJoshi-Mansi req, res, "HostName", hostname, "FQDN", fqdn, 1930ab6554f1SJoshi-Mansi "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress", 1931ab6554f1SJoshi-Mansi macAddress, "StaticNameServers", staticNameServers, 1932ab6554f1SJoshi-Mansi "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses", 1933ab6554f1SJoshi-Mansi ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6, 1934ab6554f1SJoshi-Mansi "InterfaceEnabled", interfaceEnabled)) 19351abe55efSEd Tanous { 1936588c3f0dSKowalski, Kamil return; 1937588c3f0dSKowalski, Kamil } 1938da131a9aSJennifer Lee if (dhcpv4) 1939da131a9aSJennifer Lee { 19401f8c7b5dSJohnathan Mantey if (!json_util::readJson(*dhcpv4, res, "DHCPEnabled", 19411f8c7b5dSJohnathan Mantey v4dhcpParms.dhcpv4Enabled, "UseDNSServers", 19421f8c7b5dSJohnathan Mantey v4dhcpParms.useDNSServers, "UseNTPServers", 19431f8c7b5dSJohnathan Mantey v4dhcpParms.useNTPServers, "UseDomainName", 19441f8c7b5dSJohnathan Mantey v4dhcpParms.useUseDomainName)) 19451f8c7b5dSJohnathan Mantey { 19461f8c7b5dSJohnathan Mantey return; 19471f8c7b5dSJohnathan Mantey } 19481f8c7b5dSJohnathan Mantey } 19491f8c7b5dSJohnathan Mantey 19501f8c7b5dSJohnathan Mantey if (dhcpv6) 19511f8c7b5dSJohnathan Mantey { 19521f8c7b5dSJohnathan Mantey if (!json_util::readJson(*dhcpv6, res, "OperatingMode", 19531f8c7b5dSJohnathan Mantey v6dhcpParms.dhcpv6OperatingMode, 19541f8c7b5dSJohnathan Mantey "UseDNSServers", v6dhcpParms.useDNSServers, 19551f8c7b5dSJohnathan Mantey "UseNTPServers", v6dhcpParms.useNTPServers, 19561f8c7b5dSJohnathan Mantey "UseDomainName", 19571f8c7b5dSJohnathan Mantey v6dhcpParms.useUseDomainName)) 19581f8c7b5dSJohnathan Mantey { 19591f8c7b5dSJohnathan Mantey return; 19601f8c7b5dSJohnathan Mantey } 1961da131a9aSJennifer Lee } 1962da131a9aSJennifer Lee 196301784826SJohnathan Mantey // Get single eth interface data, and call the below callback for 196401784826SJohnathan Mantey // JSON preparation 19654a0cb85cSEd Tanous getEthernetIfaceData( 1966*2c70f800SEd Tanous ifaceId, 1967*2c70f800SEd Tanous [this, asyncResp, ifaceId, hostname = std::move(hostname), 1968ab6554f1SJoshi-Mansi fqdn = std::move(fqdn), macAddress = std::move(macAddress), 1969d1d50814SRavi Teja ipv4StaticAddresses = std::move(ipv4StaticAddresses), 19709a6fc6feSRavi Teja ipv6DefaultGateway = std::move(ipv6DefaultGateway), 1971e48c0fc5SRavi Teja ipv6StaticAddresses = std::move(ipv6StaticAddresses), 19721f8c7b5dSJohnathan Mantey staticNameServers = std::move(staticNameServers), 19731f8c7b5dSJohnathan Mantey dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), 19741f8c7b5dSJohnathan Mantey v4dhcpParms = std::move(v4dhcpParms), 1975eeedda23SJohnathan Mantey v6dhcpParms = std::move(v6dhcpParms), 1976eeedda23SJohnathan Mantey interfaceEnabled = std::move(interfaceEnabled)]( 19774a0cb85cSEd Tanous const bool& success, const EthernetInterfaceData& ethData, 1978e48c0fc5SRavi Teja const boost::container::flat_set<IPv4AddressData>& ipv4Data, 197901784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data) { 19801abe55efSEd Tanous if (!success) 19811abe55efSEd Tanous { 1982588c3f0dSKowalski, Kamil // ... otherwise return error 19831abe55efSEd Tanous // TODO(Pawel)consider distinguish between non existing 19841abe55efSEd Tanous // object, and other errors 1985fda13ad2SSunitha Harish messages::resourceNotFound(asyncResp->res, 1986*2c70f800SEd Tanous "Ethernet Interface", ifaceId); 1987588c3f0dSKowalski, Kamil return; 1988588c3f0dSKowalski, Kamil } 1989588c3f0dSKowalski, Kamil 19901f8c7b5dSJohnathan Mantey if (dhcpv4 || dhcpv6) 19911f8c7b5dSJohnathan Mantey { 1992*2c70f800SEd Tanous handleDHCPPatch(ifaceId, ethData, std::move(v4dhcpParms), 19931f8c7b5dSJohnathan Mantey std::move(v6dhcpParms), asyncResp); 19941f8c7b5dSJohnathan Mantey } 19951f8c7b5dSJohnathan Mantey 19960627a2c7SEd Tanous if (hostname) 19971abe55efSEd Tanous { 19980627a2c7SEd Tanous handleHostnamePatch(*hostname, asyncResp); 19991abe55efSEd Tanous } 20000627a2c7SEd Tanous 2001ab6554f1SJoshi-Mansi if (fqdn) 2002ab6554f1SJoshi-Mansi { 2003*2c70f800SEd Tanous handleFqdnPatch(ifaceId, *fqdn, asyncResp); 2004ab6554f1SJoshi-Mansi } 2005ab6554f1SJoshi-Mansi 2006d577665bSRatan Gupta if (macAddress) 2007d577665bSRatan Gupta { 2008*2c70f800SEd Tanous handleMACAddressPatch(ifaceId, *macAddress, asyncResp); 2009d577665bSRatan Gupta } 2010d577665bSRatan Gupta 2011d1d50814SRavi Teja if (ipv4StaticAddresses) 2012d1d50814SRavi Teja { 2013537174c4SEd Tanous // TODO(ed) for some reason the capture of ipv4Addresses 201401784826SJohnathan Mantey // above is returning a const value, not a non-const 201501784826SJohnathan Mantey // value. This doesn't really work for us, as we need to 201601784826SJohnathan Mantey // be able to efficiently move out the intermedia 201701784826SJohnathan Mantey // nlohmann::json objects. This makes a copy of the 201801784826SJohnathan Mantey // structure, and operates on that, but could be done 201901784826SJohnathan Mantey // more efficiently 2020d1d50814SRavi Teja nlohmann::json ipv4Static = std::move(*ipv4StaticAddresses); 2021*2c70f800SEd Tanous handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, 2022d1d50814SRavi Teja asyncResp); 20231abe55efSEd Tanous } 20240627a2c7SEd Tanous 2025f85837bfSRAJESWARAN THILLAIGOVINDAN if (staticNameServers) 2026f85837bfSRAJESWARAN THILLAIGOVINDAN { 2027*2c70f800SEd Tanous handleStaticNameServersPatch(ifaceId, *staticNameServers, 2028f85837bfSRAJESWARAN THILLAIGOVINDAN asyncResp); 2029f85837bfSRAJESWARAN THILLAIGOVINDAN } 20309a6fc6feSRavi Teja 20319a6fc6feSRavi Teja if (ipv6DefaultGateway) 20329a6fc6feSRavi Teja { 20339a6fc6feSRavi Teja messages::propertyNotWritable(asyncResp->res, 20349a6fc6feSRavi Teja "IPv6DefaultGateway"); 20359a6fc6feSRavi Teja } 2036e48c0fc5SRavi Teja 2037e48c0fc5SRavi Teja if (ipv6StaticAddresses) 2038e48c0fc5SRavi Teja { 2039e48c0fc5SRavi Teja nlohmann::json ipv6Static = std::move(*ipv6StaticAddresses); 2040*2c70f800SEd Tanous handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, 204101784826SJohnathan Mantey ipv6Data, asyncResp); 2042e48c0fc5SRavi Teja } 2043eeedda23SJohnathan Mantey 2044eeedda23SJohnathan Mantey if (interfaceEnabled) 2045eeedda23SJohnathan Mantey { 2046eeedda23SJohnathan Mantey setEthernetInterfaceBoolProperty( 2047*2c70f800SEd Tanous ifaceId, "NICEnabled", *interfaceEnabled, asyncResp); 2048eeedda23SJohnathan Mantey } 2049588c3f0dSKowalski, Kamil }); 2050588c3f0dSKowalski, Kamil } 20519391bb9cSRapkiewicz, Pawel }; 20529391bb9cSRapkiewicz, Pawel 2053e439f0f8SKowalski, Kamil /** 20544a0cb85cSEd Tanous * VlanNetworkInterface derived class for delivering VLANNetworkInterface 20554a0cb85cSEd Tanous * Schema 2056e439f0f8SKowalski, Kamil */ 20571abe55efSEd Tanous class VlanNetworkInterface : public Node 20581abe55efSEd Tanous { 2059e439f0f8SKowalski, Kamil public: 2060e439f0f8SKowalski, Kamil /* 2061e439f0f8SKowalski, Kamil * Default Constructor 2062e439f0f8SKowalski, Kamil */ 206352cc112dSEd Tanous VlanNetworkInterface(App& app) : 20644a0cb85cSEd Tanous Node(app, 20657af91514SGunnar Mills "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/", 20661abe55efSEd Tanous std::string(), std::string()) 20671abe55efSEd Tanous { 2068e439f0f8SKowalski, Kamil entityPrivileges = { 2069e439f0f8SKowalski, Kamil {boost::beast::http::verb::get, {{"Login"}}}, 2070e439f0f8SKowalski, Kamil {boost::beast::http::verb::head, {{"Login"}}}, 2071e439f0f8SKowalski, Kamil {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 2072e439f0f8SKowalski, Kamil {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 2073e439f0f8SKowalski, Kamil {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 2074e439f0f8SKowalski, Kamil {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 2075e439f0f8SKowalski, Kamil } 2076e439f0f8SKowalski, Kamil 2077e439f0f8SKowalski, Kamil private: 2078cb13a392SEd Tanous void parseInterfaceData(nlohmann::json& json_response, 2079cb13a392SEd Tanous const std::string& parent_iface_id, 2080cb13a392SEd Tanous const std::string& iface_id, 2081cb13a392SEd Tanous const EthernetInterfaceData& ethData) 20821abe55efSEd Tanous { 2083e439f0f8SKowalski, Kamil // Fill out obvious data... 20844a0cb85cSEd Tanous json_response["Id"] = iface_id; 20854a0cb85cSEd Tanous json_response["@odata.id"] = 20864a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id + 20874a0cb85cSEd Tanous "/VLANs/" + iface_id; 2088e439f0f8SKowalski, Kamil 20894a0cb85cSEd Tanous json_response["VLANEnable"] = true; 2090fda13ad2SSunitha Harish if (!ethData.vlan_id.empty()) 20914a0cb85cSEd Tanous { 2092fda13ad2SSunitha Harish json_response["VLANId"] = ethData.vlan_id.back(); 20934a0cb85cSEd Tanous } 2094e439f0f8SKowalski, Kamil } 2095e439f0f8SKowalski, Kamil 2096fda13ad2SSunitha Harish bool verifyNames(const std::string& parent, const std::string& iface) 20971abe55efSEd Tanous { 20981abe55efSEd Tanous if (!boost::starts_with(iface, parent + "_")) 20991abe55efSEd Tanous { 2100927a505aSKowalski, Kamil return false; 21011abe55efSEd Tanous } 21021abe55efSEd Tanous else 21031abe55efSEd Tanous { 2104927a505aSKowalski, Kamil return true; 2105927a505aSKowalski, Kamil } 2106927a505aSKowalski, Kamil } 2107927a505aSKowalski, Kamil 2108e439f0f8SKowalski, Kamil /** 2109e439f0f8SKowalski, Kamil * Functions triggers appropriate requests on DBus 2110e439f0f8SKowalski, Kamil */ 2111cb13a392SEd Tanous void doGet(crow::Response& res, const crow::Request&, 21121abe55efSEd Tanous const std::vector<std::string>& params) override 21131abe55efSEd Tanous { 21144a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 21154a0cb85cSEd Tanous // TODO(Pawel) this shall be parameterized call (two params) to get 2116e439f0f8SKowalski, Kamil // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'. 2117e439f0f8SKowalski, Kamil // Check if there is required param, truly entering this shall be 2118e439f0f8SKowalski, Kamil // impossible. 21191abe55efSEd Tanous if (params.size() != 2) 21201abe55efSEd Tanous { 2121f12894f8SJason M. Bills messages::internalError(res); 2122e439f0f8SKowalski, Kamil res.end(); 2123e439f0f8SKowalski, Kamil return; 2124e439f0f8SKowalski, Kamil } 2125e439f0f8SKowalski, Kamil 2126*2c70f800SEd Tanous const std::string& parentIfaceId = params[0]; 2127*2c70f800SEd Tanous const std::string& ifaceId = params[1]; 21280f74e643SEd Tanous res.jsonValue["@odata.type"] = 21290f74e643SEd Tanous "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; 21300f74e643SEd Tanous res.jsonValue["Name"] = "VLAN Network Interface"; 2131e439f0f8SKowalski, Kamil 2132*2c70f800SEd Tanous if (!verifyNames(parentIfaceId, ifaceId)) 21331abe55efSEd Tanous { 2134a434f2bdSEd Tanous return; 2135a434f2bdSEd Tanous } 2136a434f2bdSEd Tanous 213701784826SJohnathan Mantey // Get single eth interface data, and call the below callback for 213801784826SJohnathan Mantey // JSON preparation 21394a0cb85cSEd Tanous getEthernetIfaceData( 2140fda13ad2SSunitha Harish params[1], 2141*2c70f800SEd Tanous [this, asyncResp, parentIfaceId{std::string(params[0])}, 2142*2c70f800SEd Tanous ifaceId{std::string(params[1])}]( 21434a0cb85cSEd Tanous const bool& success, const EthernetInterfaceData& ethData, 2144cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2145cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 2146fda13ad2SSunitha Harish if (success && ethData.vlan_id.size() != 0) 21471abe55efSEd Tanous { 2148*2c70f800SEd Tanous parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId, 2149*2c70f800SEd Tanous ifaceId, ethData); 21501abe55efSEd Tanous } 21511abe55efSEd Tanous else 21521abe55efSEd Tanous { 2153e439f0f8SKowalski, Kamil // ... otherwise return error 21541abe55efSEd Tanous // TODO(Pawel)consider distinguish between non existing 21551abe55efSEd Tanous // object, and other errors 2156f12894f8SJason M. Bills messages::resourceNotFound( 2157*2c70f800SEd Tanous asyncResp->res, "VLAN Network Interface", ifaceId); 2158e439f0f8SKowalski, Kamil } 2159e439f0f8SKowalski, Kamil }); 2160e439f0f8SKowalski, Kamil } 2161e439f0f8SKowalski, Kamil 216255c7b7a2SEd Tanous void doPatch(crow::Response& res, const crow::Request& req, 21631abe55efSEd Tanous const std::vector<std::string>& params) override 21641abe55efSEd Tanous { 21654a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 21661abe55efSEd Tanous if (params.size() != 2) 21671abe55efSEd Tanous { 2168f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2169e439f0f8SKowalski, Kamil return; 2170e439f0f8SKowalski, Kamil } 2171e439f0f8SKowalski, Kamil 2172d76323e5SEd Tanous const std::string& parentIfaceId = params[0]; 217355c7b7a2SEd Tanous const std::string& ifaceId = params[1]; 2174927a505aSKowalski, Kamil 2175fda13ad2SSunitha Harish if (!verifyNames(parentIfaceId, ifaceId)) 21761abe55efSEd Tanous { 2177fda13ad2SSunitha Harish messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", 2178fda13ad2SSunitha Harish ifaceId); 2179927a505aSKowalski, Kamil return; 2180927a505aSKowalski, Kamil } 2181927a505aSKowalski, Kamil 21820627a2c7SEd Tanous bool vlanEnable = false; 218338268fa8SAndrew Geissler uint32_t vlanId = 0; 21840627a2c7SEd Tanous 21850627a2c7SEd Tanous if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId", 21860627a2c7SEd Tanous vlanId)) 21871abe55efSEd Tanous { 2188927a505aSKowalski, Kamil return; 2189927a505aSKowalski, Kamil } 2190927a505aSKowalski, Kamil 219101784826SJohnathan Mantey // Get single eth interface data, and call the below callback for 219201784826SJohnathan Mantey // JSON preparation 2193e48c0fc5SRavi Teja getEthernetIfaceData( 2194e48c0fc5SRavi Teja params[1], 2195271584abSEd Tanous [asyncResp, parentIfaceId{std::string(params[0])}, 2196cb13a392SEd Tanous ifaceId{std::string(params[1])}, &vlanEnable, 2197cb13a392SEd Tanous &vlanId](const bool& success, const EthernetInterfaceData& ethData, 2198cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2199cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 220008244d02SSunitha Harish if (success && !ethData.vlan_id.empty()) 220108244d02SSunitha Harish { 220208244d02SSunitha Harish auto callback = 220308244d02SSunitha Harish [asyncResp](const boost::system::error_code ec) { 220408244d02SSunitha Harish if (ec) 220508244d02SSunitha Harish { 220608244d02SSunitha Harish messages::internalError(asyncResp->res); 220708244d02SSunitha Harish } 220808244d02SSunitha Harish }; 220908244d02SSunitha Harish 221008244d02SSunitha Harish if (vlanEnable == true) 221108244d02SSunitha Harish { 221208244d02SSunitha Harish crow::connections::systemBus->async_method_call( 221308244d02SSunitha Harish std::move(callback), "xyz.openbmc_project.Network", 221408244d02SSunitha Harish "/xyz/openbmc_project/network/" + ifaceId, 221508244d02SSunitha Harish "org.freedesktop.DBus.Properties", "Set", 221608244d02SSunitha Harish "xyz.openbmc_project.Network.VLAN", "Id", 221708244d02SSunitha Harish std::variant<uint32_t>(vlanId)); 221808244d02SSunitha Harish } 221908244d02SSunitha Harish else 222008244d02SSunitha Harish { 2221e48c0fc5SRavi Teja BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the " 2222e48c0fc5SRavi Teja "vlan interface"; 222308244d02SSunitha Harish crow::connections::systemBus->async_method_call( 222408244d02SSunitha Harish std::move(callback), "xyz.openbmc_project.Network", 2225e48c0fc5SRavi Teja std::string("/xyz/openbmc_project/network/") + 2226e48c0fc5SRavi Teja ifaceId, 222708244d02SSunitha Harish "xyz.openbmc_project.Object.Delete", "Delete"); 222808244d02SSunitha Harish } 222908244d02SSunitha Harish } 223008244d02SSunitha Harish else 22311abe55efSEd Tanous { 22321abe55efSEd Tanous // TODO(Pawel)consider distinguish between non existing 22331abe55efSEd Tanous // object, and other errors 2234e48c0fc5SRavi Teja messages::resourceNotFound( 2235e48c0fc5SRavi Teja asyncResp->res, "VLAN Network Interface", ifaceId); 2236927a505aSKowalski, Kamil return; 2237927a505aSKowalski, Kamil } 2238927a505aSKowalski, Kamil }); 2239e439f0f8SKowalski, Kamil } 2240e439f0f8SKowalski, Kamil 2241cb13a392SEd Tanous void doDelete(crow::Response& res, const crow::Request&, 22421abe55efSEd Tanous const std::vector<std::string>& params) override 22431abe55efSEd Tanous { 22444a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 22451abe55efSEd Tanous if (params.size() != 2) 22461abe55efSEd Tanous { 2247f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2248e439f0f8SKowalski, Kamil return; 2249e439f0f8SKowalski, Kamil } 2250e439f0f8SKowalski, Kamil 2251d76323e5SEd Tanous const std::string& parentIfaceId = params[0]; 225255c7b7a2SEd Tanous const std::string& ifaceId = params[1]; 2253927a505aSKowalski, Kamil 2254fda13ad2SSunitha Harish if (!verifyNames(parentIfaceId, ifaceId)) 22551abe55efSEd Tanous { 2256fda13ad2SSunitha Harish messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", 2257fda13ad2SSunitha Harish ifaceId); 2258927a505aSKowalski, Kamil return; 2259927a505aSKowalski, Kamil } 2260927a505aSKowalski, Kamil 226101784826SJohnathan Mantey // Get single eth interface data, and call the below callback for 226201784826SJohnathan Mantey // JSON preparation 2263f12894f8SJason M. Bills getEthernetIfaceData( 2264fda13ad2SSunitha Harish params[1], 2265271584abSEd Tanous [asyncResp, parentIfaceId{std::string(params[0])}, 2266fda13ad2SSunitha Harish ifaceId{std::string(params[1])}]( 2267f12894f8SJason M. Bills const bool& success, const EthernetInterfaceData& ethData, 2268cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2269cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 2270fda13ad2SSunitha Harish if (success && !ethData.vlan_id.empty()) 22711abe55efSEd Tanous { 2272f12894f8SJason M. Bills auto callback = 2273f12894f8SJason M. Bills [asyncResp](const boost::system::error_code ec) { 22741abe55efSEd Tanous if (ec) 22751abe55efSEd Tanous { 2276f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2277927a505aSKowalski, Kamil } 22784a0cb85cSEd Tanous }; 22794a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 22804a0cb85cSEd Tanous std::move(callback), "xyz.openbmc_project.Network", 22814a0cb85cSEd Tanous std::string("/xyz/openbmc_project/network/") + ifaceId, 22824a0cb85cSEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 22831abe55efSEd Tanous } 22841abe55efSEd Tanous else 22851abe55efSEd Tanous { 2286927a505aSKowalski, Kamil // ... otherwise return error 2287f12894f8SJason M. Bills // TODO(Pawel)consider distinguish between non existing 2288f12894f8SJason M. Bills // object, and other errors 2289f12894f8SJason M. Bills messages::resourceNotFound( 2290f12894f8SJason M. Bills asyncResp->res, "VLAN Network Interface", ifaceId); 2291927a505aSKowalski, Kamil } 2292927a505aSKowalski, Kamil }); 2293e439f0f8SKowalski, Kamil } 2294e439f0f8SKowalski, Kamil }; 2295e439f0f8SKowalski, Kamil 2296e439f0f8SKowalski, Kamil /** 2297e439f0f8SKowalski, Kamil * VlanNetworkInterfaceCollection derived class for delivering 2298e439f0f8SKowalski, Kamil * VLANNetworkInterface Collection Schema 2299e439f0f8SKowalski, Kamil */ 23001abe55efSEd Tanous class VlanNetworkInterfaceCollection : public Node 23011abe55efSEd Tanous { 2302e439f0f8SKowalski, Kamil public: 230352cc112dSEd Tanous VlanNetworkInterfaceCollection(App& app) : 23044a0cb85cSEd Tanous Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/", 23054a0cb85cSEd Tanous std::string()) 23061abe55efSEd Tanous { 2307e439f0f8SKowalski, Kamil entityPrivileges = { 2308e439f0f8SKowalski, Kamil {boost::beast::http::verb::get, {{"Login"}}}, 2309e439f0f8SKowalski, Kamil {boost::beast::http::verb::head, {{"Login"}}}, 2310e439f0f8SKowalski, Kamil {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 2311e439f0f8SKowalski, Kamil {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 2312e439f0f8SKowalski, Kamil {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 2313e439f0f8SKowalski, Kamil {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 2314e439f0f8SKowalski, Kamil } 2315e439f0f8SKowalski, Kamil 2316e439f0f8SKowalski, Kamil private: 2317e439f0f8SKowalski, Kamil /** 2318e439f0f8SKowalski, Kamil * Functions triggers appropriate requests on DBus 2319e439f0f8SKowalski, Kamil */ 2320cb13a392SEd Tanous void doGet(crow::Response& res, const crow::Request&, 23211abe55efSEd Tanous const std::vector<std::string>& params) override 23221abe55efSEd Tanous { 23234a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 23241abe55efSEd Tanous if (params.size() != 1) 23251abe55efSEd Tanous { 2326e439f0f8SKowalski, Kamil // This means there is a problem with the router 2327f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2328e439f0f8SKowalski, Kamil return; 2329e439f0f8SKowalski, Kamil } 2330e439f0f8SKowalski, Kamil 23314a0cb85cSEd Tanous const std::string& rootInterfaceName = params[0]; 2332e439f0f8SKowalski, Kamil 23334a0cb85cSEd Tanous // Get eth interface list, and call the below callback for JSON 23341abe55efSEd Tanous // preparation 2335f12894f8SJason M. Bills getEthernetIfaceList( 233643b761d0SEd Tanous [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}]( 23371abe55efSEd Tanous const bool& success, 23384c9afe43SEd Tanous const boost::container::flat_set<std::string>& iface_list) { 23394a0cb85cSEd Tanous if (!success) 23401abe55efSEd Tanous { 2341f12894f8SJason M. Bills messages::internalError(asyncResp->res); 23424a0cb85cSEd Tanous return; 23431abe55efSEd Tanous } 23444c9afe43SEd Tanous 23454c9afe43SEd Tanous if (iface_list.find(rootInterfaceName) == iface_list.end()) 23464c9afe43SEd Tanous { 23474c9afe43SEd Tanous messages::resourceNotFound(asyncResp->res, 23484c9afe43SEd Tanous "VLanNetworkInterfaceCollection", 23494c9afe43SEd Tanous rootInterfaceName); 23504c9afe43SEd Tanous return; 23514c9afe43SEd Tanous } 23524c9afe43SEd Tanous 23530f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 23540f74e643SEd Tanous "#VLanNetworkInterfaceCollection." 23550f74e643SEd Tanous "VLanNetworkInterfaceCollection"; 23560f74e643SEd Tanous asyncResp->res.jsonValue["Name"] = 23570f74e643SEd Tanous "VLAN Network Interface Collection"; 23584a0cb85cSEd Tanous 2359*2c70f800SEd Tanous nlohmann::json ifaceArray = nlohmann::json::array(); 23604a0cb85cSEd Tanous 2361*2c70f800SEd Tanous for (const std::string& ifaceItem : iface_list) 23621abe55efSEd Tanous { 2363*2c70f800SEd Tanous if (boost::starts_with(ifaceItem, rootInterfaceName + "_")) 23644a0cb85cSEd Tanous { 2365*2c70f800SEd Tanous ifaceArray.push_back( 23664a0cb85cSEd Tanous {{"@odata.id", 23674a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 2368*2c70f800SEd Tanous rootInterfaceName + "/VLANs/" + ifaceItem}}); 2369e439f0f8SKowalski, Kamil } 2370e439f0f8SKowalski, Kamil } 2371e439f0f8SKowalski, Kamil 23724a0cb85cSEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 2373*2c70f800SEd Tanous ifaceArray.size(); 2374*2c70f800SEd Tanous asyncResp->res.jsonValue["Members"] = std::move(ifaceArray); 23754a0cb85cSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 23764a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 23774a0cb85cSEd Tanous rootInterfaceName + "/VLANs"; 2378e439f0f8SKowalski, Kamil }); 2379e439f0f8SKowalski, Kamil } 2380e439f0f8SKowalski, Kamil 238155c7b7a2SEd Tanous void doPost(crow::Response& res, const crow::Request& req, 23821abe55efSEd Tanous const std::vector<std::string>& params) override 23831abe55efSEd Tanous { 23844a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 23851abe55efSEd Tanous if (params.size() != 1) 23861abe55efSEd Tanous { 2387f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2388e439f0f8SKowalski, Kamil return; 2389e439f0f8SKowalski, Kamil } 2390fda13ad2SSunitha Harish bool vlanEnable = false; 23910627a2c7SEd Tanous uint32_t vlanId = 0; 2392fda13ad2SSunitha Harish if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable", 2393fda13ad2SSunitha Harish vlanEnable)) 23941abe55efSEd Tanous { 23954a0cb85cSEd Tanous return; 2396e439f0f8SKowalski, Kamil } 2397fda13ad2SSunitha Harish // Need both vlanId and vlanEnable to service this request 2398fda13ad2SSunitha Harish if (!vlanId) 2399fda13ad2SSunitha Harish { 2400fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANId"); 2401fda13ad2SSunitha Harish } 2402fda13ad2SSunitha Harish if (!vlanEnable) 2403fda13ad2SSunitha Harish { 2404fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANEnable"); 2405fda13ad2SSunitha Harish } 2406271584abSEd Tanous if (static_cast<bool>(vlanId) ^ vlanEnable) 2407fda13ad2SSunitha Harish { 2408fda13ad2SSunitha Harish return; 2409fda13ad2SSunitha Harish } 2410fda13ad2SSunitha Harish 24114a0cb85cSEd Tanous const std::string& rootInterfaceName = params[0]; 24124a0cb85cSEd Tanous auto callback = [asyncResp](const boost::system::error_code ec) { 24131abe55efSEd Tanous if (ec) 24141abe55efSEd Tanous { 24154a0cb85cSEd Tanous // TODO(ed) make more consistent error messages based on 24164a0cb85cSEd Tanous // phosphor-network responses 2417f12894f8SJason M. Bills messages::internalError(asyncResp->res); 24184a0cb85cSEd Tanous return; 24191abe55efSEd Tanous } 2420f12894f8SJason M. Bills messages::created(asyncResp->res); 2421e439f0f8SKowalski, Kamil }; 24224a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 24234a0cb85cSEd Tanous std::move(callback), "xyz.openbmc_project.Network", 24244a0cb85cSEd Tanous "/xyz/openbmc_project/network", 24254a0cb85cSEd Tanous "xyz.openbmc_project.Network.VLAN.Create", "VLAN", 24260627a2c7SEd Tanous rootInterfaceName, vlanId); 24274a0cb85cSEd Tanous } 24284a0cb85cSEd Tanous }; 24299391bb9cSRapkiewicz, Pawel } // namespace redfish 2430