Skip to content

Commit

Permalink
- update ui mod flow
Browse files Browse the repository at this point in the history
  • Loading branch information
cklewar committed Apr 10, 2018
1 parent c5f279e commit 318d394
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 68 deletions.
2 changes: 1 addition & 1 deletion data/frt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ FlowRoutesView:
active: rt-entry/active-tag
age: rt-entry/age
action: rt-entry/communities/community
#action: rt-entry/communities/extended-community
action_141: rt-entry/communities/extended-community
tsi: tsi
89 changes: 56 additions & 33 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from jinja2 import Environment, FileSystemLoader
from jnpr.junos.utils.config import Config
from jnpr.junos import Device
from jnpr.junos.exception import ConfigLoadError
from jnpr.junos.exception import ConfigLoadError, CommitError
from data.fr import FlowRoutesTable, FlowFilterTable


Expand All @@ -50,7 +50,7 @@ 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)
# print template.render(flowRouteData)

my_router = None
for router in self.routers:
Expand Down Expand Up @@ -92,18 +92,26 @@ def modFlowRoute(self, flowRouteData=None):
my_router = [value['ip']]

with Device(host=my_router[0], user=self.dev_user, password=self.dev_pw) as dev:
cu = Config(dev)
cu.lock()
cu.load(template_path='template/mod-flow-route.conf', template_vars=flowRouteData)
cu.commit()
cu.unlock()

self.flow_config[flowRouteData['flowRouteName']] = {'dstPrefix': flowRouteData['dstPrefix'],
'srcPrefix': flowRouteData['srcPrefix'],
'protocol': flowRouteData['protocol'],
'dstPort': flowRouteData['dstPort'],
'srcPort': flowRouteData['srcPort'],
'action': flowRouteData['action']}

try:
cu = Config(dev)
cu.lock()
cu.load(template_path='template/mod-flow-route.conf', template_vars=flowRouteData)
cu.commit()
cu.unlock()

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']}

return True, 'Successfully modified flow route'

def delFlowRoute(self, flowRouteData=None):

Expand Down Expand Up @@ -183,16 +191,34 @@ def getActiveFlowRoutes(self):
else:
krt_actions = _krt_actions[4]

if isinstance(flow.action, str):
if 'traffic-action' in flow.action:
commAction = flow.action.split(":")[1].lstrip().strip()
# Junos 14.1RX different XPATH for BGP communities
version = dev.facts['version'].split('R')[0].split('.')

if int(version[0]) <= 14 and int(version[1]) <= 1:

if isinstance(flow.action_141, str):
if 'traffic-action' in flow.action_141:
commAction = flow.action_141.split(":")[1].lstrip().strip()
else:
commAction = flow.action_141

elif isinstance(flow.action_141, list):
commAction = flow.action_141[1].split(':')[1].lstrip().strip()
else:
commAction = flow.action
commAction = flow.action_141

elif isinstance(flow.action, list):
commAction = flow.action[1].split(':')[1].lstrip().strip()
else:
commAction = flow.action

if isinstance(flow.action, str):
if 'traffic-action' in flow.action:
commAction = flow.action.split(":")[1].lstrip().strip()
else:
commAction = flow.action

elif isinstance(flow.action, list):
commAction = flow.action[1].split(':')[1].lstrip().strip()
else:
commAction = flow.action

if hex_dig not in self.flow_active:

Expand Down Expand Up @@ -327,12 +353,12 @@ def loadFlowRouteConfig(self):
_action[key] = {'value': None}

self.flow_config[route['name']] = {
'dstPrefix': route['match']['destination'] if 'destination' in route['match'] else '*',
'srcPrefix': route['match']['source'] if 'source' in route['match'] else '*',
'protocol': route['match']['protocol'] if 'protocol' in route['match'] else '*',
'dstPrefix': route['match']['destination'] if 'destination' in route['match'] else None,
'srcPrefix': route['match']['source'] if 'source' in route['match'] else None,
'protocol': route['match']['protocol'] if 'protocol' in route['match'] else None,
'dstPort': route['match']['destination-port'] if 'destination-port' in route[
'match'] else '*',
'srcPort': route['match']['source-port'] if 'source-port' in route['match'] else '*',
'match'] else None,
'srcPort': route['match']['source-port'] if 'source-port' in route['match'] else None,
'action': _action}
return True, self.flow_config

Expand Down Expand Up @@ -380,11 +406,9 @@ def __init__(self, my_dev=None):
def GET(self, action=None):

if action == 'active':
froutes = self.my_dev.getActiveFlowRoutes()
return True, froutes
data = self.my_dev.getActiveFlowRoutes()

else:
return False, 'Action unknown'
return data

@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
Expand All @@ -394,13 +418,12 @@ def POST(self, action=None):

input_json = cherrypy.request.json
resp = self.my_dev.addNewFlowRoute(flowRouteData=input_json)

return resp

elif action == 'mod':
input_json = cherrypy.request.json
self.my_dev.modFlowRoute(flowRouteData=input_json)
return True, 'Modified flow route'
resp = self.my_dev.modFlowRoute(flowRouteData=input_json)
return resp

elif action == 'del':
input_json = cherrypy.request.json
Expand Down
1 change: 1 addition & 0 deletions template/mod-flow-route.conf
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ routing-options {
tcp-flags {{tcp_flags}};
{%- endif %}
}
replace:
then {
{{action}};
}
Expand Down
37 changes: 18 additions & 19 deletions ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ <h4 class="modal-title">Add new flow route</h4>
</div>
</div>
<div class="form-group row" id="fg_action">
<label for="selectAction" class="col-sm-2 col-form-label">Action</label>
<label for="selectAddNewFlowAction" class="col-sm-2 col-form-label">Action</label>
<div class="col-sm-3">
<select class="form-control" id="selectAction">
<select class="form-control" id="selectAddNewFlowAction">
<option>accept</option>
<option>discard</option>
<option>sample</option>
Expand All @@ -261,7 +261,7 @@ <h4 class="modal-title">Add new flow route</h4>

<!-- Modal -->
<div id="modalFlowMod" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-dialog modal-lg">

<!-- Modal content-->
<div class="modal-content">
Expand All @@ -272,38 +272,38 @@ <h4 class="modal-title">Modify Flow Route</h4>
<div class="modal-body">
<form>
<div class="form-group row">
<label for="inputFlowRouteName" class="col-sm-4 col-form-label">Flow Route Name</label>
<div class="col-sm-6">
<label for="inputFlowRouteName" class="col-sm-2 col-form-label">Flow Route Name</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="inputModFlowRouteName" readonly>
</div>
</div>
<div class="form-group row">
<label for="inputSrcPrefix" class="col-sm-4 col-form-label">Source Prefix</label>
<div class="col-sm-4">
<label for="inputSrcPrefix" class="col-sm-2 col-form-label">Source Prefix</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="inputModSrcPrefix" placeholder="1.1.1.1/32">
</div>
</div>
<div class="form-group row">
<label for="inputSrcPort" class="col-sm-4 col-form-label">Source Port</label>
<div class="col-sm-4">
<label for="inputSrcPort" class="col-sm-2 col-form-label">Source Port</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="inputModSrcPort" placeholder="8080">
</div>
</div>
<div class="form-group row">
<label for="inputDstPrefix" class="col-sm-4 col-form-label">Destination Prefix</label>
<div class="col-sm-4">
<label for="inputDstPrefix" class="col-sm-2 col-form-label">Destination Prefix</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="inputModDstPrefix" placeholder="2.2.2.2/32">
</div>
</div>
<div class="form-group row">
<label for="inputDstPort" class="col-sm-4 col-form-label">Destination Port</label>
<div class="col-sm-4">
<label for="inputDstPort" class="col-sm-2 col-form-label">Destination Port</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="inputModDstPort" placeholder="8080">
</div>
</div>
<div class="form-group row" id="fg_mod_protocol">
<label for="selectProtocol" class="col-sm-4 col-form-label">Protocol</label>
<div class="col-sm-4">
<label for="selectProtocol" class="col-sm-2 col-form-label">Protocol</label>
<div class="col-sm-3">
<select class="form-control" id="selectModProtocol">
<option>tcp</option>
<option>udp</option>
Expand All @@ -312,17 +312,16 @@ <h4 class="modal-title">Modify Flow Route</h4>
</div>
</div>
<div class="form-group row" id="fg_mod_action">
<label for="selectAction" class="col-sm-4 col-form-label">Action</label>
<div class="col-sm-4">
<select class="form-control" id="selectModAction">
<label for="selectModFlowAction" class="col-sm-2 col-form-label">Action</label>
<div class="col-sm-3">
<select class="form-control" id="selectModFlowAction">
<option>accept</option>
<option>discard</option>
<option>sample</option>
<option>community</option>
</select>
</div>
</div>

</form>
</div>
<div class="modal-footer">
Expand Down
Loading

0 comments on commit 318d394

Please sign in to comment.