sensorconfig.cpp 59.6 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
61
    interval = 0;
    ttl = 0;
Axel Auweter's avatar
Axel Auweter committed
62
63
}

64
PublicSensor::PublicSensor (const PublicSensor &copy)
Axel Auweter's avatar
Axel Auweter committed
65
{
66
67
68
69
70
71
72
73
74
    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;
75
    interval = copy.interval;
76
    operations = copy.operations;
77
    ttl = copy.ttl;
Axel Auweter's avatar
Axel Auweter committed
78
79
}

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
PublicSensor PublicSensor::metadataToPublicSensor(const SensorMetadata& sm) {
    PublicSensor ps;
    if(sm.getPublicName())
        ps.name = *sm.getPublicName();
    if(sm.getIsVirtual())
        ps.is_virtual = *sm.getIsVirtual();
    if(sm.getPattern())
        ps.pattern = *sm.getPattern();
    if(sm.getUnit())
        ps.unit = *sm.getUnit();
    if(sm.getScale())
        ps.scaling_factor = *sm.getScale();
    if(sm.getTTL())
        ps.ttl = *sm.getTTL();
    if(sm.getInterval())
        ps.interval = *sm.getInterval();
    if(sm.getOperations())
        ps.operations = *sm.getOperations();
    uint64_t sensorMask = 0;
    if(sm.getIntegrable() && *sm.getIntegrable())
        sensorMask = sensorMask | INTEGRABLE;
    if(sm.getMonotonic() && *sm.getMonotonic())
        sensorMask = sensorMask | MONOTONIC;
    ps.sensor_mask = sensorMask;
    return ps;
}

SensorMetadata PublicSensor::publicSensorToMetadata(const PublicSensor& ps) {
    SensorMetadata sm;
    sm.setPublicName(ps.name);
    sm.setIsVirtual(ps.is_virtual);

    // Stripping whitespace from the sensor pattern in the SID
    string stripPattern = ps.pattern;
    boost::algorithm::trim(stripPattern);
    sm.setPattern(stripPattern);

    sm.setUnit(ps.unit);
    sm.setScale(ps.scaling_factor);
    sm.setTTL(ps.ttl);
    sm.setInterval(ps.interval);
    sm.setOperations(ps.operations);
    sm.setIntegrable(ps.sensor_mask & INTEGRABLE);
    sm.setMonotonic(ps.sensor_mask & MONOTONIC);
    return sm;
}

Axel Auweter's avatar
Axel Auweter committed
127
128
129
130

/*
 * SensorConfig functions
 */
131
132
133

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

Axel Auweter's avatar
Axel Auweter committed
137
138
SCError SensorConfig::publishSensor(const char* publicName, const char* sensorPattern)
{
139
    return impl->publishSensor(publicName, sensorPattern);
Axel Auweter's avatar
Axel Auweter committed
140
141
}

142
143
144
145
146
SCError SensorConfig::publishSensor(const PublicSensor& sensor)
{
    return impl->publishSensor(sensor);
}

147
148
149
150
151
SCError SensorConfig::publishSensor(const SensorMetadata& sensor)
{
    return impl->publishSensor(sensor);
}

152
SCError SensorConfig::publishVirtualSensor(const char* publicName, const char* vSensorExpression, const char* vSensorId, TimeStamp tZero, uint64_t interval)
153
{
154
    return impl->publishVirtualSensor(publicName, vSensorExpression, vSensorId, tZero, interval);
155
156
}

157
158
SCError SensorConfig::unPublishSensor(const char* publicName)
{
159
    return impl->unPublishSensor(publicName);
160
161
}

162
163
164
165
166
SCError SensorConfig::unPublishSensorsByWildcard(const char* wildcard)
{
    return impl->unPublishSensorsByWildcard(wildcard);
}

167
SCError SensorConfig::getPublicSensorNames(std::list<std::string>& publicSensors)
Axel Auweter's avatar
Axel Auweter committed
168
{
169
    return impl->getPublicSensorNames(publicSensors);
170
171
}

172
SCError SensorConfig::getPublicSensorsVerbose(std::list<PublicSensor>& publicSensors)
173
{
174
    return impl->getPublicSensorsVerbose(publicSensors);
175
176
}

177
SCError SensorConfig::getPublicSensorByName(PublicSensor& sensor, const char* publicName)
178
{
179
    return impl->getPublicSensorByName(sensor, publicName);
Axel Auweter's avatar
Axel Auweter committed
180
181
}

182
183
184
185
186
SCError SensorConfig::getPublicSensorsByWildcard(std::list<PublicSensor>& sensors, const char* wildcard)
{
    return impl->getPublicSensorsByWildcard(sensors, wildcard);
}

187
188
SCError SensorConfig::isVirtual(bool& isVirtual, std::string publicName)
{
189
    return impl->isVirtual(isVirtual, publicName);
190
191
}

Axel Auweter's avatar
Axel Auweter committed
192
193
SCError SensorConfig::getSensorPattern(std::string& pattern, std::string publicName)
{
194
    return impl->getSensorPattern(pattern, publicName);
Axel Auweter's avatar
Axel Auweter committed
195
196
197
198
}

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

202
SCError SensorConfig::getSensorListForPattern(std::list<SensorId>& sensorIds, std::string pattern, TimeStamp start, TimeStamp end)
Axel Auweter's avatar
Axel Auweter committed
203
{
204
    return impl->getSensorListForPattern(sensorIds, pattern, start, end);
Axel Auweter's avatar
Axel Auweter committed
205
206
}

207
SCError SensorConfig::setSensorScalingFactor(std::string publicName, double scalingFactor) {
208
    return impl->setSensorScalingFactor(publicName, scalingFactor);
209
210
211
212
}

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

216
SCError SensorConfig::setSensorMask(std::string publicName, uint64_t mask)
217
{
218
    return impl->setSensorMask(publicName, mask);
219
220
}

221
SCError SensorConfig::setOperations(std::string publicName, std::set<std::string> operations)
222
223
224
225
{
    return impl->setOperations(publicName,operations);
}

226
227
228
229
230
SCError SensorConfig::clearOperations(std::string publicName)
{
    return impl->clearOperations(publicName);
}

231
232
233
234
235
SCError SensorConfig::clearOperationsByWildcard(std::string wildcard)
{
    return impl->clearOperationsByWildcard(wildcard);
}

236
237
238
239
240
SCError SensorConfig::setTimeToLive(std::string publicName, uint64_t ttl)
{
    return impl->setTimeToLive(publicName, ttl);
}

241
242
SCError SensorConfig::setVirtualSensorExpression(std::string publicName, std::string expression)
{
243
    return impl->setVirtualSensorExpression(publicName, expression);
244
245
246
247
}

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

251
SCError SensorConfig::setSensorInterval(std::string publicName, uint64_t interval)
252
{
253
    return impl->setSensorInterval(publicName, interval);
254
255
}

256
SensorConfig::SensorConfig(Connection* conn)
Axel Auweter's avatar
Axel Auweter committed
257
{
258
259
    /* Allocate impl object */
    impl = new SensorConfigImpl(conn);
Axel Auweter's avatar
Axel Auweter committed
260
261
262
263
}

SensorConfig::~SensorConfig()
{
264
265
266
    if (impl) {
        delete impl;
    }
Axel Auweter's avatar
Axel Auweter committed
267
268
269
}

/*
270
 * SensorConfigImpl protected members and functions
Axel Auweter's avatar
Axel Auweter committed
271
272
273
274
275
276
277
278
279
280
 */

/*
 * 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)
{
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
    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
298

299
300
301
    /* More than one wildcard is not allowed */
    if (wildcards > 1)
        return false;
Axel Auweter's avatar
Axel Auweter committed
302

303
304
    if (strlen(sensorPattern) - seps > MAX_PATTERN_LENGTH)
        return false;
Axel Auweter's avatar
Axel Auweter committed
305

306
307
    /* Looks good */
    return true;
Axel Auweter's avatar
Axel Auweter committed
308
309
}

310
311
312
313
314
/*
 * Validate the public name of a sensor.
 */
bool SensorConfigImpl::validateSensorPublicName(std::string publicName)
{
315
    return true;
316
}
Axel Auweter's avatar
Axel Auweter committed
317
318
319
320

/*
 * SensorConfigImpl public functions
 */
321
322
323

SCError SensorConfigImpl::loadCache()
{
324
325
326
327
328
329
330
331
332
333
334
    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;
335
336
337
    }
}

Axel Auweter's avatar
Axel Auweter committed
338
SCError SensorConfigImpl::publishSensor(std::string publicName, std::string sensorPattern)
Axel Auweter's avatar
Axel Auweter committed
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
    /* 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
375
376
    cass_future_free(future);

377
    statement = cass_prepared_bind(prepared);
Axel Auweter's avatar
Axel Auweter committed
378

379
380
    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
381

382
383
    future = cass_session_execute(session, statement);
    cass_future_wait(future);
Axel Auweter's avatar
Axel Auweter committed
384

385
386
387
    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
Axel Auweter's avatar
Axel Auweter committed
388

389
390
        cass_prepared_free(prepared);
        cass_future_free(future);
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
        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;
426
    const char* query = "INSERT INTO " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " (name, pattern, virtual, scaling_factor, unit, sensor_mask, interval, ttl) VALUES (?,?, FALSE, ?, ?, ?, ?, ?);";
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450

    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_int64_by_name(statement, "interval", sensor.interval);
    cass_statement_bind_int64_by_name(statement, "ttl", sensor.ttl);
451
    
452
453
454
455
456
457
458
459
460
    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);
461
462
463
464
465
466
467
468
469
        cass_statement_free(statement);

        return SC_UNKNOWNERROR;
    }

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

470
471
472
473
474
    // Operations are inserted as an update statement, if required
    if(sensor.operations.size() > 0)
        return setOperations(sensor.name, sensor.operations);
    else
        return SC_OK;
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
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
}

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.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.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);
    }
562
    
563
564
565
566
567
568
569
570
571
    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);
572
573
574
575
        cass_statement_free(statement);

        return SC_UNKNOWNERROR;
    }
576
577
578
579

    cass_prepared_free(prepared);
    cass_future_free(future);
    cass_statement_free(statement);
580
581
582
583
584
585
    
    // Operations are inserted as an update statement, if required
    if(sensor.getOperations() && sensor.getOperations()->size()>0)
        return setOperations(*sensor.getPublicName(), *sensor.getOperations());
    else
        return SC_OK;
586
587
}

588
SCError SensorConfigImpl::publishVirtualSensor(std::string publicName, std::string vSensorExpression, std::string vSensorId, TimeStamp tZero, uint64_t interval)
589
{
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
    /* 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;
628
    const char* query = "INSERT INTO " CONFIG_KEYSPACE_NAME "." CF_PUBLISHEDSENSORS " (name, expression, vsensorid, tzero, interval, virtual) VALUES (?,?,?,?,?,TRUE);";
629
630
631
632
633
634
635
636
637
638
639
640
641

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

642
643
    cass_future_free(future);

644
    statement = cass_prepared_bind(prepared);
645

646
647
648
649
    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());
650
    cass_statement_bind_int64_by_name(statement, "interval", interval);
651

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

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

659
660
661
662
663
664
        cass_prepared_free(prepared);
        cass_future_free(future);
        cass_statement_free(statement);

        return SC_UNKNOWNERROR;
    }
665
666
667
668
669

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

670
    return SC_OK;
671
672
673
}


674
675
SCError SensorConfigImpl::unPublishSensor(std::string publicName)
{
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
    /* 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);
    }

700
701
    cass_future_free(future);

702
703
704
    statement = cass_prepared_bind(prepared);

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

706
707
    future = cass_session_execute(session, statement);
    cass_future_wait(future);
708

709
710
711
    rc = cass_future_error_code(future);
    if (rc != CASS_OK) {
        connection->printError(future);
712

713
714
715
        cass_prepared_free(prepared);
        cass_future_free(future);
        cass_statement_free(statement);
716

717
718
        return SC_UNKNOWNERROR;
    }
Axel Auweter's avatar
Axel Auweter committed
719
720
721
722
723

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

724
    return SC_OK;
Axel Auweter's avatar
Axel Auweter committed
725
726
}

727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
SCError SensorConfigImpl::unPublishSensorsByWildcard(std::string wildcard)
{
    std::list<PublicSensor> sensors;
    if(getPublicSensorsByWildcard(sensors, wildcard.c_str())!=SC_OK)
        return SC_UNKNOWNERROR;
    
    for(const auto& s : sensors) {
        if (unPublishSensor(s.name.c_str()) != SC_OK) {
            return SC_UNKNOWNERROR;
        }
    }
        
    return SC_OK;
}

742
743
SCError SensorConfigImpl::getPublicSensorNames(std::list<std::string>& publicSensors)
{
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
    /* 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);
759
    cass_statement_set_paging_size(statement, PAGING_SIZE);
760

761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
    bool morePages = false;
    do {
        future = cass_session_execute(session, statement);
        cass_future_wait(future);
    
        rc = cass_future_error_code(future);
        if (rc == CASS_OK) {
            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));
782
783
            }

784
785
786
787
788
789
790
791
792
793
            if((morePages = cass_result_has_more_pages(result)))
                cass_statement_set_paging_state(statement, result);
            
            cass_result_free(result);
            cass_iterator_free(iterator);
        } else {
            connection->printError(future);
            cass_future_free(future);
            cass_statement_free(statement);
            return SC_UNKNOWNERROR;
794
        }
795
796
797

        cass_future_free(future);
        
798
    }
799
    while(morePages);
800
801
802

    cass_statement_free(statement);
    return SC_OK;
803
804
}

805
SCError SensorConfigImpl::getPublicSensorsVerbose(std::list<PublicSensor>& publicSensors)
Axel Auweter's avatar
Axel Auweter committed
806
{
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
    /* 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);
822
    cass_statement_set_paging_size(statement, PAGING_SIZE);
823

824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
    bool morePages = false;
    do {
        future = cass_session_execute(session, statement);
        cass_future_wait(future);
    
        rc = cass_future_error_code(future);
        if (rc == CASS_OK) {
            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;
                int64_t     interval;
                int64_t     ttl;
                set<string> operations;
                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;
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
                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;
                }
                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;
                }
    
                const CassValue* opSet = nullptr;
                CassIterator *opSetIt = nullptr;
                if((opSet=cass_row_get_column_by_name(row, "operations")) && (opSetIt=cass_iterator_from_collection(opSet))) {
                    const char *opString;
                    size_t opLen;
    
                    while (cass_iterator_next(opSetIt)) {
                        if (cass_value_get_string(cass_iterator_get_value(opSetIt), &opString, &opLen) != CASS_OK) {
                            operations.clear();
                            break;
                        } else
                            operations.insert(std::string(opString, opLen));
                    }
    
                    cass_iterator_free(opSetIt);
                }
                
                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;
                sensor.interval = interval;
                sensor.ttl = ttl;
                sensor.operations = operations;
    
                publicSensors.push_back(sensor);
921
            }
922
923
924

            if((morePages = cass_result_has_more_pages(result)))
                cass_statement_set_paging_state(statement, result);
925
            
926
927
928
929
930
931
932
            cass_result_free(result);
            cass_iterator_free(iterator);
        } else {
            connection->printError(future);
            cass_future_free(future);
            cass_statement_free(statement);
            return SC_UNKNOWNERROR;
933
934
        }

935
936
937
938
939
        cass_future_free(future);
        
    }
    while(morePages);
    
940
941
    cass_statement_free(statement);
    return SC_OK;
Axel Auweter's avatar
Axel Auweter committed
942
943
}

944
SCError SensorConfigImpl::getPublicSensorByName(PublicSensor& sensor, const char* publicName)
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
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
    /* 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);
990
        cass_prepared_free(prepared);
991
992
993
994
995
996
997
998
999
1000
        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;