Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete attribute using mongo C++ legacy driver #460

Merged
merged 4 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 55 additions & 52 deletions src/lib/orionld/serviceRoutines/orionldDeleteAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,37 @@
* For those usages not covered by this license please contact with
* orionld at fiware dot org
*
* Author: Ken Zangelin
* Author: Ken Zangelin and Gabriel Quaresma
*/
#include <string> // std::string - for servicePath only
#include <vector> // std::vector - for servicePath only

extern "C"
{
#include "kbase/kMacros.h" // K_VEC_SIZE, K_FT
#include "kjson/kjBuilder.h" // kjChildRemove
#include "kjson/kjRender.h" // kjRender
#include "kjson/kjLookup.h" // kjLookup
#include "kjson/kjClone.h" // kjClone
#include "kalloc/kaAlloc.h" // kaAlloc
#include "kalloc/kaStrdup.h" // kaStrdup
}

#include "logMsg/logMsg.h" // LM_*
#include "logMsg/traceLevels.h" // Lmt*

#include "rest/ConnectionInfo.h" // ConnectionInfo
#include "rest/HttpStatusCode.h" // SccContextElementNotFound
#include "ngsi10/UpdateContextRequest.h" // UpdateContextRequest
#include "ngsi10/UpdateContextResponse.h" // UpdateContextResponse
#include "mongoBackend/mongoUpdateContext.h" // mongoUpdateContext

#include "orionld/common/orionldErrorResponse.h" // orionldErrorResponseCreate
#include "orionld/common/urlCheck.h" // urlCheck
#include "orionld/common/urnCheck.h" // urnCheck
#include "orionld/common/httpStatusCodeToOrionldErrorType.h" // httpStatusCodeToOrionldErrorType
#include "orionld/common/orionldState.h" // orionldState
#include "orionld/common/dotForEq.h" // dotForEq
#include "orionld/common/eqForDot.h" // eqForDot
#include "orionld/db/dbConfiguration.h" // dbEntityAttributeLookup, dbEntityAttributesDelete
#include "orionld/context/orionldContextItemExpand.h" // orionldContextItemExpand
#include "orionld/mongoBackend/mongoAttributeExists.h" // mongoAttributeExists
#include "orionld/serviceRoutines/orionldDeleteAttribute.h" // Own Interface


Expand All @@ -49,66 +61,57 @@
//
bool orionldDeleteAttribute(ConnectionInfo* ciP)
{
char* attrNameP;

if ((strncmp(orionldState.wildcard[1], "http://", 7) == 0) || (strncmp(orionldState.wildcard[1], "https://", 8) == 0))
attrNameP = orionldState.wildcard[1];
else
attrNameP = orionldContextItemExpand(orionldState.contextP, orionldState.wildcard[1], NULL, true, NULL);

//
// Does the attribute to be deleted even exist?
//
char* entityId = orionldState.wildcard[0];
char* attrName = orionldState.wildcard[1];
char* attrNameP;
char* details;

//
// FIXME: Extra call to mongo - can this be avoided?
// By looking at the error code from the delete operation in mongo ...
//
if (mongoAttributeExists(orionldState.wildcard[0], attrNameP, orionldState.tenant) == false)
// Make sure the Entity ID is a valid URI
if ((urlCheck(entityId, &details) == false) && (urnCheck(entityId, &details) == false))
{
orionldState.httpStatusCode = SccContextElementNotFound;
orionldErrorResponseCreate(OrionldBadRequestData, "Attribute Not Found", orionldState.wildcard[1]);
LM_W(("Bad Input (Invalid Entity ID '%s' - not a URI)", entityId));
orionldErrorResponseCreate(OrionldBadRequestData, "Invalid Entity ID", details);
orionldState.httpStatusCode = SccBadRequest;
return false;
}

// Create and fill in attribute and entity
ContextAttribute* caP = new ContextAttribute;
Entity entity;

entity.id = orionldState.wildcard[0];
caP->name = attrNameP;
entity.attributeVector.push_back(caP);

LM_T(LmtServiceRoutine, ("Deleting attribute '%s' of entity '%s'", orionldState.wildcard[1], orionldState.wildcard[0]));
if (dbEntityLookup(entityId) == NULL)
{
LM_T(LmtService, ("Entity Not Found: %s", entityId));
orionldErrorResponseCreate(OrionldResourceNotFound, "The requested entity has not been found. Check its id", entityId);
orionldState.httpStatusCode = SccNotFound; // 404
return false;
}

UpdateContextRequest ucr;
UpdateContextResponse ucResponse;
std::vector<std::string> servicePath;
if ((strncmp(attrName, "http://", 7) == 0) || (strncmp(attrName, "https://", 8) == 0))
attrNameP = attrName;
else
{
attrNameP = orionldContextItemExpand(orionldState.contextP, attrName, NULL, true, NULL);
// attrNameP might point to a field inside the context cache - must make our own copy as 'dotForEq' will modifyit
attrNameP = kaStrdup(&orionldState.kalloc, attrNameP);
}

servicePath.push_back("/");
// IMPORTANT: Must call dbEntityAttributeLookup before replacing dots for eqs
if (dbEntityAttributeLookup(entityId, attrNameP) == NULL)
{
LM_T(LmtService, ("Attribute Not Found: %s/%s", entityId, attrNameP));
orionldState.httpStatusCode = SccContextElementNotFound;
orionldErrorResponseCreate(OrionldBadRequestData, "Attribute Not Found", attrNameP);
return false;
}

ucr.fill(&entity, ActionTypeDelete);
orionldState.httpStatusCode = mongoUpdateContext(&ucr,
&ucResponse,
orionldState.tenant,
servicePath,
ciP->uriParam,
ciP->httpHeaders.xauthToken,
ciP->httpHeaders.correlator,
ciP->httpHeaders.ngsiv2AttrsFormat,
ciP->apiVersion,
NGSIV2_NO_FLAVOUR);
dotForEq(attrNameP);

if (orionldState.httpStatusCode != SccOk)
char* attrNameV[1] = { attrNameP };
if (dbEntityAttributesDelete(entityId, attrNameV, 1) == false)
{
orionldErrorResponseCreate(httpStatusCodeToOrionldErrorType(orionldState.httpStatusCode), "DELETE /ngsi-ld/v1/entities/*/attrs/*", orionldState.wildcard[0]);
ucr.release();

LM_W(("dbEntityAttributesDelete failed"));
orionldState.httpStatusCode = SccContextElementNotFound;
orionldErrorResponseCreate(OrionldBadRequestData, "Attribute Not Found", attrNameP);
return false;
}

ucr.release();
orionldState.httpStatusCode = SccNoContent;

return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ Date: REGEX(.*)
03. Attempt to delete P2, without context, using alias P2 => see error Not Found
================================================================================
HTTP/1.1 404 Not Found
Content-Length: 105
Content-Length: 150
Content-Type: application/json
Date: REGEX(.*)

{
"detail": "P2",
"detail": "https://uri.etsi.org/ngsi-ld/default-context/P2",
"title": "Attribute Not Found",
"type": "https://uri.etsi.org/ngsi-ld/errors/BadRequestData"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Attribute Deletion - create an entity with three attrs and then delete one of th
--SHELL-INIT--
export BROKER=orionld
dbInit CB
brokerStart CB 0-255 --prettyPrint
brokerStart CB 0 --prettyPrint

--SHELL--

Expand Down Expand Up @@ -160,12 +160,12 @@ Date: REGEX(.*)
05. Attempt to delete non-existing attribute P4
===============================================
HTTP/1.1 404 Not Found
Content-Length: 105
Content-Length: 150
Content-Type: application/json
Date: REGEX(.*)

{
"detail": "P4",
"detail": "https://uri.etsi.org/ngsi-ld/default-context/P4",
"title": "Attribute Not Found",
"type": "https://uri.etsi.org/ngsi-ld/errors/BadRequestData"
}
Expand Down
8 changes: 4 additions & 4 deletions test/functionalTest/cases/0000_ngsild/ngsild_url_parse.test
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,14 @@ Date: REGEX(.*)
16. DELETE /ngsi-ld/v1/entities/E1/attrs/A1
===========================================
HTTP/1.1 404 Not Found
Content-Length: 141
Content-Length: 175
Content-Type: application/json
Date: REGEX(.*)

{
"detail": "http://a.b.c/list/of/attributes/A1.xxx",
"title": "Attribute Not Found",
"type": "https://uri.etsi.org/ngsi-ld/errors/BadRequestData"
"detail": "http://a.b.c/list/of/entities/E1.xxx",
"title": "The requested entity has not been found. Check its id",
"type": "https://uri.etsi.org/ngsi-ld/errors/ResourceNotFound"
}


Expand Down