diff --git a/src/RESTAPI/RESTAPI_configurations_handler.cpp b/src/RESTAPI/RESTAPI_configurations_handler.cpp index 532450a..c9f73d4 100644 --- a/src/RESTAPI/RESTAPI_configurations_handler.cpp +++ b/src/RESTAPI/RESTAPI_configurations_handler.cpp @@ -75,47 +75,6 @@ namespace OpenWifi{ return OK(); } - bool RESTAPI_configurations_handler::ValidateConfigBlock(const ProvObjects::DeviceConfiguration &Config, std::string & Error) { - static const std::vector SectionNames{ "globals", "interfaces", "metrics", "radios", "services", "unit" }; - - for(const auto &i:Config.configuration) { - Poco::JSON::Parser P; - if(i.name.empty()) { - std::cout << "Name is empty" << std::endl; - BadRequest(RESTAPI::Errors::NameMustBeSet); - return false; - } - - try { - 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(RESTAPI::Errors::ConfigBlockInvalid); - return false; - } - } - } catch (const Poco::JSON::JSONException &E ) { - Error = "Block: " + i.name + " failed parsing: " + E.message(); - return false; - } - - try { - if (ValidateUCentralConfiguration(i.configuration, Error)) { - // std::cout << "Block: " << i.name << " is valid" << std::endl; - } else { - Error = "Block: " + i.name + " Rejected config:" + i.configuration ; - return false; - } - } catch(...) { - std::cout << "Exception in validation" << std::endl; - return false; - } - - } - return true; - } - #define __DBG__ std::cout << __LINE__ << std::endl; void RESTAPI_configurations_handler::DoPost() { diff --git a/src/RESTAPI/RESTAPI_configurations_handler.h b/src/RESTAPI/RESTAPI_configurations_handler.h index 6814eb7..0a0c209 100644 --- a/src/RESTAPI/RESTAPI_configurations_handler.h +++ b/src/RESTAPI/RESTAPI_configurations_handler.h @@ -30,7 +30,5 @@ namespace OpenWifi { void DoPost(); void DoPut(); void DoDelete(); - - bool ValidateConfigBlock(const ProvObjects::DeviceConfiguration &Config, std::string & Error); }; } diff --git a/src/RESTAPI/RESTAPI_db_helpers.h b/src/RESTAPI/RESTAPI_db_helpers.h index 4edb984..506a9a8 100644 --- a/src/RESTAPI/RESTAPI_db_helpers.h +++ b/src/RESTAPI/RESTAPI_db_helpers.h @@ -7,6 +7,7 @@ #include "RESTObjects/RESTAPI_ProvObjects.h" #include "StorageService.h" #include "framework/MicroService.h" +#include "framework/ConfigurationValidator.h" namespace OpenWifi { @@ -377,6 +378,47 @@ namespace OpenWifi { return EntityDB::RootUUID(); } + inline bool ValidateConfigBlock(const ProvObjects::DeviceConfiguration &Config, std::string & Error) { + static const std::vector SectionNames{ "globals", "interfaces", "metrics", "radios", "services", "unit" }; + + for(const auto &i:Config.configuration) { + Poco::JSON::Parser P; + if(i.name.empty()) { + std::cout << "Name is empty" << std::endl; + Error = RESTAPI::Errors::NameMustBeSet; + return false; + } + + try { + 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()) { + Error = "Unknown section: " + j; + return false; + } + } + } catch (const Poco::JSON::JSONException &E ) { + Error = "Block: " + i.name + " failed parsing: " + E.message(); + return false; + } + + try { + if (ValidateUCentralConfiguration(i.configuration, Error)) { + // std::cout << "Block: " << i.name << " is valid" << std::endl; + } else { + Error = "Block: " + i.name + " Rejected config:" + i.configuration ; + return false; + } + } catch(...) { + std::cout << "Exception in validation" << std::endl; + return false; + } + + } + return true; + } + template std::map CreateObjects(Type & NewObject, RESTAPIHandler & R, std::string &ErrorText) { std::map Result; @@ -396,16 +438,17 @@ namespace OpenWifi { if constexpr(std::is_same_v) { std::cout << "Location decoded: " << LC.info.name << std::endl; std::string ParentEntity = FindParentEntity(NewObject); - ProvObjects::CreateObjectInfo(R.UserInfo_.userinfo,LC.info); + ProvObjects::CreateObjectInfo(R.UserInfo_.userinfo, LC.info); LC.entity = ParentEntity; - if(StorageService()->LocationDB().CreateRecord(LC)) { + if (StorageService()->LocationDB().CreateRecord(LC)) { NewObject.location = LC.info.id; AddMembership(StorageService()->EntityDB(), &ProvObjects::Entity::locations, ParentEntity, LC.info.id); } } } else { - std::cout << "Location not decoded." << std::endl; + ErrorText = RESTAPI::Errors::InvalidJSONDocument; + break; } } else if (Object->has("contact")) { auto ContactDetails = Object->get("contact").extract(); @@ -415,7 +458,24 @@ namespace OpenWifi { } else { std::cout << "contact not decoded." << std::endl; } - std::cout << "No contact included." << std::endl; + } else if (Object->has("configuration")) { + auto ConfigurationDetails = Object->get("configuration").template extract(); + ProvObjects::DeviceConfiguration DC; + if(DC.from_json(ConfigurationDetails)) { + if constexpr(std::is_same_v) { + if(!ValidateConfigBlock(DC,ErrorText)) { + break; + } + std::cout << "Configuration decoded: " << DC.info.name << std::endl; + ProvObjects::CreateObjectInfo(R.UserInfo_.userinfo, DC.info); + if (StorageService()->ConfigurationDB().CreateRecord(DC)) { + NewObject.deviceConfiguration = DC.info.id; + } + } + } else { + ErrorText = RESTAPI::Errors::InvalidJSONDocument; + break; + } } } } diff --git a/src/RESTAPI/RESTAPI_inventory_handler.cpp b/src/RESTAPI/RESTAPI_inventory_handler.cpp index 537bd85..8388be3 100644 --- a/src/RESTAPI/RESTAPI_inventory_handler.cpp +++ b/src/RESTAPI/RESTAPI_inventory_handler.cpp @@ -98,7 +98,7 @@ namespace OpenWifi{ std::ostringstream OS; Configuration->stringify(OS); Results.appliedConfiguration = OS.str(); - Poco::JSON::Object::Ptr Response; + auto Response=Poco::makeShared(); Logger().debug(Poco::format("%s: Sending configuration push.",Existing.serialNumber)); if (SDK::GW::Device::Configure(this, SerialNumber, Configuration, Response)) { Logger().debug(Poco::format("%s: Sending configuration pushed.",Existing.serialNumber)); @@ -259,6 +259,12 @@ namespace OpenWifi{ } */ + std::string ErrorText; + auto ObjectsCreated = CreateObjects(NewObject,*this,ErrorText); + if(!ErrorText.empty()) { + return BadRequest(ErrorText); + } + __DBG__ if(DB_.CreateRecord(NewObject)) { __DBG__ @@ -414,6 +420,12 @@ namespace OpenWifi{ Existing.state = NewObject.state; } + std::string ErrorText; + auto ObjectsCreated = CreateObjects(NewObject,*this,ErrorText); + if(!ErrorText.empty()) { + return BadRequest(ErrorText); + } + if(StorageService()->InventoryDB().UpdateRecord("id", Existing.info.id, Existing)) { MoveUsage(StorageService()->PolicyDB(),DB_,FromPolicy,ToPolicy,Existing.info.id); MoveUsage(StorageService()->LocationDB(),DB_,FromLocation,ToLocation,Existing.info.id); diff --git a/src/framework/ConfigurationValidator.cpp b/src/framework/ConfigurationValidator.cpp index d81b8e0..1d77137 100644 --- a/src/framework/ConfigurationValidator.cpp +++ b/src/framework/ConfigurationValidator.cpp @@ -2609,6 +2609,7 @@ namespace OpenWifi { return false; } catch(...) { std::cout << "4 Some kind of bullshit exception..." << std::endl; + return false; } } return true;