Skip to content

Commit

Permalink
Merge pull request #1565 from FIWARE/contexts/subscriptions
Browse files Browse the repository at this point in the history
More fixes for context hosting
  • Loading branch information
kzangeli authored Feb 23, 2024
2 parents e0eb126 + 84bf194 commit 7b12bdb
Show file tree
Hide file tree
Showing 38 changed files with 739 additions and 97 deletions.
1 change: 1 addition & 0 deletions src/lib/orionld/common/subCacheApiSubscriptionInsert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ static void subCacheItemFill
cSubP->renderFormat = rFormat;

LM_T(LmtSysAttrs, ("sysAttrs: %s", (cSubP->sysAttrs == true)? "true" : "false"));
LM_T(LmtContextCacheStats, ("ldContext: '%s'", cSubP->ldContext));

KjNode* subscriptionIdP = kjLookup(apiSubscriptionP, "_id"); // "id" was changed to "_id" by orionldPostSubscriptions to accomodate the DB insertion
KjNode* subscriptionNameP = kjLookup(apiSubscriptionP, "subscriptionName"); // "name" is accepted too ...
Expand Down
4 changes: 3 additions & 1 deletion src/lib/orionld/context/orionldContextCreate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ OrionldContext* orionldContextCreate(const char* url, OrionldContextOrigin origi
LM_X(1, ("out of memory - trying to allocate a OrionldContext of %d bytes", sizeof(OrionldContext)));

contextP->origin = origin;
contextP->kind = OrionldContextCached; // By defaukt. Changed later to Hosted/Implicit if needed
contextP->kind = OrionldContextCached; // Default. Changed later to Hosted/Implicit if needed
contextP->parent = NULL;
contextP->createdAt = orionldState.requestTime;
contextP->usedAt = orionldState.requestTime;

// NULL URL means NOT to be saved - will live just inside the request-thread
if (url != NULL)
Expand Down
1 change: 0 additions & 1 deletion src/lib/orionld/context/orionldContextFromUrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ OrionldContext* orionldContextFromUrl(char* url, char* id)
if (contextP != NULL)
{
contextP->origin = OrionldContextDownloaded;
contextP->createdAt = orionldState.requestTime;
contextP->usedAt = orionldState.requestTime;

orionldContextCachePersist(contextP);
Expand Down
15 changes: 9 additions & 6 deletions src/lib/orionld/legacyDriver/kjTreeFromSubscription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,14 +536,17 @@ KjNode* kjTreeFromSubscription(ngsiv2::Subscription* subscriptionP, CachedSubscr
kjChildAdd(topP, nodeP);
}

// FIXME: This is not how it is meant ...
if (orionldState.out.contentType == MT_JSONLD)
// jsonldContext
if (subscriptionP->ldContext != "")
{
if (subscriptionP->ldContext != "")
nodeP = kjString(orionldState.kjsonP, "@context", subscriptionP->ldContext.c_str());
else
nodeP = kjString(orionldState.kjsonP, "@context", orionldCoreContextP->url);
nodeP = kjString(orionldState.kjsonP, "jsonldContext", subscriptionP->ldContext.c_str());
kjChildAdd(topP, nodeP);
}

// @context
if (orionldState.out.contentType == MT_JSONLD)
{
nodeP = kjString(orionldState.kjsonP, "@context", orionldState.contextP->url);
kjChildAdd(topP, nodeP);
}

Expand Down
14 changes: 14 additions & 0 deletions src/lib/orionld/mhd/mhdConnectionTreat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ extern "C"
#include "orionld/serviceRoutines/orionldGetEntity.h" // orionldGetEntity
#include "orionld/serviceRoutines/orionldGetEntities.h" // orionldGetEntities
#include "orionld/serviceRoutines/orionldPostEntities.h" // orionldPostEntities
#include "orionld/serviceRoutines/orionldPostSubscriptions.h" // orionldPostSubscriptions
#include "orionld/serviceRoutines/orionldPatchSubscription.h" // orionldPatchSubscription
#include "orionld/mhd/mhdReply.h" // mhdReply
#include "orionld/mhd/mhdConnectionTreat.h" // Own Interface

Expand Down Expand Up @@ -1110,6 +1112,15 @@ MHD_Result mhdConnectionTreat(void)
//
if (orionldState.payloadContextNode != NULL)
{
bool implicitlyCreated = false;

LM_T(LmtContextCacheStats, ("Got an @context in the payload body (type %s)", kjValueType(orionldState.payloadContextNode->type)));
if ((orionldState.serviceP->serviceRoutine == orionldPostSubscriptions) || (orionldState.serviceP->serviceRoutine == orionldPatchSubscription))
{
implicitlyCreated = true;
LM_T(LmtContextCacheStats, ("And the service is Subscription Creation"));
}

OrionldProblemDetails pd = { OrionldBadRequestData, (char*) "naught", (char*) "naught", 0 };

char* id = NULL;
Expand All @@ -1132,6 +1143,9 @@ MHD_Result mhdConnectionTreat(void)

if (pd.status >= 400)
goto respond;

if ((orionldState.contextP != orionldCoreContextP) && (implicitlyCreated == true))
orionldState.contextP->kind = OrionldContextImplicit; // Too late - the context is already in mongo
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ bye
03. GET the subscription and see the Q String Filter P2>10
==========================================================
HTTP/1.1 200 OK
Content-Length: 561
Content-Length: 648
Content-Type: application/json
Date: REGEX(.*)
Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)
Expand Down Expand Up @@ -167,7 +167,8 @@ Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)
},
"status": "ok"
},
"expiresAt": "2028-12-31T10:11:12.123Z"
"expiresAt": "2028-12-31T10:11:12.123Z",
"jsonldContext": "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-contextREGEX(.*)"
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
# Copyright 2024 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

# VALGRIND_READY - to mark the test ready for valgrindTestSuite.sh

--NAME--
Subscription Context

--SHELL-INIT--
dbInit CB
dbInit orionld
orionldStart CB
accumulatorStart --pretty-print

--SHELL--

#
# 01. Create a subscription with Content.Type app/ld+json and a compound @context
# 01b. See the subscription in mongo
# 02. GET the subscription - see the ImplicitlyCreated ldContext
# 03. Query for all hosted contexts, see 4 contexts (core, implicit-for-sub, and the two of the array)
#

echo "01. Create a subscription with Content.Type app/ld+json and a compound @context"
echo "==============================================================================="
payload='{
"id": "urn:ngsi-ld:subscriptions:01",
"type": "Subscription",
"entities": [
{
"type": "T1"
}
],
"notification": {
"endpoint": {
"uri": "http://a.b.c/123"
}
},
"@context": [
"https://fiware.github.io/NGSI-LD_TestSuite/ldContext/testContext.jsonld",
"http://smartdatamodels.org/context.jsonld"
]
}'
orionCurl --url /ngsi-ld/v1/subscriptions --payload "$payload" --in jsonld
echo
echo


echo "01b. See the subscription in mongo"
echo "=================================="
mongoCmd2 ftest "db.csubs.findOne()"
echo
echo


echo "02. GET the subscription - see the ImplicitlyCreated ldContext"
echo "=============================================================="
orionCurl --url /ngsi-ld/v1/subscriptions/urn:ngsi-ld:subscriptions:01
echo
echo


echo "03. Query for all hosted contexts, see 4 contexts (core, implicit-for-sub, and the two of the array)"
echo "===================================================================================================="
orionCurl --url /ngsi-ld/v1/jsonldContexts?details=true
echo
echo


--REGEXPECT--
01. Create a subscription with Content.Type app/ld+json and a compound @context
===============================================================================
HTTP/1.1 201 Created
Content-Length: 0
Date: REGEX(.*)
Location: /ngsi-ld/v1/subscriptions/urn:ngsi-ld:subscriptions:01



01b. See the subscription in mongo
==================================
MongoDB shell version REGEX(.*)
connecting to: REGEX(.*)
MongoDB server version: REGEX(.*)
{
"_id" : "urn:ngsi-ld:subscriptions:01",
"expiration" : 0,
"reference" : "http://a.b.c/123",
"custom" : false,
"mimeType" : "application/json",
"throttling" : 0,
"servicePath" : "/#",
"status" : "active",
"entities" : [
{
"id" : "",
"isPattern" : "",
"type" : "https://uri.etsi.org/ngsi-ld/default-context/T1",
"isTypePattern" : false
}
],
"attrs" : [ ],
"metadata" : [ ],
"blacklist" : false,
"ldContext" : "http:REGEX(.*)",
"createdAt" : REGEX(.*),
"modifiedAt" : REGEX(.*),
"conditions" : [ ],
"expression" : {
"q" : "",
"mq" : "",
"geometry" : "",
"coords" : "",
"georel" : "",
"geoproperty" : ""
},
"format" : "normalized"
}
bye


02. GET the subscription - see the ImplicitlyCreated ldContext
==============================================================
HTTP/1.1 200 OK
Content-Length: REGEX(.*)
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"

{
"entities": [
{
"type": "T1"
}
],
"id": "urn:ngsi-ld:subscriptions:01",
"isActive": true,
"jsonldContext": "REGEX(.*)"
"notification": {
"endpoint": {
"accept": "application/json",
"uri": "http://a.b.c/123"
},
"format": "normalized",
"status": "ok"
},
"status": "active",
"type": "Subscription"
}


03. Query for all hosted contexts, see 4 contexts (core, implicit-for-sub, and the two of the array)
====================================================================================================
HTTP/1.1 200 OK
Content-Length: REGEX(.*)
Content-Type: application/json
Date: REGEX(.*)

[
{
"URL": "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.6.jsonld",
"createdAt": "202REGEX(.*)",
"extraInfo": {
"compactions": 1,
"expansions": 284,
"hash-table": {
"MultiPoint": "https://purl.org/geojson/vocab#MultiPoint",
"instanceId": "https://uri.etsi.org/ngsi-ld/instanceId",
"notUpdated": "https://uri.etsi.org/ngsi-ld/notUpdated",
"notifiedAt": "https://uri.etsi.org/ngsi-ld/notifiedAt",
"observedAt": "https://uri.etsi.org/ngsi-ld/observedAt"
},
"origin": "Downloaded",
"type": "hash-table"
},
"kind": "Cached",
"localId": "REGEX(.*)"
},
{
"URL": "https://fiware.github.io/NGSI-LD_TestSuite/ldContext/testContext.jsonld",
"createdAt": "202REGEX(.*)",
"extraInfo": {
"compactions": 0,
"expansions": 0,
"hash-table": {
"P1": "http://example.org/P1",
"P2": "http://example.org/P2",
"P3": "http://example.org/P3",
"R1": "http://example.org/R1",
"T": "http://example.org/T"
},
"origin": "Downloaded",
"type": "hash-table"
},
"kind": "Cached",
"lastUsage": "202REGEX(.*)",
"localId": "REGEX(.*)",
"numberOfHits": 1
},
{
"URL": "http://smartdatamodels.org/context.jsonld",
"createdAt": "202REGEX(.*)",
"extraInfo": {
"compactions": 0,
"expansions": 2,
"hash-table": {
"carSharing": "https://uri.fiware.org/ns/data-models#carSharing",
"copyMachineOrService": "https://uri.fiware.org/ns/data-models#copyMachineOrService",
"efficCurve": "https://smart-data-models.github.io/data-models/terms.jsonld#/definitions/efficCurve",
"instanceId": "https://uri.etsi.org/ngsi-ld/instanceId",
"roadClosed": "https://uri.fiware.org/ns/data-models#roadClosed"
},
"origin": "Downloaded",
"type": "hash-table"
},
"kind": "Cached",
"lastUsage": "202REGEX(.*)",
"localId": "REGEX(.*)",
"numberOfHits": 1
},
{
"URL": "http://REGEX(.*)",
"createdAt": "202REGEX(.*)",
"extraInfo": {
"URLs": [
"https://fiware.github.io/NGSI-LD_TestSuite/ldContext/testContext.jsonld",
"http://smartdatamodels.org/context.jsonld"
],
"compactions": 0,
"expansions": 0,
"origin": "FromInline",
"type": "array"
},
"kind": "ImplicitlyCreated",
"lastUsage": "202REGEX(.*)",
"localId": "REGEX(.*)",
"numberOfHits": 2
}
]


--TEARDOWN--
brokerStop CB
dbDrop CB

Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Date: REGEX(.*)
},
{
"URL": "REGEX(.*)",
"createdAt": "1970-01-01T00:00:00.000Z",
"createdAt": "202REGEX(.*)",
"extraInfo": {
"compactions": 0,
"expansions": 0,
Expand All @@ -166,7 +166,7 @@ Date: REGEX(.*)
"type": "hash-table"
},
"kind": "Cached",
"lastUsage": "1970-01-01T00:00:00.000Z",
"lastUsage": "202REGEX(.*)",
"localId": "REGEX(.*)",
"numberOfHits": 1
},
Expand Down
Loading

0 comments on commit 7b12bdb

Please sign in to comment.