diff --git a/src/RESTAPI/RESTAPI_inventory_handler.cpp b/src/RESTAPI/RESTAPI_inventory_handler.cpp index 0bf50de..5f4f40b 100644 --- a/src/RESTAPI/RESTAPI_inventory_handler.cpp +++ b/src/RESTAPI/RESTAPI_inventory_handler.cpp @@ -59,17 +59,10 @@ namespace OpenWifi{ } return ReturnObject(Answer); } else if(HasParameter("firmwareOptions", Arg) && Arg=="true") { - ProvObjects::FIRMWARE_UPGRADE_RULES Rules; - - StorageService()->InventoryDB().FindFirmwareOptions(SerialNumber,Rules); - - if(Rules == ProvObjects::dont_upgrade) { - Answer.set("firmwareUpgrade","no"); - } else { - Answer.set("firmwareUpgrade","yes"); - if(Rules == ProvObjects::upgrade_release_only) - Answer.set("firmwareRCOnly", Rules == ProvObjects::upgrade_release_only ); - } + ProvObjects::DeviceRules Rules; + StorageService()->InventoryDB().EvaluateDeviceSerialNumberRules(SerialNumber,Rules); + Answer.set("firmwareUpgrade",Rules.firmwareUpgrade); + Answer.set("firmwareRCOnly", Rules.rcOnly == "yes" ); return ReturnObject(Answer); } else if(HasParameter("applyConfiguration",Arg) && Arg=="true") { Logger().debug(Poco::format("%s: Retrieving configuration.",Existing.serialNumber)); diff --git a/src/StorageService.h b/src/StorageService.h index 53648d9..371d209 100644 --- a/src/StorageService.h +++ b/src/StorageService.h @@ -85,13 +85,14 @@ namespace OpenWifi { return (R_res.rrm=="inherit" || R_res.rcOnly=="inherit" || R_res.firmwareUpgrade=="inherit"); } - static inline void ApplyConfigRules(ProvObjects::DeviceRules & R_res) { + static inline bool ApplyConfigRules(ProvObjects::DeviceRules & R_res) { if(R_res.firmwareUpgrade=="inherit") R_res.firmwareUpgrade=MicroService::instance().ConfigGetString("firmware.updater.upgrade","yes"); if(R_res.rcOnly=="inherit") R_res.rcOnly=MicroService::instance().ConfigGetString("firmware.updater.releaseonly","yes"); if(R_res.rrm=="inherit") R_res.rrm=MicroService::instance().ConfigGetString("rrm.default","no"); + return true; } private: diff --git a/src/storage/storage_inventory.cpp b/src/storage/storage_inventory.cpp index 9555d50..ea597b4 100644 --- a/src/storage/storage_inventory.cpp +++ b/src/storage/storage_inventory.cpp @@ -186,109 +186,27 @@ namespace OpenWifi { return false; } - bool InventoryDB::FindFirmwareOptionsForEntity(const std::string &EntityUUID, ProvObjects::FIRMWARE_UPGRADE_RULES & Rules) { - std::string UUID = EntityUUID; - while(!UUID.empty() && UUID!=EntityDB::RootUUID()) { - ProvObjects::Entity E; - if(StorageService()->EntityDB().GetRecord("id",UUID,E)) { - if(!E.deviceConfiguration.empty()) { - ProvObjects::DeviceConfiguration C; - for(const auto &i:E.deviceConfiguration) { - if(StorageService()->ConfigurationDB().GetRecord("id",i,C)) { - if(C.deviceRules.firmwareUpgrade=="no") { - Rules = ProvObjects::dont_upgrade; - return false; - } - if(C.deviceRules.firmwareUpgrade=="yes") { - if(C.deviceRules.rcOnly=="yes") - Rules = ProvObjects::upgrade_release_only; - else - Rules = ProvObjects::upgrade_latest; - return true; - } - } - } - } - UUID = E.parent; - } else { - break; - } - } - Rules = Daemon()->FirmwareRules(); + bool InventoryDB::EvaluateDeviceIDRules(const std::string &id, ProvObjects::DeviceRules &Rules) { + ProvObjects::InventoryTag T; + if(GetRecord("id", id, T)) + return EvaluateDeviceRules(T,Rules); return false; } - bool InventoryDB::FindFirmwareOptionsForVenue(const std::string &VenueUUID, ProvObjects::FIRMWARE_UPGRADE_RULES & Rules) { - std::string UUID = VenueUUID; - while(!UUID.empty()) { - ProvObjects::Venue V; - if(StorageService()->VenueDB().GetRecord("id",UUID,V)) { - if(!V.deviceConfiguration.empty()) { - ProvObjects::DeviceConfiguration C; - for(const auto &i:V.deviceConfiguration) { - if(StorageService()->ConfigurationDB().GetRecord("id",i,C)) { - if(C.deviceRules.firmwareUpgrade=="no") { - Rules = ProvObjects::dont_upgrade; - return false; - } - if(C.deviceRules.firmwareUpgrade=="yes") { - if(C.deviceRules.rcOnly=="yes") - Rules = ProvObjects::upgrade_release_only; - else - Rules = ProvObjects::upgrade_latest; - return true; - } - } - } - } - if(!V.entity.empty()) { - return FindFirmwareOptionsForEntity(V.entity,Rules); - } else { - UUID = V.parent; - } - } else { - break; - } - } - Rules = Daemon()->FirmwareRules(); + bool InventoryDB::EvaluateDeviceSerialNumberRules(const std::string &serialNumber, ProvObjects::DeviceRules &Rules) { + ProvObjects::InventoryTag T; + if(GetRecord("serialNumber", serialNumber, T)) + return EvaluateDeviceRules(T,Rules); return false; } - bool InventoryDB::FindFirmwareOptions(std::string &SerialNumber, ProvObjects::FIRMWARE_UPGRADE_RULES & Rules) { - ProvObjects::InventoryTag T; - if(GetRecord("serialNumber",SerialNumber,T)) { - std::cout << "SerialNumber: " << SerialNumber << " found device." << std::endl; - // if there is a local configuration, use this - if(!T.deviceConfiguration.empty()) { - ProvObjects::DeviceConfiguration C; - if(StorageService()->ConfigurationDB().GetRecord("id",T.deviceConfiguration,C)) { - if(C.deviceRules.firmwareUpgrade=="no") { - Rules = ProvObjects::dont_upgrade; - return false; - } - if(C.deviceRules.firmwareUpgrade=="yes") { - if(C.deviceRules.rcOnly=="yes") - Rules = ProvObjects::upgrade_release_only; - else - Rules = ProvObjects::upgrade_latest; - return true; - } - } - } - - // look at entity... - if(!T.entity.empty()) { - return FindFirmwareOptionsForEntity(T.entity,Rules); - } - - if(!T.venue.empty()) { - return FindFirmwareOptionsForVenue(T.venue,Rules); - } - Rules = Daemon()->FirmwareRules(); - return false; - } - Rules = ProvObjects::dont_upgrade; - return false; + bool InventoryDB::EvaluateDeviceRules(const ProvObjects::InventoryTag &T, ProvObjects::DeviceRules &Rules) { + Rules = T.deviceRules; + if(!T.venue.empty()) + return StorageService()->VenueDB().EvaluateDeviceRules(T.venue,Rules); + if(!T.entity.empty()) + return StorageService()->EntityDB().EvaluateDeviceRules(T.venue,Rules); + return Storage::ApplyConfigRules(Rules); } void InventoryDB::InitializeSerialCache() { @@ -296,71 +214,17 @@ namespace OpenWifi { Iterate(F); } - bool InventoryDB::LookForRRMInEntity(const std::string &Entity) { - try { - ProvObjects::Entity E; - if(StorageService()->EntityDB().GetRecord("id", Entity, E)) { - if(E.deviceRules.rrm == "inherit") { - if(!E.parent.empty()) - return LookForRRMInEntity(E.parent); - return false; - } - if(E.deviceRules.rrm=="no") - return false; - return true; - } - return false; - } catch(...) { - - } - return false; - } - - bool InventoryDB::LookForRRMInVenue(const std::string &Venue) { - try { - ProvObjects::Venue V; - if(StorageService()->VenueDB().GetRecord("id", Venue, V)) { - if(V.deviceRules.rrm == "inherit") { - if(!V.parent.empty()) - return LookForRRMInVenue(V.parent); - if(!V.entity.empty()) - return LookForRRMInEntity(V.entity); - return false; - } - if(V.deviceRules.rrm=="no") - return false; - return true; - } - return false; - } catch(...) { - - } - return false; - } - - bool InventoryDB::LookForRRM(const ProvObjects::InventoryTag &T) { - if(!T.venue.empty()) - return LookForRRMInVenue(T.venue); - if(!T.entity.empty()) - return LookForRRMInEntity(T.entity); - return false; - } - bool InventoryDB::GetRRMDeviceList(Types::UUIDvec_t &DeviceList) { // get a local copy of the cache - this could be expensive. auto C = SerialNumberCache()->GetCacheCopy(); for(const auto &i:C) { - std::string SerialNumber = Utils::IntToSerialNumber(i); ProvObjects::InventoryTag Tag; - if(StorageService()->InventoryDB().GetRecord("serialNumber",SerialNumber,Tag)) { - if(Tag.deviceRules.rrm=="no") - continue; - if(Tag.deviceRules.rrm=="inherit") { - if(!LookForRRM(Tag)) - continue; - } - DeviceList.push_back(SerialNumber); + ProvObjects::DeviceRules Rules; + std::string SerialNumber = Utils::IntToSerialNumber(i); + if(EvaluateDeviceSerialNumberRules(SerialNumber,Rules)) { + if(Rules.rrm=="yes") + DeviceList.push_back(SerialNumber); } } return true; diff --git a/src/storage/storage_inventory.h b/src/storage/storage_inventory.h index f53eaca..c4141ac 100644 --- a/src/storage/storage_inventory.h +++ b/src/storage/storage_inventory.h @@ -44,19 +44,20 @@ namespace OpenWifi { virtual ~InventoryDB() {}; bool CreateFromConnection(const std::string & SerialNumber, const std::string & ConnectionInfo, const std::string & DeviceType, const std::string &Locale ); - bool FindFirmwareOptions(std::string & SerialNumber, ProvObjects::FIRMWARE_UPGRADE_RULES & Rules); - static bool FindFirmwareOptionsForEntity(const std::string & EntityUUID, ProvObjects::FIRMWARE_UPGRADE_RULES & Rules); - static bool FindFirmwareOptionsForVenue(const std::string & VenueUUID, ProvObjects::FIRMWARE_UPGRADE_RULES & Rules); + void InitializeSerialCache(); bool GetRRMDeviceList(Types::UUIDvec_t & DeviceList); - bool LookForRRM( const ProvObjects::InventoryTag &T); - bool LookForRRMInVenue(const std::string &Venue); - bool LookForRRMInEntity(const std::string &Entity); + + bool EvaluateDeviceIDRules(const std::string &id, ProvObjects::DeviceRules &Rules); + bool EvaluateDeviceSerialNumberRules(const std::string &serialNumber, ProvObjects::DeviceRules &Rules); + inline uint32_t Version() override { return 1; } + bool Upgrade(uint32_t from, uint32_t &to) override; private: + bool EvaluateDeviceRules(const ProvObjects::InventoryTag &T, ProvObjects::DeviceRules &Rules); }; }