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; |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 85 | std::vector<std::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 | { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 195 | ethData.vlan_id.push_back(*id); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 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 | } |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 223 | else if (propertyPair.first == "Nameservers") |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 224 | { |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 225 | const std::vector<std::string> *nameservers = |
| 226 | sdbusplus::message::variant_ns::get_if< |
| 227 | std::vector<std::string>>( |
| 228 | &propertyPair.second); |
| 229 | if (nameservers != nullptr) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 230 | { |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 231 | ethData.nameservers = std::move(*nameservers); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 234 | else if (propertyPair.first == "DHCPEnabled") |
| 235 | { |
| 236 | const bool *DHCPEnabled = |
| 237 | std::get_if<bool>(&propertyPair.second); |
| 238 | if (DHCPEnabled != nullptr) |
| 239 | { |
| 240 | ethData.DHCPEnabled = *DHCPEnabled; |
| 241 | } |
| 242 | } |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | } |
| 246 | // System configuration shows up in the global namespace, so no need |
| 247 | // to check eth number |
| 248 | if (ifacePair.first == |
| 249 | "xyz.openbmc_project.Network.SystemConfiguration") |
| 250 | { |
| 251 | for (const auto &propertyPair : ifacePair.second) |
| 252 | { |
| 253 | if (propertyPair.first == "HostName") |
| 254 | { |
| 255 | const std::string *hostname = |
| 256 | sdbusplus::message::variant_ns::get_if<std::string>( |
| 257 | &propertyPair.second); |
| 258 | if (hostname != nullptr) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 259 | { |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 260 | ethData.hostname = *hostname; |
| 261 | } |
| 262 | } |
| 263 | else if (propertyPair.first == "DefaultGateway") |
| 264 | { |
| 265 | const std::string *defaultGateway = |
| 266 | sdbusplus::message::variant_ns::get_if<std::string>( |
| 267 | &propertyPair.second); |
| 268 | if (defaultGateway != nullptr) |
| 269 | { |
| 270 | ethData.default_gateway = *defaultGateway; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 271 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 276 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // Helper function that extracts data for single ethernet ipv4 address |
| 280 | inline void |
| 281 | extractIPData(const std::string ðiface_id, |
| 282 | const GetManagedObjects &dbus_data, |
| 283 | boost::container::flat_set<IPv4AddressData> &ipv4_config) |
| 284 | { |
| 285 | const std::string ipv4PathStart = |
| 286 | "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/"; |
| 287 | |
| 288 | // Since there might be several IPv4 configurations aligned with |
| 289 | // single ethernet interface, loop over all of them |
| 290 | for (const auto &objpath : dbus_data) |
| 291 | { |
| 292 | // Check if proper pattern for object path appears |
| 293 | if (boost::starts_with(objpath.first.str, ipv4PathStart)) |
| 294 | { |
| 295 | for (auto &interface : objpath.second) |
| 296 | { |
| 297 | if (interface.first == "xyz.openbmc_project.Network.IP") |
| 298 | { |
| 299 | // Instance IPv4AddressData structure, and set as |
| 300 | // appropriate |
| 301 | std::pair< |
| 302 | boost::container::flat_set<IPv4AddressData>::iterator, |
| 303 | bool> |
| 304 | it = ipv4_config.insert( |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 305 | {objpath.first.str.substr(ipv4PathStart.size())}); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 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 | /** |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 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 | { |
| 503 | messages::internalError(asyncResp->res); |
| 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, |
| 516 | std::variant<std::string>(newValue)); |
| 517 | } |
| 518 | |
| 519 | /** |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 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 | */ |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 533 | inline void changeIPv4Origin(const std::string &ifaceId, int ipIdx, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 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 | */ |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 573 | inline void changeIPv4SubnetMaskProperty(const std::string &ifaceId, int ipIdx, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 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 | */ |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 640 | inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx, |
| 641 | uint8_t subnetMask, const std::string &gateway, |
| 642 | const std::string &address, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 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 | } |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 672 | nlohmann::json &DHCPConfigTypeJson = asyncResp->res.jsonValue["DHCPv4"]; |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 673 | for (const auto &property : dbus_data) |
| 674 | { |
| 675 | auto value = |
| 676 | sdbusplus::message::variant_ns::get_if<bool>(&property.second); |
| 677 | |
| 678 | if (value == nullptr) |
| 679 | { |
| 680 | continue; |
| 681 | } |
| 682 | if (property.first == "DNSEnabled") |
| 683 | { |
| 684 | DHCPConfigTypeJson["UseDNSServers"] = *value; |
| 685 | } |
| 686 | else if (property.first == "HostNameEnabled") |
| 687 | { |
| 688 | DHCPConfigTypeJson["UseDomainName"] = *value; |
| 689 | } |
| 690 | else if (property.first == "NTPEnabled") |
| 691 | { |
| 692 | DHCPConfigTypeJson["UseNTPServers"] = *value; |
| 693 | } |
| 694 | } |
| 695 | }; |
| 696 | crow::connections::systemBus->async_method_call( |
| 697 | std::move(getConfig), "xyz.openbmc_project.Network", |
| 698 | "/xyz/openbmc_project/network/config/dhcp", |
| 699 | "org.freedesktop.DBus.Properties", "GetAll", |
| 700 | "xyz.openbmc_project.Network.DHCPConfiguration"); |
| 701 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 702 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 703 | /** |
| 704 | * Function that retrieves all properties for given Ethernet Interface |
| 705 | * Object |
| 706 | * from EntityManager Network Manager |
| 707 | * @param ethiface_id a eth interface id to query on DBus |
| 708 | * @param callback a function that shall be called to convert Dbus output |
| 709 | * into JSON |
| 710 | */ |
| 711 | template <typename CallbackFunc> |
| 712 | void getEthernetIfaceData(const std::string ðiface_id, |
| 713 | CallbackFunc &&callback) |
| 714 | { |
| 715 | crow::connections::systemBus->async_method_call( |
| 716 | [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}]( |
| 717 | const boost::system::error_code error_code, |
| 718 | const GetManagedObjects &resp) { |
| 719 | EthernetInterfaceData ethData{}; |
| 720 | boost::container::flat_set<IPv4AddressData> ipv4Data; |
| 721 | |
| 722 | if (error_code) |
| 723 | { |
| 724 | callback(false, ethData, ipv4Data); |
| 725 | return; |
| 726 | } |
| 727 | |
| 728 | extractEthernetInterfaceData(ethiface_id, resp, ethData); |
| 729 | extractIPData(ethiface_id, resp, ipv4Data); |
| 730 | |
| 731 | // Fix global GW |
| 732 | for (IPv4AddressData &ipv4 : ipv4Data) |
| 733 | { |
| 734 | if ((ipv4.linktype == LinkType::Global) && |
| 735 | (ipv4.gateway == "0.0.0.0")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 736 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 737 | ipv4.gateway = ethData.default_gateway; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | // Finally make a callback with usefull data |
| 742 | callback(true, ethData, ipv4Data); |
| 743 | }, |
| 744 | "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", |
| 745 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 746 | }; |
| 747 | |
| 748 | /** |
| 749 | * Function that retrieves all Ethernet Interfaces available through Network |
| 750 | * Manager |
| 751 | * @param callback a function that shall be called to convert Dbus output |
| 752 | * into JSON. |
| 753 | */ |
| 754 | template <typename CallbackFunc> |
| 755 | void getEthernetIfaceList(CallbackFunc &&callback) |
| 756 | { |
| 757 | crow::connections::systemBus->async_method_call( |
| 758 | [callback{std::move(callback)}]( |
| 759 | const boost::system::error_code error_code, |
| 760 | GetManagedObjects &resp) { |
| 761 | // Callback requires vector<string> to retrieve all available |
| 762 | // ethernet interfaces |
| 763 | std::vector<std::string> iface_list; |
| 764 | iface_list.reserve(resp.size()); |
| 765 | if (error_code) |
| 766 | { |
| 767 | callback(false, iface_list); |
| 768 | return; |
| 769 | } |
| 770 | |
| 771 | // Iterate over all retrieved ObjectPaths. |
| 772 | for (const auto &objpath : resp) |
| 773 | { |
| 774 | // And all interfaces available for certain ObjectPath. |
| 775 | for (const auto &interface : objpath.second) |
| 776 | { |
| 777 | // If interface is |
| 778 | // xyz.openbmc_project.Network.EthernetInterface, this is |
| 779 | // what we're looking for. |
| 780 | if (interface.first == |
| 781 | "xyz.openbmc_project.Network.EthernetInterface") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 782 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 783 | // Cut out everyting until last "/", ... |
| 784 | const std::string &iface_id = objpath.first.str; |
| 785 | std::size_t last_pos = iface_id.rfind("/"); |
| 786 | if (last_pos != std::string::npos) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 787 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 788 | // and put it into output vector. |
| 789 | iface_list.emplace_back( |
| 790 | iface_id.substr(last_pos + 1)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 794 | } |
| 795 | // Finally make a callback with useful data |
| 796 | callback(true, iface_list); |
| 797 | }, |
| 798 | "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", |
| 799 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 800 | }; |
| 801 | |
| 802 | /** |
| 803 | * EthernetCollection derived class for delivering Ethernet Collection Schema |
| 804 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 805 | class EthernetCollection : public Node |
| 806 | { |
| 807 | public: |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 808 | template <typename CrowApp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 809 | EthernetCollection(CrowApp &app) : |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 810 | Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 811 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 812 | entityPrivileges = { |
| 813 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 814 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 815 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 816 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 817 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 818 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
| 819 | } |
| 820 | |
| 821 | private: |
| 822 | /** |
| 823 | * Functions triggers appropriate requests on DBus |
| 824 | */ |
| 825 | void doGet(crow::Response &res, const crow::Request &req, |
| 826 | const std::vector<std::string> ¶ms) override |
| 827 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 828 | res.jsonValue["@odata.type"] = |
| 829 | "#EthernetInterfaceCollection.EthernetInterfaceCollection"; |
| 830 | res.jsonValue["@odata.context"] = |
| 831 | "/redfish/v1/" |
| 832 | "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection"; |
| 833 | res.jsonValue["@odata.id"] = |
| 834 | "/redfish/v1/Managers/bmc/EthernetInterfaces"; |
| 835 | res.jsonValue["Name"] = "Ethernet Network Interface Collection"; |
| 836 | res.jsonValue["Description"] = |
| 837 | "Collection of EthernetInterfaces for this Manager"; |
| 838 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 839 | // Get eth interface list, and call the below callback for JSON |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 840 | // preparation |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 841 | getEthernetIfaceList( |
| 842 | [&res](const bool &success, |
| 843 | const std::vector<std::string> &iface_list) { |
| 844 | if (!success) |
| 845 | { |
| 846 | messages::internalError(res); |
| 847 | res.end(); |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | nlohmann::json &iface_array = res.jsonValue["Members"]; |
| 852 | iface_array = nlohmann::json::array(); |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 853 | std::string tag = "_"; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 854 | for (const std::string &iface_item : iface_list) |
| 855 | { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 856 | std::size_t found = iface_item.find(tag); |
| 857 | if (found == std::string::npos) |
| 858 | { |
| 859 | iface_array.push_back( |
| 860 | {{"@odata.id", |
| 861 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + |
| 862 | iface_item}}); |
| 863 | } |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | res.jsonValue["Members@odata.count"] = iface_array.size(); |
| 867 | res.jsonValue["@odata.id"] = |
| 868 | "/redfish/v1/Managers/bmc/EthernetInterfaces"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 869 | res.end(); |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 870 | }); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 871 | } |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 872 | }; |
| 873 | |
| 874 | /** |
| 875 | * EthernetInterface derived class for delivering Ethernet Schema |
| 876 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 877 | class EthernetInterface : public Node |
| 878 | { |
| 879 | public: |
| 880 | /* |
| 881 | * Default Constructor |
| 882 | */ |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 883 | template <typename CrowApp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 884 | EthernetInterface(CrowApp &app) : |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 885 | Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/", |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 886 | std::string()) |
| 887 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 888 | entityPrivileges = { |
| 889 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 890 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 891 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 892 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 893 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 894 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 895 | } |
| 896 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 897 | private: |
Ed Tanous | bc0bd6e | 2018-12-10 14:07:55 -0800 | [diff] [blame] | 898 | void handleHostnamePatch(const std::string &hostname, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 899 | const std::shared_ptr<AsyncResp> asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 900 | { |
Ed Tanous | bc0bd6e | 2018-12-10 14:07:55 -0800 | [diff] [blame] | 901 | asyncResp->res.jsonValue["HostName"] = hostname; |
| 902 | crow::connections::systemBus->async_method_call( |
| 903 | [asyncResp](const boost::system::error_code ec) { |
| 904 | if (ec) |
| 905 | { |
| 906 | messages::internalError(asyncResp->res); |
| 907 | } |
| 908 | }, |
| 909 | "xyz.openbmc_project.Network", |
| 910 | "/xyz/openbmc_project/network/config", |
| 911 | "org.freedesktop.DBus.Properties", "Set", |
| 912 | "xyz.openbmc_project.Network.SystemConfiguration", "HostName", |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 913 | std::variant<std::string>(hostname)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 914 | } |
| 915 | |
Ratan Gupta | d577665 | 2019-03-03 08:47:22 +0530 | [diff] [blame] | 916 | void handleMACAddressPatch(const std::string &ifaceId, |
| 917 | const std::string &macAddress, |
| 918 | const std::shared_ptr<AsyncResp> &asyncResp) |
| 919 | { |
| 920 | crow::connections::systemBus->async_method_call( |
| 921 | [asyncResp, macAddress](const boost::system::error_code ec) { |
| 922 | if (ec) |
| 923 | { |
| 924 | messages::internalError(asyncResp->res); |
| 925 | return; |
| 926 | } |
| 927 | asyncResp->res.jsonValue["MACAddress"] = std::move(macAddress); |
| 928 | }, |
| 929 | "xyz.openbmc_project.Network", |
| 930 | "/xyz/openbmc_project/network/" + ifaceId, |
| 931 | "org.freedesktop.DBus.Properties", "Set", |
| 932 | "xyz.openbmc_project.Network.MACAddress", "MACAddress", |
| 933 | std::variant<std::string>(macAddress)); |
| 934 | } |
| 935 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 936 | void handleIPv4Patch( |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 937 | const std::string &ifaceId, nlohmann::json &input, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 938 | const boost::container::flat_set<IPv4AddressData> &ipv4Data, |
| 939 | const std::shared_ptr<AsyncResp> asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 940 | { |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 941 | if (!input.is_array()) |
| 942 | { |
| 943 | messages::propertyValueTypeError(asyncResp->res, input.dump(), |
| 944 | "IPv4Addresses"); |
| 945 | return; |
| 946 | } |
| 947 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 948 | int entryIdx = 0; |
| 949 | boost::container::flat_set<IPv4AddressData>::const_iterator thisData = |
| 950 | ipv4Data.begin(); |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 951 | for (nlohmann::json &thisJson : input) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 952 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 953 | std::string pathString = |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 954 | "IPv4Addresses/" + std::to_string(entryIdx); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 955 | |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 956 | if (thisJson.is_null()) |
| 957 | { |
| 958 | if (thisData != ipv4Data.end()) |
| 959 | { |
| 960 | deleteIPv4(ifaceId, thisData->id, entryIdx, asyncResp); |
| 961 | thisData++; |
| 962 | } |
| 963 | else |
| 964 | { |
| 965 | messages::propertyValueFormatError( |
| 966 | asyncResp->res, input.dump(), pathString); |
| 967 | return; |
| 968 | // TODO(ratagupt) Not sure about the property where value is |
| 969 | // list and if unable to update one of the |
| 970 | // list value then should we proceed further or |
| 971 | // break there, would ask in the redfish forum |
| 972 | // till then we stop processing the next list item. |
| 973 | } |
| 974 | entryIdx++; |
| 975 | continue; // not an error as per the redfish spec. |
| 976 | } |
| 977 | |
Ratan Gupta | 9474b37 | 2019-03-01 15:13:37 +0530 | [diff] [blame] | 978 | if (thisJson.empty()) |
| 979 | { |
| 980 | if (thisData != ipv4Data.end()) |
| 981 | { |
| 982 | thisData++; |
| 983 | } |
| 984 | else |
| 985 | { |
| 986 | messages::propertyMissing(asyncResp->res, |
| 987 | pathString + "/Address"); |
| 988 | return; |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 989 | // TODO(ratagupt) Not sure about the property where value is |
Ratan Gupta | 9474b37 | 2019-03-01 15:13:37 +0530 | [diff] [blame] | 990 | // list and if unable to update one of the |
| 991 | // list value then should we proceed further or |
| 992 | // break there, would ask in the redfish forum |
| 993 | // till then we stop processing the next list item. |
| 994 | } |
| 995 | entryIdx++; |
| 996 | continue; // not an error as per the redfish spec. |
| 997 | } |
| 998 | |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 999 | std::optional<std::string> address; |
| 1000 | std::optional<std::string> addressOrigin; |
| 1001 | std::optional<std::string> subnetMask; |
| 1002 | std::optional<std::string> gateway; |
| 1003 | |
| 1004 | if (!json_util::readJson(thisJson, asyncResp->res, "Address", |
| 1005 | address, "AddressOrigin", addressOrigin, |
| 1006 | "SubnetMask", subnetMask, "Gateway", |
| 1007 | gateway)) |
| 1008 | { |
| 1009 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1012 | if (address) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1013 | { |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1014 | if (!ipv4VerifyIpAndGetBitcount(*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 | messages::propertyValueFormatError(asyncResp->res, *address, |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 1017 | pathString + "/Address"); |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1018 | return; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1019 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1020 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1021 | |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1022 | uint8_t prefixLength = 0; |
| 1023 | if (subnetMask) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1024 | { |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1025 | if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength)) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1026 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1027 | messages::propertyValueFormatError( |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1028 | asyncResp->res, *subnetMask, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1029 | pathString + "/SubnetMask"); |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1030 | return; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1031 | } |
| 1032 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1033 | std::string addressOriginInDBusFormat; |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1034 | if (addressOrigin) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1035 | { |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1036 | // Get Address origin in proper format |
| 1037 | addressOriginInDBusFormat = |
| 1038 | translateAddressOriginRedfishToDbus(*addressOrigin); |
| 1039 | if (addressOriginInDBusFormat.empty()) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1040 | { |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1041 | messages::propertyValueNotInList( |
| 1042 | asyncResp->res, *addressOrigin, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1043 | pathString + "/AddressOrigin"); |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1044 | return; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1048 | if (gateway) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1049 | { |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1050 | if (!ipv4VerifyIpAndGetBitcount(*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 | messages::propertyValueFormatError(asyncResp->res, *gateway, |
| 1053 | pathString + "/Gateway"); |
| 1054 | return; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1058 | // if IP address exist then modify it. |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1059 | if (thisData != ipv4Data.end()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1060 | { |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1061 | // Apply changes |
| 1062 | if (address) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1063 | { |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1064 | auto callback = [asyncResp, entryIdx, |
| 1065 | address{std::string(*address)}]( |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1066 | const boost::system::error_code ec) { |
| 1067 | if (ec) |
| 1068 | { |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 1069 | messages::internalError(asyncResp->res); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1070 | return; |
| 1071 | } |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1072 | asyncResp->res |
| 1073 | .jsonValue["IPv4Addresses"][entryIdx]["Address"] = |
| 1074 | std::move(address); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1075 | }; |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1076 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1077 | crow::connections::systemBus->async_method_call( |
| 1078 | std::move(callback), "xyz.openbmc_project.Network", |
| 1079 | "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + |
| 1080 | thisData->id, |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1081 | "org.freedesktop.DBus.Properties", "Set", |
| 1082 | "xyz.openbmc_project.Network.IP", "Address", |
| 1083 | std::variant<std::string>(*address)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1084 | } |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1085 | |
| 1086 | if (subnetMask) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1087 | { |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1088 | changeIPv4SubnetMaskProperty(ifaceId, entryIdx, |
| 1089 | thisData->id, *subnetMask, |
| 1090 | prefixLength, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1091 | } |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1092 | |
| 1093 | if (addressOrigin) |
| 1094 | { |
| 1095 | changeIPv4Origin(ifaceId, entryIdx, thisData->id, |
| 1096 | *addressOrigin, addressOriginInDBusFormat, |
| 1097 | asyncResp); |
| 1098 | } |
| 1099 | |
| 1100 | if (gateway) |
| 1101 | { |
| 1102 | auto callback = [asyncResp, entryIdx, |
| 1103 | gateway{std::string(*gateway)}]( |
| 1104 | const boost::system::error_code ec) { |
| 1105 | if (ec) |
| 1106 | { |
| 1107 | messages::internalError(asyncResp->res); |
| 1108 | return; |
| 1109 | } |
| 1110 | asyncResp->res |
| 1111 | .jsonValue["IPv4Addresses"][entryIdx]["Gateway"] = |
| 1112 | std::move(gateway); |
| 1113 | }; |
| 1114 | |
| 1115 | crow::connections::systemBus->async_method_call( |
| 1116 | std::move(callback), "xyz.openbmc_project.Network", |
| 1117 | "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + |
| 1118 | thisData->id, |
| 1119 | "org.freedesktop.DBus.Properties", "Set", |
| 1120 | "xyz.openbmc_project.Network.IP", "Gateway", |
| 1121 | std::variant<std::string>(*gateway)); |
| 1122 | } |
| 1123 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1124 | thisData++; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1125 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1126 | else |
| 1127 | { |
| 1128 | // Create IPv4 with provided data |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1129 | if (!gateway) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1130 | { |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 1131 | messages::propertyMissing(asyncResp->res, |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1132 | pathString + "/Gateway"); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1133 | continue; |
| 1134 | } |
| 1135 | |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1136 | if (!address) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1137 | { |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 1138 | messages::propertyMissing(asyncResp->res, |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1139 | pathString + "/Address"); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1140 | continue; |
| 1141 | } |
| 1142 | |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1143 | if (!subnetMask) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1144 | { |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 1145 | messages::propertyMissing(asyncResp->res, |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1146 | pathString + "/SubnetMask"); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1147 | continue; |
| 1148 | } |
| 1149 | |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 1150 | createIPv4(ifaceId, entryIdx, prefixLength, *gateway, *address, |
| 1151 | asyncResp); |
Ratan Gupta | 95897b2 | 2019-03-07 18:25:57 +0530 | [diff] [blame] | 1152 | |
| 1153 | nlohmann::json &ipv4AddressJson = |
| 1154 | asyncResp->res.jsonValue["IPv4Addresses"][entryIdx]; |
| 1155 | ipv4AddressJson["Address"] = *address; |
| 1156 | ipv4AddressJson["SubnetMask"] = *subnetMask; |
| 1157 | ipv4AddressJson["Gateway"] = *gateway; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1158 | } |
| 1159 | entryIdx++; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1163 | void handleStaticNameServersPatch( |
| 1164 | const std::string &ifaceId, |
| 1165 | const std::vector<std::string> &updatedStaticNameServers, |
| 1166 | const std::shared_ptr<AsyncResp> &asyncResp) |
| 1167 | { |
| 1168 | crow::connections::systemBus->async_method_call( |
| 1169 | [asyncResp, |
| 1170 | updatedStaticNameServers](const boost::system::error_code ec) { |
| 1171 | if (ec) |
| 1172 | { |
| 1173 | messages::internalError(asyncResp->res); |
| 1174 | return; |
| 1175 | } |
| 1176 | asyncResp->res.jsonValue["NameServers"] = |
| 1177 | updatedStaticNameServers; |
| 1178 | asyncResp->res.jsonValue["StaticNameServers"] = |
| 1179 | updatedStaticNameServers; |
| 1180 | }, |
| 1181 | "xyz.openbmc_project.Network", |
| 1182 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1183 | "org.freedesktop.DBus.Properties", "Set", |
| 1184 | "xyz.openbmc_project.Network.EthernetInterface", "Nameservers", |
| 1185 | std::variant<std::vector<std::string>>{updatedStaticNameServers}); |
| 1186 | } |
| 1187 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1188 | void parseInterfaceData( |
| 1189 | nlohmann::json &json_response, const std::string &iface_id, |
| 1190 | const EthernetInterfaceData ðData, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1191 | const boost::container::flat_set<IPv4AddressData> &ipv4Data) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1192 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1193 | json_response["Id"] = iface_id; |
| 1194 | json_response["@odata.id"] = |
| 1195 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id; |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 1196 | json_response["InterfaceEnabled"] = true; |
| 1197 | if (ethData.speed == 0) |
| 1198 | { |
| 1199 | json_response["LinkStatus"] = "NoLink"; |
| 1200 | json_response["Status"] = { |
| 1201 | {"Health", "OK"}, |
| 1202 | {"State", "Disabled"}, |
| 1203 | }; |
| 1204 | } |
| 1205 | else |
| 1206 | { |
| 1207 | json_response["LinkStatus"] = "LinkUp"; |
| 1208 | json_response["Status"] = { |
| 1209 | {"Health", "OK"}, |
| 1210 | {"State", "Enabled"}, |
| 1211 | }; |
| 1212 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1213 | json_response["SpeedMbps"] = ethData.speed; |
| 1214 | json_response["MACAddress"] = ethData.mac_address; |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1215 | json_response["DHCPv4"]["DHCPEnabled"] = ethData.DHCPEnabled; |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 1216 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1217 | if (!ethData.hostname.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1218 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1219 | json_response["HostName"] = ethData.hostname; |
| 1220 | } |
| 1221 | |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1222 | json_response["VLANs"] = { |
| 1223 | {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" + |
| 1224 | iface_id + "/VLANs"}}; |
| 1225 | |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 1226 | json_response["NameServers"] = ethData.nameservers; |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1227 | json_response["StaticNameServers"] = ethData.nameservers; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1228 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1229 | if (ipv4Data.size() > 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1230 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1231 | nlohmann::json &ipv4_array = json_response["IPv4Addresses"]; |
| 1232 | ipv4_array = nlohmann::json::array(); |
| 1233 | for (auto &ipv4_config : ipv4Data) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1234 | { |
Gunnar Mills | fa5053a | 2019-04-03 16:53:44 -0500 | [diff] [blame^] | 1235 | |
| 1236 | std::string gatewayStr = ipv4_config.gateway; |
| 1237 | if (gatewayStr.empty()) |
| 1238 | { |
| 1239 | gatewayStr = "0.0.0.0"; |
| 1240 | } |
| 1241 | |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 1242 | ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin}, |
| 1243 | {"SubnetMask", ipv4_config.netmask}, |
| 1244 | {"Address", ipv4_config.address}, |
Gunnar Mills | fa5053a | 2019-04-03 16:53:44 -0500 | [diff] [blame^] | 1245 | {"Gateway", gatewayStr}}); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1246 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1247 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | /** |
| 1251 | * Functions triggers appropriate requests on DBus |
| 1252 | */ |
| 1253 | void doGet(crow::Response &res, const crow::Request &req, |
| 1254 | const std::vector<std::string> ¶ms) override |
| 1255 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1256 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1257 | if (params.size() != 1) |
| 1258 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1259 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1260 | return; |
| 1261 | } |
| 1262 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1263 | getEthernetIfaceData( |
| 1264 | params[0], |
| 1265 | [this, asyncResp, iface_id{std::string(params[0])}]( |
| 1266 | const bool &success, const EthernetInterfaceData ðData, |
| 1267 | const boost::container::flat_set<IPv4AddressData> &ipv4Data) { |
| 1268 | if (!success) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1269 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1270 | // TODO(Pawel)consider distinguish between non existing |
| 1271 | // object, and other errors |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1272 | messages::resourceNotFound(asyncResp->res, |
| 1273 | "EthernetInterface", iface_id); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1274 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1275 | } |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1276 | asyncResp->res.jsonValue["@odata.type"] = |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1277 | "#EthernetInterface.v1_4_1.EthernetInterface"; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1278 | asyncResp->res.jsonValue["@odata.context"] = |
| 1279 | "/redfish/v1/$metadata#EthernetInterface.EthernetInterface"; |
| 1280 | asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface"; |
| 1281 | asyncResp->res.jsonValue["Description"] = |
| 1282 | "Management Network Interface"; |
| 1283 | |
| 1284 | parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData, |
| 1285 | ipv4Data); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1286 | }); |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 1287 | getDHCPConfigData(asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | void doPatch(crow::Response &res, const crow::Request &req, |
| 1291 | const std::vector<std::string> ¶ms) override |
| 1292 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1293 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1294 | if (params.size() != 1) |
| 1295 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1296 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1297 | return; |
| 1298 | } |
| 1299 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1300 | const std::string &iface_id = params[0]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1301 | |
Ed Tanous | bc0bd6e | 2018-12-10 14:07:55 -0800 | [diff] [blame] | 1302 | std::optional<std::string> hostname; |
Ratan Gupta | d577665 | 2019-03-03 08:47:22 +0530 | [diff] [blame] | 1303 | std::optional<std::string> macAddress; |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1304 | std::optional<nlohmann::json> ipv4Addresses; |
| 1305 | std::optional<nlohmann::json> ipv6Addresses; |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1306 | std::optional<std::vector<std::string>> staticNameServers; |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1307 | |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1308 | if (!json_util::readJson( |
| 1309 | req, res, "HostName", hostname, "IPv4Addresses", ipv4Addresses, |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1310 | "IPv6Addresses", ipv6Addresses, "MACAddress", macAddress, |
| 1311 | "StaticNameServers", staticNameServers)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1312 | { |
| 1313 | return; |
| 1314 | } |
Ratan Gupta | f15aad3 | 2019-03-01 13:41:13 +0530 | [diff] [blame] | 1315 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1316 | // Get single eth interface data, and call the below callback for JSON |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1317 | // preparation |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1318 | getEthernetIfaceData( |
| 1319 | iface_id, |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1320 | [this, asyncResp, iface_id, hostname = std::move(hostname), |
| 1321 | macAddress = std::move(macAddress), |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1322 | ipv4Addresses = std::move(ipv4Addresses), |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1323 | ipv6Addresses = std::move(ipv6Addresses), |
| 1324 | staticNameServers = std::move(staticNameServers)]( |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1325 | const bool &success, const EthernetInterfaceData ðData, |
| 1326 | const boost::container::flat_set<IPv4AddressData> &ipv4Data) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1327 | if (!success) |
| 1328 | { |
| 1329 | // ... otherwise return error |
| 1330 | // TODO(Pawel)consider distinguish between non existing |
| 1331 | // object, and other errors |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1332 | messages::resourceNotFound(asyncResp->res, |
| 1333 | "Ethernet Interface", iface_id); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1334 | return; |
| 1335 | } |
| 1336 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1337 | parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData, |
| 1338 | ipv4Data); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1339 | |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1340 | if (hostname) |
| 1341 | { |
| 1342 | handleHostnamePatch(*hostname, asyncResp); |
| 1343 | } |
| 1344 | |
Ratan Gupta | d577665 | 2019-03-03 08:47:22 +0530 | [diff] [blame] | 1345 | if (macAddress) |
| 1346 | { |
| 1347 | handleMACAddressPatch(iface_id, *macAddress, asyncResp); |
| 1348 | } |
| 1349 | |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1350 | if (ipv4Addresses) |
| 1351 | { |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1352 | // TODO(ed) for some reason the capture of ipv4Addresses |
| 1353 | // above is returning a const value, not a non-const value. |
| 1354 | // This doesn't really work for us, as we need to be able to |
| 1355 | // efficiently move out the intermedia nlohmann::json |
| 1356 | // objects. This makes a copy of the structure, and operates |
| 1357 | // on that, but could be done more efficiently |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1358 | nlohmann::json ipv4 = std::move(*ipv4Addresses); |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1359 | handleIPv4Patch(iface_id, ipv4, ipv4Data, asyncResp); |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | if (ipv6Addresses) |
| 1363 | { |
| 1364 | // TODO(kkowalsk) IPv6 Not supported on D-Bus yet |
| 1365 | messages::propertyNotWritable(asyncResp->res, |
| 1366 | "IPv6Addresses"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1367 | } |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1368 | |
| 1369 | if (staticNameServers) |
| 1370 | { |
| 1371 | handleStaticNameServersPatch(iface_id, *staticNameServers, |
| 1372 | asyncResp); |
| 1373 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1374 | }); |
| 1375 | } |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 1376 | }; |
| 1377 | |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 1378 | /** |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1379 | * VlanNetworkInterface derived class for delivering VLANNetworkInterface |
| 1380 | * Schema |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 1381 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1382 | class VlanNetworkInterface : public Node |
| 1383 | { |
| 1384 | public: |
| 1385 | /* |
| 1386 | * Default Constructor |
| 1387 | */ |
| 1388 | template <typename CrowApp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1389 | VlanNetworkInterface(CrowApp &app) : |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1390 | Node(app, |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1391 | "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>", |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1392 | std::string(), std::string()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1393 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1394 | entityPrivileges = { |
| 1395 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 1396 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 1397 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 1398 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 1399 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 1400 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 1401 | } |
| 1402 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1403 | private: |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1404 | void parseInterfaceData( |
| 1405 | nlohmann::json &json_response, const std::string &parent_iface_id, |
| 1406 | const std::string &iface_id, const EthernetInterfaceData ðData, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1407 | const boost::container::flat_set<IPv4AddressData> &ipv4Data) |
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 | // Fill out obvious data... |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1410 | json_response["Id"] = iface_id; |
| 1411 | json_response["@odata.id"] = |
| 1412 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id + |
| 1413 | "/VLANs/" + iface_id; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1414 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1415 | json_response["VLANEnable"] = true; |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1416 | if (!ethData.vlan_id.empty()) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1417 | { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1418 | json_response["VLANId"] = ethData.vlan_id.back(); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1419 | } |
Ed Tanous | a434f2b | 2018-07-27 13:04:22 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1422 | bool verifyNames(const std::string &parent, const std::string &iface) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1423 | { |
| 1424 | if (!boost::starts_with(iface, parent + "_")) |
| 1425 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1426 | return false; |
| 1427 | } |
| 1428 | else |
| 1429 | { |
| 1430 | return true; |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | /** |
| 1435 | * Functions triggers appropriate requests on DBus |
| 1436 | */ |
| 1437 | void doGet(crow::Response &res, const crow::Request &req, |
| 1438 | const std::vector<std::string> ¶ms) override |
| 1439 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1440 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
| 1441 | // TODO(Pawel) this shall be parameterized call (two params) to get |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1442 | // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'. |
| 1443 | // Check if there is required param, truly entering this shall be |
| 1444 | // impossible. |
| 1445 | if (params.size() != 2) |
| 1446 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1447 | messages::internalError(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1448 | res.end(); |
Kowalski, Kamil | 927a505 | 2018-07-03 14:16:46 +0200 | [diff] [blame] | 1449 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1450 | } |
Kowalski, Kamil | 927a505 | 2018-07-03 14:16:46 +0200 | [diff] [blame] | 1451 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1452 | const std::string &parent_iface_id = params[0]; |
| 1453 | const std::string &iface_id = params[1]; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1454 | res.jsonValue["@odata.type"] = |
| 1455 | "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; |
| 1456 | res.jsonValue["@odata.context"] = |
| 1457 | "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface"; |
| 1458 | res.jsonValue["Name"] = "VLAN Network Interface"; |
Kowalski, Kamil | 927a505 | 2018-07-03 14:16:46 +0200 | [diff] [blame] | 1459 | |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1460 | if (!verifyNames(parent_iface_id, iface_id)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1461 | { |
| 1462 | return; |
| 1463 | } |
Kowalski, Kamil | 927a505 | 2018-07-03 14:16:46 +0200 | [diff] [blame] | 1464 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1465 | // Get single eth interface data, and call the below callback for JSON |
| 1466 | // preparation |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1467 | getEthernetIfaceData( |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1468 | params[1], |
| 1469 | [this, asyncResp, parent_iface_id{std::string(params[0])}, |
| 1470 | iface_id{std::string(params[1])}]( |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1471 | const bool &success, const EthernetInterfaceData ðData, |
| 1472 | const boost::container::flat_set<IPv4AddressData> &ipv4Data) { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1473 | if (success && ethData.vlan_id.size() != 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1474 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1475 | parseInterfaceData(asyncResp->res.jsonValue, |
| 1476 | parent_iface_id, iface_id, ethData, |
| 1477 | ipv4Data); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1478 | } |
| 1479 | else |
| 1480 | { |
| 1481 | // ... otherwise return error |
| 1482 | // TODO(Pawel)consider distinguish between non existing |
| 1483 | // object, and other errors |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1484 | messages::resourceNotFound( |
| 1485 | asyncResp->res, "VLAN Network Interface", iface_id); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1486 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1487 | }); |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 1488 | } |
| 1489 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1490 | void doPatch(crow::Response &res, const crow::Request &req, |
| 1491 | const std::vector<std::string> ¶ms) override |
| 1492 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1493 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1494 | if (params.size() != 2) |
| 1495 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1496 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1497 | return; |
| 1498 | } |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 1499 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1500 | const std::string &parentIfaceId = params[0]; |
| 1501 | const std::string &ifaceId = params[1]; |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 1502 | |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1503 | if (!verifyNames(parentIfaceId, ifaceId)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1504 | { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1505 | messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", |
| 1506 | ifaceId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1507 | return; |
| 1508 | } |
| 1509 | |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1510 | bool vlanEnable = false; |
| 1511 | uint64_t vlanId = 0; |
| 1512 | |
| 1513 | if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId", |
| 1514 | vlanId)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1515 | { |
| 1516 | return; |
| 1517 | } |
| 1518 | |
| 1519 | // Get single eth interface data, and call the below callback for JSON |
| 1520 | // preparation |
Sunitha Harish | 08244d0 | 2019-04-01 03:57:25 -0500 | [diff] [blame] | 1521 | getEthernetIfaceData(params[1], [this, asyncResp, |
| 1522 | parentIfaceId{std::string(params[0])}, |
| 1523 | ifaceId{std::string(params[1])}, |
| 1524 | &vlanEnable, &vlanId]( |
| 1525 | const bool &success, |
| 1526 | const EthernetInterfaceData |
| 1527 | ðData, |
| 1528 | const boost::container::flat_set< |
| 1529 | IPv4AddressData> &ipv4Data) { |
| 1530 | if (success && !ethData.vlan_id.empty()) |
| 1531 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1532 | parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId, |
| 1533 | ifaceId, ethData, ipv4Data); |
Sunitha Harish | 08244d0 | 2019-04-01 03:57:25 -0500 | [diff] [blame] | 1534 | auto callback = |
| 1535 | [asyncResp](const boost::system::error_code ec) { |
| 1536 | if (ec) |
| 1537 | { |
| 1538 | messages::internalError(asyncResp->res); |
| 1539 | } |
| 1540 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1541 | |
Sunitha Harish | 08244d0 | 2019-04-01 03:57:25 -0500 | [diff] [blame] | 1542 | if (vlanEnable == true) |
| 1543 | { |
| 1544 | crow::connections::systemBus->async_method_call( |
| 1545 | std::move(callback), "xyz.openbmc_project.Network", |
| 1546 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1547 | "org.freedesktop.DBus.Properties", "Set", |
| 1548 | "xyz.openbmc_project.Network.VLAN", "Id", |
| 1549 | std::variant<uint32_t>(vlanId)); |
| 1550 | } |
| 1551 | else |
| 1552 | { |
| 1553 | BMCWEB_LOG_DEBUG |
| 1554 | << "vlanEnable is false. Deleting the vlan interface"; |
| 1555 | crow::connections::systemBus->async_method_call( |
| 1556 | std::move(callback), "xyz.openbmc_project.Network", |
| 1557 | std::string("/xyz/openbmc_project/network/") + ifaceId, |
| 1558 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 1559 | } |
| 1560 | } |
| 1561 | else |
| 1562 | { |
| 1563 | // TODO(Pawel)consider distinguish between non existing |
| 1564 | // object, and other errors |
| 1565 | messages::resourceNotFound(asyncResp->res, |
| 1566 | "VLAN Network Interface", ifaceId); |
| 1567 | return; |
| 1568 | } |
| 1569 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | void doDelete(crow::Response &res, const crow::Request &req, |
| 1573 | const std::vector<std::string> ¶ms) override |
| 1574 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1575 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1576 | if (params.size() != 2) |
| 1577 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1578 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1579 | return; |
| 1580 | } |
| 1581 | |
| 1582 | const std::string &parentIfaceId = params[0]; |
| 1583 | const std::string &ifaceId = params[1]; |
| 1584 | |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1585 | if (!verifyNames(parentIfaceId, ifaceId)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1586 | { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1587 | messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", |
| 1588 | ifaceId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1589 | return; |
| 1590 | } |
| 1591 | |
| 1592 | // Get single eth interface data, and call the below callback for JSON |
| 1593 | // preparation |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1594 | getEthernetIfaceData( |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1595 | params[1], |
| 1596 | [this, asyncResp, parentIfaceId{std::string(params[0])}, |
| 1597 | ifaceId{std::string(params[1])}]( |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1598 | const bool &success, const EthernetInterfaceData ðData, |
| 1599 | const boost::container::flat_set<IPv4AddressData> &ipv4Data) { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1600 | if (success && !ethData.vlan_id.empty()) |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1601 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1602 | parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId, |
| 1603 | ifaceId, ethData, ipv4Data); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1604 | |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1605 | auto callback = |
| 1606 | [asyncResp](const boost::system::error_code ec) { |
| 1607 | if (ec) |
| 1608 | { |
| 1609 | messages::internalError(asyncResp->res); |
| 1610 | } |
| 1611 | }; |
| 1612 | crow::connections::systemBus->async_method_call( |
| 1613 | std::move(callback), "xyz.openbmc_project.Network", |
| 1614 | std::string("/xyz/openbmc_project/network/") + ifaceId, |
| 1615 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 1616 | } |
| 1617 | else |
| 1618 | { |
| 1619 | // ... otherwise return error |
| 1620 | // TODO(Pawel)consider distinguish between non existing |
| 1621 | // object, and other errors |
| 1622 | messages::resourceNotFound( |
| 1623 | asyncResp->res, "VLAN Network Interface", ifaceId); |
| 1624 | } |
| 1625 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1626 | } |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 1627 | }; |
| 1628 | |
| 1629 | /** |
| 1630 | * VlanNetworkInterfaceCollection derived class for delivering |
| 1631 | * VLANNetworkInterface Collection Schema |
| 1632 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1633 | class VlanNetworkInterfaceCollection : public Node |
| 1634 | { |
| 1635 | public: |
| 1636 | template <typename CrowApp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1637 | VlanNetworkInterfaceCollection(CrowApp &app) : |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1638 | Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/", |
| 1639 | std::string()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1640 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1641 | entityPrivileges = { |
| 1642 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 1643 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 1644 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 1645 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 1646 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 1647 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 1648 | } |
| 1649 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1650 | private: |
| 1651 | /** |
| 1652 | * Functions triggers appropriate requests on DBus |
| 1653 | */ |
| 1654 | void doGet(crow::Response &res, const crow::Request &req, |
| 1655 | const std::vector<std::string> ¶ms) override |
| 1656 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1657 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1658 | if (params.size() != 1) |
| 1659 | { |
| 1660 | // This means there is a problem with the router |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1661 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1662 | return; |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1665 | const std::string &rootInterfaceName = params[0]; |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 1666 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1667 | // Get eth interface list, and call the below callback for JSON |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1668 | // preparation |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1669 | getEthernetIfaceList( |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 1670 | [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}]( |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1671 | const bool &success, |
| 1672 | const std::vector<std::string> &iface_list) { |
| 1673 | if (!success) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1674 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1675 | messages::internalError(asyncResp->res); |
| 1676 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1677 | } |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1678 | asyncResp->res.jsonValue["@odata.type"] = |
| 1679 | "#VLanNetworkInterfaceCollection." |
| 1680 | "VLanNetworkInterfaceCollection"; |
| 1681 | asyncResp->res.jsonValue["@odata.context"] = |
| 1682 | "/redfish/v1/$metadata" |
| 1683 | "#VLanNetworkInterfaceCollection." |
| 1684 | "VLanNetworkInterfaceCollection"; |
| 1685 | asyncResp->res.jsonValue["Name"] = |
| 1686 | "VLAN Network Interface Collection"; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1687 | |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1688 | nlohmann::json iface_array = nlohmann::json::array(); |
| 1689 | |
| 1690 | for (const std::string &iface_item : iface_list) |
| 1691 | { |
| 1692 | if (boost::starts_with(iface_item, rootInterfaceName + "_")) |
| 1693 | { |
| 1694 | iface_array.push_back( |
| 1695 | {{"@odata.id", |
| 1696 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + |
| 1697 | rootInterfaceName + "/VLANs/" + iface_item}}); |
| 1698 | } |
| 1699 | } |
| 1700 | |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1701 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 1702 | iface_array.size(); |
| 1703 | asyncResp->res.jsonValue["Members"] = std::move(iface_array); |
| 1704 | asyncResp->res.jsonValue["@odata.id"] = |
| 1705 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + |
| 1706 | rootInterfaceName + "/VLANs"; |
| 1707 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1708 | } |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 1709 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1710 | void doPost(crow::Response &res, const crow::Request &req, |
| 1711 | const std::vector<std::string> ¶ms) override |
| 1712 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1713 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1714 | if (params.size() != 1) |
| 1715 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1716 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1717 | return; |
| 1718 | } |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1719 | bool vlanEnable = false; |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1720 | uint32_t vlanId = 0; |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1721 | if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable", |
| 1722 | vlanEnable)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1723 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1724 | return; |
| 1725 | } |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1726 | // Need both vlanId and vlanEnable to service this request |
| 1727 | if (!vlanId) |
| 1728 | { |
| 1729 | messages::propertyMissing(asyncResp->res, "VLANId"); |
| 1730 | } |
| 1731 | if (!vlanEnable) |
| 1732 | { |
| 1733 | messages::propertyMissing(asyncResp->res, "VLANEnable"); |
| 1734 | } |
| 1735 | if (static_cast<bool>(vlanId) ^ static_cast<bool>(vlanEnable)) |
| 1736 | { |
| 1737 | return; |
| 1738 | } |
| 1739 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1740 | const std::string &rootInterfaceName = params[0]; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1741 | auto callback = [asyncResp](const boost::system::error_code ec) { |
| 1742 | if (ec) |
| 1743 | { |
| 1744 | // TODO(ed) make more consistent error messages based on |
| 1745 | // phosphor-network responses |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1746 | messages::internalError(asyncResp->res); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1747 | return; |
| 1748 | } |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1749 | messages::created(asyncResp->res); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1750 | }; |
| 1751 | crow::connections::systemBus->async_method_call( |
| 1752 | std::move(callback), "xyz.openbmc_project.Network", |
| 1753 | "/xyz/openbmc_project/network", |
| 1754 | "xyz.openbmc_project.Network.VLAN.Create", "VLAN", |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1755 | rootInterfaceName, vlanId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1756 | } |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 1757 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1758 | } // namespace redfish |