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