blob: 059e3905340171db3f9dafb2a41ad1646639cc2f [file] [log] [blame]
Rich Lane629393f2013-01-10 15:37:33 -08001
2# Python OpenFlow action wrapper classes
3
4from cstruct import *
5from match import roundup
6from match_list import match_list
7
Rich Lane63393492013-01-11 09:21:12 -08008class pop_mpls(ofp_action_pop_mpls):
Rich Lane629393f2013-01-10 15:37:33 -08009 """
10 Wrapper class for pop_mpls action object
11
12 Data members inherited from ofp_action_pop_mpls:
13 @arg type
14 @arg len
15 @arg ethertype
16
17 """
Rich Lane5de2d942013-01-11 11:49:36 -080018 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -080019 ofp_action_pop_mpls.__init__(self)
20 self.type = OFPAT_POP_MPLS
21 self.len = self.__len__()
Rich Lane5de2d942013-01-11 11:49:36 -080022 for (k, v) in kwargs.items():
23 if hasattr(self, k):
24 setattr(self, k, v)
25 else:
26 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -080027 def show(self, prefix=''):
28 outstr = prefix + "action_pop_mpls\n"
29 outstr += ofp_action_pop_mpls.show(self, prefix)
30 return outstr
31
32
Rich Lane63393492013-01-11 09:21:12 -080033class push_vlan(ofp_action_push):
Rich Lane629393f2013-01-10 15:37:33 -080034 """
35 Wrapper class for push_vlan action object
36
37 Data members inherited from ofp_action_push:
38 @arg type
39 @arg len
40 @arg ethertype
41
42 """
Rich Lane5de2d942013-01-11 11:49:36 -080043 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -080044 ofp_action_push.__init__(self)
45 self.type = OFPAT_PUSH_VLAN
46 self.len = self.__len__()
Rich Lane5de2d942013-01-11 11:49:36 -080047 for (k, v) in kwargs.items():
48 if hasattr(self, k):
49 setattr(self, k, v)
50 else:
51 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -080052 def show(self, prefix=''):
53 outstr = prefix + "action_push_vlan\n"
54 outstr += ofp_action_push.show(self, prefix)
55 return outstr
56
57
Rich Lane63393492013-01-11 09:21:12 -080058class experimenter(ofp_action_experimenter_header):
Rich Lane629393f2013-01-10 15:37:33 -080059 """
60 Wrapper class for experimenter action object
61
62 Data members inherited from ofp_action_experimenter_header:
63 @arg type
64 @arg len
65 @arg experimenter
66
67 """
Rich Lane5de2d942013-01-11 11:49:36 -080068 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -080069 ofp_action_experimenter_header.__init__(self)
70 self.type = OFPAT_EXPERIMENTER
71 self.len = self.__len__()
Rich Lane5de2d942013-01-11 11:49:36 -080072 for (k, v) in kwargs.items():
73 if hasattr(self, k):
74 setattr(self, k, v)
75 else:
76 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -080077 def show(self, prefix=''):
78 outstr = prefix + "action_experimenter\n"
79 outstr += ofp_action_experimenter_header.show(self, prefix)
80 return outstr
81
82
Rich Lane63393492013-01-11 09:21:12 -080083class dec_mpls_ttl(ofp_action_header):
Rich Lane629393f2013-01-10 15:37:33 -080084 """
85 Wrapper class for dec_mpls_ttl action object
86
87 Data members inherited from ofp_action_header:
88 @arg type
89 @arg len
90
91 """
Rich Lane5de2d942013-01-11 11:49:36 -080092 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -080093 ofp_action_header.__init__(self)
94 self.type = OFPAT_DEC_MPLS_TTL
95 self.len = self.__len__()
Rich Lane5de2d942013-01-11 11:49:36 -080096 for (k, v) in kwargs.items():
97 if hasattr(self, k):
98 setattr(self, k, v)
99 else:
100 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -0800101 def show(self, prefix=''):
102 outstr = prefix + "action_dec_mpls_ttl\n"
103 outstr += ofp_action_header.show(self, prefix)
104 return outstr
105
106
Rich Lane63393492013-01-11 09:21:12 -0800107class set_nw_ttl(ofp_action_nw_ttl):
Rich Lane629393f2013-01-10 15:37:33 -0800108 """
109 Wrapper class for set_nw_ttl action object
110
111 Data members inherited from ofp_action_nw_ttl:
112 @arg type
113 @arg len
114 @arg nw_ttl
115
116 """
Rich Lane5de2d942013-01-11 11:49:36 -0800117 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -0800118 ofp_action_nw_ttl.__init__(self)
119 self.type = OFPAT_SET_NW_TTL
120 self.len = self.__len__()
Rich Lane5de2d942013-01-11 11:49:36 -0800121 for (k, v) in kwargs.items():
122 if hasattr(self, k):
123 setattr(self, k, v)
124 else:
125 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -0800126 def show(self, prefix=''):
127 outstr = prefix + "action_set_nw_ttl\n"
128 outstr += ofp_action_nw_ttl.show(self, prefix)
129 return outstr
130
131
Rich Lane63393492013-01-11 09:21:12 -0800132class copy_ttl_in(ofp_action_header):
Rich Lane629393f2013-01-10 15:37:33 -0800133 """
134 Wrapper class for copy_ttl_in action object
135
136 Data members inherited from ofp_action_header:
137 @arg type
138 @arg len
139
140 """
Rich Lane5de2d942013-01-11 11:49:36 -0800141 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -0800142 ofp_action_header.__init__(self)
143 self.type = OFPAT_COPY_TTL_IN
144 self.len = self.__len__()
Rich Lane5de2d942013-01-11 11:49:36 -0800145 for (k, v) in kwargs.items():
146 if hasattr(self, k):
147 setattr(self, k, v)
148 else:
149 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -0800150 def show(self, prefix=''):
151 outstr = prefix + "action_copy_ttl_in\n"
152 outstr += ofp_action_header.show(self, prefix)
153 return outstr
154
155
Rich Lane63393492013-01-11 09:21:12 -0800156class group(ofp_action_group):
Rich Lane629393f2013-01-10 15:37:33 -0800157 """
158 Wrapper class for group action object
159
160 Data members inherited from ofp_action_group:
161 @arg type
162 @arg len
163 @arg group_id
164
165 """
Rich Lane5de2d942013-01-11 11:49:36 -0800166 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -0800167 ofp_action_group.__init__(self)
168 self.type = OFPAT_GROUP
169 self.len = self.__len__()
Rich Lane5de2d942013-01-11 11:49:36 -0800170 for (k, v) in kwargs.items():
171 if hasattr(self, k):
172 setattr(self, k, v)
173 else:
174 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -0800175 def show(self, prefix=''):
176 outstr = prefix + "action_group\n"
177 outstr += ofp_action_group.show(self, prefix)
178 return outstr
179 def __len__(self):
180 return roundup(4 + 4,8)
181
182
Rich Lane63393492013-01-11 09:21:12 -0800183class set_queue(ofp_action_set_queue):
Rich Lane629393f2013-01-10 15:37:33 -0800184 """
185 Wrapper class for set_queue action object
186
187 Data members inherited from ofp_action_set_queue:
188 @arg type
189 @arg len
190 @arg queue_id
191
192 """
Rich Lane5de2d942013-01-11 11:49:36 -0800193 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -0800194 ofp_action_set_queue.__init__(self)
195 self.type = OFPAT_SET_QUEUE
196 self.len = self.__len__()
Rich Lane5de2d942013-01-11 11:49:36 -0800197 for (k, v) in kwargs.items():
198 if hasattr(self, k):
199 setattr(self, k, v)
200 else:
201 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -0800202 def show(self, prefix=''):
203 outstr = prefix + "action_set_queue\n"
204 outstr += ofp_action_set_queue.show(self, prefix)
205 return outstr
206
207
Rich Lane63393492013-01-11 09:21:12 -0800208class push_mpls(ofp_action_push):
Rich Lane629393f2013-01-10 15:37:33 -0800209 """
210 Wrapper class for push_mpls action object
211
212 Data members inherited from ofp_action_push:
213 @arg type
214 @arg len
215 @arg ethertype
216
217 """
Rich Lane5de2d942013-01-11 11:49:36 -0800218 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -0800219 ofp_action_push.__init__(self)
220 self.type = OFPAT_PUSH_MPLS
221 self.len = self.__len__()
Rich Lane5de2d942013-01-11 11:49:36 -0800222 for (k, v) in kwargs.items():
223 if hasattr(self, k):
224 setattr(self, k, v)
225 else:
226 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -0800227 def show(self, prefix=''):
228 outstr = prefix + "action_push_mpls\n"
229 outstr += ofp_action_push.show(self, prefix)
230 return outstr
231
232
Rich Lane63393492013-01-11 09:21:12 -0800233class copy_ttl_out(ofp_action_header):
Rich Lane629393f2013-01-10 15:37:33 -0800234 """
235 Wrapper class for copy_ttl_out action object
236
237 Data members inherited from ofp_action_header:
238 @arg type
239 @arg len
240
241 """
Rich Lane5de2d942013-01-11 11:49:36 -0800242 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -0800243 ofp_action_header.__init__(self)
244 self.type = OFPAT_COPY_TTL_OUT
245 self.len = self.__len__()
Rich Lane5de2d942013-01-11 11:49:36 -0800246 for (k, v) in kwargs.items():
247 if hasattr(self, k):
248 setattr(self, k, v)
249 else:
250 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -0800251 def show(self, prefix=''):
252 outstr = prefix + "action_copy_ttl_out\n"
253 outstr += ofp_action_header.show(self, prefix)
254 return outstr
255
256
Rich Lane63393492013-01-11 09:21:12 -0800257class set_field(ofp_action_set_field):
Rich Lane629393f2013-01-10 15:37:33 -0800258 """
259 Wrapper class for set_field action object
260
261 Data members inherited from ofp_action_set_field:
262 @arg type
263 @arg len
264 @arg field
265
266 """
Rich Lane5de2d942013-01-11 11:49:36 -0800267 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -0800268 ofp_action_set_field.__init__(self)
269 self.type = OFPAT_SET_FIELD
270 self.len = self.__len__()
271 self.field = match_list()
Rich Lane5de2d942013-01-11 11:49:36 -0800272 for (k, v) in kwargs.items():
273 if hasattr(self, k):
274 setattr(self, k, v)
275 else:
276 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -0800277
278 def pack(self):
279 packed = ""
280 if len(self.field) <= 4:
281 packed += ofp_action_set_field.pack()
282 else:
283 self.len = len(self)
284 packed += struct.pack("!HH", self.type, self.len)
285 packed += self.field.pack()
286 padding_size = roundup(len(self.field) -4,8) - (len(self.field) -4)
287 if padding_size:
288 padding = [0] * padding_size
289 packed += struct.pack("!" + str(padding_size) + "B", *padding)
290 return packed
291
292 def unpack(self, binary_string):
293 if len(binary_string) <= 8:
294 binary_string = ofp_action_set_field.unpack(self)
295 else:
296 (self.type, self.len) = struct.unpack("!HH", binary_string[0:4])
297 binary_string = binary_string[4:]
298 binary_string = self.field.unpack(binary_string, bytes = self.len - 4)
299 padding_size = roundup(len(self.field) -4,8) - (len(self.field) -4)
300 if padding_size:
301 binary_string = binary_string[padding_size:]
302 return binary_string
303
304 def show(self, prefix=''):
305 outstr = prefix + "action_set_field\n"
306 outstr += ofp_action_set_field.show(self, prefix)
307 return outstr
308
309 def __len__(self):
310 return roundup(4 + len(self.field),8)
311
312
Rich Lane63393492013-01-11 09:21:12 -0800313class set_mpls_ttl(ofp_action_mpls_ttl):
Rich Lane629393f2013-01-10 15:37:33 -0800314 """
315 Wrapper class for set_mpls_ttl action object
316
317 Data members inherited from ofp_action_mpls_ttl:
318 @arg type
319 @arg len
320 @arg mpls_ttl
321
322 """
Rich Lane5de2d942013-01-11 11:49:36 -0800323 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -0800324 ofp_action_mpls_ttl.__init__(self)
325 self.type = OFPAT_SET_MPLS_TTL
326 self.len = self.__len__()
Rich Lane5de2d942013-01-11 11:49:36 -0800327 for (k, v) in kwargs.items():
328 if hasattr(self, k):
329 setattr(self, k, v)
330 else:
331 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -0800332 def show(self, prefix=''):
333 outstr = prefix + "action_set_mpls_ttl\n"
334 outstr += ofp_action_mpls_ttl.show(self, prefix)
335 return outstr
336
337
Rich Lane63393492013-01-11 09:21:12 -0800338class pop_vlan(ofp_action_header):
Rich Lane629393f2013-01-10 15:37:33 -0800339 """
340 Wrapper class for pop_vlan action object
341
342 Data members inherited from ofp_action_header:
343 @arg type
344 @arg len
345
346 """
Rich Lane5de2d942013-01-11 11:49:36 -0800347 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -0800348 ofp_action_header.__init__(self)
349 self.type = OFPAT_POP_VLAN
350 self.len = self.__len__()
Rich Lane5de2d942013-01-11 11:49:36 -0800351 for (k, v) in kwargs.items():
352 if hasattr(self, k):
353 setattr(self, k, v)
354 else:
355 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -0800356 def show(self, prefix=''):
357 outstr = prefix + "action_pop_vlan\n"
358 outstr += ofp_action_header.show(self, prefix)
359 return outstr
360
361
Rich Lane63393492013-01-11 09:21:12 -0800362class dec_nw_ttl(ofp_action_header):
Rich Lane629393f2013-01-10 15:37:33 -0800363 """
364 Wrapper class for dec_nw_ttl action object
365
366 Data members inherited from ofp_action_header:
367 @arg type
368 @arg len
369
370 """
Rich Lane5de2d942013-01-11 11:49:36 -0800371 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -0800372 ofp_action_header.__init__(self)
373 self.type = OFPAT_DEC_NW_TTL
374 self.len = self.__len__()
Rich Lane5de2d942013-01-11 11:49:36 -0800375 for (k, v) in kwargs.items():
376 if hasattr(self, k):
377 setattr(self, k, v)
378 else:
379 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -0800380 def show(self, prefix=''):
381 outstr = prefix + "action_dec_nw_ttl\n"
382 outstr += ofp_action_header.show(self, prefix)
383 return outstr
384
385
Rich Lane63393492013-01-11 09:21:12 -0800386class output(ofp_action_output):
Rich Lane629393f2013-01-10 15:37:33 -0800387 """
388 Wrapper class for output action object
389
390 Data members inherited from ofp_action_output:
391 @arg type
392 @arg len
393 @arg port
394 @arg max_len
395
396 """
Rich Lane5de2d942013-01-11 11:49:36 -0800397 def __init__(self, **kwargs):
Rich Lane629393f2013-01-10 15:37:33 -0800398 ofp_action_output.__init__(self)
399 self.type = OFPAT_OUTPUT
400 self.len = self.__len__()
Rich Lane5de2d942013-01-11 11:49:36 -0800401 for (k, v) in kwargs.items():
402 if hasattr(self, k):
403 setattr(self, k, v)
404 else:
405 raise NameError("field %s does not exist in %s" % (k, self.__class__))
Rich Lane629393f2013-01-10 15:37:33 -0800406 def show(self, prefix=''):
407 outstr = prefix + "action_output\n"
408 outstr += ofp_action_output.show(self, prefix)
409 return outstr
410
411action_class_list = (
Rich Lane63393492013-01-11 09:21:12 -0800412 copy_ttl_in,
413 copy_ttl_out,
414 dec_mpls_ttl,
415 dec_nw_ttl,
416 experimenter,
417 group,
418 output,
419 pop_mpls,
420 pop_vlan,
421 push_mpls,
422 push_vlan,
423 set_field,
424 set_mpls_ttl,
425 set_nw_ttl,
426 set_queue)
Rich Lane629393f2013-01-10 15:37:33 -0800427