From ef63dcd5b9b998e00d37a9faafc196f64786201e Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Fri, 1 Jul 2022 09:41:31 -0700 Subject: [PATCH] https://telecominfraproject.atlassian.net/browse/WIFI-10040 Signed-off-by: stephb9959 --- build | 2 +- openapi/owfms.yaml | 55 ++-------------------- src/AutoUpdater.cpp | 1 + src/ManifestCreator.cpp | 1 + src/NewCommandHandler.cpp | 1 + src/NewConnectionHandler.cpp | 1 + src/framework/MicroService.h | 89 ++++++++++++++++++++++++++++++------ src/framework/orm.h | 31 +++++++++++++ 8 files changed, 116 insertions(+), 65 deletions(-) diff --git a/build b/build index 7d37386..abc4eff 100644 --- a/build +++ b/build @@ -1 +1 @@ -45 \ No newline at end of file +46 \ No newline at end of file diff --git a/openapi/owfms.yaml b/openapi/owfms.yaml index 728f3b6..b523f2f 100644 --- a/openapi/owfms.yaml +++ b/openapi/owfms.yaml @@ -30,58 +30,13 @@ components: responses: NotFound: - description: The specified resource was not found. - content: - application/json: - schema: - properties: - ErrorCode: - type: integer - ErrorDetails: - type: string - ErrorDescription: - type: string - + $ref: 'https://github.com/Telecominfraproject/wlan-cloud-ucentralsec/blob/main/openpapi/owsec.yaml#/components/responses/NotFound' Unauthorized: - description: The requested does not have sufficient rights to perform the operation. - content: - application/json: - schema: - properties: - ErrorCode: - type: integer - enum: - - 0 # Success - - 1 # PASSWORD_CHANGE_REQUIRED, - - 2 # INVALID_CREDENTIALS, - - 3 # PASSWORD_ALREADY_USED, - - 4 # USERNAME_PENDING_VERIFICATION, - - 5 # PASSWORD_INVALID, - - 6 # INTERNAL_ERROR, - - 7 # ACCESS_DENIED, - - 8 # INVALID_TOKEN - - 9 # EXPIRED_TOKEN - - 10 # RATE_LIMIT_EXCEEDED - - 11 # BAD_MFA_TRANSACTION - - 12 # MFA_FAILURE - - 13 # SECURITY_SERVICE_UNREACHABLE - ErrorDetails: - type: string - ErrorDescription: - type: string - + $ref: 'https://github.com/Telecominfraproject/wlan-cloud-ucentralsec/blob/main/openpapi/owsec.yaml#/components/responses/Unauthorized' Success: - description: The requested operation was performed. - content: - application/json: - schema: - properties: - Operation: - type: string - Details: - type: string - Code: - type: integer + $ref: 'https://github.com/Telecominfraproject/wlan-cloud-ucentralsec/blob/main/openpapi/owsec.yaml#/components/responses/Success' + BadRequest: + $ref: 'https://github.com/Telecominfraproject/wlan-cloud-ucentralsec/blob/main/openpapi/owsec.yaml#/components/responses/BadRequest' schemas: FirmwareDetails: diff --git a/src/AutoUpdater.cpp b/src/AutoUpdater.cpp index 32dee79..30f76fd 100644 --- a/src/AutoUpdater.cpp +++ b/src/AutoUpdater.cpp @@ -38,6 +38,7 @@ namespace OpenWifi { } void AutoUpdater::onTimer([[maybe_unused]] Poco::Timer & timer) { + Utils::SetThreadName("auto-updater"); Running_ = true; std::unique_lock L(Mutex_); while(!Queue_.empty() && Running_) { diff --git a/src/ManifestCreator.cpp b/src/ManifestCreator.cpp index 1c35751..8a30228 100644 --- a/src/ManifestCreator.cpp +++ b/src/ManifestCreator.cpp @@ -17,6 +17,7 @@ namespace OpenWifi { void ManifestCreator::onTimer([[maybe_unused]] Poco::Timer &timer) { + Utils::SetThreadName("manifest"); Logger().information("Performing DB refresh"); S3BucketContent BucketList; StorageService()->FirmwaresDB().RemoveOldFirmware(); diff --git a/src/NewCommandHandler.cpp b/src/NewCommandHandler.cpp index e988e55..5d6ae55 100644 --- a/src/NewCommandHandler.cpp +++ b/src/NewCommandHandler.cpp @@ -9,6 +9,7 @@ namespace OpenWifi { void NewCommandHandler::run() { Running_ = true ; + Utils::SetThreadName("cmd-handler"); while(Running_) { Poco::Thread::trySleep(2000); diff --git a/src/NewConnectionHandler.cpp b/src/NewConnectionHandler.cpp index 29cfc1e..a5344ca 100644 --- a/src/NewConnectionHandler.cpp +++ b/src/NewConnectionHandler.cpp @@ -22,6 +22,7 @@ namespace OpenWifi { void NewConnectionHandler::run() { + Utils::SetThreadName("conn-handler"); Running_ = true ; while(Running_) { Poco::Thread::trySleep(2000); diff --git a/src/framework/MicroService.h b/src/framework/MicroService.h index 9169c10..85dcf5d 100644 --- a/src/framework/MicroService.h +++ b/src/framework/MicroService.h @@ -27,6 +27,11 @@ namespace OpenWifi { inline uint64_t Now() { return std::time(nullptr); }; } +namespace OpenWifi::Utils { + std::vector base64decode(const std::string& input); + std::string base64encode(const unsigned char *input, uint32_t size); +} + using namespace std::chrono_literals; #include "Poco/Util/Application.h" @@ -238,6 +243,11 @@ namespace OpenWifi::RESTAPI_utils { Obj.set(Field,Value); } + inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const Poco::Data::BLOB &Value) { + auto Result = Utils::base64encode((const unsigned char *)Value.rawContent(),Value.size()); + Obj.set(Field,Result); + } + inline void field_to_json(Poco::JSON::Object &Obj, const char *Field, const Types::StringPairVec & S) { Poco::JSON::Array Array; for(const auto &i:S) { @@ -334,12 +344,12 @@ namespace OpenWifi::RESTAPI_utils { inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, double & Value) { if(Obj->has(Field) && !Obj->isNull(Field)) - Value = (double) Obj->get(Field); + Value = (double)Obj->get(Field); } inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, float & Value) { if(Obj->has(Field) && !Obj->isNull(Field)) - Value = (float) Obj->get(Field); + Value = (float)Obj->get(Field); } inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, bool &Value) { @@ -374,7 +384,14 @@ namespace OpenWifi::RESTAPI_utils { inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, uint64_t &Value) { if(Obj->has(Field) && !Obj->isNull(Field)) - Value = (uint64_t ) Obj->get(Field); + Value = (uint64_t)Obj->get(Field); + } + + inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, Poco::Data::BLOB &Value) { + if(Obj->has(Field) && !Obj->isNull(Field)) { + auto Result = Utils::base64decode(Obj->get(Field).toString()); + Value.assignRaw((const unsigned char *)&Result[0],Result.size()); + } } inline void field_from_json(const Poco::JSON::Object::Ptr &Obj, const char *Field, Types::StringPairVec &Vec) { @@ -643,6 +660,27 @@ namespace OpenWifi::RESTAPI_utils { namespace OpenWifi::Utils { + inline void SetThreadName(const char *name) { +#ifdef __linux__ + Poco::Thread::current()->setName(name); + pthread_setname_np(pthread_self(), name); +#endif +#ifdef __APPLE__ + Poco::Thread::current()->setName(name); + pthread_setname_np(name); +#endif + } + + inline void SetThreadName(Poco::Thread &thr, const char *name) { +#ifdef __linux__ + thr.setName(name); + pthread_setname_np(thr.tid(), name); +#endif +#ifdef __APPLE__ + thr.setName(name); +#endif + } + enum MediaTypeEncodings { PLAIN, BINARY, @@ -1316,7 +1354,7 @@ namespace OpenWifi { inline void Start(); inline void Stop(); private: - std::atomic_bool Running_ = false; + mutable std::atomic_bool Running_ = false; Poco::Thread Thread_; }; @@ -1847,7 +1885,8 @@ namespace OpenWifi { Request = &RequestIn; Response = &ResponseIn; - Poco::Thread::current()->setName("WebServerThread_" + std::to_string(TransactionId_)); + std::string th_name = "restsvr_" + std::to_string(TransactionId_); + Utils::SetThreadName(th_name.c_str()); if(Request->getContentLength()>0) { if(Request->getContentType().find("application/json")!=std::string::npos) { @@ -2042,6 +2081,17 @@ namespace OpenWifi { return false; } + static inline bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, const std::string &Field, Poco::Data::BLOB &Value) { + if(O->has(Field)) { + std::string Content = O->get(Field).toString(); + auto DecodedBlob = Utils::base64decode(Content); + Value.assignRaw((const unsigned char *)&DecodedBlob[0],DecodedBlob.size()); + return true; + } + return false; + } + + template bool AssignIfPresent(const Poco::JSON::Object::Ptr &O, const std::string &Field, const T &value, T & assignee) { if(O->has(Field)) { assignee = value; @@ -2579,7 +2629,7 @@ namespace OpenWifi { private: std::recursive_mutex Mutex_; Poco::Thread Worker_; - std::atomic_bool Running_=false; + mutable std::atomic_bool Running_=false; Poco::NotificationQueue Queue_; }; @@ -2605,7 +2655,7 @@ namespace OpenWifi { private: std::recursive_mutex Mutex_; Poco::Thread Worker_; - std::atomic_bool Running_=false; + mutable std::atomic_bool Running_=false; }; class KafkaDispatcher : public Poco::Runnable { @@ -2662,6 +2712,7 @@ namespace OpenWifi { inline void run() override { Poco::AutoPtr Note(Queue_.waitDequeueNotification()); + Utils::SetThreadName("kafka-dispatch"); while(Note && Running_) { auto Msg = dynamic_cast(Note.get()); if(Msg!= nullptr) { @@ -2687,7 +2738,7 @@ namespace OpenWifi { std::recursive_mutex Mutex_; Types::NotifyTable Notifiers_; Poco::Thread Worker_; - std::atomic_bool Running_=false; + mutable std::atomic_bool Running_=false; uint64_t FunctionId_=1; Poco::NotificationQueue Queue_; }; @@ -2882,6 +2933,7 @@ namespace OpenWifi { void handleRequest(Poco::Net::HTTPServerRequest& Request, Poco::Net::HTTPServerResponse& Response) override { + Utils::SetThreadName("alb-request"); try { if((id_ % 100) == 0) { Logger_.debug(fmt::format("ALB-REQUEST({}): ALB Request {}.", @@ -2950,7 +3002,7 @@ namespace OpenWifi { std::unique_ptr Server_; std::unique_ptr Socket_; int Port_ = 0; - std::atomic_bool Running_=false; + mutable std::atomic_bool Running_=false; }; inline auto ALBHealthCheckServer() { return ALBHealthCheckServer::instance(); } @@ -2982,7 +3034,7 @@ namespace OpenWifi { inline Poco::Net::HTTPRequestHandler *CallServer(const std::string &Path, uint64_t Id) { RESTAPIHandler::BindingMap Bindings; - Poco::Thread::current()->setName(fmt::format("RESTAPI_ExtServer_{}",Id)); + Utils::SetThreadName(fmt::format("rest_ext_{}",Id).c_str()); return RESTAPI_ExtRouter(Path, Bindings, Logger(), Server_, Id); } @@ -3006,7 +3058,7 @@ namespace OpenWifi { inline Poco::Net::HTTPRequestHandler *createRequestHandler(const Poco::Net::HTTPServerRequest &Request) override { try { Poco::URI uri(Request.getURI()); - Poco::Thread::current()->setName(fmt::format("ExtWebServer_{}",TransactionId_)); + Utils::SetThreadName(fmt::format("rest_ext_{}",TransactionId_).c_str()); return RESTAPI_ExtServer()->CallServer(uri.getPath(), TransactionId_++); } catch (...) { @@ -3115,7 +3167,7 @@ namespace OpenWifi { inline Poco::Net::HTTPRequestHandler *CallServer(const std::string &Path, uint64_t Id) { RESTAPIHandler::BindingMap Bindings; - Poco::Thread::current()->setName(fmt::format("RESTAPI_IntServer_{}",Id)); + Utils::SetThreadName(fmt::format("rest_int_{}",Id).c_str()); return RESTAPI_IntRouter(Path, Bindings, Logger(), Server_, Id); } private: @@ -3524,7 +3576,9 @@ namespace OpenWifi { void DaemonPostInitialization(Poco::Util::Application &self); inline void MicroService::initialize(Poco::Util::Application &self) { - // add the default services + // Utils::SetThreadName("microservice"); + + // add the default services LoadConfigurationFile(); InitializeLoggingSystem(); @@ -3919,6 +3973,7 @@ namespace OpenWifi { inline int MicroService::main([[maybe_unused]] const ArgVec &args) { + // Utils::SetThreadName("main"); MyErrorHandler ErrorHandler(*this); Poco::ErrorHandler::set(&ErrorHandler); @@ -4034,6 +4089,7 @@ namespace OpenWifi { inline void BusEventManager::run() { Running_ = true; + Utils::SetThreadName("BusEventManager"); auto Msg = MicroService::instance().MakeSystemEventMessage(KafkaTopics::ServiceEvents::EVENT_JOIN); KafkaManager()->PostMessage(KafkaTopics::SERVICE_EVENTS,MicroService::instance().PrivateEndPoint(),Msg, false); while(Running_) { @@ -4119,6 +4175,8 @@ namespace OpenWifi { } inline void KafkaProducer::run() { + + Utils::SetThreadName("KafkaProducer"); cppkafka::Configuration Config({ { "client.id", MicroService::instance().ConfigGetString("openwifi.kafka.client.id") }, { "metadata.broker.list", MicroService::instance().ConfigGetString("openwifi.kafka.brokerlist") } @@ -4157,6 +4215,8 @@ namespace OpenWifi { } inline void KafkaConsumer::run() { + Utils::SetThreadName("KafkaConsumer"); + cppkafka::Configuration Config({ { "client.id", MicroService::instance().ConfigGetString("openwifi.kafka.client.id") }, { "metadata.broker.list", MicroService::instance().ConfigGetString("openwifi.kafka.brokerlist") }, @@ -4815,7 +4875,7 @@ namespace OpenWifi { [[nodiscard]] bool SendToUser(const std::string &userName, const std::string &Payload); void SendToAll(const std::string &Payload); private: - std::atomic_bool Running_ = false; + mutable std::atomic_bool Running_ = false; Poco::Thread Thr_; // std::unique_ptr ReactorPool_; Poco::Net::SocketReactor Reactor_; @@ -4912,6 +4972,7 @@ namespace OpenWifi { inline void WebSocketClientServer::run() { Running_ = true ; + Utils::SetThreadName("ws:clnt-svr"); while(Running_) { Poco::Thread::trySleep(2000); diff --git a/src/framework/orm.h b/src/framework/orm.h index 7b358a9..4fc8a15 100644 --- a/src/framework/orm.h +++ b/src/framework/orm.h @@ -133,6 +133,37 @@ namespace ORM { return R; } + inline std::string WHERE_AND_(std::string Result) { + return Result; + } + + template std::string WHERE_AND_(std::string Result, const char *fieldName, const T &Value, Args... args) { + if constexpr(std::is_same_v) + { + if(!Value.empty()) { + if(!Result.empty()) + Result += " and "; + Result += fieldName; + Result += '='; + Result += "'"; + Result += Escape(Value); + Result += "'"; + } + } else { + if(!Result.empty()) + Result += " and "; + Result += fieldName ; + Result += '='; + Result += std::to_string(Value); + } + return WHERE_AND_(Result,args...); + } + + template std::string WHERE_AND(Args... args) { + std::string Result; + return WHERE_AND_(Result, args...); + } + enum SqlComparison { EQ = 0, NEQ, LT, LTE, GT, GTE }; enum SqlBinaryOp { AND = 0 , OR };