Skip to content

Commit

Permalink
sysAttrs in notifications triggered by PUT /entities/{EID}/attrs/{att…
Browse files Browse the repository at this point in the history
…rName}
  • Loading branch information
kzangeli committed Jun 28, 2023
1 parent 1c68a85 commit c4fb3d6
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/lib/orionld/serviceRoutines/orionldPutAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ extern "C"
#include "orionld/forwarding/distOpSuccess.h" // distOpSuccess
#include "orionld/notifications/alteration.h" // alteration
#include "orionld/notifications/previousValuePopulate.h" // previousValuePopulate
#include "orionld/notifications/sysAttrsStrip.h" // sysAttrsStrip
#include "orionld/serviceRoutines/orionldPutAttribute.h" // Own Interface


Expand Down Expand Up @@ -96,12 +97,17 @@ bool orionldPutAttribute(void)
char* attrLongNameEq = kaStrdup(&orionldState.kalloc, attrLongName);
KjNode* apiAttributeAsEntityP = NULL;
KjNode* dbEntityCopy = NULL;
KjNode* apiEntityP = NULL;
KjNode* oldAttrP = NULL;
KjNode* apiAttributeClone = NULL;
bool entityNotFoundLocally = false;
bool attrNotFoundLocally = false;

OrionldAlteration* alterationP = NULL;
KjNode* finalApiEntityWithSysAttrs = NULL;
KjNode* finalApiEntity = NULL;
KjNode* createdAtP = NULL;
KjNode* modifiedAtP = NULL;

dotForEq(attrLongNameEq);

if (dbEntityP == NULL)
Expand Down Expand Up @@ -198,6 +204,7 @@ bool orionldPutAttribute(void)

// Set creDate (mongocAttributeReplace sets modDate)
dbModelAttributeCreatedAtSet(orionldState.requestTree, createdAt);
kjTreeLog(orionldState.requestTree, "orionldState.requestTree", LmtSR);

// Write to mongo
if (mongocAttributeReplace(entityId, orionldState.requestTree, &detail) == false)
Expand Down Expand Up @@ -233,15 +240,16 @@ bool orionldPutAttribute(void)
// o Insert a copy of the attribute that was replaced (new copy)
//
dbEntityCopy = kjClone(orionldState.kjsonP, dbEntityP);
apiEntityP = dbModelToApiEntity2(dbEntityCopy, false, RF_NORMALIZED, NULL, false, &orionldState.pd);

if (apiEntityP == NULL)
finalApiEntityWithSysAttrs = dbModelToApiEntity2(dbEntityCopy, true, RF_NORMALIZED, NULL, false, &orionldState.pd);

if (finalApiEntityWithSysAttrs == NULL)
{
LM_E(("dbModelToApiEntity unable to convert DB Entity '%s' to API Entity (%s: %s)", entityId, orionldState.pd.title, orionldState.pd.detail));
goto response;
}

oldAttrP = kjLookup(apiEntityP, attrLongName);
oldAttrP = kjLookup(finalApiEntityWithSysAttrs, attrLongName);
if (oldAttrP == NULL)
{
LM_E(("Unable to find the attribute '%s' in the entity '%s'", attrLongName, entityId));
Expand All @@ -254,10 +262,32 @@ bool orionldPutAttribute(void)
LM_E(("Unable to clone the attribute '%s' in the entity '%s'", attrLongName, entityId));
goto response;
}
kjChildRemove(apiEntityP, oldAttrP);
kjChildAdd(apiEntityP, apiAttributeClone);
kjChildRemove(finalApiEntityWithSysAttrs, oldAttrP);
kjChildAdd(finalApiEntityWithSysAttrs, apiAttributeClone);

// Now get createdAt/modifiedAt from oldAttrP
createdAtP = kjLookup(oldAttrP, "createdAt");
modifiedAtP = kjLookup(oldAttrP, "modifiedAt");

if (createdAtP != NULL)
{
kjChildRemove(oldAttrP, createdAtP);
kjChildAdd(apiAttributeClone, createdAtP);
}

if (modifiedAtP != NULL)
{
kjChildRemove(oldAttrP, modifiedAtP);
kjChildAdd(apiAttributeClone, modifiedAtP);
}


finalApiEntity = kjClone(orionldState.kjsonP, finalApiEntityWithSysAttrs); // Check for NULL !
sysAttrsStrip(finalApiEntity);


alteration(entityId, entityType, apiEntityP, apiAttributeAsEntityP, dbEntityP);
alterationP = alteration(entityId, entityType, finalApiEntity, apiAttributeAsEntityP, dbEntityP);
alterationP->finalApiEntityWithSysAttrsP = finalApiEntityWithSysAttrs;

response:
if (distOpList != NULL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,24 @@ echo
echo


echo "17. PUT /ngsi-ld/v1/entities/urn:E1/attrs/P1"
echo "============================================"
payload='{
"value": 17
}'
orionCurl --url /ngsi-ld/v1/entities/urn:E1/attrs/P1 --payload "$payload" -X PUT
echo
echo


echo "18. See the notification in the accumulator, especially the sysAttrs (createdAt+modifiedAt)"
echo "==========================================================================================="
accumulatorDump
accumulatorReset
echo
echo


echo "19. PUT /ngsi-ld/v1/entities/urn:E1"
echo "==================================="
payload='{
Expand Down Expand Up @@ -598,6 +616,47 @@ Ngsild-Attribute-Format: Normalized
=======================================


17. PUT /ngsi-ld/v1/entities/urn:E1/attrs/P1
============================================
HTTP/1.1 204 No Content
Date: REGEX(.*)



18. See the notification in the accumulator, especially the sysAttrs (createdAt+modifiedAt)
===========================================================================================
POST http://REGEX(.*)/notify?subscriptionId=urn:S1
Content-Length: 388
User-Agent: orionld/REGEX(.*)
Host: tux
Accept: application/json
Content-Type: application/json
Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"
Ngsild-Attribute-Format: Normalized

{
"data": [
{
"P1": {
"createdAt": "202REGEX(.*)Z",
"modifiedAt": "202REGEX(.*)Z",
"type": "Property",
"value": 17
},
"createdAt": "202REGEX(.*)Z",
"id": "urn:E1",
"modifiedAt": "202REGEX(.*)Z",
"type": "T"
}
],
"id": "urn:ngsi-ld:Notification:REGEX(.*)",
"notifiedAt": "202REGEX(.*)Z",
"subscriptionId": "urn:S1",
"type": "Notification"
}
=======================================


19. PUT /ngsi-ld/v1/entities/urn:E1
===================================
HTTP/1.1 204 No Content
Expand Down

0 comments on commit c4fb3d6

Please sign in to comment.