From: Harish Kumara Marappa Date: Mon, 7 Mar 2016 10:30:52 +0000 (+0530) Subject: Merge branch 'simulator'. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a29c54ef36ae8b0ea7769604aabd5851a2103d33;p=contrib%2Fiotivity.git Merge branch 'simulator'. Change-Id: I7566052325446e1887fa531af4e6f47c4f4089db Signed-off-by: Harish Kumara Marappa --- a29c54ef36ae8b0ea7769604aabd5851a2103d33 diff --cc service/simulator/SConscript index 867584f,06c4639..8a03a54 --- a/service/simulator/SConscript +++ b/service/simulator/SConscript @@@ -68,11 -67,13 +68,13 @@@ java_headers.append(os.path.join(java_h java_headers.append(os.path.join(java_headers[0], 'solaris')) simulator_env.AppendUnique(CPPPATH = java_headers) + simulator_env.Append( RPATH = env.Literal('\\$$ORIGIN')) + simulator_env.AppendUnique(CPPPATH = ['../../extlibs/cjson']) -simulator_env.PrependUnique(LIBS = ['octbstack', 'oc', 'RamlParser']) +simulator_env.PrependUnique(LIBS = ['oc', 'octbstack', 'RamlParser']) simulator_env.AppendUnique(LIBS = ['pthread']) - simulator_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++0x']) + simulator_env.AppendUnique(CXXFLAGS = ['-O2', '-Wall', '-fmessage-length=0', '-std=c++0x']) ###################################################################### # Source files and Targets diff --cc service/simulator/src/client/get_request_generator.cpp index 0b3020a,73b3928..6e1e330 --- a/service/simulator/src/client/get_request_generator.cpp +++ b/service/simulator/src/client/get_request_generator.cpp @@@ -62,40 -49,37 +49,37 @@@ void GETRequestGenerator::stopSending( void GETRequestGenerator::SendAllRequests() { // Notify the progress status - OC_LOG(DEBUG, TAG, "Sending OP_START event"); + OIC_LOG(DEBUG, TAG, "Sending OP_START event"); m_callback(m_id, OP_START); + QPGenerator queryParamGen(m_requestSchema->getQueryParams()); do { - if (!m_stopRequested) - { - // Get the next possible queryParameter - std::map queryParam = m_queryParamGen.next(); + // Get the next possible queryParameter + auto queryParam = queryParamGen.next(); + try + { // Send the request - try - { - m_requestSender->sendRequest(queryParam, std::bind(&GETRequestGenerator::onResponseReceived, this, - std::placeholders::_1, std::placeholders::_2), true); - m_requestCnt++; - } - catch (SimulatorException &e) - { - m_stopRequested = true; - return completed(); - } + m_requestSender.send(queryParam, std::bind(&GETRequestGenerator::onResponseReceived, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + m_requestCnt++; + } + catch (SimulatorException &e) + { + m_stopRequested = true; + break; } } - while (m_queryParamGen.hasNext()); + while (!m_stopRequested && queryParamGen.hasNext()); - m_requestsSent = true; completed(); } void GETRequestGenerator::onResponseReceived(SimulatorResult result, - SimulatorResourceModelSP repModel) + const SimulatorResourceModel &repModel, const RequestInfo &reqInfo) { - OIC_LOG_V(INFO, TAG, "Response recieved result:%d", result); - OC_LOG(DEBUG, TAG, "Response recieved"); ++ OIC_LOG(DEBUG, TAG, "Response recieved"); m_responseCnt++; completed(); } diff --cc service/simulator/src/client/post_request_generator.cpp index 547fb6a,6a69ccb..bc0cda0 --- a/service/simulator/src/client/post_request_generator.cpp +++ b/service/simulator/src/client/post_request_generator.cpp @@@ -73,13 -49,23 +49,23 @@@ void POSTRequestGenerator::stopSending( void POSTRequestGenerator::SendAllRequests() { // Notify the progress status - OC_LOG(DEBUG, TAG, "Sending OP_START event"); + OIC_LOG(DEBUG, TAG, "Sending OP_START event"); m_callback(m_id, OP_START); + std::shared_ptr repSchema = + m_requestSchema->getRequestRepSchema(); + + if (!repSchema) + { - OC_LOG(ERROR, TAG, "Request representation model is null!"); ++ OIC_LOG(ERROR, TAG, "Request representation model is null!"); + m_callback(m_id, OP_COMPLETE); + return; + } + // Create attribute generator for value manipulation std::vector attributeGenList; - for (auto &attributeElement : m_rep->getAttributes()) - attributeGenList.push_back(AttributeGenerator(attributeElement.second)); + for (auto &propertyElement : repSchema->getChildProperties()) + attributeGenList.push_back(AttributeGenerator(propertyElement.first, propertyElement.second)); if (!attributeGenList.size()) { @@@ -120,9 -106,9 +106,9 @@@ } void POSTRequestGenerator::onResponseReceived(SimulatorResult result, - SimulatorResourceModelSP repModel) + const SimulatorResourceModel &repModel, const RequestInfo &reqInfo) { - OIC_LOG_V(INFO, TAG, "Response recieved result:%d", result); - OC_LOG(DEBUG, TAG, "Response recieved"); ++ OIC_LOG(DEBUG, TAG, "Response recieved"); m_responseCnt++; completed(); } diff --cc service/simulator/src/client/put_request_generator.cpp index b712957,1484846..65ae77d --- a/service/simulator/src/client/put_request_generator.cpp +++ b/service/simulator/src/client/put_request_generator.cpp @@@ -72,20 -48,37 +48,37 @@@ void PUTRequestGenerator::stopSending( void PUTRequestGenerator::SendAllRequests() { - OC_LOG(DEBUG, TAG, "Sending OP_START event"); + OIC_LOG(DEBUG, TAG, "Sending OP_START event"); m_callback(m_id, OP_START); + std::shared_ptr repSchema = + m_requestSchema->getRequestRepSchema(); + + if (!repSchema) + { - OC_LOG(ERROR, TAG, "Request representation model is null!"); ++ OIC_LOG(ERROR, TAG, "Request representation model is null!"); + m_callback(m_id, OP_ABORT); + return; + } + + SimulatorResourceModel representation = repSchema->buildResourceModel(); + // Create attribute combination generator for generating resource model // with different attribute values - std::vector attributes; - for (auto &attributeElement : m_rep->getAttributes()) + std::vector attributes; + for (auto &attributeElement : representation.getAttributeValues()) { - attributes.push_back(attributeElement.second); + SimulatorResourceAttribute attribute; + attribute.setName(attributeElement.first); + attribute.setValue(attributeElement.second); + attribute.setProperty(repSchema->get(attributeElement.first)); + attributes.push_back(attribute); } if (!attributes.size()) { - OC_LOG(ERROR, TAG, "Zero attribute found from resource model!"); + OIC_LOG(ERROR, TAG, "Zero attribute found from resource model!"); + m_callback(m_id, OP_COMPLETE); return; } @@@ -122,9 -108,9 +108,9 @@@ } void PUTRequestGenerator::onResponseReceived(SimulatorResult result, - SimulatorResourceModelSP repModel) + const SimulatorResourceModel &repModel, const RequestInfo &reqInfo) { - OIC_LOG_V(INFO, TAG, "Response recieved result:%d", result); - OC_LOG(DEBUG, TAG, "Response recieved"); ++ OIC_LOG(DEBUG, TAG, "Response recieved"); m_responseCnt++; completed(); } diff --cc service/simulator/src/client/request_automation_manager.cpp index d01f67e,24842e1..7749ab5 --- a/service/simulator/src/client/request_automation_manager.cpp +++ b/service/simulator/src/client/request_automation_manager.cpp @@@ -27,25 -28,32 +28,32 @@@ #define TAG "AUTO_REQ_GEN_MNGR" - int AutoRequestGenMngr::startOnGET(RequestSenderSP requestSender, - const std::map> &queryParams, - AutoRequestGeneration::ProgressStateCallback callback) + RequestAutomationMngr::RequestAutomationMngr( + const std::shared_ptr &ocResource) + : m_id(0), m_ocResource(ocResource) {} + + int RequestAutomationMngr::startOnGET( + const std::shared_ptr &requestSchema, + RequestGeneration::ProgressStateCallback callback) { - // Input validation - if (!requestSender) + if (!requestSchema) { - throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Invalid request sender given!"); - OC_LOG(ERROR, TAG, "Request schema is null!"); ++ OIC_LOG(ERROR, TAG, "Request schema is null!"); + throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Request model is null!"); } if (!callback) { - OC_LOG(ERROR, TAG, "Invalid callback!"); ++ OIC_LOG(ERROR, TAG, "Invalid callback!"); throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!"); } // Check is there auto request generation session already going on for GET requests if (isInProgress(RequestType::RQ_TYPE_GET)) { - throw OperationInProgressException("Another GET request generation session is already in progress!"); - OC_LOG(ERROR, TAG, "Auto requesting for GET is already in progress!"); ++ OIC_LOG(ERROR, TAG, "Auto requesting for GET is already in progress!"); + throw OperationInProgressException( + "Another GET request generation session is already in progress!"); } // Create request generation session @@@ -71,26 -79,27 +79,27 @@@ return m_id++; } - int AutoRequestGenMngr::startOnPUT(RequestSenderSP requestSender, - const std::map> &queryParams, - SimulatorResourceModelSP resModel, - AutoRequestGeneration::ProgressStateCallback callback) + int RequestAutomationMngr::startOnPUT(const std::shared_ptr &requestSchema, + RequestGeneration::ProgressStateCallback callback) { - // Input validation - if (!requestSender) + if (!requestSchema) { - throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Invalid request sender given!"); - OC_LOG(ERROR, TAG, "Request schema is null!"); ++ OIC_LOG(ERROR, TAG, "Request schema is null!"); + throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Request model is null!"); } if (!callback) { - OC_LOG(ERROR, TAG, "Invalid callback!"); ++ OIC_LOG(ERROR, TAG, "Invalid callback!"); throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!"); } // Check is there auto request generation session already going on for GET requests if (isInProgress(RequestType::RQ_TYPE_PUT)) { - throw OperationInProgressException("Another GET request generation session is already in progress!"); - OC_LOG(ERROR, TAG, "Auto requesting for PUT is already in progress!"); ++ OIC_LOG(ERROR, TAG, "Auto requesting for PUT is already in progress!"); + throw OperationInProgressException( + "Another PUT request generation session is already in progress!"); } // Create request generation session @@@ -117,26 -126,27 +126,27 @@@ return m_id++; } - int AutoRequestGenMngr::startOnPOST(RequestSenderSP requestSender, - const std::map> &queryParams, - SimulatorResourceModelSP resModel, - AutoRequestGeneration::ProgressStateCallback callback) + int RequestAutomationMngr::startOnPOST(const std::shared_ptr &requestSchema, + RequestGeneration::ProgressStateCallback callback) { - // Input validation - if (!requestSender) + if (!requestSchema) { - throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Invalid request sender given!"); - OC_LOG(ERROR, TAG, "Request schema is null!"); ++ OIC_LOG(ERROR, TAG, "Request schema is null!"); + throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Request model is null!"); } if (!callback) { - OC_LOG(ERROR, TAG, "Invalid callback!"); ++ OIC_LOG(ERROR, TAG, "Invalid callback!"); throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!"); } // Check is there auto request generation session already going on for GET requests if (isInProgress(RequestType::RQ_TYPE_POST)) { - throw OperationInProgressException("Another GET request generation session is already in progress!"); - OC_LOG(ERROR, TAG, "Auto requesting for POST is already in progress!"); ++ OIC_LOG(ERROR, TAG, "Auto requesting for POST is already in progress!"); + throw OperationInProgressException( + "Another POST request generation session is already in progress!"); } // Create request generation session diff --cc service/simulator/src/client/simulator_remote_resource_impl.cpp index f40055d,d6234b9..3473eda --- a/service/simulator/src/client/simulator_remote_resource_impl.cpp +++ b/service/simulator/src/client/simulator_remote_resource_impl.cpp @@@ -82,6 -123,9 +123,9 @@@ void SimulatorRemoteResourceImpl::obser std::lock_guard lock(m_observeLock); if (m_observeState) { - OC_LOG(ERROR, TAG, "Resource is already being observed!"); ++ OIC_LOG(ERROR, TAG, "Resource is already being observed!"); + SIM_LOG(ILogger::ERROR, "[URI: " << getURI() << "] Resource is already being observed!") + throw SimulatorException(SIMULATOR_ERROR, "Resource is already being observed!"); } @@@ -123,9 -167,15 +167,15 @@@ void SimulatorRemoteResourceImpl::cance { OCStackResult ocResult = m_ocResource->cancelObserve(OC::QualityOfService::HighQos); if (OC_STACK_OK != ocResult) - throw SimulatorException(static_cast(ocResult), OC::OCException::reason(ocResult)); + { - OC_LOG(ERROR, TAG, "Cancelling observe failed!"); ++ OIC_LOG(ERROR, TAG, "Cancelling observe failed!"); + SIM_LOG(ILogger::INFO, "[URI: " << getURI() << "] Sending OBSERVE cancel request failed!") + + throw SimulatorException(static_cast(ocResult), + OC::OCException::reason(ocResult)); + } - SIM_LOG(ILogger::INFO, "OBSERVE CANCEL request sent"); + SIM_LOG(ILogger::INFO, "[URI: " << getURI() << "] Sent OBSERVE cancel request.") } catch (OC::OCException &e) { @@@ -142,16 -190,17 +190,17 @@@ void SimulatorRemoteResourceImpl::get(c { VALIDATE_CALLBACK(callback) - if (!m_getRequestSender) + SimulatorResult result = m_getRequestSender.send(std::bind( + &SimulatorRemoteResourceImpl::onResponseReceived, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3, callback)); + + if (SIMULATOR_OK != result) { - OIC_LOG(ERROR, TAG, "Invalid GET request sender!"); - throw NoSupportException("Can not send GET request on this resource!"); - } - OC_LOG(ERROR, TAG, "Failed to send GET request!"); ++ OIC_LOG(ERROR, TAG, "Failed to send GET request!"); + SIM_LOG(ILogger::ERROR, "[URI: " << getURI() << "] Failed to send GET request!") - m_getRequestSender->sendRequest(interfaceType, queryParams, - nullptr, std::bind( - &SimulatorRemoteResourceImpl::onResponseReceived, - this, std::placeholders::_1, std::placeholders::_2, callback)); + throw SimulatorException(result, "Failed to send GET request!"); + } } void SimulatorRemoteResourceImpl::get(const std::map &queryParams, @@@ -159,218 -208,244 +208,244 @@@ { VALIDATE_CALLBACK(callback) - if (!m_getRequestSender) + SimulatorResult result = m_getRequestSender.send(queryParams, std::bind( + &SimulatorRemoteResourceImpl::onResponseReceived, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3, callback)); + + if (SIMULATOR_OK != result) { - OIC_LOG(ERROR, TAG, "Invalid GET request sender !"); - throw NoSupportException("Can not send GET request on this resource!"); - } - OC_LOG(ERROR, TAG, "Failed to send GET request!"); ++ OIC_LOG(ERROR, TAG, "Failed to send GET request!"); + SIM_LOG(ILogger::ERROR, "[URI: " << getURI() << "] Failed to send GET request!") - m_getRequestSender->sendRequest(std::string(), queryParams, - nullptr, std::bind( - &SimulatorRemoteResourceImpl::onResponseReceived, - this, std::placeholders::_1, std::placeholders::_2, callback)); + throw SimulatorException(result, "Failed to send GET request!"); + } } - void SimulatorRemoteResourceImpl::put(const std::string &interfaceType, + void SimulatorRemoteResourceImpl::get(const std::string &interfaceType, const std::map &queryParams, - SimulatorResourceModelSP resourceModel, - ResponseCallback callback) + const GetResponseCallback &callback) { VALIDATE_CALLBACK(callback) - if (!m_putRequestSender) + std::map queryParamsCpy = queryParams; + if (!interfaceType.empty() && queryParamsCpy.end() == queryParamsCpy.find("if")) { - OIC_LOG(ERROR, TAG, "Invalid PUT request sender !"); - throw NoSupportException("Can not send PUT request on this resource!"); + queryParamsCpy["if"] = interfaceType; } - m_putRequestSender->sendRequest(interfaceType, queryParams, - resourceModel, std::bind( - &SimulatorRemoteResourceImpl::onResponseReceived, - this, std::placeholders::_1, std::placeholders::_2, callback)); + SimulatorResult result = m_getRequestSender.send(queryParamsCpy, std::bind( + &SimulatorRemoteResourceImpl::onResponseReceived, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3, callback)); + + if (SIMULATOR_OK != result) + { - OC_LOG(ERROR, TAG, "Failed to send GET request!"); ++ OIC_LOG(ERROR, TAG, "Failed to send GET request!"); + SIM_LOG(ILogger::ERROR, "[URI: " << getURI() << "] Failed to send GET request!") + + throw SimulatorException(result, "Failed to send GET request!"); + } } - void SimulatorRemoteResourceImpl::put(const std::map &queryParams, - SimulatorResourceModelSP resourceModel, - ResponseCallback callback) + void SimulatorRemoteResourceImpl::put(const SimulatorResourceModel &representation, + const PutResponseCallback &callback) { VALIDATE_CALLBACK(callback) - if (!m_putRequestSender) + SimulatorResult result = m_putRequestSender.send(representation, std::bind( + &SimulatorRemoteResourceImpl::onResponseReceived, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3, callback)); + + if (SIMULATOR_OK != result) { - OIC_LOG(ERROR, TAG, "Invalid PUT request sender !"); - throw NoSupportException("Can not send PUT request on this resource!"); - } - OC_LOG(ERROR, TAG, "Failed to send PUT request!"); ++ OIC_LOG(ERROR, TAG, "Failed to send PUT request!"); + SIM_LOG(ILogger::ERROR, "[URI: " << getURI() << "] Failed to send PUT request!") - m_putRequestSender->sendRequest(std::string(), queryParams, - resourceModel, std::bind( - &SimulatorRemoteResourceImpl::onResponseReceived, - this, std::placeholders::_1, std::placeholders::_2, callback)); + throw SimulatorException(result, "Failed to send PUT request!"); + } } - void SimulatorRemoteResourceImpl::post(const std::string &interfaceType, - const std::map &queryParams, - SimulatorResourceModelSP resourceModel, - ResponseCallback callback) + void SimulatorRemoteResourceImpl::put(const std::map &queryParams, + const SimulatorResourceModel &representation, + const PutResponseCallback &callback) { VALIDATE_CALLBACK(callback) - if (!m_postRequestSender) + SimulatorResult result = m_putRequestSender.send(queryParams, representation, std::bind( + &SimulatorRemoteResourceImpl::onResponseReceived, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3, callback)); + + if (SIMULATOR_OK != result) { - OIC_LOG(ERROR, TAG, "Invalid POST request sender !"); - throw NoSupportException("Can not send POST request on this resource!"); - } - OC_LOG(ERROR, TAG, "Failed to send PUT request!"); ++ OIC_LOG(ERROR, TAG, "Failed to send PUT request!"); + SIM_LOG(ILogger::ERROR, "[URI: " << getURI() << "] Failed to send PUT request!") - m_postRequestSender->sendRequest(interfaceType, queryParams, - resourceModel, std::bind( - &SimulatorRemoteResourceImpl::onResponseReceived, - this, std::placeholders::_1, std::placeholders::_2, callback)); + throw SimulatorException(result, "Failed to send PUT request!"); + } } - void SimulatorRemoteResourceImpl::post(const std::map &queryParams, - SimulatorResourceModelSP resourceModel, - ResponseCallback callback) + void SimulatorRemoteResourceImpl::put(const std::string &interfaceType, + const std::map &queryParams, + const SimulatorResourceModel &representation, + const PutResponseCallback &callback) { VALIDATE_CALLBACK(callback) - if (!m_postRequestSender) + std::map queryParamsCpy = queryParams; + if (!interfaceType.empty() && queryParamsCpy.end() == queryParamsCpy.find("if")) { - OIC_LOG(ERROR, TAG, "Invalid POST request sender !"); - throw NoSupportException("Can not send POST request on this resource!"); + queryParamsCpy["if"] = interfaceType; } - m_postRequestSender->sendRequest(std::string(), queryParams, - resourceModel, std::bind( - &SimulatorRemoteResourceImpl::onResponseReceived, - this, std::placeholders::_1, std::placeholders::_2, callback)); + SimulatorResult result = m_putRequestSender.send(queryParamsCpy, representation, std::bind( + &SimulatorRemoteResourceImpl::onResponseReceived, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3, callback)); + + if (SIMULATOR_OK != result) + { - OC_LOG(ERROR, TAG, "Failed to send PUT request!"); ++ OIC_LOG(ERROR, TAG, "Failed to send PUT request!"); + SIM_LOG(ILogger::ERROR, "[URI: " << getURI() << "] Failed to send PUT request!") + + throw SimulatorException(result, "Failed to send PUT request!"); + } } - int SimulatorRemoteResourceImpl::startVerification(RequestType type, - StateCallback callback) + void SimulatorRemoteResourceImpl::post(const SimulatorResourceModel &representation, + const PostResponseCallback &callback) { VALIDATE_CALLBACK(callback) - if (!m_autoRequestGenMngr) + SimulatorResult result = m_postRequestSender.send(representation, std::bind( + &SimulatorRemoteResourceImpl::onResponseReceived, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3, callback)); + + if (SIMULATOR_OK != result) { - OIC_LOG(ERROR, TAG, "Invalid auto request generation manager !"); - throw NoSupportException("Resource is not configured with RAML!"); - OC_LOG(ERROR, TAG, "Failed to send POST request!"); ++ OIC_LOG(ERROR, TAG, "Failed to send POST request!"); + SIM_LOG(ILogger::ERROR, "[URI: " << getURI() << "] Failed to send POST request!") + + throw SimulatorException(result, "Failed to send POST request!"); } + } - if (m_requestModelList.end() == m_requestModelList.find(type)) - throw NoSupportException("Resource does not support this request type!"); + void SimulatorRemoteResourceImpl::post(const std::map &queryParams, + const SimulatorResourceModel &representation, + const PostResponseCallback &callback) + { + VALIDATE_CALLBACK(callback) - // Local callback for handling progress sate callback - AutoRequestGeneration::ProgressStateCallback localCallback = [this, callback]( - int sessionId, OperationState state) - { - callback(m_id, sessionId, state); - }; + SimulatorResult result = m_postRequestSender.send(queryParams, representation, std::bind( + &SimulatorRemoteResourceImpl::onResponseReceived, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3, callback)); - switch (type) + if (SIMULATOR_OK != result) { - case RequestType::RQ_TYPE_GET: - if (m_getRequestSender) - { - return m_autoRequestGenMngr->startOnGET(m_getRequestSender, - m_requestModelList[RequestType::RQ_TYPE_GET]->getQueryParams(), - localCallback); - } - break; - OC_LOG(ERROR, TAG, "Failed to send POST request!"); ++ OIC_LOG(ERROR, TAG, "Failed to send POST request!"); + SIM_LOG(ILogger::ERROR, "[URI: " << getURI() << "] Failed to send POST request!") - case RequestType::RQ_TYPE_PUT: - if (m_putRequestSender) - { - return m_autoRequestGenMngr->startOnPUT(m_putRequestSender, - m_requestModelList[RequestType::RQ_TYPE_PUT]->getQueryParams(), - m_requestModelList[RequestType::RQ_TYPE_PUT]->getRepSchema(), - localCallback); - } - break; - - case RequestType::RQ_TYPE_POST: - if (m_postRequestSender) - { - return m_autoRequestGenMngr->startOnPOST(m_postRequestSender, - m_requestModelList[RequestType::RQ_TYPE_POST]->getQueryParams(), - m_requestModelList[RequestType::RQ_TYPE_POST]->getRepSchema(), - localCallback); - } - break; - - case RequestType::RQ_TYPE_DELETE: - default: - throw NoSupportException("Resource does not support this request type!"); + throw SimulatorException(result, "Failed to send POST request!"); } - - return -1; // Code should not reach here } - void SimulatorRemoteResourceImpl::stopVerification(int id) + void SimulatorRemoteResourceImpl::post(const std::string &interfaceType, + const std::map &queryParams, + const SimulatorResourceModel &representation, + const PostResponseCallback &callback) { - if (id < 0) + VALIDATE_CALLBACK(callback) + + std::map queryParamsCpy = queryParams; + if (!interfaceType.empty() && queryParamsCpy.end() == queryParamsCpy.find("if")) { - OIC_LOG(ERROR, TAG, "Invalid session id!"); - throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "Invalid ID!"); + queryParamsCpy["if"] = interfaceType; } - if (!m_autoRequestGenMngr) + SimulatorResult result = m_postRequestSender.send(queryParamsCpy, representation, std::bind( + &SimulatorRemoteResourceImpl::onResponseReceived, this, std::placeholders::_1, + std::placeholders::_2, std::placeholders::_3, callback)); + + if (SIMULATOR_OK != result) { - OIC_LOG(ERROR, TAG, "Invalid auto request generation manager !"); - throw NoSupportException("Resource is not configured with RAML!"); - } - OC_LOG(ERROR, TAG, "Failed to send POST request!"); ++ OIC_LOG(ERROR, TAG, "Failed to send POST request!"); + SIM_LOG(ILogger::ERROR, "[URI: " << getURI() << "] Failed to send POST request!") - m_autoRequestGenMngr->stop(id); + throw SimulatorException(result, "Failed to send POST request!"); + } } - SimulatorResourceModelSP SimulatorRemoteResourceImpl::configure(const std::string &path) + std::map + SimulatorRemoteResourceImpl::configure(const std::string &path) { VALIDATE_INPUT(path.empty(), "Path is empty!") - std::shared_ptr ramlParser = std::make_shared(path); - RAML::RamlPtr raml = ramlParser->getRamlPtr(); - - configure(raml); + try + { + std::shared_ptr ramlParser = + std::make_shared(path); + RAML::RamlPtr raml = ramlParser->getRamlPtr(); - if (m_requestModelList.empty()) - throw InvalidArgsException(SIMULATOR_INVALID_PARAM, "RAML file is invalid for the resource!"); + m_requestModels = RequestModelBuilder().build(raml, m_ocResource->uri()); + } + catch (RAML::RamlException &e) + { + throw SimulatorException(SIMULATOR_ERROR, "Failed to configure resource!"); + } - for (auto &model : m_requestModelList) + std::map requestModels; + for (auto &requestModelEntry : m_requestModels) { - SimulatorResourceModelSP schema = (model.second)->getRepSchema(); - if (schema) - { - return std::make_shared(*(schema.get())); - } + RequestType requestType = requestTypeToEnum(requestModelEntry.first); + SimulatorRequestModel requestModel(requestType); + requestModel.setQueryParams((requestModelEntry.second)->getQueryParams()); + requestModel.setRequestBodySchema((requestModelEntry.second)->getRequestRepSchema()); + + requestModels[requestType] = requestModel; } - return std::make_shared(); + return requestModels; } - void SimulatorRemoteResourceImpl::configure(std::shared_ptr &raml) + int SimulatorRemoteResourceImpl::startAutoRequesting(RequestType type, + AutoRequestGenerationCallback callback) { - m_requestModelList = RequestModelBuilder(raml).build(m_ocResource->uri()); - if (m_getRequestSender && - m_requestModelList.end() != m_requestModelList.find(RequestType::RQ_TYPE_GET)) - { - m_getRequestSender->setRequestModel(m_requestModelList[RequestType::RQ_TYPE_GET]); - } + VALIDATE_CALLBACK(callback) - if (m_putRequestSender && - m_requestModelList.end() != m_requestModelList.find(RequestType::RQ_TYPE_PUT)) + // Check if resource supports request type + std::string requestType = requestTypeToString(type); + if (m_requestModels.end() == m_requestModels.find(requestType)) { - m_putRequestSender->setRequestModel(m_requestModelList[RequestType::RQ_TYPE_PUT]); - OC_LOG(ERROR, TAG, "Resource is not configured for this request type!"); ++ OIC_LOG(ERROR, TAG, "Resource is not configured for this request type!"); + throw NoSupportException("Resource is not configured for this request type!"); } - if (m_postRequestSender && - m_requestModelList.end() != m_requestModelList.find(RequestType::RQ_TYPE_POST)) + std::shared_ptr requestModel = m_requestModels[requestType]; + switch (type) { - m_postRequestSender->setRequestModel(m_requestModelList[RequestType::RQ_TYPE_POST]); - } + case RequestType::RQ_TYPE_GET: + return m_requestAutomationMngr.startOnGET(requestModel, + std::bind(&SimulatorRemoteResourceImpl::onAutoRequestingState, this, + std::placeholders::_1, std::placeholders::_2, callback)); - if (!m_autoRequestGenMngr) - { - m_autoRequestGenMngr = std::make_shared(); + case RequestType::RQ_TYPE_PUT: + return m_requestAutomationMngr.startOnPUT(requestModel, + std::bind(&SimulatorRemoteResourceImpl::onAutoRequestingState, this, + std::placeholders::_1, std::placeholders::_2, callback)); + + case RequestType::RQ_TYPE_POST: + return m_requestAutomationMngr.startOnPOST(requestModel, + std::bind(&SimulatorRemoteResourceImpl::onAutoRequestingState, this, + std::placeholders::_1, std::placeholders::_2, callback)); + + case RequestType::RQ_TYPE_DELETE: + default: + throw NoSupportException("Not implemented!"); } + + return -1; // Code should not reach here + } + + void SimulatorRemoteResourceImpl::stopAutoRequesting(int id) + { + m_requestAutomationMngr.stop(id); } void SimulatorRemoteResourceImpl::onResponseReceived(SimulatorResult result, diff --cc service/simulator/src/common/request_model_builder.cpp index 79319b3,0787177..ff62586 --- a/service/simulator/src/common/request_model_builder.cpp +++ b/service/simulator/src/common/request_model_builder.cpp @@@ -23,18 -25,35 +25,35 @@@ #define TAG "REQ_MODEL_BUILDER" - RequestModelBuilder::RequestModelBuilder(std::shared_ptr &raml) - : m_raml (raml) {} + static std::string getRequestType(RAML::ActionType actionType) + { + switch (actionType) + { + case RAML::ActionType::GET: + return "GET"; + case RAML::ActionType::PUT: + return "PUT"; + case RAML::ActionType::POST: + return "POST"; + case RAML::ActionType::DELETE: + return "DELETE"; + } - std::map RequestModelBuilder::build(const std::string &uri) + return ""; // This code should never reach + } + + std::unordered_map RequestModelBuilder::build( + const std::shared_ptr &raml, const std::string &uri) { - std::map modelList; - if (!m_raml) + std::unordered_map requestModels; + + if (!raml) { - return modelList; - OC_LOG(ERROR, TAG, "Raml pointer is null!"); ++ OIC_LOG(ERROR, TAG, "Raml pointer is null!"); + return requestModels; } - for (auto &resource : m_raml->getResources()) + for (auto &resource : raml->getResources()) { // Pick the resource based on the resource uri. if (std::string::npos == uri.find((resource.second)->getResourceUri())) @@@ -45,17 -64,43 +64,43 @@@ { RequestModelSP requestModel = createRequestModel(action.second); if (requestModel) - modelList[requestModel->type()] = requestModel; + { + requestModels[requestModel->getType()] = requestModel; + } } + + break; } - return modelList; + return requestModels; } - RequestModelSP RequestModelBuilder::createRequestModel(const RAML::ActionPtr &action) + std::unordered_map RequestModelBuilder::build( + const std::shared_ptr &resource) { - OIC_LOG(DEBUG, TAG, "Creating request model"); + std::unordered_map requestModels; + if (!resource) + { - OC_LOG(ERROR, TAG, "Resource pointer is null!"); ++ OIC_LOG(ERROR, TAG, "Resource pointer is null!"); + return requestModels; + } + + // Construct Request and Response Model from RAML::Action + for (auto &action : resource->getActions()) + { + RequestModelSP requestModel = createRequestModel(action.second); + if (requestModel) + { + requestModels[requestModel->getType()] = requestModel; + } + } + + return requestModels; + } + + RequestModelSP RequestModelBuilder::createRequestModel(const RAML::ActionPtr &action) + { // Validate the action type. Only GET, PUT, POST and DELETE are supported. RAML::ActionType actionType = action->getType(); if (actionType != RAML::ActionType::GET @@@ -63,7 -108,7 +108,7 @@@ && actionType != RAML::ActionType::POST && actionType != RAML::ActionType::DELETE) { - OIC_LOG(ERROR, TAG, "Failed to create request model as it is of unknown type!"); - OC_LOG(ERROR, TAG, "Request model is of unknown type!"); ++ OIC_LOG(ERROR, TAG, "Request model is of unknown type!"); return nullptr; } @@@ -108,17 -151,12 +151,12 @@@ ResponseModelSP RequestModelBuilder::cr return responseModel; } - SimulatorResourceModelSP RequestModelBuilder::createRepSchema(const RAML::RequestResponseBodyPtr - &rep) + std::shared_ptr RequestModelBuilder::createRepSchema( + const RAML::RequestResponseBodyPtr &responseBody) { - if (!rep) - { - return nullptr; - } - - RAML::SchemaPtr schema = rep->getSchema(); - if (!schema) + if (!responseBody) { - OC_LOG(ERROR, TAG, "Response body is null!"); ++ OIC_LOG(ERROR, TAG, "Response body is null!"); return nullptr; } diff --cc service/simulator/src/common/resource_model_schema_builder.cpp index 0000000,5ddf5ba..d584a2d mode 000000,100644..100644 --- a/service/simulator/src/common/resource_model_schema_builder.cpp +++ b/service/simulator/src/common/resource_model_schema_builder.cpp @@@ -1,0 -1,329 +1,329 @@@ + /****************************************************************** + * + * Copyright 2016 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + + #include "resource_model_schema_builder.h" + #include "simulator_resource_model_schema.h" + #include "Raml.h" + #include "Properties.h" + #include "logger.h" + + #define TAG "RES_MODEL_BUILDER" + + ResourceModelSchemaBuilder::ResourceModelSchemaBuilder( + const std::shared_ptr &ramlSchema) + : m_ramlSchema (ramlSchema) {} + + std::shared_ptr ResourceModelSchemaBuilder::build() + { + if (!m_ramlSchema) + { - OC_LOG(ERROR, TAG, "RAML schema pointer is null!"); ++ OIC_LOG(ERROR, TAG, "RAML schema pointer is null!"); + return nullptr; + } + + std::shared_ptr modelSchema = + SimulatorResourceModelSchema::build(); + + auto jsonSchema = m_ramlSchema->getSchema()->getProperties(); + for (auto &propertyElement : jsonSchema->getProperties()) + { + auto property = propertyElement.second; + auto attributeProperty = buildAttribute(property); + + if (!attributeProperty) + return nullptr; + + modelSchema->add(property->getName(), attributeProperty); + } + + return modelSchema; + } + + std::shared_ptr ResourceModelSchemaBuilder::buildAttribute( + const std::shared_ptr &property) + { + switch (property->getType().type()) + { + case RAML::VariantType::INTEGER: + return buildIntegerAttribute(property); + + case RAML::VariantType::DOUBLE: + return buildDoubleAttribute(property); + + case RAML::VariantType::BOOLEAN: + return buildBooleanAttribute(property); + + case RAML::VariantType::STRING: + return buildStringAttribute(property); + + case RAML::VariantType::PROPERTY: + return buildArrayAttribute(property); + + case RAML::VariantType::ARRAY: + return buildModelArrayAttribute(property); + + case RAML::VariantType::OBJECT: + return buildModelAttribute(property); + + default: - OC_LOG(ERROR, TAG, "Unknown type!"); ++ OIC_LOG(ERROR, TAG, "Unknown type!"); + return nullptr; + } + + return nullptr; + } + + std::shared_ptr ResourceModelSchemaBuilder::buildIntegerAttribute( + const std::shared_ptr &property) + { + std::shared_ptr integerProperty = IntegerProperty::build(); + + // Set Default value + if (property->isDefaultValue()) + { + integerProperty->setDefaultValue(property->getValue()); + } + + // Set the Range/Enum property + for (auto &valueProperty : property->getValueProperties()) + { + if (RAML::ValueProperty::Type::RANGE == valueProperty->type()) + { + integerProperty->setRange(valueProperty->min(), valueProperty->max()); + break; + } + else if (RAML::ValueProperty::Type::VALUE_SET == valueProperty->type()) + { + std::vector values; + for (auto &value : valueProperty->valueSet()) + values.push_back(boost::get(value)); + integerProperty->setValues(values); + break; + } + } + + return integerProperty; + } + + std::shared_ptr ResourceModelSchemaBuilder::buildDoubleAttribute( + const std::shared_ptr &property) + { + std::shared_ptr doubleProperty = DoubleProperty::build(); + + // Set Default value + if (property->isDefaultValue()) + { + doubleProperty->setDefaultValue(property->getValue()); + } + + // Set the Range/Enum property + for (auto &valueProperty : property->getValueProperties()) + { + if (RAML::ValueProperty::Type::RANGE == valueProperty->type()) + { + doubleProperty->setRange(valueProperty->min(), valueProperty->max()); + break; + } + else if (RAML::ValueProperty::Type::VALUE_SET == valueProperty->type()) + { + std::vector values; + for (auto &value : valueProperty->valueSet()) + values.push_back(boost::get(value)); + doubleProperty->setValues(values); + break; + } + } + + return doubleProperty; + } + + std::shared_ptr ResourceModelSchemaBuilder::buildBooleanAttribute( + const std::shared_ptr &property) + { + std::shared_ptr boolProperty = BooleanProperty::build(); + + // Set Default value + if (property->isDefaultValue()) + { + boolProperty->setDefaultValue(property->getValue()); + } + + return boolProperty; + } + + std::shared_ptr ResourceModelSchemaBuilder::buildStringAttribute( + const std::shared_ptr &property) + { + std::shared_ptr stringProperty = StringProperty::build(); + + // Set Default value + if (property->isDefaultValue()) + { + stringProperty->setDefaultValue(property->getValue()); + } + + // Set the Range/Enum property + for (auto &valueProperty : property->getValueProperties()) + { + if (RAML::ValueProperty::Type::RANGE == valueProperty->type()) + { + stringProperty->setRange(valueProperty->min(), valueProperty->max()); + break; + } + else if (RAML::ValueProperty::Type::VALUE_SET == valueProperty->type()) + { + std::vector values; + for (auto &value : valueProperty->valueSet()) + values.push_back(boost::get(value)); + stringProperty->setValues(values); + break; + } + } + + return stringProperty; + } + + std::shared_ptr ResourceModelSchemaBuilder::buildArrayAttribute( + const std::shared_ptr &property) + { + std::shared_ptr arrayProperty = ArrayProperty::build(); + + auto arrayAttribute = boost::get(property->getValue()); + switch (arrayAttribute.getType().type()) + { + case RAML::VariantType::INTEGER: + { + std::shared_ptr elementAttribute = + std::make_shared(arrayAttribute); + + std::shared_ptr elementProperty = + buildIntegerAttribute(elementAttribute); + + setArrayProperties(property, arrayProperty); + arrayProperty->setElementProperty(elementProperty); + } + break; + + case RAML::VariantType::DOUBLE : + { + std::shared_ptr elementAttribute = + std::make_shared(arrayAttribute); + + std::shared_ptr elementProperty = + buildDoubleAttribute(elementAttribute); + + setArrayProperties(property, arrayProperty); + arrayProperty->setElementProperty(elementProperty); + } + break; + + case RAML::VariantType::BOOLEAN : + { + std::shared_ptr elementAttribute = + std::make_shared(arrayAttribute); + + std::shared_ptr elementProperty = + buildBooleanAttribute(elementAttribute); + + setArrayProperties(property, arrayProperty); + arrayProperty->setElementProperty(elementProperty); + } + break; + + case RAML::VariantType::STRING : + { + std::shared_ptr elementAttribute = + std::make_shared(arrayAttribute); + + std::shared_ptr elementProperty = + buildStringAttribute(elementAttribute); + + setArrayProperties(property, arrayProperty); + arrayProperty->setElementProperty(elementProperty); + } + break; + + default: - OC_LOG(ERROR, TAG, "Unknown array element type!"); ++ OIC_LOG(ERROR, TAG, "Unknown array element type!"); + return nullptr; + } + + return arrayProperty; + } + + std::shared_ptr ResourceModelSchemaBuilder::buildModelArrayAttribute( + const std::shared_ptr &property) + { + std::vector attributes = + boost::get >(property->getValue()); + + std::shared_ptr modelProperty = ModelProperty::build(); + + for (auto attributeProp : attributes) + { + auto elementProperty = std::make_shared(attributeProp); + modelProperty->add(elementProperty->getName(), buildAttribute(elementProperty)); + } + + std::shared_ptr arrayProperty = ArrayProperty::build(); + setArrayProperties(property, arrayProperty); + arrayProperty->setElementProperty(modelProperty); + + return arrayProperty; + } + + std::shared_ptr ResourceModelSchemaBuilder::buildModelAttribute( + const std::shared_ptr &property) + { + std::shared_ptr modelProperty = ModelProperty::build(); + + std::vector attributes = + boost::get >(property->getValue()); + + for (auto attributeProp : attributes) + { + auto elementProperty = std::make_shared(attributeProp); + modelProperty->add(elementProperty->getName(), buildAttribute(elementProperty)); + } + + return modelProperty; + } + + void ResourceModelSchemaBuilder::setArrayProperties( + const std::shared_ptr &property, + const std::shared_ptr &arrayProperty) + { + for (auto &valueProperty : property->getValueProperties()) + { + if (RAML::ValueProperty::Type::ARRAY == valueProperty->type()) + { + int minItems = 0; + int maxItems = 0; + bool unique = false; + bool additionalItems = false; + + valueProperty->valueArray(minItems, maxItems, unique, additionalItems); + arrayProperty->setRange(minItems, maxItems); + arrayProperty->setUnique(unique); + arrayProperty->setVariable(additionalItems); + break; + } + } + } diff --cc service/simulator/src/server/resource_update_automation.cpp index 42e3fed,ad2f997..8ccf78e --- a/service/simulator/src/server/resource_update_automation.cpp +++ b/service/simulator/src/server/resource_update_automation.cpp @@@ -51,7 -55,14 +55,14 @@@ AttributeUpdateAutomation::~AttributeUp void AttributeUpdateAutomation::start() { - m_thread = new std::thread(&AttributeUpdateAutomation::updateAttribute, this); + SimulatorResourceAttribute attribute; + if (false == m_resource->getAttribute(m_attrName, attribute)) + { - OC_LOG(ERROR, TAG, "Attribute is not present in resource!"); ++ OIC_LOG(ERROR, ATAG, "Attribute is not present in resource!"); + throw SimulatorException(SIMULATOR_ERROR, "Attribute is not present in resource!"); + } + + m_thread.reset(new std::thread(&AttributeUpdateAutomation::updateAttribute, this, attribute)); } void AttributeUpdateAutomation::stop() @@@ -97,8 -110,14 +110,14 @@@ void AttributeUpdateAutomation::updateA if (!m_stopRequested) { - OC_LOG_V(DEBUG, ATAG, "Attribute:%s automation is completed!", m_attrName.c_str()); + OIC_LOG_V(DEBUG, ATAG, "Attribute:%s automation is completed!", m_attrName.c_str()); - SIM_LOG(ILogger::INFO, "Automation of " << m_attrName << " attribute is completed."); + SIM_LOG(ILogger::INFO, "Attribute automation completed [Name: \"" << m_attrName + << "\", id: " << m_id <<"]."); + } + else + { + SIM_LOG(ILogger::INFO, "Attribute automation stopped [Name: \"" << m_attrName + << "\", id: " << m_id <<"]."); } // Notify application through callback @@@ -173,7 -208,14 +208,14 @@@ void ResourceUpdateAutomation::updateAt if (!m_stopRequested) { - SIM_LOG(ILogger::INFO, "Resource update automation complete [id: " << m_id << "]"); - OC_LOG_V(DEBUG, ATAG, "Resource update automation complete [id: %d]!", m_id); ++ OIC_LOG_V(DEBUG, RTAG, "Resource update automation complete [id: %d]!", m_id); + SIM_LOG(ILogger::INFO, "Resource automation completed [URI: \"" << m_resource->getURI() + << "\", id: " << m_id << "]."); + } + else + { + SIM_LOG(ILogger::INFO, "Resource automation stopped [URI: \"" << m_resource->getURI() + << "\", id: " << m_id <<"]."); } // Notify application diff --cc service/simulator/src/server/simulator_collection_resource_impl.cpp index fb7f434,64b5ac3..f7b1a2a --- a/service/simulator/src/server/simulator_collection_resource_impl.cpp +++ b/service/simulator/src/server/simulator_collection_resource_impl.cpp @@@ -344,8 -386,12 +386,12 @@@ OCEntityHandlerResult SimulatorCollecti std::shared_ptr request) { if (!request) + { - OC_LOG(ERROR, TAG, "Request received from stack is null!"); ++ OIC_LOG(ERROR, TAG, "Request received from stack is null!"); return OC_EH_ERROR; + } + // Handle GET, PUT, POST and DELETE requests if (OC::RequestHandlerFlag::RequestFlag & request->getRequestHandlerFlag()) { { diff --cc service/simulator/src/server/simulator_resource_factory.cpp index cc932c0,fb73db7..c746811 mode 100644,100755..100644 --- a/service/simulator/src/server/simulator_resource_factory.cpp +++ b/service/simulator/src/server/simulator_resource_factory.cpp @@@ -104,248 -105,106 +105,106 @@@ std::shared_ptr(collectionResource); } - SimulatorResourceModel::Attribute SimulatorResourceFactory::buildAttribute( - std::shared_ptr propertyElement) + std::shared_ptr SimulatorResourceFactory::buildResource( + const std::shared_ptr &ramlResource) { - std::string propName = propertyElement->getName(); + // Build resource request and respone model schema + RequestModelBuilder requestModelBuilder; + std::unordered_map requestModels = + requestModelBuilder.build(ramlResource); - // Build representation attribute - SimulatorResourceModel::Attribute attribute(propName); - switch (propertyElement->getVariantType()) + // Build SimulatorResourceModel from "GET" response schema + if (requestModels.end() == requestModels.find("GET")) { - case RAML::VariantType::INT: - { - attribute.setValue(propertyElement->getValue()); - - // Convert suppoted values - std::vector allowedValues = propertyElement->getAllowedValuesInt(); - if (allowedValues.size() > 0) - { - SimulatorResourceModel::AttributeProperty attrProp(allowedValues); - attribute.setProperty(attrProp); - } - } - break; - - case RAML::VariantType::DOUBLE: - { - attribute.setValue(propertyElement->getValue()); - - // Convert suppoted values - std::vector allowedValues = propertyElement->getAllowedValuesDouble(); - if (allowedValues.size() > 0) - { - SimulatorResourceModel::AttributeProperty attrProp(allowedValues); - attribute.setProperty(attrProp); - } - } - break; - - case RAML::VariantType::BOOL: - { - attribute.setValue(propertyElement->getValue()); - - std::vector allowedValues = {true, false}; - SimulatorResourceModel::AttributeProperty attrProp(allowedValues); - attribute.setProperty(attrProp); - } - break; - - case RAML::VariantType::STRING: - { - attribute.setValue(propertyElement->getValue()); - - // Convert suppoted values - std::vector allowedValues = propertyElement->getAllowedValuesString(); - if (allowedValues.size() > 0) - { - SimulatorResourceModel::AttributeProperty attrProp(allowedValues); - attribute.setProperty(attrProp); - } - } - break; - OC_LOG(ERROR, TAG, "Resource's RAML does not have GET request model!"); ++ OIC_LOG(ERROR, TAG, "Resource's RAML does not have GET request model!"); + return nullptr; } - // Set the range property if its present - double min, max; - int multipleof; - propertyElement->getRange(min, max, multipleof); - if (min != INT_MIN && max != INT_MAX) + RequestModelSP getRequestModel = requestModels["GET"]; + ResponseModelSP getResponseModel = getRequestModel->getResponseModel(200); + if (!getResponseModel) { - SimulatorResourceModel::AttributeProperty attrProp(min, max); - attribute.setProperty(attrProp); - OC_LOG(ERROR, TAG, "Resource's RAML does not have response for GET request!"); ++ OIC_LOG(ERROR, TAG, "Resource's RAML does not have response for GET request!"); + return nullptr; } - return attribute; - } - SimulatorResourceModel SimulatorResourceFactory::buildResourceModel( - std::shared_ptr item) - { - SimulatorResourceModel itemModel; - for ( auto &propElement : item->getProperties()) + std::shared_ptr responseSchema = + getResponseModel->getSchema(); + if (!responseSchema) { - if (!propElement.second) - continue; - OC_LOG(ERROR, TAG, "Failed to get schema from response model!"); ++ OIC_LOG(ERROR, TAG, "Failed to get schema from response model!"); + return nullptr; + } - std::string propName = propElement.second->getName(); - if ("p" == propName || "n" == propName || "id" == propName) - { - continue; - } + SimulatorResourceModel resourceModel = responseSchema->buildResourceModel(); - if ("array" == propElement.second->getType()) - { - std::vector arrayResModel; - for ( auto &propertyItem : propElement.second->getItems()) - { - arrayResModel.push_back(buildResourceModel(propertyItem)); - } - itemModel.add(propName, arrayResModel); - } - else - { - itemModel.add(buildAttribute(propElement.second)); - } - } - return itemModel; - } + // Remove the common properties from resource Model + std::string resourceURI = ramlResource->getResourceUri(); + std::string resourceName = ramlResource->getDisplayName(); + std::string resourceType; - RAML::RequestResponseBodyPtr SimulatorResourceFactory::getRAMLResponseBody( - std::shared_ptr ramlResource, RAML::ActionType type, std::string responseCode) - { - // Get the resource representation schema from response body - RAML::ActionPtr action = ramlResource->getAction(type); - if (!action) + // Extracting resource type. + if (resourceModel.contains("rt")) { - OIC_LOG(ERROR, TAG, "Resource does not possess the request!"); - return nullptr; + resourceType = resourceModel.get("rt"); + resourceModel.remove("rt"); } - - RAML::ResponsePtr response = action->getResponse(responseCode); - if (!response) + else if (resourceModel.contains("resourceType")) { - OIC_LOG(ERROR, TAG, "Resource does not provide valid GET response!"); - return nullptr; + resourceType = resourceModel.get("resourceType"); + resourceModel.remove("resourceType"); } - RAML::RequestResponseBodyPtr responseBody = response->getResponseBody("application/json"); - if (!responseBody) + // Construct resource type from uri + if(resourceType.empty()) { - OIC_LOG(ERROR, TAG, "GET response is not of type \"application/json\" "); - return nullptr; - } + std::ostringstream rtString; + rtString << "oic.r."; - return responseBody; - } - - SimulatorResourceModel SimulatorResourceFactory::buildModelFromResponseBody( - RAML::RequestResponseBodyPtr responseBody, std::string &resourceType, - std::vector &interfaceType) - { - SimulatorResourceModel resModel; - - if (!responseBody) - return resModel; - - // Iterate throgh all resource property and extract information needed for simulating resource. - RAML::JsonSchemaPtr resourceProperties = responseBody->getSchema()->getProperties(); + size_t pos = resourceURI.rfind("/"); + if (pos == std::string::npos) + pos = -1; + std::string rtName = resourceURI.substr(pos+1); + std::transform(rtName.begin(), rtName.end(), rtName.begin(), ::tolower); + rtString << rtName; + resourceType = rtString.str(); + } - for ( auto &propertyElement : resourceProperties->getProperties()) + // Extracting interface type. + std::vector interfaceTypes; + if (resourceModel.contains("if")) { - if (!propertyElement.second) - continue; - - std::string propName = propertyElement.second->getName(); - - // Resource type - if ("rt" == propName || "resourceType" == propName) + SimulatorResourceModel::TypeInfo typeInfo = resourceModel.getType("if"); + if(AttributeValueType::STRING == typeInfo.type()) { - resourceType = propertyElement.second->getValueString(); - continue; + interfaceTypes.push_back(resourceModel.get("if")); } - - // Interface type - if ("if" == propName) + else if(AttributeValueType::VECTOR == typeInfo.type() + && AttributeValueType::STRING == typeInfo.baseType() + && typeInfo.depth() == 1) { - if ("string" == propertyElement.second->getType()) - { - interfaceType.push_back(propertyElement.second->getValueString()); - } - else if ("array" == propertyElement.second->getType()) - { - for (auto &item : propertyElement.second->getItems()) - { - if ("string" == item->getType()) - { - interfaceType = item->getAllowedValuesString(); - break; - } - } - } - continue; + interfaceTypes = resourceModel.get>("if"); + if (interfaceTypes.size() > 1) + interfaceTypes.erase(interfaceTypes.begin()+1, interfaceTypes.end()); } - // Other Standard properties which should not be part of resource model - if ("p" == propName || "n" == propName || "id" == propName) - { - continue; - } - - // Add the attribute to resource model - if ("array" == propertyElement.second->getType()) - { - std::vector arrayResModel; - for ( auto &propertyItem : propertyElement.second->getItems()) - { - arrayResModel.push_back(buildResourceModel(propertyItem)); - } - resModel.add(propName, arrayResModel); - } - else - { - resModel.add(buildAttribute(propertyElement.second)); - } + resourceModel.remove("if"); } - if ("array" == resourceProperties->getType()) + for (auto &requestModel : requestModels) { - std::vector arrayResModel; - for ( auto &propertyItem : resourceProperties->getItems()) + if (requestModel.second) { - arrayResModel.push_back(buildResourceModel(propertyItem)); + addInterfaceFromQueryParameter((requestModel.second)->getQueryParams("if"), + interfaceTypes); } - resModel.add("links", arrayResModel); } - return resModel; - } - - std::shared_ptr SimulatorResourceFactory::buildResource( - std::shared_ptr ramlResource) - { - std::string name; - std::string uri; - std::string resourceType, rt; - std::vector interfaceType, ifType; - - name = ramlResource->getDisplayName(); - uri = ramlResource->getResourceUri(); - std::map actionType = ramlResource->getActions(); - - RAML::RequestResponseBodyPtr successResponseBody = getRAMLResponseBody( - ramlResource, RAML::ActionType::GET, "200"); - RAML::RequestResponseBodyPtr putErrorResponseBody = getRAMLResponseBody( - ramlResource, RAML::ActionType::PUT, "403"); - RAML::RequestResponseBodyPtr postErrorResponseBody = getRAMLResponseBody( - ramlResource, RAML::ActionType::POST, "403"); - - SimulatorResourceModel successResponseModel = buildModelFromResponseBody( - successResponseBody, resourceType, interfaceType); - SimulatorResourceModel putErrorResponseModel = buildModelFromResponseBody( - putErrorResponseBody, rt, ifType); - SimulatorResourceModel postErrorResponseModel = buildModelFromResponseBody( - postErrorResponseBody, rt, ifType); + // Remove properties which are not part of resource representation + resourceModel.remove("p"); + resourceModel.remove("n"); + resourceModel.remove("id"); // Create simple/collection resource std::shared_ptr simResource; diff --cc service/simulator/src/server/simulator_single_resource_impl.cpp index f4f4885,f33a225..5cc5c87 --- a/service/simulator/src/server/simulator_single_resource_impl.cpp +++ b/service/simulator/src/server/simulator_single_resource_impl.cpp @@@ -459,10 -639,15 +639,15 @@@ void SimulatorSingleResourceImpl::setCo OCEntityHandlerResult SimulatorSingleResourceImpl::handleRequests( std::shared_ptr request) { - OCEntityHandlerResult errCode = OC_EH_ERROR; if (!request) + { - OC_LOG(ERROR, TAG, "Request received from stack is null!"); ++ OIC_LOG(ERROR, TAG, "Request received from stack is null!"); return OC_EH_ERROR; + } + + OCEntityHandlerResult result = OC_EH_OK; + // Handle GET, PUT, POST and DELETE requests if (OC::RequestHandlerFlag::RequestFlag & request->getRequestHandlerFlag()) { {