Skip to content

Commit

Permalink
interfaces-plugin: add IPv4 address list libyang conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
zinccyy committed Dec 8, 2022
1 parent 53d01fd commit 9f1759a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 7 deletions.
79 changes: 78 additions & 1 deletion src/interfaces/src/plugin/data/interfaces/interface.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "interface.h"
#include "interface/ipv4.h"
#include "libyang/log.h"
#include "libyang/tree_data.h"
#include "plugin/common.h"
#include "plugin/ly_tree.h"
#include "plugin/types.h"
#include "srpc/ly_tree.h"
#include "sysrepo.h"
#include "uthash.h"
Expand Down Expand Up @@ -315,10 +318,84 @@ int interfaces_interface_hash_from_ly(interfaces_interface_hash_element_t** if_h
return error;
}

int interfaces_interface_hash_to_ly(interfaces_interface_hash_element_t* if_hash, struct lyd_node** interfaces_container_node)
int interfaces_interface_hash_to_ly(const struct ly_ctx* ly_ctx, interfaces_interface_hash_element_t* if_hash, struct lyd_node** interfaces_container_node)
{
int error = 0;

// interface iterator
interfaces_interface_hash_element_t *if_iter = NULL, *tmp = NULL;

// address iterators
interfaces_interface_ipv4_address_element_t* v4_addr_iter = NULL;
interfaces_interface_ipv6_address_element_t* v6_addr_iter = NULL;

// neighbor iterators
interfaces_interface_ipv4_neighbor_element_t* v4_neigh_iter = NULL;
interfaces_interface_ipv6_neighbor_element_t* v6_neigh_iter = NULL;

// libyang data
struct lyd_node* interface_list_node = NULL;
struct lyd_node *ipv4_container_node = NULL, *ipv6_container_node = NULL;
struct lyd_node *ipv4_address_node = NULL, *ipv6_address_node = NULL;
struct lyd_node *ipv4_neighbor_node = NULL, *ipv6_neighbor_node = NULL;

// buffers
char mtu_buffer[20] = { 0 };
char prefix_length_buffer[20] = { 0 };

HASH_ITER(hh, if_hash, if_iter, tmp)
{
// alloc new interface list node and fill that node with info
// name added via list creation
SRPC_SAFE_CALL_ERR(error, interfaces_ly_tree_create_interfaces_interface(ly_ctx, *interfaces_container_node, &interface_list_node, if_iter->interface.name), error_out);

// enabled
SRPC_SAFE_CALL_ERR(error, interfaces_ly_tree_create_interfaces_interface_enabled(ly_ctx, interface_list_node, if_iter->interface.enabled ? "true" : "false"), error_out);

// type
SRPC_SAFE_CALL_ERR(error, interfaces_ly_tree_create_interfaces_interface_type(ly_ctx, interface_list_node, if_iter->interface.type), error_out);

// IPv4
SRPC_SAFE_CALL_ERR(error, interfaces_ly_tree_create_interfaces_interface_ipv4(ly_ctx, interface_list_node, &ipv4_container_node), error_out);

// IPv4 properties
SRPC_SAFE_CALL_ERR(error, interfaces_ly_tree_create_interfaces_interface_ipv4_enabled(ly_ctx, ipv4_container_node, if_iter->interface.ipv4.enabled ? "enabled" : "disabled"), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_ly_tree_create_interfaces_interface_ipv4_forwarding(ly_ctx, ipv4_container_node, if_iter->interface.ipv4.forwarding ? "enabled" : "disabled"), error_out);

// write MTU to the buffer
SRPC_SAFE_CALL_ERR_COND(error, error < 0, snprintf(mtu_buffer, sizeof(mtu_buffer), "%d", if_iter->interface.ipv4.mtu), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_ly_tree_create_interfaces_interface_ipv4_mtu(ly_ctx, ipv4_container_node, mtu_buffer), error_out);

// address list
LL_FOREACH(if_iter->interface.ipv4.address, v4_addr_iter)
{
// create list element
SRPC_SAFE_CALL_ERR(error, interfaces_ly_tree_create_interfaces_interface_ipv4_address(ly_ctx, ipv4_container_node, &ipv4_address_node, v4_addr_iter->address.ip), error_out);

// add properties to the list element
switch (v4_addr_iter->address.subnet_type) {
case interfaces_interface_ipv4_address_subnet_none:
break;
case interfaces_interface_ipv4_address_subnet_prefix_length:
SRPC_SAFE_CALL_ERR_COND(error, error < 0, snprintf(prefix_length_buffer, sizeof(prefix_length_buffer), "%d", v4_addr_iter->address.subnet.prefix_length), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_ly_tree_create_interfaces_interface_ipv4_address_prefix_length(ly_ctx, ipv4_address_node, prefix_length_buffer), error_out);
break;
case interfaces_interface_ipv4_address_subnet_netmask:
SRPC_SAFE_CALL_ERR(error, interfaces_ly_tree_create_interfaces_interface_ipv4_address_netmask(ly_ctx, ipv4_address_node, v4_addr_iter->address.subnet.netmask), error_out);
break;
}
}

// IPv6
SRPC_SAFE_CALL_ERR(error, interfaces_ly_tree_create_interfaces_interface_ipv6(ly_ctx, interface_list_node, &ipv6_container_node), error_out);
}

goto out;

error_out:
error = -1;

out:
return error;
}

Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/src/plugin/data/interfaces/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
interfaces_interface_hash_element_t* interfaces_interface_hash_new(void);
void interfaces_interface_hash_print_debug(const interfaces_interface_hash_element_t* if_hash);
int interfaces_interface_hash_from_ly(interfaces_interface_hash_element_t** if_hash, const struct lyd_node* interface_list_node);
int interfaces_interface_hash_to_ly(interfaces_interface_hash_element_t* if_hash, struct lyd_node** interfaces_container_node);
int interfaces_interface_hash_to_ly(const struct ly_ctx* ly_ctx, interfaces_interface_hash_element_t* if_hash, struct lyd_node** interfaces_container_node);
int interfaces_interface_hash_add_element(interfaces_interface_hash_element_t** hash, interfaces_interface_hash_element_t* new_element);
interfaces_interface_hash_element_t* interfaces_interface_hash_get_element(interfaces_interface_hash_element_t** hash, const char* name);
void interfaces_interface_hash_free(interfaces_interface_hash_element_t** hash);
Expand Down
12 changes: 7 additions & 5 deletions src/interfaces/src/plugin/startup/load.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "load.h"
#include "libyang/printer_data.h"
#include "plugin/common.h"
#include "plugin/data/interfaces/interface.h"
#include "plugin/ly_tree.h"
Expand All @@ -25,7 +26,7 @@ int interfaces_startup_load(interfaces_ctx_t* ctx, sr_session_ctx_t* session)

srpc_startup_load_t load_values[] = {
{
"/ietf-interfaces:interfaces/interface[name='%s']",
"/ietf-interfaces:interfaces/interface",
interfaces_startup_load_interface,
},
};
Expand Down Expand Up @@ -73,6 +74,7 @@ int interfaces_startup_load(interfaces_ctx_t* ctx, sr_session_ctx_t* session)
lyd_free_tree(root_node);
}
sr_release_context(conn_ctx);

return error;
}

Expand All @@ -85,11 +87,11 @@ static int interfaces_startup_load_interface(void* priv, sr_session_ctx_t* sessi
// load interfaces data
SRPC_SAFE_CALL_ERR(error, interfaces_load_interface(ctx, &interface_hash), error_out);

// print debug info for now
// interfaces_interface_hash_print_debug(interface_hash);

// convert to libyang
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_to_ly(interface_hash, &parent_node), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_to_ly(ly_ctx, interface_hash, &parent_node), error_out);

// print created tree
lyd_print_file(stdout, parent_node, LYD_XML, 0);

goto out;

Expand Down

0 comments on commit 9f1759a

Please sign in to comment.