def _of_message_to_object(binary_string): """ Map a binary string to the corresponding class. Appropriately resolves subclasses """ hdr = ofp.ofp_header() hdr.unpack(binary_string) # FIXME: Add error detection if not hdr.type in msg_type_subclassed: return msg_type_to_class_map[hdr.type]() if hdr.type == ofp.OFPT_STATS_REQUEST: sub_hdr = ofp.ofp_stats_request() sub_hdr.unpack(binary_string[ofp.OFP_HEADER_BYTES:]) try: obj = stats_request_to_class_map[sub_hdr.type]() except LookupError: obj = None return obj elif hdr.type == ofp.OFPT_STATS_REPLY: sub_hdr = ofp.ofp_stats_reply() sub_hdr.unpack(binary_string[ofp.OFP_HEADER_BYTES:]) try: obj = stats_reply_to_class_map[sub_hdr.type]() except LookupError: obj = None return obj elif hdr.type == ofp.OFPT_ERROR: sub_hdr = ofp.ofp_error_msg() sub_hdr.unpack(binary_string[ofp.OFP_HEADER_BYTES:]) return error_to_class_map[sub_hdr.type]() else: parse_logger.error("Cannot parse pkt to message") return None
def runTest(self): logging.info("Running Grp100No60 BadRequestBadLength test") #In Message module at pack time the length is computed #avoid this by using cstruct module logging.info("Sending stats_request message..") stats_request = ofp.ofp_stats_request() header=ofp.ofp_header() header.type = ofp.OFPT_STATS_REQUEST # normal the header length is 12bytes changed it to 18bytes header.length=18; packed=header.pack()+stats_request.pack() sleep(2) rv=self.controller.message_send(packed) sleep(2) self.assertTrue(rv != -1,"Unable to send the message") logging.info("Waiting for OFPT_ERROR message..") (response, pkt) = self.controller.poll(exp_msg=ofp.OFPT_ERROR, timeout=5) self.assertTrue(response is not None, 'Switch did not reply with expected error messge') self.assertTrue(response.type==ofp.OFPET_BAD_REQUEST, 'Error type is not OFPET_BAD_REQUEST') self.assertTrue(response.type==ofp.OFPBRC_BAD_LEN, 'Error code is not OFPBRC_BAD_LEN')