Skip to content

Commit

Permalink
- update community stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
cklewar committed Apr 10, 2018
1 parent 688bef9 commit 2ffd5c5
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 53 deletions.
3 changes: 2 additions & 1 deletion data/frt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ FlowRoutesView:
term: rt-prefix-length
active: rt-entry/active-tag
age: rt-entry/age
action: rt-entry/communities/extended-community
action: rt-entry/communities/community
#action: rt-entry/communities/extended-community
tsi: tsi
49 changes: 28 additions & 21 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import yaml
import re

from jinja2 import Environment, FileSystemLoader
from jnpr.junos.utils.config import Config
from jnpr.junos import Device
from jnpr.junos.exception import ConfigLoadError
Expand All @@ -45,9 +46,11 @@ def __init__(self):

def addNewFlowRoute(self, flowRouteData=None):

#env = Environment(autoescape=False,
# loader=FileSystemLoader('./template'), trim_blocks=False, lstrip_blocks=False)
#template = env.get_template('set-flow-route.conf')
env = Environment(autoescape=False,
loader=FileSystemLoader('./template'), trim_blocks=False, lstrip_blocks=False)
template = env.get_template('set-flow-route.conf')

#print template.render(flowRouteData)

my_router = None
for router in self.routers:
Expand All @@ -70,12 +73,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 Down Expand Up @@ -156,18 +160,19 @@ def getActiveFlowRoutes(self):

if len(flow.age) <= 2:
_age['current'] = datetime.timedelta(seconds=int(flow.age))
elif len(flow.age) ==4 or len(flow.age) == 5:
elif len(flow.age) == 4 or len(flow.age) == 5:
ms = flow.age.split(':')
_age['current'] = datetime.timedelta(minutes=int(ms[0]), seconds=int(ms[1]))
elif len(flow.age) == 7 or len(flow.age) == 8:
ms = flow.age.split(':')
_age['current'] = datetime.timedelta(hours=int(ms[0]),minutes=int(ms[1]), seconds=int(ms[2]))
_age['current'] = datetime.timedelta(hours=int(ms[0]), minutes=int(ms[1]),
seconds=int(ms[2]))
else:
pattern = r'(.*)\s(.*?):(.*?):(.*)'
regex = re.compile(pattern)
age = re.findall(regex, flow.age)
_age['current'] = datetime.timedelta(days=int(age[0][0][:-1]), hours=int(age[0][1]),
minutes=int(age[0][2]), seconds=int(age[0][3]))
minutes=int(age[0][2]), seconds=int(age[0][3]))

pattern = r'([^\s]+)'
regex = re.compile(pattern)
Expand All @@ -182,12 +187,12 @@ def getActiveFlowRoutes(self):
if 'traffic-action' in flow.action:
commAction = flow.action.split(":")[1].lstrip().strip()
else:
commAction = None
commAction = flow.action

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

if hex_dig not in self.flow_active:

Expand Down Expand Up @@ -321,12 +326,14 @@ def loadFlowRouteConfig(self):
else:
_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 '*',
'dstPort': route['match']['destination-port'] if 'destination-port' in route['match'] else '*',
'srcPort': route['match']['source-port'] if 'source-port' in route['match'] else '*',
'action': _action}
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 '*',
'dstPort': route['match']['destination-port'] if 'destination-port' in route[
'match'] else '*',
'srcPort': route['match']['source-port'] if 'source-port' in route['match'] else '*',
'action': _action}
return True, self.flow_config

else:
Expand All @@ -337,7 +344,7 @@ def save_settings(self, dev_user=None, dev_pw=None, routers=None, age_out_interv
self.dev_user = dev_user
self.dev_pw = dev_pw
self.age_out_interval = age_out_interval
#self.routers = routers
# self.routers = routers

# with open('ui/config.yml', 'w') as fp:
# config = {'dev_user': self.dev_user, 'dev_pw': self.dev_pw, 'routers': self.routers,
Expand Down
30 changes: 15 additions & 15 deletions ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ <h2 style="text-align:right;float:right;" class="mainheader">Monitoring BGP Flow

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

<!-- Modal content-->
<div class="modal-content">
Expand All @@ -197,39 +197,39 @@ <h4 class="modal-title">Add new 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="inputFlowRouteName"
placeholder="My New Flow Route">
</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="inputSrcPrefix" 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="inputSrcPort" 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="inputDstPrefix" 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="inputDstPort" placeholder="8080">
</div>
</div>
<div class="form-group row" id="fg_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="selectProtocol">
<option>tcp</option>
<option>udp</option>
Expand All @@ -238,8 +238,8 @@ <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-4 col-form-label">Action</label>
<div class="col-sm-4">
<label for="selectAction" class="col-sm-2 col-form-label">Action</label>
<div class="col-sm-3">
<select class="form-control" id="selectAction">
<option>accept</option>
<option>discard</option>
Expand Down
13 changes: 9 additions & 4 deletions ui/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ $(document).ready(function () {
action_val.push([action]);

} else {
action_val.push([action, value]);
action_val.push([action, value['value']]);
}
});

Expand Down Expand Up @@ -299,9 +299,9 @@ $(document).ready(function () {
$('#g_community').remove();
}

var html = "<div id=\"g_community\"><label for=\"inputDstPort\" class=\"col-sm-2 col-form-label\">Community</label>" +
"<div class=\"col-sm-3\">" +
"<input type=\"text\" class=\"form-control\" id=\"inputActionCommunity\" placeholder=\"My BGP Community\">" +
var html = "<div id=\"g_community\"><label for=\"inputActionCommunity\" class=\"col-sm-2 col-form-label\">Community</label>" +
"<div class=\"col-sm-4\">" +
"<input type=\"text\" class=\"form-control\" id=\"inputActionCommunity\" placeholder=\"65000:667\">" +
"</div>" +
"</div>";

Expand Down Expand Up @@ -336,6 +336,11 @@ function flowRouteAddNewConfigEventHandler(){
}

data.action = $('#selectAction').val();

if (data.action == 'community') {
data.action = 'community ' + $('#inputActionCommunity').val();
console.log(data.action);
}
addNewFlowRouteConfig(data);
});
}
Expand Down
43 changes: 31 additions & 12 deletions utils/testdata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import random
import StringIO
import yaml
import pprint

from jinja2 import Environment, FileSystemLoader
Expand All @@ -11,25 +12,42 @@

print 'Generate static BGP Flow Spec test data on RR device'

with Device(host='10.11.111.120', user='root', password='juniper123') as dev:
with open('../ui/config.yml', 'r') as fp:
_config = fp.read()
config = yaml.safe_load(_config)
dev_user = config['dev_user']
dev_pw = config['dev_pw']
routers = config['routers']

my_router = None
for router in routers:

for name, value in router.iteritems():
if 'rr' in value['type']:
my_router = [value['ip']]

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

testdata = dict()
start = 1
stop = 101
stop = 1001
step = 1
protocol = ['tcp', 'udp']
action = ['accept', 'discard', 'sample']
action = ['accept', 'discard', 'sample', 'community']
communities = ['bgp_flow_arbor:1000:1666']

for idx in range(start, stop, step):
testdata['flowRoute' + str(idx)] = {
'dstPrefix': '10.{0}.{1}.{2}/32'.format(random.randint(1, 100), random.randint(1, 100),
random.randint(1, 100)),
'srcPrefix': '10.{0}.{1}.{2}/32'.format(random.randint(1, 100), random.randint(1, 100),
random.randint(1, 100)),
'dstPrefix': '10.{0}.{1}.{2}/32'.format(random.randint(1, 200), random.randint(1, 200),
random.randint(1, 200)),
'srcPrefix': '10.{0}.{1}.{2}/32'.format(random.randint(1, 200), random.randint(1, 200),
random.randint(1, 200)),
'protocol': protocol[random.randint(0, 1)], 'dstPort': '{0}'.format(random.randint(1, 9999)),
'srcPort': '{0}'.format(random.randint(1, 9999)), 'action': action[random.randint(0, 2)]}
'srcPort': '{0}'.format(random.randint(1, 9999)),
'action': '{0} {1}'.format(action[3],communities[0]) if 'community' in action[
random.randint(0, 3)] else action[random.randint(0, 2)]}

# pprint.pprint(testdata)
#pprint.pprint(testdata)

env = Environment(autoescape=False,
loader=FileSystemLoader('../template'), trim_blocks=False, lstrip_blocks=False)
Expand All @@ -40,16 +58,17 @@
for key, flow in testdata.iteritems():
_template.write(template.render(flowRouteName=key, **flow))

#print _template.getvalue()

try:

cu = Config(dev)
cu.lock()

cu.load(_template.getvalue(), format='text', merge=True)
cu.commit()
cu.unlock()

except ConfigLoadError as cle:
print cle.message

_template.close()
_template.close()

0 comments on commit 2ffd5c5

Please sign in to comment.