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

Implemented issue #1682 - default user context #1683

Merged
merged 4 commits into from
Oct 3, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/functional.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
outputs: type=docker,dest=/tmp/test-image.tar

- name: Upload artifact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: test-image
path: /tmp/test-image.tar
Expand Down Expand Up @@ -78,7 +78,7 @@ jobs:
uses: docker/setup-qemu-action@v1

- name: Download artifact
uses: actions/download-artifact@v2
uses: actions/download-artifact@v4
with:
name: test-image
path: /tmp
Expand Down
3 changes: 2 additions & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Fixed Issues:
#1621: URL-Encoding single quote in attribute values in TRoE DB
#1649: Crash when using subscriptions in a mixed environment (LD and V2)
#1649: Crash when using subscriptions in a mixed environment (LD and V2)
#1682: Default User Context

## New Features:
* Distributed subscriptions: subordinate subscriptions are DELETED when their "father" is deleted.
Expand Down
3 changes: 3 additions & 0 deletions src/app/orionld/orionld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ bool noprom = false;
bool noArrayReduction = false;
char subordinateEndpoint[256];
int pageSize = 20;
char defaultUserContextUrl[256];



Expand Down Expand Up @@ -344,6 +345,7 @@ int pageSize = 20;
#define NO_ARR_REDUCT_DESC "skip JSON-LD Array Reduction"
#define SUBORDINATE_ENDPOINT_DESC "endpoint URL for reception of notificatiopns from subordinate subscriptions (distributed subscriptions)"
#define PAGE_SIZE_DESC "default page size (no of entities, subscriptions, registrations)"
#define DUC_URL_DESC "URL to default user context"



Expand Down Expand Up @@ -451,6 +453,7 @@ PaArgument paArgs[] =
{ "-noArrayReduction", &noArrayReduction, "NO_ARRAY_REDUCTION", PaBool, PaHid, false, false, true, NO_ARR_REDUCT_DESC },
{ "-subordinateEndpoint", &subordinateEndpoint, "SUBORDINATE_ENDPOINT", PaStr, PaOpt, _i "", PaNL, PaNL, SUBORDINATE_ENDPOINT_DESC },
{ "-pageSize", &pageSize, "PAGE_SIZE", PaInt, PaOpt, 20, 1, 1000, PAGE_SIZE_DESC },
{ "-duc", defaultUserContextUrl, "DUC_URL", PaString, PaOpt, _i "", PaNL, PaNL, DUC_URL_DESC },

PA_END_OF_ARGS
};
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 @@ -105,6 +105,7 @@ typedef enum TraceLevels
LmtContextCacheStats, // Context Cache Statistics
LmtContextDownload, // Context Download
LmtCoreContext, // Core Context
LmtUserContext, // User Context

// GeoJSON
LmtGeoJSON = 110, // 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 @@ -110,7 +110,7 @@ PernotSubCache pernotSubCache;
EntityMap* entityMaps = NULL; // Used by GET /entities in the distributed case, for pagination
bool entityMapsEnabled = false;
bool distSubsEnabled = false;

OrionldContext* defaultUserContextP = NULL;


//
Expand Down
2 changes: 2 additions & 0 deletions src/lib/orionld/common/orionldState.h
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,8 @@ extern bool entityMapsEnabled; // Enable Entity Maps
extern bool distSubsEnabled; // Enable distributed subscriptions
extern bool noArrayReduction; // Used by arrayReduce in pCheckAttribute.cpp
extern int pageSize; // Pagination limit
extern char defaultUserContextUrl[256];
extern OrionldContext* defaultUserContextP;

extern char localIpAndPort[135]; // Local address for X-Forwarded-For (from orionld.cpp)
extern unsigned long long inReqPayloadMaxSize;
Expand Down
10 changes: 10 additions & 0 deletions src/lib/orionld/contextCache/orionldContextCacheInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ void orionldContextCacheInit(void)
LM_W(("Falling back to Built-in Core Context (hard-coded copy of %s)", builtinCoreContextUrl));
}

//
// Default User Context
//
if (defaultUserContextUrl[0] != 0)
{
defaultUserContextP = orionldContextFromUrl(defaultUserContextUrl, NULL);
if (defaultUserContextP == NULL)
LM_W(("Unable to download the default user context"));
}

if (contextArray == NULL)
return;

Expand Down
19 changes: 17 additions & 2 deletions src/lib/orionld/mhd/mhdConnectionTreat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,9 +1239,24 @@ MHD_Result mhdConnectionTreat(void)
}
}

if (orionldState.contextP == NULL)
orionldState.contextP = orionldCoreContextP;
LM_T(LmtUserContext, ("orionldState.contextP at %p", orionldState.contextP));
LM_T(LmtUserContext, ("Core Context at %p", orionldCoreContextP));
if (orionldState.contextP == orionldCoreContextP)
{
if (defaultUserContextP != NULL)
{
LM_T(LmtUserContext, ("Using the default user context (%s)", defaultUserContextUrl));
orionldState.contextP = defaultUserContextP;
}
else
{
LM_T(LmtUserContext, ("No default user context, using only the core context"));
orionldState.contextP = orionldCoreContextP;
}
}

LM_T(LmtUserContext, ("orionldState.contextP: '%s'", orionldState.contextP->url));
LM_T(LmtUserContext, ("-------------------------------------------"));
if (orionldState.link == NULL)
orionldState.link = orionldState.contextP->url;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@ Usage: orionld [option '-U' (extended usage)]
[option '-brokerId' <identity of this broker instance for registrations - for the Via header>]
[option '-subordinateEndpoint' <endpoint URL for reception of notificatiopns from subordinate subscriptions (distributed subscriptions)>]
[option '-pageSize' <default page size (no of entities, subscriptions, registrations)>]
[option '-duc' <URL to default user context>]

--TEARDOWN--
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@ Usage: orionld [option '-U' (extended usage)]
[option '-brokerId' <identity of this broker instance for registrations - for the Via header>]
[option '-subordinateEndpoint' <endpoint URL for reception of notificatiopns from subordinate subscriptions (distributed subscriptions)>]
[option '-pageSize' <default page size (no of entities, subscriptions, registrations)>]
[option '-duc' <URL to default user context>]

--TEARDOWN--
176 changes: 176 additions & 0 deletions test/functionalTest/cases/0000_ngsild/ngsild_default_user_context.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# 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--
Default user context

--SHELL-INIT--
dbInit orionld # Remove the entire database where the @context cache lives
dbInit CB

curl http://localhost:7080/jsonldContexts/urn:C1 -X DELETE > /dev/null 2>&1
curl http://localhost:7080/jsonldContexts/urn:C2 -X DELETE > /dev/null 2>&1

payload='{
"@context": {
"T": "urn:duc:T",
"P1": "urn:duc:P1"
}
}'
cServerCurl --url /jsonldContexts/urn:C1 --payload "$payload" --verb POST > /dev/null 2>&1

payload='{
"@context": {
"T": "urn:c2:T",
"P1": "urn:c2:P1"
}
}'
cServerCurl --url /jsonldContexts/urn:C2 --payload "$payload" --verb POST > /dev/null 2>&1

orionldStart CB -experimental -duc http://localhost:7080/jsonldContexts/urn:C1

--SHELL--
#
# 01. Create an entity urn:E1 w/o user context and with a type and an attribute P1 from context urn:C1 (default user context)
# 02. Create an entity urn:E2 with user context urn:C2 and with the same type and attribute
# 03. GET all entities, w/o user context (=> default user context urn:C1), see urn:E1 with shortnames and urn:E2 with longnames
# 04. GET all entities, with user context urn:C2, see urn:E2 with shortnames and urn:E1 with longnames
#

echo "01. Create an entity urn:E1 w/o user context and with a type and an attribute P1 from context urn:C1 (default user context)"
echo "==========================================================================================================================="
payload='{
"id": "urn:E1",
"type": "T",
"P1": "P1 of E1"
}'
orionCurl --url /ngsi-ld/v1/entities --payload "$payload"
echo
echo


echo "02. Create an entity urn:E2 with user context urn:C2 and with the same type and attribute"
echo "========================================================================================="
payload='{
"id": "urn:E2",
"type": "T",
"P1": "P1 of E2"
}'
orionCurl --url /ngsi-ld/v1/entities --payload "$payload" -H "Link: <http://localhost:7080/jsonldContexts/urn:C2>"
echo
echo


echo "03. GET all entities, w/o user context (=> default user context urn:C1), see urn:E1 with shortnames and urn:E2 with longnames"
echo "============================================================================================================================="
orionCurl --url /ngsi-ld/v1/entities?local=true
echo
echo


echo "04. GET all entities, with user context urn:C2, see urn:E2 with shortnames and urn:E1 with longnames"
echo "===================================================================================================="
orionCurl --url /ngsi-ld/v1/entities?local=true -H "Link: <http://localhost:7080/jsonldContexts/urn:C2>"
echo
echo


--REGEXPECT--
01. Create an entity urn:E1 w/o user context and with a type and an attribute P1 from context urn:C1 (default user context)
===========================================================================================================================
HTTP/1.1 201 Created
Content-Length: 0
Date: REGEX(.*)
Location: /ngsi-ld/v1/entities/urn:E1



02. Create an entity urn:E2 with user context urn:C2 and with the same type and attribute
=========================================================================================
HTTP/1.1 201 Created
Content-Length: 0
Date: REGEX(.*)
Location: /ngsi-ld/v1/entities/urn:E2



03. GET all entities, w/o user context (=> default user context urn:C1), see urn:E1 with shortnames and urn:E2 with longnames
=============================================================================================================================
HTTP/1.1 200 OK
Content-Length: 157
Content-Type: application/json
Date: REGEX(.*)
Link: <http://localhost:7080/jsonldContexts/urn:C1>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"

[
{
"P1": {
"type": "Property",
"value": "P1 of E1"
},
"id": "urn:E1",
"type": "T"
},
{
"id": "urn:E2",
"type": "urn:c2:T",
"urn:c2:P1": {
"type": "Property",
"value": "P1 of E2"
}
}
]


04. GET all entities, with user context urn:C2, see urn:E2 with shortnames and urn:E1 with longnames
====================================================================================================
HTTP/1.1 200 OK
Content-Length: 159
Content-Type: application/json
Date: REGEX(.*)
Link: <http://localhost:7080/jsonldContexts/urn:C2>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"

[
{
"id": "urn:E1",
"type": "urn:duc:T",
"urn:duc:P1": {
"type": "Property",
"value": "P1 of E1"
}
},
{
"P1": {
"type": "Property",
"value": "P1 of E2"
},
"id": "urn:E2",
"type": "T"
}
]


--TEARDOWN--
brokerStop CB
dbDrop CB
curl http://localhost:7080/jsonldContexts/urn:C1 -X DELETE > /dev/null 2>&1
curl http://localhost:7080/jsonldContexts/urn:C2 -X DELETE > /dev/null 2>&1
1 change: 1 addition & 0 deletions test/unittests/main_UnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ bool triggerOperation = false;
bool noArrayReduction = false;
char subordinateEndpoint[256];
int pageSize = 20;
char defaultUserContextUrl[256];



Expand Down
Loading