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> |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 24 | #include <regex> |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 25 | #include <utils/json_utils.hpp> |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 26 | #include <variant> |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 27 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 28 | namespace redfish |
| 29 | { |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * DBus types primitives for several generic DBus interfaces |
| 33 | * TODO(Pawel) consider move this to separate file into boost::dbus |
| 34 | */ |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 35 | using PropertiesMapType = boost::container::flat_map< |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 36 | std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t, |
| 37 | int32_t, uint32_t, int64_t, uint64_t, double>>; |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 38 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 39 | using GetManagedObjects = std::vector<std::pair< |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 40 | sdbusplus::message::object_path, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 41 | std::vector<std::pair< |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 42 | std::string, |
| 43 | boost::container::flat_map< |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 44 | std::string, sdbusplus::message::variant< |
| 45 | std::string, bool, uint8_t, int16_t, uint16_t, |
| 46 | int32_t, uint32_t, int64_t, uint64_t, double, |
| 47 | std::vector<std::string>>>>>>>; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 48 | |
| 49 | enum class LinkType |
| 50 | { |
| 51 | Local, |
| 52 | Global |
| 53 | }; |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Structure for keeping IPv4 data required by Redfish |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 57 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 58 | struct IPv4AddressData |
| 59 | { |
| 60 | std::string id; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 61 | std::string address; |
| 62 | std::string domain; |
| 63 | std::string gateway; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | std::string netmask; |
| 65 | std::string origin; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 66 | LinkType linktype; |
| 67 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 68 | bool operator<(const IPv4AddressData &obj) const |
| 69 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 70 | return id < obj.id; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 71 | } |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /** |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 75 | * Structure for keeping IPv6 data required by Redfish |
| 76 | */ |
| 77 | struct IPv6AddressData |
| 78 | { |
| 79 | std::string id; |
| 80 | std::string address; |
| 81 | std::string origin; |
| 82 | uint8_t prefixLength; |
| 83 | |
| 84 | bool operator<(const IPv6AddressData &obj) const |
| 85 | { |
| 86 | return id < obj.id; |
| 87 | } |
| 88 | }; |
| 89 | /** |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 90 | * Structure for keeping basic single Ethernet Interface information |
| 91 | * available from DBus |
| 92 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 93 | struct EthernetInterfaceData |
| 94 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 95 | uint32_t speed; |
| 96 | bool auto_neg; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 97 | bool DNSEnabled; |
| 98 | bool NTPEnabled; |
| 99 | bool HostNameEnabled; |
| 100 | bool SendHostNameEnabled; |
Johnathan Mantey | aa05fb2 | 2020-01-08 12:08:44 -0800 | [diff] [blame] | 101 | bool linkUp; |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 102 | bool nicEnabled; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 103 | std::string DHCPEnabled; |
| 104 | std::string operatingMode; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 105 | std::string hostname; |
| 106 | std::string default_gateway; |
Ravi Teja | 9a6fc6f | 2019-04-16 02:43:13 -0500 | [diff] [blame] | 107 | std::string ipv6_default_gateway; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 108 | std::string mac_address; |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 109 | std::vector<std::uint32_t> vlan_id; |
manojkiran.eda@gmail.com | 0f6efdc | 2019-10-03 04:53:44 -0500 | [diff] [blame] | 110 | std::vector<std::string> nameServers; |
| 111 | std::vector<std::string> staticNameServers; |
Jennifer Lee | d24bfc7 | 2019-03-05 13:03:37 -0800 | [diff] [blame] | 112 | std::vector<std::string> domainnames; |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 115 | struct DHCPParameters |
| 116 | { |
| 117 | std::optional<bool> dhcpv4Enabled; |
| 118 | std::optional<bool> useDNSServers; |
| 119 | std::optional<bool> useNTPServers; |
| 120 | std::optional<bool> useUseDomainName; |
| 121 | std::optional<std::string> dhcpv6OperatingMode; |
| 122 | }; |
| 123 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 124 | // Helper function that changes bits netmask notation (i.e. /24) |
| 125 | // into full dot notation |
| 126 | inline std::string getNetmask(unsigned int bits) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 127 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 128 | uint32_t value = 0xffffffff << (32 - bits); |
| 129 | std::string netmask = std::to_string((value >> 24) & 0xff) + "." + |
| 130 | std::to_string((value >> 16) & 0xff) + "." + |
| 131 | std::to_string((value >> 8) & 0xff) + "." + |
| 132 | std::to_string(value & 0xff); |
| 133 | return netmask; |
| 134 | } |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 135 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 136 | inline bool translateDHCPEnabledToBool(const std::string &inputDHCP, |
| 137 | bool isIPv4) |
| 138 | { |
| 139 | if (isIPv4) |
| 140 | { |
| 141 | return ( |
| 142 | (inputDHCP == |
| 143 | "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") || |
| 144 | (inputDHCP == |
| 145 | "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); |
| 146 | } |
| 147 | return ((inputDHCP == |
| 148 | "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") || |
| 149 | (inputDHCP == |
| 150 | "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); |
| 151 | } |
| 152 | |
| 153 | inline std::string GetDHCPEnabledEnumeration(bool isIPv4, bool isIPv6) |
| 154 | { |
| 155 | if (isIPv4 && isIPv6) |
| 156 | { |
| 157 | return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"; |
| 158 | } |
| 159 | else if (isIPv4) |
| 160 | { |
| 161 | return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4"; |
| 162 | } |
| 163 | else if (isIPv6) |
| 164 | { |
| 165 | return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6"; |
| 166 | } |
| 167 | return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none"; |
| 168 | } |
| 169 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 170 | inline std::string |
| 171 | translateAddressOriginDbusToRedfish(const std::string &inputOrigin, |
| 172 | bool isIPv4) |
| 173 | { |
| 174 | if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 175 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 176 | return "Static"; |
| 177 | } |
| 178 | if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal") |
| 179 | { |
| 180 | if (isIPv4) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 181 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 182 | return "IPv4LinkLocal"; |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | return "LinkLocal"; |
| 187 | } |
| 188 | } |
| 189 | if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP") |
| 190 | { |
| 191 | if (isIPv4) |
| 192 | { |
| 193 | return "DHCP"; |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | return "DHCPv6"; |
| 198 | } |
| 199 | } |
| 200 | if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC") |
| 201 | { |
| 202 | return "SLAAC"; |
| 203 | } |
| 204 | return ""; |
| 205 | } |
| 206 | |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 207 | inline bool extractEthernetInterfaceData(const std::string ðiface_id, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 208 | const GetManagedObjects &dbus_data, |
| 209 | EthernetInterfaceData ðData) |
| 210 | { |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 211 | bool idFound = false; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 212 | for (const auto &objpath : dbus_data) |
| 213 | { |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 214 | for (const auto &ifacePair : objpath.second) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 215 | { |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 216 | if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 217 | { |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 218 | idFound = true; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 219 | if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 220 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 221 | for (const auto &propertyPair : ifacePair.second) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 222 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 223 | if (propertyPair.first == "MACAddress") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 224 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 225 | const std::string *mac = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 226 | std::get_if<std::string>(&propertyPair.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 227 | if (mac != nullptr) |
| 228 | { |
| 229 | ethData.mac_address = *mac; |
| 230 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 231 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN") |
| 235 | { |
| 236 | for (const auto &propertyPair : ifacePair.second) |
| 237 | { |
| 238 | if (propertyPair.first == "Id") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 239 | { |
Ed Tanous | 1b6b96c | 2018-11-30 11:35:41 -0800 | [diff] [blame] | 240 | const uint32_t *id = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 241 | std::get_if<uint32_t>(&propertyPair.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 242 | if (id != nullptr) |
| 243 | { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 244 | ethData.vlan_id.push_back(*id); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 245 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 246 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | else if (ifacePair.first == |
| 250 | "xyz.openbmc_project.Network.EthernetInterface") |
| 251 | { |
| 252 | for (const auto &propertyPair : ifacePair.second) |
| 253 | { |
| 254 | if (propertyPair.first == "AutoNeg") |
| 255 | { |
| 256 | const bool *auto_neg = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 257 | std::get_if<bool>(&propertyPair.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 258 | if (auto_neg != nullptr) |
| 259 | { |
| 260 | ethData.auto_neg = *auto_neg; |
| 261 | } |
| 262 | } |
| 263 | else if (propertyPair.first == "Speed") |
| 264 | { |
| 265 | const uint32_t *speed = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 266 | std::get_if<uint32_t>(&propertyPair.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 267 | if (speed != nullptr) |
| 268 | { |
| 269 | ethData.speed = *speed; |
| 270 | } |
| 271 | } |
Johnathan Mantey | aa05fb2 | 2020-01-08 12:08:44 -0800 | [diff] [blame] | 272 | else if (propertyPair.first == "LinkUp") |
| 273 | { |
| 274 | const bool *linkUp = |
| 275 | std::get_if<bool>(&propertyPair.second); |
| 276 | if (linkUp != nullptr) |
| 277 | { |
| 278 | ethData.linkUp = *linkUp; |
| 279 | } |
| 280 | } |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 281 | else if (propertyPair.first == "NICEnabled") |
| 282 | { |
| 283 | const bool *nicEnabled = |
| 284 | std::get_if<bool>(&propertyPair.second); |
| 285 | if (nicEnabled != nullptr) |
| 286 | { |
| 287 | ethData.nicEnabled = *nicEnabled; |
| 288 | } |
| 289 | } |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 290 | else if (propertyPair.first == "Nameservers") |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 291 | { |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 292 | const std::vector<std::string> *nameservers = |
| 293 | sdbusplus::message::variant_ns::get_if< |
| 294 | std::vector<std::string>>( |
| 295 | &propertyPair.second); |
| 296 | if (nameservers != nullptr) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 297 | { |
manojkiran.eda@gmail.com | 0f6efdc | 2019-10-03 04:53:44 -0500 | [diff] [blame] | 298 | ethData.nameServers = std::move(*nameservers); |
| 299 | } |
| 300 | } |
| 301 | else if (propertyPair.first == "StaticNameServers") |
| 302 | { |
| 303 | const std::vector<std::string> *staticNameServers = |
| 304 | sdbusplus::message::variant_ns::get_if< |
| 305 | std::vector<std::string>>( |
| 306 | &propertyPair.second); |
| 307 | if (staticNameServers != nullptr) |
| 308 | { |
| 309 | ethData.staticNameServers = |
| 310 | std::move(*staticNameServers); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 313 | else if (propertyPair.first == "DHCPEnabled") |
| 314 | { |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 315 | const std::string *DHCPEnabled = |
| 316 | std::get_if<std::string>(&propertyPair.second); |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 317 | if (DHCPEnabled != nullptr) |
| 318 | { |
| 319 | ethData.DHCPEnabled = *DHCPEnabled; |
| 320 | } |
| 321 | } |
Jennifer Lee | d24bfc7 | 2019-03-05 13:03:37 -0800 | [diff] [blame] | 322 | else if (propertyPair.first == "DomainName") |
| 323 | { |
| 324 | const std::vector<std::string> *domainNames = |
| 325 | sdbusplus::message::variant_ns::get_if< |
| 326 | std::vector<std::string>>( |
| 327 | &propertyPair.second); |
| 328 | if (domainNames != nullptr) |
| 329 | { |
| 330 | ethData.domainnames = std::move(*domainNames); |
| 331 | } |
| 332 | } |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | } |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 336 | |
| 337 | if (objpath.first == "/xyz/openbmc_project/network/config/dhcp") |
| 338 | { |
| 339 | if (ifacePair.first == |
| 340 | "xyz.openbmc_project.Network.DHCPConfiguration") |
| 341 | { |
| 342 | for (const auto &propertyPair : ifacePair.second) |
| 343 | { |
| 344 | if (propertyPair.first == "DNSEnabled") |
| 345 | { |
| 346 | const bool *DNSEnabled = |
| 347 | std::get_if<bool>(&propertyPair.second); |
| 348 | if (DNSEnabled != nullptr) |
| 349 | { |
| 350 | ethData.DNSEnabled = *DNSEnabled; |
| 351 | } |
| 352 | } |
| 353 | else if (propertyPair.first == "NTPEnabled") |
| 354 | { |
| 355 | const bool *NTPEnabled = |
| 356 | std::get_if<bool>(&propertyPair.second); |
| 357 | if (NTPEnabled != nullptr) |
| 358 | { |
| 359 | ethData.NTPEnabled = *NTPEnabled; |
| 360 | } |
| 361 | } |
| 362 | else if (propertyPair.first == "HostNameEnabled") |
| 363 | { |
| 364 | const bool *HostNameEnabled = |
| 365 | std::get_if<bool>(&propertyPair.second); |
| 366 | if (HostNameEnabled != nullptr) |
| 367 | { |
| 368 | ethData.HostNameEnabled = *HostNameEnabled; |
| 369 | } |
| 370 | } |
| 371 | else if (propertyPair.first == "SendHostNameEnabled") |
| 372 | { |
| 373 | const bool *SendHostNameEnabled = |
| 374 | std::get_if<bool>(&propertyPair.second); |
| 375 | if (SendHostNameEnabled != nullptr) |
| 376 | { |
| 377 | ethData.SendHostNameEnabled = |
| 378 | *SendHostNameEnabled; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | } |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 384 | // System configuration shows up in the global namespace, so no need |
| 385 | // to check eth number |
| 386 | if (ifacePair.first == |
| 387 | "xyz.openbmc_project.Network.SystemConfiguration") |
| 388 | { |
| 389 | for (const auto &propertyPair : ifacePair.second) |
| 390 | { |
| 391 | if (propertyPair.first == "HostName") |
| 392 | { |
| 393 | const std::string *hostname = |
| 394 | sdbusplus::message::variant_ns::get_if<std::string>( |
| 395 | &propertyPair.second); |
| 396 | if (hostname != nullptr) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 397 | { |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 398 | ethData.hostname = *hostname; |
| 399 | } |
| 400 | } |
| 401 | else if (propertyPair.first == "DefaultGateway") |
| 402 | { |
| 403 | const std::string *defaultGateway = |
| 404 | sdbusplus::message::variant_ns::get_if<std::string>( |
| 405 | &propertyPair.second); |
| 406 | if (defaultGateway != nullptr) |
| 407 | { |
| 408 | ethData.default_gateway = *defaultGateway; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 409 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 410 | } |
Ravi Teja | 9a6fc6f | 2019-04-16 02:43:13 -0500 | [diff] [blame] | 411 | else if (propertyPair.first == "DefaultGateway6") |
| 412 | { |
| 413 | const std::string *defaultGateway6 = |
| 414 | sdbusplus::message::variant_ns::get_if<std::string>( |
| 415 | &propertyPair.second); |
| 416 | if (defaultGateway6 != nullptr) |
| 417 | { |
| 418 | ethData.ipv6_default_gateway = *defaultGateway6; |
| 419 | } |
| 420 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 424 | } |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 425 | return idFound; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 428 | // Helper function that extracts data for single ethernet ipv6 address |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 429 | inline void |
| 430 | extractIPV6Data(const std::string ðiface_id, |
| 431 | const GetManagedObjects &dbus_data, |
| 432 | boost::container::flat_set<IPv6AddressData> &ipv6_config) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 433 | { |
| 434 | const std::string ipv6PathStart = |
| 435 | "/xyz/openbmc_project/network/" + ethiface_id + "/ipv6/"; |
| 436 | |
| 437 | // Since there might be several IPv6 configurations aligned with |
| 438 | // single ethernet interface, loop over all of them |
| 439 | for (const auto &objpath : dbus_data) |
| 440 | { |
| 441 | // Check if proper pattern for object path appears |
| 442 | if (boost::starts_with(objpath.first.str, ipv6PathStart)) |
| 443 | { |
| 444 | for (auto &interface : objpath.second) |
| 445 | { |
| 446 | if (interface.first == "xyz.openbmc_project.Network.IP") |
| 447 | { |
| 448 | // Instance IPv6AddressData structure, and set as |
| 449 | // appropriate |
| 450 | std::pair< |
| 451 | boost::container::flat_set<IPv6AddressData>::iterator, |
| 452 | bool> |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 453 | it = ipv6_config.insert(IPv6AddressData{}); |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 454 | IPv6AddressData &ipv6_address = *it.first; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 455 | ipv6_address.id = |
| 456 | objpath.first.str.substr(ipv6PathStart.size()); |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 457 | for (auto &property : interface.second) |
| 458 | { |
| 459 | if (property.first == "Address") |
| 460 | { |
| 461 | const std::string *address = |
| 462 | std::get_if<std::string>(&property.second); |
| 463 | if (address != nullptr) |
| 464 | { |
| 465 | ipv6_address.address = *address; |
| 466 | } |
| 467 | } |
| 468 | else if (property.first == "Origin") |
| 469 | { |
| 470 | const std::string *origin = |
| 471 | std::get_if<std::string>(&property.second); |
| 472 | if (origin != nullptr) |
| 473 | { |
| 474 | ipv6_address.origin = |
| 475 | translateAddressOriginDbusToRedfish(*origin, |
| 476 | false); |
| 477 | } |
| 478 | } |
| 479 | else if (property.first == "PrefixLength") |
| 480 | { |
| 481 | const uint8_t *prefix = |
| 482 | std::get_if<uint8_t>(&property.second); |
| 483 | if (prefix != nullptr) |
| 484 | { |
| 485 | ipv6_address.prefixLength = *prefix; |
| 486 | } |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | BMCWEB_LOG_ERROR |
| 491 | << "Got extra property: " << property.first |
| 492 | << " on the " << objpath.first.str << " object"; |
| 493 | } |
| 494 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 501 | // Helper function that extracts data for single ethernet ipv4 address |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 502 | inline void |
| 503 | extractIPData(const std::string ðiface_id, |
| 504 | const GetManagedObjects &dbus_data, |
| 505 | boost::container::flat_set<IPv4AddressData> &ipv4_config) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 506 | { |
| 507 | const std::string ipv4PathStart = |
| 508 | "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/"; |
| 509 | |
| 510 | // Since there might be several IPv4 configurations aligned with |
| 511 | // single ethernet interface, loop over all of them |
| 512 | for (const auto &objpath : dbus_data) |
| 513 | { |
| 514 | // Check if proper pattern for object path appears |
| 515 | if (boost::starts_with(objpath.first.str, ipv4PathStart)) |
| 516 | { |
| 517 | for (auto &interface : objpath.second) |
| 518 | { |
| 519 | if (interface.first == "xyz.openbmc_project.Network.IP") |
| 520 | { |
| 521 | // Instance IPv4AddressData structure, and set as |
| 522 | // appropriate |
| 523 | std::pair< |
| 524 | boost::container::flat_set<IPv4AddressData>::iterator, |
| 525 | bool> |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 526 | it = ipv4_config.insert(IPv4AddressData{}); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 527 | IPv4AddressData &ipv4_address = *it.first; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 528 | ipv4_address.id = |
| 529 | objpath.first.str.substr(ipv4PathStart.size()); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 530 | for (auto &property : interface.second) |
| 531 | { |
| 532 | if (property.first == "Address") |
| 533 | { |
| 534 | const std::string *address = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 535 | std::get_if<std::string>(&property.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 536 | if (address != nullptr) |
| 537 | { |
| 538 | ipv4_address.address = *address; |
| 539 | } |
| 540 | } |
| 541 | else if (property.first == "Gateway") |
| 542 | { |
| 543 | const std::string *gateway = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 544 | std::get_if<std::string>(&property.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 545 | if (gateway != nullptr) |
| 546 | { |
| 547 | ipv4_address.gateway = *gateway; |
| 548 | } |
| 549 | } |
| 550 | else if (property.first == "Origin") |
| 551 | { |
| 552 | const std::string *origin = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 553 | std::get_if<std::string>(&property.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 554 | if (origin != nullptr) |
| 555 | { |
| 556 | ipv4_address.origin = |
| 557 | translateAddressOriginDbusToRedfish(*origin, |
| 558 | true); |
| 559 | } |
| 560 | } |
| 561 | else if (property.first == "PrefixLength") |
| 562 | { |
| 563 | const uint8_t *mask = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 564 | std::get_if<uint8_t>(&property.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 565 | if (mask != nullptr) |
| 566 | { |
| 567 | // convert it to the string |
| 568 | ipv4_address.netmask = getNetmask(*mask); |
| 569 | } |
| 570 | } |
| 571 | else |
| 572 | { |
| 573 | BMCWEB_LOG_ERROR |
| 574 | << "Got extra property: " << property.first |
| 575 | << " on the " << objpath.first.str << " object"; |
| 576 | } |
| 577 | } |
| 578 | // Check if given address is local, or global |
| 579 | ipv4_address.linktype = |
| 580 | boost::starts_with(ipv4_address.address, "169.254.") |
Johnathan Mantey | 18659d1 | 2019-06-07 10:26:29 -0700 | [diff] [blame] | 581 | ? LinkType::Local |
| 582 | : LinkType::Global; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * @brief Sets given Id on the given VLAN interface through D-Bus |
| 591 | * |
| 592 | * @param[in] ifaceId Id of VLAN interface that should be modified |
| 593 | * @param[in] inputVlanId New ID of the VLAN |
| 594 | * @param[in] callback Function that will be called after the operation |
| 595 | * |
| 596 | * @return None. |
| 597 | */ |
| 598 | template <typename CallbackFunc> |
| 599 | void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId, |
| 600 | CallbackFunc &&callback) |
| 601 | { |
| 602 | crow::connections::systemBus->async_method_call( |
| 603 | callback, "xyz.openbmc_project.Network", |
| 604 | std::string("/xyz/openbmc_project/network/") + ifaceId, |
| 605 | "org.freedesktop.DBus.Properties", "Set", |
| 606 | "xyz.openbmc_project.Network.VLAN", "Id", |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 607 | std::variant<uint32_t>(inputVlanId)); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /** |
| 611 | * @brief Helper function that verifies IP address to check if it is in |
| 612 | * proper format. If bits pointer is provided, also calculates active |
| 613 | * bit count for Subnet Mask. |
| 614 | * |
| 615 | * @param[in] ip IP that will be verified |
| 616 | * @param[out] bits Calculated mask in bits notation |
| 617 | * |
| 618 | * @return true in case of success, false otherwise |
| 619 | */ |
| 620 | inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip, |
| 621 | uint8_t *bits = nullptr) |
| 622 | { |
| 623 | std::vector<std::string> bytesInMask; |
| 624 | |
| 625 | boost::split(bytesInMask, ip, boost::is_any_of(".")); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 626 | |
| 627 | static const constexpr int ipV4AddressSectionsCount = 4; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 628 | if (bytesInMask.size() != ipV4AddressSectionsCount) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 629 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 630 | return false; |
| 631 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 632 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 633 | if (bits != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 634 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 635 | *bits = 0; |
| 636 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 637 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 638 | char *endPtr; |
| 639 | long previousValue = 255; |
| 640 | bool firstZeroInByteHit; |
| 641 | for (const std::string &byte : bytesInMask) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 642 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 643 | if (byte.empty()) |
| 644 | { |
| 645 | return false; |
| 646 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 647 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 648 | // Use strtol instead of stroi to avoid exceptions |
| 649 | long value = std::strtol(byte.c_str(), &endPtr, 10); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 650 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 651 | // endPtr should point to the end of the string, otherwise given string |
| 652 | // is not 100% number |
| 653 | if (*endPtr != '\0') |
| 654 | { |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | // Value should be contained in byte |
| 659 | if (value < 0 || value > 255) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 660 | { |
| 661 | return false; |
| 662 | } |
| 663 | |
| 664 | if (bits != nullptr) |
| 665 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 666 | // Mask has to be continuous between bytes |
| 667 | if (previousValue != 255 && value != 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 668 | { |
| 669 | return false; |
| 670 | } |
| 671 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 672 | // Mask has to be continuous inside bytes |
| 673 | firstZeroInByteHit = false; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 674 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 675 | // Count bits |
| 676 | for (int bitIdx = 7; bitIdx >= 0; bitIdx--) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 677 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 678 | if (value & (1 << bitIdx)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 679 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 680 | if (firstZeroInByteHit) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 681 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 682 | // Continuity not preserved |
| 683 | return false; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 684 | } |
| 685 | else |
| 686 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 687 | (*bits)++; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 690 | else |
| 691 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 692 | firstZeroInByteHit = true; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 693 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
| 697 | previousValue = value; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 700 | return true; |
| 701 | } |
| 702 | |
| 703 | /** |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 704 | * @brief Deletes given IPv4 interface |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 705 | * |
| 706 | * @param[in] ifaceId Id of interface whose IP should be deleted |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 707 | * @param[in] ipHash DBus Hash id of IP that should be deleted |
| 708 | * @param[io] asyncResp Response object that will be returned to client |
| 709 | * |
| 710 | * @return None |
| 711 | */ |
| 712 | inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 713 | const std::shared_ptr<AsyncResp> asyncResp) |
| 714 | { |
| 715 | crow::connections::systemBus->async_method_call( |
Johnathan Mantey | 286b911 | 2019-06-10 13:38:04 -0700 | [diff] [blame] | 716 | [asyncResp](const boost::system::error_code ec) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 717 | if (ec) |
| 718 | { |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 719 | messages::internalError(asyncResp->res); |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 720 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 721 | }, |
| 722 | "xyz.openbmc_project.Network", |
| 723 | "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, |
| 724 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 725 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 726 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 727 | /** |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 728 | * @brief Creates a static IPv4 entry |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 729 | * |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 730 | * @param[in] ifaceId Id of interface upon which to create the IPv4 entry |
| 731 | * @param[in] prefixLength IPv4 prefix syntax for the subnet mask |
| 732 | * @param[in] gateway IPv4 address of this interfaces gateway |
| 733 | * @param[in] address IPv4 address to assign to this interface |
| 734 | * @param[io] asyncResp Response object that will be returned to client |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 735 | * |
| 736 | * @return None |
| 737 | */ |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 738 | inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 739 | uint8_t prefixLength, const std::string &gateway, |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 740 | const std::string &address, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 741 | std::shared_ptr<AsyncResp> asyncResp) |
| 742 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 743 | crow::connections::systemBus->async_method_call( |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 744 | [asyncResp](const boost::system::error_code ec) { |
| 745 | if (ec) |
| 746 | { |
| 747 | messages::internalError(asyncResp->res); |
| 748 | } |
| 749 | }, |
| 750 | "xyz.openbmc_project.Network", |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 751 | "/xyz/openbmc_project/network/" + ifaceId, |
| 752 | "xyz.openbmc_project.Network.IP.Create", "IP", |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 753 | "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 754 | gateway); |
| 755 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 756 | |
| 757 | /** |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 758 | * @brief Deletes the IPv4 entry for this interface and creates a replacement |
| 759 | * static IPv4 entry |
| 760 | * |
| 761 | * @param[in] ifaceId Id of interface upon which to create the IPv4 entry |
| 762 | * @param[in] id The unique hash entry identifying the DBus entry |
| 763 | * @param[in] prefixLength IPv4 prefix syntax for the subnet mask |
| 764 | * @param[in] gateway IPv4 address of this interfaces gateway |
| 765 | * @param[in] address IPv4 address to assign to this interface |
| 766 | * @param[io] asyncResp Response object that will be returned to client |
| 767 | * |
| 768 | * @return None |
| 769 | */ |
| 770 | inline void deleteAndCreateIPv4(const std::string &ifaceId, |
| 771 | const std::string &id, uint8_t prefixLength, |
| 772 | const std::string &gateway, |
| 773 | const std::string &address, |
| 774 | std::shared_ptr<AsyncResp> asyncResp) |
| 775 | { |
| 776 | crow::connections::systemBus->async_method_call( |
| 777 | [asyncResp, ifaceId, address, prefixLength, |
| 778 | gateway](const boost::system::error_code ec) { |
| 779 | if (ec) |
| 780 | { |
| 781 | messages::internalError(asyncResp->res); |
| 782 | } |
| 783 | crow::connections::systemBus->async_method_call( |
| 784 | [asyncResp](const boost::system::error_code ec) { |
| 785 | if (ec) |
| 786 | { |
| 787 | messages::internalError(asyncResp->res); |
| 788 | } |
| 789 | }, |
| 790 | "xyz.openbmc_project.Network", |
| 791 | "/xyz/openbmc_project/network/" + ifaceId, |
| 792 | "xyz.openbmc_project.Network.IP.Create", "IP", |
| 793 | "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, |
| 794 | prefixLength, gateway); |
| 795 | }, |
| 796 | "xyz.openbmc_project.Network", |
| 797 | +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id, |
| 798 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 799 | } |
| 800 | |
| 801 | /** |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 802 | * @brief Deletes given IPv6 |
| 803 | * |
| 804 | * @param[in] ifaceId Id of interface whose IP should be deleted |
| 805 | * @param[in] ipHash DBus Hash id of IP that should be deleted |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 806 | * @param[io] asyncResp Response object that will be returned to client |
| 807 | * |
| 808 | * @return None |
| 809 | */ |
| 810 | inline void deleteIPv6(const std::string &ifaceId, const std::string &ipHash, |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 811 | const std::shared_ptr<AsyncResp> asyncResp) |
| 812 | { |
| 813 | crow::connections::systemBus->async_method_call( |
Johnathan Mantey | 286b911 | 2019-06-10 13:38:04 -0700 | [diff] [blame] | 814 | [asyncResp](const boost::system::error_code ec) { |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 815 | if (ec) |
| 816 | { |
| 817 | messages::internalError(asyncResp->res); |
| 818 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 819 | }, |
| 820 | "xyz.openbmc_project.Network", |
| 821 | "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash, |
| 822 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 823 | } |
| 824 | |
| 825 | /** |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 826 | * @brief Deletes the IPv6 entry for this interface and creates a replacement |
| 827 | * static IPv6 entry |
| 828 | * |
| 829 | * @param[in] ifaceId Id of interface upon which to create the IPv6 entry |
| 830 | * @param[in] id The unique hash entry identifying the DBus entry |
| 831 | * @param[in] prefixLength IPv6 prefix syntax for the subnet mask |
| 832 | * @param[in] address IPv6 address to assign to this interface |
| 833 | * @param[io] asyncResp Response object that will be returned to client |
| 834 | * |
| 835 | * @return None |
| 836 | */ |
| 837 | inline void deleteAndCreateIPv6(const std::string &ifaceId, |
| 838 | const std::string &id, uint8_t prefixLength, |
| 839 | const std::string &address, |
| 840 | std::shared_ptr<AsyncResp> asyncResp) |
| 841 | { |
| 842 | crow::connections::systemBus->async_method_call( |
| 843 | [asyncResp, ifaceId, address, |
| 844 | prefixLength](const boost::system::error_code ec) { |
| 845 | if (ec) |
| 846 | { |
| 847 | messages::internalError(asyncResp->res); |
| 848 | } |
| 849 | crow::connections::systemBus->async_method_call( |
| 850 | [asyncResp](const boost::system::error_code ec) { |
| 851 | if (ec) |
| 852 | { |
| 853 | messages::internalError(asyncResp->res); |
| 854 | } |
| 855 | }, |
| 856 | "xyz.openbmc_project.Network", |
| 857 | "/xyz/openbmc_project/network/" + ifaceId, |
| 858 | "xyz.openbmc_project.Network.IP.Create", "IP", |
| 859 | "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, |
| 860 | prefixLength, ""); |
| 861 | }, |
| 862 | "xyz.openbmc_project.Network", |
| 863 | +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id, |
| 864 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 865 | } |
| 866 | |
| 867 | /** |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 868 | * @brief Creates IPv6 with given data |
| 869 | * |
| 870 | * @param[in] ifaceId Id of interface whose IP should be added |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 871 | * @param[in] prefixLength Prefix length that needs to be added |
| 872 | * @param[in] address IP address that needs to be added |
| 873 | * @param[io] asyncResp Response object that will be returned to client |
| 874 | * |
| 875 | * @return None |
| 876 | */ |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 877 | inline void createIPv6(const std::string &ifaceId, uint8_t prefixLength, |
| 878 | const std::string &address, |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 879 | std::shared_ptr<AsyncResp> asyncResp) |
| 880 | { |
| 881 | auto createIpHandler = [asyncResp](const boost::system::error_code ec) { |
| 882 | if (ec) |
| 883 | { |
| 884 | messages::internalError(asyncResp->res); |
| 885 | } |
| 886 | }; |
| 887 | // Passing null for gateway, as per redfish spec IPv6StaticAddresses object |
| 888 | // does not have assosiated gateway property |
| 889 | crow::connections::systemBus->async_method_call( |
| 890 | std::move(createIpHandler), "xyz.openbmc_project.Network", |
| 891 | "/xyz/openbmc_project/network/" + ifaceId, |
| 892 | "xyz.openbmc_project.Network.IP.Create", "IP", |
| 893 | "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength, |
| 894 | ""); |
| 895 | } |
| 896 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 897 | /** |
| 898 | * Function that retrieves all properties for given Ethernet Interface |
| 899 | * Object |
| 900 | * from EntityManager Network Manager |
| 901 | * @param ethiface_id a eth interface id to query on DBus |
| 902 | * @param callback a function that shall be called to convert Dbus output |
| 903 | * into JSON |
| 904 | */ |
| 905 | template <typename CallbackFunc> |
| 906 | void getEthernetIfaceData(const std::string ðiface_id, |
| 907 | CallbackFunc &&callback) |
| 908 | { |
| 909 | crow::connections::systemBus->async_method_call( |
| 910 | [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}]( |
| 911 | const boost::system::error_code error_code, |
| 912 | const GetManagedObjects &resp) { |
| 913 | EthernetInterfaceData ethData{}; |
| 914 | boost::container::flat_set<IPv4AddressData> ipv4Data; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 915 | boost::container::flat_set<IPv6AddressData> ipv6Data; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 916 | |
| 917 | if (error_code) |
| 918 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 919 | callback(false, ethData, ipv4Data, ipv6Data); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 920 | return; |
| 921 | } |
| 922 | |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 923 | bool found = |
| 924 | extractEthernetInterfaceData(ethiface_id, resp, ethData); |
| 925 | if (!found) |
| 926 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 927 | callback(false, ethData, ipv4Data, ipv6Data); |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 928 | return; |
| 929 | } |
| 930 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 931 | extractIPData(ethiface_id, resp, ipv4Data); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 932 | // Fix global GW |
| 933 | for (IPv4AddressData &ipv4 : ipv4Data) |
| 934 | { |
Ravi Teja | c619141 | 2019-07-30 00:53:50 -0500 | [diff] [blame] | 935 | if (((ipv4.linktype == LinkType::Global) && |
| 936 | (ipv4.gateway == "0.0.0.0")) || |
| 937 | (ipv4.origin == "DHCP")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 938 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 939 | ipv4.gateway = ethData.default_gateway; |
| 940 | } |
| 941 | } |
| 942 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 943 | extractIPV6Data(ethiface_id, resp, ipv6Data); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 944 | // Finally make a callback with usefull data |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 945 | callback(true, ethData, ipv4Data, ipv6Data); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 946 | }, |
| 947 | "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", |
| 948 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 949 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 950 | |
| 951 | /** |
| 952 | * Function that retrieves all Ethernet Interfaces available through Network |
| 953 | * Manager |
| 954 | * @param callback a function that shall be called to convert Dbus output |
| 955 | * into JSON. |
| 956 | */ |
| 957 | template <typename CallbackFunc> |
| 958 | void getEthernetIfaceList(CallbackFunc &&callback) |
| 959 | { |
| 960 | crow::connections::systemBus->async_method_call( |
| 961 | [callback{std::move(callback)}]( |
| 962 | const boost::system::error_code error_code, |
| 963 | GetManagedObjects &resp) { |
| 964 | // Callback requires vector<string> to retrieve all available |
| 965 | // ethernet interfaces |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 966 | boost::container::flat_set<std::string> iface_list; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 967 | iface_list.reserve(resp.size()); |
| 968 | if (error_code) |
| 969 | { |
| 970 | callback(false, iface_list); |
| 971 | return; |
| 972 | } |
| 973 | |
| 974 | // Iterate over all retrieved ObjectPaths. |
| 975 | for (const auto &objpath : resp) |
| 976 | { |
| 977 | // And all interfaces available for certain ObjectPath. |
| 978 | for (const auto &interface : objpath.second) |
| 979 | { |
| 980 | // If interface is |
| 981 | // xyz.openbmc_project.Network.EthernetInterface, this is |
| 982 | // what we're looking for. |
| 983 | if (interface.first == |
| 984 | "xyz.openbmc_project.Network.EthernetInterface") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 985 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 986 | // Cut out everyting until last "/", ... |
| 987 | const std::string &iface_id = objpath.first.str; |
| 988 | std::size_t last_pos = iface_id.rfind("/"); |
| 989 | if (last_pos != std::string::npos) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 990 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 991 | // and put it into output vector. |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 992 | iface_list.emplace(iface_id.substr(last_pos + 1)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 996 | } |
| 997 | // Finally make a callback with useful data |
| 998 | callback(true, iface_list); |
| 999 | }, |
| 1000 | "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", |
| 1001 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1002 | } |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 1003 | |
| 1004 | /** |
| 1005 | * EthernetCollection derived class for delivering Ethernet Collection Schema |
| 1006 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1007 | class EthernetCollection : public Node |
| 1008 | { |
| 1009 | public: |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1010 | template <typename CrowApp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1011 | EthernetCollection(CrowApp &app) : |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1012 | Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1013 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1014 | entityPrivileges = { |
| 1015 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 1016 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 1017 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 1018 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 1019 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 1020 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
| 1021 | } |
| 1022 | |
| 1023 | private: |
| 1024 | /** |
| 1025 | * Functions triggers appropriate requests on DBus |
| 1026 | */ |
| 1027 | void doGet(crow::Response &res, const crow::Request &req, |
| 1028 | const std::vector<std::string> ¶ms) override |
| 1029 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1030 | res.jsonValue["@odata.type"] = |
| 1031 | "#EthernetInterfaceCollection.EthernetInterfaceCollection"; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1032 | res.jsonValue["@odata.id"] = |
| 1033 | "/redfish/v1/Managers/bmc/EthernetInterfaces"; |
| 1034 | res.jsonValue["Name"] = "Ethernet Network Interface Collection"; |
| 1035 | res.jsonValue["Description"] = |
| 1036 | "Collection of EthernetInterfaces for this Manager"; |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 1037 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1038 | // Get eth interface list, and call the below callback for JSON |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1039 | // preparation |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1040 | getEthernetIfaceList( |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 1041 | [asyncResp]( |
| 1042 | const bool &success, |
| 1043 | const boost::container::flat_set<std::string> &iface_list) { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1044 | if (!success) |
| 1045 | { |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 1046 | messages::internalError(asyncResp->res); |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1047 | return; |
| 1048 | } |
| 1049 | |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 1050 | nlohmann::json &iface_array = |
| 1051 | asyncResp->res.jsonValue["Members"]; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1052 | iface_array = nlohmann::json::array(); |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1053 | std::string tag = "_"; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1054 | for (const std::string &iface_item : iface_list) |
| 1055 | { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1056 | std::size_t found = iface_item.find(tag); |
| 1057 | if (found == std::string::npos) |
| 1058 | { |
| 1059 | iface_array.push_back( |
| 1060 | {{"@odata.id", |
| 1061 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + |
| 1062 | iface_item}}); |
| 1063 | } |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 1066 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 1067 | iface_array.size(); |
| 1068 | asyncResp->res.jsonValue["@odata.id"] = |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1069 | "/redfish/v1/Managers/bmc/EthernetInterfaces"; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1070 | }); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1071 | } |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 1072 | }; |
| 1073 | |
| 1074 | /** |
| 1075 | * EthernetInterface derived class for delivering Ethernet Schema |
| 1076 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1077 | class EthernetInterface : public Node |
| 1078 | { |
| 1079 | public: |
| 1080 | /* |
| 1081 | * Default Constructor |
| 1082 | */ |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1083 | template <typename CrowApp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1084 | EthernetInterface(CrowApp &app) : |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1085 | Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/", |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1086 | std::string()) |
| 1087 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1088 | entityPrivileges = { |
| 1089 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 1090 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 1091 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 1092 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 1093 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 1094 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 1095 | } |
| 1096 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1097 | private: |
Ed Tanous | bc0bd6e | 2018-12-10 14:07:55 -0800 | [diff] [blame] | 1098 | void handleHostnamePatch(const std::string &hostname, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1099 | const std::shared_ptr<AsyncResp> asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1100 | { |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1101 | // SHOULD handle host names of up to 255 characters(RFC 1123) |
| 1102 | if (hostname.length() > 255) |
| 1103 | { |
| 1104 | messages::propertyValueFormatError(asyncResp->res, hostname, |
| 1105 | "HostName"); |
| 1106 | return; |
| 1107 | } |
Ed Tanous | bc0bd6e | 2018-12-10 14:07:55 -0800 | [diff] [blame] | 1108 | crow::connections::systemBus->async_method_call( |
| 1109 | [asyncResp](const boost::system::error_code ec) { |
| 1110 | if (ec) |
| 1111 | { |
| 1112 | messages::internalError(asyncResp->res); |
| 1113 | } |
| 1114 | }, |
| 1115 | "xyz.openbmc_project.Network", |
| 1116 | "/xyz/openbmc_project/network/config", |
| 1117 | "org.freedesktop.DBus.Properties", "Set", |
| 1118 | "xyz.openbmc_project.Network.SystemConfiguration", "HostName", |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 1119 | std::variant<std::string>(hostname)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1122 | void handleDomainnamePatch(const std::string &ifaceId, |
| 1123 | const std::string &domainname, |
| 1124 | const std::shared_ptr<AsyncResp> asyncResp) |
| 1125 | { |
| 1126 | std::vector<std::string> vectorDomainname = {domainname}; |
| 1127 | crow::connections::systemBus->async_method_call( |
| 1128 | [asyncResp](const boost::system::error_code ec) { |
| 1129 | if (ec) |
| 1130 | { |
| 1131 | messages::internalError(asyncResp->res); |
| 1132 | } |
| 1133 | }, |
| 1134 | "xyz.openbmc_project.Network", |
| 1135 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1136 | "org.freedesktop.DBus.Properties", "Set", |
| 1137 | "xyz.openbmc_project.Network.EthernetInterface", "DomainName", |
| 1138 | std::variant<std::vector<std::string>>(vectorDomainname)); |
| 1139 | } |
| 1140 | |
| 1141 | void handleFqdnPatch(const std::string &ifaceId, const std::string &fqdn, |
| 1142 | const std::shared_ptr<AsyncResp> asyncResp) |
| 1143 | { |
| 1144 | // Total length of FQDN must not exceed 255 characters(RFC 1035) |
| 1145 | if (fqdn.length() > 255) |
| 1146 | { |
| 1147 | messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); |
| 1148 | return; |
| 1149 | } |
| 1150 | |
| 1151 | size_t pos = fqdn.find('.'); |
| 1152 | if (pos == std::string::npos) |
| 1153 | { |
| 1154 | messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); |
| 1155 | return; |
| 1156 | } |
| 1157 | |
| 1158 | std::string hostname; |
| 1159 | std::string domainname; |
| 1160 | domainname = (fqdn).substr(pos + 1); |
| 1161 | hostname = (fqdn).substr(0, pos); |
| 1162 | |
| 1163 | if (!isHostnameValid(hostname) || !isDomainnameValid(domainname)) |
| 1164 | { |
| 1165 | messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); |
| 1166 | return; |
| 1167 | } |
| 1168 | |
| 1169 | handleHostnamePatch(hostname, asyncResp); |
| 1170 | handleDomainnamePatch(ifaceId, domainname, asyncResp); |
| 1171 | } |
| 1172 | |
| 1173 | bool isHostnameValid(const std::string &hostname) |
| 1174 | { |
| 1175 | // A valid host name can never have the dotted-decimal form (RFC 1123) |
| 1176 | if (std::all_of(hostname.begin(), hostname.end(), ::isdigit)) |
| 1177 | { |
| 1178 | return false; |
| 1179 | } |
| 1180 | // Each label(hostname/subdomains) within a valid FQDN |
| 1181 | // MUST handle host names of up to 63 characters (RFC 1123) |
| 1182 | // labels cannot start or end with hyphens (RFC 952) |
| 1183 | // labels can start with numbers (RFC 1123) |
| 1184 | const std::regex pattern( |
| 1185 | "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$"); |
| 1186 | |
| 1187 | return std::regex_match(hostname, pattern); |
| 1188 | } |
| 1189 | |
| 1190 | bool isDomainnameValid(const std::string &domainname) |
| 1191 | { |
| 1192 | // Can have multiple subdomains |
| 1193 | // Top Level Domain's min length is 2 character |
| 1194 | const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]" |
| 1195 | "{1,30}\\.)*[a-zA-Z]{2,}$"); |
| 1196 | |
| 1197 | return std::regex_match(domainname, pattern); |
| 1198 | } |
| 1199 | |
Ratan Gupta | d577665 | 2019-03-03 08:47:22 +0530 | [diff] [blame] | 1200 | void handleMACAddressPatch(const std::string &ifaceId, |
| 1201 | const std::string &macAddress, |
| 1202 | const std::shared_ptr<AsyncResp> &asyncResp) |
| 1203 | { |
| 1204 | crow::connections::systemBus->async_method_call( |
| 1205 | [asyncResp, macAddress](const boost::system::error_code ec) { |
| 1206 | if (ec) |
| 1207 | { |
| 1208 | messages::internalError(asyncResp->res); |
| 1209 | return; |
| 1210 | } |
Ratan Gupta | d577665 | 2019-03-03 08:47:22 +0530 | [diff] [blame] | 1211 | }, |
| 1212 | "xyz.openbmc_project.Network", |
| 1213 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1214 | "org.freedesktop.DBus.Properties", "Set", |
| 1215 | "xyz.openbmc_project.Network.MACAddress", "MACAddress", |
| 1216 | std::variant<std::string>(macAddress)); |
| 1217 | } |
Johnathan Mantey | 286b911 | 2019-06-10 13:38:04 -0700 | [diff] [blame] | 1218 | |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1219 | void setDHCPEnabled(const std::string &ifaceId, |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1220 | const std::string &propertyName, const bool v4Value, |
| 1221 | const bool v6Value, |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1222 | const std::shared_ptr<AsyncResp> asyncResp) |
| 1223 | { |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1224 | const std::string dhcp = GetDHCPEnabledEnumeration(v4Value, v6Value); |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1225 | crow::connections::systemBus->async_method_call( |
| 1226 | [asyncResp](const boost::system::error_code ec) { |
| 1227 | if (ec) |
| 1228 | { |
| 1229 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 1230 | messages::internalError(asyncResp->res); |
| 1231 | return; |
| 1232 | } |
| 1233 | }, |
| 1234 | "xyz.openbmc_project.Network", |
| 1235 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1236 | "org.freedesktop.DBus.Properties", "Set", |
| 1237 | "xyz.openbmc_project.Network.EthernetInterface", propertyName, |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1238 | std::variant<std::string>{dhcp}); |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1239 | } |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1240 | |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1241 | void setEthernetInterfaceBoolProperty( |
| 1242 | const std::string &ifaceId, const std::string &propertyName, |
| 1243 | const bool &value, const std::shared_ptr<AsyncResp> asyncResp) |
| 1244 | { |
| 1245 | crow::connections::systemBus->async_method_call( |
| 1246 | [asyncResp](const boost::system::error_code ec) { |
| 1247 | if (ec) |
| 1248 | { |
| 1249 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 1250 | messages::internalError(asyncResp->res); |
| 1251 | return; |
| 1252 | } |
| 1253 | }, |
| 1254 | "xyz.openbmc_project.Network", |
| 1255 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1256 | "org.freedesktop.DBus.Properties", "Set", |
| 1257 | "xyz.openbmc_project.Network.EthernetInterface", propertyName, |
| 1258 | std::variant<bool>{value}); |
| 1259 | } |
| 1260 | |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1261 | void setDHCPv4Config(const std::string &propertyName, const bool &value, |
| 1262 | const std::shared_ptr<AsyncResp> asyncResp) |
| 1263 | { |
| 1264 | BMCWEB_LOG_DEBUG << propertyName << " = " << value; |
| 1265 | crow::connections::systemBus->async_method_call( |
| 1266 | [asyncResp](const boost::system::error_code ec) { |
| 1267 | if (ec) |
| 1268 | { |
| 1269 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 1270 | messages::internalError(asyncResp->res); |
| 1271 | return; |
| 1272 | } |
| 1273 | }, |
| 1274 | "xyz.openbmc_project.Network", |
| 1275 | "/xyz/openbmc_project/network/config/dhcp", |
| 1276 | "org.freedesktop.DBus.Properties", "Set", |
| 1277 | "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, |
| 1278 | std::variant<bool>{value}); |
| 1279 | } |
Ratan Gupta | d577665 | 2019-03-03 08:47:22 +0530 | [diff] [blame] | 1280 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1281 | void handleDHCPPatch(const std::string &ifaceId, |
| 1282 | const EthernetInterfaceData ðData, |
| 1283 | DHCPParameters v4dhcpParms, DHCPParameters v6dhcpParms, |
| 1284 | const std::shared_ptr<AsyncResp> asyncResp) |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1285 | { |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1286 | bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true); |
| 1287 | bool ipv6Active = |
| 1288 | translateDHCPEnabledToBool(ethData.DHCPEnabled, false); |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1289 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1290 | bool nextv4DHCPState = |
| 1291 | v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active; |
| 1292 | |
| 1293 | bool nextv6DHCPState{}; |
| 1294 | if (v6dhcpParms.dhcpv6OperatingMode) |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1295 | { |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1296 | if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") && |
| 1297 | (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") && |
| 1298 | (*v6dhcpParms.dhcpv6OperatingMode != "Disabled")) |
| 1299 | { |
| 1300 | messages::propertyValueFormatError( |
| 1301 | asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode, |
| 1302 | "OperatingMode"); |
| 1303 | return; |
| 1304 | } |
| 1305 | nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful"); |
| 1306 | } |
| 1307 | else |
| 1308 | { |
| 1309 | nextv6DHCPState = ipv6Active; |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1312 | bool nextDNS{}; |
| 1313 | if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers) |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1314 | { |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1315 | if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers) |
| 1316 | { |
| 1317 | messages::generalError(asyncResp->res); |
| 1318 | return; |
| 1319 | } |
| 1320 | nextDNS = *v4dhcpParms.useDNSServers; |
| 1321 | } |
| 1322 | else if (v4dhcpParms.useDNSServers) |
| 1323 | { |
| 1324 | nextDNS = *v4dhcpParms.useDNSServers; |
| 1325 | } |
| 1326 | else if (v6dhcpParms.useDNSServers) |
| 1327 | { |
| 1328 | nextDNS = *v6dhcpParms.useDNSServers; |
| 1329 | } |
| 1330 | else |
| 1331 | { |
| 1332 | nextDNS = ethData.DNSEnabled; |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1335 | bool nextNTP{}; |
| 1336 | if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers) |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1337 | { |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1338 | if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers) |
| 1339 | { |
| 1340 | messages::generalError(asyncResp->res); |
| 1341 | return; |
| 1342 | } |
| 1343 | nextNTP = *v4dhcpParms.useNTPServers; |
| 1344 | } |
| 1345 | else if (v4dhcpParms.useNTPServers) |
| 1346 | { |
| 1347 | nextNTP = *v4dhcpParms.useNTPServers; |
| 1348 | } |
| 1349 | else if (v6dhcpParms.useNTPServers) |
| 1350 | { |
| 1351 | nextNTP = *v6dhcpParms.useNTPServers; |
| 1352 | } |
| 1353 | else |
| 1354 | { |
| 1355 | nextNTP = ethData.NTPEnabled; |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1358 | bool nextUseDomain{}; |
| 1359 | if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName) |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1360 | { |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1361 | if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName) |
| 1362 | { |
| 1363 | messages::generalError(asyncResp->res); |
| 1364 | return; |
| 1365 | } |
| 1366 | nextUseDomain = *v4dhcpParms.useUseDomainName; |
| 1367 | } |
| 1368 | else if (v4dhcpParms.useUseDomainName) |
| 1369 | { |
| 1370 | nextUseDomain = *v4dhcpParms.useUseDomainName; |
| 1371 | } |
| 1372 | else if (v6dhcpParms.useUseDomainName) |
| 1373 | { |
| 1374 | nextUseDomain = *v6dhcpParms.useUseDomainName; |
| 1375 | } |
| 1376 | else |
| 1377 | { |
| 1378 | nextUseDomain = ethData.HostNameEnabled; |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1381 | BMCWEB_LOG_DEBUG << "set DHCPEnabled..."; |
| 1382 | setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState, |
| 1383 | asyncResp); |
| 1384 | BMCWEB_LOG_DEBUG << "set DNSEnabled..."; |
| 1385 | setDHCPv4Config("DNSEnabled", nextDNS, asyncResp); |
| 1386 | BMCWEB_LOG_DEBUG << "set NTPEnabled..."; |
| 1387 | setDHCPv4Config("NTPEnabled", nextNTP, asyncResp); |
| 1388 | BMCWEB_LOG_DEBUG << "set HostNameEnabled..."; |
| 1389 | setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp); |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1390 | } |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1391 | |
| 1392 | boost::container::flat_set<IPv4AddressData>::const_iterator |
| 1393 | GetNextStaticIPEntry( |
| 1394 | boost::container::flat_set<IPv4AddressData>::const_iterator head, |
| 1395 | boost::container::flat_set<IPv4AddressData>::const_iterator end) |
| 1396 | { |
| 1397 | for (; head != end; head++) |
| 1398 | { |
| 1399 | if (head->origin == "Static") |
| 1400 | { |
| 1401 | return head; |
| 1402 | } |
| 1403 | } |
| 1404 | return end; |
| 1405 | } |
| 1406 | |
| 1407 | boost::container::flat_set<IPv6AddressData>::const_iterator |
| 1408 | GetNextStaticIPEntry( |
| 1409 | boost::container::flat_set<IPv6AddressData>::const_iterator head, |
| 1410 | boost::container::flat_set<IPv6AddressData>::const_iterator end) |
| 1411 | { |
| 1412 | for (; head != end; head++) |
| 1413 | { |
| 1414 | if (head->origin == "Static") |
| 1415 | { |
| 1416 | return head; |
| 1417 | } |
| 1418 | } |
| 1419 | return end; |
| 1420 | } |
| 1421 | |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1422 | void handleIPv4StaticPatch( |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1423 | const std::string &ifaceId, nlohmann::json &input, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1424 | const boost::container::flat_set<IPv4AddressData> &ipv4Data, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1425 | const std::shared_ptr<AsyncResp> asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1426 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1427 | if ((!input.is_array()) || input.empty()) |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1428 | { |
| 1429 | messages::propertyValueTypeError(asyncResp->res, input.dump(), |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1430 | "IPv4StaticAddresses"); |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1431 | return; |
| 1432 | } |
| 1433 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1434 | unsigned entryIdx = 1; |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1435 | // Find the first static IP address currently active on the NIC and |
| 1436 | // match it to the first JSON element in the IPv4StaticAddresses array. |
| 1437 | // Match each subsequent JSON element to the next static IP programmed |
| 1438 | // into the NIC. |
| 1439 | boost::container::flat_set<IPv4AddressData>::const_iterator NICIPentry = |
| 1440 | GetNextStaticIPEntry(ipv4Data.cbegin(), ipv4Data.cend()); |
| 1441 | |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 1442 | for (nlohmann::json &thisJson : input) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1443 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1444 | std::string pathString = |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1445 | "IPv4StaticAddresses/" + std::to_string(entryIdx); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1446 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1447 | if (!thisJson.is_null() && !thisJson.empty()) |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1448 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1449 | std::optional<std::string> address; |
| 1450 | std::optional<std::string> subnetMask; |
| 1451 | std::optional<std::string> gateway; |
| 1452 | |
| 1453 | if (!json_util::readJson(thisJson, asyncResp->res, "Address", |
| 1454 | address, "SubnetMask", subnetMask, |
| 1455 | "Gateway", gateway)) |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1456 | { |
| 1457 | messages::propertyValueFormatError( |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1458 | asyncResp->res, thisJson.dump(), pathString); |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1459 | return; |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1460 | } |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1461 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1462 | // Find the address/subnet/gateway values. Any values that are |
| 1463 | // not explicitly provided are assumed to be unmodified from the |
| 1464 | // current state of the interface. Merge existing state into the |
| 1465 | // current request. |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1466 | const std::string *addr = nullptr; |
| 1467 | const std::string *gw = nullptr; |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1468 | uint8_t prefixLength = 0; |
| 1469 | bool errorInEntry = false; |
| 1470 | if (address) |
Ratan Gupta | 9474b37 | 2019-03-01 15:13:37 +0530 | [diff] [blame] | 1471 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1472 | if (ipv4VerifyIpAndGetBitcount(*address)) |
| 1473 | { |
| 1474 | addr = &(*address); |
| 1475 | } |
| 1476 | else |
| 1477 | { |
| 1478 | messages::propertyValueFormatError( |
| 1479 | asyncResp->res, *address, pathString + "/Address"); |
| 1480 | errorInEntry = true; |
| 1481 | } |
| 1482 | } |
| 1483 | else if (NICIPentry != ipv4Data.cend()) |
| 1484 | { |
| 1485 | addr = &(NICIPentry->address); |
Ratan Gupta | 9474b37 | 2019-03-01 15:13:37 +0530 | [diff] [blame] | 1486 | } |
| 1487 | else |
| 1488 | { |
| 1489 | messages::propertyMissing(asyncResp->res, |
| 1490 | pathString + "/Address"); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1491 | errorInEntry = true; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1492 | } |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1493 | |
| 1494 | if (subnetMask) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1495 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1496 | if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength)) |
| 1497 | { |
| 1498 | messages::propertyValueFormatError( |
| 1499 | asyncResp->res, *subnetMask, |
| 1500 | pathString + "/SubnetMask"); |
| 1501 | errorInEntry = true; |
| 1502 | } |
| 1503 | } |
| 1504 | else if (NICIPentry != ipv4Data.cend()) |
| 1505 | { |
| 1506 | if (!ipv4VerifyIpAndGetBitcount(NICIPentry->netmask, |
| 1507 | &prefixLength)) |
| 1508 | { |
| 1509 | messages::propertyValueFormatError( |
| 1510 | asyncResp->res, NICIPentry->netmask, |
| 1511 | pathString + "/SubnetMask"); |
| 1512 | errorInEntry = true; |
| 1513 | } |
| 1514 | } |
| 1515 | else |
| 1516 | { |
| 1517 | messages::propertyMissing(asyncResp->res, |
| 1518 | pathString + "/SubnetMask"); |
| 1519 | errorInEntry = true; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1520 | } |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1521 | |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1522 | if (gateway) |
| 1523 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1524 | if (ipv4VerifyIpAndGetBitcount(*gateway)) |
| 1525 | { |
| 1526 | gw = &(*gateway); |
| 1527 | } |
| 1528 | else |
| 1529 | { |
| 1530 | messages::propertyValueFormatError( |
| 1531 | asyncResp->res, *gateway, pathString + "/Gateway"); |
| 1532 | errorInEntry = true; |
| 1533 | } |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1534 | } |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1535 | else if (NICIPentry != ipv4Data.cend()) |
| 1536 | { |
| 1537 | gw = &NICIPentry->gateway; |
| 1538 | } |
| 1539 | else |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1540 | { |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 1541 | messages::propertyMissing(asyncResp->res, |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1542 | pathString + "/Gateway"); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1543 | errorInEntry = true; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1546 | if (errorInEntry) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1547 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1548 | return; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1549 | } |
| 1550 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1551 | if (NICIPentry != ipv4Data.cend()) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1552 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1553 | if (gw != nullptr || addr != nullptr) |
| 1554 | { |
| 1555 | // Shouldn't be possible based on errorInEntry, but |
| 1556 | // it flags -wmaybe-uninitialized in the compiler, |
| 1557 | // so defend against that |
| 1558 | return; |
| 1559 | } |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1560 | deleteAndCreateIPv4(ifaceId, NICIPentry->id, prefixLength, |
| 1561 | *gw, *addr, asyncResp); |
| 1562 | NICIPentry = |
| 1563 | GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend()); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1564 | } |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1565 | else |
| 1566 | { |
| 1567 | createIPv4(ifaceId, entryIdx, prefixLength, *gateway, |
| 1568 | *address, asyncResp); |
| 1569 | } |
| 1570 | entryIdx++; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1571 | } |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1572 | else |
| 1573 | { |
| 1574 | if (NICIPentry == ipv4Data.cend()) |
| 1575 | { |
| 1576 | // Requesting a DELETE/DO NOT MODIFY action for an item |
| 1577 | // that isn't present on the eth(n) interface. Input JSON is |
| 1578 | // in error, so bail out. |
| 1579 | if (thisJson.is_null()) |
| 1580 | { |
| 1581 | messages::resourceCannotBeDeleted(asyncResp->res); |
| 1582 | return; |
| 1583 | } |
| 1584 | else |
| 1585 | { |
| 1586 | messages::propertyValueFormatError( |
| 1587 | asyncResp->res, thisJson.dump(), pathString); |
| 1588 | return; |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | if (thisJson.is_null()) |
| 1593 | { |
| 1594 | deleteIPv4(ifaceId, NICIPentry->id, asyncResp); |
| 1595 | } |
| 1596 | if (NICIPentry != ipv4Data.cend()) |
| 1597 | { |
| 1598 | NICIPentry = |
| 1599 | GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend()); |
| 1600 | } |
| 1601 | entryIdx++; |
| 1602 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1606 | void handleStaticNameServersPatch( |
| 1607 | const std::string &ifaceId, |
| 1608 | const std::vector<std::string> &updatedStaticNameServers, |
| 1609 | const std::shared_ptr<AsyncResp> &asyncResp) |
| 1610 | { |
| 1611 | crow::connections::systemBus->async_method_call( |
Johnathan Mantey | 286b911 | 2019-06-10 13:38:04 -0700 | [diff] [blame] | 1612 | [asyncResp](const boost::system::error_code ec) { |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1613 | if (ec) |
| 1614 | { |
| 1615 | messages::internalError(asyncResp->res); |
| 1616 | return; |
| 1617 | } |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1618 | }, |
| 1619 | "xyz.openbmc_project.Network", |
| 1620 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1621 | "org.freedesktop.DBus.Properties", "Set", |
manojkiran.eda@gmail.com | 0f6efdc | 2019-10-03 04:53:44 -0500 | [diff] [blame] | 1622 | "xyz.openbmc_project.Network.EthernetInterface", |
| 1623 | "StaticNameServers", |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1624 | std::variant<std::vector<std::string>>{updatedStaticNameServers}); |
| 1625 | } |
| 1626 | |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1627 | void handleIPv6StaticAddressesPatch( |
| 1628 | const std::string &ifaceId, nlohmann::json &input, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1629 | const boost::container::flat_set<IPv6AddressData> &ipv6Data, |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1630 | const std::shared_ptr<AsyncResp> asyncResp) |
| 1631 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1632 | if (!input.is_array() || input.empty()) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1633 | { |
| 1634 | messages::propertyValueTypeError(asyncResp->res, input.dump(), |
| 1635 | "IPv6StaticAddresses"); |
| 1636 | return; |
| 1637 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1638 | size_t entryIdx = 1; |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1639 | boost::container::flat_set<IPv6AddressData>::const_iterator NICIPentry = |
| 1640 | GetNextStaticIPEntry(ipv6Data.cbegin(), ipv6Data.cend()); |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1641 | for (nlohmann::json &thisJson : input) |
| 1642 | { |
| 1643 | std::string pathString = |
| 1644 | "IPv6StaticAddresses/" + std::to_string(entryIdx); |
| 1645 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1646 | if (!thisJson.is_null() && !thisJson.empty()) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1647 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1648 | std::optional<std::string> address; |
| 1649 | std::optional<uint8_t> prefixLength; |
| 1650 | |
| 1651 | if (!json_util::readJson(thisJson, asyncResp->res, "Address", |
| 1652 | address, "PrefixLength", prefixLength)) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1653 | { |
| 1654 | messages::propertyValueFormatError( |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1655 | asyncResp->res, thisJson.dump(), pathString); |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1656 | return; |
| 1657 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1658 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1659 | const std::string *addr; |
| 1660 | uint8_t prefix; |
| 1661 | |
| 1662 | // Find the address and prefixLength values. Any values that are |
| 1663 | // not explicitly provided are assumed to be unmodified from the |
| 1664 | // current state of the interface. Merge existing state into the |
| 1665 | // current request. |
| 1666 | if (address) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1667 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1668 | addr = &(*address); |
| 1669 | } |
| 1670 | else if (NICIPentry != ipv6Data.end()) |
| 1671 | { |
| 1672 | addr = &(NICIPentry->address); |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1673 | } |
| 1674 | else |
| 1675 | { |
| 1676 | messages::propertyMissing(asyncResp->res, |
| 1677 | pathString + "/Address"); |
| 1678 | return; |
| 1679 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1680 | |
| 1681 | if (prefixLength) |
| 1682 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1683 | prefix = *prefixLength; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1684 | } |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1685 | else if (NICIPentry != ipv6Data.end()) |
| 1686 | { |
| 1687 | prefix = NICIPentry->prefixLength; |
| 1688 | } |
| 1689 | else |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1690 | { |
| 1691 | messages::propertyMissing(asyncResp->res, |
| 1692 | pathString + "/PrefixLength"); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1693 | return; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1694 | } |
| 1695 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1696 | if (NICIPentry != ipv6Data.end()) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1697 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1698 | deleteAndCreateIPv6(ifaceId, NICIPentry->id, prefix, *addr, |
| 1699 | asyncResp); |
| 1700 | NICIPentry = |
| 1701 | GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend()); |
| 1702 | } |
| 1703 | else |
| 1704 | { |
| 1705 | createIPv6(ifaceId, *prefixLength, *addr, asyncResp); |
| 1706 | } |
| 1707 | entryIdx++; |
| 1708 | } |
| 1709 | else |
| 1710 | { |
| 1711 | if (NICIPentry == ipv6Data.end()) |
| 1712 | { |
| 1713 | // Requesting a DELETE/DO NOT MODIFY action for an item |
| 1714 | // that isn't present on the eth(n) interface. Input JSON is |
| 1715 | // in error, so bail out. |
| 1716 | if (thisJson.is_null()) |
| 1717 | { |
| 1718 | messages::resourceCannotBeDeleted(asyncResp->res); |
| 1719 | return; |
| 1720 | } |
| 1721 | else |
| 1722 | { |
| 1723 | messages::propertyValueFormatError( |
| 1724 | asyncResp->res, thisJson.dump(), pathString); |
| 1725 | return; |
| 1726 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1727 | } |
| 1728 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1729 | if (thisJson.is_null()) |
| 1730 | { |
| 1731 | deleteIPv6(ifaceId, NICIPentry->id, asyncResp); |
| 1732 | } |
| 1733 | if (NICIPentry != ipv6Data.cend()) |
| 1734 | { |
| 1735 | NICIPentry = |
| 1736 | GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend()); |
| 1737 | } |
| 1738 | entryIdx++; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1739 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1743 | void parseInterfaceData( |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1744 | std::shared_ptr<AsyncResp> asyncResp, const std::string &iface_id, |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1745 | const EthernetInterfaceData ðData, |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1746 | const boost::container::flat_set<IPv4AddressData> &ipv4Data, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1747 | const boost::container::flat_set<IPv6AddressData> &ipv6Data) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1748 | { |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1749 | constexpr const std::array<const char *, 1> inventoryForEthernet = { |
| 1750 | "xyz.openbmc_project.Inventory.Item.Ethernet"}; |
| 1751 | |
| 1752 | nlohmann::json &json_response = asyncResp->res.jsonValue; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1753 | json_response["Id"] = iface_id; |
| 1754 | json_response["@odata.id"] = |
| 1755 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id; |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1756 | json_response["InterfaceEnabled"] = ethData.nicEnabled; |
| 1757 | |
| 1758 | auto health = std::make_shared<HealthPopulate>(asyncResp); |
| 1759 | |
| 1760 | crow::connections::systemBus->async_method_call( |
| 1761 | [health](const boost::system::error_code ec, |
| 1762 | std::vector<std::string> &resp) { |
| 1763 | if (ec) |
| 1764 | { |
| 1765 | return; |
| 1766 | } |
| 1767 | |
| 1768 | health->inventory = std::move(resp); |
| 1769 | }, |
| 1770 | "xyz.openbmc_project.ObjectMapper", |
| 1771 | "/xyz/openbmc_project/object_mapper", |
| 1772 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", |
| 1773 | int32_t(0), inventoryForEthernet); |
| 1774 | |
| 1775 | health->populate(); |
| 1776 | |
| 1777 | if (ethData.nicEnabled) |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 1778 | { |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1779 | json_response["LinkStatus"] = "LinkUp"; |
| 1780 | json_response["Status"]["State"] = "Enabled"; |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 1781 | } |
| 1782 | else |
| 1783 | { |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1784 | json_response["LinkStatus"] = "NoLink"; |
| 1785 | json_response["Status"]["State"] = "Disabled"; |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 1786 | } |
Johnathan Mantey | aa05fb2 | 2020-01-08 12:08:44 -0800 | [diff] [blame] | 1787 | |
| 1788 | json_response["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown"; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1789 | json_response["SpeedMbps"] = ethData.speed; |
| 1790 | json_response["MACAddress"] = ethData.mac_address; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1791 | json_response["DHCPv4"]["DHCPEnabled"] = |
| 1792 | translateDHCPEnabledToBool(ethData.DHCPEnabled, true); |
| 1793 | json_response["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled; |
| 1794 | json_response["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled; |
| 1795 | json_response["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled; |
| 1796 | |
| 1797 | json_response["DHCPv6"]["OperatingMode"] = |
| 1798 | translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful" |
| 1799 | : "Disabled"; |
| 1800 | json_response["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled; |
| 1801 | json_response["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled; |
| 1802 | json_response["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled; |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 1803 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1804 | if (!ethData.hostname.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1805 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1806 | json_response["HostName"] = ethData.hostname; |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1807 | |
| 1808 | // When domain name is empty then it means, that it is a network |
| 1809 | // without domain names, and the host name itself must be treated as |
| 1810 | // FQDN |
| 1811 | std::string FQDN = std::move(ethData.hostname); |
Jennifer Lee | d24bfc7 | 2019-03-05 13:03:37 -0800 | [diff] [blame] | 1812 | if (!ethData.domainnames.empty()) |
| 1813 | { |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1814 | FQDN += "." + ethData.domainnames[0]; |
Jennifer Lee | d24bfc7 | 2019-03-05 13:03:37 -0800 | [diff] [blame] | 1815 | } |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1816 | json_response["FQDN"] = FQDN; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1819 | json_response["VLANs"] = { |
| 1820 | {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" + |
| 1821 | iface_id + "/VLANs"}}; |
| 1822 | |
manojkiran.eda@gmail.com | 0f6efdc | 2019-10-03 04:53:44 -0500 | [diff] [blame] | 1823 | json_response["NameServers"] = ethData.nameServers; |
| 1824 | json_response["StaticNameServers"] = ethData.staticNameServers; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1825 | |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1826 | nlohmann::json &ipv4_array = json_response["IPv4Addresses"]; |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1827 | nlohmann::json &ipv4_static_array = |
| 1828 | json_response["IPv4StaticAddresses"]; |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1829 | ipv4_array = nlohmann::json::array(); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1830 | ipv4_static_array = nlohmann::json::array(); |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1831 | for (auto &ipv4_config : ipv4Data) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1832 | { |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1833 | |
| 1834 | std::string gatewayStr = ipv4_config.gateway; |
| 1835 | if (gatewayStr.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1836 | { |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1837 | gatewayStr = "0.0.0.0"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1838 | } |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1839 | |
| 1840 | ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin}, |
| 1841 | {"SubnetMask", ipv4_config.netmask}, |
| 1842 | {"Address", ipv4_config.address}, |
| 1843 | {"Gateway", gatewayStr}}); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1844 | if (ipv4_config.origin == "Static") |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1845 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1846 | ipv4_static_array.push_back( |
| 1847 | {{"AddressOrigin", ipv4_config.origin}, |
| 1848 | {"SubnetMask", ipv4_config.netmask}, |
| 1849 | {"Address", ipv4_config.address}, |
| 1850 | {"Gateway", gatewayStr}}); |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1851 | } |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1852 | } |
| 1853 | |
Ravi Teja | 9a6fc6f | 2019-04-16 02:43:13 -0500 | [diff] [blame] | 1854 | json_response["IPv6DefaultGateway"] = ethData.ipv6_default_gateway; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1855 | |
| 1856 | nlohmann::json &ipv6_array = json_response["IPv6Addresses"]; |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1857 | nlohmann::json &ipv6_static_array = |
| 1858 | json_response["IPv6StaticAddresses"]; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1859 | ipv6_array = nlohmann::json::array(); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1860 | ipv6_static_array = nlohmann::json::array(); |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1861 | for (auto &ipv6_config : ipv6Data) |
| 1862 | { |
| 1863 | ipv6_array.push_back({{"Address", ipv6_config.address}, |
| 1864 | {"PrefixLength", ipv6_config.prefixLength}, |
| 1865 | {"AddressOrigin", ipv6_config.origin}}); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1866 | if (ipv6_config.origin == "Static") |
| 1867 | { |
| 1868 | ipv6_static_array.push_back( |
| 1869 | {{"Address", ipv6_config.address}, |
| 1870 | {"PrefixLength", ipv6_config.prefixLength}, |
| 1871 | {"AddressOrigin", ipv6_config.origin}}); |
| 1872 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1873 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | /** |
| 1877 | * Functions triggers appropriate requests on DBus |
| 1878 | */ |
| 1879 | void doGet(crow::Response &res, const crow::Request &req, |
| 1880 | const std::vector<std::string> ¶ms) override |
| 1881 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1882 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1883 | if (params.size() != 1) |
| 1884 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1885 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1886 | return; |
| 1887 | } |
| 1888 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1889 | getEthernetIfaceData( |
| 1890 | params[0], |
| 1891 | [this, asyncResp, iface_id{std::string(params[0])}]( |
| 1892 | const bool &success, const EthernetInterfaceData ðData, |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1893 | const boost::container::flat_set<IPv4AddressData> &ipv4Data, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1894 | const boost::container::flat_set<IPv6AddressData> &ipv6Data) { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1895 | if (!success) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1896 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1897 | // TODO(Pawel)consider distinguish between non existing |
| 1898 | // object, and other errors |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1899 | messages::resourceNotFound(asyncResp->res, |
| 1900 | "EthernetInterface", iface_id); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1901 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1902 | } |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 1903 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1904 | asyncResp->res.jsonValue["@odata.type"] = |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1905 | "#EthernetInterface.v1_4_1.EthernetInterface"; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1906 | asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface"; |
| 1907 | asyncResp->res.jsonValue["Description"] = |
| 1908 | "Management Network Interface"; |
| 1909 | |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1910 | parseInterfaceData(asyncResp, iface_id, ethData, ipv4Data, |
| 1911 | ipv6Data); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1912 | }); |
| 1913 | } |
| 1914 | |
| 1915 | void doPatch(crow::Response &res, const crow::Request &req, |
| 1916 | const std::vector<std::string> ¶ms) override |
| 1917 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1918 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1919 | if (params.size() != 1) |
| 1920 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1921 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1922 | return; |
| 1923 | } |
| 1924 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1925 | const std::string &iface_id = params[0]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1926 | |
Ed Tanous | bc0bd6e | 2018-12-10 14:07:55 -0800 | [diff] [blame] | 1927 | std::optional<std::string> hostname; |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1928 | std::optional<std::string> fqdn; |
Ratan Gupta | d577665 | 2019-03-03 08:47:22 +0530 | [diff] [blame] | 1929 | std::optional<std::string> macAddress; |
Ravi Teja | 9a6fc6f | 2019-04-16 02:43:13 -0500 | [diff] [blame] | 1930 | std::optional<std::string> ipv6DefaultGateway; |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1931 | std::optional<nlohmann::json> ipv4StaticAddresses; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1932 | std::optional<nlohmann::json> ipv6StaticAddresses; |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1933 | std::optional<std::vector<std::string>> staticNameServers; |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1934 | std::optional<nlohmann::json> dhcpv4; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1935 | std::optional<nlohmann::json> dhcpv6; |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1936 | std::optional<bool> interfaceEnabled; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1937 | DHCPParameters v4dhcpParms; |
| 1938 | DHCPParameters v6dhcpParms; |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1939 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1940 | if (!json_util::readJson( |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1941 | req, res, "HostName", hostname, "FQDN", fqdn, |
| 1942 | "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress", |
| 1943 | macAddress, "StaticNameServers", staticNameServers, |
| 1944 | "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses", |
| 1945 | ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6, |
| 1946 | "InterfaceEnabled", interfaceEnabled)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1947 | { |
| 1948 | return; |
| 1949 | } |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1950 | if (dhcpv4) |
| 1951 | { |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1952 | if (!json_util::readJson(*dhcpv4, res, "DHCPEnabled", |
| 1953 | v4dhcpParms.dhcpv4Enabled, "UseDNSServers", |
| 1954 | v4dhcpParms.useDNSServers, "UseNTPServers", |
| 1955 | v4dhcpParms.useNTPServers, "UseDomainName", |
| 1956 | v4dhcpParms.useUseDomainName)) |
| 1957 | { |
| 1958 | return; |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | if (dhcpv6) |
| 1963 | { |
| 1964 | if (!json_util::readJson(*dhcpv6, res, "OperatingMode", |
| 1965 | v6dhcpParms.dhcpv6OperatingMode, |
| 1966 | "UseDNSServers", v6dhcpParms.useDNSServers, |
| 1967 | "UseNTPServers", v6dhcpParms.useNTPServers, |
| 1968 | "UseDomainName", |
| 1969 | v6dhcpParms.useUseDomainName)) |
| 1970 | { |
| 1971 | return; |
| 1972 | } |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1973 | } |
| 1974 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1975 | // Get single eth interface data, and call the below callback for |
| 1976 | // JSON preparation |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1977 | getEthernetIfaceData( |
| 1978 | iface_id, |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1979 | [this, asyncResp, iface_id, hostname = std::move(hostname), |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1980 | fqdn = std::move(fqdn), macAddress = std::move(macAddress), |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1981 | ipv4StaticAddresses = std::move(ipv4StaticAddresses), |
Ravi Teja | 9a6fc6f | 2019-04-16 02:43:13 -0500 | [diff] [blame] | 1982 | ipv6DefaultGateway = std::move(ipv6DefaultGateway), |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1983 | ipv6StaticAddresses = std::move(ipv6StaticAddresses), |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1984 | staticNameServers = std::move(staticNameServers), |
| 1985 | dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), |
| 1986 | v4dhcpParms = std::move(v4dhcpParms), |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1987 | v6dhcpParms = std::move(v6dhcpParms), |
| 1988 | interfaceEnabled = std::move(interfaceEnabled)]( |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1989 | const bool &success, const EthernetInterfaceData ðData, |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1990 | const boost::container::flat_set<IPv4AddressData> &ipv4Data, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1991 | const boost::container::flat_set<IPv6AddressData> &ipv6Data) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1992 | if (!success) |
| 1993 | { |
| 1994 | // ... otherwise return error |
| 1995 | // TODO(Pawel)consider distinguish between non existing |
| 1996 | // object, and other errors |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1997 | messages::resourceNotFound(asyncResp->res, |
| 1998 | "Ethernet Interface", iface_id); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1999 | return; |
| 2000 | } |
| 2001 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2002 | if (dhcpv4 || dhcpv6) |
| 2003 | { |
| 2004 | handleDHCPPatch(iface_id, ethData, std::move(v4dhcpParms), |
| 2005 | std::move(v6dhcpParms), asyncResp); |
| 2006 | } |
| 2007 | |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 2008 | if (hostname) |
| 2009 | { |
| 2010 | handleHostnamePatch(*hostname, asyncResp); |
| 2011 | } |
| 2012 | |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 2013 | if (fqdn) |
| 2014 | { |
| 2015 | handleFqdnPatch(iface_id, *fqdn, asyncResp); |
| 2016 | } |
| 2017 | |
Ratan Gupta | d577665 | 2019-03-03 08:47:22 +0530 | [diff] [blame] | 2018 | if (macAddress) |
| 2019 | { |
| 2020 | handleMACAddressPatch(iface_id, *macAddress, asyncResp); |
| 2021 | } |
| 2022 | |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 2023 | if (ipv4StaticAddresses) |
| 2024 | { |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 2025 | // TODO(ed) for some reason the capture of ipv4Addresses |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2026 | // above is returning a const value, not a non-const |
| 2027 | // value. This doesn't really work for us, as we need to |
| 2028 | // be able to efficiently move out the intermedia |
| 2029 | // nlohmann::json objects. This makes a copy of the |
| 2030 | // structure, and operates on that, but could be done |
| 2031 | // more efficiently |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 2032 | nlohmann::json ipv4Static = std::move(*ipv4StaticAddresses); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2033 | handleIPv4StaticPatch(iface_id, ipv4Static, ipv4Data, |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 2034 | asyncResp); |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 2035 | } |
| 2036 | |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 2037 | if (staticNameServers) |
| 2038 | { |
| 2039 | handleStaticNameServersPatch(iface_id, *staticNameServers, |
| 2040 | asyncResp); |
| 2041 | } |
Ravi Teja | 9a6fc6f | 2019-04-16 02:43:13 -0500 | [diff] [blame] | 2042 | |
| 2043 | if (ipv6DefaultGateway) |
| 2044 | { |
| 2045 | messages::propertyNotWritable(asyncResp->res, |
| 2046 | "IPv6DefaultGateway"); |
| 2047 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2048 | |
| 2049 | if (ipv6StaticAddresses) |
| 2050 | { |
| 2051 | nlohmann::json ipv6Static = std::move(*ipv6StaticAddresses); |
| 2052 | handleIPv6StaticAddressesPatch(iface_id, ipv6Static, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2053 | ipv6Data, asyncResp); |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2054 | } |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 2055 | |
| 2056 | if (interfaceEnabled) |
| 2057 | { |
| 2058 | setEthernetInterfaceBoolProperty( |
| 2059 | iface_id, "NICEnabled", *interfaceEnabled, asyncResp); |
| 2060 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2061 | }); |
| 2062 | } |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 2063 | }; |
| 2064 | |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2065 | /** |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2066 | * VlanNetworkInterface derived class for delivering VLANNetworkInterface |
| 2067 | * Schema |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2068 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2069 | class VlanNetworkInterface : public Node |
| 2070 | { |
| 2071 | public: |
| 2072 | /* |
| 2073 | * Default Constructor |
| 2074 | */ |
| 2075 | template <typename CrowApp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2076 | VlanNetworkInterface(CrowApp &app) : |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2077 | Node(app, |
Gunnar Mills | 7af9151 | 2020-04-14 22:16:57 -0500 | [diff] [blame^] | 2078 | "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/", |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2079 | std::string(), std::string()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2080 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2081 | entityPrivileges = { |
| 2082 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 2083 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 2084 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 2085 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 2086 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 2087 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2088 | } |
| 2089 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2090 | private: |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 2091 | void parseInterfaceData( |
| 2092 | nlohmann::json &json_response, const std::string &parent_iface_id, |
| 2093 | const std::string &iface_id, const EthernetInterfaceData ðData, |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2094 | const boost::container::flat_set<IPv4AddressData> &ipv4Data, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2095 | const boost::container::flat_set<IPv6AddressData> &ipv6Data) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2096 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2097 | // Fill out obvious data... |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2098 | json_response["Id"] = iface_id; |
| 2099 | json_response["@odata.id"] = |
| 2100 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id + |
| 2101 | "/VLANs/" + iface_id; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2102 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2103 | json_response["VLANEnable"] = true; |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2104 | if (!ethData.vlan_id.empty()) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2105 | { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2106 | json_response["VLANId"] = ethData.vlan_id.back(); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2107 | } |
Ed Tanous | a434f2b | 2018-07-27 13:04:22 -0700 | [diff] [blame] | 2108 | } |
| 2109 | |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2110 | bool verifyNames(const std::string &parent, const std::string &iface) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2111 | { |
| 2112 | if (!boost::starts_with(iface, parent + "_")) |
| 2113 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2114 | return false; |
| 2115 | } |
| 2116 | else |
| 2117 | { |
| 2118 | return true; |
| 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | /** |
| 2123 | * Functions triggers appropriate requests on DBus |
| 2124 | */ |
| 2125 | void doGet(crow::Response &res, const crow::Request &req, |
| 2126 | const std::vector<std::string> ¶ms) override |
| 2127 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2128 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
| 2129 | // TODO(Pawel) this shall be parameterized call (two params) to get |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2130 | // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'. |
| 2131 | // Check if there is required param, truly entering this shall be |
| 2132 | // impossible. |
| 2133 | if (params.size() != 2) |
| 2134 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2135 | messages::internalError(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2136 | res.end(); |
Kowalski, Kamil | 927a505 | 2018-07-03 14:16:46 +0200 | [diff] [blame] | 2137 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2138 | } |
Kowalski, Kamil | 927a505 | 2018-07-03 14:16:46 +0200 | [diff] [blame] | 2139 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2140 | const std::string &parent_iface_id = params[0]; |
| 2141 | const std::string &iface_id = params[1]; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 2142 | res.jsonValue["@odata.type"] = |
| 2143 | "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 2144 | res.jsonValue["Name"] = "VLAN Network Interface"; |
Kowalski, Kamil | 927a505 | 2018-07-03 14:16:46 +0200 | [diff] [blame] | 2145 | |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2146 | if (!verifyNames(parent_iface_id, iface_id)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2147 | { |
| 2148 | return; |
| 2149 | } |
Kowalski, Kamil | 927a505 | 2018-07-03 14:16:46 +0200 | [diff] [blame] | 2150 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2151 | // Get single eth interface data, and call the below callback for |
| 2152 | // JSON preparation |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2153 | getEthernetIfaceData( |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2154 | params[1], |
| 2155 | [this, asyncResp, parent_iface_id{std::string(params[0])}, |
| 2156 | iface_id{std::string(params[1])}]( |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2157 | const bool &success, const EthernetInterfaceData ðData, |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2158 | const boost::container::flat_set<IPv4AddressData> &ipv4Data, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2159 | const boost::container::flat_set<IPv6AddressData> &ipv6Data) { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2160 | if (success && ethData.vlan_id.size() != 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2161 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 2162 | parseInterfaceData(asyncResp->res.jsonValue, |
| 2163 | parent_iface_id, iface_id, ethData, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2164 | ipv4Data, ipv6Data); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2165 | } |
| 2166 | else |
| 2167 | { |
| 2168 | // ... otherwise return error |
| 2169 | // TODO(Pawel)consider distinguish between non existing |
| 2170 | // object, and other errors |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2171 | messages::resourceNotFound( |
| 2172 | asyncResp->res, "VLAN Network Interface", iface_id); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2173 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2174 | }); |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2175 | } |
| 2176 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2177 | void doPatch(crow::Response &res, const crow::Request &req, |
| 2178 | const std::vector<std::string> ¶ms) override |
| 2179 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2180 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2181 | if (params.size() != 2) |
| 2182 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2183 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2184 | return; |
| 2185 | } |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2186 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2187 | const std::string &parentIfaceId = params[0]; |
| 2188 | const std::string &ifaceId = params[1]; |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2189 | |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2190 | if (!verifyNames(parentIfaceId, ifaceId)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2191 | { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2192 | messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", |
| 2193 | ifaceId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2194 | return; |
| 2195 | } |
| 2196 | |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 2197 | bool vlanEnable = false; |
| 2198 | uint64_t vlanId = 0; |
| 2199 | |
| 2200 | if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId", |
| 2201 | vlanId)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2202 | { |
| 2203 | return; |
| 2204 | } |
| 2205 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2206 | // Get single eth interface data, and call the below callback for |
| 2207 | // JSON preparation |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2208 | getEthernetIfaceData( |
| 2209 | params[1], |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 2210 | [asyncResp, parentIfaceId{std::string(params[0])}, |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2211 | ifaceId{std::string(params[1])}, &vlanEnable, &vlanId]( |
| 2212 | const bool &success, const EthernetInterfaceData ðData, |
| 2213 | const boost::container::flat_set<IPv4AddressData> &ipv4Data, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2214 | const boost::container::flat_set<IPv6AddressData> &ipv6Data) { |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2215 | if (success && !ethData.vlan_id.empty()) |
Sunitha Harish | 08244d0 | 2019-04-01 03:57:25 -0500 | [diff] [blame] | 2216 | { |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2217 | auto callback = |
| 2218 | [asyncResp](const boost::system::error_code ec) { |
| 2219 | if (ec) |
| 2220 | { |
| 2221 | messages::internalError(asyncResp->res); |
| 2222 | } |
| 2223 | }; |
| 2224 | |
| 2225 | if (vlanEnable == true) |
| 2226 | { |
| 2227 | crow::connections::systemBus->async_method_call( |
| 2228 | std::move(callback), "xyz.openbmc_project.Network", |
| 2229 | "/xyz/openbmc_project/network/" + ifaceId, |
| 2230 | "org.freedesktop.DBus.Properties", "Set", |
| 2231 | "xyz.openbmc_project.Network.VLAN", "Id", |
| 2232 | std::variant<uint32_t>(vlanId)); |
| 2233 | } |
| 2234 | else |
| 2235 | { |
| 2236 | BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the " |
| 2237 | "vlan interface"; |
| 2238 | crow::connections::systemBus->async_method_call( |
| 2239 | std::move(callback), "xyz.openbmc_project.Network", |
| 2240 | std::string("/xyz/openbmc_project/network/") + |
| 2241 | ifaceId, |
| 2242 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 2243 | } |
Sunitha Harish | 08244d0 | 2019-04-01 03:57:25 -0500 | [diff] [blame] | 2244 | } |
| 2245 | else |
| 2246 | { |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2247 | // TODO(Pawel)consider distinguish between non existing |
| 2248 | // object, and other errors |
| 2249 | messages::resourceNotFound( |
| 2250 | asyncResp->res, "VLAN Network Interface", ifaceId); |
| 2251 | return; |
Sunitha Harish | 08244d0 | 2019-04-01 03:57:25 -0500 | [diff] [blame] | 2252 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2253 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | void doDelete(crow::Response &res, const crow::Request &req, |
| 2257 | const std::vector<std::string> ¶ms) override |
| 2258 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2259 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2260 | if (params.size() != 2) |
| 2261 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2262 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2263 | return; |
| 2264 | } |
| 2265 | |
| 2266 | const std::string &parentIfaceId = params[0]; |
| 2267 | const std::string &ifaceId = params[1]; |
| 2268 | |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2269 | if (!verifyNames(parentIfaceId, ifaceId)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2270 | { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2271 | messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", |
| 2272 | ifaceId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2273 | return; |
| 2274 | } |
| 2275 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2276 | // Get single eth interface data, and call the below callback for |
| 2277 | // JSON preparation |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2278 | getEthernetIfaceData( |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2279 | params[1], |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 2280 | [asyncResp, parentIfaceId{std::string(params[0])}, |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2281 | ifaceId{std::string(params[1])}]( |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2282 | const bool &success, const EthernetInterfaceData ðData, |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2283 | const boost::container::flat_set<IPv4AddressData> &ipv4Data, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2284 | const boost::container::flat_set<IPv6AddressData> &ipv6Data) { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2285 | if (success && !ethData.vlan_id.empty()) |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2286 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2287 | auto callback = |
| 2288 | [asyncResp](const boost::system::error_code ec) { |
| 2289 | if (ec) |
| 2290 | { |
| 2291 | messages::internalError(asyncResp->res); |
| 2292 | } |
| 2293 | }; |
| 2294 | crow::connections::systemBus->async_method_call( |
| 2295 | std::move(callback), "xyz.openbmc_project.Network", |
| 2296 | std::string("/xyz/openbmc_project/network/") + ifaceId, |
| 2297 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 2298 | } |
| 2299 | else |
| 2300 | { |
| 2301 | // ... otherwise return error |
| 2302 | // TODO(Pawel)consider distinguish between non existing |
| 2303 | // object, and other errors |
| 2304 | messages::resourceNotFound( |
| 2305 | asyncResp->res, "VLAN Network Interface", ifaceId); |
| 2306 | } |
| 2307 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2308 | } |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2309 | }; |
| 2310 | |
| 2311 | /** |
| 2312 | * VlanNetworkInterfaceCollection derived class for delivering |
| 2313 | * VLANNetworkInterface Collection Schema |
| 2314 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2315 | class VlanNetworkInterfaceCollection : public Node |
| 2316 | { |
| 2317 | public: |
| 2318 | template <typename CrowApp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2319 | VlanNetworkInterfaceCollection(CrowApp &app) : |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2320 | Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/", |
| 2321 | std::string()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2322 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2323 | entityPrivileges = { |
| 2324 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 2325 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 2326 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 2327 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 2328 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 2329 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2330 | } |
| 2331 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2332 | private: |
| 2333 | /** |
| 2334 | * Functions triggers appropriate requests on DBus |
| 2335 | */ |
| 2336 | void doGet(crow::Response &res, const crow::Request &req, |
| 2337 | const std::vector<std::string> ¶ms) override |
| 2338 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2339 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2340 | if (params.size() != 1) |
| 2341 | { |
| 2342 | // This means there is a problem with the router |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2343 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2344 | return; |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame] | 2345 | } |
| 2346 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2347 | const std::string &rootInterfaceName = params[0]; |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2348 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2349 | // Get eth interface list, and call the below callback for JSON |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2350 | // preparation |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2351 | getEthernetIfaceList( |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 2352 | [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}]( |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2353 | const bool &success, |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 2354 | const boost::container::flat_set<std::string> &iface_list) { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2355 | if (!success) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2356 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2357 | messages::internalError(asyncResp->res); |
| 2358 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2359 | } |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 2360 | |
| 2361 | if (iface_list.find(rootInterfaceName) == iface_list.end()) |
| 2362 | { |
| 2363 | messages::resourceNotFound(asyncResp->res, |
| 2364 | "VLanNetworkInterfaceCollection", |
| 2365 | rootInterfaceName); |
| 2366 | return; |
| 2367 | } |
| 2368 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 2369 | asyncResp->res.jsonValue["@odata.type"] = |
| 2370 | "#VLanNetworkInterfaceCollection." |
| 2371 | "VLanNetworkInterfaceCollection"; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 2372 | asyncResp->res.jsonValue["Name"] = |
| 2373 | "VLAN Network Interface Collection"; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2374 | |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2375 | nlohmann::json iface_array = nlohmann::json::array(); |
| 2376 | |
| 2377 | for (const std::string &iface_item : iface_list) |
| 2378 | { |
| 2379 | if (boost::starts_with(iface_item, rootInterfaceName + "_")) |
| 2380 | { |
| 2381 | iface_array.push_back( |
| 2382 | {{"@odata.id", |
| 2383 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + |
| 2384 | rootInterfaceName + "/VLANs/" + iface_item}}); |
| 2385 | } |
| 2386 | } |
| 2387 | |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2388 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 2389 | iface_array.size(); |
| 2390 | asyncResp->res.jsonValue["Members"] = std::move(iface_array); |
| 2391 | asyncResp->res.jsonValue["@odata.id"] = |
| 2392 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + |
| 2393 | rootInterfaceName + "/VLANs"; |
| 2394 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2395 | } |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2396 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2397 | void doPost(crow::Response &res, const crow::Request &req, |
| 2398 | const std::vector<std::string> ¶ms) override |
| 2399 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2400 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2401 | if (params.size() != 1) |
| 2402 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2403 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2404 | return; |
| 2405 | } |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2406 | bool vlanEnable = false; |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 2407 | uint32_t vlanId = 0; |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2408 | if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable", |
| 2409 | vlanEnable)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2410 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2411 | return; |
| 2412 | } |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2413 | // Need both vlanId and vlanEnable to service this request |
| 2414 | if (!vlanId) |
| 2415 | { |
| 2416 | messages::propertyMissing(asyncResp->res, "VLANId"); |
| 2417 | } |
| 2418 | if (!vlanEnable) |
| 2419 | { |
| 2420 | messages::propertyMissing(asyncResp->res, "VLANEnable"); |
| 2421 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 2422 | if (static_cast<bool>(vlanId) ^ vlanEnable) |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2423 | { |
| 2424 | return; |
| 2425 | } |
| 2426 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2427 | const std::string &rootInterfaceName = params[0]; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2428 | auto callback = [asyncResp](const boost::system::error_code ec) { |
| 2429 | if (ec) |
| 2430 | { |
| 2431 | // TODO(ed) make more consistent error messages based on |
| 2432 | // phosphor-network responses |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2433 | messages::internalError(asyncResp->res); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2434 | return; |
| 2435 | } |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2436 | messages::created(asyncResp->res); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2437 | }; |
| 2438 | crow::connections::systemBus->async_method_call( |
| 2439 | std::move(callback), "xyz.openbmc_project.Network", |
| 2440 | "/xyz/openbmc_project/network", |
| 2441 | "xyz.openbmc_project.Network.VLAN.Create", "VLAN", |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 2442 | rootInterfaceName, vlanId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2443 | } |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2444 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2445 | } // namespace redfish |