diff --git a/board/common/rootfs/etc/nginx/nginx.conf b/board/common/rootfs/etc/nginx/nginx.conf index aaa3fd7ca6..b044b4c8b5 100644 --- a/board/common/rootfs/etc/nginx/nginx.conf +++ b/board/common/rootfs/etc/nginx/nginx.conf @@ -30,6 +30,7 @@ http { 1 $binary_remote_addr; } include /etc/nginx/restconf-rate-zone.conf; + limit_conn_zone $restconf_limit_key zone=restconf_streams:32k; include /etc/nginx/enabled/*.conf; diff --git a/board/common/rootfs/etc/nginx/restconf-proxy.conf b/board/common/rootfs/etc/nginx/restconf-proxy.conf new file mode 100644 index 0000000000..bdeefa7252 --- /dev/null +++ b/board/common/rootfs/etc/nginx/restconf-proxy.conf @@ -0,0 +1,9 @@ +grpc_pass grpc://[::1]:10080; +grpc_set_header Host $host; +grpc_set_header X-Real-IP $remote_addr; +grpc_set_header X-Forwarded-For $proxy_add_x_forwarded_for; +grpc_set_header X-Forwarded-Proto $scheme; +grpc_set_header Forward "proto=$scheme;host=$host"; + +# Any error_page here stops the inherited one, 599 is never sent +error_page 599 /50x.html; diff --git a/board/common/rootfs/etc/nginx/restconf.app b/board/common/rootfs/etc/nginx/restconf.app index 0513ed4f57..4cefd80c35 100644 --- a/board/common/rootfs/etc/nginx/restconf.app +++ b/board/common/rootfs/etc/nginx/restconf.app @@ -2,15 +2,18 @@ location ~ ^/(restconf|yang|.well-known)/ { include /etc/nginx/restconf-access.conf; include /etc/nginx/restconf-rate-limit.conf; - grpc_pass grpc://[::1]:10080; - grpc_set_header Host $host; - grpc_set_header X-Real-IP $remote_addr; - grpc_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - grpc_set_header X-Forwarded-Proto $scheme; # A little longer than the RESTCONF server waits, see /etc/default/confd grpc_read_timeout 130s; + include /etc/nginx/restconf-proxy.conf; +} - # Any error_page here stops the inherited one, 599 is never sent - error_page 599 /50x.html; +# Event streams stay open, rousette sends a keep-alive every 55 s +location /streams/ { + include /etc/nginx/restconf-access.conf; + include /etc/nginx/restconf-rate-limit.conf; + limit_conn restconf_streams 16; + limit_conn_status 429; + grpc_read_timeout 120s; + include /etc/nginx/restconf-proxy.conf; } diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 6aea9295c0..441056429e 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -113,6 +113,11 @@ All notable changes to the project are documented in this file. control list's cycle each probe left and arrived - NETCONF and RESTCONF requests now get 120 seconds to complete, up from 60. A large change can take more than a minute on a small device +- RESTCONF clients can now subscribe to notifications and read them as + an event stream, see [Notifications][restconf-notif] +- On-change subscriptions to the operational datastore, over NETCONF + and RESTCONF, now report when an interface, its addresses, the LLDP + neighbors or the alarm list change, see [Subscribe to Changes][on-change] ### Fixes @@ -417,6 +422,8 @@ All notable changes to the project are documented in this file. [pppoe]: https://www.kernelkit.org/infix/latest/pppoe/ [modem]: https://www.kernelkit.org/infix/latest/modem/ [alarms]: https://www.kernelkit.org/infix/latest/alarms/ +[restconf-notif]: https://www.kernelkit.org/infix/latest/scripting-restconf/#notifications +[on-change]: https://www.kernelkit.org/infix/latest/scripting-restconf/#subscribe-to-changes [vpd]: https://www.kernelkit.org/infix/latest/vpd/#vpd-file-on-the-aux-partition [usb]: https://www.kernelkit.org/infix/latest/hardware/#usb-ports [gps]: https://www.kernelkit.org/infix/latest/hardware/#anchor-a-gps-receiver-to-a-usb-port diff --git a/doc/scripting-restconf.md b/doc/scripting-restconf.md index 3aa637dba2..75c2f5fd1e 100644 --- a/doc/scripting-restconf.md +++ b/doc/scripting-restconf.md @@ -648,6 +648,93 @@ Verify the change with SSH: Wed Apr 17 14:48:12 UTC 2024 ``` +## Notifications + +A client can subscribe to notifications and read them as a stream of +server-sent events, instead of polling. Subscribing is an RPC, and the +reply has the address of the stream: + +```bash +~$ curl -sk -u admin:admin -X POST \ + -H 'Content-Type: application/yang-data+json' \ + -d '{"ietf-subscribed-notifications:input":{"stream":"NETCONF","encoding":"encode-json"}}' \ + https://example.local/restconf/operations/ietf-subscribed-notifications:establish-subscription +{ + "ietf-subscribed-notifications:output": { + "id": 1, + "ietf-restconf-subscribed-notifications:uri": "https://example.local/streams/subscribed/5c7d0cfb-93a1-4a31-9cb4-1ee3c8b4a6f4" + } +} +``` + +Open the stream within a minute, or the device removes the subscription: + +```bash +~$ curl -sk -N -u admin:admin -H 'Accept: text/event-stream' \ + https://example.local/streams/subscribed/5c7d0cfb-93a1-4a31-9cb4-1ee3c8b4a6f4 +``` + +The stream stays open until the client closes it. One client address +can have up to 16 streams open at a time. + +The address in the reply has the same host name or IP address as the +request. If you used an IPv6 link-local address, the reply has it +without the zone. Add the zone again, e.g. `%25eth0`, before you open +the stream. + +### Subscribe to Changes + +To be told when operational data changes, subscribe with `on-change` +and a filter for the data you want. This subscription reports when an +interface goes up or down: + +```bash +~$ curl -sk -u admin:admin -X POST \ + -H 'Content-Type: application/yang-data+json' \ + -d '{"ietf-subscribed-notifications:input":{ + "ietf-yang-push:datastore":"ietf-datastores:operational", + "ietf-yang-push:datastore-xpath-filter":"/ietf-interfaces:interfaces/interface/oper-status", + "ietf-yang-push:on-change":{}, + "encoding":"encode-json"}}' \ + https://example.local/restconf/operations/ietf-subscribed-notifications:establish-subscription +``` + +The stream starts with the current values in a `push-update`, followed +by a `push-change-update` for every change: + +```json +{ + "ietf-restconf:notification": { + "ietf-yang-push:push-change-update": { + "id": 2, + "datastore-changes": { + "yang-patch": { + "patch-id": "patch-1", + "edit": [ + { + "edit-id": "edit-1", + "operation": "replace", + "target": "/ietf-interfaces:interfaces/interface[name='e2']/oper-status", + "value": { + "ietf-interfaces:oper-status": "down" + } + } + ] + } + } + }, + "eventTime": "2026-11-19T09:39:39.286415871+00:00" + } +} +``` + +The device reports changes to the interfaces and their addresses, the +LLDP neighbors and the alarm list. It does not report counters, read +them with a GET. + +A NETCONF client can make the same subscription. Its notifications +arrive on the session that subscribed, so there is no stream to open. + ## Advanced Examples ### Makefile for Common Operations diff --git a/package/statd/statd.conf b/package/statd/statd.conf index cbc1e9d9cd..4b3b9f93be 100644 --- a/package/statd/statd.conf +++ b/package/statd/statd.conf @@ -1,2 +1,3 @@ #set DEBUG=1 service name:statd [12345] statd -- Status daemon +service name:statd-push [12345] statd-push -- Status change publisher diff --git a/patches/sysrepo/5.2.27/0011-yang-push-find-the-previous-edit-of-a-target-without.patch b/patches/sysrepo/5.2.27/0011-yang-push-find-the-previous-edit-of-a-target-without.patch new file mode 100644 index 0000000000..20b0e059d8 --- /dev/null +++ b/patches/sysrepo/5.2.27/0011-yang-push-find-the-previous-edit-of-a-target-without.patch @@ -0,0 +1,66 @@ +From ce9193012b5e521ea4de52a872201e3506354ef7 Mon Sep 17 00:00:00 2001 +From: Mattias Walström +Date: Thu, 19 Nov 2026 15:06:39 +0100 +Subject: [PATCH] yang-push: find the previous edit of a target without XPath +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +The target was put in an XPath literal to look up an earlier edit of +the same node. A list key that is itself an instance-identifier makes +the path hold both kinds of quotes, the expression then fails to parse +and the whole on-change notification is dropped without a trace. + +Signed-off-by: Mattias Walström +--- + src/utils/sn_yang_push.c | 34 ++++++++++++---------------------- + 1 file changed, 12 insertions(+), 22 deletions(-) + +diff --git a/src/utils/sn_yang_push.c b/src/utils/sn_yang_push.c +index 65b1d4d8..caeda676 100644 +--- a/src/utils/sn_yang_push.c ++++ b/src/utils/sn_yang_push.c +@@ -407,31 +407,21 @@ srsn_yp_op2str(srsn_yp_change_t op) + static sr_error_info_t * + srsn_yp_ntf_change_edit_clear_target(struct lyd_node *ly_yp, const char *target) + { +- sr_error_info_t *err_info = NULL; +- struct ly_set *set = NULL; +- char quot, *xpath = NULL; +- +- /* find the edit of this target, be careful with the quotes in the XPath */ +- quot = strchr(target, '\'') ? '\"' : '\''; +- if (asprintf(&xpath, "/ietf-yang-push:push-change-update/datastore-changes/yang-patch/edit[target=%c%s%c]", +- quot, target, quot) == -1) { +- SR_ERRINFO_MEM(&err_info); +- goto cleanup; +- } +- if ((err_info = sr_lyd_find_xpath(ly_yp, xpath, &set))) { +- goto cleanup; +- } +- assert((set->count == 0) || (set->count == 1)); ++ struct lyd_node *edit, *leaf; + +- /* remove the previous change of this target */ +- if (set->count) { +- lyd_free_tree(set->dnodes[0]); ++ /* compare the values, a target with both kinds of quotes cannot be an XPath literal */ ++ LY_LIST_FOR(lyd_child(ly_yp), edit) { ++ if (strcmp(LYD_NAME(edit), "edit") || lyd_find_path(edit, "target", 0, &leaf)) { ++ continue; ++ } ++ if (!strcmp(lyd_get_value(leaf), target)) { ++ /* remove the previous change of this target */ ++ lyd_free_tree(edit); ++ break; ++ } + } + +-cleanup: +- free(xpath); +- ly_set_free(set, NULL); +- return err_info; ++ return NULL; + } + + /** diff --git a/src/statd/Makefile.am b/src/statd/Makefile.am index a16aedc030..93afa4f1b2 100644 --- a/src/statd/Makefile.am +++ b/src/statd/Makefile.am @@ -1,7 +1,7 @@ DISTCLEANFILES = *~ *.d ACLOCAL_AMFLAGS = -I m4 -sbin_PROGRAMS = statd +sbin_PROGRAMS = statd statd-push statd_SOURCES = statd.c shared.c shared.h journal.c journal_retention.c journal.h avahi.c avahi.h yangerd.c yangerd.h statd_CPPFLAGS = -D_DEFAULT_SOURCE -D_GNU_SOURCE statd_CPPFLAGS += -DSTATD_VERSION=\"$(PACKAGE_VERSION)\" @@ -13,6 +13,14 @@ statd_LDADD = $(jansson_LIBS) $(libyang_LIBS) $(sysrepo_LIBS) statd_LDADD += $(libsrx_LIBS) $(libite_LIBS) $(EV_LIBS) -lz statd_LDADD += $(avahi_client_LIBS) +statd_push_SOURCES = push.c yangerd.c yangerd.h +statd_push_CPPFLAGS = -D_DEFAULT_SOURCE -D_GNU_SOURCE +statd_push_CFLAGS = -W -Wall -Wextra +statd_push_CFLAGS += $(jansson_CFLAGS) $(libyang_CFLAGS) $(sysrepo_CFLAGS) +statd_push_CFLAGS += $(libsrx_CFLAGS) $(libite_CFLAGS) +statd_push_LDADD = $(jansson_LIBS) $(libyang_LIBS) $(sysrepo_LIBS) +statd_push_LDADD += $(libsrx_LIBS) $(libite_LIBS) $(EV_LIBS) + # Test stub for journal retention policy (no dependencies, standalone) noinst_PROGRAMS = journal_retention_stub journal_retention_stub_SOURCES = journal_retention_stub.c journal_retention.c journal.h diff --git a/src/statd/push.c b/src/statd/push.c new file mode 100644 index 0000000000..5fa4809840 --- /dev/null +++ b/src/statd/push.c @@ -0,0 +1,182 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ + +/* + * A GET is answered by asking yangerd, which gives sysrepo nothing to + * compare, so a client with an on-change subscription hears nothing. + * For the modules below, also write the data yangerd sends when it has + * changed into the operational datastore and let sysrepo tell the + * subscribers what differs. + * + * A daemon of its own. The write needs the lock of a module that a + * client holds while it waits for statd to answer its GET. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "yangerd.h" + +#define PUSH_RETRY 1.0 /* s */ +#define PUSH_TIMEOUT 5000 /* ms */ + +struct push { + sr_conn_ctx_t *conn; + sr_session_ctx_t *ses; + ev_io io; + ev_timer timer; /* opens a new watch */ +}; + +static const char *push_keys[] = { + "ietf-interfaces:interfaces", + "ieee802-dot1ab-lldp:lldp", + "ietf-alarms:alarms", +}; + +/* Returns -1 when the write is worth another try */ +static int push(sr_conn_ctx_t *conn, sr_session_ctx_t *ses, const char *key, const char *json) +{ + struct lyd_node *tree = NULL; + const struct ly_ctx *ly; + static unsigned int pushed; /* keys with data in operational */ + unsigned int bit = 0; + char xpath[128]; + int has_data; + size_t i; + int err; + + for (i = 0; i < NELEMS(push_keys); i++) { + if (!strcmp(key, push_keys[i])) + bit = 1u << i; + } + if (!bit) + return 0; + + ly = sr_acquire_context(conn); + if (!ly) + return -1; + + if (yangerd_parse(ly, key, json, &tree) || (!tree && !(pushed & bit))) { + sr_release_context(conn); + return 0; + } + + has_data = tree != NULL; + if (has_data) { + err = sr_edit_batch(ses, tree, "replace"); + lyd_free_all(tree); + } else { + snprintf(xpath, sizeof(xpath), "/%s", key); + err = sr_delete_item(ses, xpath, 0); + } + sr_release_context(conn); + + err = err ?: sr_apply_changes(ses, PUSH_TIMEOUT); + if (err) { + sr_discard_changes(ses); + if (err == SR_ERR_TIME_OUT || err == SR_ERR_LOCKED) + return -1; + ERROR("push: %s: %s", key, sr_strerror(err)); + } else if (has_data) { + pushed |= bit; + } else { + pushed &= ~bit; + } + + return 0; +} + +static void watch_stop(struct ev_loop *loop, struct push *ctx) +{ + ev_io_stop(loop, &ctx->io); + close(ctx->io.fd); + ev_timer_set(&ctx->timer, PUSH_RETRY, 0.0); + ev_timer_start(loop, &ctx->timer); +} + +static void watch_cb(struct ev_loop *loop, ev_io *w, int revents) +{ + struct push *ctx = w->data; + char *key, *json; + int rc; + + if (yangerd_watch_read(w->fd, &key, &json)) { + watch_stop(loop, ctx); + return; + } + + rc = push(ctx->conn, ctx->ses, key, json); + free(key); + free(json); + if (rc) + watch_stop(loop, ctx); +} + +/* yangerd sends every key again on a new watch */ +static void timer_cb(struct ev_loop *loop, ev_timer *w, int revents) +{ + struct push *ctx = w->data; + int fd; + + fd = yangerd_watch(push_keys, NELEMS(push_keys)); + if (fd < 0) { + ev_timer_set(&ctx->timer, PUSH_RETRY, 0.0); + ev_timer_start(loop, &ctx->timer); + return; + } + + ev_io_init(&ctx->io, watch_cb, fd, EV_READ); + ctx->io.data = ctx; + ev_io_start(loop, &ctx->io); +} + +static void signal_cb(struct ev_loop *loop, ev_signal *w, int revents) +{ + ev_break(loop, EVBREAK_ALL); +} + +int main(void) +{ + struct ev_loop *loop = EV_DEFAULT; + ev_signal sigterm, sigint; + struct push ctx = { 0 }; + + if (getenv("DEBUG")) + debug = 1; + openlog("statd-push", LOG_PID | LOG_NDELAY | (debug ? LOG_PERROR : 0), LOG_DAEMON); + + if (sr_connect(SR_CONN_DEFAULT, &ctx.conn)) { + ERROR("push: no sysrepo connection"); + return EXIT_FAILURE; + } + if (sr_session_start(ctx.conn, SR_DS_OPERATIONAL, &ctx.ses)) { + ERROR("push: no sysrepo session"); + sr_disconnect(ctx.conn); + return EXIT_FAILURE; + } + + ev_signal_init(&sigterm, signal_cb, SIGTERM); + ev_signal_start(loop, &sigterm); + ev_signal_init(&sigint, signal_cb, SIGINT); + ev_signal_start(loop, &sigint); + + ev_timer_init(&ctx.timer, timer_cb, 0.0, 0.0); + ctx.timer.data = &ctx; + ev_timer_start(loop, &ctx.timer); + + pidfile(NULL); + ev_run(loop, 0); + + sr_session_stop(ctx.ses); + sr_disconnect(ctx.conn); + + return EXIT_SUCCESS; +} diff --git a/src/statd/statd.c b/src/statd/statd.c index 4411704734..4e10757095 100644 --- a/src/statd/statd.c +++ b/src/statd/statd.c @@ -136,99 +136,17 @@ static int graft(struct lyd_node *parent, struct lyd_node *tree, const char *pat return SR_ERR_OK; } -/* - * One bad value must not cost every GET of the module. When yangerd's - * answer does not parse, try each entry of every list under the top - * container on its own, drop the ones libyang rejects, loudly, and parse - * the rest. Only runs on the error path. - */ -static int parse_salvage(const struct ly_ctx *ctx, const char *json, const char *key, - struct lyd_node **tree) -{ - json_t *root, *top, *list, *good, *one, *entry, *name; - const char *lname; - size_t i, dropped = 0; - char *text; - int rc = -1; - - root = json_loads(json, 0, NULL); - top = json_object_get(root, key); - if (!json_is_object(top)) - goto out; - - json_object_foreach(top, lname, list) { - if (!json_is_array(list)) - continue; - - good = json_array(); - json_array_foreach(list, i, entry) { - struct lyd_node *probe = NULL; - LY_ERR err; - - one = json_pack("{s:{s:[O]}}", key, lname, entry); - text = json_dumps(one, JSON_COMPACT); - json_decref(one); - err = text ? lyd_parse_data_mem(ctx, text, LYD_JSON, LYD_PARSE_ONLY, 0, &probe) : LY_EMEM; - free(text); - lyd_free_all(probe); - if (!err) { - json_array_append(good, entry); - continue; - } - - name = json_object_get(entry, "name"); - ERROR("yangerd: dropping %s %s[%s]: %s", key, lname, - json_is_string(name) ? json_string_value(name) : "?", ly_last_logmsg()); - dropped++; - } - json_object_set_new(top, lname, good); - } - - if (!dropped) - goto out; - - text = json_dumps(root, JSON_COMPACT); - if (text && !lyd_parse_data_mem(ctx, text, LYD_JSON, LYD_PARSE_ONLY, 0, tree)) - rc = 0; - free(text); -out: - json_decref(root); - return rc; -} - static int ly_add_yangerd_data(const struct ly_ctx *ctx, struct lyd_node **parent, const char *path, const char *key) { - struct lyd_node *tree = NULL; - char *json = NULL; - size_t len = 0; + struct lyd_node *tree; int rc; - rc = yangerd_query(key, &json, &len); - if (rc > 0) { - WARN("yangerd: no data for %s yet, not ready", key); + rc = yangerd_tree(ctx, key, &tree); + if (rc > 0) return SR_ERR_OK; - } - if (rc) { - ERROR("yangerd: query failed for %s", key); + if (rc) return SR_ERR_INTERNAL; - } - - DEBUG("yangerd: got %zu bytes JSON for %s", len, key); - if (!json || !len) { - free(json); - return SR_ERR_OK; /* feature not active, no data */ - } - - if (lyd_parse_data_mem(ctx, json, LYD_JSON, LYD_PARSE_ONLY, 0, &tree)) { - ERROR("Failed parsing yangerd data for %s: %s", key, ly_errmsg(ctx)); - tree = NULL; - if (parse_salvage(ctx, json, key, &tree)) { - free(json); - return SR_ERR_INTERNAL; - } - } - free(json); if (!tree) return SR_ERR_OK; /* "{}", nothing to add */ diff --git a/src/statd/yangerd.c b/src/statd/yangerd.c index 23527a0ee8..3a7b381cf7 100644 --- a/src/statd/yangerd.c +++ b/src/statd/yangerd.c @@ -108,17 +108,26 @@ static int yangerd_read_all(int fd, void *buf, size_t len) return 0; } -static int yangerd_send_request(int fd, const char *path) +static int yangerd_send_request(int fd, const char *method, const char *path, + const char **keys, size_t num) { - json_t *req; + json_t *req, *arr; char *json_str; size_t json_len; unsigned char hdr[5]; int rc = -1; - req = json_pack("{s:s, s:s}", "method", "get", "path", path); + req = json_pack("{s:s}", "method", method); if (!req) return -1; + if (path) + json_object_set_new(req, "path", json_string(path)); + if (num) { + arr = json_array(); + while (num--) + json_array_append_new(arr, json_string(*keys++)); + json_object_set_new(req, "keys", arr); + } json_str = json_dumps(req, JSON_COMPACT); json_decref(req); @@ -193,9 +202,10 @@ static int yangerd_recv_frame(int fd, char **buf, size_t *len) * The response header is a small JSON object, the data, if any, * follows raw in a frame of its own and goes to the caller untouched. */ -static int yangerd_recv_response(int fd, char **buf, size_t *len) +static int yangerd_recv_response(int fd, char **key, char **buf, size_t *len) { json_t *resp, *status, *raw; + const char *name; json_error_t jerr; size_t hdr_len; char *hdr; @@ -228,6 +238,12 @@ static int yangerd_recv_response(int fd, char **buf, size_t *len) return -1; } + name = json_string_value(json_object_get(resp, "key")); + if (key && !(*key = strdup(name ?: ""))) { + json_decref(resp); + return -1; + } + raw = json_object_get(resp, "raw"); rc = json_is_true(raw); json_decref(resp); @@ -257,13 +273,13 @@ int yangerd_query(const char *path, char **buf, size_t *len) if (fd < 0) return -1; - if (yangerd_send_request(fd, path) < 0) { + if (yangerd_send_request(fd, "get", path, NULL, 0) < 0) { ERROR("yangerd: failed sending request for %s", path); close(fd); return -1; } - rc = yangerd_recv_response(fd, buf, len); + rc = yangerd_recv_response(fd, NULL, buf, len); if (rc < 0) ERROR("yangerd: failed reading response for %s", path); @@ -271,3 +287,130 @@ int yangerd_query(const char *path, char **buf, size_t *len) return rc; } + +/* + * One bad value must not cost every GET of the module. When yangerd's + * answer does not parse, try each entry of every list under the top + * container on its own, drop the ones libyang rejects, loudly, and parse + * the rest. Only runs on the error path. + */ +static int parse_salvage(const struct ly_ctx *ctx, const char *json, const char *key, + struct lyd_node **tree) +{ + json_t *root, *top, *list, *good, *one, *entry, *name; + const char *lname; + size_t i, dropped = 0; + char *text; + int rc = -1; + + root = json_loads(json, 0, NULL); + top = json_object_get(root, key); + if (!json_is_object(top)) + goto out; + + json_object_foreach(top, lname, list) { + if (!json_is_array(list)) + continue; + + good = json_array(); + json_array_foreach(list, i, entry) { + struct lyd_node *probe = NULL; + LY_ERR err; + + one = json_pack("{s:{s:[O]}}", key, lname, entry); + text = json_dumps(one, JSON_COMPACT); + json_decref(one); + err = text ? lyd_parse_data_mem(ctx, text, LYD_JSON, LYD_PARSE_ONLY, 0, &probe) : LY_EMEM; + free(text); + lyd_free_all(probe); + if (!err) { + json_array_append(good, entry); + continue; + } + + name = json_object_get(entry, "name"); + ERROR("yangerd: dropping %s %s[%s]: %s", key, lname, + json_is_string(name) ? json_string_value(name) : "?", ly_last_logmsg()); + dropped++; + } + json_object_set_new(top, lname, good); + } + + if (!dropped) + goto out; + + text = json_dumps(root, JSON_COMPACT); + if (text && !lyd_parse_data_mem(ctx, text, LYD_JSON, LYD_PARSE_ONLY, 0, tree)) + rc = 0; + free(text); +out: + json_decref(root); + return rc; +} + +int yangerd_parse(const struct ly_ctx *ctx, const char *key, const char *json, struct lyd_node **tree) +{ + *tree = NULL; + + if (lyd_parse_data_mem(ctx, json, LYD_JSON, LYD_PARSE_ONLY, 0, tree)) { + ERROR("Failed parsing yangerd data for %s: %s", key, ly_errmsg(ctx)); + *tree = NULL; + return parse_salvage(ctx, json, key, tree); + } + + return 0; +} + +int yangerd_tree(const struct ly_ctx *ctx, const char *key, struct lyd_node **tree) +{ + char *json = NULL; + size_t len = 0; + int rc; + + *tree = NULL; + + rc = yangerd_query(key, &json, &len); + if (rc > 0) { + WARN("yangerd: no data for %s yet, not ready", key); + return 1; + } + if (rc) { + ERROR("yangerd: query failed for %s", key); + return -1; + } + + DEBUG("yangerd: got %zu bytes JSON for %s", len, key); + rc = yangerd_parse(ctx, key, json, tree); + free(json); + + return rc; +} + +int yangerd_watch(const char **keys, size_t num) +{ + int fd; + + fd = yangerd_connect(); + if (fd < 0) + return -1; + + if (yangerd_send_request(fd, "watch", NULL, keys, num) < 0) { + close(fd); + return -1; + } + + return fd; +} + +int yangerd_watch_read(int fd, char **key, char **json) +{ + size_t len; + + *key = NULL; + if (yangerd_recv_response(fd, key, json, &len)) { + free(*key); + return -1; + } + + return 0; +} diff --git a/src/statd/yangerd.h b/src/statd/yangerd.h index a7275c102f..5dbe043153 100644 --- a/src/statd/yangerd.h +++ b/src/statd/yangerd.h @@ -4,6 +4,7 @@ #define STATD_YANGERD_H_ #include +#include #define YANGERD_SOCKET_DEFAULT "/run/yangerd.sock" #define YANGERD_TIMEOUT_SEC 5 @@ -12,9 +13,9 @@ /** * yangerd_query() - Query yangerd daemon for operational YANG data - * @path: YANG model path, e.g. "ietf-interfaces:interfaces" - * @buf: Output pointer to malloc'd JSON string (caller must free) - * @len: Output length of JSON data + * @path: YANG model path, e.g. "ietf-interfaces:interfaces" + * @buf: Output pointer to malloc'd JSON string (caller must free) + * @len: Output length of JSON data * * Connects to the yangerd Unix socket, sends a "get" request for @path, * and returns the data frame of the response. The socket path defaults @@ -26,4 +27,16 @@ */ int yangerd_query(const char *path, char **buf, size_t *len); +/* 0 with the parsed data or NULL in tree, -1 when json is not valid for the models */ +int yangerd_parse(const struct ly_ctx *ctx, const char *key, const char *json, struct lyd_node **tree); + +/* 0 with the parsed answer or NULL in tree, 1 when yangerd is not ready, -1 on error */ +int yangerd_tree(const struct ly_ctx *ctx, const char *key, struct lyd_node **tree); + +/* Returns a socket that is readable when one of keys has changed, or -1 */ +int yangerd_watch(const char **keys, size_t num); + +/* Reads one changed key and its stored data from a yangerd_watch() socket */ +int yangerd_watch_read(int fd, char **key, char **json); + #endif diff --git a/src/yangerd/README.md b/src/yangerd/README.md index 333c478146..9063bf42d6 100644 --- a/src/yangerd/README.md +++ b/src/yangerd/README.md @@ -193,6 +193,14 @@ follows in a second frame, so statd can pass it to keys, and `health` returns each key's size and last update time. The maximum frame is 4 MiB. +`{"method": "watch", "keys": [...]}` keeps the connection. Each time +the stored data of one of the keys changes, yangerd waits 100 ms for +the burst to end and answers with `{"status": "ok", "key": ""}` +and the data like a `get`, without the values a tree provider computes +per request. The first answers cover every key asked for. A key +without data gets `{}`. A write that leaves a key byte for byte the +same is not a change. + `yangerctl` speaks the same protocol for debugging: ``` @@ -223,6 +231,17 @@ in mind: that line, fix the value in yangerd. +- **Changes are pushed for some modules.** A pull subscription gives + sysrepo nothing to compare, so an on-change subscription to it never + fires. For the keys listed in `src/statd/push.c`, `statd-push` + follows `watch` and writes the data it gets to the operational + datastore, and sysrepo sends the difference to the subscribers. A + GET is still answered by statd's pull subscription. The write is in + a daemon of its own: it needs the module lock that a client holds + while it waits for statd to answer its GET. + Values that only a tree provider computes, such as the byte counters, + never produce a change event. + ## Configuration Environment from `/etc/default/yangerd`, installed from diff --git a/src/yangerd/internal/ipc/conn_test.go b/src/yangerd/internal/ipc/conn_test.go index e1bbc02dfe..5512b3ee54 100644 --- a/src/yangerd/internal/ipc/conn_test.go +++ b/src/yangerd/internal/ipc/conn_test.go @@ -12,12 +12,12 @@ import ( "github.com/kernelkit/infix/src/yangerd/internal/tree" ) -func startServer(t *testing.T, timeout time.Duration) (string, context.CancelFunc, chan error) { +func startServer(t *testing.T, tr *tree.Tree, timeout time.Duration) (string, context.CancelFunc, chan error) { t.Helper() sockPath := filepath.Join(t.TempDir(), "conn.sock") ready := &atomic.Bool{} ready.Store(true) - srv := NewServer(tree.New(), ready) + srv := NewServer(tr, ready) srv.timeout = timeout if err := srv.Listen(sockPath); err != nil { t.Fatal(err) @@ -30,7 +30,7 @@ func startServer(t *testing.T, timeout time.Duration) (string, context.CancelFun // A client that connects and never sends is dropped at the deadline. func TestServerIdleConnDeadline(t *testing.T) { - sockPath, cancel, _ := startServer(t, 200*time.Millisecond) + sockPath, cancel, _ := startServer(t, tree.New(), 200*time.Millisecond) defer cancel() conn, err := net.Dial("unix", sockPath) @@ -51,7 +51,7 @@ func TestServerIdleConnDeadline(t *testing.T) { // Shutdown must not wait for a connection that is still open. func TestServerShutdownClosesConns(t *testing.T) { - sockPath, cancel, done := startServer(t, time.Hour) + sockPath, cancel, done := startServer(t, tree.New(), time.Hour) conn, err := net.Dial("unix", sockPath) if err != nil { diff --git a/src/yangerd/internal/ipc/protocol.go b/src/yangerd/internal/ipc/protocol.go index 2d502f0480..c7f3a73b11 100644 --- a/src/yangerd/internal/ipc/protocol.go +++ b/src/yangerd/internal/ipc/protocol.go @@ -33,6 +33,9 @@ const ( type Request struct { Method string `json:"method"` Path string `json:"path,omitempty"` + + // Keys limits "watch" to these tree keys, all of them when empty. + Keys []string `json:"keys,omitempty"` } // Response is the IPC response to a client. @@ -44,9 +47,12 @@ type Response struct { // Raw is set on the wire when Data follows in its own frame. Raw bool `json:"raw,omitempty"` - // Used by "get" responses, sent as the second frame. + // Used by "get" and "watch" responses, sent as the second frame. Data json.RawMessage `json:"-"` + // Used by "watch" responses, the key that changed. + Key string `json:"key,omitempty"` + // Used by "health" responses. Models map[string]json.RawMessage `json:"models,omitempty"` } diff --git a/src/yangerd/internal/ipc/server.go b/src/yangerd/internal/ipc/server.go index 33840ea3d2..c47230f70d 100644 --- a/src/yangerd/internal/ipc/server.go +++ b/src/yangerd/internal/ipc/server.go @@ -3,6 +3,7 @@ package ipc import ( "context" "encoding/json" + "io" "log" "net" "os" @@ -27,6 +28,9 @@ type Server struct { // client in statd uses the same. timeout time.Duration + // debounce is how long "watch" collects a burst of changes. + debounce time.Duration + mu sync.Mutex conns map[net.Conn]struct{} } @@ -35,10 +39,11 @@ type Server struct { // While ready is false, all requests receive a 503 "starting" response. func NewServer(t *tree.Tree, ready *atomic.Bool) *Server { return &Server{ - tree: t, - ready: ready, - timeout: 5 * time.Second, - conns: make(map[net.Conn]struct{}), + tree: t, + ready: ready, + timeout: 5 * time.Second, + debounce: 100 * time.Millisecond, + conns: make(map[net.Conn]struct{}), } } @@ -127,6 +132,8 @@ func (s *Server) handleConn(conn net.Conn) { s.handleGet(conn, req) case "health": s.handleHealth(conn) + case "watch": + s.handleWatch(conn, req) default: WriteResponse(conn, &Response{ Status: "error", @@ -148,23 +155,7 @@ func (s *Server) handleGet(conn net.Conn, req *Request) { key = key[1:] } - data := s.tree.Get(key) - if data == nil { - // An absent subtree is a normal answer for operational data -- - // the feature is simply not active (e.g. NTP unconfigured). - // Answer ok with an empty object rather than an error, so every - // client gets "no data" without special-casing. Deliberately - // NOT {"": {}}: that would make libyang instantiate the - // container, which for presence containers is real data. - WriteResponse(conn, &Response{ - Status: "ok", - Data: json.RawMessage(`{}`), - }) - return - } - - envelope := map[string]json.RawMessage{key: data} - body, err := json.Marshal(envelope) + body, err := envelope(key, s.tree.Get(key)) if err != nil { WriteResponse(conn, &Response{ Status: "error", @@ -180,6 +171,60 @@ func (s *Server) handleGet(conn net.Conn, req *Request) { }) } +// envelope wraps the data of a key the way libyang wants it. +func envelope(key string, data json.RawMessage) (json.RawMessage, error) { + if data == nil { + // An absent subtree is a normal answer for operational data -- + // the feature is simply not active (e.g. NTP unconfigured). + // Answer ok with an empty object rather than an error, so every + // client gets "no data" without special-casing. Deliberately + // NOT {"": {}}: that would make libyang instantiate the + // container, which for presence containers is real data. + return json.RawMessage(`{}`), nil + } + return json.Marshal(map[string]json.RawMessage{key: data}) +} + +// handleWatch keeps the connection and writes the stored data of each +// key that changes, starting with all of them, until the client hangs +// up. Values a tree provider computes per request are left out. +func (s *Server) handleWatch(conn net.Conn, req *Request) { + conn.SetDeadline(time.Time{}) + + w := s.tree.Watch(req.Keys...) + defer s.tree.Unwatch(w) + + gone := make(chan struct{}) + go func() { + io.Copy(io.Discard, conn) + close(gone) + }() + + for { + select { + case <-gone: + return + case <-w.C: + } + select { + case <-gone: + return + case <-time.After(s.debounce): + } + for _, key := range w.Take() { + body, err := envelope(key, s.tree.GetCached(key)) + if err != nil { + log.Printf("ipc: watch %s: %v", key, err) + continue + } + conn.SetWriteDeadline(time.Now().Add(s.timeout)) + if WriteResponse(conn, &Response{Status: "ok", Key: key, Data: body}) != nil { + return + } + } + } +} + func (s *Server) handleDump(conn net.Conn) { keys := s.tree.Keys() blobs := s.tree.GetMulti(keys) diff --git a/src/yangerd/internal/ipc/server_test.go b/src/yangerd/internal/ipc/server_test.go index 63e5b23d61..729d8be499 100644 --- a/src/yangerd/internal/ipc/server_test.go +++ b/src/yangerd/internal/ipc/server_test.go @@ -155,3 +155,45 @@ func TestServerSocketIsOwnerOnly(t *testing.T) { t.Fatalf("socket mode %o, want 600", perm) } } + +func TestServerWatch(t *testing.T) { + tr := tree.New() + tr.Set("a", json.RawMessage(`{"stored":1}`)) + tr.Set("other", json.RawMessage(`1`)) + tr.RegisterProvider("a", func() json.RawMessage { return json.RawMessage(`{"computed":2}`) }) + + sockPath, cancel, _ := startServer(t, tr, time.Hour) + defer cancel() + + conn, err := net.Dial("unix", sockPath) + if err != nil { + t.Fatal(err) + } + defer conn.Close() + conn.SetDeadline(time.Now().Add(5 * time.Second)) + + payload, _ := json.Marshal(&Request{Method: "watch", Keys: []string{"a"}}) + if err := WriteFrame(conn, payload); err != nil { + t.Fatal(err) + } + + expect := func(want string) { + t.Helper() + resp, err := ReadResponse(conn) + if err != nil { + t.Fatal(err) + } + if resp.Key != "a" || string(resp.Data) != want { + t.Fatalf("expected a with %s, got %s with %s", want, resp.Key, resp.Data) + } + } + + expect(`{"a":{"stored":1}}`) + + tr.Set("other", json.RawMessage(`2`)) + tr.Set("a", json.RawMessage(`{"stored":3}`)) + expect(`{"a":{"stored":3}}`) + + tr.Delete("a") + expect(`{}`) +} diff --git a/src/yangerd/internal/tree/tree.go b/src/yangerd/internal/tree/tree.go index 18c5a86968..9b5abf0ba8 100644 --- a/src/yangerd/internal/tree/tree.go +++ b/src/yangerd/internal/tree/tree.go @@ -8,6 +8,7 @@ package tree import ( + "bytes" "encoding/json" "sync" "time" @@ -31,6 +32,79 @@ type Tree struct { mu sync.RWMutex // protects the models map itself models map[string]*modelEntry providers map[string]OnDemandFunc // on-demand overlay providers + + wmu sync.RWMutex + watchers map[*Watcher]struct{} +} + +// Watcher collects the keys whose cached data changed. C gets a +// token when there is something to Take. +type Watcher struct { + C chan struct{} + keys map[string]struct{} // nil for every key + mu sync.Mutex + dirty map[string]struct{} +} + +// Watch registers a Watcher for the given keys, or for every key when +// none is given. It starts out with all of them changed. +func (t *Tree) Watch(keys ...string) *Watcher { + w := &Watcher{C: make(chan struct{}, 1), dirty: make(map[string]struct{})} + if len(keys) > 0 { + w.keys = make(map[string]struct{}, len(keys)) + for _, key := range keys { + w.keys[key] = struct{}{} + } + } else { + keys = t.Keys() + } + t.wmu.Lock() + t.watchers[w] = struct{}{} + t.wmu.Unlock() + for _, key := range keys { + w.mark(key) + } + return w +} + +// Unwatch removes a Watcher. +func (t *Tree) Unwatch(w *Watcher) { + t.wmu.Lock() + delete(t.watchers, w) + t.wmu.Unlock() +} + +// Take returns the keys changed since the last call. +func (w *Watcher) Take() []string { + w.mu.Lock() + defer w.mu.Unlock() + keys := make([]string, 0, len(w.dirty)) + for key := range w.dirty { + keys = append(keys, key) + } + clear(w.dirty) + return keys +} + +func (w *Watcher) mark(key string) { + if _, ok := w.keys[key]; w.keys != nil && !ok { + return + } + w.mu.Lock() + w.dirty[key] = struct{}{} + w.mu.Unlock() + select { + case w.C <- struct{}{}: + default: + } +} + +func (t *Tree) changed(key string) { + t.wmu.RLock() + defer t.wmu.RUnlock() + for w := range t.watchers { + w.mark(key) + } } // New creates an empty Tree. @@ -38,6 +112,7 @@ func New() *Tree { return &Tree{ models: make(map[string]*modelEntry), providers: make(map[string]OnDemandFunc), + watchers: make(map[*Watcher]struct{}), } } @@ -74,9 +149,13 @@ func (t *Tree) entry(key string) *modelEntry { func (t *Tree) Set(key string, v json.RawMessage) { entry := t.entry(key) entry.mu.Lock() + same := bytes.Equal(entry.data, v) entry.data = v entry.updated = time.Now() entry.mu.Unlock() + if !same { + t.changed(key) + } } // Get returns the raw JSON for the given module key. @@ -161,21 +240,25 @@ type ModelInfo struct { func (t *Tree) Merge(key string, partial json.RawMessage) { entry := t.entry(key) entry.mu.Lock() - defer entry.mu.Unlock() + old := entry.data + entry.data = merge(old, partial) + entry.updated = time.Now() + same := bytes.Equal(old, entry.data) + entry.mu.Unlock() + if !same { + t.changed(key) + } +} - // Unmarshal existing data. +func merge(data, partial json.RawMessage) json.RawMessage { var base map[string]json.RawMessage - if len(entry.data) == 0 || json.Unmarshal(entry.data, &base) != nil { + if len(data) == 0 || json.Unmarshal(data, &base) != nil { base = make(map[string]json.RawMessage) } - // Unmarshal partial. var overlay map[string]json.RawMessage if json.Unmarshal(partial, &overlay) != nil { - // partial is not a JSON object — fall back to full replace. - entry.data = partial - entry.updated = time.Now() - return + return partial } for k, v := range overlay { @@ -184,20 +267,20 @@ func (t *Tree) Merge(key string, partial json.RawMessage) { merged, err := json.Marshal(base) if err != nil { - // Should never happen with valid JSON inputs. - entry.data = partial - entry.updated = time.Now() - return + return partial } - entry.data = merged - entry.updated = time.Now() + return merged } // Delete removes a key from the tree entirely. func (t *Tree) Delete(key string) { t.mu.Lock() + _, ok := t.models[key] delete(t.models, key) t.mu.Unlock() + if ok { + t.changed(key) + } } // ShallowMerge overlays the top-level fields of overlay onto base and diff --git a/src/yangerd/internal/tree/tree_test.go b/src/yangerd/internal/tree/tree_test.go index 443dcaca4b..61bda81665 100644 --- a/src/yangerd/internal/tree/tree_test.go +++ b/src/yangerd/internal/tree/tree_test.go @@ -382,3 +382,60 @@ func TestConcurrentFirstMerge(t *testing.T) { } } } + +func TestWatchStartsWithEveryKey(t *testing.T) { + tr := New() + tr.Set("a", json.RawMessage(`1`)) + tr.Set("b", json.RawMessage(`2`)) + + w := tr.Watch() + defer tr.Unwatch(w) + + <-w.C + if keys := w.Take(); len(keys) != 2 { + t.Fatalf("expected both keys, got %v", keys) + } +} + +func TestWatchGivenKeys(t *testing.T) { + tr := New() + tr.Set("a", json.RawMessage(`1`)) + + w := tr.Watch("b") + defer tr.Unwatch(w) + + if keys := w.Take(); len(keys) != 1 || keys[0] != "b" { + t.Fatalf("expected b at start, got %v", keys) + } + + tr.Set("a", json.RawMessage(`2`)) + if keys := w.Take(); len(keys) != 0 { + t.Fatalf("unwatched key reported: %v", keys) + } +} + +func TestWatchOnlyReportsChanges(t *testing.T) { + tr := New() + tr.Set("a", json.RawMessage(`{"x":1}`)) + + w := tr.Watch() + defer tr.Unwatch(w) + w.Take() + + tr.Set("a", json.RawMessage(`{"x":1}`)) + tr.Merge("a", json.RawMessage(`{"x":1}`)) + tr.Delete("missing") + if keys := w.Take(); len(keys) != 0 { + t.Fatalf("unchanged data reported as changed: %v", keys) + } + + tr.Merge("a", json.RawMessage(`{"x":2}`)) + if keys := w.Take(); len(keys) != 1 || keys[0] != "a" { + t.Fatalf("expected a after merge, got %v", keys) + } + + tr.Delete("a") + if keys := w.Take(); len(keys) != 1 || keys[0] != "a" { + t.Fatalf("expected a after delete, got %v", keys) + } +} diff --git a/test/case/interfaces/wifi_mesh_roaming/topology.svg b/test/case/interfaces/wifi_mesh_roaming/topology.svg index cf6b33df5e..b9cd1844c0 100644 --- a/test/case/interfaces/wifi_mesh_roaming/topology.svg +++ b/test/case/interfaces/wifi_mesh_roaming/topology.svg @@ -1,6 +1,7 @@ + diff --git a/test/case/interfaces/wifi_radio_validation/topology.svg b/test/case/interfaces/wifi_radio_validation/topology.svg index 6e9c18d2f5..8058df0aeb 100644 --- a/test/case/interfaces/wifi_radio_validation/topology.svg +++ b/test/case/interfaces/wifi_radio_validation/topology.svg @@ -1,8 +1,7 @@ - + diff --git a/test/case/misc/Readme.adoc b/test/case/misc/Readme.adoc index ff72130b08..f111f2ecc6 100644 --- a/test/case/misc/Readme.adoc +++ b/test/case/misc/Readme.adoc @@ -5,9 +5,14 @@ Tests verifying system utilities and operational features: - Operational datastore query for all configuration - Support data collection with work-dir and GPG encryption + - On-change subscriptions to the operational datastore include::operational_all/Readme.adoc[] <<< include::support_collect/Readme.adoc[] + +<<< + +include::on_change/Readme.adoc[] diff --git a/test/case/misc/all.yaml b/test/case/misc/all.yaml index ff077c15ed..94faf9f460 100644 --- a/test/case/misc/all.yaml +++ b/test/case/misc/all.yaml @@ -5,5 +5,8 @@ - name: Support Data Collection case: support_collect/test.py +- name: On-Change Subscription + case: on_change/test.py + #- name: start_from_startup # case: start_from_startup.py diff --git a/test/case/misc/on_change/Readme.adoc b/test/case/misc/on_change/Readme.adoc new file mode 120000 index 0000000000..ae32c84120 --- /dev/null +++ b/test/case/misc/on_change/Readme.adoc @@ -0,0 +1 @@ +test.adoc \ No newline at end of file diff --git a/test/case/misc/on_change/test.adoc b/test/case/misc/on_change/test.adoc new file mode 100644 index 0000000000..5f6c1146fd --- /dev/null +++ b/test/case/misc/on_change/test.adoc @@ -0,0 +1,37 @@ +=== On-Change Subscription + +ifdef::topdoc[:imagesdir: {topdoc}../../test/case/misc/on_change] + +==== Description + +Verify that a client subscribed to changes in the operational datastore +is told within a second when an interface loses its link and when the +link returns, without polling. A veth pair gives a link the test +controls: disabling one end takes the link away from the other. One +subscription follows the operational state of the interfaces, a second +one the alarm list, where the lost link raises a link-down alarm that +is cleared when the link returns. + +==== Topology + +image::topology.svg[On-Change Subscription topology, align=center, scaledwidth=75%] + +==== Sequence + +. Set up topology and attach to target DUT +. Configure veth pair veth0a and veth0b, both enabled +. Verify veth0a is up +. Subscribe to changes of oper-status in the operational datastore +. Verify the subscription starts with the oper-status of all interfaces +. Subscribe to changes of the alarm list in the operational datastore +. Verify the alarm subscription starts with the alarm list +. Disable veth0b +. Verify the subscription reports veth0a no longer up within 1 second +. Verify the alarm subscription reports a link-down alarm on veth0a within 15 seconds +. Enable veth0b +. Verify the subscription reports veth0a up within 1 second +. Verify the alarm subscription reports the alarm on veth0a cleared within 2 seconds +. Remove veth pair +. Purge cleared alarms + + diff --git a/test/case/misc/on_change/test.py b/test/case/misc/on_change/test.py new file mode 100755 index 0000000000..3d4a942965 --- /dev/null +++ b/test/case/misc/on_change/test.py @@ -0,0 +1,128 @@ +#!/usr/bin/env python3 +""" +On-change subscription + +Verify that a client subscribed to changes in the operational datastore +is told within a second when an interface loses its link and when the +link returns, without polling. A veth pair gives a link the test +controls: disabling one end takes the link away from the other. One +subscription follows the operational state of the interfaces, a second +one the alarm list, where the lost link raises a link-down alarm that +is cleared when the link returns. +""" +import json + +import infamy +from infamy import iface +from infamy.util import until + +ALARMS = "/ietf-alarms:alarms" + + +def veth(enabled_b): + return { + "ietf-interfaces": { + "interfaces": { + "interface": [ + { + "name": "veth0a", + "type": "infix-if-type:veth", + "enabled": True, + "infix-interfaces:veth": {"peer": "veth0b"}, + }, + { + "name": "veth0b", + "type": "infix-if-type:veth", + "enabled": enabled_b, + "infix-interfaces:veth": {"peer": "veth0a"}, + }, + ] + } + } + } + + +def edits(event): + update = event.get("ietf-yang-push:push-change-update", {}) + return update.get("datastore-changes", {}).get("yang-patch", {}).get("edit", []) + + +def oper_status(port, up): + path = f"/ietf-interfaces:interfaces/interface[name='{port}']/oper-status" + + def match(event): + return any(edit["target"] == path and + (edit.get("value", {}).get("ietf-interfaces:oper-status") == "up") == up + for edit in edits(event)) + + return match + + +def alarm_cleared(name, cleared): + def match(event): + return any(f"interface[name='{name}']" in edit["target"] and + f"is-cleared: {cleared}" in json.dumps(edit.get("value")).replace('"', "") + for edit in edits(event)) + + return match + + +with infamy.Test() as test: + with test.step("Set up topology and attach to target DUT"): + env = infamy.Env() + target = env.attach("target", "mgmt") + + with test.step("Configure veth pair veth0a and veth0b, both enabled"): + target.put_config_dicts(veth(True)) + + with test.step("Verify veth0a is up"): + until(lambda: iface.get_param(target, "veth0a", "oper-status") == "up") + + with test.step("Subscribe to changes of oper-status in the operational datastore"): + status = target.subscribe({ + "ietf-yang-push:datastore": "ietf-datastores:operational", + "ietf-yang-push:datastore-xpath-filter": + "/ietf-interfaces:interfaces/interface/oper-status", + "ietf-yang-push:on-change": {} + }) + + with test.step("Verify the subscription starts with the oper-status of all interfaces"): + assert status.wait_for(lambda event: "ietf-yang-push:push-update" in event, 10) + + with test.step("Subscribe to changes of the alarm list in the operational datastore"): + alarms = target.subscribe({ + "ietf-yang-push:datastore": "ietf-datastores:operational", + "ietf-yang-push:datastore-xpath-filter": "/ietf-alarms:alarms/alarm-list", + "ietf-yang-push:on-change": {} + }) + + with test.step("Verify the alarm subscription starts with the alarm list"): + assert alarms.wait_for(lambda event: "ietf-yang-push:push-update" in event, 10) + + with test.step("Disable veth0b"): + target.put_config_dicts(veth(False)) + + with test.step("Verify the subscription reports veth0a no longer up within 1 second"): + assert status.wait_for(oper_status("veth0a", False), 1) + + with test.step("Verify the alarm subscription reports a link-down alarm on veth0a within 15 seconds"): + assert alarms.wait_for(alarm_cleared("veth0a", "false"), 15) + + with test.step("Enable veth0b"): + target.put_config_dicts(veth(True)) + + with test.step("Verify the subscription reports veth0a up within 1 second"): + assert status.wait_for(oper_status("veth0a", True), 1) + + with test.step("Verify the alarm subscription reports the alarm on veth0a cleared within 2 seconds"): + assert alarms.wait_for(alarm_cleared("veth0a", "true"), 2) + + with test.step("Remove veth pair"): + target.put_config_dicts({}, delete=["/ietf-interfaces:interfaces/interface[name='veth0a']", + "/ietf-interfaces:interfaces/interface[name='veth0b']"]) + + with test.step("Purge cleared alarms"): + target.call_action_output(f"{ALARMS}/alarm-list/purge-alarms", + {"alarm-clearance-status": "cleared"}) + + test.succeed() diff --git a/test/case/misc/on_change/topology.dot b/test/case/misc/on_change/topology.dot new file mode 120000 index 0000000000..02b788692c --- /dev/null +++ b/test/case/misc/on_change/topology.dot @@ -0,0 +1 @@ +../../../infamy/topologies/1x1.dot \ No newline at end of file diff --git a/test/case/misc/on_change/topology.svg b/test/case/misc/on_change/topology.svg new file mode 100644 index 0000000000..6fc6f47a8e --- /dev/null +++ b/test/case/misc/on_change/topology.svg @@ -0,0 +1,33 @@ + + + + + + +1x1 + + + +host + +host + +mgmt + + + +target + +mgmt + +target + + + +host:mgmt--target:mgmt + + + + diff --git a/test/case/services/Readme.adoc b/test/case/services/Readme.adoc index 2a321fa341..72c2cdb0b6 100644 --- a/test/case/services/Readme.adoc +++ b/test/case/services/Readme.adoc @@ -9,6 +9,7 @@ Tests verifying network services configuration and operation: - LLDP per-interface administrative status configuration - LLDP IEEE group address forwarding behavior - RESTCONF rate limiting + - RESTCONF event streams - SSH server configuration and access control - SSH public key authentication mechanisms - TFTP server file serving and operational file listing @@ -41,6 +42,10 @@ include::restconf/rate_limit/Readme.adoc[] <<< +include::restconf/streams/Readme.adoc[] + +<<< + include::ssh/ssh_server_config/Readme.adoc[] <<< diff --git a/test/case/services/restconf/all.yaml b/test/case/services/restconf/all.yaml index 077b85edfa..438b4ef351 100644 --- a/test/case/services/restconf/all.yaml +++ b/test/case/services/restconf/all.yaml @@ -1,3 +1,6 @@ --- - name: RESTCONF Rate Limit case: rate_limit/test.py + +- name: RESTCONF Event Stream + case: streams/test.py diff --git a/test/case/services/restconf/streams/Readme.adoc b/test/case/services/restconf/streams/Readme.adoc new file mode 120000 index 0000000000..ae32c84120 --- /dev/null +++ b/test/case/services/restconf/streams/Readme.adoc @@ -0,0 +1 @@ +test.adoc \ No newline at end of file diff --git a/test/case/services/restconf/streams/test.adoc b/test/case/services/restconf/streams/test.adoc new file mode 100644 index 0000000000..7816f12027 --- /dev/null +++ b/test/case/services/restconf/streams/test.adoc @@ -0,0 +1,25 @@ +=== RESTCONF Event Stream + +ifdef::topdoc[:imagesdir: {topdoc}../../test/case/services/restconf/streams] + +==== Description + +Verify that a client can subscribe to notifications over RESTCONF +without sending a Forward header, that the event stream stays open +while nothing happens, and that it then delivers an event. + +==== Topology + +image::topology.svg[RESTCONF Event Stream topology, align=center, scaledwidth=75%] + +==== Sequence + +. Set up topology and attach to target DUT +. Subscribe to the NETCONF stream without a Forward header +. Verify the reply has a stream URI on the address of the DUT +. Open the event stream from the host +. Verify the stream is still open after 150 seconds without events +. Set hostname to stream-test +. Verify the stream delivers a netconf-config-change event + + diff --git a/test/case/services/restconf/streams/test.py b/test/case/services/restconf/streams/test.py new file mode 100755 index 0000000000..333c86194f --- /dev/null +++ b/test/case/services/restconf/streams/test.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +""" +RESTCONF event stream + +Verify that a client can subscribe to notifications over RESTCONF +without sending a Forward header, that the event stream stays open +while nothing happens, and that it then delivers an event. +""" +import time + +import infamy +import infamy.sse as sse + + +with infamy.Test() as test: + with test.step("Set up topology and attach to target DUT"): + env = infamy.Env() + target = env.attach("target", "mgmt", "restconf") + host = target.get_mgmt_ip() + + with test.step("Subscribe to the NETCONF stream without a Forward header"): + uri = sse.subscribe(target, { + "stream": "NETCONF", + "encoding": "encode-json" + }) + + with test.step("Verify the reply has a stream URI on the address of the DUT"): + address = host.split("%")[0] + assert uri.startswith(f"https://[{address}]/streams/subscribed/"), uri + + with test.step("Open the event stream from the host"): + stream = sse.Stream(target, uri) + + with test.step("Verify the stream is still open after 150 seconds without events"): + time.sleep(150) + assert not stream.closed.is_set() + assert stream.events.empty() + + with test.step("Set hostname to stream-test"): + target.put_config_dicts({ + "ietf-system": { + "system": { + "hostname": "stream-test" + } + } + }) + + with test.step("Verify the stream delivers a netconf-config-change event"): + assert stream.wait_for(lambda event: "ietf-netconf-notifications:netconf-config-change" in event, 10) + + test.succeed() diff --git a/test/case/services/restconf/streams/topology.dot b/test/case/services/restconf/streams/topology.dot new file mode 120000 index 0000000000..54a4185e12 --- /dev/null +++ b/test/case/services/restconf/streams/topology.dot @@ -0,0 +1 @@ +../../../../infamy/topologies/1x1.dot \ No newline at end of file diff --git a/test/case/services/restconf/streams/topology.svg b/test/case/services/restconf/streams/topology.svg new file mode 100644 index 0000000000..6fc6f47a8e --- /dev/null +++ b/test/case/services/restconf/streams/topology.svg @@ -0,0 +1,33 @@ + + + + + + +1x1 + + + +host + +host + +mgmt + + + +target + +mgmt + +target + + + +host:mgmt--target:mgmt + + + + diff --git a/test/infamy/netconf.py b/test/infamy/netconf.py index 1bee1193b5..1cf3b9e25a 100644 --- a/test/infamy/netconf.py +++ b/test/infamy/netconf.py @@ -18,7 +18,9 @@ import netconf_client.ncclient import netconf_client.session import paramiko -from infamy.transport import Transport,infer_put_dict +import json +import threading +from infamy.transport import Events, Transport, infer_put_dict from netconf_client.error import RpcError from . import env, netutil, coverage @@ -99,6 +101,50 @@ def __init__(self, raw): self.schema = self.ele.text +class Subscription(Events): + """Notifications of one subscription, on a NETCONF session of its own""" + + def __init__(self, dev, params): + super().__init__() + self.ly = dev.ly + self.ncc = Manager(dev._ncc_connect(dev.location), timeout=150) + mod = self.ly.get_module("ietf-subscribed-notifications") + rpc = mod.parse_data_dict({"establish-subscription": params}, rpc=True) + xml = rpc.print_mem("xml", with_siblings=True, pretty=False) + + # The server resolves the module names in the filter as XML prefixes + xmlns = "".join(f' xmlns:{m["name"]}="{m["namespace"]}"' for m in + dev._modules_in_xpath(params.get("ietf-yang-push:datastore-xpath-filter", ""))) + self.ncc.dispatch(xml.replace("= end: + return None + + def infer_put_dict(name, models): if not models.get("ietf-system"): models["ietf-system"] = { "system": { "hostname": name} } @@ -57,6 +83,15 @@ def rpc_output(self, module, rpc, input_data=None): """ pass + @abstractmethod + def subscribe(self, params): + """Establish a subscription and return its Events. + + `params` is the input of establish-subscription, without the + encoding, which each transport sets for itself. + """ + pass + @abstractmethod def call_action(self, xpath, input_data=None): """Invoke a YANG action at `xpath`.