SHARE
TWEET

Untitled

a guest May 24th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. traffic_all_all=[]
  2. check_first = False
  3. all_switch_checked = False
  4. checked_switches =[]
  5. checked_switches_ports = []
  6. link_ports_all = []
  7.  
  8. tx_sum = 0
  9. dp_tables=[]
  10.  
  11. class ProjectController(app_manager.RyuApp):
  12.     OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
  13.  
  14.     def __init__(self, *args, **kwargs):
  15.  
  16.         super(ProjectController, self).__init__(*args, **kwargs)
  17.         self.mac_to_port = {}
  18.         self.topology_api_app = self
  19.         self.net = nx.DiGraph()
  20.         self.nodes = {}
  21.         self.links = {}
  22.         self.links_with_traffic = {}
  23.         self.links_short = {}
  24.         self.no_of_nodes = 0
  25.         self.no_of_links = 0
  26.         self.sw1 = 0
  27.         self.sw2 = 0
  28.         self.link_ports_all = {}
  29.         self.received_stats = []
  30.         self.time_check = 10
  31.         self.check_request = []
  32.         self.inquiry=0
  33.         self.first_iteration = True
  34.         self.datapaths = {}
  35.  
  36. ********************************************************
  37.  
  38.     def run(self):
  39.  
  40.         check_request = [[1, 12], [4, 9]]
  41.         self.check_request = check_request
  42.         for element in check_request:
  43.             sw1=element[0]
  44.             sw2=element[1]
  45.             self.checkPaths(sw1, sw2)
  46.         self._request_stats(dp_tables)
  47.  
  48.     def checkPaths(self, sw1, sw2):
  49.         self.inquiry += 1
  50.         print "Stats from host "+str(sw1) + " and "+str(sw2)
  51.         pathA = nx.all_simple_paths(self.net, sw1, sw2)
  52.         print "All available paths between switch "+str(sw1)+" and " + str(sw2)+":"
  53.         listA = list(pathA)
  54.         print listA
  55.         new_lista = []
  56.         lista_hopow = []
  57.  
  58.         print "Choosing the paths with the least number of hops"
  59.         for elem in listA:
  60.             liczb_hop = (len(elem)-1)
  61.             print "Path: " + str(elem)+ "- number of hops: " +str(liczb_hop)
  62.             if liczb_hop not in lista_hopow:
  63.                 lista_hopow.append(liczb_hop)
  64.         lista_hopow.sort()
  65.         print "Check number of hops"
  66.         print lista_hopow
  67.         nr=0
  68.         nr_intr_value=2
  69.         while nr<nr_intr_value:
  70.             for elem in listA:
  71.                 if (len(elem)-1)==lista_hopow[nr]:
  72.                     new_lista.append(elem)
  73.             nr+=1
  74.  
  75.         print "Chosen paths: "
  76.         print new_lista
  77.         kkk=0
  78.  
  79.         link_ports_single =[]
  80.  
  81.         for one_path in new_lista:
  82.             for k in self.links:
  83.                 n=0
  84.                 while n <(len(one_path)-1):
  85.                     if k['src: '] == one_path[n] and k['dst: '] == one_path[n+1]:
  86.  
  87.                         if k not in link_ports_single:
  88.                             link_ports_single.append(k)
  89.                     else:
  90.                         n+=1
  91.                         continue
  92.                     n += 1
  93.  
  94.             for ii in link_ports_single:
  95.                 s=copy.deepcopy(ii)
  96.                 s.update({'path: ': kkk})
  97.                 s.update({'inquiry: ': self.inquiry})
  98.                 link_ports_all.append(s)
  99.             kkk+=1
  100.             link_ports_single=[]
  101.  
  102.         print "Connections in all interesting paths"
  103.         for i in link_ports_all:
  104.             i.update({'tx_bytes: ': 0})
  105.             print i
  106.  
  107.         global dp_tables
  108.         while len(dp_tables)<(len(new_lista[0])-1):
  109.             for dp in self.datapaths.values():
  110.                 dp_tables.append(dp)
  111.  
  112.         return dp_tables
  113.  
  114.     def _request_stats(self, dp_tables):
  115.  
  116.         for i in dp_tables:
  117.             parser = i.ofproto_parser
  118.             # req = parser.OFPFlowStatsRequest(i)
  119.             # i.send_msg(req)
  120.             req = parser.OFPPortStatsRequest(i)
  121.             i.send_msg(req)
  122.  
  123.     @set_ev_cls(event.EventSwitchEnter)
  124.     def get_topology_data(self, ev):
  125.         switch_list = get_switch(self.topology_api_app, None)
  126.         switches = [switch.dp.id for switch in switch_list]
  127.         self.net.add_nodes_from(switches)
  128.         traffic = 0
  129.         links_list = get_link(self.topology_api_app, None)
  130.         links_my = [({'src: ': link.src.dpid, 'dst: ': link.dst.dpid, 'src_port: ': link.src.port_no}) for link in links_list]
  131.         links = [(link.src.dpid, link.dst.dpid, {'port': link.src.port_no}) for link in links_list]
  132.         links_with_traffic = [({'src: ': link.src.dpid, 'dst: ': link.dst.dpid, 'src_port: ': link.src.port_no, 'traffic:': traffic}) for link in links_list]
  133.         links_short = [(link.src.dpid,link.src.port_no) for link in links_list]
  134.         self.links = links_my
  135.         self.links_with_traffic = links_with_traffic
  136.         self.links_short=links_short
  137.  
  138.         self.net.add_edges_from(links)
  139.         links = [(link.dst.dpid, link.src.dpid, {'port': link.dst.port_no}) for link in links_list]
  140.  
  141.         self.net.add_edges_from(links)
  142.  
  143.     # @set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)
  144.     # def _flow_stats_reply_handler(self, ev):
  145.     #     body = ev.msg.body
  146.     #     sw_id=ev.msg.datapath.id
  147.     #     src = ev.msg.datapath
  148.  
  149.     @set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER)
  150.     def _port_stats_reply_handler(self, ev):
  151.  
  152.         body = ev.msg.body
  153.         sw_id=ev.msg.datapath.id
  154.         Stats().checkStats2(body, sw_id, self.check_request)
  155.  
  156. class Stats():
  157.  
  158.     def __init__(self):
  159.  
  160.         self.time_check = 10
  161.         self.received_stats = []
  162.         self.first_iteration = True
  163.         self.dp_tables = {}
  164.         self.src_dst_tx_bytes = []
  165.         global link_ports_all
  166.         self.link_ports_all = link_ports_all
  167.  
  168.     def checkStats2(self,body, sw_id, check_request):
  169.  
  170.         global logger_my_port, traffic_all_all
  171.         checked_switches_ports.append(sw_id)
  172.  
  173.         for stat in sorted(body, key=attrgetter('port_no')):
  174.                 print ('datapath         port     '
  175.                                     'rx-pkts  rx-bytes rx-error '
  176.                                     'tx-pkts  tx-bytes tx-error')
  177.                 print('---------------- -------- '
  178.                                     '-------- -------- -------- '
  179.                                     '-------- -------- --------')
  180.                 print(sw_id, stat.port_no, stat.rx_packets, stat.rx_bytes, stat.rx_errors, stat.tx_packets, stat.tx_bytes, stat.tx_errors)
  181.  
  182.                 for i in self.link_ports_all:
  183.                     if sw_id ==i['src: '] and stat.port_no ==i['src_port: ']:
  184.                          i["tx_bytes: "]=stat.tx_bytes
  185.         if len(dp_tables) == len(checked_switches_ports):
  186.             print "All switch checked"
  187.             global all_switch_checked, tx_sum
  188.             all_switch_checked = True
  189.             for i in self.link_ports_all:
  190.                 print i
  191.             for elem in check_request:
  192.                 sw1=elem[0]
  193.                 sw2=elem[1]
  194.                 self.prepareStats(self.link_ports_all,sw1, sw2)
  195.  
  196.     def prepareStats(self, link_ports_all, sw1, sw2):
  197.         global tx_sum, all_switch_checked
  198.  
  199.         if all_switch_checked:
  200.             nr_path=0
  201.             n=0
  202.             nn=0
  203.             list_tx_kbytes=[]
  204.  
  205.             while n < len(link_ports_all):
  206.                 for one_link in link_ports_all:
  207.                     if one_link['path: ']==nr_path:
  208.                         nn+=1
  209.                         tx_sum+= (one_link['tx_bytes: ']/1024)
  210.                         list_tx_kbytes.append((one_link['tx_bytes: ']/1024))
  211.                         n+=1
  212.                 s=[]
  213.                 tx_max=max(list_tx_kbytes)
  214.                 s.extend([sw1,sw2,nn,tx_max,tx_sum/nn])
  215.                 traffic_all_all.append(s)
  216.                 nr_path+=1
  217.                 nn=0
  218.                 tx_sum=0
  219.         print "Generated data"
  220.         print traffic_all_all
  221.         return traffic_all_all
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top