diff --git a/include/neuron/adapter.h b/include/neuron/adapter.h index 623377a9..1cec695f 100644 --- a/include/neuron/adapter.h +++ b/include/neuron/adapter.h @@ -116,6 +116,8 @@ typedef enum neu_reqresp_type { NEU_REQ_ADD_NDRIVER_MAP, NEU_REQ_DEL_NDRIVER_MAP, + NEU_REQ_GET_NDRIVER_MAPS, + NEU_RESP_GET_NDRIVER_MAPS, NEU_REQ_UPDATE_LOG_LEVEL, @@ -209,8 +211,10 @@ static const char *neu_reqresp_type_string_t[] = { [NEU_REQRESP_NODES_STATE] = "NEU_REQRESP_NODES_STATE", [NEU_REQRESP_NODE_DELETED] = "NEU_REQRESP_NODE_DELETED", - [NEU_REQ_ADD_NDRIVER_MAP] = "NEU_REQ_ADD_NDRIVER_MAP", - [NEU_REQ_DEL_NDRIVER_MAP] = "NEU_REQ_DEL_NDRIVER_MAP", + [NEU_REQ_ADD_NDRIVER_MAP] = "NEU_REQ_ADD_NDRIVER_MAP", + [NEU_REQ_DEL_NDRIVER_MAP] = "NEU_REQ_DEL_NDRIVER_MAP", + [NEU_REQ_GET_NDRIVER_MAPS] = "NEU_REQ_GET_NDRIVER_MAPS", + [NEU_RESP_GET_NDRIVER_MAPS] = "NEU_RESP_GET_NDRIVER_MAPS", [NEU_REQ_UPDATE_LOG_LEVEL] = "NEU_REQ_UPDATE_LOG_LEVEL", @@ -646,6 +650,19 @@ typedef struct { char group[NEU_GROUP_NAME_LEN]; } neu_req_ndriver_map_t; +typedef struct { + char ndriver[NEU_NODE_NAME_LEN]; +} neu_req_get_ndriver_maps_t; + +typedef struct { + char driver[NEU_NODE_NAME_LEN]; + char group[NEU_GROUP_NAME_LEN]; +} neu_resp_ndriver_map_info_t; + +typedef struct { + UT_array *groups; // array of neu_resp_ndriver_map_info_t +} neu_resp_get_ndriver_maps_t; + typedef struct neu_req_update_log_level { char node[NEU_NODE_NAME_LEN]; } neu_req_update_log_level_t; diff --git a/plugins/restful/handle.c b/plugins/restful/handle.c index 7c0d10e1..dac96443 100644 --- a/plugins/restful/handle.c +++ b/plugins/restful/handle.c @@ -442,6 +442,12 @@ static struct neu_http_handler rest_handlers[] = { .url = "/api/v2/ndriver/map", .value.handler = handle_del_ndriver_map, }, + { + .method = NEU_HTTP_METHOD_GET, + .type = NEU_HTTP_HANDLER_FUNCTION, + .url = "/api/v2/ndriver/map", + .value.handler = handle_get_ndriver_maps, + }, }; void neu_rest_handler(const struct neu_http_handler **handlers, uint32_t *size) diff --git a/plugins/restful/ndriver_handle.c b/plugins/restful/ndriver_handle.c index 47859574..485bca22 100644 --- a/plugins/restful/ndriver_handle.c +++ b/plugins/restful/ndriver_handle.c @@ -84,3 +84,65 @@ void handle_del_ndriver_map(nng_aio *aio) } }) } + +void handle_get_ndriver_maps(nng_aio *aio) +{ + int ret = 0; + neu_plugin_t *plugin = neu_rest_get_plugin(); + + NEU_VALIDATE_JWT(aio); + + neu_req_get_ndriver_maps_t cmd = { 0 }; + if (neu_http_get_param_str(aio, "ndriver", cmd.ndriver, + sizeof(cmd.ndriver)) <= 0 || + strlen(cmd.ndriver) >= NEU_NODE_NAME_LEN) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, { + neu_http_response(aio, error_code.error, result_error); + }) + return; + } + + neu_reqresp_head_t header = { + .ctx = aio, + .type = NEU_REQ_GET_NDRIVER_MAPS, + }; + + ret = neu_plugin_op(plugin, header, &cmd); + if (ret != 0) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, { + neu_http_response(aio, NEU_ERR_IS_BUSY, result_error); + }); + } +} + +void handle_get_ndriver_maps_resp(nng_aio * aio, + neu_resp_get_ndriver_maps_t *groups) +{ + neu_json_ndriver_map_group_array_t group_arr = { 0 }; + + group_arr.n_group = utarray_len(groups->groups); + group_arr.groups = calloc(group_arr.n_group, sizeof(*group_arr.groups)); + + int index = 0; + utarray_foreach(groups->groups, neu_resp_ndriver_map_info_t *, group) + { + group_arr.groups[index].driver = group->driver; + group_arr.groups[index].group = group->group; + ++index; + } + + char *result = NULL; + neu_json_encode_by_fn(&group_arr, neu_json_encode_get_ndriver_maps_resp, + &result); + if (result) { + neu_http_ok(aio, result); + } else { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_EINTERNAL, { + neu_http_response(aio, NEU_ERR_EINTERNAL, result_error); + }); + } + + free(result); + free(group_arr.groups); + utarray_free(groups->groups); +} diff --git a/plugins/restful/ndriver_handle.h b/plugins/restful/ndriver_handle.h index 5e2e7e56..05b3ffbd 100644 --- a/plugins/restful/ndriver_handle.h +++ b/plugins/restful/ndriver_handle.h @@ -25,5 +25,8 @@ void handle_add_ndriver_map(nng_aio *aio); void handle_del_ndriver_map(nng_aio *aio); +void handle_get_ndriver_maps(nng_aio *aio); +void handle_get_ndriver_maps_resp(nng_aio * aio, + neu_resp_get_ndriver_maps_t *groups); #endif diff --git a/plugins/restful/rest.c b/plugins/restful/rest.c index f222b70d..9bd84e3a 100644 --- a/plugins/restful/rest.c +++ b/plugins/restful/rest.c @@ -31,6 +31,7 @@ #include "global_config_handle.h" #include "group_config_handle.h" #include "handle.h" +#include "ndriver_handle.h" #include "plugin_handle.h" #include "rest.h" #include "rw_handle.h" @@ -232,6 +233,10 @@ static int dashb_plugin_request(neu_plugin_t * plugin, handle_get_templates_resp(header->ctx, (neu_resp_get_templates_t *) data); break; + case NEU_RESP_GET_NDRIVER_MAPS: + handle_get_ndriver_maps_resp(header->ctx, + (neu_resp_get_ndriver_maps_t *) data); + break; default: nlog_fatal("recv unhandle msg: %s", neu_reqresp_type_string(header->type)); diff --git a/src/adapter/adapter.c b/src/adapter/adapter.c index 155b0c98..72079361 100644 --- a/src/adapter/adapter.c +++ b/src/adapter/adapter.c @@ -452,6 +452,7 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data) case NEU_RESP_GET_PLUGIN: case NEU_RESP_GET_TEMPLATE: case NEU_RESP_GET_TEMPLATES: + case NEU_RESP_GET_NDRIVER_MAPS: case NEU_RESP_GET_GROUP: case NEU_RESP_ERROR: case NEU_REQRESP_TRANS_DATA: @@ -1391,6 +1392,12 @@ void *neu_msg_gen(neu_reqresp_head_t *header, void *data) case NEU_REQ_DEL_NDRIVER_MAP: data_size = sizeof(neu_req_ndriver_map_t); break; + case NEU_REQ_GET_NDRIVER_MAPS: + data_size = sizeof(neu_req_get_ndriver_maps_t); + break; + case NEU_RESP_GET_NDRIVER_MAPS: + data_size = sizeof(neu_resp_get_ndriver_maps_t); + break; case NEU_REQ_UPDATE_LOG_LEVEL: data_size = sizeof(neu_req_update_log_level_t); break; diff --git a/src/core/manager.c b/src/core/manager.c index b02bd9c5..accd34db 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -946,6 +946,28 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data) reply(manager, header, &error); break; } + case NEU_REQ_GET_NDRIVER_MAPS: { + neu_req_get_ndriver_maps_t *cmd = + (neu_req_get_ndriver_maps_t *) &header[1]; + neu_resp_error_t e = { 0 }; + neu_resp_get_ndriver_maps_t resp = { 0 }; + + // TODO + (void) cmd; + UT_icd icd = { sizeof(neu_resp_get_ndriver_maps_t), NULL, NULL, NULL }; + utarray_new(resp.groups, &icd); + + strcpy(header->receiver, header->sender); + if (0 == e.error) { + header->type = NEU_RESP_GET_NDRIVER_MAPS; + reply(manager, header, &resp); + } else { + header->type = NEU_RESP_ERROR; + reply(manager, header, &e); + } + + break; + } case NEU_REQ_UPDATE_LOG_LEVEL: if (neu_node_manager_find(manager->node_manager, header->receiver) == NULL) { diff --git a/src/parser/neu_json_ndriver.c b/src/parser/neu_json_ndriver.c index 910e3be2..4880bb72 100644 --- a/src/parser/neu_json_ndriver.c +++ b/src/parser/neu_json_ndriver.c @@ -116,3 +116,51 @@ void neu_json_decode_ndriver_map_free(neu_json_ndriver_map_t *req) free(req); } } + +int neu_json_encode_ndriver_group_array(void *json_arr, void *param) +{ + neu_json_ndriver_map_group_array_t *resp = + (neu_json_ndriver_map_group_array_t *) param; + + for (int i = 0; i < resp->n_group; i++) { + json_t *ob = json_object(); + + if (0 != + json_object_set_new(ob, "driver", + json_string(resp->groups[i].driver)) || + 0 != + json_object_set_new(ob, "group", + json_string(resp->groups[i].group)) || + 0 != json_array_append_new(json_arr, ob)) { + json_decref(ob); + return -1; + } + } + + return 0; +} + +int neu_json_encode_get_ndriver_maps_resp(void *json_obj, void *param) +{ + int ret = 0; + + void *group_array = neu_json_array(); + if (NULL == group_array) { + return -1; + } + + if (0 != neu_json_encode_ndriver_group_array(group_array, param)) { + neu_json_encode_free(group_array); + return -1; + } + + neu_json_elem_t resp_elems[] = { { + .name = "groups", + .t = NEU_JSON_OBJECT, + .v.val_object = group_array, + } }; + ret = neu_json_encode_field(json_obj, resp_elems, + NEU_JSON_ELEM_SIZE(resp_elems)); + + return ret; +} diff --git a/src/parser/neu_json_ndriver.h b/src/parser/neu_json_ndriver.h index b50e8d7d..6abf6d7b 100644 --- a/src/parser/neu_json_ndriver.h +++ b/src/parser/neu_json_ndriver.h @@ -36,6 +36,20 @@ int neu_json_encode_ndriver_map(void *json_object, void *param); int neu_json_decode_ndriver_map(char *buf, neu_json_ndriver_map_t **result); void neu_json_decode_ndriver_map_free(neu_json_ndriver_map_t *req); +typedef struct { + char *driver; + char *group; +} neu_json_ndriver_map_group_t; + +typedef struct { + int n_group; + neu_json_ndriver_map_group_t *groups; +} neu_json_ndriver_map_group_array_t; + +int neu_json_encode_ndriver_group_array(void *json_obj, void *param); + +int neu_json_encode_get_ndriver_maps_resp(void *json_object, void *param); + #ifdef __cplusplus } #endif