blob: da4df20df56256d69c7682a753ff8342cdc6e48b [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>
20#include <boost/optional.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020021#include <dbus_singleton.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020022#include <error_messages.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020023#include <node.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020024#include <utils/json_utils.hpp>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010025
Ed Tanous1abe55e2018-09-05 08:30:59 -070026namespace redfish
27{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010028
29/**
30 * DBus types primitives for several generic DBus interfaces
31 * TODO(Pawel) consider move this to separate file into boost::dbus
32 */
Ed Tanousaa2e59c2018-04-12 12:17:20 -070033using PropertiesMapType = boost::container::flat_map<
34 std::string,
35 sdbusplus::message::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<
43 std::string, sdbusplus::message::variant<
44 std::string, bool, uint8_t, int16_t, uint16_t,
Ed Tanous4a0cb852018-10-15 07:55:04 -070045 int32_t, uint32_t, int64_t, uint64_t, double>>>>>>;
46
47enum class LinkType
48{
49 Local,
50 Global
51};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010052
53/**
54 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010055 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070056struct IPv4AddressData
57{
58 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070059 std::string address;
60 std::string domain;
61 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 std::string netmask;
63 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070064 LinkType linktype;
65
Ed Tanous1abe55e2018-09-05 08:30:59 -070066 bool operator<(const IPv4AddressData &obj) const
67 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070068 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070069 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010070};
71
72/**
73 * Structure for keeping basic single Ethernet Interface information
74 * available from DBus
75 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070076struct EthernetInterfaceData
77{
Ed Tanous4a0cb852018-10-15 07:55:04 -070078 uint32_t speed;
79 bool auto_neg;
80 std::string hostname;
81 std::string default_gateway;
82 std::string mac_address;
83 boost::optional<uint32_t> vlan_id;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010084};
85
Ed Tanous4a0cb852018-10-15 07:55:04 -070086// Helper function that changes bits netmask notation (i.e. /24)
87// into full dot notation
88inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -070089{
Ed Tanous4a0cb852018-10-15 07:55:04 -070090 uint32_t value = 0xffffffff << (32 - bits);
91 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
92 std::to_string((value >> 16) & 0xff) + "." +
93 std::to_string((value >> 8) & 0xff) + "." +
94 std::to_string(value & 0xff);
95 return netmask;
96}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010097
Ed Tanous4a0cb852018-10-15 07:55:04 -070098inline std::string
99 translateAddressOriginDbusToRedfish(const std::string &inputOrigin,
100 bool isIPv4)
101{
102 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700104 return "Static";
105 }
106 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
107 {
108 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700110 return "IPv4LinkLocal";
111 }
112 else
113 {
114 return "LinkLocal";
115 }
116 }
117 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
118 {
119 if (isIPv4)
120 {
121 return "DHCP";
122 }
123 else
124 {
125 return "DHCPv6";
126 }
127 }
128 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
129 {
130 return "SLAAC";
131 }
132 return "";
133}
134
135inline std::string
136 translateAddressOriginRedfishToDbus(const std::string &inputOrigin)
137{
138 if (inputOrigin == "Static")
139 {
140 return "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
141 }
142 if (inputOrigin == "DHCP" || inputOrigin == "DHCPv6")
143 {
144 return "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
145 }
146 if (inputOrigin == "IPv4LinkLocal" || inputOrigin == "LinkLocal")
147 {
148 return "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal";
149 }
150 if (inputOrigin == "SLAAC")
151 {
152 return "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC";
153 }
154 return "";
155}
156
157inline void extractEthernetInterfaceData(const std::string &ethiface_id,
158 const GetManagedObjects &dbus_data,
159 EthernetInterfaceData &ethData)
160{
161 for (const auto &objpath : dbus_data)
162 {
163 if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
164 {
165 for (const auto &ifacePair : objpath.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700166 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700167 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700168 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700169 for (const auto &propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700170 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700171 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700172 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700173 const std::string *mac =
174 mapbox::getPtr<const std::string>(
175 propertyPair.second);
176 if (mac != nullptr)
177 {
178 ethData.mac_address = *mac;
179 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700180 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700181 }
182 }
183 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
184 {
185 for (const auto &propertyPair : ifacePair.second)
186 {
187 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700188 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700189 const uint32_t *id = mapbox::getPtr<const uint32_t>(
190 propertyPair.second);
191 if (id != nullptr)
192 {
193 ethData.vlan_id = *id;
194 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700195 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700196 }
197 }
198 else if (ifacePair.first ==
199 "xyz.openbmc_project.Network.EthernetInterface")
200 {
201 for (const auto &propertyPair : ifacePair.second)
202 {
203 if (propertyPair.first == "AutoNeg")
204 {
205 const bool *auto_neg =
206 mapbox::getPtr<const bool>(propertyPair.second);
207 if (auto_neg != nullptr)
208 {
209 ethData.auto_neg = *auto_neg;
210 }
211 }
212 else if (propertyPair.first == "Speed")
213 {
214 const uint32_t *speed =
215 mapbox::getPtr<const uint32_t>(
216 propertyPair.second);
217 if (speed != nullptr)
218 {
219 ethData.speed = *speed;
220 }
221 }
222 }
223 }
224 else if (ifacePair.first ==
225 "xyz.openbmc_project.Network.SystemConfiguration")
226 {
227 for (const auto &propertyPair : ifacePair.second)
228 {
229 if (propertyPair.first == "HostName")
230 {
231 const std::string *hostname =
232 mapbox::getPtr<const std::string>(
233 propertyPair.second);
234 if (hostname != nullptr)
235 {
236 ethData.hostname = *hostname;
237 }
238 }
239 else if (propertyPair.first == "DefaultGateway")
240 {
241 const std::string *defaultGateway =
242 mapbox::getPtr<const std::string>(
243 propertyPair.second);
244 if (defaultGateway != nullptr)
245 {
246 ethData.default_gateway = *defaultGateway;
247 }
248 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700249 }
250 }
251 }
252 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700253 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700254}
255
256// Helper function that extracts data for single ethernet ipv4 address
257inline void
258 extractIPData(const std::string &ethiface_id,
259 const GetManagedObjects &dbus_data,
260 boost::container::flat_set<IPv4AddressData> &ipv4_config)
261{
262 const std::string ipv4PathStart =
263 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
264
265 // Since there might be several IPv4 configurations aligned with
266 // single ethernet interface, loop over all of them
267 for (const auto &objpath : dbus_data)
268 {
269 // Check if proper pattern for object path appears
270 if (boost::starts_with(objpath.first.str, ipv4PathStart))
271 {
272 for (auto &interface : objpath.second)
273 {
274 if (interface.first == "xyz.openbmc_project.Network.IP")
275 {
276 // Instance IPv4AddressData structure, and set as
277 // appropriate
278 std::pair<
279 boost::container::flat_set<IPv4AddressData>::iterator,
280 bool>
281 it = ipv4_config.insert(
282 {objpath.first.str.substr(ipv4PathStart.size())});
283 IPv4AddressData &ipv4_address = *it.first;
284 for (auto &property : interface.second)
285 {
286 if (property.first == "Address")
287 {
288 const std::string *address =
289 mapbox::getPtr<const std::string>(
290 property.second);
291 if (address != nullptr)
292 {
293 ipv4_address.address = *address;
294 }
295 }
296 else if (property.first == "Gateway")
297 {
298 const std::string *gateway =
299 mapbox::getPtr<const std::string>(
300 property.second);
301 if (gateway != nullptr)
302 {
303 ipv4_address.gateway = *gateway;
304 }
305 }
306 else if (property.first == "Origin")
307 {
308 const std::string *origin =
309 mapbox::getPtr<const std::string>(
310 property.second);
311 if (origin != nullptr)
312 {
313 ipv4_address.origin =
314 translateAddressOriginDbusToRedfish(*origin,
315 true);
316 }
317 }
318 else if (property.first == "PrefixLength")
319 {
320 const uint8_t *mask =
321 mapbox::getPtr<uint8_t>(property.second);
322 if (mask != nullptr)
323 {
324 // convert it to the string
325 ipv4_address.netmask = getNetmask(*mask);
326 }
327 }
328 else
329 {
330 BMCWEB_LOG_ERROR
331 << "Got extra property: " << property.first
332 << " on the " << objpath.first.str << " object";
333 }
334 }
335 // Check if given address is local, or global
336 ipv4_address.linktype =
337 boost::starts_with(ipv4_address.address, "169.254.")
338 ? LinkType::Global
339 : LinkType::Local;
340 }
341 }
342 }
343 }
344}
345
346/**
347 * @brief Sets given Id on the given VLAN interface through D-Bus
348 *
349 * @param[in] ifaceId Id of VLAN interface that should be modified
350 * @param[in] inputVlanId New ID of the VLAN
351 * @param[in] callback Function that will be called after the operation
352 *
353 * @return None.
354 */
355template <typename CallbackFunc>
356void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId,
357 CallbackFunc &&callback)
358{
359 crow::connections::systemBus->async_method_call(
360 callback, "xyz.openbmc_project.Network",
361 std::string("/xyz/openbmc_project/network/") + ifaceId,
362 "org.freedesktop.DBus.Properties", "Set",
363 "xyz.openbmc_project.Network.VLAN", "Id",
364 sdbusplus::message::variant<uint32_t>(inputVlanId));
365}
366
367/**
368 * @brief Helper function that verifies IP address to check if it is in
369 * proper format. If bits pointer is provided, also calculates active
370 * bit count for Subnet Mask.
371 *
372 * @param[in] ip IP that will be verified
373 * @param[out] bits Calculated mask in bits notation
374 *
375 * @return true in case of success, false otherwise
376 */
377inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip,
378 uint8_t *bits = nullptr)
379{
380 std::vector<std::string> bytesInMask;
381
382 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700383
384 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700385 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700386 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700387 return false;
388 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700389
Ed Tanous4a0cb852018-10-15 07:55:04 -0700390 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700391 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700392 *bits = 0;
393 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700394
Ed Tanous4a0cb852018-10-15 07:55:04 -0700395 char *endPtr;
396 long previousValue = 255;
397 bool firstZeroInByteHit;
398 for (const std::string &byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700399 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700400 if (byte.empty())
401 {
402 return false;
403 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700404
Ed Tanous4a0cb852018-10-15 07:55:04 -0700405 // Use strtol instead of stroi to avoid exceptions
406 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700407
Ed Tanous4a0cb852018-10-15 07:55:04 -0700408 // endPtr should point to the end of the string, otherwise given string
409 // is not 100% number
410 if (*endPtr != '\0')
411 {
412 return false;
413 }
414
415 // Value should be contained in byte
416 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700417 {
418 return false;
419 }
420
421 if (bits != nullptr)
422 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700423 // Mask has to be continuous between bytes
424 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700425 {
426 return false;
427 }
428
Ed Tanous4a0cb852018-10-15 07:55:04 -0700429 // Mask has to be continuous inside bytes
430 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700431
Ed Tanous4a0cb852018-10-15 07:55:04 -0700432 // Count bits
433 for (int bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700434 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700435 if (value & (1 << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700436 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700437 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700438 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700439 // Continuity not preserved
440 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700441 }
442 else
443 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700444 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700445 }
446 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700447 else
448 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700449 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700450 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700451 }
452 }
453
454 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700455 }
456
Ed Tanous4a0cb852018-10-15 07:55:04 -0700457 return true;
458}
459
460/**
461 * @brief Changes IPv4 address type property (Address, Gateway)
462 *
463 * @param[in] ifaceId Id of interface whose IP should be modified
464 * @param[in] ipIdx Index of IP in input array that should be modified
465 * @param[in] ipHash DBus Hash id of modified IP
466 * @param[in] name Name of field in JSON representation
467 * @param[in] newValue New value that should be written
468 * @param[io] asyncResp Response object that will be returned to client
469 *
470 * @return true if give IP is valid and has been sent do D-Bus, false
471 * otherwise
472 */
473inline void changeIPv4AddressProperty(
474 const std::string &ifaceId, int ipIdx, const std::string &ipHash,
475 const std::string &name, const std::string &newValue,
476 const std::shared_ptr<AsyncResp> asyncResp)
477{
478 auto callback = [asyncResp, ipIdx, name{std::string(name)},
479 newValue{std::move(newValue)}](
480 const boost::system::error_code ec) {
481 if (ec)
482 {
483 messages::addMessageToJson(
484 asyncResp->res.jsonValue, messages::internalError(),
485 "/IPv4Addresses/" + std::to_string(ipIdx) + "/" + name);
486 }
487 else
488 {
489 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx][name] = newValue;
490 }
491 };
492
493 crow::connections::systemBus->async_method_call(
494 std::move(callback), "xyz.openbmc_project.Network",
495 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
496 "org.freedesktop.DBus.Properties", "Set",
497 "xyz.openbmc_project.Network.IP", name,
498 sdbusplus::message::variant<std::string>(newValue));
499}
500
501/**
502 * @brief Changes IPv4 address origin property
503 *
504 * @param[in] ifaceId Id of interface whose IP should be modified
505 * @param[in] ipIdx Index of IP in input array that should be
506 * modified
507 * @param[in] ipHash DBus Hash id of modified IP
508 * @param[in] newValue New value in Redfish format
509 * @param[in] newValueDbus New value in D-Bus format
510 * @param[io] asyncResp Response object that will be returned to client
511 *
512 * @return true if give IP is valid and has been sent do D-Bus, false
513 * otherwise
514 */
515inline void changeIPv4Origin(const std::string &ifaceId, int ipIdx,
516 const std::string &ipHash,
517 const std::string &newValue,
518 const std::string &newValueDbus,
519 const std::shared_ptr<AsyncResp> asyncResp)
520{
521 auto callback = [asyncResp, ipIdx, newValue{std::move(newValue)}](
522 const boost::system::error_code ec) {
523 if (ec)
524 {
525 messages::addMessageToJson(
526 asyncResp->res.jsonValue, messages::internalError(),
527 "/IPv4Addresses/" + std::to_string(ipIdx) + "/AddressOrigin");
528 }
529 else
530 {
531 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["AddressOrigin"] =
532 newValue;
533 }
534 };
535
536 crow::connections::systemBus->async_method_call(
537 std::move(callback), "xyz.openbmc_project.Network",
538 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
539 "org.freedesktop.DBus.Properties", "Set",
540 "xyz.openbmc_project.Network.IP", "Origin",
541 sdbusplus::message::variant<std::string>(newValueDbus));
542}
543
544/**
545 * @brief Modifies SubnetMask for given IP
546 *
547 * @param[in] ifaceId Id of interface whose IP should be modified
548 * @param[in] ipIdx Index of IP in input array that should be
549 * modified
550 * @param[in] ipHash DBus Hash id of modified IP
551 * @param[in] newValueStr Mask in dot notation as string
552 * @param[in] newValue Mask as PrefixLength in bitcount
553 * @param[io] asyncResp Response object that will be returned to client
554 *
555 * @return None
556 */
557inline void changeIPv4SubnetMaskProperty(const std::string &ifaceId, int ipIdx,
558 const std::string &ipHash,
559 const std::string &newValueStr,
560 uint8_t &newValue,
561 std::shared_ptr<AsyncResp> asyncResp)
562{
563 auto callback = [asyncResp, ipIdx, newValueStr{std::move(newValueStr)}](
564 const boost::system::error_code ec) {
565 if (ec)
566 {
567 messages::addMessageToJson(
568 asyncResp->res.jsonValue, messages::internalError(),
569 "/IPv4Addresses/" + std::to_string(ipIdx) + "/SubnetMask");
570 }
571 else
572 {
573 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["SubnetMask"] =
574 newValueStr;
575 }
576 };
577
578 crow::connections::systemBus->async_method_call(
579 std::move(callback), "xyz.openbmc_project.Network",
580 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
581 "org.freedesktop.DBus.Properties", "Set",
582 "xyz.openbmc_project.Network.IP", "PrefixLength",
583 sdbusplus::message::variant<uint8_t>(newValue));
584}
585
586/**
587 * @brief Sets given HostName of the machine through D-Bus
588 *
589 * @param[in] newHostname New name that HostName will be changed to
590 * @param[in] callback Function that will be called after the operation
591 *
592 * @return None.
593 */
594template <typename CallbackFunc>
595void setHostName(const std::string &newHostname, CallbackFunc &&callback)
596{
597 crow::connections::systemBus->async_method_call(
598 callback, "xyz.openbmc_project.Network",
599 "/xyz/openbmc_project/network/config",
600 "org.freedesktop.DBus.Properties", "Set",
601 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
602 sdbusplus::message::variant<std::string>(newHostname));
603}
604
605/**
606 * @brief Deletes given IPv4
607 *
608 * @param[in] ifaceId Id of interface whose IP should be deleted
609 * @param[in] ipIdx Index of IP in input array that should be deleted
610 * @param[in] ipHash DBus Hash id of IP that should be deleted
611 * @param[io] asyncResp Response object that will be returned to client
612 *
613 * @return None
614 */
615inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
616 unsigned int ipIdx,
617 const std::shared_ptr<AsyncResp> asyncResp)
618{
619 crow::connections::systemBus->async_method_call(
620 [ipIdx, asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700621 if (ec)
622 {
623 messages::addMessageToJson(
624 asyncResp->res.jsonValue, messages::internalError(),
625 "/IPv4Addresses/" + std::to_string(ipIdx) + "/");
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100626 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700627 else
628 {
629 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx] = nullptr;
630 }
631 },
632 "xyz.openbmc_project.Network",
633 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
634 "xyz.openbmc_project.Object.Delete", "Delete");
635}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700636
Ed Tanous4a0cb852018-10-15 07:55:04 -0700637/**
638 * @brief Creates IPv4 with given data
639 *
640 * @param[in] ifaceId Id of interface whose IP should be deleted
641 * @param[in] ipIdx Index of IP in input array that should be deleted
642 * @param[in] ipHash DBus Hash id of IP that should be deleted
643 * @param[io] asyncResp Response object that will be returned to client
644 *
645 * @return None
646 */
647inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
648 uint8_t subnetMask, const std::string &gateway,
649 const std::string &address,
650 std::shared_ptr<AsyncResp> asyncResp)
651{
652 auto createIpHandler = [ipIdx,
653 asyncResp](const boost::system::error_code ec) {
654 if (ec)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700655 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700656 messages::addMessageToJson(
657 asyncResp->res.jsonValue, messages::internalError(),
658 "/IPv4Addresses/" + std::to_string(ipIdx) + "/");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700659 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700660 };
661
Ed Tanous4a0cb852018-10-15 07:55:04 -0700662 crow::connections::systemBus->async_method_call(
663 std::move(createIpHandler), "xyz.openbmc_project.Network",
664 "/xyz/openbmc_project/network/" + ifaceId,
665 "xyz.openbmc_project.Network.IP.Create", "IP",
666 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, subnetMask,
667 gateway);
668}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700669
Ed Tanous4a0cb852018-10-15 07:55:04 -0700670/**
671 * Function that retrieves all properties for given Ethernet Interface
672 * Object
673 * from EntityManager Network Manager
674 * @param ethiface_id a eth interface id to query on DBus
675 * @param callback a function that shall be called to convert Dbus output
676 * into JSON
677 */
678template <typename CallbackFunc>
679void getEthernetIfaceData(const std::string &ethiface_id,
680 CallbackFunc &&callback)
681{
682 crow::connections::systemBus->async_method_call(
683 [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
684 const boost::system::error_code error_code,
685 const GetManagedObjects &resp) {
686 EthernetInterfaceData ethData{};
687 boost::container::flat_set<IPv4AddressData> ipv4Data;
688
689 if (error_code)
690 {
691 callback(false, ethData, ipv4Data);
692 return;
693 }
694
695 extractEthernetInterfaceData(ethiface_id, resp, ethData);
696 extractIPData(ethiface_id, resp, ipv4Data);
697
698 // Fix global GW
699 for (IPv4AddressData &ipv4 : ipv4Data)
700 {
701 if ((ipv4.linktype == LinkType::Global) &&
702 (ipv4.gateway == "0.0.0.0"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700703 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700704 ipv4.gateway = ethData.default_gateway;
705 }
706 }
707
708 // Finally make a callback with usefull data
709 callback(true, ethData, ipv4Data);
710 },
711 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
712 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
713};
714
715/**
716 * Function that retrieves all Ethernet Interfaces available through Network
717 * Manager
718 * @param callback a function that shall be called to convert Dbus output
719 * into JSON.
720 */
721template <typename CallbackFunc>
722void getEthernetIfaceList(CallbackFunc &&callback)
723{
724 crow::connections::systemBus->async_method_call(
725 [callback{std::move(callback)}](
726 const boost::system::error_code error_code,
727 GetManagedObjects &resp) {
728 // Callback requires vector<string> to retrieve all available
729 // ethernet interfaces
730 std::vector<std::string> iface_list;
731 iface_list.reserve(resp.size());
732 if (error_code)
733 {
734 callback(false, iface_list);
735 return;
736 }
737
738 // Iterate over all retrieved ObjectPaths.
739 for (const auto &objpath : resp)
740 {
741 // And all interfaces available for certain ObjectPath.
742 for (const auto &interface : objpath.second)
743 {
744 // If interface is
745 // xyz.openbmc_project.Network.EthernetInterface, this is
746 // what we're looking for.
747 if (interface.first ==
748 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700749 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700750 // Cut out everyting until last "/", ...
751 const std::string &iface_id = objpath.first.str;
752 std::size_t last_pos = iface_id.rfind("/");
753 if (last_pos != std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700754 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700755 // and put it into output vector.
756 iface_list.emplace_back(
757 iface_id.substr(last_pos + 1));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700758 }
759 }
760 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700761 }
762 // Finally make a callback with useful data
763 callback(true, iface_list);
764 },
765 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
766 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100767};
768
769/**
770 * EthernetCollection derived class for delivering Ethernet Collection Schema
771 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700772class EthernetCollection : public Node
773{
774 public:
Ed Tanous4a0cb852018-10-15 07:55:04 -0700775 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700776 EthernetCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -0700777 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700778 {
779 Node::json["@odata.type"] =
780 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
781 Node::json["@odata.context"] =
782 "/redfish/v1/"
783 "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700784 Node::json["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700785 Node::json["Name"] = "Ethernet Network Interface Collection";
786 Node::json["Description"] =
787 "Collection of EthernetInterfaces for this Manager";
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100788
Ed Tanous1abe55e2018-09-05 08:30:59 -0700789 entityPrivileges = {
790 {boost::beast::http::verb::get, {{"Login"}}},
791 {boost::beast::http::verb::head, {{"Login"}}},
792 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
793 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
794 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
795 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
796 }
797
798 private:
799 /**
800 * Functions triggers appropriate requests on DBus
801 */
802 void doGet(crow::Response &res, const crow::Request &req,
803 const std::vector<std::string> &params) override
804 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700805 res.jsonValue = Node::json;
806 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -0700807 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -0700808 getEthernetIfaceList([&res](
809 const bool &success,
810 const std::vector<std::string> &iface_list) {
811 if (!success)
812 {
813 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700814 res.end();
Ed Tanous4a0cb852018-10-15 07:55:04 -0700815 return;
816 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700817
Ed Tanous4a0cb852018-10-15 07:55:04 -0700818 nlohmann::json &iface_array = res.jsonValue["Members"];
819 iface_array = nlohmann::json::array();
820 for (const std::string &iface_item : iface_list)
821 {
822 iface_array.push_back(
823 {{"@odata.id",
824 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
825 iface_item}});
826 }
827
828 res.jsonValue["Members@odata.count"] = iface_array.size();
829 res.jsonValue["@odata.id"] =
830 "/redfish/v1/Managers/bmc/EthernetInterfaces";
831 res.end();
832 });
833 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100834};
835
836/**
837 * EthernetInterface derived class for delivering Ethernet Schema
838 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700839class EthernetInterface : public Node
840{
841 public:
842 /*
843 * Default Constructor
844 */
Ed Tanous4a0cb852018-10-15 07:55:04 -0700845 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700846 EthernetInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -0700847 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -0700848 std::string())
849 {
850 Node::json["@odata.type"] =
851 "#EthernetInterface.v1_2_0.EthernetInterface";
852 Node::json["@odata.context"] =
853 "/redfish/v1/$metadata#EthernetInterface.EthernetInterface";
854 Node::json["Name"] = "Manager Ethernet Interface";
855 Node::json["Description"] = "Management Network Interface";
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100856
Ed Tanous1abe55e2018-09-05 08:30:59 -0700857 entityPrivileges = {
858 {boost::beast::http::verb::get, {{"Login"}}},
859 {boost::beast::http::verb::head, {{"Login"}}},
860 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
861 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
862 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
863 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200864 }
865
Ed Tanous1abe55e2018-09-05 08:30:59 -0700866 // TODO(kkowalsk) Find a suitable class/namespace for this
867 static void handleVlanPatch(const std::string &ifaceId,
868 const nlohmann::json &input,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700869 const EthernetInterfaceData &ethData,
870 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700871 {
872 if (!input.is_object())
873 {
874 messages::addMessageToJson(
875 asyncResp->res.jsonValue,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700876 messages::propertyValueTypeError(input.dump(), "VLAN"), "/");
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200877 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700878 }
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200879
Ed Tanous4a0cb852018-10-15 07:55:04 -0700880 nlohmann::json::const_iterator vlanEnable = input.find("VLANEnable");
881 if (vlanEnable == input.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700882 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700883 messages::addMessageToJson(asyncResp->res.jsonValue,
884 messages::propertyMissing("VLANEnable"),
885 "/VLANEnable");
886 return;
887 }
888 const bool *vlanEnableBool = vlanEnable->get_ptr<const bool *>();
889 if (vlanEnableBool == nullptr)
890 {
891 messages::addMessageToJson(asyncResp->res.jsonValue,
892 messages::propertyValueTypeError(
893 vlanEnable->dump(), "VLANEnable"),
894 "/VLANEnable");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700895 return;
896 }
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200897
Ed Tanous4a0cb852018-10-15 07:55:04 -0700898 nlohmann::json::const_iterator vlanId = input.find("VLANId");
899 if (vlanId == input.end())
900 {
901 messages::addMessageToJson(asyncResp->res.jsonValue,
902 messages::propertyMissing("VLANId"),
903 "/VLANId");
904 return;
905 }
906 const uint64_t *vlanIdUint = vlanId->get_ptr<const uint64_t *>();
907 if (vlanIdUint == nullptr)
908 {
909 messages::addMessageToJson(
910 asyncResp->res.jsonValue,
911 messages::propertyValueTypeError(vlanId->dump(), "VLANId"),
912 "/VLANId");
913 return;
914 }
915
916 if (!ethData.vlan_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700917 {
918 // This interface is not a VLAN. Cannot do anything with it
919 // TODO(kkowalsk) Change this message
Ed Tanous4a0cb852018-10-15 07:55:04 -0700920 messages::addMessageToJson(
921 asyncResp->res.jsonValue,
922 messages::propertyNotWritable("VLANEnable"), "/VLANEnable");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700923
Ed Tanous1abe55e2018-09-05 08:30:59 -0700924 return;
925 }
926
927 // VLAN is configured on the interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700928 if (*vlanEnableBool == true)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700929 {
930 // Change VLAN Id
Ed Tanous4a0cb852018-10-15 07:55:04 -0700931 asyncResp->res.jsonValue["VLANId"] = *vlanIdUint;
932 auto callback = [asyncResp](const boost::system::error_code ec) {
933 if (ec)
934 {
935 messages::addMessageToJson(asyncResp->res.jsonValue,
936 messages::internalError(), "/");
937 }
938 else
939 {
940 asyncResp->res.jsonValue["VLANEnable"] = true;
941 }
942 };
943 crow::connections::systemBus->async_method_call(
944 std::move(callback), "xyz.openbmc_project.Network",
945 "/xyz/openbmc_project/network/" + ifaceId,
946 "org.freedesktop.DBus.Properties", "Set",
947 "xyz.openbmc_project.Network.VLAN", "Id",
948 sdbusplus::message::variant<uint32_t>(*vlanIdUint));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700949 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700950 else
Ed Tanous1abe55e2018-09-05 08:30:59 -0700951 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700952 auto callback = [asyncResp](const boost::system::error_code ec) {
953 if (ec)
954 {
955 messages::addMessageToJson(asyncResp->res.jsonValue,
956 messages::internalError(), "/");
957 return;
958 }
959 asyncResp->res.jsonValue["VLANEnable"] = false;
960 };
961
962 crow::connections::systemBus->async_method_call(
963 std::move(callback), "xyz.openbmc_project.Network",
964 "/xyz/openbmc_project/network/" + ifaceId,
965 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700966 }
967 }
968
969 private:
970 void handleHostnamePatch(const nlohmann::json &input,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700971 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700972 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700973 const std::string *newHostname = input.get_ptr<const std::string *>();
974 if (newHostname == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700975 {
976 messages::addMessageToJson(
977 asyncResp->res.jsonValue,
978 messages::propertyValueTypeError(input.dump(), "HostName"),
979 "/HostName");
Ed Tanous4a0cb852018-10-15 07:55:04 -0700980 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700981 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700982
983 // Change hostname
984 setHostName(
985 *newHostname, [asyncResp, newHostname{std::string(*newHostname)}](
986 const boost::system::error_code ec) {
987 if (ec)
988 {
989 messages::addMessageToJson(asyncResp->res.jsonValue,
990 messages::internalError(),
991 "/HostName");
992 }
993 else
994 {
995 asyncResp->res.jsonValue["HostName"] = newHostname;
996 }
997 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700998 }
999
Ed Tanous4a0cb852018-10-15 07:55:04 -07001000 void handleIPv4Patch(
1001 const std::string &ifaceId, const nlohmann::json &input,
1002 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
1003 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001004 {
1005 if (!input.is_array())
1006 {
1007 messages::addMessageToJson(
1008 asyncResp->res.jsonValue,
1009 messages::propertyValueTypeError(input.dump(), "IPv4Addresses"),
1010 "/IPv4Addresses");
1011 return;
1012 }
1013
1014 // According to Redfish PATCH definition, size must be at least equal
Ed Tanous4a0cb852018-10-15 07:55:04 -07001015 if (input.size() < ipv4Data.size())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001016 {
1017 // TODO(kkowalsk) This should be a message indicating that not
1018 // enough data has been provided
1019 messages::addMessageToJson(asyncResp->res.jsonValue,
1020 messages::internalError(),
1021 "/IPv4Addresses");
1022 return;
1023 }
1024
Ed Tanous4a0cb852018-10-15 07:55:04 -07001025 int entryIdx = 0;
1026 boost::container::flat_set<IPv4AddressData>::const_iterator thisData =
1027 ipv4Data.begin();
1028 for (const nlohmann::json &thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001029 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001030 std::string pathString =
1031 "/IPv4Addresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001032 // Check that entry is not of some unexpected type
Ed Tanous4a0cb852018-10-15 07:55:04 -07001033 if (!thisJson.is_object() && !thisJson.is_null())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001034 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001035 messages::addMessageToJson(asyncResp->res.jsonValue,
1036 messages::propertyValueTypeError(
1037 thisJson.dump(), "IPv4Address"),
1038 pathString);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001039
1040 continue;
1041 }
1042
Ed Tanous4a0cb852018-10-15 07:55:04 -07001043 nlohmann::json::const_iterator addressFieldIt =
1044 thisJson.find("Address");
1045 const std::string *addressField = nullptr;
1046 if (addressFieldIt != thisJson.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001047 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001048 addressField = addressFieldIt->get_ptr<const std::string *>();
1049 if (addressField == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001050 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001051 messages::addMessageToJson(
1052 asyncResp->res.jsonValue,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001053 messages::propertyValueFormatError(
1054 addressFieldIt->dump(), "Address"),
1055 pathString + "/Address");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001056 continue;
1057 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001058 else
1059 {
1060 if (!ipv4VerifyIpAndGetBitcount(*addressField))
1061 {
1062 messages::addMessageToJson(
1063 asyncResp->res.jsonValue,
1064 messages::propertyValueFormatError(*addressField,
1065 "Address"),
1066 pathString + "/Address");
1067 continue;
1068 }
1069 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001070 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001071
1072 boost::optional<uint8_t> prefixLength;
1073 const std::string *subnetField = nullptr;
1074 nlohmann::json::const_iterator subnetFieldIt =
1075 thisJson.find("SubnetMask");
1076 if (subnetFieldIt != thisJson.end())
1077 {
1078 subnetField = subnetFieldIt->get_ptr<const std::string *>();
1079 if (subnetField == nullptr)
1080 {
1081 messages::addMessageToJson(
1082 asyncResp->res.jsonValue,
1083 messages::propertyValueFormatError(*subnetField,
1084 "SubnetMask"),
1085 pathString + "/SubnetMask");
1086 continue;
1087 }
1088 else
1089 {
1090 prefixLength = 0;
1091 if (!ipv4VerifyIpAndGetBitcount(*subnetField,
1092 &*prefixLength))
1093 {
1094 messages::addMessageToJson(
1095 asyncResp->res.jsonValue,
1096 messages::propertyValueFormatError(*subnetField,
1097 "SubnetMask"),
1098 pathString + "/SubnetMask");
1099 continue;
1100 }
1101 }
1102 }
1103
1104 std::string addressOriginInDBusFormat;
1105 const std::string *addressOriginField = nullptr;
1106 nlohmann::json::const_iterator addressOriginFieldIt =
1107 thisJson.find("AddressOrigin");
1108 if (addressOriginFieldIt != thisJson.end())
1109 {
1110 const std::string *addressOriginField =
1111 addressOriginFieldIt->get_ptr<const std::string *>();
1112 if (addressOriginField == nullptr)
1113 {
1114 messages::addMessageToJson(
1115 asyncResp->res.jsonValue,
1116 messages::propertyValueFormatError(*addressOriginField,
1117 "AddressOrigin"),
1118 pathString + "/AddressOrigin");
1119 continue;
1120 }
1121 else
1122 {
1123 // Get Address origin in proper format
1124 addressOriginInDBusFormat =
1125 translateAddressOriginRedfishToDbus(
1126 *addressOriginField);
1127 if (addressOriginInDBusFormat.empty())
1128 {
1129 messages::addMessageToJson(
1130 asyncResp->res.jsonValue,
1131 messages::propertyValueNotInList(
1132 *addressOriginField, "AddressOrigin"),
1133 pathString + "/AddressOrigin");
1134 continue;
1135 }
1136 }
1137 }
1138
1139 nlohmann::json::const_iterator gatewayFieldIt =
1140 thisJson.find("Gateway");
1141 const std::string *gatewayField = nullptr;
1142 if (gatewayFieldIt != thisJson.end())
1143 {
1144 const std::string *gatewayField =
1145 gatewayFieldIt->get_ptr<const std::string *>();
1146 if (gatewayField == nullptr ||
1147 !ipv4VerifyIpAndGetBitcount(*gatewayField))
1148 {
1149 messages::addMessageToJson(
1150 asyncResp->res.jsonValue,
1151 messages::propertyValueFormatError(*gatewayField,
1152 "Gateway"),
1153 pathString + "/Gateway");
1154 continue;
1155 }
1156 }
1157
1158 // if a vlan already exists, modify the existing
1159 if (thisData != ipv4Data.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001160 {
1161 // Existing object that should be modified/deleted/remain
1162 // unchanged
Ed Tanous4a0cb852018-10-15 07:55:04 -07001163 if (thisJson.is_null())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001164 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001165 auto callback = [entryIdx{std::to_string(entryIdx)},
1166 asyncResp](
1167 const boost::system::error_code ec) {
1168 if (ec)
1169 {
1170 messages::addMessageToJson(
1171 asyncResp->res.jsonValue,
1172 messages::internalError(),
1173 "/IPv4Addresses/" + entryIdx + "/");
1174 return;
1175 }
1176 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] =
1177 nullptr;
1178 };
1179 crow::connections::systemBus->async_method_call(
1180 std::move(callback), "xyz.openbmc_project.Network",
1181 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" +
1182 thisData->id,
1183 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001184 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001185 else if (thisJson.is_object())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001186 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001187 // Apply changes
Ed Tanous4a0cb852018-10-15 07:55:04 -07001188 if (addressField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001189 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001190 auto callback =
1191 [asyncResp, entryIdx,
1192 addressField{std::string(*addressField)}](
1193 const boost::system::error_code ec) {
1194 if (ec)
1195 {
1196 messages::addMessageToJson(
1197 asyncResp->res.jsonValue,
1198 messages::internalError(),
1199 "/IPv4Addresses/" +
1200 std::to_string(entryIdx) +
1201 "/Address");
1202 return;
1203 }
1204 asyncResp->res
1205 .jsonValue["IPv4Addresses"][std::to_string(
1206 entryIdx)]["Address"] = addressField;
1207 };
1208
1209 crow::connections::systemBus->async_method_call(
1210 std::move(callback), "xyz.openbmc_project.Network",
1211 "/xyz/openbmc_project/network/" + ifaceId +
1212 "/ipv4/" + thisData->id,
1213 "org.freedesktop.DBus.Properties", "Set",
1214 "xyz.openbmc_project.Network.IP", "Address",
1215 sdbusplus::message::variant<std::string>(
1216 *addressField));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001217 }
1218
Ed Tanous4a0cb852018-10-15 07:55:04 -07001219 if (prefixLength && subnetField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001220 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001221 changeIPv4SubnetMaskProperty(ifaceId, entryIdx,
1222 thisData->id, *subnetField,
1223 *prefixLength, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001224 }
1225
Ed Tanous4a0cb852018-10-15 07:55:04 -07001226 if (!addressOriginInDBusFormat.empty() &&
1227 addressOriginField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001228 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001229 changeIPv4Origin(ifaceId, entryIdx, thisData->id,
1230 *addressOriginField,
1231 addressOriginInDBusFormat, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001232 }
1233
Ed Tanous4a0cb852018-10-15 07:55:04 -07001234 if (gatewayField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001235 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001236 auto callback =
1237 [asyncResp, entryIdx,
1238 gatewayField{std::string(*gatewayField)}](
1239 const boost::system::error_code ec) {
1240 if (ec)
1241 {
1242 messages::addMessageToJson(
1243 asyncResp->res.jsonValue,
1244 messages::internalError(),
1245 "/IPv4Addresses/" +
1246 std::to_string(entryIdx) +
1247 "/Gateway");
1248 return;
1249 }
1250 asyncResp->res
1251 .jsonValue["IPv4Addresses"][std::to_string(
1252 entryIdx)]["Gateway"] =
1253 std::move(gatewayField);
1254 };
1255
1256 crow::connections::systemBus->async_method_call(
1257 std::move(callback), "xyz.openbmc_project.Network",
1258 "/xyz/openbmc_project/network/" + ifaceId +
1259 "/ipv4/" + thisData->id,
1260 "org.freedesktop.DBus.Properties", "Set",
1261 "xyz.openbmc_project.Network.IP", "Gateway",
1262 sdbusplus::message::variant<std::string>(
1263 *gatewayField));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001264 }
1265 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001266 thisData++;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001267 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001268 else
1269 {
1270 // Create IPv4 with provided data
1271 if (gatewayField == nullptr)
1272 {
1273 messages::addMessageToJson(
1274 asyncResp->res.jsonValue,
1275 messages::propertyMissing("Gateway"),
1276 pathString + "/Gateway");
1277 continue;
1278 }
1279
1280 if (addressField == nullptr)
1281 {
1282 messages::addMessageToJson(
1283 asyncResp->res.jsonValue,
1284 messages::propertyMissing("Address"),
1285 pathString + "/Address");
1286 continue;
1287 }
1288
1289 if (!prefixLength)
1290 {
1291 messages::addMessageToJson(
1292 asyncResp->res.jsonValue,
1293 messages::propertyMissing("SubnetMask"),
1294 pathString + "/SubnetMask");
1295 continue;
1296 }
1297
1298 createIPv4(ifaceId, entryIdx, *prefixLength, *gatewayField,
1299 *addressField, asyncResp);
1300 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] = thisJson;
1301 }
1302 entryIdx++;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001303 }
1304 }
1305
Ed Tanous4a0cb852018-10-15 07:55:04 -07001306 nlohmann::json parseInterfaceData(
1307 const std::string &iface_id, const EthernetInterfaceData &ethData,
1308 const boost::container::flat_set<IPv4AddressData> &ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001309 {
1310 // Copy JSON object to avoid race condition
Ed Tanous4a0cb852018-10-15 07:55:04 -07001311 nlohmann::json json_response(Node::json);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001312
Ed Tanous4a0cb852018-10-15 07:55:04 -07001313 json_response["Id"] = iface_id;
1314 json_response["@odata.id"] =
1315 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001316
Ed Tanous4a0cb852018-10-15 07:55:04 -07001317 json_response["SpeedMbps"] = ethData.speed;
1318 json_response["MACAddress"] = ethData.mac_address;
1319 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001320 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001321 json_response["HostName"] = ethData.hostname;
1322 }
1323
1324 nlohmann::json &vlanObj = json_response["VLAN"];
1325 if (ethData.vlan_id)
1326 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001327 vlanObj["VLANEnable"] = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001328 vlanObj["VLANId"] = *ethData.vlan_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001329 }
1330 else
1331 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001332 vlanObj["VLANEnable"] = false;
1333 vlanObj["VLANId"] = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001334 }
1335
Ed Tanous4a0cb852018-10-15 07:55:04 -07001336 if (ipv4Data.size() > 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001337 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001338 nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
1339 ipv4_array = nlohmann::json::array();
1340 for (auto &ipv4_config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001341 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001342 if (!ipv4_config.address.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001343 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001344 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
1345 {"SubnetMask", ipv4_config.netmask},
1346 {"Address", ipv4_config.address}});
Ed Tanous1abe55e2018-09-05 08:30:59 -07001347
Ed Tanous4a0cb852018-10-15 07:55:04 -07001348 if (!ipv4_config.gateway.empty())
1349 {
1350 ipv4_array.back()["Gateway"] = ipv4_config.gateway;
1351 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001352 }
1353 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001354 }
1355
Ed Tanous4a0cb852018-10-15 07:55:04 -07001356 return json_response;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001357 }
1358
1359 /**
1360 * Functions triggers appropriate requests on DBus
1361 */
1362 void doGet(crow::Response &res, const crow::Request &req,
1363 const std::vector<std::string> &params) override
1364 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001365 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001366 if (params.size() != 1)
1367 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001368 asyncResp->res.result(
1369 boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001370 return;
1371 }
1372
Ed Tanous4a0cb852018-10-15 07:55:04 -07001373 getEthernetIfaceData(
1374 params[0],
1375 [this, asyncResp, iface_id{std::string(params[0])}](
1376 const bool &success, const EthernetInterfaceData &ethData,
1377 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1378 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001379 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001380 // TODO(Pawel)consider distinguish between non existing
1381 // object, and other errors
1382 messages::addMessageToErrorJson(
Ed Tanous4a0cb852018-10-15 07:55:04 -07001383 asyncResp->res.jsonValue,
1384 messages::resourceNotFound("EthernetInterface",
1385 iface_id));
1386 asyncResp->res.result(
1387 boost::beast::http::status::not_found);
1388 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001389 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001390 asyncResp->res.jsonValue =
1391 parseInterfaceData(iface_id, ethData, ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001392 });
1393 }
1394
1395 void doPatch(crow::Response &res, const crow::Request &req,
1396 const std::vector<std::string> &params) override
1397 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001398 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001399 if (params.size() != 1)
1400 {
1401 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001402 return;
1403 }
1404
Ed Tanous4a0cb852018-10-15 07:55:04 -07001405 const std::string &iface_id = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001406
1407 nlohmann::json patchReq;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001408 if (!json_util::processJsonFromRequest(res, req, patchReq))
1409 {
1410 return;
1411 }
1412
Ed Tanous4a0cb852018-10-15 07:55:04 -07001413 // Get single eth interface data, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001414 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001415 getEthernetIfaceData(
1416 iface_id,
1417 [this, asyncResp, iface_id, patchReq = std::move(patchReq)](
1418 const bool &success, const EthernetInterfaceData &ethData,
1419 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001420 if (!success)
1421 {
1422 // ... otherwise return error
1423 // TODO(Pawel)consider distinguish between non existing
1424 // object, and other errors
1425 messages::addMessageToErrorJson(
Ed Tanous4a0cb852018-10-15 07:55:04 -07001426 asyncResp->res.jsonValue,
1427 messages::resourceNotFound("VLAN Network Interface",
1428 iface_id));
1429 asyncResp->res.result(
1430 boost::beast::http::status::not_found);
1431 asyncResp->res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001432
1433 return;
1434 }
1435
Ed Tanous4a0cb852018-10-15 07:55:04 -07001436 asyncResp->res.jsonValue =
1437 parseInterfaceData(iface_id, ethData, ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001438
Ed Tanous4a0cb852018-10-15 07:55:04 -07001439 for (auto propertyIt : patchReq.items())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001440 {
1441 if (propertyIt.key() == "VLAN")
1442 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001443 handleVlanPatch(iface_id, propertyIt.value(), ethData,
1444 asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001445 }
1446 else if (propertyIt.key() == "HostName")
1447 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001448 handleHostnamePatch(propertyIt.value(), asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001449 }
1450 else if (propertyIt.key() == "IPv4Addresses")
1451 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001452 handleIPv4Patch(iface_id, propertyIt.value(), ipv4Data,
Ed Tanous1abe55e2018-09-05 08:30:59 -07001453 asyncResp);
1454 }
1455 else if (propertyIt.key() == "IPv6Addresses")
1456 {
1457 // TODO(kkowalsk) IPv6 Not supported on D-Bus yet
1458 messages::addMessageToJsonRoot(
Ed Tanous4a0cb852018-10-15 07:55:04 -07001459 asyncResp->res.jsonValue,
Ed Tanous1abe55e2018-09-05 08:30:59 -07001460 messages::propertyNotWritable(propertyIt.key()));
1461 }
1462 else
1463 {
1464 auto fieldInJsonIt =
Ed Tanous4a0cb852018-10-15 07:55:04 -07001465 asyncResp->res.jsonValue.find(propertyIt.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001466
Ed Tanous4a0cb852018-10-15 07:55:04 -07001467 if (fieldInJsonIt == asyncResp->res.jsonValue.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001468 {
1469 // Field not in scope of defined fields
1470 messages::addMessageToJsonRoot(
Ed Tanous4a0cb852018-10-15 07:55:04 -07001471 asyncResp->res.jsonValue,
Ed Tanous1abe55e2018-09-05 08:30:59 -07001472 messages::propertyUnknown(propertyIt.key()));
1473 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001474 else
Ed Tanous1abe55e2018-09-05 08:30:59 -07001475 {
1476 // User attempted to modify non-writable field
1477 messages::addMessageToJsonRoot(
Ed Tanous4a0cb852018-10-15 07:55:04 -07001478 asyncResp->res.jsonValue,
1479 messages::propertyNotWritable(
1480 propertyIt.key()));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001481 }
1482 }
1483 }
1484 });
1485 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001486};
1487
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001488/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07001489 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
1490 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001491 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001492class VlanNetworkInterface : public Node
1493{
1494 public:
1495 /*
1496 * Default Constructor
1497 */
1498 template <typename CrowApp>
1499 // TODO(Pawel) Remove line from below, where we assume that there is only
Ed Tanous4a0cb852018-10-15 07:55:04 -07001500 // one manager called openbmc. This shall be generic, but requires to
1501 // update GetSubroutes method
Ed Tanous1abe55e2018-09-05 08:30:59 -07001502 VlanNetworkInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001503 Node(app,
1504 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>>/VLANs/"
1505 "<str>",
1506 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001507 {
1508 Node::json["@odata.type"] =
1509 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1510 Node::json["@odata.context"] =
1511 "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface";
1512 Node::json["Name"] = "VLAN Network Interface";
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001513
Ed Tanous1abe55e2018-09-05 08:30:59 -07001514 entityPrivileges = {
1515 {boost::beast::http::verb::get, {{"Login"}}},
1516 {boost::beast::http::verb::head, {{"Login"}}},
1517 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1518 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1519 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1520 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001521 }
1522
Ed Tanous1abe55e2018-09-05 08:30:59 -07001523 private:
Ed Tanous4a0cb852018-10-15 07:55:04 -07001524 nlohmann::json parseInterfaceData(
1525 const std::string &parent_iface_id, const std::string &iface_id,
1526 const EthernetInterfaceData &ethData,
1527 const boost::container::flat_set<IPv4AddressData> &ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001528 {
1529 // Copy JSON object to avoid race condition
Ed Tanous4a0cb852018-10-15 07:55:04 -07001530 nlohmann::json json_response(Node::json);
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001531
Ed Tanous1abe55e2018-09-05 08:30:59 -07001532 // Fill out obvious data...
Ed Tanous4a0cb852018-10-15 07:55:04 -07001533 json_response["Id"] = iface_id;
1534 json_response["@odata.id"] =
1535 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
1536 "/VLANs/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001537
Ed Tanous4a0cb852018-10-15 07:55:04 -07001538 json_response["VLANEnable"] = true;
1539 if (ethData.vlan_id)
1540 {
1541 json_response["VLANId"] = *ethData.vlan_id;
1542 }
1543 return json_response;
Ed Tanousa434f2b2018-07-27 13:04:22 -07001544 }
1545
Ed Tanous1abe55e2018-09-05 08:30:59 -07001546 bool verifyNames(crow::Response &res, const std::string &parent,
1547 const std::string &iface)
1548 {
1549 if (!boost::starts_with(iface, parent + "_"))
1550 {
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001551 messages::addMessageToErrorJson(
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001552 res.jsonValue,
Ed Tanous1abe55e2018-09-05 08:30:59 -07001553 messages::resourceNotFound("VLAN Network Interface", iface));
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001554 res.result(boost::beast::http::status::not_found);
1555 res.end();
1556
Ed Tanous1abe55e2018-09-05 08:30:59 -07001557 return false;
1558 }
1559 else
1560 {
1561 return true;
1562 }
1563 }
1564
1565 /**
1566 * Functions triggers appropriate requests on DBus
1567 */
1568 void doGet(crow::Response &res, const crow::Request &req,
1569 const std::vector<std::string> &params) override
1570 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001571 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1572 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07001573 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1574 // Check if there is required param, truly entering this shall be
1575 // impossible.
1576 if (params.size() != 2)
1577 {
1578 res.result(boost::beast::http::status::internal_server_error);
1579 res.end();
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001580 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001581 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001582
Ed Tanous4a0cb852018-10-15 07:55:04 -07001583 const std::string &parent_iface_id = params[0];
1584 const std::string &iface_id = params[1];
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001585
Ed Tanous4a0cb852018-10-15 07:55:04 -07001586 if (!verifyNames(res, parent_iface_id, iface_id))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001587 {
1588 return;
1589 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001590
Ed Tanous1abe55e2018-09-05 08:30:59 -07001591 // Get single eth interface data, and call the below callback for JSON
1592 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001593 getEthernetIfaceData(
1594 iface_id,
1595 [this, asyncResp, parent_iface_id, iface_id](
1596 const bool &success, const EthernetInterfaceData &ethData,
1597 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1598 if (success && ethData.vlan_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001599 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001600 asyncResp->res.jsonValue = parseInterfaceData(
1601 parent_iface_id, iface_id, ethData, ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001602 }
1603 else
1604 {
1605 // ... otherwise return error
1606 // TODO(Pawel)consider distinguish between non existing
1607 // object, and other errors
1608 messages::addMessageToErrorJson(
Ed Tanous4a0cb852018-10-15 07:55:04 -07001609 asyncResp->res.jsonValue,
1610 messages::resourceNotFound("VLAN Network Interface",
1611 iface_id));
1612 asyncResp->res.result(
1613 boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001614 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001615 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001616 }
1617
Ed Tanous1abe55e2018-09-05 08:30:59 -07001618 void doPatch(crow::Response &res, const crow::Request &req,
1619 const std::vector<std::string> &params) override
1620 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001621 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001622 if (params.size() != 2)
1623 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001624 asyncResp->res.result(
1625 boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001626 return;
1627 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001628
Ed Tanous1abe55e2018-09-05 08:30:59 -07001629 const std::string &parentIfaceId = params[0];
1630 const std::string &ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001631
Ed Tanous1abe55e2018-09-05 08:30:59 -07001632 if (!verifyNames(res, parentIfaceId, ifaceId))
1633 {
1634 return;
1635 }
1636
1637 nlohmann::json patchReq;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001638 if (!json_util::processJsonFromRequest(res, req, patchReq))
1639 {
1640 return;
1641 }
1642
1643 // Get single eth interface data, and call the below callback for JSON
1644 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001645 getEthernetIfaceData(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001646 ifaceId,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001647 [this, asyncResp, parentIfaceId, ifaceId,
1648 patchReq{std::move(patchReq)}](
1649 const bool &success, const EthernetInterfaceData &ethData,
1650 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001651 if (!success)
1652 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001653 // TODO(Pawel)consider distinguish between non existing
1654 // object, and other errors
1655 messages::addMessageToErrorJson(
Ed Tanous4a0cb852018-10-15 07:55:04 -07001656 asyncResp->res.jsonValue,
1657 messages::resourceNotFound("VLAN Network Interface",
1658 ifaceId));
1659 asyncResp->res.result(
1660 boost::beast::http::status::not_found);
1661 asyncResp->res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001662
1663 return;
1664 }
1665
Ed Tanous4a0cb852018-10-15 07:55:04 -07001666 asyncResp->res.jsonValue = parseInterfaceData(
1667 parentIfaceId, ifaceId, ethData, ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001668
Ed Tanous4a0cb852018-10-15 07:55:04 -07001669 for (auto propertyIt : patchReq.items())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001670 {
1671 if (propertyIt.key() != "VLANEnable" &&
1672 propertyIt.key() != "VLANId")
1673 {
1674 auto fieldInJsonIt =
Ed Tanous4a0cb852018-10-15 07:55:04 -07001675 asyncResp->res.jsonValue.find(propertyIt.key());
1676 if (fieldInJsonIt == asyncResp->res.jsonValue.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001677 {
1678 // Field not in scope of defined fields
1679 messages::addMessageToJsonRoot(
Ed Tanous4a0cb852018-10-15 07:55:04 -07001680 asyncResp->res.jsonValue,
Ed Tanous1abe55e2018-09-05 08:30:59 -07001681 messages::propertyUnknown(propertyIt.key()));
1682 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001683 else
Ed Tanous1abe55e2018-09-05 08:30:59 -07001684 {
1685 // User attempted to modify non-writable field
1686 messages::addMessageToJsonRoot(
Ed Tanous4a0cb852018-10-15 07:55:04 -07001687 asyncResp->res.jsonValue,
1688 messages::propertyNotWritable(
1689 propertyIt.key()));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001690 }
1691 }
1692 }
1693
Ed Tanous4a0cb852018-10-15 07:55:04 -07001694 EthernetInterface::handleVlanPatch(ifaceId, patchReq, ethData,
1695 asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001696 });
1697 }
1698
1699 void doDelete(crow::Response &res, const crow::Request &req,
1700 const std::vector<std::string> &params) override
1701 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001702 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001703 if (params.size() != 2)
1704 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001705 asyncResp->res.result(
1706 boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001707 return;
1708 }
1709
1710 const std::string &parentIfaceId = params[0];
1711 const std::string &ifaceId = params[1];
1712
Ed Tanous4a0cb852018-10-15 07:55:04 -07001713 if (!verifyNames(asyncResp->res, parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001714 {
1715 return;
1716 }
1717
1718 // Get single eth interface data, and call the below callback for JSON
1719 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001720 getEthernetIfaceData(ifaceId, [this, asyncResp,
1721 parentIfaceId{
1722 std::string(parentIfaceId)},
1723 ifaceId{std::string(ifaceId)}](
1724 const bool &success,
1725 const EthernetInterfaceData &ethData,
1726 const boost::container::flat_set<
1727 IPv4AddressData> &ipv4Data) {
1728 if (success && ethData.vlan_id)
1729 {
1730 asyncResp->res.jsonValue = parseInterfaceData(
1731 parentIfaceId, ifaceId, ethData, ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001732
Ed Tanous4a0cb852018-10-15 07:55:04 -07001733 auto callback = [asyncResp](
1734 const boost::system::error_code ec) {
1735 if (ec)
1736 {
1737 messages::addMessageToErrorJson(
1738 asyncResp->res.jsonValue,
1739 messages::internalError());
1740 asyncResp->res.result(
1741 boost::beast::http::status::internal_server_error);
1742 }
1743 };
1744 crow::connections::systemBus->async_method_call(
1745 std::move(callback), "xyz.openbmc_project.Network",
1746 std::string("/xyz/openbmc_project/network/") + ifaceId,
1747 "xyz.openbmc_project.Object.Delete", "Delete");
1748 }
1749 else
1750 {
1751 // ... otherwise return error
1752 // TODO(Pawel)consider distinguish between non existing object,
1753 // and
1754 // other errors
1755 messages::addMessageToErrorJson(
1756 asyncResp->res.jsonValue,
1757 messages::resourceNotFound("VLAN Network Interface",
1758 ifaceId));
1759 asyncResp->res.result(boost::beast::http::status::not_found);
1760 asyncResp->res.end();
1761 }
1762 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001763 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001764};
1765
1766/**
1767 * VlanNetworkInterfaceCollection derived class for delivering
1768 * VLANNetworkInterface Collection Schema
1769 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001770class VlanNetworkInterfaceCollection : public Node
1771{
1772 public:
1773 template <typename CrowApp>
1774 // TODO(Pawel) Remove line from below, where we assume that there is only
Ed Tanous4a0cb852018-10-15 07:55:04 -07001775 // one manager called openbmc. This shall be generic, but requires to
1776 // update GetSubroutes method
Ed Tanous1abe55e2018-09-05 08:30:59 -07001777 VlanNetworkInterfaceCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001778 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
1779 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001780 {
1781 Node::json["@odata.type"] =
1782 "#VLanNetworkInterfaceCollection.VLanNetworkInterfaceCollection";
1783 Node::json["@odata.context"] =
1784 "/redfish/v1/$metadata"
1785 "#VLanNetworkInterfaceCollection.VLanNetworkInterfaceCollection";
1786 Node::json["Name"] = "VLAN Network Interface Collection";
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001787
Ed Tanous1abe55e2018-09-05 08:30:59 -07001788 entityPrivileges = {
1789 {boost::beast::http::verb::get, {{"Login"}}},
1790 {boost::beast::http::verb::head, {{"Login"}}},
1791 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1792 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1793 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1794 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001795 }
1796
Ed Tanous1abe55e2018-09-05 08:30:59 -07001797 private:
1798 /**
1799 * Functions triggers appropriate requests on DBus
1800 */
1801 void doGet(crow::Response &res, const crow::Request &req,
1802 const std::vector<std::string> &params) override
1803 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001804 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001805 if (params.size() != 1)
1806 {
1807 // This means there is a problem with the router
Ed Tanous4a0cb852018-10-15 07:55:04 -07001808 asyncResp->res.result(
1809 boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001810 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07001811 }
1812
Ed Tanous4a0cb852018-10-15 07:55:04 -07001813 const std::string &rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001814
Ed Tanous4a0cb852018-10-15 07:55:04 -07001815 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001816 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001817 getEthernetIfaceList([this, asyncResp,
1818 rootInterfaceName{
1819 std::string(rootInterfaceName)}](
1820 const bool &success,
1821 const std::vector<std::string> &iface_list) {
1822 if (!success)
1823 {
1824 asyncResp->res.result(
1825 boost::beast::http::status::internal_server_error);
1826 return;
1827 }
1828 asyncResp->res.jsonValue = Node::json;
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001829
Ed Tanous4a0cb852018-10-15 07:55:04 -07001830 nlohmann::json iface_array = nlohmann::json::array();
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001831
Ed Tanous4a0cb852018-10-15 07:55:04 -07001832 for (const std::string &iface_item : iface_list)
1833 {
1834 if (boost::starts_with(iface_item, rootInterfaceName + "_"))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001835 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001836 iface_array.push_back(
1837 {{"@odata.id",
1838 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1839 rootInterfaceName + "/VLANs/" + iface_item}});
Ed Tanous1abe55e2018-09-05 08:30:59 -07001840 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001841 }
1842
1843 if (iface_array.empty())
1844 {
1845 messages::addMessageToErrorJson(
1846 asyncResp->res.jsonValue,
1847 messages::resourceNotFound("EthernetInterface",
1848 rootInterfaceName));
1849 asyncResp->res.result(boost::beast::http::status::not_found);
1850 return;
1851 }
1852 asyncResp->res.jsonValue["Members@odata.count"] =
1853 iface_array.size();
1854 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
1855 asyncResp->res.jsonValue["@odata.id"] =
1856 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1857 rootInterfaceName + "/VLANs";
1858 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001859 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001860
Ed Tanous1abe55e2018-09-05 08:30:59 -07001861 void doPost(crow::Response &res, const crow::Request &req,
1862 const std::vector<std::string> &params) override
1863 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001864 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001865 if (params.size() != 1)
1866 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001867 asyncResp->res.result(
1868 boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001869 return;
1870 }
1871
1872 nlohmann::json postReq;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001873 if (!json_util::processJsonFromRequest(res, req, postReq))
1874 {
1875 return;
1876 }
1877
Ed Tanous4a0cb852018-10-15 07:55:04 -07001878 auto vlanIdJson = json.find("VLANId");
1879 if (vlanIdJson == json.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001880 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001881 messages::addMessageToJson(asyncResp->res.jsonValue,
1882 messages::propertyMissing("VLANId"),
1883 "/VLANId");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001884 return;
1885 }
1886
Ed Tanous4a0cb852018-10-15 07:55:04 -07001887 const uint64_t *vlanId = vlanIdJson->get_ptr<const uint64_t *>();
1888 if (vlanId == nullptr)
1889 {
1890 messages::addMessageToJson(
1891 asyncResp->res.jsonValue,
1892 messages::propertyValueTypeError(vlanIdJson->dump(), "VLANId"),
1893 "/VLANId");
1894 return;
1895 }
1896 const std::string &rootInterfaceName = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001897
Ed Tanous4a0cb852018-10-15 07:55:04 -07001898 auto callback = [asyncResp](const boost::system::error_code ec) {
1899 if (ec)
1900 {
1901 // TODO(ed) make more consistent error messages based on
1902 // phosphor-network responses
1903 messages::addMessageToErrorJson(asyncResp->res.jsonValue,
1904 messages::internalError());
1905 return;
1906 }
1907 asyncResp->res.result(boost::beast::http::status::created);
1908 messages::addMessageToErrorJson(asyncResp->res.jsonValue,
1909 messages::created());
1910 };
1911 crow::connections::systemBus->async_method_call(
1912 std::move(callback), "xyz.openbmc_project.Network",
1913 "/xyz/openbmc_project/network",
1914 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
1915 rootInterfaceName, static_cast<uint32_t>(*vlanId));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001916 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001917};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001918} // namespace redfish