diff options
Diffstat (limited to 'src/components/policy/policy_external/src/cache_manager.cc')
-rw-r--r-- | src/components/policy/policy_external/src/cache_manager.cc | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/src/components/policy/policy_external/src/cache_manager.cc b/src/components/policy/policy_external/src/cache_manager.cc index 3e1538ab04..e1015ea574 100644 --- a/src/components/policy/policy_external/src/cache_manager.cc +++ b/src/components/policy/policy_external/src/cache_manager.cc @@ -269,7 +269,8 @@ CacheManager::CacheManager() : CacheManagerInterface() , pt_(new policy_table::Table) , backup_(new SQLPTExtRepresentation()) - , update_required(false) { + , update_required(false) + , removed_custom_vd_items_() { InitBackupThread(); } @@ -758,6 +759,12 @@ bool CacheManager::ApplyUpdate(const policy_table::Table& update_pt) { // Apply update for vehicle data if (update_pt.policy_table.vehicle_data.is_initialized()) { + policy_table::VehicleDataItems custom_items_before_apply; + if (pt_->policy_table.vehicle_data->schema_items.is_initialized()) { + custom_items_before_apply = + CollectCustomVDItems(*pt_->policy_table.vehicle_data->schema_items); + } + if (!update_pt.policy_table.vehicle_data->schema_items.is_initialized() || update_pt.policy_table.vehicle_data->schema_items->empty()) { pt_->policy_table.vehicle_data->schema_items = @@ -771,6 +778,12 @@ bool CacheManager::ApplyUpdate(const policy_table::Table& update_pt) { pt_->policy_table.vehicle_data->schema_items = rpc::Optional<policy_table::VehicleDataItems>(custom_items); } + + policy_table::VehicleDataItems custom_items_after_apply = + *pt_->policy_table.vehicle_data->schema_items; + const auto& items_diff = CalculateCustomVdItemsDiff( + custom_items_before_apply, custom_items_after_apply); + SetRemovedCustomVdItems(items_diff); } ResetCalculatedPermissions(); @@ -1430,6 +1443,12 @@ CacheManager::GetVehicleDataItems() const { return std::vector<policy_table::VehicleDataItem>(); } +std::vector<policy_table::VehicleDataItem> +CacheManager::GetRemovedVehicleDataItems() const { + CACHE_MANAGER_CHECK(std::vector<policy_table::VehicleDataItem>()); + return removed_custom_vd_items_; +} + Json::Value CacheManager::GetPolicyTableData() const { return pt_->policy_table.ToJsonValue(); } @@ -1922,6 +1941,46 @@ void CacheManager::CheckSnapshotInitialization() { } } +policy_table::VehicleDataItems CacheManager::CalculateCustomVdItemsDiff( + const policy_table::VehicleDataItems& items_before, + const policy_table::VehicleDataItems& items_after) const { + LOG4CXX_AUTO_TRACE(logger_); + if (items_before.empty()) { + LOG4CXX_DEBUG(logger_, "No custom VD items found in policy"); + return policy_table::VehicleDataItems(); + } + + if (items_after.empty()) { + LOG4CXX_DEBUG(logger_, + "All custom VD items were removed after policy update"); + return items_before; + } + + policy_table::VehicleDataItems removed_items; + for (auto& item_to_search : items_before) { + auto item_predicate = + [&item_to_search](const policy_table::VehicleDataItem& item_to_check) { + return item_to_search.name == item_to_check.name; + }; + + auto it = + std::find_if(items_after.begin(), items_after.end(), item_predicate); + if (items_after.end() == it) { + removed_items.push_back(item_to_search); + } + } + + LOG4CXX_DEBUG(logger_, + "Found " << removed_items.size() << " removed VD items"); + return removed_items; +} + +void CacheManager::SetRemovedCustomVdItems( + const policy_table::VehicleDataItems& removed_items) { + LOG4CXX_AUTO_TRACE(logger_); + removed_custom_vd_items_ = removed_items; +} + void CacheManager::PersistData() { LOG4CXX_AUTO_TRACE(logger_); if (backup_.use_count() != 0) { |