Skip to content

Commit

Permalink
cmdgen: support for cmdgen functionality (#12)
Browse files Browse the repository at this point in the history
* cmdgen: initial support for cmdgen; add test cases.
  • Loading branch information
Ali-aqrabawi authored Feb 22, 2024
1 parent 42f4734 commit 8af0490
Show file tree
Hide file tree
Showing 10 changed files with 649 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/actions-compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,7 @@ jobs:
echo "Failed: did not exit with code 1."
exit 1
fi
- name: run iproute2-sysrepo tests
run : chmod +x tests/run_tests.sh && sudo ./tests/run_tests.sh
- name: yanglint
run : make check
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ EXEC = iproute2-sysrepo
BIN = bin

CC ?= gcc
LDFLAGS = -lsysrepo -lbpf -lelf -lmnl -lbsd -lcap -lselinux -lm -ldl -rdynamic -L/usr/local/lib
LDFLAGS = -lyang -lsysrepo -lbpf -lelf -lmnl -lbsd -lcap -lselinux -lm -ldl -rdynamic -L/usr/local/lib
SUBDIRS = iproute2

IPR2_SR_LIB_SRC = $(wildcard src/lib/*.c)
Expand Down Expand Up @@ -65,3 +65,4 @@ $(IPR2_SR_LIB_OBJ): $(IPR2_SR_LIB_SRC)

$(IPR2_SR_OBJ): $(IPR2_SR_SRC)
$(CC) -c $< -o $@ -Iiproute2/ip -Iiproute2/bridge -Iiproute2/tc -Iiproute2/include

59 changes: 51 additions & 8 deletions src/iproute2_sysrepo.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,30 @@ int ip_sr_config_change_cb_apply(const struct lyd_node *change_dnode)
}

ipr2_cmds = lyd2cmd_argv(change_dnode);

if (ipr2_cmds == NULL){
fprintf(stderr,
"%s: failed to generate commands for the change \n",
__func__);
return EXIT_FAILURE;
}
for (int i = 0; ipr2_cmds[i] != NULL; i++) {
fprintf(stdout,"%s: executing command: ",__func__ );
for (int k = 0; k < ipr2_cmds[i]->argc; k++) {
printf("%s", ipr2_cmds[i]->argv[k]);
if (i < ipr2_cmds[i]->argc - 1) {
printf(" ");
}
}
fprintf(stdout,"\n");

ret = do_cmd(ipr2_cmds[i]->argc, ipr2_cmds[i]->argv);
if (ret != EXIT_SUCCESS)// TODO: add rollback functionality.
if (ret != EXIT_SUCCESS) {
// TODO: add rollback functionality.
free(ipr2_cmds);
return SR_ERR_INTERNAL;
}
}
free(ipr2_cmds);

return SR_ERR_OK;
}
Expand All @@ -369,24 +387,49 @@ int ip_sr_config_change_cb(sr_session_ctx_t *session, uint32_t sub_id,
sr_event_t sr_ev, uint32_t request_id,
void *private_data)
{
sr_change_iter_t *it;
int ret;
struct lyd_node *root_dnode;
const struct lyd_node *dnode;
sr_change_oper_t sr_op;

ret = sr_get_changes_iter(session, "//*", &it);
if (ret != SR_ERR_OK) {
fprintf(stderr,
"%s: sr_get_changes_iter() failed for \"%s\"",
__func__, module_name);
return ret;
}

// we don't iterate through all changes, we just get the first changed node, then find it's root.
// this root will have all the changes. NOTE: this approach will not work if the yang has more than
// root containers. which is not the case in iproute2-yang modules.
ret = sr_get_change_tree_next(session, it, &sr_op,
&dnode, NULL,
NULL, NULL);
if (ret != SR_ERR_OK){
fprintf(stderr, "%s: failed to get next change node: %s\n",
__func__ ,sr_strerror(ret));
return SR_ERR_INTERNAL;
}

root_dnode = (struct lyd_node *) dnode;
while (root_dnode->parent != NULL)
root_dnode = (struct lyd_node *) root_dnode->parent;

switch (sr_ev) {
case SR_EV_ENABLED:
case SR_EV_CHANGE:
dnode = sr_get_changes(session);
ret = ip_sr_config_change_cb_prepare(dnode);
ret = ip_sr_config_change_cb_apply(root_dnode);
break;
case SR_EV_DONE:
dnode = sr_get_change_diff(session);
ret = ip_sr_config_change_cb_apply(dnode);
break;
// TODO: add cleanup functionality.
case SR_EV_ABORT:
// TODO: add abort event functionality.
case SR_EV_RPC:
// TODO: rpc support for iproute2 commands.
case SR_EV_UPDATE:
return 0;
return SR_ERR_OK;
default:
fprintf(stderr, "%s: unexpected sysrepo event: %u\n", __func__,
sr_ev);
Expand Down
Loading

0 comments on commit 8af0490

Please sign in to comment.