blob: aecfc21fe4a405f474635a6f79bb2a5c7d77bd99 [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{
635 auto createIpHandler = [ipIdx,
636 asyncResp](const boost::system::error_code ec) {
637 if (ec)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700638 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800639 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700640 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700641 };
642
Ed Tanous4a0cb852018-10-15 07:55:04 -0700643 crow::connections::systemBus->async_method_call(
644 std::move(createIpHandler), "xyz.openbmc_project.Network",
645 "/xyz/openbmc_project/network/" + ifaceId,
646 "xyz.openbmc_project.Network.IP.Create", "IP",
647 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, subnetMask,
648 gateway);
649}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700650
Ed Tanous4a0cb852018-10-15 07:55:04 -0700651/**
652 * Function that retrieves all properties for given Ethernet Interface
653 * Object
654 * from EntityManager Network Manager
655 * @param ethiface_id a eth interface id to query on DBus
656 * @param callback a function that shall be called to convert Dbus output
657 * into JSON
658 */
659template <typename CallbackFunc>
660void getEthernetIfaceData(const std::string &ethiface_id,
661 CallbackFunc &&callback)
662{
663 crow::connections::systemBus->async_method_call(
664 [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
665 const boost::system::error_code error_code,
666 const GetManagedObjects &resp) {
667 EthernetInterfaceData ethData{};
668 boost::container::flat_set<IPv4AddressData> ipv4Data;
669
670 if (error_code)
671 {
672 callback(false, ethData, ipv4Data);
673 return;
674 }
675
676 extractEthernetInterfaceData(ethiface_id, resp, ethData);
677 extractIPData(ethiface_id, resp, ipv4Data);
678
679 // Fix global GW
680 for (IPv4AddressData &ipv4 : ipv4Data)
681 {
682 if ((ipv4.linktype == LinkType::Global) &&
683 (ipv4.gateway == "0.0.0.0"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700684 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700685 ipv4.gateway = ethData.default_gateway;
686 }
687 }
688
689 // Finally make a callback with usefull data
690 callback(true, ethData, ipv4Data);
691 },
692 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
693 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
694};
695
696/**
697 * Function that retrieves all Ethernet Interfaces available through Network
698 * Manager
699 * @param callback a function that shall be called to convert Dbus output
700 * into JSON.
701 */
702template <typename CallbackFunc>
703void getEthernetIfaceList(CallbackFunc &&callback)
704{
705 crow::connections::systemBus->async_method_call(
706 [callback{std::move(callback)}](
707 const boost::system::error_code error_code,
708 GetManagedObjects &resp) {
709 // Callback requires vector<string> to retrieve all available
710 // ethernet interfaces
711 std::vector<std::string> iface_list;
712 iface_list.reserve(resp.size());
713 if (error_code)
714 {
715 callback(false, iface_list);
716 return;
717 }
718
719 // Iterate over all retrieved ObjectPaths.
720 for (const auto &objpath : resp)
721 {
722 // And all interfaces available for certain ObjectPath.
723 for (const auto &interface : objpath.second)
724 {
725 // If interface is
726 // xyz.openbmc_project.Network.EthernetInterface, this is
727 // what we're looking for.
728 if (interface.first ==
729 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700730 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700731 // Cut out everyting until last "/", ...
732 const std::string &iface_id = objpath.first.str;
733 std::size_t last_pos = iface_id.rfind("/");
734 if (last_pos != std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700735 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700736 // and put it into output vector.
737 iface_list.emplace_back(
738 iface_id.substr(last_pos + 1));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700739 }
740 }
741 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700742 }
743 // Finally make a callback with useful data
744 callback(true, iface_list);
745 },
746 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
747 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100748};
749
750/**
751 * EthernetCollection derived class for delivering Ethernet Collection Schema
752 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700753class EthernetCollection : public Node
754{
755 public:
Ed Tanous4a0cb852018-10-15 07:55:04 -0700756 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700757 EthernetCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -0700758 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700759 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700760 entityPrivileges = {
761 {boost::beast::http::verb::get, {{"Login"}}},
762 {boost::beast::http::verb::head, {{"Login"}}},
763 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
764 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
765 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
766 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
767 }
768
769 private:
770 /**
771 * Functions triggers appropriate requests on DBus
772 */
773 void doGet(crow::Response &res, const crow::Request &req,
774 const std::vector<std::string> &params) override
775 {
Ed Tanous0f74e642018-11-12 15:17:05 -0800776 res.jsonValue["@odata.type"] =
777 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
778 res.jsonValue["@odata.context"] =
779 "/redfish/v1/"
780 "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection";
781 res.jsonValue["@odata.id"] =
782 "/redfish/v1/Managers/bmc/EthernetInterfaces";
783 res.jsonValue["Name"] = "Ethernet Network Interface Collection";
784 res.jsonValue["Description"] =
785 "Collection of EthernetInterfaces for this Manager";
786
Ed Tanous4a0cb852018-10-15 07:55:04 -0700787 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -0700788 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -0700789 getEthernetIfaceList(
790 [&res](const bool &success,
791 const std::vector<std::string> &iface_list) {
792 if (!success)
793 {
794 messages::internalError(res);
795 res.end();
796 return;
797 }
798
799 nlohmann::json &iface_array = res.jsonValue["Members"];
800 iface_array = nlohmann::json::array();
801 for (const std::string &iface_item : iface_list)
802 {
803 iface_array.push_back(
804 {{"@odata.id",
805 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
806 iface_item}});
807 }
808
809 res.jsonValue["Members@odata.count"] = iface_array.size();
810 res.jsonValue["@odata.id"] =
811 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700812 res.end();
Jason M. Billsf12894f2018-10-09 12:45:45 -0700813 });
Ed Tanous4a0cb852018-10-15 07:55:04 -0700814 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100815};
816
817/**
818 * EthernetInterface derived class for delivering Ethernet Schema
819 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700820class EthernetInterface : public Node
821{
822 public:
823 /*
824 * Default Constructor
825 */
Ed Tanous4a0cb852018-10-15 07:55:04 -0700826 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700827 EthernetInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -0700828 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -0700829 std::string())
830 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700831 entityPrivileges = {
832 {boost::beast::http::verb::get, {{"Login"}}},
833 {boost::beast::http::verb::head, {{"Login"}}},
834 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
835 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
836 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
837 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200838 }
839
Ed Tanous1abe55e2018-09-05 08:30:59 -0700840 // TODO(kkowalsk) Find a suitable class/namespace for this
Ed Tanous0627a2c2018-11-29 17:09:23 -0800841 static void handleVlanPatch(const std::string &ifaceId, bool vlanEnable,
842 uint64_t vlanId,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700843 const EthernetInterfaceData &ethData,
844 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700845 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700846 if (!ethData.vlan_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700847 {
848 // This interface is not a VLAN. Cannot do anything with it
849 // TODO(kkowalsk) Change this message
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800850 messages::propertyNotWritable(asyncResp->res, "VLANEnable");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700851
Ed Tanous1abe55e2018-09-05 08:30:59 -0700852 return;
853 }
854
855 // VLAN is configured on the interface
Ed Tanous0627a2c2018-11-29 17:09:23 -0800856 if (vlanEnable == true)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700857 {
858 // Change VLAN Id
Ed Tanous0627a2c2018-11-29 17:09:23 -0800859 asyncResp->res.jsonValue["VLANId"] = vlanId;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700860 auto callback = [asyncResp](const boost::system::error_code ec) {
861 if (ec)
862 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700863 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700864 }
865 else
866 {
867 asyncResp->res.jsonValue["VLANEnable"] = true;
868 }
869 };
870 crow::connections::systemBus->async_method_call(
871 std::move(callback), "xyz.openbmc_project.Network",
872 "/xyz/openbmc_project/network/" + ifaceId,
873 "org.freedesktop.DBus.Properties", "Set",
874 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanousabf2add2019-01-22 16:40:12 -0800875 std::variant<uint32_t>(vlanId));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700876 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700877 else
Ed Tanous1abe55e2018-09-05 08:30:59 -0700878 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700879 auto callback = [asyncResp](const boost::system::error_code ec) {
880 if (ec)
881 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700882 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700883 return;
884 }
885 asyncResp->res.jsonValue["VLANEnable"] = false;
886 };
887
888 crow::connections::systemBus->async_method_call(
889 std::move(callback), "xyz.openbmc_project.Network",
890 "/xyz/openbmc_project/network/" + ifaceId,
891 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700892 }
893 }
894
895 private:
Ed Tanousbc0bd6e2018-12-10 14:07:55 -0800896 void handleHostnamePatch(const std::string &hostname,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700897 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700898 {
Ed Tanousbc0bd6e2018-12-10 14:07:55 -0800899 asyncResp->res.jsonValue["HostName"] = hostname;
900 crow::connections::systemBus->async_method_call(
901 [asyncResp](const boost::system::error_code ec) {
902 if (ec)
903 {
904 messages::internalError(asyncResp->res);
905 }
906 },
907 "xyz.openbmc_project.Network",
908 "/xyz/openbmc_project/network/config",
909 "org.freedesktop.DBus.Properties", "Set",
910 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanousabf2add2019-01-22 16:40:12 -0800911 std::variant<std::string>(hostname));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700912 }
913
Ed Tanous4a0cb852018-10-15 07:55:04 -0700914 void handleIPv4Patch(
915 const std::string &ifaceId, const nlohmann::json &input,
916 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
917 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700918 {
919 if (!input.is_array())
920 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700921 messages::propertyValueTypeError(asyncResp->res, input.dump(),
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800922 "IPv4Addresses");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700923 return;
924 }
925
926 // According to Redfish PATCH definition, size must be at least equal
Ed Tanous4a0cb852018-10-15 07:55:04 -0700927 if (input.size() < ipv4Data.size())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700928 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800929 messages::propertyValueFormatError(asyncResp->res, input.dump(),
930 "IPv4Addresses");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700931 return;
932 }
933
Ed Tanous4a0cb852018-10-15 07:55:04 -0700934 int entryIdx = 0;
935 boost::container::flat_set<IPv4AddressData>::const_iterator thisData =
936 ipv4Data.begin();
937 for (const nlohmann::json &thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700938 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700939 std::string pathString =
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800940 "IPv4Addresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700941 // Check that entry is not of some unexpected type
Ed Tanous4a0cb852018-10-15 07:55:04 -0700942 if (!thisJson.is_object() && !thisJson.is_null())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700943 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800944 messages::propertyValueTypeError(asyncResp->res,
945 thisJson.dump(),
946 pathString + "/IPv4Address");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700947
948 continue;
949 }
950
Ed Tanous4a0cb852018-10-15 07:55:04 -0700951 nlohmann::json::const_iterator addressFieldIt =
952 thisJson.find("Address");
953 const std::string *addressField = nullptr;
954 if (addressFieldIt != thisJson.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700955 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700956 addressField = addressFieldIt->get_ptr<const std::string *>();
957 if (addressField == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700958 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800959 messages::propertyValueFormatError(asyncResp->res,
960 addressFieldIt->dump(),
961 pathString + "/Address");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700962 continue;
963 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700964 else
965 {
966 if (!ipv4VerifyIpAndGetBitcount(*addressField))
967 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700968 messages::propertyValueFormatError(
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800969 asyncResp->res, *addressField,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700970 pathString + "/Address");
971 continue;
972 }
973 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700974 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700975
Ed Tanousa24526d2018-12-10 15:17:59 -0800976 std::optional<uint8_t> prefixLength;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700977 const std::string *subnetField = nullptr;
978 nlohmann::json::const_iterator subnetFieldIt =
979 thisJson.find("SubnetMask");
980 if (subnetFieldIt != thisJson.end())
981 {
982 subnetField = subnetFieldIt->get_ptr<const std::string *>();
983 if (subnetField == nullptr)
984 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700985 messages::propertyValueFormatError(
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800986 asyncResp->res, *subnetField,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700987 pathString + "/SubnetMask");
988 continue;
989 }
990 else
991 {
992 prefixLength = 0;
993 if (!ipv4VerifyIpAndGetBitcount(*subnetField,
994 &*prefixLength))
995 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700996 messages::propertyValueFormatError(
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800997 asyncResp->res, *subnetField,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700998 pathString + "/SubnetMask");
999 continue;
1000 }
1001 }
1002 }
1003
1004 std::string addressOriginInDBusFormat;
1005 const std::string *addressOriginField = nullptr;
1006 nlohmann::json::const_iterator addressOriginFieldIt =
1007 thisJson.find("AddressOrigin");
1008 if (addressOriginFieldIt != thisJson.end())
1009 {
1010 const std::string *addressOriginField =
1011 addressOriginFieldIt->get_ptr<const std::string *>();
1012 if (addressOriginField == nullptr)
1013 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001014 messages::propertyValueFormatError(
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001015 asyncResp->res, *addressOriginField,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001016 pathString + "/AddressOrigin");
1017 continue;
1018 }
1019 else
1020 {
1021 // Get Address origin in proper format
1022 addressOriginInDBusFormat =
1023 translateAddressOriginRedfishToDbus(
1024 *addressOriginField);
1025 if (addressOriginInDBusFormat.empty())
1026 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001027 messages::propertyValueNotInList(
1028 asyncResp->res, *addressOriginField,
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001029 pathString + "/AddressOrigin");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001030 continue;
1031 }
1032 }
1033 }
1034
1035 nlohmann::json::const_iterator gatewayFieldIt =
1036 thisJson.find("Gateway");
1037 const std::string *gatewayField = nullptr;
1038 if (gatewayFieldIt != thisJson.end())
1039 {
1040 const std::string *gatewayField =
1041 gatewayFieldIt->get_ptr<const std::string *>();
1042 if (gatewayField == nullptr ||
1043 !ipv4VerifyIpAndGetBitcount(*gatewayField))
1044 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001045 messages::propertyValueFormatError(
1046 asyncResp->res, *gatewayField, pathString + "/Gateway");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001047 continue;
1048 }
1049 }
1050
1051 // if a vlan already exists, modify the existing
1052 if (thisData != ipv4Data.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001053 {
1054 // Existing object that should be modified/deleted/remain
1055 // unchanged
Ed Tanous4a0cb852018-10-15 07:55:04 -07001056 if (thisJson.is_null())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001057 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001058 auto callback = [entryIdx{std::to_string(entryIdx)},
1059 asyncResp](
1060 const boost::system::error_code ec) {
1061 if (ec)
1062 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001063 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001064 return;
1065 }
1066 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] =
1067 nullptr;
1068 };
1069 crow::connections::systemBus->async_method_call(
1070 std::move(callback), "xyz.openbmc_project.Network",
1071 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" +
1072 thisData->id,
1073 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001074 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001075 else if (thisJson.is_object())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001076 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001077 // Apply changes
Ed Tanous4a0cb852018-10-15 07:55:04 -07001078 if (addressField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001079 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001080 auto callback =
1081 [asyncResp, entryIdx,
1082 addressField{std::string(*addressField)}](
1083 const boost::system::error_code ec) {
1084 if (ec)
1085 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001086 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001087 return;
1088 }
1089 asyncResp->res
1090 .jsonValue["IPv4Addresses"][std::to_string(
1091 entryIdx)]["Address"] = addressField;
1092 };
1093
1094 crow::connections::systemBus->async_method_call(
1095 std::move(callback), "xyz.openbmc_project.Network",
1096 "/xyz/openbmc_project/network/" + ifaceId +
1097 "/ipv4/" + thisData->id,
1098 "org.freedesktop.DBus.Properties", "Set",
1099 "xyz.openbmc_project.Network.IP", "Address",
Ed Tanousabf2add2019-01-22 16:40:12 -08001100 std::variant<std::string>(*addressField));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001101 }
1102
Ed Tanous4a0cb852018-10-15 07:55:04 -07001103 if (prefixLength && subnetField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001104 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001105 changeIPv4SubnetMaskProperty(ifaceId, entryIdx,
1106 thisData->id, *subnetField,
1107 *prefixLength, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001108 }
1109
Ed Tanous4a0cb852018-10-15 07:55:04 -07001110 if (!addressOriginInDBusFormat.empty() &&
1111 addressOriginField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001112 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001113 changeIPv4Origin(ifaceId, entryIdx, thisData->id,
1114 *addressOriginField,
1115 addressOriginInDBusFormat, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001116 }
1117
Ed Tanous4a0cb852018-10-15 07:55:04 -07001118 if (gatewayField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001119 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001120 auto callback =
1121 [asyncResp, entryIdx,
1122 gatewayField{std::string(*gatewayField)}](
1123 const boost::system::error_code ec) {
1124 if (ec)
1125 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001126 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001127 return;
1128 }
1129 asyncResp->res
1130 .jsonValue["IPv4Addresses"][std::to_string(
1131 entryIdx)]["Gateway"] =
1132 std::move(gatewayField);
1133 };
1134
1135 crow::connections::systemBus->async_method_call(
1136 std::move(callback), "xyz.openbmc_project.Network",
1137 "/xyz/openbmc_project/network/" + ifaceId +
1138 "/ipv4/" + thisData->id,
1139 "org.freedesktop.DBus.Properties", "Set",
1140 "xyz.openbmc_project.Network.IP", "Gateway",
Ed Tanousabf2add2019-01-22 16:40:12 -08001141 std::variant<std::string>(*gatewayField));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001142 }
1143 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001144 thisData++;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001145 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001146 else
1147 {
1148 // Create IPv4 with provided data
1149 if (gatewayField == nullptr)
1150 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001151 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001152 pathString + "/Gateway");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001153 continue;
1154 }
1155
1156 if (addressField == nullptr)
1157 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001158 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001159 pathString + "/Address");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001160 continue;
1161 }
1162
1163 if (!prefixLength)
1164 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001165 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001166 pathString + "/SubnetMask");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001167 continue;
1168 }
1169
1170 createIPv4(ifaceId, entryIdx, *prefixLength, *gatewayField,
1171 *addressField, asyncResp);
1172 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] = thisJson;
1173 }
1174 entryIdx++;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001175 }
1176 }
1177
Ed Tanous0f74e642018-11-12 15:17:05 -08001178 void parseInterfaceData(
1179 nlohmann::json &json_response, const std::string &iface_id,
1180 const EthernetInterfaceData &ethData,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001181 const boost::container::flat_set<IPv4AddressData> &ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001182 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001183 json_response["Id"] = iface_id;
1184 json_response["@odata.id"] =
1185 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
Ed Tanous029573d2019-02-01 10:57:49 -08001186 json_response["InterfaceEnabled"] = true;
1187 if (ethData.speed == 0)
1188 {
1189 json_response["LinkStatus"] = "NoLink";
1190 json_response["Status"] = {
1191 {"Health", "OK"},
1192 {"State", "Disabled"},
1193 };
1194 }
1195 else
1196 {
1197 json_response["LinkStatus"] = "LinkUp";
1198 json_response["Status"] = {
1199 {"Health", "OK"},
1200 {"State", "Enabled"},
1201 };
1202 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001203 json_response["SpeedMbps"] = ethData.speed;
1204 json_response["MACAddress"] = ethData.mac_address;
1205 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001206 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001207 json_response["HostName"] = ethData.hostname;
1208 }
1209
1210 nlohmann::json &vlanObj = json_response["VLAN"];
1211 if (ethData.vlan_id)
1212 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001213 vlanObj["VLANEnable"] = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001214 vlanObj["VLANId"] = *ethData.vlan_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001215 }
1216 else
1217 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001218 vlanObj["VLANEnable"] = false;
1219 vlanObj["VLANId"] = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001220 }
Ed Tanous029573d2019-02-01 10:57:49 -08001221 json_response["NameServers"] = ethData.nameservers;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001222
Ed Tanous4a0cb852018-10-15 07:55:04 -07001223 if (ipv4Data.size() > 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001224 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001225 nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
1226 ipv4_array = nlohmann::json::array();
1227 for (auto &ipv4_config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001228 {
Ed Tanous029573d2019-02-01 10:57:49 -08001229 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
1230 {"SubnetMask", ipv4_config.netmask},
1231 {"Address", ipv4_config.address},
1232 {"Gateway", ipv4_config.gateway}});
Ed Tanous1abe55e2018-09-05 08:30:59 -07001233 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001234 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001235 }
1236
1237 /**
1238 * Functions triggers appropriate requests on DBus
1239 */
1240 void doGet(crow::Response &res, const crow::Request &req,
1241 const std::vector<std::string> &params) override
1242 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001243 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001244 if (params.size() != 1)
1245 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001246 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001247 return;
1248 }
1249
Ed Tanous4a0cb852018-10-15 07:55:04 -07001250 getEthernetIfaceData(
1251 params[0],
1252 [this, asyncResp, iface_id{std::string(params[0])}](
1253 const bool &success, const EthernetInterfaceData &ethData,
1254 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1255 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001256 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001257 // TODO(Pawel)consider distinguish between non existing
1258 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001259 messages::resourceNotFound(asyncResp->res,
1260 "EthernetInterface", iface_id);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001261 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001262 }
Ed Tanous0f74e642018-11-12 15:17:05 -08001263 asyncResp->res.jsonValue["@odata.type"] =
1264 "#EthernetInterface.v1_2_0.EthernetInterface";
1265 asyncResp->res.jsonValue["@odata.context"] =
1266 "/redfish/v1/$metadata#EthernetInterface.EthernetInterface";
1267 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1268 asyncResp->res.jsonValue["Description"] =
1269 "Management Network Interface";
1270
1271 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
1272 ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001273 });
1274 }
1275
1276 void doPatch(crow::Response &res, const crow::Request &req,
1277 const std::vector<std::string> &params) override
1278 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001279 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001280 if (params.size() != 1)
1281 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001282 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001283 return;
1284 }
1285
Ed Tanous4a0cb852018-10-15 07:55:04 -07001286 const std::string &iface_id = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001287
Ed Tanous0627a2c2018-11-29 17:09:23 -08001288 std::optional<nlohmann::json> vlan;
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001289 std::optional<std::string> hostname;
Ed Tanous0627a2c2018-11-29 17:09:23 -08001290 std::optional<nlohmann::json> ipv4Addresses;
1291 std::optional<nlohmann::json> ipv6Addresses;
1292
1293 if (!json_util::readJson(req, res, "VLAN", vlan, "HostName", hostname,
1294 "IPv4Addresses", ipv4Addresses,
1295 "IPv6Addresses", ipv6Addresses))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001296 {
1297 return;
1298 }
Ed Tanous0627a2c2018-11-29 17:09:23 -08001299 std::optional<uint64_t> vlanId = 0;
1300 std::optional<bool> vlanEnable = false;
1301 if (vlan)
1302 {
1303 if (!json_util::readJson(*vlan, res, "VLANEnable", vlanEnable,
1304 "VLANId", vlanId))
1305 {
1306 return;
1307 }
1308 // Need both vlanId and vlanEnable to service this request
1309 if (static_cast<bool>(vlanId) ^ static_cast<bool>(vlanEnable))
1310 {
1311 if (vlanId)
1312 {
1313 messages::propertyMissing(asyncResp->res, "VLANEnable");
1314 }
1315 else
1316 {
1317 messages::propertyMissing(asyncResp->res, "VLANId");
1318 }
1319
1320 return;
1321 }
1322 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001323
Ed Tanous4a0cb852018-10-15 07:55:04 -07001324 // Get single eth interface data, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001325 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001326 getEthernetIfaceData(
1327 iface_id,
Ed Tanous0627a2c2018-11-29 17:09:23 -08001328 [this, asyncResp, iface_id, vlanId, vlanEnable,
1329 hostname = std::move(hostname),
1330 ipv4Addresses = std::move(ipv4Addresses),
1331 ipv6Addresses = std::move(ipv6Addresses)](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001332 const bool &success, const EthernetInterfaceData &ethData,
1333 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001334 if (!success)
1335 {
1336 // ... otherwise return error
1337 // TODO(Pawel)consider distinguish between non existing
1338 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001339 messages::resourceNotFound(
1340 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001341 return;
1342 }
1343
Ed Tanous0f74e642018-11-12 15:17:05 -08001344 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
1345 ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001346
Ed Tanous0627a2c2018-11-29 17:09:23 -08001347 if (vlanId && vlanEnable)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001348 {
Ed Tanous0627a2c2018-11-29 17:09:23 -08001349 handleVlanPatch(iface_id, *vlanId, *vlanEnable, ethData,
1350 asyncResp);
1351 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001352
Ed Tanous0627a2c2018-11-29 17:09:23 -08001353 if (hostname)
1354 {
1355 handleHostnamePatch(*hostname, asyncResp);
1356 }
1357
1358 if (ipv4Addresses)
1359 {
1360 handleIPv4Patch(iface_id, *ipv4Addresses, ipv4Data,
1361 asyncResp);
1362 }
1363
1364 if (ipv6Addresses)
1365 {
1366 // TODO(kkowalsk) IPv6 Not supported on D-Bus yet
1367 messages::propertyNotWritable(asyncResp->res,
1368 "IPv6Addresses");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001369 }
1370 });
1371 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001372};
1373
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001374/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07001375 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
1376 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001377 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001378class VlanNetworkInterface : public Node
1379{
1380 public:
1381 /*
1382 * Default Constructor
1383 */
1384 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001385 VlanNetworkInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001386 Node(app,
Ed Tanous0f74e642018-11-12 15:17:05 -08001387 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>",
Ed Tanous4a0cb852018-10-15 07:55:04 -07001388 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001389 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001390 entityPrivileges = {
1391 {boost::beast::http::verb::get, {{"Login"}}},
1392 {boost::beast::http::verb::head, {{"Login"}}},
1393 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1394 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1395 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1396 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001397 }
1398
Ed Tanous1abe55e2018-09-05 08:30:59 -07001399 private:
Ed Tanous0f74e642018-11-12 15:17:05 -08001400 void parseInterfaceData(
1401 nlohmann::json &json_response, const std::string &parent_iface_id,
1402 const std::string &iface_id, const EthernetInterfaceData &ethData,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001403 const boost::container::flat_set<IPv4AddressData> &ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001404 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001405 // Fill out obvious data...
Ed Tanous4a0cb852018-10-15 07:55:04 -07001406 json_response["Id"] = iface_id;
1407 json_response["@odata.id"] =
1408 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
1409 "/VLANs/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001410
Ed Tanous4a0cb852018-10-15 07:55:04 -07001411 json_response["VLANEnable"] = true;
1412 if (ethData.vlan_id)
1413 {
1414 json_response["VLANId"] = *ethData.vlan_id;
1415 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07001416 }
1417
Ed Tanous1abe55e2018-09-05 08:30:59 -07001418 bool verifyNames(crow::Response &res, const std::string &parent,
1419 const std::string &iface)
1420 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001421 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001422 if (!boost::starts_with(iface, parent + "_"))
1423 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001424 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
1425 iface);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001426 return false;
1427 }
1428 else
1429 {
1430 return true;
1431 }
1432 }
1433
1434 /**
1435 * Functions triggers appropriate requests on DBus
1436 */
1437 void doGet(crow::Response &res, const crow::Request &req,
1438 const std::vector<std::string> &params) override
1439 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001440 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1441 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07001442 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1443 // Check if there is required param, truly entering this shall be
1444 // impossible.
1445 if (params.size() != 2)
1446 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001447 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001448 res.end();
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001449 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001450 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001451
Ed Tanous4a0cb852018-10-15 07:55:04 -07001452 const std::string &parent_iface_id = params[0];
1453 const std::string &iface_id = params[1];
Ed Tanous0f74e642018-11-12 15:17:05 -08001454 res.jsonValue["@odata.type"] =
1455 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1456 res.jsonValue["@odata.context"] =
1457 "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface";
1458 res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001459
Ed Tanous4a0cb852018-10-15 07:55:04 -07001460 if (!verifyNames(res, parent_iface_id, iface_id))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001461 {
1462 return;
1463 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001464
Ed Tanous1abe55e2018-09-05 08:30:59 -07001465 // Get single eth interface data, and call the below callback for JSON
1466 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001467 getEthernetIfaceData(
1468 iface_id,
1469 [this, asyncResp, parent_iface_id, iface_id](
1470 const bool &success, const EthernetInterfaceData &ethData,
1471 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1472 if (success && ethData.vlan_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001473 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001474 parseInterfaceData(asyncResp->res.jsonValue,
1475 parent_iface_id, iface_id, ethData,
1476 ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001477 }
1478 else
1479 {
1480 // ... otherwise return error
1481 // TODO(Pawel)consider distinguish between non existing
1482 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001483 messages::resourceNotFound(
1484 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001485 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001486 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001487 }
1488
Ed Tanous1abe55e2018-09-05 08:30:59 -07001489 void doPatch(crow::Response &res, const crow::Request &req,
1490 const std::vector<std::string> &params) override
1491 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001492 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001493 if (params.size() != 2)
1494 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001495 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001496 return;
1497 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001498
Ed Tanous1abe55e2018-09-05 08:30:59 -07001499 const std::string &parentIfaceId = params[0];
1500 const std::string &ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001501
Ed Tanous1abe55e2018-09-05 08:30:59 -07001502 if (!verifyNames(res, parentIfaceId, ifaceId))
1503 {
1504 return;
1505 }
1506
Ed Tanous0627a2c2018-11-29 17:09:23 -08001507 bool vlanEnable = false;
1508 uint64_t vlanId = 0;
1509
1510 if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
1511 vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001512 {
1513 return;
1514 }
1515
1516 // Get single eth interface data, and call the below callback for JSON
1517 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001518 getEthernetIfaceData(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001519 ifaceId,
Ed Tanous0627a2c2018-11-29 17:09:23 -08001520 [this, asyncResp, parentIfaceId, ifaceId, vlanEnable, vlanId](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001521 const bool &success, const EthernetInterfaceData &ethData,
1522 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001523 if (!success)
1524 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001525 // TODO(Pawel)consider distinguish between non existing
1526 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001527 messages::resourceNotFound(
1528 asyncResp->res, "VLAN Network Interface", ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001529
1530 return;
1531 }
1532
Ed Tanous0f74e642018-11-12 15:17:05 -08001533 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
1534 ifaceId, ethData, ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001535
Ed Tanous0627a2c2018-11-29 17:09:23 -08001536 EthernetInterface::handleVlanPatch(ifaceId, vlanId, vlanEnable,
1537 ethData, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001538 });
1539 }
1540
1541 void doDelete(crow::Response &res, const crow::Request &req,
1542 const std::vector<std::string> &params) override
1543 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001544 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001545 if (params.size() != 2)
1546 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001547 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001548 return;
1549 }
1550
1551 const std::string &parentIfaceId = params[0];
1552 const std::string &ifaceId = params[1];
1553
Ed Tanous4a0cb852018-10-15 07:55:04 -07001554 if (!verifyNames(asyncResp->res, parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001555 {
1556 return;
1557 }
1558
1559 // Get single eth interface data, and call the below callback for JSON
1560 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001561 getEthernetIfaceData(
1562 ifaceId,
1563 [this, asyncResp, parentIfaceId{std::string(parentIfaceId)},
1564 ifaceId{std::string(ifaceId)}](
1565 const bool &success, const EthernetInterfaceData &ethData,
1566 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1567 if (success && ethData.vlan_id)
1568 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001569 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
1570 ifaceId, ethData, ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001571
Jason M. Billsf12894f2018-10-09 12:45:45 -07001572 auto callback =
1573 [asyncResp](const boost::system::error_code ec) {
1574 if (ec)
1575 {
1576 messages::internalError(asyncResp->res);
1577 }
1578 };
1579 crow::connections::systemBus->async_method_call(
1580 std::move(callback), "xyz.openbmc_project.Network",
1581 std::string("/xyz/openbmc_project/network/") + ifaceId,
1582 "xyz.openbmc_project.Object.Delete", "Delete");
1583 }
1584 else
1585 {
1586 // ... otherwise return error
1587 // TODO(Pawel)consider distinguish between non existing
1588 // object, and other errors
1589 messages::resourceNotFound(
1590 asyncResp->res, "VLAN Network Interface", ifaceId);
1591 }
1592 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001593 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001594};
1595
1596/**
1597 * VlanNetworkInterfaceCollection derived class for delivering
1598 * VLANNetworkInterface Collection Schema
1599 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001600class VlanNetworkInterfaceCollection : public Node
1601{
1602 public:
1603 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001604 VlanNetworkInterfaceCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001605 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
1606 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001607 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001608 entityPrivileges = {
1609 {boost::beast::http::verb::get, {{"Login"}}},
1610 {boost::beast::http::verb::head, {{"Login"}}},
1611 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1612 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1613 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1614 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001615 }
1616
Ed Tanous1abe55e2018-09-05 08:30:59 -07001617 private:
1618 /**
1619 * Functions triggers appropriate requests on DBus
1620 */
1621 void doGet(crow::Response &res, const crow::Request &req,
1622 const std::vector<std::string> &params) override
1623 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001624 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001625 if (params.size() != 1)
1626 {
1627 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07001628 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001629 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07001630 }
1631
Ed Tanous4a0cb852018-10-15 07:55:04 -07001632 const std::string &rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001633
Ed Tanous4a0cb852018-10-15 07:55:04 -07001634 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001635 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001636 getEthernetIfaceList(
1637 [this, asyncResp,
1638 rootInterfaceName{std::string(rootInterfaceName)}](
1639 const bool &success,
1640 const std::vector<std::string> &iface_list) {
1641 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001642 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001643 messages::internalError(asyncResp->res);
1644 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001645 }
Ed Tanous0f74e642018-11-12 15:17:05 -08001646 asyncResp->res.jsonValue["@odata.type"] =
1647 "#VLanNetworkInterfaceCollection."
1648 "VLanNetworkInterfaceCollection";
1649 asyncResp->res.jsonValue["@odata.context"] =
1650 "/redfish/v1/$metadata"
1651 "#VLanNetworkInterfaceCollection."
1652 "VLanNetworkInterfaceCollection";
1653 asyncResp->res.jsonValue["Name"] =
1654 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07001655
Jason M. Billsf12894f2018-10-09 12:45:45 -07001656 nlohmann::json iface_array = nlohmann::json::array();
1657
1658 for (const std::string &iface_item : iface_list)
1659 {
1660 if (boost::starts_with(iface_item, rootInterfaceName + "_"))
1661 {
1662 iface_array.push_back(
1663 {{"@odata.id",
1664 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1665 rootInterfaceName + "/VLANs/" + iface_item}});
1666 }
1667 }
1668
1669 if (iface_array.empty())
1670 {
1671 messages::resourceNotFound(
1672 asyncResp->res, "EthernetInterface", rootInterfaceName);
1673 return;
1674 }
1675 asyncResp->res.jsonValue["Members@odata.count"] =
1676 iface_array.size();
1677 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
1678 asyncResp->res.jsonValue["@odata.id"] =
1679 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1680 rootInterfaceName + "/VLANs";
1681 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001682 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001683
Ed Tanous1abe55e2018-09-05 08:30:59 -07001684 void doPost(crow::Response &res, const crow::Request &req,
1685 const std::vector<std::string> &params) override
1686 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001687 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001688 if (params.size() != 1)
1689 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001690 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001691 return;
1692 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001693
Ed Tanous0627a2c2018-11-29 17:09:23 -08001694 uint32_t vlanId = 0;
1695 if (!json_util::readJson(req, res, "VLANId", vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001696 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001697 return;
1698 }
1699 const std::string &rootInterfaceName = params[0];
Ed Tanous4a0cb852018-10-15 07:55:04 -07001700 auto callback = [asyncResp](const boost::system::error_code ec) {
1701 if (ec)
1702 {
1703 // TODO(ed) make more consistent error messages based on
1704 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07001705 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001706 return;
1707 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001708 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001709 };
1710 crow::connections::systemBus->async_method_call(
1711 std::move(callback), "xyz.openbmc_project.Network",
1712 "/xyz/openbmc_project/network",
1713 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
Ed Tanous0627a2c2018-11-29 17:09:23 -08001714 rootInterfaceName, vlanId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001715 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001716};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001717} // namespace redfish