cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1353
Views
5
Helpful
3
Replies

How to have a leafref with multiple paths based on a condition

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)

1 Accepted Solution

Accepted Solutions

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";
        }
      }
}

View solution in original post

3 Replies 3

vleijon
Cisco Employee
Cisco Employee
I am afraid I don’t quite understand the question. But, you probably can, you can build most Boolean expressions on the database. What you cannot do is literally change the path (though you can enumerate your options).

The must expression is an XPath expression.

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.

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";
        }
      }
}
Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the NSO Developer community: