-
Notifications
You must be signed in to change notification settings - Fork 29
/
visualRules.js
387 lines (373 loc) · 14.7 KB
/
visualRules.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright 2015 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of perseo-fe
*
* perseo-fe 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.
*
* perseo-fe 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 perseo-fe.
* If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with::[contacto@tid.es]
*/
'use strict';
var util = require('util'),
rules = require('./rules'),
myutils = require('../myutils'),
eplTemplate = 'select *,"${name}" as ruleName' + ' from pattern [every ev=iotEvent(${conditions})]',
operatorMap = {
GREATER_THAN: ' > ',
EQUAL_TO: ' = ',
MINOR_THAN: ' < ',
DIFFERENT_TO: ' != ',
GREATER_OR_EQUAL_THAN: ' >= ',
MINOR_OR_EQUAL_THAN: ' <= ',
MATCH: ' regexp '
},
errors = {},
logger = require('logops');
function errorOperator(op) {
if (operatorMap[op] === undefined) {
return new errors.UnknownOperator(op);
}
return null;
}
function castVar(str, isNumber) {
str = util.format('cast(`%s`?, String)', str);
if (isNumber === true) {
str = util.format('cast(%s, float)', str);
}
return str;
}
function adaptExp(str, isNumber) {
if (str.indexOf('${') !== 0) {
if (isNumber !== true) {
str = util.format('%j', str);
}
return str;
}
//Remove starting "${" and trailing "}"
str = str.slice(2, -1);
//Change dot to double underscore
str = str.replace(/\./g, '__');
return castVar(str, isNumber);
}
function makeUserPropCondition(exprL, op, exprR, isNumber) {
exprL = adaptExp(exprL, isNumber);
exprR = adaptExp(exprR, isNumber);
return util.format('(%s %s %s)', exprL, operatorMap[op], exprR);
}
function makeObservationCondition(exprL, op, exprR, isNumber) {
exprL = castVar(exprL, isNumber);
exprR = adaptExp(exprR, isNumber);
return util.format('(%s %s %s)', exprL, operatorMap[op], exprR);
}
function plainParams(p) {
var i,
obj = {};
for (i = 0; i < p.length; i += 1) {
obj[p[i].name] = p[i].value;
}
return obj;
}
function vr2rule(cardRule) {
var r,
i,
card,
conditions = [],
action = {},
noSignal,
id,
idRegexp,
type,
plain,
checkInterval,
errOp;
try {
r = { VR: cardRule, name: cardRule.name };
for (i = 0; i < cardRule.cards.length; i += 1) {
card = cardRule.cards[i];
switch (card.type) {
case 'SensorCard':
switch (card.conditionList[0].scope) {
case 'USER_PROP':
errOp = errorOperator(card.conditionList[0].operator);
if (errOp) {
return errOp;
}
conditions.push(
makeUserPropCondition(
card.conditionList[0].userProp,
card.conditionList[0].operator,
card.conditionList[0].parameterValue,
false
)
);
break;
case 'XPATH':
errOp = errorOperator(card.conditionList[0].operator);
if (errOp) {
return errOp;
}
conditions.push(
makeObservationCondition(
card.conditionList[0].parameterName,
card.conditionList[0].operator,
card.conditionList[0].parameterValue,
card.sensorData.dataType === 'Quantity'
)
);
if (card.conditionList[0].parameterName === 'id') {
if (card.conditionList[0].operator === 'MATCH') {
idRegexp = card.conditionList[0].parameterValue;
//Just to check it is a valid regexp
new RegExp(idRegexp);
} else {
id = card.conditionList[0].parameterValue;
}
} else if (card.conditionList[0].parameterName === 'type') {
type = card.conditionList[0].parameterValue;
} else {
//Added default else clause
logger.debug('vr2rule() - Default else clause');
}
break;
case 'OBSERVATION':
errOp = errorOperator(card.conditionList[0].operator);
if (errOp) {
return errOp;
}
if (card.sensorData.measureName === 'id') {
return new errors.IdAsAttribute('');
} else if (card.sensorData.measureName === 'type') {
return new errors.TypeAsAttribute('');
} else {
//Added default else clause
logger.debug('vr2rule() - Default else clause');
}
conditions.push(
makeObservationCondition(
card.sensorData.measureName,
card.conditionList[0].operator,
card.conditionList[0].parameterValue,
card.sensorData.dataType === 'Quantity'
)
);
break;
case 'LAST_MEASURE':
checkInterval = parseInt(card.timeData.interval, 10);
if (isNaN(checkInterval) || checkInterval < 1) {
return new errors.InvalidVisualRule('check interval ' + card.timeData.interval);
}
noSignal = {
checkInterval: card.timeData.interval,
attribute: card.sensorData.measureName,
reportInterval: card.conditionList[0].parameterValue
};
break;
default:
return new errors.NotSupportedActionScope(card.conditionList[0].scope);
}
break;
case 'ActionCard':
switch (card.actionData.type) {
case 'SendEmailAction':
plain = plainParams(card.actionData.userParams);
action.type = 'email';
action.template = plain['mail.message'];
action.parameters = {
to: plain['mail.to'],
from: plain['mail.from'],
subject: plain['mail.subject']
};
break;
case 'SendSmsMibAction':
plain = plainParams(card.actionData.userParams);
action.type = 'sms';
action.template = plain['sms.message'];
action.parameters = {
to: plain['sms.to']
};
break;
case 'updateAttribute':
action.type = 'update';
action.parameters = {
name: card.actionData.userParams.name,
value: card.actionData.userParams.value
};
if (card.actionData.userParams.actionType) {
action.parameters.actionType = card.actionData.userParams.actionType;
}
break;
default:
return new errors.NotSupportedActionType(card.actionData.type);
}
break;
case 'TimeCard':
if (card.configData) {
switch (card.configData.timeType) {
case 'timeInterval': //No signal detection
// This is different from old DCA rules. No-signal cards are now of type
// SensorCard with a timeData field. https://jirapdi.tid.es/browse/PIOTP-855
return new errors.NotSupportedTimeCard(card.configData.timeType);
//break; not necessary with return
case 'timeElapsed': // Minimal interval for actions
action.interval = card.timeData.interval;
break;
default:
//nothing to do
}
} else {
return new errors.MissingConfigDataInTimeCard(JSON.stringify(card));
}
break;
default:
//nothing to do
}
}
r.action = action;
r.subservice = cardRule.subservice;
r.service = cardRule.service;
if (noSignal) {
r.nosignal = noSignal;
r.nosignal.id = id;
r.nosignal.idRegexp = idRegexp;
r.nosignal.type = type;
} else {
r.text = myutils.expandVar(
eplTemplate,
{
name: r.name,
conditions: conditions.join(' AND '),
service: r.service,
subservice: r.subservice
},
true
);
}
} catch (ex) {
// SHOULD BE ex instanceof TypeError. Do not do anything else inside try
// Also it can be instance of SyntaxError: Invalid regular expression
return new errors.InvalidVisualRule(ex.toString());
}
return r;
}
module.exports = {
FindAll: function FindAll(service, subservice, callback) {
rules.FindAll(service, subservice, function(err, data) {
if (err) {
return callback(err, null);
}
var vrdata = [],
i;
for (i = 0; i < data.length; i += 1) {
if (data[i].VR) {
vrdata.push(data[i].VR);
}
}
return callback(err, vrdata);
});
},
Find: function Find(rule, callback) {
rules.Find(rule, function(err, data) {
if (err) {
return callback(err, null);
}
data = data || {};
data.VR = data.VR || {};
return callback(err, data.VR);
});
},
Save: function Save(vrrule, callback) {
var rule = vr2rule(vrrule);
if (rule instanceof Error) {
myutils.logErrorIf(rule);
return callback(rule, null);
}
rules.Save(rule, function(err, data) {
if (err) {
return callback(err, null);
}
return callback(null, data[2].VR);
});
},
Remove: function Remove(id, callback) {
rules.Remove(id, callback);
},
Put: function(id, rule, callback) {
rule = vr2rule(rule);
if (rule instanceof Error) {
myutils.logErrorIf(rule);
return callback(rule, null);
}
rules.Put(id, rule, function(err, data) {
if (err) {
return callback(err, null);
}
callback(err, data[0].VR);
});
}
};
/**
* Constructors for possible errors from this module
*
* @type {Object}
*/
module.exports.errors = errors;
// Only for testing, not intended for external use
module.exports.vr2rule = vr2rule;
module.exports.errorOperator = errorOperator;
(function() {
errors.InvalidVisualRule = function InvalidVisualRule(msg) {
this.name = 'INVALID_VISUAL_RULE';
this.message = 'invalid visual rule format ' + msg;
this.httpCode = 400;
};
errors.NotSupportedTimeCard = function NotSupportedTimeCard(msg) {
this.message = 'TimeCard not supported ' + msg;
this.httpCode = 400;
};
errors.NotSupportedActionType = function NotSupportedActionType(msg) {
this.name = 'ACTION_TYPE_NOT_SUPPORTED';
this.message = 'ActionType not supported ' + msg;
this.httpCode = 400;
};
errors.NotSupportedActionScope = function NotSupportedActionScope(msg) {
this.name = 'UNKNOWN_SCOPE_ACTION';
this.message = 'Scope not supported ' + msg;
this.httpCode = 400;
};
errors.MissingConfigDataInTimeCard = function MissingConfigDataInTimeCard(msg) {
this.name = 'MISSING_CONFIGDATA_TIMECARD';
this.message = 'Missing config data in time card' + msg;
this.httpCode = 400;
};
errors.IdAsAttribute = function IdAsAttribute(msg) {
this.name = 'ID_ATTRIBUTE';
this.message = 'id as attribute ' + msg;
this.httpCode = 400;
};
errors.TypeAsAttribute = function TypeAsAttribute(msg) {
this.name = 'TYPE_ATTRIBUTE';
this.message = 'type as attribute ' + msg;
this.httpCode = 400;
};
errors.UnknownOperator = function UnknownOperator(msg) {
this.name = 'UNKNOWN_OPERATOR';
this.message = 'unknown operator ' + msg;
this.httpCode = 400;
};
Object.keys(errors).forEach(function(element) {
util.inherits(errors[element], Error);
});
})();