Skip to content

Commit

Permalink
style(src): clang-format bridging plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
precla committed Sep 22, 2023
1 parent 1029fb4 commit 5cbb4bd
Show file tree
Hide file tree
Showing 28 changed files with 2,060 additions and 2,060 deletions.
74 changes: 37 additions & 37 deletions src/bridging/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,48 @@ static void sigint_handler(__attribute__((unused)) int signum);

int main(void)
{
int error = SR_ERR_OK;
sr_conn_ctx_t *connection = NULL;
sr_session_ctx_t *session = NULL;
void *private_data = NULL;

sr_log_stderr(SR_LL_INF);

/* connect to sysrepo */
error = sr_connect(SR_CONN_DEFAULT, &connection);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_connect error (%d): %s", error, sr_strerror(error));
goto out;
}

error = sr_session_start(connection, SR_DS_RUNNING, &session);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_session_start error (%d): %s", error, sr_strerror(error));
goto out;
}

error = sr_plugin_init_cb(session, &private_data);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_plugin_init_cb error");
goto out;
}

/* loop until ctrl-c is pressed / SIGINT is received */
signal(SIGINT, sigint_handler);
signal(SIGPIPE, SIG_IGN);
while (!exit_application) {
sleep(1);
}
int error = SR_ERR_OK;
sr_conn_ctx_t* connection = NULL;
sr_session_ctx_t* session = NULL;
void* private_data = NULL;

sr_log_stderr(SR_LL_INF);

/* connect to sysrepo */
error = sr_connect(SR_CONN_DEFAULT, &connection);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_connect error (%d): %s", error, sr_strerror(error));
goto out;
}

error = sr_session_start(connection, SR_DS_RUNNING, &session);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_session_start error (%d): %s", error, sr_strerror(error));
goto out;
}

error = sr_plugin_init_cb(session, &private_data);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_plugin_init_cb error");
goto out;
}

/* loop until ctrl-c is pressed / SIGINT is received */
signal(SIGINT, sigint_handler);
signal(SIGPIPE, SIG_IGN);
while (!exit_application) {
sleep(1);
}

out:
sr_plugin_cleanup_cb(session, private_data);
sr_disconnect(connection);
sr_plugin_cleanup_cb(session, private_data);
sr_disconnect(connection);

return error ? -1 : 0;
return error ? -1 : 0;
}

static void sigint_handler(__attribute__((unused)) int signum)
{
SRPLG_LOG_INF(PLUGIN_NAME, "Sigint called, exiting...");
exit_application = 1;
SRPLG_LOG_INF(PLUGIN_NAME, "Sigint called, exiting...");
exit_application = 1;
}
266 changes: 133 additions & 133 deletions src/bridging/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,147 +20,147 @@
// libyang
#include <libyang/tree_data.h>

int sr_plugin_init_cb(sr_session_ctx_t *running_session, void **private_data)
int sr_plugin_init_cb(sr_session_ctx_t* running_session, void** private_data)
{
int error = 0;
bool empty_startup = true;

// sysrepo
sr_session_ctx_t *startup_session = NULL;
sr_conn_ctx_t *connection = NULL;
sr_subscription_ctx_t *subscription = NULL;

// plugin
bridging_ctx_t *ctx = NULL;

// first entry has highest priority, last lowest priority
bridging_module_change_t module_changes[] = {
{
// bridge change subscription
BASE_YANG_MODEL,
{
BRIDGING_BRIDGE_LIST_YANG_PATH,
bridging_bridge_list_change_cb,
},
},
{
// bridge-port change subscription
INTERFACES_YANG_MODEL,
{
INTERFACES_LIST_PATH,
bridge_port_change_cb,
},
},
{
// bridge filtering-database change subscription
BASE_YANG_MODEL,
{
BRIDGING_BRIDGE_COMPONENT_FILTERING_DATABASE_YANG_PATH,
bridging_bridge_list_change_cb,
},
},
};

// operational getters
srpc_operational_t oper[] = {
{
BASE_YANG_MODEL,
BRIDGING_BRIDGE_LIST_YANG_PATH,
bridging_oper_get_bridges,
},
{
BASE_YANG_MODEL,
BRIDGING_BRIDGE_COMPONENT_BRIDGE_VLAN_YANG_PATH,
bridging_oper_get_bridge_vlan,
},
};

// init context
ctx = xmalloc(sizeof(*ctx));
*ctx = (bridging_ctx_t){0};

*private_data = ctx;

connection = sr_session_get_connection(running_session);
error = sr_session_start(connection, SR_DS_STARTUP, &startup_session);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_session_start() error (%d): %s", error, sr_strerror(error));
goto error_out;
}

ctx->startup_session = startup_session;

SRPC_SAFE_CALL_ERR(error, srpc_check_empty_datastore(startup_session, BRIDGING_BRIDGE_LIST_YANG_PATH, &empty_startup), error_out);

if (empty_startup) {
SRPLG_LOG_INF(PLUGIN_NAME, "Startup datasore is empty");
SRPLG_LOG_INF(PLUGIN_NAME, "Loading initial system data");
error = bridging_startup_load_data(ctx, startup_session);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "Error loading initial data into the startup datastore... exiting");
goto error_out;
}

// copy contents of the startup session to the current running session
error = sr_copy_config(running_session, BASE_YANG_MODEL, SR_DS_STARTUP, 0);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_copy_config() error (%d): %s", error, sr_strerror(error));
goto error_out;
}
}

// subscribe every module change with priority set
for (uint32_t i = 0; i < ARRAY_SIZE(module_changes); i++) {
const bridging_module_change_t* change = &module_changes[i];

SRPLG_LOG_DBG(PLUGIN_NAME, "Subscribing module change callback %s", change->sub.path);

// in case of work on a specific callback set it to NULL
if (change->sub.cb) {
error = sr_module_change_subscribe(running_session, change->module_name, change->sub.path, change->sub.cb, *private_data, ARRAY_SIZE(module_changes) - i, SR_SUBSCR_DEFAULT, &subscription);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_module_change_subscribe() error for \"%s\" (%d): %s", change->sub.path, error, sr_strerror(error));
goto error_out;
}
}
}

// operational subscriptions - merge with existing data (SR_SUBSCR_OPER_MERGE flag)
for (uint32_t i = 0; i < ARRAY_SIZE(oper); i++) {
const srpc_operational_t* op = &oper[i];

SRPLG_LOG_DBG(PLUGIN_NAME, "Subscribing operational callback %s:%s", op->module, op->path);

// in case of work on a specific callback set it to NULL
if (op->cb) {
error = sr_oper_get_subscribe(running_session, op->module, op->path, op->cb, *private_data, SR_SUBSCR_DEFAULT | SR_SUBSCR_OPER_MERGE, &subscription);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_oper_get_subscribe() error for \"%s\" (%d): %s", op->path, error, sr_strerror(error));
goto error_out;
}
}
}

goto out;
int error = 0;
bool empty_startup = true;

// sysrepo
sr_session_ctx_t* startup_session = NULL;
sr_conn_ctx_t* connection = NULL;
sr_subscription_ctx_t* subscription = NULL;

// plugin
bridging_ctx_t* ctx = NULL;

// first entry has highest priority, last lowest priority
bridging_module_change_t module_changes[] = {
{
// bridge change subscription
BASE_YANG_MODEL,
{
BRIDGING_BRIDGE_LIST_YANG_PATH,
bridging_bridge_list_change_cb,
},
},
{
// bridge-port change subscription
INTERFACES_YANG_MODEL,
{
INTERFACES_LIST_PATH,
bridge_port_change_cb,
},
},
{
// bridge filtering-database change subscription
BASE_YANG_MODEL,
{
BRIDGING_BRIDGE_COMPONENT_FILTERING_DATABASE_YANG_PATH,
bridging_bridge_list_change_cb,
},
},
};

// operational getters
srpc_operational_t oper[] = {
{
BASE_YANG_MODEL,
BRIDGING_BRIDGE_LIST_YANG_PATH,
bridging_oper_get_bridges,
},
{
BASE_YANG_MODEL,
BRIDGING_BRIDGE_COMPONENT_BRIDGE_VLAN_YANG_PATH,
bridging_oper_get_bridge_vlan,
},
};

// init context
ctx = xmalloc(sizeof(*ctx));
*ctx = (bridging_ctx_t) { 0 };

*private_data = ctx;

connection = sr_session_get_connection(running_session);
error = sr_session_start(connection, SR_DS_STARTUP, &startup_session);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_session_start() error (%d): %s", error, sr_strerror(error));
goto error_out;
}

ctx->startup_session = startup_session;

SRPC_SAFE_CALL_ERR(error, srpc_check_empty_datastore(startup_session, BRIDGING_BRIDGE_LIST_YANG_PATH, &empty_startup), error_out);

if (empty_startup) {
SRPLG_LOG_INF(PLUGIN_NAME, "Startup datasore is empty");
SRPLG_LOG_INF(PLUGIN_NAME, "Loading initial system data");
error = bridging_startup_load_data(ctx, startup_session);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "Error loading initial data into the startup datastore... exiting");
goto error_out;
}

// copy contents of the startup session to the current running session
error = sr_copy_config(running_session, BASE_YANG_MODEL, SR_DS_STARTUP, 0);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_copy_config() error (%d): %s", error, sr_strerror(error));
goto error_out;
}
}

// subscribe every module change with priority set
for (uint32_t i = 0; i < ARRAY_SIZE(module_changes); i++) {
const bridging_module_change_t* change = &module_changes[i];

SRPLG_LOG_DBG(PLUGIN_NAME, "Subscribing module change callback %s", change->sub.path);

// in case of work on a specific callback set it to NULL
if (change->sub.cb) {
error = sr_module_change_subscribe(running_session, change->module_name, change->sub.path, change->sub.cb, *private_data, ARRAY_SIZE(module_changes) - i, SR_SUBSCR_DEFAULT, &subscription);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_module_change_subscribe() error for \"%s\" (%d): %s", change->sub.path, error, sr_strerror(error));
goto error_out;
}
}
}

// operational subscriptions - merge with existing data (SR_SUBSCR_OPER_MERGE flag)
for (uint32_t i = 0; i < ARRAY_SIZE(oper); i++) {
const srpc_operational_t* op = &oper[i];

SRPLG_LOG_DBG(PLUGIN_NAME, "Subscribing operational callback %s:%s", op->module, op->path);

// in case of work on a specific callback set it to NULL
if (op->cb) {
error = sr_oper_get_subscribe(running_session, op->module, op->path, op->cb, *private_data, SR_SUBSCR_DEFAULT | SR_SUBSCR_OPER_MERGE, &subscription);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_oper_get_subscribe() error for \"%s\" (%d): %s", op->path, error, sr_strerror(error));
goto error_out;
}
}
}

goto out;

error_out:
error = -1;
SRPLG_LOG_ERR(PLUGIN_NAME, "Error occured while initializing the plugin (%d)", error);
error = -1;
SRPLG_LOG_ERR(PLUGIN_NAME, "Error occured while initializing the plugin (%d)", error);
out:
return error ? SR_ERR_CALLBACK_FAILED : SR_ERR_OK;
return error ? SR_ERR_CALLBACK_FAILED : SR_ERR_OK;
}

void sr_plugin_cleanup_cb(sr_session_ctx_t *running_session, void *private_data)
void sr_plugin_cleanup_cb(sr_session_ctx_t* running_session, void* private_data)
{
int error = 0;
int error = 0;

bridging_ctx_t *ctx = (bridging_ctx_t *) private_data;
bridging_ctx_t* ctx = (bridging_ctx_t*)private_data;

// save current running configuration into startup for next time when the plugin starts
error = sr_copy_config(ctx->startup_session, BASE_YANG_MODEL, SR_DS_RUNNING, 0);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_copy_config() error (%d): %s", error, sr_strerror(error));
}
// save current running configuration into startup for next time when the plugin starts
error = sr_copy_config(ctx->startup_session, BASE_YANG_MODEL, SR_DS_RUNNING, 0);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_copy_config() error (%d): %s", error, sr_strerror(error));
}

FREE_SAFE(ctx);
FREE_SAFE(ctx);
}
4 changes: 2 additions & 2 deletions src/bridging/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <sysrepo_types.h>

int sr_plugin_init_cb(sr_session_ctx_t *session, void **private_data);
void sr_plugin_cleanup_cb(sr_session_ctx_t *session, void *private_data);
int sr_plugin_init_cb(sr_session_ctx_t* session, void** private_data);
void sr_plugin_cleanup_cb(sr_session_ctx_t* session, void* private_data);

#endif // BRIDGING_PLUGIN_H
Loading

0 comments on commit 5cbb4bd

Please sign in to comment.