Skip to content

Commit

Permalink
ADD evalPriority feature
Browse files Browse the repository at this point in the history
  • Loading branch information
fgalan committed Jul 2, 2024
1 parent ba69714 commit 0fe0be5
Show file tree
Hide file tree
Showing 11 changed files with 402 additions and 35 deletions.
33 changes: 19 additions & 14 deletions src/lib/common/JsonHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ std::string objectToJson(std::map<std::string, std::string>& list)
*
* JsonObjectHelper -
*/
JsonObjectHelper::JsonObjectHelper(): empty(true), closed(false)
JsonObjectHelper::JsonObjectHelper(): empty(true)
{
ss += '{';
}
Expand Down Expand Up @@ -280,15 +280,17 @@ void JsonObjectHelper::addNull(const std::string& key)
*
* JsonObjectHelper::str -
*/
std::string JsonObjectHelper::str()
std::string JsonObjectHelper::str(bool closed)
{
// This check allows to call str() several times (needed when this is used in ExprContext)
if (!closed)
// closed == false used in ExprContext logic
if (closed)
{
ss += '}';
closed = true;
return ss + '}';
}
else
{
return ss;
}
return ss;
}


Expand All @@ -297,7 +299,7 @@ std::string JsonObjectHelper::str()
*
* JsonVectorHelper -
*/
JsonVectorHelper::JsonVectorHelper(): empty(true), closed(false)
JsonVectorHelper::JsonVectorHelper(): empty(true)
{
ss += '[';
}
Expand Down Expand Up @@ -425,14 +427,17 @@ void JsonVectorHelper::addNull(void)
/* ****************************************************************************
*
* JsonVectorHelper::str -
* FIXME PR: bool closed probably unneded in vectors
*/
std::string JsonVectorHelper::str()
std::string JsonVectorHelper::str(bool closed)
{
// This check allows to call str() several times (needed when this is used in ExprContext)
if (!closed)
// closed == false used in ExprContext logic
if (closed)
{
ss += ']';
closed = true;
return ss + ']';
}
else
{
return ss;
}
return ss;
}
6 changes: 2 additions & 4 deletions src/lib/common/JsonHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ class JsonObjectHelper
void addBool(const std::string& key, bool b);
void addNull(const std::string& key);

std::string str();
std::string str(bool closed = true);

private:
std::string ss;
bool empty;
bool closed;
};


Expand All @@ -68,12 +67,11 @@ class JsonVectorHelper
void addNull(void);


std::string str();
std::string str(bool closed = true);

private:
std::string ss;
bool empty;
bool closed;
};


Expand Down
2 changes: 1 addition & 1 deletion src/lib/common/errorMessages.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
#define ERROR_DESC_BAD_REQUEST_FORMAT_INVALID "invalid render format for notifications"
#define ERROR_DESC_BAD_REQUEST_SERVICE_NOT_FOUND "Service not found. Check your URL as probably it is wrong."
#define ERROR_DESC_BAD_REQUEST_WRONG_GEOJSON "Wrong GeoJson"
#define ERROR_DESC_BAD_REQUEST_METADATA_NOT_ALLOWED_CUSTOM_NOTIF "metadata are not allowed in ngsi field in custom notifications"
#define ERROR_DESC_BAD_REQUEST_METADATA_NOT_ALLOWED_CUSTOM_NOTIF "only evalPriority metadata is allowed in ngsi field in custom notifications"

#define ERROR_NOT_FOUND "NotFound"
#define ERROR_DESC_NOT_FOUND_ENTITY "The requested entity has not been found. Check type and id"
Expand Down
10 changes: 10 additions & 0 deletions src/lib/common/limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,14 @@



/* ****************************************************************************
*
* MIX_PRIORITY and MAX_PRIORITY
*
*/
#define MIN_PRIORITY 1
#define MAX_PRIORITY LLONG_MAX



#endif // SRC_LIB_COMMON_LIMITS_H_
11 changes: 9 additions & 2 deletions src/lib/expressions/ExprContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ExprContextObject::ExprContextObject(bool _basic)
*/
std::string ExprContextObject::getJexlContext(void)
{
return jh.str();
return jh.str(false) + '}';
}


Expand Down Expand Up @@ -83,7 +83,14 @@ void ExprContextObject::add(const std::string &key, const std::string &_value, b
else
{
LM_T(LmtExpr, ("adding to JEXL expression context object (string): %s=%s", key.c_str(), _value.c_str()));
jh.addString(key, _value);
if (raw)
{
jh.addRaw(key, _value);
}
else
{
jh.addString(key, _value);
}
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/lib/jsonParseV2/parseSubscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,19 @@ static std::string parseCustomPayload
return badInput(ciP, r);
}

// metadadata are now allowed in this case
if (caP->metadataVector.size() > 0)
// only evalPriority metadadata is allowed in this case
for (unsigned ix = 0; ix < caP->metadataVector.size(); ix++)
{
return badInput(ciP, ERROR_DESC_BAD_REQUEST_METADATA_NOT_ALLOWED_CUSTOM_NOTIF);
if (caP->metadataVector[ix]->name != NGSI_MD_EVAL_PRIORITY)
{
return badInput(ciP, ERROR_DESC_BAD_REQUEST_METADATA_NOT_ALLOWED_CUSTOM_NOTIF);
}
else
{
// priority must be a number
// priority must be between MIX_PRIORITY and MAX_PRIORITY
// FIXME: PR (include a .test to asses the checkings works)
}
}
}
}
Expand Down
31 changes: 26 additions & 5 deletions src/lib/ngsi/ContextAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,26 @@ bool ContextAttribute::getLocation(orion::BSONObj* attrsP) const



/* ****************************************************************************
*
* getEvalPriority -
*/
double ContextAttribute::getEvalPriority(void)
{
for (unsigned int ix = 0; ix < metadataVector.size(); ix++)
{
if (metadataVector[ix]->name == NGSI_MD_EVAL_PRIORITY)
{
return metadataVector[ix]->numberValue;
}
}

// if the attribute doesn't have evalPriority metadata, then max priority is assumed
return MAX_PRIORITY;
}



/* ****************************************************************************
*
* toJsonV1AsObject -
Expand Down Expand Up @@ -1140,9 +1160,10 @@ std::string ContextAttribute::toJson(const std::vector<std::string>& metadataFi
* toJsonValue -
*
* To be used by options=values and options=unique renderings
* Also used by the ngsi expression logic
*
*/
std::string ContextAttribute::toJsonValue(void)
std::string ContextAttribute::toJsonValue(ExprContextObject* exprContextObjectP)
{
if (compoundValueP != NULL)
{
Expand All @@ -1164,10 +1185,10 @@ std::string ContextAttribute::toJsonValue(void)
}
else if (valueType == orion::ValueTypeString)
{
std::string out = "\"";
out += toJsonString(stringValue);
out += '"';
return out;
//std::string out = "\"";
//out += toJsonString(stringValue);
//out += '"';
return smartStringValue(stringValue, exprContextObjectP, "null");
}
else if (valueType == orion::ValueTypeBoolean)
{
Expand Down
4 changes: 3 additions & 1 deletion src/lib/ngsi/ContextAttribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ typedef struct ContextAttribute
/* Check if attribute means a location */
bool getLocation(orion::BSONObj* attrsP) const;

double getEvalPriority(void);

std::string toJsonV1(bool asJsonObject,
RequestType request,
const std::vector<std::string>& metadataFilter,
Expand All @@ -110,7 +112,7 @@ typedef struct ContextAttribute

std::string toJson(const std::vector<std::string>& metadataFilter, bool renderNgsiField = false, ExprContextObject* exprContextObjectP = NULL);

std::string toJsonValue(void);
std::string toJsonValue(ExprContextObject* exprContextObjectP = NULL);

std::string toJsonAsValue(ApiVersion apiVersion,
bool acceptedTextPlain,
Expand Down
1 change: 1 addition & 0 deletions src/lib/ngsi/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*
* Metadata interpreted by Orion Context Broker, i.e. not custom metadata
*/
#define NGSI_MD_EVAL_PRIORITY "evalPriority"
#define NGSI_MD_IGNORE_TYPE "ignoreType"
#define NGSI_MD_PREVIOUSVALUE "previousValue" // Special metadata
#define NGSI_MD_ACTIONTYPE "actionType" // Special metadata
Expand Down
62 changes: 57 additions & 5 deletions src/lib/ngsiNotify/Notifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,51 @@ static bool setJsonPayload



/* ****************************************************************************
*
* orderByPriority -
*
* Return false if some problem occur
*/
static void orderByPriority
(
const Entity& ngsi,
std::vector<ContextAttribute*>* orderedAttrs
)
{
// Add all the attributes to a temporal vector
std::vector<ContextAttribute*> attrs;

for (unsigned ix = 0; ix < ngsi.attributeVector.size(); ix++)
{
attrs.push_back(ngsi.attributeVector[ix]);
}

// Pass the content of attrs to orderedAttrs based in the evalPriority element
while (!attrs.empty())
{
ContextAttribute* selectedAttr = attrs[0];
unsigned int selectedIx = 0;
double prio = selectedAttr->getEvalPriority();

for (unsigned ix = 0; ix < attrs.size(); ix++)
{
double newPrio = attrs[ix]->getEvalPriority();
if (newPrio < prio)
{
selectedAttr = attrs[ix];
selectedIx = ix;
prio = newPrio;
}
}

orderedAttrs->push_back(selectedAttr);
attrs.erase(attrs.begin() + selectedIx);
}
}



/* ****************************************************************************
*
* setNgsiPayload -
Expand All @@ -209,7 +254,8 @@ static bool setNgsiPayload
bool blacklist,
const std::vector<std::string>& metadataFilter,
std::string* payloadP,
RenderFormat renderFormat
RenderFormat renderFormat,
bool basic // used by TIME_EXPR_CTXBLD_START/STOP macros
)
{
NotifyContextRequest ncr;
Expand Down Expand Up @@ -239,10 +285,16 @@ static bool setNgsiPayload

cer.entity.fill(effectiveId, effectiveType, en.isPattern, en.servicePath);

// First we add attributes in the ngsi field
for (unsigned int ix = 0; ix < ngsi.attributeVector.size(); ix++)
// First we add attributes in the ngsi field, adding calculated expressions to context in order of priority
std::vector<ContextAttribute*> orderedNgsiAttrs;
orderByPriority(ngsi, &orderedNgsiAttrs);

for (unsigned int ix = 0; ix < orderedNgsiAttrs.size(); ix++)
{
cer.entity.attributeVector.push_back(new ContextAttribute(ngsi.attributeVector[ix], false, true));
cer.entity.attributeVector.push_back(new ContextAttribute(orderedNgsiAttrs[ix], false, true));
TIME_EXPR_CTXBLD_START();
exprContextObjectP->add(orderedNgsiAttrs[ix]->name, orderedNgsiAttrs[ix]->toJsonValue(exprContextObjectP), true);
TIME_EXPR_CTXBLD_STOP();
}
// Next, other attributes in the original entity not already added
for (unsigned int ix = 0; ix < en.attributeVector.size(); ix++)
Expand Down Expand Up @@ -393,7 +445,7 @@ static SenderThreadParams* buildSenderParamsCustom
{
// Important to use const& for Entity here. Otherwise problems may occur in the object release logic
const Entity& ngsi = (notification.type == ngsiv2::HttpNotification ? notification.httpInfo.ngsi : notification.mqttInfo.ngsi);
if (!setNgsiPayload(ngsi, subscriptionId, en, &exprContext, attrsFilter, blacklist, metadataFilter, &payload, renderFormat))
if (!setNgsiPayload(ngsi, subscriptionId, en, &exprContext, attrsFilter, blacklist, metadataFilter, &payload, renderFormat, basic))
{
// Warning already logged in macroSubstitute()
return NULL;
Expand Down
Loading

0 comments on commit 0fe0be5

Please sign in to comment.