Commit 8c3a1619 authored by Axel Auweter's avatar Axel Auweter
Browse files

Rework "Alias" into the new nomenclature of "publishing sensors".

parent af2fa3dd
......@@ -24,7 +24,7 @@
#define CF_SENSORDATA "sensordata"
#define CONFIG_KEYSPACE_NAME KEYSPACE_NAME "_config"
#define CF_SENSORALIASES "sensoralias"
#define CF_PUBLISHEDSENSORS "publishedsensors"
#define CF_VIRTUALSENSORS "virtualsensors"
/**
......
......@@ -108,7 +108,7 @@ void SensorDataStoreImpl::init(std::string hostname, int port) {
exit(EXIT_FAILURE);
}
/* Keyspace and column family for sensor aliases */
/* Keyspace and column family for published sensors */
/* FIXME: We should have a good way to determine the number of replicas here */
if (!csBackend->existsKeyspace(CONFIG_KEYSPACE_NAME)) {
std::cout << "Creating Keyspace " << CONFIG_KEYSPACE_NAME << "...\n";
......@@ -122,9 +122,9 @@ void SensorDataStoreImpl::init(std::string hostname, int port) {
exit(EXIT_FAILURE);
}
if (!csBackend->existsColumnFamily(CF_SENSORALIASES)) {
std::cout << "Creating Column Familiy " CF_SENSORALIASES "...\n";
csBackend->createColumnFamily(CF_SENSORALIASES,
if (!csBackend->existsColumnFamily(CF_PUBLISHEDSENSORS)) {
std::cout << "Creating Column Familiy " CF_PUBLISHEDSENSORS "...\n";
csBackend->createColumnFamily(CF_PUBLISHEDSENSORS,
"name varchar, pattern varchar, scaling_factor double, unit varchar",
"name",
"COMPACT STORAGE AND CACHING = all");
......
......@@ -21,11 +21,13 @@
*/
void SensorAction::printHelp(int argc, char* argv[])
{
/* 01234567890123456789012345678901234567890123456789012345678901234567890123456789 */
std::cout << "SENSOR command help" << std::endl;
std::cout << "The SENSOR command has the following options:" << std::endl;
std::cout << " aliasadd <alias name> <pattern> - Add a sensor alias matching a given MQTT" << std::endl;
std::cout << " topic pattern." << std::endl;
std::cout << " aliaslist - List all configured aliases." << std::endl;
std::cout << " PUBLISH <public name> <pattern> - Make a sensor publicly available under the " << std::endl;
std::cout << " <public name> comprising of all internal" << std::endl;
std::cout << " sensors matching the <pattern>." << std::endl;
std::cout << " LISTPUBLIC - List all public sensors and their patterns." << std::endl;
}
/*
......@@ -52,17 +54,17 @@ int SensorAction::executeCommand(int argc, char* argv[], int argvidx, const char
goto executeCommandError;
}
if (strcasecmp(argv[argvidx], "aliasadd") == 0) {
/* aliasadd needs two more parameters */
if (strcasecmp(argv[argvidx], "PUBLISH") == 0) {
/* PUBLISH needs two more parameters */
if (argvidx+2 >= argc) {
std::cout << "ALIASADD needs two more parameters!" << std::endl;
std::cout << "PUBLISH needs two more parameters!" << std::endl;
goto executeCommandError;
}
doAddAlias(session, argv[argvidx+1], argv[argvidx+2]);
doPublishSensor(session, argv[argvidx+1], argv[argvidx+2]);
}
else if (strcasecmp(argv[argvidx], "aliaslist") == 0) {
doListAliases(session);
else if (strcasecmp(argv[argvidx], "LISTPUBLIC") == 0) {
doListPublicSensors(session);
}
else {
std::cout << "Invalid SENSOR command: " << argv[argvidx] << std::endl;
......@@ -81,19 +83,19 @@ executeCommandError:
}
/*
* Add a Sensor Alias entry into the database
* Add an entry into the database of published sensors
*/
void SensorAction::doAddAlias(CassSession* session, const char* aliasName, const char* aliasPattern)
void SensorAction::doPublishSensor(CassSession* session, const char* publicName, const char* sensorPattern)
{
/*
* FIXME: Merge the core functionality into DCDBLib and stick to the
* CONFIG_KEYSPACE_NAME/CF_SENSORALIASES instead of hard coding to
* dcdb_config.sensoralias
* CONFIG_KEYSPACE_NAME/CF_PUBLISHEDSENSORS instead of hard coding to
* dcdb_config.publishedsensors
*/
/* Check if the pattern matches the requirements */
if (!validateAliasPattern(aliasPattern)) {
std::cout << "Invalid pattern. Alias was not added to database." << std::endl;
if (!validateSensorPattern(sensorPattern)) {
std::cout << "Invalid pattern. Sensor could not be published." << std::endl;
return;
}
......@@ -101,7 +103,7 @@ void SensorAction::doAddAlias(CassSession* session, const char* aliasName, const
CassStatement* statement = nullptr;
CassFuture* future = nullptr;
const CassPrepared* prepared = nullptr;
CassString query = cass_string_init("INSERT INTO dcdb_config.sensoralias (name, pattern) VALUES (?,?);");
CassString query = cass_string_init("INSERT INTO dcdb_config.publishedsensors (name, pattern) VALUES (?,?);");
future = cass_session_prepare(session, query);
cass_future_wait(future);
......@@ -115,8 +117,8 @@ void SensorAction::doAddAlias(CassSession* session, const char* aliasName, const
statement = cass_prepared_bind(prepared);
cass_statement_bind_string_by_name(statement, "name", cass_string_init(aliasName));
cass_statement_bind_string_by_name(statement, "pattern", cass_string_init(aliasPattern));
cass_statement_bind_string_by_name(statement, "name", cass_string_init(publicName));
cass_statement_bind_string_by_name(statement, "pattern", cass_string_init(sensorPattern));
future = cass_session_execute(session, statement);
......@@ -132,14 +134,14 @@ void SensorAction::doAddAlias(CassSession* session, const char* aliasName, const
}
/*
* List all Sensor Alias entries from the database
* List all published sensor entries from the database
*/
void SensorAction::doListAliases(CassSession* session)
void SensorAction::doListPublicSensors(CassSession* session)
{
CassError rc = CASS_OK;
CassStatement* statement = nullptr;
CassFuture* future = nullptr;
CassString query = cass_string_init("SELECT * FROM dcdb_config.sensoralias;");
CassString query = cass_string_init("SELECT * FROM dcdb_config.publishedsensors;");
CassString name, pattern;
statement = cass_statement_new(query, 0);
......@@ -167,25 +169,25 @@ void SensorAction::doListAliases(CassSession* session)
}
/*
* Validate the pattern for a Sensor Alias
* Alias patterns may only consist of hex numbers and forward slashes
* 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 SensorAction::validateAliasPattern(const char* aliasPattern)
bool SensorAction::validateSensorPattern(const char* sensorPattern)
{
unsigned int wildcards, bits;
/* 128 bits in HEX is at most 32 characters. Considering forward-slash separators, 64 is a reasonable maxmimum string length */
if (strlen(aliasPattern) > 64) {
if (strlen(sensorPattern) > 64) {
return false;
}
/* Iterate through the string and validate/count the characters */
bits = 0;
wildcards = 0;
for (unsigned int c = 0; c < strlen(aliasPattern); c++) {
switch (aliasPattern[c]) {
for (unsigned int c = 0; c < strlen(sensorPattern); c++) {
switch (sensorPattern[c]) {
/* Regular HEX digit */
case '0':
......
......@@ -19,10 +19,10 @@ public:
int executeCommand(int argc, char* argv[], int argvidx, const char* hostname);
protected:
void doListAliases(CassSession* session);
void doAddAlias(CassSession* session, const char* aliasName, const char* aliasPattern);
void doListPublicSensors(CassSession* session);
void doPublishSensor(CassSession* session, const char* publicName, const char* sensorPattern);
bool validateAliasPattern(const char* aliasPattern);
bool validateSensorPattern(const char* sensorPattern);
};
#endif
......@@ -24,7 +24,7 @@ void usage(void)
<< "dcdbquery [-h <hostname>] <Sensor 1> [<Sensor 2> ...] <Start> <End>" << std::endl
<< "where" << std::endl
<< " <hostname> - the name of the DB server"
<< " <Sensor n> - a sensor name alias" << std::endl
<< " <Sensor n> - a public sensor name" << std::endl
<< " <Start> - start of time series" << std::endl
<< " <End> - end of time series" << std::endl
<< "Return the readings for the sensors in the interval from <Start> to <End>." << std::endl
......
......@@ -35,16 +35,14 @@ void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DC
exit(EXIT_FAILURE);
}
/* Iterate over sensors */
/* Iterate over list of sensors requestes by the user */
for (std::list<std::string>::iterator it = sensors.begin(); it != sensors.end(); it++) {
/* Lookup the sensor in the alias table */
std::string aliasPattern;
lookupAlias(session, *it, aliasPattern);
// std::cout << "Found alias: " << aliasPattern << std::endl;
/* Lookup the sensor in the published sensors table */
std::string sensorPattern;
lookupPublishedSensorPattern(session, *it,sensorPattern);
std::list<SensorId> sidList;
expandAlias(session, aliasPattern, start, end, sidList);
expandSensorPattern(session, sensorPattern, start, end, sidList);
#if 0
for (std::list<SensorId>::iterator sit = sidList.begin(); sit != sidList.end(); sit++) {
......@@ -64,14 +62,14 @@ void DCDBQuery::doQuery(const char* hostname, std::list<std::string> sensors, DC
}
void DCDBQuery::lookupAlias(CassSession* session, std::string name, std::string& pattern)
void DCDBQuery::lookupPublishedSensorPattern(CassSession* session, std::string name, std::string& pattern)
{
CassError rc = CASS_OK;
CassStatement* statement = nullptr;
CassFuture* future = nullptr;
const CassPrepared* prepared = nullptr;
CassString query = cass_string_init("SELECT pattern FROM dcdb_config.sensoralias WHERE name = ? ;");
CassString query = cass_string_init("SELECT pattern FROM dcdb_config.publishedsensors WHERE name = ? ;");
CassString pattern_cstr;
future = cass_session_prepare(session, query);
......@@ -112,20 +110,20 @@ void DCDBQuery::lookupAlias(CassSession* session, std::string name, std::string&
}
}
void DCDBQuery::expandAlias(CassSession* session, std::string aliasName, DCDBTimeStamp start, DCDBTimeStamp end, std::list<SensorId>& sensorIds)
void DCDBQuery::expandSensorPattern(CassSession* session, std::string sensorPattern, DCDBTimeStamp start, DCDBTimeStamp end, std::list<SensorId>& sensorIds)
{
/* Clear the list of sensorIds */
sensorIds.clear();
/* Strip all slashes from aliasName */
aliasName.erase(std::remove(aliasName.begin(), aliasName.end(), '/'), aliasName.end());
/* Strip all slashes from publishedSensorName */
sensorPattern.erase(std::remove(sensorPattern.begin(), sensorPattern.end(), '/'), sensorPattern.end());
/* Calculate lower and upper boundaries for the expansion of the alias */
std::string low = aliasName;
std::string high = aliasName;
if (aliasName.find("*") != std::string::npos) {
low.replace(aliasName.find("*"), 1, 33-aliasName.length(), '0');
high.replace(aliasName.find("*"), 1, 33-aliasName.length(), 'F');
/* Calculate lower and upper boundaries for the expansion of the pattern */
std::string low = sensorPattern;
std::string high = sensorPattern;
if (sensorPattern.find("*") != std::string::npos) {
low.replace(sensorPattern.find("*"), 1, 33-sensorPattern.length(), '0');
high.replace(sensorPattern.find("*"), 1, 33-sensorPattern.length(), 'F');
}
SensorId lowId, highId;
......@@ -191,7 +189,7 @@ void DCDBQuery::expandAlias(CassSession* session, std::string aliasName, DCDBTim
sensor.raw[1] = Endian::BEToHost(((uint64_t*)res.data)[1]);
/* Check if the sensorId matches the pattern */
if (sidPatternMatch(sensor, aliasName)) {
if (sidPatternMatch(sensor, sensorPattern)) {
sensorIds.push_back(sensor);
}
}
......
......@@ -23,8 +23,8 @@ public:
static void doQuery(const char* hostname, std::list<std::string> sensors, DCDBTimeStamp start, DCDBTimeStamp end);
protected:
static void lookupAlias(CassSession* session, std::string name, std::string& pattern);
static void expandAlias(CassSession* session, std::string aliasName, DCDBTimeStamp start, DCDBTimeStamp end, std::list<SensorId>& sensorIds);
static void lookupPublishedSensorPattern(CassSession* session, std::string name, std::string& pattern);
static void expandSensorPattern(CassSession* session, std::string sensorPattern, DCDBTimeStamp start, DCDBTimeStamp end, std::list<SensorId>& sensorIds);
static bool topicToSid(SensorId& sid, std::string topic);
static std::string sidConvert(SensorId& sid);
static bool sidPatternMatch(SensorId& sid, std::string pattern);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment