diff options
author | JackLivio <jack@livio.io> | 2019-02-24 13:45:46 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-24 13:45:46 -0500 |
commit | 13155e460f972f967b2197cf209e2d6417cf89b0 (patch) | |
tree | eb1d28209a17150cb0b6b2239429f4babf70f354 | |
parent | c3f3360bebde3f1c6a52234c94749434cfa69693 (diff) | |
parent | da17adca89fa2cc7429230aae5c179cd3b1f4832 (diff) | |
download | sdl_core-13155e460f972f967b2197cf209e2d6417cf89b0.tar.gz |
Merge pull request #2801 from smartdevicelink/feature/app_service_policies
App Service Policy Implementation
42 files changed, 1193 insertions, 10 deletions
diff --git a/src/components/application_manager/include/application_manager/policies/policy_handler.h b/src/components/application_manager/include/application_manager/policies/policy_handler.h index 8ee5ff64f6..df96ade313 100644 --- a/src/components/application_manager/include/application_manager/policies/policy_handler.h +++ b/src/components/application_manager/include/application_manager/policies/policy_handler.h @@ -447,6 +447,31 @@ class PolicyHandler : public PolicyHandlerInterface, void OnSetCloudAppProperties( const smart_objects::SmartObject& message) OVERRIDE; + /** + * @brief Get app service parameters from the policy table + * @param policy_app_id Unique application id + * @param app_service_parameters Pointer to struct containing all app service + * information + */ + void GetAppServiceParameters(const std::string& policy_app_id, + policy_table::AppServiceParameters* + app_service_parameters) const OVERRIDE; + /** + * @brief Check app service parameters from an app against policies + * @param policy_app_id Unique application id + * @param requested_service_name Service name published by app service + * provider + * @param requested_service_type Service type published by app service + * provider + * @param requested_handled_rpcs Vector of requested function ids an app + * service wants to handle from consumers + */ + bool CheckAppServiceParameters( + const std::string& policy_app_id, + const std::string& requested_service_name, + const std::string& requested_service_type, + smart_objects::SmartArray* requested_handled_rpcs) const OVERRIDE; + virtual void OnUpdateHMIAppType( std::map<std::string, StringArray> app_hmi_types) OVERRIDE; diff --git a/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/on_app_service_data_notification_from_mobile.cc b/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/on_app_service_data_notification_from_mobile.cc index b46bc7934e..1aa8504ff9 100644 --- a/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/on_app_service_data_notification_from_mobile.cc +++ b/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/on_app_service_data_notification_from_mobile.cc @@ -59,6 +59,22 @@ void OnAppServiceDataNotificationFromMobile::Run() { LOG4CXX_AUTO_TRACE(logger_); LOG4CXX_DEBUG(logger_, "Received an OnAppServiceData"); MessageHelper::PrintSmartObject(*message_); + + std::string service_type = + (*message_)[strings::msg_params][strings::app_service_manifest] + [strings::service_type].asString(); + + ApplicationSharedPtr app = application_manager_.application(connection_key()); + + bool result = policy_handler_.CheckAppServiceParameters( + app->policy_app_id(), std::string(), service_type, NULL); + + if (!result) { + LOG4CXX_DEBUG(logger_, + "Incorrect service type received in " + "OnAppServiceDataNotificationFromMobile"); + return; + } } } // namespace commands diff --git a/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/publish_app_service_request.cc b/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/publish_app_service_request.cc index ed60bb14df..f951d2b126 100644 --- a/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/publish_app_service_request.cc +++ b/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/publish_app_service_request.cc @@ -84,6 +84,41 @@ void PublishAppServiceRequest::Run() { } ApplicationSharedPtr app = application_manager_.application(connection_key()); + + std::string requested_service_name = ""; + + if ((*message_)[strings::msg_params][strings::app_service_manifest].keyExists( + strings::service_name)) { + requested_service_name = + (*message_)[strings::msg_params][strings::app_service_manifest] + [strings::service_name].asString(); + } + + std::string requested_service_type = + (*message_)[strings::msg_params][strings::app_service_manifest] + [strings::service_type].asString(); + + smart_objects::SmartArray* requested_handled_rpcs = NULL; + if ((*message_)[strings::msg_params][strings::app_service_manifest].keyExists( + strings::handled_rpcs)) { + requested_handled_rpcs = + (*message_)[strings::msg_params][strings::app_service_manifest] + [strings::handled_rpcs].asArray(); + } + + bool result = + policy_handler_.CheckAppServiceParameters(app->policy_app_id(), + requested_service_name, + requested_service_type, + requested_handled_rpcs); + + if (!result) { + SendResponse(false, + mobile_apis::Result::DISALLOWED, + "Service disallowed by policies", + NULL); + } + auto& ext = sdl_rpc_plugin::SystemCapabilityAppExtension::ExtractExtension(*app); ext.SubscribeTo(mobile_apis::SystemCapabilityType::APP_SERVICES); diff --git a/src/components/application_manager/src/policies/policy_handler.cc b/src/components/application_manager/src/policies/policy_handler.cc index 3be3ef0da7..c267ac5de9 100644 --- a/src/components/application_manager/src/policies/policy_handler.cc +++ b/src/components/application_manager/src/policies/policy_handler.cc @@ -1937,6 +1937,74 @@ void PolicyHandler::OnSetCloudAppProperties( } } +void PolicyHandler::GetAppServiceParameters( + const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters) const { + POLICY_LIB_CHECK_VOID(); + policy_manager_->GetAppServiceParameters(policy_app_id, + app_service_parameters); +} + +bool PolicyHandler::CheckAppServiceParameters( + const std::string& policy_app_id, + const std::string& requested_service_name, + const std::string& requested_service_type, + smart_objects::SmartArray* requested_handled_rpcs) const { + std::string service_name = std::string(); + std::string service_type = std::string(); + std::vector<int32_t> handled_rpcs = {}; + + policy_table::AppServiceParameters app_service_parameters = + policy_table::AppServiceParameters(); + this->GetAppServiceParameters(policy_app_id, &app_service_parameters); + + if (app_service_parameters.find(requested_service_type) == + app_service_parameters.end()) { + LOG4CXX_DEBUG(logger_, + "Disallowed service type: " << requested_service_type); + return false; + } + + auto service_names = + *(app_service_parameters[requested_service_type].service_names); + if (!service_names.is_initialized()) { + LOG4CXX_DEBUG(logger_, + "Pt Service Name is Null, All service names accepted"); + } else if (!requested_service_name.empty()) { + auto find_name_result = + std::find(service_names.begin(), + service_names.end(), + rpc::String<0, 255>(requested_service_name)); + if (find_name_result == service_names.end()) { + LOG4CXX_DEBUG(logger_, + "Disallowed service name: " << requested_service_name); + return false; + } + } + + if (requested_handled_rpcs) { + auto temp_rpcs = + app_service_parameters[requested_service_type].handled_rpcs; + for (auto handled_it = temp_rpcs.begin(); handled_it != temp_rpcs.end(); + ++handled_it) { + handled_rpcs.push_back(handled_it->function_id); + } + + for (auto requested_it = requested_handled_rpcs->begin(); + requested_it != requested_handled_rpcs->end(); + ++requested_it) { + auto find_result = std::find( + handled_rpcs.begin(), handled_rpcs.end(), requested_it->asInt()); + if (find_result == handled_rpcs.end()) { + LOG4CXX_DEBUG(logger_, + "Disallowed by handled rpc: " << requested_it->asInt()); + return false; + } + } + } + return true; +} + uint32_t PolicyHandler::HeartBeatTimeout(const std::string& app_id) const { POLICY_LIB_CHECK(0); return policy_manager_->HeartBeatTimeout(app_id); diff --git a/src/components/application_manager/test/message_helper/CMakeLists.txt b/src/components/application_manager/test/message_helper/CMakeLists.txt index d115ad8b40..99031849f6 100755 --- a/src/components/application_manager/test/message_helper/CMakeLists.txt +++ b/src/components/application_manager/test/message_helper/CMakeLists.txt @@ -40,6 +40,7 @@ set(LIBRARIES ApplicationManager MessageHelper jsoncpp + Policy connectionHandler MediaManager Resumption diff --git a/src/components/application_manager/test/message_helper/message_helper_test.cc b/src/components/application_manager/test/message_helper/message_helper_test.cc index 15a09c33cc..b813c36bb4 100644 --- a/src/components/application_manager/test/message_helper/message_helper_test.cc +++ b/src/components/application_manager/test/message_helper/message_helper_test.cc @@ -49,6 +49,9 @@ #include "application_manager/state_controller.h" #include "application_manager/resumption/resume_ctrl.h" +#include "policy/policy_table/types.h" +#include "rpc_base/rpc_base_json_inl.h" + #ifdef EXTERNAL_PROPRIETARY_MODE #include "policy/policy_external/include/policy/policy_types.h" #endif diff --git a/src/components/include/application_manager/policies/policy_handler_interface.h b/src/components/include/application_manager/policies/policy_handler_interface.h index 8c8a12ced3..64f0b893b2 100644 --- a/src/components/include/application_manager/policies/policy_handler_interface.h +++ b/src/components/include/application_manager/policies/policy_handler_interface.h @@ -488,6 +488,32 @@ class PolicyHandlerInterface { virtual void OnSetCloudAppProperties( const smart_objects::SmartObject& message) = 0; + /** + * @brief Get app service parameters from the policy table + * @param policy_app_id Unique application id + * @param app_service_parameters Pointer to struct containing all app service + * information + */ + virtual void GetAppServiceParameters( + const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters) const = 0; + + /** + * @brief Check app service parameters from an app against policies + * @param policy_app_id Unique application id + * @param requested_service_name Service name published by app service + * provider + * @param requested_service_type Service type published by app service + * provider + * @param requested_handled_rpcs Vector of requested function ids an app + * service wants to handle from consumers + */ + virtual bool CheckAppServiceParameters( + const std::string& policy_app_id, + const std::string& requested_service_name, + const std::string& requested_service_type, + smart_objects::SmartArray* requested_handled_rpcs) const = 0; + #ifdef EXTERNAL_PROPRIETARY_MODE /** * @brief Gets meta information diff --git a/src/components/include/policy/policy_external/policy/policy_manager.h b/src/components/include/policy/policy_external/policy/policy_manager.h index 92c0305279..e7aefe9f6c 100644 --- a/src/components/include/policy/policy_external/policy/policy_manager.h +++ b/src/components/include/policy/policy_external/policy/policy_manager.h @@ -593,6 +593,16 @@ class PolicyManager : public usage_statistics::StatisticsManager { const std::string& hybrid_app_preference) = 0; /** + * @brief Get app service parameters from the policy table + * @param policy_app_id Unique application id + * @param app_service_parameters Pointer to struct containing all app service + * information + */ + virtual void GetAppServiceParameters( + const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters) const = 0; + + /** * @brief Gets meta information * @return meta information */ diff --git a/src/components/include/policy/policy_regular/policy/policy_manager.h b/src/components/include/policy/policy_regular/policy/policy_manager.h index b2d48585f4..e4ee95fda0 100644 --- a/src/components/include/policy/policy_regular/policy/policy_manager.h +++ b/src/components/include/policy/policy_regular/policy/policy_manager.h @@ -574,6 +574,16 @@ class PolicyManager : public usage_statistics::StatisticsManager { const std::string& hybrid_app_preference) = 0; /** + * @brief Get app service parameters from the policy table + * @param policy_app_id Unique application id + * @param app_service_parameters Pointer to struct containing all app service + * information + */ + virtual void GetAppServiceParameters( + const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters) const = 0; + + /** * @brief OnAppRegisteredOnMobile allows to handle event when application were * succesfully registered on mobile device. * It will send OnAppPermissionSend notification and will try to start PTU. * diff --git a/src/components/include/test/application_manager/policies/mock_policy_handler_interface.h b/src/components/include/test/application_manager/policies/mock_policy_handler_interface.h index 02a0d73086..1db0338caf 100644 --- a/src/components/include/test/application_manager/policies/mock_policy_handler_interface.h +++ b/src/components/include/test/application_manager/policies/mock_policy_handler_interface.h @@ -227,6 +227,15 @@ class MockPolicyHandlerInterface : public policy::PolicyHandlerInterface { std::string& hybrid_app_preference)); MOCK_METHOD1(OnSetCloudAppProperties, void(const smart_objects::SmartObject& message)); + MOCK_CONST_METHOD2( + GetAppServiceParameters, + void(const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters)); + MOCK_CONST_METHOD4(CheckAppServiceParameters, + bool(const std::string& policy_app_id, + const std::string& requested_service_name, + const std::string& requested_service_type, + smart_objects::SmartArray* requested_handled_rpcs)); #ifdef EXTERNAL_PROPRIETARY_MODE MOCK_CONST_METHOD0(GetMetaInfo, const policy::MetaInfo()); diff --git a/src/components/include/test/policy/policy_external/policy/mock_cache_manager.h b/src/components/include/test/policy/policy_external/policy/mock_cache_manager.h index da2b4cc2a1..25c391205d 100644 --- a/src/components/include/test/policy/policy_external/policy/mock_cache_manager.h +++ b/src/components/include/test/policy/policy_external/policy/mock_cache_manager.h @@ -98,6 +98,10 @@ class MockCacheManagerInterface : public ::policy::CacheManagerInterface { MOCK_METHOD2(SetHybridAppPreference, void(const std::string& policy_app_id, const std::string& hybrid_app_preference)); + MOCK_CONST_METHOD2( + GetAppServiceParameters, + void(const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters)); MOCK_CONST_METHOD1(GetDeviceConsent, DeviceConsent(const std::string& device_id)); MOCK_METHOD2(SetDeviceConsent, diff --git a/src/components/include/test/policy/policy_external/policy/mock_policy_manager.h b/src/components/include/test/policy/policy_external/policy/mock_policy_manager.h index 72dcb147e3..7c47202496 100644 --- a/src/components/include/test/policy/policy_external/policy/mock_policy_manager.h +++ b/src/components/include/test/policy/policy_external/policy/mock_policy_manager.h @@ -206,6 +206,10 @@ class MockPolicyManager : public PolicyManager { MOCK_METHOD2(SetHybridAppPreference, void(const std::string& policy_app_id, const std::string& hybrid_app_preference)); + MOCK_CONST_METHOD2( + GetAppServiceParameters, + void(const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters)); MOCK_CONST_METHOD0(GetMetaInfo, const policy::MetaInfo()); MOCK_CONST_METHOD0(RetrieveCertificate, std::string()); MOCK_CONST_METHOD0(HasCertificate, bool()); diff --git a/src/components/include/test/policy/policy_regular/policy/mock_cache_manager.h b/src/components/include/test/policy/policy_regular/policy/mock_cache_manager.h index 24fe9b2393..c8bff0bbc0 100644 --- a/src/components/include/test/policy/policy_regular/policy/mock_cache_manager.h +++ b/src/components/include/test/policy/policy_regular/policy/mock_cache_manager.h @@ -90,6 +90,10 @@ class MockCacheManagerInterface : public CacheManagerInterface { std::vector<UserFriendlyMessage>( const std::vector<std::string>& msg_codes, const std::string& language)); + MOCK_CONST_METHOD2( + GetAppServiceParameters, + void(const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters)); MOCK_METHOD1( GetNotificationsNumber, diff --git a/src/components/include/test/policy/policy_regular/policy/mock_policy_manager.h b/src/components/include/test/policy/policy_regular/policy/mock_policy_manager.h index a5ce488e1a..fe0a301a58 100644 --- a/src/components/include/test/policy/policy_regular/policy/mock_policy_manager.h +++ b/src/components/include/test/policy/policy_regular/policy/mock_policy_manager.h @@ -203,6 +203,10 @@ class MockPolicyManager : public PolicyManager { MOCK_METHOD2(SetHybridAppPreference, void(const std::string& policy_app_id, const std::string& hybrid_app_preference)); + MOCK_CONST_METHOD2( + GetAppServiceParameters, + void(const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters)); MOCK_CONST_METHOD0(GetMetaInfo, const policy::MetaInfo()); MOCK_CONST_METHOD0(RetrieveCertificate, std::string()); MOCK_CONST_METHOD0(HasCertificate, bool()); diff --git a/src/components/policy/policy_external/include/policy/cache_manager.h b/src/components/policy/policy_external/include/policy/cache_manager.h index 3344b149e4..3813b58ca3 100644 --- a/src/components/policy/policy_external/include/policy/cache_manager.h +++ b/src/components/policy/policy_external/include/policy/cache_manager.h @@ -229,6 +229,16 @@ class CacheManager : public CacheManagerInterface { const std::string& hybrid_app_preference); /** + * @brief Get app service parameters from the policy table + * @param policy_app_id Unique application id + * @param app_service_parameters Pointer to struct containing all app service + * information + */ + virtual void GetAppServiceParameters( + const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters) const; + + /** * @brief Allows to update 'vin' field in module_meta table. * * @param new 'vin' value. diff --git a/src/components/policy/policy_external/include/policy/cache_manager_interface.h b/src/components/policy/policy_external/include/policy/cache_manager_interface.h index 6a172e6f4b..21c8a947cc 100644 --- a/src/components/policy/policy_external/include/policy/cache_manager_interface.h +++ b/src/components/policy/policy_external/include/policy/cache_manager_interface.h @@ -238,6 +238,16 @@ class CacheManagerInterface { const std::string& hybrid_app_preference) = 0; /** + * @brief Get app service parameters from the policy table + * @param policy_app_id Unique application id + * @param app_service_parameters Pointer to struct containing all app service + * information + */ + virtual void GetAppServiceParameters( + const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters) const = 0; + + /** * @brief Allows to update 'vin' field in module_meta table. * * @param new 'vin' value. diff --git a/src/components/policy/policy_external/include/policy/policy_manager_impl.h b/src/components/policy/policy_external/include/policy/policy_manager_impl.h index 399c95170d..9cf067c0ec 100644 --- a/src/components/policy/policy_external/include/policy/policy_manager_impl.h +++ b/src/components/policy/policy_external/include/policy/policy_manager_impl.h @@ -640,6 +640,16 @@ class PolicyManagerImpl : public PolicyManager { const std::string& hybrid_app_preference) OVERRIDE; /** + * @brief Get app service parameters from the policy table + * @param policy_app_id Unique application id + * @param app_service_parameters Pointer to struct containing all app service + * information + */ + void GetAppServiceParameters(const std::string& policy_app_id, + policy_table::AppServiceParameters* + app_service_parameters) const OVERRIDE; + + /** * @brief OnAppRegisteredOnMobile allows to handle event when application were * succesfully registered on mobile device. * It will send OnAppPermissionSend notification and will try to start PTU. * diff --git a/src/components/policy/policy_external/include/policy/policy_table/types.h b/src/components/policy/policy_external/include/policy/policy_table/types.h index f37531eb63..0edfa55ab8 100644 --- a/src/components/policy/policy_external/include/policy/policy_table/types.h +++ b/src/components/policy/policy_external/include/policy/policy_table/types.h @@ -52,6 +52,8 @@ struct MessageLanguages; struct MessageString; struct RpcParameters; struct Rpcs; +struct AppServiceHandledRpc; +struct AppServiceInfo; } // namespace policy_table_interface_base } // namespace rpc @@ -104,6 +106,13 @@ typedef Array<Enum<RequestType>, 0, 255> RequestsTypeArray; typedef Strings RequestSubTypes; +typedef String<0, 255> AppServiceType; +typedef String<0, 255> AppServiceName; +typedef Array<AppServiceName, 0, 255> AppServiceNames; +typedef Array<AppServiceHandledRpc, 0, 255> AppServiceHandledRpcs; +typedef Map<AppServiceInfo, 0, 255> AppServiceParameters; +typedef Integer<int32_t, 0, INT32_MAX> FunctionIDInt; + typedef Map<Strings, 0, 255> RemoteRpcs; typedef Map<RemoteRpcs, 0, 255> AccessModules; typedef Array<Enum<ModuleType>, 0, 255> ModuleTypes; @@ -111,6 +120,45 @@ typedef Array<Enum<ModuleType>, 0, 255> ModuleTypes; typedef AppHMIType AppHmiType; typedef std::vector<AppHMIType> AppHmiTypes; +struct AppServiceHandledRpc : CompositeType { + public: + FunctionIDInt function_id; + + public: + AppServiceHandledRpc(); + ~AppServiceHandledRpc(); + AppServiceHandledRpc(const Json::Value* value__); + Json::Value ToJsonValue() const; + bool is_valid() const; + bool is_initialized() const; + bool struct_empty() const; + virtual void SetPolicyTableType(PolicyTableType pt_type); + void ReportErrors(rpc::ValidationReport* report__) const; + + private: + bool Validate() const; +}; + +struct AppServiceInfo : CompositeType { + public: + Optional<AppServiceNames> service_names; + AppServiceHandledRpcs handled_rpcs; + + public: + AppServiceInfo(); + ~AppServiceInfo(); + AppServiceInfo(const Json::Value* value__); + Json::Value ToJsonValue() const; + bool is_valid() const; + bool is_initialized() const; + bool struct_empty() const; + virtual void SetPolicyTableType(PolicyTableType pt_type); + void ReportErrors(rpc::ValidationReport* report__) const; + + private: + bool Validate() const; +}; + struct RequestTypes : public RequestsTypeArray { RequestTypes(); explicit RequestTypes(Json::Value* value); @@ -185,6 +233,9 @@ struct ApplicationParams : PolicyBase { Optional<String<0, 65535> > auth_token; Optional<String<0, 255> > cloud_transport_type; + // App Service Params + Optional<AppServiceParameters> app_service_parameters; + public: ApplicationParams(); ApplicationParams(const Strings& groups, diff --git a/src/components/policy/policy_external/include/policy/policy_table_interface_ext.xml b/src/components/policy/policy_external/include/policy/policy_table_interface_ext.xml index 5ed852cf45..7749c4ee8f 100644 --- a/src/components/policy/policy_external/include/policy/policy_table_interface_ext.xml +++ b/src/components/policy/policy_external/include/policy/policy_table_interface_ext.xml @@ -108,6 +108,9 @@ <param name="cloud_transport_type" type="String" minlength="0" maxlength="255" mandatory="false" /> <param name="hybrid_app_preference" type="HybridAppPreference" mandatory="false" /> + <param name="service_name" type="String" mandatory="false" /> + <param name="service_type" type="String" mandatory="false" /> + <param name="handled_rpcs" array="true" mandatory="false"> </struct> <typedef name="HmiLevels" type="HmiLevel" array="true" diff --git a/src/components/policy/policy_external/include/policy/sql_pt_queries.h b/src/components/policy/policy_external/include/policy/sql_pt_queries.h index dd9e0204e7..5382ff2d6b 100644 --- a/src/components/policy/policy_external/include/policy/sql_pt_queries.h +++ b/src/components/policy/policy_external/include/policy/sql_pt_queries.h @@ -66,6 +66,9 @@ extern const std::string kSelectNicknames; extern const std::string kSelectAppTypes; extern const std::string kSelectRequestTypes; extern const std::string kSelectRequestSubTypes; +extern const std::string kSelectAppServiceTypes; +extern const std::string kSelectAppServiceNames; +extern const std::string kSelectAppServiceHandledRpcs; extern const std::string kSelectSecondsBetweenRetries; extern const std::string kSelectIgnitionCycles; extern const std::string kSelectKilometers; @@ -80,6 +83,9 @@ extern const std::string kInsertNickname; extern const std::string kInsertAppType; extern const std::string kInsertRequestType; extern const std::string kInsertRequestSubType; +extern const std::string kInsertAppServiceTypes; +extern const std::string kInsertAppServiceNames; +extern const std::string kInsertAppServiceHandledRpcs; extern const std::string kInsertMessageType; extern const std::string kInsertLanguage; extern const std::string kInsertMessageString; @@ -101,6 +107,9 @@ extern const std::string kDeleteAppGroup; extern const std::string kDeleteApplication; extern const std::string kDeleteRequestType; extern const std::string kDeleteRequestSubType; +extern const std::string kDeleteAppServiceTypes; +extern const std::string kDeleteAppServiceNames; +extern const std::string kDeleteAppServiceHandledRpcs; extern const std::string kDeleteDevice; extern const std::string kIncrementIgnitionCycles; extern const std::string kResetIgnitionCycles; diff --git a/src/components/policy/policy_external/include/policy/sql_pt_representation.h b/src/components/policy/policy_external/include/policy/sql_pt_representation.h index 13816eb6c5..581340ba02 100644 --- a/src/components/policy/policy_external/include/policy/sql_pt_representation.h +++ b/src/components/policy/policy_external/include/policy/sql_pt_representation.h @@ -134,6 +134,9 @@ class SQLPTRepresentation : public virtual PTRepresentation { bool GatherRequestSubType( const std::string& app_id, policy_table::RequestSubTypes* request_subtypes) const; + bool GatherAppServiceParameters( + const std::string& app_id, + policy_table::AppServiceParameters* app_service_parameters) const; bool GatherNickName(const std::string& app_id, policy_table::Strings* nicknames) const; @@ -172,6 +175,9 @@ class SQLPTRepresentation : public virtual PTRepresentation { bool SaveRequestSubType( const std::string& app_id, const policy_table::RequestSubTypes& request_subtypes); + bool SaveAppServiceParameters( + const std::string& app_id, + const policy_table::AppServiceParameters& app_service_parameters); public: virtual std::string GetLockScreenIconUrl() const; diff --git a/src/components/policy/policy_external/src/cache_manager.cc b/src/components/policy/policy_external/src/cache_manager.cc index 8cec6ef91c..6d400cd0b4 100644 --- a/src/components/policy/policy_external/src/cache_manager.cc +++ b/src/components/policy/policy_external/src/cache_manager.cc @@ -1501,6 +1501,22 @@ void CacheManager::SetHybridAppPreference( } } +void CacheManager::GetAppServiceParameters( + const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters) const { + LOG4CXX_AUTO_TRACE(logger_); + const policy_table::ApplicationPolicies& policies = + pt_->policy_table.app_policies_section.apps; + policy_table::ApplicationPolicies::const_iterator policy_iter = + policies.find(policy_app_id); + if (policies.end() != policy_iter) { + auto app_policy = (*policy_iter).second; + if (app_policy.app_service_parameters.is_initialized()) { + *app_service_parameters = *(app_policy.app_service_parameters); + } + } +} + std::vector<UserFriendlyMessage> CacheManager::GetUserFriendlyMsg( const std::vector<std::string>& msg_codes, const std::string& language, diff --git a/src/components/policy/policy_external/src/policy_manager_impl.cc b/src/components/policy/policy_external/src/policy_manager_impl.cc index 0844edd8eb..404c48d2e4 100644 --- a/src/components/policy/policy_external/src/policy_manager_impl.cc +++ b/src/components/policy/policy_external/src/policy_manager_impl.cc @@ -772,6 +772,12 @@ void PolicyManagerImpl::SetHybridAppPreference( cache_->SetHybridAppPreference(policy_app_id, hybrid_app_preference); } +void PolicyManagerImpl::GetAppServiceParameters( + const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters) const { + cache_->GetAppServiceParameters(policy_app_id, app_service_parameters); +} + void PolicyManagerImpl::CheckPermissions(const PTString& app_id, const PTString& hmi_level, const PTString& rpc, diff --git a/src/components/policy/policy_external/src/policy_table/types.cc b/src/components/policy/policy_external/src/policy_table/types.cc index 977448dacd..db28b5b4fb 100644 --- a/src/components/policy/policy_external/src/policy_table/types.cc +++ b/src/components/policy/policy_external/src/policy_table/types.cc @@ -221,6 +221,110 @@ void ApplicationPoliciesSection::SetPolicyTableType(PolicyTableType pt_type) { apps.SetPolicyTableType(pt_type); } +// Handled RPC Methods +AppServiceHandledRpc::AppServiceHandledRpc() : CompositeType(kUninitialized) {} + +AppServiceHandledRpc::~AppServiceHandledRpc() {} + +AppServiceHandledRpc::AppServiceHandledRpc(const Json::Value* value__) + : CompositeType(InitHelper(value__, &Json::Value::isObject)) + , function_id(impl::ValueMember(value__, "function_id")) {} + +Json::Value AppServiceHandledRpc::ToJsonValue() const { + Json::Value result__(Json::objectValue); + impl::WriteJsonField("function_id", function_id, &result__); + return result__; +} + +bool AppServiceHandledRpc::is_valid() const { + if (!function_id.is_valid()) { + return false; + } + return Validate(); +} + +bool AppServiceHandledRpc::is_initialized() const { + return (initialization_state__ != kUninitialized) || (!struct_empty()); +} + +bool AppServiceHandledRpc::struct_empty() const { + if (function_id.is_initialized()) { + return false; + } + return true; +} + +void AppServiceHandledRpc::SetPolicyTableType(PolicyTableType pt_type) { + function_id.SetPolicyTableType(pt_type); +} + +void AppServiceHandledRpc::ReportErrors(rpc::ValidationReport* report__) const { + if (struct_empty()) { + rpc::CompositeType::ReportErrors(report__); + } + if (!function_id.is_valid()) { + function_id.ReportErrors(&report__->ReportSubobject("function_id")); + } +} + +// AppServiceInfo methods +AppServiceInfo::AppServiceInfo() : CompositeType(kUninitialized) {} + +AppServiceInfo::~AppServiceInfo() {} + +AppServiceInfo::AppServiceInfo(const Json::Value* value__) + : CompositeType(InitHelper(value__, &Json::Value::isObject)) + , service_names(impl::ValueMember(value__, "service_names")) + , handled_rpcs(impl::ValueMember(value__, "handled_rpcs")) {} + +Json::Value AppServiceInfo::ToJsonValue() const { + Json::Value result__(Json::objectValue); + impl::WriteJsonField("service_names", service_names, &result__); + impl::WriteJsonField("parameters", handled_rpcs, &result__); + return result__; +} + +bool AppServiceInfo::is_valid() const { + if (!service_names.is_valid()) { + return false; + } + if (!handled_rpcs.is_valid()) { + return false; + } + return Validate(); +} + +bool AppServiceInfo::is_initialized() const { + return (initialization_state__ != kUninitialized) || (!struct_empty()); +} + +bool AppServiceInfo::struct_empty() const { + if (service_names.is_initialized()) { + return false; + } + if (handled_rpcs.is_initialized()) { + return false; + } + return true; +} + +void AppServiceInfo::SetPolicyTableType(PolicyTableType pt_type) { + service_names.SetPolicyTableType(pt_type); + handled_rpcs.SetPolicyTableType(pt_type); +} + +void AppServiceInfo::ReportErrors(rpc::ValidationReport* report__) const { + if (struct_empty()) { + rpc::CompositeType::ReportErrors(report__); + } + if (!service_names.is_valid()) { + service_names.ReportErrors(&report__->ReportSubobject("service_names")); + } + if (!handled_rpcs.is_valid()) { + handled_rpcs.ReportErrors(&report__->ReportSubobject("handled_rpcs")); + } +} + // ApplicationParams methods ApplicationParams::ApplicationParams() : PolicyBase() {} @@ -247,8 +351,8 @@ ApplicationParams::ApplicationParams(const Json::Value* value__) , endpoint(impl::ValueMember(value__, "endpoint")) , enabled(impl::ValueMember(value__, "enabled")) , auth_token(impl::ValueMember(value__, "auth_token")) - , cloud_transport_type(impl::ValueMember(value__, "cloud_transport_type")) { -} + , cloud_transport_type(impl::ValueMember(value__, "cloud_transport_type")) + , app_service_parameters(impl::ValueMember(value__, "app_services")) {} Json::Value ApplicationParams::ToJsonValue() const { Json::Value result__(PolicyBase::ToJsonValue()); @@ -267,6 +371,7 @@ Json::Value ApplicationParams::ToJsonValue() const { impl::WriteJsonField("enabled", enabled, &result__); impl::WriteJsonField("auth_token", auth_token, &result__); impl::WriteJsonField("cloud_transport_type", cloud_transport_type, &result__); + impl::WriteJsonField("app_services", app_service_parameters, &result__); return result__; } @@ -309,6 +414,9 @@ bool ApplicationParams::is_valid() const { if (!hybrid_app_preference.is_valid()) { return false; } + if (!app_service_parameters.is_valid()) { + return false; + } return Validate(); } @@ -359,6 +467,9 @@ bool ApplicationParams::struct_empty() const { if (hybrid_app_preference.is_initialized()) { return false; } + if (app_service_parameters.is_initialized()) { + return false; + } return true; } @@ -426,6 +537,10 @@ void ApplicationParams::ReportErrors(rpc::ValidationReport* report__) const { moduleType.ReportErrors( &report__->ReportSubobject("hybrid_app_preference")); } + if (!app_service_parameters.is_valid()) { + app_service_parameters.ReportErrors( + &report__->ReportSubobject("app_services")); + } } void ApplicationParams::SetPolicyTableType(PolicyTableType pt_type) { @@ -441,6 +556,7 @@ void ApplicationParams::SetPolicyTableType(PolicyTableType pt_type) { enabled.SetPolicyTableType(pt_type); cloud_transport_type.SetPolicyTableType(pt_type); hybrid_app_preference.SetPolicyTableType(pt_type); + app_service_parameters.SetPolicyTableType(pt_type); } // RpcParameters methods diff --git a/src/components/policy/policy_external/src/policy_table/validation.cc b/src/components/policy/policy_external/src/policy_table/validation.cc index 9b227615d8..fad8509711 100644 --- a/src/components/policy/policy_external/src/policy_table/validation.cc +++ b/src/components/policy/policy_external/src/policy_table/validation.cc @@ -194,6 +194,14 @@ bool ApplicationParams::ValidateModuleTypes() const { return true; } +bool AppServiceHandledRpc::Validate() const { + return true; +} + +bool AppServiceInfo::Validate() const { + return true; +} + bool ApplicationParams::Validate() const { if (is_initialized()) { if (preconsented_groups.is_initialized()) { diff --git a/src/components/policy/policy_external/src/sql_pt_ext_representation.cc b/src/components/policy/policy_external/src/sql_pt_ext_representation.cc index 43961815d6..6e0da9e5de 100644 --- a/src/components/policy/policy_external/src/sql_pt_ext_representation.cc +++ b/src/components/policy/policy_external/src/sql_pt_ext_representation.cc @@ -675,6 +675,21 @@ bool SQLPTExtRepresentation::SaveApplicationPoliciesSection( return false; } + if (!query_delete.Exec(sql_pt::kDeleteAppServiceHandledRpcs)) { + LOG4CXX_WARN(logger_, "Incorrect delete from handled rpcs."); + return false; + } + + if (!query_delete.Exec(sql_pt::kDeleteAppServiceNames)) { + LOG4CXX_WARN(logger_, "Incorrect delete from service names."); + return false; + } + + if (!query_delete.Exec(sql_pt::kDeleteAppServiceTypes)) { + LOG4CXX_WARN(logger_, "Incorrect delete from handled service types."); + return false; + } + // First, all predefined apps (e.g. default, pre_DataConsent) should be saved, // otherwise another app with the predefined permissions can get incorrect // permissions @@ -729,6 +744,10 @@ bool SQLPTExtRepresentation::SaveSpecificAppPolicy( if (!SaveRequestSubType(app.first, *app.second.RequestSubType)) { return false; } + if (!SaveAppServiceParameters(app.first, + *app.second.app_service_parameters)) { + return false; + } // Stop saving other params, since predefined permissions already set return true; } @@ -805,6 +824,11 @@ bool SQLPTExtRepresentation::SaveSpecificAppPolicy( return false; } + if (!SaveAppServiceParameters(app.first, + *app.second.app_service_parameters)) { + return false; + } + return true; } @@ -941,6 +965,10 @@ bool SQLPTExtRepresentation::GatherApplicationPoliciesSection( if (!GatherRequestSubType(gather_app_id, &*params.RequestSubType)) { return false; } + if (!GatherAppServiceParameters(gather_app_id, + &*params.app_service_parameters)) { + return false; + } GatherPreconsentedGroup(gather_app_id, &*params.preconsented_groups); (*policies).apps[app_id] = params; } diff --git a/src/components/policy/policy_external/src/sql_pt_queries.cc b/src/components/policy/policy_external/src/sql_pt_queries.cc index 162d6957e7..51f4e4c13e 100644 --- a/src/components/policy/policy_external/src/sql_pt_queries.cc +++ b/src/components/policy/policy_external/src/sql_pt_queries.cc @@ -301,6 +301,30 @@ const std::string kCreateSchema = " FOREIGN KEY(`application_id`) " " REFERENCES `application`(`id`) " "); " + "CREATE TABLE IF NOT EXISTS `app_service_types`( " + " `id` INTEGER PRIMARY KEY NOT NULL, " + " `service_type` VARCHAR(50), " + " `application_id` VARCHAR(45) NOT NULL COLLATE NOCASE, " + " CONSTRAINT `fk_service_type_app_id` " + " FOREIGN KEY(`application_id`) " + " REFERENCES `application`(`id`) " + "); " + "CREATE TABLE IF NOT EXISTS `app_service_names`( " + " `service_type_id` INTEGER NOT NULL, " + " `service_name` VARCHAR(45), " + " PRIMARY KEY(`service_name`,`service_type_id`), " + " CONSTRAINT `fk_service_name_service_type_id` " + " FOREIGN KEY(`service_type_id`) " + " REFERENCES `app_service_types`(`id`) " + "); " + "CREATE TABLE IF NOT EXISTS `app_service_handled_rpcs`( " + " `service_type_id` INTEGER NOT NULL, " + " `function_id` INTEGER, " + " PRIMARY KEY(`function_id`,`service_type_id`), " + " CONSTRAINT `fk_function_id_service_type_id` " + " FOREIGN KEY(`service_type_id`) " + " REFERENCES `app_service_types`(`id`) " + "); " "CREATE INDEX IF NOT EXISTS `app_type.fk_app_type_application1_idx` " " ON `app_type`(`application_id` COLLATE NOCASE); " "CREATE TABLE IF NOT EXISTS `consent_group`( " @@ -509,6 +533,9 @@ const std::string kDropSchema = "DROP TABLE IF EXISTS `app_type`; " "DROP TABLE IF EXISTS `request_type`; " "DROP TABLE IF EXISTS `request_subtype`; " + "DROP TABLE IF EXISTS `app_service_types`; " + "DROP TABLE IF EXISTS `app_service_names`; " + "DROP TABLE IF EXISTS `app_service_handled_rpcs`; " "DROP INDEX IF EXISTS `nickname.fk_nickname_application1_idx`; " "DROP TABLE IF EXISTS `nickname`; " "DROP INDEX IF EXISTS `app_level.fk_app_level_language2_idx`; " @@ -671,6 +698,21 @@ const std::string kInsertRequestSubType = "`request_subtype`) " "VALUES (?, ?)"; +const std::string kInsertAppServiceTypes = + "INSERT INTO `app_service_types` (`id`, " + "`service_type`, `application_id`) " + "VALUES (?, ?, ?)"; + +const std::string kInsertAppServiceNames = + "INSERT INTO `app_service_names` (`service_type_id`, " + "`service_name`) " + "VALUES (?, ?)"; + +const std::string kInsertAppServiceHandledRpcs = + "INSERT INTO `app_service_handled_rpcs` (`service_type_id`, " + "`function_id`) " + "VALUES (?, ?)"; + const std::string kUpdateVersion = "UPDATE `version` SET `number`= ?"; const std::string kInsertMessageType = @@ -802,6 +844,21 @@ const std::string kSelectRequestSubTypes = "`application_id` " "= ?"; +const std::string kSelectAppServiceTypes = + "SELECT `id`, `service_type` FROM `app_service_types` WHERE " + "`application_id` " + "= ?"; + +const std::string kSelectAppServiceNames = + "SELECT DISTINCT `service_name` FROM `app_service_names` WHERE " + "`service_type_id` " + "= ?"; + +const std::string kSelectAppServiceHandledRpcs = + "SELECT DISTINCT `function_id` FROM `app_service_handled_rpcs` WHERE " + "`service_type_id` " + "= ?"; + const std::string kSelectSecondsBetweenRetries = "SELECT `value` FROM `seconds_between_retry` ORDER BY `index`"; @@ -849,6 +906,13 @@ const std::string kDeleteRequestType = "DELETE FROM `request_type`"; const std::string kDeleteRequestSubType = "DELETE FROM `request_subtype`"; +const std::string kDeleteAppServiceTypes = "DELETE FROM `app_service_types`"; + +const std::string kDeleteAppServiceNames = "DELETE FROM `app_service_names`"; + +const std::string kDeleteAppServiceHandledRpcs = + "DELETE FROM `app_service_handled_rpcs`"; + const std::string kSelectApplicationRevoked = "SELECT `is_revoked` FROM `application` WHERE `id` = ?"; @@ -882,7 +946,7 @@ const std::string kSelectApplicationFull = "SELECT `keep_context`, `steal_focus`, `default_hmi`, `priority_value`, " " `is_revoked`, `is_default`, `is_predata`, `memory_kb`," " `heart_beat_timeout_ms`, `certificate`, `hybrid_app_preference_value`, " - " `endpoint`, `enabled`, `auth_token`, `cloud_transport_type` " + " `endpoint`, `enabled`, `auth_token`, `cloud_transport_type`" "FROM `application` " "WHERE `id` = ?"; diff --git a/src/components/policy/policy_external/src/sql_pt_representation.cc b/src/components/policy/policy_external/src/sql_pt_representation.cc index b794d4c3a6..0b60daa0e1 100644 --- a/src/components/policy/policy_external/src/sql_pt_representation.cc +++ b/src/components/policy/policy_external/src/sql_pt_representation.cc @@ -810,6 +810,10 @@ bool SQLPTRepresentation::GatherApplicationPoliciesSection( if (!GatherRequestSubType(gather_app_id, &*params.RequestSubType)) { return false; } + if (!GatherAppServiceParameters(gather_app_id, + &*params.app_service_parameters)) { + return false; + } (*policies).apps[app_id] = params; } @@ -1001,6 +1005,21 @@ bool SQLPTRepresentation::SaveApplicationPoliciesSection( return false; } + if (!query_delete.Exec(sql_pt::kDeleteAppServiceHandledRpcs)) { + LOG4CXX_WARN(logger_, "Incorrect delete from handled rpcs."); + return false; + } + + if (!query_delete.Exec(sql_pt::kDeleteAppServiceNames)) { + LOG4CXX_WARN(logger_, "Incorrect delete from service names."); + return false; + } + + if (!query_delete.Exec(sql_pt::kDeleteAppServiceTypes)) { + LOG4CXX_WARN(logger_, "Incorrect delete from handled service types."); + return false; + } + // All predefined apps (e.g. default, pre_DataConsent) should be saved first, // otherwise another app with the predefined permissions can get incorrect // permissions @@ -1106,6 +1125,10 @@ bool SQLPTRepresentation::SaveSpecificAppPolicy( if (!SaveRequestType(app.first, *app.second.RequestType)) { return false; } + if (!SaveAppServiceParameters(app.first, + *app.second.app_service_parameters)) { + return false; + } return true; } @@ -1270,6 +1293,85 @@ bool SQLPTRepresentation::SaveRequestSubType( return true; } +bool SQLPTRepresentation::SaveAppServiceParameters( + const std::string& app_id, + const policy_table::AppServiceParameters& app_service_parameters) { + LOG4CXX_INFO(logger_, "Save app service parameters"); + utils::dbms::SQLQuery query(db()); + + if (!query.Prepare(sql_pt::kInsertAppServiceTypes)) { + LOG4CXX_WARN(logger_, "Incorrect insert statement for app service types"); + return false; + } + policy_table::AppServiceParameters::const_iterator it; + for (it = app_service_parameters.begin(); it != app_service_parameters.end(); + ++it) { + // Create service type id from hashing app_id and service_type + std::string str_to_hash = std::string(app_id + it->first); + const long int id = abs(CacheManager::GenerateHash(str_to_hash)); + query.Bind(0, static_cast<int64_t>(id)); + query.Bind(1, it->first); + query.Bind(2, app_id); + if (!query.Exec() || !query.Reset()) { + LOG4CXX_WARN(logger_, "Insert execute failed for into app service types"); + return false; + } + + // Insert app names array into db + utils::dbms::SQLQuery service_name_query(db()); + if (!service_name_query.Prepare(sql_pt::kInsertAppServiceNames)) { + LOG4CXX_WARN(logger_, "Incorrect insert statement for app service names"); + return false; + } + + auto app_service_names = it->second.service_names; + + if (app_service_names.is_initialized() && app_service_names->empty()) { + // App service names is an empty array + LOG4CXX_DEBUG(logger_, "App Service Names is Empty Array"); + service_name_query.Bind(0, static_cast<int64_t>(id)); + service_name_query.Bind(1); + if (!service_name_query.Exec() || !service_name_query.Reset()) { + LOG4CXX_WARN(logger_, "Incorrect insert into empty app service names"); + return false; + } + } else { + policy_table::AppServiceNames::const_iterator names_it; + for (names_it = app_service_names->begin(); + names_it != app_service_names->end(); + ++names_it) { + service_name_query.Bind(0, static_cast<int64_t>(id)); + service_name_query.Bind(1, *names_it); + if (!service_name_query.Exec() || !service_name_query.Reset()) { + LOG4CXX_WARN(logger_, "Incorrect insert into app service names"); + return false; + } + } + } + + // Insert handled rpcs array into db + utils::dbms::SQLQuery handled_rpcs_query(db()); + if (!handled_rpcs_query.Prepare(sql_pt::kInsertAppServiceHandledRpcs)) { + LOG4CXX_WARN(logger_, + "Incorrect insert statement for app service handled rpcs"); + return false; + } + + auto handled_rpcs = it->second.handled_rpcs; + policy_table::AppServiceHandledRpcs::const_iterator rpc_it; + for (rpc_it = handled_rpcs.begin(); rpc_it != handled_rpcs.end(); + ++rpc_it) { + handled_rpcs_query.Bind(0, static_cast<int64_t>(id)); + handled_rpcs_query.Bind(1, static_cast<int32_t>(rpc_it->function_id)); + if (!handled_rpcs_query.Exec() || !handled_rpcs_query.Reset()) { + LOG4CXX_WARN(logger_, "Incorrect insert into app service handled rpcs"); + return false; + } + } + } + return true; +} + bool SQLPTRepresentation::SaveModuleMeta(const policy_table::ModuleMeta& meta) { // Section Module Meta is empty for SDL specific return true; @@ -1696,6 +1798,57 @@ bool SQLPTRepresentation::GatherRequestSubType( return true; } +bool SQLPTRepresentation::GatherAppServiceParameters( + const std::string& app_id, + policy_table::AppServiceParameters* app_service_parameters) const { + LOG4CXX_INFO(logger_, "Gather app service info"); + utils::dbms::SQLQuery service_type_query(db()); + if (!service_type_query.Prepare(sql_pt::kSelectAppServiceTypes)) { + LOG4CXX_WARN(logger_, "Incorrect select from service_types"); + return false; + } + + utils::dbms::SQLQuery service_name_query(db()); + if (!service_name_query.Prepare(sql_pt::kSelectAppServiceNames)) { + LOG4CXX_WARN(logger_, "Incorrect select all from app_service_names"); + return false; + } + + utils::dbms::SQLQuery handled_rpcs_query(db()); + if (!handled_rpcs_query.Prepare(sql_pt::kSelectAppServiceHandledRpcs)) { + LOG4CXX_WARN(logger_, "Incorrect select all from app_service_handled_rpcs"); + return false; + } + + service_type_query.Bind(0, app_id); + while (service_type_query.Next()) { + const int service_type_id = service_type_query.GetInteger(0); + std::string service_type = service_type_query.GetString(1); + (*app_service_parameters)[service_type] = policy_table::AppServiceInfo(); + + service_name_query.Bind(0, service_type_id); + while (service_name_query.Next()) { + LOG4CXX_DEBUG(logger_, "Loading service name"); + (*app_service_parameters)[service_type].service_names->push_back( + service_name_query.GetString(0)); + (*app_service_parameters)[service_type].service_names->mark_initialized(); + } + + handled_rpcs_query.Bind(0, service_type_id); + while (handled_rpcs_query.Next()) { + policy_table::AppServiceHandledRpc handled_rpc; + handled_rpc.function_id = handled_rpcs_query.GetInteger(0); + (*app_service_parameters)[service_type].handled_rpcs.push_back( + handled_rpc); + } + + service_name_query.Reset(); + handled_rpcs_query.Reset(); + } + + return true; +} + bool SQLPTRepresentation::GatherNickName( const std::string& app_id, policy_table::Strings* nicknames) const { utils::dbms::SQLQuery query(db()); @@ -2041,6 +2194,12 @@ bool SQLPTRepresentation::SetDefaultPolicy(const std::string& app_id) { return false; } + policy_table::AppServiceParameters app_service_parameters; + if (!GatherAppServiceParameters(kDefaultId, &app_service_parameters) || + !SaveAppServiceParameters(app_id, app_service_parameters)) { + return false; + } + policy_table::Strings default_groups; bool ret = (GatherAppGroup(kDefaultId, &default_groups) && SaveAppGroup(app_id, default_groups)); @@ -2142,7 +2301,7 @@ bool SQLPTRepresentation::CopyApplication(const std::string& source, utils::dbms::SQLQuery query(db()); if (!query.Prepare(sql_pt::kInsertApplicationFull)) { - LOG4CXX_WARN(logger_, "Incorrect insert statement into application."); + LOG4CXX_WARN(logger_, "Incorrect insert statement into application full."); return false; } query.Bind(0, destination); diff --git a/src/components/policy/policy_external/test/sql_pt_representation_test.cc b/src/components/policy/policy_external/test/sql_pt_representation_test.cc index fddc5e631e..c43c9dace6 100644 --- a/src/components/policy/policy_external/test/sql_pt_representation_test.cc +++ b/src/components/policy/policy_external/test/sql_pt_representation_test.cc @@ -406,8 +406,8 @@ TEST_F(SQLPTRepresentationTest, query.Prepare(query_select); query.Next(); - // 34 - is current total tables number created by schema - const int policy_tables_number = 34; + // 37 - is current total tables number created by schema + const int policy_tables_number = 37; ASSERT_EQ(policy_tables_number, query.GetInteger(0)); const std::string query_select_count_of_iap_buffer_full = diff --git a/src/components/policy/policy_regular/include/policy/cache_manager.h b/src/components/policy/policy_regular/include/policy/cache_manager.h index 8c8f0c55c6..354a979287 100644 --- a/src/components/policy/policy_regular/include/policy/cache_manager.h +++ b/src/components/policy/policy_regular/include/policy/cache_manager.h @@ -217,6 +217,16 @@ class CacheManager : public CacheManagerInterface { const std::string& hybrid_app_preference); /** + * @brief Get app service parameters from the policy table + * @param policy_app_id Unique application id + * @param app_service_parameters Pointer to struct containing all app service + * information + */ + virtual void GetAppServiceParameters( + const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters) const; + + /** * @brief Allows to update 'vin' field in module_meta table. * * @param new 'vin' value. diff --git a/src/components/policy/policy_regular/include/policy/cache_manager_interface.h b/src/components/policy/policy_regular/include/policy/cache_manager_interface.h index 50b546ecc5..639a9f9c18 100644 --- a/src/components/policy/policy_regular/include/policy/cache_manager_interface.h +++ b/src/components/policy/policy_regular/include/policy/cache_manager_interface.h @@ -224,6 +224,16 @@ class CacheManagerInterface { const std::string& hybrid_app_preference) = 0; /** + * @brief Get app service parameters from the policy table + * @param policy_app_id Unique application id + * @param app_service_parameters Pointer to struct containing all app service + * information + */ + virtual void GetAppServiceParameters( + const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters) const = 0; + + /** * @brief Allows to update 'vin' field in module_meta table. * * @param new 'vin' value. diff --git a/src/components/policy/policy_regular/include/policy/policy_manager_impl.h b/src/components/policy/policy_regular/include/policy/policy_manager_impl.h index 20a37717a7..4ed8903b00 100644 --- a/src/components/policy/policy_regular/include/policy/policy_manager_impl.h +++ b/src/components/policy/policy_regular/include/policy/policy_manager_impl.h @@ -635,6 +635,16 @@ class PolicyManagerImpl : public PolicyManager { const std::string& hybrid_app_preference) OVERRIDE; /** + * @brief Get app service parameters from the policy table + * @param policy_app_id Unique application id + * @param app_service_parameters Pointer to struct containing all app service + * information + */ + void GetAppServiceParameters(const std::string& policy_app_id, + policy_table::AppServiceParameters* + app_service_parameters) const OVERRIDE; + + /** * @brief OnAppRegisteredOnMobile allows to handle event when application were * succesfully registered on mobile device. * It will send OnAppPermissionSend notification and will try to start PTU. * diff --git a/src/components/policy/policy_regular/include/policy/policy_table/types.h b/src/components/policy/policy_regular/include/policy/policy_table/types.h index 58f07492c4..118f98677a 100644 --- a/src/components/policy/policy_regular/include/policy/policy_table/types.h +++ b/src/components/policy/policy_regular/include/policy/policy_table/types.h @@ -50,6 +50,8 @@ struct MessageLanguages; struct MessageString; struct RpcParameters; struct Rpcs; +struct AppServiceHandledRpc; +struct AppServiceInfo; } // namespace policy_table_interface_base } // namespace rpc @@ -98,6 +100,13 @@ typedef Array<Enum<RequestType>, 0, 255> RequestTypes; typedef Strings RequestSubTypes; +typedef String<0, 255> AppServiceType; +typedef String<0, 255> AppServiceName; +typedef Array<AppServiceName, 0, 255> AppServiceNames; +typedef Array<AppServiceHandledRpc, 0, 255> AppServiceHandledRpcs; +typedef Map<AppServiceInfo, 0, 255> AppServiceParameters; +typedef Integer<int32_t, 0, INT32_MAX> FunctionIDInt; + typedef Map<Strings, 0, 255> RemoteRpcs; typedef Map<RemoteRpcs, 0, 255> AccessModules; typedef Array<Enum<ModuleType>, 0, 255> ModuleTypes; @@ -105,6 +114,45 @@ typedef Array<Enum<ModuleType>, 0, 255> ModuleTypes; typedef AppHMIType AppHmiType; typedef std::vector<AppHMIType> AppHmiTypes; +struct AppServiceHandledRpc : CompositeType { + public: + FunctionIDInt function_id; + + public: + AppServiceHandledRpc(); + ~AppServiceHandledRpc(); + AppServiceHandledRpc(const Json::Value* value__); + Json::Value ToJsonValue() const; + bool is_valid() const; + bool is_initialized() const; + bool struct_empty() const; + virtual void SetPolicyTableType(PolicyTableType pt_type); + void ReportErrors(rpc::ValidationReport* report__) const; + + private: + bool Validate() const; +}; + +struct AppServiceInfo : CompositeType { + public: + Optional<AppServiceNames> service_names; + AppServiceHandledRpcs handled_rpcs; + + public: + AppServiceInfo(); + ~AppServiceInfo(); + AppServiceInfo(const Json::Value* value__); + Json::Value ToJsonValue() const; + bool is_valid() const; + bool is_initialized() const; + bool struct_empty() const; + virtual void SetPolicyTableType(PolicyTableType pt_type); + void ReportErrors(rpc::ValidationReport* report__) const; + + private: + bool Validate() const; +}; + struct PolicyBase : CompositeType { public: Enum<Priority> priority; @@ -151,6 +199,9 @@ struct ApplicationParams : PolicyBase { Optional<String<0, 65535> > auth_token; Optional<String<0, 255> > cloud_transport_type; + // App Service Params + Optional<AppServiceParameters> app_service_parameters; + public: ApplicationParams(); ApplicationParams(const Strings& groups, Priority priority); diff --git a/src/components/policy/policy_regular/include/policy/sql_pt_queries.h b/src/components/policy/policy_regular/include/policy/sql_pt_queries.h index 191873efe0..bada9a003a 100644 --- a/src/components/policy/policy_regular/include/policy/sql_pt_queries.h +++ b/src/components/policy/policy_regular/include/policy/sql_pt_queries.h @@ -66,6 +66,9 @@ extern const std::string kSelectNicknames; extern const std::string kSelectAppTypes; extern const std::string kSelectRequestTypes; extern const std::string kSelectRequestSubTypes; +extern const std::string kSelectAppServiceTypes; +extern const std::string kSelectAppServiceNames; +extern const std::string kSelectAppServiceHandledRpcs; extern const std::string kSelectSecondsBetweenRetries; extern const std::string kSelectIgnitionCycles; extern const std::string kSelectKilometers; @@ -80,6 +83,9 @@ extern const std::string kInsertNickname; extern const std::string kInsertAppType; extern const std::string kInsertRequestType; extern const std::string kInsertRequestSubType; +extern const std::string kInsertAppServiceTypes; +extern const std::string kInsertAppServiceNames; +extern const std::string kInsertAppServiceHandledRpcs; extern const std::string kInsertMessageType; extern const std::string kInsertLanguage; extern const std::string kInsertMessageString; @@ -100,6 +106,9 @@ extern const std::string kDeleteAppGroup; extern const std::string kDeleteApplication; extern const std::string kDeleteRequestType; extern const std::string kDeleteRequestSubType; +extern const std::string kDeleteAppServiceTypes; +extern const std::string kDeleteAppServiceNames; +extern const std::string kDeleteAppServiceHandledRpcs; extern const std::string kDeleteDevice; extern const std::string kIncrementIgnitionCycles; extern const std::string kResetIgnitionCycles; diff --git a/src/components/policy/policy_regular/include/policy/sql_pt_representation.h b/src/components/policy/policy_regular/include/policy/sql_pt_representation.h index 0e9302dfac..dfa318454b 100644 --- a/src/components/policy/policy_regular/include/policy/sql_pt_representation.h +++ b/src/components/policy/policy_regular/include/policy/sql_pt_representation.h @@ -133,6 +133,10 @@ class SQLPTRepresentation : public virtual PTRepresentation { bool GatherRequestSubType( const std::string& app_id, policy_table::RequestSubTypes* request_subtypes) const; + bool GatherAppServiceParameters( + const std::string& app_id, + policy_table::AppServiceParameters* app_service_parameters) const; + bool GatherNickName(const std::string& app_id, policy_table::Strings* nicknames) const; @@ -171,6 +175,9 @@ class SQLPTRepresentation : public virtual PTRepresentation { bool SaveRequestSubType( const std::string& app_id, const policy_table::RequestSubTypes& request_subtypes); + bool SaveAppServiceParameters( + const std::string& app_id, + const policy_table::AppServiceParameters& app_service_parameters); public: bool UpdateRequired() const; diff --git a/src/components/policy/policy_regular/src/cache_manager.cc b/src/components/policy/policy_regular/src/cache_manager.cc index ca61a338f1..5abe4158f1 100644 --- a/src/components/policy/policy_regular/src/cache_manager.cc +++ b/src/components/policy/policy_regular/src/cache_manager.cc @@ -796,6 +796,22 @@ void CacheManager::SetHybridAppPreference( } } +void CacheManager::GetAppServiceParameters( + const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters) const { + LOG4CXX_AUTO_TRACE(logger_); + const policy_table::ApplicationPolicies& policies = + pt_->policy_table.app_policies_section.apps; + policy_table::ApplicationPolicies::const_iterator policy_iter = + policies.find(policy_app_id); + if (policies.end() != policy_iter) { + auto app_policy = (*policy_iter).second; + if (app_policy.app_service_parameters.is_initialized()) { + *app_service_parameters = *(app_policy.app_service_parameters); + } + } +} + std::vector<UserFriendlyMessage> CacheManager::GetUserFriendlyMsg( const std::vector<std::string>& msg_codes, const std::string& language) { LOG4CXX_AUTO_TRACE(logger_); diff --git a/src/components/policy/policy_regular/src/policy_manager_impl.cc b/src/components/policy/policy_regular/src/policy_manager_impl.cc index 9b2392a015..5f09d166b1 100644 --- a/src/components/policy/policy_regular/src/policy_manager_impl.cc +++ b/src/components/policy/policy_regular/src/policy_manager_impl.cc @@ -591,6 +591,12 @@ void PolicyManagerImpl::SetHybridAppPreference( cache_->SetHybridAppPreference(policy_app_id, hybrid_app_preference); } +void PolicyManagerImpl::GetAppServiceParameters( + const std::string& policy_app_id, + policy_table::AppServiceParameters* app_service_parameters) const { + cache_->GetAppServiceParameters(policy_app_id, app_service_parameters); +} + void PolicyManagerImpl::CheckPermissions(const PTString& device_id, const PTString& app_id, const PTString& hmi_level, diff --git a/src/components/policy/policy_regular/src/policy_table/types.cc b/src/components/policy/policy_regular/src/policy_table/types.cc index 88564df050..5f7d2e57bf 100644 --- a/src/components/policy/policy_regular/src/policy_table/types.cc +++ b/src/components/policy/policy_regular/src/policy_table/types.cc @@ -149,6 +149,109 @@ void ApplicationPoliciesSection::SetPolicyTableType(PolicyTableType pt_type) { device.SetPolicyTableType(pt_type); apps.SetPolicyTableType(pt_type); } +// Handled RPC Methods +AppServiceHandledRpc::AppServiceHandledRpc() : CompositeType(kUninitialized) {} + +AppServiceHandledRpc::~AppServiceHandledRpc() {} + +AppServiceHandledRpc::AppServiceHandledRpc(const Json::Value* value__) + : CompositeType(InitHelper(value__, &Json::Value::isObject)) + , function_id(impl::ValueMember(value__, "function_id")) {} + +Json::Value AppServiceHandledRpc::ToJsonValue() const { + Json::Value result__(Json::objectValue); + impl::WriteJsonField("function_id", function_id, &result__); + return result__; +} + +bool AppServiceHandledRpc::is_valid() const { + if (!function_id.is_valid()) { + return false; + } + return Validate(); +} + +bool AppServiceHandledRpc::is_initialized() const { + return (initialization_state__ != kUninitialized) || (!struct_empty()); +} + +bool AppServiceHandledRpc::struct_empty() const { + if (function_id.is_initialized()) { + return false; + } + return true; +} + +void AppServiceHandledRpc::SetPolicyTableType(PolicyTableType pt_type) { + function_id.SetPolicyTableType(pt_type); +} + +void AppServiceHandledRpc::ReportErrors(rpc::ValidationReport* report__) const { + if (struct_empty()) { + rpc::CompositeType::ReportErrors(report__); + } + if (!function_id.is_valid()) { + function_id.ReportErrors(&report__->ReportSubobject("function_id")); + } +} + +// AppServiceInfo methods +AppServiceInfo::AppServiceInfo() : CompositeType(kUninitialized) {} + +AppServiceInfo::~AppServiceInfo() {} + +AppServiceInfo::AppServiceInfo(const Json::Value* value__) + : CompositeType(InitHelper(value__, &Json::Value::isObject)) + , service_names(impl::ValueMember(value__, "service_names")) + , handled_rpcs(impl::ValueMember(value__, "handled_rpcs")) {} + +Json::Value AppServiceInfo::ToJsonValue() const { + Json::Value result__(Json::objectValue); + impl::WriteJsonField("service_names", service_names, &result__); + impl::WriteJsonField("parameters", handled_rpcs, &result__); + return result__; +} + +bool AppServiceInfo::is_valid() const { + if (!service_names.is_valid()) { + return false; + } + if (!handled_rpcs.is_valid()) { + return false; + } + return Validate(); +} + +bool AppServiceInfo::is_initialized() const { + return (initialization_state__ != kUninitialized) || (!struct_empty()); +} + +bool AppServiceInfo::struct_empty() const { + if (service_names.is_initialized()) { + return false; + } + if (handled_rpcs.is_initialized()) { + return false; + } + return true; +} + +void AppServiceInfo::SetPolicyTableType(PolicyTableType pt_type) { + service_names.SetPolicyTableType(pt_type); + handled_rpcs.SetPolicyTableType(pt_type); +} + +void AppServiceInfo::ReportErrors(rpc::ValidationReport* report__) const { + if (struct_empty()) { + rpc::CompositeType::ReportErrors(report__); + } + if (!service_names.is_valid()) { + service_names.ReportErrors(&report__->ReportSubobject("service_names")); + } + if (!handled_rpcs.is_valid()) { + handled_rpcs.ReportErrors(&report__->ReportSubobject("handled_rpcs")); + } +} // ApplicationParams methods ApplicationParams::ApplicationParams() : PolicyBase(), groups() {} @@ -173,8 +276,8 @@ ApplicationParams::ApplicationParams(const Json::Value* value__) , endpoint(impl::ValueMember(value__, "endpoint")) , enabled(impl::ValueMember(value__, "enabled")) , auth_token(impl::ValueMember(value__, "auth_token")) - , cloud_transport_type(impl::ValueMember(value__, "cloud_transport_type")) { -} + , cloud_transport_type(impl::ValueMember(value__, "cloud_transport_type")) + , app_service_parameters(impl::ValueMember(value__, "app_services")) {} Json::Value ApplicationParams::ToJsonValue() const { Json::Value result__(PolicyBase::ToJsonValue()); @@ -194,6 +297,7 @@ Json::Value ApplicationParams::ToJsonValue() const { impl::WriteJsonField("enabled", enabled, &result__); impl::WriteJsonField("auth_token", auth_token, &result__); impl::WriteJsonField("cloud_transport_type", cloud_transport_type, &result__); + impl::WriteJsonField("app_services", app_service_parameters, &result__); return result__; } @@ -240,6 +344,9 @@ bool ApplicationParams::is_valid() const { if (!hybrid_app_preference.is_valid()) { return false; } + if (!app_service_parameters.is_valid()) { + return false; + } return Validate(); } @@ -293,6 +400,9 @@ bool ApplicationParams::struct_empty() const { if (hybrid_app_preference.is_initialized()) { return false; } + if (app_service_parameters.is_initialized()) { + return false; + } return true; } @@ -347,6 +457,10 @@ void ApplicationParams::ReportErrors(rpc::ValidationReport* report__) const { moduleType.ReportErrors( &report__->ReportSubobject("hybrid_app_preference")); } + if (!app_service_parameters.is_valid()) { + app_service_parameters.ReportErrors( + &report__->ReportSubobject("app_services")); + } } void ApplicationParams::SetPolicyTableType(PolicyTableType pt_type) { @@ -363,6 +477,7 @@ void ApplicationParams::SetPolicyTableType(PolicyTableType pt_type) { enabled.SetPolicyTableType(pt_type); cloud_transport_type.SetPolicyTableType(pt_type); hybrid_app_preference.SetPolicyTableType(pt_type); + app_service_parameters.SetPolicyTableType(pt_type); } // RpcParameters methods diff --git a/src/components/policy/policy_regular/src/policy_table/validation.cc b/src/components/policy/policy_regular/src/policy_table/validation.cc index 714dfaae5a..4db84e06ff 100644 --- a/src/components/policy/policy_regular/src/policy_table/validation.cc +++ b/src/components/policy/policy_regular/src/policy_table/validation.cc @@ -177,6 +177,14 @@ bool ApplicationParams::ValidateModuleTypes() const { return true; } +bool AppServiceHandledRpc::Validate() const { + return true; +} + +bool AppServiceInfo::Validate() const { + return true; +} + bool ApplicationParams::Validate() const { return ValidateModuleTypes(); } diff --git a/src/components/policy/policy_regular/src/sql_pt_queries.cc b/src/components/policy/policy_regular/src/sql_pt_queries.cc index 297031d4c0..610a24eb19 100644 --- a/src/components/policy/policy_regular/src/sql_pt_queries.cc +++ b/src/components/policy/policy_regular/src/sql_pt_queries.cc @@ -287,6 +287,30 @@ const std::string kCreateSchema = " FOREIGN KEY(`application_id`) " " REFERENCES `application`(`id`) " "); " + "CREATE TABLE IF NOT EXISTS `app_service_types`( " + " `id` INTEGER PRIMARY KEY NOT NULL, " + " `service_type` VARCHAR(50), " + " `application_id` VARCHAR(45) NOT NULL COLLATE NOCASE, " + " CONSTRAINT `fk_service_type_app_id` " + " FOREIGN KEY(`application_id`) " + " REFERENCES `application`(`id`) " + "); " + "CREATE TABLE IF NOT EXISTS `app_service_names`( " + " `service_type_id` INTEGER NOT NULL, " + " `service_name` VARCHAR(45), " + " PRIMARY KEY(`service_name`,`service_type_id`), " + " CONSTRAINT `fk_service_name_service_type_id` " + " FOREIGN KEY(`service_type_id`) " + " REFERENCES `app_service_types`(`id`) " + "); " + "CREATE TABLE IF NOT EXISTS `app_service_handled_rpcs`( " + " `service_type_id` INTEGER NOT NULL, " + " `function_id` INTEGER, " + " PRIMARY KEY(`function_id`,`service_type_id`), " + " CONSTRAINT `fk_function_id_service_type_id` " + " FOREIGN KEY(`service_type_id`) " + " REFERENCES `app_service_types`(`id`) " + "); " "CREATE INDEX IF NOT EXISTS `app_type.fk_app_type_application1_idx` " " ON `app_type`(`application_id` COLLATE NOCASE); " "CREATE TABLE IF NOT EXISTS `consent_group`( " @@ -467,6 +491,9 @@ const std::string kDropSchema = "DROP TABLE IF EXISTS `app_type`; " "DROP TABLE IF EXISTS `request_type`; " "DROP TABLE IF EXISTS `request_subtype`; " + "DROP TABLE IF EXISTS `app_service_types`; " + "DROP TABLE IF EXISTS `app_service_names`; " + "DROP TABLE IF EXISTS `app_service_handled_rpcs`; " "DROP INDEX IF EXISTS `nickname.fk_nickname_application1_idx`; " "DROP TABLE IF EXISTS `nickname`; " "DROP INDEX IF EXISTS `app_level.fk_app_level_language2_idx`; " @@ -620,6 +647,21 @@ const std::string kInsertRequestSubType = "`request_subtype`) " "VALUES (?, ?)"; +const std::string kInsertAppServiceTypes = + "INSERT INTO `app_service_types` (`id`, " + "`service_type`, `application_id`) " + "VALUES (?, ?, ?)"; + +const std::string kInsertAppServiceNames = + "INSERT INTO `app_service_names` (`service_type_id`, " + "`service_name`) " + "VALUES (?, ?)"; + +const std::string kInsertAppServiceHandledRpcs = + "INSERT INTO `app_service_handled_rpcs` (`service_type_id`, " + "`function_id`) " + "VALUES (?, ?)"; + const std::string kUpdateVersion = "UPDATE `version` SET `number`= ?"; const std::string kInsertMessageType = @@ -737,6 +779,21 @@ const std::string kSelectRequestSubTypes = "`application_id` " "= ?"; +const std::string kSelectAppServiceTypes = + "SELECT `id`, `service_type` FROM `app_service_types` WHERE " + "`application_id` " + "= ?"; + +const std::string kSelectAppServiceNames = + "SELECT DISTINCT `service_name` FROM `app_service_names` WHERE " + "`service_type_id` " + "= ?"; + +const std::string kSelectAppServiceHandledRpcs = + "SELECT DISTINCT `function_id` FROM `app_service_handled_rpcs` WHERE " + "`service_type_id` " + "= ?"; + const std::string kSelectSecondsBetweenRetries = "SELECT `value` FROM `seconds_between_retry` ORDER BY `index`"; @@ -784,6 +841,13 @@ const std::string kDeleteRequestType = "DELETE FROM `request_type`"; const std::string kDeleteRequestSubType = "DELETE FROM `request_subtype`"; +const std::string kDeleteAppServiceTypes = "DELETE FROM `app_service_types`"; + +const std::string kDeleteAppServiceNames = "DELETE FROM `app_service_names`"; + +const std::string kDeleteAppServiceHandledRpcs = + "DELETE FROM `app_service_handled_rpcs`"; + const std::string kSelectApplicationRevoked = "SELECT `is_revoked` FROM `application` WHERE `id` = ?"; 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 9282caf535..661be436f0 100644 --- a/src/components/policy/policy_regular/src/sql_pt_representation.cc +++ b/src/components/policy/policy_regular/src/sql_pt_representation.cc @@ -766,6 +766,11 @@ bool SQLPTRepresentation::GatherApplicationPoliciesSection( return false; } + if (!GatherAppServiceParameters(gather_app_id, + &*params.app_service_parameters)) { + return false; + } + (*policies).apps[app_id] = params; } return true; @@ -942,6 +947,21 @@ bool SQLPTRepresentation::SaveApplicationPoliciesSection( return false; } + if (!query_delete.Exec(sql_pt::kDeleteAppServiceHandledRpcs)) { + LOG4CXX_WARN(logger_, "Incorrect delete from handled rpcs."); + return false; + } + + if (!query_delete.Exec(sql_pt::kDeleteAppServiceNames)) { + LOG4CXX_WARN(logger_, "Incorrect delete from service names."); + return false; + } + + if (!query_delete.Exec(sql_pt::kDeleteAppServiceTypes)) { + LOG4CXX_WARN(logger_, "Incorrect delete from handled service types."); + return false; + } + // All predefined apps (e.g. default, pre_DataConsent) should be saved first, // otherwise another app with the predefined permissions can get incorrect // permissions @@ -1050,6 +1070,11 @@ bool SQLPTRepresentation::SaveSpecificAppPolicy( return false; } + if (!SaveAppServiceParameters(app.first, + *app.second.app_service_parameters)) { + return false; + } + return true; } @@ -1209,6 +1234,85 @@ bool SQLPTRepresentation::SaveRequestSubType( return true; } +bool SQLPTRepresentation::SaveAppServiceParameters( + const std::string& app_id, + const policy_table::AppServiceParameters& app_service_parameters) { + LOG4CXX_INFO(logger_, "Save app service parameters"); + utils::dbms::SQLQuery query(db()); + + if (!query.Prepare(sql_pt::kInsertAppServiceTypes)) { + LOG4CXX_WARN(logger_, "Incorrect insert statement for app service types"); + return false; + } + policy_table::AppServiceParameters::const_iterator it; + for (it = app_service_parameters.begin(); it != app_service_parameters.end(); + ++it) { + // Create service type id from hashing app_id and service_type + std::string str_to_hash = std::string(app_id + it->first); + const long int id = abs(CacheManager::GenerateHash(str_to_hash)); + query.Bind(0, static_cast<int64_t>(id)); + query.Bind(1, it->first); + query.Bind(2, app_id); + if (!query.Exec() || !query.Reset()) { + LOG4CXX_WARN(logger_, "Insert execute failed for into app service types"); + return false; + } + + // Insert app names array into db + utils::dbms::SQLQuery service_name_query(db()); + if (!service_name_query.Prepare(sql_pt::kInsertAppServiceNames)) { + LOG4CXX_WARN(logger_, "Incorrect insert statement for app service names"); + return false; + } + + auto app_service_names = it->second.service_names; + + if (app_service_names.is_initialized() && app_service_names->empty()) { + // App service names is an empty array + LOG4CXX_DEBUG(logger_, "App Service Names is Empty Array"); + service_name_query.Bind(0, static_cast<int64_t>(id)); + service_name_query.Bind(1); + if (!service_name_query.Exec() || !service_name_query.Reset()) { + LOG4CXX_WARN(logger_, "Incorrect insert into empty app service names"); + return false; + } + } else { + policy_table::AppServiceNames::const_iterator names_it; + for (names_it = app_service_names->begin(); + names_it != app_service_names->end(); + ++names_it) { + service_name_query.Bind(0, static_cast<int64_t>(id)); + service_name_query.Bind(1, *names_it); + if (!service_name_query.Exec() || !service_name_query.Reset()) { + LOG4CXX_WARN(logger_, "Incorrect insert into app service names"); + return false; + } + } + } + + // Insert handled rpcs array into db + utils::dbms::SQLQuery handled_rpcs_query(db()); + if (!handled_rpcs_query.Prepare(sql_pt::kInsertAppServiceHandledRpcs)) { + LOG4CXX_WARN(logger_, + "Incorrect insert statement for app service handled rpcs"); + return false; + } + + auto handled_rpcs = it->second.handled_rpcs; + policy_table::AppServiceHandledRpcs::const_iterator rpc_it; + for (rpc_it = handled_rpcs.begin(); rpc_it != handled_rpcs.end(); + ++rpc_it) { + handled_rpcs_query.Bind(0, static_cast<int64_t>(id)); + handled_rpcs_query.Bind(1, static_cast<int32_t>(rpc_it->function_id)); + if (!handled_rpcs_query.Exec() || !handled_rpcs_query.Reset()) { + LOG4CXX_WARN(logger_, "Incorrect insert into app service handled rpcs"); + return false; + } + } + } + return true; +} + bool SQLPTRepresentation::SaveModuleMeta(const policy_table::ModuleMeta& meta) { utils::dbms::SQLQuery query(db()); @@ -1647,6 +1751,58 @@ bool SQLPTRepresentation::GatherRequestSubType( return true; } +bool SQLPTRepresentation::GatherAppServiceParameters( + const std::string& app_id, + policy_table::AppServiceParameters* app_service_parameters) const { + LOG4CXX_INFO(logger_, "Gather app service info"); + utils::dbms::SQLQuery service_type_query(db()); + if (!service_type_query.Prepare(sql_pt::kSelectAppServiceTypes)) { + LOG4CXX_WARN(logger_, "Incorrect select from service_types"); + return false; + } + + utils::dbms::SQLQuery service_name_query(db()); + if (!service_name_query.Prepare(sql_pt::kSelectAppServiceNames)) { + LOG4CXX_WARN(logger_, "Incorrect select all from app_service_names"); + return false; + } + + utils::dbms::SQLQuery handled_rpcs_query(db()); + if (!handled_rpcs_query.Prepare(sql_pt::kSelectAppServiceHandledRpcs)) { + LOG4CXX_WARN(logger_, "Incorrect select all from app_service_handled_rpcs"); + return false; + } + + service_type_query.Bind(0, app_id); + while (service_type_query.Next()) { + const int service_type_id = service_type_query.GetInteger(0); + std::string service_type = service_type_query.GetString(1); + LOG4CXX_WARN(logger_, "Load service type from pt: " << service_type); + (*app_service_parameters)[service_type] = policy_table::AppServiceInfo(); + + service_name_query.Bind(0, service_type_id); + while (service_name_query.Next()) { + LOG4CXX_DEBUG(logger_, "Loading service name"); + (*app_service_parameters)[service_type].service_names->push_back( + service_name_query.GetString(0)); + (*app_service_parameters)[service_type].service_names->mark_initialized(); + } + + handled_rpcs_query.Bind(0, service_type_id); + while (handled_rpcs_query.Next()) { + policy_table::AppServiceHandledRpc handled_rpc; + handled_rpc.function_id = handled_rpcs_query.GetInteger(0); + (*app_service_parameters)[service_type].handled_rpcs.push_back( + handled_rpc); + } + + service_name_query.Reset(); + handled_rpcs_query.Reset(); + } + + return true; +} + bool SQLPTRepresentation::GatherNickName( const std::string& app_id, policy_table::Strings* nicknames) const { utils::dbms::SQLQuery query(db()); @@ -1997,6 +2153,12 @@ bool SQLPTRepresentation::SetDefaultPolicy(const std::string& app_id) { return false; } + policy_table::AppServiceParameters app_service_parameters; + if (!GatherAppServiceParameters(kDefaultId, &app_service_parameters) || + !SaveAppServiceParameters(app_id, app_service_parameters)) { + return false; + } + return SetIsDefault(app_id, true); } diff --git a/src/components/policy/policy_regular/test/sql_pt_representation_test.cc b/src/components/policy/policy_regular/test/sql_pt_representation_test.cc index 030dc374dd..6448a47b42 100644 --- a/src/components/policy/policy_regular/test/sql_pt_representation_test.cc +++ b/src/components/policy/policy_regular/test/sql_pt_representation_test.cc @@ -426,8 +426,8 @@ TEST_F(SQLPTRepresentationTest, ASSERT_TRUE(reps->RefreshDB()); // Check PT structure destroyed and tables number is 0 - // There are 30 tables in the database, now. - const int32_t total_tables_number = 30; + // There are 33 tables in the database, now. + const int32_t total_tables_number = 33; ASSERT_EQ(total_tables_number, dbms->FetchOneInt(query_select)); const char* query_select_count_of_iap_buffer_full = "SELECT `count_of_iap_buffer_full` FROM `usage_and_error_count`"; |