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