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