Skip to content

Commit 37a0800

Browse files
mauriciovasquezbernalfrisso
authored andcommitted
add service to base datamodel (#112)
There is an ongoing work to implement a /cubes/ api that will allow to create a list of cubes with a single request. In order to support that operation there should be a service field that indicates the service type of the cube. This commit adds the service to be base datamodel and updates polycubed to support it. Signed-off-by: Mauricio Vasquez B <mauriciovasquezbernal@gmail.com>
1 parent 9dc6142 commit 37a0800

8 files changed

Lines changed: 48 additions & 24 deletions

File tree

src/polycubed/src/base_cube.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ nlohmann::json BaseCube::to_json() const {
383383

384384
j["name"] = name_;
385385
j["uuid"] = uuid_.str();
386-
j["service"] = service_name_;
386+
j["service-name"] = service_name_;
387387
j["type"] = cube_type_to_string(type_);
388388
j["loglevel"] = logLevelString(level_);
389389

src/polycubed/src/base_model.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ Response BaseModel::get_parent(const std::string &cube_name) const {
8989
return Response{kOk, ::strdup(parent_name.data())};
9090
}
9191

92+
Response BaseModel::get_service(const std::string &cube_name) const {
93+
auto cube_ = ServiceController::get_cube(cube_name);
94+
if (cube_ == nullptr) {
95+
return Response{kNoContent, ::strdup("Cube does not exist")};
96+
}
97+
98+
auto service_name = "\"" + cube_->get_service_name() + "\"";
99+
100+
return Response{kOk, ::strdup(service_name.data())};
101+
}
102+
92103
Response BaseModel::get_port_uuid(const std::string &cube_name,
93104
const std::string &port_name) const {
94105
auto cube_ = ServiceController::get_cube(cube_name);

src/polycubed/src/base_model.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class BaseModel {
3434

3535
Response get_parent(const std::string &cube_name) const;
3636

37+
Response get_service(const std::string &cube_name) const;
38+
3739
// polycube-standard-base module
3840
Response get_port_uuid(const std::string &cube_name,
3941
const std::string &port_name) const;

src/polycubed/src/server/Resources/Data/BaseModel/ConcreteFactory.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bool ConcreteFactory::IsBaseModel(
4545
if (tree_names_.size() == 1) {
4646
auto leaf = tree_names_.front();
4747
if (leaf == "type" || leaf == "uuid" || leaf == "loglevel" ||
48-
leaf == "parent") {
48+
leaf == "parent" || leaf == "service-name") {
4949
return true;
5050
}
5151
} else if (tree_names_.size() == 2) {
@@ -151,8 +151,13 @@ std::unique_ptr<Endpoint::LeafResource> ConcreteFactory::RestLeaf(
151151
const ListKeyValues &keys) -> Response {
152152
return local_core->base_model()->get_parent(cube_name);
153153
};
154+
} else if (leaf == "service-name") {
155+
read_handler_ = [local_core](const std::string &cube_name,
156+
const ListKeyValues &keys) -> Response {
157+
return local_core->base_model()->get_service(cube_name);
158+
};
154159
} else {
155-
throw std::runtime_error("unkown element found in base datamodel");
160+
throw std::runtime_error("unkown element found in base datamodel:" + leaf);
156161
}
157162
} else if (tree_names_.size() == 2) {
158163
if (tree_names_.front() != "ports") {

src/polycubed/src/server/Resources/Endpoint/Service.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Service::Service(const std::string &name, const std::string &description,
3737
false),
3838
Body::Service(name, description, cli_example, version, nullptr),
3939
body_rest_endpoint_(base_address + name + '/'),
40-
path_param_{} {
40+
cube_names_{} {
4141
using Pistache::Rest::Routes::bind;
4242
auto router = core_->get_rest_server()->get_router();
4343

@@ -62,21 +62,11 @@ const std::string Service::Cube(const Pistache::Rest::Request &request) {
6262

6363
void Service::ClearCubes() {
6464
auto k = ListKeyValues{};
65-
for (const auto &cube_name : path_param_.Values()) {
65+
for (const auto &cube_name : cube_names_.Values()) {
6666
DeleteValue(cube_name, k);
6767
}
6868
}
6969

70-
std::vector<Response> Service::RequestValidate(
71-
const Request &request,
72-
[[maybe_unused]] const std::string &caller_name) const {
73-
std::vector<Response> errors;
74-
if (!path_param_.Validate(request.param(":name").as<std::string>())) {
75-
errors.push_back({ErrorTag::kBadElement, ::strdup(":name")});
76-
}
77-
return errors;
78-
}
79-
8070
void Service::CreateReplaceUpdate(const std::string &name, nlohmann::json &body,
8171
ResponseWriter response, bool update,
8272
bool initialization) {
@@ -99,7 +89,7 @@ void Service::CreateReplaceUpdate(const std::string &name, nlohmann::json &body,
9989
if (!update && (resp.error_tag == ErrorTag::kOk ||
10090
resp.error_tag == ErrorTag::kCreated ||
10191
resp.error_tag == ErrorTag::kNoContent)) {
102-
path_param_.AddValue(name);
92+
cube_names_.AddValue(name);
10393
}
10494
Server::ResponseGenerator::Generate(std::vector<Response>{resp},
10595
std::move(response));
@@ -136,6 +126,7 @@ void Service::post_body(const Request &request, ResponseWriter response) {
136126
std::move(response));
137127
return;
138128
}
129+
139130
CreateReplaceUpdate(body["name"].get<std::string>(), body,
140131
std::move(response), false, true);
141132
}
@@ -149,6 +140,15 @@ void Service::post(const Request &request, ResponseWriter response) {
149140
body = nlohmann::json::parse(request.body());
150141
}
151142
body["name"] = name;
143+
144+
if (body.count("service-name")) {
145+
if (body["service-name"] != Name()) {
146+
Server::ResponseGenerator::Generate({{kInvalidValue, nullptr}},
147+
std::move(response));
148+
return;
149+
}
150+
}
151+
152152
CreateReplaceUpdate(name, body, std::move(response), false, true);
153153
}
154154

@@ -179,10 +179,13 @@ void Service::patch(const Request &request, ResponseWriter response) {
179179
void Service::del(const Pistache::Rest::Request &request,
180180
Pistache::Http::ResponseWriter response) {
181181
auto name = request.param(":name").as<std::string>();
182-
if (ServiceController::exists_cube(name)) {
183-
path_param_.RemoveValue(name);
182+
if (!ServiceController::exists_cube(name)) {
183+
Server::ResponseGenerator::Generate({{kDataMissing, nullptr}},
184+
std::move(response));
185+
return;
184186
}
185187

188+
cube_names_.RemoveValue(name);
186189
auto k = ListKeyValues{};
187190
auto res = DeleteValue(name, k);
188191
Server::ResponseGenerator::Generate(std::vector<Response>{res},

src/polycubed/src/server/Resources/Endpoint/Service.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,14 @@ class Service : public ParentResource, public Body::Service {
5858

5959
private:
6060
const std::string body_rest_endpoint_;
61-
Validators::InSetValidator path_param_;
61+
Validators::InSetValidator cube_names_;
6262

6363
void CreateReplaceUpdate(const std::string &name, nlohmann::json &body,
6464
ResponseWriter response, bool replace,
6565
bool initialization);
6666

6767
nlohmann::json getServiceKeys() const;
6868

69-
std::vector<Response> RequestValidate(
70-
const Pistache::Rest::Request &request,
71-
const std::string &caller_name) const final;
72-
7369
void post(const Request &request, ResponseWriter response) final;
7470

7571
void put(const Request &request, ResponseWriter response) final;

src/polycubed/src/server/Validators/InSetValidator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace polycube::polycubed::Rest::Validators {
2020
InSetValidator::InSetValidator() : invalid_values_() {}
2121

2222
bool InSetValidator::Validate(const std::string &value) const {
23-
return invalid_values_.count(value) == 1;
23+
return invalid_values_.count(value) == 0;
2424
}
2525

2626
void InSetValidator::AddValue(const std::string &value) {

src/services/datamodel-common/polycube-base.yang

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ module polycube-base {
7272
polycube-base:cli-example "TC";
7373
}
7474

75+
leaf service-name {
76+
type string;
77+
config true;
78+
polycube-base:init-only-config;
79+
polycube-base:cli-example "helloworld";
80+
}
81+
7582
leaf loglevel {
7683
type enumeration {
7784
enum TRACE;

0 commit comments

Comments
 (0)