1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "resource_manager.h"
22 #include "simulator_logger.h"
25 #define TAG "RESOURCE_MANAGER"
27 ResourceManager *ResourceManager::getInstance()
29 static ResourceManager s_instance;
33 SimulatorResourceServerSP ResourceManager::createResource(const std::string &configPath,
34 SimulatorResourceServer::ResourceModelChangedCB callback)
36 OC_LOG_V(INFO, "Create resource request : config=%s", configPath.c_str());
39 if (configPath.empty())
41 OC_LOG(ERROR, TAG, "Invalid config file path!");
42 throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Invalid RAML file path!");
47 OC_LOG(ERROR, TAG, "Invalid callback!");
48 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
51 return buildResource(configPath, callback);
54 std::vector<SimulatorResourceServerSP> ResourceManager::createResource(
55 const std::string &configPath, unsigned short count,
56 SimulatorResourceServer::ResourceModelChangedCB callback)
58 OC_LOG_V(INFO, "Create multiple resource request : config=%s, count=%d", configPath.c_str(),
62 if (configPath.empty())
64 OC_LOG(ERROR, TAG, "Invalid config file path!");
65 throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Invalid RAML file path!");
70 OC_LOG(ERROR, TAG, "Invalid count value!");
71 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid count value!");
76 OC_LOG(ERROR, TAG, "Invalid callback!");
77 throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
80 std::vector<SimulatorResourceServerSP> resourceList;
83 for (unsigned short i = 0; i < count; i++)
85 OC_LOG_V(INFO, TAG, "Creating resource [%d]", i + 1);
86 SIM_LOG(ILogger::INFO, "Creating resource [" << i + 1 << "]");
88 SimulatorResourceServerSP resource = buildResource(configPath, callback);
94 resourceList.push_back(resource);
97 SIM_LOG(ILogger::INFO, "[" << resourceList.size() << " out of " << count <<
98 "] resource(s) created successfully.");
103 std::vector<SimulatorResourceServerSP> ResourceManager::getResources(
104 const std::string &resourceType)
106 std::lock_guard<std::recursive_mutex> lock(m_lock);
108 std::vector<SimulatorResourceServerSP> resourceList;
109 for (auto resourceTableEntry : m_resources)
111 if (!resourceType.empty() && resourceType.compare(resourceTableEntry.first))
114 for (auto resourceEntry : resourceTableEntry.second)
116 resourceList.push_back(resourceEntry.second);
123 void ResourceManager::deleteResource(const SimulatorResourceServerSP &resource)
127 OC_LOG(ERROR, TAG, "Invalid resource object!");
128 throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Invalid resource object!");
131 std::lock_guard<std::recursive_mutex> lock(m_lock);
132 auto resourceTableEntry = m_resources.find(resource->getResourceType());
133 if (m_resources.end() != resourceTableEntry)
135 auto resourceEntry = resourceTableEntry->second.find(resource->getURI());
136 if (resourceTableEntry->second.end() != resourceEntry)
138 SimulatorResourceServerImplSP resourceImpl =
139 std::dynamic_pointer_cast<SimulatorResourceServerImpl>(resource);
140 resourceImpl->stop();
141 resourceTableEntry->second.erase(resourceEntry);
142 SIM_LOG(ILogger::INFO, "Resource (" << resource->getURI() <<
143 ") deleted successfully.");
148 void ResourceManager::deleteResources(const std::string &resourceType)
150 std::lock_guard<std::recursive_mutex> lock(m_lock);
151 for (auto & resourceTableEntry : m_resources)
153 if (!resourceType.empty() && resourceType.compare(resourceTableEntry.first))
156 for (auto & resourceEntry : resourceTableEntry.second)
158 SimulatorResourceServerSP resource = resourceEntry.second;
159 SimulatorResourceServerImplSP resourceImpl =
160 std::dynamic_pointer_cast<SimulatorResourceServerImpl>(resource);
161 resourceImpl->stop();
162 SIM_LOG(ILogger::INFO, "Resource (" << resource->getURI() <<
163 ") deleted successfully.");
166 // Erase the entry for resource type from resources list
167 m_resources.erase(resourceTableEntry.first);
172 * This method does not validate the input given, thus Caller of this method must validate
173 * the inputs before invoking this private method.
175 SimulatorResourceServerSP ResourceManager::buildResource(const std::string &configPath,
176 SimulatorResourceServer::ResourceModelChangedCB callback)
178 // Create resource based on the RAML file.
179 SimulatorResourceServerImplSP resourceImpl = m_resourceCreator.createResource(configPath);
182 OC_LOG(ERROR, TAG, "Failed to create resource!");
183 throw SimulatorException(SIMULATOR_ERROR, "Failed to create resource!");
186 resourceImpl->setModelChangeCallback(callback);
187 resourceImpl->start();
189 // Add the resource to resource list table
190 std::lock_guard<std::recursive_mutex> lock(m_lock);
191 SimulatorResourceServerSP resource =
192 std::dynamic_pointer_cast<SimulatorResourceServer>(resourceImpl);
193 m_resources[resourceImpl->getResourceType()].insert(
194 std::pair<std::string, SimulatorResourceServerSP>(resourceImpl->getURI(), resourceImpl));
196 SIM_LOG(ILogger::INFO, "Created an OIC resource of type [" <<
197 resourceImpl->getResourceType() << "]");
202 * This method appends a unique key to the given URI to make the URI unique in simulator.
203 * Example: If input is "/a/light", then the output will be "/a/light/simulator/0" for the first resource
204 * and "/a/light/simulator/1" for the second resource and so on.
206 std::string ResourceManager::constructURI(const std::string &uri)
208 std::ostringstream os;
210 if (!uri.empty() && '/' != uri[uri.length() - 1])
212 os << "simulator/" << m_id++;