• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "dispatch_rule_parser.h"
16 
17 #include <fstream>
18 #include "hiview_logger.h"
19 #include "sys_event.h"
20 
21 namespace OHOS {
22 namespace HiviewDFX {
23 namespace {
24 const std::map<std::string, uint8_t> EVENT_TYPE_MAP = {
25     {"FAULT", SysEventCreator::FAULT}, {"STATISTIC", SysEventCreator::STATISTIC},
26     {"SECURITY", SysEventCreator::SECURITY}, {"BEHAVIOR", SysEventCreator::BEHAVIOR}
27 };
28 }
29 DEFINE_LOG_TAG("DispatchRuleParser");
30 
DispatchRuleParser(const std::string & filePath)31 DispatchRuleParser::DispatchRuleParser(const std::string& filePath)
32 {
33     Json::Value root;
34     std::ifstream fin(filePath, std::ifstream::binary);
35     if (!fin.is_open()) {
36         HIVIEW_LOGW("failed to open file, path: %{public}s.", filePath.c_str());
37         return;
38     }
39     Json::CharReaderBuilder jsonRBuilder;
40     Json::CharReaderBuilder::strictMode(&jsonRBuilder.settings_);
41     JSONCPP_STRING errs;
42     if (!parseFromStream(jsonRBuilder, fin, &root, &errs)) {
43         HIVIEW_LOGE("parse json file failed, please check the style of json file: %{public}s",
44             filePath.c_str());
45         return;
46     }
47     dispatchRule_ = std::make_shared<DispatchRule>();
48     ParseEventTypes(root);
49     ParseEvents(root);
50     ParseTagEvents(root);
51     ParseDomainRule(root);
52 }
53 
GetRule()54 std::shared_ptr<DispatchRule> DispatchRuleParser::GetRule()
55 {
56     return dispatchRule_;
57 }
58 
ParseEventTypes(const Json::Value & root)59 void DispatchRuleParser::ParseEventTypes(const Json::Value& root)
60 {
61     if (dispatchRule_ == nullptr) {
62         return;
63     }
64     if (root.isNull() || !root.isMember("types") || !root["types"].isArray()) {
65         HIVIEW_LOGD("failed to parse the types");
66         return;
67     }
68     auto jsonTypeArray = root["types"];
69     int jsonSize = static_cast<int>(jsonTypeArray.size());
70     for (int i = 0; i < jsonSize; ++i) {
71         if (!jsonTypeArray[i].isString()) {
72             continue;
73         }
74         std::string key = jsonTypeArray[i].asString();
75         if (EVENT_TYPE_MAP.find(key) != EVENT_TYPE_MAP.end()) {
76             dispatchRule_->typeList.insert(EVENT_TYPE_MAP.at(key));
77         }
78     }
79 }
80 
ParseTagEvents(const Json::Value & root)81 void DispatchRuleParser::ParseTagEvents(const Json::Value& root)
82 {
83     if (dispatchRule_ == nullptr) {
84         return;
85     }
86     if (root.isNull() || !root.isMember("tags") || !root["tags"].isArray()) {
87         HIVIEW_LOGD("failed to parse the tags");
88         return;
89     }
90     auto jsonTagArray = root["tags"];
91     int jsonSize = static_cast<int>(jsonTagArray.size());
92     for (int i = 0; i < jsonSize; i++) {
93         if (!jsonTagArray[i].isString()) {
94             continue;
95         }
96         dispatchRule_->tagList.insert(jsonTagArray[i].asString());
97     }
98 }
99 
ParseEvents(const Json::Value & root)100 void DispatchRuleParser::ParseEvents(const Json::Value& root)
101 {
102     if (dispatchRule_ == nullptr) {
103         return;
104     }
105     if (root.isNull() || !root.isMember("events") || !root["events"].isArray()) {
106         HIVIEW_LOGD("failed to parse the events");
107         return;
108     }
109     auto jsonEventArray = root["events"];
110     int jsonSize = static_cast<int>(jsonEventArray.size());
111     for (int i = 0; i < jsonSize; i++) {
112         if (!jsonEventArray[i].isString()) {
113             continue;
114         }
115         dispatchRule_->eventList.insert(jsonEventArray[i].asString());
116     }
117 }
118 
ParseDomainRule(const Json::Value & root)119 void DispatchRuleParser::ParseDomainRule(const Json::Value& root)
120 {
121     if (dispatchRule_ == nullptr) {
122         return;
123     }
124     if (root.isNull() || !root.isMember("domains") || !root["domains"].isArray()) {
125         HIVIEW_LOGD("failed to parse the domains");
126         return;
127     }
128     auto jsonDomainArray = root["domains"];
129     int jsonSize = static_cast<int>(jsonDomainArray.size());
130     for (int i = 0; i < jsonSize; i++) {
131         if (jsonDomainArray[i].isNull() || !jsonDomainArray[i].isMember("domain")) {
132             continue;
133         }
134         if (!jsonDomainArray[i]["domain"].isString()) {
135             continue;
136         }
137         DomainRule domainRule;
138         std::string domainName = jsonDomainArray[i]["domain"].asString();
139         ParseDomains(jsonDomainArray[i], domainRule);
140         dispatchRule_->domainRuleMap[domainName] = domainRule;
141     }
142 }
143 
ParseDomains(const Json::Value & json,DomainRule & domainRule)144 void DispatchRuleParser::ParseDomains(const Json::Value& json, DomainRule& domainRule)
145 {
146     Json::Value jsonArray;
147     if (json.isMember("include") && json["include"].isArray()) {
148         domainRule.filterType = DomainRule::INCLUDE;
149         jsonArray = json["include"];
150     } else if (json.isMember("exclude") && json["exclude"].isArray()) {
151         domainRule.filterType = DomainRule::EXCLUDE;
152         jsonArray = json["exclude"];
153     } else {
154         return;
155     }
156     int jsonSize = static_cast<int>(jsonArray.size());
157     for (int i = 0; i < jsonSize; i++) {
158         if (!jsonArray[i].isString()) {
159             continue;
160         }
161         domainRule.eventlist.insert(jsonArray[i].asString());
162     }
163 }
164 
FindEvent(const std::string & domain,const std::string & eventName)165 bool DispatchRule::FindEvent(const std::string& domain, const std::string& eventName)
166 {
167     if (eventList.find(eventName) != eventList.end()) {
168         return true;
169     }
170     auto itDomainRule = domainRuleMap.find(domain);
171     if (itDomainRule != domainRuleMap.end()) {
172         return itDomainRule->second.FindEvent(eventName);
173     }
174     return false;
175 }
176 
FindEvent(const std::string & eventName) const177 bool DomainRule::FindEvent(const std::string& eventName) const
178 {
179     if (filterType == INCLUDE) {
180         return eventlist.find(eventName) != eventlist.end();
181     } else {
182         return eventlist.find(eventName) == eventlist.end();
183     }
184 }
185 } // namespace HiviewDFX
186 } // namespace OHOS