diff --git a/src/lib/apiTypesV2/Attribute.cpp b/src/lib/apiTypesV2/Attribute.cpp deleted file mode 100644 index 8e9e911263..0000000000 --- a/src/lib/apiTypesV2/Attribute.cpp +++ /dev/null @@ -1,142 +0,0 @@ -/* -* -* Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U -* -* This file is part of Orion Context Broker. -* -* Orion Context Broker is free software: you can redistribute it and/or -* modify it under the terms of the GNU Affero General Public License as -* published by the Free Software Foundation, either version 3 of the -* License, or (at your option) any later version. -* -* Orion Context Broker is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero -* General Public License for more details. -* -* You should have received a copy of the GNU Affero General Public License -* along with Orion Context Broker. If not, see http://www.gnu.org/licenses/. -* -* For those usages not covered by this license please contact with -* iot_support at tid dot es -* -* Author: Orion dev team -*/ -#include -#include - -#include "common/errorMessages.h" -#include "common/RenderFormat.h" -#include "common/string.h" -#include "common/JsonHelper.h" -#include "logMsg/logMsg.h" -#include "ngsi10/QueryContextResponse.h" -#include "apiTypesV2/Attribute.h" -#include "rest/OrionError.h" - - - - -/* **************************************************************************** -* -* Attribute::toJson - -*/ -std::string Attribute::toJson -( - bool acceptedTextPlain, // in parameter (pass-through) - bool acceptedJson, // in parameter (pass-through) - MimeType outFormatSelection, // in parameter (pass-through) - MimeType* outMimeTypeP, // out parameter (pass-through) - HttpStatusCode* scP, // out parameter (pass-through) - bool keyValues, // in parameter - const std::vector& metadataFilter, // in parameter - bool asValue // in parameter -) -{ - if (contextAttributeP == NULL) { - LM_E(("Runtime Error (NULL contextAttributeP)")); - return ""; - } - - RenderFormat renderFormat = (keyValues == true)? NGSI_V2_KEYVALUES : NGSI_V2_NORMALIZED; - - std::string out; - - if (asValue) - { - out = contextAttributeP->toJsonAsValue(acceptedTextPlain, - acceptedJson, - outFormatSelection, - outMimeTypeP, - scP); - } - else - { - if (renderFormat == NGSI_V2_KEYVALUES) - { - JsonObjectHelper jh; - jh.addRaw(contextAttributeP->name, contextAttributeP->toJsonValue()); - out = jh.str(); - } - else // NGSI_V2_NORMALIZED - { - out = contextAttributeP->toJson(metadataFilter); - } - } - - return out; -} - - - - -/* **************************************************************************** -* -* Attribute::fill - -* -* CAUTION -* The Query should be for an indvidual entity -* -*/ -void Attribute::fill(const QueryContextResponse& qcrs, const std::string& attrName, OrionError* oeP) -{ - if (qcrs.errorCode.code == SccContextElementNotFound) - { - oeP->fill(SccContextElementNotFound, ERROR_DESC_NOT_FOUND_ENTITY, ERROR_NOT_FOUND); - } - else if (qcrs.errorCode.code != SccOk) - { - // - // any other error distinct from Not Found - // - oeP->fill(qcrs.errorCode.code, qcrs.errorCode.details, qcrs.errorCode.reasonPhrase); - } - else if (qcrs.contextElementResponseVector.size() > 1) // qcrs.errorCode.code == SccOk - { - // - // If there are more than one entity, we return an error - // - oeP->fill(SccConflict, ERROR_DESC_TOO_MANY_ENTITIES, ERROR_TOO_MANY); - } - else - { - contextAttributeP = NULL; - // Look for the attribute by name - - ContextElementResponse* cerP = qcrs.contextElementResponseVector[0]; - - for (std::size_t i = 0; i < cerP->entity.attributeVector.size(); ++i) - { - if (cerP->entity.attributeVector[i]->name == attrName) - { - contextAttributeP = cerP->entity.attributeVector[i]; - break; - } - } - - if (contextAttributeP == NULL) - { - oeP->fill(SccContextElementNotFound, ERROR_DESC_NOT_FOUND_ATTRIBUTE, ERROR_NOT_FOUND); - } - } -} diff --git a/src/lib/apiTypesV2/Attribute.h b/src/lib/apiTypesV2/Attribute.h deleted file mode 100644 index a16a7b77b1..0000000000 --- a/src/lib/apiTypesV2/Attribute.h +++ /dev/null @@ -1,69 +0,0 @@ -#ifndef SRC_LIB_APITYPESV2_ATTRIBUTE_H_ -#define SRC_LIB_APITYPESV2_ATTRIBUTE_H_ - -/* -* -* Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U -* -* This file is part of Orion Context Broker. -* -* Orion Context Broker is free software: you can redistribute it and/or -* modify it under the terms of the GNU Affero General Public License as -* published by the Free Software Foundation, either version 3 of the -* License, or (at your option) any later version. -* -* Orion Context Broker is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero -* General Public License for more details. -* -* You should have received a copy of the GNU Affero General Public License -* along with Orion Context Broker. If not, see http://www.gnu.org/licenses/. -* -* For those usages not covered by this license please contact with -* iot_support at tid dot es -* -* Author: Orion dev team -*/ -#include -#include -#include - -#include "ngsi/ContextAttributeVector.h" -#include "ngsi/ContextAttribute.h" -#include "rest/OrionError.h" - - - -/* **************************************************************************** -* -* To avoid a problematic and not necessary include -*/ -struct QueryContextResponse; - - - -/* **************************************************************************** -* -* Attribute - -*/ -class Attribute -{ - public: - ContextAttribute* contextAttributeP; // Mandatory - - Attribute(): contextAttributeP(0) {} - - std::string toJson(bool acceptedTextPlain, - bool acceptedJson, - MimeType outFormatSelection, - MimeType* outMimeTypeP, - HttpStatusCode* scP, - bool keyValues, - const std::vector& metadataFilter, - bool asValue); - - void fill(const QueryContextResponse& qcrs, const std::string& attrName, OrionError* oeP); -}; - -#endif // SRC_LIB_APITYPESV2_ATTRIBUTE_H_ diff --git a/src/lib/apiTypesV2/CMakeLists.txt b/src/lib/apiTypesV2/CMakeLists.txt index 18be8b1975..da8090aca6 100644 --- a/src/lib/apiTypesV2/CMakeLists.txt +++ b/src/lib/apiTypesV2/CMakeLists.txt @@ -24,7 +24,6 @@ SET (SOURCES Entity.cpp EntityVector.cpp Entities.cpp - Attribute.cpp Subscription.cpp Registration.cpp EntID.cpp @@ -38,7 +37,6 @@ SET (HEADERS Entity.h EntityVector.h Entities.h - Attribute.h Subscription.h Registration.h EntID.h diff --git a/src/lib/ngsi10/QueryContextResponse.cpp b/src/lib/ngsi10/QueryContextResponse.cpp index 9d966e0fff..5cac915f90 100644 --- a/src/lib/ngsi10/QueryContextResponse.cpp +++ b/src/lib/ngsi10/QueryContextResponse.cpp @@ -28,6 +28,7 @@ #include "logMsg/logMsg.h" #include "common/string.h" +#include "common/errorMessages.h" #include "alarmMgr/alarmMgr.h" #include "rest/HttpStatusCode.h" #include "ngsi/StatusCode.h" @@ -81,3 +82,58 @@ void QueryContextResponse::fill(const Entities& entities) contextElementResponseVector.push_back(cerP); } } + + +/* **************************************************************************** +* +* QueryContextResponse::getAttr - +* +* If attribute is found: +* - It is returned by the function +* - The OrionError is set to SccNone +* +* If attribute is not found +* - Function returns NULL +* - The OrionError is not touched +* +*/ +ContextAttribute* QueryContextResponse::getAttr(const std::string& attrName, OrionError* oeP) +{ + if (errorCode.code == SccContextElementNotFound) + { + oeP->fill(SccContextElementNotFound, ERROR_DESC_NOT_FOUND_ENTITY, ERROR_NOT_FOUND); + return NULL; + } + + if (errorCode.code != SccOk) + { + // + // any other error distinct from Not Found + // + oeP->fill(errorCode.code, errorCode.details, errorCode.reasonPhrase); + return NULL; + } + + if (contextElementResponseVector.size() > 1) // errorCode.code == SccOk + { + // + // If there are more than one entity, we return an error + // + oeP->fill(SccConflict, ERROR_DESC_TOO_MANY_ENTITIES, ERROR_TOO_MANY); + return NULL; + } + + // Look for the attribute by name + ContextElementResponse* cerP = contextElementResponseVector[0]; + + for (std::size_t i = 0; i < cerP->entity.attributeVector.size(); ++i) + { + if (cerP->entity.attributeVector[i]->name == attrName) + { + return cerP->entity.attributeVector[i]; + } + } + + oeP->fill(SccContextElementNotFound, ERROR_DESC_NOT_FOUND_ATTRIBUTE, ERROR_NOT_FOUND); + return NULL; +} diff --git a/src/lib/ngsi10/QueryContextResponse.h b/src/lib/ngsi10/QueryContextResponse.h index 249d816999..1123f12016 100644 --- a/src/lib/ngsi10/QueryContextResponse.h +++ b/src/lib/ngsi10/QueryContextResponse.h @@ -53,6 +53,7 @@ typedef struct QueryContextResponse void release(void); void fill(const Entities& entities); + ContextAttribute* getAttr(const std::string& attrName, OrionError* oeP); } QueryContextResponse; #endif // SRC_LIB_NGSI10_QUERYCONTEXTRESPONSE_H_ diff --git a/src/lib/serviceRoutinesV2/getEntityAttribute.cpp b/src/lib/serviceRoutinesV2/getEntityAttribute.cpp index 923e5376e2..097a4dad4f 100644 --- a/src/lib/serviceRoutinesV2/getEntityAttribute.cpp +++ b/src/lib/serviceRoutinesV2/getEntityAttribute.cpp @@ -30,7 +30,6 @@ #include "common/errorMessages.h" #include "rest/uriParamNames.h" -#include "apiTypesV2/Attribute.h" #include "rest/ConnectionInfo.h" #include "ngsi/ParseData.h" #include "rest/EntityTypeInfo.h" @@ -65,7 +64,6 @@ std::string getEntityAttribute { std::string type = ciP->uriParam["type"]; std::string answer; - Attribute attribute; if (forbiddenIdCharsV2(compV[2].c_str(), NULL) || forbiddenIdCharsV2(compV[4].c_str(), NULL)) @@ -78,27 +76,26 @@ std::string getEntityAttribute // 01. Fill in QueryContextRequest parseDataP->qcr.res.fill(compV[2], type, "false", EntityTypeEmptyOrNotEmpty, ""); - // 02. Call standard op postQueryContext + OrionError oe; postQueryContext(ciP, components, compV, parseDataP); - + ContextAttribute* caP = parseDataP->qcrs.res.getAttr(compV[4], &oe); // 03. Render entity attribute response - OrionError oe; - attribute.fill(parseDataP->qcrs.res, compV[4], &oe); - - if (oe.code == SccNone) + if (caP != NULL) { - StringList metadataFilter; - setMetadataFilter(ciP->uriParam, &metadataFilter); - TIMED_RENDER(answer = attribute.toJson(ciP->httpHeaders.accepted("text/plain"), - ciP->httpHeaders.accepted("application/json"), - ciP->httpHeaders.outformatSelect(), - &(ciP->outMimeType), - &(ciP->httpStatusCode), - ciP->uriParamOptions[OPT_KEY_VALUES], - metadataFilter.stringV, - false)); + if (ciP->uriParamOptions[OPT_KEY_VALUES]) // NGSI_V2_KEYVALUES + { + JsonObjectHelper jh; + jh.addRaw(caP->name, caP->toJsonValue()); + TIMED_RENDER(answer = jh.str()); + } + else // NGSI_V2_NORMALIZED + { + StringList metadataFilter; + setMetadataFilter(ciP->uriParam, &metadataFilter); + TIMED_RENDER(answer = caP->toJson(metadataFilter.stringV)); + } } else { diff --git a/src/lib/serviceRoutinesV2/getEntityAttributeValue.cpp b/src/lib/serviceRoutinesV2/getEntityAttributeValue.cpp index 7977e0b59a..60232b2264 100644 --- a/src/lib/serviceRoutinesV2/getEntityAttributeValue.cpp +++ b/src/lib/serviceRoutinesV2/getEntityAttributeValue.cpp @@ -30,7 +30,6 @@ #include "common/errorMessages.h" #include "common/string.h" -#include "apiTypesV2/Attribute.h" #include "rest/ConnectionInfo.h" #include "ngsi/ParseData.h" #include "ngsi/ContextAttribute.h" @@ -65,7 +64,6 @@ std::string getEntityAttributeValue ParseData* parseDataP ) { - Attribute attribute; std::string answer; std::string type = ciP->uriParam["type"]; @@ -83,9 +81,9 @@ std::string getEntityAttributeValue // Call standard op postQueryContext OrionError oe; postQueryContext(ciP, components, compV, parseDataP); - attribute.fill(parseDataP->qcrs.res, compV[4], &oe); + ContextAttribute* caP = parseDataP->qcrs.res.getAttr(compV[4], &oe); - if (oe.code != SccNone) + if (caP == NULL) { TIMED_RENDER(answer = oe.toJson()); ciP->httpStatusCode = oe.code; @@ -93,48 +91,41 @@ std::string getEntityAttributeValue else { // save the original attribute type - std::string attributeType = attribute.contextAttributeP->type; + std::string attributeType = caP->type; // the same of the wrapped operation ciP->httpStatusCode = parseDataP->qcrs.res.errorCode.code; // Remove unwanted fields from attribute before rendering - attribute.contextAttributeP->type = ""; - attribute.contextAttributeP->metadataVector.release(); + caP->type = ""; + caP->metadataVector.release(); if (ciP->outMimeType == JSON) { // Do not use attribute name, change to 'value' - attribute.contextAttributeP->name = "value"; - - StringList metadataFilter; - setMetadataFilter(ciP->uriParam, &metadataFilter); - - TIMED_RENDER(answer = attribute.toJson(ciP->httpHeaders.accepted("text/plain"), - ciP->httpHeaders.accepted("application/json"), - ciP->httpHeaders.outformatSelect(), - &(ciP->outMimeType), - &(ciP->httpStatusCode), - ciP->uriParamOptions[OPT_KEY_VALUES], - metadataFilter.stringV, - true)); + caP->name = "value"; + TIMED_RENDER(answer = caP->toJsonAsValue(ciP->httpHeaders.accepted("text/plain"), + ciP->httpHeaders.accepted("application/json"), + ciP->httpHeaders.outformatSelect(), + &(ciP->outMimeType), + &(ciP->httpStatusCode))); } else { - if (attribute.contextAttributeP->compoundValueP != NULL) + if (caP->compoundValueP != NULL) { - TIMED_RENDER(answer = attribute.contextAttributeP->compoundValueP->toJson()); + TIMED_RENDER(answer = caP->compoundValueP->toJson()); } else { if ((attributeType == DATE_TYPE) || (attributeType == DATE_TYPE_ALT)) { - TIMED_RENDER(answer = isodate2str(attribute.contextAttributeP->numberValue)); + TIMED_RENDER(answer = isodate2str(caP->numberValue)); } else { - TIMED_RENDER(answer = attribute.contextAttributeP->getValue()); - if (attribute.contextAttributeP->valueType == orion::ValueTypeString) + TIMED_RENDER(answer = caP->getValue()); + if (caP->valueType == orion::ValueTypeString) { answer = '"' + answer + '"'; }