Exemplo n.º 1
0
 def _route(self, req, **kwargs):
     srcdpid=dpid_lib.str_to_dpid(kwargs['srcdpid'])
     srcport=port_no_lib.str_to_port_no(kwargs['srcport'])
     dstdpid=dpid_lib.str_to_dpid(kwargs['dstdpid'])
     dstport=port_no_lib.str_to_port_no(kwargs['dstport'])
     links = get_link(self.topology_api_app, None)
     
     topology = nx.MultiDiGraph()
     for link in links:
         print link
         topology.add_edge(link.src.dpid, link.dst.dpid, src_port=link.src.port_no, dst_port=link.dst.port_no)
     
     try:    
         shortest_path = nx.shortest_path(topology, srcdpid, dstdpid)
     except (nx.NetworkXError, nx.NetworkXNoPath):
         body = json.dumps([])
         print "Error"
         return Response(content_type='application/json', body=body)
         
     ingressPort = NodePortTuple(srcdpid, srcport)
     egressPort = NodePortTuple(dstdpid, dstport)
     route = []
     route.append(ingressPort)
     
     for i in range(0, len(shortest_path)-1):
         link = topology[shortest_path[i]][shortest_path[i+1]]
         index = randrange(len(link))
         dstPort = NodePortTuple(shortest_path[i], link[index]['src_port'])
         srcPort = NodePortTuple(shortest_path[i+1], link[index]['dst_port'])
         route.append(dstPort)
         route.append(srcPort)
         
     route.append(egressPort)
     body = json.dumps([hop.to_dict() for hop in route])
     return Response(content_type='application/json', body=body)
Exemplo n.º 2
0
 def _event_switch_enter_handler(self, ev):
     msg = ev.switch.to_dict()
     dpid = str_to_dpid(msg['dpid'])
     self.swPort_to_mac.setdefault(dpid, {})
     self.swPort_to_dpid.setdefault(dpid, {})
     for port in msg['ports']:
         port_no = str_to_port_no(port['port_no'])
         self.swPort_to_mac[dpid][port_no] = port['hw_addr']
         self.border_mac.add(port['hw_addr'])
Exemplo n.º 3
0
 def _receive_from_handler(self, req, **kwargs):
     try:
         data = eval(req.body) if req.body else {}
     except SyntaxError:
         self.visual_server.logger.info("source_for_handler: "
                                        "invalid syntax %s" %  req.body)
         return Response(status=400)
     try:
         mac = data["mac"]
         dpid = str_to_dpid(data["dpid"])
         port_no = str_to_port_no(data["port_no"])
         stream_id = data["stream_id"]
     except KeyError, message:
         return Response(status=400, body=str(message))
Exemplo n.º 4
0
    def put_switch_port_gateway(self, req, **kwargs):
        try:
            switch = self.routing_flow_app.switches[dpid_lib.str_to_dpid(
                kwargs['dpid'])]
        except:
            return Response(status=404)

        payload = json.loads(req.body)

        switch.update_gateway_with_prefixlen(
            ipv4=payload['ipv4'],
            ipv4_prefixlen=int(payload['ipv4_prefixlen']),
            ipv6=payload['ipv6'],
            ipv6_prefixlen=int(payload['ipv6_prefixlen']),
            port_no=portno_lib.str_to_port_no(kwargs['portno']))

        return Response(status=200, body=rest_body_ok)
Exemplo n.º 5
0
 def from_rest_dict(cls, p):
     return cls(str_to_dpid(p['dpid']),
                str_to_port_no(p['port_no']),
                p['hw_addr'],
                p['name'])
Exemplo n.º 6
0
 def from_rest_dict(cls, p):
     return cls(str_to_dpid(p['dpid']), str_to_port_no(p['port_no']),
                p['hw_addr'], p['name'])
Exemplo n.º 7
0
    def put_switch_port_gateway(self, req, **kwargs):
        try:
            switch = self.routing_flow_app.switches[dpid_lib.str_to_dpid(kwargs['dpid'])]
        except:
            return Response(status=404)

        payload = json.loads(req.body)

        switch.update_gateway_with_prefixlen(ipv4=payload['ipv4'], ipv4_prefixlen=int(payload['ipv4_prefixlen']),
                                ipv6=payload['ipv6'], ipv6_prefixlen=int(payload['ipv6_prefixlen']), port_no=portno_lib.str_to_port_no(kwargs['portno']))
            
        return Response(status=200, body=rest_body_ok)
Exemplo n.º 8
0
    def push_flow(self, req, source_dpid_int, destn_dpid_int, mac, **_kwargs):
        path = self.shortest_route(source_dpid_int, destn_dpid_int)

        source_dpid = self.append(source_dpid_int)
        destn_dpid = self.append(destn_dpid_int)
	source_dpid1 = dpid_lib.str_to_dpid(source_dpid)
        destn_dpid1 = dpid_lib.str_to_dpid(destn_dpid)
        source_dp = self.dpset.get(source_dpid1)
        destn_dp = self.dpset.get(destn_dpid1)
	 
        if source_dp is None:
		return Response(status=404)
            
        for node in range(len(path)-1):
            tsrc_dpid = int(path[node].replace("dpid=",""))
            tdst_dpid = int(path[node+1].replace("dpid=",""))
            tsrc_dpid = self.append(tsrc_dpid)
            tdst_dpid = self.append(tdst_dpid)
            tsrc_dpid = dpid_lib.str_to_dpid(tsrc_dpid)
            tdst_dpid = dpid_lib.str_to_dpid(tdst_dpid)
            tsrc_dp = self.dpset.get(tsrc_dpid)
	   
            links = get_link(self.topology_api_app, tsrc_dpid)
		
            if links is None:
                return Response(status=404)            
            for i in range(len(links)):
                links_dpid = dpid_lib.str_to_dpid(links[i].to_dict()["dst"]["dpid"])
                if links_dpid == tdst_dpid:
                    str_port = links[i].to_dict()["src"]["port_no"]  
                    break            
            port = port_lib.str_to_port_no(str_port)
            actions = [tsrc_dp.ofproto_parser.OFPActionOutput(int(port))]
            # pushing flows
            ofproto = tsrc_dp.ofproto
            
            match = tsrc_dp.ofproto_parser.OFPMatch(dl_dst=haddr_to_bin(mac))
            mod = tsrc_dp.ofproto_parser.OFPFlowMod(
                datapath=tsrc_dp, match=match, cookie=0,
                command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
                priority=ofproto.OFP_DEFAULT_PRIORITY,
                flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
            tsrc_dp.send_msg(mod)

        
        ofproto = destn_dp.ofproto
       
        if(destn_dpid1 == 0000000000000001):
            logging.debug("PORT")
            out_port = 1
            logging.debug(out_port)
            logging.debug("PORT")
        if(destn_dpid1 == 0000000000000002):
            out_port = 3
        if(destn_dpid1 == 0000000000000003):
            out_port = 2
        actions1 = [destn_dp.ofproto_parser.OFPActionOutput(1)]
        match1 = destn_dp.ofproto_parser.OFPMatch(dl_dst=haddr_to_bin(mac))
       
        mod1 = destn_dp.ofproto_parser.OFPFlowMod(
            datapath=destn_dp, match=match1, cookie=0,
            command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
            priority=ofproto.OFP_DEFAULT_PRIORITY,
            flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions1)
        destn_dp.send_msg(mod1)
	
        body = json.dumps([link.to_dict() for link in links])
        return Response(content_type='application/json', body=body)
Exemplo n.º 9
0
    def push_flow(self, req, source_dpid_int, destn_dpid_int, mac, **_kwargs):
        path = self.shortest_route(source_dpid_int, destn_dpid_int)

        source_dpid = self.append(source_dpid_int)
        destn_dpid = self.append(destn_dpid_int)
        source_dpid1 = dpid_lib.str_to_dpid(source_dpid)
        destn_dpid1 = dpid_lib.str_to_dpid(destn_dpid)
        source_dp = self.dpset.get(source_dpid1)
        destn_dp = self.dpset.get(destn_dpid1)

        if source_dp is None:
            return Response(status=404)

        for node in range(len(path) - 1):
            tsrc_dpid = int(path[node].replace("dpid=", ""))
            tdst_dpid = int(path[node + 1].replace("dpid=", ""))
            tsrc_dpid = self.append(tsrc_dpid)
            tdst_dpid = self.append(tdst_dpid)
            tsrc_dpid = dpid_lib.str_to_dpid(tsrc_dpid)
            tdst_dpid = dpid_lib.str_to_dpid(tdst_dpid)
            tsrc_dp = self.dpset.get(tsrc_dpid)

            links = get_link(self.topology_api_app, tsrc_dpid)

            if links is None:
                return Response(status=404)
            for i in range(len(links)):
                links_dpid = dpid_lib.str_to_dpid(
                    links[i].to_dict()["dst"]["dpid"])
                if links_dpid == tdst_dpid:
                    str_port = links[i].to_dict()["src"]["port_no"]
                    break
            port = port_lib.str_to_port_no(str_port)
            actions = [tsrc_dp.ofproto_parser.OFPActionOutput(int(port))]
            # pushing flows
            ofproto = tsrc_dp.ofproto

            match = tsrc_dp.ofproto_parser.OFPMatch(dl_dst=haddr_to_bin(mac))
            mod = tsrc_dp.ofproto_parser.OFPFlowMod(
                datapath=tsrc_dp,
                match=match,
                cookie=0,
                command=ofproto.OFPFC_ADD,
                idle_timeout=0,
                hard_timeout=0,
                priority=ofproto.OFP_DEFAULT_PRIORITY,
                flags=ofproto.OFPFF_SEND_FLOW_REM,
                actions=actions)
            tsrc_dp.send_msg(mod)

        ofproto = destn_dp.ofproto

        if (destn_dpid1 == 0000000000000001):
            logging.debug("PORT")
            out_port = 1
            logging.debug(out_port)
            logging.debug("PORT")
        if (destn_dpid1 == 0000000000000002):
            out_port = 3
        if (destn_dpid1 == 0000000000000003):
            out_port = 2
        actions1 = [destn_dp.ofproto_parser.OFPActionOutput(1)]
        match1 = destn_dp.ofproto_parser.OFPMatch(dl_dst=haddr_to_bin(mac))

        mod1 = destn_dp.ofproto_parser.OFPFlowMod(
            datapath=destn_dp,
            match=match1,
            cookie=0,
            command=ofproto.OFPFC_ADD,
            idle_timeout=0,
            hard_timeout=0,
            priority=ofproto.OFP_DEFAULT_PRIORITY,
            flags=ofproto.OFPFF_SEND_FLOW_REM,
            actions=actions1)
        destn_dp.send_msg(mod1)

        body = json.dumps([link.to_dict() for link in links])
        return Response(content_type='application/json', body=body)