From f839095692915fff9fe1a96235425e7ca09c6c3e Mon Sep 17 00:00:00 2001 From: fengzero Date: Mon, 22 Nov 2021 09:31:16 +0000 Subject: [PATCH] api: node supports updating name --- docs/json-api/neu_json_node.json | 5 ++-- ft/node_api_test.robot | 43 ++++++++++++++++++++++++-------- ft/tag_api_test.robot | 9 ++++--- include/json/neu_json_mqtt.h | 2 ++ include/json/neu_json_node.h | 3 +-- include/neu_adapter.h | 7 ++---- include/neu_plugin.h | 5 ++-- plugins/mqtt/command/node.c | 24 ++++++++++-------- plugins/restful/adapter_handle.c | 13 +++------- plugins/restful/http.c | 1 + src/adapter/adapter.c | 6 +++++ src/adapter/adapter_internal.h | 1 + src/base/neu_plugin_common.c | 12 ++++----- src/core/neu_manager.c | 25 ++++++++++++++++--- src/parser/neu_json_node.c | 12 +++------ tests/json_nodes_test.cc | 8 +++--- 16 files changed, 105 insertions(+), 71 deletions(-) diff --git a/docs/json-api/neu_json_node.json b/docs/json-api/neu_json_node.json index f306025d..039f4856 100644 --- a/docs/json-api/neu_json_node.json +++ b/docs/json-api/neu_json_node.json @@ -8,9 +8,8 @@ "id": 7 }, "update_node_req": { - "type": 1, - "name": "modbus-tcp-node", - "plugin_name": "modbus-plugin-tcp" + "id": 1, + "name": "modbus-tcp-node" }, "get_nodes_req": { "node_type": 1 diff --git a/ft/node_api_test.robot b/ft/node_api_test.robot index 3a19ee09..6da98d11 100644 --- a/ft/node_api_test.robot +++ b/ft/node_api_test.robot @@ -124,22 +124,21 @@ Add setting to non-existent node, it should return failure Integer response status 404 Integer response body error ${ERR_NODE_NOT_EXIST} +Get setting from non-existent node, it should return failure + GET /api/v2/node/setting?node_id=999 -#Get setting from non-existent node, it should return failure - #GET /api/v2/node/setting?node_id=999 + Integer response status 404 + Integer response body error ${ERR_NODE_NOT_EXIST} - #Integer response status 404 - #Integer response body error ${ERR_NODE_NOT_EXIST} +Get setting from a node that has never been set, it should return failure + ${driver_node_id} Add Node ${NODE_DRIVER} driver-nodee ${PLUGIN_MODBUS} -#Get setting from a node that has never been set, it should return failure - #${driver_node_id} Add Node ${NODE_DRIVER} driver-node1 ${PLUGIN_MODBUS} + GET /api/v2/node/setting?node_id=${driver_node_id} - #GET /api/v2/node/setting?node_id=${driver_node_id} + Integer response status 404 + Integer response body error ${ERR_NODE_SETTING_NOT_EXIST} - #Integer response status 404 - #Integer response body error ${ERR_NODE_SETTING_NOT_EXIST} - - #Del Node ${driver_node_id} + Del Node ${driver_node_id} Add the correct settings to the node, it should return success ${driver_node_id} Add Node ${NODE_DRIVER} driver-node ${PLUGIN_MODBUS} @@ -170,6 +169,28 @@ Add wrong settings to node, it should return failure Integer response status 400 Integer response body error ${ERR_NODE_SETTING_INVALID} +Update a node that does not exist, it should return failure + PUT /api/v2/node {"id": 99, "name": "test-name"} + + Integer response status 404 + Integer response body error ${ERR_NODE_NOT_EXIST} + +Update the name of the node, it should return success + ${app_node_id} Add Node ${NODE_APP} app-node ${PLUGIN_MQTT} + + PUT /api/v2/node {"id": ${app_node_id}, "name": "test-name"} + + Integer response status 200 + + GET /api/v2/node?type=${NODE_APP} + + Integer response status 200 + Array $.nodes + String $.nodes[*].name sample-app-adapter test-name + + Del Node ${app_node_id} + + #Start an unconfigured node, it should return failure diff --git a/ft/tag_api_test.robot b/ft/tag_api_test.robot index 73c0678a..27ea60b5 100644 --- a/ft/tag_api_test.robot +++ b/ft/tag_api_test.robot @@ -80,10 +80,11 @@ Delete non-existent tags, it should return success Integer response status 200 Integer response body error ${ERR_SUCCESS} -#Query tag from non-existent node, it should return failure - #GET /api/v2/tags?node_id=999 - #Integer response status 404 - #Integer response body error ${ERR_NODE_NOT_EXIST} +Query tag from non-existent node, it should return failure + GET /api/v2/tags?node_id=999 + + Integer response status 404 + Integer response body error ${ERR_NODE_NOT_EXIST} After adding the tag, query the tag, it should return the added tag ${test_node_id} Get Node ID ${NODE_DRIVER} ${test_node} diff --git a/include/json/neu_json_mqtt.h b/include/json/neu_json_mqtt.h index e02c0b24..b20be79c 100644 --- a/include/json/neu_json_mqtt.h +++ b/include/json/neu_json_mqtt.h @@ -70,6 +70,8 @@ typedef enum neu_mqtt_function { NEU_MQTT_OP_DELETE_PLUGIN, NEU_MQTT_OP_GET_PLUGIN, + NEU_MQTT_OP_PING, + } neu_mqtt_function_e; typedef struct { diff --git a/include/json/neu_json_node.h b/include/json/neu_json_node.h index 65f5cff4..aa34d757 100644 --- a/include/json/neu_json_node.h +++ b/include/json/neu_json_node.h @@ -73,9 +73,8 @@ typedef struct { int neu_json_encode_get_nodes_resp(void *json_object, void *param); typedef struct { + int64_t id; char * name; - int64_t type; - char * plugin_name; } neu_json_update_node_req_t; int neu_json_decode_update_node_req(char * buf, diff --git a/include/neu_adapter.h b/include/neu_adapter.h index 9183179e..21fe5764 100644 --- a/include/neu_adapter.h +++ b/include/neu_adapter.h @@ -162,11 +162,8 @@ typedef struct neu_cmd_del_node { /* NEU_REQRESP_UPDATE_NODE */ typedef struct neu_cmd_update_node { - neu_node_type_e node_type; - const char * adapter_name; - const char * plugin_name; - // optional value, it is nothing when set to 0 - plugin_id_t plugin_id; + neu_node_id_t node_id; + const char * node_name; } neu_cmd_update_node_t; /* NEU_REQRESP_GET_NODES */ diff --git a/include/neu_plugin.h b/include/neu_plugin.h index da656e10..fbd623f6 100644 --- a/include/neu_plugin.h +++ b/include/neu_plugin.h @@ -73,9 +73,8 @@ neu_datatag_table_t *neu_system_get_datatags_table(neu_plugin_t *plugin, intptr_t neu_system_add_node(neu_plugin_t *plugin, neu_node_type_e node_type, const char *adapter_name, const char *plugin_name); intptr_t neu_system_del_node(neu_plugin_t *plugin, neu_node_id_t node_id); -intptr_t neu_system_update_node(neu_plugin_t *plugin, neu_node_type_e node_type, - const char *adapter_name, - const char *plugin_name); +intptr_t neu_system_update_node(neu_plugin_t *plugin, neu_node_id_t node_id, + const char *node_name); // uninit vector vector_t neu_system_get_nodes(neu_plugin_t *plugin, neu_node_type_e node_type); intptr_t neu_system_add_group_config(neu_plugin_t * plugin, diff --git a/plugins/mqtt/command/node.c b/plugins/mqtt/command/node.c index 5c4b14ba..c3cddf96 100644 --- a/plugins/mqtt/command/node.c +++ b/plugins/mqtt/command/node.c @@ -74,17 +74,21 @@ char *command_add_node(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, char *command_update_node(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, neu_json_update_node_req_t *req) { - log_info("Update node uuid:%s, node type:%d", mqtt->uuid, req->type); - intptr_t rc = - neu_system_update_node(plugin, req->type, req->name, req->plugin_name); - char *json_str = NULL; - if (rc != 0) { - json_str = strdup("{\"error\": 1}"); - } else { - json_str = strdup("{\"error\": 0}"); - } + (void) plugin; + (void) mqtt; + (void) req; + // log_info("Update node uuid:%s, node type:%d", mqtt->uuid, req->type); + // intptr_t rc = + // neu_system_update_node(plugin, req->type, req->name, req->plugin_name); + // char *json_str = NULL; + // if (rc != 0) { + // json_str = strdup("{\"error\": 1}"); + //} else { + // json_str = strdup("{\"error\": 0}"); + //} - return json_str; + // return json_str; + return NULL; } char *command_delete_node(neu_plugin_t *plugin, neu_json_mqtt_t *mqtt, diff --git a/plugins/restful/adapter_handle.c b/plugins/restful/adapter_handle.c index 3d1fde33..64b0d14f 100644 --- a/plugins/restful/adapter_handle.c +++ b/plugins/restful/adapter_handle.c @@ -69,15 +69,10 @@ void handle_update_adapter(nng_aio *aio) neu_plugin_t *plugin = neu_rest_get_plugin(); REST_PROCESS_HTTP_REQUEST( - aio, neu_json_update_node_req_t, neu_json_decode_update_node_req, { - intptr_t err = neu_system_update_node(plugin, req->type, req->name, - req->plugin_name); - if (err != 0) { - http_bad_request(aio, "{\"error\": 1}"); - } else { - http_ok(aio, "{\"error\": 0}"); - } - }) + aio, neu_json_update_node_req_t, neu_json_decode_update_node_req, + { NEU_JSON_RESPONSE_ERROR( + neu_system_update_node(plugin, req->id, req->name), + { http_response(aio, error_code.error, result_error); }) }) } void handle_get_adapter(nng_aio *aio) diff --git a/plugins/restful/http.c b/plugins/restful/http.c index 460c6736..a50037d9 100644 --- a/plugins/restful/http.c +++ b/plugins/restful/http.c @@ -78,6 +78,7 @@ static char *get_param(const char *url, const char *name) for (uint8_t i = 0; i < 64 && i < strlen(find); i++) { if (*(find + len + i) == '&' || *(find + len + i) == '\0') { + s_value[i] = '\0'; break; } s_value[i] = *(find + len + i); diff --git a/src/adapter/adapter.c b/src/adapter/adapter.c index a33b2048..4ee415cd 100644 --- a/src/adapter/adapter.c +++ b/src/adapter/adapter.c @@ -1053,6 +1053,12 @@ const char *neu_adapter_get_name(neu_adapter_t *adapter) return (const char *) adapter->name; } +void neu_adapter_rename(neu_adapter_t *adapter, const char *name) +{ + free(adapter->name); + adapter->name = strdup(name); +} + neu_manager_t *neu_adapter_get_manager(neu_adapter_t *adapter) { if (adapter == NULL) { diff --git a/src/adapter/adapter_internal.h b/src/adapter/adapter_internal.h index bfb1b988..d446ead1 100644 --- a/src/adapter/adapter_internal.h +++ b/src/adapter/adapter_internal.h @@ -33,6 +33,7 @@ void neu_adapter_destroy(neu_adapter_t *adapter); int neu_adapter_start(neu_adapter_t *adapter); int neu_adapter_stop(neu_adapter_t *adapter); const char * neu_adapter_get_name(neu_adapter_t *adapter); +void neu_adapter_rename(neu_adapter_t *adapter, const char *name); neu_manager_t *neu_adapter_get_manager(neu_adapter_t *adapter); nng_socket neu_adapter_get_sock(neu_adapter_t *adapter); adapter_id_t neu_adapter_get_id(neu_adapter_t *adapter); diff --git a/src/base/neu_plugin_common.c b/src/base/neu_plugin_common.c index a20dc03e..3ddb39bf 100644 --- a/src/base/neu_plugin_common.c +++ b/src/base/neu_plugin_common.c @@ -147,16 +147,14 @@ intptr_t neu_system_del_node(neu_plugin_t *plugin, neu_node_id_t node_id) return errorcode; } -intptr_t neu_system_update_node(neu_plugin_t *plugin, neu_node_type_e node_type, - const char *adapter_name, - const char *plugin_name) +intptr_t neu_system_update_node(neu_plugin_t *plugin, neu_node_id_t node_id, + const char *node_name) { - intptr_t errorcode = -1; + intptr_t errorcode = NEU_ERR_SUCCESS; neu_cmd_update_node_t node_update_cmd = { 0 }; - node_update_cmd.adapter_name = adapter_name; - node_update_cmd.plugin_name = plugin_name; - node_update_cmd.node_type = node_type; + node_update_cmd.node_name = node_name; + node_update_cmd.node_id = node_id; PLUGIN_CALL_CMD(plugin, NEU_REQRESP_UPDATE_NODE, node_update_cmd, intptr_t, { errorcode = (intptr_t) resp; }) diff --git a/src/core/neu_manager.c b/src/core/neu_manager.c index fc7ac40c..c48e2c3a 100644 --- a/src/core/neu_manager.c +++ b/src/core/neu_manager.c @@ -1546,10 +1546,29 @@ int neu_manager_del_node(neu_manager_t *manager, neu_node_id_t node_id) int neu_manager_update_node(neu_manager_t *manager, neu_cmd_update_node_t *cmd) { - int rv = 0; + int rv = NEU_ERR_NODE_NOT_EXIST; + size_t index = 0; + neu_adapter_t *adapter = NULL; + vector_t * reg_adapters = &manager->reg_adapters; + adapter_id_t adapter_id = + neu_manager_adapter_id_from_node_id(manager, cmd->node_id); + + nng_mtx_lock(manager->adapters_mtx); + index = find_reg_adapter_index_by_id(reg_adapters, adapter_id); + if (index != SIZE_MAX) { + adapter_reg_entity_t *reg_entity; + + reg_entity = (adapter_reg_entity_t *) vector_get(reg_adapters, index); + adapter = reg_entity->adapter; + + if (adapter != NULL) { + + neu_adapter_rename(adapter, cmd->node_name); + rv = NEU_ERR_SUCCESS; + } + } + nng_mtx_unlock(manager->adapters_mtx); - (void) manager; - (void) cmd; return rv; } diff --git a/src/parser/neu_json_node.c b/src/parser/neu_json_node.c index cbaead18..caee54a1 100644 --- a/src/parser/neu_json_node.c +++ b/src/parser/neu_json_node.c @@ -207,13 +207,9 @@ int neu_json_decode_update_node_req(char * buf, neu_json_update_node_req_t *req = calloc(1, sizeof(neu_json_update_node_req_t)); neu_json_elem_t req_elems[] = { { - .name = "type", + .name = "id", .t = NEU_JSON_INT, }, - { - .name = "plugin_name", - .t = NEU_JSON_STR, - }, { .name = "name", .t = NEU_JSON_STR, @@ -223,9 +219,8 @@ int neu_json_decode_update_node_req(char * buf, goto decode_fail; } - req->type = req_elems[0].v.val_int; - req->plugin_name = req_elems[1].v.val_str; - req->name = req_elems[2].v.val_str; + req->id = req_elems[0].v.val_int; + req->name = req_elems[1].v.val_str; *result = req; return ret; @@ -240,7 +235,6 @@ decode_fail: void neu_json_decode_update_node_req_free(neu_json_update_node_req_t *req) { - free(req->plugin_name); free(req->name); free(req); diff --git a/tests/json_nodes_test.cc b/tests/json_nodes_test.cc index a2bb66b1..e866c772 100644 --- a/tests/json_nodes_test.cc +++ b/tests/json_nodes_test.cc @@ -84,16 +84,14 @@ TEST(JsonNodesTest, DeleteNodesDecode) TEST(JsonNodesTest, UpdateNodesDecode) { - char *buf = (char *) "{\"type\": 1, " - "\"name\": \"adapter1\", " - "\"plugin_name\": \"plugin1\" }"; + char *buf = (char *) "{\"id\": 1, " + "\"name\": \"adapter1\" }"; neu_json_update_node_req_t *req = NULL; EXPECT_EQ(0, neu_json_decode_update_node_req(buf, &req)); - EXPECT_EQ(1, req->type); + EXPECT_EQ(1, req->id); EXPECT_STREQ("adapter1", req->name); - EXPECT_STREQ("plugin1", req->plugin_name); neu_json_decode_update_node_req_free(req); }