From 926a1de19705b69cd7352ab5c9bdceab0c28df53 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Thu, 9 Sep 2021 12:08:39 -0700 Subject: [PATCH] Adding configuration block management. --- build | 2 +- src/RESTAPI_configurations_handler.cpp | 69 ++++++++------ src/RESTAPI_configurations_handler.h | 5 + test_scripts/curl/import_tree.json | 122 ++++++++++++++----------- 4 files changed, 115 insertions(+), 83 deletions(-) diff --git a/build b/build index b9c6c00..af21323 100644 --- a/build +++ b/build @@ -1 +1 @@ -131 \ No newline at end of file +133 \ No newline at end of file diff --git a/src/RESTAPI_configurations_handler.cpp b/src/RESTAPI_configurations_handler.cpp index 405e6fe..baf3164 100644 --- a/src/RESTAPI_configurations_handler.cpp +++ b/src/RESTAPI_configurations_handler.cpp @@ -89,6 +89,42 @@ namespace OpenWifi{ BadRequest(Request, Response, "Internal error. Consult documentation and try again."); } + // interfaces + // metrics + // radios + // services + // globals + // unit + bool RESTAPI_configurations_handler::ValidateConfigBlock(const ProvObjects::DeviceConfiguration &Config, + Poco::Net::HTTPServerRequest &Request, + Poco::Net::HTTPServerResponse &Response) { + static const std::vector SectionNames{ "globals", "interfaces", "metrics", "radios", "services", "unit" }; + + try { + for(const auto &i:Config.configuration) { + Poco::JSON::Parser P; + + if(i.name.empty()) { + BadRequest(Request, Response, "The configuration block name must be included."); + return false; + } + auto Blocks = P.parse(i.configuration).extract(); + auto N = Blocks->getNames(); + + for(const auto &j:N) { + if(std::find(SectionNames.cbegin(),SectionNames.cend(),j)==SectionNames.cend()) { + BadRequest(Request, Response, "Configuration block type invalid."); + return false; + } + } + } + } catch (const Poco::Exception &E) { + BadRequest(Request, Response, "Invalid configuration portion."); + return false; + } + return true; + } + void RESTAPI_configurations_handler::DoPost(Poco::Net::HTTPServerRequest &Request, Poco::Net::HTTPServerResponse &Response) { try { @@ -127,20 +163,8 @@ namespace OpenWifi{ return; } - try { - for(const auto &i:C.configuration) { - Poco::JSON::Parser P; - // std::cout << "Config:>>>" << std::endl << i.configuration << std::endl << "<<<" << std::endl; - if(i.name.empty()) { - BadRequest(Request, Response, "The configuration block name must be included."); - return; - } - P.parse(i.configuration).extract(); - } - } catch (const Poco::Exception &E) { - BadRequest(Request, Response, "Invalid configuration portion."); + if(!ValidateConfigBlock(C, Request, Response)) return; - } if(Storage()->ConfigurationDB().CreateRecord(C)) { Storage()->ConfigurationDB().GetRecord("id", C.info.id, C); @@ -208,23 +232,8 @@ namespace OpenWifi{ NewConfig.info.modified = std::time(nullptr); - if(!NewConfig.configuration.empty()) { - try { - for(const auto &i:NewConfig.configuration) { - if(i.name.empty()) { - BadRequest(Request, Response, "The configuration block name must be included."); - return; - } - Poco::JSON::Parser P; - auto T = P.parse(i.configuration).extract(); - } - } catch (const Poco::Exception &E) { - BadRequest(Request, Response, "Invalid configuration portion."); - return; - } - - Existing.configuration = NewConfig.configuration; - } + if(!ValidateConfigBlock(NewConfig, Request, Response)) + return; if(!NewConfig.variables.empty()) Existing.variables = NewConfig.variables; diff --git a/src/RESTAPI_configurations_handler.h b/src/RESTAPI_configurations_handler.h index 5cd00c5..b8c835f 100644 --- a/src/RESTAPI_configurations_handler.h +++ b/src/RESTAPI_configurations_handler.h @@ -11,6 +11,7 @@ #include "RESTAPI_handler.h" #include "Poco/Net/HTTPServerRequest.h" #include "Poco/Net/HTTPServerResponse.h" +#include "RESTAPI_ProvObjects.h" namespace OpenWifi { class RESTAPI_configurations_handler : public RESTAPIHandler { @@ -34,6 +35,10 @@ namespace OpenWifi { Poco::Net::HTTPServerResponse &Response); void DoDelete(Poco::Net::HTTPServerRequest &Request, Poco::Net::HTTPServerResponse &Response); + private: + bool ValidateConfigBlock(const ProvObjects::DeviceConfiguration &Config, + Poco::Net::HTTPServerRequest &Request, + Poco::Net::HTTPServerResponse &Response); }; } diff --git a/test_scripts/curl/import_tree.json b/test_scripts/curl/import_tree.json index 37978a6..43f8eaa 100644 --- a/test_scripts/curl/import_tree.json +++ b/test_scripts/curl/import_tree.json @@ -1,53 +1,71 @@ { - "type" : "entity", - "name" : "world", - "children" : [{ - "type" : "entity", - "name" : "root" , - "children" : [ { - "type" : "entity" , - "name" : "sub-root1" , - "children" : [{ - "type" : "entity" , - "name" : "sub-1.1" , - "children" : [] , - "venues" : [] - }, - { - "type" : "entity" , - "name" : "sub-1.2" , - "children" : [] , - "venues" : [] - } - ] , - "venues" : [{ - "type" : "venue" , - "name" : "sub-r1-v1" , - "venues" : [] - }, - { - "type" : "venue" , - "name" : "sub-r1-v2" , - "venues" : [] - }] - }, - { - "type" : "entity" , - "name" : "sub-root2" , - "children" : [] , - "venues" : [] - } - ] , - "venues" : [ { - "type" : "venue" , - "name" : "sub-v1" , - "venues" : [] - }, - { - "type" : "venue" , - "name" : "sub-v2" , - "venues" : [] - }] - }], - "venues" : [] - } \ No newline at end of file + "type": "entity", + "name": "world", + "children": [{ + "type": "entity", + "name": "Canada", + "children": [{ + "type": "entity", + "name": "Provinces", + "children": [{ + "type": "entity", + "name": "Quebec", + "children": [], + "venues": [{ + "type": "venue", + "name": "Bell Center", + "venues": [] + }, + { + "type": "venue", + "name": "Olymic Statium", + "venues": [] + } + ] + }, + { + "type": "entity", + "name": "Ontario", + "children": [], + "venues": [{ + "type": "venue", + "name": "CN Tower", + "venues": [] + }, + { + "type": "venue", + "name": "Rogers Center", + "venues": [] + } + ] + }, + { + "type": "entity", + "name": "BC", + "children": [], + "venues": [{ + "type": "venue", + "name": "BC Place", + "venues": [] + }, + { + "type": "venue", + "name": "Stanley Park", + "venues": [] + } + ] + } + ], + "venues": [] + }], + "venues": [] + }, + { + "type": "entity", + "name": "Mexico", + "children": [], + "venues": [] + } + ], + "venues": [] +} \ No newline at end of file