• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_config.h"
16 
17 #include <fstream>
18 #include <algorithm>
19 #include "logger.h"
20 #include "sys_event.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace {
25 const std::map<std::string, uint8_t> EVENT_TYPE_MAP = {
26     {"FAULT", SysEventCreator::FAULT}, {"STATISTIC", SysEventCreator::STATISTIC},
27     {"SECURITY", SysEventCreator::SECURITY}, {"BEHAVIOR", SysEventCreator::BEHAVIOR}
28 };
29 }
30 DEFINE_LOG_TAG("HiView-DispatchRule");
31 
HiviewRuleParser(const std::string & filePath)32 HiviewRuleParser::HiviewRuleParser(const std::string &filePath)
33 {
34     Json::Value root;
35     std::ifstream fin(filePath, std::ifstream::binary);
36 #ifdef JSONCPP_VERSION_STRING
37     Json::CharReaderBuilder jsonRBuilder;
38     Json::CharReaderBuilder::strictMode(&jsonRBuilder.settings_);
39     JSONCPP_STRING errs;
40     if (!parseFromStream(jsonRBuilder, fin, &root, &errs)) {
41 #else
42     Json::Reader reader(Json::Features::strictMode());
43     if (!reader.parse(fin, root)) {
44 #endif
45         HIVIEW_LOGE("parse json file failed, please check the style of json file: %{public}s",
46             filePath.c_str());
47         return;
48     }
49     dispatchRule_ = std::make_shared<DispatchRule>();
50     ParseEventTypes(root);
51     ParseEvents(root);
52     ParseTagEvents(root);
53     ParseDomainRule(root);
54 }
55 
56 std::shared_ptr<DispatchRule> HiviewRuleParser::getRule()
57 {
58     return dispatchRule_;
59 }
60 
61 void HiviewRuleParser::ParseEventTypes(const Json::Value &root)
62 {
63     if (dispatchRule_ == nullptr) {
64         return;
65     }
66     if (root.isNull() || !root.isMember("types") || !root["types"].isArray()) {
67         HIVIEW_LOGE("ParseEventTypes failed");
68         return;
69     }
70     auto jsonTypeArray = root["types"];
71     int jsonSize = static_cast<int>(jsonTypeArray.size());
72     for (int i = 0; i < jsonSize; ++i) {
73         if (!jsonTypeArray[i].isString()) {
74             continue;
75         }
76         std::string key = jsonTypeArray[i].asString();
77         if (EVENT_TYPE_MAP.find(key) != EVENT_TYPE_MAP.end()) {
78             dispatchRule_->typeList.insert(EVENT_TYPE_MAP.at(key));
79         }
80     }
81 }
82 
83 void HiviewRuleParser::ParseTagEvents(const Json::Value &root)
84 {
85     if (dispatchRule_ == nullptr) {
86         return;
87     }
88     if (root.isNull() || !root.isMember("tags") || !root["tags"].isArray()) {
89         HIVIEW_LOGE("ParseTagEvents failed");
90         return;
91     }
92     auto jsonTagArray = root["tags"];
93     int jsonSize = static_cast<int>(jsonTagArray.size());
94     for (int i = 0; i < jsonSize; i++) {
95         if (!jsonTagArray[i].isString()) {
96             continue;
97         }
98         dispatchRule_->tagList.insert(jsonTagArray[i].asString());
99     }
100 }
101 
102 void HiviewRuleParser::ParseEvents(const Json::Value &root)
103 {
104     if (dispatchRule_ == nullptr) {
105         return;
106     }
107     if (root.isNull() || !root.isMember("events") || !root["events"].isArray()) {
108         HIVIEW_LOGE("ParseEvents failed");
109         return;
110     }
111     auto jsonEventArray = root["events"];
112     int jsonSize = static_cast<int>(jsonEventArray.size());
113     for (int i = 0; i < jsonSize; i++) {
114         if (!jsonEventArray[i].isString()) {
115             continue;
116         }
117         dispatchRule_->eventList.insert(jsonEventArray[i].asString());
118     }
119 }
120 
121 void HiviewRuleParser::ParseDomainRule(const Json::Value &root)
122 {
123     if (dispatchRule_ == nullptr) {
124         return;
125     }
126     if (root.isNull() || !root.isMember("domains") || !root["domains"].isArray()) {
127         HIVIEW_LOGE("ParseDomainRule failed");
128         return;
129     }
130     auto jsonDomainArray = root["domains"];
131     int jsonSize = static_cast<int>(jsonDomainArray.size());
132     for (int i = 0; i < jsonSize; i++) {
133         if (jsonDomainArray[i].isNull() || !jsonDomainArray[i].isMember("domain")) {
134             continue;
135         }
136         if (!jsonDomainArray[i]["domain"].isString()) {
137             continue;
138         }
139         DomainRule domainRule;
140         std::string domainName = jsonDomainArray[i]["domain"].asString();
141         ParseDomains(jsonDomainArray[i], domainRule);
142         dispatchRule_->domainRuleMap[domainName] = domainRule;
143     }
144 }
145 
146 void HiviewRuleParser::ParseDomains(const Json::Value &json, DomainRule &domainRule)
147 {
148     Json::Value jsonArray;
149     if (json.isMember("include") && json["include"].isArray()) {
150         domainRule.filterType = DomainRule::INCLUDE;
151         jsonArray = json["include"];
152     } else if (json.isMember("exclude") && json["exclude"].isArray()) {
153         domainRule.filterType = DomainRule::EXCLUDE;
154         jsonArray = json["exclude"];
155     } else {
156         return;
157     }
158     int jsonSize = static_cast<int>(jsonArray.size());
159     for (int i = 0; i < jsonSize; i++) {
160         if (!jsonArray[i].isString()) {
161             continue;
162         }
163         domainRule.eventlist.insert(jsonArray[i].asString());
164     }
165 }
166 
167 bool DispatchRule::FindEvent(const std::string &domain, const std::string &eventName)
168 {
169     if (eventList.find(eventName) != eventList.end()) {
170         return true;
171     }
172     auto itDomainRule = domainRuleMap.find(domain);
173     if (itDomainRule != domainRuleMap.end()) {
174         return itDomainRule->second.FindEvent(eventName);
175     }
176     return false;
177 }
178 
179 bool DomainRule::FindEvent(const std::string &eventName) const
180 {
181     if (filterType == INCLUDE) {
182         return eventlist.find(eventName) != eventlist.end();
183     } else {
184         return eventlist.find(eventName) == eventlist.end();
185     }
186 }
187 }// namespace HiviewDFX
188 }// namespace OHOS