def add_flow(flow_rule): """ Adds a flow rule to a particular ONOS switch Arguments: flow_rule {Dictionary} -- A flow rule formatted to be read by an ONOS controller Returns: [Dictionary] -- Returns a status dict depending on the outcome of the operation """ with requests.Session() as session: mac_addr = flow_rule['deviceId'] uri = add_flow_rule_endpoint uri = uri.replace('deviceId', mac_addr) req = requests.post(uri, json=flow_rule, headers=headers, auth=(onos_login_username, onos_login_password)) session.close() # check return code data = {} if req.status_code == 201: data = { 'status_success': 'Flow successfully added', 'flow_url': req.headers['location'] } else: data = create_error_status(req, 'Error adding flow') return data
def del_flow(flow_name, switch_mac_addr): """ Delete an existing flow rule from a switch Arguments: flow_name {String} -- The name of the flow to delete switch_mac_addr {String} -- The name of the switch to delete the flow from Returns: [Dictionary] -- Returns a status message depending on the outcome of the operation """ flow_name = {"name": flow_name} with requests.Session() as session: req = requests.delete(pusher, json=flow_name, headers=headers) session.close() # Need to check status code + the status return message as a successful delete and a failed delete # Returns the same html status code.... data = {} #print('-------------------') #print(req.json()) #print('-------------------') if req.status_code == 200 and req.json( )['status'] == 'Entry ' + flow_name['name'] + ' deleted': data = {'status_success': 'Flow successfully deleted'} else: data = create_error_status(req, 'Failed to delete flow') return data
def del_flow(device_id, flow_id): """ Deletes a flow rule from a specific switch Arguments: device_id {String} -- The id of the switch to delete the flow from flow_id {[type]} -- The id of the flow to delete Returns: [Dictionary] -- Returns a status dict depending on the outcome of the operation """ with requests.Session() as session: # add details of flow to delete to endpoint uri uri = delete_flow_endpoint uri = uri.replace('deviceId', device_id) uri = uri.replace('flowId', flow_id) #print(uri) req = requests.delete(uri, headers=del_headers, auth=(onos_login_username, onos_login_password)) session.close() # check status code return { 'status_success': 'Flow rule deleted' } if req.status_code == 204 else create_error_status( req, 'Failed to delete flow rule')
def get_host_information(): """ Queries the Floodlight controller for information about hosts on the network Returns: [Dictionary] -- Returns host information """ with requests.Session() as session: req = requests.get(get_host_information_endpoint, headers=headers) session.close() return req.json() if req.status_code == 200 else create_error_status( req, 'Failed to get switch link information')
def get_switch_information(): """ Queries the Floodlight controller for information about all the switches connected to the controller Returns: [Dictionary] -- A dictionary with switch information """ with requests.Session() as session: req = requests.get(get_info_about_all_switches, headers=headers) session.close() return req.json() if req.status_code == 200 else create_error_status( req, 'Failed to get switch information')
def get_network_statistics(): """ Queries the ODL controller for topology information which is the main dict used in get_network_information() Returns: [Dictionary] -- Information about switches, hosts and links on the network """ with requests.Session() as session: req = requests.get(get_network_statistics_endpoint, headers=headers, auth=(odl_login_username, odl_login_password)) session.close() # check return code return json.loads(req.text) if req.status_code == 200 else create_error_status(req, 'Failed to get topology information')
def remove_all_flows_and_groups(): """ Deletes all flows and groups from the Floodlight network Returns: [Dictionary] -- Returns a status dictionary depending on the outcome """ with requests.Session() as session: req = requests.get(clear_switches, headers=headers) session.close() return { 'status_success': 'Deleted All flows/groups' } if req.status_code == 200 else create_error_status( req, 'Failed to get flow rule from switch')
def get_network_statistics(): """ Gets network topology information about hosts, switches, links and other info Returns: [Dictionary] -- Returns the topology information or a status dict """ with requests.Session() as session: req = requests.get(get_network_statistics_endpoint, auth=(onos_login_username, onos_login_password)) session.close() # Check the status code return json.loads( req.text) if req.status_code == 200 else create_error_status( req, 'Failed to get network statistics')
def get_flows(): """ Gets all the flows on all the switches connected to the ONOS controller Returns: [Dictionary] -- Returns a list of flows or a status dictionary depending on the success of the API call """ with requests.Session() as session: req = requests.get(get_all_flows_endpoint, auth=(onos_login_username, onos_login_password)) session.close() # Check the status code return json.loads( req.text)['flows'] if req.status_code == 200 else create_error_status( req, 'Failed to get flows')
def get_network_statistics(switch, port): """ Queries the Floodlight controller for information about hosts on the network Returns: [Dictionary] -- Returns host information """ endpoint = get_network_statistics_endpoint endpoint = endpoint.replace('<switch>', switch).replace('<port>', port) #print(endpoint) with requests.Session() as session: req = requests.get(endpoint, headers=headers) session.close() return req.json() if req.status_code == 200 else create_error_status( req, 'Failed to get switch link information')
def get_switch_flow_rules(switch_mac_addr): """ Gets the flows for a particular switch, or for all switches Arguments: switch_mac_addr {String} -- Give the switch name to get flows for a particular switch, or give 'all' and get the flows across all switches Returns: [Dictionary] -- Returns a dictionary with the list of flows, or a status message depending on the outcome """ endpoint = controller_ip + 'staticentrypusher/list/' + switch_mac_addr + '/json' with requests.Session() as session: req = requests.get(endpoint, headers=headers) session.close() return req.json() if req.status_code == 200 else create_error_status( req, 'Failed to get flow rule from switch')
def add_flow(flow_rule): """ Add a flow to a particular switch Arguments: flow_rule {Dictionary} -- A dictionary that represents a flow rule Returns: [Dictionary] -- Returns a status message depending on the outcome of the operation """ with requests.Session() as session: req = requests.post(pusher, json=flow_rule, headers=headers) session.close() # check to see if the request was successful return { 'status_success': 'Flow successfully added' } if req.status_code == 200 else create_error_status( req, 'Failed to add flow')
def del_flow(switch_name, flow_id): """ Deletes a single flow rule Arguments: switch_name {String} -- The name of the switch, for ODL formatted as 'openflow:x' flow_id {String} -- The id of the flow rule to delete Returns: [Dictionary] -- Returns a status_success or status_error dictionary """ with requests.Session() as session: # add details of flow to delete to endpoint uri uri = del_flow_endpoint uri = uri.replace('flow_id', flow_id, 2).replace('switch_name', switch_name).replace('table_number', '1') #print(uri) req = requests.delete(uri, headers=del_headers, auth=(odl_login_username, odl_login_password)) session.close() # check status code return {'status_success':'Flow rule deleted'} if req.status_code == 200 else create_error_status(req, 'Failed to delete flow rule')
def get_flows(switch_name): """ Returns all the flows on a particular switch Arguments: switch_name {String} -- Name of the switch, for ODL switch names are formatted as 'openflow:x' Returns: [Dictionary] -- Returns a dictionary of flow rules if successful, else returns a satus_error dictionary """ with requests.Session() as session: uri = get_flows_endpoint uri = uri.replace('switch_name', switch_name) #pp.pprint(flow_rule[0]) req = requests.get(uri, headers=headers, auth=(odl_login_username, odl_login_password)) session.close() # check return code return json.loads(req.text)['node'][0]['flow-node-inventory:table'] if req.status_code == 200 else create_error_status(req, 'Failed to get flow')
def add_flow(flow_rule, switch_name): """Add a flow rule to a particular switch Arguments: flow_rule {String} -- An XML file formatted as a string, can be generated from the function create_flow_rule() switch_name {String} -- Name of the switch, for ODL switch names are formatted as 'openflow:x' Returns: [Dictionary] -- Returns a status_success or status_error dictionary depending on the outcome of the operation """ with requests.Session() as session: #bs = BeautifulSoup(flow_rule, features='html.parser') #flow_table_id = str(bs.findAll('id')[0]).replace('<id>', '').replace('</id>', '') uri = add_flow_rule_endpoint uri = uri.replace('switch_name', switch_name) uri = uri.replace('table_id', '1') #print(uri) #pp.pprint(flow_rule[0]) req = requests.post(uri, data=flow_rule, headers=headers, auth=(odl_login_username, odl_login_password)) session.close() # check return code return {'status_success': 'Flow successfully added', 'flow_url': req.headers['location']} if req.status_code == 204 else create_error_status(req, 'Error adding flow')