Skip to content

Commit

Permalink
Merge pull request #717 from telefonicaid/task/remove_update_ngsiv1
Browse files Browse the repository at this point in the history
remove  ngsiv1 from updateAction
  • Loading branch information
fgalan committed Jul 3, 2023
2 parents 7a84619 + 0c53acf commit 1c77e7f
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 355 deletions.
2 changes: 1 addition & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1 +1 @@

- Remove ngsiv1 support for updateAction (#714)
2 changes: 0 additions & 2 deletions docs/API/plain_rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
155 changes: 7 additions & 148 deletions lib/models/updateAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {};

Expand All @@ -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
*
Expand Down Expand Up @@ -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);
});
}
};
}
Expand Down Expand Up @@ -653,34 +523,23 @@ 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);
}
}

module.exports.doIt = doIt;
module.exports.buildUpdateOptions = buildUpdateOptions;

/**
* Constructors for possible errors from this module
Expand Down
9 changes: 2 additions & 7 deletions test/component/metrics/metrics_actions_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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
Expand Down Expand Up @@ -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);
});
Expand Down
1 change: 0 additions & 1 deletion test/data/good_rules/blood_rule_update.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"action": {
"type": "update",
"parameters": {
"version": "1",
"name": "abnormal",
"value": "true",
"type": "boolean"
Expand Down
1 change: 0 additions & 1 deletion test/data/good_rules/blood_rule_update_trust.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"action": {
"type": "update",
"parameters": {
"version": "1",
"name": "abnormal",
"value": "true",
"type": "boolean",
Expand Down
Loading

0 comments on commit 1c77e7f

Please sign in to comment.