dmulyalin / ttp

Template Text Parser
MIT License
350 stars 34 forks source link

Not closing group match properly when matching lines appear in multiple places in config #99

Closed refriedjello closed 1 year ago

refriedjello commented 1 year ago

Parsing VRF configuration from Cisco IOS-XR routers and the issue is when parsing the entire configuration I get improper matches against certain configuration lines that appear in multiple places throughout the configuration. Certain lines appear within the expected group but also elsewhere. The issue is the last match group will "catch" those lines from outside the group, ie from the wrong places in the configuration. Ultimately it appears the end tags are not working properly.

Note the issue appears with the output key named "VRF-L3VPN-RJELL001-101" and specifically it catches address family's ipv6,l2vpn,vpnv4,vpnv6. These are not actually defined under that global VRF definition.

If you run TTP using the provided template against the "vrf.config" file which just contains the VRF output the data is clean.

But if you run TTP against the full config output "clean.config" you will note the extra address-family matches.

I have played with the TTP template in several different ways and am unable to force TTP to stop parsing at the end of the VRF definition blocks.

iosxr_vrf.ttp.txt ncs-5501-01.lab.lou03 - clean config - 2.15.2022.txt ncs-5501-01.lab.lou03 - vrf config - 2.15.2022.txt

refriedjello commented 1 year ago

I adjusted the TTP template to restructure how the groups are created and broke out the different expressions of VRF configs into completely separate groups and that resolved the issue.

iosxr_vrf_two.ttp.txt

dmulyalin commented 1 year ago

Also was able to make it work by flattening group structure a little bit: image

Tempalte:

And your ttp template here<doc>
Template for parsing VRF configuration from Cisco IOSXR devices.
</doc>

<vars>
base_defaults = {
    "ttp_template_file"  : "iosxr_vrf.ttp",
    "ttp_template_group" : "base"
}
</vars>

<group name="vrf*.{{ vrf }}*">
vrf {{ vrf | _start_ }}
 description {{ description | ORPHRASE }}
 vpn id {{ vpn_id }}
 <group name="address_family.{{ address_family }}**" containsall="address_family,address_family_sub" default="base_defaults">
 address-family {{ address_family }} {{ address_family_sub | _start_ }}
  import route-policy {{ route_policy_import }}
  <group containsall="route_target_import">
  import route-target {{ _start_ }}
   {{ route_target_import | split('\n') | joinmatches('\n') }}
  ! {{ _end_ }}
  </group>
  export route-policy {{ route_policy_export }}
  <group containsall="route_target_export">
  export route-target {{ _start_ }}
   {{ route_target_export | split('\n') | joinmatches('\n') }}
  ! {{ _end_ }}
  </group>
  maximum prefix {{ prefix_max }} {{ prefix_mid_thresold }}
 ! {{ _end_ }}
 </group>
! {{ _end_ }}
</group>