Add automatic configuration creation.

This commit is contained in:
stephb9959
2022-03-10 15:12:24 -08:00
parent 20070a060f
commit 7bc4766c35
5 changed files with 78 additions and 48 deletions

View File

@@ -75,47 +75,6 @@ namespace OpenWifi{
return OK();
}
bool RESTAPI_configurations_handler::ValidateConfigBlock(const ProvObjects::DeviceConfiguration &Config, std::string & Error) {
static const std::vector<std::string> 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<Poco::JSON::Object::Ptr>();
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() {

View File

@@ -30,7 +30,5 @@ namespace OpenWifi {
void DoPost();
void DoPut();
void DoDelete();
bool ValidateConfigBlock(const ProvObjects::DeviceConfiguration &Config, std::string & Error);
};
}

View File

@@ -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<std::string> 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<Poco::JSON::Object::Ptr>();
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 <typename Type> std::map<std::string,std::string> CreateObjects(Type & NewObject, RESTAPIHandler & R, std::string &ErrorText) {
std::map<std::string,std::string> Result;
@@ -396,16 +438,17 @@ namespace OpenWifi {
if constexpr(std::is_same_v<Type,ProvObjects::Venue>) {
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<Poco::JSON::Object::Ptr>();
@@ -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<Poco::JSON::Object::Ptr>();
ProvObjects::DeviceConfiguration DC;
if(DC.from_json(ConfigurationDetails)) {
if constexpr(std::is_same_v<Type, ProvObjects::InventoryTag>) {
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;
}
}
}
}

View File

@@ -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<Poco::JSON::Object>();
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);

View File

@@ -2609,6 +2609,7 @@ namespace OpenWifi {
return false;
} catch(...) {
std::cout << "4 Some kind of bullshit exception..." << std::endl;
return false;
}
}
return true;