Skip to content

Commit

Permalink
cmdgen,make,gitignore: move oper_t struct and get_operation header to…
Browse files Browse the repository at this point in the history
… cmdgen.h (#16)

* cmdgen,make: move oper_t struct and get_operation header to cmdgen.h,add option to clean sysrepo module only

---------

Signed-off-by: Amjad Daraiseh <adaraiseh@okdanetworks.com>
  • Loading branch information
adaraiseh authored Feb 23, 2024
1 parent ebfde74 commit f61caaf
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ dkms.conf

# local outputs
bin/iproute2-sysrepo

#vscode
.vscode/
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 && \
Expand Down
4 changes: 2 additions & 2 deletions src/iproute2_sysrepo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 9 additions & 16 deletions src/lib/change_cmdgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
46 changes: 32 additions & 14 deletions src/lib/cmdgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f61caaf

Please sign in to comment.