From 35505a6a1ed89c00bf84896224c9155167096e94 Mon Sep 17 00:00:00 2001 From: Adam Capparelli Date: Tue, 12 Mar 2024 14:08:26 -0400 Subject: [PATCH] Auto push config on venue change and capabilities message. (#9) Signed-off-by: Adam Capparelli Co-authored-by: Adam Capparelli --- src/AutoDiscovery.cpp | 6 ++++ src/RESTAPI/RESTAPI_inventory_handler.cpp | 26 +++++++--------- src/Tasks/VenueConfigUpdater.h | 36 +++++++++++++++++++++++ 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/AutoDiscovery.cpp b/src/AutoDiscovery.cpp index b6ec3f8..3b401b9 100644 --- a/src/AutoDiscovery.cpp +++ b/src/AutoDiscovery.cpp @@ -5,6 +5,7 @@ #include "AutoDiscovery.h" #include "Poco/JSON/Parser.h" #include "StorageService.h" +#include "Tasks/VenueConfigUpdater.h" #include "framework/KafkaManager.h" #include "framework/KafkaTopics.h" #include "framework/ow_constants.h" @@ -107,6 +108,11 @@ namespace OpenWifi { if (!SerialNumber.empty() && Connected) { StorageService()->InventoryDB().CreateFromConnection( SerialNumber, ConnectedIP, Compatible, Locale, isConnection); + // Now that the entry has been created, we can try to push a config if + // the connection was a capabilities message. + if (isConnection){ + ComputeAndPushConfig(SerialNumber, Compatible, Logger()); + } } } } catch (const Poco::Exception &E) { diff --git a/src/RESTAPI/RESTAPI_inventory_handler.cpp b/src/RESTAPI/RESTAPI_inventory_handler.cpp index 8a831c8..025ec3c 100644 --- a/src/RESTAPI/RESTAPI_inventory_handler.cpp +++ b/src/RESTAPI/RESTAPI_inventory_handler.cpp @@ -14,28 +14,13 @@ #include "RESTAPI/RESTAPI_db_helpers.h" #include "SerialNumberCache.h" #include "StorageService.h" +#include "Tasks/VenueConfigUpdater.h" #include "framework/utils.h" #include "sdks/SDK_gw.h" #include "sdks/SDK_sec.h" namespace OpenWifi { - void GetRejectedLines(const Poco::JSON::Object::Ptr &Response, Types::StringVec &Warnings) { - try { - if (Response->has("results")) { - auto Results = Response->get("results").extract(); - auto Status = Results->get("status").extract(); - auto Rejected = Status->getArray("rejected"); - std::transform( - Rejected->begin(), Rejected->end(), std::back_inserter(Warnings), - [](auto i) -> auto { return i.toString(); }); - // for(const auto &i:*Rejected) - // Warnings.push_back(i.toString()); - } - } catch (...) { - } - } - void RESTAPI_inventory_handler::DoGet() { ProvObjects::InventoryTag Existing; @@ -314,6 +299,8 @@ namespace OpenWifi { return NotFound(); } + std::string previous_venue = Existing.venue; + auto RemoveSubscriber = GetParameter("removeSubscriber"); if (!RemoveSubscriber.empty()) { if (Existing.subscriber == RemoveSubscriber) { @@ -471,6 +458,13 @@ namespace OpenWifi { SDK::GW::Device::SetOwnerShip(this, SerialNumber, Existing.entity, Existing.venue, Existing.subscriber); + // Attempt an automatic config push when the venue is set and different than what is + // in DB. + poco_information(Logger(), fmt::format("New Venue {} Old Venue {}", NewObject.venue, previous_venue)); + if (!NewObject.venue.empty() && NewObject.venue != previous_venue) { + ComputeAndPushConfig(SerialNumber, NewObject.deviceType, Logger()); + } + ProvObjects::InventoryTag NewObjectCreated; DB_.GetRecord("id", Existing.info.id, NewObjectCreated); Poco::JSON::Object Answer; diff --git a/src/Tasks/VenueConfigUpdater.h b/src/Tasks/VenueConfigUpdater.h index c5aaae0..5e4d72e 100644 --- a/src/Tasks/VenueConfigUpdater.h +++ b/src/Tasks/VenueConfigUpdater.h @@ -28,6 +28,42 @@ namespace OpenWifi { } } + [[maybe_unused]] static void ComputeAndPushConfig(const std::string &SerialNumber, const std::string &DeviceType, Poco::Logger &Logger) { + /* + Generic Helper to compute a device's config and push it down to the device. + */ + poco_information(Logger, fmt::format("Attempting to push venue config for device {}", SerialNumber)); + auto DeviceConfig = std::make_shared(SerialNumber, + DeviceType, Logger, false); + auto Configuration = Poco::makeShared(); + try { + if (DeviceConfig->Get(Configuration)) { + std::ostringstream OS; + Configuration->stringify(OS); + auto Response = Poco::makeShared(); + poco_debug(Logger, + fmt::format("{}: Pushing configuration.", SerialNumber)); + if (SDK::GW::Device::Configure(nullptr, SerialNumber, Configuration, + Response)) { + Logger.debug( + fmt::format("{}: Configuration pushed.", SerialNumber)); + poco_information(Logger, + fmt::format("{}: Updated.", SerialNumber)); + } else { + poco_information(Logger, + fmt::format("{}: Not updated.", SerialNumber)); + } + } else { + poco_debug(Logger, + fmt::format("{}: Configuration is bad.", SerialNumber)); + } + } catch (...) { + poco_debug(Logger, + fmt::format("{}: Configuration is bad (caused an exception).", + SerialNumber)); + } + } + class VenueDeviceConfigUpdater : public Poco::Runnable { public: VenueDeviceConfigUpdater(const std::string &UUID, const std::string &venue, Poco::Logger &L)