Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to correlate/enforce previously matched attribute? #93

Open
vlisjak opened this issue Nov 23, 2022 · 2 comments
Open

How to correlate/enforce previously matched attribute? #93

vlisjak opened this issue Nov 23, 2022 · 2 comments

Comments

@vlisjak
Copy link

vlisjak commented Nov 23, 2022

Hi Denis,

is there a way to match an attribute, then modify that attribute, and finally use the modified attribute when matching at later lines?

In below example, I would like to save router-id (1.1.1.64), and then use only last octet (64) to make sure that RP_LOOP2BGP is called with correct argument:

router bgp 6805
 bgp router-id 1.1.1.64
 address-family ipv4 unicast
  dynamic-med interval 0
  network 1.1.1.64/32 route-policy RP_LOOP2BGP(64)
!

Something like:

<macro>
def add_last_octet(data):
  return(data, {"last_octet": '64'})
</macro>

<group name="config">
<group name="routing.bgp">

router bgp {{ asn }}
 bgp router-id {{ router_id | macro(add_last_octet) }}
 <group name="ipv4_af">
 address-family ipv4 unicast {{ _start_ }}{{ _exact_ }}
  dynamic-med interval {{ dynamic_med_interval | equal('0') | default(MISSING)}}
  network {{ network | equal(router_id) | default(MISSING) }}/32 route-policy RP_LOOP2BGP({{ RP_LOOP2BGP | equal(last_octet) | default(MISSING) }})
</group>
 !
@vlisjak
Copy link
Author

vlisjak commented Nov 26, 2022

FYI - I guess I found a (not so elegant) solution using macros:

  • during match: save the attribute X in _ttp[vars][X]
  • later when i want to check/enforce previous match: compare with previously saved _ttp[vars][X]

@dmulyalin, do you perhaps see a better way of doing this?

<macro>
def save_last_octet(data):
  tmp = data.split('.')[3]
  _ttp_['vars']['last_octet'] = tmp 
  return(data, {"last_octet": tmp})

def save_router_id(data):
  tmp = data
  _ttp_['vars']['router_id'] = tmp 
  return(data, {"router_id": tmp})

def is_router_id_32(data):
  if 'router_id' in _ttp_['vars']:
    if data == _ttp_['vars']['router_id'] + '/32':
      return(True)
  return(False)

def is_last_octet(data):
  if 'last_octet' in _ttp_['vars']:
    if data == _ttp_['vars']['last_octet']:
      return(True)
  return(False)
</macro>

<group name="config">
<group name="routing.bgp">

router bgp {{ asn }}
 bgp router-id {{ router_id | macro("save_router_id") | macro("save_last_octet")  | default(MISSING) }}

 <group name="af_ipv4">
 address-family ipv4 unicast {{ _start_ |  _exact_ }}
  dynamic-med interval {{ dynamic_med_interval | equal('0') | default(MISSING)}}
  network {{ network | macro(is_router_id_32) | default(MISSING) }} route-policy {{ network_rpl | equal("RP_LOOP2BGP") | default(MISSING) }}({{ network_rpl_arg | macro(is_last_octet) | default(MISSING)}})

 !{{ _end_ }}
 </group>

@dmulyalin
Copy link
Owner

Yeah, using macro is the most strightforward path here, one suggestion is replacing this

bgp router-id {{ router_id | macro("save_router_id") | macro("save_last_octet")  | default(MISSING) }}

with this:

bgp router-id {{ router_id | record("router_id ") | macro("save_last_octet")  | default(MISSING) }}

should give same results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants