05-15-2021 04:28 PM
Hello,
Is it possible to have a leaf with leafref as a type that point to multiple path based on a condition.
For example :
when "/ncs:devices/ncs:device[ncs:name=current()/../device]/ncs:config/ios:interface/ios:GigabitEthernet/ios:name and ../interface = 'GigabitEthernet' "
path "To Cisco IOS device GigabitEthernet IDs"
when "/ncs:devices/ncs:device[ncs:name=current()/../device]/ncs:config/cisco-ios-xr:interface/cisco-ios-xr:GigabitEthernet/cisco-ios-xr:id and ../interface = 'GigabitEthernet' "
path "To Cisco IOSXR device GigabitEthernet IDs"
when "/ncs:devices/ncs:device[ncs:name=current()/../device]/ncs:config/vrp:interface/vrp:GigabitEthernet/vrp:name and ../interface = 'GigabitEthernet' "
path "To Huawei VRP device GigabitEthernet IDs"
I want to do this because I already have a service with a data model that have a leaf for interface ID with type string and I want to transform it to a leafref while keeping the same leaf name.
(I know if I will make a choice statement I will have many leafs with differents names)
Solved! Go to Solution.
05-17-2021 09:38 AM
There are different things you can do:
1) Since YANG 1.1, you can use "union" of leafref. However, in this case you would not have "tab" options in the CLI nor validation. NSO would treat the fields as string.
2) Build your logic using "choice" (like a case for YANG). Stealing the following example from a colleague (see that the leaf names have to be different in each option):
grouping interface-leafrefs-grouping { choice interface-type { leaf ios-GigabitEthernet{ when "derived-from(/ncs:devices/ncs:device" + "[ncs:name=current()/../device]/ncs:device-type" + "/ncs:cli/ncs:ned-id, 'ios-ned-id:cisco-ios-cli')"; tailf:info "Cisco IOS Gigabit Ethernet interface"; type leafref { path "deref(../device)/../ncs:config" + "/ios:interface/ios:GigabitEthernet/ios:name"; } } leaf ios-xr-GigabitEthernet { when "derived-from(/ncs:devices/ncs:device" + "[ncs:name=current()/../device]/ncs:device-type" + "/ncs:cli/ncs:ned-id, 'iosxr-ned-id:cisco-iosxr-cli')"; tailf:info "Cisco IOS-XR Gigabit Ethernet interface"; type leafref { path "deref(../device)/../ncs:config" + "/cisco-ios-xr:interface" + "/cisco-ios-xr:GigabitEthernet/cisco-ios-xr:id"; } } leaf ios-xr-TenGigE { when "derived-from(/ncs:devices/ncs:device" + "[ncs:name=current()/../device]/ncs:device-type" + "/ncs:cli/ncs:ned-id, 'iosxr-ned-id:cisco-iosxr-cli')"; tailf:info "Cisco IOS-XR 10 Gigabit Ethernet interface"; type leafref { path "deref(../device)/../ncs:config" + "/cisco-ios-xr:interface" + "/cisco-ios-xr:TenGigE/cisco-ios-xr:id"; } } leaf junos-interface { when "derived-from(/ncs:devices/ncs:device" + "[ncs:name=current()/../device]/ncs:device-type" + "/ncs:netconf/ncs:ned-id, 'junos-ned-id:juniper-junos-nc')"; tailf:info "Junos interface"; type leafref { path "deref(../device)/../ncs:config/junos:configuration" + "/junos:interfaces/junos:interface/junos:name"; } } leaf alu-interface { when "derived-from(/ncs:devices/ncs:device" + "[ncs:name=current()/../device]/ncs:device-type" + "/ncs:cli/ncs:ned-id, 'alu-ned-id:alu-sr-cli')"; tailf:info "ALU interface"; type leafref { path "deref(../device)/../ncs:config/alu:port/alu:port-id"; } }
}
05-16-2021 01:45 PM
05-17-2021 12:30 AM
Hello vleijon,
Thank you for reply. What I meant is: Is it possible to have a leaf with a leafref type that can point to multiple path based on a condition. For exemple like this:
leaf customer-port-id {
type leafref {
if device-type "cisco-ios" {
path "/ncs:devices/ncs:device[ncs:name=current()/../device]/ncs:config/ios:interface/ios:GigabitEthernet/ios:name";
}
if device-type "cisco-ios-xr" {
path "/ncs:devices/ncs:device[ncs:name=current()/../device]/ncs:config/cisco-ios-xr:interface/cisco-ios-xr:GigabitEthernet/cisco-ios-xr:id";
}
if device-type "huawei-vrp" {
path "/ncs:devices/ncs:device[ncs:name=current()/../device]/ncs:config/vrp:interface/vrp:GigabitEthernet/vrp:name";
}
}
}
Please note that using a choice statement is not an option for me.
05-17-2021 09:38 AM
There are different things you can do:
1) Since YANG 1.1, you can use "union" of leafref. However, in this case you would not have "tab" options in the CLI nor validation. NSO would treat the fields as string.
2) Build your logic using "choice" (like a case for YANG). Stealing the following example from a colleague (see that the leaf names have to be different in each option):
grouping interface-leafrefs-grouping { choice interface-type { leaf ios-GigabitEthernet{ when "derived-from(/ncs:devices/ncs:device" + "[ncs:name=current()/../device]/ncs:device-type" + "/ncs:cli/ncs:ned-id, 'ios-ned-id:cisco-ios-cli')"; tailf:info "Cisco IOS Gigabit Ethernet interface"; type leafref { path "deref(../device)/../ncs:config" + "/ios:interface/ios:GigabitEthernet/ios:name"; } } leaf ios-xr-GigabitEthernet { when "derived-from(/ncs:devices/ncs:device" + "[ncs:name=current()/../device]/ncs:device-type" + "/ncs:cli/ncs:ned-id, 'iosxr-ned-id:cisco-iosxr-cli')"; tailf:info "Cisco IOS-XR Gigabit Ethernet interface"; type leafref { path "deref(../device)/../ncs:config" + "/cisco-ios-xr:interface" + "/cisco-ios-xr:GigabitEthernet/cisco-ios-xr:id"; } } leaf ios-xr-TenGigE { when "derived-from(/ncs:devices/ncs:device" + "[ncs:name=current()/../device]/ncs:device-type" + "/ncs:cli/ncs:ned-id, 'iosxr-ned-id:cisco-iosxr-cli')"; tailf:info "Cisco IOS-XR 10 Gigabit Ethernet interface"; type leafref { path "deref(../device)/../ncs:config" + "/cisco-ios-xr:interface" + "/cisco-ios-xr:TenGigE/cisco-ios-xr:id"; } } leaf junos-interface { when "derived-from(/ncs:devices/ncs:device" + "[ncs:name=current()/../device]/ncs:device-type" + "/ncs:netconf/ncs:ned-id, 'junos-ned-id:juniper-junos-nc')"; tailf:info "Junos interface"; type leafref { path "deref(../device)/../ncs:config/junos:configuration" + "/junos:interfaces/junos:interface/junos:name"; } } leaf alu-interface { when "derived-from(/ncs:devices/ncs:device" + "[ncs:name=current()/../device]/ncs:device-type" + "/ncs:cli/ncs:ned-id, 'alu-ned-id:alu-sr-cli')"; tailf:info "ALU interface"; type leafref { path "deref(../device)/../ncs:config/alu:port/alu:port-id"; } }
}
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide