Skip to content

Commit

Permalink
Add VRF and family capabilities to Add/Mod/Del
Browse files Browse the repository at this point in the history
  • Loading branch information
knewell committed Mar 19, 2019
1 parent d5517c1 commit a799bfe
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 122 deletions.
130 changes: 72 additions & 58 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ def addNewFlowRoute(self, flowRouteData=None):
loader=FileSystemLoader('./template'), trim_blocks=False, lstrip_blocks=False)
template = env.get_template('set-flow-route.conf')

# print template.render(flowRouteData)
if flowRouteData['vrf'] == 'default':
flowRouteData.pop('vrf', None)
#print template.render(flowRouteData)

my_router = None
for router in self.routers:
Expand All @@ -74,13 +76,13 @@ def addNewFlowRoute(self, flowRouteData=None):
except ConfigLoadError as cle:
return False, cle.message

self.flow_config[flowRouteData['flowRouteName']] = {
'dstPrefix': flowRouteData['dstPrefix'] if 'dstPrefix' in flowRouteData else None,
'srcPrefix': flowRouteData['srcPrefix'] if 'srcPrefix' in flowRouteData else None,
'protocol': flowRouteData['protocol'] if 'protocol' in flowRouteData else None,
'dstPort': flowRouteData['dstPort'] if 'dstPort' in flowRouteData else None,
'srcPort': flowRouteData['srcPort'] if 'srcPort' in flowRouteData else None,
'action': flowRouteData['action']}
# self.flow_config[flowRouteData['flowRouteName']] = {
# 'dstPrefix': flowRouteData['dstPrefix'] if 'dstPrefix' in flowRouteData else None,
# 'srcPrefix': flowRouteData['srcPrefix'] if 'srcPrefix' in flowRouteData else None,
# 'protocol': flowRouteData['protocol'] if 'protocol' in flowRouteData else None,
# 'dstPort': flowRouteData['dstPort'] if 'dstPort' in flowRouteData else None,
# 'srcPort': flowRouteData['srcPort'] if 'srcPort' in flowRouteData else None,
# 'action': flowRouteData['action']}
return True, 'Successfully added new flow route'

def modFlowRoute(self, flowRouteData=None):
Expand All @@ -104,13 +106,13 @@ def modFlowRoute(self, flowRouteData=None):
except CommitError as ce:
return False, ce.message

self.flow_config[flowRouteData['flowRouteName']] = {
'dstPrefix': flowRouteData['dstPrefix'] if 'dstPrefix' in flowRouteData else None,
'srcPrefix': flowRouteData['srcPrefix'] if 'srcPrefix' in flowRouteData else None,
'protocol': flowRouteData['protocol'] if 'protocol' in flowRouteData else None,
'dstPort': flowRouteData['dstPort'] if 'dstPort' in flowRouteData else None,
'srcPort': flowRouteData['srcPort'] if 'srcPort' in flowRouteData else None,
'action': flowRouteData['action']}
# self.flow_config[flowRouteData['flowRouteName']] = {
# 'dstPrefix': flowRouteData['dstPrefix'] if 'dstPrefix' in flowRouteData else None,
# 'srcPrefix': flowRouteData['srcPrefix'] if 'srcPrefix' in flowRouteData else None,
# 'protocol': flowRouteData['protocol'] if 'protocol' in flowRouteData else None,
# 'dstPort': flowRouteData['dstPort'] if 'dstPort' in flowRouteData else None,
# 'srcPort': flowRouteData['srcPort'] if 'srcPort' in flowRouteData else None,
# 'action': flowRouteData['action']}

return True, 'Successfully modified flow route'

Expand All @@ -122,6 +124,11 @@ def delFlowRoute(self, flowRouteData=None):
for name, value in router.iteritems():
if 'rr' in value['type']:
my_router = [value['ip']]

if 'default' in flowRouteData['vrf']:
flowRouteData.pop('vrf', None)

flowRouteData.update({'flowRouteName': flowRouteData['name']})

with Device(host=my_router[0], user=self.dev_user, password=self.dev_pw) as dev:

Expand Down Expand Up @@ -164,20 +171,21 @@ def getActiveFlowRoutes(self):
frt.get(table=flowtable)

_flowtable = flowtable.split('.')
vrf = ''
family = ''
_vrf = ['','']
if len(_flowtable) == 2:
_vrf[0] = 'default'
vrf = 'default'
if _flowtable[0] == 'inetflow':
_vrf[1] = 'v4'
family = 'v4'
if _flowtable[0] == 'inet6flow':
_vrf[1] = 'v6'
family = 'v6'
if len(_flowtable) > 2:
_vrf[0] = _flowtable[0]
vrf = _flowtable[0]
if _flowtable[1] == 'inetflow':
_vrf[1] = 'v4'
family = 'v4'
if _flowtable[1] == 'inet6flow':
_vrf[1] = 'v6'
vrf = '{} ({})'.format(_vrf[0], _vrf[1])
family = 'v6'

for flow in frt:

Expand Down Expand Up @@ -247,7 +255,7 @@ def getActiveFlowRoutes(self):

if hex_dig not in self.flow_active:

self.flow_active[hex_dig] = {'router': name, 'vrf': vrf, 'term': flow.term, 'destination': destination,
self.flow_active[hex_dig] = {'router': name, 'vrf': vrf, 'family': family, 'term': flow.term, 'destination': destination,
'commAction': commAction, 'krtAction': krt_actions,
'age': str(_age['current']),
'hash': hex_dig, 'status': 'new'}
Expand Down Expand Up @@ -304,13 +312,14 @@ def getActiveFlowRouteFilter(self):
data[didx] = _item[1] if len(_item) > 1 else _item[0]

_vrf = table.split('_')
family = ''
if _vrf[4] == 'inet':
_vrf[4] = 'v4'
family = 'v4'
if _vrf[4] == 'inet6':
_vrf[4] = 'v6'
vrf = '{} ({})'.format(_vrf[3], _vrf[4])
family = 'v6'
vrf = '{}'.format(_vrf[3])

self.filter_active[name].append({'vrf': vrf, 'data': data, 'packet_count': filter.packet_count,
self.filter_active[name].append({'vrf': vrf, 'family': family, 'data': data, 'packet_count': filter.packet_count,
'byte_count': filter.byte_count})

return True, self.filter_active
Expand Down Expand Up @@ -392,11 +401,12 @@ def loadFlowRouteConfig(self):
else:
options = dev.rpc.get_config(options={'format': 'json'}, filter_xml='routing-options')
instances = dev.rpc.get_config(options={'format': 'json'}, filter_xml='routing-instances')

data = options['configuration']
data.update(instances['configuration'])

for vrf in self.vrfs:
family = ''
if vrf == 'default':
_vrf_data = data['routing-options']
else:
Expand All @@ -406,40 +416,44 @@ def loadFlowRouteConfig(self):

for table in ['inet', 'inet6']:
if table == 'inet6':
_data = _vrf_data['rib'][0]['flow']
vrf_table = '(v6)'
if 'flow' in _vrf_data['rib'][0]:
_data = _vrf_data['rib'][0]['flow']
family = 'v6'
else:
_data = _vrf_data['flow']
vrf_table = '(v4)'

for route in _data['route']:
_action = dict()
if 'flow' in _vrf_data:
_data = _vrf_data['flow']
family = 'v4'

if 'route' in _data:
for route in _data['route']:
_action = dict()

for key, value in route['then'].iteritems():
for key, value in route['then'].iteritems():

if value[0]:
_action[key] = {'value': value}
if value[0]:
_action[key] = {'value': value}
else:
_action[key] = {'value': None}

self.flow_config[route['name']] = {
'vrf': vrf,
'family': family,
'protocol': route['match']['protocol'] if 'protocol' in route['match'] else None,
'dstPort': route['match']['destination-port'] if 'destination-port' in route[
'match'] else None,
'srcPort': route['match']['source-port'] if 'source-port' in route['match'] else None,
'action': _action
}
if family == 'v6':
self.flow_config[route['name']].update({
'dstPrefix': route['match']['destination']['prefix'] if 'destination' in route['match'] else None,
'srcPrefix': route['match']['source']['prefix'] if 'source' in route['match'] else None
})
else:
_action[key] = {'value': None}

self.flow_config[route['name']] = {
'vrf': '{} {}'.format(vrf, vrf_table),
'protocol': route['match']['protocol'] if 'protocol' in route['match'] else None,
'dstPort': route['match']['destination-port'] if 'destination-port' in route[
'match'] else None,
'srcPort': route['match']['source-port'] if 'source-port' in route['match'] else None,
'action': _action
}
if table == 'inet6':
self.flow_config[route['name']].update({
'dstPrefix': route['match']['destination']['prefix'] if 'destination' in route['match'] else None,
'srcPrefix': route['match']['source']['prefix'] if 'source' in route['match'] else None
})
else:
self.flow_config[route['name']].update({
'dstPrefix': route['match']['destination'] if 'destination' in route['match'] else None,
'srcPrefix': route['match']['source'] if 'source' in route['match'] else None
})
self.flow_config[route['name']].update({
'dstPrefix': route['match']['destination'] if 'destination' in route['match'] else None,
'srcPrefix': route['match']['source'] if 'source' in route['match'] else None
})

return True, self.flow_config

Expand Down
28 changes: 23 additions & 5 deletions template/delete-flow-route.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
routing-options {
flow {
delete:
route {{ flowRouteName }};
}
{%- if vrf is defined %}
routing-instances {
{{vrf}} {
{%- endif %}
routing-options {
{%- if family == 'v6' %}
{%- if vrf is defined %}
rib {{vrf}}.inet6.0 {
{%- else %}
rib inet6.0 {
{% endif %}
{% endif %}
flow {
delete:
route {{ flowRouteName }};
}
{%- if family == 'v6' %}
}
{%- endif %}
}
{%- if vrf is defined %}
}
}
{%- endif %}
108 changes: 63 additions & 45 deletions template/mod-flow-route.conf
Original file line number Diff line number Diff line change
@@ -1,49 +1,67 @@
routing-options {
flow {
route {{ flowRouteName }} {
replace:
match {
{%- if dstPrefix is defined and dstPrefix !=None %}
destination {{dstPrefix}};
{%- endif %}
{%- if dstPort is defined and dstPort !=None %}
destination-port {{ dstPort }};
{%- endif %}
{%- if dscp is defined and dscp !=None %}
dscp {{dscp}};
{%- endif %}
{%- if fragment is defined and fragment !=None %}
fragment {{fragment}};
{%- endif %}
{%- if icmp_code is defined and icmp_code !=None %}
icmp-code {{icmp_code}};
{%- endif %}
{%- if icmp_type is defined and icmp_type !=None %}
icmp-type {{icmp_type}};
{%- endif %}
{%- if packet_length is defined and packet_length !=None %}
packet-length {{packet_length}};
{%- endif %}
{%- if port is defined and port !=None %}
port {{port}};
{%- endif %}
{%- if protocol is defined and protocol !=None %}
protocol {{protocol}};
{%- endif %}
{%- if srcPrefix is defined and srcPrefix !=None %}
source {{srcPrefix}};
{%- endif %}
{%- if srcPort is defined and srcPortt !=None %}
source-port {{srcPort}};
{%- endif %}
{%- if tcp_flags is defined and tcp_flags !=None %}
tcp-flags {{tcp_flags}};
{%- endif %}
{%- if vrf is defined %}
routing-instances {
{{vrf}} {
{%- endif %}
routing-options {
{%- if family == 'v6' %}
{%- if vrf is defined %}
rib {{vrf}}.inet6.0 {
{%- else %}
rib inet6.0 {
{% endif %}
{% endif %}
flow {
route {{ flowRouteName }} {
replace:
match {
{%- if dstPrefix is defined and dstPrefix !=None %}
destination {{dstPrefix}};
{%- endif %}
{%- if dstPort is defined and dstPort !=None %}
destination-port {{ dstPort }};
{%- endif %}
{%- if dscp is defined and dscp !=None %}
dscp {{dscp}};
{%- endif %}
{%- if fragment is defined and fragment !=None %}
fragment {{fragment}};
{%- endif %}
{%- if icmp_code is defined and icmp_code !=None %}
icmp-code {{icmp_code}};
{%- endif %}
{%- if icmp_type is defined and icmp_type !=None %}
icmp-type {{icmp_type}};
{%- endif %}
{%- if packet_length is defined and packet_length !=None %}
packet-length {{packet_length}};
{%- endif %}
{%- if port is defined and port !=None %}
port {{port}};
{%- endif %}
{%- if protocol is defined and protocol !=None %}
protocol {{protocol}};
{%- endif %}
{%- if srcPrefix is defined and srcPrefix !=None %}
source {{srcPrefix}};
{%- endif %}
{%- if srcPort is defined and srcPortt !=None %}
source-port {{srcPort}};
{%- endif %}
{%- if tcp_flags is defined and tcp_flags !=None %}
tcp-flags {{tcp_flags}};
{%- endif %}
}
replace:
then {
{{action}};
}
}
}
replace:
then {
{{action}};
{%- if family == 'v6' %}
}
{%- endif %}
}
{%- if vrf is defined %}
}
}
}
{%- endif %}
14 changes: 13 additions & 1 deletion template/set-flow-route.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ routing-instances {
{{vrf}} {
{%- endif %}
routing-options {
{%- if family == 'v6' %}
{%- if vrf is defined %}
rib {{vrf}}.inet6.0 {
{%- else %}
rib inet6.0 {
{% endif %}
{% endif %}
flow {
route {{ flowRouteName }} {
match {
Expand Down Expand Up @@ -44,8 +51,13 @@ routing-instances {
{%- endif %}
}
then {
{{action}};
{{action}};
}
}
}
{%- if family == 'v6' %}
}
{%- endif %}
}
{%- if vrf is defined %}
}
Expand Down
Loading

0 comments on commit a799bfe

Please sign in to comment.