60d669dad4ed7573fcf37d5bbd765b60610d1d05
[platform/core/security/suspicious-activity-monitor.git] / device-agent / samonitor / dpm / tvext_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) 2017 Samsung Electronics Co., Ltd. All rights reserved.
5  */
6 /**
7  * @file   iot_tvext_enforce.cpp
8  * @brief  Implementation of TV 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
14 #include "iot_tvext_enforce.h"
15 #include "logging.h"
16 #include "dpm_api_mapper.h"
17 #include <thread>
18 #include <chrono>
19
20 #define TAG "nmdaemon"
21
22 namespace iot
23 {
24 namespace core
25 {
26
27 TvExtPolicyEnforce::TvExtPolicyEnforce(PolicyEnforce& enforce)
28     : m_enforce(enforce)
29 {
30     LOG_D(TAG, "Register tvext policy enforce class");
31     m_enforce.RegisterGroup(static_cast<IPolicyGroupEnforce*>(this), std::string("tv-extension"));
32 }
33
34 TvExtPolicyEnforce::~TvExtPolicyEnforce()
35 {
36 }
37
38 bool TvExtPolicyEnforce::Init()
39 {
40     LOG_D(TAG, "Init tvext policy applier");
41
42     return true;
43 }
44
45 void TvExtPolicyEnforce::Deinit()
46 {
47     LOG_D(TAG, "De-Init tvext policy applier");
48 }
49
50 bool TvExtPolicyEnforce::ParseGroup(Json::Value& groupList)
51 {
52     LOG_D(TAG, "...Start tvext policies parsing and applying...");
53
54     dpm_api::Mapper mapper;
55     bool first_time = true;
56
57     for (auto& node : groupList) {
58         std::string name = node.get("name", "").asString();
59         int         state = node.get("state", 0).asInt();
60         Json::Value items = node.get("items", "");
61
62         if (name.empty()) {
63             LOG_E(TAG, "skiping policy without name.");
64             continue;
65         }
66
67         dpm_api::ErrorCode err;
68
69         std::vector<std::string> v;
70         for (auto& item : items) {
71             v.push_back(item.asString());
72         }
73
74         LOG_D(TAG, "  Enforce policy [%s] to state %d", name.c_str(), state);
75
76         try {
77             if (first_time) {
78                 first_time = false;
79             } else {
80                 std::this_thread::sleep_for(std::chrono::seconds(1));
81             }
82
83             LOG_D(TAG, "  mapper apply %s to state %d", name.c_str(), state);
84
85             err = mapper.apply(name, state, v);
86
87             if (err != dpm_api::SUCCESS) {
88                 LOG_E(TAG, "  Enforce policy [%s] error: %s", name.c_str(), mapper.getErrorString(err));
89             } else {
90                 LOG_D(TAG, "  Enforce policy [%s] ok", name.c_str());
91             }
92         } catch (std::exception& e) {
93             LOG_E(TAG, "  Enforce policy [%s] exception: %s", name.c_str(), e.what());
94         }
95     }
96     LOG_D(TAG, "...Finish tvext policies parsing and applying...");
97
98     return true;
99 }
100
101 TvExtPolicyEnforce tvextEnforce(PolicyEnforce::GetInstance());
102
103 } //namespace core
104 } //namespace iot