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 
16 #include "model_config.h"
17 
18 #include "file_ex.h"
19 
20 #include "config_data_manager.h"
21 #include "json_cfg.h"
22 #include "model_analysis_define.h"
23 #include "model_cfg_marshalling.h"
24 #include "security_guard_log.h"
25 #include "security_guard_utils.h"
26 
27 namespace OHOS::Security::SecurityGuard {
Load(int mode)28 bool ModelConfig::Load(int mode)
29 {
30     std::string path;
31     if (mode == INIT_MODE) {
32         if (FileExists(CONFIG_UPTATE_FILES[MODEL_CFG_INDEX])) {
33             path = CONFIG_UPTATE_FILES[MODEL_CFG_INDEX];
34         } else if (FileExists(CONFIG_PRESET_FILES[MODEL_CFG_INDEX])) {
35             path = CONFIG_PRESET_FILES[MODEL_CFG_INDEX];
36         }
37     } else if (mode == UPDATE_MODE) {
38         if (FileExists(CONFIG_CACHE_FILES[MODEL_CFG_INDEX])) {
39             path = CONFIG_CACHE_FILES[MODEL_CFG_INDEX];
40         }
41     }
42     SGLOGD("path=%{public}s", path.c_str());
43     if (path.empty()) {
44         SGLOGE("path is empty");
45         return false;
46     }
47     stream_ = std::ifstream(path, std::ios::in);
48     if (!stream_.is_open() || !stream_) {
49         SGLOGE("stream error, %{public}s", strerror(errno));
50         return false;
51     }
52     return true;
53 }
54 
Parse()55 bool ModelConfig::Parse()
56 {
57     if (!stream_.is_open() || !stream_) {
58         SGLOGE("stream error");
59         return false;
60     }
61     nlohmann::json jsonObj = nlohmann::json::parse(stream_, nullptr, false);
62     stream_.close();
63 
64     if (jsonObj.is_discarded()) {
65         SGLOGI("json is discarded");
66         return false;
67     }
68 
69     std::vector<ModelCfg> configs;
70     bool success = ParseModelConfig(configs, jsonObj);
71     if (!success) {
72         SGLOGE("parse ModelConfig error");
73         return false;
74     }
75     CacheModelConfig(configs);
76     CacheModelToEvent(configs);
77     SGLOGI("cache ModelConfig success");
78     return true;
79 }
80 
Update()81 bool ModelConfig::Update()
82 {
83     if (!stream_.is_open() || !stream_) {
84         SGLOGE("stream error");
85         return false;
86     }
87     nlohmann::json jsonObj = nlohmann::json::parse(stream_, nullptr, false);
88     stream_.close();
89 
90     if (jsonObj.is_discarded()) {
91         SGLOGI("json is discarded");
92         return false;
93     }
94 
95     std::vector<ModelCfg> configs;
96     bool success = ParseModelConfig(configs, jsonObj);
97     if (!success) {
98         SGLOGE("parse EventConfig error");
99         return false;
100     }
101 
102     SecurityGuardUtils::CopyFile(CONFIG_CACHE_FILES[MODEL_CFG_INDEX], CONFIG_UPTATE_FILES[MODEL_CFG_INDEX]);
103     ConfigDataManager::GetInstance().ResetModelMap();
104     CacheModelConfig(configs);
105     CacheModelToEvent(configs);
106     SGLOGI("cache ModelConfig success");
107     return true;
108 }
109 
ParseModelConfig(std::vector<ModelCfg> & configs,nlohmann::json & jsonObj)110 bool ModelConfig::ParseModelConfig(std::vector<ModelCfg> &configs, nlohmann::json &jsonObj)
111 {
112     return JsonCfg::Unmarshal<ModelCfg>(configs, jsonObj, MODEL_CFG_KEY);
113 };
114 
CacheModelConfig(const std::vector<ModelCfg> & configs)115 void ModelConfig::CacheModelConfig(const std::vector<ModelCfg> &configs)
116 {
117     for (const ModelCfg &config : configs) {
118         SGLOGD("modelId=%{public}u", config.modelId);
119         ConfigDataManager::GetInstance().InsertModelMap(config.modelId, config);
120     }
121 }
122 
CacheModelToEvent(const std::vector<ModelCfg> & configs)123 void ModelConfig::CacheModelToEvent(const std::vector<ModelCfg> &configs)
124 {
125     for (const ModelCfg &config : configs) {
126         SGLOGD("modelId=%{public}u", config.modelId);
127         std::set<int64_t> set;
128         for (int64_t event : config.eventList) {
129             set.emplace(event);
130         }
131         ConfigDataManager::GetInstance().InsertModelToEventMap(config.modelId, set);
132     }
133 }
134 } // OHOS::Security::SecurityGuard