Skip to content

Commit

Permalink
feat!: load common configuration from config provider (#484)
Browse files Browse the repository at this point in the history
* feat!: load common configuration from config provider

Signed-off-by: FelixTing <[email protected]>

---------

Signed-off-by: FelixTing <[email protected]>
  • Loading branch information
FelixTing authored Oct 6, 2023
1 parent b359547 commit 4c52de3
Show file tree
Hide file tree
Showing 18 changed files with 362 additions and 157 deletions.
4 changes: 2 additions & 2 deletions src/c/bus-redstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ static bool edgex_bus_redstr_auth (iot_logger_t *lc, redisContext *ctx, const ch
return result;
}

edgex_bus_t *edgex_bus_create_redstr (iot_logger_t *lc, const char *svcname, const iot_data_t *cfg, edgex_secret_provider_t *secstore, iot_threadpool_t *queue, const devsdk_timeout *tm)
edgex_bus_t *edgex_bus_create_redstr (iot_logger_t *lc, const char *svcname, const iot_data_t *cfg, edgex_secret_provider_t *secstore, iot_threadpool_t *queue, const devsdk_timeout *tm, bool secureMode)
{
struct timeval tv;
edgex_bus_t *result = NULL;
Expand Down Expand Up @@ -175,7 +175,7 @@ edgex_bus_t *edgex_bus_create_redstr (iot_logger_t *lc, const char *svcname, con
return NULL;
}

if (strcmp (iot_data_string_map_get_string (cfg, EX_BUS_AUTHMODE), "usernamepassword") == 0)
if (secureMode && strcmp (iot_data_string_map_get_string (cfg, EX_BUS_AUTHMODE), "usernamepassword") == 0)
{
bool auth = true;
iot_data_t *secrets = edgex_secrets_get (secstore, iot_data_string_map_get_string (cfg, EX_BUS_SECRETNAME));
Expand Down
2 changes: 1 addition & 1 deletion src/c/bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ JSON_Value *edgex_bus_config_json (const iot_data_t *allconf);
edgex_bus_t *edgex_bus_create_mqtt
(iot_logger_t *lc, const char *svcname, const iot_data_t *cfg, edgex_secret_provider_t *secstore, iot_threadpool_t *queue, const devsdk_timeout *tm);
edgex_bus_t *edgex_bus_create_redstr
(iot_logger_t *lc, const char *svcname, const iot_data_t *cfg, edgex_secret_provider_t *secstore, iot_threadpool_t *queue, const devsdk_timeout *tm);
(iot_logger_t *lc, const char *svcname, const iot_data_t *cfg, edgex_secret_provider_t *secstore, iot_threadpool_t *queue, const devsdk_timeout *tm, bool secureMode);

void edgex_bus_register_handler (edgex_bus_t *bus, const char *path, void *ctx, edgex_handler_fn handler);
char *edgex_bus_mktopic (edgex_bus_t *bus, const char *type, const char *param);
Expand Down
124 changes: 90 additions & 34 deletions src/c/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,27 @@
#include "edgex-rest.h"
#include "edgex-logging.h"
#include "devutil.h"
#include "autoevent.h"
#include "device.h"
#include "edgex/devices.h"

#include <microhttpd.h>

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/utsname.h>

#include <iot/file.h>

#include <iot/data.h>

#include <iot/time.h>

#define ERRBUFSZ 1024
#define DEFAULTREG "consul.http://localhost:8500"
#define DEFAULTSECPATH "%s/"
#define DEFAULTSECCAFILE "/tmp/edgex/secrets/%s/secrets-token.json"
#define DEFAULTMETRICSTOPIC "edgex/telemetry"

iot_data_t *edgex_config_defaults (const iot_data_t *driverconf, const char *svcname)
iot_data_t *edgex_common_config_defaults (const char *svcname)
{
struct utsname utsbuffer;
uname (&utsbuffer);
Expand All @@ -56,19 +57,14 @@ iot_data_t *edgex_config_defaults (const iot_data_t *driverconf, const char *svc

iot_data_t *result = iot_data_alloc_map (IOT_DATA_STRING);

iot_data_string_map_add (result, DYN_PREFIX "LogLevel", iot_data_alloc_string ("WARNING", IOT_DATA_REF));
iot_data_string_map_add (result, DYN_PREFIX "Device/DataTransform", iot_data_alloc_bool (true));
iot_data_string_map_add (result, DYN_PREFIX "Device/Discovery/Enabled", iot_data_alloc_bool (true));
iot_data_string_map_add (result, DYN_PREFIX "Device/Discovery/Interval", iot_data_alloc_ui32 (0));
iot_data_string_map_add (result, DYN_PREFIX "Device/UpdateLastConnected", iot_data_alloc_bool (false));
iot_data_string_map_add (result, DYN_PREFIX "Device/MaxCmdOps", iot_data_alloc_ui32 (0));
iot_data_string_map_add (result, DYN_PREFIX "Device/MaxEventSize", iot_data_alloc_ui32 (0));
iot_data_string_map_add (result, "Device/DataTransform", iot_data_alloc_bool (true));
iot_data_string_map_add (result, "Device/Discovery/Enabled", iot_data_alloc_bool (true));
iot_data_string_map_add (result, "Device/Discovery/Interval", iot_data_alloc_ui32 (0));
iot_data_string_map_add (result, "Device/MaxCmdOps", iot_data_alloc_ui32 (0));

iot_data_string_map_add (result, DYN_PREFIX "Telemetry/Interval", iot_data_alloc_string ("30s", IOT_DATA_REF));
iot_data_string_map_add (result, DYN_PREFIX "Telemetry/PublishTopicPrefix", iot_data_alloc_string (DEFAULTMETRICSTOPIC, IOT_DATA_REF));
iot_data_string_map_add (result, DYN_PREFIX "Telemetry/Metrics/EventsSent", iot_data_alloc_bool (false));
iot_data_string_map_add (result, DYN_PREFIX "Telemetry/Metrics/ReadingsSent", iot_data_alloc_bool (false));
iot_data_string_map_add (result, DYN_PREFIX "Telemetry/Metrics/ReadCommandsExecuted", iot_data_alloc_bool (false));
iot_data_string_map_add (result, DYN_PREFIX "Telemetry/Metrics/SecuritySecretsRequested", iot_data_alloc_bool (false));
iot_data_string_map_add (result, DYN_PREFIX "Telemetry/Metrics/SecuritySecretsStored", iot_data_alloc_bool (false));

Expand All @@ -92,7 +88,7 @@ iot_data_t *edgex_config_defaults (const iot_data_t *driverconf, const char *svc
iot_data_string_map_add (result, "Device/DevicesDir", iot_data_alloc_string ("", IOT_DATA_REF));
iot_data_string_map_add (result, "Device/EventQLength", iot_data_alloc_ui32 (0));

iot_data_string_map_add (result, EX_BUS_TYPE, iot_data_alloc_string ("", IOT_DATA_REF));
iot_data_string_map_add (result, EX_BUS_TYPE, iot_data_alloc_string ("redis", IOT_DATA_REF));
edgex_bus_config_defaults (result, svcname);

iot_data_string_map_add (result, "SecretStore/Type", iot_data_alloc_string ("vault", IOT_DATA_REF));
Expand All @@ -107,6 +103,27 @@ iot_data_t *edgex_config_defaults (const iot_data_t *driverconf, const char *svc
iot_data_string_map_add (result, "SecretStore/SecretsFile", iot_data_alloc_string ("", IOT_DATA_REF));
iot_data_string_map_add (result, "SecretStore/DisableScrubSecretsFile", iot_data_alloc_bool (false));

return result;
}

iot_data_t *edgex_private_config_defaults (const iot_data_t *driverconf)
{
struct utsname utsbuffer;
uname (&utsbuffer);
iot_data_t *result = iot_data_alloc_map (IOT_DATA_STRING);

iot_data_string_map_add (result, DYN_PREFIX "LogLevel", iot_data_alloc_string ("WARNING", IOT_DATA_REF));

iot_data_string_map_add (result, "Device/UpdateLastConnected", iot_data_alloc_bool (false));
iot_data_string_map_add (result, "MaxEventSize", iot_data_alloc_ui32 (0));

iot_data_string_map_add (result, DYN_PREFIX "Telemetry/PublishTopicPrefix", iot_data_alloc_string (DEFAULTMETRICSTOPIC, IOT_DATA_REF));
iot_data_string_map_add (result, DYN_PREFIX "Telemetry/Metrics/ReadCommandsExecuted", iot_data_alloc_bool (false));

iot_data_string_map_add (result, "Service/Host", iot_data_alloc_string (utsbuffer.nodename, IOT_DATA_COPY));
iot_data_string_map_add (result, "Service/Port", iot_data_alloc_ui16 (59999));
iot_data_string_map_add (result, "Service/StartupMsg", iot_data_alloc_string ("", IOT_DATA_REF));

if (driverconf)
{
iot_data_map_iter_t iter;
Expand Down Expand Up @@ -434,12 +451,9 @@ void edgex_device_overrideConfig_nvpairs (iot_data_t *config, const devsdk_nvpai
addInsecureSecretsPairs (config, pairs);
}

static void edgex_device_populateConfigFromMap (edgex_device_config *config, const iot_data_t *map)
static void edgex_device_populateCommonConfigFromMap (edgex_device_config *config, const iot_data_t *map)
{
config->service.host = iot_data_string_map_get_string (map, "Service/Host");
config->service.port = iot_data_ui16 (iot_data_string_map_get (map, "Service/Port"));
config->service.timeout = edgex_parsetime (iot_data_string_map_get_string (map, "Service/RequestTimeout"));
config->service.startupmsg = iot_data_string_map_get_string (map, "Service/StartupMsg");
config->service.checkinterval = iot_data_string_map_get_string (map, "Service/HealthCheckInterval");
config->service.bindaddr = iot_data_string_map_get_string (map, "Service/ServerBindAddr");
config->service.maxreqsz = iot_data_ui64 (iot_data_string_map_get (map, "Service/MaxRequestSize"));
Expand Down Expand Up @@ -470,25 +484,34 @@ static void edgex_device_populateConfigFromMap (edgex_device_config *config, con
config->service.labels[0] = NULL;
}

config->device.datatransform = iot_data_bool (iot_data_string_map_get (map, DYN_PREFIX "Device/DataTransform"));
config->device.discovery_enabled = iot_data_bool (iot_data_string_map_get (map, DYN_PREFIX "Device/Discovery/Enabled"));
config->device.discovery_interval = iot_data_ui32 (iot_data_string_map_get (map, DYN_PREFIX "Device/Discovery/Interval"));
config->device.maxcmdops = iot_data_ui32 (iot_data_string_map_get (map, DYN_PREFIX "Device/MaxCmdOps"));
config->device.maxeventsize = iot_data_ui32 (iot_data_string_map_get (map, DYN_PREFIX "Device/MaxEventSize"));
config->device.datatransform = iot_data_bool (iot_data_string_map_get (map, "Device/DataTransform"));
config->device.discovery_enabled = iot_data_bool (iot_data_string_map_get (map, "Device/Discovery/Enabled"));
config->device.discovery_interval = iot_data_ui32 (iot_data_string_map_get (map, "Device/Discovery/Interval"));
config->device.maxcmdops = iot_data_ui32 (iot_data_string_map_get (map, "Device/MaxCmdOps"));
config->device.maxeventsize = iot_data_ui32 (iot_data_string_map_get (map, "MaxEventSize"));
config->device.profilesdir = iot_data_string_map_get_string (map, "Device/ProfilesDir");
config->device.devicesdir = iot_data_string_map_get_string (map, "Device/DevicesDir");
config->device.updatelastconnected = iot_data_bool (iot_data_string_map_get (map, DYN_PREFIX "Device/UpdateLastConnected"));
config->device.eventqlen = iot_data_ui32 (iot_data_string_map_get (map, "Device/EventQLength"));

config->metrics.interval = iot_data_string_map_get_string (map, DYN_PREFIX "Telemetry/Interval");
config->metrics.topic = iot_data_string_map_get_string (map, DYN_PREFIX "Telemetry/PublishTopicPrefix");
config->metrics.flags = iot_data_bool (iot_data_string_map_get (map, DYN_PREFIX "Telemetry/Metrics/EventsSent")) ? EX_METRIC_EVSENT : 0;
if (iot_data_bool (iot_data_string_map_get (map, DYN_PREFIX "Telemetry/Metrics/ReadingsSent"))) config->metrics.flags |= EX_METRIC_RDGSENT;
if (iot_data_bool (iot_data_string_map_get (map, DYN_PREFIX "Telemetry/Metrics/ReadCommandsExecuted"))) config->metrics.flags |= EX_METRIC_RDCMDS;
if (iot_data_bool (iot_data_string_map_get (map, DYN_PREFIX "Telemetry/Metrics/SecuritySecretsRequested"))) config->metrics.flags |= EX_METRIC_SECREQ;
if (iot_data_bool (iot_data_string_map_get (map, DYN_PREFIX "Telemetry/Metrics/SecuritySecretsStored"))) config->metrics.flags |= EX_METRIC_SECSTO;
}

static void edgex_device_populateConfigFromMap (edgex_device_config *config, const iot_data_t *map)
{
config->service.host = iot_data_string_map_get_string (map, "Service/Host");
config->service.port = iot_data_ui16 (iot_data_string_map_get (map, "Service/Port"));
config->service.startupmsg = iot_data_string_map_get_string (map, "Service/StartupMsg");

config->device.updatelastconnected = iot_data_bool (iot_data_string_map_get (map, "Device/UpdateLastConnected"));
config->device.eventqlen = iot_data_ui32 (iot_data_string_map_get (map, "Device/EventQLength"));

config->metrics.topic = iot_data_string_map_get_string (map, DYN_PREFIX "Telemetry/PublishTopicPrefix");
if (iot_data_bool (iot_data_string_map_get (map, DYN_PREFIX "Telemetry/Metrics/ReadCommandsExecuted"))) config->metrics.flags |= EX_METRIC_RDCMDS;
}

void edgex_device_populateConfig (devsdk_service_t *svc, iot_data_t *config)
{
iot_data_map_iter_t iter;
Expand All @@ -513,22 +536,60 @@ void edgex_device_populateConfig (devsdk_service_t *svc, iot_data_t *config)
}
}

edgex_device_populateCommonConfigFromMap (&svc->config, config);
edgex_device_populateConfigFromMap (&svc->config, config);

edgex_config_setloglevel (svc->logger, iot_data_string_map_get_string (config, DYN_PREFIX "LogLevel"), &svc->config.loglevel);
}

void edgex_device_updateCommonConf (void *p, const devsdk_nvpairs *config)
{
bool updatemetrics = false;
devsdk_service_t *svc = (devsdk_service_t *)p;

devsdk_timeout *timeout = (devsdk_timeout *)malloc(sizeof(devsdk_timeout));
uint64_t t1, t2;
timeout->deadline = iot_time_secs () + 10;
timeout->interval = 1;
t1 = iot_time_secs ();
while (svc->config.sdkconf == NULL)
{
t2 = iot_time_secs ();
if (t2 > timeout->deadline - timeout->interval)
{
iot_log_error (svc->logger, "SDK configuration is not ready");
return;
}
if (timeout->interval > t2 - t1)
{
iot_log_warn(svc->logger, "waiting for SDK configuration to be available.");
iot_wait_secs (timeout->interval - (t2 - t1));
}
t1 = iot_time_secs ();
}
free (timeout);

iot_log_info (svc->logger, "Reconfiguring");

edgex_device_overrideConfig_nvpairs (svc->config.sdkconf, config);
updatemetrics = strcmp (svc->config.metrics.interval, iot_data_string_map_get_string (svc->config.sdkconf, DYN_PREFIX "Telemetry/Interval"));
edgex_device_populateCommonConfigFromMap (&svc->config, svc->config.sdkconf);

if (updatemetrics)
{
devsdk_schedule_metrics (svc);
}
}

void edgex_device_updateConf (void *p, const devsdk_nvpairs *config)
{
iot_data_map_iter_t iter;
bool updatedriver = false;
bool updatemetrics = false;
devsdk_service_t *svc = (devsdk_service_t *)p;

iot_log_info (svc->logger, "Reconfiguring");

edgex_device_overrideConfig_nvpairs (svc->config.sdkconf, config);
updatemetrics = strcmp (svc->config.metrics.interval, iot_data_string_map_get_string (svc->config.sdkconf, DYN_PREFIX "Telemetry/Interval"));
edgex_device_populateConfigFromMap (&svc->config, svc->config.sdkconf);

const char *lname = devsdk_nvpairs_value (config, DYN_PREFIX "LogLevel");
Expand All @@ -539,11 +600,6 @@ void edgex_device_updateConf (void *p, const devsdk_nvpairs *config)

edgex_device_periodic_discovery_configure (svc->discovery, svc->config.device.discovery_enabled, svc->config.device.discovery_interval);

if (updatemetrics)
{
devsdk_schedule_metrics (svc);
}

if (svc->secretstore)
{
edgex_secrets_reconfigure (svc->secretstore, svc->config.sdkconf);
Expand Down
7 changes: 6 additions & 1 deletion src/c/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "devsdk/devsdk.h"
#include "rest-server.h"
#include "map.h"
#include "registry.h"

#define EX_METRIC_EVSENT 0x1
#define EX_METRIC_RDGSENT 0x2
Expand Down Expand Up @@ -86,7 +87,9 @@ typedef struct edgex_device_config

iot_data_t *edgex_device_loadConfig (iot_logger_t *lc, const char *path, devsdk_error *err);

iot_data_t *edgex_config_defaults (const iot_data_t *driverconf, const char *svcname);
iot_data_t *edgex_common_config_defaults (const char *svcname);

iot_data_t *edgex_private_config_defaults (const iot_data_t *driverconf);

char *edgex_device_getRegURL (const iot_data_t *config);

Expand All @@ -100,6 +103,8 @@ void edgex_device_overrideConfig_env (iot_logger_t *lc, iot_data_t *config);

void edgex_device_overrideConfig_nvpairs (iot_data_t *config, const devsdk_nvpairs *pairs);

void edgex_device_updateCommonConf (void *svc, const devsdk_nvpairs *config);

void edgex_device_updateConf (void *svc, const devsdk_nvpairs *config);

void edgex_device_processDriverConfig (iot_data_t *driverconf, const iot_data_t *allconf);
Expand Down
Loading

0 comments on commit 4c52de3

Please sign in to comment.