diff options
-rw-r--r-- | ryu/tests/unit/ofproto/test_parser_v12.py | 4035 |
1 files changed, 2951 insertions, 1084 deletions
diff --git a/ryu/tests/unit/ofproto/test_parser_v12.py b/ryu/tests/unit/ofproto/test_parser_v12.py index b05f9f6d..be10443a 100644 --- a/ryu/tests/unit/ofproto/test_parser_v12.py +++ b/ryu/tests/unit/ofproto/test_parser_v12.py @@ -21,14 +21,14 @@ from struct import * from nose.tools import * from nose.plugins.skip import Skip, SkipTest from ryu.ofproto.ofproto_v1_2_parser import * -from ryu.ofproto import ofproto_v1_2_parser +from ryu.ofproto import ofproto_v1_2_parser, nx_match LOG = logging.getLogger('test_ofproto_v12') class TestMsgParser(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.msg_parser + """ Test case for ofproto_v1_2_parser.msg_parser """ def setUp(self): @@ -45,121 +45,33 @@ class TestMsgParser(unittest.TestCase): # OFP_HEADER_PACK_STR # '!BBHI'...version, msg_type, msg_len, xid - version = {'buf': '\x03', 'val': ofproto_v1_2.OFP_VERSION} - msg_type = {'buf': '\x00', 'val': ofproto_v1_2.OFPT_HELLO} - msg_len = {'buf': '\x00\x08', 'val': ofproto_v1_2.OFP_HEADER_SIZE} - xid = {'buf': '\x50\x26\x6a\x4c', 'val': 1344694860} - - buf = version['buf'] \ - + msg_type['buf'] \ - + msg_len['buf'] \ - + xid['buf'] - - c = msg_parser(Datapath, - version['val'], - msg_type['val'], - msg_len['val'], - xid['val'], - buf) - - eq_(version['val'], c.version) - eq_(msg_type['val'], c.msg_type) - eq_(msg_len['val'], c.msg_len) - eq_(xid['val'], c.xid) + version = ofproto_v1_2.OFP_VERSION + msg_type = ofproto_v1_2.OFPT_HELLO + msg_len = ofproto_v1_2.OFP_HEADER_SIZE + xid = 2495926989 - # buf fmt = ofproto_v1_2.OFP_HEADER_PACK_STR - res = struct.unpack(fmt, c.buf) - - eq_(version['val'], res[0]) - eq_(msg_type['val'], res[1]) - eq_(msg_len['val'], res[2]) - eq_(xid['val'], res[3]) + buf = pack(fmt, version, msg_type, msg_len, xid) + c = msg_parser(Datapath, version, msg_type, msg_len, xid, buf) -class TestOFPPort(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPPort - """ + eq_(version, c.version) + eq_(msg_type, c.msg_type) + eq_(msg_len, c.msg_len) + eq_(xid, c.xid) - # OFP_PORT_PACK_STR - # '!I4x6s2x16sIIIIIIII'... port_no, pad(4), hw_addr, pad(2), - # name, config, state, curr, advertised, - # peer, curr_speed, max_speed - port_no = {'buf': '\x42\xbd\x27\xfc', 'val': 1119692796} - hw_addr = 'hw'.ljust(6) - name = 'name'.ljust(16) - config = {'buf': '\x84\xb6\x8c\x53', 'val': 2226555987} - state = {'buf': '\x64\x07\xfb\xc9', 'val': 1678244809} - curr = {'buf': '\xa9\xe8\x0a\x2b', 'val': 2850556459} - advertised = {'buf': '\x78\xb9\x7b\x72', 'val': 2025421682} - supported = {'buf': '\x7e\x65\x68\xad', 'val': 2120575149} - peer = {'buf': '\xa4\x5b\x8b\xed', 'val': 2757463021} - curr_speed = {'buf': '\x9d\x6f\xdb\x23', 'val': 2641353507} - max_speed = {'buf': '\x6b\x20\x7e\x98', 'val': 1797291672} - - buf = port_no['buf'] \ - + pack('4x') \ - + hw_addr \ - + pack('2x') \ - + name \ - + config['buf'] \ - + state['buf'] \ - + curr['buf'] \ - + advertised['buf'] \ - + supported['buf'] \ - + peer['buf'] \ - + curr_speed['buf'] \ - + max_speed['buf'] - - c = OFPPort(port_no['val'], - hw_addr, - name, - config['val'], - state['val'], - curr['val'], - advertised['val'], - supported['val'], - peer['val'], - curr_speed['val'], - max_speed['val']) - - def setUp(self): - pass - - def tearDown(self): - pass - - def test_init(self): - eq_(self.port_no['val'], self.c.port_no) - eq_(self.hw_addr, self.c.hw_addr) - eq_(self.name, self.c.name) - eq_(self.config['val'], self.c.config) - eq_(self.state['val'], self.c.state) - eq_(self.curr['val'], self.c.curr) - eq_(self.advertised['val'], self.c.advertised) - eq_(self.supported['val'], self.c.supported) - eq_(self.peer['val'], self.c.peer) - eq_(self.curr_speed['val'], self.c.curr_speed) - eq_(self.max_speed['val'], self.c.max_speed) - - def test_parser(self): - res = self.c.parser(self.buf, 0) + # buf + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + res = struct.unpack(fmt, c.buf) - eq_(self.port_no['val'], res.port_no) - eq_(self.hw_addr, res.hw_addr) - eq_(self.name, res.name) - eq_(self.config['val'], res.config) - eq_(self.state['val'], res.state) - eq_(self.curr['val'], res.curr) - eq_(self.advertised['val'], res.advertised) - eq_(self.supported['val'], res.supported) - eq_(self.peer['val'], res.peer) - eq_(self.curr_speed['val'], res.curr_speed) - eq_(self.max_speed['val'], res.max_speed) + eq_(version, res[0]) + eq_(msg_type, res[1]) + eq_(msg_len, res[2]) + eq_(xid, res[3]) class TestOFPHello(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPHello + """ Test case for ofproto_v1_2_parser.OFPHello """ def setUp(self): @@ -185,24 +97,6 @@ class TestOFPHello(unittest.TestCase): eq_(ofproto_v1_2.OFP_HEADER_SIZE, res.msg_len) eq_(xid, res.xid) - # test __str__() - list_ = ('version:', 'msg_type', 'xid') - check = {} - str_ = str(res) - str_ = str_.rsplit() - - i = 0 - for s in str_: - if s in list_: - check[str_[i]] = str_[i + 1] - i += 1 - - # comparison fails in some environment - # such as hex() returns string with suffix 'L' - eq_(hex(ofproto_v1_2.OFP_VERSION).find(check['version:']), 0) - eq_(hex(ofproto_v1_2.OFPT_HELLO).find(check['msg_type']), 0) - eq_(hex(xid).find(check['xid']), 0) - def test_serialize(self): class Datapath(object): @@ -216,15 +110,33 @@ class TestOFPHello(unittest.TestCase): eq_(0, c.xid) -class TestOFPFeaturesRequest(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPFeaturesRequest +class TestOFPErrorMsg(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPErrorMsg """ class Datapath(object): ofproto = ofproto_v1_2 ofproto_parser = ofproto_v1_2_parser - c = OFPFeaturesRequest(Datapath) + # OFP_HEADER_PACK_STR + # '!BBHI'...version, msg_type, msg_len, xid + version = ofproto_v1_2.OFP_VERSION + msg_type = ofproto_v1_2.OFPT_ERROR + msg_len = ofproto_v1_2.OFP_ERROR_MSG_SIZE + xid = 2495926989 + + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + buf = pack(fmt, version, msg_type, msg_len, xid) + + # OFP_ERROR_MSG_PACK_STR = '!HH' + type_ = ofproto_v1_2.OFPET_HELLO_FAILED + code = ofproto_v1_2.OFPHFC_EPERM + data = 'Error Message.' + + fmt = ofproto_v1_2.OFP_ERROR_MSG_PACK_STR + buf += pack(fmt, type_, code) + data + + c = OFPErrorMsg(Datapath) def setUp(self): pass @@ -233,38 +145,66 @@ class TestOFPFeaturesRequest(unittest.TestCase): pass def test_init(self): - pass + self.c.code = self.code + self.c.type = self.type_ + self.c.data = self.data def test_parser(self): - # Not used. - pass + res = self.c.parser(object, self.version, self.msg_type, + self.msg_len, self.xid, self.buf) + + eq_(res.version, self.version) + eq_(res.msg_type, self.msg_type) + eq_(res.msg_len, self.msg_len) + eq_(res.xid, self.xid) + eq_(res.type, self.type_) + eq_(res.code, self.code) + eq_(res.data, self.data) def test_serialize(self): self.c.serialize() eq_(ofproto_v1_2.OFP_VERSION, self.c.version) - eq_(ofproto_v1_2.OFPT_FEATURES_REQUEST, self.c.msg_type) + eq_(ofproto_v1_2.OFPT_ERROR, self.c.msg_type) eq_(0, self.c.xid) - fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + fmt = '!' \ + + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_ERROR_MSG_PACK_STR.replace('!', '') \ + + str(len(self.data)) + 's' res = struct.unpack(fmt, str(self.c.buf)) - eq_(ofproto_v1_2.OFP_VERSION, res[0]) - eq_(ofproto_v1_2.OFPT_FEATURES_REQUEST, res[1]) - eq_(len(self.c.buf), res[2]) - eq_(0, res[3]) + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_ERROR) + eq_(res[2], len(self.c.buf)) + eq_(res[3], 0) + eq_(res[4], self.type_) + eq_(res[5], self.code) + eq_(res[6], self.data) -class TestOFPSwitchFeatures(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPSwitchFeatures +class TestOFPEchoRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPEchoRequest """ class Datapath(object): ofproto = ofproto_v1_2 ofproto_parser = ofproto_v1_2_parser - c = OFPSwitchFeatures(Datapath) + # OFP_HEADER_PACK_STR + # '!BBHI'...version, msg_type, msg_len, xid + version = ofproto_v1_2.OFP_VERSION + msg_type = ofproto_v1_2.OFPT_ECHO_REQUEST + msg_len = ofproto_v1_2.OFP_HEADER_SIZE + xid = 2495926989 + + data = 'Request Message.' + + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + buf = pack(fmt, version, msg_type, msg_len, xid) + data + + c = OFPEchoRequest(Datapath) def setUp(self): pass @@ -273,139 +213,59 @@ class TestOFPSwitchFeatures(unittest.TestCase): pass def test_init(self): - pass + self.c.data = self.data def test_parser(self): + res = self.c.parser(object, self.version, self.msg_type, + self.msg_len, self.xid, self.buf) - # OFP_HEADER_PACK_STR - # '!BBHI'...version, msg_type, msg_len, xid - version = {'buf': '\x03', 'val': ofproto_v1_2.OFP_VERSION} - msg_type = {'buf': '\x06', 'val': ofproto_v1_2.OFPT_FEATURES_REPLY} - msg_len = {'buf': '\x00\x4c', - 'val': ofproto_v1_2.OFP_SWITCH_FEATURES_SIZE \ - + ofproto_v1_2.OFP_PORT_SIZE} - xid = {'buf': '\xcc\x0a\x41\xd4', 'val': 3423224276} - - buf = version['buf'] \ - + msg_type['buf'] \ - + msg_len['buf'] \ - + xid['buf'] + eq_(res.version, self.version) + eq_(res.msg_type, self.msg_type) + eq_(res.msg_len, self.msg_len) + eq_(res.xid, self.xid) + eq_(res.data, self.data) - # OFP_SWITCH_FEATURES_PACK_STR - # '!QIB3xII'...datapath_id, n_buffers, n_tables, - # pad(3), capabilities, reserved - datapath_id = {'buf': '\x11\xa3\x72\x63\x61\xde\x39\x81', - 'val': 1270985291017894273} - n_buffers = {'buf': '\x80\x14\xd7\xf6', 'val': 2148849654} - n_tables = {'buf': '\xe4', 'val': 228} - capabilities = {'buf': '\x69\x4f\xe4\xc2', 'val': 1766843586} - reserved = {'buf': '\x78\x06\xd9\x0c', 'val': 2013714700} - - buf += datapath_id['buf'] \ - + n_buffers['buf'] \ - + n_tables['buf'] \ - + pack('3x') \ - + capabilities['buf'] \ - + reserved['buf'] + def test_serialize(self): + self.c.serialize() - # OFP_PORT_PACK_STR - # '!I4x6s2x16sIIIIIIII'... port_no, pad(4), hw_addr, pad(2), - # name, config, state, curr, advertised, - # peer, curr_speed, max_speed - port_no = {'buf': '\x42\xbd\x27\xfc', 'val': 1119692796} - hw_addr = 'hw'.ljust(6) - name = 'name'.ljust(16) - config = {'buf': '\x84\xb6\x8c\x53', 'val': 2226555987} - state = {'buf': '\x64\x07\xfb\xc9', 'val': 1678244809} - curr = {'buf': '\xa9\xe8\x0a\x2b', 'val': 2850556459} - advertised = {'buf': '\x78\xb9\x7b\x72', 'val': 2025421682} - supported = {'buf': '\x7e\x65\x68\xad', 'val': 2120575149} - peer = {'buf': '\xa4\x5b\x8b\xed', 'val': 2757463021} - curr_speed = {'buf': '\x9d\x6f\xdb\x23', 'val': 2641353507} - max_speed = {'buf': '\x6b\x20\x7e\x98', 'val': 1797291672} - - buf += port_no['buf'] \ - + pack('4x') \ - + hw_addr \ - + pack('2x') \ - + name \ - + config['buf'] \ - + state['buf'] \ - + curr['buf'] \ - + advertised['buf'] \ - + supported['buf'] \ - + peer['buf'] \ - + curr_speed['buf'] \ - + max_speed['buf'] - - res = OFPSwitchFeatures.parser(object, - version['val'], - msg_type['val'], - msg_len['val'], - xid['val'], - buf) - - eq_(version['val'], res.version) - eq_(msg_type['val'], res.msg_type) - eq_(msg_len['val'], res.msg_len) - eq_(xid['val'], res.xid) - eq_(datapath_id['val'], res.datapath_id) - eq_(n_buffers['val'], res.n_buffers) - eq_(n_tables['val'], res.n_tables) - eq_(capabilities['val'], res.capabilities) - eq_(reserved['val'], res.reserved) - - # port - port = res.ports[port_no['val']] - eq_(port_no['val'], port.port_no) - eq_(hw_addr, port.hw_addr) - eq_(name, port.name) - eq_(config['val'], port.config) - eq_(state['val'], port.state) - eq_(curr['val'], port.curr) - eq_(advertised['val'], port.advertised) - eq_(supported['val'], port.supported) - eq_(peer['val'], port.peer) - eq_(curr_speed['val'], port.curr_speed) - eq_(max_speed['val'], port.max_speed) - - # test __str__() - list_ = ('version:', 'msg_type', 'xid',) - check = {} - str_ = str(res) - str_ = str_.rsplit() - - i = 0 - for s in str_: - if s in list_: - check[str_[i]] = str_[i + 1] - i += 1 - - eq_(hex(version['val']).find(check['version:']), 0) - eq_(hex(msg_type['val']).find(check['msg_type']), 0) - eq_(hex(xid['val']).find(check['xid']), 0) + eq_(ofproto_v1_2.OFP_VERSION, self.c.version) + eq_(ofproto_v1_2.OFPT_ECHO_REQUEST, self.c.msg_type) + eq_(0, self.c.xid) - def test_serialize(self): - # Not used. - pass + fmt = '!' \ + + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ + + str(len(self.data)) + 's' + res = struct.unpack(fmt, str(self.c.buf)) -class TestOFPSetConfig(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPSetConfig + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_ECHO_REQUEST) + eq_(res[2], len(self.c.buf)) + eq_(res[3], 0) + eq_(res[4], self.data) + + +class TestOFPEchoReply(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPEchoReply """ class Datapath(object): ofproto = ofproto_v1_2 ofproto_parser = ofproto_v1_2_parser - # OFP_SWITCH_CONFIG_PACK_STR - # '!HH'...flags, miss_send_len - flags = {'buf': '\xa0\xe2', 'val': 41186} - miss_send_len = {'buf': '\x36\x0e', 'val': 13838} + # OFP_HEADER_PACK_STR + # '!BBHI'...version, msg_type, msg_len, xid + version = ofproto_v1_2.OFP_VERSION + msg_type = ofproto_v1_2.OFPT_ECHO_REPLY + msg_len = ofproto_v1_2.OFP_HEADER_SIZE + xid = 2495926989 - c = OFPSetConfig(Datapath, - flags['val'], - miss_send_len['val']) + data = 'Reply Message.' + + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + buf = pack(fmt, version, msg_type, msg_len, xid) + data + + c = OFPEchoReply(Datapath) def setUp(self): pass @@ -414,38 +274,48 @@ class TestOFPSetConfig(unittest.TestCase): pass def test_init(self): - eq_(self.flags['val'], self.c.flags) - eq_(self.miss_send_len['val'], self.c.miss_send_len) + self.c.data = self.data def test_parser(self): - # Not used. - pass + res = self.c.parser(object, self.version, self.msg_type, + self.msg_len, self.xid, self.buf) + + eq_(res.version, self.version) + eq_(res.msg_type, self.msg_type) + eq_(res.msg_len, self.msg_len) + eq_(res.xid, self.xid) + eq_(res.data, self.data) def test_serialize(self): self.c.serialize() eq_(ofproto_v1_2.OFP_VERSION, self.c.version) - eq_(ofproto_v1_2.OFPT_SET_CONFIG, self.c.msg_type) + eq_(ofproto_v1_2.OFPT_ECHO_REPLY, self.c.msg_type) eq_(0, self.c.xid) fmt = '!' \ + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ - + ofproto_v1_2.OFP_SWITCH_CONFIG_PACK_STR.replace('!', '') + + str(len(self.data)) + 's' res = struct.unpack(fmt, str(self.c.buf)) - eq_(ofproto_v1_2.OFP_VERSION, res[0]) - eq_(ofproto_v1_2.OFPT_SET_CONFIG, res[1]) - eq_(len(self.c.buf), res[2]) - eq_(0, res[3]) - eq_(self.flags['val'], res[4]) - eq_(self.miss_send_len['val'], res[5]) + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_ECHO_REPLY) + eq_(res[2], len(self.c.buf)) + eq_(res[3], 0) + eq_(res[4], self.data) -class TestOFPEchoRequest(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPEchoRequest +class TestOFPExperimenter(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPExperimenter """ + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + c = OFPExperimenter(Datapath) + def setUp(self): pass @@ -456,87 +326,109 @@ class TestOFPEchoRequest(unittest.TestCase): pass def test_parser(self): - # OFP_HEADER_PACK_STR # '!BBHI'...version, msg_type, msg_len, xid - version = {'buf': '\x01', 'val': ofproto_v1_2.OFP_VERSION} - msg_type = {'buf': '\x02', 'val': ofproto_v1_2.OFPT_ECHO_REQUEST} - msg_len = {'buf': '\x00\x08', - 'val': ofproto_v1_2.OFP_HEADER_SIZE} - xid = {'buf': '\x84\x47\xef\x3f', 'val': 2219306815} - - data = 'Request Message.' - - buf = version['buf'] \ - + msg_type['buf'] \ - + msg_len['buf'] \ - + xid['buf'] \ - + data - - res = OFPEchoRequest.parser(object, - version['val'], - msg_type['val'], - msg_len['val'], - xid['val'], - buf) - - eq_(version['val'], res.version) - eq_(msg_type['val'], res.msg_type) - eq_(msg_len['val'], res.msg_len) - eq_(xid['val'], res.xid) - eq_(data, res.data) - - # test __str__() - list_ = ('version:', 'msg_type', 'xid') - check = {} - str_ = str(res) - str_ = str_.rsplit() - - i = 0 - for s in str_: - if s in list_: - check[str_[i]] = str_[i + 1] - i += 1 - - # comparison fails in some environment - # such as hex() returns string with suffix 'L' - eq_(hex(version['val']).find(check['version:']), 0) - eq_(hex(msg_type['val']).find(check['msg_type']), 0) - eq_(hex(xid['val']).find(check['xid']), 0) + version = ofproto_v1_2.OFP_VERSION + msg_type = ofproto_v1_2.OFPT_EXPERIMENTER + msg_len = ofproto_v1_2.OFP_EXPERIMENTER_HEADER_SIZE + xid = 2495926989 - def test_serialize(self): - class Datapath(object): - ofproto = ofproto_v1_2 - ofproto_parser = ofproto_v1_2_parser + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + buf = pack(fmt, version, msg_type, msg_len, xid) - data = 'Request Message.' + # OFP_EXPERIMENTER_HEADER_PACK_STR + # '!II'...experimenter, exp_type + experimenter = 0 + exp_type = 1 - c = OFPEchoRequest(Datapath) - c.data = data + fmt = ofproto_v1_2.OFP_EXPERIMENTER_HEADER_PACK_STR + buf += pack(fmt, experimenter, exp_type) - c.serialize() + res = self.c.parser(object, version, msg_type, msg_len, xid, buf) - eq_(ofproto_v1_2.OFP_VERSION, c.version) - eq_(ofproto_v1_2.OFPT_ECHO_REQUEST, c.msg_type) - eq_(0, c.xid) + # OFP_HEADER_PACK_STR + eq_(version, res.version) + eq_(msg_type, res.msg_type) + eq_(msg_len, res.msg_len) + eq_(xid, res.xid) - fmt = '!' \ - + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ - + str(len(data)) + 's' + # OFP_EXPERIMENTER_HEADER_PACK_STR + eq_(experimenter, res.experimenter) + eq_(exp_type, res.exp_type) - res = struct.unpack(fmt, str(c.buf)) - eq_(ofproto_v1_2.OFP_VERSION, res[0]) - eq_(ofproto_v1_2.OFPT_ECHO_REQUEST, res[1]) - eq_(len(c.buf), res[2]) - eq_(0, res[3]) - eq_(data, res[4]) +class TestOFPPort(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPPort + """ + # OFP_PORT_PACK_STR + # '!I4x6s2x16sIIIIIIII'... port_no, pad(4), hw_addr, pad(2), + # name, config, state, curr, advertised, + # peer, curr_speed, max_speed + port_no = 1119692796 + hw_addr = 'hw'.ljust(6) + name = 'name'.ljust(16) + config = 2226555987 + state = 1678244809 + curr = 2850556459 + advertised = 2025421682 + supported = 2120575149 + peer = 2757463021 + curr_speed = 2641353507 + max_speed = 1797291672 + + fmt = ofproto_v1_2.OFP_PORT_PACK_STR + buf = pack(fmt, port_no, hw_addr, name, config, state, curr, + advertised, supported, peer, curr_speed, max_speed) + + c = OFPPort(port_no, hw_addr, name, config, state, curr, + advertised, supported, peer, curr_speed, max_speed) -class TestOFPEchoReply(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPEchoReply + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.port_no, self.c.port_no) + eq_(self.hw_addr, self.c.hw_addr) + eq_(self.name, self.c.name) + eq_(self.config, self.c.config) + eq_(self.state, self.c.state) + eq_(self.curr, self.c.curr) + eq_(self.advertised, self.c.advertised) + eq_(self.supported, self.c.supported) + eq_(self.peer, self.c.peer) + eq_(self.curr_speed, self.c.curr_speed) + eq_(self.max_speed, self.c.max_speed) + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(self.port_no, res.port_no) + eq_(self.hw_addr, res.hw_addr) + eq_(self.name, res.name) + eq_(self.config, res.config) + eq_(self.state, res.state) + eq_(self.curr, res.curr) + eq_(self.advertised, res.advertised) + eq_(self.supported, res.supported) + eq_(self.peer, res.peer) + eq_(self.curr_speed, res.curr_speed) + eq_(self.max_speed, res.max_speed) + + +class TestOFPFeaturesRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPFeaturesRequest """ + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + c = OFPFeaturesRequest(Datapath) + def setUp(self): pass @@ -546,86 +438,118 @@ class TestOFPEchoReply(unittest.TestCase): def test_init(self): pass - def test_parser(self): + def test_serialize(self): + self.c.serialize() - # OFP_HEADER_PACK_STR - # '!BBHI'...version, msg_type, msg_len, xid - version = {'buf': '\x01', 'val': ofproto_v1_2.OFP_VERSION} - msg_type = {'buf': '\x03', 'val': ofproto_v1_2.OFPT_ECHO_REPLY} - msg_len = {'buf': '\x00\x08', - 'val': ofproto_v1_2.OFP_HEADER_SIZE} - xid = {'buf': '\x6e\x21\x3e\x62', 'val': 1847672418} - - data = 'Reply Message.' - - buf = version['buf'] \ - + msg_type['buf'] \ - + msg_len['buf'] \ - + xid['buf'] \ - + data - - res = OFPEchoReply.parser(object, - version['val'], - msg_type['val'], - msg_len['val'], - xid['val'], - buf) - - eq_(version['val'], res.version) - eq_(msg_type['val'], res.msg_type) - eq_(msg_len['val'], res.msg_len) - eq_(xid['val'], res.xid) - eq_(data, res.data) - - # test __str__() - list_ = ('version:', 'msg_type', 'xid') - check = {} - str_ = str(res) - str_ = str_.rsplit() - - i = 0 - for s in str_: - if s in list_: - check[str_[i]] = str_[i + 1] - i += 1 - - # comparison fails in some environment - # such as hex() returns string with suffix 'L' - eq_(hex(version['val']).find(check['version:']), 0) - eq_(hex(msg_type['val']).find(check['msg_type']), 0) - eq_(hex(xid['val']).find(check['xid']), 0) + eq_(ofproto_v1_2.OFP_VERSION, self.c.version) + eq_(ofproto_v1_2.OFPT_FEATURES_REQUEST, self.c.msg_type) + eq_(0, self.c.xid) - def test_serialize(self): - class Datapath(object): - ofproto = ofproto_v1_2 - ofproto_parser = ofproto_v1_2_parser + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR - data = 'Reply Message.' + res = struct.unpack(fmt, str(self.c.buf)) - c = OFPEchoReply(Datapath) - c.data = data + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_FEATURES_REQUEST) + eq_(res[2], len(self.c.buf)) + eq_(res[3], 0) - c.serialize() - eq_(ofproto_v1_2.OFP_VERSION, c.version) - eq_(ofproto_v1_2.OFPT_ECHO_REPLY, c.msg_type) - eq_(0, c.xid) +class TestOFPSwitchFeatures(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPSwitchFeatures + """ - fmt = '!' \ - + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ - + str(len(data)) + 's' + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser - res = struct.unpack(fmt, str(c.buf)) + c = OFPSwitchFeatures(Datapath) - eq_(ofproto_v1_2.OFP_VERSION, res[0]) - eq_(ofproto_v1_2.OFPT_ECHO_REPLY, res[1]) - eq_(len(c.buf), res[2]) - eq_(0, res[3]) - eq_(data, res[4]) + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + pass + + def test_parser(self): + + # OFP_HEADER_PACK_STR + # '!BBHI'...version, msg_type, msg_len, xid + version = ofproto_v1_2.OFP_VERSION + msg_type = ofproto_v1_2.OFPT_FEATURES_REPLY + msg_len = ofproto_v1_2.OFP_SWITCH_FEATURES_SIZE \ + + ofproto_v1_2.OFP_PORT_SIZE + xid = 2495926989 + + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + buf = pack(fmt, version, msg_type, msg_len, xid) + + # OFP_SWITCH_FEATURES_PACK_STR + # '!QIB3xII'...datapath_id, n_buffers, n_tables, + # pad(3), capabilities, reserved + datapath_id = 1270985291017894273 + n_buffers = 2148849654 + n_tables = 228 + capabilities = 1766843586 + reserved = 2013714700 + + fmt = ofproto_v1_2.OFP_SWITCH_FEATURES_PACK_STR + buf += pack(fmt, datapath_id, n_buffers, n_tables, + capabilities, reserved) + + # OFP_PORT_PACK_STR + # '!I4x6s2x16sIIIIIIII'... port_no, pad(4), hw_addr, pad(2), + # name, config, state, curr, advertised, + # peer, curr_speed, max_speed + port_no = 1119692796 + hw_addr = 'hw'.ljust(6) + name = 'name'.ljust(16) + config = 2226555987 + state = 1678244809 + curr = 2850556459 + advertised = 2025421682 + supported = 2120575149 + peer = 2757463021 + curr_speed = 2641353507 + max_speed = 1797291672 + + fmt = ofproto_v1_2.OFP_PORT_PACK_STR + buf += pack(fmt, port_no, hw_addr, name, config, state, curr, + advertised, supported, peer, curr_speed, max_speed) + + res = OFPSwitchFeatures.parser(object, version, msg_type, + msg_len, xid, buf) + + eq_(res.version, version) + eq_(res.msg_type, msg_type) + eq_(res.msg_len, msg_len) + eq_(res.xid, xid) + + eq_(res.datapath_id, datapath_id) + eq_(res.n_buffers, n_buffers) + eq_(res.n_tables, n_tables) + eq_(res.capabilities, capabilities) + eq_(res.reserved, reserved) + + port = res.ports[port_no] + eq_(port.port_no, port_no) + eq_(port.hw_addr, hw_addr) + eq_(port.name, name) + eq_(port.config, config) + eq_(port.state, state) + eq_(port.curr, curr) + eq_(port.advertised, advertised) + eq_(port.supported, supported) + eq_(port.peer, peer) + eq_(port.curr_speed, curr_speed) + eq_(port.max_speed, max_speed) class TestOFPGetConfigRequest(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPGetConfigRequest + """ Test case for ofproto_v1_2_parser.OFPGetConfigRequest """ class Datapath(object): @@ -643,10 +567,6 @@ class TestOFPGetConfigRequest(unittest.TestCase): def test_init(self): pass - def test_parser(self): - # Not used. - pass - def test_serialize(self): self.c.serialize() @@ -658,14 +578,14 @@ class TestOFPGetConfigRequest(unittest.TestCase): res = struct.unpack(fmt, str(self.c.buf)) - eq_(ofproto_v1_2.OFP_VERSION, res[0]) - eq_(ofproto_v1_2.OFPT_GET_CONFIG_REQUEST, res[1]) - eq_(len(self.c.buf), res[2]) - eq_(0, res[3]) + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_GET_CONFIG_REQUEST) + eq_(res[2], len(self.c.buf)) + eq_(res[3], 0) class TestOFPGetConfigReply(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPGetConfigReply + """ Test case for ofproto_v1_2_parser.OFPGetConfigReply """ class Datapath(object): @@ -687,61 +607,81 @@ class TestOFPGetConfigReply(unittest.TestCase): # OFP_HEADER_PACK_STR # '!BBHI'...version, msg_type, msg_len, xid - version = {'buf': '\x01', 'val': ofproto_v1_2.OFP_VERSION} - msg_type = {'buf': '\x0a', 'val': ofproto_v1_2.OFPT_GET_CONFIG_REPLY} - msg_len = {'buf': '\x00\x14', - 'val': ofproto_v1_2.OFP_SWITCH_CONFIG_SIZE} - xid = {'buf': '\x94\xc4\xd2\xcd', 'val': 2495926989} + version = ofproto_v1_2.OFP_VERSION + msg_type = ofproto_v1_2.OFPT_GET_CONFIG_REPLY + msg_len = ofproto_v1_2.OFP_SWITCH_CONFIG_SIZE + xid = 3423224276 + + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + buf = pack(fmt, version, msg_type, msg_len, xid) # OFP_SWITCH_CONFIG_PACK_STR # '!HH'...flags, miss_send_len - flags = {'buf': '\xa0\xe2', 'val': 41186} - miss_send_len = {'buf': '\x36\x0e', 'val': 13838} - - buf = version['buf'] \ - + msg_type['buf'] \ - + msg_len['buf'] \ - + xid['buf'] \ - + flags['buf'] \ - + miss_send_len['buf'] - - res = OFPGetConfigReply.parser(object, - version['val'], - msg_type['val'], - msg_len['val'], - xid['val'], - buf) - - eq_(version['val'], res.version) - eq_(msg_type['val'], res.msg_type) - eq_(msg_len['val'], res.msg_len) - eq_(xid['val'], res.xid) - eq_(flags['val'], res.flags) - eq_(miss_send_len['val'], res.miss_send_len) - - # test __str__() - list_ = ('version:', 'msg_type', 'xid',) - check = {} - str_ = str(res) - str_ = str_.rsplit() - - i = 0 - for s in str_: - if s in list_: - check[str_[i]] = str_[i + 1] - i += 1 - - eq_(hex(version['val']).find(check['version:']), 0) - eq_(hex(msg_type['val']).find(check['msg_type']), 0) - eq_(hex(xid['val']).find(check['xid']), 0) + flags = 41186 + miss_send_len = 13838 - def test_serialize(self): - # Not used. + fmt = ofproto_v1_2.OFP_SWITCH_CONFIG_PACK_STR + buf += pack(fmt, flags, miss_send_len) + + res = OFPGetConfigReply.parser(object, version, msg_type, + msg_len, xid, buf) + + eq_(version, res.version) + eq_(msg_type, res.msg_type) + eq_(msg_len, res.msg_len) + eq_(xid, res.xid) + eq_(flags, res.flags) + eq_(miss_send_len, res.miss_send_len) + + +class TestOFPSetConfig(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPSetConfig + """ + + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + # OFP_SWITCH_CONFIG_PACK_STR + # '!HH'...flags, miss_send_len + flags = 41186 + miss_send_len = 13838 + + c = OFPSetConfig(Datapath, flags, miss_send_len) + + def setUp(self): + pass + + def tearDown(self): pass + def test_init(self): + eq_(self.flags, self.c.flags) + eq_(self.miss_send_len, self.c.miss_send_len) + + def test_serialize(self): + self.c.serialize() + + eq_(ofproto_v1_2.OFP_VERSION, self.c.version) + eq_(ofproto_v1_2.OFPT_SET_CONFIG, self.c.msg_type) + eq_(0, self.c.xid) + + fmt = '!' \ + + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_SWITCH_CONFIG_PACK_STR.replace('!', '') + + res = struct.unpack(fmt, str(self.c.buf)) + + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_SET_CONFIG) + eq_(res[2], len(self.c.buf)) + eq_(res[3], 0) + eq_(res[4], self.flags) + eq_(res[5], self.miss_send_len) + class TestOFPPacketIn(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPPacketIn + """ Test case for ofproto_v1_2_parser.OFPPacketIn """ class Datapath(object): @@ -760,78 +700,54 @@ class TestOFPPacketIn(unittest.TestCase): pass def test_parser(self): - # OFP_HEADER_PACK_STR # '!BBHI'...version, msg_type, msg_len, xid - version = {'buf': '\x01', 'val': ofproto_v1_2.OFP_VERSION} - msg_type = {'buf': '\x0a', 'val': ofproto_v1_2.OFPT_PACKET_IN} - msg_len = {'buf': '\x00\x14', 'val': ofproto_v1_2.OFP_PACKET_IN_SIZE} - xid = {'buf': '\xd0\x23\x8c\x34', 'val': 3491990580} + version = ofproto_v1_2.OFP_VERSION + msg_type = ofproto_v1_2.OFPT_PACKET_IN + msg_len = ofproto_v1_2.OFP_PACKET_IN_SIZE + xid = 3423224276 - buf = version['buf'] \ - + msg_type['buf'] \ - + msg_len['buf'] \ - + xid['buf'] + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + buf = pack(fmt, version, msg_type, msg_len, xid) - # OFP_PACKET_IN_PACK_STR v1.2 + # OFP_PACKET_IN_PACK_STR # '!IHHBB'...buffer_id, total_len, reason, table_id - buffer_id = {'buf': '\xae\x73\x90\xec', 'val': 2926809324} - total_len = {'buf': '\x00\x10', 'val': 16} - reason = {'buf': '\x43', 'val': 67} - table_id = {'buf': '\x03', 'val': 3} - - buf += buffer_id['buf'] \ - + total_len['buf'] \ - + reason['buf'] \ - + table_id['buf'] - - # OFP_MATCH_PACK_STR v1.2 - # '!HHBBBB'...type, length, oxm_fields[4] - type = {'buf': '\x00\x01', 'val': 1} - length = {'buf': '\x00\x04', 'val': 4} - oxm_fields = [] - oxm_fields.append({'buf': '\x79', 'val': 121}) - oxm_fields.append({'buf': '\x7a', 'val': 122}) - oxm_fields.append({'buf': '\x7b', 'val': 123}) - oxm_fields.append({'buf': '\x7c', 'val': 124}) - - buf += type['buf'] \ - + length['buf'] \ - + oxm_fields[0]['buf'] \ - + oxm_fields[1]['buf'] \ - + oxm_fields[2]['buf'] \ - + oxm_fields[3]['buf'] - - res = OFPPacketIn.parser(object, - version['val'], - msg_type['val'], - msg_len['val'], - xid['val'], - buf) + buffer_id = 2926809324 + total_len = ofproto_v1_2.OFP_MATCH_SIZE + reason = 1 + table_id = 3 - # OFP_HEADER_PACK_STR - eq_(version['val'], res.version) - eq_(msg_type['val'], res.msg_type) - eq_(msg_len['val'], res.msg_len) - eq_(xid['val'], res.xid) - - # OFP_PACKET_IN_PACK_STR - eq_(buffer_id['val'], res.buffer_id) - eq_(total_len['val'], res.total_len) - eq_(reason['val'], res.reason) - eq_(table_id['val'], res.table_id) + fmt = ofproto_v1_2.OFP_PACKET_IN_PACK_STR + buf += pack(fmt, buffer_id, total_len, reason, table_id) # OFP_MATCH_PACK_STR - eq_(type['val'], res.match.type) - eq_(length['val'], res.match.length) + match = OFPMatch() + buf_match = bytearray() + match.serialize(buf_match, 0) - def test_serialize(self): - # Not used. - pass + buf += str(buf_match) + + # the last 2x is for ofp_packet_in::data + data = 'data'.ljust(16) + + buf += pack('2x16s', data) + + res = OFPPacketIn.parser(object, version, msg_type, msg_len, + xid, buf) + + eq_(version, res.version) + eq_(msg_type, res.msg_type) + eq_(msg_len, res.msg_len) + eq_(xid, res.xid) + eq_(buffer_id, res.buffer_id) + eq_(total_len, res.total_len) + eq_(reason, res.reason) + eq_(table_id, res.table_id) + eq_(data[:8], res.data) class TestOFPFlowRemoved(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPFlowRemoved + """ Test case for ofproto_v1_2_parser.OFPFlowRemoved """ class Datapath(object): @@ -850,103 +766,64 @@ class TestOFPFlowRemoved(unittest.TestCase): pass def test_parser(self): - # OFP_HEADER_PACK_STR # '!BBHI'...version, msg_type, msg_len, xid - version = {'buf': '\x01', 'val': ofproto_v1_2.OFP_VERSION} - msg_type = {'buf': '\x0a', 'val': ofproto_v1_2.OFPT_FLOW_REMOVED} - msg_len = {'buf': '\x00\x14', - 'val': ofproto_v1_2.OFP_FLOW_REMOVED_SIZE} - xid = {'buf': '\x94\xc4\xd2\xcd', 'val': 2495926989} - - buf = version['buf'] \ - + msg_type['buf'] \ - + msg_len['buf'] \ - + xid['buf'] - - # OFP_FLOW_REMOVED_PACK_STR0 v1.2 + version = ofproto_v1_2.OFP_VERSION + msg_type = ofproto_v1_2.OFPT_FLOW_REMOVED + msg_len = ofproto_v1_2.OFP_FLOW_REMOVED_SIZE + xid = 3423224276 + + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + buf = pack(fmt, version, msg_type, msg_len, xid) + + # OFP_FLOW_REMOVED_PACK_STR0 # '!QHBBIIHHQQ' ...cookie, priority, reason, table_id, # duration_sec, duration_nsec, idle_timeout, # hard_timeout, packet_count, byte_count - cookie = {'buf': '\x02\x79\xba\x00\xef\xab\xee\x44', - 'val': 178378173441633860} - priority = {'buf': '\x02\xce', 'val': 718} - reason = {'buf': '\x01', 'val': 1} - table_id = {'buf': '\xa9', 'val': 169} - duration_sec = {'buf': '\x86\x24\xa3\xba', 'val': 2250548154} - duration_nsec = {'buf': '\x94\x94\xc2\x23', 'val': 2492776995} - idle_timeout = {'buf': '\xeb\x7c', 'val': 60284} - hard_timeout = {'buf': '\xeb\x7d', 'val': 60285} - packet_count = {'buf': '\x5a\x0d\xf2\x03\x8e\x0a\xbb\x8d', - 'val': 6489108735192644493} - byte_count = {'buf': '\x65\xc8\xd3\x72\x51\xb5\xbb\x7c', - 'val': 7334344481123449724} - - buf += cookie['buf'] \ - + priority['buf'] \ - + reason['buf'] \ - + table_id['buf'] \ - + duration_sec['buf'] \ - + duration_nsec['buf'] \ - + idle_timeout['buf'] \ - + hard_timeout['buf'] \ - + packet_count['buf'] \ - + byte_count['buf'] - - # OFP_MATCH_PACK_STR v1.2 - # '!HHBBBB'...type, length, oxm_fields[4] - type = {'buf': '\x00\x01', 'val': 1} - length = {'buf': '\x00\x04', 'val': 4} - oxm_fields = [] - oxm_fields.append({'buf': '\x79', 'val': 121}) - oxm_fields.append({'buf': '\x7a', 'val': 122}) - oxm_fields.append({'buf': '\x7b', 'val': 123}) - oxm_fields.append({'buf': '\x7c', 'val': 124}) - - buf += type['buf'] \ - + length['buf'] \ - + oxm_fields[0]['buf'] \ - + oxm_fields[1]['buf'] \ - + oxm_fields[2]['buf'] \ - + oxm_fields[2]['buf'] \ - + oxm_fields[3]['buf'] - - res = OFPFlowRemoved.parser(object, - version['val'], - msg_type['val'], - msg_len['val'], - xid['val'], - buf) + cookie = 178378173441633860 + priority = 718 + reason = 1 + table_id = 169 + duration_sec = 2250548154 + duration_nsec = 2492776995 + idle_timeout = 60284 + hard_timeout = 60285 + packet_count = 6489108735192644493 + byte_count = 7334344481123449724 + + fmt = ofproto_v1_2.OFP_FLOW_REMOVED_PACK_STR0 + buf += pack(fmt, cookie, priority, reason, table_id, + duration_sec, duration_nsec, idle_timeout, + hard_timeout, packet_count, byte_count) - # OFP_HEADER_PACK_STR - eq_(version['val'], res.version) - eq_(msg_type['val'], res.msg_type) - eq_(msg_len['val'], res.msg_len) - eq_(xid['val'], res.xid) + # OFP_MATCH_PACK_STR + match = OFPMatch() + buf_match = bytearray() + match.serialize(buf_match, 0) - # OFP_FLOW_REMOVED_PACK_STR0 - eq_(cookie['val'], res.cookie) - eq_(priority['val'], res.priority) - eq_(reason['val'], res.reason) - eq_(table_id['val'], res.table_id) - eq_(duration_sec['val'], res.duration_sec) - eq_(duration_nsec['val'], res.duration_nsec) - eq_(idle_timeout['val'], res.idle_timeout) - eq_(hard_timeout['val'], res.hard_timeout) - eq_(packet_count['val'], res.packet_count) - eq_(byte_count['val'], res.byte_count) + buf += str(buf_match) - # OFP_MATCH_PACK_STR - eq_(type['val'], res.match.type) - eq_(length['val'], res.match.length) + res = OFPFlowRemoved.parser(object, version, msg_type, + msg_len, xid, buf) - def test_serialize(self): - # Not used. - pass + eq_(version, res.version) + eq_(msg_type, res.msg_type) + eq_(msg_len, res.msg_len) + eq_(xid, res.xid) + eq_(cookie, res.cookie) + eq_(priority, res.priority) + eq_(reason, res.reason) + eq_(table_id, res.table_id) + eq_(duration_sec, res.duration_sec) + eq_(duration_nsec, res.duration_nsec) + eq_(idle_timeout, res.idle_timeout) + eq_(hard_timeout, res.hard_timeout) + eq_(packet_count, res.packet_count) + eq_(byte_count, res.byte_count) class TestOFPPortStatus(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPPortStatus + """ Test case for ofproto_v1_2_parser.OFPPortStatus """ def setUp(self): @@ -959,190 +836,149 @@ class TestOFPPortStatus(unittest.TestCase): pass def test_parser(self): - # OFP_HEADER_PACK_STR # '!BBHI'...version, msg_type, msg_len, xid - version = {'buf': '\x03', 'val': ofproto_v1_2.OFP_VERSION} - msg_type = {'buf': '\x0c', 'val': ofproto_v1_2.OFPT_PORT_STATUS} - msg_len = {'buf': '\x00\x50', 'val': 80} - xid = {'buf': '\xcc\x0a\x41\xd4', 'val': 3423224276} + version = ofproto_v1_2.OFP_VERSION + msg_type = ofproto_v1_2.OFPT_PORT_STATUS + msg_len = ofproto_v1_2.OFP_PORT_STATUS_SIZE + xid = 3423224276 - buf = version['buf'] \ - + msg_type['buf'] \ - + msg_len['buf'] \ - + xid['buf'] + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + buf = pack(fmt, version, msg_type, msg_len, xid) - # OFP_PORT_STATUS_PACK_STR + # OFP_PORT_STATUS_PACK_STR = '!B7x' + _OFP_PORT_PACK_STR # '!B7x'...reason, pad(7) - reason = {'buf': '\x00', 'val': 0} - - buf += reason['buf'] \ - + pack('7x') + reason = 0 # OFP_PORT_PACK_STR # '!I4x6s2x16sIIIIIIII'... port_no, pad(4), hw_addr, pad(2), # name, config, state, curr, advertised, # peer, curr_speed, max_speed - port_no = {'buf': '\x42\xbd\x27\xfc', 'val': 1119692796} + port_no = 1119692796 hw_addr = 'hw'.ljust(6) name = 'name'.ljust(16) - config = {'buf': '\x84\xb6\x8c\x53', 'val': 2226555987} - state = {'buf': '\x64\x07\xfb\xc9', 'val': 1678244809} - curr = {'buf': '\xa9\xe8\x0a\x2b', 'val': 2850556459} - advertised = {'buf': '\x78\xb9\x7b\x72', 'val': 2025421682} - supported = {'buf': '\x7e\x65\x68\xad', 'val': 2120575149} - peer = {'buf': '\xa4\x5b\x8b\xed', 'val': 2757463021} - curr_speed = {'buf': '\x9d\x6f\xdb\x23', 'val': 2641353507} - max_speed = {'buf': '\x6b\x20\x7e\x98', 'val': 1797291672} - - buf += port_no['buf'] \ - + pack('4x') \ - + hw_addr \ - + pack('2x') \ - + name \ - + config['buf'] \ - + state['buf'] \ - + curr['buf'] \ - + advertised['buf'] \ - + supported['buf'] \ - + peer['buf'] \ - + curr_speed['buf'] \ - + max_speed['buf'] - - res = OFPPortStatus.parser(object, - version['val'], - msg_type['val'], - msg_len['val'], - xid['val'], - buf) - - # OFP_PORT_STATUS_PACK_STR - eq_(reason['val'], res.reason) - - # OFP_PORT_PACK_STR - eq_(port_no['val'], res.desc.port_no) + config = 2226555987 + state = 1678244809 + curr = 2850556459 + advertised = 2025421682 + supported = 2120575149 + peer = 2757463021 + curr_speed = 2641353507 + max_speed = 1797291672 + + fmt = ofproto_v1_2.OFP_PORT_STATUS_PACK_STR + buf += pack(fmt, reason, + port_no, hw_addr, name, config, state, curr, + advertised, supported, peer, curr_speed, max_speed) + + res = OFPPortStatus.parser(object, version, msg_type, msg_len, + xid, buf) + + eq_(reason, res.reason) + eq_(port_no, res.desc.port_no) eq_(hw_addr, res.desc.hw_addr) eq_(name, res.desc.name) - eq_(config['val'], res.desc.config) - eq_(state['val'], res.desc.state) - eq_(curr['val'], res.desc.curr) - eq_(advertised['val'], res.desc.advertised) - eq_(supported['val'], res.desc.supported) - eq_(peer['val'], res.desc.peer) - eq_(curr_speed['val'], res.desc.curr_speed) - eq_(max_speed['val'], res.desc.max_speed) - - def test_serialize(self): - # Not used. - pass + eq_(config, res.desc.config) + eq_(state, res.desc.state) + eq_(curr, res.desc.curr) + eq_(advertised, res.desc.advertised) + eq_(supported, res.desc.supported) + eq_(peer, res.desc.peer) + eq_(curr_speed, res.desc.curr_speed) + eq_(max_speed, res.desc.max_speed) class TestOFPPacketOut(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPPacketOut + """ Test case for ofproto_v1_2_parser.OFPPacketOut """ + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + # OFP_PACKET_OUT_PACK_STR = '!IIH6x' + buffer_id = 0xffffffff + in_port = 0x00040455 + data = 'Message' + + # OFP_ACTION (OFP_ACTION_OUTPUT) port = 0x00002ae0 actions = [OFPActionOutput(port, 0)] + c = OFPPacketOut(Datapath, buffer_id, in_port, actions, data) + def setUp(self): pass def tearDown(self): pass - def _get_obj(self, buffer_id, in_port, data=None): - class Datapath(object): - ofproto = ofproto_v1_2 - ofproto_parser = ofproto_v1_2_parser - - c = OFPPacketOut(Datapath, - buffer_id, - in_port, - self.actions, - data) - return c - def test_init(self): - buffer_id = 0xffffffff - in_port = 0x00040455 - data = 'Message' - - c = self._get_obj(buffer_id, in_port, data) - - eq_(buffer_id, c.buffer_id) - eq_(in_port, c.in_port) - eq_(data, c.data) - - def test_parser(self): - # Not used. - pass + eq_(self.buffer_id, self.c.buffer_id) + eq_(self.in_port, self.c.in_port) + eq_(self.data, self.c.data) + eq_(self.actions, self.c.actions) def test_serialize(self): - buffer_id = 0xffffffff - in_port = 0x10009e07 - data = 'Message' - - c = self._get_obj(buffer_id, in_port, data) - c.serialize() + self.c.serialize() - eq_(ofproto_v1_2.OFP_VERSION, c.version) - eq_(ofproto_v1_2.OFPT_PACKET_OUT, c.msg_type) - eq_(0, c.xid) + eq_(ofproto_v1_2.OFP_VERSION, self.c.version) + eq_(ofproto_v1_2.OFPT_PACKET_OUT, self.c.msg_type) + eq_(0, self.c.xid) fmt = '!' \ + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ + ofproto_v1_2.OFP_PACKET_OUT_PACK_STR.replace('!', '') \ + ofproto_v1_2.OFP_ACTION_OUTPUT_PACK_STR.replace('!', '') \ - + str(len(data)) + 's' - - res = struct.unpack(fmt, str(c.buf)) - - # OFP_HEADER_PACK_STR - eq_(ofproto_v1_2.OFP_VERSION, res[0]) - eq_(ofproto_v1_2.OFPT_PACKET_OUT, res[1]) - eq_(len(c.buf), res[2]) - eq_(0, res[3]) + + str(len(self.data)) + 's' - # OFP_PACKET_OUT_PACK_STR - eq_(buffer_id, res[4]) - eq_(in_port, res[5]) - eq_(ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE, res[6]) - - # OFP_ACTION_OUTPUT_PACK_STR - eq_(ofproto_v1_2.OFPAT_OUTPUT, res[7]) - eq_(ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE, res[8]) - eq_(self.port, res[9]) - eq_(0, res[10]) + res = struct.unpack(fmt, str(self.c.buf)) - # data - eq_(data, res[11]) + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_PACKET_OUT) + eq_(res[2], len(self.c.buf)) + eq_(res[3], 0) + eq_(res[4], self.buffer_id) + eq_(res[5], self.in_port) + eq_(res[6], ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE) + eq_(res[7], ofproto_v1_2.OFPAT_OUTPUT) + eq_(res[8], ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE) + eq_(res[9], self.port) + eq_(res[10], 0) + eq_(res[11], self.data) class TestOFPFlowMod(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPFlowMod + """ Test case for ofproto_v1_2_parser.OFPFlowMod """ - # OFP_FLOW_MOD_PACK_STR0 v1.2 + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + # OFP_FLOW_MOD_PACK_STR0 # '!QQBBHHHIIIH2x'...cookie, cookie_mask, table_id, command, # idle_timeout, hard_timeout, priority, buffer_id, # out_port, out_group, flags - cookie = {'buf': '\x1d\x86\xce\x6e\x8d\xc0\xbe\xa8', - 'val': 2127614848199081640} - cookie_mask = {'buf': '\x1d\x86\xce\x6e\x8d\xc0\xbe\xa9', - 'val': 2127614848199081641} - table_id = {'buf': '\x03', 'val': 3} - command = {'buf': '\x00', 'val': 0} - idle_timeout = {'buf': '\xf3\x6d', 'val': 62317} - hard_timeout = {'buf': '\x1c\xc5', 'val': 7365} - priority = {'buf': '\x9c\xe3', 'val': 40163} - buffer_id = {'buf': '\xf0\xa1\x80\x33', 'val': 4037115955} - out_port = {'buf': '\x00\x00\xfe\x0d', 'val': 65037} - out_group = {'buf': '\x00\x00\x19\xce', 'val': 6606} - flags = {'buf': '\x00\x87', 'val': 135} + cookie = 2127614848199081640 + cookie_mask = 2127614848199081641 + table_id = 3 + command = 0 + idle_timeout = 62317 + hard_timeout = 7365 + priority = 40163 + buffer_id = 4037115955 + out_port = 65037 + out_group = 6606 + flags = 135 match = OFPMatch() - instructions = [OFPInstructionGotoTable(table_id['val'])] + instructions = [OFPInstructionGotoTable(table_id)] + + c = OFPFlowMod(Datapath, cookie, cookie_mask, table_id, command, + idle_timeout, hard_timeout, priority, buffer_id, + out_port, out_group, flags, match, instructions) def setUp(self): pass @@ -1150,98 +986,216 @@ class TestOFPFlowMod(unittest.TestCase): def tearDown(self): pass - def _get_obj(self): + def test_init(self): + eq_(self.cookie, self.c.cookie) + eq_(self.cookie_mask, self.c.cookie_mask) + eq_(self.table_id, self.c.table_id) + eq_(self.command, self.c.command) + eq_(self.idle_timeout, self.c.idle_timeout) + eq_(self.hard_timeout, self.c.hard_timeout) + eq_(self.priority, self.c.priority) + eq_(self.buffer_id, self.c.buffer_id) + eq_(self.out_port, self.c.out_port) + eq_(self.out_group, self.c.out_group) + eq_(self.flags, self.c.flags) + eq_(self.match, self.c.match) + eq_(self.instructions[0], self.c.instructions[0]) - class Datapath(object): - ofproto = ofproto_v1_2 - ofproto_parser = ofproto_v1_2_parser + def test_serialize(self): + self.c.serialize() - c = OFPFlowMod(Datapath, - self.cookie['val'], - self.cookie_mask['val'], - self.table_id['val'], - self.command['val'], - self.idle_timeout['val'], - self.hard_timeout['val'], - self.priority['val'], - self.buffer_id['val'], - self.out_port['val'], - self.out_group['val'], - self.flags['val'], - self.match, - self.instructions) - - return c - - def test_init(self): - c = self._get_obj() - - eq_(self.cookie['val'], c.cookie) - eq_(self.cookie_mask['val'], c.cookie_mask) - eq_(self.table_id['val'], c.table_id) - eq_(self.command['val'], c.command) - eq_(self.idle_timeout['val'], c.idle_timeout) - eq_(self.hard_timeout['val'], c.hard_timeout) - eq_(self.priority['val'], c.priority) - eq_(self.buffer_id['val'], c.buffer_id) - eq_(self.out_port['val'], c.out_port) - eq_(self.out_group['val'], c.out_group) - eq_(self.flags['val'], c.flags) - eq_(self.match, c.match) - eq_(self.instructions[0], c.instructions[0]) + eq_(ofproto_v1_2.OFP_VERSION, self.c.version) + eq_(ofproto_v1_2.OFPT_FLOW_MOD, self.c.msg_type) + eq_(0, self.c.xid) + + fmt = '!' \ + + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_FLOW_MOD_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_INSTRUCTION_GOTO_TABLE_PACK_STR.replace('!', '') + + res = struct.unpack(fmt, str(self.c.buf)) + + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_FLOW_MOD) + eq_(res[2], len(self.c.buf)) + eq_(res[3], 0) + eq_(res[4], self.cookie) + eq_(res[5], self.cookie_mask) + eq_(res[6], self.table_id) + eq_(res[7], self.command) + eq_(res[8], self.idle_timeout) + eq_(res[9], self.hard_timeout) + eq_(res[10], self.priority) + eq_(res[11], self.buffer_id) + eq_(res[12], self.out_port) + eq_(res[13], self.out_group) + eq_(res[14], self.flags) + + +class TestOFPInstructionGotoTable(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPInstructionGotoTable + """ + + # OFP_INSTRUCTION_GOTO_TABLE_PACK_STR + # '!HHB3x'...type, len, table_id, pad(3) + type_ = ofproto_v1_2.OFPIT_GOTO_TABLE + len_ = ofproto_v1_2.OFP_INSTRUCTION_GOTO_TABLE_SIZE + table_id = 3 + + fmt = ofproto_v1_2.OFP_INSTRUCTION_GOTO_TABLE_PACK_STR + buf = pack(fmt, type_, len_, table_id) + + c = OFPInstructionGotoTable(table_id) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.table_id, self.c.table_id) def test_parser(self): - # Not used. + res = self.c.parser(self.buf, 0) + + eq_(res.len, self.len_) + eq_(res.type, self.type_) + eq_(res.table_id, self.table_id) + + def test_serialize(self): + buf = bytearray() + self.c.serialize(buf, 0) + + fmt = ofproto_v1_2.OFP_INSTRUCTION_GOTO_TABLE_PACK_STR + res = struct.unpack(fmt, buffer(buf)) + + eq_(res[0], self.type_) + eq_(res[1], self.len_) + eq_(res[2], self.table_id) + + +class TestOFPInstructionWriteMetadata(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPInstructionWriteMetadata + """ + + # OFP_INSTRUCTION_WRITE_METADATA_PACK_STR + # '!HH4xQQ'...type, len, pad(4), metadata, metadata_mask + type_ = ofproto_v1_2.OFPIT_WRITE_METADATA + len_ = ofproto_v1_2.OFP_INSTRUCTION_WRITE_METADATA_SIZE + metadata = 2127614848199081640 + metadata_mask = 2127614848199081641 + + fmt = ofproto_v1_2.OFP_INSTRUCTION_WRITE_METADATA_PACK_STR + buf = pack(fmt, type_, len_, metadata, metadata_mask) + + c = OFPInstructionWriteMetadata(metadata, metadata_mask) + + def setUp(self): + pass + + def tearDown(self): pass + def test_init(self): + eq_(self.metadata, self.c.metadata) + eq_(self.metadata_mask, self.c.metadata_mask) + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(res.len, self.len_) + eq_(res.type, self.type_) + eq_(res.metadata, self.metadata) + eq_(res.metadata_mask, self.metadata_mask) + def test_serialize(self): - c = self._get_obj() - c.serialize() + buf = bytearray() + self.c.serialize(buf, 0) - eq_(ofproto_v1_2.OFP_VERSION, c.version) - eq_(ofproto_v1_2.OFPT_FLOW_MOD, c.msg_type) - eq_(0, c.xid) + fmt = ofproto_v1_2.OFP_INSTRUCTION_WRITE_METADATA_PACK_STR + res = struct.unpack(fmt, buffer(buf)) + + eq_(res[0], self.type_) + eq_(res[1], self.len_) + eq_(res[2], self.metadata) + eq_(res[3], self.metadata_mask) + + +class TestOFPInstructionActions(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPInstructionActions + """ + # OFP_INSTRUCTION_ACTIONS_PACK_STR + # '!HH4x'...type, len, pad(4) + type_ = ofproto_v1_2.OFPIT_WRITE_ACTIONS + len_ = ofproto_v1_2.OFP_INSTRUCTION_ACTIONS_SIZE \ + + ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE + + fmt = ofproto_v1_2.OFP_INSTRUCTION_ACTIONS_PACK_STR + buf = pack(fmt, type_, len_) + + # OFP_ACTION (OFP_ACTION_OUTPUT) + port = 0x00002ae0 + max_len = ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE + actions = [OFPActionOutput(port, max_len)] + buf_actions = bytearray() + actions[0].serialize(buf_actions, 0) + + buf += str(buf_actions) + + c = OFPInstructionActions(type_, actions) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.actions, self.c.actions) + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(res.len, self.len_) + eq_(res.type, self.type_) + + eq_(res.actions[0].type, self.actions[0].type) + eq_(res.actions[0].len, self.actions[0].len) + eq_(res.actions[0].port, self.actions[0].port) + eq_(res.actions[0].max_len, self.actions[0].max_len) + + def test_serialize(self): + buf = bytearray() + self.c.serialize(buf, 0) fmt = '!' \ - + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ - + ofproto_v1_2.OFP_FLOW_MOD_PACK_STR.replace('!', '') \ - + ofproto_v1_2.OFP_INSTRUCTION_GOTO_TABLE_PACK_STR.replace('!', '') - res = struct.unpack(fmt, str(c.buf)) + + ofproto_v1_2.OFP_INSTRUCTION_ACTIONS_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_ACTION_OUTPUT_PACK_STR.replace('!', '') - # OFP_HEADER_PACK_STR - eq_(ofproto_v1_2.OFP_VERSION, res[0]) - eq_(ofproto_v1_2.OFPT_FLOW_MOD, res[1]) - eq_(len(c.buf), res[2]) - eq_(0, res[3]) + res = struct.unpack(fmt, buffer(buf)) - # OFP_FLOW_MOD_PACK_STR0 - eq_(self.cookie['val'], res[4]) - eq_(self.cookie_mask['val'], res[5]) - eq_(self.table_id['val'], res[6]) - eq_(self.command['val'], res[7]) - eq_(self.idle_timeout['val'], res[8]) - eq_(self.hard_timeout['val'], res[9]) - eq_(self.priority['val'], res[10]) - eq_(self.buffer_id['val'], res[11]) - eq_(self.out_port['val'], res[12]) - eq_(self.out_group['val'], res[13]) - eq_(self.flags['val'], res[14]) + eq_(res[0], self.type_) + eq_(res[1], self.len_) + eq_(res[2], self.actions[0].type) + eq_(res[3], self.actions[0].len) + eq_(res[4], self.actions[0].port) + eq_(res[5], self.actions[0].max_len) class TestOFPActionHeader(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPActionHeader + """ Test case for ofproto_v1_2_parser.OFPActionHeader """ # OFP_ACTION_HEADER_PACK_STR # '!HH4x'...type, len, pad(4) - type = {'buf': '\x00\x02', 'val': 2} - len = {'buf': '\x00\x08', 'val': ofproto_v1_2.OFP_ACTION_HEADER_SIZE} + type_ = ofproto_v1_2.OFPAT_OUTPUT + len_ = ofproto_v1_2.OFP_ACTION_HEADER_SIZE - buf = type['buf'] \ - + len['buf'] \ - + pack('4x') + fmt = ofproto_v1_2.OFP_ACTION_HEADER_PACK_STR + buf = pack(fmt, type_, len_) - c = OFPActionHeader(type['val'], len['val']) + c = OFPActionHeader(type_, len_) def setUp(self): pass @@ -1250,8 +1204,8 @@ class TestOFPActionHeader(unittest.TestCase): pass def test_init(self): - eq_(self.type['val'], self.c.type) - eq_(self.len['val'], self.c.len) + eq_(self.type_, self.c.type) + eq_(self.len_, self.c.len) def test_serialize(self): buf = bytearray() @@ -1260,28 +1214,25 @@ class TestOFPActionHeader(unittest.TestCase): fmt = ofproto_v1_2.OFP_ACTION_HEADER_PACK_STR res = struct.unpack(fmt, buffer(buf)) - eq_(self.type['val'], res[0]) - eq_(self.len['val'], res[1]) + eq_(res[0], self.type_) + eq_(res[1], self.len_) class TestOFPActionOutput(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPActionOutput + """ Test case for ofproto_v1_2_parser.OFPActionOutput """ - # OFP_ACTION_OUTPUT_PACK_STR v1.2 + # OFP_ACTION_OUTPUT_PACK_STR # '!HHIH6x'...type, len, port, max_len, pad(6) - type_ = {'buf': '\x00\x00', 'val': ofproto_v1_2.OFPAT_OUTPUT} - len_ = {'buf': '\x00\x10', 'val': ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE} - port = {'buf': '\x00\x00\x19\xce', 'val': 6606} - max_len = {'buf': '\x00\x10', 'val': ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE} + type_ = ofproto_v1_2.OFPAT_OUTPUT + len_ = ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE + port = 6606 + max_len = ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE - buf = type_['buf'] \ - + len_['buf'] \ - + port['buf'] \ - + max_len['buf'] \ - + pack('6x') + fmt = ofproto_v1_2.OFP_ACTION_OUTPUT_PACK_STR + buf = pack(fmt, type_, len_, port, max_len) - c = OFPActionOutput(port['val'], max_len['val']) + c = OFPActionOutput(port, max_len) def setUp(self): pass @@ -1290,14 +1241,16 @@ class TestOFPActionOutput(unittest.TestCase): pass def test_init(self): - eq_(self.port['val'], self.c.port) - eq_(self.max_len['val'], self.c.max_len) + eq_(self.port, self.c.port) + eq_(self.max_len, self.c.max_len) def test_parser(self): res = self.c.parser(self.buf, 0) - eq_(self.port['val'], res.port) - eq_(self.max_len['val'], res.max_len) + eq_(res.len, self.len_) + eq_(res.type, self.type_) + eq_(res.port, self.port) + eq_(res.max_len, self.max_len) def test_serialize(self): buf = bytearray() @@ -1306,27 +1259,26 @@ class TestOFPActionOutput(unittest.TestCase): fmt = ofproto_v1_2.OFP_ACTION_OUTPUT_PACK_STR res = struct.unpack(fmt, buffer(buf)) - eq_(self.type_['val'], res[0]) - eq_(self.len_['val'], res[1]) - eq_(self.port['val'], res[2]) - eq_(self.max_len['val'], res[3]) + eq_(res[0], self.type_) + eq_(res[1], self.len_) + eq_(res[2], self.port) + eq_(res[3], self.max_len) class TestOFPActionGroup(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPActionGroup + """ Test case for ofproto_v1_2_parser.OFPActionGroup """ - # OFP_ACTION_GROUP_PACK_STR v1.2 + # OFP_ACTION_GROUP_PACK_STR # '!HHI'...type, len, group_id - type_ = {'buf': '\x00\x16', 'val': ofproto_v1_2.OFPAT_GROUP} - len_ = {'buf': '\x00\x08', 'val': ofproto_v1_2.OFP_ACTION_GROUP_SIZE} - group_id = {'buf': '\x00\x00\x19\xce', 'val': 6606} + type_ = ofproto_v1_2.OFPAT_GROUP + len_ = ofproto_v1_2.OFP_ACTION_GROUP_SIZE + group_id = 6606 - buf = type_['buf'] \ - + len_['buf'] \ - + group_id['buf'] + fmt = ofproto_v1_2.OFP_ACTION_GROUP_PACK_STR + buf = pack(fmt, type_, len_, group_id) - c = OFPActionGroup(group_id['val']) + c = OFPActionGroup(group_id) def setUp(self): pass @@ -1335,12 +1287,14 @@ class TestOFPActionGroup(unittest.TestCase): pass def test_init(self): - eq_(self.group_id['val'], self.c.group_id) + eq_(self.group_id, self.c.group_id) def test_parser(self): res = self.c.parser(self.buf, 0) - eq_(self.group_id['val'], res.group_id) + eq_(res.len, self.len_) + eq_(res.type, self.type_) + eq_(res.group_id, self.group_id) def test_serialize(self): buf = bytearray() @@ -1349,26 +1303,25 @@ class TestOFPActionGroup(unittest.TestCase): fmt = ofproto_v1_2.OFP_ACTION_GROUP_PACK_STR res = struct.unpack(fmt, buffer(buf)) - eq_(self.type_['val'], res[0]) - eq_(self.len_['val'], res[1]) - eq_(self.group_id['val'], res[2]) + eq_(res[0], self.type_) + eq_(res[1], self.len_) + eq_(res[2], self.group_id) class TestOFPActionSetQueue(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPActionSetQueue + """ Test case for ofproto_v1_2_parser.OFPActionSetQueue """ - # OFP_ACTION_SET_QUEUE_PACK_STR v1.2 + # OFP_ACTION_SET_QUEUE_PACK_STR # '!HHI'...type, len, queue_id - type_ = {'buf': '\x00\x15', 'val': ofproto_v1_2.OFPAT_SET_QUEUE} - len_ = {'buf': '\x00\x08', 'val': ofproto_v1_2.OFP_ACTION_SET_QUEUE_SIZE} - queue_id = {'buf': '\x00\x00\x19\xce', 'val': 6606} + type_ = ofproto_v1_2.OFPAT_SET_QUEUE + len_ = ofproto_v1_2.OFP_ACTION_SET_QUEUE_SIZE + queue_id = 6606 - buf = type_['buf'] \ - + len_['buf'] \ - + queue_id['buf'] + fmt = ofproto_v1_2.OFP_ACTION_SET_QUEUE_PACK_STR + buf = pack(fmt, type_, len_, queue_id) - c = OFPActionSetQueue(queue_id['val']) + c = OFPActionSetQueue(queue_id) def setUp(self): pass @@ -1377,12 +1330,14 @@ class TestOFPActionSetQueue(unittest.TestCase): pass def test_init(self): - eq_(self.queue_id['val'], self.c.queue_id) + eq_(self.queue_id, self.c.queue_id) def test_parser(self): res = self.c.parser(self.buf, 0) - eq_(self.queue_id['val'], res.queue_id) + eq_(res.len, self.len_) + eq_(res.type, self.type_) + eq_(res.queue_id, self.queue_id) def test_serialize(self): buf = bytearray() @@ -1391,27 +1346,25 @@ class TestOFPActionSetQueue(unittest.TestCase): fmt = ofproto_v1_2.OFP_ACTION_SET_QUEUE_PACK_STR res = struct.unpack(fmt, buffer(buf)) - eq_(self.type_['val'], res[0]) - eq_(self.len_['val'], res[1]) - eq_(self.queue_id['val'], res[2]) + eq_(res[0], self.type_) + eq_(res[1], self.len_) + eq_(res[2], self.queue_id) class TestOFPActionSetMplsTtl(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPActionSetMplsTtl + """ Test case for ofproto_v1_2_parser.OFPActionSetMplsTtl """ - # OFP_ACTION_MPLS_TTL_PACK_STR v1.2 + # OFP_ACTION_MPLS_TTL_PACK_STR # '!HHB3x'...type, len, mpls_ttl, pad(3) - type_ = {'buf': '\x00\x0f', 'val': ofproto_v1_2.OFPAT_SET_MPLS_TTL} - len_ = {'buf': '\x00\x08', 'val': ofproto_v1_2.OFP_ACTION_MPLS_TTL_SIZE} - mpls_ttl = {'buf': '\xfe', 'val': 254} + type_ = ofproto_v1_2.OFPAT_SET_MPLS_TTL + len_ = ofproto_v1_2.OFP_ACTION_MPLS_TTL_SIZE + mpls_ttl = 254 - buf = type_['buf'] \ - + len_['buf'] \ - + mpls_ttl['buf'] \ - + pack('3x') + fmt = ofproto_v1_2.OFP_ACTION_MPLS_TTL_PACK_STR + buf = pack(fmt, type_, len_, mpls_ttl) - c = OFPActionSetMplsTtl(mpls_ttl['val']) + c = OFPActionSetMplsTtl(mpls_ttl) def setUp(self): pass @@ -1420,12 +1373,14 @@ class TestOFPActionSetMplsTtl(unittest.TestCase): pass def test_init(self): - eq_(self.mpls_ttl['val'], self.c.mpls_ttl) + eq_(self.mpls_ttl, self.c.mpls_ttl) def test_parser(self): res = self.c.parser(self.buf, 0) - eq_(self.mpls_ttl['val'], res.mpls_ttl) + eq_(res.len, self.len_) + eq_(res.type, self.type_) + eq_(res.mpls_ttl, self.mpls_ttl) def test_serialize(self): buf = bytearray() @@ -1434,19 +1389,23 @@ class TestOFPActionSetMplsTtl(unittest.TestCase): fmt = ofproto_v1_2.OFP_ACTION_MPLS_TTL_PACK_STR res = struct.unpack(fmt, buffer(buf)) - eq_(self.type_['val'], res[0]) - eq_(self.len_['val'], res[1]) - eq_(self.mpls_ttl['val'], res[2]) + eq_(res[0], self.type_) + eq_(res[1], self.len_) + eq_(res[2], self.mpls_ttl) class TestOFPActionDecMplsTtl(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPActionDecMplsTtl + """ Test case for ofproto_v1_2_parser.OFPActionDecMplsTtl """ - # OFP_ACTION_HEADER_PACK_STR - # '!HH'...type, len - type_ = {'buf': '\x00\x10', 'val': ofproto_v1_2.OFPAT_DEC_MPLS_TTL} - len_ = {'buf': '\x00\x08', 'val': ofproto_v1_2.OFP_ACTION_HEADER_SIZE} + # OFP_ACTION_MPLS_TTL_PACK_STR + # '!HHB3x'...type, len, mpls_ttl, pad(3) + type_ = ofproto_v1_2.OFPAT_DEC_MPLS_TTL + len_ = ofproto_v1_2.OFP_ACTION_MPLS_TTL_SIZE + mpls_ttl = 254 + + fmt = ofproto_v1_2.OFP_ACTION_MPLS_TTL_PACK_STR + buf = pack(fmt, type_, len_, mpls_ttl) c = OFPActionDecMplsTtl() @@ -1459,6 +1418,12 @@ class TestOFPActionDecMplsTtl(unittest.TestCase): def test_init(self): pass + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(res.len, self.len_) + eq_(res.type, self.type_) + def test_serialize(self): buf = bytearray() self.c.serialize(buf, 0) @@ -1466,26 +1431,24 @@ class TestOFPActionDecMplsTtl(unittest.TestCase): fmt = ofproto_v1_2.OFP_ACTION_HEADER_PACK_STR res = struct.unpack(fmt, buffer(buf)) - eq_(self.type_['val'], res[0]) - eq_(self.len_['val'], res[1]) + eq_(res[0], self.type_) + eq_(res[1], self.len_) class TestOFPActionSetNwTtl(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPActionSetNwTtl + """ Test case for ofproto_v1_2_parser.OFPActionSetNwTtl """ - # OFP_ACTION_NW_TTL_PACK_STR v1.2 + # OFP_ACTION_NW_TTL_PACK_STR # '!HHB3x'...type, len, nw_ttl, pad(3) - type_ = {'buf': '\x00\x17', 'val': ofproto_v1_2.OFPAT_SET_NW_TTL} - len_ = {'buf': '\x00\x08', 'val': ofproto_v1_2.OFP_ACTION_NW_TTL_SIZE} - nw_ttl = {'buf': '\xf0', 'val': 240} + type_ = ofproto_v1_2.OFPAT_SET_NW_TTL + len_ = ofproto_v1_2.OFP_ACTION_NW_TTL_SIZE + nw_ttl = 240 - buf = type_['buf'] \ - + len_['buf'] \ - + nw_ttl['buf'] \ - + pack('3x') + fmt = ofproto_v1_2.OFP_ACTION_NW_TTL_PACK_STR + buf = pack(fmt, type_, len_, nw_ttl) - c = OFPActionSetNwTtl(nw_ttl['val']) + c = OFPActionSetNwTtl(nw_ttl) def setUp(self): pass @@ -1494,12 +1457,14 @@ class TestOFPActionSetNwTtl(unittest.TestCase): pass def test_init(self): - eq_(self.nw_ttl['val'], self.c.nw_ttl) + eq_(self.nw_ttl, self.c.nw_ttl) def test_parser(self): res = self.c.parser(self.buf, 0) - eq_(self.nw_ttl['val'], res.nw_ttl) + eq_(res.type, self.type_) + eq_(res.len, self.len_) + eq_(res.nw_ttl, self.nw_ttl) def test_serialize(self): buf = bytearray() @@ -1508,19 +1473,23 @@ class TestOFPActionSetNwTtl(unittest.TestCase): fmt = ofproto_v1_2.OFP_ACTION_NW_TTL_PACK_STR res = struct.unpack(fmt, buffer(buf)) - eq_(self.type_['val'], res[0]) - eq_(self.len_['val'], res[1]) - eq_(self.nw_ttl['val'], res[2]) + eq_(res[0], self.type_) + eq_(res[1], self.len_) + eq_(res[2], self.nw_ttl) class TestOFPActionDecNwTtl(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPActionDecNwTtl + """ Test case for ofproto_v1_2_parser.OFPActionDecNwTtl """ - # OFP_ACTION_HEADER_PACK_STR - # '!HH'...type, len - type_ = {'buf': '\x00\x18', 'val': ofproto_v1_2.OFPAT_DEC_NW_TTL} - len_ = {'buf': '\x00\x08', 'val': ofproto_v1_2.OFP_ACTION_HEADER_SIZE} + # OFP_ACTION_NW_TTL_PACK_STR + # '!HHB3x'...type, len, nw_ttl, pad(3) + type_ = ofproto_v1_2.OFPAT_DEC_NW_TTL + len_ = ofproto_v1_2.OFP_ACTION_NW_TTL_SIZE + nw_ttl = 240 + + fmt = ofproto_v1_2.OFP_ACTION_NW_TTL_PACK_STR + buf = pack(fmt, type_, len_, nw_ttl) c = OFPActionDecNwTtl() @@ -1533,6 +1502,12 @@ class TestOFPActionDecNwTtl(unittest.TestCase): def test_init(self): pass + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(res.len, self.len_) + eq_(res.type, self.type_) + def test_serialize(self): buf = bytearray() self.c.serialize(buf, 0) @@ -1540,26 +1515,84 @@ class TestOFPActionDecNwTtl(unittest.TestCase): fmt = ofproto_v1_2.OFP_ACTION_HEADER_PACK_STR res = struct.unpack(fmt, buffer(buf)) - eq_(self.type_['val'], res[0]) - eq_(self.len_['val'], res[1]) + eq_(res[0], self.type_) + eq_(res[1], self.len_) + + +class TestOFPActionCopyTtlOut(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPActionCopyTtlOut + """ + + # OFP_ACTION_HEADER_PACK_STR + # '!HH'...type, len + type_ = ofproto_v1_2.OFPAT_COPY_TTL_OUT + len_ = ofproto_v1_2.OFP_ACTION_HEADER_SIZE + + fmt = ofproto_v1_2.OFP_ACTION_HEADER_PACK_STR + buf = pack(fmt, type_, len_) + + c = OFPActionCopyTtlOut() + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + pass + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(res.len, self.len_) + eq_(res.type, self.type_) + + +class TestOFPActionCopyTtlIn(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPActionCopyTtlIn + """ + + # OFP_ACTION_HEADER_PACK_STR + # '!HH'...type, len + type_ = ofproto_v1_2.OFPAT_COPY_TTL_IN + len_ = ofproto_v1_2.OFP_ACTION_HEADER_SIZE + + fmt = ofproto_v1_2.OFP_ACTION_HEADER_PACK_STR + buf = pack(fmt, type_, len_) + + c = OFPActionCopyTtlIn() + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + pass + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(res.len, self.len_) + eq_(res.type, self.type_) class TestOFPActionPushVlan(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPActionPushVlan + """ Test case for ofproto_v1_2_parser.OFPActionPushVlan """ - # OFP_ACTION_PUSH_PACK_STR v1.2 + # OFP_ACTION_PUSH_PACK_STR # '!HHB3x'...type, len, ethertype, pad(2) - type_ = {'buf': '\x00\x11', 'val': ofproto_v1_2.OFPAT_PUSH_VLAN} - len_ = {'buf': '\x00\x08', 'val': ofproto_v1_2.OFP_ACTION_PUSH_SIZE} - ethertype = {'buf': '\x1f\xa4', 'val': 8100} + type_ = ofproto_v1_2.OFPAT_PUSH_VLAN + len_ = ofproto_v1_2.OFP_ACTION_PUSH_SIZE + ethertype = 0x8100 - buf = type_['buf'] \ - + len_['buf'] \ - + ethertype['buf'] \ - + pack('2x') + fmt = ofproto_v1_2.OFP_ACTION_PUSH_PACK_STR + buf = pack(fmt, type_, len_, ethertype) - c = OFPActionPushVlan(ethertype['val']) + c = OFPActionPushVlan(ethertype) def setUp(self): pass @@ -1568,12 +1601,14 @@ class TestOFPActionPushVlan(unittest.TestCase): pass def test_init(self): - eq_(self.ethertype['val'], self.c.ethertype) + eq_(self.ethertype, self.c.ethertype) def test_parser(self): res = self.c.parser(self.buf, 0) - eq_(self.ethertype['val'], res.ethertype) + eq_(res.type, self.type_) + eq_(res.len, self.len_) + eq_(res.ethertype, self.ethertype) def test_serialize(self): buf = bytearray() @@ -1582,27 +1617,25 @@ class TestOFPActionPushVlan(unittest.TestCase): fmt = ofproto_v1_2.OFP_ACTION_PUSH_PACK_STR res = struct.unpack(fmt, buffer(buf)) - eq_(self.type_['val'], res[0]) - eq_(self.len_['val'], res[1]) - eq_(self.ethertype['val'], res[2]) + eq_(res[0], self.type_) + eq_(res[1], self.len_) + eq_(res[2], self.ethertype) class TestOFPActionPushMpls(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPActionPushMpls + """ Test case for ofproto_v1_2_parser.OFPActionPushMpls """ - # OFP_ACTION_PUSH_PACK_STR v1.2 + # OFP_ACTION_PUSH_PACK_STR # '!HHH2x'...type, len, ethertype, pad(2) - type_ = {'buf': '\x00\x13', 'val': ofproto_v1_2.OFPAT_PUSH_MPLS} - len_ = {'buf': '\x00\x08', 'val': ofproto_v1_2.OFP_ACTION_PUSH_SIZE} - ethertype = {'buf': '\x22\x8f', 'val': 8847} + type_ = ofproto_v1_2.OFPAT_PUSH_MPLS + len_ = ofproto_v1_2.OFP_ACTION_PUSH_SIZE + ethertype = 0x8847 - buf = type_['buf'] \ - + len_['buf'] \ - + ethertype['buf'] \ - + pack('2x') + fmt = ofproto_v1_2.OFP_ACTION_PUSH_PACK_STR + buf = pack(fmt, type_, len_, ethertype) - c = OFPActionPushMpls(ethertype['val']) + c = OFPActionPushMpls(ethertype) def setUp(self): pass @@ -1611,12 +1644,14 @@ class TestOFPActionPushMpls(unittest.TestCase): pass def test_init(self): - eq_(self.ethertype['val'], self.c.ethertype) + eq_(self.ethertype, self.c.ethertype) def test_parser(self): res = self.c.parser(self.buf, 0) - eq_(self.ethertype['val'], res.ethertype) + eq_(res.type, self.type_) + eq_(res.len, self.len_) + eq_(res.ethertype, self.ethertype) def test_serialize(self): buf = bytearray() @@ -1625,27 +1660,55 @@ class TestOFPActionPushMpls(unittest.TestCase): fmt = ofproto_v1_2.OFP_ACTION_PUSH_PACK_STR res = struct.unpack(fmt, buffer(buf)) - eq_(self.type_['val'], res[0]) - eq_(self.len_['val'], res[1]) - eq_(self.ethertype['val'], res[2]) + eq_(res[0], self.type_) + eq_(res[1], self.len_) + eq_(res[2], self.ethertype) -class OFPActionPopMpls(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPActionPopMpls +class TestOFPActionPopVlan(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPActionPopVlan + """ + + # OFP_ACTION_HEADER_PACK_STR + # '!HH'...type, len + type_ = ofproto_v1_2.OFPAT_POP_VLAN + len_ = ofproto_v1_2.OFP_ACTION_HEADER_SIZE + + fmt = ofproto_v1_2.OFP_ACTION_HEADER_PACK_STR + buf = pack(fmt, type_, len_) + + c = OFPActionPopVlan() + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + pass + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(self.type_, res.type) + eq_(self.len_, res.len) + + +class TestOFPActionPopMpls(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPActionPopMpls """ # OFP_ACTION_POP_MPLS_PACK_STR # '!HHH2x'...type, len, ethertype, pad(2) - type_ = {'buf': '\x00\x14', 'val': ofproto_v1_2.OFPAT_POP_MPLS} - len_ = {'buf': '\x00\x08', 'val': ofproto_v1_2.OFP_ACTION_POP_MPLS_SIZE} - ethertype = {'buf': '\x1f\xa4', 'val': 8100} + type_ = ofproto_v1_2.OFPAT_POP_MPLS + len_ = ofproto_v1_2.OFP_ACTION_POP_MPLS_SIZE + ethertype = 0x8100 - buf = type_['buf'] \ - + len_['buf'] \ - + ethertype['buf'] \ - + pack('2x') + fmt = ofproto_v1_2.OFP_ACTION_POP_MPLS_PACK_STR + buf = pack(fmt, type_, len_, ethertype) - c = OFPActionPopMpls(ethertype['val']) + c = OFPActionPopMpls(ethertype) def setUp(self): pass @@ -1654,12 +1717,14 @@ class OFPActionPopMpls(unittest.TestCase): pass def test_init(self): - eq_(self.ethertype['val'], self.c.ethertype) + eq_(self.ethertype, self.c.ethertype) def test_parser(self): res = self.c.parser(self.buf, 0) - eq_(self.ethertype['val'], res.ethertype) + eq_(res.type, self.type_) + eq_(res.len, self.len_) + eq_(res.ethertype, self.ethertype) def test_serialize(self): buf = bytearray() @@ -1668,27 +1733,71 @@ class OFPActionPopMpls(unittest.TestCase): fmt = ofproto_v1_2.OFP_ACTION_POP_MPLS_PACK_STR res = struct.unpack(fmt, buffer(buf)) - eq_(self.type_['val'], res[0]) - eq_(self.len_['val'], res[1]) - eq_(self.ethertype['val'], res[2]) + eq_(res[0], self.type_) + eq_(res[1], self.len_) + eq_(res[2], self.ethertype) + + +class TestOFPActionSetField(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPActionSetField + """ + + type_ = ofproto_v1_2.OFPAT_SET_FIELD + header = ofproto_v1_2.OXM_OF_IN_PORT + in_port = 6606 + + field = MTInPort(header, in_port) + length = ofproto_v1_2.OFP_ACTION_SET_FIELD_SIZE + field.oxm_len() + len_ = utils.round_up(length, 8) + + fmt = '!HHII4x' + buf = pack(fmt, type_, len_, header, in_port) + + c = OFPActionSetField(field) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.field, self.c.field) + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(res.type, self.type_) + eq_(res.len, self.len_) + eq_(res.field.header, self.header) + eq_(res.field.value, self.in_port) + + def test_serialize(self): + buf = bytearray() + self.c.serialize(buf, 0) + + res = struct.unpack(self.fmt, buffer(buf)) + + eq_(res[0], self.type_) + eq_(res[1], self.len_) + eq_(res[2], self.header) + eq_(res[3], self.in_port) class TestOFPActionExperimenter(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPActionExperimenter + """ Test case for ofproto_v1_2_parser.OFPActionExperimenter """ # OFP_ACTION_EXPERIMENTER_HEADER_PACK_STR v1.2 # '!HHI'...type, len, experimenter - type_ = {'buf': '\xff\xff', 'val': ofproto_v1_2.OFPAT_EXPERIMENTER} - len_ = {'buf': '\x00\x08', - 'val': ofproto_v1_2.OFP_ACTION_EXPERIMENTER_HEADER_SIZE} - experimenter = {'buf': '\xff\xff\xff\xff', 'val': 4294967295} + type_ = ofproto_v1_2.OFPAT_EXPERIMENTER + len_ = ofproto_v1_2.OFP_ACTION_EXPERIMENTER_HEADER_SIZE + experimenter = 4294967295 - buf = type_['buf'] \ - + len_['buf'] \ - + experimenter['buf'] + fmt = ofproto_v1_2.OFP_ACTION_EXPERIMENTER_HEADER_PACK_STR + buf = pack(fmt, type_, len_, experimenter) - c = OFPActionExperimenter(experimenter['val']) + c = OFPActionExperimenter(experimenter) def setUp(self): pass @@ -1697,12 +1806,14 @@ class TestOFPActionExperimenter(unittest.TestCase): pass def test_init(self): - eq_(self.experimenter['val'], self.c.experimenter) + eq_(self.experimenter, self.c.experimenter) def test_parser(self): res = self.c.parser(self.buf, 0) - eq_(self.experimenter['val'], res.experimenter) + eq_(res.type, self.type_) + eq_(res.len, self.len_) + eq_(res.experimenter, self.experimenter) def test_serialize(self): buf = bytearray() @@ -1711,13 +1822,155 @@ class TestOFPActionExperimenter(unittest.TestCase): fmt = ofproto_v1_2.OFP_ACTION_EXPERIMENTER_HEADER_PACK_STR res = struct.unpack(fmt, buffer(buf)) - eq_(self.type_['val'], res[0]) - eq_(self.len_['val'], res[1]) - eq_(self.experimenter['val'], res[2]) + eq_(res[0], self.type_) + eq_(res[1], self.len_) + eq_(res[2], self.experimenter) + + +class TestOFPBucket(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPBucket + """ + + # OFP_BUCKET_PACK_STR + # '!HHII4x'...len, weight, watch_port, watch_group, pad(4) + len_ = ofproto_v1_2.OFP_BUCKET_SIZE \ + + ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE + weight = 4386 + watch_port = 6606 + watch_group = 3 + + fmt = ofproto_v1_2.OFP_BUCKET_PACK_STR + buf = pack(fmt, len_, weight, watch_port, watch_group) + + # OFP_ACTION (OFP_ACTION_OUTPUT) + port = 0x00002ae0 + max_len = ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE + actions = [OFPActionOutput(port, max_len)] + buf_actions = bytearray() + actions[0].serialize(buf_actions, 0) + + buf += str(buf_actions) + + c = OFPBucket(len_, weight, watch_port, watch_group, actions) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.len_, self.c.len) + eq_(self.weight, self.c.weight) + eq_(self.watch_port, self.c.watch_port) + eq_(self.watch_group, self.c.watch_group) + eq_(self.actions, self.c.actions) + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(self.len_, res.len) + eq_(self.weight, res.weight) + eq_(self.watch_port, res.watch_port) + eq_(self.watch_group, res.watch_group) + + eq_(self.actions[0].type, res.actions[0].type) + eq_(self.actions[0].len, res.actions[0].len) + eq_(self.actions[0].port, res.actions[0].port) + eq_(self.actions[0].max_len, res.actions[0].max_len) + + def test_serialize(self): + buf = bytearray() + self.c.serialize(buf, 0) + + fmt = '!' \ + + ofproto_v1_2.OFP_BUCKET_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_ACTION_OUTPUT_PACK_STR.replace('!', '') + res = struct.unpack(fmt, buffer(buf)) + + eq_(res[0], self.len_) + eq_(res[1], self.weight) + eq_(res[2], self.watch_port) + eq_(res[3], self.watch_group) + eq_(res[4], self.actions[0].type) + eq_(res[5], self.actions[0].len) + eq_(res[6], self.actions[0].port) + eq_(res[7], self.actions[0].max_len) + + +class TestOFPGroupMod(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPGroupMod + """ + + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + # OFP_GROUP_MOD_PACK_STR + # '!HBBI'...command, type, pad, group_id + command = ofproto_v1_2.OFPFC_ADD + type_ = ofproto_v1_2.OFPGT_SELECT + group_id = 6606 + + # OFP_ACTION (OFP_ACTION_OUTPUT) + port = 0x00002ae0 + max_len = ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE + actions = [OFPActionOutput(port, max_len)] + + # OFP_BUCKET + len_ = ofproto_v1_2.OFP_BUCKET_SIZE + ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE + weight = 4386 + watch_port = 8006 + watch_group = 3 + buckets = [OFPBucket(len_, weight, watch_port, watch_group, actions)] + + c = OFPGroupMod(Datapath, command, type_, group_id, buckets) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.command, self.c.command) + eq_(self.type_, self.c.type) + eq_(self.group_id, self.c.group_id) + eq_(self.buckets, self.c.buckets) + + def test_serialize(self): + self.c.serialize() + + eq_(ofproto_v1_2.OFP_VERSION, self.c.version) + eq_(ofproto_v1_2.OFPT_GROUP_MOD, self.c.msg_type) + eq_(0, self.c.xid) + + fmt = '!' \ + + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_GROUP_MOD_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_BUCKET_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_ACTION_OUTPUT_PACK_STR.replace('!', '') + res = struct.unpack(fmt, str(self.c.buf)) + + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_GROUP_MOD) + eq_(res[2], ofproto_v1_2.OFP_GROUP_MOD_SIZE + self.len_) + eq_(res[3], 0) + eq_(res[4], self.command) + eq_(res[5], self.type_) + eq_(res[6], self.group_id) + eq_(res[7], self.len_) + eq_(res[8], self.weight) + eq_(res[9], self.watch_port) + eq_(res[10], self.watch_group) + eq_(res[11], self.actions[0].type) + eq_(res[12], self.actions[0].len) + eq_(res[13], self.port) + eq_(res[14], self.max_len) class TestOFPPortMod(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPPortMod + """ Test case for ofproto_v1_2_parser.OFPPortMod """ class Datapath(object): @@ -1727,14 +1980,13 @@ class TestOFPPortMod(unittest.TestCase): # OFP_PORT_MOD_PACK_STR v1.2 # '!I4xs2xIII4x'...port_no, pad(4), hw_addr, pad(2), # config, mask, advertise, pad(4) - port_no = {'buf': '\x42\xbd\x27\xfc', 'val': 1119692796} + port_no = 1119692796 hw_addr = 'hw'.ljust(ofproto_v1_2.OFP_ETH_ALEN) - config = {'buf': '\x84\xb6\x8c\x53', 'val': 2226555987} - mask = {'buf': '\x64\x07\xfb\xc9', 'val': 1678244809} - advertise = {'buf': '\x78\xb9\x7b\x72', 'val': 2025421682} + config = 2226555987 + mask = 1678244809 + advertise = 2025421682 - c = OFPPortMod(Datapath, port_no['val'], hw_addr, config['val'], - mask['val'], advertise['val']) + c = OFPPortMod(Datapath, port_no, hw_addr, config, mask, advertise) def setUp(self): pass @@ -1743,15 +1995,11 @@ class TestOFPPortMod(unittest.TestCase): pass def test_init(self): - eq_(self.port_no['val'], self.c.port_no) + eq_(self.port_no, self.c.port_no) eq_(self.hw_addr, self.c.hw_addr) - eq_(self.config['val'], self.c.config) - eq_(self.mask['val'], self.c.mask) - eq_(self.advertise['val'], self.c.advertise) - - def test_parser(self): - # Not used. - pass + eq_(self.config, self.c.config) + eq_(self.mask, self.c.mask) + eq_(self.advertise, self.c.advertise) def test_serialize(self): self.c.serialize() @@ -1766,19 +2014,19 @@ class TestOFPPortMod(unittest.TestCase): res = struct.unpack(fmt, str(self.c.buf)) - eq_(ofproto_v1_2.OFP_VERSION, res[0]) - eq_(ofproto_v1_2.OFPT_PORT_MOD, res[1]) - eq_(len(self.c.buf), res[2]) - eq_(0, res[3]) - eq_(self.port_no['val'], res[4]) - eq_(self.hw_addr, res[5]) - eq_(self.config['val'], res[6]) - eq_(self.mask['val'], res[7]) - eq_(self.advertise['val'], res[8]) + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_PORT_MOD) + eq_(res[2], len(self.c.buf)) + eq_(res[3], 0) + eq_(res[4], self.port_no) + eq_(res[5], self.hw_addr) + eq_(res[6], self.config) + eq_(res[7], self.mask) + eq_(res[8], self.advertise) class TestOFPTableMod(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPTableMod + """ Test case for ofproto_v1_2_parser.OFPTableMod """ class Datapath(object): @@ -1787,10 +2035,10 @@ class TestOFPTableMod(unittest.TestCase): # OFP_PORT_TABLE_PACK_STR v1.2 # '!B3xI'...table_id, pad(3), config - table_id = {'buf': '\x03', 'val': 3} - config = {'buf': '\x84\xb6\x8c\x53', 'val': 2226555987} + table_id = 3 + config = 2226555987 - c = OFPTableMod(Datapath, table_id['val'], config['val']) + c = OFPTableMod(Datapath, table_id, config) def setUp(self): pass @@ -1799,12 +2047,8 @@ class TestOFPTableMod(unittest.TestCase): pass def test_init(self): - eq_(self.table_id['val'], self.c.table_id) - eq_(self.config['val'], self.c.config) - - def test_parser(self): - # Not used. - pass + eq_(self.table_id, self.c.table_id) + eq_(self.config, self.c.config) def test_serialize(self): self.c.serialize() @@ -1819,27 +2063,733 @@ class TestOFPTableMod(unittest.TestCase): res = struct.unpack(fmt, str(self.c.buf)) - eq_(ofproto_v1_2.OFP_VERSION, res[0]) - eq_(ofproto_v1_2.OFPT_TABLE_MOD, res[1]) - eq_(len(self.c.buf), res[2]) - eq_(0, res[3]) - eq_(self.table_id['val'], res[4]) - eq_(self.config['val'], res[5]) + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_TABLE_MOD) + eq_(res[2], len(self.c.buf)) + eq_(res[3], 0) + eq_(res[4], self.table_id) + eq_(res[5], self.config) -class TestOFPQueueGetConfigRequest(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPQueueGetConfigRequest +class TestOFPStatsRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPStatsRequest """ class Datapath(object): ofproto = ofproto_v1_2 ofproto_parser = ofproto_v1_2_parser - # OFP_QUEUE_GET_CONFIG_REQUEST_PACK_STR v1.2 - # '!I4x'...port, pad(4) - port = {'buf': '\x00\x00\xa0\xe2', 'val': 41186} + type_ = ofproto_v1_2.OFPST_DESC + + c = OFPStatsRequest(Datapath, type_) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.type_, self.c.type) + eq_(0, self.c.type) + + def test_serialize_stats_body(self): + self.c._serialize_stats_body() + + +class TestOFPStatsReply(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPStatsReply + """ + + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + c = OFPStatsReply(Datapath) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + pass + + def test_parser_single_struct_true(self): + # OFP_HEADER_PACK_STR + # '!BBHI'...version, msg_type, msg_len, xid + version = ofproto_v1_2.OFP_VERSION + msg_type = ofproto_v1_2.OFPT_STATS_REPLY + msg_len = ofproto_v1_2.OFP_STATS_REPLY_SIZE \ + + ofproto_v1_2.OFP_AGGREGATE_STATS_REPLY_SIZE + xid = 2495926989 + + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + buf = pack(fmt, version, msg_type, msg_len, xid) + + # OFP_STATS_REPLY_PACK_STR + # '!HH4x'...type, flags, pad(4) + type_ = ofproto_v1_2.OFPST_AGGREGATE + flags = 0 + + fmt = ofproto_v1_2.OFP_STATS_REPLY_PACK_STR + buf += pack(fmt, type_, flags) + + # OFP_AGGREGATE_STATS_REPLY_PACK_STR + packet_count = 5142202600015232219 + byte_count = 2659740543924820419 + flow_count = 1344694860 + body = OFPAggregateStatsReply(packet_count, byte_count, flow_count) + + fmt = ofproto_v1_2.OFP_AGGREGATE_STATS_REPLY_PACK_STR + buf += pack(fmt, packet_count, byte_count, flow_count) + + res = self.c.parser(object, version, msg_type, msg_len, xid, buf) + + eq_(version, res.version) + eq_(msg_type, res.msg_type) + eq_(msg_len, res.msg_len) + eq_(xid, res.xid) + eq_(type_, res.type) + eq_(flags, res.flags) + eq_(body, res.body) + eq_(packet_count, res.body.packet_count) + eq_(byte_count, res.body.byte_count) + eq_(flow_count, res.body.flow_count) + + def test_parser_single_struct_flase(self): + # OFP_HEADER_PACK_STR + # '!BBHI'...version, msg_type, msg_len, xid + version = ofproto_v1_2.OFP_VERSION + msg_type = ofproto_v1_2.OFPT_STATS_REPLY + msg_len = ofproto_v1_2.OFP_STATS_REPLY_SIZE \ + + ofproto_v1_2.OFP_QUEUE_STATS_SIZE + xid = 2495926989 + + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + buf = pack(fmt, version, msg_type, msg_len, xid) + + # OFP_STATS_REPLY_PACK_STR + # '!HH4x'...type, flags, pad(4) + type_ = ofproto_v1_2.OFPST_QUEUE + flags = 0 + + fmt = ofproto_v1_2.OFP_STATS_REPLY_PACK_STR + buf += pack(fmt, type_, flags) + + # OFP_QUEUE_STATS_PACK_STR + port_no = 41186 + queue_id = 6606 + tx_bytes = 8638420181865882538 + tx_packets = 2856480458895760962 + tx_errors = 6283093430376743019 + body = [OFPQueueStats(port_no, queue_id, tx_bytes, tx_packets, + tx_errors)] + + fmt = ofproto_v1_2.OFP_QUEUE_STATS_PACK_STR + buf += pack(fmt, port_no, queue_id, tx_bytes, tx_packets, tx_errors) + + res = self.c.parser(object, version, msg_type, msg_len, xid, buf) + + eq_(version, res.version) + eq_(msg_type, res.msg_type) + eq_(msg_len, res.msg_len) + eq_(xid, res.xid) + eq_(type_, res.type) + eq_(flags, res.flags) + eq_(body, res.body) + eq_(port_no, res.body[0].port_no) + eq_(queue_id, res.body[0].queue_id) + eq_(tx_bytes, res.body[0].tx_bytes) + eq_(tx_packets, res.body[0].tx_packets) + eq_(tx_errors, res.body[0].tx_errors) + + +class TestOFPDescStatsRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPDescStatsRequest + """ + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + OFPDescStatsRequest(Datapath) + + +class TestOFPDescStats(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPDescStats + """ + + # OFP_DESC_STATS_PACK_STR + # '!256s256s256s32s256s'...mfr_desc, hw_desc, sw_desc, serial_num, dp_desc + mfr_desc = 'mfr_desc'.ljust(256) + hw_desc = 'hw_desc'.ljust(256) + sw_desc = 'sw_desc'.ljust(256) + serial_num = 'serial_num'.ljust(32) + dp_desc = 'dp_desc'.ljust(256) + + buf = mfr_desc \ + + hw_desc \ + + sw_desc \ + + serial_num \ + + dp_desc + + c = OFPDescStats(mfr_desc, hw_desc, sw_desc, serial_num, dp_desc) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.mfr_desc, self.c.mfr_desc) + eq_(self.hw_desc, self.c.hw_desc) + eq_(self.sw_desc, self.c.sw_desc) + eq_(self.serial_num, self.c.serial_num) + eq_(self.dp_desc, self.c.dp_desc) + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(self.mfr_desc, res.mfr_desc) + eq_(self.hw_desc, res.hw_desc) + eq_(self.sw_desc, res.sw_desc) + eq_(self.serial_num, res.serial_num) + eq_(self.dp_desc, res.dp_desc) + + +class TestOFPFlowStatsRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPFlowStatsRequest + """ + + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + # OFP_FLOW_STATS_REQUEST_PACK_STR + # '!B3xII4xQQ'...table_id, pad(3), out_port, out_group, pad(4), + # cookie, cookie_mask + table_id = 3 + out_port = 65037 + out_group = 6606 + cookie = 2127614848199081640 + cookie_mask = 2127614848199081641 + + match = OFPMatch() + + c = OFPFlowStatsRequest(Datapath, table_id, out_port, out_group, + cookie, cookie_mask, match) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.table_id, self.c.table_id) + eq_(self.out_port, self.c.out_port) + eq_(self.out_group, self.c.out_group) + eq_(self.cookie, self.c.cookie) + eq_(self.cookie_mask, self.c.cookie_mask) + eq_(self.match, self.c.match) + + def test_serialize(self): + self.c.serialize() + + eq_(ofproto_v1_2.OFP_VERSION, self.c.version) + eq_(ofproto_v1_2.OFPT_STATS_REQUEST, self.c.msg_type) + eq_(0, self.c.xid) + + fmt = '!' \ + + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_STATS_REQUEST_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_FLOW_STATS_REQUEST_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_MATCH_PACK_STR.replace('!', '') + + res = struct.unpack(fmt, str(self.c.buf)) + + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_STATS_REQUEST) + eq_(res[2], ofproto_v1_2.OFP_STATS_REPLY_SIZE \ + + ofproto_v1_2.OFP_FLOW_STATS_REQUEST_SIZE) + eq_(res[3], 0) + eq_(res[4], ofproto_v1_2.OFPST_FLOW) + eq_(res[5], 0) + eq_(res[6], self.table_id) + eq_(res[7], self.out_port) + eq_(res[8], self.out_group) + eq_(res[9], self.cookie) + eq_(res[10], self.cookie_mask) + + +class TestOFPFlowStats(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPFlowStats + """ + + # OFP_FLOW_STATS_PACK_STR + # '!HBxIIHHH6xQQQ'...length, table_id, pad, duration_sec, duration_nsec, + # priority, idle_timeoutl, hard_timeout, pad(6), + # cookie, packet_count, byte_count + length = ofproto_v1_2.OFP_FLOW_STATS_SIZE \ + + ofproto_v1_2.OFP_INSTRUCTION_GOTO_TABLE_SIZE + table_id = 81 + duration_sec = 2484712402 + duration_nsec = 3999715196 + priority = 57792 + idle_timeout = 36368 + hard_timeout = 54425 + cookie = 793171083674290912 + packet_count = 5142202600015232219 + byte_count = 2659740543924820419 + + match = OFPMatch() + buf_match = bytearray() + match.serialize(buf_match, 0) + + instructions = [OFPInstructionGotoTable(table_id)] + buf_instructions = bytearray() + instructions[0].serialize(buf_instructions, 0) + + fmt = ofproto_v1_2.OFP_FLOW_STATS_PACK_STR + buf = pack(fmt, length, table_id, duration_sec, duration_nsec, + priority, idle_timeout, hard_timeout, cookie, + packet_count, byte_count) \ + + str(buf_match) \ + + str(buf_instructions) + + c = OFPFlowStats(length, table_id, duration_sec, duration_nsec, + priority, idle_timeout, hard_timeout, cookie, + packet_count, byte_count, match, instructions) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.length, self.c.length) + eq_(self.table_id, self.c.table_id) + eq_(self.duration_sec, self.c.duration_sec) + eq_(self.duration_nsec, self.c.duration_nsec) + eq_(self.priority, self.c.priority) + eq_(self.idle_timeout, self.c.idle_timeout) + eq_(self.hard_timeout, self.c.hard_timeout) + eq_(self.cookie, self.c.cookie) + eq_(self.packet_count, self.c.packet_count) + eq_(self.byte_count, self.c.byte_count) + eq_(self.match, self.c.match) + eq_(self.instructions, self.c.instructions) + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(self.length, res.length) + eq_(self.table_id, res.table_id) + eq_(self.duration_sec, res.duration_sec) + eq_(self.duration_nsec, res.duration_nsec) + eq_(self.priority, res.priority) + eq_(self.idle_timeout, res.idle_timeout) + eq_(self.hard_timeout, res.hard_timeout) + eq_(self.cookie, res.cookie) + eq_(self.packet_count, res.packet_count) + eq_(self.byte_count, res.byte_count) + eq_(self.instructions[0].type, res.instructions[0].type) + eq_(self.instructions[0].len, res.instructions[0].len) + eq_(self.instructions[0].table_id, res.instructions[0].table_id) + + +class TestOFPAggregateStatsRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPAggregateStatsRequest + """ + + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + # OFP_AGGREGATE_STATS_REQUEST_PACK_STR + # '!B3xII4xQQ'...table_id, pad(3), out_port, out_group, pad(4), + # cookie, cookie_mask + table_id = 3 + out_port = 65037 + out_group = 6606 + cookie = 2127614848199081640 + cookie_mask = 2127614848199081641 + + match = OFPMatch() + + c = OFPAggregateStatsRequest(Datapath, table_id, out_port, out_group, + cookie, cookie_mask, match) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.table_id, self.c.table_id) + eq_(self.out_port, self.c.out_port) + eq_(self.out_group, self.c.out_group) + eq_(self.cookie, self.c.cookie) + eq_(self.cookie_mask, self.c.cookie_mask) + eq_(self.match, self.c.match) + + def test_serialize(self): + self.c.serialize() + + eq_(ofproto_v1_2.OFP_VERSION, self.c.version) + eq_(ofproto_v1_2.OFPT_STATS_REQUEST, self.c.msg_type) + eq_(0, self.c.xid) + + fmt = '!' \ + + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_STATS_REQUEST_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_AGGREGATE_STATS_REQUEST_PACK_STR.replace('!', + '') \ + + ofproto_v1_2.OFP_MATCH_PACK_STR.replace('!', '') + + res = struct.unpack(fmt, str(self.c.buf)) + + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_STATS_REQUEST) + eq_(res[2], ofproto_v1_2.OFP_STATS_REPLY_SIZE + + ofproto_v1_2.OFP_AGGREGATE_STATS_REQUEST_SIZE) + eq_(res[3], 0) + eq_(res[4], ofproto_v1_2.OFPST_AGGREGATE) + eq_(res[5], 0) + eq_(res[6], self.table_id) + eq_(res[7], self.out_port) + eq_(res[8], self.out_group) + eq_(res[9], self.cookie) + eq_(res[10], self.cookie_mask) + + +class TestOFPAggregateStatsReply(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPAggregateStatsReply + """ + + # OFP_AGGREGATE_STATS_REPLY_PACK_STR + # '!QQI4x'...packet_count, byte_count, flow_count, pad(4) + packet_count = 5142202600015232219 + byte_count = 2659740543924820419 + flow_count = 1344694860 + + fmt = ofproto_v1_2.OFP_AGGREGATE_STATS_REPLY_PACK_STR + buf = pack(fmt, packet_count, byte_count, flow_count) + + c = OFPAggregateStatsReply(packet_count, byte_count, flow_count) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + pass + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(self.packet_count, res.packet_count) + eq_(self.byte_count, res.byte_count) + eq_(self.flow_count, res.flow_count) + + +class TestOFPTableStatsRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPTableStatsRequest + """ + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + OFPTableStatsRequest(Datapath) + + +class TestOFPTableStats(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPTableStats + """ + + # OFP_TABLE_STATS_PACK_STR + # '!B7x32sQQIIQQQQIIIIQQ' + # ...table_id, name, match, wildcards, write_actions, apply_actions, + # write_setfields, apply_setfields', metadata_match, metadata_write, + # instructions, config, max_entries, + # active_count, lookup_count, matched_count + table_id = 91 + name = 'name'.ljust(ofproto_v1_2.OFP_MAX_TABLE_NAME_LEN) + match = 1270985291017894273 + wildcards = 3316608530 + write_actions = 2484712402 + apply_actions = 3999715196 + write_setfields = 5142202600015232219 + apply_setfields = 2659740543924820419 + metadata_match = 2127614848199081640 + metadata_write = 2127614848199081641 + instructions = 1119692796 + config = 2226555987 + max_entries = 2506913869 + active_count = 2024581150 + lookup_count = 4620020561814017052 + matched_count = 2825167325263435621 + + fmt = ofproto_v1_2.OFP_TABLE_STATS_PACK_STR + buf = pack(fmt, table_id, name, match, wildcards, write_actions, + apply_actions, write_setfields, apply_setfields, + metadata_match, metadata_write, instructions, config, + max_entries, active_count, lookup_count, matched_count) + + c = OFPTableStats(table_id, name, match, wildcards, write_actions, + apply_actions, write_setfields, apply_setfields, + metadata_match, metadata_write, instructions, config, + max_entries, active_count, lookup_count, matched_count) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + pass + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(self.table_id, res.table_id) + eq_(self.name, res.name) + eq_(self.match, res.match) + eq_(self.wildcards, res.wildcards) + eq_(self.write_actions, res.write_actions) + eq_(self.apply_actions, res.apply_actions) + eq_(self.write_setfields, res.write_setfields) + eq_(self.apply_setfields, res.apply_setfields) + eq_(self.metadata_match, res.metadata_match) + eq_(self.metadata_write, res.metadata_write) + eq_(self.instructions, res.instructions) + eq_(self.config, res.config) + eq_(self.max_entries, res.max_entries) + eq_(self.active_count, res.active_count) + eq_(self.lookup_count, res.lookup_count) + eq_(self.matched_count, res.matched_count) + + +class TestOFPPortStatsRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPPortStatsRequest + """ + + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + # OFP_PORT_STATS_REQUEST_PACK_STR + # '!I4x'...port_no, pad(4) + port_no = 41186 + + c = OFPPortStatsRequest(Datapath, port_no) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.port_no, self.c.port_no) + + def test_serialize(self): + self.c.serialize() + + eq_(ofproto_v1_2.OFP_VERSION, self.c.version) + eq_(ofproto_v1_2.OFPT_STATS_REQUEST, self.c.msg_type) + eq_(0, self.c.xid) + + fmt = '!' \ + + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_STATS_REQUEST_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_PORT_STATS_REQUEST_PACK_STR.replace('!', '') + res = struct.unpack(fmt, str(self.c.buf)) + + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_STATS_REQUEST) + eq_(res[2], ofproto_v1_2.OFP_STATS_REQUEST_SIZE \ + + ofproto_v1_2.OFP_PORT_STATS_REQUEST_SIZE) + eq_(res[3], 0) + eq_(res[4], ofproto_v1_2.OFPST_PORT) + eq_(res[5], 0) + eq_(res[6], self.port_no) + + +class TestOFPPortStats(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPPortStats + """ + + # OFP_PORT_STATS_PACK_STR = '!H6xQQQQQQQQQQQQ' + port_no = 6606 + rx_packets = 5999980397101236279 + tx_packets = 2856480458895760962 + rx_bytes = 6170274950576278921 + tx_bytes = 8638420181865882538 + rx_dropped = 6982303461569875546 + tx_dropped = 661287462113808071 + rx_errors = 3422231811478788365 + tx_errors = 6283093430376743019 + rx_frame_err = 876072919806406283 + rx_over_err = 6525873760178941600 + rx_crc_err = 8303073210207070535 + collisions = 3409801584220270201 + + fmt = ofproto_v1_2.OFP_PORT_STATS_PACK_STR + buf = pack(fmt, port_no, rx_packets, tx_packets, rx_bytes, tx_bytes, + rx_dropped, tx_dropped, rx_errors, tx_errors, rx_frame_err, + rx_over_err, rx_crc_err, collisions) + + c = OFPPortStats(port_no, rx_packets, tx_packets, rx_bytes, tx_bytes, + rx_dropped, tx_dropped, rx_errors, tx_errors, + rx_frame_err, rx_over_err, rx_crc_err, collisions) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + pass + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(self.port_no, res.port_no) + eq_(self.rx_packets, res.rx_packets) + eq_(self.tx_packets, res.tx_packets) + eq_(self.rx_bytes, res.rx_bytes) + eq_(self.tx_bytes, res.tx_bytes) + eq_(self.rx_dropped, res.rx_dropped) + eq_(self.tx_dropped, res.tx_dropped) + eq_(self.rx_errors, res.rx_errors) + eq_(self.tx_errors, res.tx_errors) + eq_(self.rx_frame_err, res.rx_frame_err) + eq_(self.rx_over_err, res.rx_over_err) + eq_(self.rx_crc_err, res.rx_crc_err) + eq_(self.collisions, res.collisions) + + +class TestOFPQueueStatsRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPQueueStatsRequest + """ + + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + # OFP_QUEUE_STATS_REQUEST_PACK_STR + # '!II'...port_no, queue_id + port_no = {'buf': '\x00\x00\xa0\xe2', 'val': 41186} + queue_id = {'buf': '\x00\x00\x19\xce', 'val': 6606} + + c = OFPQueueStatsRequest(Datapath, port_no['val'], queue_id['val']) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.port_no['val'], self.c.port_no) + eq_(self.queue_id['val'], self.c.queue_id) + + def test_serialize(self): + self.c.serialize() + + eq_(ofproto_v1_2.OFP_VERSION, self.c.version) + eq_(ofproto_v1_2.OFPT_STATS_REQUEST, self.c.msg_type) + eq_(0, self.c.xid) + + fmt = '!' \ + + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_STATS_REQUEST_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_QUEUE_STATS_REQUEST_PACK_STR.replace('!', '') + res = struct.unpack(fmt, str(self.c.buf)) + + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_STATS_REQUEST) + eq_(res[2], ofproto_v1_2.OFP_STATS_REQUEST_SIZE \ + + ofproto_v1_2.OFP_QUEUE_STATS_REQUEST_SIZE) + eq_(res[3], 0) + eq_(res[4], ofproto_v1_2.OFPST_QUEUE) + eq_(res[5], 0) + eq_(res[6], self.port_no['val']) + eq_(res[7], self.queue_id['val']) + + +class TestOFPQueueStats(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPQueueStats + """ + + # OFP_QUEUE_STATS_PACK_STR = '!IIQQQ' + port_no = 41186 + queue_id = 6606 + tx_bytes = 8638420181865882538 + tx_packets = 2856480458895760962 + tx_errors = 6283093430376743019 + + fmt = ofproto_v1_2.OFP_QUEUE_STATS_PACK_STR + buf = pack(fmt, port_no, queue_id, tx_bytes, tx_packets, tx_errors) + + c = OFPQueueStats(port_no, queue_id, tx_bytes, tx_packets, tx_errors) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + pass + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(self.port_no, res.port_no) + eq_(self.queue_id, res.queue_id) + eq_(self.tx_bytes, res.tx_bytes) + eq_(self.tx_packets, res.tx_packets) + eq_(self.tx_errors, res.tx_errors) + + +class TestOFPBucketCounter(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPBucketCounter + """ + + # OFP_BUCKET_COUNTER_PACK_STR = '!QQ' + packet_count = 6489108735192644493 + byte_count = 7334344481123449724 - c = OFPQueueGetConfigRequest(Datapath, port['val']) + fmt = ofproto_v1_2.OFP_BUCKET_COUNTER_PACK_STR + buf = pack(fmt, packet_count, byte_count) + + c = OFPBucketCounter(packet_count, byte_count) def setUp(self): pass @@ -1848,12 +2798,322 @@ class TestOFPQueueGetConfigRequest(unittest.TestCase): pass def test_init(self): - eq_(self.port['val'], self.c.port) + eq_(self.packet_count, self.c.packet_count) + eq_(self.byte_count, self.c.byte_count) def test_parser(self): - # Not used. + res = self.c.parser(self.buf, 0) + + eq_(self.packet_count, res.packet_count) + eq_(self.byte_count, res.byte_count) + + +class TestOFPGroupStatsRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPGroupStatsRequest + """ + + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + # OFP_GROUP_STATS_REQUEST_PACK_STR + # '!I4x'...group_id, pad(4) + group_id = 6606 + + c = OFPGroupStatsRequest(Datapath, group_id) + + def setUp(self): pass + def tearDown(self): + pass + + def test_init(self): + eq_(self.group_id, self.c.group_id) + + def test_serialize(self): + self.c.serialize() + + eq_(ofproto_v1_2.OFP_VERSION, self.c.version) + eq_(ofproto_v1_2.OFPT_STATS_REQUEST, self.c.msg_type) + eq_(0, self.c.xid) + + fmt = '!' \ + + ofproto_v1_2.OFP_HEADER_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_STATS_REQUEST_PACK_STR.replace('!', '') \ + + ofproto_v1_2.OFP_GROUP_STATS_REQUEST_PACK_STR.replace('!', '') + res = struct.unpack(fmt, str(self.c.buf)) + + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_STATS_REQUEST) + eq_(res[2], ofproto_v1_2.OFP_STATS_REQUEST_SIZE \ + + ofproto_v1_2.OFP_GROUP_STATS_REQUEST_SIZE) + eq_(res[3], 0) + eq_(res[4], ofproto_v1_2.OFPST_GROUP) + eq_(res[5], 0) + eq_(res[6], self.group_id) + + +class TestOFPGroupStats(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPGroupStats + """ + + # OFP_GROUP_STATS_PACK_STR = '!H2xII4xQQ' + length = ofproto_v1_2.OFP_GROUP_STATS_SIZE \ + + ofproto_v1_2.OFP_BUCKET_COUNTER_SIZE + group_id = 6606 + ref_count = 2102 + packet_count = 6489108735192644493 + byte_count = 7334344481123449724 + + # OFP_BUCKET_COUNTER_PACK_STR = '!QQ' + buck_packet_count = 3519264449364891087 + buck_byte_count = 3123449724733434448 + bucket_counters = [OFPBucketCounter(buck_packet_count, buck_byte_count)] + buf_bucket_counters = pack(ofproto_v1_2.OFP_BUCKET_COUNTER_PACK_STR, + buck_packet_count, buck_byte_count) + + fmt = ofproto_v1_2.OFP_GROUP_STATS_PACK_STR + buf = pack(fmt, length, group_id, ref_count, packet_count, byte_count) \ + + buf_bucket_counters + + c = OFPGroupStats(length, group_id, ref_count, packet_count, + byte_count, bucket_counters) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.length, self.c.length) + eq_(self.group_id, self.c.group_id) + eq_(self.ref_count, self.c.ref_count) + eq_(self.packet_count, self.c.packet_count) + eq_(self.byte_count, self.c.byte_count) + eq_(self.bucket_counters, self.c.bucket_counters) + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(self.length, res.length) + eq_(self.group_id, res.group_id) + eq_(self.ref_count, res.ref_count) + eq_(self.packet_count, res.packet_count) + eq_(self.byte_count, res.byte_count) + eq_(self.buck_packet_count, res.bucket_counters[0].packet_count) + eq_(self.buck_byte_count, res.bucket_counters[0].byte_count) + + +class TestOFPGroupDescStatsRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPGroupDescStatsRequest + """ + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + OFPGroupDescStatsRequest(Datapath) + + +class TestOFPGroupDescStats(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPGroupDescStats + """ + + # OFP_GROUP_DESC_STATS_PACK_STR = '!HBxI' + length = ofproto_v1_2.OFP_GROUP_DESC_STATS_SIZE \ + + ofproto_v1_2.OFP_BUCKET_SIZE \ + + ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE + type_ = ofproto_v1_2.OFPGT_ALL + group_id = 6606 + + # OFP_ACTION (OFP_ACTION_OUTPUT) + port = 0x00002ae0 + max_len = ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE + actions = [OFPActionOutput(port, max_len)] + buf_actions = bytearray() + actions[0].serialize(buf_actions, 0) + + # OFP_BUCKET + len_ = ofproto_v1_2.OFP_BUCKET_SIZE + ofproto_v1_2.OFP_ACTION_OUTPUT_SIZE + weight = 4386 + watch_port = 8006 + watch_group = 3 + buckets = [OFPBucket(len_, weight, watch_port, watch_group, actions)] + buf_buckets = bytearray() + buckets[0].serialize(buf_buckets, 0) + + fmt = ofproto_v1_2.OFP_GROUP_DESC_STATS_PACK_STR + buf = pack(fmt, length, type_, group_id) + str(buf_buckets) + + c = OFPGroupDescStats(length, type_, group_id, buckets) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.length, self.c.length) + eq_(self.type_, self.c.type) + eq_(self.group_id, self.c.group_id) + eq_(self.buckets, self.c.buckets) + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(self.type_, res.type) + eq_(self.length, res.length) + eq_(self.group_id, res.group_id) + eq_(self.len_, res.buckets[0].len) + eq_(self.weight, res.buckets[0].weight) + eq_(self.watch_port, res.buckets[0].watch_port) + eq_(self.watch_group, res.buckets[0].watch_group) + eq_(self.port, res.buckets[0].actions[0].port) + eq_(self.max_len, res.buckets[0].actions[0].max_len) + + +class TestOFPGroupFeaturesStatsRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPGroupFeaturesStatsRequest + """ + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + OFPGroupFeaturesStatsRequest(Datapath) + + +class TestOFPGroupFeaturesStats(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPGroupFeaturesStats + """ + + # OFP_GROUP_FEATURES_STATS_PACK_STR = '!II4I4I' + types = ofproto_v1_2.OFPGT_ALL + capabilities = ofproto_v1_2.OFPGFC_SELECT_WEIGHT + max_groups = (1, 2, 3, 4) + actions = (ofproto_v1_2.OFPAT_OUTPUT, + ofproto_v1_2.OFPAT_COPY_TTL_OUT, + ofproto_v1_2.OFPAT_SET_MPLS_TTL, + ofproto_v1_2.OFPAT_PUSH_VLAN) + + buf = pack('!I', types) \ + + pack('!I', capabilities) \ + + pack('!I', max_groups[0]) \ + + pack('!I', max_groups[1]) \ + + pack('!I', max_groups[2]) \ + + pack('!I', max_groups[3]) \ + + pack('!I', actions[0]) \ + + pack('!I', actions[1]) \ + + pack('!I', actions[2]) \ + + pack('!I', actions[3]) + + c = OFPGroupFeaturesStats(types, capabilities, max_groups, actions) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.types, self.c.types) + eq_(self.capabilities, self.c.capabilities) + eq_(self.max_groups, self.c.max_groups) + eq_(self.actions, self.c.actions) + + def test_parser(self): + res = self.c.parser(self.buf, 0) + + eq_(self.types, res.types) + eq_(self.capabilities, res.capabilities) + eq_(self.max_groups, res.max_groups) + eq_(self.actions, res.actions) + + +class TestOFPQueuePropMinRate(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPQueuePropMinRate + """ + + rate = 0 + buf = pack(ofproto_v1_2.OFP_QUEUE_PROP_MIN_RATE_PACK_STR, rate) + c = OFPQueuePropMinRate(rate) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.rate, self.c.rate) + + def test_parser(self): + res = self.c.parser(self.buf, 0) + eq_(self.rate, res.rate) + + +class TestOFPQueuePropMaxRate(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPQueuePropMaxRate + """ + + rate = 100 + buf = pack(ofproto_v1_2.OFP_QUEUE_PROP_MAX_RATE_PACK_STR, rate) + c = OFPQueuePropMaxRate(rate) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.rate, self.c.rate) + + def test_parser(self): + res = self.c.parser(self.buf, 0) + eq_(self.rate, res.rate) + + +class TestOFPQueueGetConfigRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPQueueGetConfigRequest + """ + + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + # OFP_QUEUE_GET_CONFIG_REQUEST_PACK_STR v1.2 + # '!I4x'...port, pad(4) + port = 41186 + + c = OFPQueueGetConfigRequest(Datapath, port) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.port, self.c.port) + def test_serialize(self): self.c.serialize() @@ -1867,27 +3127,22 @@ class TestOFPQueueGetConfigRequest(unittest.TestCase): res = struct.unpack(fmt, str(self.c.buf)) - eq_(ofproto_v1_2.OFP_VERSION, res[0]) - eq_(ofproto_v1_2.OFPT_QUEUE_GET_CONFIG_REQUEST, res[1]) - eq_(len(self.c.buf), res[2]) - eq_(0, res[3]) - eq_(self.port['val'], res[4]) + eq_(res[0], ofproto_v1_2.OFP_VERSION) + eq_(res[1], ofproto_v1_2.OFPT_QUEUE_GET_CONFIG_REQUEST) + eq_(res[2], len(self.c.buf)) + eq_(res[3], 0) + eq_(res[4], self.port) -class OFPQueuePropHeader(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPQueuePropHeader +class TestOFPQueuePropHeader(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPQueuePropHeader """ - # OFP_QUEUE_PROP_HEADER_PACK_STR - # '!HH4x'...property, len, pad(4) - property = {'buf': '\x00\x01', 'val': 1} - len = {'buf': '\x00\x08', 'val': ofproto_v1_2.OFP_QUEUE_PROP_HEADER_SIZE} - - buf = property['buf'] \ - + len['buf'] \ - + pack('4x') + # OFP_QUEUE_PROP_HEADER_PACK_STR = '!HH4x' + property_ = 1 + len_ = ofproto_v1_2.OFP_QUEUE_PROP_HEADER_SIZE - c = OFPQueuePropHeader(property['val'], len['val']) + c = OFPQueuePropHeader(property_, len_) def setUp(self): pass @@ -1896,8 +3151,8 @@ class OFPQueuePropHeader(unittest.TestCase): pass def test_init(self): - eq_(self.property['val'], self.c.property) - eq_(self.len['val'], self.c.len) + eq_(self.property_, self.c.property) + eq_(self.len_, self.c.len) def test_serialize(self): buf = bytearray() @@ -1906,27 +3161,57 @@ class OFPQueuePropHeader(unittest.TestCase): fmt = ofproto_v1_2.OFP_QUEUE_PROP_HEADER_PACK_STR res = struct.unpack(fmt, buffer(buf)) - eq_(self.property['val'], res[0]) - eq_(self.len['val'], res[1]) + eq_(res[0], self.property_) + eq_(res[1], self.len_) -class OFPRoleRequest(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPRoleRequest +class TestOFPQueueGetConfigReply(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPQueueGetConfigReply """ class Datapath(object): ofproto = ofproto_v1_2 ofproto_parser = ofproto_v1_2_parser - # OFP_ROLE_REQUEST_PACK_STR - # '!I4xQ'...role, pad(4), generation_id - role = {'buf': '\x00\x00\x00\x0a', 'val': 10} - generation_id = {'buf': '\x11\xa3\x72\x63\x61\xde\x39\x81', - 'val': 1270985291017894273} + # OFP_HEADER_PACK_STR + version = ofproto_v1_2.OFP_VERSION + msg_type = ofproto_v1_2.OFPT_QUEUE_GET_CONFIG_REPLY + msg_len = ofproto_v1_2.OFP_QUEUE_GET_CONFIG_REPLY_SIZE \ + + ofproto_v1_2.OFP_PACKET_QUEUE_SIZE \ + + ofproto_v1_2.OFP_QUEUE_PROP_HEADER_SIZE + xid = 2495926989 + + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + buf = pack(fmt, version, msg_type, msg_len, xid) + + # OFP_QUEUE_GET_CONFIG_REPLY_PACK_STR = '!I4x' + # OFP_QUEUE_GET_CONFIG_REPLY_SIZE = 16 + port = 65037 + + fmt = ofproto_v1_2.OFP_QUEUE_GET_CONFIG_REPLY_PACK_STR + buf += pack(fmt, port) + + # OFP_QUEUE_PROP_HEADER_PACK_STR = '!HH4x' + # OFP_QUEUE_PROP_HEADER_SIZE = 8 + property_ = ofproto_v1_2.OFPQT_MIN_RATE + properties_len = ofproto_v1_2.OFP_QUEUE_PROP_HEADER_SIZE + properties = [OFPQueuePropHeader(property_, properties_len)] + + buf_properties = bytearray() + properties[0].serialize(buf_properties, 0) - c = OFPRoleRequest(Datapath, - role['val'], - generation_id['val']) + # OFP_PACKET_QUEUE_PACK_STR = '!IIH6x' + # OFP_PACKET_QUEUE_SIZE = 16 + queue_id = 6606 + queue_port = 41186 + queue_len = ofproto_v1_2.OFP_PACKET_QUEUE_SIZE \ + + ofproto_v1_2.OFP_QUEUE_PROP_HEADER_SIZE + queues = [OFPPacketQueue(queue_id, queue_port, queue_len, properties)] + + fmt = ofproto_v1_2.OFP_PACKET_QUEUE_PACK_STR + buf += pack(fmt, queue_id, queue_port, queue_len) + buf_properties + + c = OFPQueueGetConfigReply(Datapath) def setUp(self): pass @@ -1935,13 +3220,84 @@ class OFPRoleRequest(unittest.TestCase): pass def test_init(self): - eq_(self.role['val'], self.c.role) - eq_(self.generation_id['val'], self.c.generation_id) + pass def test_parser(self): - # Not used. + res = self.c.parser(object, self.version, self.msg_type, self.msg_len, + self.xid, self.buf) + + eq_(self.version, res.version) + eq_(self.msg_type, res.msg_type) + eq_(self.msg_len, res.msg_len) + eq_(self.xid, res.xid) + + eq_(self.queue_id, res.queues[0].queue_id) + eq_(self.queue_port, res.queues[0].port) + eq_(self.queue_len, res.queues[0].len) + eq_(self.property_, res.queues[0].properties[0].property) + + +class TestOFPBarrierRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPBarrierRequest + """ + + def setUp(self): pass + def tearDown(self): + pass + + def test_init(self): + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + OFPBarrierRequest(Datapath) + + +class TestOFPBarrierReply(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPBarrierReply + """ + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + OFPBarrierReply(Datapath) + + +class TestOFPRoleRequest(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPRoleRequest + """ + + class Datapath(object): + ofproto = ofproto_v1_2 + ofproto_parser = ofproto_v1_2_parser + + # OFP_ROLE_REQUEST_PACK_STR + # '!I4xQ'...role, pad(4), generation_id + role = ofproto_v1_2.OFPCR_ROLE_NOCHANGE + generation_id = 1270985291017894273 + + c = OFPRoleRequest(Datapath, role, generation_id) + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_init(self): + eq_(self.role, self.c.role) + eq_(self.generation_id, self.c.generation_id) + def test_serialize(self): self.c.serialize() @@ -1959,12 +3315,12 @@ class OFPRoleRequest(unittest.TestCase): eq_(ofproto_v1_2.OFPT_ROLE_REQUEST, res[1]) eq_(len(self.c.buf), res[2]) eq_(0, res[3]) - eq_(self.role['val'], res[4]) - eq_(self.generation_id['val'], res[5]) + eq_(self.role, res[4]) + eq_(self.generation_id, res[5]) class TestOFPRoleReply(unittest.TestCase): - """ Test case for ofprotp_v1_2_parser.OFPRoleReply + """ Test case for ofproto_v1_2_parser.OFPRoleReply """ class Datapath(object): @@ -1983,47 +3339,558 @@ class TestOFPRoleReply(unittest.TestCase): pass def test_parser(self): - # OFP_HEADER_PACK_STR - # '!BBHI'...version, msg_type, msg_len, xid - version = {'buf': '\x01', 'val': ofproto_v1_2.OFP_VERSION} - msg_type = {'buf': '\x19', 'val': ofproto_v1_2.OFPT_ROLE_REPLY} - msg_len = {'buf': '\x00\x18', - 'val': ofproto_v1_2.OFP_ROLE_REQUEST_SIZE} - xid = {'buf': '\x94\xc4\xd2\xcd', 'val': 2495926989} + version = ofproto_v1_2.OFP_VERSION + msg_type = ofproto_v1_2.OFPT_ROLE_REPLY + msg_len = ofproto_v1_2.OFP_ROLE_REQUEST_SIZE + xid = 2495926989 - buf = version['buf'] \ - + msg_type['buf'] \ - + msg_len['buf'] \ - + xid['buf'] + fmt = ofproto_v1_2.OFP_HEADER_PACK_STR + buf = pack(fmt, version, msg_type, msg_len, xid) # OFP_ROLE_REQUEST_PACK_STR # '!I4xQ'...role, pad(4), generation_id - role = {'buf': '\x00\x00\x00\x0a', 'val': 10} - generation_id = {'buf': '\x11\xa3\x72\x63\x61\xde\x39\x81', - 'val': 1270985291017894273} + role = ofproto_v1_2.OFPCR_ROLE_NOCHANGE + generation_id = 1270985291017894273 - buf += role['buf'] \ - + pack('4x') \ - + generation_id['buf'] + fmt = ofproto_v1_2.OFP_ROLE_REQUEST_PACK_STR + buf += pack(fmt, role, generation_id) - res = OFPRoleReply.parser(object, - version['val'], - msg_type['val'], - msg_len['val'], - xid['val'], - buf) + res = OFPRoleReply.parser(object, version, msg_type, msg_len, xid, buf) # OFP_HEADER_PACK_STR - eq_(version['val'], res.version) - eq_(msg_type['val'], res.msg_type) - eq_(msg_len['val'], res.msg_len) - eq_(xid['val'], res.xid) + eq_(version, res.version) + eq_(msg_type, res.msg_type) + eq_(msg_len, res.msg_len) + eq_(xid, res.xid) # OFP_ROLE_REQUEST_PACK_STR - eq_(role['val'], res.role) - eq_(generation_id['val'], res.generation_id) + eq_(role, res.role) + eq_(generation_id, res.generation_id) - def test_serialize(self): - # Not used. + +class TestOFPMatch(unittest.TestCase): + """ Test case for ofproto_v1_2_parser.OFPMatch + """ + + def setUp(self): + pass + + def tearDown(self): pass + + def test_init(self): + pass + + def _test_serialize_and_parser(self, header, value, match): + # match_serialize + buf = bytearray() + length = match.serialize(buf, 0) + + cls_ = OFPMatchField._FIELDS_HEADERS.get(header) + fmt = '!HHI' + cls_.pack_str.replace('!', '') + res = unpack_from(fmt, buffer(buf), 0) + + if type(value) is list: + eq_(list(res)[3:], value) + else: + eq_(res[3], value) + + # match_parser + res = match.parser(str(buf), 0) + + eq_(res.type, ofproto_v1_2.OFPMT_OXM) + eq_(res.fields[0].header, header) + eq_(res.fields[0].value, value) + + def test_set_in_port(self): + header = ofproto_v1_2.OXM_OF_IN_PORT + value = in_port = 0xfff8 + + match = OFPMatch() + match.set_in_port(in_port) + + self._test_serialize_and_parser(header, value, match) + + def test_set_in_phy_port(self): + header = ofproto_v1_2.OXM_OF_IN_PHY_PORT + value = phy_port = 1 + + match = OFPMatch() + match.set_in_phy_port(phy_port) + + self._test_serialize_and_parser(header, value, match) + + def test_set_metadata(self): + header = ofproto_v1_2.OXM_OF_METADATA + value = metadata = 0x1fffffffffffff80 + + match = OFPMatch() + match.set_metadata(metadata) + + self._test_serialize_and_parser(header, value, match) + + def test_set_metadata_masked(self): + header = ofproto_v1_2.OXM_OF_METADATA_W + value = metadata = 0x1fffffffffffff80 + mask = 0xfffffffffffffff0 + + match = OFPMatch() + match.set_metadata_masked(metadata, mask) + + self._test_serialize_and_parser(header, value, match) + + def test_set_dl_dst(self): + header = ofproto_v1_2.OXM_OF_ETH_DST + value = dl_dst = mac.haddr_to_bin('e2:7a:09:79:0b:0f') + + match = OFPMatch() + match.set_dl_dst(dl_dst) + + self._test_serialize_and_parser(header, value, match) + + def test_set_dl_dst_masked(self): + header = ofproto_v1_2.OXM_OF_ETH_DST_W + value = dl_dst = mac.haddr_to_bin('e2:7a:09:79:0b:0f') + mask = mac.haddr_to_bin('ff:ff:ff:ff:ff:ff') + + match = OFPMatch() + match.set_dl_dst_masked(dl_dst, mask) + + self._test_serialize_and_parser(header, value, match) + + def test_set_dl_src(self): + header = ofproto_v1_2.OXM_OF_ETH_SRC + value = dl_src = mac.haddr_to_bin('d0:98:79:b4:75:b5') + + match = OFPMatch() + match.set_dl_src(dl_src) + + self._test_serialize_and_parser(header, value, match) + + def test_set_dl_src_masked(self): + header = ofproto_v1_2.OXM_OF_ETH_SRC_W + value = dl_src = mac.haddr_to_bin('d0:98:79:b4:75:b5') + mask = mac.haddr_to_bin('ff:ff:ff:ff:ff:ff') + + match = OFPMatch() + match.set_dl_src_masked(dl_src, mask) + + self._test_serialize_and_parser(header, value, match) + + def test_set_dl_type(self): + header = ofproto_v1_2.OXM_OF_ETH_TYPE + value = dl_type = nx_match.ETH_TYPE_IP + + match = OFPMatch() + match.set_dl_type(dl_type) + + self._test_serialize_and_parser(header, value, match) + + def test_set_vlan_vid(self): + header = ofproto_v1_2.OXM_OF_VLAN_VID + value = vid = 0b101010101010 + + match = OFPMatch() + match.set_vlan_vid(vid) + + self._test_serialize_and_parser(header, value, match) + + def test_set_vlan_vid_masked(self): + header = ofproto_v1_2.OXM_OF_VLAN_VID_W + value = vid = 0b101010101010 + mask = 0xfff + + match = OFPMatch() + match.set_vlan_vid_masked(vid, mask) + + self._test_serialize_and_parser(header, value, match) + + def test_set_vlan_pcp(self): + header = ofproto_v1_2.OXM_OF_VLAN_PCP + value = pcp = 5 + + match = OFPMatch() + match.set_vlan_pcp(pcp) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ip_dscp(self): + header = ofproto_v1_2.OXM_OF_IP_DSCP + value = ip_dscp = 36 + + match = OFPMatch() + match.set_ip_dscp(ip_dscp) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ip_ecn(self): + header = ofproto_v1_2.OXM_OF_IP_ECN + value = ip_ecn = 3 + + match = OFPMatch() + match.set_ip_ecn(ip_ecn) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ip_proto(self): + header = ofproto_v1_2.OXM_OF_IP_PROTO + value = ip_proto = 6 + + match = OFPMatch() + match.set_ip_proto(ip_proto) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ipv4_src(self): + header = ofproto_v1_2.OXM_OF_IPV4_SRC + ip = '192.168.196.250' + ipv4 = 0 + for b in ip.split("."): + ipv4 = (ipv4 << 8) | int(b) + value = ipv4_src = ipv4 + + match = OFPMatch() + match.set_ipv4_src(ipv4_src) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ipv4_src_masked(self): + header = ofproto_v1_2.OXM_OF_IPV4_SRC_W + ip = '192.168.196.250' + mask = 0xffffff00 + ipv4 = 0 + for b in ip.split("."): + ipv4 = (ipv4 << 8) | int(b) + value = ipv4_src = ipv4 + + match = OFPMatch() + match.set_ipv4_src_masked(ipv4_src, mask) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ipv4_dst(self): + header = ofproto_v1_2.OXM_OF_IPV4_DST + ip = '192.168.196.250' + ipv4 = 0 + for b in ip.split("."): + ipv4 = (ipv4 << 8) | int(b) + value = ipv4_dst = ipv4 + + match = OFPMatch() + match.set_ipv4_dst(ipv4_dst) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ipv4_dst_masked(self): + header = ofproto_v1_2.OXM_OF_IPV4_DST_W + ip = '192.168.196.250' + mask = 0xffffff00 + ipv4 = 0 + for b in ip.split("."): + ipv4 = (ipv4 << 8) | int(b) + value = ipv4_dst = ipv4 + + match = OFPMatch() + match.set_ipv4_dst_masked(ipv4_dst, mask) + + self._test_serialize_and_parser(header, value, match) + + def test_set_tcp_src(self): + header = ofproto_v1_2.OXM_OF_TCP_SRC + value = tcp_src = 1103 + + match = OFPMatch() + match.set_tcp_src(tcp_src) + + self._test_serialize_and_parser(header, value, match) + + def test_set_tcp_dst(self): + header = ofproto_v1_2.OXM_OF_TCP_DST + value = tcp_dst = 236 + + match = OFPMatch() + match.set_tcp_dst(tcp_dst) + + self._test_serialize_and_parser(header, value, match) + + def test_set_udp_src(self): + header = ofproto_v1_2.OXM_OF_UDP_SRC + value = udp_src = 56617 + + match = OFPMatch() + match.set_udp_src(udp_src) + + self._test_serialize_and_parser(header, value, match) + + def test_set_udp_dst(self): + header = ofproto_v1_2.OXM_OF_UDP_DST + value = udp_dst = 61278 + + match = OFPMatch() + match.set_udp_dst(udp_dst) + + self._test_serialize_and_parser(header, value, match) + + def test_set_sctp_src(self): + header = ofproto_v1_2.OXM_OF_SCTP_SRC + value = sctp_src = 9999 + + match = OFPMatch() + match.set_sctp_src(sctp_src) + + self._test_serialize_and_parser(header, value, match) + + def test_set_sctp_dst(self): + header = ofproto_v1_2.OXM_OF_SCTP_DST + value = sctp_dst = 1234 + + match = OFPMatch() + match.set_sctp_dst(sctp_dst) + + self._test_serialize_and_parser(header, value, match) + + def test_set_icmpv4_type(self): + header = ofproto_v1_2.OXM_OF_ICMPV4_TYPE + value = icmpv4_type = 8 + + match = OFPMatch() + match.set_icmpv4_type(icmpv4_type) + + self._test_serialize_and_parser(header, value, match) + + def test_set_icmpv4_code(self): + header = ofproto_v1_2.OXM_OF_ICMPV4_CODE + value = icmpv4_code = 1 + + match = OFPMatch() + match.set_icmpv4_code(icmpv4_code) + + self._test_serialize_and_parser(header, value, match) + + def test_set_arp_opcode(self): + header = ofproto_v1_2.OXM_OF_ARP_OP + value = arp_op = 1 + + match = OFPMatch() + match.set_arp_opcode(arp_op) + + self._test_serialize_and_parser(header, value, match) + + def test_set_arp_spa(self): + header = ofproto_v1_2.OXM_OF_ARP_SPA + ip = '192.168.227.57' + ipv4 = 0 + for b in ip.split("."): + ipv4 = (ipv4 << 8) | int(b) + value = arp_spa = ipv4 + + match = OFPMatch() + match.set_arp_spa(arp_spa) + + self._test_serialize_and_parser(header, value, match) + + def test_set_arp_spa_masked(self): + header = ofproto_v1_2.OXM_OF_ARP_SPA_W + ip = '192.168.227.57' + mask = 0xffffff00 + ipv4 = 0 + for b in ip.split("."): + ipv4 = (ipv4 << 8) | int(b) + value = arp_spa = ipv4 + + match = OFPMatch() + match.set_arp_spa_masked(arp_spa, mask) + + self._test_serialize_and_parser(header, value, match) + + def test_set_arp_tpa(self): + header = ofproto_v1_2.OXM_OF_ARP_TPA + ip = '192.168.198.233' + ipv4 = 0 + for b in ip.split("."): + ipv4 = (ipv4 << 8) | int(b) + value = arp_tpa = ipv4 + + match = OFPMatch() + match.set_arp_tpa(arp_tpa) + + self._test_serialize_and_parser(header, value, match) + + def test_set_arp_tpa_masked(self): + header = ofproto_v1_2.OXM_OF_ARP_TPA_W + ip = '192.168.198.233' + mask = 0xffffff00 + ipv4 = 0 + for b in ip.split("."): + ipv4 = (ipv4 << 8) | int(b) + value = arp_tpa = ipv4 + + match = OFPMatch() + match.set_arp_tpa_masked(arp_tpa, mask) + + self._test_serialize_and_parser(header, value, match) + + def test_set_arp_sha(self): + header = ofproto_v1_2.OXM_OF_ARP_SHA + value = arp_sha = mac.haddr_to_bin('3e:ec:13:9b:f3:0b') + + match = OFPMatch() + match.set_arp_sha(arp_sha) + + self._test_serialize_and_parser(header, value, match) + + def test_set_arp_sha_masked(self): + header = ofproto_v1_2.OXM_OF_ARP_SHA_W + value = arp_sha = mac.haddr_to_bin('3e:ec:13:9b:f3:0b') + mask = mac.haddr_to_bin('ff:ff:ff:ff:ff:ff') + + match = OFPMatch() + match.set_arp_sha_masked(arp_sha, mask) + + self._test_serialize_and_parser(header, value, match) + + def test_set_arp_tha(self): + header = ofproto_v1_2.OXM_OF_ARP_THA + value = arp_tha = mac.haddr_to_bin('83:6c:21:52:49:68') + + match = OFPMatch() + match.set_arp_tha(arp_tha) + + self._test_serialize_and_parser(header, value, match) + + def test_set_arp_tha_masked(self): + header = ofproto_v1_2.OXM_OF_ARP_THA_W + value = arp_tha = mac.haddr_to_bin('83:6c:21:52:49:68') + mask = mac.haddr_to_bin('ff:ff:ff:ff:ff:ff') + + match = OFPMatch() + match.set_arp_tha_masked(arp_tha, mask) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ipv6_src(self): + header = ofproto_v1_2.OXM_OF_IPV6_SRC + ip = '2001:db8:bd05:1d2:288a:1fc0:1:10ee' + value = ipv6_src = [int(x, 16) for x in ip.split(":")] + + match = OFPMatch() + match.set_ipv6_src(ipv6_src) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ipv6_src_masked(self): + header = ofproto_v1_2.OXM_OF_IPV6_SRC_W + ip = '2001:db8:bd05:1d2:288a:1fc0:1:10ee' + value = ipv6_src = [int(x, 16) for x in ip.split(":")] + mask = [0xffff for x in range(8)] + + match = OFPMatch() + match.set_ipv6_src_masked(ipv6_src, mask) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ipv6_dst(self): + header = ofproto_v1_2.OXM_OF_IPV6_DST + ip = 'e9e8:9ea5:7d67:82cc:ca54:1fc0:2d24:f038' + value = ipv6_dst = [int(x, 16) for x in ip.split(":")] + + match = OFPMatch() + match.set_ipv6_dst(ipv6_dst) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ipv6_dst_masked(self): + header = ofproto_v1_2.OXM_OF_IPV6_DST_W + ip = 'e9e8:9ea5:7d67:82cc:ca54:1fc0:2d24:f038' + value = ipv6_dst = [int(x, 16) for x in ip.split(":")] + mask = [0xffff for x in range(8)] + + match = OFPMatch() + match.set_ipv6_dst_masked(ipv6_dst, mask) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ipv6_flabel(self): + header = ofproto_v1_2.OXM_OF_IPV6_FLABEL + value = flabel = 0xc5384 + + match = OFPMatch() + match.set_ipv6_flabel(flabel) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ipv6_flabel_masked(self): + header = ofproto_v1_2.OXM_OF_IPV6_FLABEL_W + value = flabel = 0xc5384 + mask = 0xfffff + + match = OFPMatch() + match.set_ipv6_flabel_masked(flabel, mask) + + self._test_serialize_and_parser(header, value, match) + + def test_set_icmpv6_type(self): + header = ofproto_v1_2.OXM_OF_ICMPV6_TYPE + value = icmpv6_type = 129 + + match = OFPMatch() + match.set_icmpv6_type(icmpv6_type) + + self._test_serialize_and_parser(header, value, match) + + def test_set_icmpv6_code(self): + header = ofproto_v1_2.OXM_OF_ICMPV6_CODE + value = icmpv6_code = 1 + + match = OFPMatch() + match.set_icmpv6_code(icmpv6_code) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ipv6_nd_target(self): + header = ofproto_v1_2.OXM_OF_IPV6_ND_TARGET + ip = '5420:db3f:921b:3e33:2791:98f:dd7f:2e19' + value = target = [int(x, 16) for x in ip.split(":")] + cls_ = OFPMatchField._FIELDS_HEADERS.get(header) + + match = OFPMatch() + match.set_ipv6_nd_target(target) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ipv6_nd_sll(self): + header = ofproto_v1_2.OXM_OF_IPV6_ND_SLL + value = nd_sll = mac.haddr_to_bin('93:6d:d0:d4:e8:36') + + match = OFPMatch() + match.set_ipv6_nd_sll(nd_sll) + + self._test_serialize_and_parser(header, value, match) + + def test_set_ipv6_nd_tll(self): + header = ofproto_v1_2.OXM_OF_IPV6_ND_TLL + value = nd_tll = mac.haddr_to_bin('18:f6:66:b6:f1:b3') + + match = OFPMatch() + match.set_ipv6_nd_tll(nd_tll) + + self._test_serialize_and_parser(header, value, match) + + def test_set_mpls_label(self): + header = ofproto_v1_2.OXM_OF_MPLS_LABEL + value = mpls_label = 2144 + + match = OFPMatch() + match.set_mpls_label(mpls_label) + + self._test_serialize_and_parser(header, value, match) + + def test_set_mpls_(self): + header = ofproto_v1_2.OXM_OF_MPLS_TC + value = mpls_tc = 3 + + match = OFPMatch() + match.set_mpls_tc(mpls_tc) + + self._test_serialize_and_parser(header, value, match) |