def setUp(self): """Basic test setup.""" self.message = StatsRequest() self.message.header.xid = 1 self.message.type = StatsTypes.OFPST_FLOW self.message.flags = 1 self.message.body = []
def test_pack_unpack_port_stats(self): """Pack and unpack PortStatsRequest.""" body = PortStatsRequest(Port.OFPP_NONE) req = StatsRequest(16909060, body_type=StatsTypes.OFPST_PORT, body=body) pack = req.pack() unpacked = unpack_message(pack) self.assertEqual(req, unpacked)
def request(self, conn): """Ask for flow stats.""" body = AggregateStatsRequest() # Port.OFPP_NONE and All Tables req = StatsRequest(body_type=StatsType.OFPST_AGGREGATE, body=body) self._send_event(req, conn) log.debug('Aggregate Stats request for switch %s sent.', conn.switch.dpid)
def _get_versioned_request(of_version): if of_version == 0x01: return StatsRequest(body_type=StatsType.OFPST_PORT, body=v0x01.PortStatsRequest( Port.OFPP_NONE)) # All ports return MultipartRequest(multipart_type=MultipartType.OFPMP_PORT_STATS, body=v0x04.PortStatsRequest())
class TestStatsRequest(unittest.TestCase): """Test for StatsRequest message.""" def setUp(self): """Basic test setup.""" self.message = StatsRequest() self.message.header.xid = 1 self.message.type = StatsTypes.OFPST_FLOW self.message.flags = 1 self.message.body = [] def test_get_size(self): """[Controller2Switch/StatsRequest] - size 12.""" self.assertEqual(self.message.get_size(), 12) def test_pack_unpack_port_stats(self): """Pack and unpack PortStatsRequest.""" body = PortStatsRequest(Port.OFPP_NONE) req = StatsRequest(16909060, body_type=StatsTypes.OFPST_PORT, body=body) pack = req.pack() unpacked = unpack_message(pack) self.assertEqual(req, unpacked) @unittest.skip('Not yet implemented') def test_pack(self): """[Controller2Switch/StatsRequest] - packing.""" # TODO pass @unittest.skip('Not yet implemented') def test_unpack(self): """[Controller2Switch/StatsRequest] - unpacking.""" # TODO pass
def request(self, conn): """Ask for switch description. It is done only once per switch.""" dpid = conn.switch.dpid if dpid not in self._desc: req = StatsRequest(body_type=StatsTypes.OFPST_DESC) self._send_event(req, conn) log.debug('Desc request for switch %s sent.', dpid)
def _get_versioned_request(of_version): if of_version == 0x01: return StatsRequest( body_type=StatsType.OFPST_FLOW, body=v0x01.FlowStatsRequest()) return MultipartRequest( multipart_type=MultipartType.OFPMP_FLOW, body=v0x04.FlowStatsRequest())
def send_desc_request(controller, switch): """Send a description request to the switch. Args: controller(:class:`~kytos.core.controller.Controller`): the controller being used. switch(:class:`~kytos.core.switch.Switch`): target to send a stats request. """ stats_request = StatsRequest(body_type=StatsType.OFPST_DESC) emit_message_out(controller, switch.connection, stats_request)
def request_port_stats(controller, switch): """Request port stats from switches. Args: controller(:class:`~kytos.core.controller.Controller`): the controller being used. switch(:class:`~kytos.core.switch.Switch`): target to send a stats request. """ body = PortStatsRequest() stats_request = StatsRequest(body_type=StatsType.OFPST_PORT, body=body) # req.pack() emit_message_out(controller, switch.connection, stats_request)
def update_flow_list(controller, switch): """Request flow stats from switches. Args: controller(:class:`~kytos.core.controller.Controller`): the controller being used. switch(:class:`~kytos.core.switch.Switch`): target to send a stats request. """ body = FlowStatsRequest() stats_request = StatsRequest(body_type=StatsType.OFPST_FLOW, body=body) # req.pack() emit_message_out(controller, switch.connection, stats_request)
def request(self, conn): """Ask for flow stats.""" body = FlowStatsRequest() # Port.OFPP_NONE and All Tables req = StatsRequest(body_type=StatsTypes.OFPST_FLOW, body=body) self._send_event(req, conn) log.debug('Flow Stats request for switch %s sent.', conn.switch.dpid)
def request(self, conn): """Ask for port stats.""" body = PortStatsRequest(Port.OFPP_NONE) # All ports req = StatsRequest(body_type=StatsTypes.OFPST_PORT, body=body) self._send_event(req, conn) log.debug('Port Stats request for switch %s sent.', conn.switch.dpid)
def handleStatsRequest(device, header, body, verbose): if (verbose): print("Got StatsReq") ofStatsReq = StatsRequest() ofStatsReq.unpack(body) if (verbose): print(ofStatsReq.body_type) if (ofStatsReq.body_type == 0): #Description flag descReply = DescStats( mfr_desc=device.switch_desc["switch_mfr_desc"], hw_desc=device.switch_desc["switch_hw_desc"], sw_desc=device.switch_desc["switch_sw_desc"], serial_num=device.switch_desc["switch_serial_num"], dp_desc=device.switch_desc["switch_dp_desc"]) statsReplyBody = descReply.pack() ofStatsReply = StatsReply(xid=header.xid, body_type=ofStatsReq.body_type, flags=UBInt16(0x00000000), body=statsReplyBody) device.comm_sock.send(ofStatsReply.pack()) elif (ofStatsReq.body_type == 1): #Flow flag if (verbose): print("Got flow stats req") #There may be an issue with the library here. Getting "Pack error: UBInt16 could not pack NoneType = None.". Must investigate further #ofFlowStatsReq = FlowStatsRequest() #ofFlowStatsReq.unpack(body) #for f in device.switch_flows: #flowStats = FlowStats(length=device.switch_flows[f].length, #table_id=device.switch_flows[f].table_id, #match=ofFlowStatsReq.match, #duration_sec=device.switch_stats["flow"]["duration_sec"], #duration_nsec=device.switch_stats["flow"]["duration_nsec"], #priority=device.switch_flows[f].priority, #idle_timeout=device.switch_flows[f].idle_timeout, #hard_timeout=device.switch_flows[f].hard_timeout, #cookie=device.switch_flows[f].cookie, #packet_count=device.switch_stats["flow"]["packet_count"], #byte_count=device.switch_stats["flow"]["byte_count"], #actions=device.switch_flows[f].actions #) #statsReplyBody = flowStats.pack() #ofStatsReply = StatsReply(xid=header.xid, body_type=ofStatsReq.body_type, flags=UBInt16(0x00000000), body=statsReplyBody) #device.comm_sock.send(ofStatsReply.pack()) elif (ofStatsReq.body_type == 2): #Aggregate flag aggReply = AggregateStatsReply( packet_count=device.switch_stats["aggregate"]["packet_count"], byte_count=device.switch_stats["aggregate"]["byte_count"], flow_count=device.switch_stats["aggregate"]["flow_count"]) statsReplyBody = aggReply.pack() ofStatsReply = StatsReply(xid=header.xid, body_type=ofStatsReq.body_type, flags=UBInt16(0x00000000), body=statsReplyBody) device.comm_sock.send(ofStatsReply.pack()) elif (ofStatsReq.body_type == 3): #Table flag if (verbose): print("Got table") elif (ofStatsReq.body_type == 4): #Port flag if (verbose): print("Got port") ofStatsReqBody = PortStatsRequest() ofStatsReqBody.unpack(body) if (ofStatsReqBody.port_no == ofStatsReq.body_type): #It wants stats from all ports statsReplyBody = b'' for p in device.switch_stats["port"]: portStats = device.switch_stats["port"][p] statsReplyBody += portStats.pack() ofStatsReply = StatsReply(xid=header.xid, body_type=ofStatsReq.body_type, flags=UBInt16(0x00000000), body=statsReplyBody) device.comm_sock.send(ofStatsReply.pack()) else: #Just one port portStats = device.switch_stats["port"][str( ofStatsReqBody.port_no)] statsReplyBody = portStats.pack() ofStatsReply = StatsReply(xid=header.xid, body_type=ofStatsReq.body_type, flags=UBInt16(0x00000000), body=statsReplyBody) device.comm_sock.send(ofStatsReply.pack()) elif (ofStatsReq.body_type == 5): #Queue flag if (verbose): print("Got Queue") ofStatsReqBody = QueueStatsRequest() ofStatsReqBody.unpack(body) if (ofStatsReqBody.port_no == ofStatsReq.body_type): #Wants stats for all ports statsReplyBody = b'' for q in device.switch_stats["queue"]: if (verbose): print(statsReplyBody) queueStats = device.switch_stats["queue"][q] statsReplyBody += queueStats.pack() ofStatsReply = StatsReply(xid=header.xid, body_type=ofStatsReq.body_type, flags=UBInt16(0x00000000), body=statsReplyBody) device.comm_sock.send(ofStatsReply.pack()) else: #Just one port queueStats = device.switch_stats["queue"][ str(ofStatsReqBody.port_no) + ":" + str(ofStatsReqBody.queue_id)] statsReplyBody = queueStats.pack() ofStatsReply = StatsReply(xid=header.xid, body_type=ofStatsReq.body_type, flags=UBInt16(0x00000000), body=statsReplyBody) device.comm_sock.send(ofStatsReply.pack()) if (verbose): print("Sent StatsRes")