diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 8b137891..af3d2831 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1 +1 @@ - +- Remove ngsiv1 support for updateAction (#714) diff --git a/docs/API/plain_rules.md b/docs/API/plain_rules.md index 1e5564bf..0ffcffe5 100644 --- a/docs/API/plain_rules.md +++ b/docs/API/plain_rules.md @@ -383,8 +383,6 @@ the Perseo configuration). The `parameters` map includes the following fields: the rule is used, i.e. `${id}`) - type: optional, the type of the entity which attribute is to be updated (by default the type of the entity that triggers the rule is usedi.e. `${type}`) -- version: optional, The NGSI version for the update action. Set this attribute to `2` or `"2"` if you want to use - NGSv2 format. `2` by default. **It is NOT recommended to use `1`**. - isPattern: optional, `false` by default. (Only for NGSIv1. If `version` is set to 2, this attribute will be ignored) - attributes: _mandatory_, array of target attributes to update. Each element of the array must contain the fields - **name**: _mandatory_, attribute name to set diff --git a/lib/models/updateAction.js b/lib/models/updateAction.js index aed0d8b7..a013ae27 100644 --- a/lib/models/updateAction.js +++ b/lib/models/updateAction.js @@ -37,7 +37,6 @@ var util = require('util'), newTokenEventName = 'new_token', MAX_LISTENERS = 10e3, metrics = require('./metrics'), - URL = require('url').URL, NGSI = require('ngsijs'); function getCachedToken(service, subservice, name) { @@ -73,62 +72,6 @@ function generateToken(trust, cached, authentication) { }); } -function buildUpdateOptions(action, event) { - var options, - attr, - parameterTypeGiven = action.parameters.type !== undefined; - - action.parameters.id = action.parameters.id || '${id}'; - action.parameters.type = action.parameters.type || '${type}' || 'None'; - action.parameters.isPattern = action.parameters.isPattern || 'false'; - - action.parameters.attributes = action.parameters.attributes || []; - - // old format, with only one attribute to update - if (action.parameters.name) { - action.parameters.attributes.push({ - name: action.parameters.name, - value: action.parameters.value, - type: action.parameters.attrType - }); - } - options = { - id: myutils.expandVar(action.parameters.id, event), - isPattern: myutils.expandVar(action.parameters.isPattern, event) - }; - if (action.parameters.service !== undefined) { - options.service = myutils.expandVar(action.parameters.service, event); - } - if (action.parameters.subservice !== undefined) { - options.subservice = myutils.expandVar(action.parameters.subservice, event); - } - if (action.parameters.externalCBUrl !== undefined) { - options.externalCBUrl = myutils.expandVar(action.parameters.externalCBUrl, event); - } - if (action.parameters.actionType) { - options.actionType = myutils.expandVar(action.parameters.actionType, event); - } - options.attributes = []; - for (var i = 0; i < action.parameters.attributes.length; i++) { - attr = { - name: myutils.expandVar(action.parameters.attributes[i].name, event), - value: myutils.expandVar(action.parameters.attributes[i].value, event, true) - }; - - //The portal does not provide 'type' for attribute, "plain" rules could - if (action.parameters.attributes[i].type !== undefined) { - attr.type = myutils.expandVar(action.parameters.attributes[i].type, event); - } - options.attributes.push(attr); - } - - if (event.type !== undefined || parameterTypeGiven) { - options.type = myutils.expandVar(action.parameters.type, event); - } - - return options; -} - function buildQueryOptionsParams(action, event) { var filter = {}; @@ -155,73 +98,6 @@ function buildQueryOptionsParams(action, event) { return filter; } -function doRequest(action, event, token, callback) { - var subservice, service, headers, options, updateOrion; - - options = buildUpdateOptions(action, event); - - subservice = options.subservice ? options.subservice : event.subservice; - service = options.service ? options.service : event.service; - - updateOrion = { - contextElements: [ - { - isPattern: options.isPattern, - id: options.id, - attributes: options.attributes - } - ], - updateAction: 'APPEND' - }; - if (options.actionType !== undefined) { - updateOrion.updateAction = options.actionType; - } - - if (options.type !== undefined) { - updateOrion.contextElements[0].type = options.type; - } - - headers = { - Accept: 'application/json' - }; - if (token !== null) { - headers[constants.AUTH_HEADER] = token; - } - headers[constants.SUBSERVICE_HEADER] = subservice; - headers[constants.SERVICE_HEADER] = service; - - var orionUrl = config.orion.URL; - if (options.service !== undefined || options.subservice !== undefined) { - orionUrl = options.externalCBUrl !== undefined ? options.externalCBUrl : config.pep.URL; - } - metrics.IncMetrics(event.service, event.subservice, metrics.actionEntityUpdate); - logger.debug('body to post %j ', updateOrion); - myutils.requestHelper( - 'post', - { - url: new URL('v1/updateContext', orionUrl), - body: updateOrion, - json: true, - headers: headers - }, - function(err, data) { - if (err) { - metrics.IncMetrics(event.service, event.subservice, metrics.failedActionEntityUpdate); - - alarm.raise(alarm.ORION, null, err); - } else { - metrics.IncMetrics(event.service, event.subservice, metrics.okActionEntityUpdate); - - alarm.release(alarm.ORION); - } - if (!err && data && data.body && data.body.orionError) { - err = new errors.OrionError(JSON.stringify(data.body.orionError)); - } - callback(err, data); - } - ); -} - /** * Process NGSIv2 action do request option Params * @@ -602,15 +478,9 @@ function makeTokenListenerFunc(action, event, version, callback) { return callback(error); } else { logger.debug('tokenHandlerFunc retrying with', token); - if (version === 1) { - return doRequest(action, event, token, function cbDoReqUpdAxn(error, data) { - callback(error, data); - }); - } else { - return doRequestV2(action, event, token, function cbDoReqUpdAxn(error, data) { - callback(error, data); - }); - } + return doRequestV2(action, event, token, function cbDoReqUpdAxn(error, data) { + callback(error, data); + }); } }; } @@ -653,26 +523,16 @@ function doItWithToken(action, event, version, callback) { logger.debug('waiting token to be generated %s %s %s', service, subservice, ruleName); cached.emitter.once(newTokenEventName, newTokenListener); } else { - if (version === 1) { - doRequest(action, event, cached.token, cbDoReqUpdAxn); - } else { - doRequestV2(action, event, cached.token, cbDoReqUpdAxn); - } + doRequestV2(action, event, cached.token, cbDoReqUpdAxn); } } function doIt(action, event, callback) { try { - if (action.parameters.version !== '1' && action.parameters.version !== 1) { - if (action.parameters.trust) { - return doItWithToken(action, event, 2, callback); - } else { - return doRequestV2(action, event, null, callback); - } - } else if (action.parameters.trust) { - return doItWithToken(action, event, 1, callback); + if (action.parameters.trust) { + return doItWithToken(action, event, 2, callback); } else { - return doRequest(action, event, null, callback); + return doRequestV2(action, event, null, callback); } } catch (ex) { return callback(ex); @@ -680,7 +540,6 @@ function doIt(action, event, callback) { } module.exports.doIt = doIt; -module.exports.buildUpdateOptions = buildUpdateOptions; /** * Constructors for possible errors from this module diff --git a/test/component/metrics/metrics_actions_test.js b/test/component/metrics/metrics_actions_test.js index 10ad7ac2..89a88f67 100644 --- a/test/component/metrics/metrics_actions_test.js +++ b/test/component/metrics/metrics_actions_test.js @@ -184,10 +184,7 @@ describe('Metrics', function() { msub = m.services.unknownt.subservices['/']; should.equal(m.services.unknownt.sum.actionEntityUpdate, 1); - should.equal(m.services.unknownt.sum.okActionEntityUpdate, 1); - should.equal(m.services.unknownt.sum.failedActionEntityUpdate, 0); - should.equal(m.services.unknownt.sum.outgoingTransactions, 1); - should.equal(m.services.unknownt.sum.outgoingTransactionsErrors, 0); + // V2 request is expecting a valid response. V1 not. return callback(); }, 50); }); @@ -202,7 +199,7 @@ describe('Metrics', function() { // previously we were using 'http://inventedurl.notexists.com' but that domain exists. // It's better to use localhost in an invalid port (even in the case of using non-existing domains // timeouts due to DNS lookup may expire and the test to fail) - // port is a better option + // port is a better option var invalidPort = 3333; utilsT.getConfig().orion.URL = new URL(util.format('http://localhost:%s', invalidPort)); metrics.GetDecorated(true); // reset metrics @@ -231,8 +228,6 @@ describe('Metrics', function() { should.equal(m.services.unknownt.sum.actionEntityUpdate, 1); should.equal(m.services.unknownt.sum.okActionEntityUpdate, 0); should.equal(m.services.unknownt.sum.failedActionEntityUpdate, 1); - should.equal(m.services.unknownt.sum.outgoingTransactions, 1); - should.equal(m.services.unknownt.sum.outgoingTransactionsErrors, 1); return callback(); }, 1500); }); diff --git a/test/data/good_rules/blood_rule_update.json b/test/data/good_rules/blood_rule_update.json index 2d41bf5c..591aa2ac 100644 --- a/test/data/good_rules/blood_rule_update.json +++ b/test/data/good_rules/blood_rule_update.json @@ -4,7 +4,6 @@ "action": { "type": "update", "parameters": { - "version": "1", "name": "abnormal", "value": "true", "type": "boolean" diff --git a/test/data/good_rules/blood_rule_update_trust.json b/test/data/good_rules/blood_rule_update_trust.json index 2a3a3dbf..f6dc549f 100644 --- a/test/data/good_rules/blood_rule_update_trust.json +++ b/test/data/good_rules/blood_rule_update_trust.json @@ -4,7 +4,6 @@ "action": { "type": "update", "parameters": { - "version": "1", "name": "abnormal", "value": "true", "type": "boolean", diff --git a/test/unit/axn_param_utest.js b/test/unit/axn_param_utest.js index cb2b9d50..0762d38e 100644 --- a/test/unit/axn_param_utest.js +++ b/test/unit/axn_param_utest.js @@ -27,8 +27,7 @@ var should = require('should'), emailAction = require('../../lib/models/emailAction'), postAction = require('../../lib/models/postAction'), smsAction = require('../../lib/models/smsAction'), - twitterAction = require('../../lib/models/twitterAction'), - updateAction = require('../../lib/models/updateAction'); + twitterAction = require('../../lib/models/twitterAction'); describe('AxnParams', function() { describe('#buildMailOptions()', function() { @@ -184,197 +183,4 @@ describe('AxnParams', function() { /*jshint quotmark: single */ }); }); - describe('#buildUpdateOptions()', function() { - it('should substitute params (old format)', function() { - var event = { a: 'ID', b: 'TYPE', c: 'NAME', d: 'VALUE', e: 'ISPATTERN', f: 'ATTRTYPE' }, - action = { - parameters: { - id: '${a}', - type: '${b}', - name: '${c}', - value: '${d}', - isPattern: '${e}', - attrType: '${f}' - } - }, - options = updateAction.buildUpdateOptions(action, event); - - should.equal(options.id, 'ID'); - should.equal(options.type, 'TYPE'); - should.equal(options.attributes[0].name, 'NAME'); - should.equal(options.attributes[0].value, 'VALUE'); - should.equal(options.isPattern, 'ISPATTERN'); - should.equal(options.attributes[0].type, 'ATTRTYPE'); - }); - it('should keep params without placeholders (old format)', function() { - var event = { id: 'ID', type: 'TYPE' }, - action = { - parameters: { - name: 'NAME', - value: 'VALUE', - attrType: 'ATTRTYPE' - } - }, - options = updateAction.buildUpdateOptions(action, event); - - should.equal(options.id, 'ID'); // default value - should.equal(options.type, 'TYPE'); // default value - should.equal(options.attributes[0].name, 'NAME'); - should.equal(options.attributes[0].value, 'VALUE'); - should.equal(options.isPattern, 'false'); // default value - should.equal(options.attributes[0].type, 'ATTRTYPE'); - }); - it('should not use type if not specified and not in event (old format)', function() { - /* - Some entities do not have type. To update the same origin - entity, 'type' field should not be propagated to Context Broker. - If the action does not include an explicit parameter for 'type' - and the incoming event has not 'type' neither, options should - not include a 'type' - */ - var event = { id: 'ID', x: 'X' }, - action = { - parameters: { - name: 'NAME', - value: 'VALUE', - attrType: '${x}' - } - }, - options = updateAction.buildUpdateOptions(action, event); - - should.equal(options.id, 'ID'); // default value - should.not.exists(options.type); // not in event - should.equal(options.isPattern, 'false'); // default value - should.equal(options.attributes[0].name, 'NAME'); - should.equal(options.attributes[0].value, 'VALUE'); - should.equal(options.attributes[0].type, 'X'); - }); - it('should substitute params (array format)', function() { - var event = { - a: 'ID', - b: 'TYPE', - c: 'NAME', - d: 'VALUE', - e: 'ISPATTERN', - f: 'ATTRTYPE', - c1: 'NAME1', - d1: 'VALUE1', - f1: 'ATTRTYPE1', - c2: 'NAME2', - d2: 'VALUE2', - f2: 'ATTRTYPE2' - }, - action = { - parameters: { - id: '${a}', - type: '${b}', - isPattern: '${e}', - attributes: [ - { - name: '${c}', - value: '${d}', - type: '${f}' - }, - { - name: '${c1}', - value: '${d1}', - type: '${f1}' - }, - { - name: '${c2}', - value: '${d2}', - type: '${f2}' - } - ] - } - }, - options = updateAction.buildUpdateOptions(action, event); - - should.equal(options.id, 'ID'); - should.equal(options.type, 'TYPE'); - should.equal(options.isPattern, 'ISPATTERN'); - - should.equal(options.attributes[0].name, 'NAME'); - should.equal(options.attributes[0].value, 'VALUE'); - should.equal(options.attributes[0].type, 'ATTRTYPE'); - - should.equal(options.attributes[1].name, 'NAME1'); - should.equal(options.attributes[1].value, 'VALUE1'); - should.equal(options.attributes[1].type, 'ATTRTYPE1'); - - should.equal(options.attributes[2].name, 'NAME2'); - should.equal(options.attributes[2].value, 'VALUE2'); - should.equal(options.attributes[2].type, 'ATTRTYPE2'); - }); - it('should keep params without placeholders (array format)', function() { - var event = { id: 'ID', type: 'TYPE' }, - action = { - parameters: { - attributes: [ - { - name: 'NAME', - value: 'VALUE', - type: 'ATTRTYPE' - }, - { - name: 'NAME1', - value: 'VALUE1', - type: 'ATTRTYPE1' - }, - { - name: 'NAME2', - value: 'VALUE2', - type: 'ATTRTYPE2' - } - ] - } - }, - options = updateAction.buildUpdateOptions(action, event); - - should.equal(options.id, 'ID'); // default value - should.equal(options.type, 'TYPE'); // default value - should.equal(options.isPattern, 'false'); // default value - - should.equal(options.attributes[0].name, 'NAME'); - should.equal(options.attributes[0].value, 'VALUE'); - should.equal(options.attributes[0].type, 'ATTRTYPE'); - - should.equal(options.attributes[1].name, 'NAME1'); - should.equal(options.attributes[1].value, 'VALUE1'); - should.equal(options.attributes[1].type, 'ATTRTYPE1'); - - should.equal(options.attributes[2].name, 'NAME2'); - should.equal(options.attributes[2].value, 'VALUE2'); - should.equal(options.attributes[2].type, 'ATTRTYPE2'); - }); - it('should not use type if not specified and not in event (array format)', function() { - /* - Some entities do not have type. To update the same origin - entity, 'type' field should not be propagated to Context Broker. - If the action does not include an explicit parameter for 'type' - and the incoming event has not 'type' neither, options should - not include a 'type' - */ - var event = { id: 'ID', x: 'X' }, - action = { - parameters: { - attributes: [ - { - name: 'NAME', - value: 'VALUE', - type: '${x}' - } - ] - } - }, - options = updateAction.buildUpdateOptions(action, event); - - should.equal(options.id, 'ID'); // default value - should.not.exists(options.type); // not in event - should.equal(options.isPattern, 'false'); // default value - should.equal(options.attributes[0].name, 'NAME'); - should.equal(options.attributes[0].value, 'VALUE'); - should.equal(options.attributes[0].type, 'X'); - }); - }); });