diff options
author | Alexander Kutsan <akutsan@luxoft.com> | 2017-08-10 14:07:09 +0300 |
---|---|---|
committer | Andrey Oleynik <aoleynik@luxoft.com> | 2017-08-16 22:34:23 +0300 |
commit | 59f437dc8608e1ec54c517416a39860d9a7be3b8 (patch) | |
tree | 2908c276bd5ea066741c034eb2bd144682ba8649 /src/components/policy/policy_regular/src/sql_pt_representation.cc | |
parent | 06729e8b744374a4d9694f669101b6c554ce42e0 (diff) | |
download | sdl_core-59f437dc8608e1ec54c517416a39860d9a7be3b8.tar.gz |
Implementation of Remote Control plugin
Stype changes after integration
Fix policy handler remote unit tests
Diffstat (limited to 'src/components/policy/policy_regular/src/sql_pt_representation.cc')
-rw-r--r-- | src/components/policy/policy_regular/src/sql_pt_representation.cc | 376 |
1 files changed, 374 insertions, 2 deletions
diff --git a/src/components/policy/policy_regular/src/sql_pt_representation.cc b/src/components/policy/policy_regular/src/sql_pt_representation.cc index e94c853414..854b6e4804 100644 --- a/src/components/policy/policy_regular/src/sql_pt_representation.cc +++ b/src/components/policy/policy_regular/src/sql_pt_representation.cc @@ -526,6 +526,12 @@ void SQLPTRepresentation::GatherModuleConfig( *config->vehicle_model = query.GetString(7); *config->vehicle_year = query.GetString(8); *config->preloaded_date = query.GetString(9); +#ifdef SDL_REMOTE_CONTROL + *config->user_consent_passengersRC = + query.IsNull(8) ? true : query.GetBoolean(8); + *config->country_consent_passengersRC = + query.IsNull(9) ? true : query.GetBoolean(9); +#endif // SDL_REMOTE_CONTROL } utils::dbms::SQLQuery endpoints(db()); @@ -705,6 +711,23 @@ bool SQLPTRepresentation::GatherApplicationPoliciesSection( if (!GatherAppGroup(app_id, ¶ms.groups)) { return false; } +#ifdef SDL_REMOTE_CONTROL + if (!GatherAppGroupPrimary(app_id, &*params.groups_primaryRC)) { + return false; + } + if (!GatherAppGroupNonPrimary(app_id, &*params.groups_nonPrimaryRC)) { + return false; + } + bool denied = false; + if (!GatherRemoteControlDenied(app_id, &denied)) { + return false; + } + if (!denied) { + if (!GatherModuleType(app_id, &*params.moduleType)) { + return false; + } + } +#endif // SDL_REMOTE_CONTROL if (!GatherNickName(app_id, &*params.nicknames)) { return false; } @@ -859,6 +882,20 @@ bool SQLPTRepresentation::SaveApplicationPoliciesSection( LOG4CXX_WARN(logger_, "Incorrect delete from app_group."); return false; } +#ifdef SDL_REMOTE_CONTROL + if (!query_delete.Exec(sql_pt::kDeleteAppGroupPrimary)) { + LOG4CXX_WARN(logger_, "Incorrect delete from app_group_primary."); + return false; + } + if (!query_delete.Exec(sql_pt::kDeleteAppGroupNonPrimary)) { + LOG4CXX_WARN(logger_, "Incorrect delete from app_group_non_primary."); + return false; + } + if (!query_delete.Exec(sql_pt::kDeleteModuleTypes)) { + LOG4CXX_WARN(logger_, "Incorrect delete from module_type."); + return false; + } +#endif // SDL_REMOTE_CONTROL if (!query_delete.Exec(sql_pt::kDeleteApplication)) { LOG4CXX_WARN(logger_, "Incorrect delete from application."); return false; @@ -941,6 +978,20 @@ bool SQLPTRepresentation::SaveSpecificAppPolicy( if (!SaveAppGroup(app.first, app.second.groups)) { return false; } +#ifdef SDL_REMOTE_CONTROL + if (!SaveAppGroupPrimary(app.first, *app.second.groups_primaryRC)) { + return false; + } + if (!SaveAppGroupNonPrimary(app.first, *app.second.groups_nonPrimaryRC)) { + return false; + } + + bool denied = !app.second.moduleType->is_initialized(); + if (!SaveRemoteControlDenied(app.first, denied) || + !SaveModuleType(app.first, *app.second.moduleType)) { + return false; + } +#endif // SDL_REMOTE_CONTROL if (!SaveNickname(app.first, *app.second.nicknames)) { return false; } @@ -1111,7 +1162,14 @@ bool SQLPTRepresentation::SaveModuleConfig( : query.Bind(7); config.vehicle_year.is_initialized() ? query.Bind(8, *(config.vehicle_year)) : query.Bind(8); - +#ifdef SDL_REMOTE_CONTROL + config.user_consent_passengersRC.is_initialized() + ? query.Bind(9, *(config.user_consent_passengersRC)) + : query.Bind(9); + config.country_consent_passengersRC.is_initialized() + ? query.Bind(10, *(config.country_consent_passengersRC)) + : query.Bind(10); +#endif // SDL_REMOTE_CONTROL if (!query.Exec()) { LOG4CXX_WARN(logger_, "Incorrect update module config"); return false; @@ -1506,6 +1564,280 @@ bool SQLPTRepresentation::GatherAppGroup( return true; } +#ifdef SDL_REMOTE_CONTROL +bool SQLPTRepresentation::GatherAppGroupPrimary( + const std::string& app_id, policy_table::Strings* app_groups) const { + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(sql_pt::kSelectAppGroupsPrimary)) { + LOG4CXX_WARN(logger_, "Incorrect select from app groups for primary RC"); + return false; + } + + query.Bind(0, app_id); + while (query.Next()) { + app_groups->push_back(query.GetString(0)); + } + return true; +} + +bool SQLPTRepresentation::GatherAppGroupNonPrimary( + const std::string& app_id, policy_table::Strings* app_groups) const { + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(sql_pt::kSelectAppGroupsNonPrimary)) { + LOG4CXX_WARN(logger_, + "Incorrect select from app groups for non primary RC"); + return false; + } + + query.Bind(0, app_id); + while (query.Next()) { + app_groups->push_back(query.GetString(0)); + } + return true; +} + +bool SQLPTRepresentation::GatherRemoteControlDenied(const std::string& app_id, + bool* denied) const { + LOG4CXX_AUTO_TRACE(logger_); + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(sql_pt::kSelectRemoteControlDenied)) { + LOG4CXX_WARN(logger_, "Incorrect select remote control flag"); + return false; + } + query.Bind(0, app_id); + if (query.Next()) { + *denied = query.GetBoolean(0); + } else { + return false; + } + return true; +} + +bool SQLPTRepresentation::GatherModuleType( + const std::string& app_id, policy_table::ModuleTypes* app_types) const { + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(sql_pt::kSelectModuleTypes)) { + LOG4CXX_WARN(logger_, "Incorrect select from app types"); + return false; + } + + query.Bind(0, app_id); + while (query.Next()) { + policy_table::ModuleType type; + if (!policy_table::EnumFromJsonString(query.GetString(0), &type)) { + return false; + } + app_types->push_back(type); + } + return true; +} + +bool SQLPTRepresentation::SaveAppGroupPrimary( + const std::string& app_id, const policy_table::Strings& app_groups) { + LOG4CXX_AUTO_TRACE(logger_); + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(sql_pt::kInsertAppGroupPrimary)) { + LOG4CXX_WARN(logger_, "Incorrect insert statement for app group primary"); + return false; + } + policy_table::Strings::const_iterator it; + for (it = app_groups.begin(); it != app_groups.end(); ++it) { + std::string ssss = *it; + LOG4CXX_INFO(logger_, "Group: " << ssss); + query.Bind(0, app_id); + query.Bind(1, *it); + if (!query.Exec() || !query.Reset()) { + LOG4CXX_WARN(logger_, + "Incorrect insert into app group primary." + << query.LastError().text()); + return false; + } + } + + return true; +} + +bool SQLPTRepresentation::SaveAppGroupNonPrimary( + const std::string& app_id, const policy_table::Strings& app_groups) { + LOG4CXX_AUTO_TRACE(logger_); + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(sql_pt::kInsertAppGroupNonPrimary)) { + LOG4CXX_WARN(logger_, + "Incorrect insert statement for app group non primary"); + return false; + } + policy_table::Strings::const_iterator it; + for (it = app_groups.begin(); it != app_groups.end(); ++it) { + std::string ssss = *it; + LOG4CXX_INFO(logger_, "Group: " << ssss); + query.Bind(0, app_id); + query.Bind(1, *it); + if (!query.Exec() || !query.Reset()) { + LOG4CXX_WARN(logger_, + "Incorrect insert into app group non primary." + << query.LastError().text()); + return false; + } + } + + return true; +} + +bool SQLPTRepresentation::SaveRemoteControlDenied(const std::string& app_id, + bool deny) { + LOG4CXX_AUTO_TRACE(logger_); + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(sql_pt::kUpdateRemoteControlDenied)) { + LOG4CXX_WARN(logger_, "Incorrect update statement for remote control flag"); + return false; + } + LOG4CXX_DEBUG(logger_, "App: " << app_id << std::boolalpha << " - " << deny); + query.Bind(0, deny); + query.Bind(1, app_id); + if (!query.Exec()) { + LOG4CXX_WARN(logger_, "Incorrect update remote control flag."); + return false; + } + return true; +} + +bool SQLPTRepresentation::SaveModuleType( + const std::string& app_id, const policy_table::ModuleTypes& types) { + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(sql_pt::kInsertModuleType)) { + LOG4CXX_WARN(logger_, "Incorrect insert statement for module type"); + return false; + } + + policy_table::ModuleTypes::const_iterator it; + for (it = types.begin(); it != types.end(); ++it) { + query.Bind(0, app_id); + std::string module(policy_table::EnumToJsonString(*it)); + query.Bind(1, module); + LOG4CXX_DEBUG(logger_, + "Module(app: " << app_id << ", type: " << module << ")"); + if (!query.Exec() || !query.Reset()) { + LOG4CXX_WARN(logger_, "Incorrect insert into module type."); + return false; + } + } + + return true; +} + +bool SQLPTRepresentation::SaveAccessModule( + TypeAccess access, const policy_table::AccessModules& modules) { + LOG4CXX_AUTO_TRACE(logger_); + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(sql_pt::kInsertAccessModule)) { + LOG4CXX_WARN(logger_, "Incorrect insert statement for access module"); + return false; + } + + policy_table::AccessModules::const_iterator i; + for (i = modules.begin(); i != modules.end(); ++i) { + const std::string& name = i->first; + const policy_table::RemoteRpcs& rpcs = i->second; + query.Bind(0, name); + query.Bind(1, access); + if (!query.Exec()) { + LOG4CXX_WARN(logger_, "Incorrect insert into access module."); + return false; + } + int id = query.LastInsertId(); + if (!query.Reset()) { + LOG4CXX_WARN(logger_, "Couldn't reset query access module."); + return false; + } + if (!SaveRemoteRpc(id, rpcs)) { + return false; + } + } + return true; +} + +bool SQLPTRepresentation::GatherAccessModule( + TypeAccess access, policy_table::AccessModules* modules) const { + LOG4CXX_AUTO_TRACE(logger_); + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(sql_pt::kSelectAccessModules)) { + LOG4CXX_WARN(logger_, "Incorrect select from access module"); + return false; + } + + query.Bind(0, access); + while (query.Next()) { + int id = query.GetInteger(0); + std::string name = query.GetString(1); + policy_table::RemoteRpcs rpcs; + if (!GatherRemoteRpc(id, &rpcs)) { + return false; + } + modules->insert(std::make_pair(name, rpcs)); + } + return true; +} + +bool SQLPTRepresentation::SaveRemoteRpc(int module_id, + const policy_table::RemoteRpcs& rpcs) { + LOG4CXX_AUTO_TRACE(logger_); + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(sql_pt::kInsertRemoteRpc)) { + LOG4CXX_WARN(logger_, "Incorrect insert statement for remote rpc"); + return false; + } + policy_table::RemoteRpcs::const_iterator i; + for (i = rpcs.begin(); i != rpcs.end(); ++i) { + const std::string& name = i->first; + const policy_table::Strings& params = i->second; + policy_table::Strings::const_iterator j; + if (params.empty()) { + query.Bind(0, module_id); + query.Bind(1, name); + query.Bind(2); + if (!query.Exec() || !query.Reset()) { + LOG4CXX_WARN(logger_, "Incorrect insert into remote rpc."); + return false; + } + } else { + for (j = params.begin(); j != params.end(); ++j) { + const std::string& param = *j; + query.Bind(0, module_id); + query.Bind(1, name); + query.Bind(2, param); + if (!query.Exec() || !query.Reset()) { + LOG4CXX_WARN(logger_, "Incorrect insert into remote rpc."); + return false; + } + } + } + } + return true; +} + +bool SQLPTRepresentation::GatherRemoteRpc( + int module_id, policy_table::RemoteRpcs* rpcs) const { + LOG4CXX_AUTO_TRACE(logger_); + utils::dbms::SQLQuery query(db()); + if (!query.Prepare(sql_pt::kSelectRemoteRpcs)) { + LOG4CXX_WARN(logger_, "Incorrect select from remote rpc"); + return false; + } + + query.Bind(0, module_id); + while (query.Next()) { + std::string name = query.GetString(0); + if (!query.IsNull(1)) { + std::string parameter = query.GetString(1); + (*rpcs)[name].push_back(parameter); + } else { + rpcs->insert(std::make_pair(name, policy_table::Strings())); + } + } + return true; +} +#endif // SDL_REMOTE_CONTROL + bool SQLPTRepresentation::SaveApplicationCustomData(const std::string& app_id, bool is_revoked, bool is_default, @@ -1590,6 +1922,31 @@ bool SQLPTRepresentation::SetDefaultPolicy(const std::string& app_id) { LOG4CXX_ERROR(logger_, "Failed deleting from app_group."); return false; } +#ifdef SDL_REMOTE_CONTROL + utils::dbms::SQLQuery query_p(db()); + if (!query_p.Prepare(sql_pt::kDeleteAppGroupPrimaryByApplicationId)) { + LOG4CXX_ERROR(logger_, + "Incorrect statement to delete from app_group_primary."); + return false; + } + query_p.Bind(0, app_id); + if (!query_p.Exec()) { + LOG4CXX_ERROR(logger_, "Failed deleting from app_group_primary."); + return false; + } + + utils::dbms::SQLQuery query_np(db()); + if (!query_np.Prepare(sql_pt::kDeleteAppGroupNonPrimaryByApplicationId)) { + LOG4CXX_ERROR(logger_, + "Incorrect statement to delete from app_group_non_primary."); + return false; + } + query_np.Bind(0, app_id); + if (!query_np.Exec()) { + LOG4CXX_ERROR(logger_, "Failed deleting from app_group_non_primary."); + return false; + } +#endif // SDL_REMOTE_CONTROL if (!CopyApplication(kDefaultId, app_id)) { return false; @@ -1612,7 +1969,22 @@ bool SQLPTRepresentation::SetDefaultPolicy(const std::string& app_id) { !SaveAppType(app_id, app_types)) { return false; } - return SetIsDefault(app_id, true); + + bool ret = (GatherAppGroup(kDefaultId, &default_groups) && + SaveAppGroup(app_id, default_groups)); +#ifdef SDL_REMOTE_CONTROL + policy_table::Strings groups_primary; + ret = ret && (GatherAppGroupPrimary(kDefaultId, &groups_primary) && + SaveAppGroupPrimary(app_id, groups_primary)); + policy_table::Strings groups_non_primary; + ret = ret && (GatherAppGroupNonPrimary(kDefaultId, &groups_non_primary) && + SaveAppGroupNonPrimary(app_id, groups_non_primary)); +#endif // SDL_REMOTE_CONTROL + + if (ret) { + return SetIsDefault(app_id, true); + } + return false; } bool SQLPTRepresentation::SetIsDefault(const std::string& app_id, |