From acc1f9a105f935bf292908faba5292577cd066a1 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 5 Sep 2023 09:44:57 +0200 Subject: [PATCH 01/20] use apikey to find device --- lib/services/devices/deviceRegistryMongoDB.js | 10 ++++++---- lib/services/devices/deviceService.js | 10 +++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/services/devices/deviceRegistryMongoDB.js b/lib/services/devices/deviceRegistryMongoDB.js index 8e6164904..9867e9dd6 100644 --- a/lib/services/devices/deviceRegistryMongoDB.js +++ b/lib/services/devices/deviceRegistryMongoDB.js @@ -211,12 +211,14 @@ function findOneInMongoDB(queryParams, id, callback) { * Internal function used to find a device in the DB. * * @param {String} id ID of the Device to find. + * @param {String} Apikey Apikey of the Device to find. * @param {String} service Service the device belongs to (optional). * @param {String} subservice Division inside the service (optional). */ -function getDeviceById(id, service, subservice, callback) { +function getDeviceById(id, apikey, service, subservice, callback) { const queryParams = { id, + apikey, service, subservice }; @@ -232,8 +234,8 @@ function getDeviceById(id, service, subservice, callback) { * @param {String} service Service the device belongs to. * @param {String} subservice Division inside the service. */ -function getDevice(id, service, subservice, callback) { - getDeviceById(id, service, subservice, function (error, data) { +function getDevice(id, apikey, service, subservice, callback) { + getDeviceById(id, apikey, service, subservice, function (error, data) { if (error) { callback(error); } else { @@ -284,7 +286,7 @@ function getByName(name, service, servicepath, callback) { */ function update(device, callback) { logger.debug(context, 'Storing updated values for device [%s]:\n%s', device.id, JSON.stringify(device, null, 4)); - getDeviceById(device.id, device.service, device.subservice, function (error, data) { + getDeviceById(device.id, device.apikey, device.service, device.subservice, function (error, data) { if (error) { callback(error); } else { diff --git a/lib/services/devices/deviceService.js b/lib/services/devices/deviceService.js index b68bd0273..950a43a09 100644 --- a/lib/services/devices/deviceService.js +++ b/lib/services/devices/deviceService.js @@ -540,8 +540,8 @@ function getDevice(deviceId, service, subservice, callback) { * @param {String} service Service for which the requested device. * @param {String} subservice Subservice inside the service for which the device is requested. */ -function getDeviceSilently(deviceId, service, subservice, callback) { - config.getRegistry().getSilently(deviceId, service, subservice, callback); +function getDeviceSilently(deviceId, apikey, service, subservice, callback) { + config.getRegistry().getSilently(deviceId, apikey, service, subservice, callback); } /** @@ -608,8 +608,8 @@ function checkRegistry(fn) { }; } -function findOrCreate(deviceId, group, callback) { - getDeviceSilently(deviceId, group.service, group.subservice, function (error, device) { +function findOrCreate(deviceId, apikey, group, callback) { + getDeviceSilently(deviceId, apikey, group.service, group.subservice, function (error, device) { if (!error && device) { callback(null, device, group); } else if (error.name === 'DEVICE_NOT_FOUND') { @@ -678,7 +678,7 @@ function retrieveDevice(deviceId, apiKey, callback) { async.waterfall( [ apply(groupService.get, config.getConfig().defaultResource || '', apiKey), - apply(findOrCreate, deviceId), + apply(findOrCreate, deviceId, apikey), apply( mergeDeviceWithConfiguration, ['lazy', 'active', 'staticAttributes', 'commands', 'subscriptions'], From 70d1d3ddc5cc85f37a49c774d35833f99830fb53 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 5 Sep 2023 10:01:13 +0200 Subject: [PATCH 02/20] fix typo apikey -> apiKey --- lib/services/devices/deviceService.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/devices/deviceService.js b/lib/services/devices/deviceService.js index 950a43a09..a3c868981 100644 --- a/lib/services/devices/deviceService.js +++ b/lib/services/devices/deviceService.js @@ -678,7 +678,7 @@ function retrieveDevice(deviceId, apiKey, callback) { async.waterfall( [ apply(groupService.get, config.getConfig().defaultResource || '', apiKey), - apply(findOrCreate, deviceId, apikey), + apply(findOrCreate, deviceId, apiKey), apply( mergeDeviceWithConfiguration, ['lazy', 'active', 'staticAttributes', 'commands', 'subscriptions'], From 7ec6e79bf9d650500e6d566d0526e01226659c53 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 5 Sep 2023 11:03:54 +0200 Subject: [PATCH 03/20] provide device apikey to getDevice --- lib/services/devices/deviceRegistryMemory.js | 2 +- lib/services/devices/deviceRegistryMongoDB.js | 6 ++++-- lib/services/devices/deviceService.js | 5 +++-- lib/services/devices/devices-NGSI-LD.js | 16 ++++++++++++++-- lib/services/devices/devices-NGSI-v2.js | 16 ++++++++++++++-- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/services/devices/deviceRegistryMemory.js b/lib/services/devices/deviceRegistryMemory.js index 8227620fe..f3038c45d 100644 --- a/lib/services/devices/deviceRegistryMemory.js +++ b/lib/services/devices/deviceRegistryMemory.js @@ -141,7 +141,7 @@ function listDevices(type, service, subservice, limit, offset, callback) { }); } -function getDevice(id, service, subservice, callback) { +function getDevice(id, apikey, service, subservice, callback) { if (registeredDevices[service] && registeredDevices[service][id]) { callback(null, registeredDevices[service][id]); } else { diff --git a/lib/services/devices/deviceRegistryMongoDB.js b/lib/services/devices/deviceRegistryMongoDB.js index 9867e9dd6..95bbc8ada 100644 --- a/lib/services/devices/deviceRegistryMongoDB.js +++ b/lib/services/devices/deviceRegistryMongoDB.js @@ -216,12 +216,14 @@ function findOneInMongoDB(queryParams, id, callback) { * @param {String} subservice Division inside the service (optional). */ function getDeviceById(id, apikey, service, subservice, callback) { - const queryParams = { + let queryParams = { id, - apikey, service, subservice }; + if (apikey) { + queryParams.apikey = apikey; + } context = fillService(context, queryParams); logger.debug(context, 'Looking for device with id [%s].', id); findOneInMongoDB(queryParams, id, callback); diff --git a/lib/services/devices/deviceService.js b/lib/services/devices/deviceService.js index a3c868981..bd5ade227 100644 --- a/lib/services/devices/deviceService.js +++ b/lib/services/devices/deviceService.js @@ -253,6 +253,7 @@ function registerDevice(deviceObj, callback) { function checkDuplicates(deviceObj, innerCb) { config.getRegistry().getSilently( deviceObj.id, + deviceObj.apikey, deviceObj.service, deviceObj.subservice, /* eslint-disable-next-line no-unused-vars */ @@ -430,7 +431,7 @@ function unregisterDevice(id, service, subservice, callback) { logger.debug(context, 'Removing device register in Device Service'); - config.getRegistry().get(id, service, subservice, function (error, device) { + config.getRegistry().get(id, null, service, subservice, function (error, device) { if (error) { callback(error); } else { @@ -530,7 +531,7 @@ function listDevices(service, subservice, limit, offset, callback) { * @param {String} subservice Subservice inside the service for which the device is requested. */ function getDevice(deviceId, service, subservice, callback) { - config.getRegistry().get(deviceId, service, subservice, callback); + config.getRegistry().get(deviceId, null, service, subservice, callback); } /** diff --git a/lib/services/devices/devices-NGSI-LD.js b/lib/services/devices/devices-NGSI-LD.js index dd962f188..36f131601 100644 --- a/lib/services/devices/devices-NGSI-LD.js +++ b/lib/services/devices/devices-NGSI-LD.js @@ -369,7 +369,13 @@ function updateRegisterDeviceNgsiLD(deviceObj, entityInfoUpdated, callback) { if (entityInfoUpdated) { async.waterfall( [ - apply(config.getRegistry().get, deviceObj.id, deviceObj.service, deviceObj.subservice), + apply( + config.getRegistry().get, + deviceObj.id, + deviceObj.apikey, + deviceObj.service, + deviceObj.subservice + ), apply(extractDeviceDifference, deviceObj), createInitialEntityNgsiLD, apply(combineWithNewDevice, deviceObj), @@ -382,7 +388,13 @@ function updateRegisterDeviceNgsiLD(deviceObj, entityInfoUpdated, callback) { } else { async.waterfall( [ - apply(config.getRegistry().get, deviceObj.id, deviceObj.service, deviceObj.subservice), + apply( + config.getRegistry().get, + deviceObj.id, + deviceObj.apikey, + deviceObj.service, + deviceObj.subservice + ), apply(extractDeviceDifference, deviceObj), updateEntityNgsiLD, apply(combineWithNewDevice, deviceObj), diff --git a/lib/services/devices/devices-NGSI-v2.js b/lib/services/devices/devices-NGSI-v2.js index 1ebc7f4cd..90e47ed35 100644 --- a/lib/services/devices/devices-NGSI-v2.js +++ b/lib/services/devices/devices-NGSI-v2.js @@ -424,7 +424,13 @@ function updateRegisterDeviceNgsi2(deviceObj, entityInfoUpdated, callback) { if (entityInfoUpdated) { async.waterfall( [ - apply(config.getRegistry().get, deviceObj.id, deviceObj.service, deviceObj.subservice), + apply( + config.getRegistry().get, + deviceObj.id, + deviceObj.apikey, + deviceObj.service, + deviceObj.subservice + ), apply(extractDeviceDifference, deviceObj), createInitialEntityNgsi2, apply(combineWithNewDevice, deviceObj), @@ -437,7 +443,13 @@ function updateRegisterDeviceNgsi2(deviceObj, entityInfoUpdated, callback) { } else { async.waterfall( [ - apply(config.getRegistry().get, deviceObj.id, deviceObj.service, deviceObj.subservice), + apply( + config.getRegistry().get, + deviceObj.id, + deviceObj.apikey, + deviceObj.service, + deviceObj.subservice + ), apply(extractDeviceDifference, deviceObj), updateEntityNgsi2, apply(combineWithNewDevice, deviceObj), From 190e6bf0e6c38875364119ea20caf62d04549515 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 5 Sep 2023 13:23:49 +0200 Subject: [PATCH 04/20] update CNR --- CHANGES_NEXT_RELEASE | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 01345a6bd..6088928c8 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,2 +1,3 @@ -- Upgrade mongodb dev dep from 4.7.0 to 4.17.0 -- Upgrade mongoose dep from 5.13.14 to 5.13.20 +- Use apikey from measure/group to find device (#1426) +- Upgrade mongodb dev dep from 4.7.0 to 4.17.0 +- Upgrade mongoose dep from 5.13.14 to 5.13.20 From 32fa98148d5f47848a05b04feb0850e7cb068449 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 5 Sep 2023 15:16:17 +0200 Subject: [PATCH 05/20] extend log --- lib/services/devices/deviceRegistryMongoDB.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/devices/deviceRegistryMongoDB.js b/lib/services/devices/deviceRegistryMongoDB.js index 95bbc8ada..1105ddd0c 100644 --- a/lib/services/devices/deviceRegistryMongoDB.js +++ b/lib/services/devices/deviceRegistryMongoDB.js @@ -225,7 +225,7 @@ function getDeviceById(id, apikey, service, subservice, callback) { queryParams.apikey = apikey; } context = fillService(context, queryParams); - logger.debug(context, 'Looking for device with id [%s].', id); + logger.debug(context, 'Looking for device with id [%s] and queryParams [%j].', id, queryParams); findOneInMongoDB(queryParams, id, callback); } From 3abe8e868805e1749fe07525d47162790422ecd6 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 5 Sep 2023 15:56:54 +0200 Subject: [PATCH 06/20] try find without apikey --- lib/services/devices/deviceRegistryMongoDB.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/services/devices/deviceRegistryMongoDB.js b/lib/services/devices/deviceRegistryMongoDB.js index 1105ddd0c..d26f63ea3 100644 --- a/lib/services/devices/deviceRegistryMongoDB.js +++ b/lib/services/devices/deviceRegistryMongoDB.js @@ -239,7 +239,14 @@ function getDeviceById(id, apikey, service, subservice, callback) { function getDevice(id, apikey, service, subservice, callback) { getDeviceById(id, apikey, service, subservice, function (error, data) { if (error) { - callback(error); + // Try without apikey: apikey will be added + getDeviceById(id, null, service, subservice, function (error, data) { + if (error) { + callback(error); + } else { + callback(null, data); + } + }); } else { callback(null, data); } From 13dc7327d79b1f8def7d0928904e0dbb76027273 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 5 Sep 2023 17:18:21 +0200 Subject: [PATCH 07/20] export updateDevice --- lib/fiware-iotagent-lib.js | 1 + lib/services/devices/deviceRegistryMongoDB.js | 2 +- lib/services/devices/deviceService.js | 12 +++++++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/fiware-iotagent-lib.js b/lib/fiware-iotagent-lib.js index e4862feaf..28603583f 100644 --- a/lib/fiware-iotagent-lib.js +++ b/lib/fiware-iotagent-lib.js @@ -302,6 +302,7 @@ exports.update = ngsi.update; exports.setCommandResult = ngsi.setCommandResult; exports.listDevices = deviceService.listDevices; exports.getDevice = deviceService.getDevice; +exports.updateDevice = deviceService.updateDevice; exports.getDeviceSilently = deviceService.getDeviceSilently; exports.getDeviceByName = deviceService.getDeviceByName; exports.getDeviceByNameAndType = deviceService.getDeviceByNameAndType; diff --git a/lib/services/devices/deviceRegistryMongoDB.js b/lib/services/devices/deviceRegistryMongoDB.js index d26f63ea3..5831b04c1 100644 --- a/lib/services/devices/deviceRegistryMongoDB.js +++ b/lib/services/devices/deviceRegistryMongoDB.js @@ -239,7 +239,7 @@ function getDeviceById(id, apikey, service, subservice, callback) { function getDevice(id, apikey, service, subservice, callback) { getDeviceById(id, apikey, service, subservice, function (error, data) { if (error) { - // Try without apikey: apikey will be added + // Try without apikey: apikey will be added to device later getDeviceById(id, null, service, subservice, function (error, data) { if (error) { callback(error); diff --git a/lib/services/devices/deviceService.js b/lib/services/devices/deviceService.js index 8a9027480..20da7def6 100644 --- a/lib/services/devices/deviceService.js +++ b/lib/services/devices/deviceService.js @@ -534,6 +534,10 @@ function getDevice(deviceId, service, subservice, callback) { config.getRegistry().get(deviceId, null, service, subservice, callback); } +function updateDevice(device, callback) { + config.getRegistry().update(device, callback); +} + /** * Retrieve a device from the device registry, allowing not found it (will be created later) * @@ -619,11 +623,12 @@ function findOrCreate(deviceId, apikey, group, callback) { ) { logger.info(context, 'Update provisioned device %j with measure/group apikey %j', device, group.apikey); device.apikey = group.apikey; // group apikey is the same of current measure apikey - updateRegisterDevice(device, function (error, device) { - callback(error, device, group); + updateDevice(device, function (error) { + callback(null, device, group); }); + } else { + callback(null, device, group); } - callback(null, device, group); } else if (error.name === 'DEVICE_NOT_FOUND') { const newDevice = { id: deviceId, @@ -705,6 +710,7 @@ function retrieveDevice(deviceId, apiKey, callback) { exports.listDevices = intoTrans(context, checkRegistry)(listDevices); exports.listDevicesWithType = intoTrans(context, checkRegistry)(listDevicesWithType); exports.getDevice = intoTrans(context, checkRegistry)(getDevice); +exports.updateDevice = intoTrans(context, checkRegistry)(updateDevice); exports.getDeviceSilently = intoTrans(context, checkRegistry)(getDeviceSilently); exports.getDevicesByAttribute = intoTrans(context, checkRegistry)(getDevicesByAttribute); exports.getDeviceByName = intoTrans(context, checkRegistry)(getDeviceByName); From 5d0ca70d652f05ef9949ecec46f207673bdfdf46 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Wed, 6 Sep 2023 09:48:40 +0200 Subject: [PATCH 08/20] fix linter --- lib/services/devices/deviceService.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/devices/deviceService.js b/lib/services/devices/deviceService.js index 20da7def6..2da247132 100644 --- a/lib/services/devices/deviceService.js +++ b/lib/services/devices/deviceService.js @@ -624,7 +624,7 @@ function findOrCreate(deviceId, apikey, group, callback) { logger.info(context, 'Update provisioned device %j with measure/group apikey %j', device, group.apikey); device.apikey = group.apikey; // group apikey is the same of current measure apikey updateDevice(device, function (error) { - callback(null, device, group); + callback(error, device, group); }); } else { callback(null, device, group); From 8ef817b496adbd0d1f5bad88a5de4f3770c5d748 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Wed, 6 Sep 2023 11:04:10 +0200 Subject: [PATCH 09/20] use getDevice when update --- lib/services/devices/deviceRegistryMongoDB.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/devices/deviceRegistryMongoDB.js b/lib/services/devices/deviceRegistryMongoDB.js index 5831b04c1..835a50ae6 100644 --- a/lib/services/devices/deviceRegistryMongoDB.js +++ b/lib/services/devices/deviceRegistryMongoDB.js @@ -295,7 +295,7 @@ function getByName(name, service, servicepath, callback) { */ function update(device, callback) { logger.debug(context, 'Storing updated values for device [%s]:\n%s', device.id, JSON.stringify(device, null, 4)); - getDeviceById(device.id, device.apikey, device.service, device.subservice, function (error, data) { + getDevice(device.id, device.apikey, device.service, device.subservice, function (error, data) { if (error) { callback(error); } else { From 47a79d4c1f172fa24dfa6397ff8219abe41ebc7b Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Wed, 6 Sep 2023 12:46:19 +0200 Subject: [PATCH 10/20] update CNR --- CHANGES_NEXT_RELEASE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 4e2eb353a..03a571ee3 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,4 +1,4 @@ -- Use apikey from measure/group to find device (#1426) +- Try to use apikey from measure/group to find device in first attempt (#1426) - Fix: ensure device apikey in already provisioned device (#1430) - Upgrade mongodb dev dep from 4.7.0 to 4.17.0 - Upgrade mongoose dep from 5.13.14 to 5.13.20 From f9aa1923316fa83fb52ee53b0d8ed1fd35b7bd6a Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Wed, 6 Sep 2023 13:14:53 +0200 Subject: [PATCH 11/20] Update CHANGES_NEXT_RELEASE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fermín Galán Márquez --- CHANGES_NEXT_RELEASE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 03a571ee3..d977f4456 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,4 +1,4 @@ -- Try to use apikey from measure/group to find device in first attempt (#1426) +- Fix: try to use apikey from measure/group to find device in first attempt (#1426) - Fix: ensure device apikey in already provisioned device (#1430) - Upgrade mongodb dev dep from 4.7.0 to 4.17.0 - Upgrade mongoose dep from 5.13.14 to 5.13.20 From e41511f40a12d66de47cf8ea952deca5541917d7 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Wed, 6 Sep 2023 16:10:00 +0200 Subject: [PATCH 12/20] update doc --- lib/services/devices/deviceService.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/services/devices/deviceService.js b/lib/services/devices/deviceService.js index 2da247132..8e905b323 100644 --- a/lib/services/devices/deviceService.js +++ b/lib/services/devices/deviceService.js @@ -534,6 +534,11 @@ function getDevice(deviceId, service, subservice, callback) { config.getRegistry().get(deviceId, null, service, subservice, callback); } +/** + * Update a device from the device registry. + * + * @param {String} device JSON object contain the device to update. + */ function updateDevice(device, callback) { config.getRegistry().update(device, callback); } From 009358ede068ea6c0dd4f8eefd1d8458ef0a7965 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Thu, 7 Sep 2023 13:10:49 +0200 Subject: [PATCH 13/20] use apkey to delete device --- lib/services/commands/commandService.js | 2 +- lib/services/devices/deviceRegistryMemory.js | 2 +- lib/services/devices/deviceRegistryMongoDB.js | 6 ++++-- lib/services/devices/deviceService.js | 12 +++++------ lib/services/groups/groupService.js | 2 +- .../northBound/deviceProvisioningServer.js | 19 +++++++++++++----- ...ontextBrokerKeystoneSecurityAccess-test.js | 4 ++-- test/unit/mongodb/mongodb-registry-test.js | 2 +- .../contextBrokerOAuthSecurityAccess-test.js | 4 ++-- .../ngsi-ld/general/https-support-test.js | 2 +- .../ngsi-ld/ngsiService/subscriptions-test.js | 20 +++++++++---------- .../provisioning/device-registration_test.js | 8 ++++---- .../device-update-registration_test.js | 3 ++- .../contextBrokerOAuthSecurityAccess-test.js | 4 ++-- .../unit/ngsiv2/general/https-support-test.js | 2 +- .../ngsiv2/ngsiService/subscriptions-test.js | 20 +++++++++---------- .../provisioning/device-registration_test.js | 14 +++++++------ .../device-update-registration_test.js | 3 ++- 18 files changed, 72 insertions(+), 57 deletions(-) diff --git a/lib/services/commands/commandService.js b/lib/services/commands/commandService.js index b6452579e..757e4d2ba 100644 --- a/lib/services/commands/commandService.js +++ b/lib/services/commands/commandService.js @@ -120,7 +120,7 @@ function markAsExpired(command) { async.waterfall( [ - apply(deviceService.getDevice, command.deviceId, command.service, command.subservice), + apply(deviceService.getDevice, command.deviceId, null, command.service, command.subservice), getGroup, calculateTypeInformation, updateExpiredCommand diff --git a/lib/services/devices/deviceRegistryMemory.js b/lib/services/devices/deviceRegistryMemory.js index f3038c45d..7f7408dd0 100644 --- a/lib/services/devices/deviceRegistryMemory.js +++ b/lib/services/devices/deviceRegistryMemory.js @@ -71,7 +71,7 @@ function storeDevice(newDevice, callback) { * @param {String} service Service of the device to remove. * @param {String} subservice Subservice inside the service for the removed device. */ -function removeDevice(id, service, subservice, callback) { +function removeDevice(id, apikey, service, subservice, callback) { const services = Object.keys(registeredDevices); for (let i = 0; i < services.length; i++) { diff --git a/lib/services/devices/deviceRegistryMongoDB.js b/lib/services/devices/deviceRegistryMongoDB.js index 835a50ae6..be92335bb 100644 --- a/lib/services/devices/deviceRegistryMongoDB.js +++ b/lib/services/devices/deviceRegistryMongoDB.js @@ -119,13 +119,15 @@ function storeDevice(newDevice, callback) { * @param {String} service Service of the device to remove. * @param {String} subservice Subservice inside the service for the removed device. */ -function removeDevice(id, service, subservice, callback) { +function removeDevice(id, apikey, service, subservice, callback) { const condition = { id, service, subservice }; - + if (apikey) { + condition.apikey = apikey; + } logger.debug(context, 'Removing device with id [%s]', id); Device.model.deleteOne(condition, function (error) { diff --git a/lib/services/devices/deviceService.js b/lib/services/devices/deviceService.js index 8e905b323..f6768944e 100644 --- a/lib/services/devices/deviceService.js +++ b/lib/services/devices/deviceService.js @@ -420,7 +420,7 @@ function removeAllSubscriptions(device, callback) { * @param {String} service Service of the device to unregister. * @param {String} subservice Subservice inside the service for the unregisterd device. */ -function unregisterDevice(id, service, subservice, callback) { +function unregisterDevice(id, apikey, service, subservice, callback) { function processContextUnregister(body, innerCallback) { innerCallback(null); } @@ -429,9 +429,9 @@ function unregisterDevice(id, service, subservice, callback) { innerCallback(null); } - logger.debug(context, 'Removing device register in Device Service'); + logger.debug(context, 'Removing device %j %j %j %j register in Device Service', id, apikey, service, subservice); - config.getRegistry().get(id, null, service, subservice, function (error, device) { + config.getRegistry().get(id, apikey, service, subservice, function (error, device) { if (error) { callback(error); } else { @@ -455,7 +455,7 @@ function unregisterDevice(id, service, subservice, callback) { processUnsubscribes, apply(registrationUtils.sendRegistrations, true, mergedDevice), processContextUnregister, - apply(config.getRegistry().remove, id, service, subservice) + apply(config.getRegistry().remove, id, apikey, service, subservice) ], callback ); @@ -530,8 +530,8 @@ function listDevices(service, subservice, limit, offset, callback) { * @param {String} service Service for which the requested device. * @param {String} subservice Subservice inside the service for which the device is requested. */ -function getDevice(deviceId, service, subservice, callback) { - config.getRegistry().get(deviceId, null, service, subservice, callback); +function getDevice(deviceId, apikey, service, subservice, callback) { + config.getRegistry().get(deviceId, apikey, service, subservice, callback); } /** diff --git a/lib/services/groups/groupService.js b/lib/services/groups/groupService.js index 89a1db5f4..adea091cc 100644 --- a/lib/services/groups/groupService.js +++ b/lib/services/groups/groupService.js @@ -172,7 +172,7 @@ function remove(service, subservice, resource, apikey, device, callback) { } function unregisterDevice(device, cb) { - deviceService.unregister(device.id, service, subservice, function (error) { + deviceService.unregister(device.id, device.apikey, service, subservice, function (error) { if (error) { cb(error); } diff --git a/lib/services/northBound/deviceProvisioningServer.js b/lib/services/northBound/deviceProvisioningServer.js index 4003d6dd8..fa55f0932 100644 --- a/lib/services/northBound/deviceProvisioningServer.js +++ b/lib/services/northBound/deviceProvisioningServer.js @@ -251,6 +251,7 @@ function handleListDevices(req, res, next) { function handleGetDevice(req, res, next) { deviceService.getDevice( req.params.deviceId, + req.params.apikey, req.headers['fiware-service'], req.headers['fiware-servicepath'], function (error, device) { @@ -269,8 +270,8 @@ function handleGetDevice(req, res, next) { * This middleware handles the removal of a particular device specified with the deviceId. */ function handleRemoveDevice(req, res, next) { - function getDevice(deviceId, service, subservice, callback) { - deviceService.getDevice(deviceId, service, subservice, function (error, device) { + function getDevice(deviceId, apikey, service, subservice, callback) { + deviceService.getDevice(deviceId, apikey, service, subservice, function (error, device) { if (error) { callback(error); } else if (device) { @@ -289,18 +290,25 @@ function handleRemoveDevice(req, res, next) { } } - function unregisterDevice(deviceId, service, subservice, device, callback) { - return deviceService.unregister(deviceId, service, subservice, callback); + function unregisterDevice(deviceId, apikey, service, subservice, device, callback) { + return deviceService.unregister(deviceId, apikey, service, subservice, callback); } async.waterfall( [ apply(statsRegistry.add, 'deviceRemovalRequests', 1), - apply(getDevice, req.params.deviceId, req.headers['fiware-service'], req.headers['fiware-servicepath']), + apply( + getDevice, + req.params.deviceId, + req.params.apikey, + req.headers['fiware-service'], + req.headers['fiware-servicepath'] + ), applyRemoveDeviceHandler, apply( unregisterDevice, req.params.deviceId, + req.params.apikey, req.headers['fiware-service'], req.headers['fiware-servicepath'] ) @@ -334,6 +342,7 @@ function handleUpdateDevice(req, res, next) { } else { deviceService.getDevice( req.params.deviceId, + req.params.apikey, req.headers['fiware-service'], req.headers['fiware-servicepath'], function (error, device) { diff --git a/test/unit/general/contextBrokerKeystoneSecurityAccess-test.js b/test/unit/general/contextBrokerKeystoneSecurityAccess-test.js index af0641c19..165333f74 100644 --- a/test/unit/general/contextBrokerKeystoneSecurityAccess-test.js +++ b/test/unit/general/contextBrokerKeystoneSecurityAccess-test.js @@ -293,7 +293,7 @@ describe('NGSI-v2 - Secured access to the Context Broker with Keystone', functio }); it('subscribe requests use auth header', function (done) { - iotAgentLib.getDevice('Light1', 'smartgondor', 'electricity', function (error, device) { + iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) { iotAgentLib.subscribe(device, ['dimming'], null, function (error) { should.not.exist(error); @@ -314,7 +314,7 @@ describe('NGSI-v2 - Secured access to the Context Broker with Keystone', functio 'X-Subject-Token': '12345679ABCDEF' }); - iotAgentLib.getDevice('Light1', 'smartgondor', 'electricity', function (error, device) { + iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) { iotAgentLib.subscribe(device, ['dimming'], null, function (error) { iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) { contextBrokerMock.done(); diff --git a/test/unit/mongodb/mongodb-registry-test.js b/test/unit/mongodb/mongodb-registry-test.js index cc2f2a992..01b64f6a1 100644 --- a/test/unit/mongodb/mongodb-registry-test.js +++ b/test/unit/mongodb/mongodb-registry-test.js @@ -387,7 +387,7 @@ describe('NGSI-v2 - MongoDB Device Registry', function () { }); it('should be removed from MongoDB', function (done) { - iotAgentLib.unregister(device1.id, 'smartgondor', 'gardens', function (error) { + iotAgentLib.unregister(device1.id, null, 'smartgondor', 'gardens', function (error) { iotAgentDb .db() .collection('devices') diff --git a/test/unit/ngsi-ld/general/contextBrokerOAuthSecurityAccess-test.js b/test/unit/ngsi-ld/general/contextBrokerOAuthSecurityAccess-test.js index 8e248ced3..43be63c8f 100644 --- a/test/unit/ngsi-ld/general/contextBrokerOAuthSecurityAccess-test.js +++ b/test/unit/ngsi-ld/general/contextBrokerOAuthSecurityAccess-test.js @@ -318,7 +318,7 @@ describe('NGSI-LD - Secured access to the Context Broker with OAuth2 provider', }); it('subscribe requests use auth header', function (done) { - iotAgentLib.getDevice('Light1', 'smartgondor', 'electricity', function (error, device) { + iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) { iotAgentLib.subscribe(device, ['dimming'], null, function (error) { should.not.exist(error); @@ -339,7 +339,7 @@ describe('NGSI-LD - Secured access to the Context Broker with OAuth2 provider', contextBrokerMock.delete('/ngsi-ld/v1/subscriptions/51c0ac9ed714fb3b37d7d5a8', '').reply(204); - iotAgentLib.getDevice('Light1', 'smartgondor', 'electricity', function (error, device) { + iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) { iotAgentLib.subscribe(device, ['dimming'], null, function (error) { iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) { contextBrokerMock.done(); diff --git a/test/unit/ngsi-ld/general/https-support-test.js b/test/unit/ngsi-ld/general/https-support-test.js index fb6221091..a48da7d37 100644 --- a/test/unit/ngsi-ld/general/https-support-test.js +++ b/test/unit/ngsi-ld/general/https-support-test.js @@ -213,7 +213,7 @@ describe('NGSI-LD - HTTPS support tests', function () { }); it('should send the appropriate request to the Context Broker', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { should.not.exist(error); diff --git a/test/unit/ngsi-ld/ngsiService/subscriptions-test.js b/test/unit/ngsi-ld/ngsiService/subscriptions-test.js index 25c8baceb..069457254 100644 --- a/test/unit/ngsi-ld/ngsiService/subscriptions-test.js +++ b/test/unit/ngsi-ld/ngsiService/subscriptions-test.js @@ -103,7 +103,7 @@ describe('NGSI-LD - Subscription tests', function () { describe('When a client invokes the subscribe() function for device', function () { it('should send the appropriate request to the Context Broker', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { should.not.exist(error); @@ -114,9 +114,9 @@ describe('NGSI-LD - Subscription tests', function () { }); }); it('should store the subscription ID in the Device Registry', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { should.not.exist(error); should.exist(device); should.exist(device.subscriptions); @@ -139,10 +139,10 @@ describe('NGSI-LD - Subscription tests', function () { done(); }); it('should delete the subscription from the CB', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { contextBrokerMock.done(); done(); }); @@ -151,10 +151,10 @@ describe('NGSI-LD - Subscription tests', function () { }); }); it('should remove the id from the subscriptions array', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { should.not.exist(error); should.exist(device); should.exist(device.subscriptions); @@ -177,9 +177,9 @@ describe('NGSI-LD - Subscription tests', function () { }); it('should delete the subscription from the CB', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { - iotAgentLib.unregister(device.id, 'smartgondor', '/gardens', function (error) { + iotAgentLib.unregister(device.id, null, 'smartgondor', '/gardens', function (error) { contextBrokerMock.done(); done(); }); @@ -189,7 +189,7 @@ describe('NGSI-LD - Subscription tests', function () { }); describe('When a new notification comes to the IoTAgent', function () { beforeEach(function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { done(); }); diff --git a/test/unit/ngsi-ld/provisioning/device-registration_test.js b/test/unit/ngsi-ld/provisioning/device-registration_test.js index d93e78517..6c979ebfd 100644 --- a/test/unit/ngsi-ld/provisioning/device-registration_test.js +++ b/test/unit/ngsi-ld/provisioning/device-registration_test.js @@ -222,7 +222,7 @@ describe('NGSI-LD - IoT Agent Device Registration', function () { it("should return all the device's information", function (done) { iotAgentLib.register(device1, function (error) { - iotAgentLib.getDevice('light1', 'smartgondor', 'gardens', function (error, data) { + iotAgentLib.getDevice('light1', null, 'smartgondor', 'gardens', function (error, data) { should.not.exist(error); should.exist(data); data.type.should.equal('Light'); @@ -252,7 +252,7 @@ describe('NGSI-LD - IoT Agent Device Registration', function () { it('should return a ENTITY_NOT_FOUND error', function (done) { iotAgentLib.register(device1, function (error) { - iotAgentLib.getDevice('lightUnexistent', 'smartgondor', 'gardens', function (error, data) { + iotAgentLib.getDevice('lightUnexistent', null, 'smartgondor', 'gardens', function (error, data) { should.exist(error); should.not.exist(data); error.code.should.equal(404); @@ -301,7 +301,7 @@ describe('NGSI-LD - IoT Agent Device Registration', function () { }); it('should update the devices information in Context Broker', function (done) { - iotAgentLib.unregister(device1.id, 'smartgondor', 'gardens', function (error) { + iotAgentLib.unregister(device1.id, null, 'smartgondor', 'gardens', function (error) { should.not.exist(error); contextBrokerMock.done(); done(); @@ -346,7 +346,7 @@ describe('NGSI-LD - IoT Agent Device Registration', function () { it('should not remove the device from the internal registry'); it('should return a UNREGISTRATION_ERROR error to the caller', function (done) { - iotAgentLib.unregister(device1.id, 'smartgondor', 'gardens', function (error) { + iotAgentLib.unregister(device1.id, null, 'smartgondor', 'gardens', function (error) { should.exist(error); should.exist(error.name); error.name.should.equal('UNREGISTRATION_ERROR'); diff --git a/test/unit/ngsi-ld/provisioning/device-update-registration_test.js b/test/unit/ngsi-ld/provisioning/device-update-registration_test.js index ff2ede4bf..306dad6b2 100644 --- a/test/unit/ngsi-ld/provisioning/device-update-registration_test.js +++ b/test/unit/ngsi-ld/provisioning/device-update-registration_test.js @@ -202,7 +202,7 @@ describe('NGSI-LD - IoT Agent Device Update Registration', function () { }); it('should store the new values in the registry', function (done) { iotAgentLib.updateRegister(deviceUpdated, false, function (error, data) { - iotAgentLib.getDevice(deviceUpdated.id, 'smartgondor', 'gardens', function (error, deviceResult) { + iotAgentLib.getDevice(deviceUpdated.id, null, 'smartgondor', 'gardens', function (error, deviceResult) { should.not.exist(error); should.exist(deviceResult); deviceResult.internalId.should.equal(deviceUpdated.internalId); @@ -247,6 +247,7 @@ describe('NGSI-LD - IoT Agent Device Update Registration', function () { iotAgentLib.updateRegister(deviceCommandUpdated, false, function (error, data) { iotAgentLib.getDevice( deviceCommandUpdated.id, + null, 'smartgondor', 'gardens', function (error, deviceResult) { diff --git a/test/unit/ngsiv2/general/contextBrokerOAuthSecurityAccess-test.js b/test/unit/ngsiv2/general/contextBrokerOAuthSecurityAccess-test.js index 14388b028..87f492189 100644 --- a/test/unit/ngsiv2/general/contextBrokerOAuthSecurityAccess-test.js +++ b/test/unit/ngsiv2/general/contextBrokerOAuthSecurityAccess-test.js @@ -310,7 +310,7 @@ describe('NGSI-v2 - Secured access to the Context Broker with OAuth2 provider', }); it('subscribe requests use auth header', function (done) { - iotAgentLib.getDevice('Light1', 'smartgondor', 'electricity', function (error, device) { + iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) { iotAgentLib.subscribe(device, ['dimming'], null, function (error) { should.not.exist(error); @@ -331,7 +331,7 @@ describe('NGSI-v2 - Secured access to the Context Broker with OAuth2 provider', contextBrokerMock.delete('/v2/subscriptions/51c0ac9ed714fb3b37d7d5a8', '').reply(204); - iotAgentLib.getDevice('Light1', 'smartgondor', 'electricity', function (error, device) { + iotAgentLib.getDevice('Light1', null, 'smartgondor', 'electricity', function (error, device) { iotAgentLib.subscribe(device, ['dimming'], null, function (error) { iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) { contextBrokerMock.done(); diff --git a/test/unit/ngsiv2/general/https-support-test.js b/test/unit/ngsiv2/general/https-support-test.js index 3f3ac93be..d92b9daa9 100644 --- a/test/unit/ngsiv2/general/https-support-test.js +++ b/test/unit/ngsiv2/general/https-support-test.js @@ -210,7 +210,7 @@ describe('NGSI-v2 - HTTPS support tests', function () { }); it('should send the appropriate request to the Context Broker', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { should.not.exist(error); diff --git a/test/unit/ngsiv2/ngsiService/subscriptions-test.js b/test/unit/ngsiv2/ngsiService/subscriptions-test.js index d8d1fd3a7..c971a95d8 100644 --- a/test/unit/ngsiv2/ngsiService/subscriptions-test.js +++ b/test/unit/ngsiv2/ngsiService/subscriptions-test.js @@ -95,7 +95,7 @@ describe('NGSI-v2 - Subscription tests', function () { describe('When a client invokes the subscribe() function for device', function () { it('should send the appropriate request to the Context Broker', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { should.not.exist(error); @@ -106,9 +106,9 @@ describe('NGSI-v2 - Subscription tests', function () { }); }); it('should store the subscription ID in the Device Registry', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { should.not.exist(error); should.exist(device); should.exist(device.subscriptions); @@ -132,10 +132,10 @@ describe('NGSI-v2 - Subscription tests', function () { done(); }); it('should delete the subscription from the CB', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { contextBrokerMock.done(); done(); }); @@ -144,10 +144,10 @@ describe('NGSI-v2 - Subscription tests', function () { }); }); it('should remove the id from the subscriptions array', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { iotAgentLib.unsubscribe(device, '51c0ac9ed714fb3b37d7d5a8', function (error) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { should.not.exist(error); should.exist(device); should.exist(device.subscriptions); @@ -171,9 +171,9 @@ describe('NGSI-v2 - Subscription tests', function () { }); it('should delete the subscription from the CB', function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { - iotAgentLib.unregister(device.id, 'smartgondor', '/gardens', function (error) { + iotAgentLib.unregister(device.id, null, 'smartgondor', '/gardens', function (error) { contextBrokerMock.done(); done(); }); @@ -183,7 +183,7 @@ describe('NGSI-v2 - Subscription tests', function () { }); describe('When a new notification comes to the IoTAgent', function () { beforeEach(function (done) { - iotAgentLib.getDevice('MicroLight1', 'smartgondor', '/gardens', function (error, device) { + iotAgentLib.getDevice('MicroLight1', null, 'smartgondor', '/gardens', function (error, device) { iotAgentLib.subscribe(device, ['attr_name'], null, function (error) { done(); }); diff --git a/test/unit/ngsiv2/provisioning/device-registration_test.js b/test/unit/ngsiv2/provisioning/device-registration_test.js index 532357382..ba881d4ed 100644 --- a/test/unit/ngsiv2/provisioning/device-registration_test.js +++ b/test/unit/ngsiv2/provisioning/device-registration_test.js @@ -82,13 +82,15 @@ const device1 = { id: 'light1', type: 'Light', service: 'smartgondor', - subservice: 'gardens' + subservice: 'gardens', + apikey: null }; const device2 = { id: 'term2', type: 'Termometer', service: 'smartgondor', - subservice: 'gardens' + subservice: 'gardens', + apikey: null }; describe('NGSI-v2 - IoT Agent Device Registration', function () { @@ -223,7 +225,7 @@ describe('NGSI-v2 - IoT Agent Device Registration', function () { it("should return all the device's information", function (done) { iotAgentLib.register(device1, function (error) { - iotAgentLib.getDevice('light1', 'smartgondor', 'gardens', function (error, data) { + iotAgentLib.getDevice('light1', null, 'smartgondor', 'gardens', function (error, data) { should.not.exist(error); should.exist(data); data.type.should.equal('Light'); @@ -254,7 +256,7 @@ describe('NGSI-v2 - IoT Agent Device Registration', function () { it('should return a ENTITY_NOT_FOUND error', function (done) { iotAgentLib.register(device1, function (error) { - iotAgentLib.getDevice('lightUnexistent', 'smartgondor', 'gardens', function (error, data) { + iotAgentLib.getDevice('lightUnexistent', null, 'smartgondor', 'gardens', function (error, data) { should.exist(error); should.not.exist(data); error.code.should.equal(404); @@ -301,7 +303,7 @@ describe('NGSI-v2 - IoT Agent Device Registration', function () { }); it('should update the devices information in Context Broker', function (done) { - iotAgentLib.unregister(device1.id, 'smartgondor', 'gardens', function (error) { + iotAgentLib.unregister(device1.id, null, 'smartgondor', 'gardens', function (error) { should.not.exist(error); contextBrokerMock.done(); done(); @@ -346,7 +348,7 @@ describe('NGSI-v2 - IoT Agent Device Registration', function () { it('should not remove the device from the internal registry'); it('should return a UNREGISTRATION_ERROR error to the caller', function (done) { - iotAgentLib.unregister(device1.id, 'smartgondor', 'gardens', function (error) { + iotAgentLib.unregister(device1.id, null, 'smartgondor', 'gardens', function (error) { should.exist(error); should.exist(error.name); error.name.should.equal('UNREGISTRATION_ERROR'); diff --git a/test/unit/ngsiv2/provisioning/device-update-registration_test.js b/test/unit/ngsiv2/provisioning/device-update-registration_test.js index 6326e755a..5b1576ac2 100644 --- a/test/unit/ngsiv2/provisioning/device-update-registration_test.js +++ b/test/unit/ngsiv2/provisioning/device-update-registration_test.js @@ -206,7 +206,7 @@ describe('NGSI-v2 - IoT Agent Device Update Registration', function () { it('should store the new values in the registry', function (done) { iotAgentLib.updateRegister(deviceUpdated, false, function (error, data) { - iotAgentLib.getDevice(deviceUpdated.id, 'smartgondor', 'gardens', function (error, deviceResult) { + iotAgentLib.getDevice(deviceUpdated.id, null, 'smartgondor', 'gardens', function (error, deviceResult) { should.not.exist(error); should.exist(deviceResult); deviceResult.internalId.should.equal(deviceUpdated.internalId); @@ -264,6 +264,7 @@ describe('NGSI-v2 - IoT Agent Device Update Registration', function () { iotAgentLib.updateRegister(deviceCommandUpdated, false, function (error, data) { iotAgentLib.getDevice( deviceCommandUpdated.id, + null, 'smartgondor', 'gardens', function (error, deviceResult) { From b8aa35221998b36f3d1b88433314d80f1812d869 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 8 Sep 2023 11:13:16 +0200 Subject: [PATCH 14/20] print query params --- lib/services/common/genericMiddleware.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/common/genericMiddleware.js b/lib/services/common/genericMiddleware.js index 5f632faea..3a1094c9d 100644 --- a/lib/services/common/genericMiddleware.js +++ b/lib/services/common/genericMiddleware.js @@ -56,7 +56,7 @@ function handleError(error, req, res, next) { * Express middleware for tracing the complete request arriving to the IoTA in debug mode. */ function traceRequest(req, res, next) { - logger.debug(context, 'Request for path [%s] from [%s]', req.path, req.get('host')); + logger.debug(context, 'Request for path [%s] query [%j] from [%s]', req.path, req, query, req.get('host')); if (req.is('json') || req.is('application/ld+json')) { logger.debug(context, 'Body:\n\n%s\n\n', JSON.stringify(req.body, null, 4)); From 09318f04a0927fa0f5c9f3d4c05101233401508b Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 8 Sep 2023 11:30:15 +0200 Subject: [PATCH 15/20] fix typo --- lib/services/common/genericMiddleware.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/common/genericMiddleware.js b/lib/services/common/genericMiddleware.js index 3a1094c9d..2f9c0cbd9 100644 --- a/lib/services/common/genericMiddleware.js +++ b/lib/services/common/genericMiddleware.js @@ -56,7 +56,7 @@ function handleError(error, req, res, next) { * Express middleware for tracing the complete request arriving to the IoTA in debug mode. */ function traceRequest(req, res, next) { - logger.debug(context, 'Request for path [%s] query [%j] from [%s]', req.path, req, query, req.get('host')); + logger.debug(context, 'Request for path [%s] query [%j] from [%s]', req.path, req.query, req.get('host')); if (req.is('json') || req.is('application/ld+json')) { logger.debug(context, 'Body:\n\n%s\n\n', JSON.stringify(req.body, null, 4)); From 4e7ef853bbe94108afbe1269859c1c15614f021e Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 8 Sep 2023 13:31:36 +0200 Subject: [PATCH 16/20] replace req.params.apikey -> req.query.apikey --- lib/services/northBound/deviceProvisioningServer.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/services/northBound/deviceProvisioningServer.js b/lib/services/northBound/deviceProvisioningServer.js index fa55f0932..9a179d6b5 100644 --- a/lib/services/northBound/deviceProvisioningServer.js +++ b/lib/services/northBound/deviceProvisioningServer.js @@ -251,7 +251,7 @@ function handleListDevices(req, res, next) { function handleGetDevice(req, res, next) { deviceService.getDevice( req.params.deviceId, - req.params.apikey, + req.query.apikey, req.headers['fiware-service'], req.headers['fiware-servicepath'], function (error, device) { @@ -300,7 +300,7 @@ function handleRemoveDevice(req, res, next) { apply( getDevice, req.params.deviceId, - req.params.apikey, + req.query.apikey, req.headers['fiware-service'], req.headers['fiware-servicepath'] ), @@ -308,7 +308,7 @@ function handleRemoveDevice(req, res, next) { apply( unregisterDevice, req.params.deviceId, - req.params.apikey, + req.query.apikey, req.headers['fiware-service'], req.headers['fiware-servicepath'] ) @@ -342,7 +342,7 @@ function handleUpdateDevice(req, res, next) { } else { deviceService.getDevice( req.params.deviceId, - req.params.apikey, + req.query.apikey, req.headers['fiware-service'], req.headers['fiware-servicepath'], function (error, device) { From 138259208b3960d62d41d7d8b44d6aceaedf68d2 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 11 Sep 2023 09:40:27 +0200 Subject: [PATCH 17/20] Update CNR --- CHANGES_NEXT_RELEASE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index d977f4456..7b187b502 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,4 +1,4 @@ -- Fix: try to use apikey from measure/group to find device in first attempt (#1426) +- Fix: try to use apikey from measure/group to find, update, remove device in first attempt (#1426) - Fix: ensure device apikey in already provisioned device (#1430) - Upgrade mongodb dev dep from 4.7.0 to 4.17.0 - Upgrade mongoose dep from 5.13.14 to 5.13.20 From c1b7e22b927241abe9df635265045e697010891f Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 11 Sep 2023 10:04:46 +0200 Subject: [PATCH 18/20] Update CHANGES_NEXT_RELEASE --- CHANGES_NEXT_RELEASE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 7b187b502..97218e308 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,4 +1,4 @@ -- Fix: try to use apikey from measure/group to find, update, remove device in first attempt (#1426) +- Fix: try to use apikey from measure/group to find, update, remove device in first attempt (#1426,#1435) - Fix: ensure device apikey in already provisioned device (#1430) - Upgrade mongodb dev dep from 4.7.0 to 4.17.0 - Upgrade mongoose dep from 5.13.14 to 5.13.20 From 895c42e5489d60e079e12bcf56312dfb6e9930d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferm=C3=ADn=20Gal=C3=A1n=20M=C3=A1rquez?= Date: Mon, 11 Sep 2023 10:05:53 +0200 Subject: [PATCH 19/20] Update CHANGES_NEXT_RELEASE --- CHANGES_NEXT_RELEASE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 97218e308..6a5e64f07 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,4 +1,4 @@ -- Fix: try to use apikey from measure/group to find, update, remove device in first attempt (#1426,#1435) +- Fix: try to use apikey from measure/group to find, update, remove device in first attempt (#1426, #1435) - Fix: ensure device apikey in already provisioned device (#1430) - Upgrade mongodb dev dep from 4.7.0 to 4.17.0 - Upgrade mongoose dep from 5.13.14 to 5.13.20 From 6b26373a237e25afe4294a2c71e84900873dac19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferm=C3=ADn=20Gal=C3=A1n=20M=C3=A1rquez?= Date: Mon, 11 Sep 2023 10:06:05 +0200 Subject: [PATCH 20/20] Update CHANGES_NEXT_RELEASE --- CHANGES_NEXT_RELEASE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 6a5e64f07..0d2bfb0b0 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,4 +1,4 @@ -- Fix: try to use apikey from measure/group to find, update, remove device in first attempt (#1426, #1435) +- Fix: try to use apikey from measure/group to find, update, remove device in first attempt (#1426, #1435) - Fix: ensure device apikey in already provisioned device (#1430) - Upgrade mongodb dev dep from 4.7.0 to 4.17.0 - Upgrade mongoose dep from 5.13.14 to 5.13.20