summaryrefslogtreecommitdiff
path: root/src/components/policy/src/policy/src/sql_pt_representation.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/policy/src/policy/src/sql_pt_representation.cc')
-rw-r--r--src/components/policy/src/policy/src/sql_pt_representation.cc117
1 files changed, 78 insertions, 39 deletions
diff --git a/src/components/policy/src/policy/src/sql_pt_representation.cc b/src/components/policy/src/policy/src/sql_pt_representation.cc
index cd8e5dcf0..838949cba 100644
--- a/src/components/policy/src/policy/src/sql_pt_representation.cc
+++ b/src/components/policy/src/policy/src/sql_pt_representation.cc
@@ -34,6 +34,7 @@
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
+#include <unistd.h>
#include "utils/logger.h"
#include "policy/sql_pt_representation.h"
@@ -41,9 +42,7 @@
#include "policy/sql_pt_queries.h"
#include "policy/policy_helper.h"
#include "policy/cache_manager.h"
-#ifndef __QNX__
-# include "config_profile/profile.h"
-#endif // __QNX__
+#include "config_profile/profile.h"
namespace policy {
@@ -61,7 +60,6 @@ template<typename T, typename K> void InsertUnique(K value, T* array) {
}
} // namespace
-// CUSTOMER_PASA
const std::string SQLPTRepresentation::kDatabaseName = "policy";
SQLPTRepresentation::SQLPTRepresentation()
@@ -75,7 +73,6 @@ SQLPTRepresentation::SQLPTRepresentation()
}
SQLPTRepresentation::~SQLPTRepresentation() {
- db_->Backup();
db_->Close();
delete db_;
}
@@ -153,8 +150,7 @@ int SQLPTRepresentation::KilometersBeforeExchange(int current) {
bool SQLPTRepresentation::SetCountersPassedForSuccessfulUpdate(
int kilometers, int days_after_epoch) {
- LOG4CXX_INFO(logger_,
- "SQLPTRepresentation::SetCountersPassedForSuccessfulUpdate");
+ LOG4CXX_AUTO_TRACE(logger_);
dbms::SQLQuery query(db());
if (!query.Prepare(sql_pt::kUpdateCountersSuccessfulUpdate)) {
LOG4CXX_WARN(logger_,
@@ -249,29 +245,6 @@ EndpointUrls SQLPTRepresentation::GetUpdateUrls(int service_type) {
return ret;
}
-std::string SQLPTRepresentation::GetLockScreenIconUrl() const {
- dbms::SQLQuery query(db());
- std::string ret;
- if (query.Prepare(sql_pt::kSelectLockScreenIcon)) {
- query.Bind(0, std::string("lock_screen_icon_url"));
- query.Bind(1, std::string("default"));
-
- if(!query.Exec()) {
- LOG4CXX_WARN(logger_, "Incorrect select from notifications by priority.");
- return ret;
- }
-
- if (!query.IsNull(0)) {
- ret = query.GetString(0);
- }
-
- } else {
- LOG4CXX_WARN(logger_, "Invalid select endpoints statement.");
- }
- return ret;
-}
-
-
int SQLPTRepresentation::GetNotificationsNumber(const std::string& priority) {
LOG4CXX_INFO(logger_, "GetNotificationsNumber");
dbms::SQLQuery query(db());
@@ -324,12 +297,43 @@ bool SQLPTRepresentation::GetPriority(const std::string& policy_app_id,
}
InitResult SQLPTRepresentation::Init() {
- LOG4CXX_INFO(logger_, "SQLPTRepresentation::Init");
+ LOG4CXX_AUTO_TRACE(logger_);
if (!db_->Open()) {
- LOG4CXX_ERROR(logger_, "Failed opening database");
+ LOG4CXX_ERROR(logger_, "Failed opening database.");
+ LOG4CXX_INFO(logger_, "Starting opening retries.");
+ const uint16_t attempts =
+ profile::Profile::instance()->attempts_to_open_policy_db();
+ LOG4CXX_DEBUG(logger_, "Total attempts number is: " << attempts);
+ bool is_opened = false;
+ const uint16_t open_attempt_timeout_ms =
+ profile::Profile::instance()->open_attempt_timeout_ms();
+ const useconds_t sleep_interval_mcsec = open_attempt_timeout_ms * 1000;
+ LOG4CXX_DEBUG(logger_, "Open attempt timeout(ms) is: "
+ << open_attempt_timeout_ms);
+ for (int i = 0; i < attempts; ++i) {
+ usleep(sleep_interval_mcsec);
+ LOG4CXX_INFO(logger_, "Attempt: " << i+1);
+ if (db_->Open()){
+ LOG4CXX_INFO(logger_, "Database opened.");
+ is_opened = true;
+ break;
+ }
+ }
+ if (!is_opened) {
+ LOG4CXX_ERROR(logger_, "Open retry sequence failed. Tried "
+ << attempts << " attempts with "
+ << open_attempt_timeout_ms
+ << " open timeout(ms) for each.");
+ return InitResult::FAIL;
+ }
+ }
+#ifndef __QNX__
+ if (!db_->IsReadWrite()) {
+ LOG4CXX_ERROR(logger_, "There are no read/write permissions for database");
return InitResult::FAIL;
}
+#endif // __QNX__
dbms::SQLQuery check_pages(db());
if (!check_pages.Prepare(sql_pt::kCheckPgNumber) || !check_pages.Next()) {
LOG4CXX_WARN(logger_, "Incorrect pragma for page counting.");
@@ -400,6 +404,10 @@ bool SQLPTRepresentation::Drop() {
return true;
}
+void SQLPTRepresentation::WriteDb() {
+ db_->Backup();
+}
+
bool SQLPTRepresentation::Clear() {
dbms::SQLQuery query(db());
if (!query.Exec(sql_pt::kDeleteData)) {
@@ -416,6 +424,28 @@ bool SQLPTRepresentation::Clear() {
return true;
}
+bool SQLPTRepresentation::RefreshDB() {
+ dbms::SQLQuery query(db());
+ if (!query.Exec(sql_pt::kDropSchema)) {
+ LOG4CXX_WARN(logger_,
+ "Failed dropping database: " << query.LastError().text());
+ return false;
+ }
+ if (!query.Exec(sql_pt::kCreateSchema)) {
+ LOG4CXX_ERROR(
+ logger_,
+ "Failed creating schema of database: " << query.LastError().text());
+ return false;
+ }
+ if (!query.Exec(sql_pt::kInsertInitData)) {
+ LOG4CXX_ERROR(
+ logger_,
+ "Failed insert init data to database: " << query.LastError().text());
+ return false;
+ }
+ return true;
+}
+
utils::SharedPtr<policy_table::Table>
SQLPTRepresentation::GenerateSnapshot() const {
LOG4CXX_INFO(logger_, "GenerateSnapshot");
@@ -621,7 +651,7 @@ bool SQLPTRepresentation::GatherApplicationPolicies(
}
bool SQLPTRepresentation::Save(const policy_table::Table& table) {
- LOG4CXX_INFO(logger_, "SQLPTRepresentation::Save");
+ LOG4CXX_AUTO_TRACE(logger_);
db_->BeginTransaction();
if (!SaveFunctionalGroupings(table.policy_table.functional_groupings)) {
db_->RollbackTransaction();
@@ -726,9 +756,9 @@ bool SQLPTRepresentation::SaveRpcs(int64_t group_id,
for (ps_it = parameters.begin(); ps_it != parameters.end(); ++ps_it) {
query_parameter.Bind(0, it->first);
query_parameter.Bind(
- 1, std::string(policy_table::EnumToJsonString(*hmi_it)));
+ 1, std::string(policy_table::EnumToJsonString(*hmi_it)));
query_parameter.Bind(
- 2, std::string(policy_table::EnumToJsonString(*ps_it)));
+ 2, std::string(policy_table::EnumToJsonString(*ps_it)));
query_parameter.Bind(3, group_id);
if (!query_parameter.Exec() || !query_parameter.Reset()) {
LOG4CXX_WARN(logger_, "Incorrect insert into rpc with parameter");
@@ -981,7 +1011,11 @@ bool SQLPTRepresentation::SaveServiceEndpoints(
const policy_table::URL& urls = app_it->second;
policy_table::URL::const_iterator url_it;
for (url_it = urls.begin(); url_it != urls.end(); ++url_it) {
- query.Bind(0, it->first);
+ std::stringstream temp_stream(it->first);
+ int service;
+ temp_stream.seekg(3);
+ temp_stream >> service;
+ query.Bind(0, service);
query.Bind(1, *url_it);
query.Bind(2, app_it->first);
if (!query.Exec() || !query.Reset()) {
@@ -997,7 +1031,7 @@ bool SQLPTRepresentation::SaveServiceEndpoints(
bool SQLPTRepresentation::SaveConsumerFriendlyMessages(
const policy_table::ConsumerFriendlyMessages& messages) {
- LOG4CXX_TRACE_ENTER(logger_);
+ LOG4CXX_AUTO_TRACE(logger_);
// According CRS-2419 If there is no “consumer_friendly_messages” key,
// the current local consumer_friendly_messages section shall be maintained in
@@ -1152,6 +1186,7 @@ bool SQLPTRepresentation::SaveDeviceData(
bool SQLPTRepresentation::SaveUsageAndErrorCounts(
const policy_table::UsageAndErrorCounts& counts) {
+ const_cast<policy_table::UsageAndErrorCounts&>(counts).mark_initialized();
dbms::SQLQuery query(db());
if (!query.Exec(sql_pt::kDeleteAppLevel)) {
LOG4CXX_WARN(logger_, "Incorrect delete from app level.");
@@ -1164,6 +1199,7 @@ bool SQLPTRepresentation::SaveUsageAndErrorCounts(
policy_table::AppLevels::const_iterator it;
const policy_table::AppLevels& app_levels = *counts.app_level;
+ const_cast<policy_table::AppLevels&>(*counts.app_level).mark_initialized();
for (it = app_levels.begin(); it != app_levels.end(); ++it) {
query.Bind(0, it->first);
if (!query.Exec()) {
@@ -1327,9 +1363,12 @@ bool SQLPTRepresentation::IsApplicationRevoked(
if (!query.Prepare(sql_pt::kSelectApplicationRevoked)) {
LOG4CXX_WARN(logger_, "Incorrect select from is_revoked of application");
}
- if (!query.Exec()) {
+
+ query.Bind(0, app_id);
+
+ if (!query.Exec()) {
LOG4CXX_WARN(logger_, "Failed select is_revoked of application");
- return false;
+ return false;
}
return query.IsNull(0) ? false : query.GetBoolean(0);
}