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> 23a24526dcSEd Tanous #include <optional> 24588c3f0dSKowalski, Kamil #include <utils/json_utils.hpp> 25abf2add6SEd Tanous #include <variant> 269391bb9cSRapkiewicz, Pawel 271abe55efSEd Tanous namespace redfish 281abe55efSEd Tanous { 299391bb9cSRapkiewicz, Pawel 309391bb9cSRapkiewicz, Pawel /** 319391bb9cSRapkiewicz, Pawel * DBus types primitives for several generic DBus interfaces 329391bb9cSRapkiewicz, Pawel * TODO(Pawel) consider move this to separate file into boost::dbus 339391bb9cSRapkiewicz, Pawel */ 34aa2e59c1SEd Tanous using PropertiesMapType = boost::container::flat_map< 35abf2add6SEd Tanous std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t, 36aa2e59c1SEd Tanous int32_t, uint32_t, int64_t, uint64_t, double>>; 379391bb9cSRapkiewicz, Pawel 384a0cb85cSEd Tanous using GetManagedObjects = std::vector<std::pair< 39aa2e59c1SEd Tanous sdbusplus::message::object_path, 404a0cb85cSEd Tanous std::vector<std::pair< 41aa2e59c1SEd Tanous std::string, 42aa2e59c1SEd Tanous boost::container::flat_map< 43029573d4SEd Tanous std::string, sdbusplus::message::variant< 44029573d4SEd Tanous std::string, bool, uint8_t, int16_t, uint16_t, 45029573d4SEd Tanous int32_t, uint32_t, int64_t, uint64_t, double, 46029573d4SEd Tanous std::vector<std::string>>>>>>>; 474a0cb85cSEd Tanous 484a0cb85cSEd Tanous enum class LinkType 494a0cb85cSEd Tanous { 504a0cb85cSEd Tanous Local, 514a0cb85cSEd Tanous Global 524a0cb85cSEd Tanous }; 539391bb9cSRapkiewicz, Pawel 549391bb9cSRapkiewicz, Pawel /** 559391bb9cSRapkiewicz, Pawel * Structure for keeping IPv4 data required by Redfish 569391bb9cSRapkiewicz, Pawel */ 571abe55efSEd Tanous struct IPv4AddressData 581abe55efSEd Tanous { 59179db1d7SKowalski, Kamil std::string id; 604a0cb85cSEd Tanous std::string address; 614a0cb85cSEd Tanous std::string domain; 624a0cb85cSEd Tanous std::string gateway; 639391bb9cSRapkiewicz, Pawel std::string netmask; 649391bb9cSRapkiewicz, Pawel std::string origin; 654a0cb85cSEd Tanous LinkType linktype; 664a0cb85cSEd Tanous 671abe55efSEd Tanous bool operator<(const IPv4AddressData &obj) const 681abe55efSEd Tanous { 694a0cb85cSEd Tanous return id < obj.id; 701abe55efSEd Tanous } 719391bb9cSRapkiewicz, Pawel }; 729391bb9cSRapkiewicz, Pawel 739391bb9cSRapkiewicz, Pawel /** 749391bb9cSRapkiewicz, Pawel * Structure for keeping basic single Ethernet Interface information 759391bb9cSRapkiewicz, Pawel * available from DBus 769391bb9cSRapkiewicz, Pawel */ 771abe55efSEd Tanous struct EthernetInterfaceData 781abe55efSEd Tanous { 794a0cb85cSEd Tanous uint32_t speed; 804a0cb85cSEd Tanous bool auto_neg; 812a133282Smanojkiraneda bool DHCPEnabled; 824a0cb85cSEd Tanous std::string hostname; 834a0cb85cSEd Tanous std::string default_gateway; 849a6fc6feSRavi Teja std::string ipv6_default_gateway; 854a0cb85cSEd Tanous std::string mac_address; 86fda13ad2SSunitha Harish std::vector<std::uint32_t> vlan_id; 87029573d4SEd Tanous std::vector<std::string> nameservers; 889391bb9cSRapkiewicz, Pawel }; 899391bb9cSRapkiewicz, Pawel 909391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24) 919391bb9cSRapkiewicz, Pawel // into full dot notation 921abe55efSEd Tanous inline std::string getNetmask(unsigned int bits) 931abe55efSEd Tanous { 949391bb9cSRapkiewicz, Pawel uint32_t value = 0xffffffff << (32 - bits); 959391bb9cSRapkiewicz, Pawel std::string netmask = std::to_string((value >> 24) & 0xff) + "." + 969391bb9cSRapkiewicz, Pawel std::to_string((value >> 16) & 0xff) + "." + 979391bb9cSRapkiewicz, Pawel std::to_string((value >> 8) & 0xff) + "." + 989391bb9cSRapkiewicz, Pawel std::to_string(value & 0xff); 999391bb9cSRapkiewicz, Pawel return netmask; 1009391bb9cSRapkiewicz, Pawel } 1019391bb9cSRapkiewicz, Pawel 1024a0cb85cSEd Tanous inline std::string 1034a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(const std::string &inputOrigin, 1044a0cb85cSEd Tanous bool isIPv4) 1051abe55efSEd Tanous { 1064a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static") 1071abe55efSEd Tanous { 1084a0cb85cSEd Tanous return "Static"; 1099391bb9cSRapkiewicz, Pawel } 1104a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal") 1111abe55efSEd Tanous { 1124a0cb85cSEd Tanous if (isIPv4) 1131abe55efSEd Tanous { 1144a0cb85cSEd Tanous return "IPv4LinkLocal"; 1151abe55efSEd Tanous } 1161abe55efSEd Tanous else 1171abe55efSEd Tanous { 1184a0cb85cSEd Tanous return "LinkLocal"; 1199391bb9cSRapkiewicz, Pawel } 1209391bb9cSRapkiewicz, Pawel } 1214a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP") 1221abe55efSEd Tanous { 1234a0cb85cSEd Tanous if (isIPv4) 1244a0cb85cSEd Tanous { 1254a0cb85cSEd Tanous return "DHCP"; 1264a0cb85cSEd Tanous } 1274a0cb85cSEd Tanous else 1284a0cb85cSEd Tanous { 1294a0cb85cSEd Tanous return "DHCPv6"; 1304a0cb85cSEd Tanous } 1314a0cb85cSEd Tanous } 1324a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC") 1334a0cb85cSEd Tanous { 1344a0cb85cSEd Tanous return "SLAAC"; 1354a0cb85cSEd Tanous } 1364a0cb85cSEd Tanous return ""; 1374a0cb85cSEd Tanous } 1384a0cb85cSEd Tanous 1394a0cb85cSEd Tanous inline std::string 1404a0cb85cSEd Tanous translateAddressOriginRedfishToDbus(const std::string &inputOrigin) 1414a0cb85cSEd Tanous { 1424a0cb85cSEd Tanous if (inputOrigin == "Static") 1434a0cb85cSEd Tanous { 1444a0cb85cSEd Tanous return "xyz.openbmc_project.Network.IP.AddressOrigin.Static"; 1454a0cb85cSEd Tanous } 1464a0cb85cSEd Tanous if (inputOrigin == "DHCP" || inputOrigin == "DHCPv6") 1474a0cb85cSEd Tanous { 1484a0cb85cSEd Tanous return "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP"; 1494a0cb85cSEd Tanous } 1504a0cb85cSEd Tanous if (inputOrigin == "IPv4LinkLocal" || inputOrigin == "LinkLocal") 1514a0cb85cSEd Tanous { 1524a0cb85cSEd Tanous return "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal"; 1534a0cb85cSEd Tanous } 1544a0cb85cSEd Tanous if (inputOrigin == "SLAAC") 1554a0cb85cSEd Tanous { 1564a0cb85cSEd Tanous return "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC"; 1574a0cb85cSEd Tanous } 1584a0cb85cSEd Tanous return ""; 1594a0cb85cSEd Tanous } 1604a0cb85cSEd Tanous 161*4c9afe43SEd Tanous inline bool extractEthernetInterfaceData(const std::string ðiface_id, 1624a0cb85cSEd Tanous const GetManagedObjects &dbus_data, 1634a0cb85cSEd Tanous EthernetInterfaceData ðData) 1644a0cb85cSEd Tanous { 165*4c9afe43SEd Tanous bool idFound = false; 1664a0cb85cSEd Tanous for (const auto &objpath : dbus_data) 1674a0cb85cSEd Tanous { 1684a0cb85cSEd Tanous for (const auto &ifacePair : objpath.second) 1694a0cb85cSEd Tanous { 170029573d4SEd Tanous if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id) 171029573d4SEd Tanous { 172*4c9afe43SEd Tanous idFound = true; 1734a0cb85cSEd Tanous if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") 1744a0cb85cSEd Tanous { 1754a0cb85cSEd Tanous for (const auto &propertyPair : ifacePair.second) 1764a0cb85cSEd Tanous { 1774a0cb85cSEd Tanous if (propertyPair.first == "MACAddress") 1784a0cb85cSEd Tanous { 1794a0cb85cSEd Tanous const std::string *mac = 180abf2add6SEd Tanous std::get_if<std::string>(&propertyPair.second); 1814a0cb85cSEd Tanous if (mac != nullptr) 1824a0cb85cSEd Tanous { 1834a0cb85cSEd Tanous ethData.mac_address = *mac; 1844a0cb85cSEd Tanous } 1854a0cb85cSEd Tanous } 1864a0cb85cSEd Tanous } 1874a0cb85cSEd Tanous } 1884a0cb85cSEd Tanous else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN") 1894a0cb85cSEd Tanous { 1904a0cb85cSEd Tanous for (const auto &propertyPair : ifacePair.second) 1914a0cb85cSEd Tanous { 1924a0cb85cSEd Tanous if (propertyPair.first == "Id") 1934a0cb85cSEd Tanous { 1941b6b96c5SEd Tanous const uint32_t *id = 195abf2add6SEd Tanous std::get_if<uint32_t>(&propertyPair.second); 1964a0cb85cSEd Tanous if (id != nullptr) 1974a0cb85cSEd Tanous { 198fda13ad2SSunitha Harish ethData.vlan_id.push_back(*id); 1994a0cb85cSEd Tanous } 2004a0cb85cSEd Tanous } 2014a0cb85cSEd Tanous } 2024a0cb85cSEd Tanous } 2034a0cb85cSEd Tanous else if (ifacePair.first == 2044a0cb85cSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 2054a0cb85cSEd Tanous { 2064a0cb85cSEd Tanous for (const auto &propertyPair : ifacePair.second) 2074a0cb85cSEd Tanous { 2084a0cb85cSEd Tanous if (propertyPair.first == "AutoNeg") 2094a0cb85cSEd Tanous { 2104a0cb85cSEd Tanous const bool *auto_neg = 211abf2add6SEd Tanous std::get_if<bool>(&propertyPair.second); 2124a0cb85cSEd Tanous if (auto_neg != nullptr) 2134a0cb85cSEd Tanous { 2144a0cb85cSEd Tanous ethData.auto_neg = *auto_neg; 2154a0cb85cSEd Tanous } 2164a0cb85cSEd Tanous } 2174a0cb85cSEd Tanous else if (propertyPair.first == "Speed") 2184a0cb85cSEd Tanous { 2194a0cb85cSEd Tanous const uint32_t *speed = 220abf2add6SEd Tanous std::get_if<uint32_t>(&propertyPair.second); 2214a0cb85cSEd Tanous if (speed != nullptr) 2224a0cb85cSEd Tanous { 2234a0cb85cSEd Tanous ethData.speed = *speed; 2244a0cb85cSEd Tanous } 2254a0cb85cSEd Tanous } 226f85837bfSRAJESWARAN THILLAIGOVINDAN else if (propertyPair.first == "Nameservers") 227029573d4SEd Tanous { 228029573d4SEd Tanous const std::vector<std::string> *nameservers = 229029573d4SEd Tanous sdbusplus::message::variant_ns::get_if< 230029573d4SEd Tanous std::vector<std::string>>( 231029573d4SEd Tanous &propertyPair.second); 232029573d4SEd Tanous if (nameservers != nullptr) 233029573d4SEd Tanous { 234029573d4SEd Tanous ethData.nameservers = std::move(*nameservers); 2354a0cb85cSEd Tanous } 2364a0cb85cSEd Tanous } 2372a133282Smanojkiraneda else if (propertyPair.first == "DHCPEnabled") 2382a133282Smanojkiraneda { 2392a133282Smanojkiraneda const bool *DHCPEnabled = 2402a133282Smanojkiraneda std::get_if<bool>(&propertyPair.second); 2412a133282Smanojkiraneda if (DHCPEnabled != nullptr) 2422a133282Smanojkiraneda { 2432a133282Smanojkiraneda ethData.DHCPEnabled = *DHCPEnabled; 2442a133282Smanojkiraneda } 2452a133282Smanojkiraneda } 246029573d4SEd Tanous } 247029573d4SEd Tanous } 248029573d4SEd Tanous } 249029573d4SEd Tanous // System configuration shows up in the global namespace, so no need 250029573d4SEd Tanous // to check eth number 251029573d4SEd Tanous if (ifacePair.first == 2524a0cb85cSEd Tanous "xyz.openbmc_project.Network.SystemConfiguration") 2534a0cb85cSEd Tanous { 2544a0cb85cSEd Tanous for (const auto &propertyPair : ifacePair.second) 2554a0cb85cSEd Tanous { 2564a0cb85cSEd Tanous if (propertyPair.first == "HostName") 2574a0cb85cSEd Tanous { 2584a0cb85cSEd Tanous const std::string *hostname = 259029573d4SEd Tanous sdbusplus::message::variant_ns::get_if<std::string>( 260029573d4SEd Tanous &propertyPair.second); 2614a0cb85cSEd Tanous if (hostname != nullptr) 2624a0cb85cSEd Tanous { 2634a0cb85cSEd Tanous ethData.hostname = *hostname; 2644a0cb85cSEd Tanous } 2654a0cb85cSEd Tanous } 2664a0cb85cSEd Tanous else if (propertyPair.first == "DefaultGateway") 2674a0cb85cSEd Tanous { 2684a0cb85cSEd Tanous const std::string *defaultGateway = 269029573d4SEd Tanous sdbusplus::message::variant_ns::get_if<std::string>( 270029573d4SEd Tanous &propertyPair.second); 2714a0cb85cSEd Tanous if (defaultGateway != nullptr) 2724a0cb85cSEd Tanous { 2734a0cb85cSEd Tanous ethData.default_gateway = *defaultGateway; 2744a0cb85cSEd Tanous } 2754a0cb85cSEd Tanous } 2769a6fc6feSRavi Teja else if (propertyPair.first == "DefaultGateway6") 2779a6fc6feSRavi Teja { 2789a6fc6feSRavi Teja const std::string *defaultGateway6 = 2799a6fc6feSRavi Teja sdbusplus::message::variant_ns::get_if<std::string>( 2809a6fc6feSRavi Teja &propertyPair.second); 2819a6fc6feSRavi Teja if (defaultGateway6 != nullptr) 2829a6fc6feSRavi Teja { 2839a6fc6feSRavi Teja ethData.ipv6_default_gateway = *defaultGateway6; 2849a6fc6feSRavi Teja } 2859a6fc6feSRavi Teja } 2864a0cb85cSEd Tanous } 2874a0cb85cSEd Tanous } 2884a0cb85cSEd Tanous } 2894a0cb85cSEd Tanous } 290*4c9afe43SEd Tanous return idFound; 2914a0cb85cSEd Tanous } 2924a0cb85cSEd Tanous 2934a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address 2944a0cb85cSEd Tanous inline void 2954a0cb85cSEd Tanous extractIPData(const std::string ðiface_id, 2964a0cb85cSEd Tanous const GetManagedObjects &dbus_data, 2974a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData> &ipv4_config) 2984a0cb85cSEd Tanous { 2994a0cb85cSEd Tanous const std::string ipv4PathStart = 3004a0cb85cSEd Tanous "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/"; 3014a0cb85cSEd Tanous 3024a0cb85cSEd Tanous // Since there might be several IPv4 configurations aligned with 3034a0cb85cSEd Tanous // single ethernet interface, loop over all of them 3044a0cb85cSEd Tanous for (const auto &objpath : dbus_data) 3054a0cb85cSEd Tanous { 3064a0cb85cSEd Tanous // Check if proper pattern for object path appears 3074a0cb85cSEd Tanous if (boost::starts_with(objpath.first.str, ipv4PathStart)) 3084a0cb85cSEd Tanous { 3094a0cb85cSEd Tanous for (auto &interface : objpath.second) 3104a0cb85cSEd Tanous { 3114a0cb85cSEd Tanous if (interface.first == "xyz.openbmc_project.Network.IP") 3124a0cb85cSEd Tanous { 3134a0cb85cSEd Tanous // Instance IPv4AddressData structure, and set as 3144a0cb85cSEd Tanous // appropriate 3154a0cb85cSEd Tanous std::pair< 3164a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData>::iterator, 3174a0cb85cSEd Tanous bool> 3184a0cb85cSEd Tanous it = ipv4_config.insert( 319b01bf299SEd Tanous {objpath.first.str.substr(ipv4PathStart.size())}); 3204a0cb85cSEd Tanous IPv4AddressData &ipv4_address = *it.first; 3214a0cb85cSEd Tanous for (auto &property : interface.second) 3224a0cb85cSEd Tanous { 3234a0cb85cSEd Tanous if (property.first == "Address") 3244a0cb85cSEd Tanous { 3254a0cb85cSEd Tanous const std::string *address = 326abf2add6SEd Tanous std::get_if<std::string>(&property.second); 3274a0cb85cSEd Tanous if (address != nullptr) 3284a0cb85cSEd Tanous { 3294a0cb85cSEd Tanous ipv4_address.address = *address; 3304a0cb85cSEd Tanous } 3314a0cb85cSEd Tanous } 3324a0cb85cSEd Tanous else if (property.first == "Gateway") 3334a0cb85cSEd Tanous { 3344a0cb85cSEd Tanous const std::string *gateway = 335abf2add6SEd Tanous std::get_if<std::string>(&property.second); 3364a0cb85cSEd Tanous if (gateway != nullptr) 3374a0cb85cSEd Tanous { 3384a0cb85cSEd Tanous ipv4_address.gateway = *gateway; 3394a0cb85cSEd Tanous } 3404a0cb85cSEd Tanous } 3414a0cb85cSEd Tanous else if (property.first == "Origin") 3424a0cb85cSEd Tanous { 3434a0cb85cSEd Tanous const std::string *origin = 344abf2add6SEd Tanous std::get_if<std::string>(&property.second); 3454a0cb85cSEd Tanous if (origin != nullptr) 3464a0cb85cSEd Tanous { 3474a0cb85cSEd Tanous ipv4_address.origin = 3484a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(*origin, 3494a0cb85cSEd Tanous true); 3504a0cb85cSEd Tanous } 3514a0cb85cSEd Tanous } 3524a0cb85cSEd Tanous else if (property.first == "PrefixLength") 3534a0cb85cSEd Tanous { 3544a0cb85cSEd Tanous const uint8_t *mask = 355abf2add6SEd Tanous std::get_if<uint8_t>(&property.second); 3564a0cb85cSEd Tanous if (mask != nullptr) 3574a0cb85cSEd Tanous { 3584a0cb85cSEd Tanous // convert it to the string 3594a0cb85cSEd Tanous ipv4_address.netmask = getNetmask(*mask); 3604a0cb85cSEd Tanous } 3614a0cb85cSEd Tanous } 3624a0cb85cSEd Tanous else 3634a0cb85cSEd Tanous { 3644a0cb85cSEd Tanous BMCWEB_LOG_ERROR 3654a0cb85cSEd Tanous << "Got extra property: " << property.first 3664a0cb85cSEd Tanous << " on the " << objpath.first.str << " object"; 3674a0cb85cSEd Tanous } 3684a0cb85cSEd Tanous } 3694a0cb85cSEd Tanous // Check if given address is local, or global 3704a0cb85cSEd Tanous ipv4_address.linktype = 3714a0cb85cSEd Tanous boost::starts_with(ipv4_address.address, "169.254.") 3724a0cb85cSEd Tanous ? LinkType::Global 3734a0cb85cSEd Tanous : LinkType::Local; 3744a0cb85cSEd Tanous } 3754a0cb85cSEd Tanous } 3764a0cb85cSEd Tanous } 3774a0cb85cSEd Tanous } 3784a0cb85cSEd Tanous } 379588c3f0dSKowalski, Kamil 380588c3f0dSKowalski, Kamil /** 381588c3f0dSKowalski, Kamil * @brief Sets given Id on the given VLAN interface through D-Bus 382588c3f0dSKowalski, Kamil * 383588c3f0dSKowalski, Kamil * @param[in] ifaceId Id of VLAN interface that should be modified 384588c3f0dSKowalski, Kamil * @param[in] inputVlanId New ID of the VLAN 385588c3f0dSKowalski, Kamil * @param[in] callback Function that will be called after the operation 386588c3f0dSKowalski, Kamil * 387588c3f0dSKowalski, Kamil * @return None. 388588c3f0dSKowalski, Kamil */ 389588c3f0dSKowalski, Kamil template <typename CallbackFunc> 3904a0cb85cSEd Tanous void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId, 3911abe55efSEd Tanous CallbackFunc &&callback) 3921abe55efSEd Tanous { 39355c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 394588c3f0dSKowalski, Kamil callback, "xyz.openbmc_project.Network", 395588c3f0dSKowalski, Kamil std::string("/xyz/openbmc_project/network/") + ifaceId, 396588c3f0dSKowalski, Kamil "org.freedesktop.DBus.Properties", "Set", 397588c3f0dSKowalski, Kamil "xyz.openbmc_project.Network.VLAN", "Id", 398abf2add6SEd Tanous std::variant<uint32_t>(inputVlanId)); 3994a0cb85cSEd Tanous } 400588c3f0dSKowalski, Kamil 401588c3f0dSKowalski, Kamil /** 402179db1d7SKowalski, Kamil * @brief Helper function that verifies IP address to check if it is in 403179db1d7SKowalski, Kamil * proper format. If bits pointer is provided, also calculates active 404179db1d7SKowalski, Kamil * bit count for Subnet Mask. 405179db1d7SKowalski, Kamil * 406179db1d7SKowalski, Kamil * @param[in] ip IP that will be verified 407179db1d7SKowalski, Kamil * @param[out] bits Calculated mask in bits notation 408179db1d7SKowalski, Kamil * 409179db1d7SKowalski, Kamil * @return true in case of success, false otherwise 410179db1d7SKowalski, Kamil */ 4114a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip, 4121abe55efSEd Tanous uint8_t *bits = nullptr) 4131abe55efSEd Tanous { 414179db1d7SKowalski, Kamil std::vector<std::string> bytesInMask; 415179db1d7SKowalski, Kamil 416179db1d7SKowalski, Kamil boost::split(bytesInMask, ip, boost::is_any_of(".")); 417179db1d7SKowalski, Kamil 4184a0cb85cSEd Tanous static const constexpr int ipV4AddressSectionsCount = 4; 4191abe55efSEd Tanous if (bytesInMask.size() != ipV4AddressSectionsCount) 4201abe55efSEd Tanous { 421179db1d7SKowalski, Kamil return false; 422179db1d7SKowalski, Kamil } 423179db1d7SKowalski, Kamil 4241abe55efSEd Tanous if (bits != nullptr) 4251abe55efSEd Tanous { 426179db1d7SKowalski, Kamil *bits = 0; 427179db1d7SKowalski, Kamil } 428179db1d7SKowalski, Kamil 429179db1d7SKowalski, Kamil char *endPtr; 430179db1d7SKowalski, Kamil long previousValue = 255; 431179db1d7SKowalski, Kamil bool firstZeroInByteHit; 4321abe55efSEd Tanous for (const std::string &byte : bytesInMask) 4331abe55efSEd Tanous { 4341abe55efSEd Tanous if (byte.empty()) 4351abe55efSEd Tanous { 4361db9ca37SKowalski, Kamil return false; 4371db9ca37SKowalski, Kamil } 4381db9ca37SKowalski, Kamil 439179db1d7SKowalski, Kamil // Use strtol instead of stroi to avoid exceptions 4401db9ca37SKowalski, Kamil long value = std::strtol(byte.c_str(), &endPtr, 10); 441179db1d7SKowalski, Kamil 4424a0cb85cSEd Tanous // endPtr should point to the end of the string, otherwise given string 4434a0cb85cSEd Tanous // is not 100% number 4441abe55efSEd Tanous if (*endPtr != '\0') 4451abe55efSEd Tanous { 446179db1d7SKowalski, Kamil return false; 447179db1d7SKowalski, Kamil } 448179db1d7SKowalski, Kamil 449179db1d7SKowalski, Kamil // Value should be contained in byte 4501abe55efSEd Tanous if (value < 0 || value > 255) 4511abe55efSEd Tanous { 452179db1d7SKowalski, Kamil return false; 453179db1d7SKowalski, Kamil } 454179db1d7SKowalski, Kamil 4551abe55efSEd Tanous if (bits != nullptr) 4561abe55efSEd Tanous { 457179db1d7SKowalski, Kamil // Mask has to be continuous between bytes 4581abe55efSEd Tanous if (previousValue != 255 && value != 0) 4591abe55efSEd Tanous { 460179db1d7SKowalski, Kamil return false; 461179db1d7SKowalski, Kamil } 462179db1d7SKowalski, Kamil 463179db1d7SKowalski, Kamil // Mask has to be continuous inside bytes 464179db1d7SKowalski, Kamil firstZeroInByteHit = false; 465179db1d7SKowalski, Kamil 466179db1d7SKowalski, Kamil // Count bits 4671abe55efSEd Tanous for (int bitIdx = 7; bitIdx >= 0; bitIdx--) 4681abe55efSEd Tanous { 4691abe55efSEd Tanous if (value & (1 << bitIdx)) 4701abe55efSEd Tanous { 4711abe55efSEd Tanous if (firstZeroInByteHit) 4721abe55efSEd Tanous { 473179db1d7SKowalski, Kamil // Continuity not preserved 474179db1d7SKowalski, Kamil return false; 4751abe55efSEd Tanous } 4761abe55efSEd Tanous else 4771abe55efSEd Tanous { 478179db1d7SKowalski, Kamil (*bits)++; 479179db1d7SKowalski, Kamil } 4801abe55efSEd Tanous } 4811abe55efSEd Tanous else 4821abe55efSEd Tanous { 483179db1d7SKowalski, Kamil firstZeroInByteHit = true; 484179db1d7SKowalski, Kamil } 485179db1d7SKowalski, Kamil } 486179db1d7SKowalski, Kamil } 487179db1d7SKowalski, Kamil 488179db1d7SKowalski, Kamil previousValue = value; 489179db1d7SKowalski, Kamil } 490179db1d7SKowalski, Kamil 491179db1d7SKowalski, Kamil return true; 492179db1d7SKowalski, Kamil } 493179db1d7SKowalski, Kamil 494179db1d7SKowalski, Kamil /** 495b01bf299SEd Tanous * @brief Changes IPv4 address type property (Address, Gateway) 496b01bf299SEd Tanous * 497b01bf299SEd Tanous * @param[in] ifaceId Id of interface whose IP should be modified 498b01bf299SEd Tanous * @param[in] ipIdx Index of IP in input array that should be modified 499b01bf299SEd Tanous * @param[in] ipHash DBus Hash id of modified IP 500b01bf299SEd Tanous * @param[in] name Name of field in JSON representation 501b01bf299SEd Tanous * @param[in] newValue New value that should be written 502b01bf299SEd Tanous * @param[io] asyncResp Response object that will be returned to client 503b01bf299SEd Tanous * 504b01bf299SEd Tanous * @return true if give IP is valid and has been sent do D-Bus, false 505b01bf299SEd Tanous * otherwise 506b01bf299SEd Tanous */ 507b01bf299SEd Tanous inline void changeIPv4AddressProperty( 508b01bf299SEd Tanous const std::string &ifaceId, int ipIdx, const std::string &ipHash, 509b01bf299SEd Tanous const std::string &name, const std::string &newValue, 510b01bf299SEd Tanous const std::shared_ptr<AsyncResp> asyncResp) 511b01bf299SEd Tanous { 512b01bf299SEd Tanous auto callback = [asyncResp, ipIdx, name{std::string(name)}, 513b01bf299SEd Tanous newValue{std::move(newValue)}]( 514b01bf299SEd Tanous const boost::system::error_code ec) { 515b01bf299SEd Tanous if (ec) 516b01bf299SEd Tanous { 517b01bf299SEd Tanous messages::internalError(asyncResp->res); 518b01bf299SEd Tanous } 519b01bf299SEd Tanous else 520b01bf299SEd Tanous { 521b01bf299SEd Tanous asyncResp->res.jsonValue["IPv4Addresses"][ipIdx][name] = newValue; 522b01bf299SEd Tanous } 523b01bf299SEd Tanous }; 524b01bf299SEd Tanous 525b01bf299SEd Tanous crow::connections::systemBus->async_method_call( 526b01bf299SEd Tanous std::move(callback), "xyz.openbmc_project.Network", 527b01bf299SEd Tanous "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 528b01bf299SEd Tanous "org.freedesktop.DBus.Properties", "Set", 529b01bf299SEd Tanous "xyz.openbmc_project.Network.IP", name, 530b01bf299SEd Tanous std::variant<std::string>(newValue)); 531b01bf299SEd Tanous } 532b01bf299SEd Tanous 533b01bf299SEd Tanous /** 534179db1d7SKowalski, Kamil * @brief Changes IPv4 address origin property 535179db1d7SKowalski, Kamil * 536179db1d7SKowalski, Kamil * @param[in] ifaceId Id of interface whose IP should be modified 5374a0cb85cSEd Tanous * @param[in] ipIdx Index of IP in input array that should be 5381abe55efSEd Tanous * modified 539179db1d7SKowalski, Kamil * @param[in] ipHash DBus Hash id of modified IP 540179db1d7SKowalski, Kamil * @param[in] newValue New value in Redfish format 541179db1d7SKowalski, Kamil * @param[in] newValueDbus New value in D-Bus format 542179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 543179db1d7SKowalski, Kamil * 544179db1d7SKowalski, Kamil * @return true if give IP is valid and has been sent do D-Bus, false 545179db1d7SKowalski, Kamil * otherwise 546179db1d7SKowalski, Kamil */ 547b01bf299SEd Tanous inline void changeIPv4Origin(const std::string &ifaceId, int ipIdx, 5481abe55efSEd Tanous const std::string &ipHash, 5491abe55efSEd Tanous const std::string &newValue, 550179db1d7SKowalski, Kamil const std::string &newValueDbus, 5514a0cb85cSEd Tanous const std::shared_ptr<AsyncResp> asyncResp) 5521abe55efSEd Tanous { 5534a0cb85cSEd Tanous auto callback = [asyncResp, ipIdx, newValue{std::move(newValue)}]( 5541abe55efSEd Tanous const boost::system::error_code ec) { 5551abe55efSEd Tanous if (ec) 5561abe55efSEd Tanous { 557a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 5581abe55efSEd Tanous } 5591abe55efSEd Tanous else 5601abe55efSEd Tanous { 5614a0cb85cSEd Tanous asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["AddressOrigin"] = 562179db1d7SKowalski, Kamil newValue; 563179db1d7SKowalski, Kamil } 564179db1d7SKowalski, Kamil }; 565179db1d7SKowalski, Kamil 56655c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 567179db1d7SKowalski, Kamil std::move(callback), "xyz.openbmc_project.Network", 568179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 569179db1d7SKowalski, Kamil "org.freedesktop.DBus.Properties", "Set", 570179db1d7SKowalski, Kamil "xyz.openbmc_project.Network.IP", "Origin", 571abf2add6SEd Tanous std::variant<std::string>(newValueDbus)); 5724a0cb85cSEd Tanous } 573179db1d7SKowalski, Kamil 574179db1d7SKowalski, Kamil /** 575179db1d7SKowalski, Kamil * @brief Modifies SubnetMask for given IP 576179db1d7SKowalski, Kamil * 577179db1d7SKowalski, Kamil * @param[in] ifaceId Id of interface whose IP should be modified 5784a0cb85cSEd Tanous * @param[in] ipIdx Index of IP in input array that should be 5791abe55efSEd Tanous * modified 580179db1d7SKowalski, Kamil * @param[in] ipHash DBus Hash id of modified IP 581179db1d7SKowalski, Kamil * @param[in] newValueStr Mask in dot notation as string 582179db1d7SKowalski, Kamil * @param[in] newValue Mask as PrefixLength in bitcount 583179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 584179db1d7SKowalski, Kamil * 585179db1d7SKowalski, Kamil * @return None 586179db1d7SKowalski, Kamil */ 587b01bf299SEd Tanous inline void changeIPv4SubnetMaskProperty(const std::string &ifaceId, int ipIdx, 5884a0cb85cSEd Tanous const std::string &ipHash, 5894a0cb85cSEd Tanous const std::string &newValueStr, 5904a0cb85cSEd Tanous uint8_t &newValue, 5914a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp) 5921abe55efSEd Tanous { 5934a0cb85cSEd Tanous auto callback = [asyncResp, ipIdx, newValueStr{std::move(newValueStr)}]( 5941abe55efSEd Tanous const boost::system::error_code ec) { 5951abe55efSEd Tanous if (ec) 5961abe55efSEd Tanous { 597a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 5981abe55efSEd Tanous } 5991abe55efSEd Tanous else 6001abe55efSEd Tanous { 60155c7b7a2SEd Tanous asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["SubnetMask"] = 602179db1d7SKowalski, Kamil newValueStr; 603179db1d7SKowalski, Kamil } 604179db1d7SKowalski, Kamil }; 605179db1d7SKowalski, Kamil 60655c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 607179db1d7SKowalski, Kamil std::move(callback), "xyz.openbmc_project.Network", 608179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 609179db1d7SKowalski, Kamil "org.freedesktop.DBus.Properties", "Set", 610179db1d7SKowalski, Kamil "xyz.openbmc_project.Network.IP", "PrefixLength", 611abf2add6SEd Tanous std::variant<uint8_t>(newValue)); 6124a0cb85cSEd Tanous } 613588c3f0dSKowalski, Kamil 614588c3f0dSKowalski, Kamil /** 615179db1d7SKowalski, Kamil * @brief Deletes given IPv4 616179db1d7SKowalski, Kamil * 617179db1d7SKowalski, Kamil * @param[in] ifaceId Id of interface whose IP should be deleted 6184a0cb85cSEd Tanous * @param[in] ipIdx Index of IP in input array that should be deleted 619179db1d7SKowalski, Kamil * @param[in] ipHash DBus Hash id of IP that should be deleted 620179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 621179db1d7SKowalski, Kamil * 622179db1d7SKowalski, Kamil * @return None 623179db1d7SKowalski, Kamil */ 6244a0cb85cSEd Tanous inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash, 625179db1d7SKowalski, Kamil unsigned int ipIdx, 6264a0cb85cSEd Tanous const std::shared_ptr<AsyncResp> asyncResp) 6271abe55efSEd Tanous { 62855c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 6294a0cb85cSEd Tanous [ipIdx, asyncResp](const boost::system::error_code ec) { 6301abe55efSEd Tanous if (ec) 6311abe55efSEd Tanous { 632a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 6331abe55efSEd Tanous } 6341abe55efSEd Tanous else 6351abe55efSEd Tanous { 63655c7b7a2SEd Tanous asyncResp->res.jsonValue["IPv4Addresses"][ipIdx] = nullptr; 637179db1d7SKowalski, Kamil } 638179db1d7SKowalski, Kamil }, 639179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", 640179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 641179db1d7SKowalski, Kamil "xyz.openbmc_project.Object.Delete", "Delete"); 642179db1d7SKowalski, Kamil } 643179db1d7SKowalski, Kamil 644179db1d7SKowalski, Kamil /** 645179db1d7SKowalski, Kamil * @brief Creates IPv4 with given data 646179db1d7SKowalski, Kamil * 647179db1d7SKowalski, Kamil * @param[in] ifaceId Id of interface whose IP should be deleted 6484a0cb85cSEd Tanous * @param[in] ipIdx Index of IP in input array that should be deleted 649179db1d7SKowalski, Kamil * @param[in] ipHash DBus Hash id of IP that should be deleted 650179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 651179db1d7SKowalski, Kamil * 652179db1d7SKowalski, Kamil * @return None 653179db1d7SKowalski, Kamil */ 654b01bf299SEd Tanous inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx, 655b01bf299SEd Tanous uint8_t subnetMask, const std::string &gateway, 656b01bf299SEd Tanous const std::string &address, 6574a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp) 6581abe55efSEd Tanous { 65943b761d0SEd Tanous auto createIpHandler = [asyncResp](const boost::system::error_code ec) { 6601abe55efSEd Tanous if (ec) 6611abe55efSEd Tanous { 662a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 663179db1d7SKowalski, Kamil } 664179db1d7SKowalski, Kamil }; 665179db1d7SKowalski, Kamil 66655c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 667179db1d7SKowalski, Kamil std::move(createIpHandler), "xyz.openbmc_project.Network", 668179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId, 669179db1d7SKowalski, Kamil "xyz.openbmc_project.Network.IP.Create", "IP", 670179db1d7SKowalski, Kamil "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, subnetMask, 671179db1d7SKowalski, Kamil gateway); 672179db1d7SKowalski, Kamil } 6732a133282Smanojkiraneda using GetAllPropertiesType = 6742a133282Smanojkiraneda boost::container::flat_map<std::string, sdbusplus::message::variant<bool>>; 6752a133282Smanojkiraneda 6762a133282Smanojkiraneda inline void getDHCPConfigData(const std::shared_ptr<AsyncResp> asyncResp) 6772a133282Smanojkiraneda { 6782a133282Smanojkiraneda auto getConfig = [asyncResp](const boost::system::error_code error_code, 6792a133282Smanojkiraneda const GetAllPropertiesType &dbus_data) { 6802a133282Smanojkiraneda if (error_code) 6812a133282Smanojkiraneda { 6822a133282Smanojkiraneda BMCWEB_LOG_ERROR << "D-Bus response error: " << error_code; 6832a133282Smanojkiraneda messages::internalError(asyncResp->res); 6842a133282Smanojkiraneda return; 6852a133282Smanojkiraneda } 686fda13ad2SSunitha Harish nlohmann::json &DHCPConfigTypeJson = asyncResp->res.jsonValue["DHCPv4"]; 6872a133282Smanojkiraneda for (const auto &property : dbus_data) 6882a133282Smanojkiraneda { 6892a133282Smanojkiraneda auto value = 6902a133282Smanojkiraneda sdbusplus::message::variant_ns::get_if<bool>(&property.second); 6912a133282Smanojkiraneda 6922a133282Smanojkiraneda if (value == nullptr) 6932a133282Smanojkiraneda { 6942a133282Smanojkiraneda continue; 6952a133282Smanojkiraneda } 6962a133282Smanojkiraneda if (property.first == "DNSEnabled") 6972a133282Smanojkiraneda { 6982a133282Smanojkiraneda DHCPConfigTypeJson["UseDNSServers"] = *value; 6992a133282Smanojkiraneda } 7002a133282Smanojkiraneda else if (property.first == "HostNameEnabled") 7012a133282Smanojkiraneda { 7022a133282Smanojkiraneda DHCPConfigTypeJson["UseDomainName"] = *value; 7032a133282Smanojkiraneda } 7042a133282Smanojkiraneda else if (property.first == "NTPEnabled") 7052a133282Smanojkiraneda { 7062a133282Smanojkiraneda DHCPConfigTypeJson["UseNTPServers"] = *value; 7072a133282Smanojkiraneda } 7082a133282Smanojkiraneda } 7092a133282Smanojkiraneda }; 7102a133282Smanojkiraneda crow::connections::systemBus->async_method_call( 7112a133282Smanojkiraneda std::move(getConfig), "xyz.openbmc_project.Network", 7122a133282Smanojkiraneda "/xyz/openbmc_project/network/config/dhcp", 7132a133282Smanojkiraneda "org.freedesktop.DBus.Properties", "GetAll", 7142a133282Smanojkiraneda "xyz.openbmc_project.Network.DHCPConfiguration"); 7152a133282Smanojkiraneda } 716179db1d7SKowalski, Kamil 717179db1d7SKowalski, Kamil /** 718179db1d7SKowalski, Kamil * Function that retrieves all properties for given Ethernet Interface 719179db1d7SKowalski, Kamil * Object 720179db1d7SKowalski, Kamil * from EntityManager Network Manager 7214a0cb85cSEd Tanous * @param ethiface_id a eth interface id to query on DBus 722179db1d7SKowalski, Kamil * @param callback a function that shall be called to convert Dbus output 723179db1d7SKowalski, Kamil * into JSON 724179db1d7SKowalski, Kamil */ 725179db1d7SKowalski, Kamil template <typename CallbackFunc> 7264a0cb85cSEd Tanous void getEthernetIfaceData(const std::string ðiface_id, 7271abe55efSEd Tanous CallbackFunc &&callback) 7281abe55efSEd Tanous { 72955c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 7304a0cb85cSEd Tanous [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}]( 7311abe55efSEd Tanous const boost::system::error_code error_code, 7324a0cb85cSEd Tanous const GetManagedObjects &resp) { 73355c7b7a2SEd Tanous EthernetInterfaceData ethData{}; 7344a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData> ipv4Data; 735179db1d7SKowalski, Kamil 7361abe55efSEd Tanous if (error_code) 7371abe55efSEd Tanous { 73855c7b7a2SEd Tanous callback(false, ethData, ipv4Data); 739179db1d7SKowalski, Kamil return; 740179db1d7SKowalski, Kamil } 741179db1d7SKowalski, Kamil 742*4c9afe43SEd Tanous bool found = 7434a0cb85cSEd Tanous extractEthernetInterfaceData(ethiface_id, resp, ethData); 744*4c9afe43SEd Tanous if (!found) 745*4c9afe43SEd Tanous { 746*4c9afe43SEd Tanous callback(false, ethData, ipv4Data); 747*4c9afe43SEd Tanous return; 748*4c9afe43SEd Tanous } 749*4c9afe43SEd Tanous 7504a0cb85cSEd Tanous extractIPData(ethiface_id, resp, ipv4Data); 751179db1d7SKowalski, Kamil 752179db1d7SKowalski, Kamil // Fix global GW 7531abe55efSEd Tanous for (IPv4AddressData &ipv4 : ipv4Data) 7541abe55efSEd Tanous { 7554a0cb85cSEd Tanous if ((ipv4.linktype == LinkType::Global) && 7564a0cb85cSEd Tanous (ipv4.gateway == "0.0.0.0")) 7571abe55efSEd Tanous { 7584a0cb85cSEd Tanous ipv4.gateway = ethData.default_gateway; 759179db1d7SKowalski, Kamil } 760179db1d7SKowalski, Kamil } 761179db1d7SKowalski, Kamil 7624a0cb85cSEd Tanous // Finally make a callback with usefull data 76355c7b7a2SEd Tanous callback(true, ethData, ipv4Data); 764179db1d7SKowalski, Kamil }, 765179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 766179db1d7SKowalski, Kamil "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 767179db1d7SKowalski, Kamil }; 768179db1d7SKowalski, Kamil 769179db1d7SKowalski, Kamil /** 7709391bb9cSRapkiewicz, Pawel * Function that retrieves all Ethernet Interfaces available through Network 7719391bb9cSRapkiewicz, Pawel * Manager 7721abe55efSEd Tanous * @param callback a function that shall be called to convert Dbus output 7731abe55efSEd Tanous * into JSON. 7749391bb9cSRapkiewicz, Pawel */ 7759391bb9cSRapkiewicz, Pawel template <typename CallbackFunc> 7761abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc &&callback) 7771abe55efSEd Tanous { 77855c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 7794a0cb85cSEd Tanous [callback{std::move(callback)}]( 7809391bb9cSRapkiewicz, Pawel const boost::system::error_code error_code, 7814a0cb85cSEd Tanous GetManagedObjects &resp) { 7821abe55efSEd Tanous // Callback requires vector<string> to retrieve all available 7831abe55efSEd Tanous // ethernet interfaces 784*4c9afe43SEd Tanous boost::container::flat_set<std::string> iface_list; 7854a0cb85cSEd Tanous iface_list.reserve(resp.size()); 7861abe55efSEd Tanous if (error_code) 7871abe55efSEd Tanous { 7884a0cb85cSEd Tanous callback(false, iface_list); 7899391bb9cSRapkiewicz, Pawel return; 7909391bb9cSRapkiewicz, Pawel } 7919391bb9cSRapkiewicz, Pawel 7929391bb9cSRapkiewicz, Pawel // Iterate over all retrieved ObjectPaths. 7934a0cb85cSEd Tanous for (const auto &objpath : resp) 7941abe55efSEd Tanous { 7959391bb9cSRapkiewicz, Pawel // And all interfaces available for certain ObjectPath. 7964a0cb85cSEd Tanous for (const auto &interface : objpath.second) 7971abe55efSEd Tanous { 7981abe55efSEd Tanous // If interface is 7994a0cb85cSEd Tanous // xyz.openbmc_project.Network.EthernetInterface, this is 8004a0cb85cSEd Tanous // what we're looking for. 8019391bb9cSRapkiewicz, Pawel if (interface.first == 8021abe55efSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 8031abe55efSEd Tanous { 8044a0cb85cSEd Tanous // Cut out everyting until last "/", ... 8054a0cb85cSEd Tanous const std::string &iface_id = objpath.first.str; 8064a0cb85cSEd Tanous std::size_t last_pos = iface_id.rfind("/"); 8074a0cb85cSEd Tanous if (last_pos != std::string::npos) 8081abe55efSEd Tanous { 8099391bb9cSRapkiewicz, Pawel // and put it into output vector. 810*4c9afe43SEd Tanous iface_list.emplace(iface_id.substr(last_pos + 1)); 8119391bb9cSRapkiewicz, Pawel } 8129391bb9cSRapkiewicz, Pawel } 8139391bb9cSRapkiewicz, Pawel } 8149391bb9cSRapkiewicz, Pawel } 815a434f2bdSEd Tanous // Finally make a callback with useful data 8164a0cb85cSEd Tanous callback(true, iface_list); 8179391bb9cSRapkiewicz, Pawel }, 818aa2e59c1SEd Tanous "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 819aa2e59c1SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 8209391bb9cSRapkiewicz, Pawel }; 8219391bb9cSRapkiewicz, Pawel 8229391bb9cSRapkiewicz, Pawel /** 8239391bb9cSRapkiewicz, Pawel * EthernetCollection derived class for delivering Ethernet Collection Schema 8249391bb9cSRapkiewicz, Pawel */ 8251abe55efSEd Tanous class EthernetCollection : public Node 8261abe55efSEd Tanous { 8279391bb9cSRapkiewicz, Pawel public: 8284a0cb85cSEd Tanous template <typename CrowApp> 8291abe55efSEd Tanous EthernetCollection(CrowApp &app) : 8304a0cb85cSEd Tanous Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") 8311abe55efSEd Tanous { 832588c3f0dSKowalski, Kamil entityPrivileges = { 833588c3f0dSKowalski, Kamil {boost::beast::http::verb::get, {{"Login"}}}, 834e0d918bcSEd Tanous {boost::beast::http::verb::head, {{"Login"}}}, 835e0d918bcSEd Tanous {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 836e0d918bcSEd Tanous {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 837e0d918bcSEd Tanous {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 838e0d918bcSEd Tanous {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 8399391bb9cSRapkiewicz, Pawel } 8409391bb9cSRapkiewicz, Pawel 8419391bb9cSRapkiewicz, Pawel private: 8429391bb9cSRapkiewicz, Pawel /** 8439391bb9cSRapkiewicz, Pawel * Functions triggers appropriate requests on DBus 8449391bb9cSRapkiewicz, Pawel */ 84555c7b7a2SEd Tanous void doGet(crow::Response &res, const crow::Request &req, 8461abe55efSEd Tanous const std::vector<std::string> ¶ms) override 8471abe55efSEd Tanous { 8480f74e643SEd Tanous res.jsonValue["@odata.type"] = 8490f74e643SEd Tanous "#EthernetInterfaceCollection.EthernetInterfaceCollection"; 8500f74e643SEd Tanous res.jsonValue["@odata.context"] = 8510f74e643SEd Tanous "/redfish/v1/" 8520f74e643SEd Tanous "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection"; 8530f74e643SEd Tanous res.jsonValue["@odata.id"] = 8540f74e643SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 8550f74e643SEd Tanous res.jsonValue["Name"] = "Ethernet Network Interface Collection"; 8560f74e643SEd Tanous res.jsonValue["Description"] = 8570f74e643SEd Tanous "Collection of EthernetInterfaces for this Manager"; 858*4c9afe43SEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 8594a0cb85cSEd Tanous // Get eth interface list, and call the below callback for JSON 8601abe55efSEd Tanous // preparation 861f12894f8SJason M. Bills getEthernetIfaceList( 862*4c9afe43SEd Tanous [asyncResp]( 863*4c9afe43SEd Tanous const bool &success, 864*4c9afe43SEd Tanous const boost::container::flat_set<std::string> &iface_list) { 8654a0cb85cSEd Tanous if (!success) 8661abe55efSEd Tanous { 867*4c9afe43SEd Tanous messages::internalError(asyncResp->res); 8684a0cb85cSEd Tanous return; 8694a0cb85cSEd Tanous } 8704a0cb85cSEd Tanous 871*4c9afe43SEd Tanous nlohmann::json &iface_array = 872*4c9afe43SEd Tanous asyncResp->res.jsonValue["Members"]; 8734a0cb85cSEd Tanous iface_array = nlohmann::json::array(); 874fda13ad2SSunitha Harish std::string tag = "_"; 8754a0cb85cSEd Tanous for (const std::string &iface_item : iface_list) 8761abe55efSEd Tanous { 877fda13ad2SSunitha Harish std::size_t found = iface_item.find(tag); 878fda13ad2SSunitha Harish if (found == std::string::npos) 879fda13ad2SSunitha Harish { 8804a0cb85cSEd Tanous iface_array.push_back( 8814a0cb85cSEd Tanous {{"@odata.id", 8824a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 8834a0cb85cSEd Tanous iface_item}}); 8849391bb9cSRapkiewicz, Pawel } 885fda13ad2SSunitha Harish } 8864a0cb85cSEd Tanous 887*4c9afe43SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 888*4c9afe43SEd Tanous iface_array.size(); 889*4c9afe43SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 8904a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 8919391bb9cSRapkiewicz, Pawel }); 8929391bb9cSRapkiewicz, Pawel } 8939391bb9cSRapkiewicz, Pawel }; 8949391bb9cSRapkiewicz, Pawel 8959391bb9cSRapkiewicz, Pawel /** 8969391bb9cSRapkiewicz, Pawel * EthernetInterface derived class for delivering Ethernet Schema 8979391bb9cSRapkiewicz, Pawel */ 8981abe55efSEd Tanous class EthernetInterface : public Node 8991abe55efSEd Tanous { 9009391bb9cSRapkiewicz, Pawel public: 9019391bb9cSRapkiewicz, Pawel /* 9029391bb9cSRapkiewicz, Pawel * Default Constructor 9039391bb9cSRapkiewicz, Pawel */ 9044a0cb85cSEd Tanous template <typename CrowApp> 9051abe55efSEd Tanous EthernetInterface(CrowApp &app) : 9064a0cb85cSEd Tanous Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/", 9071abe55efSEd Tanous std::string()) 9081abe55efSEd Tanous { 909588c3f0dSKowalski, Kamil entityPrivileges = { 910588c3f0dSKowalski, Kamil {boost::beast::http::verb::get, {{"Login"}}}, 911e0d918bcSEd Tanous {boost::beast::http::verb::head, {{"Login"}}}, 912e0d918bcSEd Tanous {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 913e0d918bcSEd Tanous {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 914e0d918bcSEd Tanous {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 915e0d918bcSEd Tanous {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 9169391bb9cSRapkiewicz, Pawel } 9179391bb9cSRapkiewicz, Pawel 918e439f0f8SKowalski, Kamil private: 919bc0bd6e0SEd Tanous void handleHostnamePatch(const std::string &hostname, 9204a0cb85cSEd Tanous const std::shared_ptr<AsyncResp> asyncResp) 9211abe55efSEd Tanous { 922bc0bd6e0SEd Tanous asyncResp->res.jsonValue["HostName"] = hostname; 923bc0bd6e0SEd Tanous crow::connections::systemBus->async_method_call( 924bc0bd6e0SEd Tanous [asyncResp](const boost::system::error_code ec) { 9254a0cb85cSEd Tanous if (ec) 9264a0cb85cSEd Tanous { 927a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 9281abe55efSEd Tanous } 929bc0bd6e0SEd Tanous }, 930bc0bd6e0SEd Tanous "xyz.openbmc_project.Network", 931bc0bd6e0SEd Tanous "/xyz/openbmc_project/network/config", 932bc0bd6e0SEd Tanous "org.freedesktop.DBus.Properties", "Set", 933bc0bd6e0SEd Tanous "xyz.openbmc_project.Network.SystemConfiguration", "HostName", 934abf2add6SEd Tanous std::variant<std::string>(hostname)); 935588c3f0dSKowalski, Kamil } 936588c3f0dSKowalski, Kamil 937d577665bSRatan Gupta void handleMACAddressPatch(const std::string &ifaceId, 938d577665bSRatan Gupta const std::string &macAddress, 939d577665bSRatan Gupta const std::shared_ptr<AsyncResp> &asyncResp) 940d577665bSRatan Gupta { 941d577665bSRatan Gupta crow::connections::systemBus->async_method_call( 942d577665bSRatan Gupta [asyncResp, macAddress](const boost::system::error_code ec) { 943d577665bSRatan Gupta if (ec) 944d577665bSRatan Gupta { 945d577665bSRatan Gupta messages::internalError(asyncResp->res); 946d577665bSRatan Gupta return; 947d577665bSRatan Gupta } 948d577665bSRatan Gupta asyncResp->res.jsonValue["MACAddress"] = std::move(macAddress); 949d577665bSRatan Gupta }, 950d577665bSRatan Gupta "xyz.openbmc_project.Network", 951d577665bSRatan Gupta "/xyz/openbmc_project/network/" + ifaceId, 952d577665bSRatan Gupta "org.freedesktop.DBus.Properties", "Set", 953d577665bSRatan Gupta "xyz.openbmc_project.Network.MACAddress", "MACAddress", 954d577665bSRatan Gupta std::variant<std::string>(macAddress)); 955d577665bSRatan Gupta } 956d577665bSRatan Gupta 9574a0cb85cSEd Tanous void handleIPv4Patch( 958f476acbfSRatan Gupta const std::string &ifaceId, nlohmann::json &input, 9594a0cb85cSEd Tanous const boost::container::flat_set<IPv4AddressData> &ipv4Data, 9604a0cb85cSEd Tanous const std::shared_ptr<AsyncResp> asyncResp) 9611abe55efSEd Tanous { 962f476acbfSRatan Gupta if (!input.is_array()) 963f476acbfSRatan Gupta { 964f476acbfSRatan Gupta messages::propertyValueTypeError(asyncResp->res, input.dump(), 965f476acbfSRatan Gupta "IPv4Addresses"); 966f476acbfSRatan Gupta return; 967f476acbfSRatan Gupta } 968f476acbfSRatan Gupta 9694a0cb85cSEd Tanous int entryIdx = 0; 9704a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData>::const_iterator thisData = 9714a0cb85cSEd Tanous ipv4Data.begin(); 972537174c4SEd Tanous for (nlohmann::json &thisJson : input) 9731abe55efSEd Tanous { 9744a0cb85cSEd Tanous std::string pathString = 975a08b46ccSJason M. Bills "IPv4Addresses/" + std::to_string(entryIdx); 976179db1d7SKowalski, Kamil 977f476acbfSRatan Gupta if (thisJson.is_null()) 978f476acbfSRatan Gupta { 979f476acbfSRatan Gupta if (thisData != ipv4Data.end()) 980f476acbfSRatan Gupta { 981f476acbfSRatan Gupta deleteIPv4(ifaceId, thisData->id, entryIdx, asyncResp); 982f476acbfSRatan Gupta thisData++; 983f476acbfSRatan Gupta } 984f476acbfSRatan Gupta else 985f476acbfSRatan Gupta { 986f476acbfSRatan Gupta messages::propertyValueFormatError( 987f476acbfSRatan Gupta asyncResp->res, input.dump(), pathString); 988f476acbfSRatan Gupta return; 989f476acbfSRatan Gupta // TODO(ratagupt) Not sure about the property where value is 990f476acbfSRatan Gupta // list and if unable to update one of the 991f476acbfSRatan Gupta // list value then should we proceed further or 992f476acbfSRatan Gupta // break there, would ask in the redfish forum 993f476acbfSRatan Gupta // till then we stop processing the next list item. 994f476acbfSRatan Gupta } 995f476acbfSRatan Gupta entryIdx++; 996f476acbfSRatan Gupta continue; // not an error as per the redfish spec. 997f476acbfSRatan Gupta } 998f476acbfSRatan Gupta 9999474b378SRatan Gupta if (thisJson.empty()) 10009474b378SRatan Gupta { 10019474b378SRatan Gupta if (thisData != ipv4Data.end()) 10029474b378SRatan Gupta { 10039474b378SRatan Gupta thisData++; 10049474b378SRatan Gupta } 10059474b378SRatan Gupta else 10069474b378SRatan Gupta { 10079474b378SRatan Gupta messages::propertyMissing(asyncResp->res, 10089474b378SRatan Gupta pathString + "/Address"); 10099474b378SRatan Gupta return; 1010f476acbfSRatan Gupta // TODO(ratagupt) Not sure about the property where value is 10119474b378SRatan Gupta // list and if unable to update one of the 10129474b378SRatan Gupta // list value then should we proceed further or 10139474b378SRatan Gupta // break there, would ask in the redfish forum 10149474b378SRatan Gupta // till then we stop processing the next list item. 10159474b378SRatan Gupta } 10169474b378SRatan Gupta entryIdx++; 10179474b378SRatan Gupta continue; // not an error as per the redfish spec. 10189474b378SRatan Gupta } 10199474b378SRatan Gupta 1020537174c4SEd Tanous std::optional<std::string> address; 1021537174c4SEd Tanous std::optional<std::string> addressOrigin; 1022537174c4SEd Tanous std::optional<std::string> subnetMask; 1023537174c4SEd Tanous std::optional<std::string> gateway; 1024537174c4SEd Tanous 1025537174c4SEd Tanous if (!json_util::readJson(thisJson, asyncResp->res, "Address", 1026537174c4SEd Tanous address, "AddressOrigin", addressOrigin, 1027537174c4SEd Tanous "SubnetMask", subnetMask, "Gateway", 1028537174c4SEd Tanous gateway)) 1029537174c4SEd Tanous { 1030537174c4SEd Tanous return; 1031179db1d7SKowalski, Kamil } 1032179db1d7SKowalski, Kamil 1033537174c4SEd Tanous if (address) 10341abe55efSEd Tanous { 1035537174c4SEd Tanous if (!ipv4VerifyIpAndGetBitcount(*address)) 10361abe55efSEd Tanous { 1037537174c4SEd Tanous messages::propertyValueFormatError(asyncResp->res, *address, 10384a0cb85cSEd Tanous pathString + "/Address"); 1039537174c4SEd Tanous return; 10404a0cb85cSEd Tanous } 10414a0cb85cSEd Tanous } 10424a0cb85cSEd Tanous 1043537174c4SEd Tanous uint8_t prefixLength = 0; 1044537174c4SEd Tanous if (subnetMask) 10454a0cb85cSEd Tanous { 1046537174c4SEd Tanous if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength)) 10474a0cb85cSEd Tanous { 1048f12894f8SJason M. Bills messages::propertyValueFormatError( 1049537174c4SEd Tanous asyncResp->res, *subnetMask, 10504a0cb85cSEd Tanous pathString + "/SubnetMask"); 1051537174c4SEd Tanous return; 10524a0cb85cSEd Tanous } 10534a0cb85cSEd Tanous } 10544a0cb85cSEd Tanous std::string addressOriginInDBusFormat; 1055537174c4SEd Tanous if (addressOrigin) 10564a0cb85cSEd Tanous { 10574a0cb85cSEd Tanous // Get Address origin in proper format 10584a0cb85cSEd Tanous addressOriginInDBusFormat = 1059537174c4SEd Tanous translateAddressOriginRedfishToDbus(*addressOrigin); 10604a0cb85cSEd Tanous if (addressOriginInDBusFormat.empty()) 10614a0cb85cSEd Tanous { 10624a0cb85cSEd Tanous messages::propertyValueNotInList( 1063537174c4SEd Tanous asyncResp->res, *addressOrigin, 1064a08b46ccSJason M. Bills pathString + "/AddressOrigin"); 1065537174c4SEd Tanous return; 10664a0cb85cSEd Tanous } 10674a0cb85cSEd Tanous } 10684a0cb85cSEd Tanous 1069537174c4SEd Tanous if (gateway) 10704a0cb85cSEd Tanous { 1071537174c4SEd Tanous if (!ipv4VerifyIpAndGetBitcount(*gateway)) 10724a0cb85cSEd Tanous { 1073537174c4SEd Tanous messages::propertyValueFormatError(asyncResp->res, *gateway, 1074537174c4SEd Tanous pathString + "/Gateway"); 1075537174c4SEd Tanous return; 10764a0cb85cSEd Tanous } 10774a0cb85cSEd Tanous } 10784a0cb85cSEd Tanous 1079f476acbfSRatan Gupta // if IP address exist then modify it. 10804a0cb85cSEd Tanous if (thisData != ipv4Data.end()) 10814a0cb85cSEd Tanous { 1082179db1d7SKowalski, Kamil // Apply changes 1083537174c4SEd Tanous if (address) 10841abe55efSEd Tanous { 1085f476acbfSRatan Gupta auto callback = [asyncResp, entryIdx, 1086f476acbfSRatan Gupta address{std::string(*address)}]( 10874a0cb85cSEd Tanous const boost::system::error_code ec) { 10884a0cb85cSEd Tanous if (ec) 10891abe55efSEd Tanous { 1090a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 10914a0cb85cSEd Tanous return; 10924a0cb85cSEd Tanous } 1093f476acbfSRatan Gupta asyncResp->res 1094f476acbfSRatan Gupta .jsonValue["IPv4Addresses"][entryIdx]["Address"] = 1095f476acbfSRatan Gupta std::move(address); 10964a0cb85cSEd Tanous }; 10974a0cb85cSEd Tanous 10984a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 10994a0cb85cSEd Tanous std::move(callback), "xyz.openbmc_project.Network", 1100f476acbfSRatan Gupta "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + 1101f476acbfSRatan Gupta thisData->id, 11024a0cb85cSEd Tanous "org.freedesktop.DBus.Properties", "Set", 11034a0cb85cSEd Tanous "xyz.openbmc_project.Network.IP", "Address", 1104537174c4SEd Tanous std::variant<std::string>(*address)); 1105179db1d7SKowalski, Kamil } 1106179db1d7SKowalski, Kamil 1107537174c4SEd Tanous if (subnetMask) 11081abe55efSEd Tanous { 11094a0cb85cSEd Tanous changeIPv4SubnetMaskProperty(ifaceId, entryIdx, 1110537174c4SEd Tanous thisData->id, *subnetMask, 1111537174c4SEd Tanous prefixLength, asyncResp); 1112179db1d7SKowalski, Kamil } 1113179db1d7SKowalski, Kamil 1114537174c4SEd Tanous if (addressOrigin) 11151abe55efSEd Tanous { 11164a0cb85cSEd Tanous changeIPv4Origin(ifaceId, entryIdx, thisData->id, 1117f476acbfSRatan Gupta *addressOrigin, addressOriginInDBusFormat, 1118f476acbfSRatan Gupta asyncResp); 1119179db1d7SKowalski, Kamil } 1120179db1d7SKowalski, Kamil 1121537174c4SEd Tanous if (gateway) 11221abe55efSEd Tanous { 1123f476acbfSRatan Gupta auto callback = [asyncResp, entryIdx, 1124537174c4SEd Tanous gateway{std::string(*gateway)}]( 11254a0cb85cSEd Tanous const boost::system::error_code ec) { 11264a0cb85cSEd Tanous if (ec) 11271abe55efSEd Tanous { 1128a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 11294a0cb85cSEd Tanous return; 11304a0cb85cSEd Tanous } 1131f476acbfSRatan Gupta asyncResp->res 1132f476acbfSRatan Gupta .jsonValue["IPv4Addresses"][entryIdx]["Gateway"] = 1133537174c4SEd Tanous std::move(gateway); 11344a0cb85cSEd Tanous }; 11354a0cb85cSEd Tanous 11364a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 11374a0cb85cSEd Tanous std::move(callback), "xyz.openbmc_project.Network", 1138f476acbfSRatan Gupta "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + 1139f476acbfSRatan Gupta thisData->id, 11404a0cb85cSEd Tanous "org.freedesktop.DBus.Properties", "Set", 11414a0cb85cSEd Tanous "xyz.openbmc_project.Network.IP", "Gateway", 1142537174c4SEd Tanous std::variant<std::string>(*gateway)); 11434a0cb85cSEd Tanous } 1144f476acbfSRatan Gupta 11454a0cb85cSEd Tanous thisData++; 11461abe55efSEd Tanous } 11471abe55efSEd Tanous else 11481abe55efSEd Tanous { 11494a0cb85cSEd Tanous // Create IPv4 with provided data 1150537174c4SEd Tanous if (!gateway) 11511abe55efSEd Tanous { 1152a08b46ccSJason M. Bills messages::propertyMissing(asyncResp->res, 11534a0cb85cSEd Tanous pathString + "/Gateway"); 11544a0cb85cSEd Tanous continue; 11554a0cb85cSEd Tanous } 11564a0cb85cSEd Tanous 1157537174c4SEd Tanous if (!address) 11581abe55efSEd Tanous { 1159a08b46ccSJason M. Bills messages::propertyMissing(asyncResp->res, 11604a0cb85cSEd Tanous pathString + "/Address"); 11614a0cb85cSEd Tanous continue; 11624a0cb85cSEd Tanous } 11634a0cb85cSEd Tanous 1164537174c4SEd Tanous if (!subnetMask) 11651abe55efSEd Tanous { 1166a08b46ccSJason M. Bills messages::propertyMissing(asyncResp->res, 11674a0cb85cSEd Tanous pathString + "/SubnetMask"); 11684a0cb85cSEd Tanous continue; 1169588c3f0dSKowalski, Kamil } 1170588c3f0dSKowalski, Kamil 1171b01bf299SEd Tanous createIPv4(ifaceId, entryIdx, prefixLength, *gateway, *address, 1172b01bf299SEd Tanous asyncResp); 117395897b20SRatan Gupta 117495897b20SRatan Gupta nlohmann::json &ipv4AddressJson = 117595897b20SRatan Gupta asyncResp->res.jsonValue["IPv4Addresses"][entryIdx]; 117695897b20SRatan Gupta ipv4AddressJson["Address"] = *address; 117795897b20SRatan Gupta ipv4AddressJson["SubnetMask"] = *subnetMask; 117895897b20SRatan Gupta ipv4AddressJson["Gateway"] = *gateway; 11794a0cb85cSEd Tanous } 11804a0cb85cSEd Tanous entryIdx++; 11814a0cb85cSEd Tanous } 11824a0cb85cSEd Tanous } 11834a0cb85cSEd Tanous 1184f85837bfSRAJESWARAN THILLAIGOVINDAN void handleStaticNameServersPatch( 1185f85837bfSRAJESWARAN THILLAIGOVINDAN const std::string &ifaceId, 1186f85837bfSRAJESWARAN THILLAIGOVINDAN const std::vector<std::string> &updatedStaticNameServers, 1187f85837bfSRAJESWARAN THILLAIGOVINDAN const std::shared_ptr<AsyncResp> &asyncResp) 1188f85837bfSRAJESWARAN THILLAIGOVINDAN { 1189f85837bfSRAJESWARAN THILLAIGOVINDAN crow::connections::systemBus->async_method_call( 1190f85837bfSRAJESWARAN THILLAIGOVINDAN [asyncResp, 1191f85837bfSRAJESWARAN THILLAIGOVINDAN updatedStaticNameServers](const boost::system::error_code ec) { 1192f85837bfSRAJESWARAN THILLAIGOVINDAN if (ec) 1193f85837bfSRAJESWARAN THILLAIGOVINDAN { 1194f85837bfSRAJESWARAN THILLAIGOVINDAN messages::internalError(asyncResp->res); 1195f85837bfSRAJESWARAN THILLAIGOVINDAN return; 1196f85837bfSRAJESWARAN THILLAIGOVINDAN } 1197f85837bfSRAJESWARAN THILLAIGOVINDAN asyncResp->res.jsonValue["NameServers"] = 1198f85837bfSRAJESWARAN THILLAIGOVINDAN updatedStaticNameServers; 1199f85837bfSRAJESWARAN THILLAIGOVINDAN asyncResp->res.jsonValue["StaticNameServers"] = 1200f85837bfSRAJESWARAN THILLAIGOVINDAN updatedStaticNameServers; 1201f85837bfSRAJESWARAN THILLAIGOVINDAN }, 1202f85837bfSRAJESWARAN THILLAIGOVINDAN "xyz.openbmc_project.Network", 1203f85837bfSRAJESWARAN THILLAIGOVINDAN "/xyz/openbmc_project/network/" + ifaceId, 1204f85837bfSRAJESWARAN THILLAIGOVINDAN "org.freedesktop.DBus.Properties", "Set", 1205f85837bfSRAJESWARAN THILLAIGOVINDAN "xyz.openbmc_project.Network.EthernetInterface", "Nameservers", 1206f85837bfSRAJESWARAN THILLAIGOVINDAN std::variant<std::vector<std::string>>{updatedStaticNameServers}); 1207f85837bfSRAJESWARAN THILLAIGOVINDAN } 1208f85837bfSRAJESWARAN THILLAIGOVINDAN 12090f74e643SEd Tanous void parseInterfaceData( 12100f74e643SEd Tanous nlohmann::json &json_response, const std::string &iface_id, 12110f74e643SEd Tanous const EthernetInterfaceData ðData, 12124a0cb85cSEd Tanous const boost::container::flat_set<IPv4AddressData> &ipv4Data) 12134a0cb85cSEd Tanous { 12144a0cb85cSEd Tanous json_response["Id"] = iface_id; 12154a0cb85cSEd Tanous json_response["@odata.id"] = 12164a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id; 1217029573d4SEd Tanous json_response["InterfaceEnabled"] = true; 1218029573d4SEd Tanous if (ethData.speed == 0) 1219029573d4SEd Tanous { 1220029573d4SEd Tanous json_response["LinkStatus"] = "NoLink"; 1221029573d4SEd Tanous json_response["Status"] = { 1222029573d4SEd Tanous {"Health", "OK"}, 1223029573d4SEd Tanous {"State", "Disabled"}, 1224029573d4SEd Tanous }; 1225029573d4SEd Tanous } 1226029573d4SEd Tanous else 1227029573d4SEd Tanous { 1228029573d4SEd Tanous json_response["LinkStatus"] = "LinkUp"; 1229029573d4SEd Tanous json_response["Status"] = { 1230029573d4SEd Tanous {"Health", "OK"}, 1231029573d4SEd Tanous {"State", "Enabled"}, 1232029573d4SEd Tanous }; 1233029573d4SEd Tanous } 12344a0cb85cSEd Tanous json_response["SpeedMbps"] = ethData.speed; 12354a0cb85cSEd Tanous json_response["MACAddress"] = ethData.mac_address; 1236fda13ad2SSunitha Harish json_response["DHCPv4"]["DHCPEnabled"] = ethData.DHCPEnabled; 12372a133282Smanojkiraneda 12384a0cb85cSEd Tanous if (!ethData.hostname.empty()) 12394a0cb85cSEd Tanous { 12404a0cb85cSEd Tanous json_response["HostName"] = ethData.hostname; 12414a0cb85cSEd Tanous } 12424a0cb85cSEd Tanous 1243fda13ad2SSunitha Harish json_response["VLANs"] = { 1244fda13ad2SSunitha Harish {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1245fda13ad2SSunitha Harish iface_id + "/VLANs"}}; 1246fda13ad2SSunitha Harish 1247029573d4SEd Tanous json_response["NameServers"] = ethData.nameservers; 1248f85837bfSRAJESWARAN THILLAIGOVINDAN json_response["StaticNameServers"] = ethData.nameservers; 12494a0cb85cSEd Tanous 12504a0cb85cSEd Tanous if (ipv4Data.size() > 0) 12514a0cb85cSEd Tanous { 12524a0cb85cSEd Tanous nlohmann::json &ipv4_array = json_response["IPv4Addresses"]; 12534a0cb85cSEd Tanous ipv4_array = nlohmann::json::array(); 12544a0cb85cSEd Tanous for (auto &ipv4_config : ipv4Data) 12554a0cb85cSEd Tanous { 1256fa5053a6SGunnar Mills 1257fa5053a6SGunnar Mills std::string gatewayStr = ipv4_config.gateway; 1258fa5053a6SGunnar Mills if (gatewayStr.empty()) 1259fa5053a6SGunnar Mills { 1260fa5053a6SGunnar Mills gatewayStr = "0.0.0.0"; 1261fa5053a6SGunnar Mills } 1262fa5053a6SGunnar Mills 12634a0cb85cSEd Tanous ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin}, 12644a0cb85cSEd Tanous {"SubnetMask", ipv4_config.netmask}, 1265029573d4SEd Tanous {"Address", ipv4_config.address}, 1266fa5053a6SGunnar Mills {"Gateway", gatewayStr}}); 12674a0cb85cSEd Tanous } 12684a0cb85cSEd Tanous } 12699a6fc6feSRavi Teja json_response["IPv6DefaultGateway"] = ethData.ipv6_default_gateway; 1270588c3f0dSKowalski, Kamil } 1271588c3f0dSKowalski, Kamil 12729391bb9cSRapkiewicz, Pawel /** 12739391bb9cSRapkiewicz, Pawel * Functions triggers appropriate requests on DBus 12749391bb9cSRapkiewicz, Pawel */ 127555c7b7a2SEd Tanous void doGet(crow::Response &res, const crow::Request &req, 12761abe55efSEd Tanous const std::vector<std::string> ¶ms) override 12771abe55efSEd Tanous { 12784a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 12791abe55efSEd Tanous if (params.size() != 1) 12801abe55efSEd Tanous { 1281f12894f8SJason M. Bills messages::internalError(asyncResp->res); 12829391bb9cSRapkiewicz, Pawel return; 12839391bb9cSRapkiewicz, Pawel } 12849391bb9cSRapkiewicz, Pawel 12854a0cb85cSEd Tanous getEthernetIfaceData( 12864a0cb85cSEd Tanous params[0], 12874a0cb85cSEd Tanous [this, asyncResp, iface_id{std::string(params[0])}]( 12884a0cb85cSEd Tanous const bool &success, const EthernetInterfaceData ðData, 12894a0cb85cSEd Tanous const boost::container::flat_set<IPv4AddressData> &ipv4Data) { 12904a0cb85cSEd Tanous if (!success) 12911abe55efSEd Tanous { 12921abe55efSEd Tanous // TODO(Pawel)consider distinguish between non existing 12931abe55efSEd Tanous // object, and other errors 1294f12894f8SJason M. Bills messages::resourceNotFound(asyncResp->res, 1295f12894f8SJason M. Bills "EthernetInterface", iface_id); 12964a0cb85cSEd Tanous return; 12979391bb9cSRapkiewicz, Pawel } 1298*4c9afe43SEd Tanous 1299*4c9afe43SEd Tanous // because this has no dependence on the interface at this 1300*4c9afe43SEd Tanous // point, it needs to be done after we know the interface 1301*4c9afe43SEd Tanous // exists, not before. 1302*4c9afe43SEd Tanous getDHCPConfigData(asyncResp); 1303*4c9afe43SEd Tanous 13040f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1305fda13ad2SSunitha Harish "#EthernetInterface.v1_4_1.EthernetInterface"; 13060f74e643SEd Tanous asyncResp->res.jsonValue["@odata.context"] = 13070f74e643SEd Tanous "/redfish/v1/$metadata#EthernetInterface.EthernetInterface"; 13080f74e643SEd Tanous asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface"; 13090f74e643SEd Tanous asyncResp->res.jsonValue["Description"] = 13100f74e643SEd Tanous "Management Network Interface"; 13110f74e643SEd Tanous 13120f74e643SEd Tanous parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData, 13130f74e643SEd Tanous ipv4Data); 13149391bb9cSRapkiewicz, Pawel }); 13159391bb9cSRapkiewicz, Pawel } 13169391bb9cSRapkiewicz, Pawel 131755c7b7a2SEd Tanous void doPatch(crow::Response &res, const crow::Request &req, 13181abe55efSEd Tanous const std::vector<std::string> ¶ms) override 13191abe55efSEd Tanous { 13204a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 13211abe55efSEd Tanous if (params.size() != 1) 13221abe55efSEd Tanous { 1323f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1324588c3f0dSKowalski, Kamil return; 1325588c3f0dSKowalski, Kamil } 1326588c3f0dSKowalski, Kamil 13274a0cb85cSEd Tanous const std::string &iface_id = params[0]; 1328588c3f0dSKowalski, Kamil 1329bc0bd6e0SEd Tanous std::optional<std::string> hostname; 1330d577665bSRatan Gupta std::optional<std::string> macAddress; 13319a6fc6feSRavi Teja std::optional<std::string> ipv6DefaultGateway; 1332f476acbfSRatan Gupta std::optional<nlohmann::json> ipv4Addresses; 1333f476acbfSRatan Gupta std::optional<nlohmann::json> ipv6Addresses; 1334f85837bfSRAJESWARAN THILLAIGOVINDAN std::optional<std::vector<std::string>> staticNameServers; 13355112e9b4SRAJESWARAN THILLAIGOVINDAN std::optional<std::vector<std::string>> nameServers; 13360627a2c7SEd Tanous 1337fda13ad2SSunitha Harish if (!json_util::readJson( 1338fda13ad2SSunitha Harish req, res, "HostName", hostname, "IPv4Addresses", ipv4Addresses, 1339f85837bfSRAJESWARAN THILLAIGOVINDAN "IPv6Addresses", ipv6Addresses, "MACAddress", macAddress, 13409a6fc6feSRavi Teja "StaticNameServers", staticNameServers, "IPv6DefaultGateway", 13415112e9b4SRAJESWARAN THILLAIGOVINDAN ipv6DefaultGateway, "NameServers", nameServers)) 13421abe55efSEd Tanous { 1343588c3f0dSKowalski, Kamil return; 1344588c3f0dSKowalski, Kamil } 1345f15aad37SRatan Gupta 13464a0cb85cSEd Tanous // Get single eth interface data, and call the below callback for JSON 1347588c3f0dSKowalski, Kamil // preparation 13484a0cb85cSEd Tanous getEthernetIfaceData( 13494a0cb85cSEd Tanous iface_id, 1350fda13ad2SSunitha Harish [this, asyncResp, iface_id, hostname = std::move(hostname), 1351fda13ad2SSunitha Harish macAddress = std::move(macAddress), 13520627a2c7SEd Tanous ipv4Addresses = std::move(ipv4Addresses), 1353f85837bfSRAJESWARAN THILLAIGOVINDAN ipv6Addresses = std::move(ipv6Addresses), 13549a6fc6feSRavi Teja ipv6DefaultGateway = std::move(ipv6DefaultGateway), 13555112e9b4SRAJESWARAN THILLAIGOVINDAN staticNameServers = std::move(staticNameServers), 13565112e9b4SRAJESWARAN THILLAIGOVINDAN nameServers = std::move(nameServers)]( 13574a0cb85cSEd Tanous const bool &success, const EthernetInterfaceData ðData, 13584a0cb85cSEd Tanous const boost::container::flat_set<IPv4AddressData> &ipv4Data) { 13591abe55efSEd Tanous if (!success) 13601abe55efSEd Tanous { 1361588c3f0dSKowalski, Kamil // ... otherwise return error 13621abe55efSEd Tanous // TODO(Pawel)consider distinguish between non existing 13631abe55efSEd Tanous // object, and other errors 1364fda13ad2SSunitha Harish messages::resourceNotFound(asyncResp->res, 1365fda13ad2SSunitha Harish "Ethernet Interface", iface_id); 1366588c3f0dSKowalski, Kamil return; 1367588c3f0dSKowalski, Kamil } 1368588c3f0dSKowalski, Kamil 13690f74e643SEd Tanous parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData, 13700f74e643SEd Tanous ipv4Data); 1371588c3f0dSKowalski, Kamil 13720627a2c7SEd Tanous if (hostname) 13731abe55efSEd Tanous { 13740627a2c7SEd Tanous handleHostnamePatch(*hostname, asyncResp); 13751abe55efSEd Tanous } 13760627a2c7SEd Tanous 1377d577665bSRatan Gupta if (macAddress) 1378d577665bSRatan Gupta { 1379d577665bSRatan Gupta handleMACAddressPatch(iface_id, *macAddress, asyncResp); 1380d577665bSRatan Gupta } 1381d577665bSRatan Gupta 13820627a2c7SEd Tanous if (ipv4Addresses) 13831abe55efSEd Tanous { 1384537174c4SEd Tanous // TODO(ed) for some reason the capture of ipv4Addresses 1385537174c4SEd Tanous // above is returning a const value, not a non-const value. 1386537174c4SEd Tanous // This doesn't really work for us, as we need to be able to 1387537174c4SEd Tanous // efficiently move out the intermedia nlohmann::json 1388537174c4SEd Tanous // objects. This makes a copy of the structure, and operates 1389537174c4SEd Tanous // on that, but could be done more efficiently 1390f476acbfSRatan Gupta nlohmann::json ipv4 = std::move(*ipv4Addresses); 1391537174c4SEd Tanous handleIPv4Patch(iface_id, ipv4, ipv4Data, asyncResp); 13921abe55efSEd Tanous } 13930627a2c7SEd Tanous 13945112e9b4SRAJESWARAN THILLAIGOVINDAN if (nameServers) 13955112e9b4SRAJESWARAN THILLAIGOVINDAN { 13965112e9b4SRAJESWARAN THILLAIGOVINDAN // Data.Permissions is read-only 13975112e9b4SRAJESWARAN THILLAIGOVINDAN messages::propertyNotWritable(asyncResp->res, 13985112e9b4SRAJESWARAN THILLAIGOVINDAN "NameServers"); 13995112e9b4SRAJESWARAN THILLAIGOVINDAN } 14005112e9b4SRAJESWARAN THILLAIGOVINDAN 14010627a2c7SEd Tanous if (ipv6Addresses) 14021abe55efSEd Tanous { 1403179db1d7SKowalski, Kamil // TODO(kkowalsk) IPv6 Not supported on D-Bus yet 1404a08b46ccSJason M. Bills messages::propertyNotWritable(asyncResp->res, 14050627a2c7SEd Tanous "IPv6Addresses"); 1406588c3f0dSKowalski, Kamil } 1407f85837bfSRAJESWARAN THILLAIGOVINDAN 1408f85837bfSRAJESWARAN THILLAIGOVINDAN if (staticNameServers) 1409f85837bfSRAJESWARAN THILLAIGOVINDAN { 1410f85837bfSRAJESWARAN THILLAIGOVINDAN handleStaticNameServersPatch(iface_id, *staticNameServers, 1411f85837bfSRAJESWARAN THILLAIGOVINDAN asyncResp); 1412f85837bfSRAJESWARAN THILLAIGOVINDAN } 14139a6fc6feSRavi Teja 14149a6fc6feSRavi Teja if (ipv6DefaultGateway) 14159a6fc6feSRavi Teja { 14169a6fc6feSRavi Teja messages::propertyNotWritable(asyncResp->res, 14179a6fc6feSRavi Teja "IPv6DefaultGateway"); 14189a6fc6feSRavi Teja } 1419588c3f0dSKowalski, Kamil }); 1420588c3f0dSKowalski, Kamil } 14219391bb9cSRapkiewicz, Pawel }; 14229391bb9cSRapkiewicz, Pawel 1423e439f0f8SKowalski, Kamil /** 14244a0cb85cSEd Tanous * VlanNetworkInterface derived class for delivering VLANNetworkInterface 14254a0cb85cSEd Tanous * Schema 1426e439f0f8SKowalski, Kamil */ 14271abe55efSEd Tanous class VlanNetworkInterface : public Node 14281abe55efSEd Tanous { 1429e439f0f8SKowalski, Kamil public: 1430e439f0f8SKowalski, Kamil /* 1431e439f0f8SKowalski, Kamil * Default Constructor 1432e439f0f8SKowalski, Kamil */ 1433e439f0f8SKowalski, Kamil template <typename CrowApp> 14341abe55efSEd Tanous VlanNetworkInterface(CrowApp &app) : 14354a0cb85cSEd Tanous Node(app, 14360f74e643SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>", 14371abe55efSEd Tanous std::string(), std::string()) 14381abe55efSEd Tanous { 1439e439f0f8SKowalski, Kamil entityPrivileges = { 1440e439f0f8SKowalski, Kamil {boost::beast::http::verb::get, {{"Login"}}}, 1441e439f0f8SKowalski, Kamil {boost::beast::http::verb::head, {{"Login"}}}, 1442e439f0f8SKowalski, Kamil {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 1443e439f0f8SKowalski, Kamil {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 1444e439f0f8SKowalski, Kamil {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 1445e439f0f8SKowalski, Kamil {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 1446e439f0f8SKowalski, Kamil } 1447e439f0f8SKowalski, Kamil 1448e439f0f8SKowalski, Kamil private: 14490f74e643SEd Tanous void parseInterfaceData( 14500f74e643SEd Tanous nlohmann::json &json_response, const std::string &parent_iface_id, 14510f74e643SEd Tanous const std::string &iface_id, const EthernetInterfaceData ðData, 14524a0cb85cSEd Tanous const boost::container::flat_set<IPv4AddressData> &ipv4Data) 14531abe55efSEd Tanous { 1454e439f0f8SKowalski, Kamil // Fill out obvious data... 14554a0cb85cSEd Tanous json_response["Id"] = iface_id; 14564a0cb85cSEd Tanous json_response["@odata.id"] = 14574a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id + 14584a0cb85cSEd Tanous "/VLANs/" + iface_id; 1459e439f0f8SKowalski, Kamil 14604a0cb85cSEd Tanous json_response["VLANEnable"] = true; 1461fda13ad2SSunitha Harish if (!ethData.vlan_id.empty()) 14624a0cb85cSEd Tanous { 1463fda13ad2SSunitha Harish json_response["VLANId"] = ethData.vlan_id.back(); 14644a0cb85cSEd Tanous } 1465e439f0f8SKowalski, Kamil } 1466e439f0f8SKowalski, Kamil 1467fda13ad2SSunitha Harish bool verifyNames(const std::string &parent, const std::string &iface) 14681abe55efSEd Tanous { 14691abe55efSEd Tanous if (!boost::starts_with(iface, parent + "_")) 14701abe55efSEd Tanous { 1471927a505aSKowalski, Kamil return false; 14721abe55efSEd Tanous } 14731abe55efSEd Tanous else 14741abe55efSEd Tanous { 1475927a505aSKowalski, Kamil return true; 1476927a505aSKowalski, Kamil } 1477927a505aSKowalski, Kamil } 1478927a505aSKowalski, Kamil 1479e439f0f8SKowalski, Kamil /** 1480e439f0f8SKowalski, Kamil * Functions triggers appropriate requests on DBus 1481e439f0f8SKowalski, Kamil */ 148255c7b7a2SEd Tanous void doGet(crow::Response &res, const crow::Request &req, 14831abe55efSEd Tanous const std::vector<std::string> ¶ms) override 14841abe55efSEd Tanous { 14854a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 14864a0cb85cSEd Tanous // TODO(Pawel) this shall be parameterized call (two params) to get 1487e439f0f8SKowalski, Kamil // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'. 1488e439f0f8SKowalski, Kamil // Check if there is required param, truly entering this shall be 1489e439f0f8SKowalski, Kamil // impossible. 14901abe55efSEd Tanous if (params.size() != 2) 14911abe55efSEd Tanous { 1492f12894f8SJason M. Bills messages::internalError(res); 1493e439f0f8SKowalski, Kamil res.end(); 1494e439f0f8SKowalski, Kamil return; 1495e439f0f8SKowalski, Kamil } 1496e439f0f8SKowalski, Kamil 14974a0cb85cSEd Tanous const std::string &parent_iface_id = params[0]; 14984a0cb85cSEd Tanous const std::string &iface_id = params[1]; 14990f74e643SEd Tanous res.jsonValue["@odata.type"] = 15000f74e643SEd Tanous "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; 15010f74e643SEd Tanous res.jsonValue["@odata.context"] = 15020f74e643SEd Tanous "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface"; 15030f74e643SEd Tanous res.jsonValue["Name"] = "VLAN Network Interface"; 1504e439f0f8SKowalski, Kamil 1505fda13ad2SSunitha Harish if (!verifyNames(parent_iface_id, iface_id)) 15061abe55efSEd Tanous { 1507a434f2bdSEd Tanous return; 1508a434f2bdSEd Tanous } 1509a434f2bdSEd Tanous 1510e439f0f8SKowalski, Kamil // Get single eth interface data, and call the below callback for JSON 1511e439f0f8SKowalski, Kamil // preparation 15124a0cb85cSEd Tanous getEthernetIfaceData( 1513fda13ad2SSunitha Harish params[1], 1514fda13ad2SSunitha Harish [this, asyncResp, parent_iface_id{std::string(params[0])}, 1515fda13ad2SSunitha Harish iface_id{std::string(params[1])}]( 15164a0cb85cSEd Tanous const bool &success, const EthernetInterfaceData ðData, 15174a0cb85cSEd Tanous const boost::container::flat_set<IPv4AddressData> &ipv4Data) { 1518fda13ad2SSunitha Harish if (success && ethData.vlan_id.size() != 0) 15191abe55efSEd Tanous { 15200f74e643SEd Tanous parseInterfaceData(asyncResp->res.jsonValue, 15210f74e643SEd Tanous parent_iface_id, iface_id, ethData, 15220f74e643SEd Tanous ipv4Data); 15231abe55efSEd Tanous } 15241abe55efSEd Tanous else 15251abe55efSEd Tanous { 1526e439f0f8SKowalski, Kamil // ... otherwise return error 15271abe55efSEd Tanous // TODO(Pawel)consider distinguish between non existing 15281abe55efSEd Tanous // object, and other errors 1529f12894f8SJason M. Bills messages::resourceNotFound( 1530f12894f8SJason M. Bills asyncResp->res, "VLAN Network Interface", iface_id); 1531e439f0f8SKowalski, Kamil } 1532e439f0f8SKowalski, Kamil }); 1533e439f0f8SKowalski, Kamil } 1534e439f0f8SKowalski, Kamil 153555c7b7a2SEd Tanous void doPatch(crow::Response &res, const crow::Request &req, 15361abe55efSEd Tanous const std::vector<std::string> ¶ms) override 15371abe55efSEd Tanous { 15384a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 15391abe55efSEd Tanous if (params.size() != 2) 15401abe55efSEd Tanous { 1541f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1542e439f0f8SKowalski, Kamil return; 1543e439f0f8SKowalski, Kamil } 1544e439f0f8SKowalski, Kamil 1545d76323e5SEd Tanous const std::string &parentIfaceId = params[0]; 154655c7b7a2SEd Tanous const std::string &ifaceId = params[1]; 1547927a505aSKowalski, Kamil 1548fda13ad2SSunitha Harish if (!verifyNames(parentIfaceId, ifaceId)) 15491abe55efSEd Tanous { 1550fda13ad2SSunitha Harish messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", 1551fda13ad2SSunitha Harish ifaceId); 1552927a505aSKowalski, Kamil return; 1553927a505aSKowalski, Kamil } 1554927a505aSKowalski, Kamil 15550627a2c7SEd Tanous bool vlanEnable = false; 15560627a2c7SEd Tanous uint64_t vlanId = 0; 15570627a2c7SEd Tanous 15580627a2c7SEd Tanous if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId", 15590627a2c7SEd Tanous vlanId)) 15601abe55efSEd Tanous { 1561927a505aSKowalski, Kamil return; 1562927a505aSKowalski, Kamil } 1563927a505aSKowalski, Kamil 1564927a505aSKowalski, Kamil // Get single eth interface data, and call the below callback for JSON 1565927a505aSKowalski, Kamil // preparation 156608244d02SSunitha Harish getEthernetIfaceData(params[1], [this, asyncResp, 156708244d02SSunitha Harish parentIfaceId{std::string(params[0])}, 156808244d02SSunitha Harish ifaceId{std::string(params[1])}, 156908244d02SSunitha Harish &vlanEnable, &vlanId]( 157008244d02SSunitha Harish const bool &success, 157108244d02SSunitha Harish const EthernetInterfaceData 157208244d02SSunitha Harish ðData, 157308244d02SSunitha Harish const boost::container::flat_set< 157408244d02SSunitha Harish IPv4AddressData> &ipv4Data) { 157508244d02SSunitha Harish if (success && !ethData.vlan_id.empty()) 157608244d02SSunitha Harish { 157708244d02SSunitha Harish parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId, 157808244d02SSunitha Harish ifaceId, ethData, ipv4Data); 157908244d02SSunitha Harish auto callback = 158008244d02SSunitha Harish [asyncResp](const boost::system::error_code ec) { 158108244d02SSunitha Harish if (ec) 158208244d02SSunitha Harish { 158308244d02SSunitha Harish messages::internalError(asyncResp->res); 158408244d02SSunitha Harish } 158508244d02SSunitha Harish }; 158608244d02SSunitha Harish 158708244d02SSunitha Harish if (vlanEnable == true) 158808244d02SSunitha Harish { 158908244d02SSunitha Harish crow::connections::systemBus->async_method_call( 159008244d02SSunitha Harish std::move(callback), "xyz.openbmc_project.Network", 159108244d02SSunitha Harish "/xyz/openbmc_project/network/" + ifaceId, 159208244d02SSunitha Harish "org.freedesktop.DBus.Properties", "Set", 159308244d02SSunitha Harish "xyz.openbmc_project.Network.VLAN", "Id", 159408244d02SSunitha Harish std::variant<uint32_t>(vlanId)); 159508244d02SSunitha Harish } 159608244d02SSunitha Harish else 159708244d02SSunitha Harish { 159808244d02SSunitha Harish BMCWEB_LOG_DEBUG 159908244d02SSunitha Harish << "vlanEnable is false. Deleting the vlan interface"; 160008244d02SSunitha Harish crow::connections::systemBus->async_method_call( 160108244d02SSunitha Harish std::move(callback), "xyz.openbmc_project.Network", 160208244d02SSunitha Harish std::string("/xyz/openbmc_project/network/") + ifaceId, 160308244d02SSunitha Harish "xyz.openbmc_project.Object.Delete", "Delete"); 160408244d02SSunitha Harish } 160508244d02SSunitha Harish } 160608244d02SSunitha Harish else 16071abe55efSEd Tanous { 16081abe55efSEd Tanous // TODO(Pawel)consider distinguish between non existing 16091abe55efSEd Tanous // object, and other errors 161008244d02SSunitha Harish messages::resourceNotFound(asyncResp->res, 161108244d02SSunitha Harish "VLAN Network Interface", ifaceId); 1612927a505aSKowalski, Kamil return; 1613927a505aSKowalski, Kamil } 1614927a505aSKowalski, Kamil }); 1615e439f0f8SKowalski, Kamil } 1616e439f0f8SKowalski, Kamil 161755c7b7a2SEd Tanous void doDelete(crow::Response &res, const crow::Request &req, 16181abe55efSEd Tanous const std::vector<std::string> ¶ms) override 16191abe55efSEd Tanous { 16204a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 16211abe55efSEd Tanous if (params.size() != 2) 16221abe55efSEd Tanous { 1623f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1624e439f0f8SKowalski, Kamil return; 1625e439f0f8SKowalski, Kamil } 1626e439f0f8SKowalski, Kamil 1627d76323e5SEd Tanous const std::string &parentIfaceId = params[0]; 162855c7b7a2SEd Tanous const std::string &ifaceId = params[1]; 1629927a505aSKowalski, Kamil 1630fda13ad2SSunitha Harish if (!verifyNames(parentIfaceId, ifaceId)) 16311abe55efSEd Tanous { 1632fda13ad2SSunitha Harish messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", 1633fda13ad2SSunitha Harish ifaceId); 1634927a505aSKowalski, Kamil return; 1635927a505aSKowalski, Kamil } 1636927a505aSKowalski, Kamil 1637927a505aSKowalski, Kamil // Get single eth interface data, and call the below callback for JSON 1638927a505aSKowalski, Kamil // preparation 1639f12894f8SJason M. Bills getEthernetIfaceData( 1640fda13ad2SSunitha Harish params[1], 1641fda13ad2SSunitha Harish [this, asyncResp, parentIfaceId{std::string(params[0])}, 1642fda13ad2SSunitha Harish ifaceId{std::string(params[1])}]( 1643f12894f8SJason M. Bills const bool &success, const EthernetInterfaceData ðData, 1644f12894f8SJason M. Bills const boost::container::flat_set<IPv4AddressData> &ipv4Data) { 1645fda13ad2SSunitha Harish if (success && !ethData.vlan_id.empty()) 16461abe55efSEd Tanous { 16470f74e643SEd Tanous parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId, 16480f74e643SEd Tanous ifaceId, ethData, ipv4Data); 1649927a505aSKowalski, Kamil 1650f12894f8SJason M. Bills auto callback = 1651f12894f8SJason M. Bills [asyncResp](const boost::system::error_code ec) { 16521abe55efSEd Tanous if (ec) 16531abe55efSEd Tanous { 1654f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1655927a505aSKowalski, Kamil } 16564a0cb85cSEd Tanous }; 16574a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 16584a0cb85cSEd Tanous std::move(callback), "xyz.openbmc_project.Network", 16594a0cb85cSEd Tanous std::string("/xyz/openbmc_project/network/") + ifaceId, 16604a0cb85cSEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 16611abe55efSEd Tanous } 16621abe55efSEd Tanous else 16631abe55efSEd Tanous { 1664927a505aSKowalski, Kamil // ... otherwise return error 1665f12894f8SJason M. Bills // TODO(Pawel)consider distinguish between non existing 1666f12894f8SJason M. Bills // object, and other errors 1667f12894f8SJason M. Bills messages::resourceNotFound( 1668f12894f8SJason M. Bills asyncResp->res, "VLAN Network Interface", ifaceId); 1669927a505aSKowalski, Kamil } 1670927a505aSKowalski, Kamil }); 1671e439f0f8SKowalski, Kamil } 1672e439f0f8SKowalski, Kamil }; 1673e439f0f8SKowalski, Kamil 1674e439f0f8SKowalski, Kamil /** 1675e439f0f8SKowalski, Kamil * VlanNetworkInterfaceCollection derived class for delivering 1676e439f0f8SKowalski, Kamil * VLANNetworkInterface Collection Schema 1677e439f0f8SKowalski, Kamil */ 16781abe55efSEd Tanous class VlanNetworkInterfaceCollection : public Node 16791abe55efSEd Tanous { 1680e439f0f8SKowalski, Kamil public: 1681e439f0f8SKowalski, Kamil template <typename CrowApp> 16821abe55efSEd Tanous VlanNetworkInterfaceCollection(CrowApp &app) : 16834a0cb85cSEd Tanous Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/", 16844a0cb85cSEd Tanous std::string()) 16851abe55efSEd Tanous { 1686e439f0f8SKowalski, Kamil entityPrivileges = { 1687e439f0f8SKowalski, Kamil {boost::beast::http::verb::get, {{"Login"}}}, 1688e439f0f8SKowalski, Kamil {boost::beast::http::verb::head, {{"Login"}}}, 1689e439f0f8SKowalski, Kamil {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 1690e439f0f8SKowalski, Kamil {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 1691e439f0f8SKowalski, Kamil {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 1692e439f0f8SKowalski, Kamil {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 1693e439f0f8SKowalski, Kamil } 1694e439f0f8SKowalski, Kamil 1695e439f0f8SKowalski, Kamil private: 1696e439f0f8SKowalski, Kamil /** 1697e439f0f8SKowalski, Kamil * Functions triggers appropriate requests on DBus 1698e439f0f8SKowalski, Kamil */ 169955c7b7a2SEd Tanous void doGet(crow::Response &res, const crow::Request &req, 17001abe55efSEd Tanous const std::vector<std::string> ¶ms) override 17011abe55efSEd Tanous { 17024a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 17031abe55efSEd Tanous if (params.size() != 1) 17041abe55efSEd Tanous { 1705e439f0f8SKowalski, Kamil // This means there is a problem with the router 1706f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1707e439f0f8SKowalski, Kamil return; 1708e439f0f8SKowalski, Kamil } 1709e439f0f8SKowalski, Kamil 17104a0cb85cSEd Tanous const std::string &rootInterfaceName = params[0]; 1711e439f0f8SKowalski, Kamil 17124a0cb85cSEd Tanous // Get eth interface list, and call the below callback for JSON 17131abe55efSEd Tanous // preparation 1714f12894f8SJason M. Bills getEthernetIfaceList( 171543b761d0SEd Tanous [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}]( 17161abe55efSEd Tanous const bool &success, 1717*4c9afe43SEd Tanous const boost::container::flat_set<std::string> &iface_list) { 17184a0cb85cSEd Tanous if (!success) 17191abe55efSEd Tanous { 1720f12894f8SJason M. Bills messages::internalError(asyncResp->res); 17214a0cb85cSEd Tanous return; 17221abe55efSEd Tanous } 1723*4c9afe43SEd Tanous 1724*4c9afe43SEd Tanous if (iface_list.find(rootInterfaceName) == iface_list.end()) 1725*4c9afe43SEd Tanous { 1726*4c9afe43SEd Tanous messages::resourceNotFound(asyncResp->res, 1727*4c9afe43SEd Tanous "VLanNetworkInterfaceCollection", 1728*4c9afe43SEd Tanous rootInterfaceName); 1729*4c9afe43SEd Tanous return; 1730*4c9afe43SEd Tanous } 1731*4c9afe43SEd Tanous 17320f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 17330f74e643SEd Tanous "#VLanNetworkInterfaceCollection." 17340f74e643SEd Tanous "VLanNetworkInterfaceCollection"; 17350f74e643SEd Tanous asyncResp->res.jsonValue["@odata.context"] = 17360f74e643SEd Tanous "/redfish/v1/$metadata" 17370f74e643SEd Tanous "#VLanNetworkInterfaceCollection." 17380f74e643SEd Tanous "VLanNetworkInterfaceCollection"; 17390f74e643SEd Tanous asyncResp->res.jsonValue["Name"] = 17400f74e643SEd Tanous "VLAN Network Interface Collection"; 17414a0cb85cSEd Tanous 17424a0cb85cSEd Tanous nlohmann::json iface_array = nlohmann::json::array(); 17434a0cb85cSEd Tanous 17444a0cb85cSEd Tanous for (const std::string &iface_item : iface_list) 17451abe55efSEd Tanous { 17464a0cb85cSEd Tanous if (boost::starts_with(iface_item, rootInterfaceName + "_")) 17474a0cb85cSEd Tanous { 17484a0cb85cSEd Tanous iface_array.push_back( 17494a0cb85cSEd Tanous {{"@odata.id", 17504a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 17514a0cb85cSEd Tanous rootInterfaceName + "/VLANs/" + iface_item}}); 1752e439f0f8SKowalski, Kamil } 1753e439f0f8SKowalski, Kamil } 1754e439f0f8SKowalski, Kamil 17554a0cb85cSEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 17564a0cb85cSEd Tanous iface_array.size(); 17574a0cb85cSEd Tanous asyncResp->res.jsonValue["Members"] = std::move(iface_array); 17584a0cb85cSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 17594a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 17604a0cb85cSEd Tanous rootInterfaceName + "/VLANs"; 1761e439f0f8SKowalski, Kamil }); 1762e439f0f8SKowalski, Kamil } 1763e439f0f8SKowalski, Kamil 176455c7b7a2SEd Tanous void doPost(crow::Response &res, const crow::Request &req, 17651abe55efSEd Tanous const std::vector<std::string> ¶ms) override 17661abe55efSEd Tanous { 17674a0cb85cSEd Tanous std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 17681abe55efSEd Tanous if (params.size() != 1) 17691abe55efSEd Tanous { 1770f12894f8SJason M. Bills messages::internalError(asyncResp->res); 1771e439f0f8SKowalski, Kamil return; 1772e439f0f8SKowalski, Kamil } 1773fda13ad2SSunitha Harish bool vlanEnable = false; 17740627a2c7SEd Tanous uint32_t vlanId = 0; 1775fda13ad2SSunitha Harish if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable", 1776fda13ad2SSunitha Harish vlanEnable)) 17771abe55efSEd Tanous { 17784a0cb85cSEd Tanous return; 1779e439f0f8SKowalski, Kamil } 1780fda13ad2SSunitha Harish // Need both vlanId and vlanEnable to service this request 1781fda13ad2SSunitha Harish if (!vlanId) 1782fda13ad2SSunitha Harish { 1783fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANId"); 1784fda13ad2SSunitha Harish } 1785fda13ad2SSunitha Harish if (!vlanEnable) 1786fda13ad2SSunitha Harish { 1787fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANEnable"); 1788fda13ad2SSunitha Harish } 1789fda13ad2SSunitha Harish if (static_cast<bool>(vlanId) ^ static_cast<bool>(vlanEnable)) 1790fda13ad2SSunitha Harish { 1791fda13ad2SSunitha Harish return; 1792fda13ad2SSunitha Harish } 1793fda13ad2SSunitha Harish 17944a0cb85cSEd Tanous const std::string &rootInterfaceName = params[0]; 17954a0cb85cSEd Tanous auto callback = [asyncResp](const boost::system::error_code ec) { 17961abe55efSEd Tanous if (ec) 17971abe55efSEd Tanous { 17984a0cb85cSEd Tanous // TODO(ed) make more consistent error messages based on 17994a0cb85cSEd Tanous // phosphor-network responses 1800f12894f8SJason M. Bills messages::internalError(asyncResp->res); 18014a0cb85cSEd Tanous return; 18021abe55efSEd Tanous } 1803f12894f8SJason M. Bills messages::created(asyncResp->res); 1804e439f0f8SKowalski, Kamil }; 18054a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 18064a0cb85cSEd Tanous std::move(callback), "xyz.openbmc_project.Network", 18074a0cb85cSEd Tanous "/xyz/openbmc_project/network", 18084a0cb85cSEd Tanous "xyz.openbmc_project.Network.VLAN.Create", "VLAN", 18090627a2c7SEd Tanous rootInterfaceName, vlanId); 18104a0cb85cSEd Tanous } 18114a0cb85cSEd Tanous }; 18129391bb9cSRapkiewicz, Pawel } // namespace redfish 1813