cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
209
Views
5
Helpful
4
Replies
Highlighted
Cisco Employee

Enum to string in XML template

Hello,

Is there anything extra that should be done to convert enum to string for eval at templates?

 

I have this simple model and XML template:

 

<config-template xmlns="http://tail-f.com/ns/config/1.0">
  <devices xmlns="http://tail-f.com/ns/ncs">
    <device>
      <!--
          Select the devices from some data structure in the service
          model. In this skeleton the devices are specified in a leaf-list.
          Select all devices in that leaf-list:
      -->
      <name>{/device}</name>
      <config>
        <interface xmlns="http://tail-f.com/ned/cisco-nx">
          <?if {interface_type = 'Ethernet'} ?>
            <Ethernet>
              <name>{interface_id}</name>
              <switchport>
                <access>
                  <vlan>200</vlan>
                </access>
              </switchport>
              <description>ncepnspkafd0001-1V.15.25.OB1</description>
              <speed>10000</speed>
              <duplex>full</duplex>
            </Ethernet>
          <?end?>
          <?if {interface_type = 'Loopback'} ?>
            <loopback>
              <name>{interface_id}</name>
              <description>Routing loopback interface</description>
              <ip>
                <address>
                  <ipaddr>10.2.0.3/32</ipaddr>
                  <tag>12345</tag>
                </address>
              </ip>
            </loopback>
          <?end?>
        </interface>
      </config>
    </device>
  </devices>
</config-template>

 

module xpath-yang-tests {

    namespace "http://example.com/xpath-yang-tests";
    prefix xpath-yang-tests;

    import ietf-inet-types {
        prefix inet;
    }
    import tailf-common {
        prefix tailf;
    }
    import tailf-ncs {
        prefix ncs;
    }

    description
      "Bla bla...";

    revision 2016-01-01 {
        description
          "Initial revision.";
    }

    container action {
        tailf:action double {
            tailf:actionpoint xpath-yang-tests-action;
            input {
                leaf number {
                    type uint8;
                }
            }
            output {
                leaf result {
                    type uint16;
                }
            }
        }
    }
    list xpath-yang-tests {
        description
          "This is an RFS skeleton service";

        key name;
        leaf name {
            tailf:info "Unique service id";
            tailf:cli-allow-range;
            type string;
        }

        uses ncs:service-data;
        ncs:servicepoint xpath-yang-tests-servicepoint;

        // may replace this with other ways of refering to the devices.
        leaf-list device {
            type leafref {
                path "/ncs:devices/ncs:device/ncs:name";
            }
        }

        // replace with your own stuff here
        leaf interface_id {
            type string;
        }

        leaf interface_type {
            tailf:info "nve, Ethernet, TenGigE, etc";
            description
              "nve, Ethernet, TenGigE, etc";
            type enumeration {
                enum nve;
                enum Ethernet;
                enum Loopback;
                
            }
        }
        /*
        leaf test {
          type leafref {
            path "/xpath-yang-tests:xpath-yang-tests/";
          }
        }
        */
    }
}

But I can't get the <?if ?> to work, they always evaluate to false even when the enum has the same value

Thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Try using /interface_type and /interface_id, I wonder if your context node gets moved by the implicit /device iteration, since it is a leaf-list.

View solution in original post

4 REPLIES 4
Highlighted
Cisco Employee

I don't think you need to do anything extra. But, what do you get in xpath.trace or when doing commit dry-run | debug xpath?

Highlighted

Hi Viktor, this is the output:

 

sfloresk@ncs(config)# show configuration 
xpath-yang-tests test
 device         [ eng04-cspine-01 ]
 interface_id   100
 interface_type Loopback
!
sfloresk@ncs(config)# commit dry-run | debug xpath 
 2021-02-19T15:14:22.405 xpath: evaluating: /xpath-yang-tests[name='test']/device: /ncs:devices/ncs:device/ncs:name
 2021-02-19T15:14:22.405 xpath: exists(/devices/device[name='eng04-cspine-01']) = true
 2021-02-19T15:14:22.406 xpath: result: /xpath-yang-tests[name='test']/device: true (0.000 s)
 2021-02-19T15:14:22.428 xpath: template xpath-yang-tests-template: evaluating: /device
 2021-02-19T15:14:22.428 xpath: template xpath-yang-tests-template: match: /xpath-yang-tests[name='test']/device[.='eng04-cspine-01']: eng04-cspine-01
 2021-02-19T15:14:22.428 xpath: template xpath-yang-tests-template: result: one node (0.000 s)
 2021-02-19T15:14:22.429 xpath: template xpath-yang-tests-template: evaluating: string(boolean(interface_type = 'Ethernet'))
 2021-02-19T15:14:22.429 xpath: template xpath-yang-tests-template: result: false (0.000 s)
 2021-02-19T15:14:22.429 xpath: template xpath-yang-tests-template: evaluating: string(boolean(interface_type = 'Loopback'))
 2021-02-19T15:14:22.429 xpath: template xpath-yang-tests-template: result: false (0.000 s)
 2021-02-19T15:14:22.437 xpath: evaluating: /xpath-yang-tests[name='test']/device: /ncs:devices/ncs:device/ncs:name
 2021-02-19T15:14:22.437 xpath: exists(/devices/device[name='eng04-cspine-01']) = true
 2021-02-19T15:14:22.437 xpath: result: /xpath-yang-tests[name='test']/device: true (0.000 s)
cli {
    local-node {
        data +xpath-yang-tests test {
             +    device [ eng04-cspine-01 ];
             +    interface_id 100;
             +    interface_type Loopback;
             +}
    }
}
sfloresk@ncs(config)# 
Highlighted

Try using /interface_type and /interface_id, I wonder if your context node gets moved by the implicit /device iteration, since it is a leaf-list.

View solution in original post

Highlighted

That worked, thanks!