Skip to content

Commit

Permalink
Support for different core contexts via CLI parameter -coreContext <v…
Browse files Browse the repository at this point in the history
…1.0|v1.3|v1.4|v1.5|v1.6|v1.7> (v1.6 is default)
  • Loading branch information
kzangeli committed Aug 10, 2023
1 parent be01eb8 commit 2152f58
Show file tree
Hide file tree
Showing 628 changed files with 3,002 additions and 2,663 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Fixed issues:
#280 Protection for POST /temporal/entities against usage when troe is not enabled
#280 Support for different core contexts via CLI parameter -coreContext <v1.0|v1.3|v1.4|v1.5|v1.6|v1.7> (v1.6 is default)
30 changes: 29 additions & 1 deletion src/app/orionld/orionld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ extern "C"
#include "orionld/prometheus/promInit.h" // promInit
#include "orionld/mongoc/mongocInit.h" // mongocInit
#include "orionld/mongoc/mongocServerVersionGet.h" // mongocServerVersionGet
#include "orionld/context/orionldCoreContext.h" // ORIONLD_CORE_CONTEXT_URL_*
#include "orionld/context/orionldContextFromUrl.h" // contextDownloadListInit, contextDownloadListRelease
#include "orionld/contextCache/orionldContextCacheRelease.h" // orionldContextCacheRelease
#include "orionld/rest/orionldServiceInit.h" // orionldServiceInit
Expand Down Expand Up @@ -252,6 +253,7 @@ bool experimental = false;
bool mongocOnly = false;
bool debugCurl = false;
int cSubCounters;
char coreContextVersion[64];



Expand Down Expand Up @@ -338,6 +340,7 @@ int cSubCounters;
#define DBURI_DESC "complete URI for database connection"
#define DEBUG_CURL_DESC "turn on debugging of libcurl - to the broker's logfile"
#define CSUBCOUNTERS_DESC "number of subscription counter updates before flush from sub-cache to DB (0: never, 1: always)"
#define CORE_CONTEXT_DESC "Core context version (v1.0|v1.3|v1.4|v1.5|v1.6|v1.7) - v1.6 is default"



Expand All @@ -360,6 +363,8 @@ int cSubCounters;
*/
PaArgument paArgs[] =
{
{ "-coreContext", coreContextVersion, "CORE_CONTEXT", PaString, PaOpt, _i ORIONLD_CORE_CONTEXT_URL_DEFAULT, PaNL, PaNL, CORE_CONTEXT_DESC },

{ "-noswap", &noswap, "NOSWAP", PaBool, PaHid, false, false, true, NOSWAP_DESC },
{ "-fg", &fg, "FOREGROUND", PaBool, PaOpt, false, false, true, FG_DESC },
{ "-localIp", bindAddress, "LOCALIP", PaString, PaOpt, IP_ALL, PaNL, PaNL, LOCALIP_DESC },
Expand Down Expand Up @@ -783,6 +788,7 @@ static void versionInfo(void)
LM_K(("-----------------------------------------"));
LM_K(("orionld version: %s", orionldVersion));
LM_K(("based on orion: %s", ORION_VERSION));
LM_K(("core @context: %s", coreContextUrl));
LM_K(("git hash: %s", GIT_HASH));
LM_K(("build branch: %s", ORIONLD_BRANCH));
LM_K(("compiled by: %s", COMPILED_BY));
Expand Down Expand Up @@ -883,6 +889,24 @@ void regCachePresent(void)



// -----------------------------------------------------------------------------
//
// coreContextUrlSetup -
//
static char* coreContextUrlSetup(const char* version)
{
if (strcmp(version, "v1.0") == 0) return ORIONLD_CORE_CONTEXT_URL_V1_0;
else if (strcmp(version, "v1.3") == 0) return ORIONLD_CORE_CONTEXT_URL_V1_3;
else if (strcmp(version, "v1.4") == 0) return ORIONLD_CORE_CONTEXT_URL_V1_4;
else if (strcmp(version, "v1.5") == 0) return ORIONLD_CORE_CONTEXT_URL_V1_5;
else if (strcmp(version, "v1.6") == 0) return ORIONLD_CORE_CONTEXT_URL_V1_6;
else if (strcmp(version, "v1.7") == 0) return ORIONLD_CORE_CONTEXT_URL_V1_7;

return NULL;
}



#define LOG_FILE_LINE_FORMAT "time=DATE | lvl=TYPE | corr=CORR_ID | trans=TRANS_ID | from=FROM_IP | srv=SERVICE | subsrv=SUB_SERVICE | comp=Orion | op=FILE[LINE]:FUNC | msg=TEXT"
/* ****************************************************************************
*
Expand Down Expand Up @@ -1009,6 +1033,11 @@ int main(int argC, char* argV[])
strncpy(paLogLevel, "DEBUG", sizeof(paLogLevel) - 1);

paParse(paArgs, argC, (char**) argV, 1, false);

coreContextUrl = coreContextUrlSetup(coreContextVersion);
if (coreContextUrl == NULL)
LM_X(1, ("Invalid version for the Core Context: %s (valid: v1.0|v1.3|v1.4|v1.5|v1.6|v1.7)", coreContextVersion));

lmTimeFormat(0, (char*) "%Y-%m-%dT%H:%M:%S");

#if 0
Expand Down Expand Up @@ -1078,7 +1107,6 @@ int main(int argC, char* argV[])
}

notificationModeParse(notificationMode, &notificationQueueSize, &notificationThreadNum);

LM_I(("Orion Context Broker is running"));

versionInfo();
Expand Down
1 change: 1 addition & 0 deletions src/lib/logMsg/traceLevels.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ typedef enum TraceLevels
LmtContexts = 80, // Contexts
LmtContextTree, // Context Tree
LmtContextCache, // Context Cache
LmtCoreContext, // Core Context

// GeoJSON
LmtGeoJSON = 90, // GeoJSON ... everything (for now)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/orionld/common/orionldState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Kjson kjson;
Kjson* kjsonP;
uint16_t portNo = 0;
int dbNameLen;
char* coreContextUrl = (char*) ORIONLD_CORE_CONTEXT_URL_V1_0;
char* coreContextUrl = (char*) ORIONLD_CORE_CONTEXT_URL_DEFAULT; // v1.6, see orionld/context/orionldCoreContext.h
char orionldHostName[128];
int orionldHostNameLen = -1;
OrionldGeoIndex* geoIndexList = NULL;
Expand Down
1 change: 1 addition & 0 deletions src/lib/orionld/context/orionldContextFromBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ OrionldContext* orionldContextFromBuffer(char* url, OrionldContextOrigin origin,
return NULL;
}

LM_T(LmtCoreContext, ("Parsed the context buffer into a KjNode tree and looked up the @context member"));
OrionldContext* contextP = orionldContextFromTree(url, origin, id, contextNodeP);
return contextP;
}
25 changes: 0 additions & 25 deletions src/lib/orionld/context/orionldContextFromTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,6 @@ extern "C"



// -----------------------------------------------------------------------------
//
// willBeSimplified -
//
bool willBeSimplified(KjNode* contextTreeP, int* itemsInArrayP)
{
int itemsInArray = 0;
int itemsToRemove = 0;

for (KjNode* itemP = contextTreeP->value.firstChildP; itemP != NULL; itemP = itemP->next)
{
++itemsInArray;
if ((itemP->type == KjString) && (strcmp(itemP->value.s, coreContextUrl) == 0))
++itemsToRemove;
}

if (itemsInArray - itemsToRemove > 1) // More than one item in array - must stay array
return false;

*itemsInArrayP = itemsInArray;
return true;
}



// -----------------------------------------------------------------------------
//
// orionldContextFromTree -
Expand Down
6 changes: 6 additions & 0 deletions src/lib/orionld/context/orionldContextFromUrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
#include "orionld/context/orionldContextFromUrl.h" // Own interface



// -----------------------------------------------------------------------------
//
// StringListItem -
//
typedef struct StringListItem
{
char name[256];
Expand Down Expand Up @@ -250,6 +255,7 @@ OrionldContext* orionldContextFromUrl(char* url, char* id)
if (buffer == NULL)
return NULL;

LM_T(LmtCoreContext, ("Downloaded the conext '%s'", url));
contextP = orionldContextFromBuffer(url, OrionldContextDownloaded, id, buffer);
if (contextP == NULL)
{
Expand Down
5 changes: 3 additions & 2 deletions src/lib/orionld/context/orionldContextItemExpand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ char* orionldContextItemExpand
OrionldContextItem** contextItemPP
)
{
OrionldContextItem* contextItemP;
OrionldContextItem* contextItemP = NULL;
char* colonP;

if (contextP == NULL)
Expand All @@ -75,7 +75,8 @@ char* orionldContextItemExpand
return orionldContextPrefixExpand(contextP, shortName, colonP);

// 1. Lookup in Core Context
contextItemP = orionldContextItemLookup(orionldCoreContextP, shortName, NULL);
if (orionldCoreContextP != NULL)
contextItemP = orionldContextItemLookup(orionldCoreContextP, shortName, NULL);

// 2. Lookup in given context (unless it's the Core Context)
if ((contextItemP == NULL) && (contextP != orionldCoreContextP))
Expand Down
3 changes: 2 additions & 1 deletion src/lib/orionld/context/orionldContextSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extern "C"

#include "orionld/context/OrionldContext.h" // OrionldContext
#include "orionld/common/orionldState.h" // coreContextUrl
#include "orionld/context/orionldCoreContext.h" // ORIONLD_CORE_CONTEXT_URL_V1_BASE
#include "orionld/context/orionldContextSimplify.h" // Own interface


Expand All @@ -58,7 +59,7 @@ KjNode* orionldContextSimplify(KjNode* contextTreeP, int* itemsInArrayP)
{
KjNode* next = nodeP->next;

if ((nodeP->type == KjString) && (strcmp(nodeP->value.s, coreContextUrl) == 0))
if ((nodeP->type == KjString) && (strncmp(nodeP->value.s, ORIONLD_CORE_CONTEXT_URL_V1_BASE, 52) == 0))
kjChildRemove(contextTreeP, nodeP);
else
++itemsInArray;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/orionld/context/orionldCoreContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ int orionldDefaultUrlLen;
//
// builtinCoreContext
//
const char* builtinCoreContext = "{ \"@context\": {"
const char* builtinCoreContextUrl = ORIONLD_CORE_CONTEXT_URL_V1_0;
const char* builtinCoreContext = "{ \"@context\": {"
"\"ngsi-ld\": \"https://uri.etsi.org/ngsi-ld/\","
"\"id\": \"@id\","
"\"type\": \"@type\","
Expand Down
19 changes: 18 additions & 1 deletion src/lib/orionld/context/orionldCoreContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ extern "C"
//
// ORIONLD_CORE_CONTEXT_URL -
//
#define ORIONLD_CORE_CONTEXT_URL_V1_0 (char*) "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld"
#define ORIONLD_CORE_CONTEXT_URL_V1_BASE (char*) "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context"
#define ORIONLD_CORE_CONTEXT_URL_V1_0 (char*) "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context.jsonld"
#define ORIONLD_CORE_CONTEXT_URL_V1_3 (char*) "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.3.jsonld"
#define ORIONLD_CORE_CONTEXT_URL_V1_4 (char*) "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.4.jsonld"
#define ORIONLD_CORE_CONTEXT_URL_V1_5 (char*) "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.5.jsonld"
#define ORIONLD_CORE_CONTEXT_URL_V1_6 (char*) "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.6.jsonld"
#define ORIONLD_CORE_CONTEXT_URL_V1_7 (char*) "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.7.jsonld"
#define ORIONLD_CORE_CONTEXT_URL_DEFAULT (char*) "v1.6"



Expand All @@ -64,4 +71,14 @@ extern char* orionldDefaultUrl;
//
extern int orionldDefaultUrlLen;



// -----------------------------------------------------------------------------
//
// builtinCoreContext -
//
extern const char* builtinCoreContextUrl;
extern const char* builtinCoreContext;


#endif // SRC_LIB_ORIONLD_CONTEXT_ORIONLDCORECONTEXT_H_
12 changes: 7 additions & 5 deletions src/lib/orionld/contextCache/orionldContextCacheInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extern "C"

#include "orionld/common/orionldState.h" // dbHost, coreContextUrl, builtinCoreContext
#include "orionld/mongoc/mongocContextCacheGet.h" // mongocContextCacheGet
#include "orionld/context/orionldCoreContext.h" // orionldCoreContextP
#include "orionld/context/orionldCoreContext.h" // orionldCoreContextP, builtinCoreContextUrl, builtinCoreContext
#include "orionld/context/orionldContextFromUrl.h" // orionldContextFromUrl
#include "orionld/context/orionldContextFromTree.h" // orionldContextFromTree
#include "orionld/context/orionldContextFromBuffer.h" // orionldContextFromBuffer
Expand Down Expand Up @@ -120,6 +120,7 @@ void orionldContextCacheInit(void)
//

// 1. Find the Core Context
LM_T(LmtCoreContext, ("Trying to find the core context (%s)", coreContextUrl));
if (contextArray != NULL)
{
KjNode* contextNodeP = contextArray->value.firstChildP;
Expand Down Expand Up @@ -151,9 +152,10 @@ void orionldContextCacheInit(void)
}
}

// Still no core context? - download it
// Still no core context? - try to download it
if (orionldCoreContextP == NULL)
{
LM_T(LmtCoreContext, ("Still no core context - trying to download it (%s)", coreContextUrl));
orionldCoreContextP = orionldContextFromUrl(coreContextUrl, NULL);
if (orionldCoreContextP == NULL)
LM_W(("Unable to download the core context (%s: %s)", orionldState.pd.title, orionldState.pd.detail));
Expand All @@ -162,7 +164,7 @@ void orionldContextCacheInit(void)
// Still no core context? - use the default core context, meant for airgapped setups
if (orionldCoreContextP == NULL)
{
LM_T(LmtContextCache, ("No network? Getting the core context from builtin"));
LM_T(LmtCoreContext, ("Still no core context - no network? Getting the core context from builtin"));

//
// The builtin core context is a string in a read-only segment.
Expand All @@ -175,13 +177,13 @@ void orionldContextCacheInit(void)
LM_X(1, ("Out of memory trying to allocate %d bytes for the built-in Core Context"));

memcpy(buf, builtinCoreContext, bufLen + 1);
orionldCoreContextP = orionldContextFromBuffer(coreContextUrl, OrionldContextBuiltinCoreContext, coreContextUrl, buf);
orionldCoreContextP = orionldContextFromBuffer(coreContextUrl, OrionldContextBuiltinCoreContext, (char*) builtinCoreContextUrl, buf);
free(buf);
LM_T(LmtContextCache, ("Core Context at %p", orionldCoreContextP));
if (orionldCoreContextP == NULL)
LM_X(1, ("Unable to create the core context from in-compiled default core context (%s: %s)", orionldState.pd.title, orionldState.pd.detail));
LM_W(("Falling back to Built-in Core Context (hard-coded copy of %s)", builtinCoreContextUrl));
}
LM_T(LmtContextCache, ("Core Context at %p", orionldCoreContextP));

if (contextArray == NULL)
return;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/orionld/dbModel/dbModelFromApiSubscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extern "C"

#include "orionld/common/orionldState.h" // orionldState
#include "orionld/common/orionldError.h" // orionldError
#include "orionld/context/orionldCoreContext.h" // ORIONLD_CORE_CONTEXT_URL_V1_0
#include "orionld/context/orionldCoreContext.h" // orionldCoreContextP
#include "orionld/context/orionldContextSimplify.h" // orionldContextSimplify
#include "orionld/dbModel/dbModelFromApiKeyValues.h" // dbModelFromApiKeyValues
#include "orionld/dbModel/dbModelFromApiCoordinates.h" // dbModelFromApiCoordinates
Expand Down Expand Up @@ -514,7 +514,7 @@ bool dbModelFromApiSubscription(KjNode* apiSubscriptionP, bool patch)
// For now, I'll just overwrite the context with the Core Context
//
orionldState.payloadContextNode->type = KjString;
orionldState.payloadContextNode->value.s = (char*) ORIONLD_CORE_CONTEXT_URL_V1_0;
orionldState.payloadContextNode->value.s = orionldCoreContextP->url;
LM_W(("Warning - the context is not a string - changing it for the Core Context (API Spec v1.6)"));
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/orionld/forwarding/distOpSend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ bool distOpSend(DistOp* distOpP, const char* dateHeader, const char* xForwardedF
kjChildRemove(distOpP->requestBody, contextP);

if (distOpP->regP->contextP == NULL)
contextP = kjString(orionldState.kjsonP, "@context", ORIONLD_CORE_CONTEXT_URL_V1_0);
contextP = kjString(orionldState.kjsonP, "@context", orionldCoreContextP->url);
else
contextP = kjString(orionldState.kjsonP, "@context", distOpP->regP->contextP->url);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/orionld/notifications/notificationSend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ int notificationSend(OrionldAlterationMatch* mAltP, double timestamp, CURL** cur
if ((addLinkHeader == true) && (ngsiv2 == false)) // Add Link header - but not if NGSIv2 Cross Notification
{
char linkHeader[512];
const char* link = (mAltP->subP->ldContext == "")? ORIONLD_CORE_CONTEXT_URL_V1_0 : mAltP->subP->ldContext.c_str();
const char* link = (mAltP->subP->ldContext == "")? orionldCoreContextP->url : mAltP->subP->ldContext.c_str();

snprintf(linkHeader, sizeof(linkHeader), "Link: <%s>; rel=\"http://www.w3.org/ns/json-ld#context\"; type=\"application/ld+json\"\r\n", link);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Usage: orionld [option '-U' (extended usage)]
[option '-logLevel' <initial log level (NONE, FATAL, ERROR, WARN, INFO, DEBUG)>]
[option '-logAppend' (append to log-file)]

[option '-coreContext' <Core context version (v1.0|v1.3|v1.4|v1.5|v1.6|v1.7) - v1.6 is default>]
[option '-fg' (don't start as daemon)]
[option '-localIp' <IP to receive new connections>]
[option '-port' <port to receive new connections>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Usage: orionld [option '-U' (extended usage)]
[option '-logLevel' <initial log level (NONE, FATAL, ERROR, WARN, INFO, DEBUG)>]
[option '-logAppend' (append to log-file)]

[option '-coreContext' <Core context version (v1.0|v1.3|v1.4|v1.5|v1.6|v1.7) - v1.6 is default>]
[option '-fg' (don't start as daemon)]
[option '-localIp' <IP to receive new connections>]
[option '-port' <port to receive new connections>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ HTTP/1.1 200 OK
Content-Length: 186
Content-Type: application/json
Date: REGEX(.*)
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"
Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)

{
"id": "urn:ngsi-ld:T:2018-11-22T10:00:00",
Expand All @@ -133,7 +133,7 @@ HTTP/1.1 200 OK
Content-Length: 114
Content-Type: application/json
Date: REGEX(.*)
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"
Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)

{
"id": "urn:ngsi-ld:T:2018-11-22T10:00:00",
Expand All @@ -153,7 +153,7 @@ HTTP/1.1 200 OK
Content-Length: 222
Content-Type: application/json
Date: REGEX(.*)
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"
Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)

[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ HTTP/1.1 200 OK
Content-Length: 295
Content-Type: application/json
Date: REGEX(.*)
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"
Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)

{
"id": "urn:ngsi-ld:T:2018-11-22T10:00:00",
Expand All @@ -113,7 +113,7 @@ HTTP/1.1 200 OK
Content-Length: 295
Content-Type: application/json
Date: REGEX(.*)
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"
Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)

{
"id": "urn:ngsi-ld:T:2018-11-22T10:00:00",
Expand Down
Loading

0 comments on commit 2152f58

Please sign in to comment.