Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SSMCore / src / SSMInterface / SoftSensorManager.cpp
1 /******************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ******************************************************************/
20 #include "SSMInterface/SoftSensorManager.h"
21 #include "QueryProcessor/QueryEngine.h"
22 #include "QueryProcessor/ContextModel.h"
23 #include "QueryProcessor/ConditionedModel.h"
24 #include "QueryProcessor/EvaluationEngine.h"
25 #include "QueryProcessor/PropagationEngine.h"
26 #include "SensorProcessor/SensingEngine.h"
27 #include "SensorProcessor/ContextExecutor.h"
28 #include "SensorProcessor/ContextDataReader.h"
29 #include "SensorProcessor/ResourceFinder.h"
30 #include "rapidxml/rapidxml.hpp"
31
32 inline bool operator<( const OID &lhs, const OID &rhs )
33 {
34     int ret = memcmp( &lhs, &rhs, sizeof(OID));
35     return (ret < 0 ? true : false );
36 }
37
38 SSMRESULT CSoftSensorManager::finalConstruct()
39 {
40     return SSM_S_OK;
41 }
42
43 void CSoftSensorManager::finalRelease()
44 {
45 }
46
47 SSMRESULT CSoftSensorManager::initializeCore(std::string xmlDescription)
48 {
49     SSMRESULT                   res = SSM_E_FAIL;
50     rapidxml::xml_document<>    xmlDoc;
51     std::string                 strKey;
52     std::string                 strValue;
53     rapidxml::xml_node<>        *root = NULL;
54     rapidxml::xml_node<>        *itemSSMCore = NULL;
55     rapidxml::xml_node<>        *itemDevice = NULL;
56
57     std::string                 name;
58     std::string                 type;
59     std::string                 pathSoftSensors;
60     std::string                 pathDescription;
61
62     std::string                 copyDescription = xmlDescription.c_str();
63
64     try
65     {
66         xmlDoc.parse<0>((char *)copyDescription.c_str());
67
68         root = xmlDoc.first_node();
69
70         if (!root)
71         {
72             throw rapidxml::parse_error("No Root Element", 0);
73         }
74
75         strKey = root->name();
76
77         if (strKey != "SSMCore")
78         {
79             throw rapidxml::parse_error("Invalid root tag name", 0);
80         }
81
82         for (itemSSMCore = root->first_node(); itemSSMCore; itemSSMCore = itemSSMCore->next_sibling())
83         {
84             strKey = itemSSMCore->name();
85
86             if (strKey == "Device")
87             {
88                 for (itemDevice = itemSSMCore->first_node(); itemDevice; itemDevice = itemDevice->next_sibling())
89                 {
90                     strKey = itemDevice->name();
91
92                     if (strKey == "Name")
93                     {
94                         name = itemDevice->value();
95                     }
96                     else if (strKey == "Type")
97                     {
98                         type = itemDevice->value();
99                     }
100                     else
101                     {
102                         ;/*NULL*/
103                     }
104                 }
105             }
106             else if (strKey == "Config")
107             {
108                 for (itemDevice = itemSSMCore->first_node(); itemDevice; itemDevice = itemDevice->next_sibling())
109                 {
110                     strKey = itemDevice->name();
111
112                     if (strKey == "SoftSensorRepository")
113                     {
114                         pathSoftSensors = itemDevice->value();
115                     }
116                     else if (strKey == "SoftSensorDescription")
117                     {
118                         pathDescription = itemDevice->value();
119                     }
120                     else
121                     {
122                         ;/*NULL*/
123                     }
124                 }
125             }
126             else
127             {
128                 ;/*NULL*/
129             }
130         }
131     }
132     catch (rapidxml::parse_error &e)
133     {
134         SSM_CLEANUP_ASSERT(SSM_E_INVALIDXML);
135     }
136
137     SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_ISensingEngine, (IBase **)&m_pSensingEngine));
138     SSM_CLEANUP_ASSERT(m_pSensingEngine->queryInterface(OID_IContextRepository,
139                        (IBase **)&m_pContextRepository));
140     SSM_CLEANUP_ASSERT(m_pContextRepository->initRepository(name, type, pathSoftSensors,
141                        pathDescription));
142
143     SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IPropagationEngine, (IBase **)&m_pPropagationEngine));
144
145 CLEANUP:
146     if (res != SSM_S_OK)
147     {
148         terminateCore(false);
149     }
150
151     return res;
152 }
153
154 SSMRESULT CSoftSensorManager::startCore()
155 {
156     SSMRESULT res = SSM_E_FAIL;
157
158     SSM_CLEANUP_ASSERT(m_pContextRepository->startResourceFinder());
159
160 CLEANUP:
161     return res;
162 }
163
164 SSMRESULT CSoftSensorManager::stopCore()
165 {
166     SSMRESULT res = SSM_E_FAIL;
167
168     SSM_CLEANUP_ASSERT(m_pContextRepository->stopResourceFinder());
169
170 CLEANUP:
171     return res;
172 }
173
174 SSMRESULT CSoftSensorManager::terminateCore(bool factoryResetFlag)
175 {
176     return SSM_S_OK;
177 }
178
179 SSMRESULT CSoftSensorManager::createQueryEngine(IQueryEngine **ppQueryEngine)
180 {
181     SSMRESULT res = SSM_E_FAIL;
182     IQueryEngineInternal    *pQueryEngineInternal = NULL;
183     SSM_CLEANUP_ASSERT(CreateInstance(OID_IQueryEngineInternal, (IBase **)&pQueryEngineInternal));
184     *ppQueryEngine = (IQueryEngine *)pQueryEngineInternal;
185
186 CLEANUP:
187     return res;
188 }
189
190 unsigned long CSoftSensorManager::releaseQueryEngine(IQueryEngine *pQueryEngine)
191 {
192     IQueryEngineInternal    *pQueryEngineInternal = NULL;
193     pQueryEngineInternal = (IQueryEngineInternal *)(CQueryEngine *)pQueryEngine;
194
195     return pQueryEngineInternal->release();
196 }
197
198 SSMRESULT CSoftSensorManager::getInstalledModelList(std::vector<ISSMResource *> *pList)
199 {
200     m_pSensingEngine->getList(pList);
201
202     return SSM_S_OK;
203 }
204
205 CSimpleMutex                *g_mtxGlobalInstance = NULL;
206 std::map<OID, IBase *>       *g_globalInstance = NULL;
207 IThreadPool                 *g_pThreadPool = NULL;
208
209 SSMRESULT CreateGlobalInstance(const OID &objectID, IBase **ppvObject)
210 {
211     SSMRESULT res = SSM_E_NOINTERFACE;
212
213     if (ppvObject == NULL)
214     {
215         return SSM_E_POINTER;
216     }
217
218     *ppvObject = NULL;
219
220     g_mtxGlobalInstance->lock();
221     res = SSM_S_FALSE;
222
223     if (IsEqualOID(OID_ITasker, objectID))
224     {
225         if (g_globalInstance->find(OID_ITasker) == g_globalInstance->end())
226         {
227             SSM_CLEANUP_ASSERT(CreateInstance(OID_ITasker, ppvObject));
228         }
229     }
230     else if (IsEqualOID(OID_IThreadPool, objectID))
231     {
232         if (g_globalInstance->find(OID_IThreadPool) == g_globalInstance->end())
233         {
234             SSM_CLEANUP_ASSERT(CreateInstance(OID_IThreadPool, ppvObject));
235         }
236     }
237     else if (IsEqualOID(OID_IEvaluationEngine, objectID))
238     {
239         if (g_globalInstance->find(OID_IEvaluationEngine) == g_globalInstance->end())
240         {
241             SSM_CLEANUP_ASSERT(CreateInstance(OID_IEvaluationEngine, ppvObject));
242         }
243     }
244     else if (IsEqualOID(OID_IPropagationEngine, objectID))
245     {
246         if (g_globalInstance->find(OID_IPropagationEngine) == g_globalInstance->end())
247         {
248             SSM_CLEANUP_ASSERT(CreateInstance(OID_IPropagationEngine, ppvObject));
249         }
250     }
251     else if (IsEqualOID(OID_IContextRepository, objectID))
252     {
253         if (g_globalInstance->find(OID_IContextRepository) == g_globalInstance->end())
254         {
255             SSM_CLEANUP_ASSERT(CreateInstance(OID_IContextRepository, ppvObject));
256         }
257     }
258     else if (IsEqualOID(OID_IContextDataReader, objectID))
259     {
260         if (g_globalInstance->find(OID_IContextDataReader) == g_globalInstance->end())
261         {
262             SSM_CLEANUP_ASSERT(CreateInstance(OID_IContextDataReader, ppvObject));
263         }
264     }
265     else if (IsEqualOID(OID_ISensingEngine, objectID))
266     {
267         if (g_globalInstance->find(OID_ISensingEngine) == g_globalInstance->end())
268         {
269             SSM_CLEANUP_ASSERT(CreateInstance(OID_ISensingEngine, ppvObject));
270         }
271     }
272     else
273     {
274         res = SSM_E_NOINTERFACE;
275     }
276
277     switch (res)
278     {
279         case SSM_S_OK:
280             (*g_globalInstance)[objectID] = *ppvObject;
281             break;
282
283         case SSM_S_FALSE:
284             (*g_globalInstance)[objectID]->addRef();
285             *ppvObject = (*g_globalInstance)[objectID];
286             res = SSM_S_OK;
287             break;
288
289         default:
290             goto CLEANUP;
291     }
292
293 CLEANUP:
294     g_mtxGlobalInstance->unlock();
295     return res;
296 }
297
298 SSMRESULT CreateInstance(const OID &objectID, IBase **ppObject)
299 {
300     SSMRESULT res = SSM_E_NOINTERFACE;
301
302     if (ppObject == NULL)
303     {
304         return SSM_E_POINTER;
305     }
306
307     *ppObject = NULL;
308
309     if (IsEqualOID(OID_ITasker, objectID))
310     {
311         SSM_CLEANUP_ASSERT(CreateNewObject<CTasker>(objectID, ppObject));
312     }
313     else if (IsEqualOID(OID_IWorkerThread, objectID))
314     {
315         SSM_CLEANUP_ASSERT(CreateNewObject<CWorkerThread>(objectID, ppObject));
316     }
317     else if (IsEqualOID(OID_IThreadPool, objectID))
318     {
319         SSM_CLEANUP_ASSERT(CreateNewObject<CThreadPool>(objectID, ppObject));
320     }
321     else if (IsEqualOID(OID_IEvaluationEngine, objectID))
322     {
323         SSM_CLEANUP_ASSERT(CreateNewObject<CEvaluationEngine>(objectID, ppObject));
324     }
325     else if (IsEqualOID(OID_IPropagationEngine, objectID))
326     {
327         SSM_CLEANUP_ASSERT(CreateNewObject<CPropagationEngine>(objectID, ppObject));
328     }
329     else if (IsEqualOID(OID_IContextRepository, objectID))
330     {
331         SSM_CLEANUP_ASSERT(CreateNewObject<CContextRepository>(objectID, ppObject));
332     }
333     else if (IsEqualOID(OID_ISensingEngine, objectID))
334     {
335         SSM_CLEANUP_ASSERT(CreateNewObject<CSensingEngine>(objectID, ppObject));
336     }
337     else if (IsEqualOID(OID_IContextExecutor, objectID))
338     {
339         SSM_CLEANUP_ASSERT(CreateNewObject<CContextExecutor>(objectID, ppObject));
340     }
341     else if (IsEqualOID(OID_IContextModel, objectID))
342     {
343         SSM_CLEANUP_ASSERT(CreateNewObject<CContextModel>(objectID, ppObject));
344     }
345     else if (IsEqualOID(OID_IConditionedModel, objectID))
346     {
347         SSM_CLEANUP_ASSERT(CreateNewObject<CConditionedModel>(objectID, ppObject));
348     }
349     else if (IsEqualOID(OID_IConditionedQueryResult, objectID))
350     {
351         SSM_CLEANUP_ASSERT(CreateNewObject<CConditionedQueryResult>(objectID, ppObject));
352     }
353     else if (IsEqualOID(OID_IConditionedQuery, objectID))
354     {
355         SSM_CLEANUP_ASSERT(CreateNewObject<CConditionedQuery>(objectID, ppObject));
356     }
357     else if (IsEqualOID(OID_IResourceFinder, objectID))
358     {
359         SSM_CLEANUP_ASSERT(CreateNewObject<CResourceFinder>(objectID, ppObject));
360     }
361     else if (IsEqualOID(OID_IQueryEngineInternal, objectID))
362     {
363         SSM_CLEANUP_ASSERT(CreateNewObject<CQueryEngine>(objectID, ppObject));
364     }
365     else if (IsEqualOID(OID_ISoftSensorManager, objectID))
366     {
367         SSM_CLEANUP_ASSERT(CreateNewObject<CSoftSensorManager>(objectID, ppObject));
368     }
369     else if (IsEqualOID(OID_IContextDataReader, objectID))
370     {
371         SSM_CLEANUP_ASSERT(CreateNewObject<CContextDataReader>(objectID, ppObject));
372     }
373
374 CLEANUP:
375     return res;
376 }
377
378 SSMRESULT CreateGlobalInstanceRepo()
379 {
380     SSMRESULT res = SSM_E_FAIL;
381
382     if (g_mtxGlobalInstance != NULL)
383         SSM_CLEANUP_ASSERT(SSM_E_OUTOFMEMORY);
384
385     if (g_globalInstance != NULL)
386         SSM_CLEANUP_ASSERT(SSM_E_OUTOFMEMORY);
387
388     g_mtxGlobalInstance = new CSimpleMutex();
389     SSM_CLEANUP_NULL_ASSERT(g_mtxGlobalInstance);
390     g_globalInstance = new std::map<OID, IBase *>();
391     SSM_CLEANUP_NULL_ASSERT(g_globalInstance);
392
393     SSM_CLEANUP_ASSERT(CreateGlobalInstance(OID_IThreadPool, (IBase **)&g_pThreadPool));
394
395 CLEANUP:
396     return res;
397 }
398
399 SSMRESULT DestroyGlobalInstanceRepo()
400 {
401     SSMRESULT res = SSM_E_FAIL;
402
403     SSM_CLEANUP_ASSERT(g_pThreadPool->destroyThreadPool());
404
405     SAFE_RELEASE(g_pThreadPool);
406     SAFE_DELETE(g_mtxGlobalInstance);
407     SAFE_DELETE(g_globalInstance);
408
409 CLEANUP:
410     return res;
411 }