blob: 8d7eddc9e8f07f67bbb3f7fd26dce1b5a61c0789 [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>
Ed Tanousa24526d2018-12-10 15:17:59 -080023#include <optional>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020024#include <utils/json_utils.hpp>
Ed Tanousabf2add2019-01-22 16:40:12 -080025#include <variant>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010026
Ed Tanous1abe55e2018-09-05 08:30:59 -070027namespace redfish
28{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010029
30/**
31 * DBus types primitives for several generic DBus interfaces
32 * TODO(Pawel) consider move this to separate file into boost::dbus
33 */
Ed Tanousaa2e59c2018-04-12 12:17:20 -070034using PropertiesMapType = boost::container::flat_map<
Ed Tanousabf2add2019-01-22 16:40:12 -080035 std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
36 int32_t, uint32_t, int64_t, uint64_t, double>>;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010037
Ed Tanous4a0cb852018-10-15 07:55:04 -070038using GetManagedObjects = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070039 sdbusplus::message::object_path,
Ed Tanous4a0cb852018-10-15 07:55:04 -070040 std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070041 std::string,
42 boost::container::flat_map<
Ed Tanous029573d2019-02-01 10:57:49 -080043 std::string, sdbusplus::message::variant<
44 std::string, bool, uint8_t, int16_t, uint16_t,
45 int32_t, uint32_t, int64_t, uint64_t, double,
46 std::vector<std::string>>>>>>>;
Ed Tanous4a0cb852018-10-15 07:55:04 -070047
48enum class LinkType
49{
50 Local,
51 Global
52};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010053
54/**
55 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010056 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070057struct IPv4AddressData
58{
59 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070060 std::string address;
61 std::string domain;
62 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070063 std::string netmask;
64 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070065 LinkType linktype;
66
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 bool operator<(const IPv4AddressData &obj) const
68 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070069 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010071};
72
73/**
74 * Structure for keeping basic single Ethernet Interface information
75 * available from DBus
76 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070077struct EthernetInterfaceData
78{
Ed Tanous4a0cb852018-10-15 07:55:04 -070079 uint32_t speed;
80 bool auto_neg;
81 std::string hostname;
82 std::string default_gateway;
83 std::string mac_address;
Ed Tanousa24526d2018-12-10 15:17:59 -080084 std::optional<uint32_t> vlan_id;
Ed Tanous029573d2019-02-01 10:57:49 -080085 std::vector<std::string> nameservers;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010086};
87
Ed Tanous4a0cb852018-10-15 07:55:04 -070088// Helper function that changes bits netmask notation (i.e. /24)
89// into full dot notation
90inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -070091{
Ed Tanous4a0cb852018-10-15 07:55:04 -070092 uint32_t value = 0xffffffff << (32 - bits);
93 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
94 std::to_string((value >> 16) & 0xff) + "." +
95 std::to_string((value >> 8) & 0xff) + "." +
96 std::to_string(value & 0xff);
97 return netmask;
98}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010099
Ed Tanous4a0cb852018-10-15 07:55:04 -0700100inline std::string
101 translateAddressOriginDbusToRedfish(const std::string &inputOrigin,
102 bool isIPv4)
103{
104 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700105 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700106 return "Static";
107 }
108 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
109 {
110 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700112 return "IPv4LinkLocal";
113 }
114 else
115 {
116 return "LinkLocal";
117 }
118 }
119 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
120 {
121 if (isIPv4)
122 {
123 return "DHCP";
124 }
125 else
126 {
127 return "DHCPv6";
128 }
129 }
130 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
131 {
132 return "SLAAC";
133 }
134 return "";
135}
136
137inline std::string
138 translateAddressOriginRedfishToDbus(const std::string &inputOrigin)
139{
140 if (inputOrigin == "Static")
141 {
142 return "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
143 }
144 if (inputOrigin == "DHCP" || inputOrigin == "DHCPv6")
145 {
146 return "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
147 }
148 if (inputOrigin == "IPv4LinkLocal" || inputOrigin == "LinkLocal")
149 {
150 return "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal";
151 }
152 if (inputOrigin == "SLAAC")
153 {
154 return "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC";
155 }
156 return "";
157}
158
159inline void extractEthernetInterfaceData(const std::string &ethiface_id,
160 const GetManagedObjects &dbus_data,
161 EthernetInterfaceData &ethData)
162{
163 for (const auto &objpath : dbus_data)
164 {
Ed Tanous029573d2019-02-01 10:57:49 -0800165 for (const auto &ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700166 {
Ed Tanous029573d2019-02-01 10:57:49 -0800167 if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700168 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700169 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700170 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700171 for (const auto &propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700172 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700173 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700174 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700175 const std::string *mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800176 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700177 if (mac != nullptr)
178 {
179 ethData.mac_address = *mac;
180 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700181 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700182 }
183 }
184 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
185 {
186 for (const auto &propertyPair : ifacePair.second)
187 {
188 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700189 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800190 const uint32_t *id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800191 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700192 if (id != nullptr)
193 {
194 ethData.vlan_id = *id;
195 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700196 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700197 }
198 }
199 else if (ifacePair.first ==
200 "xyz.openbmc_project.Network.EthernetInterface")
201 {
202 for (const auto &propertyPair : ifacePair.second)
203 {
204 if (propertyPair.first == "AutoNeg")
205 {
206 const bool *auto_neg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800207 std::get_if<bool>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700208 if (auto_neg != nullptr)
209 {
210 ethData.auto_neg = *auto_neg;
211 }
212 }
213 else if (propertyPair.first == "Speed")
214 {
215 const uint32_t *speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800216 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700217 if (speed != nullptr)
218 {
219 ethData.speed = *speed;
220 }
221 }
Ed Tanous029573d2019-02-01 10:57:49 -0800222 else if (propertyPair.first == "NameServers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700223 {
Ed Tanous029573d2019-02-01 10:57:49 -0800224 const std::vector<std::string> *nameservers =
225 sdbusplus::message::variant_ns::get_if<
226 std::vector<std::string>>(
227 &propertyPair.second);
228 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700229 {
Ed Tanous029573d2019-02-01 10:57:49 -0800230 ethData.nameservers = std::move(*nameservers);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700231 }
232 }
Ed Tanous029573d2019-02-01 10:57:49 -0800233 }
234 }
235 }
236 // System configuration shows up in the global namespace, so no need
237 // to check eth number
238 if (ifacePair.first ==
239 "xyz.openbmc_project.Network.SystemConfiguration")
240 {
241 for (const auto &propertyPair : ifacePair.second)
242 {
243 if (propertyPair.first == "HostName")
244 {
245 const std::string *hostname =
246 sdbusplus::message::variant_ns::get_if<std::string>(
247 &propertyPair.second);
248 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700249 {
Ed Tanous029573d2019-02-01 10:57:49 -0800250 ethData.hostname = *hostname;
251 }
252 }
253 else if (propertyPair.first == "DefaultGateway")
254 {
255 const std::string *defaultGateway =
256 sdbusplus::message::variant_ns::get_if<std::string>(
257 &propertyPair.second);
258 if (defaultGateway != nullptr)
259 {
260 ethData.default_gateway = *defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700261 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700262 }
263 }
264 }
265 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700266 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700267}
268
269// Helper function that extracts data for single ethernet ipv4 address
270inline void
271 extractIPData(const std::string &ethiface_id,
272 const GetManagedObjects &dbus_data,
273 boost::container::flat_set<IPv4AddressData> &ipv4_config)
274{
275 const std::string ipv4PathStart =
276 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
277
278 // Since there might be several IPv4 configurations aligned with
279 // single ethernet interface, loop over all of them
280 for (const auto &objpath : dbus_data)
281 {
282 // Check if proper pattern for object path appears
283 if (boost::starts_with(objpath.first.str, ipv4PathStart))
284 {
285 for (auto &interface : objpath.second)
286 {
287 if (interface.first == "xyz.openbmc_project.Network.IP")
288 {
289 // Instance IPv4AddressData structure, and set as
290 // appropriate
291 std::pair<
292 boost::container::flat_set<IPv4AddressData>::iterator,
293 bool>
294 it = ipv4_config.insert(
295 {objpath.first.str.substr(ipv4PathStart.size())});
296 IPv4AddressData &ipv4_address = *it.first;
297 for (auto &property : interface.second)
298 {
299 if (property.first == "Address")
300 {
301 const std::string *address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800302 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700303 if (address != nullptr)
304 {
305 ipv4_address.address = *address;
306 }
307 }
308 else if (property.first == "Gateway")
309 {
310 const std::string *gateway =
Ed Tanousabf2add2019-01-22 16:40:12 -0800311 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700312 if (gateway != nullptr)
313 {
314 ipv4_address.gateway = *gateway;
315 }
316 }
317 else if (property.first == "Origin")
318 {
319 const std::string *origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800320 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700321 if (origin != nullptr)
322 {
323 ipv4_address.origin =
324 translateAddressOriginDbusToRedfish(*origin,
325 true);
326 }
327 }
328 else if (property.first == "PrefixLength")
329 {
330 const uint8_t *mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800331 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700332 if (mask != nullptr)
333 {
334 // convert it to the string
335 ipv4_address.netmask = getNetmask(*mask);
336 }
337 }
338 else
339 {
340 BMCWEB_LOG_ERROR
341 << "Got extra property: " << property.first
342 << " on the " << objpath.first.str << " object";
343 }
344 }
345 // Check if given address is local, or global
346 ipv4_address.linktype =
347 boost::starts_with(ipv4_address.address, "169.254.")
348 ? LinkType::Global
349 : LinkType::Local;
350 }
351 }
352 }
353 }
354}
355
356/**
357 * @brief Sets given Id on the given VLAN interface through D-Bus
358 *
359 * @param[in] ifaceId Id of VLAN interface that should be modified
360 * @param[in] inputVlanId New ID of the VLAN
361 * @param[in] callback Function that will be called after the operation
362 *
363 * @return None.
364 */
365template <typename CallbackFunc>
366void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId,
367 CallbackFunc &&callback)
368{
369 crow::connections::systemBus->async_method_call(
370 callback, "xyz.openbmc_project.Network",
371 std::string("/xyz/openbmc_project/network/") + ifaceId,
372 "org.freedesktop.DBus.Properties", "Set",
373 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanousabf2add2019-01-22 16:40:12 -0800374 std::variant<uint32_t>(inputVlanId));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700375}
376
377/**
378 * @brief Helper function that verifies IP address to check if it is in
379 * proper format. If bits pointer is provided, also calculates active
380 * bit count for Subnet Mask.
381 *
382 * @param[in] ip IP that will be verified
383 * @param[out] bits Calculated mask in bits notation
384 *
385 * @return true in case of success, false otherwise
386 */
387inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip,
388 uint8_t *bits = nullptr)
389{
390 std::vector<std::string> bytesInMask;
391
392 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700393
394 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700395 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700396 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700397 return false;
398 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700399
Ed Tanous4a0cb852018-10-15 07:55:04 -0700400 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700401 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700402 *bits = 0;
403 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700404
Ed Tanous4a0cb852018-10-15 07:55:04 -0700405 char *endPtr;
406 long previousValue = 255;
407 bool firstZeroInByteHit;
408 for (const std::string &byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700409 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700410 if (byte.empty())
411 {
412 return false;
413 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700414
Ed Tanous4a0cb852018-10-15 07:55:04 -0700415 // Use strtol instead of stroi to avoid exceptions
416 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700417
Ed Tanous4a0cb852018-10-15 07:55:04 -0700418 // endPtr should point to the end of the string, otherwise given string
419 // is not 100% number
420 if (*endPtr != '\0')
421 {
422 return false;
423 }
424
425 // Value should be contained in byte
426 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700427 {
428 return false;
429 }
430
431 if (bits != nullptr)
432 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700433 // Mask has to be continuous between bytes
434 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700435 {
436 return false;
437 }
438
Ed Tanous4a0cb852018-10-15 07:55:04 -0700439 // Mask has to be continuous inside bytes
440 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700441
Ed Tanous4a0cb852018-10-15 07:55:04 -0700442 // Count bits
443 for (int bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700444 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700445 if (value & (1 << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700446 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700447 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700448 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700449 // Continuity not preserved
450 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700451 }
452 else
453 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700454 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700455 }
456 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700457 else
458 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700459 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700460 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700461 }
462 }
463
464 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700465 }
466
Ed Tanous4a0cb852018-10-15 07:55:04 -0700467 return true;
468}
469
470/**
471 * @brief Changes IPv4 address type property (Address, Gateway)
472 *
473 * @param[in] ifaceId Id of interface whose IP should be modified
474 * @param[in] ipIdx Index of IP in input array that should be modified
475 * @param[in] ipHash DBus Hash id of modified IP
476 * @param[in] name Name of field in JSON representation
477 * @param[in] newValue New value that should be written
478 * @param[io] asyncResp Response object that will be returned to client
479 *
480 * @return true if give IP is valid and has been sent do D-Bus, false
481 * otherwise
482 */
483inline void changeIPv4AddressProperty(
484 const std::string &ifaceId, int ipIdx, const std::string &ipHash,
485 const std::string &name, const std::string &newValue,
486 const std::shared_ptr<AsyncResp> asyncResp)
487{
488 auto callback = [asyncResp, ipIdx, name{std::string(name)},
489 newValue{std::move(newValue)}](
490 const boost::system::error_code ec) {
491 if (ec)
492 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800493 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700494 }
495 else
496 {
497 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx][name] = newValue;
498 }
499 };
500
501 crow::connections::systemBus->async_method_call(
502 std::move(callback), "xyz.openbmc_project.Network",
503 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
504 "org.freedesktop.DBus.Properties", "Set",
505 "xyz.openbmc_project.Network.IP", name,
Ed Tanousabf2add2019-01-22 16:40:12 -0800506 std::variant<std::string>(newValue));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700507}
508
509/**
510 * @brief Changes IPv4 address origin property
511 *
512 * @param[in] ifaceId Id of interface whose IP should be modified
513 * @param[in] ipIdx Index of IP in input array that should be
514 * modified
515 * @param[in] ipHash DBus Hash id of modified IP
516 * @param[in] newValue New value in Redfish format
517 * @param[in] newValueDbus New value in D-Bus format
518 * @param[io] asyncResp Response object that will be returned to client
519 *
520 * @return true if give IP is valid and has been sent do D-Bus, false
521 * otherwise
522 */
523inline void changeIPv4Origin(const std::string &ifaceId, int ipIdx,
524 const std::string &ipHash,
525 const std::string &newValue,
526 const std::string &newValueDbus,
527 const std::shared_ptr<AsyncResp> asyncResp)
528{
529 auto callback = [asyncResp, ipIdx, newValue{std::move(newValue)}](
530 const boost::system::error_code ec) {
531 if (ec)
532 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800533 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700534 }
535 else
536 {
537 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["AddressOrigin"] =
538 newValue;
539 }
540 };
541
542 crow::connections::systemBus->async_method_call(
543 std::move(callback), "xyz.openbmc_project.Network",
544 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
545 "org.freedesktop.DBus.Properties", "Set",
546 "xyz.openbmc_project.Network.IP", "Origin",
Ed Tanousabf2add2019-01-22 16:40:12 -0800547 std::variant<std::string>(newValueDbus));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700548}
549
550/**
551 * @brief Modifies SubnetMask for given IP
552 *
553 * @param[in] ifaceId Id of interface whose IP should be modified
554 * @param[in] ipIdx Index of IP in input array that should be
555 * modified
556 * @param[in] ipHash DBus Hash id of modified IP
557 * @param[in] newValueStr Mask in dot notation as string
558 * @param[in] newValue Mask as PrefixLength in bitcount
559 * @param[io] asyncResp Response object that will be returned to client
560 *
561 * @return None
562 */
563inline void changeIPv4SubnetMaskProperty(const std::string &ifaceId, int ipIdx,
564 const std::string &ipHash,
565 const std::string &newValueStr,
566 uint8_t &newValue,
567 std::shared_ptr<AsyncResp> asyncResp)
568{
569 auto callback = [asyncResp, ipIdx, newValueStr{std::move(newValueStr)}](
570 const boost::system::error_code ec) {
571 if (ec)
572 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800573 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700574 }
575 else
576 {
577 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["SubnetMask"] =
578 newValueStr;
579 }
580 };
581
582 crow::connections::systemBus->async_method_call(
583 std::move(callback), "xyz.openbmc_project.Network",
584 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
585 "org.freedesktop.DBus.Properties", "Set",
586 "xyz.openbmc_project.Network.IP", "PrefixLength",
Ed Tanousabf2add2019-01-22 16:40:12 -0800587 std::variant<uint8_t>(newValue));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700588}
589
590/**
Ed Tanous4a0cb852018-10-15 07:55:04 -0700591 * @brief Deletes given IPv4
592 *
593 * @param[in] ifaceId Id of interface whose IP should be deleted
594 * @param[in] ipIdx Index of IP in input array that should be deleted
595 * @param[in] ipHash DBus Hash id of IP that should be deleted
596 * @param[io] asyncResp Response object that will be returned to client
597 *
598 * @return None
599 */
600inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
601 unsigned int ipIdx,
602 const std::shared_ptr<AsyncResp> asyncResp)
603{
604 crow::connections::systemBus->async_method_call(
605 [ipIdx, asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700606 if (ec)
607 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800608 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100609 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700610 else
611 {
612 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx] = nullptr;
613 }
614 },
615 "xyz.openbmc_project.Network",
616 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
617 "xyz.openbmc_project.Object.Delete", "Delete");
618}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700619
Ed Tanous4a0cb852018-10-15 07:55:04 -0700620/**
621 * @brief Creates IPv4 with given data
622 *
623 * @param[in] ifaceId Id of interface whose IP should be deleted
624 * @param[in] ipIdx Index of IP in input array that should be deleted
625 * @param[in] ipHash DBus Hash id of IP that should be deleted
626 * @param[io] asyncResp Response object that will be returned to client
627 *
628 * @return None
629 */
630inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
631 uint8_t subnetMask, const std::string &gateway,
632 const std::string &address,
633 std::shared_ptr<AsyncResp> asyncResp)
634{
Ed Tanous43b761d2019-02-13 20:10:56 -0800635 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700636 if (ec)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700637 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800638 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700639 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700640 };
641
Ed Tanous4a0cb852018-10-15 07:55:04 -0700642 crow::connections::systemBus->async_method_call(
643 std::move(createIpHandler), "xyz.openbmc_project.Network",
644 "/xyz/openbmc_project/network/" + ifaceId,
645 "xyz.openbmc_project.Network.IP.Create", "IP",
646 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, subnetMask,
647 gateway);
648}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700649
Ed Tanous4a0cb852018-10-15 07:55:04 -0700650/**
651 * Function that retrieves all properties for given Ethernet Interface
652 * Object
653 * from EntityManager Network Manager
654 * @param ethiface_id a eth interface id to query on DBus
655 * @param callback a function that shall be called to convert Dbus output
656 * into JSON
657 */
658template <typename CallbackFunc>
659void getEthernetIfaceData(const std::string &ethiface_id,
660 CallbackFunc &&callback)
661{
662 crow::connections::systemBus->async_method_call(
663 [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
664 const boost::system::error_code error_code,
665 const GetManagedObjects &resp) {
666 EthernetInterfaceData ethData{};
667 boost::container::flat_set<IPv4AddressData> ipv4Data;
668
669 if (error_code)
670 {
671 callback(false, ethData, ipv4Data);
672 return;
673 }
674
675 extractEthernetInterfaceData(ethiface_id, resp, ethData);
676 extractIPData(ethiface_id, resp, ipv4Data);
677
678 // Fix global GW
679 for (IPv4AddressData &ipv4 : ipv4Data)
680 {
681 if ((ipv4.linktype == LinkType::Global) &&
682 (ipv4.gateway == "0.0.0.0"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700683 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700684 ipv4.gateway = ethData.default_gateway;
685 }
686 }
687
688 // Finally make a callback with usefull data
689 callback(true, ethData, ipv4Data);
690 },
691 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
692 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
693};
694
695/**
696 * Function that retrieves all Ethernet Interfaces available through Network
697 * Manager
698 * @param callback a function that shall be called to convert Dbus output
699 * into JSON.
700 */
701template <typename CallbackFunc>
702void getEthernetIfaceList(CallbackFunc &&callback)
703{
704 crow::connections::systemBus->async_method_call(
705 [callback{std::move(callback)}](
706 const boost::system::error_code error_code,
707 GetManagedObjects &resp) {
708 // Callback requires vector<string> to retrieve all available
709 // ethernet interfaces
710 std::vector<std::string> iface_list;
711 iface_list.reserve(resp.size());
712 if (error_code)
713 {
714 callback(false, iface_list);
715 return;
716 }
717
718 // Iterate over all retrieved ObjectPaths.
719 for (const auto &objpath : resp)
720 {
721 // And all interfaces available for certain ObjectPath.
722 for (const auto &interface : objpath.second)
723 {
724 // If interface is
725 // xyz.openbmc_project.Network.EthernetInterface, this is
726 // what we're looking for.
727 if (interface.first ==
728 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700729 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700730 // Cut out everyting until last "/", ...
731 const std::string &iface_id = objpath.first.str;
732 std::size_t last_pos = iface_id.rfind("/");
733 if (last_pos != std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700734 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700735 // and put it into output vector.
736 iface_list.emplace_back(
737 iface_id.substr(last_pos + 1));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700738 }
739 }
740 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700741 }
742 // Finally make a callback with useful data
743 callback(true, iface_list);
744 },
745 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
746 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100747};
748
749/**
750 * EthernetCollection derived class for delivering Ethernet Collection Schema
751 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700752class EthernetCollection : public Node
753{
754 public:
Ed Tanous4a0cb852018-10-15 07:55:04 -0700755 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700756 EthernetCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -0700757 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700758 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700759 entityPrivileges = {
760 {boost::beast::http::verb::get, {{"Login"}}},
761 {boost::beast::http::verb::head, {{"Login"}}},
762 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
763 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
764 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
765 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
766 }
767
768 private:
769 /**
770 * Functions triggers appropriate requests on DBus
771 */
772 void doGet(crow::Response &res, const crow::Request &req,
773 const std::vector<std::string> &params) override
774 {
Ed Tanous0f74e642018-11-12 15:17:05 -0800775 res.jsonValue["@odata.type"] =
776 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
777 res.jsonValue["@odata.context"] =
778 "/redfish/v1/"
779 "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection";
780 res.jsonValue["@odata.id"] =
781 "/redfish/v1/Managers/bmc/EthernetInterfaces";
782 res.jsonValue["Name"] = "Ethernet Network Interface Collection";
783 res.jsonValue["Description"] =
784 "Collection of EthernetInterfaces for this Manager";
785
Ed Tanous4a0cb852018-10-15 07:55:04 -0700786 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -0700787 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -0700788 getEthernetIfaceList(
789 [&res](const bool &success,
790 const std::vector<std::string> &iface_list) {
791 if (!success)
792 {
793 messages::internalError(res);
794 res.end();
795 return;
796 }
797
798 nlohmann::json &iface_array = res.jsonValue["Members"];
799 iface_array = nlohmann::json::array();
800 for (const std::string &iface_item : iface_list)
801 {
802 iface_array.push_back(
803 {{"@odata.id",
804 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
805 iface_item}});
806 }
807
808 res.jsonValue["Members@odata.count"] = iface_array.size();
809 res.jsonValue["@odata.id"] =
810 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700811 res.end();
Jason M. Billsf12894f2018-10-09 12:45:45 -0700812 });
Ed Tanous4a0cb852018-10-15 07:55:04 -0700813 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100814};
815
816/**
817 * EthernetInterface derived class for delivering Ethernet Schema
818 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700819class EthernetInterface : public Node
820{
821 public:
822 /*
823 * Default Constructor
824 */
Ed Tanous4a0cb852018-10-15 07:55:04 -0700825 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700826 EthernetInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -0700827 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -0700828 std::string())
829 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700830 entityPrivileges = {
831 {boost::beast::http::verb::get, {{"Login"}}},
832 {boost::beast::http::verb::head, {{"Login"}}},
833 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
834 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
835 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
836 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200837 }
838
Ed Tanous1abe55e2018-09-05 08:30:59 -0700839 // TODO(kkowalsk) Find a suitable class/namespace for this
Ed Tanous0627a2c2018-11-29 17:09:23 -0800840 static void handleVlanPatch(const std::string &ifaceId, bool vlanEnable,
841 uint64_t vlanId,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700842 const EthernetInterfaceData &ethData,
843 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700844 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700845 if (!ethData.vlan_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700846 {
847 // This interface is not a VLAN. Cannot do anything with it
848 // TODO(kkowalsk) Change this message
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800849 messages::propertyNotWritable(asyncResp->res, "VLANEnable");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700850
Ed Tanous1abe55e2018-09-05 08:30:59 -0700851 return;
852 }
853
854 // VLAN is configured on the interface
Ed Tanous0627a2c2018-11-29 17:09:23 -0800855 if (vlanEnable == true)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700856 {
857 // Change VLAN Id
Ed Tanous0627a2c2018-11-29 17:09:23 -0800858 asyncResp->res.jsonValue["VLANId"] = vlanId;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700859 auto callback = [asyncResp](const boost::system::error_code ec) {
860 if (ec)
861 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700862 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700863 }
864 else
865 {
866 asyncResp->res.jsonValue["VLANEnable"] = true;
867 }
868 };
869 crow::connections::systemBus->async_method_call(
870 std::move(callback), "xyz.openbmc_project.Network",
871 "/xyz/openbmc_project/network/" + ifaceId,
872 "org.freedesktop.DBus.Properties", "Set",
873 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanousabf2add2019-01-22 16:40:12 -0800874 std::variant<uint32_t>(vlanId));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700875 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700876 else
Ed Tanous1abe55e2018-09-05 08:30:59 -0700877 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700878 auto callback = [asyncResp](const boost::system::error_code ec) {
879 if (ec)
880 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700881 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700882 return;
883 }
884 asyncResp->res.jsonValue["VLANEnable"] = false;
885 };
886
887 crow::connections::systemBus->async_method_call(
888 std::move(callback), "xyz.openbmc_project.Network",
889 "/xyz/openbmc_project/network/" + ifaceId,
890 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700891 }
892 }
893
894 private:
Ed Tanousbc0bd6e2018-12-10 14:07:55 -0800895 void handleHostnamePatch(const std::string &hostname,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700896 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700897 {
Ed Tanousbc0bd6e2018-12-10 14:07:55 -0800898 asyncResp->res.jsonValue["HostName"] = hostname;
899 crow::connections::systemBus->async_method_call(
900 [asyncResp](const boost::system::error_code ec) {
901 if (ec)
902 {
903 messages::internalError(asyncResp->res);
904 }
905 },
906 "xyz.openbmc_project.Network",
907 "/xyz/openbmc_project/network/config",
908 "org.freedesktop.DBus.Properties", "Set",
909 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanousabf2add2019-01-22 16:40:12 -0800910 std::variant<std::string>(hostname));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700911 }
912
Ed Tanous4a0cb852018-10-15 07:55:04 -0700913 void handleIPv4Patch(
Ed Tanous537174c2018-12-10 15:09:31 -0800914 const std::string &ifaceId, std::vector<nlohmann::json> &input,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700915 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
916 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700917 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700918 int entryIdx = 0;
919 boost::container::flat_set<IPv4AddressData>::const_iterator thisData =
920 ipv4Data.begin();
Ed Tanous537174c2018-12-10 15:09:31 -0800921 for (nlohmann::json &thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700922 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700923 std::string pathString =
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800924 "IPv4Addresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700925
Ed Tanous537174c2018-12-10 15:09:31 -0800926 std::optional<std::string> address;
927 std::optional<std::string> addressOrigin;
928 std::optional<std::string> subnetMask;
929 std::optional<std::string> gateway;
930
931 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
932 address, "AddressOrigin", addressOrigin,
933 "SubnetMask", subnetMask, "Gateway",
934 gateway))
935 {
936 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700937 }
938
Ed Tanous537174c2018-12-10 15:09:31 -0800939 if (address)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700940 {
Ed Tanous537174c2018-12-10 15:09:31 -0800941 if (!ipv4VerifyIpAndGetBitcount(*address))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700942 {
Ed Tanous537174c2018-12-10 15:09:31 -0800943 messages::propertyValueFormatError(asyncResp->res, *address,
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800944 pathString + "/Address");
Ed Tanous537174c2018-12-10 15:09:31 -0800945 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700946 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700947 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700948
Ed Tanous537174c2018-12-10 15:09:31 -0800949 uint8_t prefixLength = 0;
950 if (subnetMask)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700951 {
Ed Tanous537174c2018-12-10 15:09:31 -0800952 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700953 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700954 messages::propertyValueFormatError(
Ed Tanous537174c2018-12-10 15:09:31 -0800955 asyncResp->res, *subnetMask,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700956 pathString + "/SubnetMask");
Ed Tanous537174c2018-12-10 15:09:31 -0800957 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700958 }
959 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700960 std::string addressOriginInDBusFormat;
Ed Tanous537174c2018-12-10 15:09:31 -0800961 if (addressOrigin)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700962 {
Ed Tanous537174c2018-12-10 15:09:31 -0800963 // Get Address origin in proper format
964 addressOriginInDBusFormat =
965 translateAddressOriginRedfishToDbus(*addressOrigin);
966 if (addressOriginInDBusFormat.empty())
Ed Tanous4a0cb852018-10-15 07:55:04 -0700967 {
Ed Tanous537174c2018-12-10 15:09:31 -0800968 messages::propertyValueNotInList(
969 asyncResp->res, *addressOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700970 pathString + "/AddressOrigin");
Ed Tanous537174c2018-12-10 15:09:31 -0800971 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700972 }
973 }
974
Ed Tanous537174c2018-12-10 15:09:31 -0800975 if (gateway)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700976 {
Ed Tanous537174c2018-12-10 15:09:31 -0800977 if (!ipv4VerifyIpAndGetBitcount(*gateway))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700978 {
Ed Tanous537174c2018-12-10 15:09:31 -0800979 messages::propertyValueFormatError(asyncResp->res, *gateway,
980 pathString + "/Gateway");
981 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700982 }
983 }
984
985 // if a vlan already exists, modify the existing
986 if (thisData != ipv4Data.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700987 {
988 // Existing object that should be modified/deleted/remain
989 // unchanged
Ed Tanous4a0cb852018-10-15 07:55:04 -0700990 if (thisJson.is_null())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700991 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700992 auto callback = [entryIdx{std::to_string(entryIdx)},
993 asyncResp](
994 const boost::system::error_code ec) {
995 if (ec)
996 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800997 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700998 return;
999 }
1000 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] =
1001 nullptr;
1002 };
1003 crow::connections::systemBus->async_method_call(
1004 std::move(callback), "xyz.openbmc_project.Network",
1005 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" +
1006 thisData->id,
1007 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001008 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001009 else if (thisJson.is_object())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001010 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001011 // Apply changes
Ed Tanous537174c2018-12-10 15:09:31 -08001012 if (address)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001013 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001014 auto callback =
Ed Tanous537174c2018-12-10 15:09:31 -08001015 [asyncResp, entryIdx, address = *address](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001016 const boost::system::error_code ec) {
1017 if (ec)
1018 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001019 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001020 return;
1021 }
Ratan Gupta82fd90f2019-03-07 21:09:25 +05301022 asyncResp->res.jsonValue["IPv4Addresses"]
1023 [entryIdx]["Address"] =
1024 address;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001025 };
1026
1027 crow::connections::systemBus->async_method_call(
1028 std::move(callback), "xyz.openbmc_project.Network",
1029 "/xyz/openbmc_project/network/" + ifaceId +
1030 "/ipv4/" + thisData->id,
1031 "org.freedesktop.DBus.Properties", "Set",
1032 "xyz.openbmc_project.Network.IP", "Address",
Ed Tanous537174c2018-12-10 15:09:31 -08001033 std::variant<std::string>(*address));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001034 }
1035
Ed Tanous537174c2018-12-10 15:09:31 -08001036 if (subnetMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001037 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001038 changeIPv4SubnetMaskProperty(ifaceId, entryIdx,
Ed Tanous537174c2018-12-10 15:09:31 -08001039 thisData->id, *subnetMask,
1040 prefixLength, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001041 }
1042
Ed Tanous537174c2018-12-10 15:09:31 -08001043 if (addressOrigin)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001044 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001045 changeIPv4Origin(ifaceId, entryIdx, thisData->id,
Ed Tanous537174c2018-12-10 15:09:31 -08001046 *addressOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001047 addressOriginInDBusFormat, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001048 }
1049
Ed Tanous537174c2018-12-10 15:09:31 -08001050 if (gateway)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001051 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001052 auto callback =
1053 [asyncResp, entryIdx,
Ed Tanous537174c2018-12-10 15:09:31 -08001054 gateway{std::string(*gateway)}](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001055 const boost::system::error_code ec) {
1056 if (ec)
1057 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001058 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001059 return;
1060 }
Ratan Gupta82fd90f2019-03-07 21:09:25 +05301061 asyncResp->res.jsonValue["IPv4Addresses"]
1062 [entryIdx]["Gateway"] =
Ed Tanous537174c2018-12-10 15:09:31 -08001063 std::move(gateway);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001064 };
1065
1066 crow::connections::systemBus->async_method_call(
1067 std::move(callback), "xyz.openbmc_project.Network",
1068 "/xyz/openbmc_project/network/" + ifaceId +
1069 "/ipv4/" + thisData->id,
1070 "org.freedesktop.DBus.Properties", "Set",
1071 "xyz.openbmc_project.Network.IP", "Gateway",
Ed Tanous537174c2018-12-10 15:09:31 -08001072 std::variant<std::string>(*gateway));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001073 }
1074 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001075 thisData++;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001076 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001077 else
1078 {
1079 // Create IPv4 with provided data
Ed Tanous537174c2018-12-10 15:09:31 -08001080 if (!gateway)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001081 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001082 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001083 pathString + "/Gateway");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001084 continue;
1085 }
1086
Ed Tanous537174c2018-12-10 15:09:31 -08001087 if (!address)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001088 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001089 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001090 pathString + "/Address");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001091 continue;
1092 }
1093
Ed Tanous537174c2018-12-10 15:09:31 -08001094 if (!subnetMask)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001095 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001096 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001097 pathString + "/SubnetMask");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001098 continue;
1099 }
1100
Ed Tanous537174c2018-12-10 15:09:31 -08001101 createIPv4(ifaceId, entryIdx, prefixLength, *gateway, *address,
1102 asyncResp);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001103 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] = thisJson;
1104 }
1105 entryIdx++;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001106 }
1107 }
1108
Ed Tanous0f74e642018-11-12 15:17:05 -08001109 void parseInterfaceData(
1110 nlohmann::json &json_response, const std::string &iface_id,
1111 const EthernetInterfaceData &ethData,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001112 const boost::container::flat_set<IPv4AddressData> &ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001113 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001114 json_response["Id"] = iface_id;
1115 json_response["@odata.id"] =
1116 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
Ed Tanous029573d2019-02-01 10:57:49 -08001117 json_response["InterfaceEnabled"] = true;
1118 if (ethData.speed == 0)
1119 {
1120 json_response["LinkStatus"] = "NoLink";
1121 json_response["Status"] = {
1122 {"Health", "OK"},
1123 {"State", "Disabled"},
1124 };
1125 }
1126 else
1127 {
1128 json_response["LinkStatus"] = "LinkUp";
1129 json_response["Status"] = {
1130 {"Health", "OK"},
1131 {"State", "Enabled"},
1132 };
1133 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001134 json_response["SpeedMbps"] = ethData.speed;
1135 json_response["MACAddress"] = ethData.mac_address;
1136 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001137 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001138 json_response["HostName"] = ethData.hostname;
1139 }
1140
1141 nlohmann::json &vlanObj = json_response["VLAN"];
1142 if (ethData.vlan_id)
1143 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001144 vlanObj["VLANEnable"] = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001145 vlanObj["VLANId"] = *ethData.vlan_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001146 }
1147 else
1148 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001149 vlanObj["VLANEnable"] = false;
1150 vlanObj["VLANId"] = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001151 }
Ed Tanous029573d2019-02-01 10:57:49 -08001152 json_response["NameServers"] = ethData.nameservers;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001153
Ed Tanous4a0cb852018-10-15 07:55:04 -07001154 if (ipv4Data.size() > 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001155 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001156 nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
1157 ipv4_array = nlohmann::json::array();
1158 for (auto &ipv4_config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001159 {
Ed Tanous029573d2019-02-01 10:57:49 -08001160 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
1161 {"SubnetMask", ipv4_config.netmask},
1162 {"Address", ipv4_config.address},
1163 {"Gateway", ipv4_config.gateway}});
Ed Tanous1abe55e2018-09-05 08:30:59 -07001164 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001165 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001166 }
1167
1168 /**
1169 * Functions triggers appropriate requests on DBus
1170 */
1171 void doGet(crow::Response &res, const crow::Request &req,
1172 const std::vector<std::string> &params) override
1173 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001174 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001175 if (params.size() != 1)
1176 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001177 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001178 return;
1179 }
1180
Ed Tanous4a0cb852018-10-15 07:55:04 -07001181 getEthernetIfaceData(
1182 params[0],
1183 [this, asyncResp, iface_id{std::string(params[0])}](
1184 const bool &success, const EthernetInterfaceData &ethData,
1185 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1186 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001187 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001188 // TODO(Pawel)consider distinguish between non existing
1189 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001190 messages::resourceNotFound(asyncResp->res,
1191 "EthernetInterface", iface_id);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001192 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001193 }
Ed Tanous0f74e642018-11-12 15:17:05 -08001194 asyncResp->res.jsonValue["@odata.type"] =
1195 "#EthernetInterface.v1_2_0.EthernetInterface";
1196 asyncResp->res.jsonValue["@odata.context"] =
1197 "/redfish/v1/$metadata#EthernetInterface.EthernetInterface";
1198 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1199 asyncResp->res.jsonValue["Description"] =
1200 "Management Network Interface";
1201
1202 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
1203 ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001204 });
1205 }
1206
1207 void doPatch(crow::Response &res, const crow::Request &req,
1208 const std::vector<std::string> &params) override
1209 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001210 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001211 if (params.size() != 1)
1212 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001213 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001214 return;
1215 }
1216
Ed Tanous4a0cb852018-10-15 07:55:04 -07001217 const std::string &iface_id = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001218
Ed Tanous0627a2c2018-11-29 17:09:23 -08001219 std::optional<nlohmann::json> vlan;
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001220 std::optional<std::string> hostname;
Ed Tanous537174c2018-12-10 15:09:31 -08001221 std::optional<std::vector<nlohmann::json>> ipv4Addresses;
1222 std::optional<std::vector<nlohmann::json>> ipv6Addresses;
Ed Tanous0627a2c2018-11-29 17:09:23 -08001223
1224 if (!json_util::readJson(req, res, "VLAN", vlan, "HostName", hostname,
1225 "IPv4Addresses", ipv4Addresses,
1226 "IPv6Addresses", ipv6Addresses))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001227 {
1228 return;
1229 }
Ratan Guptaf15aad32019-03-01 13:41:13 +05301230
1231 std::optional<uint64_t> vlanId;
1232 std::optional<bool> vlanEnable;
1233
Ed Tanous0627a2c2018-11-29 17:09:23 -08001234 if (vlan)
1235 {
1236 if (!json_util::readJson(*vlan, res, "VLANEnable", vlanEnable,
1237 "VLANId", vlanId))
1238 {
1239 return;
1240 }
1241 // Need both vlanId and vlanEnable to service this request
1242 if (static_cast<bool>(vlanId) ^ static_cast<bool>(vlanEnable))
1243 {
1244 if (vlanId)
1245 {
1246 messages::propertyMissing(asyncResp->res, "VLANEnable");
1247 }
1248 else
1249 {
1250 messages::propertyMissing(asyncResp->res, "VLANId");
1251 }
1252
1253 return;
1254 }
1255 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001256
Ed Tanous4a0cb852018-10-15 07:55:04 -07001257 // Get single eth interface data, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001258 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001259 getEthernetIfaceData(
1260 iface_id,
Ed Tanous0627a2c2018-11-29 17:09:23 -08001261 [this, asyncResp, iface_id, vlanId, vlanEnable,
1262 hostname = std::move(hostname),
1263 ipv4Addresses = std::move(ipv4Addresses),
1264 ipv6Addresses = std::move(ipv6Addresses)](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001265 const bool &success, const EthernetInterfaceData &ethData,
1266 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001267 if (!success)
1268 {
1269 // ... otherwise return error
1270 // TODO(Pawel)consider distinguish between non existing
1271 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001272 messages::resourceNotFound(
1273 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001274 return;
1275 }
1276
Ed Tanous0f74e642018-11-12 15:17:05 -08001277 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
1278 ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001279
Ed Tanous0627a2c2018-11-29 17:09:23 -08001280 if (vlanId && vlanEnable)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001281 {
Ed Tanous0627a2c2018-11-29 17:09:23 -08001282 handleVlanPatch(iface_id, *vlanId, *vlanEnable, ethData,
1283 asyncResp);
1284 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001285
Ed Tanous0627a2c2018-11-29 17:09:23 -08001286 if (hostname)
1287 {
1288 handleHostnamePatch(*hostname, asyncResp);
1289 }
1290
1291 if (ipv4Addresses)
1292 {
Ed Tanous537174c2018-12-10 15:09:31 -08001293 // TODO(ed) for some reason the capture of ipv4Addresses
1294 // above is returning a const value, not a non-const value.
1295 // This doesn't really work for us, as we need to be able to
1296 // efficiently move out the intermedia nlohmann::json
1297 // objects. This makes a copy of the structure, and operates
1298 // on that, but could be done more efficiently
1299 std::vector<nlohmann::json> ipv4 =
1300 std::move(*ipv4Addresses);
1301 handleIPv4Patch(iface_id, ipv4, ipv4Data, asyncResp);
Ed Tanous0627a2c2018-11-29 17:09:23 -08001302 }
1303
1304 if (ipv6Addresses)
1305 {
1306 // TODO(kkowalsk) IPv6 Not supported on D-Bus yet
1307 messages::propertyNotWritable(asyncResp->res,
1308 "IPv6Addresses");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001309 }
1310 });
1311 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001312};
1313
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001314/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07001315 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
1316 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001317 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001318class VlanNetworkInterface : public Node
1319{
1320 public:
1321 /*
1322 * Default Constructor
1323 */
1324 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001325 VlanNetworkInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001326 Node(app,
Ed Tanous0f74e642018-11-12 15:17:05 -08001327 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>",
Ed Tanous4a0cb852018-10-15 07:55:04 -07001328 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001329 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001330 entityPrivileges = {
1331 {boost::beast::http::verb::get, {{"Login"}}},
1332 {boost::beast::http::verb::head, {{"Login"}}},
1333 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1334 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1335 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1336 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001337 }
1338
Ed Tanous1abe55e2018-09-05 08:30:59 -07001339 private:
Ed Tanous0f74e642018-11-12 15:17:05 -08001340 void parseInterfaceData(
1341 nlohmann::json &json_response, const std::string &parent_iface_id,
1342 const std::string &iface_id, const EthernetInterfaceData &ethData,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001343 const boost::container::flat_set<IPv4AddressData> &ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001344 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001345 // Fill out obvious data...
Ed Tanous4a0cb852018-10-15 07:55:04 -07001346 json_response["Id"] = iface_id;
1347 json_response["@odata.id"] =
1348 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
1349 "/VLANs/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001350
Ed Tanous4a0cb852018-10-15 07:55:04 -07001351 json_response["VLANEnable"] = true;
1352 if (ethData.vlan_id)
1353 {
1354 json_response["VLANId"] = *ethData.vlan_id;
1355 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07001356 }
1357
Ed Tanous1abe55e2018-09-05 08:30:59 -07001358 bool verifyNames(crow::Response &res, const std::string &parent,
1359 const std::string &iface)
1360 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001361 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001362 if (!boost::starts_with(iface, parent + "_"))
1363 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001364 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
1365 iface);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001366 return false;
1367 }
1368 else
1369 {
1370 return true;
1371 }
1372 }
1373
1374 /**
1375 * Functions triggers appropriate requests on DBus
1376 */
1377 void doGet(crow::Response &res, const crow::Request &req,
1378 const std::vector<std::string> &params) override
1379 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001380 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1381 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07001382 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1383 // Check if there is required param, truly entering this shall be
1384 // impossible.
1385 if (params.size() != 2)
1386 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001387 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001388 res.end();
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001389 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001390 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001391
Ed Tanous4a0cb852018-10-15 07:55:04 -07001392 const std::string &parent_iface_id = params[0];
1393 const std::string &iface_id = params[1];
Ed Tanous0f74e642018-11-12 15:17:05 -08001394 res.jsonValue["@odata.type"] =
1395 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1396 res.jsonValue["@odata.context"] =
1397 "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface";
1398 res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001399
Ed Tanous4a0cb852018-10-15 07:55:04 -07001400 if (!verifyNames(res, parent_iface_id, iface_id))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001401 {
1402 return;
1403 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001404
Ed Tanous1abe55e2018-09-05 08:30:59 -07001405 // Get single eth interface data, and call the below callback for JSON
1406 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001407 getEthernetIfaceData(
1408 iface_id,
1409 [this, asyncResp, parent_iface_id, iface_id](
1410 const bool &success, const EthernetInterfaceData &ethData,
1411 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1412 if (success && ethData.vlan_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001413 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001414 parseInterfaceData(asyncResp->res.jsonValue,
1415 parent_iface_id, iface_id, ethData,
1416 ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001417 }
1418 else
1419 {
1420 // ... otherwise return error
1421 // TODO(Pawel)consider distinguish between non existing
1422 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001423 messages::resourceNotFound(
1424 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001425 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001426 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001427 }
1428
Ed Tanous1abe55e2018-09-05 08:30:59 -07001429 void doPatch(crow::Response &res, const crow::Request &req,
1430 const std::vector<std::string> &params) override
1431 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001432 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001433 if (params.size() != 2)
1434 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001435 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001436 return;
1437 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001438
Ed Tanous1abe55e2018-09-05 08:30:59 -07001439 const std::string &parentIfaceId = params[0];
1440 const std::string &ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001441
Ed Tanous1abe55e2018-09-05 08:30:59 -07001442 if (!verifyNames(res, parentIfaceId, ifaceId))
1443 {
1444 return;
1445 }
1446
Ed Tanous0627a2c2018-11-29 17:09:23 -08001447 bool vlanEnable = false;
1448 uint64_t vlanId = 0;
1449
1450 if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
1451 vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001452 {
1453 return;
1454 }
1455
1456 // Get single eth interface data, and call the below callback for JSON
1457 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001458 getEthernetIfaceData(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001459 ifaceId,
Ed Tanous0627a2c2018-11-29 17:09:23 -08001460 [this, asyncResp, parentIfaceId, ifaceId, vlanEnable, vlanId](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001461 const bool &success, const EthernetInterfaceData &ethData,
1462 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001463 if (!success)
1464 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001465 // TODO(Pawel)consider distinguish between non existing
1466 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001467 messages::resourceNotFound(
1468 asyncResp->res, "VLAN Network Interface", ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001469
1470 return;
1471 }
1472
Ed Tanous0f74e642018-11-12 15:17:05 -08001473 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
1474 ifaceId, ethData, ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001475
Ed Tanous0627a2c2018-11-29 17:09:23 -08001476 EthernetInterface::handleVlanPatch(ifaceId, vlanId, vlanEnable,
1477 ethData, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001478 });
1479 }
1480
1481 void doDelete(crow::Response &res, const crow::Request &req,
1482 const std::vector<std::string> &params) override
1483 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001484 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001485 if (params.size() != 2)
1486 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001487 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001488 return;
1489 }
1490
1491 const std::string &parentIfaceId = params[0];
1492 const std::string &ifaceId = params[1];
1493
Ed Tanous4a0cb852018-10-15 07:55:04 -07001494 if (!verifyNames(asyncResp->res, parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001495 {
1496 return;
1497 }
1498
1499 // Get single eth interface data, and call the below callback for JSON
1500 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001501 getEthernetIfaceData(
1502 ifaceId,
1503 [this, asyncResp, parentIfaceId{std::string(parentIfaceId)},
1504 ifaceId{std::string(ifaceId)}](
1505 const bool &success, const EthernetInterfaceData &ethData,
1506 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1507 if (success && ethData.vlan_id)
1508 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001509 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
1510 ifaceId, ethData, ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001511
Jason M. Billsf12894f2018-10-09 12:45:45 -07001512 auto callback =
1513 [asyncResp](const boost::system::error_code ec) {
1514 if (ec)
1515 {
1516 messages::internalError(asyncResp->res);
1517 }
1518 };
1519 crow::connections::systemBus->async_method_call(
1520 std::move(callback), "xyz.openbmc_project.Network",
1521 std::string("/xyz/openbmc_project/network/") + ifaceId,
1522 "xyz.openbmc_project.Object.Delete", "Delete");
1523 }
1524 else
1525 {
1526 // ... otherwise return error
1527 // TODO(Pawel)consider distinguish between non existing
1528 // object, and other errors
1529 messages::resourceNotFound(
1530 asyncResp->res, "VLAN Network Interface", ifaceId);
1531 }
1532 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001533 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001534};
1535
1536/**
1537 * VlanNetworkInterfaceCollection derived class for delivering
1538 * VLANNetworkInterface Collection Schema
1539 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001540class VlanNetworkInterfaceCollection : public Node
1541{
1542 public:
1543 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001544 VlanNetworkInterfaceCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001545 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
1546 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001547 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001548 entityPrivileges = {
1549 {boost::beast::http::verb::get, {{"Login"}}},
1550 {boost::beast::http::verb::head, {{"Login"}}},
1551 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1552 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1553 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1554 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001555 }
1556
Ed Tanous1abe55e2018-09-05 08:30:59 -07001557 private:
1558 /**
1559 * Functions triggers appropriate requests on DBus
1560 */
1561 void doGet(crow::Response &res, const crow::Request &req,
1562 const std::vector<std::string> &params) override
1563 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001564 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001565 if (params.size() != 1)
1566 {
1567 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07001568 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001569 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07001570 }
1571
Ed Tanous4a0cb852018-10-15 07:55:04 -07001572 const std::string &rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001573
Ed Tanous4a0cb852018-10-15 07:55:04 -07001574 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001575 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001576 getEthernetIfaceList(
Ed Tanous43b761d2019-02-13 20:10:56 -08001577 [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
Jason M. Billsf12894f2018-10-09 12:45:45 -07001578 const bool &success,
1579 const std::vector<std::string> &iface_list) {
1580 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001581 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001582 messages::internalError(asyncResp->res);
1583 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001584 }
Ed Tanous0f74e642018-11-12 15:17:05 -08001585 asyncResp->res.jsonValue["@odata.type"] =
1586 "#VLanNetworkInterfaceCollection."
1587 "VLanNetworkInterfaceCollection";
1588 asyncResp->res.jsonValue["@odata.context"] =
1589 "/redfish/v1/$metadata"
1590 "#VLanNetworkInterfaceCollection."
1591 "VLanNetworkInterfaceCollection";
1592 asyncResp->res.jsonValue["Name"] =
1593 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07001594
Jason M. Billsf12894f2018-10-09 12:45:45 -07001595 nlohmann::json iface_array = nlohmann::json::array();
1596
1597 for (const std::string &iface_item : iface_list)
1598 {
1599 if (boost::starts_with(iface_item, rootInterfaceName + "_"))
1600 {
1601 iface_array.push_back(
1602 {{"@odata.id",
1603 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1604 rootInterfaceName + "/VLANs/" + iface_item}});
1605 }
1606 }
1607
1608 if (iface_array.empty())
1609 {
1610 messages::resourceNotFound(
1611 asyncResp->res, "EthernetInterface", rootInterfaceName);
1612 return;
1613 }
1614 asyncResp->res.jsonValue["Members@odata.count"] =
1615 iface_array.size();
1616 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
1617 asyncResp->res.jsonValue["@odata.id"] =
1618 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1619 rootInterfaceName + "/VLANs";
1620 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001621 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001622
Ed Tanous1abe55e2018-09-05 08:30:59 -07001623 void doPost(crow::Response &res, const crow::Request &req,
1624 const std::vector<std::string> &params) override
1625 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001626 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001627 if (params.size() != 1)
1628 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001629 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001630 return;
1631 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001632
Ed Tanous0627a2c2018-11-29 17:09:23 -08001633 uint32_t vlanId = 0;
1634 if (!json_util::readJson(req, res, "VLANId", vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001635 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001636 return;
1637 }
1638 const std::string &rootInterfaceName = params[0];
Ed Tanous4a0cb852018-10-15 07:55:04 -07001639 auto callback = [asyncResp](const boost::system::error_code ec) {
1640 if (ec)
1641 {
1642 // TODO(ed) make more consistent error messages based on
1643 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07001644 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001645 return;
1646 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001647 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001648 };
1649 crow::connections::systemBus->async_method_call(
1650 std::move(callback), "xyz.openbmc_project.Network",
1651 "/xyz/openbmc_project/network",
1652 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
Ed Tanous0627a2c2018-11-29 17:09:23 -08001653 rootInterfaceName, vlanId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001654 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001655};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001656} // namespace redfish