Skip to content

Commit

Permalink
Slight improvement in distributed DELETE Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
kzangeli committed Dec 22, 2023
1 parent 3f03b98 commit 367aae7
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/lib/orionld/forwarding/distOpResponses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ void distOpResponseAccumulate(DistOp* distOpP, KjNode* responseBody, KjNode* suc
if (msgP->data.result == CURLE_OK)
{
if ((httpResponseCode >= 200) && (httpResponseCode <= 299))
distOpSuccess(responseBody, distOpP, NULL);
distOpSuccess(responseBody, distOpP, NULL, NULL);
else if (httpResponseCode == 404)
distOpFailure(responseBody, distOpP, "Not Found", NULL, 404, NULL);
}
Expand Down
10 changes: 8 additions & 2 deletions src/lib/orionld/forwarding/distOpSuccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static void attrNameToSuccess(KjNode* successV, KjNode* failureV, char* attrName
//
// distOpSuccess -
//
void distOpSuccess(KjNode* responseBody, DistOp* distOpP, char* attrName)
void distOpSuccess(KjNode* responseBody, DistOp* distOpP, const char* entityId, char* attrName)
{
KjNode* successV = kjLookup(responseBody, "success");
KjNode* failureV = kjLookup(responseBody, "failure");
Expand All @@ -81,13 +81,19 @@ void distOpSuccess(KjNode* responseBody, DistOp* distOpP, char* attrName)
kjChildAdd(responseBody, successV);
}

if ((distOpP == NULL) && (attrName == NULL))
{
KjNode* entityIdNodeP = kjString(orionldState.kjsonP, NULL, entityId);
kjChildAdd(successV, entityIdNodeP);
}

if (attrName != NULL)
attrNameToSuccess(successV, failureV, attrName);
else if (distOpP != NULL)
{
if (distOpP->attrName != NULL)
attrNameToSuccess(successV, failureV, distOpP->attrName);
else
else if (distOpP->requestBody != NULL)
{
for (KjNode* attrNameP = distOpP->requestBody->value.firstChildP; attrNameP != NULL; attrNameP = attrNameP->next)
{
Expand Down
3 changes: 2 additions & 1 deletion src/lib/orionld/forwarding/distOpSuccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ extern "C"
// PARAMETERS
// responseBody a KjNode object with two fields: "success" and "failure"
// distOpP linked list of DistOp's
// entityId the id of the entity
// attrName instead of 'distOp', here's the only attribute name already
//
extern void distOpSuccess(KjNode* responseBody, DistOp* distOpP, char* attrName);
extern void distOpSuccess(KjNode* responseBody, DistOp* distOpP, const char* entityId, char* attrName);

#endif // SRC_LIB_ORIONLD_FORWARDING_DISTOPSUCCESS_H_
2 changes: 2 additions & 0 deletions src/lib/orionld/rest/orionldServiceInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ static void restServicePrepare(OrionLdRestService* serviceP, OrionLdRestServiceS
else if (serviceP->serviceRoutine == orionldDeleteEntity)
{
serviceP->options |= ORIONLD_SERVICE_OPTION_NO_CONTEXT_NEEDED;

serviceP->uriParams |= ORIONLD_URIPARAM_TYPELIST;
}
else if (serviceP->serviceRoutine == orionldPostEntity)
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/orionld/serviceRoutines/orionldDeleteAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ bool orionldDeleteAttribute(void)
distOpFailure(responseBody, NULL, "Database Error", "(ToDo: get error from mongoc)", 500, attrName);
}
else
distOpSuccess(responseBody, NULL, attrName);
distOpSuccess(responseBody, NULL, entityId, attrName);
}

if (distOpList != NULL)
Expand Down
30 changes: 22 additions & 8 deletions src/lib/orionld/serviceRoutines/orionldDeleteEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,26 +219,39 @@ bool orionldDeleteEntity(void)
// Delete the entity in the local DB
// - Error if the entity is not found locally, and not subject to forwarding
//
if ((dbEntityP == NULL) && (distOpList == NULL))
if (dbEntityP == NULL)
{
orionldError(OrionldResourceNotFound, "Entity not found", entityId, 404);
return false;
if (distOpList == NULL)
{
orionldError(OrionldResourceNotFound, "Entity not found", entityId, 404);
return false;
}
else
distOpFailure(responseBody, NULL, "Not Found", entityId, 404, NULL);
}

//
// Delete the entity locally (if present)
// Give 404 if the entity is not present locally nor triggered any forwarded requests
//
char* detail = NULL;
if ((dbEntityP != NULL) && (mongocEntityDelete(entityId, &detail) == false))
if (dbEntityP != NULL)
{
if (distOpList == NULL) // pure local request
if (mongocEntityDelete(entityId, &detail) == true)
{
orionldError(OrionldInternalError, "Database Error", detail, 500);
return false;
// Add a success to the "success" member
distOpSuccess(responseBody, NULL, entityId, NULL);
}
else
distOpFailure(responseBody, NULL, "Database Error", detail, 500, NULL);
{
if (distOpList == NULL) // pure local request
{
orionldError(OrionldInternalError, "Database Error", detail, 500);
return false;
}
else
distOpFailure(responseBody, NULL, "Database Error", detail, 500, NULL);
}
}

if (dbEntityP != NULL)
Expand All @@ -250,6 +263,7 @@ bool orionldDeleteEntity(void)
distOpListRelease(distOpList);
}

kjTreeLog(responseBody, "responseBody", LmtSR);
responseFix(responseBody, DoDeleteEntity, 204, entityId);

return true;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/orionld/serviceRoutines/orionldPatchEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ bool orionldPatchEntity(void)

bzero(&local, sizeof(local));
local.requestBody = orionldState.requestTree;
distOpSuccess(responseBody, &local, NULL);
distOpSuccess(responseBody, &local, entityId, NULL);
}

//
Expand Down
2 changes: 1 addition & 1 deletion src/lib/orionld/serviceRoutines/orionldPatchEntity2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ bool orionldPatchEntity2(void)
local.requestBody = requestForLocal;

if (dbUpdateResult == true)
distOpSuccess(responseBody, &local, NULL);
distOpSuccess(responseBody, &local, entityId, NULL);
}

responseFix(responseBody, DoMergeEntity, 204, entityId);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/orionld/serviceRoutines/orionldPostEntities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ bool orionldPostEntities(void)
DistOp local;
bzero(&local, sizeof(local));
local.requestBody = cloneForTroeP;
distOpSuccess(responseBody, &local, NULL);
distOpSuccess(responseBody, &local, NULL, NULL);
}

//
Expand Down
4 changes: 2 additions & 2 deletions src/lib/orionld/serviceRoutines/orionldPostEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ bool orionldPostEntity(void)
kjChildRemove(orionldState.requestTree, attrP);
}
else
distOpSuccess(responseBody, NULL, attrP->name);
distOpSuccess(responseBody, NULL, entityId, attrP->name);
}
}
else
Expand All @@ -274,7 +274,7 @@ bool orionldPostEntity(void)
kjChildRemove(orionldState.requestTree, attrP);
}
else
distOpSuccess(responseBody, NULL, attrP->name);
distOpSuccess(responseBody, NULL, entityId, attrP->name);
}

attrP = next;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ orionldStart CP2 -experimental -forwarding -wip entityMaps
# 05. Create an entity urn:E2, type IE in CP1
# 06. Create an entity urn:E3, type IE in CP2
# 07. GET all entity of type IE from CB - see all three entities
# 08. DELETE urn:E2 via CB (forwarded message to both CP1 and CP2 - success from CP1, 404 from CP2)
# 09. DELETE urn:E1 via CB (forwarded message to both CP1 and CP2 - 404 from both of them)
#

HOST=$(hostname)
echo "01. Create an inclusive registration for entity type IE in CB, for CP1"
echo "======================================================================"
Expand All @@ -58,7 +61,7 @@ payload='{
}
],
"endpoint": "'$HOST:$CP1_PORT'",
"operations": [ "queryEntity" ]
"operations": [ "queryEntity", "deleteEntity" ]
}'
orionCurl --url /ngsi-ld/v1/csourceRegistrations --payload "$payload"
echo
Expand All @@ -68,7 +71,7 @@ echo
echo "02. Create an inclusive registration for entity type IE in CP1, for CP2"
echo "======================================================================="
payload='{
"id": "urn:R1",
"id": "urn:R2",
"type": "ContextSourceRegistration",
"information": [
{
Expand All @@ -80,7 +83,7 @@ payload='{
}
],
"endpoint": "'$HOST:$CP2_PORT'",
"operations": [ "queryEntity" ]
"operations": [ "queryEntity", "deleteEntity" ]
}'
orionCurl --url /ngsi-ld/v1/csourceRegistrations --payload "$payload" --port $CP1_PORT
echo
Expand All @@ -90,7 +93,7 @@ echo
echo "03. Create an inclusive registration for entity type IE in CP2, for CB"
echo "======================================================================"
payload='{
"id": "urn:R1",
"id": "urn:R3",
"type": "ContextSourceRegistration",
"information": [
{
Expand All @@ -102,7 +105,7 @@ payload='{
}
],
"endpoint": "'$HOST:$CB_PORT'",
"operations": [ "queryEntity" ]
"operations": [ "queryEntity", "deleteEntity" ]
}'
orionCurl --url /ngsi-ld/v1/csourceRegistrations --payload "$payload" --port $CP2_PORT
echo
Expand Down Expand Up @@ -161,6 +164,20 @@ echo
echo


echo "08. DELETE urn:E2 via CB (forwarded message to both CP1 and CP2 - success from CP1, 404 from CP2)"
echo "================================================================================================="
orionCurl --url /ngsi-ld/v1/entities/urn:E2?type=IE -X DELETE
echo
echo


echo "09. DELETE urn:E1 via CB (forwarded message to both CP1 and CP2 - 404 from both of them)"
echo "========================================================================================"
orionCurl --url /ngsi-ld/v1/entities/urn:E1?type=IE -X DELETE
echo
echo


--REGEXPECT--
01. Create an inclusive registration for entity type IE in CB, for CP1
======================================================================
Expand All @@ -176,7 +193,7 @@ Location: /ngsi-ld/v1/csourceRegistrations/urn:R1
HTTP/1.1 201 Created
Content-Length: 0
Date: REGEX(.*)
Location: /ngsi-ld/v1/csourceRegistrations/urn:R1
Location: /ngsi-ld/v1/csourceRegistrations/urn:R2



Expand All @@ -185,7 +202,7 @@ Location: /ngsi-ld/v1/csourceRegistrations/urn:R1
HTTP/1.1 201 Created
Content-Length: 0
Date: REGEX(.*)
Location: /ngsi-ld/v1/csourceRegistrations/urn:R1
Location: /ngsi-ld/v1/csourceRegistrations/urn:R3



Expand Down Expand Up @@ -253,6 +270,63 @@ NGSILD-EntityMap: urn:ngsi-ld:entity-map:REGEX(.*)
]


08. DELETE urn:E2 via CB (forwarded message to both CP1 and CP2 - success from CP1, 404 from CP2)
=================================================================================================
HTTP/1.1 207 Multi-Status
Content-Length: 184
Content-Type: application/json
Date: REGEX(.*)
Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.6.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"

{
"notUpdated": [
{
"detail": "urn:E2",
"statusCode": 404,
"title": "Not Found"
},
{
"detail": "urn:E2",
"registrationId": "urn:R1",
"statusCode": 404,
"title": "Entity not found"
}
],
"updated": [
"urn:E2"
]
}


09. DELETE urn:E1 via CB (forwarded message to both CP1 and CP2 - 404 from both of them)
========================================================================================
HTTP/1.1 207 Multi-Status
Content-Length: 210
Content-Type: application/json
Date: REGEX(.*)
Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.6.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"

{
"notUpdated": [
{
"detail": "urn:E1",
"registrationId": "urn:R1",
"statusCode": 404,
"title": "Not Found"
},
{
"detail": "urn:E1",
"registrationId": "urn:R1",
"statusCode": 404,
"title": "Entity not found"
}
],
"updated": [
"urn:E1"
]
}


--TEARDOWN--
brokerStop CB
brokerStop CP1
Expand Down

0 comments on commit 367aae7

Please sign in to comment.