blob: 8f08cedc64919146211eb7e367543cacd23da0a5 [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -07001:: # Copyright 2013, Big Switch Networks, Inc.
2:: #
3:: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
4:: # the following special exception:
5:: #
6:: # LOXI Exception
7:: #
8:: # As a special exception to the terms of the EPL, you may distribute libraries
9:: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
10:: # that copyright and licensing notices generated by LoxiGen are not altered or removed
11:: # from the LoxiGen Libraries and the notice provided below is (i) included in
12:: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any
13:: # documentation for the LoxiGen Libraries, if distributed in binary form.
14:: #
15:: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
16:: #
17:: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
18:: # a copy of the EPL at:
19:: #
20:: # http://www.eclipse.org/legal/epl-v10.html
21:: #
22:: # Unless required by applicable law or agreed to in writing, software
23:: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24:: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25:: # EPL for the specific language governing permissions and limitations
26:: # under the EPL.
27::
28:: import itertools
29:: include('_copyright.py')
30
31:: include('_autogen.py')
32
33import struct
34import const
35import util
36import loxi
37
38def unpack_list(buf):
39 if len(buf) % 8 != 0: raise loxi.ProtocolError("action list length not a multiple of 8")
Rich Laned4e08692013-02-20 17:40:33 -080040 def deserializer(buf):
41 type, length = struct.unpack_from("!HH", buf)
Rich Lanea06d0c32013-03-25 08:52:03 -070042 if length % 8 != 0: raise loxi.ProtocolError("action length not a multiple of 8")
Rich Lanea06d0c32013-03-25 08:52:03 -070043 parser = parsers.get(type)
44 if not parser: raise loxi.ProtocolError("unknown action type %d" % type)
Rich Laned4e08692013-02-20 17:40:33 -080045 return parser(buf)
46 return util.unpack_list(deserializer, "!2xH", buf)
Rich Lanea06d0c32013-03-25 08:52:03 -070047
48class Action(object):
49 type = None # override in subclass
50 pass
51
52:: for ofclass in ofclasses:
53:: nonskip_members = [m for m in ofclass.members if not m.skip]
54class ${ofclass.pyname}(Action):
55:: for m in ofclass.type_members:
56 ${m.name} = ${m.value}
57:: #endfor
58
59 def __init__(self, ${', '.join(["%s=None" % m.name for m in nonskip_members])}):
60:: for m in nonskip_members:
61 if ${m.name} != None:
62 self.${m.name} = ${m.name}
63 else:
64 self.${m.name} = ${m.oftype.gen_init_expr()}
65:: #endfor
66
67 def pack(self):
68 packed = []
69:: include("_pack.py", ofclass=ofclass)
70 return ''.join(packed)
71
72 @staticmethod
73 def unpack(buf):
74 obj = ${ofclass.pyname}()
75:: include("_unpack.py", ofclass=ofclass)
76 return obj
77
78 def __eq__(self, other):
79 if type(self) != type(other): return False
80 if self.type != other.type: return False
81:: for m in nonskip_members:
82 if self.${m.name} != other.${m.name}: return False
83:: #endfor
84 return True
85
86 def __ne__(self, other):
87 return not self.__eq__(other)
88
89 def show(self):
90 import loxi.pp
91 return loxi.pp.pp(self)
92
93 def pretty_print(self, q):
94:: include('_pretty_print.py', ofclass=ofclass)
95
96:: #endfor
97
98def parse_vendor(buf):
99 if len(buf) < 16:
100 raise loxi.ProtocolError("experimenter action too short")
101
102 experimenter, = struct.unpack_from("!L", buf, 4)
103 if experimenter == 0x005c16c7: # Big Switch Networks
104 subtype, = struct.unpack_from("!L", buf, 8)
105 elif experimenter == 0x00002320: # Nicira
106 subtype, = struct.unpack_from("!H", buf, 8)
107 else:
108 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
109
110 if subtype in experimenter_parsers[experimenter]:
111 return experimenter_parsers[experimenter][subtype](buf)
112 else:
113 raise loxi.ProtocolError("unexpected BSN experimenter subtype %#x" % subtype)
114
115parsers = {
116:: sort_key = lambda x: x.type_members[0].value
117:: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key)
118:: for (k, v) in msgtype_groups:
119:: v = list(v)
120:: if len(v) == 1:
121 ${k} : ${v[0].pyname}.unpack,
122:: else:
123 ${k} : parse_${k[12:].lower()},
124:: #endif
125:: #endfor
126}
127
128:: experimenter_ofclasses = [x for x in ofclasses if x.type_members[0].value == 'const.OFPAT_VENDOR']
129:: sort_key = lambda x: x.type_members[1].value
130:: experimenter_ofclasses.sort(key=sort_key)
131:: grouped = itertools.groupby(experimenter_ofclasses, sort_key)
132experimenter_parsers = {
133:: for (experimenter, v) in grouped:
134 ${experimenter} : {
135:: for ofclass in v:
136 ${ofclass.type_members[2].value}: ${ofclass.pyname}.unpack,
137:: #endfor
138 },
139:: #endfor
140}