From 2ac9cd8ac14fadad75108ab3268a2be30d571ea3 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Wed, 8 Sep 2021 10:05:28 -0700 Subject: [PATCH] Adding tree import. --- src/APConfig.cpp | 86 ++++++++++++++++++++++++++++++- src/APConfig.h | 5 ++ src/RESTAPI_inventory_handler.cpp | 26 ++++++++-- test_scripts/curl/cli | 21 +++++--- 4 files changed, 127 insertions(+), 11 deletions(-) diff --git a/src/APConfig.cpp b/src/APConfig.cpp index ed25ab7..6cd093c 100644 --- a/src/APConfig.cpp +++ b/src/APConfig.cpp @@ -4,6 +4,7 @@ #include "APConfig.h" #include "StorageService.h" +#include "APConfig.h" namespace OpenWifi { @@ -13,6 +14,83 @@ namespace OpenWifi { Logger_(L) {} + bool APConfig::FindRadio(const std::string &Band, const Poco::JSON::Array::Ptr &Arr, Poco::JSON::Object::Ptr & Radio) { + for(const auto &i:*Arr) { + auto R = i.extract(); + if(R->has("band") && R->get("band").toString()==Band) { + Radio = R; + return true; + } + } + return false; + } + + bool APConfig::mergeArray(const std::string &K, const Poco::JSON::Array::Ptr &A , const Poco::JSON::Array::Ptr &B, Poco::JSON::Array &Arr) { + if(K=="radios") { + int index=0; + for(const auto &i:*A) { + auto A_Radio = A->get(index).extract(); + if(A_Radio->has("band")) { + std::string Band = A_Radio->get("band").toString(); + Poco::JSON::Object::Ptr B_Radio; + if(FindRadio(Band,B,B_Radio)) { + Poco::JSON::Object RR; + merge(A_Radio,B_Radio,RR); + Arr.set(index, RR); + } else { + Arr.set(index,A); + } + } + ++index; + } + } else { + Arr = *A; + } + return true; + } + + bool APConfig::merge(const Poco::JSON::Object::Ptr & A, const Poco::JSON::Object::Ptr & B, Poco::JSON::Object &C) { + + for(const auto &i:*A) { + const std::string & K = i.first; + // std::cout << "KEY: " << K << std::endl; + if(B->has(K)) { + if(A->isArray(K)) { + // std::cout << "ISARRAY" << std::endl; + if(B->isArray(K)) { + Poco::JSON::Array Arr; + auto AR1=A->getArray(K); + auto AR2=B->getArray(K); + mergeArray(K,AR1,AR2,Arr); + C.set(K,Arr); + } else { + C.set(K,A->getArray(K)); + } + } + else if(A->isObject(K) && B->isObject(K)) { + // std::cout << "ISOBJECT" << std::endl; + Poco::JSON::Object R; + merge(A->getObject(K),B->getObject(K),R); + C.set(K,R); + } + else { + C.set(K,i.second); + } + } else { + C.set(K,i.second); + } + } + + for(const auto &i:*B) { + const std::string & K = i.first; + if(!A->has(K)) + C.set(K,i.second); + } + + return true; + } + + bool APConfig::Get(std::string &Config) { if(Config_.empty()) { @@ -41,7 +119,8 @@ namespace OpenWifi { // metrics // radios // services - // + // globals + // unit Poco::JSON::Object CFG; for(const auto &i:Config_) { @@ -54,6 +133,11 @@ namespace OpenWifi { } } + std::stringstream O; + Poco::JSON::Stringifier::stringify(CFG,O); + + Config = O.str(); + if(Config_.empty()) return false; diff --git a/src/APConfig.h b/src/APConfig.h index feb847d..150e28b 100644 --- a/src/APConfig.h +++ b/src/APConfig.h @@ -31,6 +31,11 @@ namespace OpenWifi { std::string CompleteConfig_; ConfigVec Config_; Types::StringPairVec Errors; + + bool FindRadio(const std::string &Band, const Poco::JSON::Array::Ptr &Arr, Poco::JSON::Object::Ptr & Radio); + bool mergeArray(const std::string &K, const Poco::JSON::Array::Ptr &A , const Poco::JSON::Array::Ptr &B, Poco::JSON::Array &Arr); + bool merge(const Poco::JSON::Object::Ptr & A, const Poco::JSON::Object::Ptr & B, Poco::JSON::Object &C); + }; } diff --git a/src/RESTAPI_inventory_handler.cpp b/src/RESTAPI_inventory_handler.cpp index 98a035b..fad3751 100644 --- a/src/RESTAPI_inventory_handler.cpp +++ b/src/RESTAPI_inventory_handler.cpp @@ -13,6 +13,7 @@ #include "Poco/JSON/Parser.h" #include "Daemon.h" #include "RESTAPI_utils.h" +#include "APConfig.h" namespace OpenWifi{ void RESTAPI_inventory_handler::handleRequest(Poco::Net::HTTPServerRequest &Request, @@ -47,10 +48,27 @@ namespace OpenWifi{ ProvObjects::InventoryTag IT; if(Storage()->InventoryDB().GetRecord(RESTAPI::Protocol::SERIALNUMBER,SerialNumber,IT)) { - Poco::JSON::Object Answer; - IT.to_json(Answer); - ReturnObject(Request, Answer, Response); - return; + std::string Arg; + if(HasParameter("config",Arg) && Arg=="true") { + APConfig Device(SerialNumber,IT.deviceType,Logger_); + + Poco::JSON::Object Answer; + std::string C; + if(Device.Get(C)) { + Answer.set("config", C); + ReturnObject(Request, Answer, Response); + } else { + Answer.set("config","none"); + } + + ReturnObject(Request, Answer, Response); + return; + } else { + Poco::JSON::Object Answer; + IT.to_json(Answer); + ReturnObject(Request, Answer, Response); + return; + } } else { NotFound(Request,Response); return; diff --git a/test_scripts/curl/cli b/test_scripts/curl/cli index 588b6e8..0306ac6 100755 --- a/test_scripts/curl/cli +++ b/test_scripts/curl/cli @@ -131,7 +131,7 @@ setroot() { addentity() { payload="{ \"name\" : \"$1\", \"description\" : \"sub entity.\", \"parent\" : \"0000-0000-0000\", \"notes\" : [ {\"note\" : \"CLI added\"} ] }"; - curl ${FLAGS} -X POST "https://${OWPROV}/api/v1/inventory/1" \ + curl ${FLAGS} -X POST "https://${OWPROV}/api/v1/entity/1" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "accept: application/json" \ @@ -139,6 +139,14 @@ addentity() { jq < ${result_file} } +getentity() { + curl ${FLAGS} "https://${OWPROV}/api/v1/entity/$1" \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer ${token}" \ + -H "accept: application/json" > ${result_file} + jq < ${result_file} +} + getentities() { curl ${FLAGS} "https://${OWPROV}/api/v1/entity" \ -H "Content-Type: application/json" \ @@ -319,20 +327,21 @@ case "$1" in "getroot") login; getroot; logout;; "setroot") login; setroot; logout;; "addentity") login; addentity "$2" ; logout;; + "getentity") login; getentity "$2" ; logout;; + "getentities") login; getentities ; logout;; + "entitycount") login; entitycount ; logout;; + "tree") login; tree ; logout;; + "importtree") login; importtree $2; logout;; "addtag") login; addtag $2 $3; logout;; "unassigntag") login; unassigntag $2; logout;; "addunassignedtag") login; addunassignedtag $2 ; logout;; - "getentities") login; getentities ; logout;; - "tree") login; tree ; logout;; + "getunassignedtags") login; getunassignedtags ; logout;; "gettags") login; gettags ; logout;; "gettag") login; gettag $2 ; logout;; "tagcount") login; tagcount ; logout;; - "entitycount") login; entitycount ; logout;; "venuecount") login; venuecount ; logout;; "contactcount") login; contactcount ; logout;; "locationcount") login; locationcount ; logout;; - "getunassignedtags") login; getunassignedtags ; logout;; - "importtree") login; importtree $2; logout;; "addconfig") login; addconfig "$2"; logout;; "listconfigs") login; listconfigs ; logout;; "getconfig") login; getconfig "$2"; logout;;