Skip to content

Commit 07e5aa5

Browse files
mauriciovasquezbernalfrisso
authored andcommitted
polycubed: set missing default elements only at initialization time (#116)
polycubed fills the default elements on a request if they are missing, this guarantees that the default elements are always set when the request arrives to the service implementation, hence the developer has not to take care of it. When an update (PATCH) request is done those fields should not be set, because their value will be modified even if the client wanted to update other elements. Fixes: d8d280c ("Added new REST API and validation code") Reported-by: Elis Lulja <elis.lulja@gmail.com> Signed-off-by: Mauricio Vasquez B <mauriciovasquezbernal@gmail.com>
1 parent 37a0800 commit 07e5aa5

15 files changed

Lines changed: 31 additions & 37 deletions

src/polycubed/src/server/Resources/Body/ChoiceResource.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,13 @@ bool ChoiceResource::IsMandatory() const {
8888
dynamic_cast<const CaseResource *const>(parent_) != nullptr;
8989
}
9090

91-
void ChoiceResource::SetDefaultIfMissing(nlohmann::json &body,
92-
bool initialization) const {
91+
void ChoiceResource::SetDefaultIfMissing(nlohmann::json &body) const {
9392
if (default_case_ != nullptr) {
9493
auto def = std::find_if(
9594
std::begin(children_), std::end(children_),
9695
[this](const auto &child) { return child->Name() == *default_case_; });
9796
// no check if def is end because it ensured by YANG validation process
98-
(*def)->SetDefaultIfMissing(body, initialization);
97+
(*def)->SetDefaultIfMissing(body);
9998
}
10099
}
101100
} // namespace polycube::polycubed::Rest::Resources::Body

src/polycubed/src/server/Resources/Body/ChoiceResource.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class ChoiceResource : public virtual ParentResource {
4040

4141
bool IsMandatory() const final;
4242

43-
void SetDefaultIfMissing(nlohmann::json &body,
44-
bool initialization) const final;
43+
void SetDefaultIfMissing(nlohmann::json &body) const final;
4544

4645
private:
4746
const bool mandatory_;

src/polycubed/src/server/Resources/Body/LeafListResource.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ std::vector<Response> LeafListResource::BodyValidate(
6565
return errors;
6666
}
6767

68-
void LeafListResource::SetDefaultIfMissing(nlohmann::json &body,
69-
bool initialization) const {
68+
void LeafListResource::SetDefaultIfMissing(nlohmann::json &body) const {
7069
if (body.empty() && !default_.empty()) {
7170
for (const auto &element : default_) {
7271
body.push_back(element);

src/polycubed/src/server/Resources/Body/LeafListResource.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class LeafListResource : public virtual LeafResource {
4040
nlohmann::json &body,
4141
bool initialization) const final;
4242

43-
void SetDefaultIfMissing(nlohmann::json &body,
44-
bool initialization) const final;
43+
void SetDefaultIfMissing(nlohmann::json &body) const final;
4544

4645
private:
4746
const std::vector<std::string> default_;

src/polycubed/src/server/Resources/Body/LeafResource.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ std::vector<Response> LeafResource::BodyValidate(const std::string &cube_name,
6161
return errors;
6262
}
6363

64-
void LeafResource::SetDefaultIfMissing(nlohmann::json &body,
65-
bool initialization) const {
64+
void LeafResource::SetDefaultIfMissing(nlohmann::json &body) const {
6665
if (body.empty() && default_ != nullptr) {
6766
switch (type_) {
6867
case Types::Scalar::Integer:

src/polycubed/src/server/Resources/Body/LeafResource.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ class LeafResource : public Resource {
5454
return configuration_;
5555
}
5656

57-
void SetDefaultIfMissing(nlohmann::json &body,
58-
bool initialization) const override;
57+
void SetDefaultIfMissing(nlohmann::json &body) const override;
5958

6059
nlohmann::json ToHelpJson() const override;
6160

src/polycubed/src/server/Resources/Body/ListResource.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ const Response ListResource::ReadWhole(const std::string &cube_name,
8585
return ParentResource::ReadValue(cube_name, keys);
8686
}
8787

88-
void ListResource::SetDefaultIfMissing(nlohmann::json &body,
89-
bool initialization) const {
88+
void ListResource::SetDefaultIfMissing(nlohmann::json &body) const {
9089
// keys default values must be ignored (RFC7950#7.8.2)
9190
std::set<std::string> key_names;
9291
for (const auto &key : keys_) {
@@ -98,8 +97,8 @@ void ListResource::SetDefaultIfMissing(nlohmann::json &body,
9897
// all non-keys can be defaulted, otherwise only the ones that are not
9998
// marked as init-only-config.
10099
if (key_names.count(child->Name()) == 0 && child->IsConfiguration() &&
101-
(initialization || !child->IsInitOnlyConfig())) {
102-
child->SetDefaultIfMissing(body[child->Name()], initialization);
100+
!child->IsInitOnlyConfig()) {
101+
child->SetDefaultIfMissing(body[child->Name()]);
103102
if (body[child->Name()].empty()) {
104103
body.erase(child->Name());
105104
}
@@ -168,7 +167,9 @@ std::vector<Response> ListResource::BodyValidateMultiple(
168167
}
169168

170169
for (auto &elem : jbody) {
171-
SetDefaultIfMissing(elem, initialization);
170+
if (initialization) {
171+
SetDefaultIfMissing(elem);
172+
}
172173
auto body = BodyValidate(cube_name, keys, elem, initialization);
173174
errors.reserve(errors.size() + body.size());
174175
std::copy(std::begin(body), std::end(body), std::back_inserter(errors));

src/polycubed/src/server/Resources/Body/ListResource.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ class ListResource : public virtual ParentResource {
4545

4646
bool IsMandatory() const override;
4747

48-
void SetDefaultIfMissing(nlohmann::json &body,
49-
bool initialization) const final;
48+
void SetDefaultIfMissing(nlohmann::json &body) const final;
5049

5150
/*
5251
* This function takes the keys (parsed from the url in "keys") and save

src/polycubed/src/server/Resources/Body/ParentResource.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ bool ParentResource::IsConfiguration() const {
155155
});
156156
}
157157

158-
void ParentResource::SetDefaultIfMissing(nlohmann::json &body,
159-
bool initialization) const {
158+
void ParentResource::SetDefaultIfMissing(nlohmann::json &body) const {
160159
for (const auto &child : children_) {
161160
auto &child_body = body[child->Name()];
162161
// Lists can't be defaulted. The only possible this is if a list
@@ -165,16 +164,13 @@ void ParentResource::SetDefaultIfMissing(nlohmann::json &body,
165164
if (std::dynamic_pointer_cast<ListResource>(child) != nullptr) {
166165
if (!child_body.empty()) {
167166
for (auto &entry : child_body) {
168-
child->SetDefaultIfMissing(entry, initialization);
167+
child->SetDefaultIfMissing(entry);
169168
}
170169
}
171170
} else {
172-
// Set default only for configuration nodes. During initialization
173-
// all can be defaulted, otherwise only the ones that are not marked
174-
// as init-only-config
175-
if (child->IsConfiguration() &&
176-
(initialization || !child->IsInitOnlyConfig())) {
177-
child->SetDefaultIfMissing(child_body, initialization);
171+
// Set default only for configuration nodes.
172+
if (child->IsConfiguration()) {
173+
child->SetDefaultIfMissing(child_body);
178174
}
179175
}
180176
if (child_body.empty()) {

src/polycubed/src/server/Resources/Body/ParentResource.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ class ParentResource : public Resource {
4747

4848
bool IsConfiguration() const final;
4949

50-
void SetDefaultIfMissing(nlohmann::json &body,
51-
bool initialization) const override;
50+
void SetDefaultIfMissing(nlohmann::json &body) const override;
5251

5352
std::shared_ptr<Resource> Child(const std::string &child_name) const;
5453

0 commit comments

Comments
 (0)