blob: 5933e142434b83f30835d2b95257703a47627655 [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:: include('_copyright.py')
29
30:: include('_autogen.py')
31
32import loxi
33import const
34
35def unpack_array(deserializer, element_size, buf):
36 """
37 Deserialize an array of fixed length elements.
38 The deserializer function should take a buffer and return the new object.
39 """
40 if len(buf) % element_size != 0: raise loxi.ProtocolError("invalid array length")
41 n = len(buf) / element_size
42 return [deserializer(buffer(buf, i*element_size, element_size)) for i in range(n)]
43
44def pretty_mac(mac):
45 return ':'.join(["%02x" % x for x in mac])
46
47def pretty_ipv4(v):
48 return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF)
49
50def pretty_flags(v, flag_names):
51 set_flags = []
52 for flag_name in flag_names:
53 flag_value = getattr(const, flag_name)
54 if v & flag_value == flag_value:
55 set_flags.append(flag_name)
56 elif v & flag_value:
57 set_flags.append('%s&%#x' % (flag_name, v & flag_value))
58 v &= ~flag_value
59 if v:
60 set_flags.append("%#x" % v)
61 return '|'.join(set_flags) or '0'
62
63def pretty_wildcards(v):
64 if v == const.OFPFW_ALL:
65 return 'OFPFW_ALL'
66 flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST',
67 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST',
68 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP',
69 'OFPFW_NW_TOS']
70 return pretty_flags(v, flag_names)
71
72def pretty_port(v):
73 named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')]
74 for (k, v2) in named_ports:
75 if v == v2:
76 return k
77 return v