Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/polycubed/src/server/Resources/Body/ListResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,46 @@ void ListResource::SetDefaultIfMissing(nlohmann::json &body,
}
}

void ListResource::FillKeys(nlohmann::json &body, const ListKeyValues &keys) {
// TODO: is there a more efficient implementation of this?
for (auto &key: keys_) {
for (auto &kv : keys) {
if (key.Name() == kv.name) {
// TODO: check if the key is present in the body and compare the data
body[key.OriginalName()] = kv.value;
break;
}
}
}
}

std::vector<Response> ListResource::BodyValidateMultiple(
const std::string &cube_name, const ListKeyValues &keys,
nlohmann::json &body, bool initialization) const {
std::vector<Response> errors;

nlohmann::json jbody;
if (body.empty()) {
jbody = nlohmann::json::parse("[]");
} else {
jbody = body;
}

if (!body.is_array()) {
errors.push_back({ErrorTag::kInvalidValue, nullptr});
return errors;
}

for (auto &elem : jbody) {
SetDefaultIfMissing(elem, initialization);
auto body = BodyValidate(cube_name, keys, elem, initialization);
errors.reserve(errors.size() + body.size());
std::copy(std::begin(body), std::end(body), std::back_inserter(errors));
}

return errors;
}

bool ListResource::IsMandatory() const {
return false;
}
Expand Down
10 changes: 10 additions & 0 deletions src/polycubed/src/server/Resources/Body/ListResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ class ListResource : public virtual ParentResource {
void SetDefaultIfMissing(nlohmann::json &body,
bool initialization) const final;

/*
* This function takes the keys (parsed from the url in "keys") and save
* them in the body of the request
*/
void FillKeys(nlohmann::json &body, const ListKeyValues &keys);

std::vector<Response> BodyValidateMultiple(
const std::string &cube_name, const ListKeyValues &keys,
nlohmann::json &body, bool initialization) const;

const std::vector<ListKey> keys_;

nlohmann::json ToHelpJson() const override;
Expand Down
18 changes: 11 additions & 7 deletions src/polycubed/src/server/Resources/Body/ParentResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ std::vector<Response> ParentResource::BodyValidate(const std::string &cube_name,
for (auto &child : children_) {
const auto &child_name = child->Name();

// TODO: what are the implications of it?
if (child->IsKey()) {
continue;
}

// Choices are a special case
if (std::dynamic_pointer_cast<ChoiceResource>(child) != nullptr) {
choices.push_back(child);
Expand All @@ -86,9 +81,18 @@ std::vector<Response> ParentResource::BodyValidate(const std::string &cube_name,
errors.push_back(
{ErrorTag::kInvalidValue, ::strdup(child_name.data())});
} else {
auto child_errors = child->BodyValidate(
cube_name, keys, body_copy[child_name], initialization);
std::vector<Response> child_errors;
// if the child is a list, perform the check over all elements
if (auto list = std::dynamic_pointer_cast<ListResource>(child)) {
child_errors = list->BodyValidateMultiple(
cube_name, keys, body_copy[child_name], initialization);
} else {
child_errors = child->BodyValidate(
cube_name, keys, body_copy[child_name], initialization);
}

errors.reserve(errors.size() + child_errors.size());

// remove current parsed element from the body so that in the
// eventuality of choices, they will operate only on unparsed
// elements. moreover, this is required for detecting unparsed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ void ParentResource::CreateReplaceUpdate(
const auto &cube_name = Service::Cube(request);
ListKeyValues keys{};
Keys(request, keys);

// fill keys if they are missing
if (auto list = dynamic_cast<ListResource*>(this)) {
list->FillKeys(jbody, keys);
}

auto body_errors = BodyValidate(cube_name, keys, jbody, initialization);
errors.reserve(errors.size() + body_errors.size());
std::move(std::begin(body_errors), std::end(body_errors),
Expand Down
3 changes: 2 additions & 1 deletion src/services/pcn-k8sfilter/datamodel/k8sfilter.yang
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ module k8sfilter {
enum INTERNAL { description "Connected to LBRP"; }
}
mandatory true;
config false;
config true;
polycube-base:init-only-config;
description "...";
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/services/pcn-k8switch/src/K8switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ K8switch::K8switch(const std::string name, const K8switchJsonObject &conf)
doSetVirtualClientSubnet(conf.getVirtualClientSubnet());

// reload code a single time
reloadConfig();
add_program(getFlags() + k8switch_code, 0);

addServiceList(conf.getService());
addPortsList(conf.getPorts());
Expand Down Expand Up @@ -170,7 +170,7 @@ void K8switch::doSetVirtualClientSubnet(const std::string &value) {
virtual_client_cidr_ = value;
}

void K8switch::reloadConfig() {
std::string K8switch::getFlags() {
std::string flags;

// ports
Expand All @@ -197,6 +197,13 @@ void K8switch::reloadConfig() {
"#define CLIENT_SUBNET " + std::to_string(htonl(client_subnet_)) + "\n";
flags += "#define VIRTUAL_CLIENT_SUBNET " +
std::to_string(htonl(virtual_client_subnet_)) + "\n";
logger()->debug("flags is {}", flags);

return flags;
}

void K8switch::reloadConfig() {
std::string flags(getFlags());

logger()->debug("Reloading code with flags port: {}", flags);

Expand Down
1 change: 1 addition & 0 deletions src/services/pcn-k8switch/src/K8switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class K8switch : public polycube::service::Cube<Ports>,
void cleanupSessionTable();
uint32_t timestampToAge(const uint64_t timestamp);
void tick();
std::string getFlags();

std::unique_ptr<std::thread> tick_thread;
bool stop_;
Expand Down