-
Notifications
You must be signed in to change notification settings - Fork 4
/
attributeFunctionInterpolator.js
333 lines (311 loc) · 11.7 KB
/
attributeFunctionInterpolator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright 2016 Telefónica Investigación y Desarrollo, S.A.U
*
* This file is part of the Short Time Historic (STH) component
*
* STH is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* STH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with STH.
* If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with: [[email protected]]
*/
'use strict';
var ROOT_PATH = require('app-root-path');
var async = require('async');
var deasync = require('deasync');
var _eval = require('eval');
var request = require('request');
var fdsErrors = require(ROOT_PATH + '/lib/errors/fdsErrors');
var domainConf, contextBrokerConf;
var VARIABLE_RE = /\${{[^{}]+}{[^{}]+}}/g;
var ENTITY_ATTRIBUTE_RE = /{[^{}]+}/g;
var STATE_RE = /\/\*\s*[^:]+:\s*(.*)\s*\*\//;
/**
* Returns the entity-attribute map for the passed spec
* @param {string} spec The spec
* @return {Object} The entity-attribute map
*/
function getEntityAttributeMap(spec) {
var entityAttributeMap = {};
var variableMatches = spec.match(VARIABLE_RE);
if (!variableMatches) {
return entityAttributeMap;
}
spec.match(VARIABLE_RE).forEach(function(specMatch) {
var entityName, attributeName;
var entry = specMatch.match(ENTITY_ATTRIBUTE_RE);
entityName = entry[0].substring(1, entry[0].length - 1);
attributeName = entry[1].substring(1, entry[1].length - 1);
entityAttributeMap[entityName] = entityAttributeMap[entityName] || [];
if (entityAttributeMap[entityName].indexOf(attributeName) === - 1) {
entityAttributeMap[entityName].push(attributeName);
}
});
return entityAttributeMap;
}
/**
* Sends a HTTP request
* @param {String} token The authorization token
* @param {Object} requestOptions The request options
* @param {Function} callback The callback
*/
function sendRequest(token, requestOptions, callback) {
requestOptions.headers['X-Auth-Token'] = token;
request(requestOptions, callback);
}
/**
* Returns the entity name from an entity name and possible type pair
* @param {String} entity The entity name and possible type pair
* @return {String} The entity name
*/
function getEntityName(entity) {
var entityName;
if (entity.indexOf(':#:') === -1) {
entityName = entity;
} else {
entityName = entity.substring(0, entity.indexOf(':#:'));
}
return entityName;
}
/**
* Returns the entity type (if any) from an entity name and possible type pair
* @param {String} entity The entity name and possible type pair
* @return {String} The entity type or undefined if no type
*/
function getEntityType(entity) {
var entityType;
if (entity.indexOf(':#:') !== -1) {
entityType = entity.substring(entity.indexOf(':#:') + 3);
}
return entityType;
}
/**
* Returns the request options from a entity-attribute map
* @param {Object} domainConf The domain configuration
* @param {Object} contextBrokerConf The Context Broker configuration
* @param {Object} entityAttributeMap The entity-attribute map
* @return {Array} The request options array
*/
function getRequestOptions(domainConf, contextBrokerConf, entityAttributeMap) {
var body,
requestOptions = [],
entityName,
entityType;
var entities = Object.getOwnPropertyNames(entityAttributeMap);
entities.forEach(function(entity) {
entityName = getEntityName(entity);
entityType = getEntityType(entity);
body = {
entities: [
{
id: entityName,
isPattern: 'false'
}
]
};
if (entityType) {
body.entities[0].type = entityType;
}
entityAttributeMap[entity].forEach(function(attribute) {
body.attributes = body.attributes || [];
body.attributes.push(attribute);
});
requestOptions.push(
{
method: 'POST',
url: contextBrokerConf.protocol + '://' + contextBrokerConf.host + ':' + contextBrokerConf.port +
'/v1/queryContext',
rejectUnauthorized: false,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Fiware-Service': domainConf.service,
'Fiware-ServicePath': domainConf.subservice
},
json: true,
body: body
}
);
});
return requestOptions;
}
/**
* Returns the attribute value from the received responses
* @param {Array} responses The array of responses
* @param {String} entity The entity name and optional type
* @param {String} attribute The attribute name
* @return {Object} The attribute value
*/
function getAttributeValue(responses, entity, attribute) {
var entityName = getEntityName(entity);
var entityType = getEntityType(entity);
for (var ii = 0; ii < responses.length; ii++) {
if (responses[ii].body.contextResponses[0].contextElement.id === entityName &&
(entityType ? responses[ii].body.contextResponses[0].contextElement.type === entityType : true)) {
for (var jj = 0; jj < responses[ii].body.contextResponses[0].contextElement.attributes.length; jj++) {
if (responses[ii].body.contextResponses[0].contextElement.attributes[jj].name === attribute) {
return responses[ii].body.contextResponses[0].contextElement.attributes[jj].value;
}
}
}
}
}
/**
* Checks if there has been an error when getting the attibute values
* @param {Array} responseArray Array of responsees obtained from the Context Broker
* @return {Boolean} True if there has been any error, false otherwise
*/
function checkError(responseArray) {
for (var ii = 0; ii < responseArray.length; ii++) {
if (parseInt(responseArray[ii].statusCode, 10) !== 200 ||
(responseArray[ii].body.errorCode && parseInt(responseArray[ii].body.errorCode.code, 10) !== 200)) {
return true;
}
}
return false;
}
/**
* Composes the state map from the state specification included in the interpolator specification
* @param {String} spec The interpolator specification
* @return {Object} The generated state map
*/
function generateStateMap(spec) {
var stateMap = {},
stateRegExpResult = STATE_RE.exec(spec),
stateVariables,
stateVariablesArray;
if (stateRegExpResult && stateRegExpResult.length >= 1) {
stateVariables = stateRegExpResult[1];
if (stateVariables) {
stateVariablesArray = stateVariables.split(',');
stateVariablesArray.forEach(function(variable) {
if (variable.indexOf('=') !== -1) {
stateMap[variable.trim().substring(0, variable.trim().indexOf('=')).trim()] =
JSON.parse(variable.trim().substring(variable.trim().indexOf('=') + 1));
} else {
stateMap[variable.trim()] = null;
}
});
}
}
return stateMap;
}
module.exports = function(interpolationSpec, theDomainConf, theContextBrokerConf){
var entityAttributeMap,
requestOptions,
stateMap;
domainConf = theDomainConf;
contextBrokerConf = theContextBrokerConf;
/**
* Returns the variable map to be passed to the Javascript code to evaluated
* @return {Object} The variable map
*/
function getVariableMap() {
var variableMap = {}, globalNames, stateMapNames;
globalNames = Object.getOwnPropertyNames(global.fdsGlobals);
globalNames.forEach(function(globalName) {
variableMap[globalName] = global.fdsGlobals[globalName];
});
stateMapNames = Object.getOwnPropertyNames(stateMap);
stateMapNames.forEach(function(stateMapEntry) {
variableMap[stateMapEntry] = stateMap[stateMapEntry];
});
return variableMap;
}
/**
* Process the evaluated value returned by the Javascript code
* @param {Object} evaluatedValue The evaluated value
* @return {Object} The post-processed evaluated value
*/
function processEvaluatedValue(evaluatedValue) {
var globalNames, stateMapNames;
if (typeof evaluatedValue === 'object') {
if (evaluatedValue.state && typeof evaluatedValue.state === 'object') {
if (evaluatedValue.state.globals) {
globalNames = Object.getOwnPropertyNames(evaluatedValue.state.globals);
globalNames.forEach(function(globalName) {
global.fdsGlobals[globalName] = evaluatedValue.state.globals[globalName];
});
delete evaluatedValue.state.globals;
}
stateMapNames = Object.getOwnPropertyNames(evaluatedValue.state);
stateMapNames.forEach(function(stateMapEntry) {
stateMap[stateMapEntry] = evaluatedValue.state[stateMapEntry];
});
}
if (evaluatedValue.result || evaluatedValue.result === 0 || evaluatedValue.result === false) {
evaluatedValue = evaluatedValue.result;
}
}
return evaluatedValue;
}
/**
* Returns the new interpolated value asynchronously
* @return {Object} The new interpolated value
*/
function attributeFunctionInterpolator(token, callback) {
var evalStr = interpolationSpec,
evalStrAux;
async.map(requestOptions, sendRequest.bind(null, token), function(err, responseArray) {
if (err || checkError(responseArray)) {
return callback(
new fdsErrors.ValueResolutionError('Error when getting some attribute value from the Context Broker ' +
'for an attribute-function-interpolator resolution with spec: \'' + interpolationSpec + '\''));
}
var entities = Object.getOwnPropertyNames(entityAttributeMap);
entities.forEach(function(entity) {
entityAttributeMap[entity].forEach(function(attribute) {
// String.replace() global replace can only be done with a regular expression so we loop until no more changes
evalStrAux = null;
while (evalStrAux !== evalStr) {
evalStrAux = evalStr;
evalStr = evalStr.replace(new RegExp('${{' + entity + '}{' + attribute + '}}', 'g').source,
getAttributeValue(responseArray, entity, attribute));
}
});
});
var evaluatedValue;
try {
if (evalStr.indexOf('module.exports') !== -1) {
global.SimulationDate = Date;
global.fdsGlobals = global.fdsGlobals || {};
var variableMap = getVariableMap();
evaluatedValue = _eval(evalStr, variableMap, true);
evaluatedValue = processEvaluatedValue(evaluatedValue);
} else {
/* jshint evil: true */
evaluatedValue = eval(evalStr);
/* jshint evil: false */
}
} catch (exception) {
return callback(
new fdsErrors.ValueResolutionError('Error when evaluating the Javascript code ' +
'for an attribute-function-interpolator resolution with spec: \'' + interpolationSpec + '\''));
}
callback(null, evaluatedValue);
});
}
if (typeof interpolationSpec !== 'string') {
try {
interpolationSpec = JSON.stringify(interpolationSpec);
} catch (exception) {
throw new fdsErrors.ValueResolutionError('Error when evaluating the Javascript code ' +
'for an attribute-function-interpolator resolution with spec: \'' + interpolationSpec + '\'');
}
}
entityAttributeMap = getEntityAttributeMap(interpolationSpec);
stateMap = generateStateMap(interpolationSpec);
requestOptions = getRequestOptions(domainConf, contextBrokerConf, entityAttributeMap);
return deasync(attributeFunctionInterpolator);
};