blob: c34f6af85b0d78bc696c2e23fcbdb9e9b862a467 [file] [log] [blame]
Rich Lane629393f2013-01-10 15:37:33 -08001"""
2OpenFlow action, instruction and bucket list classes
3"""
4
5from action import *
6from cstruct import ofp_header
7from base_list import ofp_base_list
8import copy
9
10action_object_map = {
11 OFPAT_OUTPUT : action_output,
12 OFPAT_SET_FIELD : action_set_field,
13 OFPAT_COPY_TTL_OUT : action_copy_ttl_out,
14 OFPAT_COPY_TTL_IN : action_copy_ttl_in,
15 OFPAT_SET_MPLS_TTL : action_set_mpls_ttl,
16 OFPAT_DEC_MPLS_TTL : action_dec_mpls_ttl,
17 OFPAT_PUSH_VLAN : action_push_vlan,
18 OFPAT_POP_VLAN : action_pop_vlan,
19 OFPAT_PUSH_MPLS : action_push_mpls,
20 OFPAT_POP_MPLS : action_pop_mpls,
21 OFPAT_SET_QUEUE : action_set_queue,
22 OFPAT_GROUP : action_group,
23 OFPAT_SET_NW_TTL : action_set_nw_ttl,
24 OFPAT_DEC_NW_TTL : action_dec_nw_ttl,
25 OFPAT_EXPERIMENTER : action_experimenter
26}
27
28class action_list(ofp_base_list):
29 """
30 Maintain a list of actions
31
32 Data members:
33 @arg actions: An array of action objects such as action_output, etc.
34
35 Methods:
36 @arg pack: Pack the structure into a string
37 @arg unpack: Unpack a string to objects, with proper typing
38 @arg add: Add an action to the list; you can directly access
39 the action member, but add will validate that the added object
40 is an action.
41
42 """
43
44 def __init__(self):
45 ofp_base_list.__init__(self)
46 self.actions = self.items
47 self.name = "action"
48 self.class_list = action_class_list
49
50 def unpack(self, binary_string, bytes=None):
51 """
52 Unpack a list of actions
53
54 Unpack actions from a binary string, creating an array
55 of objects of the appropriate type
56
57 @param binary_string The string to be unpacked
58
59 @param bytes The total length of the action list in bytes.
60 Ignored if decode is True. If None and decode is false, the
61 list is assumed to extend through the entire string.
62
63 @return The remainder of binary_string that was not parsed
64
65 """
66 if bytes == None:
67 bytes = len(binary_string)
68 bytes_done = 0
69 count = 0
70 cur_string = binary_string
71 while bytes_done < bytes:
72 hdr = ofp_action_header()
73 hdr.unpack(cur_string)
74 if hdr.len < OFP_ACTION_HEADER_BYTES:
75 print "ERROR: Action too short"
76 break
77 if not hdr.type in action_object_map.keys():
78 print "WARNING: Skipping unknown action ", hdr.type, hdr.len
79 else:
80 self.actions.append(action_object_map[hdr.type]())
81 self.actions[count].unpack(cur_string)
82 count += 1
83 cur_string = cur_string[hdr.len:]
84 bytes_done += hdr.len
85 return cur_string
86