yin parser CHANGE add support for deviation element
diff --git a/src/parser_yin.c b/src/parser_yin.c
index 805f5a6..056c744 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -2043,6 +2043,16 @@
     return LY_SUCCESS;
 }
 
+/**
+ * @brief Parse deviate 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] deviates Deviates to add to.
+ *
+ * @return LY_ERR values.
+ */
 static LY_ERR
 yin_parse_deviate(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
                   struct lysp_deviate **deviates)
@@ -2146,11 +2156,41 @@
 
 cleanup:
     free(d);
-    /* TODO log deviate error */
     return ret;
 }
 
 /**
+ * @brief Parse deviation 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] deviations Deviations to add to.
+ *
+ * @return LY_ERR values.
+ */
+static LY_ERR
+yin_parse_deviation(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
+                    struct lysp_deviation **deviations)
+{
+    struct lysp_deviation *dev;
+
+    /* create new deviation */
+    LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *deviations, dev, LY_EMEM);
+
+    /* parse argument */
+    LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &dev->nodeid, Y_STR_ARG, YANG_DEVIATE));
+    YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(dev->nodeid), "deviation");
+    struct yin_subelement subelems[4] = {
+                                            {YANG_DESCRIPTION, &dev->dsc, YIN_SUBELEM_UNIQUE},
+                                            {YANG_DEVIATE, &dev->deviates, YIN_SUBELEM_MANDATORY},
+                                            {YANG_REFERENCE, &dev->ref, YIN_SUBELEM_UNIQUE},
+                                            {YANG_CUSTOM, NULL, 0},
+                                        };
+    return yin_parse_content(ctx, subelems, 4, data, YANG_DEVIATE, NULL, &dev->exts);
+}
+
+/**
  * @brief Map keyword type to substatement info.
  *
  * @param[in] kw Keyword type.
@@ -2371,6 +2411,7 @@
                     ret = yin_parse_deviate(ctx, attrs, data, (struct lysp_deviate **)subelem->dest);
                     break;
                 case YANG_DEVIATION:
+                    ret = yin_parse_deviation(ctx, attrs, data, (struct lysp_deviation **)subelem->dest);
                     break;
                 case YANG_ERROR_APP_TAG:
                     ret = yin_parse_simple_element(ctx, attrs, data, kw, (const char **)subelem->dest,
@@ -2574,10 +2615,9 @@
             /* load closing element */
             LY_CHECK_RET(lyxml_get_element(&ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len));
         }
-
-        /* mandatory subelemnts are checked only after whole element was succesfully parsed */
-        LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
     }
+    /* mandatory subelemnts are checked only after whole element was succesfully parsed */
+    LY_CHECK_RET(yin_check_subelem_mandatory_constraint(ctx, subelem_info, subelem_info_size, current_element));
 
 cleanup:
     FREE_ARRAY(ctx, attrs, free_arg_rec);
diff --git a/tests/src/test_parser_yin.c b/tests/src/test_parser_yin.c
index 6153792..8e2bc82 100644
--- a/tests/src/test_parser_yin.c
+++ b/tests/src/test_parser_yin.c
@@ -45,6 +45,7 @@
 void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
 void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
 void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
+void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
 
 struct state {
     struct ly_ctx *ctx;
@@ -2559,10 +2560,10 @@
                     "<action name=\"action\"/>"
                     "<grouping name=\"grp\"/>"
                     "<notification name=\"notf\"/>"
-                    "<leaf name=\"leaf\"/>"
-                    "<leaf-list name=\"llist\"/>"
+                    "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
+                    "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
                     "<list name=\"sub-list\"/>"
-                    "<typedef name=\"tpdf\"/>"
+                    "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
                     "<uses name=\"uses-name\"/>"
                 "</list>"
            ELEMENT_WRAPPER_END;
@@ -2637,13 +2638,13 @@
                     "<anyxml name=\"anyx\"/>"
                     "<description><text>desc</text></description>"
                     "<if-feature name=\"iff\"/>"
-                    "<leaf name=\"leaf\"/>"
-                    "<leaf-list name=\"llist\"/>"
+                    "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
+                    "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
                     "<list name=\"sub-list\"/>"
                     "<must condition=\"cond\"/>"
                     "<reference><text>ref</text></reference>"
                     "<status value=\"deprecated\"/>"
-                    "<typedef name=\"tpdf\"/>"
+                    "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
                     "<uses name=\"uses-name\"/>"
                     "<container name=\"cont\"/>"
                     "<choice name=\"choice\"/>"
@@ -2707,13 +2708,13 @@
                     "<anyxml name=\"anyx\"/>"
                     "<description><text>desc</text></description>"
                     "<grouping name=\"sub-grp\"/>"
-                    "<leaf name=\"leaf\"/>"
-                    "<leaf-list name=\"llist\"/>"
+                    "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
+                    "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
                     "<list name=\"list\"/>"
                     "<notification name=\"notf\"/>"
                     "<reference><text>ref</text></reference>"
                     "<status value=\"current\"/>"
-                    "<typedef name=\"tpdf\"/>"
+                    "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
                     "<uses name=\"uses-name\"/>"
                     "<action name=\"act\"/>"
                     "<container name=\"cont\"/>"
@@ -2776,15 +2777,15 @@
                     "<description><text>desc</text></description>"
                     "<grouping name=\"sub-grp\"/>"
                     "<if-feature name=\"iff\"/>"
-                    "<leaf name=\"leaf\"/>"
-                    "<leaf-list name=\"llist\"/>"
+                    "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
+                    "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
                     "<list name=\"list\"/>"
                     "<must condition=\"cond\"/>"
                     "<notification name=\"notf\"/>"
                     "<presence value=\"presence\"/>"
                     "<reference><text>ref</text></reference>"
                     "<status value=\"current\"/>"
-                    "<typedef name=\"tpdf\"/>"
+                    "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
                     "<uses name=\"uses-name\"/>"
                     "<when condition=\"when-cond\"/>"
                     "<action name=\"act\"/>"
@@ -2860,8 +2861,8 @@
                     "<container name=\"subcont\"/>"
                     "<description><text>desc</text></description>"
                     "<if-feature name=\"iff\"/>"
-                    "<leaf name=\"leaf\"/>"
-                    "<leaf-list name=\"llist\"/>"
+                    "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
+                    "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
                     "<list name=\"list\"/>"
                     "<reference><text>ref</text></reference>"
                     "<status value=\"current\"/>"
@@ -2935,8 +2936,8 @@
                     "<default value=\"def\"/>"
                     "<description><text>desc</text></description>"
                     "<if-feature name=\"iff\"/>"
-                    "<leaf name=\"leaf\"/>"
-                    "<leaf-list name=\"llist\"/>"
+                    "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
+                    "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
                     "<list name=\"list\"/>"
                     "<mandatory value=\"true\" />"
                     "<reference><text>ref</text></reference>"
@@ -3004,11 +3005,11 @@
                     "<choice name=\"choice\"/>"
                     "<container name=\"subcont\"/>"
                     "<grouping name=\"sub-grp\"/>"
-                    "<leaf name=\"leaf\"/>"
-                    "<leaf-list name=\"llist\"/>"
+                    "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
+                    "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
                     "<list name=\"list\"/>"
                     "<must condition=\"cond\"/>"
-                    "<typedef name=\"tpdf\"/>"
+                    "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
                     "<uses name=\"uses-name\"/>"
                 "</input>"
            ELEMENT_WRAPPER_END;
@@ -3047,11 +3048,11 @@
                     "<choice name=\"choice\"/>"
                     "<container name=\"subcont\"/>"
                     "<grouping name=\"sub-grp\"/>"
-                    "<leaf name=\"leaf\"/>"
-                    "<leaf-list name=\"llist\"/>"
+                    "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
+                    "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
                     "<list name=\"list\"/>"
                     "<must condition=\"cond\"/>"
-                    "<typedef name=\"tpdf\"/>"
+                    "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
                     "<uses name=\"uses-name\"/>"
                 "</output>"
            ELEMENT_WRAPPER_END;
@@ -3118,7 +3119,7 @@
                     "<output><must condition=\"cond\"/></output>"
                     "<reference><text>ref</text></reference>"
                     "<status value=\"deprecated\"/>"
-                    "<typedef name=\"tpdf\"/>"
+                    "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
                 "</action>"
            ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
@@ -3147,7 +3148,7 @@
                     "<output><must condition=\"cond\"/></output>"
                     "<reference><text>ref</text></reference>"
                     "<status value=\"deprecated\"/>"
-                    "<typedef name=\"tpdf\"/>"
+                    "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
                 "</rpc>"
            ELEMENT_WRAPPER_END;
     assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
@@ -3195,8 +3196,8 @@
                     "<container name=\"subcont\"/>"
                     "<description><text>desc</text></description>"
                     "<if-feature name=\"iff\"/>"
-                    "<leaf name=\"leaf\"/>"
-                    "<leaf-list name=\"llist\"/>"
+                    "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
+                    "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
                     "<list name=\"list\"/>"
                     "<notification name=\"notif\"/>"
                     "<reference><text>ref</text></reference>"
@@ -3405,6 +3406,51 @@
     st->finished_correctly = true;
 }
 
+static void
+test_deviation_elem(void **state)
+{
+    struct state *st = *state;
+    const char *data;
+    struct lysp_deviation *deviations = NULL;
+
+    /* min subelems */
+    data = ELEMENT_WRAPPER_START
+                "<deviation target-node=\"target\">"
+                    "<deviate value=\"not-supported\"/>"
+                "</deviation>"
+           ELEMENT_WRAPPER_END;
+    assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, true), LY_SUCCESS);
+    assert_string_equal(deviations->nodeid, "target");
+    assert_int_equal(deviations->deviates->mod, LYS_DEV_NOT_SUPPORTED);
+    FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
+    deviations = NULL;
+
+    /* max subelems */
+    data = ELEMENT_WRAPPER_START
+                "<deviation target-node=\"target\">"
+                    "<reference><text>ref</text></reference>"
+                    "<description><text>desc</text></description>"
+                    "<deviate value=\"add\"/>"
+                "</deviation>"
+           ELEMENT_WRAPPER_END;
+    assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, true), LY_SUCCESS);
+    assert_string_equal(deviations->nodeid, "target");
+    assert_int_equal(deviations->deviates->mod, LYS_DEV_ADD);
+    assert_string_equal(deviations->ref, "ref");
+    assert_string_equal(deviations->dsc, "desc");
+    FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
+    deviations = NULL;
+
+    /* invalid */
+    data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
+    assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, false), LY_EVALID);
+    FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
+    deviations = NULL;
+    logbuf_assert("Missing mandatory subelement deviate of deviate element. Line number 1.");
+
+    st->finished_correctly = true;
+}
+
 int
 main(void)
 {
@@ -3475,6 +3521,7 @@
         cmocka_unit_test_setup_teardown(test_action_elem, setup_element_test, teardown_element_test),
         cmocka_unit_test_setup_teardown(test_augment_elem, setup_element_test, teardown_element_test),
         cmocka_unit_test_setup_teardown(test_deviate_elem, setup_element_test, teardown_element_test),
+        cmocka_unit_test_setup_teardown(test_deviation_elem, setup_element_test, teardown_element_test),
     };
 
     return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);