Skip to content

Commit

Permalink
add service to base datamodel (#112)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mauriciovasquezbernal authored and frisso committed Apr 24, 2019
1 parent 9dc6142 commit 37a0800
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/polycubed/src/base_cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ nlohmann::json BaseCube::to_json() const {

j["name"] = name_;
j["uuid"] = uuid_.str();
j["service"] = service_name_;
j["service-name"] = service_name_;
j["type"] = cube_type_to_string(type_);
j["loglevel"] = logLevelString(level_);

Expand Down
11 changes: 11 additions & 0 deletions src/polycubed/src/base_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ Response BaseModel::get_parent(const std::string &cube_name) const {
return Response{kOk, ::strdup(parent_name.data())};
}

Response BaseModel::get_service(const std::string &cube_name) const {
auto cube_ = ServiceController::get_cube(cube_name);
if (cube_ == nullptr) {
return Response{kNoContent, ::strdup("Cube does not exist")};
}

auto service_name = "\"" + cube_->get_service_name() + "\"";

return Response{kOk, ::strdup(service_name.data())};
}

Response BaseModel::get_port_uuid(const std::string &cube_name,
const std::string &port_name) const {
auto cube_ = ServiceController::get_cube(cube_name);
Expand Down
2 changes: 2 additions & 0 deletions src/polycubed/src/base_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class BaseModel {

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

Response get_service(const std::string &cube_name) const;

// polycube-standard-base module
Response get_port_uuid(const std::string &cube_name,
const std::string &port_name) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bool ConcreteFactory::IsBaseModel(
if (tree_names_.size() == 1) {
auto leaf = tree_names_.front();
if (leaf == "type" || leaf == "uuid" || leaf == "loglevel" ||
leaf == "parent") {
leaf == "parent" || leaf == "service-name") {
return true;
}
} else if (tree_names_.size() == 2) {
Expand Down Expand Up @@ -151,8 +151,13 @@ std::unique_ptr<Endpoint::LeafResource> ConcreteFactory::RestLeaf(
const ListKeyValues &keys) -> Response {
return local_core->base_model()->get_parent(cube_name);
};
} else if (leaf == "service-name") {
read_handler_ = [local_core](const std::string &cube_name,
const ListKeyValues &keys) -> Response {
return local_core->base_model()->get_service(cube_name);
};
} else {
throw std::runtime_error("unkown element found in base datamodel");
throw std::runtime_error("unkown element found in base datamodel:" + leaf);
}
} else if (tree_names_.size() == 2) {
if (tree_names_.front() != "ports") {
Expand Down
33 changes: 18 additions & 15 deletions src/polycubed/src/server/Resources/Endpoint/Service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Service::Service(const std::string &name, const std::string &description,
false),
Body::Service(name, description, cli_example, version, nullptr),
body_rest_endpoint_(base_address + name + '/'),
path_param_{} {
cube_names_{} {
using Pistache::Rest::Routes::bind;
auto router = core_->get_rest_server()->get_router();

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

void Service::ClearCubes() {
auto k = ListKeyValues{};
for (const auto &cube_name : path_param_.Values()) {
for (const auto &cube_name : cube_names_.Values()) {
DeleteValue(cube_name, k);
}
}

std::vector<Response> Service::RequestValidate(
const Request &request,
[[maybe_unused]] const std::string &caller_name) const {
std::vector<Response> errors;
if (!path_param_.Validate(request.param(":name").as<std::string>())) {
errors.push_back({ErrorTag::kBadElement, ::strdup(":name")});
}
return errors;
}

void Service::CreateReplaceUpdate(const std::string &name, nlohmann::json &body,
ResponseWriter response, bool update,
bool initialization) {
Expand All @@ -99,7 +89,7 @@ void Service::CreateReplaceUpdate(const std::string &name, nlohmann::json &body,
if (!update && (resp.error_tag == ErrorTag::kOk ||
resp.error_tag == ErrorTag::kCreated ||
resp.error_tag == ErrorTag::kNoContent)) {
path_param_.AddValue(name);
cube_names_.AddValue(name);
}
Server::ResponseGenerator::Generate(std::vector<Response>{resp},
std::move(response));
Expand Down Expand Up @@ -136,6 +126,7 @@ void Service::post_body(const Request &request, ResponseWriter response) {
std::move(response));
return;
}

CreateReplaceUpdate(body["name"].get<std::string>(), body,
std::move(response), false, true);
}
Expand All @@ -149,6 +140,15 @@ void Service::post(const Request &request, ResponseWriter response) {
body = nlohmann::json::parse(request.body());
}
body["name"] = name;

if (body.count("service-name")) {
if (body["service-name"] != Name()) {
Server::ResponseGenerator::Generate({{kInvalidValue, nullptr}},
std::move(response));
return;
}
}

CreateReplaceUpdate(name, body, std::move(response), false, true);
}

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

cube_names_.RemoveValue(name);
auto k = ListKeyValues{};
auto res = DeleteValue(name, k);
Server::ResponseGenerator::Generate(std::vector<Response>{res},
Expand Down
6 changes: 1 addition & 5 deletions src/polycubed/src/server/Resources/Endpoint/Service.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,14 @@ class Service : public ParentResource, public Body::Service {

private:
const std::string body_rest_endpoint_;
Validators::InSetValidator path_param_;
Validators::InSetValidator cube_names_;

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

nlohmann::json getServiceKeys() const;

std::vector<Response> RequestValidate(
const Pistache::Rest::Request &request,
const std::string &caller_name) const final;

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

void put(const Request &request, ResponseWriter response) final;
Expand Down
2 changes: 1 addition & 1 deletion src/polycubed/src/server/Validators/InSetValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace polycube::polycubed::Rest::Validators {
InSetValidator::InSetValidator() : invalid_values_() {}

bool InSetValidator::Validate(const std::string &value) const {
return invalid_values_.count(value) == 1;
return invalid_values_.count(value) == 0;
}

void InSetValidator::AddValue(const std::string &value) {
Expand Down
7 changes: 7 additions & 0 deletions src/services/datamodel-common/polycube-base.yang
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ module polycube-base {
polycube-base:cli-example "TC";
}

leaf service-name {
type string;
config true;
polycube-base:init-only-config;
polycube-base:cli-example "helloworld";
}

leaf loglevel {
type enumeration {
enum TRACE;
Expand Down

0 comments on commit 37a0800

Please sign in to comment.