Skip to content

Commit

Permalink
Fixeda bug about expanding attribute names in URL. Moved orionldAttri…
Browse files Browse the repository at this point in the history
…buteExpand+orionldSubAttributeExpand to own modules. Added an exhaustive TRoE test for deleting attributes
  • Loading branch information
kzangeli committed Mar 15, 2021
1 parent c52b216 commit d07d6e7
Show file tree
Hide file tree
Showing 13 changed files with 1,108 additions and 62 deletions.
2 changes: 2 additions & 0 deletions src/lib/orionld/context/CMakeLists.txt
Expand Up @@ -48,6 +48,8 @@ SET (SOURCES
orionldContextCacheGet.cpp
orionldContextCacheRelease.cpp
orionldContextItemAlreadyExpanded.cpp
orionldAttributeExpand.cpp
orionldSubAttributeExpand.cpp
)

# Include directories
Expand Down
59 changes: 59 additions & 0 deletions src/lib/orionld/context/orionldAttributeExpand.cpp
@@ -0,0 +1,59 @@
/*
*
* Copyright 2021 FIWARE Foundation e.V.
*
* This file is part of Orion-LD Context Broker.
*
* Orion-LD Context Broker is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Orion-LD Context Broker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by this license please contact with
* orionld at fiware dot org
*
* Author: Ken Zangelin
*/
#include <string.h> // strchr

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

#include "orionld/context/orionldContextItemAlreadyExpanded.h" // orionldContextItemAlreadyExpanded
#include "orionld/context/orionldContextItemExpand.h" // orionldContextItemExpand
#include "orionld/context/orionldAttributeExpand.h" // Own interface


// -----------------------------------------------------------------------------
//
// orionldAttributeExpand -
//
// This function expands unless:
// - has already been expanded
// - is a special attribute such as 'location'
//
char* orionldAttributeExpand
(
OrionldContext* contextP,
char* shortName,
bool useDefaultUrlIfNotFound,
OrionldContextItem** contextItemPP
)
{
if (orionldContextItemAlreadyExpanded(shortName) == true)
return shortName;

if (strcmp(shortName, "location") == 0) return shortName;
else if (strcmp(shortName, "observationSpace") == 0) return shortName;
else if (strcmp(shortName, "operationSpace") == 0) return shortName;

return orionldContextItemExpand(contextP, shortName, useDefaultUrlIfNotFound, contextItemPP);
}
49 changes: 49 additions & 0 deletions src/lib/orionld/context/orionldAttributeExpand.h
@@ -0,0 +1,49 @@
#ifndef SRC_LIB_ORIONLD_CONTEXT_ORIONLDATTRIBUTEEXPAND_H_
#define SRC_LIB_ORIONLD_CONTEXT_ORIONLDATTRIBUTEEXPAND_H_

/*
*
* Copyright 2019 FIWARE Foundation e.V.
*
* This file is part of Orion-LD Context Broker.
*
* Orion-LD Context Broker is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Orion-LD Context Broker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by this license please contact with
* orionld at fiware dot org
*
* Author: Ken Zangelin
*/
#include "orionld/context/OrionldContext.h" // OrionldContext
#include "orionld/context/OrionldContextItem.h" // OrionldContextItem



// -----------------------------------------------------------------------------
//
// orionldAttributeExpand.h -
//
// This function expands unless:
// - has already been expanded
// - is a special attribute such as location, datasetId, ...
//
extern char* orionldAttributeExpand
(
OrionldContext* contextP,
char* shortName,
bool useDefaultUrlIfNotFound,
OrionldContextItem** contextItemPP
);

#endif // SRC_LIB_ORIONLD_CONTEXT_ORIONLDATTRIBUTEEXPAND_H_
Expand Up @@ -30,7 +30,7 @@
//
// orionldContextItemAlreadyExpanded -
//
bool orionldContextItemAlreadyExpanded(char* value)
bool orionldContextItemAlreadyExpanded(const char* value)
{
if (value == NULL)
return false;
Expand Down
Expand Up @@ -32,6 +32,6 @@
//
// orionldContextItemAlreadyExpanded -
//
extern bool orionldContextItemAlreadyExpanded(char* value);
extern bool orionldContextItemAlreadyExpanded(const char* value);

#endif // SRC_LIB_ORIONLD_CONTEXT_ORIONLDCONTEXTITEMALREADYEXPANDED_H_
60 changes: 60 additions & 0 deletions src/lib/orionld/context/orionldSubAttributeExpand.cpp
@@ -0,0 +1,60 @@
/*
*
* Copyright 2021 FIWARE Foundation e.V.
*
* This file is part of Orion-LD Context Broker.
*
* Orion-LD Context Broker is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Orion-LD Context Broker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by this license please contact with
* orionld at fiware dot org
*
* Author: Ken Zangelin
*/
#include <string.h> // strchr

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

#include "orionld/context/orionldContextItemAlreadyExpanded.h" // orionldContextItemAlreadyExpanded
#include "orionld/context/orionldContextItemExpand.h" // orionldContextItemExpand
#include "orionld/context/orionldSubAttributeExpand.h" // Own interface


// -----------------------------------------------------------------------------
//
// orionldSubAttributeExpand -
//
// This function expands unless:
// - has already been expanded
// - is a special sub-attribute such as 'location', 'datasetyId', ...
//
char* orionldSubAttributeExpand
(
OrionldContext* contextP,
char* shortName,
bool useDefaultUrlIfNotFound,
OrionldContextItem** contextItemPP
)
{
if (orionldContextItemAlreadyExpanded(shortName) == true)
return shortName;

if (strcmp(shortName, "location") == 0) return shortName;
else if (strcmp(shortName, "observedAt") == 0) return shortName;
else if (strcmp(shortName, "unitCode") == 0) return shortName;
else if (strcmp(shortName, "datasetId") == 0) return shortName;

return orionldContextItemExpand(contextP, shortName, useDefaultUrlIfNotFound, contextItemPP);
}
49 changes: 49 additions & 0 deletions src/lib/orionld/context/orionldSubAttributeExpand.h
@@ -0,0 +1,49 @@
#ifndef SRC_LIB_ORIONLD_CONTEXT_ORIONLDSUBATTRIBUTEEXPAND_H_
#define SRC_LIB_ORIONLD_CONTEXT_ORIONLDSUBATTRIBUTEEXPAND_H_

/*
*
* Copyright 2019 FIWARE Foundation e.V.
*
* This file is part of Orion-LD Context Broker.
*
* Orion-LD Context Broker is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Orion-LD Context Broker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by this license please contact with
* orionld at fiware dot org
*
* Author: Ken Zangelin
*/
#include "orionld/context/OrionldContext.h" // OrionldContext
#include "orionld/context/OrionldContextItem.h" // OrionldContextItem



// -----------------------------------------------------------------------------
//
// orionldSubAttributeExpand.h -
//
// This function expands unless:
// - has already been expanded
// - is a special attribute such as location, datasetId, ...
//
extern char* orionldSubAttributeExpand
(
OrionldContext* contextP,
char* shortName,
bool useDefaultUrlIfNotFound,
OrionldContextItem** contextItemPP
);

#endif // SRC_LIB_ORIONLD_CONTEXT_ORIONLDSUBATTRIBUTEEXPAND_H_
50 changes: 2 additions & 48 deletions src/lib/orionld/kjTree/kjTreeToMetadata.cpp
Expand Up @@ -34,7 +34,8 @@ extern "C"
#include "ngsi/ContextAttribute.h" // ContextAttribute
#include "orionld/common/orionldState.h" // orionldState
#include "orionld/common/orionldErrorResponse.h" // orionldErrorResponseCreate
#include "orionld/context/orionldContextItemExpand.h" // orionldContextItemExpand
#include "orionld/context/orionldAttributeExpand.h" // orionldAttributeExpand
#include "orionld/context/orionldSubAttributeExpand.h" // orionldSubAttributeExpand
#include "orionld/kjTree/kjTreeToMetadata.h" // Own interface


Expand All @@ -49,53 +50,6 @@ extern bool metadataAdd(ContextAttribute* caP, KjNode* nodeP, char* caName);



// -----------------------------------------------------------------------------
//
// orionldSubAttributeExpand - expand the name of a sub-attribute
//
// FIXME: needs its own module under orionld/context
//
char* orionldSubAttributeExpand
(
OrionldContext* contextP,
char* shortName,
bool useDefaultUrlIfNotFound,
OrionldContextItem** contextItemPP
)
{
if (strcmp(shortName, "location") == 0) return shortName;
else if (strcmp(shortName, "observedAt") == 0) return shortName;
else if (strcmp(shortName, "unitCode") == 0) return shortName;
else if (strcmp(shortName, "datasetId") == 0) return shortName;

return orionldContextItemExpand(contextP, shortName, useDefaultUrlIfNotFound, contextItemPP);
}



// -----------------------------------------------------------------------------
//
// orionldAttributeExpand - expand the name of an attribute
//
// FIXME: needs its own module under orionld/context
//
char* orionldAttributeExpand
(
OrionldContext* contextP,
char* shortName,
bool useDefaultUrlIfNotFound,
OrionldContextItem** contextItemPP
)
{
if (strcmp(shortName, "location") == 0) return shortName;
else if (strcmp(shortName, "observationSpace") == 0) return shortName;
else if (strcmp(shortName, "operationSpace") == 0) return shortName;

return orionldContextItemExpand(contextP, shortName, useDefaultUrlIfNotFound, contextItemPP);
}



// -----------------------------------------------------------------------------
//
// kjTreeToMetadata -
Expand Down
11 changes: 2 additions & 9 deletions src/lib/orionld/serviceRoutines/orionldDeleteAttribute.cpp
Expand Up @@ -48,7 +48,7 @@ extern "C"
#include "orionld/common/eqForDot.h" // eqForDot
#include "orionld/payloadCheck/pcheckUri.h" // pcheckUri
#include "orionld/db/dbConfiguration.h" // dbEntityAttributeLookup, dbEntityAttributesDelete
#include "orionld/context/orionldContextItemExpand.h" // orionldContextItemExpand
#include "orionld/context/orionldAttributeExpand.h" // orionldAttributeExpand
#include "orionld/serviceRoutines/orionldDeleteAttribute.h" // Own Interface


Expand Down Expand Up @@ -81,14 +81,7 @@ bool orionldDeleteAttribute(ConnectionInfo* ciP)
return false;
}

if ((strncmp(attrName, "http://", 7) == 0) || (strncmp(attrName, "https://", 8) == 0))
attrNameP = attrName;
else
{
attrNameP = orionldContextItemExpand(orionldState.contextP, attrName, 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);
}
attrNameP = orionldAttributeExpand(orionldState.contextP, attrName, true, NULL);

// IMPORTANT: Must call dbEntityAttributeLookup before replacing dots for eqs
if (dbEntityAttributeLookup(entityId, attrNameP) == NULL)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/orionld/troe/troeDeleteAttribute.cpp
Expand Up @@ -29,7 +29,7 @@
#include "orionld/common/orionldState.h" // orionldState
#include "orionld/common/orionldErrorResponse.h" // orionldErrorResponseCreate
#include "orionld/common/uuidGenerate.h" // uuidGenerate
#include "orionld/context/orionldContextItemExpand.h" // orionldContextItemExpand
#include "orionld/context/orionldAttributeExpand.h" // orionldAttributeExpand
#include "orionld/troe/pgConnectionGet.h" // pgConnectionGet
#include "orionld/troe/pgConnectionRelease.h" // pgConnectionRelease
#include "orionld/troe/pgTransactionBegin.h" // pgTransactionBegin
Expand Down Expand Up @@ -62,7 +62,7 @@ bool troeDeleteAttribute(ConnectionInfo* ciP)
char instanceId[80];
uuidGenerate(instanceId, sizeof(instanceId), true);

attributeName = orionldContextItemExpand(orionldState.contextP, attributeName, true, NULL);
attributeName = orionldAttributeExpand(orionldState.contextP, attributeName, true, NULL);
if (pgAttributeDelete(connectionP, entityId, instanceId, attributeName, orionldState.requestTimeString) == false)
{
LM_E(("Database Error (delete attribute troe layer failed)"));
Expand Down
Expand Up @@ -29,7 +29,7 @@ dbInit CB
dbInit CB t1
pgInit $CB_DB_NAME
pgInit ${CB_DB_NAME}_t1
brokerStart CB 100 IPv4 -troe -multiservice
brokerStart CB 0 IPv4 -troe -multiservice

--SHELL--

Expand Down

0 comments on commit d07d6e7

Please sign in to comment.