Implementation of APIs for managing resource list and callback
[platform/upstream/iotivity.git] / service / simulator / src / resource_manager.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 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
21 #include "resource_manager.h"
22 #include "simulator_logger.h"
23
24 int ResourceManager::id;
25 ResourceManager *ResourceManager::getInstance()
26 {
27     static ResourceManager s_instance;
28     return &s_instance;
29 }
30
31 ResourceManager::ResourceManager()
32     : m_resourceCreator(new SimulatorResourceCreator()) {}
33
34 ResourceManager::~ResourceManager()
35 {
36     delete m_resourceCreator;
37 }
38
39 SimulatorResourceServerPtr ResourceManager::createResource(const std::string &configPath,
40         SimulatorResourceServer::ResourceModelChangedCB callback)
41 {
42     /**
43      * TODO: Temporarily creating the light resource for testing the basic flow
44      * Once the config parser is included this method will simulate the resource based on the config file
45      */
46     SimulatorResourceServerPtr simulatorResource = m_resourceCreator->createLightResoure();
47     simulatorResource->setModelChangeCallback(callback);
48     std::string uri = getURI(simulatorResource->getURI());
49     if(uri.empty())
50     {
51         SIM_LOG(ILogger::ERROR, "Cannot register resource. Resource URI is empty");
52         return NULL;
53     }
54     simulatorResource->setURI(uri);
55     SimulatorResult result = simulatorResource->start();
56     if (SIMULATOR_SUCCESS != result)
57     {
58         SIM_LOG(ILogger::ERROR, "Failed to register resource [" << simulatorResource->getURI() <<
59                 "] with platform");
60         return NULL;
61     }
62
63     // Add the resource to resource list table
64     std::lock_guard<std::recursive_mutex> lock(m_listMutex);
65     m_resourceList[simulatorResource->getResourceType()].insert(
66         std::pair<std::string, SimulatorResourceServerPtr>(simulatorResource->getURI(), simulatorResource));
67     return simulatorResource;
68 }
69
70 std::vector<SimulatorResourceServerPtr> ResourceManager::createResource(
71     const std::string &configPath,
72     const int count, SimulatorResourceServer::ResourceModelChangedCB callback)
73 {
74     std::vector<SimulatorResourceServerPtr> resourceList;
75     for (int i = 0; i < count; i++)
76     {
77         /**
78          * TODO: Temporarily creating the light resource for testing the basic flow
79          * Once the config parser is included this method will simulate the resource based on the config file
80          */
81         SimulatorResourceServerPtr simulatorResource = m_resourceCreator->createLightResoure();
82         simulatorResource->setModelChangeCallback(callback);
83         std::string uri = getURI(simulatorResource->getURI());
84         if(uri.empty())
85         {
86             SIM_LOG(ILogger::ERROR, "Cannot register resource. Resource URI is empty");
87             break;
88         }
89         simulatorResource->setURI(uri);
90         SimulatorResult result = simulatorResource->start();
91         if (SIMULATOR_SUCCESS != result)
92         {
93             resourceList.clear();
94             SIM_LOG(ILogger::ERROR, "Failed to register resources!");
95             break;
96         }
97         else
98         {
99             resourceList.push_back(simulatorResource);
100         }
101     }
102
103     // Add the resource to resource list table
104     std::lock_guard<std::recursive_mutex> lock(m_listMutex);
105     for (auto & resource : resourceList)
106     {
107         m_resourceList[resource->getResourceType()].insert(
108             std::pair<std::string, SimulatorResourceServerPtr>(resource->getURI(), resource));
109     }
110
111     return resourceList;
112 }
113
114 std::vector<SimulatorResourceServerPtr> ResourceManager::getResources(
115     const std::string &resourceType)
116 {
117     std::lock_guard<std::recursive_mutex> lock(m_listMutex);
118
119     std::vector<SimulatorResourceServerPtr> resourceList;
120     for (auto resourceTableEntry : m_resourceList)
121     {
122         if (!resourceType.empty() && resourceType.compare(resourceTableEntry.first))
123             continue;
124
125         for (auto resourceEntry : resourceTableEntry.second)
126         {
127             resourceList.push_back(resourceEntry.second);
128         }
129     }
130
131     return resourceList;
132 }
133
134 SimulatorResult ResourceManager::deleteResource(SimulatorResourceServerPtr &resource)
135 {
136     std::lock_guard<std::recursive_mutex> lock(m_listMutex);
137
138     if (nullptr == resource)
139         return SIMULATOR_BAD_INPUT;
140
141     SimulatorResult result = SIMULATOR_RESOURCE_NOT_FOUND;
142
143     try
144     {
145         auto resourceTableEntry = m_resourceList.find(resource->getResourceType());
146         if (m_resourceList.end() != resourceTableEntry)
147         {
148             auto resourceEntry = resourceTableEntry->second.find(resource->getURI());
149             if (resourceTableEntry->second.end() != resourceEntry)
150             {
151                 if (SIMULATOR_SUCCESS == resource->stop())
152                 {
153                     resourceTableEntry->second.erase(resourceEntry);
154                     result = SIMULATOR_SUCCESS;
155                 }
156                 else
157                 {
158                     result = SIMULATOR_ERROR;
159                 }
160             }
161         }
162     }
163     catch (OC::OCException &except)
164     {
165         SIM_LOG(ILogger::ERROR, except.reason() << except.code())
166         result = SIMULATOR_ERROR;
167     }
168
169     return result;
170 }
171
172 SimulatorResult ResourceManager::deleteResources(const std::string &resourceType)
173 {
174     std::lock_guard<std::recursive_mutex> lock(m_listMutex);
175
176     SimulatorResult result = SIMULATOR_RESOURCE_NOT_FOUND;
177     try
178     {
179         for (auto & resourceTableEntry : m_resourceList)
180         {
181             if (!resourceType.empty() && resourceType.compare(resourceTableEntry.first))
182                 continue;
183
184             for (auto & resourceEntry : resourceTableEntry.second)
185             {
186                 SimulatorResourceServerPtr resource = resourceEntry.second;
187                 if (SIMULATOR_SUCCESS == resource->stop())
188                 {
189                     resourceTableEntry.second.erase(resourceTableEntry.second.find(resource->getURI()));
190                     result = SIMULATOR_SUCCESS;
191                 }
192                 else
193                 {
194                     return SIMULATOR_ERROR;
195                 }
196             }
197         }
198     }
199     catch (OC::OCException &except)
200     {
201         SIM_LOG(ILogger::ERROR, except.reason() << except.code())
202         result = SIMULATOR_ERROR;
203     }
204
205     return result;
206 }
207
208 std::string ResourceManager::getURI(std::string uri)
209 {
210     if(uri.empty())
211     {
212         return uri;
213     }
214     std::ostringstream os;
215     os << uri;
216     if ('/' != uri[uri.length() - 1])
217         os << '/';
218     os << "simulator/" << id++;
219     return os.str();
220 }
221