Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- traffic_all_all=[]
- check_first = False
- all_switch_checked = False
- checked_switches =[]
- checked_switches_ports = []
- link_ports_all = []
- tx_sum = 0
- dp_tables=[]
- class ProjectController(app_manager.RyuApp):
- OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
- def __init__(self, *args, **kwargs):
- super(ProjectController, self).__init__(*args, **kwargs)
- self.mac_to_port = {}
- self.topology_api_app = self
- self.net = nx.DiGraph()
- self.nodes = {}
- self.links = {}
- self.links_with_traffic = {}
- self.links_short = {}
- self.no_of_nodes = 0
- self.no_of_links = 0
- self.sw1 = 0
- self.sw2 = 0
- self.link_ports_all = {}
- self.received_stats = []
- self.time_check = 10
- self.check_request = []
- self.inquiry=0
- self.first_iteration = True
- self.datapaths = {}
- ********************************************************
- def run(self):
- check_request = [[1, 12], [4, 9]]
- self.check_request = check_request
- for element in check_request:
- sw1=element[0]
- sw2=element[1]
- self.checkPaths(sw1, sw2)
- self._request_stats(dp_tables)
- def checkPaths(self, sw1, sw2):
- self.inquiry += 1
- print "Stats from host "+str(sw1) + " and "+str(sw2)
- pathA = nx.all_simple_paths(self.net, sw1, sw2)
- print "All available paths between switch "+str(sw1)+" and " + str(sw2)+":"
- listA = list(pathA)
- print listA
- new_lista = []
- lista_hopow = []
- print "Choosing the paths with the least number of hops"
- for elem in listA:
- liczb_hop = (len(elem)-1)
- print "Path: " + str(elem)+ "- number of hops: " +str(liczb_hop)
- if liczb_hop not in lista_hopow:
- lista_hopow.append(liczb_hop)
- lista_hopow.sort()
- print "Check number of hops"
- print lista_hopow
- nr=0
- nr_intr_value=2
- while nr<nr_intr_value:
- for elem in listA:
- if (len(elem)-1)==lista_hopow[nr]:
- new_lista.append(elem)
- nr+=1
- print "Chosen paths: "
- print new_lista
- kkk=0
- link_ports_single =[]
- for one_path in new_lista:
- for k in self.links:
- n=0
- while n <(len(one_path)-1):
- if k['src: '] == one_path[n] and k['dst: '] == one_path[n+1]:
- if k not in link_ports_single:
- link_ports_single.append(k)
- else:
- n+=1
- continue
- n += 1
- for ii in link_ports_single:
- s=copy.deepcopy(ii)
- s.update({'path: ': kkk})
- s.update({'inquiry: ': self.inquiry})
- link_ports_all.append(s)
- kkk+=1
- link_ports_single=[]
- print "Connections in all interesting paths"
- for i in link_ports_all:
- i.update({'tx_bytes: ': 0})
- print i
- global dp_tables
- while len(dp_tables)<(len(new_lista[0])-1):
- for dp in self.datapaths.values():
- dp_tables.append(dp)
- return dp_tables
- def _request_stats(self, dp_tables):
- for i in dp_tables:
- parser = i.ofproto_parser
- # req = parser.OFPFlowStatsRequest(i)
- # i.send_msg(req)
- req = parser.OFPPortStatsRequest(i)
- i.send_msg(req)
- @set_ev_cls(event.EventSwitchEnter)
- def get_topology_data(self, ev):
- switch_list = get_switch(self.topology_api_app, None)
- switches = [switch.dp.id for switch in switch_list]
- self.net.add_nodes_from(switches)
- traffic = 0
- links_list = get_link(self.topology_api_app, None)
- links_my = [({'src: ': link.src.dpid, 'dst: ': link.dst.dpid, 'src_port: ': link.src.port_no}) for link in links_list]
- links = [(link.src.dpid, link.dst.dpid, {'port': link.src.port_no}) for link in links_list]
- links_with_traffic = [({'src: ': link.src.dpid, 'dst: ': link.dst.dpid, 'src_port: ': link.src.port_no, 'traffic:': traffic}) for link in links_list]
- links_short = [(link.src.dpid,link.src.port_no) for link in links_list]
- self.links = links_my
- self.links_with_traffic = links_with_traffic
- self.links_short=links_short
- self.net.add_edges_from(links)
- links = [(link.dst.dpid, link.src.dpid, {'port': link.dst.port_no}) for link in links_list]
- self.net.add_edges_from(links)
- # @set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)
- # def _flow_stats_reply_handler(self, ev):
- # body = ev.msg.body
- # sw_id=ev.msg.datapath.id
- # src = ev.msg.datapath
- @set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER)
- def _port_stats_reply_handler(self, ev):
- body = ev.msg.body
- sw_id=ev.msg.datapath.id
- Stats().checkStats2(body, sw_id, self.check_request)
- class Stats():
- def __init__(self):
- self.time_check = 10
- self.received_stats = []
- self.first_iteration = True
- self.dp_tables = {}
- self.src_dst_tx_bytes = []
- global link_ports_all
- self.link_ports_all = link_ports_all
- def checkStats2(self,body, sw_id, check_request):
- global logger_my_port, traffic_all_all
- checked_switches_ports.append(sw_id)
- for stat in sorted(body, key=attrgetter('port_no')):
- print ('datapath port '
- 'rx-pkts rx-bytes rx-error '
- 'tx-pkts tx-bytes tx-error')
- print('---------------- -------- '
- '-------- -------- -------- '
- '-------- -------- --------')
- print(sw_id, stat.port_no, stat.rx_packets, stat.rx_bytes, stat.rx_errors, stat.tx_packets, stat.tx_bytes, stat.tx_errors)
- for i in self.link_ports_all:
- if sw_id ==i['src: '] and stat.port_no ==i['src_port: ']:
- i["tx_bytes: "]=stat.tx_bytes
- if len(dp_tables) == len(checked_switches_ports):
- print "All switch checked"
- global all_switch_checked, tx_sum
- all_switch_checked = True
- for i in self.link_ports_all:
- print i
- for elem in check_request:
- sw1=elem[0]
- sw2=elem[1]
- self.prepareStats(self.link_ports_all,sw1, sw2)
- def prepareStats(self, link_ports_all, sw1, sw2):
- global tx_sum, all_switch_checked
- if all_switch_checked:
- nr_path=0
- n=0
- nn=0
- list_tx_kbytes=[]
- while n < len(link_ports_all):
- for one_link in link_ports_all:
- if one_link['path: ']==nr_path:
- nn+=1
- tx_sum+= (one_link['tx_bytes: ']/1024)
- list_tx_kbytes.append((one_link['tx_bytes: ']/1024))
- n+=1
- s=[]
- tx_max=max(list_tx_kbytes)
- s.extend([sw1,sw2,nn,tx_max,tx_sum/nn])
- traffic_all_all.append(s)
- nr_path+=1
- nn=0
- tx_sum=0
- print "Generated data"
- print traffic_all_all
- return traffic_all_all
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement