blob: 3e10599ae034c1290bb2e30386aab198aac3f73c [file] [log] [blame]
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001/*
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 Tanous1abe55e2018-09-05 08:30:59 -070018#include <boost/container/flat_map.hpp>
Ed Tanous4a0cb852018-10-15 07:55:04 -070019#include <boost/container/flat_set.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020020#include <dbus_singleton.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020021#include <error_messages.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020022#include <node.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050023#include <utils/json_utils.hpp>
24
Ed Tanousa24526d2018-12-10 15:17:59 -080025#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053026#include <regex>
Ed Tanousabf2add2019-01-22 16:40:12 -080027#include <variant>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010028
Ed Tanous1abe55e2018-09-05 08:30:59 -070029namespace redfish
30{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010031
32/**
33 * DBus types primitives for several generic DBus interfaces
34 * TODO(Pawel) consider move this to separate file into boost::dbus
35 */
Ed Tanousaa2e59c2018-04-12 12:17:20 -070036using PropertiesMapType = boost::container::flat_map<
Ed Tanousabf2add2019-01-22 16:40:12 -080037 std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
38 int32_t, uint32_t, int64_t, uint64_t, double>>;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010039
Ed Tanous4a0cb852018-10-15 07:55:04 -070040using GetManagedObjects = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070041 sdbusplus::message::object_path,
Ed Tanous4a0cb852018-10-15 07:55:04 -070042 std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070043 std::string,
44 boost::container::flat_map<
Patrick Williams19bd78d2020-05-13 17:38:24 -050045 std::string,
46 std::variant<std::string, bool, uint8_t, int16_t, uint16_t, int32_t,
47 uint32_t, int64_t, uint64_t, double,
48 std::vector<std::string>>>>>>>;
Ed Tanous4a0cb852018-10-15 07:55:04 -070049
50enum class LinkType
51{
52 Local,
53 Global
54};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010055
56/**
57 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010058 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070059struct IPv4AddressData
60{
61 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070062 std::string address;
63 std::string domain;
64 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070065 std::string netmask;
66 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070067 LinkType linktype;
Sunitha Harish01c6e852020-03-20 05:04:09 -050068 bool isActive;
Ed Tanous4a0cb852018-10-15 07:55:04 -070069
Gunnar Mills1214b7e2020-06-04 10:11:30 -050070 bool operator<(const IPv4AddressData& obj) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070072 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010074};
75
76/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050077 * Structure for keeping IPv6 data required by Redfish
78 */
79struct IPv6AddressData
80{
81 std::string id;
82 std::string address;
83 std::string origin;
84 uint8_t prefixLength;
85
Gunnar Mills1214b7e2020-06-04 10:11:30 -050086 bool operator<(const IPv6AddressData& obj) const
Ravi Tejae48c0fc2019-04-16 08:37:20 -050087 {
88 return id < obj.id;
89 }
90};
91/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010092 * Structure for keeping basic single Ethernet Interface information
93 * available from DBus
94 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070095struct EthernetInterfaceData
96{
Ed Tanous4a0cb852018-10-15 07:55:04 -070097 uint32_t speed;
98 bool auto_neg;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070099 bool DNSEnabled;
100 bool NTPEnabled;
101 bool HostNameEnabled;
102 bool SendHostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800103 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700104 bool nicEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700105 std::string DHCPEnabled;
106 std::string operatingMode;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700107 std::string hostname;
108 std::string default_gateway;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500109 std::string ipv6_default_gateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700110 std::string mac_address;
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500111 std::vector<std::uint32_t> vlan_id;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500112 std::vector<std::string> nameServers;
113 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800114 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100115};
116
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700117struct DHCPParameters
118{
119 std::optional<bool> dhcpv4Enabled;
120 std::optional<bool> useDNSServers;
121 std::optional<bool> useNTPServers;
122 std::optional<bool> useUseDomainName;
123 std::optional<std::string> dhcpv6OperatingMode;
124};
125
Ed Tanous4a0cb852018-10-15 07:55:04 -0700126// Helper function that changes bits netmask notation (i.e. /24)
127// into full dot notation
128inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700129{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700130 uint32_t value = 0xffffffff << (32 - bits);
131 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
132 std::to_string((value >> 16) & 0xff) + "." +
133 std::to_string((value >> 8) & 0xff) + "." +
134 std::to_string(value & 0xff);
135 return netmask;
136}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100137
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500138inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700139 bool isIPv4)
140{
141 if (isIPv4)
142 {
143 return (
144 (inputDHCP ==
145 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
146 (inputDHCP ==
147 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
148 }
149 return ((inputDHCP ==
150 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
151 (inputDHCP ==
152 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
153}
154
Ed Tanous2c70f802020-09-28 14:29:23 -0700155inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700156{
157 if (isIPv4 && isIPv6)
158 {
159 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
160 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700161 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700162 {
163 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
164 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700165 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700166 {
167 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
168 }
169 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
170}
171
Ed Tanous4a0cb852018-10-15 07:55:04 -0700172inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500173 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700174 bool isIPv4)
175{
176 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700177 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700178 return "Static";
179 }
180 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
181 {
182 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700183 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700184 return "IPv4LinkLocal";
185 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700186 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700187 }
188 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
189 {
190 if (isIPv4)
191 {
192 return "DHCP";
193 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700194 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700195 }
196 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
197 {
198 return "SLAAC";
199 }
200 return "";
201}
202
Ed Tanous81ce6092020-12-17 16:54:55 +0000203inline bool extractEthernetInterfaceData(const std::string& ethifaceId,
204 GetManagedObjects& dbusData,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500205 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700206{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700207 bool idFound = false;
Ed Tanous81ce6092020-12-17 16:54:55 +0000208 for (auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700209 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700210 for (auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700211 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000212 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700213 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700214 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700215 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700216 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500217 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700218 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700219 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700220 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500221 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800222 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700223 if (mac != nullptr)
224 {
225 ethData.mac_address = *mac;
226 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700227 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700228 }
229 }
230 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
231 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500232 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700233 {
234 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700235 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500236 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800237 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700238 if (id != nullptr)
239 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500240 ethData.vlan_id.push_back(*id);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700241 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700242 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700243 }
244 }
245 else if (ifacePair.first ==
246 "xyz.openbmc_project.Network.EthernetInterface")
247 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500248 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700249 {
250 if (propertyPair.first == "AutoNeg")
251 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700252 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800253 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700254 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700255 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700256 ethData.auto_neg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700257 }
258 }
259 else if (propertyPair.first == "Speed")
260 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500261 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800262 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700263 if (speed != nullptr)
264 {
265 ethData.speed = *speed;
266 }
267 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800268 else if (propertyPair.first == "LinkUp")
269 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500270 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800271 std::get_if<bool>(&propertyPair.second);
272 if (linkUp != nullptr)
273 {
274 ethData.linkUp = *linkUp;
275 }
276 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700277 else if (propertyPair.first == "NICEnabled")
278 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500279 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700280 std::get_if<bool>(&propertyPair.second);
281 if (nicEnabled != nullptr)
282 {
283 ethData.nicEnabled = *nicEnabled;
284 }
285 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500286 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700287 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500288 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500289 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800290 &propertyPair.second);
291 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700292 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700293 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500294 }
295 }
296 else if (propertyPair.first == "StaticNameServers")
297 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500298 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500299 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500300 &propertyPair.second);
301 if (staticNameServers != nullptr)
302 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700303 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700304 }
305 }
manojkiraneda2a133282019-02-19 13:09:43 +0530306 else if (propertyPair.first == "DHCPEnabled")
307 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700308 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700309 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700310 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530311 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700312 ethData.DHCPEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530313 }
314 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800315 else if (propertyPair.first == "DomainName")
316 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500317 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500318 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800319 &propertyPair.second);
320 if (domainNames != nullptr)
321 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700322 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800323 }
324 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500325 else if (propertyPair.first == "DefaultGateway")
326 {
327 const std::string* defaultGateway =
328 std::get_if<std::string>(&propertyPair.second);
329 if (defaultGateway != nullptr)
330 {
331 std::string defaultGatewayStr = *defaultGateway;
332 if (defaultGatewayStr.empty())
333 {
334 ethData.default_gateway = "0.0.0.0";
335 }
336 else
337 {
338 ethData.default_gateway = defaultGatewayStr;
339 }
340 }
341 }
342 else if (propertyPair.first == "DefaultGateway6")
343 {
344 const std::string* defaultGateway6 =
345 std::get_if<std::string>(&propertyPair.second);
346 if (defaultGateway6 != nullptr)
347 {
348 std::string defaultGateway6Str =
349 *defaultGateway6;
350 if (defaultGateway6Str.empty())
351 {
352 ethData.ipv6_default_gateway =
353 "0:0:0:0:0:0:0:0";
354 }
355 else
356 {
357 ethData.ipv6_default_gateway =
358 defaultGateway6Str;
359 }
360 }
361 }
Ed Tanous029573d2019-02-01 10:57:49 -0800362 }
363 }
364 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700365
366 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
367 {
368 if (ifacePair.first ==
369 "xyz.openbmc_project.Network.DHCPConfiguration")
370 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500371 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700372 {
373 if (propertyPair.first == "DNSEnabled")
374 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700375 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700376 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700377 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700378 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700379 ethData.DNSEnabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700380 }
381 }
382 else if (propertyPair.first == "NTPEnabled")
383 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700384 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700385 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700386 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700387 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700388 ethData.NTPEnabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700389 }
390 }
391 else if (propertyPair.first == "HostNameEnabled")
392 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700393 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700394 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700395 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700396 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700397 ethData.HostNameEnabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700398 }
399 }
400 else if (propertyPair.first == "SendHostNameEnabled")
401 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700402 const bool* sendHostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700403 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700404 if (sendHostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700405 {
406 ethData.SendHostNameEnabled =
Ed Tanous2c70f802020-09-28 14:29:23 -0700407 *sendHostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700408 }
409 }
410 }
411 }
412 }
Ed Tanous029573d2019-02-01 10:57:49 -0800413 // System configuration shows up in the global namespace, so no need
414 // to check eth number
415 if (ifacePair.first ==
416 "xyz.openbmc_project.Network.SystemConfiguration")
417 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500418 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800419 {
420 if (propertyPair.first == "HostName")
421 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500422 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500423 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800424 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700425 {
Ed Tanous029573d2019-02-01 10:57:49 -0800426 ethData.hostname = *hostname;
427 }
428 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700429 }
430 }
431 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700432 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700433 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700434}
435
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500436// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700437inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000438 extractIPV6Data(const std::string& ethifaceId,
439 const GetManagedObjects& dbusData,
440 boost::container::flat_set<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500441{
442 const std::string ipv6PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000443 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500444
445 // Since there might be several IPv6 configurations aligned with
446 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000447 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500448 {
449 // Check if proper pattern for object path appears
450 if (boost::starts_with(objpath.first.str, ipv6PathStart))
451 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500452 for (auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500453 {
454 if (interface.first == "xyz.openbmc_project.Network.IP")
455 {
456 // Instance IPv6AddressData structure, and set as
457 // appropriate
458 std::pair<
459 boost::container::flat_set<IPv6AddressData>::iterator,
460 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000461 it = ipv6Config.insert(IPv6AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700462 IPv6AddressData& ipv6Address = *it.first;
463 ipv6Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700464 objpath.first.str.substr(ipv6PathStart.size());
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500465 for (auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500466 {
467 if (property.first == "Address")
468 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500469 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500470 std::get_if<std::string>(&property.second);
471 if (address != nullptr)
472 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700473 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500474 }
475 }
476 else if (property.first == "Origin")
477 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500478 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500479 std::get_if<std::string>(&property.second);
480 if (origin != nullptr)
481 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700482 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500483 translateAddressOriginDbusToRedfish(*origin,
484 false);
485 }
486 }
487 else if (property.first == "PrefixLength")
488 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500489 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500490 std::get_if<uint8_t>(&property.second);
491 if (prefix != nullptr)
492 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700493 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500494 }
495 }
496 else
497 {
498 BMCWEB_LOG_ERROR
499 << "Got extra property: " << property.first
500 << " on the " << objpath.first.str << " object";
501 }
502 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500503 }
504 }
505 }
506 }
507}
508
Ed Tanous4a0cb852018-10-15 07:55:04 -0700509// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700510inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000511 extractIPData(const std::string& ethifaceId,
512 const GetManagedObjects& dbusData,
513 boost::container::flat_set<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700514{
515 const std::string ipv4PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000516 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700517
518 // Since there might be several IPv4 configurations aligned with
519 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000520 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700521 {
522 // Check if proper pattern for object path appears
523 if (boost::starts_with(objpath.first.str, ipv4PathStart))
524 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500525 for (auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700526 {
527 if (interface.first == "xyz.openbmc_project.Network.IP")
528 {
529 // Instance IPv4AddressData structure, and set as
530 // appropriate
531 std::pair<
532 boost::container::flat_set<IPv4AddressData>::iterator,
533 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000534 it = ipv4Config.insert(IPv4AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700535 IPv4AddressData& ipv4Address = *it.first;
536 ipv4Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700537 objpath.first.str.substr(ipv4PathStart.size());
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500538 for (auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700539 {
540 if (property.first == "Address")
541 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500542 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800543 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700544 if (address != nullptr)
545 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700546 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700547 }
548 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700549 else if (property.first == "Origin")
550 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500551 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800552 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700553 if (origin != nullptr)
554 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700555 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700556 translateAddressOriginDbusToRedfish(*origin,
557 true);
558 }
559 }
560 else if (property.first == "PrefixLength")
561 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500562 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800563 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700564 if (mask != nullptr)
565 {
566 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700567 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700568 }
569 }
570 else
571 {
572 BMCWEB_LOG_ERROR
573 << "Got extra property: " << property.first
574 << " on the " << objpath.first.str << " object";
575 }
576 }
577 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700578 ipv4Address.linktype =
579 boost::starts_with(ipv4Address.address, "169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700580 ? LinkType::Local
581 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700582 }
583 }
584 }
585 }
586}
587
588/**
589 * @brief Sets given Id on the given VLAN interface through D-Bus
590 *
591 * @param[in] ifaceId Id of VLAN interface that should be modified
592 * @param[in] inputVlanId New ID of the VLAN
593 * @param[in] callback Function that will be called after the operation
594 *
595 * @return None.
596 */
597template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500598void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
599 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700600{
601 crow::connections::systemBus->async_method_call(
602 callback, "xyz.openbmc_project.Network",
603 std::string("/xyz/openbmc_project/network/") + ifaceId,
604 "org.freedesktop.DBus.Properties", "Set",
605 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanousabf2add2019-01-22 16:40:12 -0800606 std::variant<uint32_t>(inputVlanId));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700607}
608
609/**
610 * @brief Helper function that verifies IP address to check if it is in
611 * proper format. If bits pointer is provided, also calculates active
612 * bit count for Subnet Mask.
613 *
614 * @param[in] ip IP that will be verified
615 * @param[out] bits Calculated mask in bits notation
616 *
617 * @return true in case of success, false otherwise
618 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500619inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
620 uint8_t* bits = nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700621{
622 std::vector<std::string> bytesInMask;
623
624 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700625
626 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700627 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700628 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700629 return false;
630 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700631
Ed Tanous4a0cb852018-10-15 07:55:04 -0700632 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700633 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700634 *bits = 0;
635 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700636
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500637 char* endPtr;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700638 long previousValue = 255;
639 bool firstZeroInByteHit;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500640 for (const std::string& byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700641 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700642 if (byte.empty())
643 {
644 return false;
645 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700646
Ed Tanous4a0cb852018-10-15 07:55:04 -0700647 // Use strtol instead of stroi to avoid exceptions
648 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700649
Ed Tanous4a0cb852018-10-15 07:55:04 -0700650 // endPtr should point to the end of the string, otherwise given string
651 // is not 100% number
652 if (*endPtr != '\0')
653 {
654 return false;
655 }
656
657 // Value should be contained in byte
658 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700659 {
660 return false;
661 }
662
663 if (bits != nullptr)
664 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700665 // Mask has to be continuous between bytes
666 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700667 {
668 return false;
669 }
670
Ed Tanous4a0cb852018-10-15 07:55:04 -0700671 // Mask has to be continuous inside bytes
672 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700673
Ed Tanous4a0cb852018-10-15 07:55:04 -0700674 // Count bits
Ed Tanous23a21a12020-07-25 04:45:05 +0000675 for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700676 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000677 if (value & (1L << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700678 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700679 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700680 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700681 // Continuity not preserved
682 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700683 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700684 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700685 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700686 else
687 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700688 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700689 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700690 }
691 }
692
693 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700694 }
695
Ed Tanous4a0cb852018-10-15 07:55:04 -0700696 return true;
697}
698
699/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700700 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700701 *
702 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700703 * @param[in] ipHash DBus Hash id of IP that should be deleted
704 * @param[io] asyncResp Response object that will be returned to client
705 *
706 * @return None
707 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500708inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800709 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700710{
711 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700712 [asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700713 if (ec)
714 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800715 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100716 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700717 },
718 "xyz.openbmc_project.Network",
719 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
720 "xyz.openbmc_project.Object.Delete", "Delete");
721}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700722
Gunnar Mills244b6d52021-04-12 15:44:23 -0500723inline void updateIPv4DefaultGateway(
724 const std::string& ifaceId, const std::string& gateway,
725 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500726{
727 crow::connections::systemBus->async_method_call(
728 [asyncResp](const boost::system::error_code ec) {
729 if (ec)
730 {
731 messages::internalError(asyncResp->res);
732 return;
733 }
734 asyncResp->res.result(boost::beast::http::status::no_content);
735 },
736 "xyz.openbmc_project.Network",
737 "/xyz/openbmc_project/network/" + ifaceId,
738 "org.freedesktop.DBus.Properties", "Set",
739 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
740 std::variant<std::string>(gateway));
741}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700742/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700743 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700744 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700745 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
746 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
747 * @param[in] gateway IPv4 address of this interfaces gateway
748 * @param[in] address IPv4 address to assign to this interface
749 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700750 *
751 * @return None
752 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000753inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
754 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800755 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700756{
Ravi Teja9010ec22019-08-01 23:30:25 -0500757 auto createIpHandler = [asyncResp, ifaceId,
758 gateway](const boost::system::error_code ec) {
759 if (ec)
760 {
761 messages::internalError(asyncResp->res);
762 return;
763 }
764 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
765 };
766
Ed Tanous4a0cb852018-10-15 07:55:04 -0700767 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500768 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700769 "/xyz/openbmc_project/network/" + ifaceId,
770 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700771 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700772 gateway);
773}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500774
775/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700776 * @brief Deletes the IPv4 entry for this interface and creates a replacement
777 * static IPv4 entry
778 *
779 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
780 * @param[in] id The unique hash entry identifying the DBus entry
781 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
782 * @param[in] gateway IPv4 address of this interfaces gateway
783 * @param[in] address IPv4 address to assign to this interface
784 * @param[io] asyncResp Response object that will be returned to client
785 *
786 * @return None
787 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800788inline void
789 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
790 uint8_t prefixLength, const std::string& gateway,
791 const std::string& address,
792 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700793{
794 crow::connections::systemBus->async_method_call(
795 [asyncResp, ifaceId, address, prefixLength,
796 gateway](const boost::system::error_code ec) {
797 if (ec)
798 {
799 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500800 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700801 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500802
Johnathan Mantey01784822019-06-18 12:44:21 -0700803 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500804 [asyncResp, ifaceId,
805 gateway](const boost::system::error_code ec2) {
Ed Tanous23a21a12020-07-25 04:45:05 +0000806 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700807 {
808 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500809 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700810 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500811 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -0700812 },
813 "xyz.openbmc_project.Network",
814 "/xyz/openbmc_project/network/" + ifaceId,
815 "xyz.openbmc_project.Network.IP.Create", "IP",
816 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
817 prefixLength, gateway);
818 },
819 "xyz.openbmc_project.Network",
820 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
821 "xyz.openbmc_project.Object.Delete", "Delete");
822}
823
824/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500825 * @brief Deletes given IPv6
826 *
827 * @param[in] ifaceId Id of interface whose IP should be deleted
828 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500829 * @param[io] asyncResp Response object that will be returned to client
830 *
831 * @return None
832 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500833inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800834 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500835{
836 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700837 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500838 if (ec)
839 {
840 messages::internalError(asyncResp->res);
841 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500842 },
843 "xyz.openbmc_project.Network",
844 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
845 "xyz.openbmc_project.Object.Delete", "Delete");
846}
847
848/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700849 * @brief Deletes the IPv6 entry for this interface and creates a replacement
850 * static IPv6 entry
851 *
852 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
853 * @param[in] id The unique hash entry identifying the DBus entry
854 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
855 * @param[in] address IPv6 address to assign to this interface
856 * @param[io] asyncResp Response object that will be returned to client
857 *
858 * @return None
859 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800860inline void
861 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
862 uint8_t prefixLength, const std::string& address,
863 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700864{
865 crow::connections::systemBus->async_method_call(
866 [asyncResp, ifaceId, address,
867 prefixLength](const boost::system::error_code ec) {
868 if (ec)
869 {
870 messages::internalError(asyncResp->res);
871 }
872 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +0000873 [asyncResp](const boost::system::error_code ec2) {
874 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700875 {
876 messages::internalError(asyncResp->res);
877 }
878 },
879 "xyz.openbmc_project.Network",
880 "/xyz/openbmc_project/network/" + ifaceId,
881 "xyz.openbmc_project.Network.IP.Create", "IP",
882 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
883 prefixLength, "");
884 },
885 "xyz.openbmc_project.Network",
886 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
887 "xyz.openbmc_project.Object.Delete", "Delete");
888}
889
890/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500891 * @brief Creates IPv6 with given data
892 *
893 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500894 * @param[in] prefixLength Prefix length that needs to be added
895 * @param[in] address IP address that needs to be added
896 * @param[io] asyncResp Response object that will be returned to client
897 *
898 * @return None
899 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500900inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
901 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800902 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500903{
904 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
905 if (ec)
906 {
907 messages::internalError(asyncResp->res);
908 }
909 };
910 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500911 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500912 crow::connections::systemBus->async_method_call(
913 std::move(createIpHandler), "xyz.openbmc_project.Network",
914 "/xyz/openbmc_project/network/" + ifaceId,
915 "xyz.openbmc_project.Network.IP.Create", "IP",
916 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
917 "");
918}
919
Ed Tanous4a0cb852018-10-15 07:55:04 -0700920/**
921 * Function that retrieves all properties for given Ethernet Interface
922 * Object
923 * from EntityManager Network Manager
924 * @param ethiface_id a eth interface id to query on DBus
925 * @param callback a function that shall be called to convert Dbus output
926 * into JSON
927 */
928template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000929void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500930 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700931{
932 crow::connections::systemBus->async_method_call(
Ed Tanous81ce6092020-12-17 16:54:55 +0000933 [ethifaceId{std::string{ethifaceId}}, callback{std::move(callback)}](
934 const boost::system::error_code errorCode,
Ed Tanousf23b7292020-10-15 09:41:17 -0700935 GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700936 EthernetInterfaceData ethData{};
937 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500938 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700939
Ed Tanous81ce6092020-12-17 16:54:55 +0000940 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700941 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700942 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700943 return;
944 }
945
Ed Tanous4c9afe42019-05-03 16:59:57 -0700946 bool found =
Ed Tanous2c70f802020-09-28 14:29:23 -0700947 extractEthernetInterfaceData(ethifaceId, resp, ethData);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700948 if (!found)
949 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700950 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700951 return;
952 }
953
Ed Tanous2c70f802020-09-28 14:29:23 -0700954 extractIPData(ethifaceId, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700955 // Fix global GW
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500956 for (IPv4AddressData& ipv4 : ipv4Data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700957 {
Ravi Tejac6191412019-07-30 00:53:50 -0500958 if (((ipv4.linktype == LinkType::Global) &&
959 (ipv4.gateway == "0.0.0.0")) ||
Ravi Teja9010ec22019-08-01 23:30:25 -0500960 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700961 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700962 ipv4.gateway = ethData.default_gateway;
963 }
964 }
965
Ed Tanous2c70f802020-09-28 14:29:23 -0700966 extractIPV6Data(ethifaceId, resp, ipv6Data);
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500967 // Finally make a callback with useful data
Johnathan Mantey01784822019-06-18 12:44:21 -0700968 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700969 },
970 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
971 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700972}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700973
974/**
975 * Function that retrieves all Ethernet Interfaces available through Network
976 * Manager
977 * @param callback a function that shall be called to convert Dbus output
978 * into JSON.
979 */
980template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500981void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700982{
983 crow::connections::systemBus->async_method_call(
984 [callback{std::move(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000985 const boost::system::error_code errorCode,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500986 GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700987 // Callback requires vector<string> to retrieve all available
988 // ethernet interfaces
Ed Tanous2c70f802020-09-28 14:29:23 -0700989 boost::container::flat_set<std::string> ifaceList;
990 ifaceList.reserve(resp.size());
Ed Tanous81ce6092020-12-17 16:54:55 +0000991 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700992 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700993 callback(false, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700994 return;
995 }
996
997 // Iterate over all retrieved ObjectPaths.
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500998 for (const auto& objpath : resp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700999 {
1000 // And all interfaces available for certain ObjectPath.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001001 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001002 {
1003 // If interface is
1004 // xyz.openbmc_project.Network.EthernetInterface, this is
1005 // what we're looking for.
1006 if (interface.first ==
1007 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001008 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001009 std::string ifaceId = objpath.first.filename();
1010 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001011 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001012 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001013 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001014 // and put it into output vector.
1015 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001016 }
1017 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001018 }
1019 // Finally make a callback with useful data
Ed Tanous2c70f802020-09-28 14:29:23 -07001020 callback(true, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001021 },
1022 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1023 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -07001024}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001025
1026/**
1027 * EthernetCollection derived class for delivering Ethernet Collection Schema
1028 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001029class EthernetCollection : public Node
1030{
1031 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001032 EthernetCollection(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001033 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001034 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001035 entityPrivileges = {
1036 {boost::beast::http::verb::get, {{"Login"}}},
1037 {boost::beast::http::verb::head, {{"Login"}}},
1038 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1039 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1040 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1041 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1042 }
1043
1044 private:
1045 /**
1046 * Functions triggers appropriate requests on DBus
1047 */
zhanghch058d1b46d2021-04-01 11:18:24 +08001048 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1049 const crow::Request&, const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001050 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001051 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanous0f74e642018-11-12 15:17:05 -08001052 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
zhanghch058d1b46d2021-04-01 11:18:24 +08001053 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous0f74e642018-11-12 15:17:05 -08001054 "/redfish/v1/Managers/bmc/EthernetInterfaces";
zhanghch058d1b46d2021-04-01 11:18:24 +08001055 asyncResp->res.jsonValue["Name"] =
1056 "Ethernet Network Interface Collection";
1057 asyncResp->res.jsonValue["Description"] =
Ed Tanous0f74e642018-11-12 15:17:05 -08001058 "Collection of EthernetInterfaces for this Manager";
zhanghch058d1b46d2021-04-01 11:18:24 +08001059
Ed Tanous4a0cb852018-10-15 07:55:04 -07001060 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001061 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001062 getEthernetIfaceList(
Ed Tanous4c9afe42019-05-03 16:59:57 -07001063 [asyncResp](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001064 const bool& success,
Ed Tanous81ce6092020-12-17 16:54:55 +00001065 const boost::container::flat_set<std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001066 if (!success)
1067 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001068 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001069 return;
1070 }
1071
Ed Tanous2c70f802020-09-28 14:29:23 -07001072 nlohmann::json& ifaceArray =
Ed Tanous4c9afe42019-05-03 16:59:57 -07001073 asyncResp->res.jsonValue["Members"];
Ed Tanous2c70f802020-09-28 14:29:23 -07001074 ifaceArray = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001075 std::string tag = "_";
Ed Tanous81ce6092020-12-17 16:54:55 +00001076 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001077 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001078 std::size_t found = ifaceItem.find(tag);
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001079 if (found == std::string::npos)
1080 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001081 ifaceArray.push_back(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001082 {{"@odata.id",
1083 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
Ed Tanous2c70f802020-09-28 14:29:23 -07001084 ifaceItem}});
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001085 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001086 }
1087
Ed Tanous4c9afe42019-05-03 16:59:57 -07001088 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07001089 ifaceArray.size();
Ed Tanous4c9afe42019-05-03 16:59:57 -07001090 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001091 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001092 });
Ed Tanous4a0cb852018-10-15 07:55:04 -07001093 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001094};
1095
1096/**
1097 * EthernetInterface derived class for delivering Ethernet Schema
1098 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001099class EthernetInterface : public Node
1100{
1101 public:
1102 /*
1103 * Default Constructor
1104 */
Ed Tanous52cc1122020-07-18 13:51:21 -07001105 EthernetInterface(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001106 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -07001107 std::string())
1108 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001109 entityPrivileges = {
1110 {boost::beast::http::verb::get, {{"Login"}}},
1111 {boost::beast::http::verb::head, {{"Login"}}},
1112 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1113 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1114 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1115 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02001116 }
1117
Ed Tanous1abe55e2018-09-05 08:30:59 -07001118 private:
zhanghch058d1b46d2021-04-01 11:18:24 +08001119 void
1120 handleHostnamePatch(const std::string& hostname,
1121 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001122 {
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301123 // SHOULD handle host names of up to 255 characters(RFC 1123)
1124 if (hostname.length() > 255)
1125 {
1126 messages::propertyValueFormatError(asyncResp->res, hostname,
1127 "HostName");
1128 return;
1129 }
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001130 crow::connections::systemBus->async_method_call(
1131 [asyncResp](const boost::system::error_code ec) {
1132 if (ec)
1133 {
1134 messages::internalError(asyncResp->res);
1135 }
1136 },
1137 "xyz.openbmc_project.Network",
1138 "/xyz/openbmc_project/network/config",
1139 "org.freedesktop.DBus.Properties", "Set",
1140 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanousabf2add2019-01-22 16:40:12 -08001141 std::variant<std::string>(hostname));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001142 }
1143
zhanghch058d1b46d2021-04-01 11:18:24 +08001144 void handleDomainnamePatch(
1145 const std::string& ifaceId, const std::string& domainname,
1146 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301147 {
1148 std::vector<std::string> vectorDomainname = {domainname};
1149 crow::connections::systemBus->async_method_call(
1150 [asyncResp](const boost::system::error_code ec) {
1151 if (ec)
1152 {
1153 messages::internalError(asyncResp->res);
1154 }
1155 },
1156 "xyz.openbmc_project.Network",
1157 "/xyz/openbmc_project/network/" + ifaceId,
1158 "org.freedesktop.DBus.Properties", "Set",
1159 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1160 std::variant<std::vector<std::string>>(vectorDomainname));
1161 }
1162
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001163 void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
zhanghch058d1b46d2021-04-01 11:18:24 +08001164 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301165 {
1166 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1167 if (fqdn.length() > 255)
1168 {
1169 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1170 return;
1171 }
1172
1173 size_t pos = fqdn.find('.');
1174 if (pos == std::string::npos)
1175 {
1176 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1177 return;
1178 }
1179
1180 std::string hostname;
1181 std::string domainname;
1182 domainname = (fqdn).substr(pos + 1);
1183 hostname = (fqdn).substr(0, pos);
1184
1185 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1186 {
1187 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1188 return;
1189 }
1190
1191 handleHostnamePatch(hostname, asyncResp);
1192 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1193 }
1194
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001195 bool isHostnameValid(const std::string& hostname)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301196 {
1197 // A valid host name can never have the dotted-decimal form (RFC 1123)
1198 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1199 {
1200 return false;
1201 }
1202 // Each label(hostname/subdomains) within a valid FQDN
1203 // MUST handle host names of up to 63 characters (RFC 1123)
1204 // labels cannot start or end with hyphens (RFC 952)
1205 // labels can start with numbers (RFC 1123)
1206 const std::regex pattern(
1207 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1208
1209 return std::regex_match(hostname, pattern);
1210 }
1211
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001212 bool isDomainnameValid(const std::string& domainname)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301213 {
1214 // Can have multiple subdomains
1215 // Top Level Domain's min length is 2 character
1216 const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]"
1217 "{1,30}\\.)*[a-zA-Z]{2,}$");
1218
1219 return std::regex_match(domainname, pattern);
1220 }
1221
zhanghch058d1b46d2021-04-01 11:18:24 +08001222 void handleMACAddressPatch(
1223 const std::string& ifaceId, const std::string& macAddress,
1224 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ratan Guptad5776652019-03-03 08:47:22 +05301225 {
1226 crow::connections::systemBus->async_method_call(
1227 [asyncResp, macAddress](const boost::system::error_code ec) {
1228 if (ec)
1229 {
1230 messages::internalError(asyncResp->res);
1231 return;
1232 }
Ratan Guptad5776652019-03-03 08:47:22 +05301233 },
1234 "xyz.openbmc_project.Network",
1235 "/xyz/openbmc_project/network/" + ifaceId,
1236 "org.freedesktop.DBus.Properties", "Set",
1237 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1238 std::variant<std::string>(macAddress));
1239 }
Johnathan Mantey286b9112019-06-10 13:38:04 -07001240
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001241 void setDHCPEnabled(const std::string& ifaceId,
1242 const std::string& propertyName, const bool v4Value,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001243 const bool v6Value,
zhanghch058d1b46d2021-04-01 11:18:24 +08001244 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001245 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001246 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
Jennifer Leeda131a92019-04-24 15:13:55 -07001247 crow::connections::systemBus->async_method_call(
1248 [asyncResp](const boost::system::error_code ec) {
1249 if (ec)
1250 {
1251 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1252 messages::internalError(asyncResp->res);
1253 return;
1254 }
Jayaprakash Mutyala8f7e9c12021-03-22 10:08:00 +00001255 messages::success(asyncResp->res);
Jennifer Leeda131a92019-04-24 15:13:55 -07001256 },
1257 "xyz.openbmc_project.Network",
1258 "/xyz/openbmc_project/network/" + ifaceId,
1259 "org.freedesktop.DBus.Properties", "Set",
1260 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001261 std::variant<std::string>{dhcp});
Jennifer Leeda131a92019-04-24 15:13:55 -07001262 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001263
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001264 void setEthernetInterfaceBoolProperty(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001265 const std::string& ifaceId, const std::string& propertyName,
zhanghch058d1b46d2021-04-01 11:18:24 +08001266 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001267 {
1268 crow::connections::systemBus->async_method_call(
1269 [asyncResp](const boost::system::error_code ec) {
1270 if (ec)
1271 {
1272 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1273 messages::internalError(asyncResp->res);
1274 return;
1275 }
1276 },
1277 "xyz.openbmc_project.Network",
1278 "/xyz/openbmc_project/network/" + ifaceId,
1279 "org.freedesktop.DBus.Properties", "Set",
1280 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1281 std::variant<bool>{value});
1282 }
1283
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001284 void setDHCPv4Config(const std::string& propertyName, const bool& value,
zhanghch058d1b46d2021-04-01 11:18:24 +08001285 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001286 {
1287 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1288 crow::connections::systemBus->async_method_call(
1289 [asyncResp](const boost::system::error_code ec) {
1290 if (ec)
1291 {
1292 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1293 messages::internalError(asyncResp->res);
1294 return;
1295 }
1296 },
1297 "xyz.openbmc_project.Network",
1298 "/xyz/openbmc_project/network/config/dhcp",
1299 "org.freedesktop.DBus.Properties", "Set",
1300 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1301 std::variant<bool>{value});
1302 }
Ratan Guptad5776652019-03-03 08:47:22 +05301303
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001304 void handleDHCPPatch(const std::string& ifaceId,
1305 const EthernetInterfaceData& ethData,
Ed Tanousf23b7292020-10-15 09:41:17 -07001306 const DHCPParameters& v4dhcpParms,
1307 const DHCPParameters& v6dhcpParms,
zhanghch058d1b46d2021-04-01 11:18:24 +08001308 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001309 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001310 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1311 bool ipv6Active =
1312 translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
Jennifer Leeda131a92019-04-24 15:13:55 -07001313
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001314 bool nextv4DHCPState =
1315 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1316
1317 bool nextv6DHCPState{};
1318 if (v6dhcpParms.dhcpv6OperatingMode)
Jennifer Leeda131a92019-04-24 15:13:55 -07001319 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001320 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1321 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1322 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1323 {
1324 messages::propertyValueFormatError(
1325 asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
1326 "OperatingMode");
1327 return;
1328 }
1329 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1330 }
1331 else
1332 {
1333 nextv6DHCPState = ipv6Active;
Jennifer Leeda131a92019-04-24 15:13:55 -07001334 }
1335
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001336 bool nextDNS{};
1337 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001338 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001339 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1340 {
1341 messages::generalError(asyncResp->res);
1342 return;
1343 }
1344 nextDNS = *v4dhcpParms.useDNSServers;
1345 }
1346 else if (v4dhcpParms.useDNSServers)
1347 {
1348 nextDNS = *v4dhcpParms.useDNSServers;
1349 }
1350 else if (v6dhcpParms.useDNSServers)
1351 {
1352 nextDNS = *v6dhcpParms.useDNSServers;
1353 }
1354 else
1355 {
1356 nextDNS = ethData.DNSEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001357 }
1358
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001359 bool nextNTP{};
1360 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001361 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001362 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1363 {
1364 messages::generalError(asyncResp->res);
1365 return;
1366 }
1367 nextNTP = *v4dhcpParms.useNTPServers;
1368 }
1369 else if (v4dhcpParms.useNTPServers)
1370 {
1371 nextNTP = *v4dhcpParms.useNTPServers;
1372 }
1373 else if (v6dhcpParms.useNTPServers)
1374 {
1375 nextNTP = *v6dhcpParms.useNTPServers;
1376 }
1377 else
1378 {
1379 nextNTP = ethData.NTPEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001380 }
1381
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001382 bool nextUseDomain{};
1383 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
Jennifer Leeda131a92019-04-24 15:13:55 -07001384 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001385 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1386 {
1387 messages::generalError(asyncResp->res);
1388 return;
1389 }
1390 nextUseDomain = *v4dhcpParms.useUseDomainName;
1391 }
1392 else if (v4dhcpParms.useUseDomainName)
1393 {
1394 nextUseDomain = *v4dhcpParms.useUseDomainName;
1395 }
1396 else if (v6dhcpParms.useUseDomainName)
1397 {
1398 nextUseDomain = *v6dhcpParms.useUseDomainName;
1399 }
1400 else
1401 {
1402 nextUseDomain = ethData.HostNameEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001403 }
1404
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001405 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1406 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1407 asyncResp);
1408 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1409 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1410 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1411 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1412 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1413 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
Jennifer Leeda131a92019-04-24 15:13:55 -07001414 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001415
1416 boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanous2c70f802020-09-28 14:29:23 -07001417 getNextStaticIpEntry(
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301418 const boost::container::flat_set<IPv4AddressData>::const_iterator&
1419 head,
Ed Tanousb5a76932020-09-29 16:16:58 -07001420 const boost::container::flat_set<IPv4AddressData>::const_iterator&
1421 end)
Johnathan Mantey01784822019-06-18 12:44:21 -07001422 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301423 return std::find_if(head, end, [](const IPv4AddressData& value) {
1424 return value.origin == "Static";
1425 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001426 }
1427
1428 boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanous2c70f802020-09-28 14:29:23 -07001429 getNextStaticIpEntry(
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301430 const boost::container::flat_set<IPv6AddressData>::const_iterator&
1431 head,
Ed Tanousb5a76932020-09-29 16:16:58 -07001432 const boost::container::flat_set<IPv6AddressData>::const_iterator&
1433 end)
Johnathan Mantey01784822019-06-18 12:44:21 -07001434 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301435 return std::find_if(head, end, [](const IPv6AddressData& value) {
1436 return value.origin == "Static";
1437 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001438 }
1439
Ravi Tejad1d50812019-06-23 16:20:27 -05001440 void handleIPv4StaticPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001441 const std::string& ifaceId, nlohmann::json& input,
1442 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
zhanghch058d1b46d2021-04-01 11:18:24 +08001443 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001444 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001445 if ((!input.is_array()) || input.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301446 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001447 messages::propertyValueTypeError(
1448 asyncResp->res,
1449 input.dump(2, ' ', true,
1450 nlohmann::json::error_handler_t::replace),
1451 "IPv4StaticAddresses");
Ratan Guptaf476acb2019-03-02 16:46:57 +05301452 return;
1453 }
1454
Ed Tanous271584a2019-07-09 16:24:22 -07001455 unsigned entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001456 // Find the first static IP address currently active on the NIC and
1457 // match it to the first JSON element in the IPv4StaticAddresses array.
1458 // Match each subsequent JSON element to the next static IP programmed
1459 // into the NIC.
Ed Tanous2c70f802020-09-28 14:29:23 -07001460 boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry =
1461 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
Johnathan Mantey01784822019-06-18 12:44:21 -07001462
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001463 for (nlohmann::json& thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001464 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001465 std::string pathString =
Ravi Tejad1d50812019-06-23 16:20:27 -05001466 "IPv4StaticAddresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001467
Johnathan Mantey01784822019-06-18 12:44:21 -07001468 if (!thisJson.is_null() && !thisJson.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301469 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001470 std::optional<std::string> address;
1471 std::optional<std::string> subnetMask;
1472 std::optional<std::string> gateway;
1473
1474 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1475 address, "SubnetMask", subnetMask,
1476 "Gateway", gateway))
Ratan Guptaf476acb2019-03-02 16:46:57 +05301477 {
1478 messages::propertyValueFormatError(
Ed Tanous71f52d92021-02-19 08:51:17 -08001479 asyncResp->res,
1480 thisJson.dump(2, ' ', true,
1481 nlohmann::json::error_handler_t::replace),
1482 pathString);
Ratan Guptaf476acb2019-03-02 16:46:57 +05301483 return;
Ratan Guptaf476acb2019-03-02 16:46:57 +05301484 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301485
Johnathan Mantey01784822019-06-18 12:44:21 -07001486 // Find the address/subnet/gateway values. Any values that are
1487 // not explicitly provided are assumed to be unmodified from the
1488 // current state of the interface. Merge existing state into the
1489 // current request.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001490 const std::string* addr = nullptr;
1491 const std::string* gw = nullptr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001492 uint8_t prefixLength = 0;
1493 bool errorInEntry = false;
1494 if (address)
Ratan Gupta9474b372019-03-01 15:13:37 +05301495 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001496 if (ipv4VerifyIpAndGetBitcount(*address))
1497 {
1498 addr = &(*address);
1499 }
1500 else
1501 {
1502 messages::propertyValueFormatError(
1503 asyncResp->res, *address, pathString + "/Address");
1504 errorInEntry = true;
1505 }
1506 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001507 else if (niciPentry != ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001508 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001509 addr = &(niciPentry->address);
Ratan Gupta9474b372019-03-01 15:13:37 +05301510 }
1511 else
1512 {
1513 messages::propertyMissing(asyncResp->res,
1514 pathString + "/Address");
Johnathan Mantey01784822019-06-18 12:44:21 -07001515 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001516 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301517
1518 if (subnetMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001519 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001520 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1521 {
1522 messages::propertyValueFormatError(
1523 asyncResp->res, *subnetMask,
1524 pathString + "/SubnetMask");
1525 errorInEntry = true;
1526 }
1527 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001528 else if (niciPentry != ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001529 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001530 if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask,
Johnathan Mantey01784822019-06-18 12:44:21 -07001531 &prefixLength))
1532 {
1533 messages::propertyValueFormatError(
Ed Tanous2c70f802020-09-28 14:29:23 -07001534 asyncResp->res, niciPentry->netmask,
Johnathan Mantey01784822019-06-18 12:44:21 -07001535 pathString + "/SubnetMask");
1536 errorInEntry = true;
1537 }
1538 }
1539 else
1540 {
1541 messages::propertyMissing(asyncResp->res,
1542 pathString + "/SubnetMask");
1543 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001544 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301545
Ratan Guptaf476acb2019-03-02 16:46:57 +05301546 if (gateway)
1547 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001548 if (ipv4VerifyIpAndGetBitcount(*gateway))
1549 {
1550 gw = &(*gateway);
1551 }
1552 else
1553 {
1554 messages::propertyValueFormatError(
1555 asyncResp->res, *gateway, pathString + "/Gateway");
1556 errorInEntry = true;
1557 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301558 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001559 else if (niciPentry != ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001560 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001561 gw = &niciPentry->gateway;
Johnathan Mantey01784822019-06-18 12:44:21 -07001562 }
1563 else
Ed Tanous4a0cb852018-10-15 07:55:04 -07001564 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001565 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001566 pathString + "/Gateway");
Johnathan Mantey01784822019-06-18 12:44:21 -07001567 errorInEntry = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001568 }
1569
Johnathan Mantey01784822019-06-18 12:44:21 -07001570 if (errorInEntry)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001571 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001572 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001573 }
1574
Ed Tanous2c70f802020-09-28 14:29:23 -07001575 if (niciPentry != ipv4Data.cend())
Ed Tanous4a0cb852018-10-15 07:55:04 -07001576 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001577 deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength,
Johnathan Mantey01784822019-06-18 12:44:21 -07001578 *gw, *addr, asyncResp);
Ed Tanous2c70f802020-09-28 14:29:23 -07001579 niciPentry =
1580 getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
Ed Tanous4a0cb852018-10-15 07:55:04 -07001581 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001582 else
1583 {
Ed Tanouscb13a392020-07-25 19:02:03 +00001584 createIPv4(ifaceId, prefixLength, *gateway, *address,
1585 asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -07001586 }
1587 entryIdx++;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001588 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001589 else
1590 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001591 if (niciPentry == ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001592 {
1593 // Requesting a DELETE/DO NOT MODIFY action for an item
1594 // that isn't present on the eth(n) interface. Input JSON is
1595 // in error, so bail out.
1596 if (thisJson.is_null())
1597 {
1598 messages::resourceCannotBeDeleted(asyncResp->res);
1599 return;
1600 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001601 messages::propertyValueFormatError(
Ed Tanous71f52d92021-02-19 08:51:17 -08001602 asyncResp->res,
1603 thisJson.dump(2, ' ', true,
1604 nlohmann::json::error_handler_t::replace),
1605 pathString);
Ed Tanous3174e4d2020-10-07 11:41:22 -07001606 return;
Johnathan Mantey01784822019-06-18 12:44:21 -07001607 }
1608
1609 if (thisJson.is_null())
1610 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001611 deleteIPv4(ifaceId, niciPentry->id, asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -07001612 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001613 if (niciPentry != ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001614 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001615 niciPentry =
1616 getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
Johnathan Mantey01784822019-06-18 12:44:21 -07001617 }
1618 entryIdx++;
1619 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001620 }
1621 }
1622
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001623 void handleStaticNameServersPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001624 const std::string& ifaceId,
1625 const std::vector<std::string>& updatedStaticNameServers,
zhanghch058d1b46d2021-04-01 11:18:24 +08001626 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001627 {
1628 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -07001629 [asyncResp](const boost::system::error_code ec) {
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001630 if (ec)
1631 {
1632 messages::internalError(asyncResp->res);
1633 return;
1634 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001635 },
1636 "xyz.openbmc_project.Network",
1637 "/xyz/openbmc_project/network/" + ifaceId,
1638 "org.freedesktop.DBus.Properties", "Set",
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -05001639 "xyz.openbmc_project.Network.EthernetInterface",
1640 "StaticNameServers",
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001641 std::variant<std::vector<std::string>>{updatedStaticNameServers});
1642 }
1643
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001644 void handleIPv6StaticAddressesPatch(
Ed Tanousf23b7292020-10-15 09:41:17 -07001645 const std::string& ifaceId, const nlohmann::json& input,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001646 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
zhanghch058d1b46d2021-04-01 11:18:24 +08001647 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001648 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001649 if (!input.is_array() || input.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001650 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001651 messages::propertyValueTypeError(
1652 asyncResp->res,
1653 input.dump(2, ' ', true,
1654 nlohmann::json::error_handler_t::replace),
1655 "IPv6StaticAddresses");
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001656 return;
1657 }
Ed Tanous271584a2019-07-09 16:24:22 -07001658 size_t entryIdx = 1;
Ed Tanous2c70f802020-09-28 14:29:23 -07001659 boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry =
1660 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
Ed Tanousf23b7292020-10-15 09:41:17 -07001661 for (const nlohmann::json& thisJson : input)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001662 {
1663 std::string pathString =
1664 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1665
Johnathan Mantey01784822019-06-18 12:44:21 -07001666 if (!thisJson.is_null() && !thisJson.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001667 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001668 std::optional<std::string> address;
1669 std::optional<uint8_t> prefixLength;
Ed Tanousf23b7292020-10-15 09:41:17 -07001670 nlohmann::json thisJsonCopy = thisJson;
1671 if (!json_util::readJson(thisJsonCopy, asyncResp->res,
1672 "Address", address, "PrefixLength",
1673 prefixLength))
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001674 {
1675 messages::propertyValueFormatError(
Ed Tanous71f52d92021-02-19 08:51:17 -08001676 asyncResp->res,
1677 thisJson.dump(2, ' ', true,
1678 nlohmann::json::error_handler_t::replace),
1679 pathString);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001680 return;
1681 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001682
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001683 const std::string* addr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001684 uint8_t prefix;
1685
1686 // Find the address and prefixLength values. Any values that are
1687 // not explicitly provided are assumed to be unmodified from the
1688 // current state of the interface. Merge existing state into the
1689 // current request.
1690 if (address)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001691 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001692 addr = &(*address);
1693 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001694 else if (niciPentry != ipv6Data.end())
Johnathan Mantey01784822019-06-18 12:44:21 -07001695 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001696 addr = &(niciPentry->address);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001697 }
1698 else
1699 {
1700 messages::propertyMissing(asyncResp->res,
1701 pathString + "/Address");
1702 return;
1703 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001704
1705 if (prefixLength)
1706 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001707 prefix = *prefixLength;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001708 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001709 else if (niciPentry != ipv6Data.end())
Johnathan Mantey01784822019-06-18 12:44:21 -07001710 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001711 prefix = niciPentry->prefixLength;
Johnathan Mantey01784822019-06-18 12:44:21 -07001712 }
1713 else
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001714 {
1715 messages::propertyMissing(asyncResp->res,
1716 pathString + "/PrefixLength");
Johnathan Mantey01784822019-06-18 12:44:21 -07001717 return;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001718 }
1719
Ed Tanous2c70f802020-09-28 14:29:23 -07001720 if (niciPentry != ipv6Data.end())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001721 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001722 deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr,
Johnathan Mantey01784822019-06-18 12:44:21 -07001723 asyncResp);
Ed Tanous2c70f802020-09-28 14:29:23 -07001724 niciPentry =
1725 getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
Johnathan Mantey01784822019-06-18 12:44:21 -07001726 }
1727 else
1728 {
1729 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1730 }
1731 entryIdx++;
1732 }
1733 else
1734 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001735 if (niciPentry == ipv6Data.end())
Johnathan Mantey01784822019-06-18 12:44:21 -07001736 {
1737 // Requesting a DELETE/DO NOT MODIFY action for an item
1738 // that isn't present on the eth(n) interface. Input JSON is
1739 // in error, so bail out.
1740 if (thisJson.is_null())
1741 {
1742 messages::resourceCannotBeDeleted(asyncResp->res);
1743 return;
1744 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001745 messages::propertyValueFormatError(
Ed Tanous71f52d92021-02-19 08:51:17 -08001746 asyncResp->res,
1747 thisJson.dump(2, ' ', true,
1748 nlohmann::json::error_handler_t::replace),
1749 pathString);
Ed Tanous3174e4d2020-10-07 11:41:22 -07001750 return;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001751 }
1752
Johnathan Mantey01784822019-06-18 12:44:21 -07001753 if (thisJson.is_null())
1754 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001755 deleteIPv6(ifaceId, niciPentry->id, asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -07001756 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001757 if (niciPentry != ipv6Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001758 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001759 niciPentry =
1760 getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
Johnathan Mantey01784822019-06-18 12:44:21 -07001761 }
1762 entryIdx++;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001763 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001764 }
1765 }
1766
Ed Tanous0f74e642018-11-12 15:17:05 -08001767 void parseInterfaceData(
zhanghch058d1b46d2021-04-01 11:18:24 +08001768 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1769 const std::string& ifaceId, const EthernetInterfaceData& ethData,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001770 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1771 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001772 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001773 constexpr const std::array<const char*, 1> inventoryForEthernet = {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001774 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1775
Ed Tanous2c70f802020-09-28 14:29:23 -07001776 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
Ed Tanous81ce6092020-12-17 16:54:55 +00001777 jsonResponse["Id"] = ifaceId;
Ed Tanous2c70f802020-09-28 14:29:23 -07001778 jsonResponse["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00001779 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
Ed Tanous2c70f802020-09-28 14:29:23 -07001780 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001781
1782 auto health = std::make_shared<HealthPopulate>(asyncResp);
1783
1784 crow::connections::systemBus->async_method_call(
1785 [health](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001786 std::vector<std::string>& resp) {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001787 if (ec)
1788 {
1789 return;
1790 }
1791
1792 health->inventory = std::move(resp);
1793 },
1794 "xyz.openbmc_project.ObjectMapper",
1795 "/xyz/openbmc_project/object_mapper",
1796 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1797 int32_t(0), inventoryForEthernet);
1798
1799 health->populate();
1800
1801 if (ethData.nicEnabled)
Ed Tanous029573d2019-02-01 10:57:49 -08001802 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001803 jsonResponse["LinkStatus"] = "LinkUp";
1804 jsonResponse["Status"]["State"] = "Enabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001805 }
1806 else
1807 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001808 jsonResponse["LinkStatus"] = "NoLink";
1809 jsonResponse["Status"]["State"] = "Disabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001810 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08001811
Ed Tanous2c70f802020-09-28 14:29:23 -07001812 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
1813 jsonResponse["SpeedMbps"] = ethData.speed;
1814 jsonResponse["MACAddress"] = ethData.mac_address;
1815 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001816 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
Ed Tanous2c70f802020-09-28 14:29:23 -07001817 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1818 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1819 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001820
Ed Tanous2c70f802020-09-28 14:29:23 -07001821 jsonResponse["DHCPv6"]["OperatingMode"] =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001822 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1823 : "Disabled";
Ed Tanous2c70f802020-09-28 14:29:23 -07001824 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1825 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1826 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +05301827
Ed Tanous4a0cb852018-10-15 07:55:04 -07001828 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001829 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001830 jsonResponse["HostName"] = ethData.hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301831
1832 // When domain name is empty then it means, that it is a network
1833 // without domain names, and the host name itself must be treated as
1834 // FQDN
Ed Tanousf23b7292020-10-15 09:41:17 -07001835 std::string fqdn = ethData.hostname;
Jennifer Leed24bfc72019-03-05 13:03:37 -08001836 if (!ethData.domainnames.empty())
1837 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001838 fqdn += "." + ethData.domainnames[0];
Jennifer Leed24bfc72019-03-05 13:03:37 -08001839 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001840 jsonResponse["FQDN"] = fqdn;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001841 }
1842
Ed Tanous2c70f802020-09-28 14:29:23 -07001843 jsonResponse["VLANs"] = {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001844 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00001845 ifaceId + "/VLANs"}};
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001846
Ed Tanous2c70f802020-09-28 14:29:23 -07001847 jsonResponse["NameServers"] = ethData.nameServers;
1848 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001849
Ed Tanous2c70f802020-09-28 14:29:23 -07001850 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1851 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1852 ipv4Array = nlohmann::json::array();
1853 ipv4StaticArray = nlohmann::json::array();
1854 for (auto& ipv4Config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001855 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001856
Ed Tanous2c70f802020-09-28 14:29:23 -07001857 std::string gatewayStr = ipv4Config.gateway;
Ravi Tejad1d50812019-06-23 16:20:27 -05001858 if (gatewayStr.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001859 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001860 gatewayStr = "0.0.0.0";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001861 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001862
Ed Tanous2c70f802020-09-28 14:29:23 -07001863 ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
1864 {"SubnetMask", ipv4Config.netmask},
1865 {"Address", ipv4Config.address},
1866 {"Gateway", gatewayStr}});
1867 if (ipv4Config.origin == "Static")
Ravi Tejad1d50812019-06-23 16:20:27 -05001868 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001869 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
1870 {"SubnetMask", ipv4Config.netmask},
1871 {"Address", ipv4Config.address},
1872 {"Gateway", gatewayStr}});
Ravi Tejad1d50812019-06-23 16:20:27 -05001873 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001874 }
1875
Ravi Teja7ea79e52021-03-02 08:38:05 +05301876 std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
1877 if (ipv6GatewayStr.empty())
1878 {
1879 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1880 }
1881
1882 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001883
Ed Tanous2c70f802020-09-28 14:29:23 -07001884 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1885 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1886 ipv6Array = nlohmann::json::array();
1887 ipv6StaticArray = nlohmann::json::array();
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001888 nlohmann::json& ipv6AddrPolicyTable =
Ed Tanous2c70f802020-09-28 14:29:23 -07001889 jsonResponse["IPv6AddressPolicyTable"];
Johnathan Mantey7f2e23e2020-05-14 13:36:52 -07001890 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous2c70f802020-09-28 14:29:23 -07001891 for (auto& ipv6Config : ipv6Data)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001892 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001893 ipv6Array.push_back({{"Address", ipv6Config.address},
1894 {"PrefixLength", ipv6Config.prefixLength},
1895 {"AddressOrigin", ipv6Config.origin},
1896 {"AddressState", nullptr}});
1897 if (ipv6Config.origin == "Static")
Johnathan Mantey01784822019-06-18 12:44:21 -07001898 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001899 ipv6StaticArray.push_back(
1900 {{"Address", ipv6Config.address},
1901 {"PrefixLength", ipv6Config.prefixLength},
1902 {"AddressOrigin", ipv6Config.origin},
Johnathan Mantey5fd16e42020-05-13 15:14:47 -07001903 {"AddressState", nullptr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001904 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001905 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001906 }
1907
1908 /**
1909 * Functions triggers appropriate requests on DBus
1910 */
zhanghch058d1b46d2021-04-01 11:18:24 +08001911 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1912 const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001913 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001914 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001915 if (params.size() != 1)
1916 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001917 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001918 return;
1919 }
1920
Ed Tanous4a0cb852018-10-15 07:55:04 -07001921 getEthernetIfaceData(
1922 params[0],
Ed Tanous2c70f802020-09-28 14:29:23 -07001923 [this, asyncResp, ifaceId{std::string(params[0])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001924 const bool& success, const EthernetInterfaceData& ethData,
1925 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1926 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001927 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001928 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001929 // TODO(Pawel)consider distinguish between non existing
1930 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001931 messages::resourceNotFound(asyncResp->res,
Ed Tanous2c70f802020-09-28 14:29:23 -07001932 "EthernetInterface", ifaceId);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001933 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001934 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07001935
Ed Tanous0f74e642018-11-12 15:17:05 -08001936 asyncResp->res.jsonValue["@odata.type"] =
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001937 "#EthernetInterface.v1_4_1.EthernetInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001938 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1939 asyncResp->res.jsonValue["Description"] =
1940 "Management Network Interface";
1941
Ed Tanous2c70f802020-09-28 14:29:23 -07001942 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data,
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001943 ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001944 });
1945 }
1946
zhanghch058d1b46d2021-04-01 11:18:24 +08001947 void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1948 const crow::Request& req,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001949 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001950 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001951
Ed Tanous1abe55e2018-09-05 08:30:59 -07001952 if (params.size() != 1)
1953 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001954 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001955 return;
1956 }
1957
Ed Tanous2c70f802020-09-28 14:29:23 -07001958 const std::string& ifaceId = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001959
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001960 std::optional<std::string> hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301961 std::optional<std::string> fqdn;
Ratan Guptad5776652019-03-03 08:47:22 +05301962 std::optional<std::string> macAddress;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001963 std::optional<std::string> ipv6DefaultGateway;
Ravi Tejad1d50812019-06-23 16:20:27 -05001964 std::optional<nlohmann::json> ipv4StaticAddresses;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001965 std::optional<nlohmann::json> ipv6StaticAddresses;
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001966 std::optional<std::vector<std::string>> staticNameServers;
Jennifer Leeda131a92019-04-24 15:13:55 -07001967 std::optional<nlohmann::json> dhcpv4;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001968 std::optional<nlohmann::json> dhcpv6;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001969 std::optional<bool> interfaceEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001970 DHCPParameters v4dhcpParms;
1971 DHCPParameters v6dhcpParms;
Ed Tanous0627a2c2018-11-29 17:09:23 -08001972
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001973 if (!json_util::readJson(
zhanghch058d1b46d2021-04-01 11:18:24 +08001974 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301975 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1976 macAddress, "StaticNameServers", staticNameServers,
1977 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1978 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1979 "InterfaceEnabled", interfaceEnabled))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001980 {
1981 return;
1982 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001983 if (dhcpv4)
1984 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001985 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001986 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1987 v4dhcpParms.useDNSServers, "UseNTPServers",
1988 v4dhcpParms.useNTPServers, "UseDomainName",
1989 v4dhcpParms.useUseDomainName))
1990 {
1991 return;
1992 }
1993 }
1994
1995 if (dhcpv6)
1996 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001997 if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001998 v6dhcpParms.dhcpv6OperatingMode,
1999 "UseDNSServers", v6dhcpParms.useDNSServers,
2000 "UseNTPServers", v6dhcpParms.useNTPServers,
2001 "UseDomainName",
2002 v6dhcpParms.useUseDomainName))
2003 {
2004 return;
2005 }
Jennifer Leeda131a92019-04-24 15:13:55 -07002006 }
2007
Johnathan Mantey01784822019-06-18 12:44:21 -07002008 // Get single eth interface data, and call the below callback for
2009 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07002010 getEthernetIfaceData(
Ed Tanous2c70f802020-09-28 14:29:23 -07002011 ifaceId,
2012 [this, asyncResp, ifaceId, hostname = std::move(hostname),
Joshi-Mansiab6554f2020-03-10 18:33:36 +05302013 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
Ravi Tejad1d50812019-06-23 16:20:27 -05002014 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05002015 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002016 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002017 staticNameServers = std::move(staticNameServers),
2018 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
2019 v4dhcpParms = std::move(v4dhcpParms),
Ed Tanousf23b7292020-10-15 09:41:17 -07002020 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002021 const bool& success, const EthernetInterfaceData& ethData,
2022 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
2023 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002024 if (!success)
2025 {
2026 // ... otherwise return error
2027 // TODO(Pawel)consider distinguish between non existing
2028 // object, and other errors
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002029 messages::resourceNotFound(asyncResp->res,
Ed Tanous2c70f802020-09-28 14:29:23 -07002030 "Ethernet Interface", ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002031 return;
2032 }
2033
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002034 if (dhcpv4 || dhcpv6)
2035 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002036 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
2037 asyncResp);
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002038 }
2039
Ed Tanous0627a2c2018-11-29 17:09:23 -08002040 if (hostname)
2041 {
2042 handleHostnamePatch(*hostname, asyncResp);
2043 }
2044
Joshi-Mansiab6554f2020-03-10 18:33:36 +05302045 if (fqdn)
2046 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002047 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
Joshi-Mansiab6554f2020-03-10 18:33:36 +05302048 }
2049
Ratan Guptad5776652019-03-03 08:47:22 +05302050 if (macAddress)
2051 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002052 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
Ratan Guptad5776652019-03-03 08:47:22 +05302053 }
2054
Ravi Tejad1d50812019-06-23 16:20:27 -05002055 if (ipv4StaticAddresses)
2056 {
Ed Tanous537174c2018-12-10 15:09:31 -08002057 // TODO(ed) for some reason the capture of ipv4Addresses
Johnathan Mantey01784822019-06-18 12:44:21 -07002058 // above is returning a const value, not a non-const
2059 // value. This doesn't really work for us, as we need to
2060 // be able to efficiently move out the intermedia
2061 // nlohmann::json objects. This makes a copy of the
2062 // structure, and operates on that, but could be done
2063 // more efficiently
Ed Tanousf23b7292020-10-15 09:41:17 -07002064 nlohmann::json ipv4Static = *ipv4StaticAddresses;
Ed Tanous2c70f802020-09-28 14:29:23 -07002065 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
Ravi Tejad1d50812019-06-23 16:20:27 -05002066 asyncResp);
Ed Tanous0627a2c2018-11-29 17:09:23 -08002067 }
2068
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05002069 if (staticNameServers)
2070 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002071 handleStaticNameServersPatch(ifaceId, *staticNameServers,
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05002072 asyncResp);
2073 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05002074
2075 if (ipv6DefaultGateway)
2076 {
2077 messages::propertyNotWritable(asyncResp->res,
2078 "IPv6DefaultGateway");
2079 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002080
2081 if (ipv6StaticAddresses)
2082 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002083 nlohmann::json ipv6Static = *ipv6StaticAddresses;
Ed Tanous2c70f802020-09-28 14:29:23 -07002084 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
Johnathan Mantey01784822019-06-18 12:44:21 -07002085 ipv6Data, asyncResp);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002086 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002087
2088 if (interfaceEnabled)
2089 {
2090 setEthernetInterfaceBoolProperty(
Ed Tanous2c70f802020-09-28 14:29:23 -07002091 ifaceId, "NICEnabled", *interfaceEnabled, asyncResp);
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002092 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002093 });
2094 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002095};
2096
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002097/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07002098 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
2099 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002100 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002101class VlanNetworkInterface : public Node
2102{
2103 public:
2104 /*
2105 * Default Constructor
2106 */
Ed Tanous52cc1122020-07-18 13:51:21 -07002107 VlanNetworkInterface(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002108 Node(app,
Gunnar Mills7af91512020-04-14 22:16:57 -05002109 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/",
Ed Tanous4a0cb852018-10-15 07:55:04 -07002110 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002111 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002112 entityPrivileges = {
2113 {boost::beast::http::verb::get, {{"Login"}}},
2114 {boost::beast::http::verb::head, {{"Login"}}},
2115 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2116 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2117 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2118 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002119 }
2120
Ed Tanous1abe55e2018-09-05 08:30:59 -07002121 private:
Ed Tanous81ce6092020-12-17 16:54:55 +00002122 void parseInterfaceData(nlohmann::json& jsonResponse,
2123 const std::string& parentIfaceId,
2124 const std::string& ifaceId,
Ed Tanouscb13a392020-07-25 19:02:03 +00002125 const EthernetInterfaceData& ethData)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002126 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002127 // Fill out obvious data...
Ed Tanous81ce6092020-12-17 16:54:55 +00002128 jsonResponse["Id"] = ifaceId;
2129 jsonResponse["@odata.id"] =
2130 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parentIfaceId +
2131 "/VLANs/" + ifaceId;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002132
Ed Tanous81ce6092020-12-17 16:54:55 +00002133 jsonResponse["VLANEnable"] = true;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002134 if (!ethData.vlan_id.empty())
Ed Tanous4a0cb852018-10-15 07:55:04 -07002135 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002136 jsonResponse["VLANId"] = ethData.vlan_id.back();
Ed Tanous4a0cb852018-10-15 07:55:04 -07002137 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07002138 }
2139
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002140 bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002141 {
2142 if (!boost::starts_with(iface, parent + "_"))
2143 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002144 return false;
2145 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07002146 return true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002147 }
2148
2149 /**
2150 * Functions triggers appropriate requests on DBus
2151 */
zhanghch058d1b46d2021-04-01 11:18:24 +08002152 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2153 const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002154 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002155 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002156 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07002157 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
2158 // Check if there is required param, truly entering this shall be
2159 // impossible.
2160 if (params.size() != 2)
2161 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002162 messages::internalError(asyncResp->res);
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002163 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002164 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002165
Ed Tanous2c70f802020-09-28 14:29:23 -07002166 const std::string& parentIfaceId = params[0];
2167 const std::string& ifaceId = params[1];
zhanghch058d1b46d2021-04-01 11:18:24 +08002168 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanous0f74e642018-11-12 15:17:05 -08002169 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
zhanghch058d1b46d2021-04-01 11:18:24 +08002170 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002171
Ed Tanous2c70f802020-09-28 14:29:23 -07002172 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002173 {
2174 return;
2175 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002176
Johnathan Mantey01784822019-06-18 12:44:21 -07002177 // Get single eth interface data, and call the below callback for
2178 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07002179 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002180 params[1],
Ed Tanous2c70f802020-09-28 14:29:23 -07002181 [this, asyncResp, parentIfaceId{std::string(params[0])},
2182 ifaceId{std::string(params[1])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002183 const bool& success, const EthernetInterfaceData& ethData,
Ed Tanouscb13a392020-07-25 19:02:03 +00002184 const boost::container::flat_set<IPv4AddressData>&,
2185 const boost::container::flat_set<IPv6AddressData>&) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002186 if (success && ethData.vlan_id.size() != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002187 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002188 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
2189 ifaceId, ethData);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002190 }
2191 else
2192 {
2193 // ... otherwise return error
2194 // TODO(Pawel)consider distinguish between non existing
2195 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07002196 messages::resourceNotFound(
Ed Tanous2c70f802020-09-28 14:29:23 -07002197 asyncResp->res, "VLAN Network Interface", ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002198 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002199 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002200 }
2201
zhanghch058d1b46d2021-04-01 11:18:24 +08002202 void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2203 const crow::Request& req,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002204 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002205 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002206
Ed Tanous1abe55e2018-09-05 08:30:59 -07002207 if (params.size() != 2)
2208 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002209 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002210 return;
2211 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002212
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002213 const std::string& parentIfaceId = params[0];
2214 const std::string& ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002215
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002216 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002217 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002218 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2219 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002220 return;
2221 }
2222
Ed Tanous0627a2c2018-11-29 17:09:23 -08002223 bool vlanEnable = false;
Andrew Geissler38268fa2020-05-16 14:22:18 -05002224 uint32_t vlanId = 0;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002225
zhanghch058d1b46d2021-04-01 11:18:24 +08002226 if (!json_util::readJson(req, asyncResp->res, "VLANEnable", vlanEnable,
2227 "VLANId", vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002228 {
2229 return;
2230 }
2231
Johnathan Mantey01784822019-06-18 12:44:21 -07002232 // Get single eth interface data, and call the below callback for
2233 // JSON preparation
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002234 getEthernetIfaceData(
2235 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002236 [asyncResp, parentIfaceId{std::string(params[0])},
Ed Tanouscb13a392020-07-25 19:02:03 +00002237 ifaceId{std::string(params[1])}, &vlanEnable,
2238 &vlanId](const bool& success, const EthernetInterfaceData& ethData,
2239 const boost::container::flat_set<IPv4AddressData>&,
2240 const boost::container::flat_set<IPv6AddressData>&) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002241 if (success && !ethData.vlan_id.empty())
Sunitha Harish08244d02019-04-01 03:57:25 -05002242 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002243 auto callback =
2244 [asyncResp](const boost::system::error_code ec) {
2245 if (ec)
2246 {
2247 messages::internalError(asyncResp->res);
2248 }
2249 };
2250
2251 if (vlanEnable == true)
2252 {
2253 crow::connections::systemBus->async_method_call(
2254 std::move(callback), "xyz.openbmc_project.Network",
2255 "/xyz/openbmc_project/network/" + ifaceId,
2256 "org.freedesktop.DBus.Properties", "Set",
2257 "xyz.openbmc_project.Network.VLAN", "Id",
2258 std::variant<uint32_t>(vlanId));
2259 }
2260 else
2261 {
2262 BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2263 "vlan interface";
2264 crow::connections::systemBus->async_method_call(
2265 std::move(callback), "xyz.openbmc_project.Network",
2266 std::string("/xyz/openbmc_project/network/") +
2267 ifaceId,
2268 "xyz.openbmc_project.Object.Delete", "Delete");
2269 }
Sunitha Harish08244d02019-04-01 03:57:25 -05002270 }
2271 else
2272 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002273 // TODO(Pawel)consider distinguish between non existing
2274 // object, and other errors
2275 messages::resourceNotFound(
2276 asyncResp->res, "VLAN Network Interface", ifaceId);
2277 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002278 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002279 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002280 }
2281
zhanghch058d1b46d2021-04-01 11:18:24 +08002282 void doDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2283 const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002284 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002285 {
2286 if (params.size() != 2)
2287 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002288 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002289 return;
2290 }
2291
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002292 const std::string& parentIfaceId = params[0];
2293 const std::string& ifaceId = params[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -07002294
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002295 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002296 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002297 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2298 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002299 return;
2300 }
2301
Johnathan Mantey01784822019-06-18 12:44:21 -07002302 // Get single eth interface data, and call the below callback for
2303 // JSON preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002304 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002305 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002306 [asyncResp, parentIfaceId{std::string(params[0])},
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002307 ifaceId{std::string(params[1])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002308 const bool& success, const EthernetInterfaceData& ethData,
Ed Tanouscb13a392020-07-25 19:02:03 +00002309 const boost::container::flat_set<IPv4AddressData>&,
2310 const boost::container::flat_set<IPv6AddressData>&) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002311 if (success && !ethData.vlan_id.empty())
Jason M. Billsf12894f2018-10-09 12:45:45 -07002312 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002313 auto callback =
2314 [asyncResp](const boost::system::error_code ec) {
2315 if (ec)
2316 {
2317 messages::internalError(asyncResp->res);
2318 }
2319 };
2320 crow::connections::systemBus->async_method_call(
2321 std::move(callback), "xyz.openbmc_project.Network",
2322 std::string("/xyz/openbmc_project/network/") + ifaceId,
2323 "xyz.openbmc_project.Object.Delete", "Delete");
2324 }
2325 else
2326 {
2327 // ... otherwise return error
2328 // TODO(Pawel)consider distinguish between non existing
2329 // object, and other errors
2330 messages::resourceNotFound(
2331 asyncResp->res, "VLAN Network Interface", ifaceId);
2332 }
2333 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002334 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002335};
2336
2337/**
2338 * VlanNetworkInterfaceCollection derived class for delivering
2339 * VLANNetworkInterface Collection Schema
2340 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002341class VlanNetworkInterfaceCollection : public Node
2342{
2343 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002344 VlanNetworkInterfaceCollection(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002345 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
2346 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002347 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002348 entityPrivileges = {
2349 {boost::beast::http::verb::get, {{"Login"}}},
2350 {boost::beast::http::verb::head, {{"Login"}}},
2351 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2352 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2353 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2354 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002355 }
2356
Ed Tanous1abe55e2018-09-05 08:30:59 -07002357 private:
2358 /**
2359 * Functions triggers appropriate requests on DBus
2360 */
zhanghch058d1b46d2021-04-01 11:18:24 +08002361 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2362 const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002363 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002364 {
2365 if (params.size() != 1)
2366 {
2367 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07002368 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002369 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07002370 }
2371
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002372 const std::string& rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002373
Ed Tanous4a0cb852018-10-15 07:55:04 -07002374 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07002375 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002376 getEthernetIfaceList(
Ed Tanous43b761d2019-02-13 20:10:56 -08002377 [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002378 const bool& success,
Ed Tanous81ce6092020-12-17 16:54:55 +00002379 const boost::container::flat_set<std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002380 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002381 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002382 messages::internalError(asyncResp->res);
2383 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002384 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002385
Ed Tanous81ce6092020-12-17 16:54:55 +00002386 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
Ed Tanous4c9afe42019-05-03 16:59:57 -07002387 {
2388 messages::resourceNotFound(asyncResp->res,
2389 "VLanNetworkInterfaceCollection",
2390 rootInterfaceName);
2391 return;
2392 }
2393
Ed Tanous0f74e642018-11-12 15:17:05 -08002394 asyncResp->res.jsonValue["@odata.type"] =
2395 "#VLanNetworkInterfaceCollection."
2396 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002397 asyncResp->res.jsonValue["Name"] =
2398 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002399
Ed Tanous2c70f802020-09-28 14:29:23 -07002400 nlohmann::json ifaceArray = nlohmann::json::array();
Jason M. Billsf12894f2018-10-09 12:45:45 -07002401
Ed Tanous81ce6092020-12-17 16:54:55 +00002402 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07002403 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002404 if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002405 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002406 std::string path =
2407 "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2408 path += rootInterfaceName;
2409 path += "/VLANs/";
2410 path += ifaceItem;
2411 ifaceArray.push_back({{"@odata.id", std::move(path)}});
Jason M. Billsf12894f2018-10-09 12:45:45 -07002412 }
2413 }
2414
Jason M. Billsf12894f2018-10-09 12:45:45 -07002415 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07002416 ifaceArray.size();
2417 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
Jason M. Billsf12894f2018-10-09 12:45:45 -07002418 asyncResp->res.jsonValue["@odata.id"] =
2419 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2420 rootInterfaceName + "/VLANs";
2421 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002422 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002423
zhanghch058d1b46d2021-04-01 11:18:24 +08002424 void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2425 const crow::Request& req,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002426 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002427 {
2428 if (params.size() != 1)
2429 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002430 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002431 return;
2432 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002433 bool vlanEnable = false;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002434 uint32_t vlanId = 0;
zhanghch058d1b46d2021-04-01 11:18:24 +08002435 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId,
2436 "VLANEnable", vlanEnable))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002437 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002438 return;
2439 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002440 // Need both vlanId and vlanEnable to service this request
2441 if (!vlanId)
2442 {
2443 messages::propertyMissing(asyncResp->res, "VLANId");
2444 }
2445 if (!vlanEnable)
2446 {
2447 messages::propertyMissing(asyncResp->res, "VLANEnable");
2448 }
Ed Tanous271584a2019-07-09 16:24:22 -07002449 if (static_cast<bool>(vlanId) ^ vlanEnable)
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002450 {
2451 return;
2452 }
2453
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002454 const std::string& rootInterfaceName = params[0];
Ed Tanous4a0cb852018-10-15 07:55:04 -07002455 auto callback = [asyncResp](const boost::system::error_code ec) {
2456 if (ec)
2457 {
2458 // TODO(ed) make more consistent error messages based on
2459 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07002460 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002461 return;
2462 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002463 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002464 };
2465 crow::connections::systemBus->async_method_call(
2466 std::move(callback), "xyz.openbmc_project.Network",
2467 "/xyz/openbmc_project/network",
2468 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
Ed Tanous0627a2c2018-11-29 17:09:23 -08002469 rootInterfaceName, vlanId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002470 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002471};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002472} // namespace redfish