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