Skip to content

Commit

Permalink
Merge pull request #1616 from telefonicaid/task/device_update_handler…
Browse files Browse the repository at this point in the history
…_with_old_device_value

Task/device update handler with old device value
  • Loading branch information
fgalan authored Jun 5, 2024
2 parents f161af2 + 8a34ce3 commit 3e3842a
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Fix: update device using previous device apikey to avoid error when apikey is updated (iota-json#833)
- Fix: allow send multiple measures to CB in a batch (POST /v2/op/update) and sorted by TimeInstant when possible, instead of using multiples single request (iotagent-json#825, #1612)
- Fix: default express limit to 1Mb instead default 100Kb and allow change it throught a conf env var 'IOTA_EXPRESS_LIMIT' (iotagent-json#827)
- Fix: accept 201 status code from context broker with NGSI-LD interface when first measure is sent and device is created in it (#1617)
2 changes: 1 addition & 1 deletion lib/services/devices/deviceRegistryMemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function getByName(name, service, subservice, callback) {
getByNameAndType(name, null, service, subservice, callback);
}

function update(device, callback) {
function update(previousDevice, device, callback) {
registeredDevices[device.service][device.id] = deepClone(device);
callback(null, device);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/services/devices/deviceRegistryMongoDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ function getByName(name, service, servicepath, callback) {
*
* @param {Object} device Device object with the new values to write.
*/
function update(device, callback) {
function update(previousDevice, device, callback) {
logger.debug(context, 'Storing updated values for device [%s]:\n%s', device.id, JSON.stringify(device, null, 4));
getDevice(device.id, device.apikey, device.service, device.subservice, function (error, data) {
getDevice(device.id, previousDevice.apikey, device.service, device.subservice, function (error, data) {
if (error) {
callback(error);
} else {
Expand Down
7 changes: 4 additions & 3 deletions lib/services/devices/deviceService.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,8 @@ function unregisterDevice(id, apikey, service, subservice, callback) {
});
}

function updateRegisterDevice(deviceObj, entityInfoUpdated, callback) {
deviceHandler.updateRegisterDevice(deviceObj, entityInfoUpdated, callback);
function updateRegisterDevice(deviceObj, previousDevice, entityInfoUpdated, callback) {
deviceHandler.updateRegisterDevice(deviceObj, previousDevice, entityInfoUpdated, callback);
}

/**
Expand Down Expand Up @@ -518,7 +518,8 @@ function getDevice(deviceId, apikey, service, subservice, callback) {
* @param {String} device JSON object contain the device to update.
*/
function updateDevice(device, callback) {
config.getRegistry().update(device, callback);
logger.debug(context, 'updateDevice %j', device);
config.getRegistry().update(device, device, callback);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions lib/services/devices/devices-NGSI-LD.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function updateEntityNgsiLD(deviceData, updatedDevice, callback) {
*
* @param {Object} deviceObj Object with all the device information (mandatory).
*/
function updateRegisterDeviceNgsiLD(deviceObj, entityInfoUpdated, callback) {
function updateRegisterDeviceNgsiLD(deviceObj, previousDevice, entityInfoUpdated, callback) {
if (!deviceObj.id || !deviceObj.type) {
callback(new errors.MissingAttributes('Id or device missing', deviceObj));
return;
Expand Down Expand Up @@ -280,7 +280,7 @@ function updateRegisterDeviceNgsiLD(deviceObj, entityInfoUpdated, callback) {
apply(
config.getRegistry().get,
deviceObj.id,
deviceObj.apikey,
previousDevice.apikey,
deviceObj.service,
deviceObj.subservice
),
Expand All @@ -289,7 +289,7 @@ function updateRegisterDeviceNgsiLD(deviceObj, entityInfoUpdated, callback) {
apply(combineWithNewDevice, deviceObj),
apply(registrationUtils.sendRegistrations, false),
apply(registrationUtils.processContextRegistration, deviceObj),
config.getRegistry().update
apply(config.getRegistry().update, previousDevice)
],
callback
);
Expand All @@ -299,7 +299,7 @@ function updateRegisterDeviceNgsiLD(deviceObj, entityInfoUpdated, callback) {
apply(
config.getRegistry().get,
deviceObj.id,
deviceObj.apikey,
previousDevice.apikey,
deviceObj.service,
deviceObj.subservice
),
Expand All @@ -308,7 +308,7 @@ function updateRegisterDeviceNgsiLD(deviceObj, entityInfoUpdated, callback) {
apply(combineWithNewDevice, deviceObj),
apply(registrationUtils.sendRegistrations, false),
apply(registrationUtils.processContextRegistration, deviceObj),
config.getRegistry().update
apply(config.getRegistry().update, previousDevice)
],
callback
);
Expand Down
6 changes: 3 additions & 3 deletions lib/services/devices/devices-NGSI-mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ const deviceHandlerV2 = require('./devices-NGSI-v2');
*
* @param {Object} deviceObj Object with all the device information (mandatory).
*/
function updateRegisterDeviceNgsiMixed(deviceObj, entityInfoUpdated, callback) {
function updateRegisterDeviceNgsiMixed(deviceObj, previousDevice, entityInfoUpdated, callback) {
if (config.checkNgsiLD(deviceObj)) {
deviceHandlerLD.updateRegisterDevice(deviceObj, entityInfoUpdated, callback);
deviceHandlerLD.updateRegisterDevice(deviceObj, previousDevice, entityInfoUpdated, callback);
} else {
deviceHandlerV2.updateRegisterDevice(deviceObj, entityInfoUpdated, callback);
deviceHandlerV2.updateRegisterDevice(deviceObj, previousDevice, entityInfoUpdated, callback);
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/services/devices/devices-NGSI-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function updateEntityNgsi2(deviceData, updatedDevice, callback) {
*
* @param {Object} deviceObj Object with all the device information (mandatory).
*/
function updateRegisterDeviceNgsi2(deviceObj, entityInfoUpdated, callback) {
function updateRegisterDeviceNgsi2(deviceObj, previousDevice, entityInfoUpdated, callback) {
if (!deviceObj.id || !deviceObj.type) {
callback(new errors.MissingAttributes('Id or device missing', deviceObj));
return;
Expand Down Expand Up @@ -352,7 +352,7 @@ function updateRegisterDeviceNgsi2(deviceObj, entityInfoUpdated, callback) {
apply(
config.getRegistry().get,
deviceObj.id,
deviceObj.apikey,
previousDevice.apikey, // it could be updated
deviceObj.service,
deviceObj.subservice
),
Expand All @@ -361,7 +361,7 @@ function updateRegisterDeviceNgsi2(deviceObj, entityInfoUpdated, callback) {
apply(combineWithNewDevice, deviceObj),
apply(registrationUtils.sendRegistrations, false),
apply(registrationUtils.processContextRegistration, deviceObj),
config.getRegistry().update
apply(config.getRegistry().update, previousDevice)
],
callback
);
Expand All @@ -371,7 +371,7 @@ function updateRegisterDeviceNgsi2(deviceObj, entityInfoUpdated, callback) {
apply(
config.getRegistry().get,
deviceObj.id,
deviceObj.apikey,
previousDevice.apikey,
deviceObj.service,
deviceObj.subservice
),
Expand All @@ -380,7 +380,7 @@ function updateRegisterDeviceNgsi2(deviceObj, entityInfoUpdated, callback) {
apply(combineWithNewDevice, deviceObj),
apply(registrationUtils.sendRegistrations, false),
apply(registrationUtils.processContextRegistration, deviceObj),
config.getRegistry().update
apply(config.getRegistry().update, previousDevice)
],
callback
);
Expand Down
4 changes: 2 additions & 2 deletions lib/services/ngsi/subscription-NGSI-LD.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function createSubscriptionHandlerNgsiLD(device, triggers, store, callback) {
triggers
});

config.getRegistry().update(device, callback);
config.getRegistry().update(device, device, callback);
} else {
callback(null, response.headers.location);
}
Expand Down Expand Up @@ -203,7 +203,7 @@ function createUnsubscribeHandlerNgsiLD(device, id, callback) {
callback(new errors.BadRequest(body.orionError.details));
} else {
device.subscriptions.splice(device.subscriptions.indexOf(id), 1);
config.getRegistry().update(device, callback);
config.getRegistry().update(device, device, callback);
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions lib/services/ngsi/subscription-NGSI-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function createSubscriptionHandlerNgsi2(device, triggers, store, callback) {
triggers
});

config.getRegistry().update(device, callback);
config.getRegistry().update(device, device, callback);
} else {
callback(null, response.headers.location);
}
Expand Down Expand Up @@ -203,7 +203,7 @@ function createUnsubscribeHandlerNgsi2(device, id, callback) {
callback(new errors.BadRequest(body.orionError.details));
} else {
device.subscriptions.splice(device.subscriptions.indexOf(id), 1);
config.getRegistry().update(device, callback);
config.getRegistry().update(device, device, callback);
}
};
}
Expand Down
9 changes: 5 additions & 4 deletions lib/services/northBound/deviceProvisioningServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,11 @@ function handleRemoveDevices(req, res, next) {
* This middleware handles updates in the provisioning devices. The only attribute
*/
function handleUpdateDevice(req, res, next) {
function applyUpdatingHandler(device, callback) {
function applyUpdatingHandler(newDevice, oldDevice, callback) {
if (updatingHandler) {
updatingHandler(device, callback);
updatingHandler(newDevice, oldDevice, callback);
} else {
callback(null, device);
callback(null, newDevice);
}
}

Expand All @@ -429,10 +429,11 @@ function handleUpdateDevice(req, res, next) {
isTypeOrNameUpdated = true;
}
async.waterfall(
[apply(applyUpdatingHandler, newDevice)],
[apply(applyUpdatingHandler, newDevice, device)],
function handleUpdating(error, newDeviceUpdated) {
deviceService.updateRegister(
newDeviceUpdated,
device,
isTypeOrNameUpdated,
function handleDeviceUpdate(error) {
if (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"apikey": "APIKEY2",
"timezone": "Europe/Madrid"
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ describe('NGSI-LD - IoT Agent Device Update Registration', function () {
});

it('should register as ContextProvider of its lazy attributes', function (done) {
iotAgentLib.updateRegister(deviceUpdated, false, function (error) {
iotAgentLib.updateRegister(deviceUpdated, device1, false, function (error) {
should.not.exist(error);
contextBrokerMock.done();
done();
});
});
it('should store the new values in the registry', function (done) {
iotAgentLib.updateRegister(deviceUpdated, false, function (error, data) {
iotAgentLib.updateRegister(deviceUpdated, device1, false, function (error, data) {
iotAgentLib.getDevice(deviceUpdated.id, null, 'smartgondor', 'gardens', function (error, deviceResult) {
should.not.exist(error);
should.exist(deviceResult);
Expand Down Expand Up @@ -236,7 +236,7 @@ describe('NGSI-LD - IoT Agent Device Update Registration', function () {
// });
// });
it('should store the new values in the registry', function (done) {
iotAgentLib.updateRegister(deviceCommandUpdated, false, function (error, data) {
iotAgentLib.updateRegister(deviceCommandUpdated, device1, false, function (error, data) {
iotAgentLib.getDevice(
deviceCommandUpdated.id,
null,
Expand All @@ -257,7 +257,7 @@ describe('NGSI-LD - IoT Agent Device Update Registration', function () {

describe('When a update action is executed in a non registered device', function () {
it('should return a DEVICE_NOT_FOUND error', function (done) {
iotAgentLib.updateRegister(unknownDevice, false, function (error) {
iotAgentLib.updateRegister(unknownDevice, device1, false, function (error) {
should.exist(error);
error.name.should.equal('DEVICE_NOT_FOUND');
done();
Expand All @@ -273,7 +273,7 @@ describe('NGSI-LD - IoT Agent Device Update Registration', function () {
});

it('should return a REGISTRATION_ERROR error in the update action', function (done) {
iotAgentLib.updateRegister(deviceUpdated, false, function (error) {
iotAgentLib.updateRegister(deviceUpdated, device1, false, function (error) {
should.exist(error);
//error.name.should.equal('UNREGISTRATION_ERROR');
done();
Expand Down
12 changes: 6 additions & 6 deletions test/unit/ngsiv2/provisioning/device-update-registration_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,15 @@ describe('NGSI-v2 - IoT Agent Device Update Registration', function () {
});

it('should register as ContextProvider of its lazy attributes', function (done) {
iotAgentLib.updateRegister(deviceUpdated, false, function (error) {
iotAgentLib.updateRegister(deviceUpdated, device1, false, function (error) {
should.not.exist(error);
contextBrokerMock.done();
done();
});
});

it('should store the new values in the registry', function (done) {
iotAgentLib.updateRegister(deviceUpdated, false, function (error, data) {
iotAgentLib.updateRegister(deviceUpdated, device1, false, function (error, data) {
iotAgentLib.getDevice(deviceUpdated.id, null, 'smartgondor', 'gardens', function (error, deviceResult) {
should.not.exist(error);
should.exist(deviceResult);
Expand Down Expand Up @@ -253,15 +253,15 @@ describe('NGSI-v2 - IoT Agent Device Update Registration', function () {
});

it('should register as ContextProvider of its commands and create the additional attributes', function (done) {
iotAgentLib.updateRegister(deviceCommandUpdated, false, function (error) {
iotAgentLib.updateRegister(deviceCommandUpdated, device1, false, function (error) {
should.not.exist(error);
contextBrokerMock.done();
done();
});
});

it('should store the new values in the registry', function (done) {
iotAgentLib.updateRegister(deviceCommandUpdated, false, function (error, data) {
iotAgentLib.updateRegister(deviceCommandUpdated, device1, false, function (error, data) {
iotAgentLib.getDevice(
deviceCommandUpdated.id,
null,
Expand All @@ -282,7 +282,7 @@ describe('NGSI-v2 - IoT Agent Device Update Registration', function () {

describe('When a update action is executed in a non registered device', function () {
it('should return a DEVICE_NOT_FOUND error', function (done) {
iotAgentLib.updateRegister(unknownDevice, false, function (error) {
iotAgentLib.updateRegister(unknownDevice, device1, false, function (error) {
should.exist(error);
error.name.should.equal('DEVICE_NOT_FOUND');
done();
Expand All @@ -304,7 +304,7 @@ describe('NGSI-v2 - IoT Agent Device Update Registration', function () {
});

it('should return a REGISTRATION_ERROR error in the update action', function (done) {
iotAgentLib.updateRegister(deviceUpdated, false, function (error) {
iotAgentLib.updateRegister(deviceUpdated, device1, false, function (error) {
should.exist(error);
error.name.should.equal('UNREGISTRATION_ERROR');
done();
Expand Down
35 changes: 35 additions & 0 deletions test/unit/ngsiv2/provisioning/updateProvisionedDevices-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,41 @@ describe('NGSI-v2 - Device provisioning API: Update provisioned devices', functi
});
});
});
describe('When an update request arrives with a new Apikey', function () {
const optionsUpdate = {
url: 'http://localhost:' + iotAgentConfig.server.port + '/iot/devices/Light1',
method: 'PUT',
headers: {
'fiware-service': 'smartgondor',
'fiware-servicepath': '/gardens'
},
json: utils.readExampleFile(
'./test/unit/examples/deviceProvisioningRequests/updateProvisionDeviceWithApikey.json'
)
};

beforeEach(function () {
contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.delete('/v2/registrations/6319a7f5254b05844116584d', '')
.reply(204);

contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post('/v2/registrations')
.reply(201, null, { Location: '/v2/registrations/4419a7f5254b058441165849' });
});

it('should raise a 204 error', function (done) {
request(optionsUpdate, function (error, response, body) {
should.not.exist(error);
response.statusCode.should.equal(204);
done();
});
});
});
describe('When a wrong update request payload arrives', function () {
const optionsUpdate = {
url: 'http://localhost:' + iotAgentConfig.server.port + '/iot/devices/Light1',
Expand Down

0 comments on commit 3e3842a

Please sign in to comment.