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