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

conditional "if" statement is not working as expected

amardkum
Cisco Employee
Cisco Employee

I have a python-and-template based service created using NSO5.3.

I am trying to make use of conditional statement - "if", in my config.xml.

<config-template xmlns="http://tail-f.com/ns/config/1.0">
  <devices xmlns="http://tail-f.com/ns/ncs">
    <device tags="nocreate">
      <name>{$device}</name>
      <config tags="merge">
        <feature xmlns="http://tail-f.com/ned/cisco-nx">
          <interface-vlan/>
        </feature>
        <vlan xmlns="http://tail-f.com/ned/cisco-nx">
          <vlan-list>
            <id>{/vlan-interfaces/api/settings/id}</id>
            <name>{$device}_api</name>  <!-- this will become "NSO1-ACCESS01_api" -->
          </vlan-list>
          <vlan-list>
            <id>{/vlan-interfaces/mgmt/settings/id}</id>
            <name>{$device-index}_MGMT</name>  <!-- this will become "[1|2]_MGMT" based on device-index value-->
          </vlan-list>
        </vlan>
        <interface xmlns="http://tail-f.com/ned/cisco-nx">
          <Ethernet>
            <!-- <?if {contains($device, 'ACCESS01')='true'}?> -->
            <!-- <? if contains($device, 'ACCESS01') ?> -->
            <!-- <? if {contains($device, 'ACCESS01')} ?> -->
<!-- <?if{$device-index = '1'}?> -->
            <?if{'1' = '1'}?>   <!-- even this condition is throwing 'Processing instruction is unexpected here' error-->
              <name>1/43</name>
            <?end?>
            <?if{$device-index = '2'}?>
              <name>1/22</name>
            <?end?>
            <channel-group>
              <id>{/port-channel-interfaces/core-access-peer/settings/id}</id>
              <mode>active</mode>
            </channel-group>
            <!-- <? if contains($device , 'ACCESS01')?>
              <description>{substring-before($device, '-ACCESS01')}-CORE01 Eth1/43</description>
            <? else ?>
              <description>{substring-before($device, '-ACCESS02')}-CORE01 Eth1/44</description>
            <? end ?> -->
          </Ethernet>
        </interface>
      </config>
    </device>
  </devices>
</config-template>
 

I tried multiple options, added as comment inside xml, but none of them worked.

 

Seeking some suggestion from the subject matter experts.

1 Accepted Solution

Accepted Solutions

rslaski
Spotlight
Spotlight
In your template <name> looks like a key element of <Ethernet> list, and I am not quite sure whether 'if' statement can be used on list key elements. Have you tried moving your condition above the <Ethernet>, so it is applied to the whole list ?

View solution in original post

3 Replies 3

rslaski
Spotlight
Spotlight
In your template <name> looks like a key element of <Ethernet> list, and I am not quite sure whether 'if' statement can be used on list key elements. Have you tried moving your condition above the <Ethernet>, so it is applied to the whole list ?

@rslaski, it works as expected when I used the conditional statement above <Ethernet>.

Thank you so much.

 

Could you please elaborate on how can we determine if the tag is a key or list key?

Error message didn't say much.

The general rule is that the XML has to be correct, no matter if the if-statement is true or not, so this also affects mandatory fields. Also, NSO doesn’t do any deep analysis of the code, so even if your cases are exhaustive, NSO doesn’t detect that.