From aa7e4b69b18d4c0bc4247a35bb64c7e6a4002bc9 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Tue, 13 Jul 2021 21:32:56 -0700 Subject: [PATCH] Reword with microservice arch. --- CMakeLists.txt | 2 +- openapi/ucentralfws.yaml | 149 ----------------------------------- src/Daemon.cpp | 2 + src/DeviceCache.cpp | 28 +++++++ src/DeviceCache.h | 52 ++++++++++++ src/ManifestCreator.cpp | 17 ++-- src/ManifestCreator.h | 1 + src/NewConnectionHandler.cpp | 14 +--- ucentralfms.properties | 1 + 9 files changed, 95 insertions(+), 171 deletions(-) create mode 100644 src/DeviceCache.cpp create mode 100644 src/DeviceCache.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 087fba2..a555657 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,7 +75,7 @@ add_executable( ucentralfms src/RESTAPI_utils.cpp src/RESTAPI_utils.h src/RESTAPI_FMSObjects.cpp src/RESTAPI_FMSObjects.h src/storage_firmwares.h src/storage_history.cpp - src/storage_history.h src/storage_deviceTypes.cpp src/storage_deviceTypes.h src/RESTAPI_historyHandler.cpp src/RESTAPI_historyHandler.h src/NewConnectionHandler.cpp src/NewConnectionHandler.h src/LatestFirmwareCache.cpp src/LatestFirmwareCache.h) + src/storage_history.h src/storage_deviceTypes.cpp src/storage_deviceTypes.h src/RESTAPI_historyHandler.cpp src/RESTAPI_historyHandler.h src/NewConnectionHandler.cpp src/NewConnectionHandler.h src/LatestFirmwareCache.cpp src/LatestFirmwareCache.h src/DeviceCache.cpp src/DeviceCache.h) target_link_libraries(ucentralfms PUBLIC ${Poco_LIBRARIES} ${MySQL_LIBRARIES} diff --git a/openapi/ucentralfws.yaml b/openapi/ucentralfws.yaml index 0fe56e0..87b681e 100644 --- a/openapi/ucentralfws.yaml +++ b/openapi/ucentralfws.yaml @@ -125,39 +125,6 @@ components: items: $ref: '#/components/schemas/FirmwareDetails' - DeviceType: - type: object - properties: - id: - type: string - format: uuid - deviceType: - type: string - manufacturer: - type: string - model: - type: string - policy: - type: string - notes: - type: array - items: - $ref: '#/components/schemas/NoteInfo' - lastUpdate: - type: integer - format: int64 - created: - type: integer - format: int64 - - DeviceTypeList: - type: object - properties: - deviceTypes: - type: array - items: - $ref: '#/components/schemas/DeviceType' - RevisionHistoryEntry: type: object properties: @@ -423,122 +390,6 @@ paths: 404: $ref: '#/components/responses/NotFound' - /deviceTypes: - get: - tags: - - DeviceTypes - summary: List all the defined device types - operationId: getDeviceTypes - parameters: - - in: query - description: Pagination start (starts at 1. If not specified, 1 is assumed) - name: offset - schema: - type: integer - required: false - - in: query - description: Maximum number of entries to return (if absent, no limit is assumed) - name: limit - schema: - type: integer - required: false - - in: query - description: Filter the results - name: filter - schema: - type: string - required: false - responses: - 200: - description: List of known device types. - content: - application/json: - schema: - $ref: '#/components/schemas/DeviceTypeList' - 403: - $ref: '#/components/responses/Unauthorized' - 404: - $ref: '#/components/responses/NotFound' - - /deviceType/{id}: - get: - tags: - - DeviceTypes - summary: Get a specific DeviceType - operationId: getDeviceType - parameters: - - in: path - description: ID of deviceType to retrieve - name: id - schema: - type: string - format: uuid - required: true - responses: - 200: - description: Get a single device type. - content: - application/json: - schema: - $ref: '#/components/schemas/DeviceType' - 403: - $ref: '#/components/responses/Unauthorized' - 404: - $ref: '#/components/responses/NotFound' - - delete: - tags: - - DeviceTypes - summary: Delete a specific DeviceType - operationId: deleteDeviceType - parameters: - - in: path - description: ID of deviceType to retrieve - name: id - schema: - type: string - format: uuid - required: true - responses: - 200: - $ref: '#/components/responses/Success' - 403: - $ref: '#/components/responses/Unauthorized' - 404: - $ref: '#/components/responses/NotFound' - - put: - tags: - - DeviceTypes - summary: Modify a specific DeviceType - operationId: modifyDeviceType - parameters: - - in: path - description: ID of deviceType to modify - name: id - schema: - type: string - format: uuid - required: true - requestBody: - description: Modifications for the DeviceType - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/DeviceType' - responses: - 200: - description: Get a single device type. - content: - application/json: - schema: - $ref: '#/components/schemas/DeviceType' - 403: - $ref: '#/components/responses/Unauthorized' - 404: - $ref: '#/components/responses/NotFound' - /revisionHistory/{serialNumber}: get: tags: diff --git a/src/Daemon.cpp b/src/Daemon.cpp index 2e9d2e9..90024de 100644 --- a/src/Daemon.cpp +++ b/src/Daemon.cpp @@ -24,6 +24,7 @@ #include "KafkaManager.h" #include "NewConnectionHandler.h" #include "LatestFirmwareCache.h" +#include "DeviceCache.h" namespace uCentral { class Daemon *Daemon::instance_ = nullptr; @@ -37,6 +38,7 @@ namespace uCentral { vDAEMON_BUS_TIMER, Types::SubSystemVec{Storage(), LatestFirmwareCache(), + DeviceCache(), NewConnectionHandler(), RESTAPI_server(), RESTAPI_InternalServer(), diff --git a/src/DeviceCache.cpp b/src/DeviceCache.cpp new file mode 100644 index 0000000..881ddd1 --- /dev/null +++ b/src/DeviceCache.cpp @@ -0,0 +1,28 @@ +// +// Created by stephane bourque on 2021-07-13. +// + +#include "DeviceCache.h" + +namespace uCentral { + class DeviceCache *DeviceCache::instance_ = nullptr; + + int DeviceCache::Start() { + + return 0; + } + + void DeviceCache::Stop() { + + } + + void DeviceCache::AddToCache( + const std::string &serialNumber, const std::string & DeviceType, + const std::string &Host, const std::string &Firmware) { + + } + + void DeviceCache::DumpCache() { + + } +} \ No newline at end of file diff --git a/src/DeviceCache.h b/src/DeviceCache.h new file mode 100644 index 0000000..60cdefe --- /dev/null +++ b/src/DeviceCache.h @@ -0,0 +1,52 @@ +// +// Created by stephane bourque on 2021-07-13. +// + +#ifndef UCENTRALFMS_DEVICECACHE_H +#define UCENTRALFMS_DEVICECACHE_H + +#include +#include "SubSystemServer.h" +#include "uCentralTypes.h" + +namespace uCentral { + + struct DeviceCacheEntry { + std::string compatible; + std::string host; + std::string firmware; + }; + typedef std::map DeviceCacheMap; + + class DeviceCache : public SubSystemServer { + public: + static DeviceCache *instance() { + if (instance_ == nullptr) { + instance_ = new DeviceCache; + } + return instance_; + } + + int Start() override; + void Stop() override; + void AddToCache(const std::string &serialNumber, const std::string & DeviceType, + const std::string &Host, const std::string &Firmware); + std::string FindLatestFirmware(std::string &DeviceType); + void DumpCache(); + + private: + static DeviceCache *instance_; + std::atomic_bool Running_=false; + DeviceCacheMap DeviceCache_; + explicit DeviceCache() noexcept: + SubSystemServer("DeviceCache", "DEVICE-CACHE", "devicecache") + { + } + }; + + inline DeviceCache * DeviceCache() { return DeviceCache::instance(); } +} + + + +#endif //UCENTRALFMS_DEVICECACHE_H diff --git a/src/ManifestCreator.cpp b/src/ManifestCreator.cpp index 8cc368c..bba6f75 100644 --- a/src/ManifestCreator.cpp +++ b/src/ManifestCreator.cpp @@ -23,19 +23,17 @@ namespace uCentral { Running_ = true; while(Running_) { - Poco::Thread::trySleep(10000); + Poco::Thread::trySleep(DBRefresh_*1000); if(!Running_) break; - std::cout << "About to read bucket..." << std::endl; + Logger_.information("Performing DB refresh"); S3BucketContent BucketList; ReadBucket(BucketList); if(!Running_) break; - std::cout << "Bucket read: " << BucketList.size() << std::endl; + Logger_.information(Poco::format("Found %Lu entries in S# repository for firmware.",(uint64_t)BucketList.size())); ComputeManifest(BucketList); AddManifestToDB(BucketList); - // LatestFirmwareCache()->DumpCache(); - // Print(BucketList); } } @@ -87,9 +85,8 @@ namespace uCentral { F.revision = BucketEntry.Revision; F.deviceType = BucketEntry.Compatible; if(Storage()->AddFirmware(F)) { - std::cout << "Adding " << Release << std::endl; + Logger_.information(Poco::format("Adding firmware '%'",Release)); } else { - std::cout << "Could not add firmware..." << Release << std::endl; } } } @@ -103,6 +100,8 @@ namespace uCentral { S3Key_ = Daemon()->ConfigGetString("s3.key"); S3Retry_ = Daemon()->ConfigGetInt("s3.retry",60); + DBRefresh_ = Daemon()->ConfigGetInt("firmwaredb.refresh",30*60); + AwsConfig_.enableTcpKeepAlive = true; AwsConfig_.enableEndpointDiscovery = true; AwsConfig_.useDualStack = true; @@ -167,13 +166,9 @@ namespace uCentral { auto Outcome = S3Client.ListObjects(Request); if(Outcome.IsSuccess()) { -// std::cout << "Success..." << std::endl; -// std::cout << "Objects: ." ; Aws::Vector objects = Outcome.GetResult().GetContents(); for (const auto &Object : objects) { Poco::Path FileName(Object.GetKey().c_str()); - // std::cout << "Object: " << Object.GetKey() << std::endl; -// std::cout << "." << std::flush; if(!Running_) return false; if (FileName.getExtension() == "json") { diff --git a/src/ManifestCreator.h b/src/ManifestCreator.h index bd5862a..5e5ae68 100644 --- a/src/ManifestCreator.h +++ b/src/ManifestCreator.h @@ -64,6 +64,7 @@ namespace uCentral { uint64_t S3Retry_; Aws::Client::ClientConfiguration AwsConfig_{"ARILIA"}; Aws::Auth::AWSCredentials AwsCreds_; + uint64_t DBRefresh_ = 30 * 60; ManifestCreator() noexcept: SubSystemServer("ManifestCreator", "MANIFEST-MGR", "manifestcreator") { diff --git a/src/NewConnectionHandler.cpp b/src/NewConnectionHandler.cpp index d708cec..20bdc99 100644 --- a/src/NewConnectionHandler.cpp +++ b/src/NewConnectionHandler.cpp @@ -33,7 +33,6 @@ namespace uCentral { if(!Running_) break; - std::cout << "New connection..." << std::endl; Types::StringPair S; { SubMutexGuard G(Mutex_); @@ -41,22 +40,18 @@ namespace uCentral { NewConnections_.pop(); } auto SerialNumber = S.first; - - std::cout << "Connection data:" << S.second << std::endl; - Poco::JSON::Parser Parser; auto Object = Parser.parse(S.second).extract(); + std::string compatible, serialNumber, firmware; if(Object->has("payload")) { - std::cout << __LINE__ << std::endl; auto PayloadObj = Object->getObject("payload"); - std::cout << __LINE__ << std::endl; if(PayloadObj->has("capabilities")) { - std::cout << __LINE__ << std::endl; auto CapObj = PayloadObj->getObject("capabilities"); - std::cout << __LINE__ << std::endl; if(CapObj->has("compatible")) { - std::cout << __LINE__ << std::endl; + compatible = CapObj->get("compatible").toString(); + serialNumber = PayloadObj->get("serial").toString(); + firmware = PayloadObj->get("firmware").toString(); std::cout << "Compatible: " << CapObj->get("compatible").toString() << std::endl; } } @@ -87,6 +82,5 @@ namespace uCentral { void NewConnectionHandler::ConnectionReceived( const std::string & Key, const std::string & Message) { SubMutexGuard G(Mutex_); NewConnections_.push(std::make_pair(Key,Message)); - std::cout << "New connection..." << std::endl; } } \ No newline at end of file diff --git a/ucentralfms.properties b/ucentralfms.properties index bb62fc6..07c09c9 100644 --- a/ucentralfms.properties +++ b/ucentralfms.properties @@ -45,6 +45,7 @@ s3.key = ************************** s3.retry = 60 s3.bucket.uri = ucentral-ap-firmware.s3.amazonaws.com +firmwaredb.refresh = 1800 ############################# # Generic information for all micro services #############################