Collection resource not notifying its updated model when child resource is added...
[platform/upstream/iotivity.git] / service / simulator / src / server / resource_update_automation_mngr.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_update_automation_mngr.h"
22 #include "simulator_exceptions.h"
23 #include "simulator_logger.h"
24 #include "logger.h"
25
26 #define TAG "UPDATE_AUTOMATION_MNGR"
27
28 UpdateAutomationMngr::UpdateAutomationMngr()
29     :   m_id(0) {}
30
31 int UpdateAutomationMngr::startResourceAutomation(SimulatorSingleResource *resource,
32         AutomationType type, int interval, updateCompleteCallback callback)
33 {
34     if (!callback)
35     {
36         OC_LOG(ERROR, TAG, "Invalid callback!");
37         throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
38     }
39
40     ResourceUpdateAutomationSP resourceAutomation(new ResourceUpdateAutomation(
41                 m_id, resource, type, interval, callback,
42                 std::bind(&UpdateAutomationMngr::automationCompleted, this, std::placeholders::_1)));
43
44     std::lock_guard<std::mutex> lock(m_lock);
45     resourceAutomation->start();
46
47     OC_LOG_V(DEBUG, TAG, "Resource automation successfully started [id: %d]", m_id);
48     SIM_LOG(ILogger::INFO, "Resource automation successfully started [ id: " << m_id << " ]");
49
50     m_resourceUpdationList[m_id] = resourceAutomation;
51     return m_id++;
52 }
53
54 int UpdateAutomationMngr::startAttributeAutomation(SimulatorSingleResource *resource,
55         const std::string &attrName, AutomationType type, int interval,
56         updateCompleteCallback callback)
57 {
58     if (!callback)
59     {
60         OC_LOG(ERROR, TAG, "Invalid callback!");
61         throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");
62     }
63
64     // Check the validity of attribute
65     SimulatorResourceModel::Attribute attribute;
66     if (false == resource->getAttribute(attrName, attribute))
67     {
68         OC_LOG_V(ERROR, TAG, "Attribute:%s not present in resource!", attrName.c_str());
69         throw SimulatorException(SIMULATOR_ERROR, "Attribute is not present in resource!");
70     }
71
72     AttributeUpdateAutomationSP attributeAutomation(new AttributeUpdateAutomation(
73                 m_id, resource, attribute, type, interval, callback,
74                 std::bind(&UpdateAutomationMngr::automationCompleted, this, std::placeholders::_1)));
75
76     std::lock_guard<std::mutex> lock(m_lock);
77     attributeAutomation->start();
78
79     OC_LOG_V(DEBUG, TAG, "Attribute automation successfully started [name: %s, id: %d]",
80              attrName.c_str(), m_id);
81     SIM_LOG(ILogger::INFO, "Automation for " << attrName << " attribute has successfully started [ id: "
82             <<
83             m_id << " ]");
84
85     m_attrUpdationList[m_id] = attributeAutomation;
86     return m_id++;
87 }
88
89 std::vector<int> UpdateAutomationMngr::getResourceAutomationIds()
90 {
91     std::vector<int> ids;
92     std::lock_guard<std::mutex> lock(m_lock);
93     for (auto &automation : m_resourceUpdationList)
94         ids.push_back(automation.first);
95
96     return ids;
97 }
98
99 std::vector<int> UpdateAutomationMngr::getAttributeAutomationIds()
100 {
101     std::vector<int> ids;
102     std::lock_guard<std::mutex> lock(m_lock);
103     for (auto &automation : m_attrUpdationList)
104         ids.push_back(automation.first);
105
106     return ids;
107 }
108
109 void UpdateAutomationMngr::stop(int id)
110 {
111     std::lock_guard<std::mutex> lock(m_lock);
112     if (m_resourceUpdationList.end() != m_resourceUpdationList.find(id))
113     {
114         m_resourceUpdationList[id]->stop();
115         m_resourceUpdationList.erase(m_resourceUpdationList.find(id));
116         return;
117     }
118     else if (m_attrUpdationList.end() != m_attrUpdationList.find(id))
119     {
120         m_attrUpdationList[id]->stop();
121         m_attrUpdationList.erase(m_attrUpdationList.find(id));
122         return;
123     }
124 }
125
126 void UpdateAutomationMngr::stopAll()
127 {
128     std::lock_guard<std::mutex> lock(m_lock);
129     std::for_each(m_resourceUpdationList.begin(),
130                   m_resourceUpdationList.end(), [] (std::pair<int, ResourceUpdateAutomationSP> element)
131     {
132         element.second->stop();
133     });
134     m_resourceUpdationList.clear();
135
136     std::for_each(m_attrUpdationList.begin(),
137                   m_attrUpdationList.end(), [] (std::pair<int, AttributeUpdateAutomationSP> element)
138     {
139         element.second->stop();
140     });
141
142     m_attrUpdationList.clear();
143 }
144
145 void UpdateAutomationMngr::automationCompleted(int id)
146 {
147     std::lock_guard<std::mutex> lock(m_lock);
148     if (m_resourceUpdationList.end() != m_resourceUpdationList.find(id))
149     {
150         m_resourceUpdationList.erase(m_resourceUpdationList.find(id));
151     }
152     else if (m_attrUpdationList.end() != m_attrUpdationList.find(id))
153     {
154         m_attrUpdationList.erase(m_attrUpdationList.find(id));
155     }
156 }