blob: 878050bbfac3860b3ab6703c868b9fb529766a7b [file] [log] [blame]
Rich Lane5ec97b82013-01-06 18:04:25 -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_VLAN_VID : action_set_vlan_vid,
13 OFPAT_SET_VLAN_PCP : action_set_vlan_pcp,
14 OFPAT_SET_DL_SRC : action_set_dl_src,
15 OFPAT_SET_DL_DST : action_set_dl_dst,
16 OFPAT_SET_NW_SRC : action_set_nw_src,
17 OFPAT_SET_NW_DST : action_set_nw_dst,
18 OFPAT_SET_NW_TOS : action_set_nw_tos,
19 OFPAT_SET_NW_ECN : action_set_nw_ecn,
20 OFPAT_SET_TP_SRC : action_set_tp_src,
21 OFPAT_SET_TP_DST : action_set_tp_dst,
22 OFPAT_COPY_TTL_OUT : action_copy_ttl_out,
23 OFPAT_COPY_TTL_IN : action_copy_ttl_in,
24 OFPAT_SET_MPLS_LABEL : action_set_mpls_label,
25 OFPAT_SET_MPLS_TC : action_set_mpls_tc,
26 OFPAT_SET_MPLS_TTL : action_set_mpls_ttl,
27 OFPAT_DEC_MPLS_TTL : action_dec_mpls_ttl,
28 OFPAT_PUSH_VLAN : action_push_vlan,
29 OFPAT_POP_VLAN : action_pop_vlan,
30 OFPAT_PUSH_MPLS : action_push_mpls,
31 OFPAT_POP_MPLS : action_pop_mpls,
32 OFPAT_SET_QUEUE : action_set_queue,
33 OFPAT_GROUP : action_group,
34 OFPAT_SET_NW_TTL : action_set_nw_ttl,
35 OFPAT_DEC_NW_TTL : action_dec_nw_ttl,
36 OFPAT_EXPERIMENTER : action_experimenter
37}
38
39class action_list(ofp_base_list):
40 """
41 Maintain a list of actions
42
43 Data members:
44 @arg actions: An array of action objects such as action_output, etc.
45
46 Methods:
47 @arg pack: Pack the structure into a string
48 @arg unpack: Unpack a string to objects, with proper typing
49 @arg add: Add an action to the list; you can directly access
50 the action member, but add will validate that the added object
51 is an action.
52
53 """
54
55 def __init__(self):
56 ofp_base_list.__init__(self)
57 self.actions = self.items
58 self.name = "action"
59 self.class_list = action_class_list
60
61 def unpack(self, binary_string, bytes=None):
62 """
63 Unpack a list of actions
64
65 Unpack actions from a binary string, creating an array
66 of objects of the appropriate type
67
68 @param binary_string The string to be unpacked
69
70 @param bytes The total length of the action list in bytes.
71 Ignored if decode is True. If None and decode is false, the
72 list is assumed to extend through the entire string.
73
74 @return The remainder of binary_string that was not parsed
75
76 """
77 if bytes == None:
78 bytes = len(binary_string)
79 bytes_done = 0
80 count = 0
81 cur_string = binary_string
82 while bytes_done < bytes:
83 hdr = ofp_action_header()
84 hdr.unpack(cur_string)
85 if hdr.len < OFP_ACTION_HEADER_BYTES:
86 print "ERROR: Action too short"
87 break
88 if not hdr.type in action_object_map.keys():
89 print "WARNING: Skipping unknown action ", hdr.type, hdr.len
90 else:
91 self.actions.append(action_object_map[hdr.type]())
92 self.actions[count].unpack(cur_string)
93 count += 1
94 cur_string = cur_string[hdr.len:]
95 bytes_done += hdr.len
96 return cur_string
97