[systeminfo] Prevent possible crash when failure initialization
[platform/core/api/webapi-plugins.git] / src / ml / ml_pipeline_manager.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include "ml_pipeline_manager.h"
18 #include "common/tools.h"
19 #include "ml_pipeline_switch.h"
20
21 using common::PlatformResult;
22 using common::ErrorCode;
23 using common::tools::ReportError;
24 using common::tools::ReportSuccess;
25
26 namespace extension {
27 namespace ml {
28
29 PipelineManager::PipelineManager(common::Instance* instance_ptr) : instance_ptr_{instance_ptr} {
30   ScopeLogger();
31 }
32
33 PipelineManager::~PipelineManager() {
34   ScopeLogger();
35 }
36
37 // PipelineManager::createPipeline() begin
38 PlatformResult PipelineManager::CreatePipeline(int id, const std::string& definition,
39                                                const std::string& state_change_listener_name) {
40   ScopeLogger("id: [%d], definition: [%s], state_change_listener_name: [%s]", id,
41               definition.c_str(), state_change_listener_name.c_str());
42
43   if (pipelines_.count(id)) {
44     LoggerD("The pipeline already exists: [%d]", id);
45     return PlatformResult{ErrorCode::ABORT_ERR, "Could not create pipeline"};
46   }
47
48   std::unique_ptr<Pipeline> pipeline_ptr;
49   auto ret = Pipeline::CreatePipeline(id, definition, state_change_listener_name, instance_ptr_,
50                                       &pipeline_ptr);
51   if (!ret) {
52     return ret;
53   }
54
55   pipelines_.insert({id, std::move(pipeline_ptr)});
56
57   return PlatformResult{};
58 }
59 // PipelineManager::createPipeline() end
60
61 // Pipeline::state begin
62 PlatformResult PipelineManager::GetPipelineState(int id, std::string* out) {
63   ScopeLogger("id: [%d]", id);
64
65   auto pipeline_it = pipelines_.find(id);
66   if (pipelines_.end() == pipeline_it) {
67     LoggerD("Pipeline not found: [%d]", id);
68     *out = "NULL";
69     return PlatformResult{};
70   }
71
72   auto ret = pipeline_it->second->GetState(out);
73   return ret;
74 }
75 // Pipeline::state end
76
77 // Pipeline::start() begin
78 PlatformResult PipelineManager::Start(int id) {
79   ScopeLogger("id: [%d]", id);
80
81   auto pipeline_it = pipelines_.find(id);
82   if (pipelines_.end() == pipeline_it) {
83     LoggerD("Pipeline not found: [%d]", id);
84     return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
85   }
86
87   auto ret = pipeline_it->second->Start();
88   return ret;
89 }
90 // Pipeline::start() end
91
92 // Pipeline::stop() begin
93 PlatformResult PipelineManager::Stop(int id) {
94   ScopeLogger("id: [%d]", id);
95
96   auto pipeline_it = pipelines_.find(id);
97   if (pipelines_.end() == pipeline_it) {
98     LoggerD("Pipeline not found: [%d]", id);
99     return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
100   }
101
102   auto ret = pipeline_it->second->Stop();
103   return ret;
104 }
105 // Pipeline::stop() end
106
107 // Pipeline::dispose() begin
108 PlatformResult PipelineManager::DisposePipeline(int id) {
109   ScopeLogger("id: [%d]", id);
110
111   auto pipeline_it = pipelines_.find(id);
112   if (pipelines_.end() == pipeline_it) {
113     return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
114   }
115
116   /*
117    * Native ml_pipeline_destroy() may fail (I've checked its implementation),
118    * so we shouldn't just call pipelines_.erase(id) and let the ~Pipeline()
119    * destroy the pipeline, but only call pipelines_.erase(id) when
120    * pipeline->Dispose() succeeds.
121    */
122   auto ret = pipeline_it->second->Dispose();
123   if (ret) {
124     pipelines_.erase(id);
125   }
126
127   return ret;
128 }
129 // Pipeline::dispose() end
130
131 // Pipeline::getNodeInfo() begin
132 PlatformResult PipelineManager::GetNodeInfo(int id, std::string& name) {
133   ScopeLogger("id: [%d], name [%s]", id, name.c_str());
134
135   auto pipeline_it = pipelines_.find(id);
136   if (pipelines_.end() == pipeline_it) {
137     LoggerD("Pipeline not found: [%d]", id);
138     return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
139   }
140
141   auto ret = pipeline_it->second->GetNodeInfo(name);
142   return ret;
143 }
144 // Pipeline::getNodeInfo() end
145
146 // Pipeline::getSource() begin
147
148 // Pipeline::getSource() end
149
150 // Pipeline::getSwitch() begin
151 PlatformResult PipelineManager::GetSwitch(const std::string& name, int pipeline_id,
152                                           std::string* type) {
153   ScopeLogger("name: [%s], pipeline_id: [%d]", name.c_str(), pipeline_id);
154
155   auto pipeline_it = pipelines_.find(pipeline_id);
156   if (pipelines_.end() == pipeline_it) {
157     LoggerD("Pipeline not found: [%d]", pipeline_id);
158     return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
159   }
160
161   return pipeline_it->second->GetSwitch(name, type);
162 }
163 // Pipeline::getSwitch() end
164
165 // Pipeline::getValve() begin
166 PlatformResult PipelineManager::GetValve(const std::string& name, int pipeline_id) {
167   ScopeLogger("name: [%s], pipeline_id: [%d]", name.c_str(), pipeline_id);
168
169   auto pipeline_it = pipelines_.find(pipeline_id);
170   if (pipelines_.end() == pipeline_it) {
171     LoggerD("Pipeline not found: [%d]", pipeline_id);
172     return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
173   }
174
175   return pipeline_it->second->GetValve(name);
176 }
177 // Pipeline::getValve() end
178
179 // Pipeline::registerSinkCallback() begin
180
181 // Pipeline::registerSinkCallback() end
182
183 // Pipeline::unregisterSinkCallback() begin
184
185 // Pipeline::unregisterSinkCallback() end
186
187 // Pipeline::registerCustomFilter() begin
188
189 // Pipeline::registerCustomFilter() end
190
191 // Pipeline::unregisterCustomFilter() begin
192
193 // Pipeline::unregisterCustomFilter() end
194
195 // NodeInfo::getProperty() begin
196 PlatformResult PipelineManager::getProperty(int id, const std::string& node_name,
197                                             const std::string& name, const std::string& type,
198                                             picojson::object* property) {
199   ScopeLogger("id: [%d], name [%s], nodeName [%s], type [%s] ", id, name.c_str(), node_name.c_str(),
200               type.c_str());
201
202   auto pipeline_it = pipelines_.find(id);
203   if (pipelines_.end() == pipeline_it) {
204     LoggerD("Pipeline not found: [%d]", id);
205     return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
206   }
207
208   return pipeline_it->second->getProperty(node_name, name, type, property);
209 }
210 // NodeInfo::getProperty() end
211
212 // NodeInfo::setProperty() begin
213 PlatformResult PipelineManager::setProperty(int id, const std::string& node_name,
214                                             const std::string& name, const std::string& type,
215                                             const picojson::value& property) {
216   ScopeLogger("id: [%d], name [%s], nodeName [%s], type [%s] ", id, name.c_str(), node_name.c_str(),
217               type.c_str());
218
219   auto pipeline_it = pipelines_.find(id);
220   if (pipelines_.end() == pipeline_it) {
221     LoggerD("Pipeline not found: [%d]", id);
222     return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
223   }
224
225   return pipeline_it->second->setProperty(node_name, name, type, property);
226 }
227 // NodeInfo::setProperty() end
228
229 // Source::inputTensorsInfo begin
230
231 // Source::inputTensorsInfo end
232
233 // Source::inputData() begin
234
235 // Source::inputData() end
236
237 // Switch::getPadList() begin
238 PlatformResult PipelineManager::SwitchGetPadList(int pipeline_id, const std::string& switch_name,
239                                                  picojson::array* out) {
240   ScopeLogger("pipeline_id: [%d], switch_name: [%s]", pipeline_id, switch_name.c_str());
241
242   auto pipeline_it = pipelines_.find(pipeline_id);
243   if (pipelines_.end() == pipeline_it) {
244     LoggerD("Pipeline not found: [%d]", pipeline_id);
245     return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
246   }
247
248   Switch* switch_ptr = nullptr;
249   auto ret = pipeline_it->second->GetSwitch(switch_name, &switch_ptr);
250   if (!ret) {
251     return ret;
252   }
253
254   return switch_ptr->GetPadList(out);
255 }
256 // Switch::getPadList() end
257
258 // Switch::select() begin
259 PlatformResult PipelineManager::SwitchSelect(int pipeline_id, const std::string& switch_name,
260                                              const std::string& pad_name) {
261   ScopeLogger("pipeline_id: [%d], switch_name: [%s], pad_name: [%s]", pipeline_id,
262               switch_name.c_str(), pad_name.c_str());
263
264   auto pipeline_it = pipelines_.find(pipeline_id);
265   if (pipelines_.end() == pipeline_it) {
266     LoggerD("Pipeline not found: [%d]", pipeline_id);
267     return PlatformResult{ErrorCode::NOT_FOUND_ERR, "Pipeline not found"};
268   }
269
270   Switch* switch_ptr = nullptr;
271   auto ret = pipeline_it->second->GetSwitch(switch_name, &switch_ptr);
272   if (!ret) {
273     return ret;
274   }
275
276   return switch_ptr->Select(pad_name);
277 }
278 // Switch::select() end
279
280 // Valve::setOpen() begin
281
282 // Valve::setOpen() end
283
284 }  // namespace ml
285 }  // namespace extension