cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
246
Views
5
Helpful
1
Replies
Highlighted

Is there a way to dynamically use namespace prefixes in YANG with a leafref?

We have built an action which deploys services for us. The reason for this is we are in an enterprise environment and our services have a large number of devices under them. This action allows us to add a list or group to an existing list of devices on the service without having to overwrite the list, or add each item individually.

I would like to be able to list out the existing service-instance of the service they choose, however with the way that leafref works, it requires you to have the proper namespace prefix, for example:

 

leaf service {
  tailf:info "The service";
  //tailf:cli-completion-actionpoint cli-completion-action;
  mandatory true;
  type string;
}
leaf service-instance {
  tailf:info "The instance of that service";
  when '../service != ""';
  mandatory true;
  //type string;
  type leafref {
    path "/ncs:services/ntp:ntp/ntp:name";
  }
}

admin@Mothership-6(config)# services apply-to-devices service ntp service-instance ?
Description: The instance of that service
Possible completions:
    test

 

-----------------------------------------------------------------------------------------

However due to having multiple services, I'd like to be able to have it work for any service and not add every single leafref path to my YANG as well as having to add the YANGPATH to the make file. I was thinking it would look something like this, however it is not working, and I am getting the feeling that it cannot work.

 

leaf service {
  tailf:info "The service";
  //tailf:cli-completion-actionpoint cli-completion-action;
  mandatory true;
  type string;
}
leaf service-instance {
  tailf:info "The instance of that service";
  when '../service != ""';
  mandatory true;
  //type string;
  type leafref {
    path "/ncs:services/deref(../../service):deref(../../service)/deref(../../service):name";
  }

}

 

make clean all
rm -rf ../load-dir java/src//
mkdir -p ../load-dir
mkdir -p java/src/
/Users/sam/ncs-4.7//bin/ncsc `ls apply-to-devices-ann.yang > /dev/null 2>&1 && echo "-a apply-to-devices-ann.yang"` \
--yangpath ../../ntp/src/yang/ -c -o ../load-dir/apply-to-devices.fxs yang/apply-to-devices.yang
yang/apply-to-devices.yang:58: error: bad argument value "/ncs:services/deref(../../service):deref(../../service)/deref(../../service):name", should be of type path-arg
make: *** [../load-dir/apply-to-devices.fxs] Error 1

-----------------------------------------------------------------------------------------

You can see I'm trying to use the service name as the prefix, however I think it will not work in the place where the prefix is...

Is this possible?

Everyone's tags (5)
1 ACCEPTED SOLUTION

Accepted Solutions
Cisco Employee

Re: Is there a way to dynamically use namespace prefixes in YANG with a leafref?

No it's not possible unfortunately.

 

Also, you're not using the deref() function correctly. You should pass in a leaf of type leafref, but you're using it on string leaf. Deref will get the value of the leaf pointed at by the leafref.

 

In standard Xpath you might be able to do what you want using the local-name() function. But this isn't available in a leafref path. As you can see in the error, the path should be of type path-arg (which is only a subset of the Xpath syntax - check out the YANG RFC for more details).

 

I think the only way to achieve what you are after is using a choice node with a seperate leaf (of type leafref) for each service (and you will need add each module to the YANGPATH in the Makefile as you said).

1 REPLY 1
Cisco Employee

Re: Is there a way to dynamically use namespace prefixes in YANG with a leafref?

No it's not possible unfortunately.

 

Also, you're not using the deref() function correctly. You should pass in a leaf of type leafref, but you're using it on string leaf. Deref will get the value of the leaf pointed at by the leafref.

 

In standard Xpath you might be able to do what you want using the local-name() function. But this isn't available in a leafref path. As you can see in the error, the path should be of type path-arg (which is only a subset of the Xpath syntax - check out the YANG RFC for more details).

 

I think the only way to achieve what you are after is using a choice node with a seperate leaf (of type leafref) for each service (and you will need add each module to the YANGPATH in the Makefile as you said).