diff --git a/.gitignore b/.gitignore index 6d508d4..cf2d394 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,6 @@ dkms.conf # local outputs bin/iproute2-sysrepo + +#vscode +.vscode/ \ No newline at end of file diff --git a/Makefile b/Makefile index ea3bf7c..cfe14a4 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,12 @@ clean: rm -f $(IPR2_SR_LIB_OBJ) && \ rm -f $(BIN)/$(EXEC) +clean_srmod: + @set -e; \ + rm -f $(IPR2_SR_OBJ) && \ + rm -f $(IPR2_SR_LIB_OBJ) && \ + rm -f $(BIN)/$(EXEC) + iproute2/config.mk: @set -e; \ cd iproute2 && \ diff --git a/src/iproute2_sysrepo.c b/src/iproute2_sysrepo.c index e602b8a..2f878ca 100644 --- a/src/iproute2_sysrepo.c +++ b/src/iproute2_sysrepo.c @@ -347,12 +347,12 @@ int ip_sr_config_change_cb_prepare(const struct lyd_node *dnode) int ip_sr_config_change_cb_apply(const struct lyd_node *change_dnode) { int ret; - struct cmd_args **ipr2_cmds; + struct cmd_info **ipr2_cmds; if (change_dnode == NULL) { return SR_ERR_INVAL_ARG; } - ipr2_cmds = lyd2cmds_argv(change_dnode); + ipr2_cmds = lyd2cmds(change_dnode); if (ipr2_cmds == NULL){ fprintf(stderr, "%s: failed to generate commands for the change \n", diff --git a/src/lib/change_cmdgen.c b/src/lib/change_cmdgen.c index 86c3a44..e199823 100644 --- a/src/lib/change_cmdgen.c +++ b/src/lib/change_cmdgen.c @@ -18,13 +18,6 @@ #define CMD_LINE_SIZE 1024 -typedef enum { - ADD_OPR, - DELETE_OPR, - UPDATE_OPR, - UNKNOWN_OPR, -} oper_t; - typedef enum { // list extensions CMD_START_EXT, @@ -118,9 +111,8 @@ char *strip_yang_iden_prefix(const char *input) } -oper_t get_operation(struct lyd_node *dnode) +oper_t get_operation(const struct lyd_node *dnode) { - struct lyd_meta *next; const char *operation; LY_LIST_FOR(dnode->meta, next) @@ -215,7 +207,7 @@ void parse_command(const char *command, int *argc, char ***argv) free(cmd_copy); } -void add_command(char *cmd_line, struct cmd_args **cmds, int *cmd_idx, +void add_command(char *cmd_line, struct cmd_info **cmds, int *cmd_idx, char **oper2cmd_prefix, const struct lyd_node *start_dnode) { int argc; @@ -226,7 +218,7 @@ void add_command(char *cmd_line, struct cmd_args **cmds, int *cmd_idx, free_argv(argv, argc); return; } - (cmds)[*cmd_idx] = malloc(sizeof(struct cmd_args)); + (cmds)[*cmd_idx] = malloc(sizeof(struct cmd_info)); if ((cmds)[*cmd_idx] == NULL) { fprintf(stderr, "Memory allocation failed\n"); @@ -240,12 +232,12 @@ void add_command(char *cmd_line, struct cmd_args **cmds, int *cmd_idx, free_argv(argv, argc); } -struct cmd_args **lyd2cmds_argv(const struct lyd_node *change_node) +struct cmd_info **lyd2cmds(const struct lyd_node *change_node) { char *result; int cmd_idx = 0; char cmd_line[CMD_LINE_SIZE] = {0}; - struct cmd_args **cmds = malloc(CMDS_ARRAY_SIZE * sizeof(struct cmd_args *)); + struct cmd_info **cmds = malloc(CMDS_ARRAY_SIZE * sizeof(struct cmd_info *)); // Set all elements of the array to NULL for (int i = 0; i < CMDS_ARRAY_SIZE; i++) { cmds[i] = NULL; @@ -284,14 +276,15 @@ struct cmd_args **lyd2cmds_argv(const struct lyd_node *change_node) oper_t op_val; - struct lyd_node *next; + struct lyd_node *next, *startcmd_node; LYD_TREE_DFS_BEGIN(change_node, next) { // if this is startcmd node (schema has ipr2cgen:cmd-start), then start building a new command if (is_startcmd_node(next)) { + startcmd_node = next; // check if the cmd is not empty (first cmd) if (cmd_line[0] != 0) - add_command(cmd_line, cmds, &cmd_idx, oper2cmd_prefix, next); + add_command(cmd_line, cmds, &cmd_idx, oper2cmd_prefix, startcmd_node); // prepare for new command op_val = get_operation(next); @@ -342,7 +335,7 @@ struct cmd_args **lyd2cmds_argv(const struct lyd_node *change_node) LYD_TREE_DFS_END(change_node, next); } if (cmd_line[0] != 0) - add_command(cmd_line, cmds, &cmd_idx, oper2cmd_prefix,next); + add_command(cmd_line, cmds, &cmd_idx, oper2cmd_prefix, startcmd_node); return cmds; } diff --git a/src/lib/cmdgen.h b/src/lib/cmdgen.h index 500a22a..a35b2e6 100644 --- a/src/lib/cmdgen.h +++ b/src/lib/cmdgen.h @@ -7,33 +7,51 @@ #define CMDS_ARRAY_SIZE 1024 /** - * @brief data struct to store the int argc,char **argv pair. + * @brief data struct to store cmd operation. * - * this struct can be easily used by - * iproute2::do_cmd(argc,argv) */ -struct cmd_args{ +typedef enum { + ADD_OPR, + DELETE_OPR, + UPDATE_OPR, + UNKNOWN_OPR, +} oper_t; + +/** + * @brief data struct to store command information. + */ +struct cmd_info{ int argc; char **argv; - const struct lyd_node *cmd_start_dnode; + const struct lyd_node *cmd_start_dnode; /** lyd change node where the command starts + ipr2gen:cmd-start) */ }; /** - * @brief generate argc,argv commands out of the lyd_node (diff). + * @brief generate list of commands info from the lyd_node (diff). + * + * + * @param[in] change_node lyd_node of change data. + * @return cmd_info array. + */ +struct cmd_info** lyd2cmds(const struct lyd_node *change_node); + +/** + * @brief convert change cmd info to rollback cmd info. * * - * @param[in] node Data node diff to generate the argc, argv. - * @return Array of cmd_args struct. + * @param[in] change_node lyd_node of change data. + * @return cmd_info rollback cmd. */ -struct cmd_args** lyd2cmds_argv(const struct lyd_node *change_node); +struct cmd_info* lyd2rollback_cmd(const struct lyd_node *change_node); /** - * @brief generate argc,argv rollback command out of the lyd_node (diff). + * @brief get the change operation from change lyd_node. * * - * @param[in] node Data node diff to generate the argc, argv. - * @return Array of cmd_args struct. + * @param[in] lyd_node change node. + * @return oper_t */ -struct cmd_args* lyd2rollbackcmd_argv(const struct lyd_node *change_node); +oper_t get_operation(const struct lyd_node *dnode); -#endif// IPROUTE2_SYSREPO_CMDGEN_H +#endif// IPROUTE2_SYSREPO_CMDGEN_H \ No newline at end of file