Skip to content

Commit

Permalink
Merge pull request #4491 from telefonicaid/hardening/simplification
Browse files Browse the repository at this point in the history
FIX refactor to simplific logic
  • Loading branch information
mapedraza authored Jan 25, 2024
2 parents 43c0c7b + e64bcdb commit d3aa3e5
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 140 deletions.
4 changes: 2 additions & 2 deletions src/lib/mongoBackend/MongoCommonUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3504,7 +3504,7 @@ static unsigned int updateEntity

/* Build CER used for notifying (if needed) */
StringList emptyAttrL;
ContextElementResponse* notifyCerP = new ContextElementResponse(r, emptyAttrL, true, apiVersion);
ContextElementResponse* notifyCerP = new ContextElementResponse(r, emptyAttrL);

// The hasField() check is needed as the entity could have been created with very old Orion version not
// supporting modification/creation dates
Expand Down Expand Up @@ -3921,7 +3921,7 @@ static unsigned int updateEntity
notifyCerP->release();
delete notifyCerP;

notifyCerP = new ContextElementResponse(getObjectFieldF(reply, "value"), emptyAttrL, true, apiVersion);
notifyCerP = new ContextElementResponse(getObjectFieldF(reply, "value"), emptyAttrL);
}
}
else
Expand Down
8 changes: 1 addition & 7 deletions src/lib/mongoBackend/MongoGlobal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1385,11 +1385,6 @@ static bool isCustomAttr(std::string attrName)
* This method is used by queryContext. It takes a vector with entities and a vector
* with attributes as input and returns the corresponding ContextElementResponseVector or error.
*
* Note the includeEmpty argument. This is used if we don't want the result to include empty
* attributes, i.e. the ones that cause '<contextValue></contextValue>'. This is aimed at
* subscribeContext case, as empty values can cause problems in the case of federating Context
* Brokers (the notifyContext is processed as an updateContext and in the latter case, an
* empty value causes an error)
*/
bool entitiesQuery
(
Expand All @@ -1398,7 +1393,6 @@ bool entitiesQuery
const Restriction& res,
ContextElementResponseVector* cerV,
std::string* err,
bool includeEmpty,
const std::string& tenant,
const std::vector<std::string>& servicePath,
int offset,
Expand Down Expand Up @@ -1636,7 +1630,7 @@ bool entitiesQuery
// Build CER from BSON retrieved from DB
docs++;
LM_T(LmtMongo, ("retrieved document [%d]: '%s'", docs, r.toString().c_str()));
ContextElementResponse* cer = new ContextElementResponse(r, attrL, includeEmpty, apiVersion);
ContextElementResponse* cer = new ContextElementResponse(r, attrL);

// Add builtin attributes and metadata (only in NGSIv2)
if (apiVersion == V2)
Expand Down
1 change: 0 additions & 1 deletion src/lib/mongoBackend/MongoGlobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ extern bool entitiesQuery
const Restriction& res,
ContextElementResponseVector* cerV,
std::string* err,
bool includeEmpty,
const std::string& tenant,
const std::vector<std::string>& servicePath,
int offset = DEFAULT_PAGINATION_OFFSET_INT,
Expand Down
1 change: 0 additions & 1 deletion src/lib/mongoBackend/mongoQueryContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ HttpStatusCode mongoQueryContext
requestP->restriction,
&rawCerV,
&err,
true,
tenant,
servicePathV,
offset,
Expand Down
11 changes: 2 additions & 9 deletions src/lib/ngsi/ContextAttributeVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,7 @@ void ContextAttributeVector::fill(const ContextAttributeVector& caV, bool useDef
void ContextAttributeVector::fill
(
const orion::BSONObj& attrs,
const StringList& attrL,
bool includeEmpty,
const std::string& locAttr,
ApiVersion apiVersion
const StringList& attrL
)
{
std::set<std::string> attrNames;
Expand Down Expand Up @@ -346,10 +343,6 @@ void ContextAttributeVector::fill
{
case orion::String:
ca.stringValue = getStringFieldF(attr, ENT_ATTRS_VALUE);
if (!includeEmpty && ca.stringValue.empty())
{
continue;
}
caP = new ContextAttribute(ca.name, ca.type, ca.stringValue);
break;

Expand Down Expand Up @@ -440,7 +433,7 @@ void ContextAttributeVector::fill
void ContextAttributeVector::fill(const orion::BSONObj& attrs)
{
StringList emptyList;
return fill(attrs, emptyList, true, "", V2);
return fill(attrs, emptyList);
}


Expand Down
2 changes: 1 addition & 1 deletion src/lib/ngsi/ContextAttributeVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ typedef struct ContextAttributeVector
unsigned int size(void) const;
void release(void);
void fill(const ContextAttributeVector& caV, bool useDefaultType = false, bool cloneCompounds = false);
void fill(const orion::BSONObj& attrs, const StringList& attrL, bool includeEmpty, const std::string& locAttr, ApiVersion apiVersion);
void fill(const orion::BSONObj& attrs, const StringList& attrL);
void fill(const orion::BSONObj& attrs);
int get(const std::string& attributeName) const;

Expand Down
118 changes: 2 additions & 116 deletions src/lib/ngsi/ContextElementResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ ContextElementResponse::ContextElementResponse(ContextElementResponse* cerP, boo
ContextElementResponse::ContextElementResponse
(
const orion::BSONObj& entityDoc,
const StringList& attrL,
bool includeEmpty,
ApiVersion apiVersion
const StringList& attrL
)
{
prune = false;
Expand All @@ -125,119 +123,7 @@ ContextElementResponse::ContextElementResponse
//
// Attribute vector
//
orion::BSONObj attrs = getObjectFieldF(entityDoc, ENT_ATTRS);
std::set<std::string> attrNames;

attrs.getFieldNames(&attrNames);
for (std::set<std::string>::iterator i = attrNames.begin(); i != attrNames.end(); ++i)
{
std::string attrName = *i;
orion::BSONObj attr = getObjectFieldF(attrs, attrName);
ContextAttribute* caP = NULL;
ContextAttribute ca;

// Name and type
ca.name = dbDecode(attrName);
ca.type = getStringFieldF(attr, ENT_ATTRS_TYPE);

// Skip attribute if the attribute is in the list (or attrL is empty or includes "*")
if (!includedAttribute(ca.name, attrL))
{
continue;
}

/* It could happen (although very rarely) that the value field is missing in the
* DB for the attribute. The following is a safety check measure to protect against that */
if (!attr.hasField(ENT_ATTRS_VALUE))
{
caP = new ContextAttribute(ca.name, ca.type, "");
}
else
{
switch(getFieldF(attr, ENT_ATTRS_VALUE).type())
{
case orion::String:
ca.stringValue = getStringFieldF(attr, ENT_ATTRS_VALUE);
if (!includeEmpty && ca.stringValue.empty())
{
continue;
}
caP = new ContextAttribute(ca.name, ca.type, ca.stringValue);
break;

case orion::NumberDouble:
ca.numberValue = getNumberFieldF(attr, ENT_ATTRS_VALUE);
caP = new ContextAttribute(ca.name, ca.type, ca.numberValue);
break;

case orion::NumberInt:
ca.numberValue = (double) getIntFieldF(attr, ENT_ATTRS_VALUE);
caP = new ContextAttribute(ca.name, ca.type, ca.numberValue);
break;

case orion::Bool:
ca.boolValue = getBoolFieldF(attr, ENT_ATTRS_VALUE);
caP = new ContextAttribute(ca.name, ca.type, ca.boolValue);
break;

case orion::jstNULL:
caP = new ContextAttribute(ca.name, ca.type, "");
caP->valueType = orion::ValueTypeNull;
break;

case orion::Object:
caP = new ContextAttribute(ca.name, ca.type, "");
caP->compoundValueP = new orion::CompoundValueNode(orion::ValueTypeObject);
caP->valueType = orion::ValueTypeObject;
compoundObjectResponse(caP->compoundValueP, getFieldF(attr, ENT_ATTRS_VALUE));
break;

case orion::Array:
caP = new ContextAttribute(ca.name, ca.type, "");
caP->compoundValueP = new orion::CompoundValueNode(orion::ValueTypeVector);
caP->valueType = orion::ValueTypeVector;
compoundVectorResponse(caP->compoundValueP, getFieldF(attr, ENT_ATTRS_VALUE));
break;

default:
LM_E(("Runtime Error (unknown attribute value type in DB: %d)", getFieldF(attr, ENT_ATTRS_VALUE).type()));
}
}

/* dateExpires is managed like a regular attribute in DB, but it is a builtin and it is shadowed */
if (caP->name == DATE_EXPIRES)
{
caP->shadowed = true;
}

/* Setting custom metadata (if any) */
if (attr.hasField(ENT_ATTRS_MD))
{
orion::BSONObj mds = getObjectFieldF(attr, ENT_ATTRS_MD);
std::set<std::string> mdsSet;

mds.getFieldNames(&mdsSet);
for (std::set<std::string>::iterator i = mdsSet.begin(); i != mdsSet.end(); ++i)
{
std::string currentMd = *i;
Metadata* md = new Metadata(dbDecode(currentMd), getObjectFieldF(mds, currentMd));
caP->metadataVector.push_back(md);
}
}

/* Set creDate and modDate at attribute level */
if (attr.hasField(ENT_ATTRS_CREATION_DATE))
{
caP->creDate = getNumberFieldF(attr, ENT_ATTRS_CREATION_DATE);
}

if (attr.hasField(ENT_ATTRS_MODIFICATION_DATE))
{
caP->modDate = getNumberFieldF(attr, ENT_ATTRS_MODIFICATION_DATE);
}

entity.attributeVector.push_back(caP);
}
entity.attributeVector.fill(getObjectFieldF(entityDoc, ENT_ATTRS), attrL);

/* Set creDate and modDate at entity level */
if (entityDoc.hasField(ENT_CREATION_DATE))
Expand Down
4 changes: 1 addition & 3 deletions src/lib/ngsi/ContextElementResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ typedef struct ContextElementResponse
ContextElementResponse(EntityId* eP, ContextAttribute* aP);
ContextElementResponse(ContextElementResponse* cerP, bool cloneCompound = false);
ContextElementResponse(const orion::BSONObj& entityDoc,
const StringList& attrL,
bool includeEmpty = true,
ApiVersion apiVersion = V1);
const StringList& attrL);
ContextElementResponse(Entity* eP, bool useDefaultType = false);

std::string toJsonV1(bool asJsonObject,
Expand Down

0 comments on commit d3aa3e5

Please sign in to comment.