weixin_39959615
weixin_39959615
2020-12-01 19:08

lys_is_key is not working for some List node

Hi,

One problem I am facing related to the api(lys_is_key). I use this api to verify whether a node is a key node or not. For some keys I see that it doesnt work. Can you please have a look. PFA yang model for which I am seeing the issue. The List node for which I am seeing the problem is low-level-tx-endpoints which is present at Line 1145 of the attached yang. Below is the snippet of the yang:

o-ran-uplane-conf.zip


list low-level-tx-endpoints {
      key "name";

      description
        "Object model for low-level-tx-endpoints configuration - augmented static-low-level-tx-endpoints by local-address
        which cannot be added to static low-level-tx-endpoints as we cannot have modificable element in static object";

      leaf name {
        type leafref {
          path "/user-plane-configuration/static-low-level-tx-endpoints/name";
          require-instance false;
        }
        mandatory true;

        description
          "Unique name of low-level-tx-endpoint object. Reference to static object";
      }

该提问来源于开源项目:CESNET/libyang

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • weixin_39857792 weixin_39857792 4月前

    Hi, please provide the module with all the imports so that I do not have to look for them myself, thanks.

    点赞 评论 复制链接分享
  • weixin_39959615 weixin_39959615 4月前

    ruyang.zip

    Here is the full list of yang which I use.

    点赞 评论 复制链接分享
  • weixin_39857792 weixin_39857792 4月前

    Hi, thanks for the modules, I have tested it with this simple app and it worked fine.

    C
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <signal.h>
    #include <inttypes.h>
    #include <libyang>
    
    int main(int argc, char **argv) {
        struct ly_ctx *ctx = ly_ctx_new("/home/vasko/Downloads/yang_models", 0);
    
        ly_ctx_load_module(ctx, "o-ran-uplane-conf", NULL);
    
        const char *path = "/o-ran-uplane-conf:user-plane-configuration/low-level-tx-endpoints/name";
        const struct lys_node *schema = ly_ctx_get_node(ctx, NULL, path, 0);
    
        printf("\"%s\" is key: %s\n", path, lys_is_key((struct lys_node_leaf *)schema, NULL) ? "true" : "false");
    
        ly_ctx_destroy(ctx, NULL);
        return 0;
    }
    </libyang></inttypes.h></signal.h></string.h></unistd.h></stdlib.h></stdio.h>
    点赞 评论 复制链接分享
  • weixin_39959615 weixin_39959615 4月前

    Hi

    What I am doing is I am comparing the element name and trying to find the node using below code:

    
    const struct lys_module *mod = ly_ctx_get_module_by_ns(ctx, xmlelem->ns->value, NULL, 0);
    if(mod->augment && mod->augment->child)
        node = searchNode(xmlelem, mod->augment->child);
    else
        node = searchNode(xmlelem, mod->data);
    if(NULL != lys_is_key((struct lys_node_leaf *)node, NULL))
           printf("It is a Key Element");
    
    
    struct lys_node *searchNode(struct lyxml_elem *xml, struct lys_node *start)
    {
        struct lys_node *result, *tmp;
        for(result = start; result != NULL; result = result->next)
        {       
            if(strcmp(result->name, xml->name) == 0)
                return result;
    
            tmp = searchNode(xml, result->child);
            if(tmp)
                return tmp;
        }
    
        return NULL;
    }
    

    Is there any problem in this approach?

    The path which you have used is Json path. Is it same as XPATH? Normally for me XPATH for key element looks like below: /o-ran-uplane-conf:user-plane-configuration/low-level-tx-endpoints[name]/name

    点赞 评论 复制链接分享
  • weixin_39959615 weixin_39959615 4月前

    Hi ,

    I tried with your logic to find a node using ly_ctx_get_node api. But it is not able to find node which are feature specific. In ietf-hardware.yang(I shared attachment in this thread) we have a container named state inside component List which is feature specific. If I pass the path as "/ietf-hardware:hardware/component[name='asif']/state/admin-state" or "/ietf-hardware:hardware/component[name='asif']/state", it is not able to find the node. Can you tell me why it is not able to find the node? Am I not doing it correctly?

    container state { if-feature hardware-state;

    点赞 评论 复制链接分享
  • weixin_39857792 weixin_39857792 4月前

    Hi, as for your searchNode function, you are duplicating the functionality of libyang. It is rarely so simple so you are likely to end up with something incorrect so my suggestion is to avoid writing your own functions like this whenever possible.

    ly_ctx_get_node expects a data path, which the XPath in sysrepo is so you should be able to use it. However, like you correctly noticed, you need to enable any features that you want to use. You can do that in sysrepo using sysrepoctl.

    点赞 评论 复制链接分享