blob: 2647672c8954f4566cefb08fcbd5261674df0a73 [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 }
Ed Tanous029573d2019-02-01 10:57:49 -0800325 }
326 }
327 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700328
329 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
330 {
331 if (ifacePair.first ==
332 "xyz.openbmc_project.Network.DHCPConfiguration")
333 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500334 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700335 {
336 if (propertyPair.first == "DNSEnabled")
337 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700338 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700339 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700340 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700341 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700342 ethData.DNSEnabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700343 }
344 }
345 else if (propertyPair.first == "NTPEnabled")
346 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700347 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700348 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700349 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700350 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700351 ethData.NTPEnabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700352 }
353 }
354 else if (propertyPair.first == "HostNameEnabled")
355 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700356 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700357 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700358 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700359 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700360 ethData.HostNameEnabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700361 }
362 }
363 else if (propertyPair.first == "SendHostNameEnabled")
364 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700365 const bool* sendHostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700366 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700367 if (sendHostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700368 {
369 ethData.SendHostNameEnabled =
Ed Tanous2c70f802020-09-28 14:29:23 -0700370 *sendHostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700371 }
372 }
373 }
374 }
375 }
Ed Tanous029573d2019-02-01 10:57:49 -0800376 // System configuration shows up in the global namespace, so no need
377 // to check eth number
378 if (ifacePair.first ==
379 "xyz.openbmc_project.Network.SystemConfiguration")
380 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500381 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800382 {
383 if (propertyPair.first == "HostName")
384 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500385 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500386 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800387 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700388 {
Ed Tanous029573d2019-02-01 10:57:49 -0800389 ethData.hostname = *hostname;
390 }
391 }
392 else if (propertyPair.first == "DefaultGateway")
393 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500394 const std::string* defaultGateway =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500395 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800396 if (defaultGateway != nullptr)
397 {
398 ethData.default_gateway = *defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700399 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700400 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500401 else if (propertyPair.first == "DefaultGateway6")
402 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500403 const std::string* defaultGateway6 =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500404 std::get_if<std::string>(&propertyPair.second);
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500405 if (defaultGateway6 != nullptr)
406 {
407 ethData.ipv6_default_gateway = *defaultGateway6;
408 }
409 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700410 }
411 }
412 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700413 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700414 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700415}
416
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500417// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700418inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000419 extractIPV6Data(const std::string& ethifaceId,
420 const GetManagedObjects& dbusData,
421 boost::container::flat_set<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500422{
423 const std::string ipv6PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000424 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500425
426 // Since there might be several IPv6 configurations aligned with
427 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000428 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500429 {
430 // Check if proper pattern for object path appears
431 if (boost::starts_with(objpath.first.str, ipv6PathStart))
432 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500433 for (auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500434 {
435 if (interface.first == "xyz.openbmc_project.Network.IP")
436 {
437 // Instance IPv6AddressData structure, and set as
438 // appropriate
439 std::pair<
440 boost::container::flat_set<IPv6AddressData>::iterator,
441 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000442 it = ipv6Config.insert(IPv6AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700443 IPv6AddressData& ipv6Address = *it.first;
444 ipv6Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700445 objpath.first.str.substr(ipv6PathStart.size());
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500446 for (auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500447 {
448 if (property.first == "Address")
449 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500450 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500451 std::get_if<std::string>(&property.second);
452 if (address != nullptr)
453 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700454 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500455 }
456 }
457 else if (property.first == "Origin")
458 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500459 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500460 std::get_if<std::string>(&property.second);
461 if (origin != nullptr)
462 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700463 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500464 translateAddressOriginDbusToRedfish(*origin,
465 false);
466 }
467 }
468 else if (property.first == "PrefixLength")
469 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500470 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500471 std::get_if<uint8_t>(&property.second);
472 if (prefix != nullptr)
473 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700474 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500475 }
476 }
477 else
478 {
479 BMCWEB_LOG_ERROR
480 << "Got extra property: " << property.first
481 << " on the " << objpath.first.str << " object";
482 }
483 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500484 }
485 }
486 }
487 }
488}
489
Ed Tanous4a0cb852018-10-15 07:55:04 -0700490// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700491inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000492 extractIPData(const std::string& ethifaceId,
493 const GetManagedObjects& dbusData,
494 boost::container::flat_set<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700495{
496 const std::string ipv4PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000497 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700498
499 // Since there might be several IPv4 configurations aligned with
500 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000501 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700502 {
503 // Check if proper pattern for object path appears
504 if (boost::starts_with(objpath.first.str, ipv4PathStart))
505 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500506 for (auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700507 {
508 if (interface.first == "xyz.openbmc_project.Network.IP")
509 {
510 // Instance IPv4AddressData structure, and set as
511 // appropriate
512 std::pair<
513 boost::container::flat_set<IPv4AddressData>::iterator,
514 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000515 it = ipv4Config.insert(IPv4AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700516 IPv4AddressData& ipv4Address = *it.first;
517 ipv4Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700518 objpath.first.str.substr(ipv4PathStart.size());
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500519 for (auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700520 {
521 if (property.first == "Address")
522 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500523 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800524 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700525 if (address != nullptr)
526 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700527 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700528 }
529 }
530 else if (property.first == "Gateway")
531 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500532 const std::string* gateway =
Ed Tanousabf2add2019-01-22 16:40:12 -0800533 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700534 if (gateway != nullptr)
535 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700536 ipv4Address.gateway = *gateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700537 }
538 }
539 else if (property.first == "Origin")
540 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500541 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800542 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700543 if (origin != nullptr)
544 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700545 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700546 translateAddressOriginDbusToRedfish(*origin,
547 true);
548 }
549 }
550 else if (property.first == "PrefixLength")
551 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500552 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800553 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700554 if (mask != nullptr)
555 {
556 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700557 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700558 }
559 }
560 else
561 {
562 BMCWEB_LOG_ERROR
563 << "Got extra property: " << property.first
564 << " on the " << objpath.first.str << " object";
565 }
566 }
567 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700568 ipv4Address.linktype =
569 boost::starts_with(ipv4Address.address, "169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700570 ? LinkType::Local
571 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700572 }
573 }
574 }
575 }
576}
577
578/**
579 * @brief Sets given Id on the given VLAN interface through D-Bus
580 *
581 * @param[in] ifaceId Id of VLAN interface that should be modified
582 * @param[in] inputVlanId New ID of the VLAN
583 * @param[in] callback Function that will be called after the operation
584 *
585 * @return None.
586 */
587template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500588void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
589 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700590{
591 crow::connections::systemBus->async_method_call(
592 callback, "xyz.openbmc_project.Network",
593 std::string("/xyz/openbmc_project/network/") + ifaceId,
594 "org.freedesktop.DBus.Properties", "Set",
595 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanousabf2add2019-01-22 16:40:12 -0800596 std::variant<uint32_t>(inputVlanId));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700597}
598
599/**
600 * @brief Helper function that verifies IP address to check if it is in
601 * proper format. If bits pointer is provided, also calculates active
602 * bit count for Subnet Mask.
603 *
604 * @param[in] ip IP that will be verified
605 * @param[out] bits Calculated mask in bits notation
606 *
607 * @return true in case of success, false otherwise
608 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500609inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
610 uint8_t* bits = nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700611{
612 std::vector<std::string> bytesInMask;
613
614 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700615
616 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700617 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700618 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700619 return false;
620 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700621
Ed Tanous4a0cb852018-10-15 07:55:04 -0700622 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700623 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700624 *bits = 0;
625 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700626
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500627 char* endPtr;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700628 long previousValue = 255;
629 bool firstZeroInByteHit;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500630 for (const std::string& byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700631 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700632 if (byte.empty())
633 {
634 return false;
635 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700636
Ed Tanous4a0cb852018-10-15 07:55:04 -0700637 // Use strtol instead of stroi to avoid exceptions
638 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700639
Ed Tanous4a0cb852018-10-15 07:55:04 -0700640 // endPtr should point to the end of the string, otherwise given string
641 // is not 100% number
642 if (*endPtr != '\0')
643 {
644 return false;
645 }
646
647 // Value should be contained in byte
648 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700649 {
650 return false;
651 }
652
653 if (bits != nullptr)
654 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700655 // Mask has to be continuous between bytes
656 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700657 {
658 return false;
659 }
660
Ed Tanous4a0cb852018-10-15 07:55:04 -0700661 // Mask has to be continuous inside bytes
662 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700663
Ed Tanous4a0cb852018-10-15 07:55:04 -0700664 // Count bits
Ed Tanous23a21a12020-07-25 04:45:05 +0000665 for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700666 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000667 if (value & (1L << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700668 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700669 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700670 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700671 // Continuity not preserved
672 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700673 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700674 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700675 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700676 else
677 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700678 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700679 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700680 }
681 }
682
683 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700684 }
685
Ed Tanous4a0cb852018-10-15 07:55:04 -0700686 return true;
687}
688
689/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700690 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700691 *
692 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700693 * @param[in] ipHash DBus Hash id of IP that should be deleted
694 * @param[io] asyncResp Response object that will be returned to client
695 *
696 * @return None
697 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500698inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800699 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700700{
701 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700702 [asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700703 if (ec)
704 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800705 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100706 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700707 },
708 "xyz.openbmc_project.Network",
709 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
710 "xyz.openbmc_project.Object.Delete", "Delete");
711}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700712
Ed Tanous4a0cb852018-10-15 07:55:04 -0700713/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700714 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700715 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700716 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
717 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
718 * @param[in] gateway IPv4 address of this interfaces gateway
719 * @param[in] address IPv4 address to assign to this interface
720 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700721 *
722 * @return None
723 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000724inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
725 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800726 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700727{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700728 crow::connections::systemBus->async_method_call(
Johnathan Mantey01784822019-06-18 12:44:21 -0700729 [asyncResp](const boost::system::error_code ec) {
730 if (ec)
731 {
732 messages::internalError(asyncResp->res);
733 }
734 },
735 "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700736 "/xyz/openbmc_project/network/" + ifaceId,
737 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700738 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700739 gateway);
740}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500741
742/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700743 * @brief Deletes the IPv4 entry for this interface and creates a replacement
744 * static IPv4 entry
745 *
746 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
747 * @param[in] id The unique hash entry identifying the DBus entry
748 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
749 * @param[in] gateway IPv4 address of this interfaces gateway
750 * @param[in] address IPv4 address to assign to this interface
751 * @param[io] asyncResp Response object that will be returned to client
752 *
753 * @return None
754 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800755inline void
756 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
757 uint8_t prefixLength, const std::string& gateway,
758 const std::string& address,
759 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700760{
761 crow::connections::systemBus->async_method_call(
762 [asyncResp, ifaceId, address, prefixLength,
763 gateway](const boost::system::error_code ec) {
764 if (ec)
765 {
766 messages::internalError(asyncResp->res);
767 }
768 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +0000769 [asyncResp](const boost::system::error_code ec2) {
770 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700771 {
772 messages::internalError(asyncResp->res);
773 }
774 },
775 "xyz.openbmc_project.Network",
776 "/xyz/openbmc_project/network/" + ifaceId,
777 "xyz.openbmc_project.Network.IP.Create", "IP",
778 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
779 prefixLength, gateway);
780 },
781 "xyz.openbmc_project.Network",
782 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
783 "xyz.openbmc_project.Object.Delete", "Delete");
784}
785
786/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500787 * @brief Deletes given IPv6
788 *
789 * @param[in] ifaceId Id of interface whose IP should be deleted
790 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500791 * @param[io] asyncResp Response object that will be returned to client
792 *
793 * @return None
794 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500795inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800796 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500797{
798 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700799 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500800 if (ec)
801 {
802 messages::internalError(asyncResp->res);
803 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500804 },
805 "xyz.openbmc_project.Network",
806 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
807 "xyz.openbmc_project.Object.Delete", "Delete");
808}
809
810/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700811 * @brief Deletes the IPv6 entry for this interface and creates a replacement
812 * static IPv6 entry
813 *
814 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
815 * @param[in] id The unique hash entry identifying the DBus entry
816 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
817 * @param[in] address IPv6 address to assign to this interface
818 * @param[io] asyncResp Response object that will be returned to client
819 *
820 * @return None
821 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800822inline void
823 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
824 uint8_t prefixLength, const std::string& address,
825 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700826{
827 crow::connections::systemBus->async_method_call(
828 [asyncResp, ifaceId, address,
829 prefixLength](const boost::system::error_code ec) {
830 if (ec)
831 {
832 messages::internalError(asyncResp->res);
833 }
834 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +0000835 [asyncResp](const boost::system::error_code ec2) {
836 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700837 {
838 messages::internalError(asyncResp->res);
839 }
840 },
841 "xyz.openbmc_project.Network",
842 "/xyz/openbmc_project/network/" + ifaceId,
843 "xyz.openbmc_project.Network.IP.Create", "IP",
844 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
845 prefixLength, "");
846 },
847 "xyz.openbmc_project.Network",
848 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
849 "xyz.openbmc_project.Object.Delete", "Delete");
850}
851
852/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500853 * @brief Creates IPv6 with given data
854 *
855 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500856 * @param[in] prefixLength Prefix length that needs to be added
857 * @param[in] address IP address that needs to be added
858 * @param[io] asyncResp Response object that will be returned to client
859 *
860 * @return None
861 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500862inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
863 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800864 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500865{
866 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
867 if (ec)
868 {
869 messages::internalError(asyncResp->res);
870 }
871 };
872 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500873 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500874 crow::connections::systemBus->async_method_call(
875 std::move(createIpHandler), "xyz.openbmc_project.Network",
876 "/xyz/openbmc_project/network/" + ifaceId,
877 "xyz.openbmc_project.Network.IP.Create", "IP",
878 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
879 "");
880}
881
Ed Tanous4a0cb852018-10-15 07:55:04 -0700882/**
883 * Function that retrieves all properties for given Ethernet Interface
884 * Object
885 * from EntityManager Network Manager
886 * @param ethiface_id a eth interface id to query on DBus
887 * @param callback a function that shall be called to convert Dbus output
888 * into JSON
889 */
890template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000891void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500892 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700893{
894 crow::connections::systemBus->async_method_call(
Ed Tanous81ce6092020-12-17 16:54:55 +0000895 [ethifaceId{std::string{ethifaceId}}, callback{std::move(callback)}](
896 const boost::system::error_code errorCode,
Ed Tanousf23b7292020-10-15 09:41:17 -0700897 GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700898 EthernetInterfaceData ethData{};
899 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500900 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700901
Ed Tanous81ce6092020-12-17 16:54:55 +0000902 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700903 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700904 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700905 return;
906 }
907
Ed Tanous4c9afe42019-05-03 16:59:57 -0700908 bool found =
Ed Tanous2c70f802020-09-28 14:29:23 -0700909 extractEthernetInterfaceData(ethifaceId, resp, ethData);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700910 if (!found)
911 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700912 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700913 return;
914 }
915
Ed Tanous2c70f802020-09-28 14:29:23 -0700916 extractIPData(ethifaceId, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700917 // Fix global GW
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500918 for (IPv4AddressData& ipv4 : ipv4Data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700919 {
Ravi Tejac6191412019-07-30 00:53:50 -0500920 if (((ipv4.linktype == LinkType::Global) &&
921 (ipv4.gateway == "0.0.0.0")) ||
922 (ipv4.origin == "DHCP"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700923 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700924 ipv4.gateway = ethData.default_gateway;
925 }
926 }
927
Ed Tanous2c70f802020-09-28 14:29:23 -0700928 extractIPV6Data(ethifaceId, resp, ipv6Data);
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500929 // Finally make a callback with useful data
Johnathan Mantey01784822019-06-18 12:44:21 -0700930 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700931 },
932 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
933 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700934}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700935
936/**
937 * Function that retrieves all Ethernet Interfaces available through Network
938 * Manager
939 * @param callback a function that shall be called to convert Dbus output
940 * into JSON.
941 */
942template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500943void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700944{
945 crow::connections::systemBus->async_method_call(
946 [callback{std::move(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000947 const boost::system::error_code errorCode,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500948 GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700949 // Callback requires vector<string> to retrieve all available
950 // ethernet interfaces
Ed Tanous2c70f802020-09-28 14:29:23 -0700951 boost::container::flat_set<std::string> ifaceList;
952 ifaceList.reserve(resp.size());
Ed Tanous81ce6092020-12-17 16:54:55 +0000953 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700954 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700955 callback(false, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700956 return;
957 }
958
959 // Iterate over all retrieved ObjectPaths.
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500960 for (const auto& objpath : resp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700961 {
962 // And all interfaces available for certain ObjectPath.
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500963 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700964 {
965 // If interface is
966 // xyz.openbmc_project.Network.EthernetInterface, this is
967 // what we're looking for.
968 if (interface.first ==
969 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700970 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000971 std::string ifaceId = objpath.first.filename();
972 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700973 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000974 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700975 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +0000976 // and put it into output vector.
977 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700978 }
979 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700980 }
981 // Finally make a callback with useful data
Ed Tanous2c70f802020-09-28 14:29:23 -0700982 callback(true, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700983 },
984 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
985 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700986}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100987
988/**
989 * EthernetCollection derived class for delivering Ethernet Collection Schema
990 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700991class EthernetCollection : public Node
992{
993 public:
Ed Tanous52cc1122020-07-18 13:51:21 -0700994 EthernetCollection(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -0700995 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700996 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700997 entityPrivileges = {
998 {boost::beast::http::verb::get, {{"Login"}}},
999 {boost::beast::http::verb::head, {{"Login"}}},
1000 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1001 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1002 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1003 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1004 }
1005
1006 private:
1007 /**
1008 * Functions triggers appropriate requests on DBus
1009 */
zhanghch058d1b46d2021-04-01 11:18:24 +08001010 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1011 const crow::Request&, const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001012 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001013 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanous0f74e642018-11-12 15:17:05 -08001014 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
zhanghch058d1b46d2021-04-01 11:18:24 +08001015 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous0f74e642018-11-12 15:17:05 -08001016 "/redfish/v1/Managers/bmc/EthernetInterfaces";
zhanghch058d1b46d2021-04-01 11:18:24 +08001017 asyncResp->res.jsonValue["Name"] =
1018 "Ethernet Network Interface Collection";
1019 asyncResp->res.jsonValue["Description"] =
Ed Tanous0f74e642018-11-12 15:17:05 -08001020 "Collection of EthernetInterfaces for this Manager";
zhanghch058d1b46d2021-04-01 11:18:24 +08001021
Ed Tanous4a0cb852018-10-15 07:55:04 -07001022 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001023 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001024 getEthernetIfaceList(
Ed Tanous4c9afe42019-05-03 16:59:57 -07001025 [asyncResp](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001026 const bool& success,
Ed Tanous81ce6092020-12-17 16:54:55 +00001027 const boost::container::flat_set<std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001028 if (!success)
1029 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001030 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001031 return;
1032 }
1033
Ed Tanous2c70f802020-09-28 14:29:23 -07001034 nlohmann::json& ifaceArray =
Ed Tanous4c9afe42019-05-03 16:59:57 -07001035 asyncResp->res.jsonValue["Members"];
Ed Tanous2c70f802020-09-28 14:29:23 -07001036 ifaceArray = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001037 std::string tag = "_";
Ed Tanous81ce6092020-12-17 16:54:55 +00001038 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001039 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001040 std::size_t found = ifaceItem.find(tag);
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001041 if (found == std::string::npos)
1042 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001043 ifaceArray.push_back(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001044 {{"@odata.id",
1045 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
Ed Tanous2c70f802020-09-28 14:29:23 -07001046 ifaceItem}});
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001047 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001048 }
1049
Ed Tanous4c9afe42019-05-03 16:59:57 -07001050 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07001051 ifaceArray.size();
Ed Tanous4c9afe42019-05-03 16:59:57 -07001052 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001053 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001054 });
Ed Tanous4a0cb852018-10-15 07:55:04 -07001055 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001056};
1057
1058/**
1059 * EthernetInterface derived class for delivering Ethernet Schema
1060 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001061class EthernetInterface : public Node
1062{
1063 public:
1064 /*
1065 * Default Constructor
1066 */
Ed Tanous52cc1122020-07-18 13:51:21 -07001067 EthernetInterface(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001068 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -07001069 std::string())
1070 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001071 entityPrivileges = {
1072 {boost::beast::http::verb::get, {{"Login"}}},
1073 {boost::beast::http::verb::head, {{"Login"}}},
1074 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1075 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1076 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1077 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02001078 }
1079
Ed Tanous1abe55e2018-09-05 08:30:59 -07001080 private:
zhanghch058d1b46d2021-04-01 11:18:24 +08001081 void
1082 handleHostnamePatch(const std::string& hostname,
1083 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001084 {
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301085 // SHOULD handle host names of up to 255 characters(RFC 1123)
1086 if (hostname.length() > 255)
1087 {
1088 messages::propertyValueFormatError(asyncResp->res, hostname,
1089 "HostName");
1090 return;
1091 }
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001092 crow::connections::systemBus->async_method_call(
1093 [asyncResp](const boost::system::error_code ec) {
1094 if (ec)
1095 {
1096 messages::internalError(asyncResp->res);
1097 }
1098 },
1099 "xyz.openbmc_project.Network",
1100 "/xyz/openbmc_project/network/config",
1101 "org.freedesktop.DBus.Properties", "Set",
1102 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanousabf2add2019-01-22 16:40:12 -08001103 std::variant<std::string>(hostname));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001104 }
1105
zhanghch058d1b46d2021-04-01 11:18:24 +08001106 void handleDomainnamePatch(
1107 const std::string& ifaceId, const std::string& domainname,
1108 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301109 {
1110 std::vector<std::string> vectorDomainname = {domainname};
1111 crow::connections::systemBus->async_method_call(
1112 [asyncResp](const boost::system::error_code ec) {
1113 if (ec)
1114 {
1115 messages::internalError(asyncResp->res);
1116 }
1117 },
1118 "xyz.openbmc_project.Network",
1119 "/xyz/openbmc_project/network/" + ifaceId,
1120 "org.freedesktop.DBus.Properties", "Set",
1121 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1122 std::variant<std::vector<std::string>>(vectorDomainname));
1123 }
1124
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001125 void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
zhanghch058d1b46d2021-04-01 11:18:24 +08001126 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301127 {
1128 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1129 if (fqdn.length() > 255)
1130 {
1131 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1132 return;
1133 }
1134
1135 size_t pos = fqdn.find('.');
1136 if (pos == std::string::npos)
1137 {
1138 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1139 return;
1140 }
1141
1142 std::string hostname;
1143 std::string domainname;
1144 domainname = (fqdn).substr(pos + 1);
1145 hostname = (fqdn).substr(0, pos);
1146
1147 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1148 {
1149 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1150 return;
1151 }
1152
1153 handleHostnamePatch(hostname, asyncResp);
1154 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1155 }
1156
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001157 bool isHostnameValid(const std::string& hostname)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301158 {
1159 // A valid host name can never have the dotted-decimal form (RFC 1123)
1160 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1161 {
1162 return false;
1163 }
1164 // Each label(hostname/subdomains) within a valid FQDN
1165 // MUST handle host names of up to 63 characters (RFC 1123)
1166 // labels cannot start or end with hyphens (RFC 952)
1167 // labels can start with numbers (RFC 1123)
1168 const std::regex pattern(
1169 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1170
1171 return std::regex_match(hostname, pattern);
1172 }
1173
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001174 bool isDomainnameValid(const std::string& domainname)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301175 {
1176 // Can have multiple subdomains
1177 // Top Level Domain's min length is 2 character
1178 const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]"
1179 "{1,30}\\.)*[a-zA-Z]{2,}$");
1180
1181 return std::regex_match(domainname, pattern);
1182 }
1183
zhanghch058d1b46d2021-04-01 11:18:24 +08001184 void handleMACAddressPatch(
1185 const std::string& ifaceId, const std::string& macAddress,
1186 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ratan Guptad5776652019-03-03 08:47:22 +05301187 {
1188 crow::connections::systemBus->async_method_call(
1189 [asyncResp, macAddress](const boost::system::error_code ec) {
1190 if (ec)
1191 {
1192 messages::internalError(asyncResp->res);
1193 return;
1194 }
Ratan Guptad5776652019-03-03 08:47:22 +05301195 },
1196 "xyz.openbmc_project.Network",
1197 "/xyz/openbmc_project/network/" + ifaceId,
1198 "org.freedesktop.DBus.Properties", "Set",
1199 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1200 std::variant<std::string>(macAddress));
1201 }
Johnathan Mantey286b9112019-06-10 13:38:04 -07001202
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001203 void setDHCPEnabled(const std::string& ifaceId,
1204 const std::string& propertyName, const bool v4Value,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001205 const bool v6Value,
zhanghch058d1b46d2021-04-01 11:18:24 +08001206 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001207 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001208 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
Jennifer Leeda131a92019-04-24 15:13:55 -07001209 crow::connections::systemBus->async_method_call(
1210 [asyncResp](const boost::system::error_code ec) {
1211 if (ec)
1212 {
1213 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1214 messages::internalError(asyncResp->res);
1215 return;
1216 }
Jayaprakash Mutyala8f7e9c12021-03-22 10:08:00 +00001217 messages::success(asyncResp->res);
Jennifer Leeda131a92019-04-24 15:13:55 -07001218 },
1219 "xyz.openbmc_project.Network",
1220 "/xyz/openbmc_project/network/" + ifaceId,
1221 "org.freedesktop.DBus.Properties", "Set",
1222 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001223 std::variant<std::string>{dhcp});
Jennifer Leeda131a92019-04-24 15:13:55 -07001224 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001225
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001226 void setEthernetInterfaceBoolProperty(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001227 const std::string& ifaceId, const std::string& propertyName,
zhanghch058d1b46d2021-04-01 11:18:24 +08001228 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001229 {
1230 crow::connections::systemBus->async_method_call(
1231 [asyncResp](const boost::system::error_code ec) {
1232 if (ec)
1233 {
1234 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1235 messages::internalError(asyncResp->res);
1236 return;
1237 }
1238 },
1239 "xyz.openbmc_project.Network",
1240 "/xyz/openbmc_project/network/" + ifaceId,
1241 "org.freedesktop.DBus.Properties", "Set",
1242 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1243 std::variant<bool>{value});
1244 }
1245
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001246 void setDHCPv4Config(const std::string& propertyName, const bool& value,
zhanghch058d1b46d2021-04-01 11:18:24 +08001247 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001248 {
1249 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1250 crow::connections::systemBus->async_method_call(
1251 [asyncResp](const boost::system::error_code ec) {
1252 if (ec)
1253 {
1254 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1255 messages::internalError(asyncResp->res);
1256 return;
1257 }
1258 },
1259 "xyz.openbmc_project.Network",
1260 "/xyz/openbmc_project/network/config/dhcp",
1261 "org.freedesktop.DBus.Properties", "Set",
1262 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1263 std::variant<bool>{value});
1264 }
Ratan Guptad5776652019-03-03 08:47:22 +05301265
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001266 void handleDHCPPatch(const std::string& ifaceId,
1267 const EthernetInterfaceData& ethData,
Ed Tanousf23b7292020-10-15 09:41:17 -07001268 const DHCPParameters& v4dhcpParms,
1269 const DHCPParameters& v6dhcpParms,
zhanghch058d1b46d2021-04-01 11:18:24 +08001270 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001271 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001272 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1273 bool ipv6Active =
1274 translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
Jennifer Leeda131a92019-04-24 15:13:55 -07001275
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001276 bool nextv4DHCPState =
1277 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1278
1279 bool nextv6DHCPState{};
1280 if (v6dhcpParms.dhcpv6OperatingMode)
Jennifer Leeda131a92019-04-24 15:13:55 -07001281 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001282 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1283 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1284 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1285 {
1286 messages::propertyValueFormatError(
1287 asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
1288 "OperatingMode");
1289 return;
1290 }
1291 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1292 }
1293 else
1294 {
1295 nextv6DHCPState = ipv6Active;
Jennifer Leeda131a92019-04-24 15:13:55 -07001296 }
1297
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001298 bool nextDNS{};
1299 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001300 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001301 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1302 {
1303 messages::generalError(asyncResp->res);
1304 return;
1305 }
1306 nextDNS = *v4dhcpParms.useDNSServers;
1307 }
1308 else if (v4dhcpParms.useDNSServers)
1309 {
1310 nextDNS = *v4dhcpParms.useDNSServers;
1311 }
1312 else if (v6dhcpParms.useDNSServers)
1313 {
1314 nextDNS = *v6dhcpParms.useDNSServers;
1315 }
1316 else
1317 {
1318 nextDNS = ethData.DNSEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001319 }
1320
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001321 bool nextNTP{};
1322 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001323 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001324 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1325 {
1326 messages::generalError(asyncResp->res);
1327 return;
1328 }
1329 nextNTP = *v4dhcpParms.useNTPServers;
1330 }
1331 else if (v4dhcpParms.useNTPServers)
1332 {
1333 nextNTP = *v4dhcpParms.useNTPServers;
1334 }
1335 else if (v6dhcpParms.useNTPServers)
1336 {
1337 nextNTP = *v6dhcpParms.useNTPServers;
1338 }
1339 else
1340 {
1341 nextNTP = ethData.NTPEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001342 }
1343
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001344 bool nextUseDomain{};
1345 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
Jennifer Leeda131a92019-04-24 15:13:55 -07001346 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001347 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1348 {
1349 messages::generalError(asyncResp->res);
1350 return;
1351 }
1352 nextUseDomain = *v4dhcpParms.useUseDomainName;
1353 }
1354 else if (v4dhcpParms.useUseDomainName)
1355 {
1356 nextUseDomain = *v4dhcpParms.useUseDomainName;
1357 }
1358 else if (v6dhcpParms.useUseDomainName)
1359 {
1360 nextUseDomain = *v6dhcpParms.useUseDomainName;
1361 }
1362 else
1363 {
1364 nextUseDomain = ethData.HostNameEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001365 }
1366
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001367 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1368 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1369 asyncResp);
1370 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1371 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1372 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1373 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1374 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1375 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
Jennifer Leeda131a92019-04-24 15:13:55 -07001376 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001377
1378 boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanous2c70f802020-09-28 14:29:23 -07001379 getNextStaticIpEntry(
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301380 const boost::container::flat_set<IPv4AddressData>::const_iterator&
1381 head,
Ed Tanousb5a76932020-09-29 16:16:58 -07001382 const boost::container::flat_set<IPv4AddressData>::const_iterator&
1383 end)
Johnathan Mantey01784822019-06-18 12:44:21 -07001384 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301385 return std::find_if(head, end, [](const IPv4AddressData& value) {
1386 return value.origin == "Static";
1387 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001388 }
1389
1390 boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanous2c70f802020-09-28 14:29:23 -07001391 getNextStaticIpEntry(
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301392 const boost::container::flat_set<IPv6AddressData>::const_iterator&
1393 head,
Ed Tanousb5a76932020-09-29 16:16:58 -07001394 const boost::container::flat_set<IPv6AddressData>::const_iterator&
1395 end)
Johnathan Mantey01784822019-06-18 12:44:21 -07001396 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301397 return std::find_if(head, end, [](const IPv6AddressData& value) {
1398 return value.origin == "Static";
1399 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001400 }
1401
Ravi Tejad1d50812019-06-23 16:20:27 -05001402 void handleIPv4StaticPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001403 const std::string& ifaceId, nlohmann::json& input,
1404 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
zhanghch058d1b46d2021-04-01 11:18:24 +08001405 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001406 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001407 if ((!input.is_array()) || input.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301408 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001409 messages::propertyValueTypeError(
1410 asyncResp->res,
1411 input.dump(2, ' ', true,
1412 nlohmann::json::error_handler_t::replace),
1413 "IPv4StaticAddresses");
Ratan Guptaf476acb2019-03-02 16:46:57 +05301414 return;
1415 }
1416
Ed Tanous271584a2019-07-09 16:24:22 -07001417 unsigned entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001418 // Find the first static IP address currently active on the NIC and
1419 // match it to the first JSON element in the IPv4StaticAddresses array.
1420 // Match each subsequent JSON element to the next static IP programmed
1421 // into the NIC.
Ed Tanous2c70f802020-09-28 14:29:23 -07001422 boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry =
1423 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
Johnathan Mantey01784822019-06-18 12:44:21 -07001424
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001425 for (nlohmann::json& thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001426 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001427 std::string pathString =
Ravi Tejad1d50812019-06-23 16:20:27 -05001428 "IPv4StaticAddresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001429
Johnathan Mantey01784822019-06-18 12:44:21 -07001430 if (!thisJson.is_null() && !thisJson.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301431 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001432 std::optional<std::string> address;
1433 std::optional<std::string> subnetMask;
1434 std::optional<std::string> gateway;
1435
1436 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1437 address, "SubnetMask", subnetMask,
1438 "Gateway", gateway))
Ratan Guptaf476acb2019-03-02 16:46:57 +05301439 {
1440 messages::propertyValueFormatError(
Ed Tanous71f52d92021-02-19 08:51:17 -08001441 asyncResp->res,
1442 thisJson.dump(2, ' ', true,
1443 nlohmann::json::error_handler_t::replace),
1444 pathString);
Ratan Guptaf476acb2019-03-02 16:46:57 +05301445 return;
Ratan Guptaf476acb2019-03-02 16:46:57 +05301446 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301447
Johnathan Mantey01784822019-06-18 12:44:21 -07001448 // Find the address/subnet/gateway values. Any values that are
1449 // not explicitly provided are assumed to be unmodified from the
1450 // current state of the interface. Merge existing state into the
1451 // current request.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001452 const std::string* addr = nullptr;
1453 const std::string* gw = nullptr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001454 uint8_t prefixLength = 0;
1455 bool errorInEntry = false;
1456 if (address)
Ratan Gupta9474b372019-03-01 15:13:37 +05301457 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001458 if (ipv4VerifyIpAndGetBitcount(*address))
1459 {
1460 addr = &(*address);
1461 }
1462 else
1463 {
1464 messages::propertyValueFormatError(
1465 asyncResp->res, *address, pathString + "/Address");
1466 errorInEntry = true;
1467 }
1468 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001469 else if (niciPentry != ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001470 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001471 addr = &(niciPentry->address);
Ratan Gupta9474b372019-03-01 15:13:37 +05301472 }
1473 else
1474 {
1475 messages::propertyMissing(asyncResp->res,
1476 pathString + "/Address");
Johnathan Mantey01784822019-06-18 12:44:21 -07001477 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001478 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301479
1480 if (subnetMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001481 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001482 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1483 {
1484 messages::propertyValueFormatError(
1485 asyncResp->res, *subnetMask,
1486 pathString + "/SubnetMask");
1487 errorInEntry = true;
1488 }
1489 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001490 else if (niciPentry != ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001491 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001492 if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask,
Johnathan Mantey01784822019-06-18 12:44:21 -07001493 &prefixLength))
1494 {
1495 messages::propertyValueFormatError(
Ed Tanous2c70f802020-09-28 14:29:23 -07001496 asyncResp->res, niciPentry->netmask,
Johnathan Mantey01784822019-06-18 12:44:21 -07001497 pathString + "/SubnetMask");
1498 errorInEntry = true;
1499 }
1500 }
1501 else
1502 {
1503 messages::propertyMissing(asyncResp->res,
1504 pathString + "/SubnetMask");
1505 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001506 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301507
Ratan Guptaf476acb2019-03-02 16:46:57 +05301508 if (gateway)
1509 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001510 if (ipv4VerifyIpAndGetBitcount(*gateway))
1511 {
1512 gw = &(*gateway);
1513 }
1514 else
1515 {
1516 messages::propertyValueFormatError(
1517 asyncResp->res, *gateway, pathString + "/Gateway");
1518 errorInEntry = true;
1519 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301520 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001521 else if (niciPentry != ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001522 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001523 gw = &niciPentry->gateway;
Johnathan Mantey01784822019-06-18 12:44:21 -07001524 }
1525 else
Ed Tanous4a0cb852018-10-15 07:55:04 -07001526 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001527 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001528 pathString + "/Gateway");
Johnathan Mantey01784822019-06-18 12:44:21 -07001529 errorInEntry = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001530 }
1531
Johnathan Mantey01784822019-06-18 12:44:21 -07001532 if (errorInEntry)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001533 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001534 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001535 }
1536
Ed Tanous2c70f802020-09-28 14:29:23 -07001537 if (niciPentry != ipv4Data.cend())
Ed Tanous4a0cb852018-10-15 07:55:04 -07001538 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001539 deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength,
Johnathan Mantey01784822019-06-18 12:44:21 -07001540 *gw, *addr, asyncResp);
Ed Tanous2c70f802020-09-28 14:29:23 -07001541 niciPentry =
1542 getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
Ed Tanous4a0cb852018-10-15 07:55:04 -07001543 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001544 else
1545 {
Ed Tanouscb13a392020-07-25 19:02:03 +00001546 createIPv4(ifaceId, prefixLength, *gateway, *address,
1547 asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -07001548 }
1549 entryIdx++;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001550 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001551 else
1552 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001553 if (niciPentry == ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001554 {
1555 // Requesting a DELETE/DO NOT MODIFY action for an item
1556 // that isn't present on the eth(n) interface. Input JSON is
1557 // in error, so bail out.
1558 if (thisJson.is_null())
1559 {
1560 messages::resourceCannotBeDeleted(asyncResp->res);
1561 return;
1562 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001563 messages::propertyValueFormatError(
Ed Tanous71f52d92021-02-19 08:51:17 -08001564 asyncResp->res,
1565 thisJson.dump(2, ' ', true,
1566 nlohmann::json::error_handler_t::replace),
1567 pathString);
Ed Tanous3174e4d2020-10-07 11:41:22 -07001568 return;
Johnathan Mantey01784822019-06-18 12:44:21 -07001569 }
1570
1571 if (thisJson.is_null())
1572 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001573 deleteIPv4(ifaceId, niciPentry->id, asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -07001574 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001575 if (niciPentry != ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001576 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001577 niciPentry =
1578 getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
Johnathan Mantey01784822019-06-18 12:44:21 -07001579 }
1580 entryIdx++;
1581 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001582 }
1583 }
1584
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001585 void handleStaticNameServersPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001586 const std::string& ifaceId,
1587 const std::vector<std::string>& updatedStaticNameServers,
zhanghch058d1b46d2021-04-01 11:18:24 +08001588 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001589 {
1590 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -07001591 [asyncResp](const boost::system::error_code ec) {
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001592 if (ec)
1593 {
1594 messages::internalError(asyncResp->res);
1595 return;
1596 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001597 },
1598 "xyz.openbmc_project.Network",
1599 "/xyz/openbmc_project/network/" + ifaceId,
1600 "org.freedesktop.DBus.Properties", "Set",
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -05001601 "xyz.openbmc_project.Network.EthernetInterface",
1602 "StaticNameServers",
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001603 std::variant<std::vector<std::string>>{updatedStaticNameServers});
1604 }
1605
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001606 void handleIPv6StaticAddressesPatch(
Ed Tanousf23b7292020-10-15 09:41:17 -07001607 const std::string& ifaceId, const nlohmann::json& input,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001608 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
zhanghch058d1b46d2021-04-01 11:18:24 +08001609 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001610 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001611 if (!input.is_array() || input.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001612 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001613 messages::propertyValueTypeError(
1614 asyncResp->res,
1615 input.dump(2, ' ', true,
1616 nlohmann::json::error_handler_t::replace),
1617 "IPv6StaticAddresses");
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001618 return;
1619 }
Ed Tanous271584a2019-07-09 16:24:22 -07001620 size_t entryIdx = 1;
Ed Tanous2c70f802020-09-28 14:29:23 -07001621 boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry =
1622 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
Ed Tanousf23b7292020-10-15 09:41:17 -07001623 for (const nlohmann::json& thisJson : input)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001624 {
1625 std::string pathString =
1626 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1627
Johnathan Mantey01784822019-06-18 12:44:21 -07001628 if (!thisJson.is_null() && !thisJson.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001629 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001630 std::optional<std::string> address;
1631 std::optional<uint8_t> prefixLength;
Ed Tanousf23b7292020-10-15 09:41:17 -07001632 nlohmann::json thisJsonCopy = thisJson;
1633 if (!json_util::readJson(thisJsonCopy, asyncResp->res,
1634 "Address", address, "PrefixLength",
1635 prefixLength))
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001636 {
1637 messages::propertyValueFormatError(
Ed Tanous71f52d92021-02-19 08:51:17 -08001638 asyncResp->res,
1639 thisJson.dump(2, ' ', true,
1640 nlohmann::json::error_handler_t::replace),
1641 pathString);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001642 return;
1643 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001644
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001645 const std::string* addr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001646 uint8_t prefix;
1647
1648 // Find the address and prefixLength values. Any values that are
1649 // not explicitly provided are assumed to be unmodified from the
1650 // current state of the interface. Merge existing state into the
1651 // current request.
1652 if (address)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001653 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001654 addr = &(*address);
1655 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001656 else if (niciPentry != ipv6Data.end())
Johnathan Mantey01784822019-06-18 12:44:21 -07001657 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001658 addr = &(niciPentry->address);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001659 }
1660 else
1661 {
1662 messages::propertyMissing(asyncResp->res,
1663 pathString + "/Address");
1664 return;
1665 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001666
1667 if (prefixLength)
1668 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001669 prefix = *prefixLength;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001670 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001671 else if (niciPentry != ipv6Data.end())
Johnathan Mantey01784822019-06-18 12:44:21 -07001672 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001673 prefix = niciPentry->prefixLength;
Johnathan Mantey01784822019-06-18 12:44:21 -07001674 }
1675 else
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001676 {
1677 messages::propertyMissing(asyncResp->res,
1678 pathString + "/PrefixLength");
Johnathan Mantey01784822019-06-18 12:44:21 -07001679 return;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001680 }
1681
Ed Tanous2c70f802020-09-28 14:29:23 -07001682 if (niciPentry != ipv6Data.end())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001683 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001684 deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr,
Johnathan Mantey01784822019-06-18 12:44:21 -07001685 asyncResp);
Ed Tanous2c70f802020-09-28 14:29:23 -07001686 niciPentry =
1687 getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
Johnathan Mantey01784822019-06-18 12:44:21 -07001688 }
1689 else
1690 {
1691 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1692 }
1693 entryIdx++;
1694 }
1695 else
1696 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001697 if (niciPentry == ipv6Data.end())
Johnathan Mantey01784822019-06-18 12:44:21 -07001698 {
1699 // Requesting a DELETE/DO NOT MODIFY action for an item
1700 // that isn't present on the eth(n) interface. Input JSON is
1701 // in error, so bail out.
1702 if (thisJson.is_null())
1703 {
1704 messages::resourceCannotBeDeleted(asyncResp->res);
1705 return;
1706 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001707 messages::propertyValueFormatError(
Ed Tanous71f52d92021-02-19 08:51:17 -08001708 asyncResp->res,
1709 thisJson.dump(2, ' ', true,
1710 nlohmann::json::error_handler_t::replace),
1711 pathString);
Ed Tanous3174e4d2020-10-07 11:41:22 -07001712 return;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001713 }
1714
Johnathan Mantey01784822019-06-18 12:44:21 -07001715 if (thisJson.is_null())
1716 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001717 deleteIPv6(ifaceId, niciPentry->id, asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -07001718 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001719 if (niciPentry != ipv6Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001720 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001721 niciPentry =
1722 getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
Johnathan Mantey01784822019-06-18 12:44:21 -07001723 }
1724 entryIdx++;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001725 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001726 }
1727 }
1728
Ed Tanous0f74e642018-11-12 15:17:05 -08001729 void parseInterfaceData(
zhanghch058d1b46d2021-04-01 11:18:24 +08001730 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1731 const std::string& ifaceId, const EthernetInterfaceData& ethData,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001732 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1733 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001734 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001735 constexpr const std::array<const char*, 1> inventoryForEthernet = {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001736 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1737
Ed Tanous2c70f802020-09-28 14:29:23 -07001738 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
Ed Tanous81ce6092020-12-17 16:54:55 +00001739 jsonResponse["Id"] = ifaceId;
Ed Tanous2c70f802020-09-28 14:29:23 -07001740 jsonResponse["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00001741 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
Ed Tanous2c70f802020-09-28 14:29:23 -07001742 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001743
1744 auto health = std::make_shared<HealthPopulate>(asyncResp);
1745
1746 crow::connections::systemBus->async_method_call(
1747 [health](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001748 std::vector<std::string>& resp) {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001749 if (ec)
1750 {
1751 return;
1752 }
1753
1754 health->inventory = std::move(resp);
1755 },
1756 "xyz.openbmc_project.ObjectMapper",
1757 "/xyz/openbmc_project/object_mapper",
1758 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1759 int32_t(0), inventoryForEthernet);
1760
1761 health->populate();
1762
1763 if (ethData.nicEnabled)
Ed Tanous029573d2019-02-01 10:57:49 -08001764 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001765 jsonResponse["LinkStatus"] = "LinkUp";
1766 jsonResponse["Status"]["State"] = "Enabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001767 }
1768 else
1769 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001770 jsonResponse["LinkStatus"] = "NoLink";
1771 jsonResponse["Status"]["State"] = "Disabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001772 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08001773
Ed Tanous2c70f802020-09-28 14:29:23 -07001774 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
1775 jsonResponse["SpeedMbps"] = ethData.speed;
1776 jsonResponse["MACAddress"] = ethData.mac_address;
1777 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001778 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
Ed Tanous2c70f802020-09-28 14:29:23 -07001779 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1780 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1781 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001782
Ed Tanous2c70f802020-09-28 14:29:23 -07001783 jsonResponse["DHCPv6"]["OperatingMode"] =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001784 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1785 : "Disabled";
Ed Tanous2c70f802020-09-28 14:29:23 -07001786 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1787 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1788 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +05301789
Ed Tanous4a0cb852018-10-15 07:55:04 -07001790 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001791 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001792 jsonResponse["HostName"] = ethData.hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301793
1794 // When domain name is empty then it means, that it is a network
1795 // without domain names, and the host name itself must be treated as
1796 // FQDN
Ed Tanousf23b7292020-10-15 09:41:17 -07001797 std::string fqdn = ethData.hostname;
Jennifer Leed24bfc72019-03-05 13:03:37 -08001798 if (!ethData.domainnames.empty())
1799 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001800 fqdn += "." + ethData.domainnames[0];
Jennifer Leed24bfc72019-03-05 13:03:37 -08001801 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001802 jsonResponse["FQDN"] = fqdn;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001803 }
1804
Ed Tanous2c70f802020-09-28 14:29:23 -07001805 jsonResponse["VLANs"] = {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001806 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00001807 ifaceId + "/VLANs"}};
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001808
Ed Tanous2c70f802020-09-28 14:29:23 -07001809 jsonResponse["NameServers"] = ethData.nameServers;
1810 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001811
Ed Tanous2c70f802020-09-28 14:29:23 -07001812 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1813 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1814 ipv4Array = nlohmann::json::array();
1815 ipv4StaticArray = nlohmann::json::array();
1816 for (auto& ipv4Config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001817 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001818
Ed Tanous2c70f802020-09-28 14:29:23 -07001819 std::string gatewayStr = ipv4Config.gateway;
Ravi Tejad1d50812019-06-23 16:20:27 -05001820 if (gatewayStr.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001821 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001822 gatewayStr = "0.0.0.0";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001823 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001824
Ed Tanous2c70f802020-09-28 14:29:23 -07001825 ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
1826 {"SubnetMask", ipv4Config.netmask},
1827 {"Address", ipv4Config.address},
1828 {"Gateway", gatewayStr}});
1829 if (ipv4Config.origin == "Static")
Ravi Tejad1d50812019-06-23 16:20:27 -05001830 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001831 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
1832 {"SubnetMask", ipv4Config.netmask},
1833 {"Address", ipv4Config.address},
1834 {"Gateway", gatewayStr}});
Ravi Tejad1d50812019-06-23 16:20:27 -05001835 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001836 }
1837
Ravi Teja7ea79e52021-03-02 08:38:05 +05301838 std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
1839 if (ipv6GatewayStr.empty())
1840 {
1841 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1842 }
1843
1844 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001845
Ed Tanous2c70f802020-09-28 14:29:23 -07001846 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1847 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1848 ipv6Array = nlohmann::json::array();
1849 ipv6StaticArray = nlohmann::json::array();
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001850 nlohmann::json& ipv6AddrPolicyTable =
Ed Tanous2c70f802020-09-28 14:29:23 -07001851 jsonResponse["IPv6AddressPolicyTable"];
Johnathan Mantey7f2e23e2020-05-14 13:36:52 -07001852 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous2c70f802020-09-28 14:29:23 -07001853 for (auto& ipv6Config : ipv6Data)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001854 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001855 ipv6Array.push_back({{"Address", ipv6Config.address},
1856 {"PrefixLength", ipv6Config.prefixLength},
1857 {"AddressOrigin", ipv6Config.origin},
1858 {"AddressState", nullptr}});
1859 if (ipv6Config.origin == "Static")
Johnathan Mantey01784822019-06-18 12:44:21 -07001860 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001861 ipv6StaticArray.push_back(
1862 {{"Address", ipv6Config.address},
1863 {"PrefixLength", ipv6Config.prefixLength},
1864 {"AddressOrigin", ipv6Config.origin},
Johnathan Mantey5fd16e42020-05-13 15:14:47 -07001865 {"AddressState", nullptr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001866 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001867 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001868 }
1869
1870 /**
1871 * Functions triggers appropriate requests on DBus
1872 */
zhanghch058d1b46d2021-04-01 11:18:24 +08001873 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1874 const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001875 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001876 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001877 if (params.size() != 1)
1878 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001879 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001880 return;
1881 }
1882
Ed Tanous4a0cb852018-10-15 07:55:04 -07001883 getEthernetIfaceData(
1884 params[0],
Ed Tanous2c70f802020-09-28 14:29:23 -07001885 [this, asyncResp, ifaceId{std::string(params[0])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001886 const bool& success, const EthernetInterfaceData& ethData,
1887 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1888 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001889 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001890 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001891 // TODO(Pawel)consider distinguish between non existing
1892 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001893 messages::resourceNotFound(asyncResp->res,
Ed Tanous2c70f802020-09-28 14:29:23 -07001894 "EthernetInterface", ifaceId);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001895 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001896 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07001897
Ed Tanous0f74e642018-11-12 15:17:05 -08001898 asyncResp->res.jsonValue["@odata.type"] =
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001899 "#EthernetInterface.v1_4_1.EthernetInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001900 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1901 asyncResp->res.jsonValue["Description"] =
1902 "Management Network Interface";
1903
Ed Tanous2c70f802020-09-28 14:29:23 -07001904 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data,
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001905 ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001906 });
1907 }
1908
zhanghch058d1b46d2021-04-01 11:18:24 +08001909 void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1910 const crow::Request& req,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001911 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001912 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001913
Ed Tanous1abe55e2018-09-05 08:30:59 -07001914 if (params.size() != 1)
1915 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001916 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001917 return;
1918 }
1919
Ed Tanous2c70f802020-09-28 14:29:23 -07001920 const std::string& ifaceId = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001921
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001922 std::optional<std::string> hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301923 std::optional<std::string> fqdn;
Ratan Guptad5776652019-03-03 08:47:22 +05301924 std::optional<std::string> macAddress;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001925 std::optional<std::string> ipv6DefaultGateway;
Ravi Tejad1d50812019-06-23 16:20:27 -05001926 std::optional<nlohmann::json> ipv4StaticAddresses;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001927 std::optional<nlohmann::json> ipv6StaticAddresses;
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001928 std::optional<std::vector<std::string>> staticNameServers;
Jennifer Leeda131a92019-04-24 15:13:55 -07001929 std::optional<nlohmann::json> dhcpv4;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001930 std::optional<nlohmann::json> dhcpv6;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001931 std::optional<bool> interfaceEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001932 DHCPParameters v4dhcpParms;
1933 DHCPParameters v6dhcpParms;
Ed Tanous0627a2c2018-11-29 17:09:23 -08001934
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001935 if (!json_util::readJson(
zhanghch058d1b46d2021-04-01 11:18:24 +08001936 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301937 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1938 macAddress, "StaticNameServers", staticNameServers,
1939 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1940 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1941 "InterfaceEnabled", interfaceEnabled))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001942 {
1943 return;
1944 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001945 if (dhcpv4)
1946 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001947 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001948 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1949 v4dhcpParms.useDNSServers, "UseNTPServers",
1950 v4dhcpParms.useNTPServers, "UseDomainName",
1951 v4dhcpParms.useUseDomainName))
1952 {
1953 return;
1954 }
1955 }
1956
1957 if (dhcpv6)
1958 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001959 if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001960 v6dhcpParms.dhcpv6OperatingMode,
1961 "UseDNSServers", v6dhcpParms.useDNSServers,
1962 "UseNTPServers", v6dhcpParms.useNTPServers,
1963 "UseDomainName",
1964 v6dhcpParms.useUseDomainName))
1965 {
1966 return;
1967 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001968 }
1969
Johnathan Mantey01784822019-06-18 12:44:21 -07001970 // Get single eth interface data, and call the below callback for
1971 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001972 getEthernetIfaceData(
Ed Tanous2c70f802020-09-28 14:29:23 -07001973 ifaceId,
1974 [this, asyncResp, ifaceId, hostname = std::move(hostname),
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301975 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
Ravi Tejad1d50812019-06-23 16:20:27 -05001976 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001977 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001978 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001979 staticNameServers = std::move(staticNameServers),
1980 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
1981 v4dhcpParms = std::move(v4dhcpParms),
Ed Tanousf23b7292020-10-15 09:41:17 -07001982 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001983 const bool& success, const EthernetInterfaceData& ethData,
1984 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1985 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001986 if (!success)
1987 {
1988 // ... otherwise return error
1989 // TODO(Pawel)consider distinguish between non existing
1990 // object, and other errors
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001991 messages::resourceNotFound(asyncResp->res,
Ed Tanous2c70f802020-09-28 14:29:23 -07001992 "Ethernet Interface", ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001993 return;
1994 }
1995
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001996 if (dhcpv4 || dhcpv6)
1997 {
Ed Tanousf23b7292020-10-15 09:41:17 -07001998 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
1999 asyncResp);
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002000 }
2001
Ed Tanous0627a2c2018-11-29 17:09:23 -08002002 if (hostname)
2003 {
2004 handleHostnamePatch(*hostname, asyncResp);
2005 }
2006
Joshi-Mansiab6554f2020-03-10 18:33:36 +05302007 if (fqdn)
2008 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002009 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
Joshi-Mansiab6554f2020-03-10 18:33:36 +05302010 }
2011
Ratan Guptad5776652019-03-03 08:47:22 +05302012 if (macAddress)
2013 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002014 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
Ratan Guptad5776652019-03-03 08:47:22 +05302015 }
2016
Ravi Tejad1d50812019-06-23 16:20:27 -05002017 if (ipv4StaticAddresses)
2018 {
Ed Tanous537174c2018-12-10 15:09:31 -08002019 // TODO(ed) for some reason the capture of ipv4Addresses
Johnathan Mantey01784822019-06-18 12:44:21 -07002020 // above is returning a const value, not a non-const
2021 // value. This doesn't really work for us, as we need to
2022 // be able to efficiently move out the intermedia
2023 // nlohmann::json objects. This makes a copy of the
2024 // structure, and operates on that, but could be done
2025 // more efficiently
Ed Tanousf23b7292020-10-15 09:41:17 -07002026 nlohmann::json ipv4Static = *ipv4StaticAddresses;
Ed Tanous2c70f802020-09-28 14:29:23 -07002027 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
Ravi Tejad1d50812019-06-23 16:20:27 -05002028 asyncResp);
Ed Tanous0627a2c2018-11-29 17:09:23 -08002029 }
2030
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05002031 if (staticNameServers)
2032 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002033 handleStaticNameServersPatch(ifaceId, *staticNameServers,
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05002034 asyncResp);
2035 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05002036
2037 if (ipv6DefaultGateway)
2038 {
2039 messages::propertyNotWritable(asyncResp->res,
2040 "IPv6DefaultGateway");
2041 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002042
2043 if (ipv6StaticAddresses)
2044 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002045 nlohmann::json ipv6Static = *ipv6StaticAddresses;
Ed Tanous2c70f802020-09-28 14:29:23 -07002046 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
Johnathan Mantey01784822019-06-18 12:44:21 -07002047 ipv6Data, asyncResp);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002048 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002049
2050 if (interfaceEnabled)
2051 {
2052 setEthernetInterfaceBoolProperty(
Ed Tanous2c70f802020-09-28 14:29:23 -07002053 ifaceId, "NICEnabled", *interfaceEnabled, asyncResp);
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002054 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002055 });
2056 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002057};
2058
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002059/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07002060 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
2061 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002062 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002063class VlanNetworkInterface : public Node
2064{
2065 public:
2066 /*
2067 * Default Constructor
2068 */
Ed Tanous52cc1122020-07-18 13:51:21 -07002069 VlanNetworkInterface(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002070 Node(app,
Gunnar Mills7af91512020-04-14 22:16:57 -05002071 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/",
Ed Tanous4a0cb852018-10-15 07:55:04 -07002072 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002073 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002074 entityPrivileges = {
2075 {boost::beast::http::verb::get, {{"Login"}}},
2076 {boost::beast::http::verb::head, {{"Login"}}},
2077 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2078 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2079 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2080 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002081 }
2082
Ed Tanous1abe55e2018-09-05 08:30:59 -07002083 private:
Ed Tanous81ce6092020-12-17 16:54:55 +00002084 void parseInterfaceData(nlohmann::json& jsonResponse,
2085 const std::string& parentIfaceId,
2086 const std::string& ifaceId,
Ed Tanouscb13a392020-07-25 19:02:03 +00002087 const EthernetInterfaceData& ethData)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002088 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002089 // Fill out obvious data...
Ed Tanous81ce6092020-12-17 16:54:55 +00002090 jsonResponse["Id"] = ifaceId;
2091 jsonResponse["@odata.id"] =
2092 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parentIfaceId +
2093 "/VLANs/" + ifaceId;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002094
Ed Tanous81ce6092020-12-17 16:54:55 +00002095 jsonResponse["VLANEnable"] = true;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002096 if (!ethData.vlan_id.empty())
Ed Tanous4a0cb852018-10-15 07:55:04 -07002097 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002098 jsonResponse["VLANId"] = ethData.vlan_id.back();
Ed Tanous4a0cb852018-10-15 07:55:04 -07002099 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07002100 }
2101
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002102 bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002103 {
2104 if (!boost::starts_with(iface, parent + "_"))
2105 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002106 return false;
2107 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07002108 return true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002109 }
2110
2111 /**
2112 * Functions triggers appropriate requests on DBus
2113 */
zhanghch058d1b46d2021-04-01 11:18:24 +08002114 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2115 const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002116 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002117 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002118 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07002119 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
2120 // Check if there is required param, truly entering this shall be
2121 // impossible.
2122 if (params.size() != 2)
2123 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002124 messages::internalError(asyncResp->res);
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002125 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002126 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002127
Ed Tanous2c70f802020-09-28 14:29:23 -07002128 const std::string& parentIfaceId = params[0];
2129 const std::string& ifaceId = params[1];
zhanghch058d1b46d2021-04-01 11:18:24 +08002130 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanous0f74e642018-11-12 15:17:05 -08002131 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
zhanghch058d1b46d2021-04-01 11:18:24 +08002132 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002133
Ed Tanous2c70f802020-09-28 14:29:23 -07002134 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002135 {
2136 return;
2137 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002138
Johnathan Mantey01784822019-06-18 12:44:21 -07002139 // Get single eth interface data, and call the below callback for
2140 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07002141 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002142 params[1],
Ed Tanous2c70f802020-09-28 14:29:23 -07002143 [this, asyncResp, parentIfaceId{std::string(params[0])},
2144 ifaceId{std::string(params[1])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002145 const bool& success, const EthernetInterfaceData& ethData,
Ed Tanouscb13a392020-07-25 19:02:03 +00002146 const boost::container::flat_set<IPv4AddressData>&,
2147 const boost::container::flat_set<IPv6AddressData>&) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002148 if (success && ethData.vlan_id.size() != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002149 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002150 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
2151 ifaceId, ethData);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002152 }
2153 else
2154 {
2155 // ... otherwise return error
2156 // TODO(Pawel)consider distinguish between non existing
2157 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07002158 messages::resourceNotFound(
Ed Tanous2c70f802020-09-28 14:29:23 -07002159 asyncResp->res, "VLAN Network Interface", ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002160 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002161 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002162 }
2163
zhanghch058d1b46d2021-04-01 11:18:24 +08002164 void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2165 const crow::Request& req,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002166 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002167 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002168
Ed Tanous1abe55e2018-09-05 08:30:59 -07002169 if (params.size() != 2)
2170 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002171 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002172 return;
2173 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002174
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002175 const std::string& parentIfaceId = params[0];
2176 const std::string& ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002177
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002178 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002179 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002180 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2181 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002182 return;
2183 }
2184
Ed Tanous0627a2c2018-11-29 17:09:23 -08002185 bool vlanEnable = false;
Andrew Geissler38268fa2020-05-16 14:22:18 -05002186 uint32_t vlanId = 0;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002187
zhanghch058d1b46d2021-04-01 11:18:24 +08002188 if (!json_util::readJson(req, asyncResp->res, "VLANEnable", vlanEnable,
2189 "VLANId", vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002190 {
2191 return;
2192 }
2193
Johnathan Mantey01784822019-06-18 12:44:21 -07002194 // Get single eth interface data, and call the below callback for
2195 // JSON preparation
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002196 getEthernetIfaceData(
2197 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002198 [asyncResp, parentIfaceId{std::string(params[0])},
Ed Tanouscb13a392020-07-25 19:02:03 +00002199 ifaceId{std::string(params[1])}, &vlanEnable,
2200 &vlanId](const bool& success, const EthernetInterfaceData& ethData,
2201 const boost::container::flat_set<IPv4AddressData>&,
2202 const boost::container::flat_set<IPv6AddressData>&) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002203 if (success && !ethData.vlan_id.empty())
Sunitha Harish08244d02019-04-01 03:57:25 -05002204 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002205 auto callback =
2206 [asyncResp](const boost::system::error_code ec) {
2207 if (ec)
2208 {
2209 messages::internalError(asyncResp->res);
2210 }
2211 };
2212
2213 if (vlanEnable == true)
2214 {
2215 crow::connections::systemBus->async_method_call(
2216 std::move(callback), "xyz.openbmc_project.Network",
2217 "/xyz/openbmc_project/network/" + ifaceId,
2218 "org.freedesktop.DBus.Properties", "Set",
2219 "xyz.openbmc_project.Network.VLAN", "Id",
2220 std::variant<uint32_t>(vlanId));
2221 }
2222 else
2223 {
2224 BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2225 "vlan interface";
2226 crow::connections::systemBus->async_method_call(
2227 std::move(callback), "xyz.openbmc_project.Network",
2228 std::string("/xyz/openbmc_project/network/") +
2229 ifaceId,
2230 "xyz.openbmc_project.Object.Delete", "Delete");
2231 }
Sunitha Harish08244d02019-04-01 03:57:25 -05002232 }
2233 else
2234 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002235 // TODO(Pawel)consider distinguish between non existing
2236 // object, and other errors
2237 messages::resourceNotFound(
2238 asyncResp->res, "VLAN Network Interface", ifaceId);
2239 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002240 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002241 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002242 }
2243
zhanghch058d1b46d2021-04-01 11:18:24 +08002244 void doDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2245 const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002246 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002247 {
2248 if (params.size() != 2)
2249 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002250 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002251 return;
2252 }
2253
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002254 const std::string& parentIfaceId = params[0];
2255 const std::string& ifaceId = params[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -07002256
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002257 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002258 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002259 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2260 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002261 return;
2262 }
2263
Johnathan Mantey01784822019-06-18 12:44:21 -07002264 // Get single eth interface data, and call the below callback for
2265 // JSON preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002266 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002267 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002268 [asyncResp, parentIfaceId{std::string(params[0])},
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002269 ifaceId{std::string(params[1])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002270 const bool& success, const EthernetInterfaceData& ethData,
Ed Tanouscb13a392020-07-25 19:02:03 +00002271 const boost::container::flat_set<IPv4AddressData>&,
2272 const boost::container::flat_set<IPv6AddressData>&) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002273 if (success && !ethData.vlan_id.empty())
Jason M. Billsf12894f2018-10-09 12:45:45 -07002274 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002275 auto callback =
2276 [asyncResp](const boost::system::error_code ec) {
2277 if (ec)
2278 {
2279 messages::internalError(asyncResp->res);
2280 }
2281 };
2282 crow::connections::systemBus->async_method_call(
2283 std::move(callback), "xyz.openbmc_project.Network",
2284 std::string("/xyz/openbmc_project/network/") + ifaceId,
2285 "xyz.openbmc_project.Object.Delete", "Delete");
2286 }
2287 else
2288 {
2289 // ... otherwise return error
2290 // TODO(Pawel)consider distinguish between non existing
2291 // object, and other errors
2292 messages::resourceNotFound(
2293 asyncResp->res, "VLAN Network Interface", ifaceId);
2294 }
2295 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002296 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002297};
2298
2299/**
2300 * VlanNetworkInterfaceCollection derived class for delivering
2301 * VLANNetworkInterface Collection Schema
2302 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002303class VlanNetworkInterfaceCollection : public Node
2304{
2305 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002306 VlanNetworkInterfaceCollection(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002307 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
2308 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002309 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002310 entityPrivileges = {
2311 {boost::beast::http::verb::get, {{"Login"}}},
2312 {boost::beast::http::verb::head, {{"Login"}}},
2313 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2314 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2315 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2316 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002317 }
2318
Ed Tanous1abe55e2018-09-05 08:30:59 -07002319 private:
2320 /**
2321 * Functions triggers appropriate requests on DBus
2322 */
zhanghch058d1b46d2021-04-01 11:18:24 +08002323 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2324 const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002325 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002326 {
2327 if (params.size() != 1)
2328 {
2329 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07002330 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002331 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07002332 }
2333
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002334 const std::string& rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002335
Ed Tanous4a0cb852018-10-15 07:55:04 -07002336 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07002337 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002338 getEthernetIfaceList(
Ed Tanous43b761d2019-02-13 20:10:56 -08002339 [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002340 const bool& success,
Ed Tanous81ce6092020-12-17 16:54:55 +00002341 const boost::container::flat_set<std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002342 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002343 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002344 messages::internalError(asyncResp->res);
2345 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002346 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002347
Ed Tanous81ce6092020-12-17 16:54:55 +00002348 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
Ed Tanous4c9afe42019-05-03 16:59:57 -07002349 {
2350 messages::resourceNotFound(asyncResp->res,
2351 "VLanNetworkInterfaceCollection",
2352 rootInterfaceName);
2353 return;
2354 }
2355
Ed Tanous0f74e642018-11-12 15:17:05 -08002356 asyncResp->res.jsonValue["@odata.type"] =
2357 "#VLanNetworkInterfaceCollection."
2358 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002359 asyncResp->res.jsonValue["Name"] =
2360 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002361
Ed Tanous2c70f802020-09-28 14:29:23 -07002362 nlohmann::json ifaceArray = nlohmann::json::array();
Jason M. Billsf12894f2018-10-09 12:45:45 -07002363
Ed Tanous81ce6092020-12-17 16:54:55 +00002364 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07002365 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002366 if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002367 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002368 std::string path =
2369 "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2370 path += rootInterfaceName;
2371 path += "/VLANs/";
2372 path += ifaceItem;
2373 ifaceArray.push_back({{"@odata.id", std::move(path)}});
Jason M. Billsf12894f2018-10-09 12:45:45 -07002374 }
2375 }
2376
Jason M. Billsf12894f2018-10-09 12:45:45 -07002377 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07002378 ifaceArray.size();
2379 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
Jason M. Billsf12894f2018-10-09 12:45:45 -07002380 asyncResp->res.jsonValue["@odata.id"] =
2381 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2382 rootInterfaceName + "/VLANs";
2383 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002384 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002385
zhanghch058d1b46d2021-04-01 11:18:24 +08002386 void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2387 const crow::Request& req,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002388 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002389 {
2390 if (params.size() != 1)
2391 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002392 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002393 return;
2394 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002395 bool vlanEnable = false;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002396 uint32_t vlanId = 0;
zhanghch058d1b46d2021-04-01 11:18:24 +08002397 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId,
2398 "VLANEnable", vlanEnable))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002399 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002400 return;
2401 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002402 // Need both vlanId and vlanEnable to service this request
2403 if (!vlanId)
2404 {
2405 messages::propertyMissing(asyncResp->res, "VLANId");
2406 }
2407 if (!vlanEnable)
2408 {
2409 messages::propertyMissing(asyncResp->res, "VLANEnable");
2410 }
Ed Tanous271584a2019-07-09 16:24:22 -07002411 if (static_cast<bool>(vlanId) ^ vlanEnable)
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002412 {
2413 return;
2414 }
2415
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002416 const std::string& rootInterfaceName = params[0];
Ed Tanous4a0cb852018-10-15 07:55:04 -07002417 auto callback = [asyncResp](const boost::system::error_code ec) {
2418 if (ec)
2419 {
2420 // TODO(ed) make more consistent error messages based on
2421 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07002422 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002423 return;
2424 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002425 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002426 };
2427 crow::connections::systemBus->async_method_call(
2428 std::move(callback), "xyz.openbmc_project.Network",
2429 "/xyz/openbmc_project/network",
2430 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
Ed Tanous0627a2c2018-11-29 17:09:23 -08002431 rootInterfaceName, vlanId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002432 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002433};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002434} // namespace redfish