Skip to content

Commit d679c83

Browse files
polycubed: set peers after all cubes are created
When a list of cubes is created this is possible that a port in a cube has as peer another port in a cube that hasn't been created yet, this causes an exception that aborts the port's creation. This commit strips the port's peer and sets them after all cubes are created. Fixes: fbf9bc9 ("add /cubes/ creation api") Signed-off-by: Mauricio Vasquez B <mauriciovasquezbernal@gmail.com>
1 parent 2692c3f commit d679c83

4 files changed

Lines changed: 47 additions & 5 deletions

File tree

src/polycubed/src/polycubed_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ namespace polycubed {
3939
class RestServer;
4040

4141
class PolycubedCore {
42+
friend class RestServer;
4243
public:
4344
PolycubedCore(BaseModel *base_model);
4445
~PolycubedCore();

src/polycubed/src/rest_server.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,21 +436,34 @@ void RestServer::post_cubes(const Pistache::Rest::Request &request,
436436
Pistache::Http::ResponseWriter response) {
437437
logRequest(request);
438438
try {
439-
json j = json::parse(request.body());
440-
logJson(j);
439+
json cubes = json::parse(request.body());
440+
logJson(cubes);
441+
/*
442+
* This is possible that a cube's port has as peer another port
443+
* of a cube that hasn't been created yet. This logic removes all
444+
* the peers from the request and sets them after all cubes have
445+
* been created.
446+
*/
447+
auto peers = utils::strip_port_peers(cubes);
441448
std::vector<Response> resp = {{ErrorTag::kNoContent, nullptr}};
442449
bool error = false;
443-
for (auto &it : j) {
444-
resp = core.get_service_controller(it["service-name"]).get_management_interface()->get_service()
445-
->CreateReplaceUpdate(it["name"], it, false, true);
450+
451+
for (auto &cube : cubes) {
452+
resp = core.get_service_controller(cube["service-name"]).get_management_interface()->get_service()
453+
->CreateReplaceUpdate(cube["name"], cube, false, true);
446454
if (!error && resp[0].error_tag != kCreated) {
447455
Rest::Server::ResponseGenerator::Generate(std::move(resp), std::move(response));
448456
error = true;
449457
}
450458
}
459+
451460
if (!error) {
452461
Rest::Server::ResponseGenerator::Generate(std::move(resp), std::move(response));
453462
}
463+
464+
for (auto &[peer1, peer2] : peers) {
465+
core.try_to_set_peer(peer1, peer2);
466+
}
454467
} catch (const std::runtime_error &e) {
455468
logger->error("{0}", e.what());
456469
response.send(Pistache::Http::Code::Bad_Request, e.what());

src/polycubed/src/utils/utils.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,29 @@ bool check_kernel_version(const std::string &version) {
4444
KERNEL_VERSION(major_r, minor_r, patch_r);
4545
}
4646

47+
std::map<std::string, std::string> strip_port_peers(json &cubes) {
48+
std::map<std::string, std::string> peers;
49+
50+
for (auto &cube : cubes) {
51+
if (!cube.count("ports")) {
52+
continue;
53+
}
54+
55+
auto &ports = cube.at("ports");
56+
57+
for (auto &port : ports) {
58+
if (port.count("peer")) {
59+
auto cube_name = cube.at("name").get<std::string>();
60+
auto port_name = port.at("name").get<std::string>();
61+
peers[cube_name + ":" + port_name] = port.at("peer").get<std::string>();
62+
port.erase("peer");
63+
}
64+
}
65+
}
66+
67+
return peers;
68+
}
69+
4770
} // namespace utils
4871
} // namespace polycubed
4972
} // namespace polycube

src/polycubed/src/utils/utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@
1515
*/
1616

1717
#include <string>
18+
#include <map>
19+
#include "polycube/services/json.hpp"
20+
21+
using json = nlohmann::json;
1822

1923
namespace polycube {
2024
namespace polycubed {
2125
namespace utils {
2226

2327
bool check_kernel_version(const std::string &version);
28+
std::map<std::string, std::string> strip_port_peers(json &cubes);
2429

2530
} // namespace utils
2631
} // namespace polycubed

0 commit comments

Comments
 (0)