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