sensorconfig.cpp 40.2 KB
Newer Older
1
2
//================================================================================
// Name        : sensorconfig.cpp
3
// Author      : Axel Auweter, Daniele Tafani
4
// Copyright   : Leibniz Supercomputing Centre
Michael Ott's avatar
Michael Ott committed
5
// Description : C++ API implementation for configuring libdcdb public sensors.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2011-2016 Leibniz Supercomputing Centre
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//================================================================================
Axel Auweter's avatar
Axel Auweter committed
26
27
28

#include <cstring>
#include <iostream>
Axel Auweter's avatar
Axel Auweter committed
29
#include <algorithm>
30
31
32
#include <string>
#include <list>
#include <unordered_set>
33
#include <utility>
Axel Auweter's avatar
Axel Auweter committed
34
35

#include "cassandra.h"
36
#include <boost/regex.hpp>
Axel Auweter's avatar
Axel Auweter committed
37
38
39

#include "sensorconfig_internal.h"
#include "dcdbglobals.h"
Axel Auweter's avatar
Axel Auweter committed
40
#include "dcdbendian.h"
41
#include "dcdb/virtualsensor.h"
Axel Auweter's avatar
Axel Auweter committed
42

43
44
using namespace DCDB;

Axel Auweter's avatar
Axel Auweter committed
45
/*
46
 * PublicSensor functions.
Axel Auweter's avatar
Axel Auweter committed
47
 */
48
PublicSensor::PublicSensor()
Axel Auweter's avatar
Axel Auweter committed
49
50
{
  name = "";
51
  is_virtual = false;
Axel Auweter's avatar
Axel Auweter committed
52
53
54
  pattern = "";
  scaling_factor = 1.0;
  unit = "";
55
  sensor_mask = 0;
56
  expression = "";
57
  v_sensorid = "";
58
59
  t_zero = 0;
  frequency = 0;
Axel Auweter's avatar
Axel Auweter committed
60
61
}

62
PublicSensor::PublicSensor (const PublicSensor &copy)
Axel Auweter's avatar
Axel Auweter committed
63
64
{
  name = copy.name;
65
  is_virtual = copy.is_virtual;
Axel Auweter's avatar
Axel Auweter committed
66
67
68
  pattern = copy.pattern;
  scaling_factor = copy.scaling_factor;
  unit = copy.unit;
69
  sensor_mask = copy.sensor_mask;
70
  expression = copy.expression;
71
  v_sensorid = copy.v_sensorid;
72
73
  t_zero = copy.t_zero;
  frequency = copy.frequency;
Axel Auweter's avatar
Axel Auweter committed
74
75
76
77
78
79
}


/*
 * SensorConfig functions
 */
80
81
82
83
84
85

SCError SensorConfig::loadCache()
{
  return impl->loadCache();
}

Axel Auweter's avatar
Axel Auweter committed
86
87
88
89
90
SCError SensorConfig::publishSensor(const char* publicName, const char* sensorPattern)
{
  return impl->publishSensor(publicName, sensorPattern);
}

91
SCError SensorConfig::publishVirtualSensor(const char* publicName, const char* vSensorExpression, const char* vSensorId, TimeStamp tZero, uint64_t frequency)
92
{
93
  return impl->publishVirtualSensor(publicName, vSensorExpression, vSensorId, tZero, frequency);
94
95
}

96
97
98
99
100
SCError SensorConfig::unPublishSensor(const char* publicName)
{
  return impl->unPublishSensor(publicName);
}

101
SCError SensorConfig::getPublicSensorNames(std::list<std::string>& publicSensors)
Axel Auweter's avatar
Axel Auweter committed
102
{
103
104
105
  return impl->getPublicSensorNames(publicSensors);
}

106
SCError SensorConfig::getPublicSensorsVerbose(std::list<PublicSensor>& publicSensors)
107
108
109
110
{
  return impl->getPublicSensorsVerbose(publicSensors);
}

111
SCError SensorConfig::getPublicSensorByName(PublicSensor& sensor, const char* publicName)
112
113
{
  return impl->getPublicSensorByName(sensor, publicName);
Axel Auweter's avatar
Axel Auweter committed
114
115
}

116
117
118
119
120
SCError SensorConfig::getPublicSensorsByWildcard(std::list<PublicSensor>& sensors, const char* wildcard)
{
    return impl->getPublicSensorsByWildcard(sensors, wildcard);
}

121
122
123
124
125
SCError SensorConfig::isVirtual(bool& isVirtual, std::string publicName)
{
  return impl->isVirtual(isVirtual, publicName);
}

Axel Auweter's avatar
Axel Auweter committed
126
127
128
129
130
131
132
133
134
135
SCError SensorConfig::getSensorPattern(std::string& pattern, std::string publicName)
{
  return impl->getSensorPattern(pattern, publicName);
}

SCError SensorConfig::getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern)
{
  return impl->getSensorListForPattern(sensorIds, pattern);
}

136
SCError SensorConfig::getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern, TimeStamp start, TimeStamp end)
Axel Auweter's avatar
Axel Auweter committed
137
138
139
140
{
  return impl->getSensorListForPattern(sensorIds, pattern, start, end);
}

141
142
143
144
145
146
147
148
149
SCError SensorConfig::setSensorScalingFactor(std::string publicName, double scalingFactor) {
  return impl->setSensorScalingFactor(publicName, scalingFactor);
}

SCError SensorConfig::setSensorUnit(std::string publicName, std::string unit)
{
  return impl->setSensorUnit(publicName, unit);
}

150
SCError SensorConfig::setSensorMask(std::string publicName, uint64_t mask)
151
{
152
  return impl->setSensorMask(publicName, mask);
153
154
}

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
SCError SensorConfig::setVirtualSensorExpression(std::string publicName, std::string expression)
{
  return impl->setVirtualSensorExpression(publicName, expression);
}

SCError SensorConfig::setVirtualSensorTZero(std::string publicName, TimeStamp tZero)
{
  return impl->setVirtualSensorTZero(publicName, tZero);
}

SCError SensorConfig::setVirtualSensorFrequency(std::string publicName, uint64_t frequency)
{
  return impl->setVirtualSensorFrequency(publicName, frequency);
}

Axel Auweter's avatar
Axel Auweter committed
170

171
SensorConfig::SensorConfig(Connection* conn)
Axel Auweter's avatar
Axel Auweter committed
172
173
174
175
176
177
178
179
180
181
182
183
184
{
  /* Allocate impl object */
  impl = new SensorConfigImpl(conn);
}

SensorConfig::~SensorConfig()
{
  if (impl) {
      delete impl;
  }
}

/*
185
 * SensorConfigImpl protected members and functions
Axel Auweter's avatar
Axel Auweter committed
186
187
188
189
190
191
192
193
194
195
196
197
 */

/*
 * Validate the pattern for a Sensor to be published
 * Patterns may only consist of hex numbers and forward slashes
 * and at most one wildcard character (*). Also, the total number of
 * bits may not exceed 128.
 */
bool SensorConfigImpl::validateSensorPattern(const char* sensorPattern)
{
  unsigned int wildcards, bits;

Michael Ott's avatar
Michael Ott committed
198
199
  /* 112 bits in HEX is at most 28 characters. Considering forward-slash separators, 56 is a reasonable maxmimum string length */
  if (strlen(sensorPattern) > 56) {
Axel Auweter's avatar
Axel Auweter committed
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
      return false;
  }

  /* Iterate through the string and validate/count the characters */
  bits = 0;
  wildcards = 0;
  for (unsigned int c = 0; c < strlen(sensorPattern); c++) {
      switch (sensorPattern[c]) {

      /* Regular HEX digit */
      case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9':
      case 'a':
      case 'A':
      case 'b':
      case 'B':
      case 'c':
      case 'C':
      case 'd':
      case 'D':
      case 'e':
      case 'E':
      case 'f':
      case 'F':
        bits += 4;
        break;

      /* Wildcard */
      case '*':
        wildcards++;
        break;

      /* Forward slash */
      case '/':
        break;

      /* Everything else is not allowed */
      default:
        return false;
      }
  }

  /* More than one wildcard is not allowed */
  if (wildcards > 1)
    return false;

  /* In case of a wildcard, there should be some bits left for the wildcard to fill in */
Michael Ott's avatar
Michael Ott committed
255
  if (wildcards == 1 && bits >= 112)
Axel Auweter's avatar
Axel Auweter committed
256
257
258
259
260
261
    return false;

  /* Looks good */
  return true;
}

262
263
264
265
266
267
268
269
/*
 * Validate the public name of a sensor.
 * Sensor public names may not contain forward slash characters.
 */
bool SensorConfigImpl::validateSensorPublicName(std::string publicName)
{
  return (publicName.find("/") == std::string::npos);
}
Axel Auweter's avatar
Axel Auweter committed
270
271
272
273

/*
 * SensorConfigImpl public functions
 */
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

SCError SensorConfigImpl::loadCache()
{
  sensorList.clear();
  sensorMapByName.clear();

  SCError rc = getPublicSensorsVerbose(sensorList);
  if (rc != SC_OK) {
    return rc;
  } else {
    for (auto& s: sensorList) {
      sensorMapByName.insert(std::make_pair(s.name, std::ref(s)));
    }
    return SC_OK;
  }
}

Axel Auweter's avatar
Axel Auweter committed
291
SCError SensorConfigImpl::publishSensor(std::string publicName, std::string sensorPattern)
Axel Auweter's avatar
Axel Auweter committed
292
293
{
  /* Check if the pattern matches the requirements */
Axel Auweter's avatar
Axel Auweter committed
294
  if (!validateSensorPattern(sensorPattern.c_str())) {
Axel Auweter's avatar
Axel Auweter committed
295
296
297
      return SC_INVALIDPATTERN;
  }

298
299
300
301
302
  /* Check if the publicName is valid */
  if (!validateSensorPublicName(publicName.c_str())) {
      return SC_INVALIDPUBLICNAME;
  }

Axel Auweter's avatar
Axel Auweter committed
303
304
305
306
307
308
309
310
311
312
  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

  /* Insert the entry */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const CassPrepared* prepared = nullptr;
313
  const char* query = "INSERT INTO " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " (name, pattern, virtual) VALUES (?,?, FALSE);";
Axel Auweter's avatar
Axel Auweter committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

  future = cass_session_prepare(session, query);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    connection->printError(future);
    cass_future_free(future);
    return SC_UNKNOWNERROR;
  } else {
    prepared = cass_future_get_prepared(future);
  }

  cass_future_free(future);

  statement = cass_prepared_bind(prepared);

Axel Auweter's avatar
Axel Auweter committed
331
332
  cass_statement_bind_string_by_name(statement, "name", publicName.c_str());
  cass_statement_bind_string_by_name(statement, "pattern", sensorPattern.c_str());
Axel Auweter's avatar
Axel Auweter committed
333
334
335
336
337
338
339

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    connection->printError(future);
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354

    cass_prepared_free(prepared);
    cass_future_free(future);
    cass_statement_free(statement);

    return SC_UNKNOWNERROR;
  }

  cass_prepared_free(prepared);
  cass_future_free(future);
  cass_statement_free(statement);

  return SC_OK;
}

355
SCError SensorConfigImpl::publishVirtualSensor(std::string publicName, std::string vSensorExpression, std::string vSensorId, TimeStamp tZero, uint64_t frequency)
356
357
358
359
360
361
362
363
364
365
366
{
  /* Check if the publicName is valid */
  if (!validateSensorPublicName(publicName.c_str())) {
      return SC_INVALIDPUBLICNAME;
  }

  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

367
368
  /* Validate vSensorExpression */
  try {
369
370
371
372
373
374
375
376
377
      VSensorExpression vsExp(connection, vSensorExpression);

      /* Check that it is not recursive-pointing to itself */
      std::unordered_set<std::string> inputSet;
      vsExp.getInputsRecursive(inputSet);
      std::unordered_set<std::string>::const_iterator found = inputSet.find(publicName);
      if (found != inputSet.end()) {
          return SC_EXPRESSIONSELFREF;
      }
378
379
380
381
382
  }
  catch (std::exception& e) {
      std::cout << e.what();
      return SC_INVALIDEXPRESSION;
  }
383
384
385
386
387
388

  /* Check if the vSensorId is valid */
  SensorId vSensor;
  if (!vSensor.mqttTopicConvert(vSensorId)) {
      return SC_INVALIDVSENSORID;
  }
389
390
391
392
393
394

  /* Insert the entry */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const CassPrepared* prepared = nullptr;
395
  const char* query = "INSERT INTO " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " (name, expression, vsensorid, tzero, frequency, virtual) VALUES (?,?,?,?,?,TRUE);";
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414

  future = cass_session_prepare(session, query);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    connection->printError(future);
    cass_future_free(future);
    return SC_UNKNOWNERROR;
  } else {
    prepared = cass_future_get_prepared(future);
  }

  cass_future_free(future);

  statement = cass_prepared_bind(prepared);

  cass_statement_bind_string_by_name(statement, "name", publicName.c_str());
  cass_statement_bind_string_by_name(statement, "expression", vSensorExpression.c_str());
415
  cass_statement_bind_string_by_name(statement, "vsensorid", vSensorId.c_str());
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
  cass_statement_bind_int64_by_name(statement, "tzero", tZero.getRaw());
  cass_statement_bind_int64_by_name(statement, "frequency", frequency);

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    connection->printError(future);

    cass_prepared_free(prepared);
    cass_future_free(future);
    cass_statement_free(statement);

    return SC_UNKNOWNERROR;
  }

  cass_prepared_free(prepared);
  cass_future_free(future);
  cass_statement_free(statement);

  return SC_OK;
}


441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
SCError SensorConfigImpl::unPublishSensor(std::string publicName)
{
  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

  /* Remove the entry */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const CassPrepared* prepared = nullptr;
  const char* query = "DELETE FROM " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " WHERE name = ? ;";

  future = cass_session_prepare(session, query);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    connection->printError(future);
    cass_future_free(future);
    return SC_UNKNOWNERROR;
  } else {
    prepared = cass_future_get_prepared(future);
  }

  cass_future_free(future);

  statement = cass_prepared_bind(prepared);

  cass_statement_bind_string_by_name(statement, "name", publicName.c_str());

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
    connection->printError(future);
Axel Auweter's avatar
Axel Auweter committed
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493

    cass_prepared_free(prepared);
    cass_future_free(future);
    cass_statement_free(statement);

    return SC_UNKNOWNERROR;
  }

  cass_prepared_free(prepared);
  cass_future_free(future);
  cass_statement_free(statement);

  return SC_OK;
}

494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
SCError SensorConfigImpl::getPublicSensorNames(std::list<std::string>& publicSensors)
{
  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

  /* Clear the list */
  publicSensors.clear();

  /* Fill the list with all public sensors */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const char* query = "SELECT name FROM " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " ;";

  statement = cass_statement_new(query, 0);

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
     connection->printError(future);
     cass_future_free(future);
     cass_statement_free(statement);
     return SC_UNKNOWNERROR;
  } else {
      const CassResult* result = cass_future_get_result(future);
      CassIterator* iterator = cass_iterator_from_result(result);

      while (cass_iterator_next(iterator)) {
          const char* name;
          size_t      name_len;

          const CassRow* row = cass_iterator_get_row(iterator);

          if (cass_value_get_string(cass_row_get_column_by_name(row, "name"), &name, &name_len) != CASS_OK) {
              name = ""; name_len = 0;
          }

          publicSensors.push_back(std::string(name, name_len));
      }
      cass_result_free(result);
      cass_iterator_free(iterator);
  }

  cass_future_free(future);
  cass_statement_free(statement);
  return SC_OK;
}

546
SCError SensorConfigImpl::getPublicSensorsVerbose(std::list<PublicSensor>& publicSensors)
Axel Auweter's avatar
Axel Auweter committed
547
548
549
550
551
552
{
  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

Axel Auweter's avatar
Axel Auweter committed
553
554
555
  /* Clear the list */
  publicSensors.clear();

Axel Auweter's avatar
Axel Auweter committed
556
557
558
559
  /* Fill the list with all public sensors */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
Axel Auweter's avatar
Axel Auweter committed
560
  const char* query = "SELECT * FROM " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " ;";
Axel Auweter's avatar
Axel Auweter committed
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579

  statement = cass_statement_new(query, 0);

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
     connection->printError(future);
     cass_future_free(future);
     cass_statement_free(statement);
     return SC_UNKNOWNERROR;
  } else {
      const CassResult* result = cass_future_get_result(future);
      CassIterator* iterator = cass_iterator_from_result(result);

      while (cass_iterator_next(iterator)) {
          const char* name;
          size_t      name_len;
580
          cass_bool_t is_virtual;
Axel Auweter's avatar
Axel Auweter committed
581
582
583
584
585
          const char* pattern;
          size_t      pattern_len;
          double      scaling_factor;
          const char* unit;
          size_t      unit_len;
586
          int64_t     sensor_mask;
587
588
          const char* expression;
          size_t      expression_len;
589
590
          const char* vsensorid;
          size_t      vsensorid_len;
591
592
          int64_t     tzero;
          int64_t     frequency;
593
          PublicSensor sensor;
Axel Auweter's avatar
Axel Auweter committed
594
595
596
597
598
599

          const CassRow* row = cass_iterator_get_row(iterator);

          if (cass_value_get_string(cass_row_get_column_by_name(row, "name"), &name, &name_len) != CASS_OK) {
              name = ""; name_len = 0;
          }
600
601
602
          if (cass_value_get_bool(cass_row_get_column_by_name(row, "virtual"), &is_virtual) != CASS_OK) {
              is_virtual = cass_false;
          }
Axel Auweter's avatar
Axel Auweter committed
603
604
605
606
607
608
609
610
611
          if (cass_value_get_string(cass_row_get_column_by_name(row, "pattern"), &pattern, &pattern_len) != CASS_OK) {
              pattern = ""; pattern_len = 0;
          }
          if (cass_value_get_double(cass_row_get_column_by_name(row, "scaling_factor"), &scaling_factor) != CASS_OK) {
              scaling_factor = 1.0;
          }
          if (cass_value_get_string(cass_row_get_column_by_name(row, "unit"), &unit, &unit_len) != CASS_OK) {
              unit = ""; unit_len = 0;
          }
612
613
          if (cass_value_get_int64(cass_row_get_column_by_name(row, "sensor_mask"), &sensor_mask) != CASS_OK) {
              sensor_mask = 0;
614
          }
615
616
617
          if (cass_value_get_string(cass_row_get_column_by_name(row, "expression"), &expression, &expression_len) != CASS_OK) {
              expression = ""; expression_len = 0;
          }
618
619
620
          if (cass_value_get_string(cass_row_get_column_by_name(row, "vsensorid"), &vsensorid, &vsensorid_len) != CASS_OK) {
              vsensorid = ""; vsensorid_len = 0;
          }
621
622
623
624
625
626
          if (cass_value_get_int64(cass_row_get_column_by_name(row, "tzero"), &tzero) != CASS_OK) {
              tzero = 0;
          }
          if (cass_value_get_int64(cass_row_get_column_by_name(row, "frequency"), &frequency) != CASS_OK) {
              frequency = 0;
          }
Axel Auweter's avatar
Axel Auweter committed
627
628

          sensor.name = std::string(name, name_len);
629
          sensor.is_virtual = is_virtual == cass_true ? true : false;
Axel Auweter's avatar
Axel Auweter committed
630
631
632
          sensor.pattern = std::string(pattern, pattern_len);
          sensor.scaling_factor = scaling_factor;
          sensor.unit = std::string(unit, unit_len);
633
          sensor.sensor_mask = sensor_mask;
634
          sensor.expression = std::string(expression, expression_len);
635
          sensor.v_sensorid = std::string(vsensorid, vsensorid_len);
636
637
          sensor.t_zero = tzero;
          sensor.frequency = frequency;
Axel Auweter's avatar
Axel Auweter committed
638
639
640
641
642
643
644
645
646
647
648
649

          publicSensors.push_back(sensor);
      }
      cass_result_free(result);
      cass_iterator_free(iterator);
  }

  cass_future_free(future);
  cass_statement_free(statement);
  return SC_OK;
}

650
SCError SensorConfigImpl::getPublicSensorByName(PublicSensor& sensor, const char* publicName)
651
{
652
  /* Check if the sensor definition is already in the cache */
653
654
  SensorMap_t::const_iterator got = sensorMapByName.find(publicName);
  if (got != sensorMapByName.end()) {
655
656
657
658
659
      sensor = got->second;
      return SC_OK;
  }

  /* Not in cache - query the data base */
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

  /* Fill the list with all public sensors */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const CassPrepared* prepared = nullptr;
  const char* query = "SELECT * FROM " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " WHERE name = ?;";

  future = cass_session_prepare(session, query);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
      connection->printError(future);
      cass_future_free(future);
      return SC_UNKNOWNERROR;
  } else {
      prepared = cass_future_get_prepared(future);
  }
  cass_future_free(future);

  statement = cass_prepared_bind(prepared);
  cass_statement_bind_string_by_name(statement, "name", publicName);

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
     connection->printError(future);
     cass_future_free(future);
     cass_statement_free(statement);
     return SC_UNKNOWNERROR;
  } else {
      const CassResult* result = cass_future_get_result(future);
      CassIterator* iterator = cass_iterator_from_result(result);

      if (cass_iterator_next(iterator)) {
          const char* name;
          size_t      name_len;
704
          cass_bool_t is_virtual;
705
706
707
708
709
          const char* pattern;
          size_t      pattern_len;
          double      scaling_factor;
          const char* unit;
          size_t      unit_len;
710
          int64_t     sensor_mask;
711
712
          const char* expression;
          size_t      expression_len;
713
714
          const char* vsensorid;
          size_t      vsensorid_len;
715
716
          int64_t     tzero;
          int64_t     frequency;
717
718
719
720
721
722

          const CassRow* row = cass_iterator_get_row(iterator);

          if (cass_value_get_string(cass_row_get_column_by_name(row, "name"), &name, &name_len) != CASS_OK) {
              name = ""; name_len = 0;
          }
723
724
725
          if (cass_value_get_bool(cass_row_get_column_by_name(row, "virtual"), &is_virtual) != CASS_OK) {
              is_virtual = cass_false;
          }
726
727
728
729
730
731
732
733
734
          if (cass_value_get_string(cass_row_get_column_by_name(row, "pattern"), &pattern, &pattern_len) != CASS_OK) {
              pattern = ""; pattern_len = 0;
          }
          if (cass_value_get_double(cass_row_get_column_by_name(row, "scaling_factor"), &scaling_factor) != CASS_OK) {
              scaling_factor = 1.0;
          }
          if (cass_value_get_string(cass_row_get_column_by_name(row, "unit"), &unit, &unit_len) != CASS_OK) {
              unit = ""; unit_len = 0;
          }
735
736
          if (cass_value_get_int64(cass_row_get_column_by_name(row, "sensor_mask"), &sensor_mask) != CASS_OK) {
              sensor_mask = 0;
737
          }
738
739
740
          if (cass_value_get_string(cass_row_get_column_by_name(row, "expression"), &expression, &expression_len) != CASS_OK) {
              expression = ""; expression_len = 0;
          }
741
742
743
          if (cass_value_get_string(cass_row_get_column_by_name(row, "vsensorid"), &vsensorid, &vsensorid_len) != CASS_OK) {
              vsensorid = ""; vsensorid_len = 0;
          }
744
745
746
747
748
749
          if (cass_value_get_int64(cass_row_get_column_by_name(row, "tzero"), &tzero) != CASS_OK) {
              tzero = 0;
          }
          if (cass_value_get_int64(cass_row_get_column_by_name(row, "frequency"), &frequency) != CASS_OK) {
              frequency = 0;
          }
750
751

          sensor.name = std::string(name, name_len);
752
          sensor.is_virtual = is_virtual == cass_true ? true : false;
753
754
755
          sensor.pattern = std::string(pattern, pattern_len);
          sensor.scaling_factor = scaling_factor;
          sensor.unit = std::string(unit, unit_len);
756
          sensor.sensor_mask = sensor_mask;
757
          sensor.expression = std::string(expression, expression_len);
758
          sensor.v_sensorid = std::string(vsensorid, vsensorid_len);
759
760
761
          sensor.t_zero = tzero;
          sensor.frequency = frequency;

762
          /* Add to sensorPropertyCache for later use */
763
	  sensorMapByName.insert(std::make_pair(publicName, std::ref(sensor)));
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
      }
      else {
          cass_result_free(result);
          cass_iterator_free(iterator);
          cass_future_free(future);
          cass_statement_free(statement);

          return SC_UNKNOWNSENSOR;
      }
      cass_result_free(result);
      cass_iterator_free(iterator);
  }

  cass_future_free(future);
  cass_statement_free(statement);
779
  cass_prepared_free(prepared);
780
781
782
  return SC_OK;
}

783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
SCError SensorConfigImpl::getPublicSensorsByWildcard(std::list<PublicSensor>& sensors, const char* wildcard)
{
    SCError err = SC_OK;
    
    if (strpbrk(wildcard, "*?") == NULL) {
	PublicSensor sen;
	if ((err = getPublicSensorByName(sen, wildcard)) == SC_OK) {
	    sensors.push_back(sen);
	}
	return err;
    } else {
	if ((err = getPublicSensorsVerbose(sensors)) == SC_OK) {
	    std::string w("^");
	    while (*wildcard != 0) {
		switch (*wildcard) {
		    case '.':
		    case '[':
		    case '\\':
		    case '^':
		    case '$':
			w.append("\\");
			w.append(wildcard, 1);
			break;
		    case '?':
			w.append(".");
			break;
		    case '*':
			w.append(".*");
			break;
		    default:
			w.append(wildcard, 1);
			break;
		}
		wildcard++;
	    }
	    
	    boost::regex r(w, boost::regex::basic);
	    auto it = sensors.begin();
	    while(it != sensors.end()) {
		if (!boost::regex_match(it->name, r)) {
		    it = sensors.erase(it);
		} else {
		    it++;
		}
	    }
	    
	    if (sensors.size() > 0) {
		return SC_OK;
	    } else {
		return SC_UNKNOWNSENSOR;
	    }
	} else {
	    return err;
	}
    }
}

840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
SCError SensorConfigImpl::isVirtual(bool& isVirtual, std::string publicName)
{
  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

  /* Read the virtual field from the database */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const CassPrepared* prepared = nullptr;
  const char* query = "SELECT virtual FROM " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " WHERE name = ? ;";

  future = cass_session_prepare(session, query);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
      connection->printError(future);
      cass_future_free(future);
      return SC_UNKNOWNERROR;
  } else {
      prepared = cass_future_get_prepared(future);
  }
  cass_future_free(future);

  statement = cass_prepared_bind(prepared);
  cass_statement_bind_string_by_name(statement, "name", publicName.c_str());

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
      connection->printError(future);
  } else {
      const CassResult* result = cass_future_get_result(future);
      CassIterator* iterator = cass_iterator_from_result(result);

      if (cass_iterator_next(iterator)) {
          cass_bool_t isVirtual_;
          const CassRow* row = cass_iterator_get_row(iterator);
          cass_value_get_bool(cass_row_get_column_by_name(row, "virtual"), &isVirtual_);
          isVirtual = isVirtual_ ? true : false;
      }
      else {
          return SC_UNKNOWNSENSOR;
      }

      cass_result_free(result);
      cass_iterator_free(iterator);
  }

  cass_future_free(future);
  cass_statement_free(statement);
  cass_prepared_free(prepared);
  return SC_OK;
}

Axel Auweter's avatar
Axel Auweter committed
900
901
902
903
904
905
906
SCError SensorConfigImpl::getSensorPattern(std::string& pattern, std::string publicName)
{
  /* Check if the session is valid */
  if (!session) {
      return SC_INVALIDSESSION;
  }

907
908
909
910
911
912
913
914
915
916
  /* Ensure that the public sensor is not virtual */
  bool virt;
  SCError err = isVirtual(virt, publicName);
  if (err != SC_OK) {
      return err;
  }
  if (virt) {
      return SC_WRONGTYPE;
  }

Axel Auweter's avatar
Axel Auweter committed
917
918
919
920
921
922
923
924
925
926
927
928
929
  /* Read the Pattern string from the database */
  CassError rc = CASS_OK;
  CassStatement* statement = nullptr;
  CassFuture* future = nullptr;
  const CassPrepared* prepared = nullptr;
  const char* query = "SELECT pattern FROM " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " WHERE name = ? ;";

  future = cass_session_prepare(session, query);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
      connection->printError(future);
930
931
      cass_future_free(future);
      return SC_UNKNOWNERROR;
Axel Auweter's avatar
Axel Auweter committed
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
  } else {
      prepared = cass_future_get_prepared(future);
  }
  cass_future_free(future);

  statement = cass_prepared_bind(prepared);
  cass_statement_bind_string_by_name(statement, "name", publicName.c_str());

  future = cass_session_execute(session, statement);
  cass_future_wait(future);

  rc = cass_future_error_code(future);
  if (rc != CASS_OK) {
      connection->printError(future);
  } else {
      const CassResult* result = cass_future_get_result(future);
      CassIterator* iterator = cass_iterator_from_result(result);

      if (cass_iterator_next(iterator)) {
          const char* pattern_cstr;
          size_t      pattern_len;
          const CassRow* row = cass_iterator_get_row(iterator);
          cass_value_get_string(cass_row_get_column_by_name(row, "pattern"), &pattern_cstr, &pattern_len);
          pattern = std::string(pattern_cstr, pattern_len);
      }
      else {
          return SC_UNKNOWNSENSOR;
      }

      cass_result_free(result);
      cass_iterator_free(iterator);
  }

  cass_future_free(future);
  cass_statement_free(statement);
  cass_prepared_free(prepared);
  return SC_OK;
}

SCError SensorConfigImpl::getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern)
{
  /* Tiny hack to call the long version of this function */
974
975
  TimeStamp start((uint64_t)0x0);
  TimeStamp end((uint64_t)0x260DD31906D70000);
Axel Auweter's avatar
Axel Auweter committed
976
977
978
  return getSensorListForPattern(sensorIds, pattern, start, end);
}

979
SCError SensorConfigImpl::getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern, TimeStamp start, TimeStamp end)
Axel Auweter's avatar
Axel Auweter committed
980
981
982
983
984
985
986
987
988
989
990
{
  /* Clear the list of sensorIds */
  sensorIds.clear();

  /* Strip all slashes from publishedSensorName */
  pattern.erase(std::remove(pattern.begin(), pattern.end(), '/'), pattern.end());

  /* Calculate lower and upper boundaries for the expansion of the pattern */
  std::string low  = pattern;
  std::string high = pattern;
  if (pattern.find("*") != std::string::npos) {
Michael Ott's avatar
Michael Ott committed
991
992
      low.replace(pattern.find("*"), 1, 29-pattern.length(), '0');
      high.replace(pattern.find("*"), 1, 29-pattern.length(), 'F');
Axel Auweter's avatar
Axel Auweter committed
993
994
  }

Michael Ott's avatar
Michael Ott committed
995
996
997
998
  /* The week stamp is imprinted on the lower 4 bits */
  low.append("0000");
  high.append("FFFF");
	
Axel Auweter's avatar
Axel Auweter committed
999
1000
  SensorId lowId, highId;
  if (!lowId.mqttTopicConvert(low)) {