From c5df98aece868db29743b72e4a24112067944bd4 Mon Sep 17 00:00:00 2001 From: Christian Hopps Date: Mon, 16 Sep 2024 21:34:10 -0400 Subject: [PATCH 1/2] mgmtd: add ietf-yang-library support Signed-off-by: Christian Hopps --- lib/northbound.h | 1 + lib/northbound_oper.c | 10 ++++++++++ mgmtd/mgmt.c | 3 +++ mgmtd/mgmt_fe_adapter.c | 25 +++++++++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/lib/northbound.h b/lib/northbound.h index b2cccb67163e..dd3fbf8f7341 100644 --- a/lib/northbound.h +++ b/lib/northbound.h @@ -1716,6 +1716,7 @@ extern void nb_terminate(void); extern void nb_oper_init(struct event_loop *loop); extern void nb_oper_terminate(void); +extern bool nb_oper_is_yang_lib_query(const char *xpath); #ifdef __cplusplus } diff --git a/lib/northbound_oper.c b/lib/northbound_oper.c index e95f99a2bdcc..a3ff3607800e 100644 --- a/lib/northbound_oper.c +++ b/lib/northbound_oper.c @@ -1741,6 +1741,16 @@ static enum nb_error nb_op_walk_start(struct nb_op_yield_state *ys) return __walk(ys, false); } +bool nb_oper_is_yang_lib_query(const char *xpath) +{ + const char *libstr = "/ietf-yang-library:"; + const unsigned long liblen = strlen(libstr); + + if (strncmp(libstr, xpath, liblen)) + return false; + + return strlen(xpath) > liblen; +} void *nb_oper_walk(const char *xpath, struct yang_translator *translator, uint32_t flags, bool should_batch, nb_oper_data_cb cb, diff --git a/mgmtd/mgmt.c b/mgmtd/mgmt.c index cfadad4829e2..02c54b921542 100644 --- a/mgmtd/mgmt.c +++ b/mgmtd/mgmt.c @@ -57,6 +57,9 @@ void mgmt_init(void) /* Initialize MGMTD Transaction module */ mgmt_txn_init(mm, mm->master); + /* Add yang-library module */ + yang_module_load("ietf-yang-library", NULL); + /* Initialize the MGMTD Frontend Adapter Module */ mgmt_fe_adapter_init(mm->master); diff --git a/mgmtd/mgmt_fe_adapter.c b/mgmtd/mgmt_fe_adapter.c index 8d305ed52fd2..32f28a5774c5 100644 --- a/mgmtd/mgmt_fe_adapter.c +++ b/mgmtd/mgmt_fe_adapter.c @@ -1337,6 +1337,31 @@ static void fe_adapter_handle_get_data(struct mgmt_fe_session_ctx *session, goto done; } + /* Check for yang-library shortcut */ + if (nb_oper_is_yang_lib_query(msg->xpath)) { + struct lyd_node *ylib = NULL; + LY_ERR err; + + err = ly_ctx_get_yanglib_data(ly_native_ctx, &ylib, "%u", + ly_ctx_get_change_count( + ly_native_ctx)); + if (err) { + fe_adapter_send_error(session, req_id, false, err, + "Error getting yang-library data, session-id: %" PRIu64 + " error: %s", + session->session_id, + ly_last_errmsg()); + } else { + yang_lyd_trim_xpath(&ylib, msg->xpath); + (void)fe_adapter_send_tree_data(session, req_id, false, + msg->result_type, + wd_options, ylib, 0); + } + if (ylib) + lyd_free_all(ylib); + goto done; + } + switch (msg->datastore) { case MGMT_MSG_DATASTORE_CANDIDATE: ds_id = MGMTD_DS_CANDIDATE; From d5e4e2dfa58bc87252976163b6b6fd7637150cfe Mon Sep 17 00:00:00 2001 From: Christian Hopps Date: Tue, 17 Sep 2024 03:54:26 -0400 Subject: [PATCH 2/2] tests: add test for new ietf-yang-library support Signed-off-by: Christian Hopps --- tests/topotests/mgmt_oper/r1/frr-yanglib.conf | 10 +++++ tests/topotests/mgmt_oper/test_yanglib.py | 45 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 tests/topotests/mgmt_oper/r1/frr-yanglib.conf create mode 100644 tests/topotests/mgmt_oper/test_yanglib.py diff --git a/tests/topotests/mgmt_oper/r1/frr-yanglib.conf b/tests/topotests/mgmt_oper/r1/frr-yanglib.conf new file mode 100644 index 000000000000..f37766b158fa --- /dev/null +++ b/tests/topotests/mgmt_oper/r1/frr-yanglib.conf @@ -0,0 +1,10 @@ +log timestamp precision 6 +log file frr.log + +no debug memstats-at-exit + +debug mgmt backend datastore frontend transaction + +interface r1-eth0 + ip address 1.1.1.1/24 +exit diff --git a/tests/topotests/mgmt_oper/test_yanglib.py b/tests/topotests/mgmt_oper/test_yanglib.py new file mode 100644 index 000000000000..e094ca544374 --- /dev/null +++ b/tests/topotests/mgmt_oper/test_yanglib.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# SPDX-License-Identifier: ISC +# -*- coding: utf-8 eval: (blacken-mode 1) -*- +# +# September 17 2024, Christian Hopps +# +# Copyright (c) 2024, LabN Consulting, L.L.C. +# + +import json +import pytest +from lib.topogen import Topogen + +pytestmark = [pytest.mark.staticd, pytest.mark.mgmtd] + + +@pytest.fixture(scope="module") +def tgen(request): + "Setup/Teardown the environment and provide tgen argument to tests" + + topodef = {"s1": ("r1",)} + + tgen = Topogen(topodef, request.module.__name__) + tgen.start_topology() + + router_list = tgen.routers() + for rname, router in router_list.items(): + router.load_frr_config("frr-yanglib.conf") + + tgen.start_router() + yield tgen + tgen.stop_topology() + + +def test_yang_lib(tgen): + if tgen.routers_have_failure(): + pytest.skip(tgen.errors) + + r1 = tgen.gears["r1"].net + output = r1.cmd_nostatus( + "vtysh -c 'show mgmt get-data /ietf-yang-library:yang-library'" + ) + ret = json.loads(output) + loaded_modules = ret['ietf-yang-library:yang-library']['module-set'][0]['module'] + assert len(loaded_modules) > 10, "Modules missing from yang-library"