dmulyalin / ttp

Template Text Parser
MIT License
349 stars 34 forks source link

Incorrect "default" behavior in nested groups #94

Open vlisjak opened 1 year ago

vlisjak commented 1 year ago

Text:

router isis LAB
 <some lines removed>
 flex-algo 128
  priority 100
  metric-type delay
  advertise-definition
  prefix-metric
 !

Template: note that I define "priority" 999 so it does not match

<group name="routing.isis.LAB" default="MISSING">
router isis LAB {{ _start_ |  _exact_ }}

 <group name="fa128" default="MISSING">
 flex-algo 128 {{ _start_ |  _exact_ }}
  priority {{ priority | equal('999') }}
  metric-type {{ metric_type | equal('delay') }}
  advertise-definition {{ advertise_definition | set('true') }}
  prefix-metric {{ prefix_metric | set('true') }}
 !{{ _end_ }}
 </group>

 <group name="LEFTOVER">
 {{ LEFTOVER | _line_ | joinmatches }}
 </group>

!{{ _end_ }}
</group>

Result is wrong, because all attributes processed after "priority" are also marked MISSING (although they are supposed to match)

            fa128:
              advertise_definition: MISSING
              metric_type: MISSING
              prefix_metric: MISSING
              priority: MISSING
vlisjak commented 1 year ago

Just figured that when I remove that "LEFTOVER" group, matching in "fa128" is correct!

<group name="routing.isis.LAB" default="MISSING">

 <group name="fa128" default="MISSING">
 flex-algo 128 {{ _start_ |  _exact_ }}
  priority {{ priority | equal('999') }}
  metric-type {{ metric_type | equal('delay') }}
  advertise-definition {{ advertise_definition | set('true') }}
  prefix-metric {{ prefix_metric | set('true') }}
 !{{ _end_ }}
 </group>

 ##<group name="LEFTOVER">
 ##{{ LEFTOVER | _line_ | joinmatches }}
 ##</group>

!{{ _end_ }}
</group>

Result now correct - only priority is marked MISSING:

        isis:
          LAB:
            fa128:
              advertise_definition: 'true'
              metric_type: delay
              prefix_metric: 'true'
              priority: MISSING
vlisjak commented 1 year ago

FYI - the aim is to detect anomalies in router configurations:

(hence that final "LEFTOVER" group is needed)

dmulyalin commented 1 year ago

Thank for raising this issue, what version of ttp and python are you using?

vlisjak commented 1 year ago

Ubuntu VM:

MAC laptop:

Same behavior on both.

vlisjak commented 1 year ago

Denis, FYI: if I add LEFTOVER also within nested group "fa128", the results appear correct!

Like this:

<group name="routing.isis.LAB" default="MISSING">
router isis LAB {{ _start_ |  _exact_ }}

 <skip>

 <group name="fa128" default="MISSING">
 flex-algo 128 {{ _start_ | _exact_ }}
  priority {{ priority | equal('999') }}
  metric-type {{ metric_type | equal('delay') }}
  advertise-definition {{ advertise_definition | set('true') }}
  prefix-metric {{ prefix_metric | set('true') }}
  {{ LEFTOVER | _line_  | joinmatches }}
 !{{ _end_ }}
 </group>

 <skip>

 <group name="LEFTOVER">
 {{ LEFTOVER | _line_ | joinmatches }}
 </group>
!{{ _end_ }}
</group>

Result: (note that other attributes after priority are now correctly recognized)

    config:
      hostname: MBRH4
      routing:
        isis:
          LAB:
            LEFTOVER:
            - LEFTOVER: address-family ipv4 unicast
            - LEFTOVER: ' metric-style wide'
            - LEFTOVER: ' metric-style wide level 2'
            - LEFTOVER: ' microloop avoidance segment-routing'
            - LEFTOVER: ' advertise passive-only'
            - LEFTOVER: ' advertise link attributes'
            - LEFTOVER: ' mpls traffic-eng level-2-only'
            - LEFTOVER: ' mpls traffic-eng router-id Loopback0'
            - LEFTOVER: ' spf-interval initial-wait 50 secondary-wait 50 maximum-wait
                3200'
            - LEFTOVER: ' segment-routing mpls sr-prefer'
            - LEFTOVER: interface Loopback0
            - <skip>
            fa128:
              LEFTOVER: priority 100
              advertise_definition: 'true'
              metric_type: delay
              prefix_metric: 'true'
              priority: MISSING
            instance_id: '680500'
            is_type: level-2-only
            log_adj_changes: 'true'
            lsp_inital_wait: '1'
            lsp_max_life: '65535'
            lsp_max_wait: '3200'
            lsp_refresh: '65000'
            lsp_second_wait: '50'
            net: 49.6805.0001.0010.0100.1064.00
            ol_bit: '120'

So it is a workaround, but my goal was to consolidate all LEFTOVERs in top-level groups. Whereas now the LEFTOVERs must be distributed across all nested groups (which is less convenient from user/operational standpoint)

dmulyalin commented 1 year ago

Added fix, could you try installing TTP from master branch and testing it:

python -m pip install git+https://github.com/dmulyalin/ttp

need to have git installed on the system