Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Container node is added into Libyang data tree during parsing JSON file even if when status is false. #452

Closed
corrollFang opened this issue Mar 7, 2018 · 8 comments

Comments

@corrollFang
Copy link

corrollFang commented Mar 7, 2018

----------base.yang--------------

module base{
    namespace "http://test-when/base";
    prefix "base";
    container c1
    {
        leaf flag
        {
            type boolean;
        }
    }
}

----------augment-when.yang-----------

module augment-when {
    namespace "http://test-when/augment-when";
    prefix "augment-when";
    import base {
        prefix "base";
    }
    augment "/base:c1" 
    {
        when "flag = 'true'";
        container aug-1
        {
            leaf xxx
            {
                type uint32;
            }
        }
    }
}

---JSON files---

{"base:c1":{"flag":false}}

After calling lyd_parse_fd(tmp_ctx->ctx, fd, LYD_JSON, LYD_OPT_TRUSTED | LYD_OPT_CONFIG), the returned data tree nodes are as below:

  <c1 xmlns="http://test-when/base">
    <flag>false</flag>
    <augment-when xmlns="http://test-when/augment-when"/>
  </c1>`

The container "augment-when" is added during lyd_wd_add_subtree() and will cause validation failed. I think it's not a good behavior, so i suggest to evaluate when expressions during adding default nodes.

@michalvasko
Copy link
Member

Hi,
I believe this is a duplicate of #433 but I will merge it to master so that it is fixed everywhere. Please, try it again.

Regards,
Michal

@corrollFang
Copy link
Author

Hi Michal,

Thanks for your response, but i think the 2 issues are caused by different reasons.
#443 is caused by if-feature on augment/grouping, but #452 is caused by "when" state.
So could you help to have a double check, thanks.

Best regards
Corroll

@michalvasko
Copy link
Member

Hi Corroll,
did you try it again because I have tried it and that is why I replied what I did, it worked. The issues are caused by different conditions but they are both checked by one function which was not properly called previously.

Regards,
Michal

@corrollFang
Copy link
Author

Hi Michal,
I checked it in libyang-0.14.72(508504c), it still doesn't work well, There should not be "aug-when" node.
--------------------YANG models-----------------------------------
-------base-test-when.yang-------
module base-test-when{

namespace "http://test-when/base";
prefix "base";
revision "2018-03-05"{
    description
        "Initial revision.";
}

container c1
{
    leaf flag
    {
        type boolean;
    }
}

}
------augment-when.yang--------
module augment-when{

namespace "http://test-when/augment-when";
prefix "augment-when";

import base-test-when {
    prefix "base";
}


revision "2018-03-05"{
    description
        "Initial revision.";
}

augment "/base:c1" 
{
    when "flag = 'true'";
    container aug-when
    {
        leaf xxx
        {
            type uint32;
        }
    }
}

}
-----------------------JSON data file----------------------------------
{"base-test-when:c1":{"flag":false}}

--------------------------------My application code-------------------------------

struct ly_ctx* lLyCtxt_tmp = ly_ctx_new(NULL, 0);
const struct lys_module* lModule_base = lys_parse_path(lLyCtxt_tmp, "base-test-when.yang", LYS_IN_YANG);
const struct lys_module* lModule_augment = lys_parse_path(lLyCtxt_tmp, "augment-when.yang", LYS_IN_YANG);

struct lyd_node* lRootNode = lyd_parse_path(lLyCtxt_tmp,"data.json",LYD_JSON, LYD_OPT_TRUSTED | LYD_OPT_CONFIG);
if(lRootNode)
{
    FILE* lDataFile_Xml = fopen("data.xml", "w");
    int lRetCode = lyd_print_file(lDataFile_Xml, lRootNode, LYD_XML, LYP_WITHSIBLINGS | LYP_WD_ALL | LYP_KEEPEMPTYCONT);
    fclose(lDataFile_Xml);
}

ly_ctx_destroy(lLyCtxt_tmp, NULL);

--------Result is saved into data.xml after run this code, the content is as below:
<c1 xmlns="http://test-when/base"> <flag>false</flag> <aug-when xmlns="http://test-when/augment-when"/> </c1>

@michalvasko
Copy link
Member

Hi,
you are right, the problem was still there but caused by another issue, you found a really specific use-case. Should work now.

Regards,
Michal

@corrollFang
Copy link
Author

Hi Michal,
I updated your change and checked it again with the same YANG&C++ code, but there is a coredump issue caused by invalid pointer, I suggest you initialize the variable as "NULL" and check if it's NULL before use it.

The coredump stack is as below.
(gdb) p when
$1 = (struct lys_when *) 0x7c
(gdb) bt
#0 0xf442571d in resolve_unres_data (unres=unres@entry=0xeca69dd0, root=root@entry=0xf3b57220,
options=options@entry=8193)
at libyang-508504cfc25b9cf84e5587247c9d740550def094/src/resolve.c:7949
#1 0xf4483e17 in lyd_defaults_add_unres (root=root@entry=0xf3b57220, options=options@entry=8193, ctx=,
ctx@entry=0xeae37c18, data_tree=data_tree@entry=0x0, act_notif=0x0, unres=unres@entry=0xeca69dd0)
at libyang-508504cfc25b9cf84e5587247c9d740550def094/src/tree_data.c:6634
#2 0xf4450b4d in lyd_parse_json (ctx=ctx@entry=0xeae37c18,
data=data@entry=0xf3356000 <error: Cannot access memory at address 0xf3356000>, options=options@entry=8193,
rpc_act=rpc_act@entry=0x0, data_tree=data_tree@entry=0x0)
at libyang-508504cfc25b9cf84e5587247c9d740550def094/src/parser_json.c:1464
#3 0xf447aed8 in lyd_parse_ (data_tree=0x0, options=8193, format=LYD_JSON,
data=0xf3356000 <error: Cannot access memory at address 0xf3356000>, rpc_act=0x0, ctx=0xeae37c18)
at libyang-508504cfc25b9cf84e5587247c9d740550def094/src/tree_data.c:437
#4 lyd_parse_data_ (ctx=ctx@entry=0xeae37c18,
data=data@entry=0xf3356000 <error: Cannot access memory at address 0xf3356000>, format=format@entry=LYD_JSON,
options=options@entry=8193, ap=, ap@entry=0xf3b57320 "\374"m\367<I\006\b\030\016\344\352")
at libyang-508504cfc25b9cf84e5587247c9d740550def094/src/tree_data.c:499
#5 0xf447b0ea in lyd_parse_fd_ (ctx=0xeae37c18, fd=, format=LYD_JSON, options=8193,
ap=0xf3b57320 "\374"m\367<I\006\b\030\016\344\352")
at libyang-508504cfc25b9cf84e5587247c9d740550def094/src/tree_data.c:536
#6 0xf447b211 in lyd_parse_path (ctx=0xeae37c18, path=0x8064a41 "data.json", format=LYD_JSON, options=8193)
at libyang-508504cfc25b9cf84e5587247c9d740550def094/src/tree_data.c:575

michalvasko added a commit that referenced this issue Mar 19, 2018
Now whens are resolved normally and verbose
messages should be printed.

Fixes #452
michalvasko added a commit that referenced this issue Mar 19, 2018
Now whens are resolved normally and verbose
messages should be printed.

Fixes #452
@michalvasko
Copy link
Member

Hi,
I hope it will finally work, I should have properly tested it...

Regards,
Michal

@corrollFang
Copy link
Author

Hi,
After updating your change, it works well now, thanks a lot.

Best regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants