yin parser CHANGE add support for notification element
diff --git a/src/parser_yin.c b/src/parser_yin.c
index d57579a..a0d356f 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -1528,6 +1528,7 @@
/* parse list content */
struct tree_node_meta new_node_meta = {(struct lysp_node *)list, &list->child};
struct typedef_meta typedef_meta = {(struct lysp_node *)list, &list->typedefs};
+ struct notif_meta notif_meta = {(struct lysp_node *)list, &list->notifs};
struct yin_subelement subelems[25] = {
/* TODO action */
{YANG_ACTION, NULL, 0},
@@ -1549,8 +1550,7 @@
{YANG_MAX_ELEMENTS, list, YIN_SUBELEM_UNIQUE},
{YANG_MIN_ELEMENTS, list, YIN_SUBELEM_UNIQUE},
{YANG_MUST, &list->musts, 0},
- /* TODO notification */
- {YANG_NOTIFICATION, NULL, 0},
+ {YANG_NOTIFICATION, ¬if_meta, 0},
{YANG_ORDERED_BY, &list->flags, YIN_SUBELEM_UNIQUE},
{YANG_REFERENCE, &list->ref, YIN_SUBELEM_UNIQUE},
{YANG_STATUS, &list->flags, YIN_SUBELEM_UNIQUE},
@@ -1576,6 +1576,62 @@
}
/**
+ * @brief Parse notification element.
+ *
+ * @param[in,out] ctx YIN parser context for logging and to store current state.
+ * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
+ * @param[in,out] data Data to read from, always moved to currently handled character.
+ * @param[in,out] notif_meta Meta information about node parent and notifications to add to.
+ *
+ * @return LY_ERR values.
+ */
+static LY_ERR
+yin_parse_notification(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
+ struct notif_meta *notif_meta)
+{
+ struct lysp_notif *notif;
+
+ /* allocate new notification */
+ LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *notif_meta->notifs, notif, LY_EMEM);
+ notif->nodetype = LYS_NOTIF;
+ notif->parent = notif_meta->parent;
+
+ /* parse argument */
+ LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_NAME, ¬if->name, Y_IDENTIF_ARG, YANG_NOTIFICATION));
+
+ /* parse notification content */
+ struct tree_node_meta node_meta = {(struct lysp_node *)notif, ¬if->data};
+ struct typedef_meta typedef_meta = {(struct lysp_node *)notif, ¬if->typedefs};
+ struct yin_subelement subelems[16] = {
+ {YANG_ANYDATA, &node_meta, 0},
+ {YANG_ANYXML, &node_meta, 0},
+ /* TODO choice */
+ {YANG_CHOICE, NULL, 0},
+ /* TODO container */
+ {YANG_CONTAINER, NULL, 0},
+ {YANG_DESCRIPTION, ¬if->dsc, YIN_SUBELEM_UNIQUE},
+ /* TODO grouping */
+ {YANG_GROUPING, NULL, 0},
+ {YANG_IF_FEATURE, ¬if->iffeatures, 0},
+ {YANG_LEAF, &node_meta, 0},
+ {YANG_LEAF_LIST, &node_meta, 0},
+ {YANG_LIST, &node_meta, 0},
+ {YANG_MUST, ¬if->musts, YIN_SUBELEM_VER2},
+ {YANG_REFERENCE, ¬if->ref, YIN_SUBELEM_UNIQUE},
+ {YANG_STATUS, ¬if->flags, YIN_SUBELEM_UNIQUE},
+ {YANG_TYPEDEF, &typedef_meta, 0},
+ {YANG_USES, &node_meta, 0},
+ {YANG_CUSTOM, NULL, 0},
+ };
+ LY_CHECK_RET(yin_parse_content(ctx, subelems, 16, data, YANG_NOTIFICATION, NULL, ¬if->exts));
+
+ /* finalize parent pointers to the reallocated items */
+ LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, notif->groupings, NULL, NULL, NULL));
+
+ return LY_SUCCESS;
+}
+
+/**
* @brief Map keyword type to substatement info.
*
* @param[in] kw Keyword type.
@@ -1879,6 +1935,7 @@
YIN_ARG_URI, Y_STR_ARG, exts);
break;
case YANG_NOTIFICATION:
+ ret = yin_parse_notification(ctx, attrs, data, (struct notif_meta *)subelem->dest);
break;
case YANG_ORDERED_BY:
ret = yin_parse_orderedby(ctx, attrs, data, (uint16_t *)subelem->dest, exts);
diff --git a/src/parser_yin.h b/src/parser_yin.h
index 2fcae20..e21dd55 100644
--- a/src/parser_yin.h
+++ b/src/parser_yin.h
@@ -115,6 +115,11 @@
struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to */
};
+struct notif_meta {
+ struct lysp_node *parent; /**< parent node */
+ struct lysp_notif **notifs; /**< [Sized array](@ref sizedarrays) of notifications to add to */
+};
+
/**
* @brief Match argument name.
*
diff --git a/tests/src/test_parser_yin.c b/tests/src/test_parser_yin.c
index 461f3fa..37ccc56 100644
--- a/tests/src/test_parser_yin.c
+++ b/tests/src/test_parser_yin.c
@@ -39,6 +39,7 @@
void lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include);
void lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat);
void lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident);
+void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
struct state {
struct ly_ctx *ctx;
@@ -2551,7 +2552,7 @@
// "<action name=\"action\"/>"
// "<container name=\"cont\"/>"
// "<grouping name=\"grp\"/>"
- // "<notification name=\"notf\"/>"
+ "<notification name=\"notf\"/>"
"<leaf name=\"leaf\"/>"
"<leaf-list name=\"llist\"/>"
"<list name=\"sub-list\"/>"
@@ -2572,7 +2573,7 @@
// assert_string_equal(parsed->child->next->next->name, "cont");
// assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
// assert_string_equal(parsed->groupings->name, "grp");
- // assert_string_equal(parsed->notifs->name, "notf");
+ assert_string_equal(parsed->notifs->name, "notf");
assert_null(parsed->exts);
assert_true(parsed->flags & LYS_ORDBY_USER);
assert_true(parsed->flags & LYS_STATUS_DEPRC);
@@ -2604,6 +2605,73 @@
st->finished_correctly = true;
}
+static void
+test_notification_elem(void **state)
+{
+ struct state *st = *state;
+ const char *data;
+ struct lysp_notif *notifs = NULL;
+ struct notif_meta notif_meta = {NULL, ¬ifs};
+
+ /* max subelems */
+ st->yin_ctx->mod_version = LYS_VERSION_1_1;
+ data = ELEMENT_WRAPPER_START
+ "<notification name=\"notif-name\">"
+ "<anydata name=\"anyd\"/>"
+ "<anyxml name=\"anyx\"/>"
+ "<description><text>desc</text></description>"
+ "<if-feature name=\"iff\"/>"
+ "<leaf name=\"leaf\"/>"
+ "<leaf-list name=\"llist\"/>"
+ "<list name=\"sub-list\"/>"
+ "<must condition=\"cond\"/>"
+ "<reference><text>ref</text></reference>"
+ "<status value=\"deprecated\"/>"
+ "<typedef name=\"tpdf\"/>"
+ "<uses name=\"uses-name\"/>"
+ // "<choice name=\"choice\"/>"
+ // "<container name=\"cont\"/>"
+ // "<grouping name=\"grp\"/>"
+ "</notification>"
+ ELEMENT_WRAPPER_END;
+ assert_int_equal(test_element_helper(st, &data, ¬if_meta, NULL, NULL, true), LY_SUCCESS);
+ assert_string_equal(notifs->name, "notif-name");
+ assert_string_equal(notifs->data->name, "anyd");
+ assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
+ assert_string_equal(notifs->data->next->name, "anyx");
+ assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
+ assert_string_equal(notifs->data->next->next->name, "leaf");
+ assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
+ assert_string_equal(notifs->data->next->next->next->name, "llist");
+ assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
+ assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
+ assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
+ assert_null(notifs->exts);
+ assert_true(notifs->flags & LYS_STATUS_DEPRC);
+ // assert_string_equal(notifs->groupings->name, "grp");
+ // assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_CHOICE);
+ // assert_string_equal(notifs->data->next->next->next->next->next->name, "choice");
+ // assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_CONTAINER);
+ // assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
+ // assert_null(notifs->data->next->next->next->next->next->next->next);
+ assert_string_equal(*notifs->iffeatures, "iff");
+ assert_string_equal(notifs->musts->arg, "cond");
+ assert_int_equal(notifs->nodetype, LYS_NOTIF);
+ assert_null(notifs->parent);
+ assert_string_equal(notifs->ref, "ref");
+ assert_string_equal(notifs->typedefs->name, "tpdf");
+ FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
+ notifs = NULL;
+
+ /* min subelems */
+ data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
+ assert_int_equal(test_element_helper(st, &data, ¬if_meta, NULL, NULL, true), LY_SUCCESS);
+ assert_string_equal(notifs->name, "notif-name");
+ FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
+
+ st->finished_correctly = true;
+}
+
int
main(void)
{
@@ -2665,6 +2733,8 @@
cmocka_unit_test_setup_teardown(test_feature_elem, setup_element_test, teardown_element_test),
cmocka_unit_test_setup_teardown(test_identity_elem, setup_element_test, teardown_element_test),
cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_element_test),
+ cmocka_unit_test_setup_teardown(test_notification_elem, setup_element_test, teardown_element_test),
+
};
return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);