Skip to content
Snippets Groups Projects
Commit facac1d2 authored by Tommaso Chiarusi BO's avatar Tommaso Chiarusi BO
Browse files

added rules pro(duction)

parent 279841ac
No related branches found
No related tags found
No related merge requests found
{
"flow": [
{
"id": "2",
"match": {
"in-port": "79",
"ethernet-match": {
"ethernet-destination": {
"address": "08:00:30:00:00:00",
"mask": "ff:ff:ff:00:00:00"
}
}
},
"instructions": {
"instruction": [
{
"order": "0",
"apply-actions": {
"action": [
{
"output-action": {
"output-node-connector": "openflow:550366641185744:77",
"max-length": "60"
},
"order": "0"
},{
"output-action": {
"output-node-connector": "openflow:550366641185744:78",
"max-length": "60"
},
"order": "1"
}
]
}
}
]
},
"flow-name": "SCSW_DOMtoWRSB",
"installHw": "true",
"idle-timeout": "0",
"cookie": "2",
"table_id": "1"
}
]
}
{
"flow": [
{
"id": "2",
"match": {
"in-port": "79",
"ethernet-match": {
"ethernet-destination": {
"address": "08:00:30:00:00:00",
"mask": "ff:ff:ff:00:00:00"
}
}
},
"instructions": {
"instruction": [
{
"order": "0",
"apply-actions": {
"action": [
{
"output-action": {
"output-node-connector": "openflow:550366641185744:77",
"max-length": "60"
},
"order": "0"
}
]
}
}
]
},
"flow-name": "SCSW_DOMtoWRSB",
"installHw": "true",
"idle-timeout": "0",
"cookie": "2",
"table_id": "1"
}
]
}
{
"flow": [
{
"id": "2",
"match": {
"in-port": "79",
"ethernet-match": {
"ethernet-destination": {
"address": "08:00:30:00:00:00",
"mask": "ff:ff:ff:00:00:00"
}
}
},
"instructions": {
"instruction": [
{
"order": "0",
"apply-actions": {
"action": [
{
"output-action": {
"output-node-connector": "openflow:550366641185744:78",
"max-length": "60"
},
"order": "0"
}
]
}
}
]
},
"flow-name": "SCSW_DOMtoWRSB",
"installHw": "true",
"idle-timeout": "0",
"cookie": "2",
"table_id": "1"
}
]
}
{
"flow": [
{
"id": "1",
"match": {
"ethernet-match": {
"ethernet-source": {
"address": "08:00:30:00:00:00",
"mask": "ff:ff:ff:00:00:00"
}
}
},
"instructions": {
"instruction": [
{
"order": "0",
"apply-actions": {
"action": [
{
"output-action": {
"output-node-connector": "openflow:550366641185744:79",
"max-length": "60"
},
"order": "0"
}
]
}
}
]
},
"flow-name": "SCSW_DOMtoSCSF",
"installHw": "true",
"idle-timeout": "0",
"cookie":"1",
"table_id": "1"
}
]
}
{
"flow": [
{
"id": "1",
"match": {
"ethernet-match": {
"ethernet-destination": {
"address": "FF:FF:FF:FF:FF:FF"
}
}
},
"instructions": {
"instruction": [
{
"order": "0",
"apply-actions": {
"action": [
{
"output-action": {
"output-node-connector": "openflow:303570285128704:85",
"max-length": "60"
},
"order": "0"
}
]
}
}
]
},
"flow-name": "SCSF_BCASTtoDHCP_CU",
"installHw": "true",
"idle-timeout": "0",
"cookie": "1",
"table_id": "1"
}
]
}
{
"flow": [
{
"id": "2",
"match": {
"ethernet-match": {
"ethernet-source": {
"address": "08:00:30:00:00:00",
"mask": "ff:ff:ff:00:00:00"
},
"ethernet-destination": {
"address": "0c:c4:7a:02:80:8d"
}
}
},
"instructions": {
"instruction": [
{
"order": "0",
"apply-actions": {
"action": [
{
"output-action": {
"output-node-connector": "openflow:303570285128704:85",
"max-length": "60"
},
"order": "0"
}
]
}
}
]
},
"flow-name": "SCSF_DOMtoCU",
"installHw": "true",
"idle-timeout": "0",
"cookie": "2",
"table_id": "1"
}
]
}
{
"flow": [
{
"id": "3",
"match": {
"ethernet-match": {
"ethernet-source": {
"address": "08:00:30:00:00:00",
"mask": "ff:ff:ff:00:00:00"
},
"ethernet-destination": {
"address": "00:26:18:2c:73:91"
}
}
},
"instructions": {
"instruction": [
{
"order": "0",
"apply-actions": {
"action": [
{
"output-action": {
"output-node-connector": "openflow:303570285128704:86",
"max-length": "60"
},
"order": "0"
}
]
}
}
]
},
"flow-name": "SCSF_DOMtoDQ",
"installHw": "true",
"idle-timeout": "0",
"cookie": "3",
"table_id": "1"
}
]
}
{
"flow": [
{
"id": "4",
"match": {
"ethernet-match": {
"ethernet-source": {
"address": "0c:c4:7a:02:80:8d"
},
"ethernet-destination": {
"address": "08:00:30:00:00:00",
"mask": "ff:ff:ff:00:00:00"
}
}
},
"instructions": {
"instruction": [
{
"order": "0",
"apply-actions": {
"action": [
{
"output-action": {
"output-node-connector": "openflow:303570285128704:88",
"max-length": "60"
},
"order": "0"
}
]
}
}
]
},
"flow-name": "PUT RIGHT TITLE",
"installHw": "true",
"idle-timeout": "0",
"cookie": "4",
"table_id": "1"
}
]
}
#!/usr/bin/python
import requests
import json
import sys
import os
import glob
debug=1
def AuthenticatedHTTP_GetRequest(urlPre,service,user,passw):
URI=urlPre+service
if debug: print URI
myHeaders={}
myHeaders['Content-Type']='application/json;charset=utf8'
myHeaders['Accept']= 'application/json'
try:
response=requests.get(URI,auth=(user,passw),headers=myHeaders)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
print "{}\n".format(e)
return response
except requests.exceptions.RequestException as ex:
print ex
return None
return response.json()
def AuthenticatedHTTP_PutRequest(urlPre,service,
user,passw,payLoad):
URI=urlPre+service
if debug: print URI
myHeaders={}
myHeaders['Content-Type']='application/json;charset=utf8'
myHeaders['Accept']= 'application/json'
try :
response=requests.put(URI,data=json.dumps(payLoad),auth=(user,passw),headers=myHeaders)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
print "{}\n".format(e)
return response
except requests.exceptions.RequestException as ex:
print ex
return None
return response
def AuthenticatedHTTP_DeleteRequest(urlPre,service,
user,passw):
URI=urlPre+service
if debug: print URI
#myHeaders={}
#myHeaders['Content-Type']='application/json;charset=utf8'
#myHeaders['Accept']= 'application/json'
try :
response=requests.delete(URI,auth=(user,passw))
response.raise_for_status()
except requests.exceptions.HTTPError as e:
print "{}\n".format(e)
return response
except requests.exceptions.RequestException as ex:
print ex
return None
return response
def statsRender(httpResp,flowTag):
''' render flow statitics '''
statsDic=httpResp
msg="[Cookie {cookie}] active since {secs} seconds\n Bytes {byte} - Packets {packets}"
# si potrebbe ottimizzare facendo una sola scansione
for flows in statsDic['flow-node-inventory:table']:
for flow in flows['flow']:
cookie=int(flow['cookie'])
if cookie==int(flowTag):
seconds=flow['opendaylight-flow-statistics:flow-statistics']['duration']['second']
byteTot=flow['opendaylight-flow-statistics:flow-statistics']['byte-count']
packTot=flow['opendaylight-flow-statistics:flow-statistics']['packet-count']
print msg.format(cookie=cookie,byte=byteTot,packets=packTot,secs=seconds)
break
else:
continue
print "\n-------------------\n"
if __name__ == '__main__':
urlPref="http://127.0.0.1:8181/restconf"
targetTemplConfig="/config/opendaylight-inventory:nodes/node/{nodeid}/flow-node-inventory:table/1/flow/{flowid}/"
targetTemplOperat="/operational/opendaylight-inventory:nodes/node/{nodeid}/table/0"
# si potrebbe interrogare il controller, invece di metterli statici
#id_SCBD="openflow:585045261839360"
#id_SCSF="openflow:303570285128704"
# DA RISCRIVERE
serviceExmpl='/config/network-topology:network-topology/topology/ovsdb:1/node/ovsdb:%2F%2FHOST1'
jsonFileExmpl="json-rul"
if len(sys.argv) < 2:
print "Usage: \n{} {} {} [{}]".format(sys.argv[0],"<jsonFilePrefix>","push|pull|stats","outputNodeID")
print "For instance:\n{} {} {}\n".format(sys.argv[0],serviceExmpl,jsonFileExmpl)
print "will post all the json-rul* files in our current dir to the target URL"
print "Target url is built from hardcoded [{}] and <targetAPI>".format(urlPref)
sys.exit(-1)
filePref=sys.argv[1]
mode=sys.argv[2]
if len(sys.argv) > 3:
outputNodeID=sys.argv[3]
else:
outputNodeID=""
filePref+="*json"
username='admin'
password='admin'
for jsonfile in os.listdir("."):
if jsonfile in glob.glob(filePref):
print "sourcing {}".format(jsonfile)
with open(jsonfile,'r') as jfile:
payload=json.load(jfile)
if mode=="push":
payload['flow'][0]['hard-timeout']=unicode('0')
#payload['flow']['hard-timeout']=unicode('0')
elif mode=="pull":
payload['flow'][0]['hard-timeout']=unicode('1')
#:payload['flow']['hard-timeout']=unicode('1')
elif mode=="stats":
myCookie=payload['flow'][0]['cookie']
else:
print "{} : unknown operating mode".format(mode)
sys.exit(-1)
# DA verificare con altre regole
flowID=payload['flow'][0]['id']
#flowID=payload['flow']['id']
if outputNodeID=="":
try:
outputNodeID=payload['flow'][0]['instructions']['instruction'][0]['apply-actions']['action'][0]['output-action']['output-node-connector']
#outputNodeID=payload['flow']['instructions']['instruction'][0]['apply-actions']['action'][0]['output-action']['output-node-connector']
portSeparatorIndex=outputNodeID.rindex(':')
outputNodeID=outputNodeID[0:portSeparatorIndex]
except KeyError:
print 'Fatal : flow output node is not specified from command line and cannot be deduced from json file.'
sys.exit(-1)
if mode=="stats":
targetAPI=targetTemplOperat.format(nodeid=outputNodeID)
res=AuthenticatedHTTP_GetRequest(urlPref,targetAPI,username,password)
statsRender(res,myCookie)
else:
targetAPI=targetTemplConfig.format(nodeid=outputNodeID,flowid=flowID)
#print targetAPI
if mode == "push":
res=AuthenticatedHTTP_PutRequest(urlPref,targetAPI,username,password,payload)
elif mode == "pull":
res=AuthenticatedHTTP_DeleteRequest(urlPref,targetAPI,username,password)
else:
print "{} mode unknown, don't know what to do".format(mode)
sys.exit(-2)
# da ottimizzare !!
if mode != "stats":
if res.status_code == 201 or res.status_code == 200:
httpRes="HTTP is OK"
else:
httpRes="HTTP returned with an error code :("
print httpRes
#outMsg="========\n{}{}\n{}\n{}\n=========".format(urlPref,targetAPI,payload,httpRes)
outMsg="========\n{}{}\n{}\n=========".format(urlPref,targetAPI,payload)
jfile.close()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment