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