Skip to content

Commit a7b816d

Browse files
committed
parser UPDATE eventTime validation using a compiled leaf
Alternative to compiling a parsed type.
1 parent cba757c commit a7b816d

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

src/parser_common.c

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author Michal Vasko <[email protected]>
44
* @brief libyang common parser functions.
55
*
6-
* Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
6+
* Copyright (c) 2015 - 2024 CESNET, z.s.p.o.
77
*
88
* This source code is licensed under BSD 3-Clause License (the "License").
99
* You may not use this file except in compliance with the License.
@@ -71,33 +71,47 @@ lyd_parser_notif_eventtime_validate(const struct lyd_node *node)
7171
LY_ERR rc = LY_SUCCESS;
7272
struct ly_ctx *ctx = (struct ly_ctx *)LYD_CTX(node);
7373
struct lysc_ctx cctx;
74-
const struct lys_module *mod;
74+
const struct lys_module *mod1, *mod2;
75+
const struct lysc_node *schema;
7576
LY_ARRAY_COUNT_TYPE u;
7677
struct ly_err_item *err = NULL;
7778
struct lysp_type *type_p = NULL;
7879
struct lysc_pattern **patterns = NULL;
7980
const char *value;
8081

81-
LYSC_CTX_INIT_CTX(cctx, ctx);
82+
/* find the used modules, we will either use a compiled leaf or compile the relevant type ourselves */
83+
mod1 = ly_ctx_get_module_implemented(ctx, "notifications");
84+
mod2 = ly_ctx_get_module_latest(ctx, "ietf-yang-types");
85+
assert(mod2);
8286

83-
/* get date-and-time parsed type */
84-
mod = ly_ctx_get_module_latest(ctx, "ietf-yang-types");
85-
assert(mod);
86-
LY_ARRAY_FOR(mod->parsed->typedefs, u) {
87-
if (!strcmp(mod->parsed->typedefs[u].name, "date-and-time")) {
88-
type_p = &mod->parsed->typedefs[u].type;
89-
break;
87+
if (mod1 || !mod2->parsed) {
88+
/* get date-and-time leaf */
89+
schema = lys_find_path(LYD_CTX(node), NULL, "/notifications:notification/eventTime", 0);
90+
LY_CHECK_RET(!schema, LY_ENOTFOUND);
91+
92+
/* validate the value */
93+
value = lyd_get_value(node);
94+
LY_CHECK_RET(lyd_value_validate(LYD_CTX(node), schema, value, strlen(value), NULL, NULL, NULL));
95+
} else {
96+
LYSC_CTX_INIT_CTX(cctx, ctx);
97+
98+
/* get date-and-time parsed type */
99+
LY_ARRAY_FOR(mod2->parsed->typedefs, u) {
100+
if (!strcmp(mod2->parsed->typedefs[u].name, "date-and-time")) {
101+
type_p = &mod2->parsed->typedefs[u].type;
102+
break;
103+
}
90104
}
91-
}
92-
assert(type_p);
105+
assert(type_p);
93106

94-
/* compile patterns */
95-
assert(type_p->patterns);
96-
LY_CHECK_GOTO(rc = lys_compile_type_patterns(&cctx, type_p->patterns, NULL, &patterns), cleanup);
107+
/* compile patterns */
108+
assert(type_p->patterns);
109+
LY_CHECK_GOTO(rc = lys_compile_type_patterns(&cctx, type_p->patterns, NULL, &patterns), cleanup);
97110

98-
/* validate */
99-
value = lyd_get_value(node);
100-
rc = lyplg_type_validate_patterns(patterns, value, strlen(value), &err);
111+
/* validate */
112+
value = lyd_get_value(node);
113+
rc = lyplg_type_validate_patterns(patterns, value, strlen(value), &err);
114+
}
101115

102116
cleanup:
103117
FREE_ARRAY(&cctx.free_ctx, patterns, lysc_pattern_free);

tests/modules/yang/[email protected]

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ module notifications {
8080
}
8181
}
8282

83-
/*container notification {
83+
container notification {
8484
description "internal struct to start a notification";
8585
config false;
8686

@@ -90,6 +90,6 @@ module notifications {
9090
}
9191

9292
// eventType and any data content goes here
93-
}*/
93+
}
9494
}
9595

0 commit comments

Comments
 (0)