def list_switches(self, req, **kwargs): dpid = None if "dpid" in kwargs: dpid = dpid_lib.str_to_dpid(kwargs["dpid"]) switches = get_switch(self.topology_api_app, dpid) body = json.dumps([switch.to_dict() for switch in switches]) return Response(content_type="application/json", body=body)
def list_switches(self, req, **kwargs): dpid = None if 'dpid' in kwargs: dpid = dpid_lib.str_to_dpid(kwargs['dpid']) switches = get_switch(self.topology_api_app, dpid) body = json.dumps([switch.to_dict() for switch in switches]) return Response(content_type='application/json', body=body)
def get_dp(app, dpid): """ :type dpid: datapath id :param dpid: :rtype: ryu.controller.controller.Datapatyh :returns: datapath corresponding to dpid """ switches = topo_api.get_switch(app, dpid) if not switches: return None assert len(switches) == 1 return switches[0].dp
def topology_change_handler(self, ev): self.switches = get_switch(self) self.links = get_link(self) LOG.debug('\n\n\n') LOG.debug('show switches of topology') LOG.debug(self.switches) LOG.debug('\n\n\n') LOG.debug('show links of topology') links_json = json.dumps([link.to_dict() for link in self.links]) LOG.debug(links_json) LOG.debug('\n\n\n') LOG.debug('show switch_foo') switch_foo = Switch(self.switches.pop()) LOG.debug(switch_foo.dp) LOG.debug('\n\n\n') LOG.debug('show switch_foo.getDPID') LOG.debug(type(switch_foo.dp.getDPID())) switch_foo_dpid = switch_foo.dp.getDPID() LOG.debug(switch_foo_dpid) LOG.debug('\n\n\n') LOG.debug('show dpset') LOG.debug(self.dpset) LOG.debug('\n\n\n') flow = { 'table_id': 0, 'priority': 32768, 'match': { "ipv4_src": '10.0.0.1' #FIXME in ofctl_v1_3 is ipv4_src }, 'out_port': 1, 'actions': [ { "port": 1, "type": "OUTPUT" }, ], } cmds = { 'add': ofproto_v1_3.OFPFC_ADD, 'modif': ofproto_v1_3.OFPFC_MODIFY, 'del': ofproto_v1_3.OFPFC_DELETE, } ofctl_v1_3.mod_flow_entry(self.dpset[switch_foo_dpid], flow ,cmds['add']) LOG.debug("succefully execute the mod_flow_entry\n\n\n")
def addflowsql(self, dst, dpid, in_port, out_port, flag): print 'add flow sql !!!!!!!!!!!!!!!!!!!!!!!!!!!' print dpid,in_port,out_port data_path=get_switch(self,dpid)#TODO test print type(data_path) print '!!!!!!!!!!!!!!!!!!!' print data_path datapath=data_path[0].dp#TODO test print 'datapath = ' print datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser print 'dst = '+str(dst) print 'dpid = '+str(dpid) print 'in_port = '+str(in_port) print 'out port = '+str(out_port) actions=[parser.OFPActionOutput(out_port)] match=parser.OFPMatch(in_port=in_port,eth_dst=dst) if 1==flag: self.delete_flow(datapath, match)#TODO test delete flow!! print '!!!!!!!!!!!!!!!!!!add flow!!!!!!!!!!!!!!!!!!!!in_port='+str(in_port)+' dst='+str(dst)+' out_port='+str(out_port)+' dpid='+str(dpid) self.add_flow(datapath, 1, match, actions)
def _packet_in_handler(self, ev): #TODO using two methods to do two things respectively ? #TODO using server MAC learning period T to separate the two methods ? ## end=time.time() ## if (end-start)*1000 < self.T: ## #TODO compute the route ## print (end-start)*1000 ## self.MACLearning(ev) ## else: ## print (end-start)*1000 ## #TODO learning server MAC ## self.Routing(ev) #TODO add the topology collecting periodically self.edgenum=0 start = time.time() print start self.switches = get_switch(self) self.links = get_link(self) self.topo_col_num = self.topo_col_num + 1 end = time.time() print end print 'topology collecting time:' print end-start self.topo_col_period = self.topo_col_period + end-start #n=len(self.switches) #m=len(self.links) ## self.startnum=0 ## self.dpids_to_nums={} ## self.nums_to_dpids={} print 'dpids nums:' for switch in self.switches:#TODO this may has error if self.dpids_to_nums.get(switch.dp.id)==None: self.nums_to_dpids[self.startnum] = switch.dp.id self.dpids_to_nums[switch.dp.id] = self.startnum print str(switch.dp.id)+' '+str(self.startnum) self.startnum = self.startnum + 1 print self.dpids_to_nums self.n=self.startnum print 'edges:' self.linkgraph=[] for i in xrange(self.switch_num): self.linkgraph.append([]) for j in xrange(self.switch_num): self.linkgraph[i].append(0) for link in self.links: self.edgenum=self.edgenum+1 srcnum = self.dpids_to_nums[link.src.dpid] dstnum = self.dpids_to_nums[link.dst.dpid] self.linkgraph[srcnum][dstnum]=1 if self.graph[srcnum][dstnum]==0 and self.graph[dstnum][srcnum]==0: print str(srcnum)+' '+str(dstnum) self.dpid_to_port[(link.src.dpid, link.dst.dpid)] = (link.src.port_no, link.dst.port_no) self.dpid_to_port[(link.dst.dpid, link.src.dpid)]=(link.dst.port_no, link.src.port_no) #print>>devicegraph, str(srcnum)+' '+str(dstnum) self.graph[srcnum][dstnum] = 1 self.graph[dstnum][srcnum] = 1 self.undirected[srcnum][dstnum] = 1 self.m=self.m+1 self.G={} for i in xrange(self.switch_num): self.G[i]={} for j in xrange(self.switch_num): if self.linkgraph[i][j]==1 and self.linkgraph[j][i]==1:#TODO if only one way is ok then regard it as not ok self.G[i][j]=1 print self.G flag=0 if self.n<4 or self.m<4 or self.topo_col_num<self.topo_col_num_max or self.topo_col_period<self.topo_col_period_max:#TODO test,but when the edge is error, then it will not be ok flag=1#TODO print 'topology ok' if self.servernum<2: self.MACLearning(ev) if 2==self.servernum: #and 0==flag: src=self.server1 dst=self.server2 self.short(ev, src, dst, self.G)#add the flow entries of FF:FF:FF:FF:FF:FF src=self.server2 dst=self.server1 self.short(ev, src, dst, self.G) elif 2==self.servernum or self.edgenum!=8: #and 0==flag: #it means that two servers' MAC have been added, then add the src=self.server1 dst=self.server2 self.short(ev, src, dst, self.G)#add the flow entries of FF:FF:FF:FF:FF:FF src=self.server2 dst=self.server1 self.short(ev, src, dst, self.G)