sensorconfig.cpp 53.3 KB
Newer Older
1
2
//================================================================================
// Name        : sensorconfig.cpp
3
// Author      : Axel Auweter, Daniele Tafani
Micha Müller's avatar
Micha Müller committed
4
// Contact     : info@dcdb.it
5
// Copyright   : Leibniz Supercomputing Centre
Michael Ott's avatar
Michael Ott committed
6
// Description : C++ API implementation for configuring libdcdb public sensors.
7
8
9
10
//================================================================================

//================================================================================
// This file is part of DCDB (DataCenter DataBase)
11
// Copyright (C) 2011-2019 Leibniz Supercomputing Centre
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//
// 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
27
28
29

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

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

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

44
45
using namespace DCDB;

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

65
PublicSensor::PublicSensor (const PublicSensor &copy)
Axel Auweter's avatar
Axel Auweter committed
66
{
67
68
69
70
71
72
73
74
75
    name = copy.name;
    is_virtual = copy.is_virtual;
    pattern = copy.pattern;
    scaling_factor = copy.scaling_factor;
    unit = copy.unit;
    sensor_mask = copy.sensor_mask;
    expression = copy.expression;
    v_sensorid = copy.v_sensorid;
    t_zero = copy.t_zero;
76
    interval = copy.interval;
77
    operations = copy.operations;
78
    ttl = copy.ttl;
Axel Auweter's avatar
Axel Auweter committed
79
80
81
82
83
84
}


/*
 * SensorConfig functions
 */
85
86
87

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

Axel Auweter's avatar
Axel Auweter committed
91
92
SCError SensorConfig::publishSensor(const char* publicName, const char* sensorPattern)
{
93
    return impl->publishSensor(publicName, sensorPattern);
Axel Auweter's avatar
Axel Auweter committed
94
95
}

96
97
98
99
100
SCError SensorConfig::publishSensor(const PublicSensor& sensor)
{
    return impl->publishSensor(sensor);
}

101
102
103
104
105
SCError SensorConfig::publishSensor(const SensorMetadata& sensor)
{
    return impl->publishSensor(sensor);
}

106
SCError SensorConfig::publishVirtualSensor(const char* publicName, const char* vSensorExpression, const char* vSensorId, TimeStamp tZero, uint64_t interval)
107
{
108
    return impl->publishVirtualSensor(publicName, vSensorExpression, vSensorId, tZero, interval);
109
110
}

111
112
SCError SensorConfig::unPublishSensor(const char* publicName)
{
113
    return impl->unPublishSensor(publicName);
114
115
}

116
SCError SensorConfig::getPublicSensorNames(std::list<std::string>& publicSensors)
Axel Auweter's avatar
Axel Auweter committed
117
{
118
    return impl->getPublicSensorNames(publicSensors);
119
120
}

121
SCError SensorConfig::getPublicSensorsVerbose(std::list<PublicSensor>& publicSensors)
122
{
123
    return impl->getPublicSensorsVerbose(publicSensors);
124
125
}

126
SCError SensorConfig::getPublicSensorByName(PublicSensor& sensor, const char* publicName)
127
{
128
    return impl->getPublicSensorByName(sensor, publicName);
Axel Auweter's avatar
Axel Auweter committed
129
130
}

131
132
SCError SensorConfig::getPublicSensorByPattern(PublicSensor& sensor, const char* pattern)
{
133
    return impl->getPublicSensorByPattern(sensor, pattern);
134
135
}

136
137
138
139
140
SCError SensorConfig::getPublicSensorsByWildcard(std::list<PublicSensor>& sensors, const char* wildcard)
{
    return impl->getPublicSensorsByWildcard(sensors, wildcard);
}

141
142
SCError SensorConfig::isVirtual(bool& isVirtual, std::string publicName)
{
143
    return impl->isVirtual(isVirtual, publicName);
144
145
}

Axel Auweter's avatar
Axel Auweter committed
146
147
SCError SensorConfig::getSensorPattern(std::string& pattern, std::string publicName)
{
148
    return impl->getSensorPattern(pattern, publicName);
Axel Auweter's avatar
Axel Auweter committed
149
150
151
152
}

SCError SensorConfig::getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern)
{
153
    return impl->getSensorListForPattern(sensorIds, pattern);
Axel Auweter's avatar
Axel Auweter committed
154
155
}

156
SCError SensorConfig::getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern, TimeStamp start, TimeStamp end)
Axel Auweter's avatar
Axel Auweter committed
157
{
158
    return impl->getSensorListForPattern(sensorIds, pattern, start, end);
Axel Auweter's avatar
Axel Auweter committed
159
160
}

161
SCError SensorConfig::setSensorScalingFactor(std::string publicName, double scalingFactor) {
162
    return impl->setSensorScalingFactor(publicName, scalingFactor);
163
164
165
166
}

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

170
SCError SensorConfig::setSensorMask(std::string publicName, uint64_t mask)
171
{
172
    return impl->setSensorMask(publicName, mask);
173
174
}

175
176
177
178
179
SCError SensorConfig::setOperations(std::string publicName, std::string operations)
{
    return impl->setOperations(publicName,operations);
}

180
181
182
183
184
SCError SensorConfig::setTimeToLive(std::string publicName, uint64_t ttl)
{
    return impl->setTimeToLive(publicName, ttl);
}

185
186
SCError SensorConfig::setVirtualSensorExpression(std::string publicName, std::string expression)
{
187
    return impl->setVirtualSensorExpression(publicName, expression);
188
189
190
191
}

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

195
SCError SensorConfig::setSensorInterval(std::string publicName, uint64_t interval)
196
{
197
    return impl->setSensorInterval(publicName, interval);
198
199
}

200
SensorConfig::SensorConfig(Connection* conn)
Axel Auweter's avatar
Axel Auweter committed
201
{
202
203
    /* Allocate impl object */
    impl = new SensorConfigImpl(conn);
Axel Auweter's avatar
Axel Auweter committed
204
205
206
207
}

SensorConfig::~SensorConfig()
{
208
209
210
    if (impl) {
        delete impl;
    }
Axel Auweter's avatar
Axel Auweter committed
211
212
213
}

/*
214
 * SensorConfigImpl protected members and functions
Axel Auweter's avatar
Axel Auweter committed
215
216
217
218
219
220
221
222
223
224
 */

/*
 * 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)
{
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    unsigned int wildcards, seps;

    /* Iterate through the string and validate/count the characters */
    wildcards = 0, seps = 0;
    for (unsigned int c = 0; c < strlen(sensorPattern); c++) {
        switch (sensorPattern[c]) {
            case '*':
                wildcards++;
                break;
            case '/':
                seps++;
                break;
                /* Everything else is allowed */
            default:
                break;
        }
    }
Axel Auweter's avatar
Axel Auweter committed
242

243
244
245
    /* More than one wildcard is not allowed */
    if (wildcards > 1)
        return false;
Axel Auweter's avatar
Axel Auweter committed
246

247
248
    if (strlen(sensorPattern) - seps > MAX_PATTERN_LENGTH)
        return false;
Axel Auweter's avatar
Axel Auweter committed
249

250
251
    /* Looks good */
    return true;
Axel Auweter's avatar
Axel Auweter committed
252
253
}

254
255
256
257
258
/*
 * Validate the public name of a sensor.
 */
bool SensorConfigImpl::validateSensorPublicName(std::string publicName)
{
259
    return true;
260
}
Axel Auweter's avatar
Axel Auweter committed
261
262
263
264

/*
 * SensorConfigImpl public functions
 */
265
266
267

SCError SensorConfigImpl::loadCache()
{
268
269
270
271
272
273
274
275
276
277
278
279
280
    sensorList.clear();
    sensorMapByName.clear();
    sensorMapByPattern.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)));
            sensorMapByPattern.insert(std::make_pair(s.pattern, std::ref(s)));
        }
        return SC_OK;
281
282
283
    }
}

Axel Auweter's avatar
Axel Auweter committed
284
SCError SensorConfigImpl::publishSensor(std::string publicName, std::string sensorPattern)
Axel Auweter's avatar
Axel Auweter committed
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
    /* Check if the pattern matches the requirements */
    SensorId sid;
    if (!validateSensorPattern(sensorPattern.c_str()) || !sid.mqttTopicConvert(sensorPattern)) {
        return SC_INVALIDPATTERN;
    }

    /* 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;
    }
    
    /* Insert the entry */
    CassError rc = CASS_OK;
    CassStatement* statement = nullptr;
    CassFuture* future = nullptr;
    const CassPrepared* prepared = nullptr;
    const char* query = "INSERT INTO " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " (name, pattern, virtual) VALUES (?,?, FALSE);";

    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);
    }

Axel Auweter's avatar
Axel Auweter committed
321
322
    cass_future_free(future);

323
    statement = cass_prepared_bind(prepared);
Axel Auweter's avatar
Axel Auweter committed
324

325
326
    cass_statement_bind_string_by_name(statement, "name", publicName.c_str());
    cass_statement_bind_string_by_name(statement, "pattern", sid.getId().c_str());
Axel Auweter's avatar
Axel Auweter committed
327

328
329
    future = cass_session_execute(session, statement);
    cass_future_wait(future);
Axel Auweter's avatar
Axel Auweter committed
330

331
332
333
    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
Axel Auweter's avatar
Axel Auweter committed
334

335
336
        cass_prepared_free(prepared);
        cass_future_free(future);
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
        cass_statement_free(statement);

        return SC_UNKNOWNERROR;
    }

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

    return SC_OK;
}

SCError SensorConfigImpl::publishSensor(const PublicSensor& sensor)
{
    /* Check if the pattern matches the requirements */
    SensorId sid;
    if (!validateSensorPattern(sensor.pattern.c_str()) || !sid.mqttTopicConvert(sensor.pattern)) {
        return SC_INVALIDPATTERN;
    }

    /* Check if the publicName is valid */
    if (!validateSensorPublicName(sensor.name.c_str())) {
        return SC_INVALIDPUBLICNAME;
    }

    /* 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;
    const char* query = "INSERT INTO " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " (name, pattern, virtual, scaling_factor, unit, sensor_mask, operations, interval, ttl) VALUES (?,?, FALSE, ?, ?, ?, ?, ?, ?);";

    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", sensor.name.c_str());
    cass_statement_bind_string_by_name(statement, "pattern", sid.getId().c_str());
    cass_statement_bind_double_by_name(statement, "scaling_factor", sensor.scaling_factor);
    cass_statement_bind_string_by_name(statement, "unit", sensor.unit.c_str());
    cass_statement_bind_int64_by_name(statement, "sensor_mask", sensor.sensor_mask);
    cass_statement_bind_string_by_name(statement, "operations", sensor.operations.c_str());
    cass_statement_bind_int64_by_name(statement, "interval", sensor.interval);
    cass_statement_bind_int64_by_name(statement, "ttl", sensor.ttl);

    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);
408
409
410
411
412
413
414
415
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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
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
        cass_statement_free(statement);

        return SC_UNKNOWNERROR;
    }

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

    return SC_OK;
}

SCError SensorConfigImpl::publishSensor(const SensorMetadata& sensor)
{
    /* Check if the pattern matches the requirements */
    SensorId sid;
    if (!sensor.getPattern() || !validateSensorPattern(sensor.getPattern()->c_str()) || !sid.mqttTopicConvert(*sensor.getPattern())) {
        return SC_INVALIDPATTERN;
    }

    /* Check if the publicName is valid */
    if (!sensor.getPublicName() || !validateSensorPublicName(sensor.getPublicName()->c_str())) {
        return SC_INVALIDPUBLICNAME;
    }

    /* 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;
    
    std::string queryBuf = "INSERT INTO " + std::string(CONFIG_KEYSPACE_NAME) + "." + std::string(CF_PUBLISHEDSENSORS) + " (name, pattern, virtual";
    std::string valuesBuf = ") VALUES (?, ?, FALSE";
    std::string closingBuf = ");";
            
    if(sensor.getScale()) {
        queryBuf += ", scaling_factor";
        valuesBuf += ", ?";
    }
    if(sensor.getUnit()) {
        queryBuf += ", unit";
        valuesBuf += ", ?";
    }
    if(sensor.getIntegrable() || sensor.getMonotonic()) {
        queryBuf += ", sensor_mask";
        valuesBuf += ", ?";
    }
    if(sensor.getOperations()) {
        queryBuf += ", operations";
        valuesBuf += ", ?";
    }
    if(sensor.getInterval()) {
        queryBuf += ", interval";
        valuesBuf += ", ?";
    }
    if(sensor.getTTL()) {
        queryBuf += ", ttl";
        valuesBuf += ", ?";
    }
    
    std::string query = queryBuf + valuesBuf + closingBuf;

    future = cass_session_prepare(session, query.c_str());
    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", sensor.getPublicName()->c_str());
    cass_statement_bind_string_by_name(statement, "pattern", sid.getId().c_str());
    
    if(sensor.getScale())
        cass_statement_bind_double_by_name(statement, "scaling_factor", *sensor.getScale());
    if(sensor.getUnit())
        cass_statement_bind_string_by_name(statement, "unit", sensor.getUnit()->c_str());
    if(sensor.getOperations())
        cass_statement_bind_string_by_name(statement, "operations", sensor.getOperations()->c_str());
    if(sensor.getInterval())
        cass_statement_bind_int64_by_name(statement, "interval", *sensor.getInterval());
    if(sensor.getTTL())
        cass_statement_bind_int64_by_name(statement, "ttl", *sensor.getTTL());
    if(sensor.getIntegrable() || sensor.getMonotonic()) {
        uint64_t sensorMask = 0;
        if(sensor.getIntegrable())
            sensorMask = sensorMask | INTEGRABLE;
        if(sensor.getMonotonic())
            sensorMask = sensorMask | MONOTONIC;
        cass_statement_bind_int64_by_name(statement, "sensor_mask", sensorMask);
    }

    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);
521
522
523
524
        cass_statement_free(statement);

        return SC_UNKNOWNERROR;
    }
525
526
527
528
529

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

530
    return SC_OK;
531
532
}

533
SCError SensorConfigImpl::publishVirtualSensor(std::string publicName, std::string vSensorExpression, std::string vSensorId, TimeStamp tZero, uint64_t interval)
534
{
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
    /* 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;
    }

    /* Validate vSensorExpression */
    try {
        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;
        }
    }
    catch (std::exception& e) {
        std::cout << e.what();
        return SC_INVALIDEXPRESSION;
    }

    /* Check if the vSensorId is valid */
    SensorId vSensor;
    if (!vSensor.mqttTopicConvert(vSensorId)) {
        return SC_INVALIDVSENSORID;
    }

    /* Insert the entry */
    CassError rc = CASS_OK;
    CassStatement* statement = nullptr;
    CassFuture* future = nullptr;
    const CassPrepared* prepared = nullptr;
573
    const char* query = "INSERT INTO " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " (name, expression, vsensorid, tzero, interval, virtual) VALUES (?,?,?,?,?,TRUE);";
574
575
576
577
578
579
580
581
582
583
584
585
586

    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);
    }

587
588
    cass_future_free(future);

589
    statement = cass_prepared_bind(prepared);
590

591
592
593
594
    cass_statement_bind_string_by_name(statement, "name", publicName.c_str());
    cass_statement_bind_string_by_name(statement, "expression", vSensorExpression.c_str());
    cass_statement_bind_string_by_name(statement, "vsensorid", vSensorId.c_str());
    cass_statement_bind_int64_by_name(statement, "tzero", tZero.getRaw());
595
    cass_statement_bind_int64_by_name(statement, "interval", interval);
596

597
598
    future = cass_session_execute(session, statement);
    cass_future_wait(future);
599

600
601
602
    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
603

604
605
606
607
608
609
        cass_prepared_free(prepared);
        cass_future_free(future);
        cass_statement_free(statement);

        return SC_UNKNOWNERROR;
    }
610
611
612
613
614

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

615
    return SC_OK;
616
617
618
}


619
620
SCError SensorConfigImpl::unPublishSensor(std::string publicName)
{
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
    /* 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);
    }

645
646
    cass_future_free(future);

647
648
649
    statement = cass_prepared_bind(prepared);

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

651
652
    future = cass_session_execute(session, statement);
    cass_future_wait(future);
653

654
655
656
    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
657

658
659
660
        cass_prepared_free(prepared);
        cass_future_free(future);
        cass_statement_free(statement);
661

662
663
        return SC_UNKNOWNERROR;
    }
Axel Auweter's avatar
Axel Auweter committed
664
665
666
667
668

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

669
    return SC_OK;
Axel Auweter's avatar
Axel Auweter committed
670
671
}

672
673
SCError SensorConfigImpl::getPublicSensorNames(std::list<std::string>& publicSensors)
{
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
    /* 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;
722
723
}

724
SCError SensorConfigImpl::getPublicSensorsVerbose(std::list<PublicSensor>& publicSensors)
Axel Auweter's avatar
Axel Auweter committed
725
{
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
    /* 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 * 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;
            cass_bool_t is_virtual;
            const char* pattern;
            size_t      pattern_len;
            double      scaling_factor;
            const char* unit;
            size_t      unit_len;
            int64_t     sensor_mask;
            const char* expression;
            size_t      expression_len;
            const char* vsensorid;
            size_t      vsensorid_len;
            int64_t     tzero;
770
771
            int64_t     interval;
            int64_t     ttl;
772
773
            const char* operations;
            size_t      operations_len;
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
            PublicSensor sensor;

            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;
            }
            if (cass_value_get_bool(cass_row_get_column_by_name(row, "virtual"), &is_virtual) != CASS_OK) {
                is_virtual = cass_false;
            }
            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;
            }
            if (cass_value_get_int64(cass_row_get_column_by_name(row, "sensor_mask"), &sensor_mask) != CASS_OK) {
                sensor_mask = 0;
            }
            if (cass_value_get_string(cass_row_get_column_by_name(row, "expression"), &expression, &expression_len) != CASS_OK) {
                expression = ""; expression_len = 0;
            }
            if (cass_value_get_string(cass_row_get_column_by_name(row, "vsensorid"), &vsensorid, &vsensorid_len) != CASS_OK) {
                vsensorid = ""; vsensorid_len = 0;
            }
            if (cass_value_get_int64(cass_row_get_column_by_name(row, "tzero"), &tzero) != CASS_OK) {
                tzero = 0;
            }
805
806
807
808
809
            if (cass_value_get_int64(cass_row_get_column_by_name(row, "interval"), &interval) != CASS_OK) {
                interval = 0;
            }
            if (cass_value_get_int64(cass_row_get_column_by_name(row, "ttl"), &ttl) != CASS_OK) {
                ttl = 0;
810
            }
811
812
813
            if (cass_value_get_string(cass_row_get_column_by_name(row, "operations"), &operations, &operations_len) != CASS_OK) {
                operations = ""; operations_len = 0;
            }
814
815
816
817
818
819
820
821
822
823

            sensor.name = std::string(name, name_len);
            sensor.is_virtual = is_virtual == cass_true ? true : false;
            sensor.pattern = std::string(pattern, pattern_len);
            sensor.scaling_factor = scaling_factor;
            sensor.unit = std::string(unit, unit_len);
            sensor.sensor_mask = sensor_mask;
            sensor.expression = std::string(expression, expression_len);
            sensor.v_sensorid = std::string(vsensorid, vsensorid_len);
            sensor.t_zero = tzero;
824
825
            sensor.interval = interval;
            sensor.ttl = ttl;
826
            sensor.operations = operations;
827
828
829
830
831
832
833
834
835
836

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

    cass_future_free(future);
    cass_statement_free(statement);
    return SC_OK;
Axel Auweter's avatar
Axel Auweter committed
837
838
}

839
SCError SensorConfigImpl::getPublicSensorByName(PublicSensor& sensor, const char* publicName)
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
900
901
902
903
904
    /* Check if the sensor definition is already in the cache */
    SensorMap_t::const_iterator got = sensorMapByName.find(publicName);
    if (got != sensorMapByName.end()) {
        sensor = got->second;
        return SC_OK;
    }

    /* Not in cache - query the data base */
    /* 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;
            cass_bool_t is_virtual;
            const char* pattern;
            size_t      pattern_len;
            double      scaling_factor;
            const char* unit;
            size_t      unit_len;
            int64_t     sensor_mask;
            const char* expression;
            size_t      expression_len;
            const char* vsensorid;
            size_t      vsensorid_len;
            int64_t     tzero;
905
906
            int64_t     interval;
            int64_t     ttl;
907
908
            const char* operations;
            size_t      operations_len;
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938

            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;
            }
            if (cass_value_get_bool(cass_row_get_column_by_name(row, "virtual"), &is_virtual) != CASS_OK) {
                is_virtual = cass_false;
            }
            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;
            }
            if (cass_value_get_int64(cass_row_get_column_by_name(row, "sensor_mask"), &sensor_mask) != CASS_OK) {
                sensor_mask = 0;
            }
            if (cass_value_get_string(cass_row_get_column_by_name(row, "expression"), &expression, &expression_len) != CASS_OK) {
                expression = ""; expression_len = 0;
            }
            if (cass_value_get_string(cass_row_get_column_by_name(row, "vsensorid"), &vsensorid, &vsensorid_len) != CASS_OK) {
                vsensorid = ""; vsensorid_len = 0;
            }
            if (cass_value_get_int64(cass_row_get_column_by_name(row, "tzero"), &tzero) != CASS_OK) {
                tzero = 0;
            }
939
940
941
942
943
            if (cass_value_get_int64(cass_row_get_column_by_name(row, "interval"), &interval) != CASS_OK) {
                interval = 0;
            }
            if (cass_value_get_int64(cass_row_get_column_by_name(row, "ttl"), &ttl) != CASS_OK) {
                ttl = 0;
944
            }
945
946
947
            if (cass_value_get_string(cass_row_get_column_by_name(row, "operations"), &operations, &operations_len) != CASS_OK) {
                operations = ""; operations_len = 0;
            }
948
949
950
951
952
953
954
955
956
957

            sensor.name = std::string(name, name_len);
            sensor.is_virtual = is_virtual == cass_true ? true : false;
            sensor.pattern = std::string(pattern, pattern_len);
            sensor.scaling_factor = scaling_factor;
            sensor.unit = std::string(unit, unit_len);
            sensor.sensor_mask = sensor_mask;
            sensor.expression = std::string(expression, expression_len);
            sensor.v_sensorid = std::string(vsensorid, vsensorid_len);
            sensor.t_zero = tzero;
958
959
            sensor.interval = interval;
            sensor.ttl = ttl;
960
            sensor.operations = operations;
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982

            /* Add to sensorPropertyCache for later use */
            sensorList.push_back(sensor);
            sensorMapByName.insert(std::make_pair(publicName, std::ref(sensorList.back())));
            sensorMapByPattern.insert(std::make_pair(sensor.pattern, std::ref(sensorList.back())));
        }
        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);
    cass_prepared_free(prepared);
    return SC_OK;
983
984
}

985
986
SCError SensorConfigImpl::getPublicSensorByPattern(PublicSensor& sensor, const char* pattern)
{
987
988
989
990
991
992
993
994
995
996
997
    if (sensorList.size() == 0) {
        loadCache();
    }

    SensorMap_t::const_iterator got = sensorMapByPattern.find(pattern);
    if (got != sensorMapByPattern.end()) {
        sensor = got->second;
        return SC_OK;
    } else {
        return SC_UNKNOWNSENSOR;
    }
998
999
}

1000
1001
1002
SCError SensorConfigImpl::getPublicSensorsByWildcard(std::list<PublicSensor>& sensors, const char* wildcard)
{
    SCError err = SC_OK;
1003

1004
    if (strpbrk(wildcard, "*?") == NULL) {
1005
1006
1007
1008
1009
        PublicSensor sen;
        if ((err = getPublicSensorByName(sen, wildcard)) == SC_OK) {
            sensors.push_back(sen);
        }
        return err;
1010
    } else {
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
        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;
        }
1054
1055
1056
    }
}

1057
1058
SCError SensorConfigImpl::isVirtual(bool& isVirtual, std::string publicName)
{
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
    /* 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;
1115
1116
}

Axel Auweter's avatar
Axel Auweter committed
1117
1118
SCError SensorConfigImpl::getSensorPattern(std::string& pattern, std::string publicName)
{
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
    /* Check if the session is valid */
    if (!session) {
        return SC_INVALIDSESSION;
    }

    /* 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;
    }

    /* 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);
        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)) {
            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;
Axel Auweter's avatar
Axel Auweter committed
1186
1187
1188
1189
}

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

1196
SCError SensorConfigImpl::getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern, TimeStamp start, TimeStamp end)
Axel Auweter's avatar
Axel Auweter committed
1197
{
1198
1199
    /* Clear the list of sensorIds */
    sensorIds.clear();
Axel Auweter's avatar
Axel Auweter committed
1200

1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
    /* Strip all slashes from publishedSensorName */
    //pattern.erase(std::remove(pattern.begin(), pattern.end(), '/'), pattern.end());
    
    uint16_t wsStart = start.getWeekstamp();
    uint16_t wsEnd   = end.getWeekstamp();
    
    for(uint16_t ws=wsStart; ws<=wsEnd; ws++) {
        SensorId sensor;
        sensor.setId(pattern);
        sensor.setRsvd(ws);
        sensorIds.push_back(sensor);
    }
    
    return SC_OK;
    
//    /* 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) {
//        low.replace(pattern.find("*"), 1, MAX_PATTERN_LENGTH-pattern.length(), 0);
//        high.replace(pattern.find("*"), 1, MAX_PATTERN_LENGTH-pattern.length(), 127);
//    }
//
//    SensorId lowId, highId;
//    if (!lowId.mqttTopicConvert(low)) {
//        return SC_INVALIDPATTERN;
//    }
//    if (!highId.mqttTopicConvert(high)) {
//        return SC_INVALIDPATTERN;
//    }
//
Axel Auweter's avatar
Axel Auweter committed
1238
1239
//  std::cout << "Lower boundary for sensor scan: " << std::hex << std::setfill('0') << std::setw(16) << lowId.raw[0] << " " << std::hex << std::setfill('0') << std::setw(16) << lowId.raw[1] << std::endl;
//  std::cout << "Upper boundary for sensor scan: " << std::hex << std::setfill('0') << std::setw(16) << highId.raw[0] << " " << std::hex << std::setfill('0') << std::setw(16) << highId.raw[1] << std::endl;
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
//
//    /* Query the database to see which raw sensors actually exist in the interval between low and high */
//    CassError rc = CASS_OK;
//    CassStatement* statement = nullptr;
//    CassFuture* future = nullptr;
//    const CassPrepared* prepared = nullptr;
//    const char* query = "SELECT DISTINCT sid,ws FROM " KEYSPACE_NAME "." CF_SENSORDATA " WHERE TOKEN(sid) >= TOKEN(?) and TOKEN(sid) <= TOKEN(?) and ws >= ? and ws <= ?;";
//
//    future = cass_session_prepare(session, query);
//    cass_future_wait(future);
//
//    rc = cass_future_error_code(future);
//    if (rc != CASS_OK) {
//        connection->printError(future);
//        return SC_UNKNOWNERROR;
//    }
//
//    prepared = cass_future_get_prepared(future);
//    cass_future_free(future);
//
//    statement = cass_prepared_bind(prepared);
//
//    cass_statement_bind_string(statement, 0, low.c_str());
//    cass_statement_bind_string(statement, 1, high.c_str());
//    cass_statement_bind_int16(statement, 2, (cass_int16_t)start.getWeekstamp());
//    cass_statement_bind_int16(statement, 3, (cass_int16_t)end.getWeekstamp());
//
//    future = cass_session_execute(session, statement);
//    cass_future_wait(future);
//
//    rc = cass_future_error_code(future);
//    if (rc != CASS_OK) {
//        connection->printError(future);
//        return SC_UNKNOWNERROR;
//    }
//
//    const CassResult* result = cass_future_get_result(future);
//    cass_future_free(future);
//
//    CassIterator* iterator = cass_iterator_from_result(result);
//    while (cass_iterator_next(iterator)) {
//        const CassRow* row = cass_iterator_get_row(iterator);
//        const char* res;
//        size_t       res_len;
//        cass_int16_t res_ws;
//        cass_value_get_string(cass_row_get_column_by_name(row, "sid"), &res, &res_len);
//        cass_value_get_int16(cass_row_get_column_by_name(row, "ws"), &res_ws);
//
//        SensorId sensor;
//        std::string id(res, res_len);
//        sensor.setId(id);
//
//        /* Check if the sensorId matches the pattern and append to result */
//        if (sensor.patternMatch(pattern)) {
//            /* Only append if within the weekstamp window */
//            if ((res_ws >= start.getWeekstamp()) && (res_ws <= end.getWeekstamp())) {
//                sensorIds.push_back(sensor);
//            }
//        }
//    }
//    cass_result_free(result);
//    cass_iterator_free(iterator);
//    cass_statement_free(statement);
//    cass_prepared_free(prepared);
//
//    return SC_OK;
Axel Auweter's avatar
Axel Auweter committed
1306
1307
}

1308
1309
SCError SensorConfigImpl::setSensorScalingFactor(std::string publicName, double scalingFactor)
{
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
    SCError error = SC_UNKNOWNERROR;

    /* Check the sensorconfig whether the given public sensor has the integrable flag set to true */
    CassError rc = CASS_OK;
    CassStatement* statement = nullptr;
    CassFuture* future = nullptr;
    const CassPrepared* prepared = nullptr;
    const char* query = "UPDATE " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " SET scaling_factor = ? 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);
        return SC_UNKNOWNERROR;
    }
1327

1328
1329
    prepared = cass_future_get_prepared(future);
    cass_future_free(future);
1330

1331
    statement = cass_prepared_bind(prepared);
1332

1333
1334
    cass_statement_bind_double(statement, 0, scalingFactor);
    cass_statement_bind_string(statement, 1, publicName.c_str());
1335

1336
1337
    future = cass_session_execute(session, statement);
    cass_future_wait(future);
1338

1339
1340
1341
1342
1343
1344
1345
1346
    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
        error = SC_UNKNOWNERROR;
    }
    else {
        error = SC_OK;
    }
1347

1348
1349
    cass_statement_free(statement);
    cass_prepared_free(prepared);
1350

1351
    return error;
1352
1353
1354
1355
}

SCError SensorConfigImpl::setSensorUnit(std::string publicName, std::string unit)
{
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
    SCError error = SC_UNKNOWNERROR;

    /* Check the sensorconfig whether the given public sensor has the integrable flag set to true */
    CassError rc = CASS_OK;
    CassStatement* statement = nullptr;
    CassFuture* future = nullptr;
    const CassPrepared* prepared = nullptr;
    const char* query = "UPDATE " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " SET unit = ? 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);
        return SC_UNKNOWNERROR;
    }

    prepared = cass_future_get_prepared(future);
    cass_future_free(future);
1376

1377
    statement = cass_prepared_bind(prepared);
1378

1379
1380
    cass_statement_bind_string(statement, 0, unit.c_str());
    cass_statement_bind_string(statement, 1, publicName.c_str());
1381

1382
1383
    future = cass_session_execute(session, statement);
    cass_future_wait(future);
1384

1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
        error = SC_UNKNOWNERROR;
    }
    else {
        error = SC_OK;
    }

    cass_statement_free(statement);
    cass_prepared_free(prepared);

    return error;
}

SCError SensorConfigImpl::setSensorMask(std::string publicName, uint64_t mask)
{
    SCError error = SC_UNKNOWNERROR;

    /* Check the sensorconfig whether the given public sensor has the integrable flag set to true */
    CassError rc = CASS_OK;
    CassStatement* statement = nullptr;
    CassFuture* future = nullptr;
    const CassPrepared* prepared = nullptr;
    const char* query = "UPDATE " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " SET sensor_mask = ? 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);
        return SC_UNKNOWNERROR;
    }
1419

1420
1421
    prepared = cass_future_get_prepared(future);
    cass_future_free(future);
1422

1423
    statement = cass_prepared_bind(prepared);
1424

1425
1426
    cass_statement_bind_int64(statement, 0, mask);
    cass_statement_bind_string(statement, 1, publicName.c_str());
1427

1428
1429
    future = cass_session_execute(session, statement);
    cass_future_wait(future);
1430

1431
1432
1433
1434
1435
1436
1437
1438
    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
        error = SC_UNKNOWNERROR;
    }
    else {
        error = SC_OK;
    }
1439

1440
1441
1442
1443
    cass_statement_free(statement);
    cass_prepared_free(prepared);

    return error;
1444
1445
}

1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
SCError SensorConfigImpl::setOperations(std::string publicName, std::string operations)
{
    SCError error = SC_UNKNOWNERROR;
    
    CassError rc = CASS_OK;
    CassStatement* statement = nullptr;
    CassFuture* future = nullptr;
    const CassPrepared* prepared = nullptr;
    const char* query = "UPDATE " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " SET operations = ? 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);
        return SC_UNKNOWNERROR;
    }
    
    prepared = cass_future_get_prepared(future);
    cass_future_free(future);
    
    statement = cass_prepared_bind(prepared);
    
    cass_statement_bind_string(statement, 0, operations.c_str());
    cass_statement_bind_string(statement, 1, 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);
        error = SC_UNKNOWNERROR;
    }
    else {
        error = SC_OK;
    }
    
    cass_statement_free(statement);
    cass_prepared_free(prepared);
    
    return error;
}
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534

SCError SensorConfigImpl::setTimeToLive(std::string publicName, uint64_t ttl)
{
    SCError error = SC_UNKNOWNERROR;

    CassError rc = CASS_OK;
    CassStatement* statement = nullptr;
    CassFuture* future = nullptr;
    const CassPrepared* prepared = nullptr;
    const char* query = "UPDATE " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " SET ttl = ? 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);
        return SC_UNKNOWNERROR;
    }

    prepared = cass_future_get_prepared(future);
    cass_future_free(future);

    statement = cass_prepared_bind(prepared);

    cass_statement_bind_int64(statement, 0, ttl);
    cass_statement_bind_string(statement, 1, 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);
        error = SC_UNKNOWNERROR;
    }
    else {
        error = SC_OK;
    }

    cass_statement_free(statement);
    cass_prepared_free(prepared);

    return error;
}
1535
                                                  
1536
SCError SensorConfigImpl::setVirtualSensorExpression(std::string publicName, std::string expression)
1537
{
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
    /* Check if the session is valid */
    if (!session) {
        return SC_INVALIDSESSION;
    }

    /* Ensure that the public sensor is virtual */
    bool virt;
    SCError err = isVirtual(virt, publicName);
    if (err != SC_OK) {
        return err;
    }
    if (!virt) {
        return SC_WRONGTYPE;
    }
1552

1553
    /* TODO: Validate expression! */
1554

1555
1556
    /* Update the database with the new expression */
    SCError error = SC_UNKNOWNERROR;
1557

1558
1559
1560
1561
1562
    CassError rc = CASS_OK;
    CassStatement* statement = nullptr;
    CassFuture* future = nullptr;
    const CassPrepared* prepared = nullptr;
    const char* query = "UPDATE " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " SET expression = ? WHERE name = ? ;";
1563

1564
1565
    future = cass_session_prepare(session, query);
    cass_future_wait(future);
1566

1567
1568
1569
1570
1571
    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
        return SC_UNKNOWNERROR;
    }
1572

1573
1574
    prepared = cass_future_get_prepared(future);
    cass_future_free(future);
1575

1576
    statement = cass_prepared_bind(prepared);
1577

1578
1579
    cass_statement_bind_string(statement, 0, expression.c_str());
    cass_statement_bind_string(statement, 1, publicName.c_str());
1580

1581
1582
    future = cass_session_execute(session, statement);
    cass_future_wait(future);
1583

1584
1585
1586
1587
1588
1589
1590
1591
    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
        error = SC_UNKNOWNERROR;
    }
    else {
        error = SC_OK;
    }
Axel Auweter's avatar
Axel Auweter committed