blob: e816cc1c898756fa6ad15fca708edeb96ab0a17b [file] [log] [blame]
Dan Talaycoc85e97e2010-02-07 22:59:04 -08001import sys
Dan Talaycod7e2dbe2010-02-13 21:51:15 -08002sys.path.append('../../../src/python/oftest')
Dan Talaycoc85e97e2010-02-07 22:59:04 -08003
Dan Talaycodc881812010-02-10 22:42:12 -08004from parse import of_message_parse
5from parse import of_header_parse
Dan Talaycoc85e97e2010-02-07 22:59:04 -08006
Dan Talaycodc881812010-02-10 22:42:12 -08007from defs import *
Dan Talaycoc85e97e2010-02-07 22:59:04 -08008
Dan Talaycodc881812010-02-10 22:42:12 -08009def error_out(string):
10 print >> sys.stderr, string
11 print string
Dan Talaycoc85e97e2010-02-07 22:59:04 -080012
Dan Talaycodc881812010-02-10 22:42:12 -080013def obj_comp(orig, new, objname, errstr=None):
14 """
15 Compare two objects
16 """
17 dump = False
18 if not errstr:
19 errstr = "(unknown)"
20 errstr += " " + objname
21 if not new:
22 error_out("ERROR: obj comp, new is None for " + errstr)
23 dump = True
24 elif type(orig) != type(new):
25 error_out("ERROR: type mismatch for " + errstr + " ")
26 dump = True
27 elif orig != new:
28 error_out("ERROR: " + errstr + " orig != new")
29 dump = True
30 if dump:
31 print "Dump of mismatch for " + errstr
32 print "type orig " + str(type(orig))
33 print "orig length ", len(orig)
34 orig.show(" ")
35 if new:
36 print "type new" + str(type(new))
37 print "new length ", len(new)
38 new.show(" ")
39 print
40
41
42# Generate a long action list
43
44def action_list_create(n=10):
45 """
46 Create an action list
47
48 @param n The number of actions to put in the list
49
50 Cycle through the list of all actions, adding each type
51 """
52
53 al = action_list()
54 for i in range(n):
55 idx = i % len(action_class_list)
56 cls = action_class_list[idx]()
57 al.add(cls)
58 return al
59
60# Test classes with action lists
61def class_action_test():
62 """
63 Test objects that use action lists
64 """
65
66 print "Testing action lists: flow mod, packet out, flow stats"
67 for acount in [0, 1, 5, 16, 34]:
68 print " " + str(acount) + " actions in list"
69 obj = flow_mod()
70 obj.actions = action_list_create(acount)
71 packed = obj.pack()
72 header = of_header_parse(packed)
73 obj_check = flow_mod()
74 if obj_check.unpack(packed) != "":
75 error_out("ERROR: flow mod action list test extra " +
76 "string on unpack")
77 obj_comp(obj, obj_check, 'flow_mod', "unpack test " + str(acount))
78 obj_check = of_message_parse(packed)
79 obj_comp(obj, obj_check, 'flow_mod', "parse test " + str(acount))
80 # obj.show()
81
82 # packet out with and without data
83 obj = packet_out()
84 obj.actions = action_list_create(acount)
85 packed = obj.pack()
86 header = of_header_parse(packed)
87 obj_check = packet_out()
88 if obj_check.unpack(packed) != "":
89 error_out("ERROR: packet out packet_out test extra " +
90 "string on unpack")
91 obj_comp(obj, obj_check, 'packet_out', "unpack test " + str(acount))
92 obj_check = of_message_parse(packed)
93 obj_comp(obj, obj_check, 'packet_out', "parse test " + str(acount))
94 # obj.show()
95
96 obj = packet_out()
97 obj.actions = action_list_create(acount)
98 obj.data = "short test string for packet data"
99 packed = obj.pack()
100 header = of_header_parse(packed)
101 obj_check = packet_out()
102 if obj_check.unpack(packed) != "":
103 error_out("ERROR: packet out packet_out test extra " +
104 "string on unpack")
105 obj_comp(obj, obj_check, 'packet_out', "unpack test " + str(acount))
106 obj_check = of_message_parse(packed)
107 obj_comp(obj, obj_check, 'packet_out', "parse test " + str(acount))
108 # obj.show()
109
110 # flow stats entry (not a message)
111 obj = flow_stats_entry()
112 obj.actions = action_list_create(acount)
113 packed = obj.pack()
114 obj_check = flow_stats_entry()
115 if obj_check.unpack(packed) != "":
116 error_out("ERROR: packet out flow stats test extra " +
117 "string on unpack")
118 obj_comp(obj, obj_check, 'packet_out', "unpack test " + str(acount))
119 # obj.show()
Dan Talaycoc85e97e2010-02-07 22:59:04 -0800120
121print "Generating all classes with no data init"
122print
Dan Talaycodc881812010-02-10 22:42:12 -0800123for cls in all_objs:
Dan Talaycoc85e97e2010-02-07 22:59:04 -0800124 print "Creating class " + ofmsg_names[cls]
125 obj = cls()
126 print ofmsg_names[cls] + " length: " + str(len(obj))
127 obj.show(" ")
128 print
129
130print "End of class generation"
131print
132print
133
134print "Generating messages, packing, showing (to verify len)"
135print "and calling self unpack"
136print
Dan Talaycodc881812010-02-10 22:42:12 -0800137for cls in all_objs:
Dan Talaycoc85e97e2010-02-07 22:59:04 -0800138 print "Pack/unpack test for class " + ofmsg_names[cls]
139 obj = cls()
140 packed = obj.pack()
Dan Talaycoc85e97e2010-02-07 22:59:04 -0800141 obj_check = cls()
142 string = obj_check.unpack(packed)
Dan Talaycoc85e97e2010-02-07 22:59:04 -0800143 if string != "":
144 print >> sys.stderr, "WARNING: " + ofmsg_names[cls] + \
145 ", unpack returned string " + string
Dan Talaycodc881812010-02-10 22:42:12 -0800146 obj_comp(obj, obj_check, ofmsg_names[cls], "pack/unpack")
Dan Talaycoc85e97e2010-02-07 22:59:04 -0800147
148print "End of class pack check"
149print
Dan Talaycob9cb5482010-02-09 15:23:12 -0800150print
151
152
153print "Testing message parsing"
154print
Dan Talaycodc881812010-02-10 22:42:12 -0800155for cls in all_objs:
156 # Can only parse real messages
157 if not cls in of_messages:
158 print "Not testing " + ofmsg_names[cls]
159 continue
160 print "Parse test for class " + ofmsg_names[cls]
Dan Talaycob9cb5482010-02-09 15:23:12 -0800161 obj = cls()
Dan Talaycodc881812010-02-10 22:42:12 -0800162 packed = obj.pack()
163 header = of_header_parse(packed)
164 obj_check = of_message_parse(packed)
165 obj_comp(obj, obj_check, ofmsg_names[cls], "parse test")
Dan Talaycob9cb5482010-02-09 15:23:12 -0800166
Dan Talaycodc881812010-02-10 22:42:12 -0800167print "End of parse testing"
Dan Talaycob9cb5482010-02-09 15:23:12 -0800168print
169print
Dan Talaycoc85e97e2010-02-07 22:59:04 -0800170
Dan Talaycodc881812010-02-10 22:42:12 -0800171class_action_test()
172print
173print
Dan Talaycoc85e97e2010-02-07 22:59:04 -0800174
175#
176# TO DO
177# Generate varying actions lists and attach to flow_mod,
178# packet out and flow_stats_entry objects.
179# Generate varying lists of stats entries for replies in
180# flow_stats_reply, table_stats_reply, port_stats_reply and
181# queue_stats_reply
182# Create and test packet-to-flow function
183
184
185f = flow_stats_reply()
186ent = flow_stats_entry()
187
188
189act = action_strip_vlan()
190alist = action_list()
191alist.add(act)
192
193act = action_set_tp_dst()
194act.tp_port = 17
195
196m = ofp_match()
197m.wildcards = OFPFW_IN_PORT + OFPFW_DL_VLAN + OFPFW_DL_SRC
198
199#
200# Need: Easy reference from action to data members
201m.in_port = 12
202m.dl_src= [1,2,3,4,5,6]
203m.dl_dst= [11,22,23,24,25,26]
204m.dl_vlan = 83
205m.dl_vlan_pcp = 1
206m.dl_type = 0x12
207m.nw_tos = 3
208m.nw_proto = 0x300
209m.nw_src = 0x232323
210m.nw_dst = 0x3232123
211m.tp_src = 32
212m.tp_dst = 2
213
214m.show()
215