From 6b1ec268e068d6552f0ae90409e2cc6c337d85cf Mon Sep 17 00:00:00 2001 From: Ken Zangelin Date: Tue, 5 Sep 2023 17:49:43 +0200 Subject: [PATCH] Highly experimental feature for subscriptions: allowing a wildcard as value for entity type, to not filter on entity type --- CHANGES_NEXT_RELEASE | 1 + .../notifications/subCacheAlterationMatch.cpp | 8 +- .../orionld/payloadCheck/pcheckEntityInfo.cpp | 9 +- .../ngsild_subscription-on-type-star.test | 134 ++++++++++++++++++ 4 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 test/functionalTest/cases/0000_ngsild/ngsild_subscription-on-type-star.test diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 6fb469aee3..9058bc6e9d 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -6,3 +6,4 @@ Fixed issues: * #280 - Giving errors for expiresAt in the past (for registrations and subscriptions) * #280 - Fixed a bug in error detection of downloading errors in arrays of contexts, at lower nesting levels * #280 - Better 501 handling of temporal operations + * #280 - Highly experimental feature for subscriptions: allowing a wildcard as value for entity type, to not filter on entity type diff --git a/src/lib/orionld/notifications/subCacheAlterationMatch.cpp b/src/lib/orionld/notifications/subCacheAlterationMatch.cpp index 2677ccab6f..152dcf1617 100644 --- a/src/lib/orionld/notifications/subCacheAlterationMatch.cpp +++ b/src/lib/orionld/notifications/subCacheAlterationMatch.cpp @@ -91,9 +91,13 @@ static bool entityTypeMatch(CachedSubscription* subP, const char* entityType, in { for (int ix = 0; ix < eItems; ++ix) { - EntityInfo* eiP = subP->entityIdInfos[ix]; + EntityInfo* eiP = subP->entityIdInfos[ix]; + const char* eType = eiP->entityType.c_str(); + + if (strcmp(entityType, eType) == 0) + return true; - if (strcmp(entityType, eiP->entityType.c_str()) == 0) + if ((eType[0] == '*') && (eType[1] == 0)) return true; } diff --git a/src/lib/orionld/payloadCheck/pcheckEntityInfo.cpp b/src/lib/orionld/payloadCheck/pcheckEntityInfo.cpp index f2345651e0..4276b7a372 100644 --- a/src/lib/orionld/payloadCheck/pcheckEntityInfo.cpp +++ b/src/lib/orionld/payloadCheck/pcheckEntityInfo.cpp @@ -115,8 +115,13 @@ bool pcheckEntityInfo(KjNode* entityInfoP, bool typeMandatory, bool idMandatory, // Expand, unless already expanded // If a ':' is found inside the first 10 chars, the value is assumed to be expanded ... // - if (orionldContextItemAlreadyExpanded(entityItemP->value.s) == false) - entityItemP->value.s = orionldContextItemExpand(orionldState.contextP, entityItemP->value.s, true, NULL); // entity type + // No expansion if the type is '*' - meaning "ALL ENTITY TYPES" + // + if ((entityItemP->value.s[0] != '*') || (entityItemP->value.s[1] != 0)) + { + if (orionldContextItemAlreadyExpanded(entityItemP->value.s) == false) + entityItemP->value.s = orionldContextItemExpand(orionldState.contextP, entityItemP->value.s, true, NULL); // entity type + } } else { diff --git a/test/functionalTest/cases/0000_ngsild/ngsild_subscription-on-type-star.test b/test/functionalTest/cases/0000_ngsild/ngsild_subscription-on-type-star.test new file mode 100644 index 0000000000..5b33bb7385 --- /dev/null +++ b/test/functionalTest/cases/0000_ngsild/ngsild_subscription-on-type-star.test @@ -0,0 +1,134 @@ +# Copyright 2023 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 with type: "*", and test of notifications + +--SHELL-INIT-- +dbInit CB +orionldStart CB -experimental +accumulatorStart --pretty-print + +--SHELL-- + +# +# 01. Create a subscription on EVERYTHING +# 02. Create an entity, any +# 03. See the notification in the accumulator +# + + +echo "01. Create a subscription on EVERYTHING" +echo "=======================================" +payload='{ + "id": "urn:S1", + "description": "Notify ALL", + "type": "Subscription", + "entities": [ + { + "type": "*" + } + ], + "notification": { + "endpoint": { + "uri": "http://localhost:'$LISTENER_PORT'/notify" + } + } +}' +orionCurl --url /ngsi-ld/v1/subscriptions --payload "$payload" +echo +echo + + +echo "02. Create an entity, any" +echo "=========================" +payload='{ + "id": "urn:ngsi-ld:Blower:bd01", + "type": "BlowerDevice", + "airflow": 127 +}' +orionCurl --url /ngsi-ld/v1/entities --payload "$payload" +echo +echo + + +echo "03. See the notification in the accumulator" +echo "===========================================" +accumulatorDump +echo +echo + + +--REGEXPECT-- +01. Create a subscription on EVERYTHING +======================================= +HTTP/1.1 201 Created +Content-Length: 0 +Date: REGEX(.*) +Location: /ngsi-ld/v1/subscriptions/urn:S1 + + + +02. Create an entity, any +========================= +HTTP/1.1 201 Created +Content-Length: 0 +Date: REGEX(.*) +Location: /ngsi-ld/v1/entities/urn:ngsi-ld:Blower:bd01 + + + +03. See the notification in the accumulator +=========================================== +POST http://REGEX(.*)/notify?subscriptionId=urn:S1 +Content-Length: 264 +User-Agent: REGEX(.*) +Host: REGEX(.*) +Accept: application/json +Content-Type: application/json +Link: