[SECARSP-143] Notifications component was implemented and integrated into Dashboard
[platform/core/security/suspicious-activity-monitor.git] / device-agent / samonitor / dpm / common_enforce.cpp
1 /**
2  * Samsung Ukraine R&D Center (SRK under a contract between)
3  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
5  */
6 /**
7  * @file   common_enforce.cpp
8  * @brief  Implementation of common policy
9  * @date   Created Jul 05, 2016
10  * @author Mail to: <A HREF="mailto:a.volkov@samsung.com">Aleksey Volkov, a.volkov@samsung.com</A>
11  */
12
13 #include <thread>
14 #include <chrono>
15 #include "common_enforce.h"
16 #include "logging.h"
17 #include "samonitor_tag.h"
18 #include "dpm_api_mapper.h"
19
20
21 namespace core
22 {
23
24 CommonPolicyEnforce::CommonPolicyEnforce(PolicyEnforce& enforce)
25     : m_enforce(enforce)
26 {
27     LOG_D(TAG, "Register common policy enforce class");
28     m_enforce.RegisterGroup(static_cast<IPolicyGroupEnforce*>(this), std::string("common-policies"));
29 }
30
31 CommonPolicyEnforce::~CommonPolicyEnforce()
32 {
33 }
34
35 bool CommonPolicyEnforce::Init(/*PolicyContext& context*/)
36 {
37     LOG_D(TAG, "Init common policy applier");
38
39     return true;
40 }
41
42 void CommonPolicyEnforce::Deinit()
43 {
44     LOG_D(TAG, "De-Init common policy applier");
45 }
46
47 bool CommonPolicyEnforce::ParseGroup(Json::Value& groupList)
48 {
49     LOG_D(TAG, "...Start common policies parsing and applying...");
50
51     dpm_api::Mapper mapper;
52     bool first_time = true;
53
54     for (auto& node : groupList) {
55         std::string name = node.get("name", "").asString();
56         int         state = node.get("state", 0).asInt();
57         Json::Value items = node.get("items", "");
58
59         if (name.empty()) {
60             LOG_E(TAG, "skiping policy without name.");
61             continue;
62         }
63
64         dpm_api::ErrorCode err;
65
66         std::vector<std::string> v;
67         for (auto& item : items) {
68             v.push_back(item.asString());
69         }
70
71         LOG_D(TAG, "  Enforce policy [%s] to state %d", name.c_str(), state);
72
73         try {
74             if (first_time) {
75                 first_time = false;
76             } else {
77                 std::this_thread::sleep_for(std::chrono::seconds(1));
78             }
79
80             LOG_D(TAG, "  mapper apply %s to state %d", name.c_str(), state);
81
82             err = mapper.apply(name, state, v);
83
84             if (err != dpm_api::SUCCESS) {
85                 LOG_E(TAG, "  Enforce policy [%s] error: %s", name.c_str(), mapper.getErrorString(err));
86             } else {
87                 LOG_D(TAG, "  Enforce policy [%s] ok", name.c_str());
88             }
89         } catch (std::exception& e) {
90             LOG_E(TAG, "  Enforce policy [%s] exception: %s", name.c_str(), e.what());
91         }
92     }
93     LOG_D(TAG, "...Finish common policies parsing and applying...");
94
95     return true;
96 }
97
98 CommonPolicyEnforce commonEnforce(PolicyEnforce::GetInstance());
99
100 } //namespace core