blob: 439e56ff183f11181c4dde8d1bfc0ca6660c9763 [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")
40 actions = []
41 offset = 0
42 while offset < len(buf):
43 type, length = struct.unpack_from("!HH", buf, offset)
44 if length == 0: raise loxi.ProtocolError("action length is 0")
45 if length % 8 != 0: raise loxi.ProtocolError("action length not a multiple of 8")
46 if offset + length > len(buf): raise loxi.ProtocolError("action length overruns list length")
47 parser = parsers.get(type)
48 if not parser: raise loxi.ProtocolError("unknown action type %d" % type)
49 actions.append(parser(buffer(buf, offset, length)))
50 offset += length
51 return actions
52
53class Action(object):
54 type = None # override in subclass
55 pass
56
57:: for ofclass in ofclasses:
58:: nonskip_members = [m for m in ofclass.members if not m.skip]
59class ${ofclass.pyname}(Action):
60:: for m in ofclass.type_members:
61 ${m.name} = ${m.value}
62:: #endfor
63
64 def __init__(self, ${', '.join(["%s=None" % m.name for m in nonskip_members])}):
65:: for m in nonskip_members:
66 if ${m.name} != None:
67 self.${m.name} = ${m.name}
68 else:
69 self.${m.name} = ${m.oftype.gen_init_expr()}
70:: #endfor
71
72 def pack(self):
73 packed = []
74:: include("_pack.py", ofclass=ofclass)
75 return ''.join(packed)
76
77 @staticmethod
78 def unpack(buf):
79 obj = ${ofclass.pyname}()
80:: include("_unpack.py", ofclass=ofclass)
81 return obj
82
83 def __eq__(self, other):
84 if type(self) != type(other): return False
85 if self.type != other.type: return False
86:: for m in nonskip_members:
87 if self.${m.name} != other.${m.name}: return False
88:: #endfor
89 return True
90
91 def __ne__(self, other):
92 return not self.__eq__(other)
93
94 def show(self):
95 import loxi.pp
96 return loxi.pp.pp(self)
97
98 def pretty_print(self, q):
99:: include('_pretty_print.py', ofclass=ofclass)
100
101:: #endfor
102
103def parse_vendor(buf):
104 if len(buf) < 16:
105 raise loxi.ProtocolError("experimenter action too short")
106
107 experimenter, = struct.unpack_from("!L", buf, 4)
108 if experimenter == 0x005c16c7: # Big Switch Networks
109 subtype, = struct.unpack_from("!L", buf, 8)
110 elif experimenter == 0x00002320: # Nicira
111 subtype, = struct.unpack_from("!H", buf, 8)
112 else:
113 raise loxi.ProtocolError("unexpected experimenter id %#x" % experimenter)
114
115 if subtype in experimenter_parsers[experimenter]:
116 return experimenter_parsers[experimenter][subtype](buf)
117 else:
118 raise loxi.ProtocolError("unexpected BSN experimenter subtype %#x" % subtype)
119
120parsers = {
121:: sort_key = lambda x: x.type_members[0].value
122:: msgtype_groups = itertools.groupby(sorted(ofclasses, key=sort_key), sort_key)
123:: for (k, v) in msgtype_groups:
124:: v = list(v)
125:: if len(v) == 1:
126 ${k} : ${v[0].pyname}.unpack,
127:: else:
128 ${k} : parse_${k[12:].lower()},
129:: #endif
130:: #endfor
131}
132
133:: experimenter_ofclasses = [x for x in ofclasses if x.type_members[0].value == 'const.OFPAT_VENDOR']
134:: sort_key = lambda x: x.type_members[1].value
135:: experimenter_ofclasses.sort(key=sort_key)
136:: grouped = itertools.groupby(experimenter_ofclasses, sort_key)
137experimenter_parsers = {
138:: for (experimenter, v) in grouped:
139 ${experimenter} : {
140:: for ofclass in v:
141 ${ofclass.type_members[2].value}: ${ofclass.pyname}.unpack,
142:: #endfor
143 },
144:: #endfor
145}