2021-06-03 03:34:29 -04:00
|
|
|
/** JSON serializtion for Kafka schema/payloads.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <villas/timing.h>
|
2021-05-10 00:12:30 +02:00
|
|
|
#include <villas/utils.hpp>
|
|
|
|
#include <villas/formats/json_kafka.hpp>
|
|
|
|
#include <villas/exceptions.hpp>
|
|
|
|
|
|
|
|
using namespace villas;
|
|
|
|
using namespace villas::node;
|
2021-06-03 03:34:29 -04:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
const char * JsonKafkaFormat::villasToKafkaType(enum SignalType vt)
|
2021-06-03 03:34:29 -04:00
|
|
|
{
|
|
|
|
switch (vt) {
|
|
|
|
case SignalType::FLOAT:
|
|
|
|
return "double";
|
|
|
|
|
|
|
|
case SignalType::INTEGER:
|
|
|
|
return "int64";
|
|
|
|
|
|
|
|
case SignalType::BOOLEAN:
|
2021-06-03 03:34:55 -04:00
|
|
|
return "boolean";
|
2021-06-03 03:34:29 -04:00
|
|
|
|
|
|
|
default:
|
2021-06-03 03:34:55 -04:00
|
|
|
case SignalType::COMPLEX:
|
2021-06-03 03:34:29 -04:00
|
|
|
case SignalType::INVALID:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
int JsonKafkaFormat::packSample(json_t **json_smp, const struct sample *smp)
|
2021-06-03 03:34:29 -04:00
|
|
|
{
|
2021-05-10 00:12:30 +02:00
|
|
|
json_t *json_payload, *json_fields, *json_field, *json_value;
|
2021-06-03 03:34:29 -04:00
|
|
|
|
|
|
|
json_fields = json_array();
|
|
|
|
json_payload = json_object();
|
|
|
|
|
|
|
|
/* Include sample timestamp */
|
|
|
|
if (smp->flags & (int) SampleFlags::HAS_TS_ORIGIN) {
|
|
|
|
json_field = json_pack("{ s: s, s: b, s: s }",
|
|
|
|
"type", "int64",
|
|
|
|
"optional", false,
|
|
|
|
"field", "timestamp"
|
|
|
|
);
|
|
|
|
|
|
|
|
uint64_t ts_origin_ms = smp->ts.origin.tv_sec * 1e3 + smp->ts.origin.tv_nsec / 1e6;
|
2021-05-10 00:12:30 +02:00
|
|
|
json_array_append_new(json_fields, json_field);
|
|
|
|
json_object_set_new(json_payload, "timestamp", json_integer(ts_origin_ms));
|
2021-06-03 03:34:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Include sample sequence no */
|
|
|
|
if (smp->flags & (int) SampleFlags::HAS_SEQUENCE) {
|
|
|
|
json_field = json_pack("{ s: s, s: b, s: s }",
|
|
|
|
"type", "int64",
|
|
|
|
"optional", false,
|
|
|
|
"field", "sequence"
|
|
|
|
);
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
json_array_append_new(json_fields, json_field);
|
|
|
|
json_object_set_new(json_payload, "sequence", json_integer(smp->sequence));
|
2021-06-03 03:34:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Include sample data */
|
|
|
|
for (size_t i = 0; i < MIN(smp->length, vlist_length(smp->signals)); i++) {
|
|
|
|
struct signal *sig = (struct signal *) vlist_at(smp->signals, i);
|
2021-05-10 00:12:30 +02:00
|
|
|
const union signal_data *data = &smp->data[i];
|
2021-06-03 03:34:29 -04:00
|
|
|
|
|
|
|
json_field = json_pack("{ s: s, s: b, s: s }",
|
2021-05-10 00:12:30 +02:00
|
|
|
"type", villasToKafkaType(sig->type),
|
2021-06-03 03:34:29 -04:00
|
|
|
"optional", false,
|
|
|
|
"field", sig->name
|
|
|
|
);
|
|
|
|
|
|
|
|
json_value = signal_data_to_json(data, sig->type);
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
json_array_append_new(json_fields, json_field);
|
|
|
|
json_object_set_new(json_payload, sig->name, json_value);
|
2021-06-03 03:34:29 -04:00
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
json_object_set_new(json_schema, "fields", json_fields);
|
2021-06-03 03:34:29 -04:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
json_incref(json_schema);
|
|
|
|
*json_smp = json_pack("{ s: o, s: o }",
|
2021-06-03 03:34:29 -04:00
|
|
|
"schema", json_schema,
|
|
|
|
"payload", json_payload
|
2021-05-10 00:12:30 +02:00
|
|
|
);;
|
|
|
|
if (*json_smp == nullptr)
|
|
|
|
return -1;
|
2021-06-03 03:34:29 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
int JsonKafkaFormat::unpackSample(json_t *json_smp, struct sample *smp)
|
2021-06-03 03:34:29 -04:00
|
|
|
{
|
|
|
|
json_t *json_payload, *json_value;
|
|
|
|
|
|
|
|
json_payload = json_object_get(json_smp, "payload");
|
|
|
|
if (!json_payload)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
smp->length = 0;
|
|
|
|
smp->flags = 0;
|
2021-05-10 00:12:30 +02:00
|
|
|
smp->signals = signals;
|
2021-06-03 03:34:29 -04:00
|
|
|
|
2021-06-03 03:34:55 -04:00
|
|
|
/* Unpack timestamp */
|
2021-06-03 03:34:29 -04:00
|
|
|
json_value = json_object_get(json_payload, "timestamp");
|
|
|
|
if (json_value) {
|
|
|
|
uint64_t ts_origin_ms = json_integer_value(json_value);
|
|
|
|
smp->ts.origin = time_from_double(ts_origin_ms / 1e3);
|
|
|
|
|
|
|
|
smp->flags |= (int) SampleFlags::HAS_TS_ORIGIN;
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:34:55 -04:00
|
|
|
/* Unpack sequence no */
|
2021-06-03 03:34:29 -04:00
|
|
|
json_value = json_object_get(json_payload, "sequence");
|
|
|
|
if (json_value) {
|
|
|
|
smp->sequence = json_integer_value(json_value);
|
2021-06-03 03:34:55 -04:00
|
|
|
|
2021-06-03 03:34:29 -04:00
|
|
|
smp->flags |= (int) SampleFlags::HAS_SEQUENCE;
|
|
|
|
}
|
|
|
|
|
2021-06-03 03:34:55 -04:00
|
|
|
/* Unpack signal data */
|
2021-05-10 00:12:30 +02:00
|
|
|
for (size_t i = 0; i < vlist_length(signals); i++) {
|
|
|
|
struct signal *sig = (struct signal *) vlist_at(signals, i);
|
2021-06-03 03:34:55 -04:00
|
|
|
|
2021-06-03 03:34:29 -04:00
|
|
|
json_value = json_object_get(json_payload, sig->name);
|
|
|
|
if (!json_value)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
signal_data_parse_json(&smp->data[i], sig->type, json_value);
|
|
|
|
smp->length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (smp->length > 0)
|
|
|
|
smp->flags |= (int) SampleFlags::HAS_DATA;
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
return 0;
|
2021-06-03 03:34:29 -04:00
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
void JsonKafkaFormat::parse(json_t *json)
|
2021-06-03 03:34:29 -04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
json_error_t err;
|
2021-05-10 00:12:30 +02:00
|
|
|
json_t *json_schema_tmp = nullptr;
|
2021-06-03 03:34:29 -04:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: o }",
|
|
|
|
"schema", &json_schema_tmp
|
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(json, err, "node-config-format-json-kafka", "Failed to parse format configuration");
|
2021-06-03 03:34:29 -04:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
if (json_schema_tmp) {
|
|
|
|
if (!json_is_object(json_schema_tmp))
|
|
|
|
throw ConfigError(json, "node-config-format-json-kafka-schema", "Kafka schema must be configured as a dictionary");
|
2021-06-03 03:34:29 -04:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
if (json_schema)
|
|
|
|
json_decref(json_schema);
|
2021-06-03 03:34:29 -04:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
json_schema = json_schema_tmp;
|
2021-06-03 03:34:29 -04:00
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
JsonFormat::parse(json);
|
2021-06-03 03:34:29 -04:00
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
JsonKafkaFormat::JsonKafkaFormat(int fl) :
|
|
|
|
JsonFormat(fl)
|
2021-06-03 03:34:29 -04:00
|
|
|
{
|
2021-05-10 00:12:30 +02:00
|
|
|
json_schema = json_pack("{ s: s, s: s }",
|
|
|
|
"type", "struct",
|
|
|
|
"name", "villas-node.Value"
|
|
|
|
);
|
2021-06-03 03:34:29 -04:00
|
|
|
}
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
static char n[] = "json.kafka";
|
|
|
|
static char d[] = "JSON Kafka schema/payload messages";
|
|
|
|
static FormatPlugin<JsonKafkaFormat, n, d, (int) SampleFlags::HAS_TS_ORIGIN | (int) SampleFlags::HAS_SEQUENCE | (int) SampleFlags::HAS_DATA> p;
|