1 /* 2 // Copyright (c) 2018 Intel Corporation 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 */ 16 #pragma once 17 18 #include <boost/container/flat_map.hpp> 19 #include <boost/container/flat_set.hpp> 20 #include <dbus_singleton.hpp> 21 #include <error_messages.hpp> 22 #include <node.hpp> 23 #include <optional> 24 #include <utils/json_utils.hpp> 25 #include <variant> 26 27 namespace redfish 28 { 29 30 /** 31 * DBus types primitives for several generic DBus interfaces 32 * TODO(Pawel) consider move this to separate file into boost::dbus 33 */ 34 using PropertiesMapType = boost::container::flat_map< 35 std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t, 36 int32_t, uint32_t, int64_t, uint64_t, double>>; 37 38 using GetManagedObjects = std::vector<std::pair< 39 sdbusplus::message::object_path, 40 std::vector<std::pair< 41 std::string, 42 boost::container::flat_map< 43 std::string, sdbusplus::message::variant< 44 std::string, bool, uint8_t, int16_t, uint16_t, 45 int32_t, uint32_t, int64_t, uint64_t, double, 46 std::vector<std::string>>>>>>>; 47 48 enum class LinkType 49 { 50 Local, 51 Global 52 }; 53 54 /** 55 * Structure for keeping IPv4 data required by Redfish 56 */ 57 struct IPv4AddressData 58 { 59 std::string id; 60 std::string address; 61 std::string domain; 62 std::string gateway; 63 std::string netmask; 64 std::string origin; 65 LinkType linktype; 66 67 bool operator<(const IPv4AddressData &obj) const 68 { 69 return id < obj.id; 70 } 71 }; 72 73 /** 74 * Structure for keeping basic single Ethernet Interface information 75 * available from DBus 76 */ 77 struct EthernetInterfaceData 78 { 79 uint32_t speed; 80 bool auto_neg; 81 std::string hostname; 82 std::string default_gateway; 83 std::string mac_address; 84 std::optional<uint32_t> vlan_id; 85 std::vector<std::string> nameservers; 86 }; 87 88 // Helper function that changes bits netmask notation (i.e. /24) 89 // into full dot notation 90 inline std::string getNetmask(unsigned int bits) 91 { 92 uint32_t value = 0xffffffff << (32 - bits); 93 std::string netmask = std::to_string((value >> 24) & 0xff) + "." + 94 std::to_string((value >> 16) & 0xff) + "." + 95 std::to_string((value >> 8) & 0xff) + "." + 96 std::to_string(value & 0xff); 97 return netmask; 98 } 99 100 inline std::string 101 translateAddressOriginDbusToRedfish(const std::string &inputOrigin, 102 bool isIPv4) 103 { 104 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static") 105 { 106 return "Static"; 107 } 108 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal") 109 { 110 if (isIPv4) 111 { 112 return "IPv4LinkLocal"; 113 } 114 else 115 { 116 return "LinkLocal"; 117 } 118 } 119 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP") 120 { 121 if (isIPv4) 122 { 123 return "DHCP"; 124 } 125 else 126 { 127 return "DHCPv6"; 128 } 129 } 130 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC") 131 { 132 return "SLAAC"; 133 } 134 return ""; 135 } 136 137 inline std::string 138 translateAddressOriginRedfishToDbus(const std::string &inputOrigin) 139 { 140 if (inputOrigin == "Static") 141 { 142 return "xyz.openbmc_project.Network.IP.AddressOrigin.Static"; 143 } 144 if (inputOrigin == "DHCP" || inputOrigin == "DHCPv6") 145 { 146 return "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP"; 147 } 148 if (inputOrigin == "IPv4LinkLocal" || inputOrigin == "LinkLocal") 149 { 150 return "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal"; 151 } 152 if (inputOrigin == "SLAAC") 153 { 154 return "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC"; 155 } 156 return ""; 157 } 158 159 inline void extractEthernetInterfaceData(const std::string ðiface_id, 160 const GetManagedObjects &dbus_data, 161 EthernetInterfaceData ðData) 162 { 163 for (const auto &objpath : dbus_data) 164 { 165 for (const auto &ifacePair : objpath.second) 166 { 167 if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id) 168 { 169 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") 170 { 171 for (const auto &propertyPair : ifacePair.second) 172 { 173 if (propertyPair.first == "MACAddress") 174 { 175 const std::string *mac = 176 std::get_if<std::string>(&propertyPair.second); 177 if (mac != nullptr) 178 { 179 ethData.mac_address = *mac; 180 } 181 } 182 } 183 } 184 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN") 185 { 186 for (const auto &propertyPair : ifacePair.second) 187 { 188 if (propertyPair.first == "Id") 189 { 190 const uint32_t *id = 191 std::get_if<uint32_t>(&propertyPair.second); 192 if (id != nullptr) 193 { 194 ethData.vlan_id = *id; 195 } 196 } 197 } 198 } 199 else if (ifacePair.first == 200 "xyz.openbmc_project.Network.EthernetInterface") 201 { 202 for (const auto &propertyPair : ifacePair.second) 203 { 204 if (propertyPair.first == "AutoNeg") 205 { 206 const bool *auto_neg = 207 std::get_if<bool>(&propertyPair.second); 208 if (auto_neg != nullptr) 209 { 210 ethData.auto_neg = *auto_neg; 211 } 212 } 213 else if (propertyPair.first == "Speed") 214 { 215 const uint32_t *speed = 216 std::get_if<uint32_t>(&propertyPair.second); 217 if (speed != nullptr) 218 { 219 ethData.speed = *speed; 220 } 221 } 222 else if (propertyPair.first == "NameServers") 223 { 224 const std::vector<std::string> *nameservers = 225 sdbusplus::message::variant_ns::get_if< 226 std::vector<std::string>>( 227 &propertyPair.second); 228 if (nameservers != nullptr) 229 { 230 ethData.nameservers = std::move(*nameservers); 231 } 232 } 233 } 234 } 235 } 236 // System configuration shows up in the global namespace, so no need 237 // to check eth number 238 if (ifacePair.first == 239 "xyz.openbmc_project.Network.SystemConfiguration") 240 { 241 for (const auto &propertyPair : ifacePair.second) 242 { 243 if (propertyPair.first == "HostName") 244 { 245 const std::string *hostname = 246 sdbusplus::message::variant_ns::get_if<std::string>( 247 &propertyPair.second); 248 if (hostname != nullptr) 249 { 250 ethData.hostname = *hostname; 251 } 252 } 253 else if (propertyPair.first == "DefaultGateway") 254 { 255 const std::string *defaultGateway = 256 sdbusplus::message::variant_ns::get_if<std::string>( 257 &propertyPair.second); 258 if (defaultGateway != nullptr) 259 { 260 ethData.default_gateway = *defaultGateway; 261 } 262 } 263 } 264 } 265 } 266 } 267 } 268 269 // Helper function that extracts data for single ethernet ipv4 address 270 inline void 271 extractIPData(const std::string ðiface_id, 272 const GetManagedObjects &dbus_data, 273 boost::container::flat_set<IPv4AddressData> &ipv4_config) 274 { 275 const std::string ipv4PathStart = 276 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/"; 277 278 // Since there might be several IPv4 configurations aligned with 279 // single ethernet interface, loop over all of them 280 for (const auto &objpath : dbus_data) 281 { 282 // Check if proper pattern for object path appears 283 if (boost::starts_with(objpath.first.str, ipv4PathStart)) 284 { 285 for (auto &interface : objpath.second) 286 { 287 if (interface.first == "xyz.openbmc_project.Network.IP") 288 { 289 // Instance IPv4AddressData structure, and set as 290 // appropriate 291 std::pair< 292 boost::container::flat_set<IPv4AddressData>::iterator, 293 bool> 294 it = ipv4_config.insert( 295 {objpath.first.str.substr(ipv4PathStart.size())}); 296 IPv4AddressData &ipv4_address = *it.first; 297 for (auto &property : interface.second) 298 { 299 if (property.first == "Address") 300 { 301 const std::string *address = 302 std::get_if<std::string>(&property.second); 303 if (address != nullptr) 304 { 305 ipv4_address.address = *address; 306 } 307 } 308 else if (property.first == "Gateway") 309 { 310 const std::string *gateway = 311 std::get_if<std::string>(&property.second); 312 if (gateway != nullptr) 313 { 314 ipv4_address.gateway = *gateway; 315 } 316 } 317 else if (property.first == "Origin") 318 { 319 const std::string *origin = 320 std::get_if<std::string>(&property.second); 321 if (origin != nullptr) 322 { 323 ipv4_address.origin = 324 translateAddressOriginDbusToRedfish(*origin, 325 true); 326 } 327 } 328 else if (property.first == "PrefixLength") 329 { 330 const uint8_t *mask = 331 std::get_if<uint8_t>(&property.second); 332 if (mask != nullptr) 333 { 334 // convert it to the string 335 ipv4_address.netmask = getNetmask(*mask); 336 } 337 } 338 else 339 { 340 BMCWEB_LOG_ERROR 341 << "Got extra property: " << property.first 342 << " on the " << objpath.first.str << " object"; 343 } 344 } 345 // Check if given address is local, or global 346 ipv4_address.linktype = 347 boost::starts_with(ipv4_address.address, "169.254.") 348 ? LinkType::Global 349 : LinkType::Local; 350 } 351 } 352 } 353 } 354 } 355 356 /** 357 * @brief Sets given Id on the given VLAN interface through D-Bus 358 * 359 * @param[in] ifaceId Id of VLAN interface that should be modified 360 * @param[in] inputVlanId New ID of the VLAN 361 * @param[in] callback Function that will be called after the operation 362 * 363 * @return None. 364 */ 365 template <typename CallbackFunc> 366 void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId, 367 CallbackFunc &&callback) 368 { 369 crow::connections::systemBus->async_method_call( 370 callback, "xyz.openbmc_project.Network", 371 std::string("/xyz/openbmc_project/network/") + ifaceId, 372 "org.freedesktop.DBus.Properties", "Set", 373 "xyz.openbmc_project.Network.VLAN", "Id", 374 std::variant<uint32_t>(inputVlanId)); 375 } 376 377 /** 378 * @brief Helper function that verifies IP address to check if it is in 379 * proper format. If bits pointer is provided, also calculates active 380 * bit count for Subnet Mask. 381 * 382 * @param[in] ip IP that will be verified 383 * @param[out] bits Calculated mask in bits notation 384 * 385 * @return true in case of success, false otherwise 386 */ 387 inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip, 388 uint8_t *bits = nullptr) 389 { 390 std::vector<std::string> bytesInMask; 391 392 boost::split(bytesInMask, ip, boost::is_any_of(".")); 393 394 static const constexpr int ipV4AddressSectionsCount = 4; 395 if (bytesInMask.size() != ipV4AddressSectionsCount) 396 { 397 return false; 398 } 399 400 if (bits != nullptr) 401 { 402 *bits = 0; 403 } 404 405 char *endPtr; 406 long previousValue = 255; 407 bool firstZeroInByteHit; 408 for (const std::string &byte : bytesInMask) 409 { 410 if (byte.empty()) 411 { 412 return false; 413 } 414 415 // Use strtol instead of stroi to avoid exceptions 416 long value = std::strtol(byte.c_str(), &endPtr, 10); 417 418 // endPtr should point to the end of the string, otherwise given string 419 // is not 100% number 420 if (*endPtr != '\0') 421 { 422 return false; 423 } 424 425 // Value should be contained in byte 426 if (value < 0 || value > 255) 427 { 428 return false; 429 } 430 431 if (bits != nullptr) 432 { 433 // Mask has to be continuous between bytes 434 if (previousValue != 255 && value != 0) 435 { 436 return false; 437 } 438 439 // Mask has to be continuous inside bytes 440 firstZeroInByteHit = false; 441 442 // Count bits 443 for (int bitIdx = 7; bitIdx >= 0; bitIdx--) 444 { 445 if (value & (1 << bitIdx)) 446 { 447 if (firstZeroInByteHit) 448 { 449 // Continuity not preserved 450 return false; 451 } 452 else 453 { 454 (*bits)++; 455 } 456 } 457 else 458 { 459 firstZeroInByteHit = true; 460 } 461 } 462 } 463 464 previousValue = value; 465 } 466 467 return true; 468 } 469 470 /** 471 * @brief Changes IPv4 address type property (Address, Gateway) 472 * 473 * @param[in] ifaceId Id of interface whose IP should be modified 474 * @param[in] ipIdx Index of IP in input array that should be modified 475 * @param[in] ipHash DBus Hash id of modified IP 476 * @param[in] name Name of field in JSON representation 477 * @param[in] newValue New value that should be written 478 * @param[io] asyncResp Response object that will be returned to client 479 * 480 * @return true if give IP is valid and has been sent do D-Bus, false 481 * otherwise 482 */ 483 inline void changeIPv4AddressProperty( 484 const std::string &ifaceId, int ipIdx, const std::string &ipHash, 485 const std::string &name, const std::string &newValue, 486 const std::shared_ptr<AsyncResp> asyncResp) 487 { 488 auto callback = [asyncResp, ipIdx, name{std::string(name)}, 489 newValue{std::move(newValue)}]( 490 const boost::system::error_code ec) { 491 if (ec) 492 { 493 messages::internalError(asyncResp->res); 494 } 495 else 496 { 497 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx][name] = newValue; 498 } 499 }; 500 501 crow::connections::systemBus->async_method_call( 502 std::move(callback), "xyz.openbmc_project.Network", 503 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 504 "org.freedesktop.DBus.Properties", "Set", 505 "xyz.openbmc_project.Network.IP", name, 506 std::variant<std::string>(newValue)); 507 } 508 509 /** 510 * @brief Changes IPv4 address origin property 511 * 512 * @param[in] ifaceId Id of interface whose IP should be modified 513 * @param[in] ipIdx Index of IP in input array that should be 514 * modified 515 * @param[in] ipHash DBus Hash id of modified IP 516 * @param[in] newValue New value in Redfish format 517 * @param[in] newValueDbus New value in D-Bus format 518 * @param[io] asyncResp Response object that will be returned to client 519 * 520 * @return true if give IP is valid and has been sent do D-Bus, false 521 * otherwise 522 */ 523 inline void changeIPv4Origin(const std::string &ifaceId, int ipIdx, 524 const std::string &ipHash, 525 const std::string &newValue, 526 const std::string &newValueDbus, 527 const std::shared_ptr<AsyncResp> asyncResp) 528 { 529 auto callback = [asyncResp, ipIdx, newValue{std::move(newValue)}]( 530 const boost::system::error_code ec) { 531 if (ec) 532 { 533 messages::internalError(asyncResp->res); 534 } 535 else 536 { 537 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["AddressOrigin"] = 538 newValue; 539 } 540 }; 541 542 crow::connections::systemBus->async_method_call( 543 std::move(callback), "xyz.openbmc_project.Network", 544 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 545 "org.freedesktop.DBus.Properties", "Set", 546 "xyz.openbmc_project.Network.IP", "Origin", 547 std::variant<std::string>(newValueDbus)); 548 } 549 550 /** 551 * @brief Modifies SubnetMask for given IP 552 * 553 * @param[in] ifaceId Id of interface whose IP should be modified 554 * @param[in] ipIdx Index of IP in input array that should be 555 * modified 556 * @param[in] ipHash DBus Hash id of modified IP 557 * @param[in] newValueStr Mask in dot notation as string 558 * @param[in] newValue Mask as PrefixLength in bitcount 559 * @param[io] asyncResp Response object that will be returned to client 560 * 561 * @return None 562 */ 563 inline void changeIPv4SubnetMaskProperty(const std::string &ifaceId, int ipIdx, 564 const std::string &ipHash, 565 const std::string &newValueStr, 566 uint8_t &newValue, 567 std::shared_ptr<AsyncResp> asyncResp) 568 { 569 auto callback = [asyncResp, ipIdx, newValueStr{std::move(newValueStr)}]( 570 const boost::system::error_code ec) { 571 if (ec) 572 { 573 messages::internalError(asyncResp->res); 574 } 575 else 576 { 577 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["SubnetMask"] = 578 newValueStr; 579 } 580 }; 581 582 crow::connections::systemBus->async_method_call( 583 std::move(callback), "xyz.openbmc_project.Network", 584 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 585 "org.freedesktop.DBus.Properties", "Set", 586 "xyz.openbmc_project.Network.IP", "PrefixLength", 587 std::variant<uint8_t>(newValue)); 588 } 589 590 /** 591 * @brief Deletes given IPv4 592 * 593 * @param[in] ifaceId Id of interface whose IP should be deleted 594 * @param[in] ipIdx Index of IP in input array that should be deleted 595 * @param[in] ipHash DBus Hash id of IP that should be deleted 596 * @param[io] asyncResp Response object that will be returned to client 597 * 598 * @return None 599 */ 600 inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash, 601 unsigned int ipIdx, 602 const std::shared_ptr<AsyncResp> asyncResp) 603 { 604 crow::connections::systemBus->async_method_call( 605 [ipIdx, asyncResp](const boost::system::error_code ec) { 606 if (ec) 607 { 608 messages::internalError(asyncResp->res); 609 } 610 else 611 { 612 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx] = nullptr; 613 } 614 }, 615 "xyz.openbmc_project.Network", 616 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 617 "xyz.openbmc_project.Object.Delete", "Delete"); 618 } 619 620 /** 621 * @brief Creates IPv4 with given data 622 * 623 * @param[in] ifaceId Id of interface whose IP should be deleted 624 * @param[in] ipIdx Index of IP in input array that should be deleted 625 * @param[in] ipHash DBus Hash id of IP that should be deleted 626 * @param[io] asyncResp Response object that will be returned to client 627 * 628 * @return None 629 */ 630 inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx, 631 uint8_t subnetMask, const std::string &gateway, 632 const std::string &address, 633 std::shared_ptr<AsyncResp> asyncResp) 634 { 635 auto createIpHandler = [asyncResp](const boost::system::error_code ec) { 636 if (ec) 637 { 638 messages::internalError(asyncResp->res); 639 } 640 }; 641 642 crow::connections::systemBus->async_method_call( 643 std::move(createIpHandler), "xyz.openbmc_project.Network", 644 "/xyz/openbmc_project/network/" + ifaceId, 645 "xyz.openbmc_project.Network.IP.Create", "IP", 646 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, subnetMask, 647 gateway); 648 } 649 650 /** 651 * Function that retrieves all properties for given Ethernet Interface 652 * Object 653 * from EntityManager Network Manager 654 * @param ethiface_id a eth interface id to query on DBus 655 * @param callback a function that shall be called to convert Dbus output 656 * into JSON 657 */ 658 template <typename CallbackFunc> 659 void getEthernetIfaceData(const std::string ðiface_id, 660 CallbackFunc &&callback) 661 { 662 crow::connections::systemBus->async_method_call( 663 [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}]( 664 const boost::system::error_code error_code, 665 const GetManagedObjects &resp) { 666 EthernetInterfaceData ethData{}; 667 boost::container::flat_set<IPv4AddressData> ipv4Data; 668 669 if (error_code) 670 { 671 callback(false, ethData, ipv4Data); 672 return; 673 } 674 675 extractEthernetInterfaceData(ethiface_id, resp, ethData); 676 extractIPData(ethiface_id, resp, ipv4Data); 677 678 // Fix global GW 679 for (IPv4AddressData &ipv4 : ipv4Data) 680 { 681 if ((ipv4.linktype == LinkType::Global) && 682 (ipv4.gateway == "0.0.0.0")) 683 { 684 ipv4.gateway = ethData.default_gateway; 685 } 686 } 687 688 // Finally make a callback with usefull data 689 callback(true, ethData, ipv4Data); 690 }, 691 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 692 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 693 }; 694 695 /** 696 * Function that retrieves all Ethernet Interfaces available through Network 697 * Manager 698 * @param callback a function that shall be called to convert Dbus output 699 * into JSON. 700 */ 701 template <typename CallbackFunc> 702 void getEthernetIfaceList(CallbackFunc &&callback) 703 { 704 crow::connections::systemBus->async_method_call( 705 [callback{std::move(callback)}]( 706 const boost::system::error_code error_code, 707 GetManagedObjects &resp) { 708 // Callback requires vector<string> to retrieve all available 709 // ethernet interfaces 710 std::vector<std::string> iface_list; 711 iface_list.reserve(resp.size()); 712 if (error_code) 713 { 714 callback(false, iface_list); 715 return; 716 } 717 718 // Iterate over all retrieved ObjectPaths. 719 for (const auto &objpath : resp) 720 { 721 // And all interfaces available for certain ObjectPath. 722 for (const auto &interface : objpath.second) 723 { 724 // If interface is 725 // xyz.openbmc_project.Network.EthernetInterface, this is 726 // what we're looking for. 727 if (interface.first == 728 "xyz.openbmc_project.Network.EthernetInterface") 729 { 730 // Cut out everyting until last "/", ... 731 const std::string &iface_id = objpath.first.str; 732 std::size_t last_pos = iface_id.rfind("/"); 733 if (last_pos != std::string::npos) 734 { 735 // and put it into output vector. 736 iface_list.emplace_back( 737 iface_id.substr(last_pos + 1)); 738 } 739 } 740 } 741 } 742 // Finally make a callback with useful data 743 callback(true, iface_list); 744 }, 745 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 746 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 747 }; 748 749 /** 750 * EthernetCollection derived class for delivering Ethernet Collection Schema 751 */ 752 class EthernetCollection : public Node 753 { 754 public: 755 template <typename CrowApp> 756 EthernetCollection(CrowApp &app) : 757 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") 758 { 759 entityPrivileges = { 760 {boost::beast::http::verb::get, {{"Login"}}}, 761 {boost::beast::http::verb::head, {{"Login"}}}, 762 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 763 {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 764 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 765 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 766 } 767 768 private: 769 /** 770 * Functions triggers appropriate requests on DBus 771 */ 772 void doGet(crow::Response &res, const crow::Request &req, 773 const std::vector<std::string> ¶ms) override 774 { 775 res.jsonValue["@odata.type"] = 776 "#EthernetInterfaceCollection.EthernetInterfaceCollection"; 777 res.jsonValue["@odata.context"] = 778 "/redfish/v1/" 779 "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection"; 780 res.jsonValue["@odata.id"] = 781 "/redfish/v1/Managers/bmc/EthernetInterfaces"; 782 res.jsonValue["Name"] = "Ethernet Network Interface Collection"; 783 res.jsonValue["Description"] = 784 "Collection of EthernetInterfaces for this Manager"; 785 786 // Get eth interface list, and call the below callback for JSON 787 // preparation 788 getEthernetIfaceList( 789 [&res](const bool &success, 790 const std::vector<std::string> &iface_list) { 791 if (!success) 792 { 793 messages::internalError(res); 794 res.end(); 795 return; 796 } 797 798 nlohmann::json &iface_array = res.jsonValue["Members"]; 799 iface_array = nlohmann::json::array(); 800 for (const std::string &iface_item : iface_list) 801 { 802 iface_array.push_back( 803 {{"@odata.id", 804 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 805 iface_item}}); 806 } 807 808 res.jsonValue["Members@odata.count"] = iface_array.size(); 809 res.jsonValue["@odata.id"] = 810 "/redfish/v1/Managers/bmc/EthernetInterfaces"; 811 res.end(); 812 }); 813 } 814 }; 815 816 /** 817 * EthernetInterface derived class for delivering Ethernet Schema 818 */ 819 class EthernetInterface : public Node 820 { 821 public: 822 /* 823 * Default Constructor 824 */ 825 template <typename CrowApp> 826 EthernetInterface(CrowApp &app) : 827 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/", 828 std::string()) 829 { 830 entityPrivileges = { 831 {boost::beast::http::verb::get, {{"Login"}}}, 832 {boost::beast::http::verb::head, {{"Login"}}}, 833 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 834 {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 835 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 836 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 837 } 838 839 // TODO(kkowalsk) Find a suitable class/namespace for this 840 static void handleVlanPatch(const std::string &ifaceId, bool vlanEnable, 841 uint64_t vlanId, 842 const EthernetInterfaceData ðData, 843 const std::shared_ptr<AsyncResp> asyncResp) 844 { 845 if (!ethData.vlan_id) 846 { 847 // This interface is not a VLAN. Cannot do anything with it 848 // TODO(kkowalsk) Change this message 849 messages::propertyNotWritable(asyncResp->res, "VLANEnable"); 850 851 return; 852 } 853 854 // VLAN is configured on the interface 855 if (vlanEnable == true) 856 { 857 // Change VLAN Id 858 asyncResp->res.jsonValue["VLANId"] = vlanId; 859 auto callback = [asyncResp](const boost::system::error_code ec) { 860 if (ec) 861 { 862 messages::internalError(asyncResp->res); 863 } 864 else 865 { 866 asyncResp->res.jsonValue["VLANEnable"] = true; 867 } 868 }; 869 crow::connections::systemBus->async_method_call( 870 std::move(callback), "xyz.openbmc_project.Network", 871 "/xyz/openbmc_project/network/" + ifaceId, 872 "org.freedesktop.DBus.Properties", "Set", 873 "xyz.openbmc_project.Network.VLAN", "Id", 874 std::variant<uint32_t>(vlanId)); 875 } 876 else 877 { 878 auto callback = [asyncResp](const boost::system::error_code ec) { 879 if (ec) 880 { 881 messages::internalError(asyncResp->res); 882 return; 883 } 884 asyncResp->res.jsonValue["VLANEnable"] = false; 885 }; 886 887 crow::connections::systemBus->async_method_call( 888 std::move(callback), "xyz.openbmc_project.Network", 889 "/xyz/openbmc_project/network/" + ifaceId, 890 "xyz.openbmc_project.Object.Delete", "Delete"); 891 } 892 } 893 894 private: 895 void handleHostnamePatch(const std::string &hostname, 896 const std::shared_ptr<AsyncResp> asyncResp) 897 { 898 asyncResp->res.jsonValue["HostName"] = hostname; 899 crow::connections::systemBus->async_method_call( 900 [asyncResp](const boost::system::error_code ec) { 901 if (ec) 902 { 903 messages::internalError(asyncResp->res); 904 } 905 }, 906 "xyz.openbmc_project.Network", 907 "/xyz/openbmc_project/network/config", 908 "org.freedesktop.DBus.Properties", "Set", 909 "xyz.openbmc_project.Network.SystemConfiguration", "HostName", 910 std::variant<std::string>(hostname)); 911 } 912 913 void handleIPv4Patch( 914 const std::string &ifaceId, std::vector<nlohmann::json> &input, 915 const boost::container::flat_set<IPv4AddressData> &ipv4Data, 916 const std::shared_ptr<AsyncResp> asyncResp) 917 { 918 int entryIdx = 0; 919 boost::container::flat_set<IPv4AddressData>::const_iterator thisData = 920 ipv4Data.begin(); 921 for (nlohmann::json &thisJson : input) 922 { 923 std::string pathString = 924 "IPv4Addresses/" + std::to_string(entryIdx); 925 926 std::optional<std::string> address; 927 std::optional<std::string> addressOrigin; 928 std::optional<std::string> subnetMask; 929 std::optional<std::string> gateway; 930 931 if (!json_util::readJson(thisJson, asyncResp->res, "Address", 932 address, "AddressOrigin", addressOrigin, 933 "SubnetMask", subnetMask, "Gateway", 934 gateway)) 935 { 936 return; 937 } 938 939 if (address) 940 { 941 if (!ipv4VerifyIpAndGetBitcount(*address)) 942 { 943 messages::propertyValueFormatError(asyncResp->res, *address, 944 pathString + "/Address"); 945 return; 946 } 947 } 948 949 uint8_t prefixLength = 0; 950 if (subnetMask) 951 { 952 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength)) 953 { 954 messages::propertyValueFormatError( 955 asyncResp->res, *subnetMask, 956 pathString + "/SubnetMask"); 957 return; 958 } 959 } 960 std::string addressOriginInDBusFormat; 961 if (addressOrigin) 962 { 963 // Get Address origin in proper format 964 addressOriginInDBusFormat = 965 translateAddressOriginRedfishToDbus(*addressOrigin); 966 if (addressOriginInDBusFormat.empty()) 967 { 968 messages::propertyValueNotInList( 969 asyncResp->res, *addressOrigin, 970 pathString + "/AddressOrigin"); 971 return; 972 } 973 } 974 975 if (gateway) 976 { 977 if (!ipv4VerifyIpAndGetBitcount(*gateway)) 978 { 979 messages::propertyValueFormatError(asyncResp->res, *gateway, 980 pathString + "/Gateway"); 981 return; 982 } 983 } 984 985 // if a vlan already exists, modify the existing 986 if (thisData != ipv4Data.end()) 987 { 988 // Existing object that should be modified/deleted/remain 989 // unchanged 990 if (thisJson.is_null()) 991 { 992 auto callback = [entryIdx{std::to_string(entryIdx)}, 993 asyncResp]( 994 const boost::system::error_code ec) { 995 if (ec) 996 { 997 messages::internalError(asyncResp->res); 998 return; 999 } 1000 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] = 1001 nullptr; 1002 }; 1003 crow::connections::systemBus->async_method_call( 1004 std::move(callback), "xyz.openbmc_project.Network", 1005 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + 1006 thisData->id, 1007 "xyz.openbmc_project.Object.Delete", "Delete"); 1008 } 1009 else if (thisJson.is_object()) 1010 { 1011 // Apply changes 1012 if (address) 1013 { 1014 auto callback = 1015 [asyncResp, entryIdx, address = *address]( 1016 const boost::system::error_code ec) { 1017 if (ec) 1018 { 1019 messages::internalError(asyncResp->res); 1020 return; 1021 } 1022 asyncResp->res 1023 .jsonValue["IPv4Addresses"][std::to_string( 1024 entryIdx)]["Address"] = address; 1025 }; 1026 1027 crow::connections::systemBus->async_method_call( 1028 std::move(callback), "xyz.openbmc_project.Network", 1029 "/xyz/openbmc_project/network/" + ifaceId + 1030 "/ipv4/" + thisData->id, 1031 "org.freedesktop.DBus.Properties", "Set", 1032 "xyz.openbmc_project.Network.IP", "Address", 1033 std::variant<std::string>(*address)); 1034 } 1035 1036 if (subnetMask) 1037 { 1038 changeIPv4SubnetMaskProperty(ifaceId, entryIdx, 1039 thisData->id, *subnetMask, 1040 prefixLength, asyncResp); 1041 } 1042 1043 if (addressOrigin) 1044 { 1045 changeIPv4Origin(ifaceId, entryIdx, thisData->id, 1046 *addressOrigin, 1047 addressOriginInDBusFormat, asyncResp); 1048 } 1049 1050 if (gateway) 1051 { 1052 auto callback = 1053 [asyncResp, entryIdx, 1054 gateway{std::string(*gateway)}]( 1055 const boost::system::error_code ec) { 1056 if (ec) 1057 { 1058 messages::internalError(asyncResp->res); 1059 return; 1060 } 1061 asyncResp->res 1062 .jsonValue["IPv4Addresses"][std::to_string( 1063 entryIdx)]["Gateway"] = 1064 std::move(gateway); 1065 }; 1066 1067 crow::connections::systemBus->async_method_call( 1068 std::move(callback), "xyz.openbmc_project.Network", 1069 "/xyz/openbmc_project/network/" + ifaceId + 1070 "/ipv4/" + thisData->id, 1071 "org.freedesktop.DBus.Properties", "Set", 1072 "xyz.openbmc_project.Network.IP", "Gateway", 1073 std::variant<std::string>(*gateway)); 1074 } 1075 } 1076 thisData++; 1077 } 1078 else 1079 { 1080 // Create IPv4 with provided data 1081 if (!gateway) 1082 { 1083 messages::propertyMissing(asyncResp->res, 1084 pathString + "/Gateway"); 1085 continue; 1086 } 1087 1088 if (!address) 1089 { 1090 messages::propertyMissing(asyncResp->res, 1091 pathString + "/Address"); 1092 continue; 1093 } 1094 1095 if (!subnetMask) 1096 { 1097 messages::propertyMissing(asyncResp->res, 1098 pathString + "/SubnetMask"); 1099 continue; 1100 } 1101 1102 createIPv4(ifaceId, entryIdx, prefixLength, *gateway, *address, 1103 asyncResp); 1104 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] = thisJson; 1105 } 1106 entryIdx++; 1107 } 1108 } 1109 1110 void parseInterfaceData( 1111 nlohmann::json &json_response, const std::string &iface_id, 1112 const EthernetInterfaceData ðData, 1113 const boost::container::flat_set<IPv4AddressData> &ipv4Data) 1114 { 1115 json_response["Id"] = iface_id; 1116 json_response["@odata.id"] = 1117 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id; 1118 json_response["InterfaceEnabled"] = true; 1119 if (ethData.speed == 0) 1120 { 1121 json_response["LinkStatus"] = "NoLink"; 1122 json_response["Status"] = { 1123 {"Health", "OK"}, 1124 {"State", "Disabled"}, 1125 }; 1126 } 1127 else 1128 { 1129 json_response["LinkStatus"] = "LinkUp"; 1130 json_response["Status"] = { 1131 {"Health", "OK"}, 1132 {"State", "Enabled"}, 1133 }; 1134 } 1135 json_response["SpeedMbps"] = ethData.speed; 1136 json_response["MACAddress"] = ethData.mac_address; 1137 if (!ethData.hostname.empty()) 1138 { 1139 json_response["HostName"] = ethData.hostname; 1140 } 1141 1142 nlohmann::json &vlanObj = json_response["VLAN"]; 1143 if (ethData.vlan_id) 1144 { 1145 vlanObj["VLANEnable"] = true; 1146 vlanObj["VLANId"] = *ethData.vlan_id; 1147 } 1148 else 1149 { 1150 vlanObj["VLANEnable"] = false; 1151 vlanObj["VLANId"] = 0; 1152 } 1153 json_response["NameServers"] = ethData.nameservers; 1154 1155 if (ipv4Data.size() > 0) 1156 { 1157 nlohmann::json &ipv4_array = json_response["IPv4Addresses"]; 1158 ipv4_array = nlohmann::json::array(); 1159 for (auto &ipv4_config : ipv4Data) 1160 { 1161 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin}, 1162 {"SubnetMask", ipv4_config.netmask}, 1163 {"Address", ipv4_config.address}, 1164 {"Gateway", ipv4_config.gateway}}); 1165 } 1166 } 1167 } 1168 1169 /** 1170 * Functions triggers appropriate requests on DBus 1171 */ 1172 void doGet(crow::Response &res, const crow::Request &req, 1173 const std::vector<std::string> ¶ms) override 1174 { 1175 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1176 if (params.size() != 1) 1177 { 1178 messages::internalError(asyncResp->res); 1179 return; 1180 } 1181 1182 getEthernetIfaceData( 1183 params[0], 1184 [this, asyncResp, iface_id{std::string(params[0])}]( 1185 const bool &success, const EthernetInterfaceData ðData, 1186 const boost::container::flat_set<IPv4AddressData> &ipv4Data) { 1187 if (!success) 1188 { 1189 // TODO(Pawel)consider distinguish between non existing 1190 // object, and other errors 1191 messages::resourceNotFound(asyncResp->res, 1192 "EthernetInterface", iface_id); 1193 return; 1194 } 1195 asyncResp->res.jsonValue["@odata.type"] = 1196 "#EthernetInterface.v1_2_0.EthernetInterface"; 1197 asyncResp->res.jsonValue["@odata.context"] = 1198 "/redfish/v1/$metadata#EthernetInterface.EthernetInterface"; 1199 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface"; 1200 asyncResp->res.jsonValue["Description"] = 1201 "Management Network Interface"; 1202 1203 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData, 1204 ipv4Data); 1205 }); 1206 } 1207 1208 void doPatch(crow::Response &res, const crow::Request &req, 1209 const std::vector<std::string> ¶ms) override 1210 { 1211 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1212 if (params.size() != 1) 1213 { 1214 messages::internalError(asyncResp->res); 1215 return; 1216 } 1217 1218 const std::string &iface_id = params[0]; 1219 1220 std::optional<nlohmann::json> vlan; 1221 std::optional<std::string> hostname; 1222 std::optional<std::vector<nlohmann::json>> ipv4Addresses; 1223 std::optional<std::vector<nlohmann::json>> ipv6Addresses; 1224 1225 if (!json_util::readJson(req, res, "VLAN", vlan, "HostName", hostname, 1226 "IPv4Addresses", ipv4Addresses, 1227 "IPv6Addresses", ipv6Addresses)) 1228 { 1229 return; 1230 } 1231 std::optional<uint64_t> vlanId = 0; 1232 std::optional<bool> vlanEnable = false; 1233 if (vlan) 1234 { 1235 if (!json_util::readJson(*vlan, res, "VLANEnable", vlanEnable, 1236 "VLANId", vlanId)) 1237 { 1238 return; 1239 } 1240 // Need both vlanId and vlanEnable to service this request 1241 if (static_cast<bool>(vlanId) ^ static_cast<bool>(vlanEnable)) 1242 { 1243 if (vlanId) 1244 { 1245 messages::propertyMissing(asyncResp->res, "VLANEnable"); 1246 } 1247 else 1248 { 1249 messages::propertyMissing(asyncResp->res, "VLANId"); 1250 } 1251 1252 return; 1253 } 1254 } 1255 1256 // Get single eth interface data, and call the below callback for JSON 1257 // preparation 1258 getEthernetIfaceData( 1259 iface_id, 1260 [this, asyncResp, iface_id, vlanId, vlanEnable, 1261 hostname = std::move(hostname), 1262 ipv4Addresses = std::move(ipv4Addresses), 1263 ipv6Addresses = std::move(ipv6Addresses)]( 1264 const bool &success, const EthernetInterfaceData ðData, 1265 const boost::container::flat_set<IPv4AddressData> &ipv4Data) { 1266 if (!success) 1267 { 1268 // ... otherwise return error 1269 // TODO(Pawel)consider distinguish between non existing 1270 // object, and other errors 1271 messages::resourceNotFound( 1272 asyncResp->res, "VLAN Network Interface", iface_id); 1273 return; 1274 } 1275 1276 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData, 1277 ipv4Data); 1278 1279 if (vlanId && vlanEnable) 1280 { 1281 handleVlanPatch(iface_id, *vlanId, *vlanEnable, ethData, 1282 asyncResp); 1283 } 1284 1285 if (hostname) 1286 { 1287 handleHostnamePatch(*hostname, asyncResp); 1288 } 1289 1290 if (ipv4Addresses) 1291 { 1292 // TODO(ed) for some reason the capture of ipv4Addresses 1293 // above is returning a const value, not a non-const value. 1294 // This doesn't really work for us, as we need to be able to 1295 // efficiently move out the intermedia nlohmann::json 1296 // objects. This makes a copy of the structure, and operates 1297 // on that, but could be done more efficiently 1298 std::vector<nlohmann::json> ipv4 = 1299 std::move(*ipv4Addresses); 1300 handleIPv4Patch(iface_id, ipv4, ipv4Data, asyncResp); 1301 } 1302 1303 if (ipv6Addresses) 1304 { 1305 // TODO(kkowalsk) IPv6 Not supported on D-Bus yet 1306 messages::propertyNotWritable(asyncResp->res, 1307 "IPv6Addresses"); 1308 } 1309 }); 1310 } 1311 }; 1312 1313 /** 1314 * VlanNetworkInterface derived class for delivering VLANNetworkInterface 1315 * Schema 1316 */ 1317 class VlanNetworkInterface : public Node 1318 { 1319 public: 1320 /* 1321 * Default Constructor 1322 */ 1323 template <typename CrowApp> 1324 VlanNetworkInterface(CrowApp &app) : 1325 Node(app, 1326 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>", 1327 std::string(), std::string()) 1328 { 1329 entityPrivileges = { 1330 {boost::beast::http::verb::get, {{"Login"}}}, 1331 {boost::beast::http::verb::head, {{"Login"}}}, 1332 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 1333 {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 1334 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 1335 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 1336 } 1337 1338 private: 1339 void parseInterfaceData( 1340 nlohmann::json &json_response, const std::string &parent_iface_id, 1341 const std::string &iface_id, const EthernetInterfaceData ðData, 1342 const boost::container::flat_set<IPv4AddressData> &ipv4Data) 1343 { 1344 // Fill out obvious data... 1345 json_response["Id"] = iface_id; 1346 json_response["@odata.id"] = 1347 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id + 1348 "/VLANs/" + iface_id; 1349 1350 json_response["VLANEnable"] = true; 1351 if (ethData.vlan_id) 1352 { 1353 json_response["VLANId"] = *ethData.vlan_id; 1354 } 1355 } 1356 1357 bool verifyNames(crow::Response &res, const std::string &parent, 1358 const std::string &iface) 1359 { 1360 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1361 if (!boost::starts_with(iface, parent + "_")) 1362 { 1363 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", 1364 iface); 1365 return false; 1366 } 1367 else 1368 { 1369 return true; 1370 } 1371 } 1372 1373 /** 1374 * Functions triggers appropriate requests on DBus 1375 */ 1376 void doGet(crow::Response &res, const crow::Request &req, 1377 const std::vector<std::string> ¶ms) override 1378 { 1379 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1380 // TODO(Pawel) this shall be parameterized call (two params) to get 1381 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'. 1382 // Check if there is required param, truly entering this shall be 1383 // impossible. 1384 if (params.size() != 2) 1385 { 1386 messages::internalError(res); 1387 res.end(); 1388 return; 1389 } 1390 1391 const std::string &parent_iface_id = params[0]; 1392 const std::string &iface_id = params[1]; 1393 res.jsonValue["@odata.type"] = 1394 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; 1395 res.jsonValue["@odata.context"] = 1396 "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface"; 1397 res.jsonValue["Name"] = "VLAN Network Interface"; 1398 1399 if (!verifyNames(res, parent_iface_id, iface_id)) 1400 { 1401 return; 1402 } 1403 1404 // Get single eth interface data, and call the below callback for JSON 1405 // preparation 1406 getEthernetIfaceData( 1407 iface_id, 1408 [this, asyncResp, parent_iface_id, iface_id]( 1409 const bool &success, const EthernetInterfaceData ðData, 1410 const boost::container::flat_set<IPv4AddressData> &ipv4Data) { 1411 if (success && ethData.vlan_id) 1412 { 1413 parseInterfaceData(asyncResp->res.jsonValue, 1414 parent_iface_id, iface_id, ethData, 1415 ipv4Data); 1416 } 1417 else 1418 { 1419 // ... otherwise return error 1420 // TODO(Pawel)consider distinguish between non existing 1421 // object, and other errors 1422 messages::resourceNotFound( 1423 asyncResp->res, "VLAN Network Interface", iface_id); 1424 } 1425 }); 1426 } 1427 1428 void doPatch(crow::Response &res, const crow::Request &req, 1429 const std::vector<std::string> ¶ms) override 1430 { 1431 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1432 if (params.size() != 2) 1433 { 1434 messages::internalError(asyncResp->res); 1435 return; 1436 } 1437 1438 const std::string &parentIfaceId = params[0]; 1439 const std::string &ifaceId = params[1]; 1440 1441 if (!verifyNames(res, parentIfaceId, ifaceId)) 1442 { 1443 return; 1444 } 1445 1446 bool vlanEnable = false; 1447 uint64_t vlanId = 0; 1448 1449 if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId", 1450 vlanId)) 1451 { 1452 return; 1453 } 1454 1455 // Get single eth interface data, and call the below callback for JSON 1456 // preparation 1457 getEthernetIfaceData( 1458 ifaceId, 1459 [this, asyncResp, parentIfaceId, ifaceId, vlanEnable, vlanId]( 1460 const bool &success, const EthernetInterfaceData ðData, 1461 const boost::container::flat_set<IPv4AddressData> &ipv4Data) { 1462 if (!success) 1463 { 1464 // TODO(Pawel)consider distinguish between non existing 1465 // object, and other errors 1466 messages::resourceNotFound( 1467 asyncResp->res, "VLAN Network Interface", ifaceId); 1468 1469 return; 1470 } 1471 1472 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId, 1473 ifaceId, ethData, ipv4Data); 1474 1475 EthernetInterface::handleVlanPatch(ifaceId, vlanId, vlanEnable, 1476 ethData, asyncResp); 1477 }); 1478 } 1479 1480 void doDelete(crow::Response &res, const crow::Request &req, 1481 const std::vector<std::string> ¶ms) override 1482 { 1483 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1484 if (params.size() != 2) 1485 { 1486 messages::internalError(asyncResp->res); 1487 return; 1488 } 1489 1490 const std::string &parentIfaceId = params[0]; 1491 const std::string &ifaceId = params[1]; 1492 1493 if (!verifyNames(asyncResp->res, parentIfaceId, ifaceId)) 1494 { 1495 return; 1496 } 1497 1498 // Get single eth interface data, and call the below callback for JSON 1499 // preparation 1500 getEthernetIfaceData( 1501 ifaceId, 1502 [this, asyncResp, parentIfaceId{std::string(parentIfaceId)}, 1503 ifaceId{std::string(ifaceId)}]( 1504 const bool &success, const EthernetInterfaceData ðData, 1505 const boost::container::flat_set<IPv4AddressData> &ipv4Data) { 1506 if (success && ethData.vlan_id) 1507 { 1508 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId, 1509 ifaceId, ethData, ipv4Data); 1510 1511 auto callback = 1512 [asyncResp](const boost::system::error_code ec) { 1513 if (ec) 1514 { 1515 messages::internalError(asyncResp->res); 1516 } 1517 }; 1518 crow::connections::systemBus->async_method_call( 1519 std::move(callback), "xyz.openbmc_project.Network", 1520 std::string("/xyz/openbmc_project/network/") + ifaceId, 1521 "xyz.openbmc_project.Object.Delete", "Delete"); 1522 } 1523 else 1524 { 1525 // ... otherwise return error 1526 // TODO(Pawel)consider distinguish between non existing 1527 // object, and other errors 1528 messages::resourceNotFound( 1529 asyncResp->res, "VLAN Network Interface", ifaceId); 1530 } 1531 }); 1532 } 1533 }; 1534 1535 /** 1536 * VlanNetworkInterfaceCollection derived class for delivering 1537 * VLANNetworkInterface Collection Schema 1538 */ 1539 class VlanNetworkInterfaceCollection : public Node 1540 { 1541 public: 1542 template <typename CrowApp> 1543 VlanNetworkInterfaceCollection(CrowApp &app) : 1544 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/", 1545 std::string()) 1546 { 1547 entityPrivileges = { 1548 {boost::beast::http::verb::get, {{"Login"}}}, 1549 {boost::beast::http::verb::head, {{"Login"}}}, 1550 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 1551 {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 1552 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 1553 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 1554 } 1555 1556 private: 1557 /** 1558 * Functions triggers appropriate requests on DBus 1559 */ 1560 void doGet(crow::Response &res, const crow::Request &req, 1561 const std::vector<std::string> ¶ms) override 1562 { 1563 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1564 if (params.size() != 1) 1565 { 1566 // This means there is a problem with the router 1567 messages::internalError(asyncResp->res); 1568 return; 1569 } 1570 1571 const std::string &rootInterfaceName = params[0]; 1572 1573 // Get eth interface list, and call the below callback for JSON 1574 // preparation 1575 getEthernetIfaceList( 1576 [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}]( 1577 const bool &success, 1578 const std::vector<std::string> &iface_list) { 1579 if (!success) 1580 { 1581 messages::internalError(asyncResp->res); 1582 return; 1583 } 1584 asyncResp->res.jsonValue["@odata.type"] = 1585 "#VLanNetworkInterfaceCollection." 1586 "VLanNetworkInterfaceCollection"; 1587 asyncResp->res.jsonValue["@odata.context"] = 1588 "/redfish/v1/$metadata" 1589 "#VLanNetworkInterfaceCollection." 1590 "VLanNetworkInterfaceCollection"; 1591 asyncResp->res.jsonValue["Name"] = 1592 "VLAN Network Interface Collection"; 1593 1594 nlohmann::json iface_array = nlohmann::json::array(); 1595 1596 for (const std::string &iface_item : iface_list) 1597 { 1598 if (boost::starts_with(iface_item, rootInterfaceName + "_")) 1599 { 1600 iface_array.push_back( 1601 {{"@odata.id", 1602 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1603 rootInterfaceName + "/VLANs/" + iface_item}}); 1604 } 1605 } 1606 1607 if (iface_array.empty()) 1608 { 1609 messages::resourceNotFound( 1610 asyncResp->res, "EthernetInterface", rootInterfaceName); 1611 return; 1612 } 1613 asyncResp->res.jsonValue["Members@odata.count"] = 1614 iface_array.size(); 1615 asyncResp->res.jsonValue["Members"] = std::move(iface_array); 1616 asyncResp->res.jsonValue["@odata.id"] = 1617 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1618 rootInterfaceName + "/VLANs"; 1619 }); 1620 } 1621 1622 void doPost(crow::Response &res, const crow::Request &req, 1623 const std::vector<std::string> ¶ms) override 1624 { 1625 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1626 if (params.size() != 1) 1627 { 1628 messages::internalError(asyncResp->res); 1629 return; 1630 } 1631 1632 uint32_t vlanId = 0; 1633 if (!json_util::readJson(req, res, "VLANId", vlanId)) 1634 { 1635 return; 1636 } 1637 const std::string &rootInterfaceName = params[0]; 1638 auto callback = [asyncResp](const boost::system::error_code ec) { 1639 if (ec) 1640 { 1641 // TODO(ed) make more consistent error messages based on 1642 // phosphor-network responses 1643 messages::internalError(asyncResp->res); 1644 return; 1645 } 1646 messages::created(asyncResp->res); 1647 }; 1648 crow::connections::systemBus->async_method_call( 1649 std::move(callback), "xyz.openbmc_project.Network", 1650 "/xyz/openbmc_project/network", 1651 "xyz.openbmc_project.Network.VLAN.Create", "VLAN", 1652 rootInterfaceName, vlanId); 1653 } 1654 }; 1655 } // namespace redfish 1656