Implementation of APIs for managing resource list and callback
[platform/upstream/iotivity.git] / service / simulator / src / resource_manager.h
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 /**
22  * @file   resource_manager.h
23  *
24  * @brief   This file provides a class for a set of APIs relating to resource management
25  *               in the Service Provider side.
26  */
27
28 #ifndef RESOURCE_MANAGER_H_
29 #define RESOURCE_MANAGER_H_
30
31 #include <string>
32 #include <vector>
33 #include <map>
34 #include <mutex>
35 #include "simulator_resource_server.h"
36 #include "simulator_resource_creator.h"
37 #include "simulator_error_codes.h"
38
39 /**
40  * @class   ResourceManager
41  * @brief   This class provides a set of functions for managing the resource(s) in the Service Provider Module.
42  */
43 class ResourceManager
44 {
45     public:
46         /**
47          * This method is used to create/obtain the singleton instance of ResourceManager.
48          *
49          * @return ResourceManager - Singleton instance of ResourceManager.
50          */
51         static ResourceManager *getInstance(void);
52
53         /**
54          * This method is called for creating a single resource from the configuration file.
55          *
56          * @param configPath - RAML configuration file path.
57          * @param callback - Callback method for receive notifications when resource model changes.
58          *
59          * @return SimulatorResourceServerPtr - Shared pointer of SimulatorResourceServer on success, otherwise NULL.
60          */
61         SimulatorResourceServerPtr createResource(const std::string &configPath,
62                 SimulatorResourceServer::ResourceModelChangedCB callback);
63
64         /**
65          * This method is called for creating a collection of resources from the configuration file.
66          *
67          * @param configPath - RAML configuration file path.
68          * @param count - Number of resource to be created.
69          * @param callback - Callback method for receive notifications when resource model changes.
70          *
71          * @return SimulatorResourceServerPtr - A vector of Shared pointers of SimulatorResourceServer Objects.
72          */
73         std::vector<SimulatorResourceServerPtr> createResource(const std::string &configPath,
74                 const int count,
75                 SimulatorResourceServer::ResourceModelChangedCB callback);
76
77         /**
78          * This method is called for obtaining a list of created resources.
79          *
80          * @return SimulatorResourceServerPtr - A vector of Shared pointers of SimulatorResourceServer Objects.
81          */
82         std::vector<SimulatorResourceServerPtr> getResources(const std::string &resourceType = "");
83
84         /**
85          * This method is called for deleting a single resource.
86          *
87          * @param resource - Shared pointer of the SimulatorResourceServer to be deleted.
88          *
89          * @return SimulatorResult
90          */
91         SimulatorResult deleteResource(SimulatorResourceServerPtr &resource);
92
93         /**
94          * This method is called for deleting multiple resources.
95          * If this method is called without any parameter, then all resources will be deleted.
96          * If thie method is called with a specific resourcetype as a parameter, then all the resources
97          * of that particular type will be deleted.
98          *
99          * @param resourceType - Resource type of the resource
100          *
101          * @return SimulatorResult
102          */
103         SimulatorResult deleteResources(const std::string &resourceType = "");
104
105     private:
106         /**
107          * This method is called for obtaining a unique URI when creating multiple resources using createResource API.
108          * It appends a unique key to the given URI.
109          * Example: If input is "/a/light", then the output will be "/a/light/simulator/0" for the first resource
110          * and "/a/light/simulator/1" for the second resource and so on.
111          *
112          * @param uri - URI of the resource
113          *
114          * @return Unique URI for the resource
115          */
116         std::string getURI(std::string uri);
117
118         ResourceManager();
119         ~ResourceManager();
120
121         SimulatorResourceCreator *m_resourceCreator;
122         static int id;
123
124         /**
125          * This multi-level map organizes the resources in the form of ResourceType as the key
126          * and a set of resources of that resourceType as the value.
127          * The value is another map which has the ResourceURI as the key and the shared pointer
128          * of the SimulatorResourceServer object as the value.
129          */
130         std::map<std::string, std::map<std::string, SimulatorResourceServerPtr>> m_resourceList;
131         std::recursive_mutex m_listMutex;
132 };
133
134 #endif
135