diff --git a/lib/Makefile.inc b/lib/Makefile.inc index 0529873aa..07d4e8dc8 100644 --- a/lib/Makefile.inc +++ b/lib/Makefile.inc @@ -18,8 +18,9 @@ LIB_LDFLAGS = -shared LIB_LDLIBS = $(LDLIBS) -ldl -lrt -Wl,-soname,$(LIB_NAME).so.$(LIB_ABI_VERSION) -include lib/hooks/Makefile.inc --include lib/apis/Makefile.inc +-include lib/api/Makefile.inc -include lib/fpga/Makefile.inc +-include lib/web/Makefile.inc LIB_PKGS = openssl diff --git a/lib/api/actions/Makefile.inc b/lib/api/actions/Makefile.inc new file mode 100644 index 000000000..743cd617e --- /dev/null +++ b/lib/api/actions/Makefile.inc @@ -0,0 +1 @@ +LIB_SRCS += $(wildcard lib/api/actions/*.c) \ No newline at end of file diff --git a/lib/api/actions/capabiltities.c b/lib/api/actions/capabiltities.c new file mode 100644 index 000000000..89cc3adb6 --- /dev/null +++ b/lib/api/actions/capabiltities.c @@ -0,0 +1,45 @@ +/** The "capabiltities" API ressource. + * + * @author Steffen Vogel + * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC + *********************************************************************************/ + +#include "plugin.h" + +static int api_capabilities(struct api_action *h, json_t *args, json_t **resp, struct api_session *s) +{ + json_t *json_hooks = json_array(); + json_t *json_apis = json_array(); + json_t *json_nodes = json_array(); + json_t *json_name; + + for (size_t i = 0; i < list_length(&plugins); i++) { + struct plugin *p = list_at(&plugins, i); + + json_name = json_string(p->name); + + switch (p->type) { + case PLUGIN_TYPE_NODE: json_array_append_new(json_nodes, json_name); break; + case PLUGIN_TYPE_HOOK: json_array_append_new(json_hooks, json_name); break; + case PLUGIN_TYPE_API: json_array_append_new(json_apis, json_name); break; + default: { } + } + } + + *resp = json_pack("{ s: s, s: o, s: o, s: o }", + "build", BUILDID, + "hooks", json_hooks, + "node-types", json_nodes, + "apis", json_apis); + + return 0; +} + +static struct plugin p = { + .name = "capabilities", + .description = "get capabiltities and details about this VILLASnode instance", + .type = PLUGIN_TYPE_API, + .api.cb = api_capabilities +}; + +REGISTER_PLUGIN(&p) \ No newline at end of file diff --git a/lib/api/actions/config.c b/lib/api/actions/config.c new file mode 100644 index 000000000..8292007cc --- /dev/null +++ b/lib/api/actions/config.c @@ -0,0 +1,29 @@ +/** The "config" API ressource. + * + * @author Steffen Vogel + * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC + *********************************************************************************/ + +#include + +#include "api.h" +#include "utils.h" +#include "plugin.h" + +static int api_config(struct api_action *h, json_t *args, json_t **resp, struct api_session *s) +{ + config_setting_t *cfg_root = config_root_setting(&s->api->super_node->cfg); + + *resp = cfg_root ? config_to_json(cfg_root) : json_object(); + + return 0; +} + +static struct plugin p = { + .name = "config", + .description = "retrieve current VILLASnode configuration", + .type = PLUGIN_TYPE_API, + .api.cb = api_config +}; + +REGISTER_PLUGIN(&p) \ No newline at end of file diff --git a/lib/api/actions/nodes.c b/lib/api/actions/nodes.c new file mode 100644 index 000000000..72c64cb1a --- /dev/null +++ b/lib/api/actions/nodes.c @@ -0,0 +1,51 @@ +/** The "nodes" API ressource. + * + * @author Steffen Vogel + * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC + *********************************************************************************/ + +#include + +#include "plugin.h" +#include "node.h" +#include "utils.h" + +#include "api.h" + +extern struct list nodes; + +static int api_nodes(struct api_action *r, json_t *args, json_t **resp, struct api_session *s) +{ + json_t *json_nodes = json_array(); + + for (size_t i = 0; i < list_length(&s->api->super_node->nodes); i++) { + struct node *n = list_at(&s->api->super_node->nodes, i); + + json_t *json_node = json_pack("{ s: s, s: i, s: i, s: i, s: i }", + "name", node_name_short(n), + "state", n->state, + "vectorize", n->vectorize, + "affinity", n->affinity, + "id", i + ); + + /* Add all additional fields of node here. + * This can be used for metadata */ + json_object_update(json_node, config_to_json(n->cfg)); + + json_array_append_new(json_nodes, json_node); + } + + *resp = json_nodes; + + return 0; +} + +static struct plugin p = { + .name = "nodes", + .description = "retrieve list of all known nodes", + .type = PLUGIN_TYPE_API, + .api.cb = api_nodes +}; + +REGISTER_PLUGIN(&p) \ No newline at end of file diff --git a/lib/api/actions/reload.c b/lib/api/actions/reload.c new file mode 100644 index 000000000..b5821b8cb --- /dev/null +++ b/lib/api/actions/reload.c @@ -0,0 +1,23 @@ +/** The "restart" API ressource. + * + * @author Steffen Vogel + * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC + *********************************************************************************/ + +#include "plugin.h" +#include "api.h" + +/** @todo not implemented yet */ +static int api_restart(struct api_action *h, json_t *args, json_t **resp, struct api_session *s) +{ + return -1; +} + +static struct plugin p = { + .name = "restart", + .description = "restart VILLASnode with new configuration", + .type = PLUGIN_TYPE_API, + .api.cb = api_restart +}; + +REGISTER_PLUGIN(&p) \ No newline at end of file