Rich Lane | c2ee4b8 | 2013-04-24 17:12:38 -0700 | [diff] [blame] | 1 | # Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior University |
| 2 | # Copyright (c) 2011, 2012 Open Networking Foundation |
| 3 | # Copyright (c) 2012, 2013 Big Switch Networks, Inc. |
Dan Talayco | f620225 | 2013-07-02 01:00:29 -0700 | [diff] [blame] | 4 | # See the file LICENSE.pyloxi which should have been included in the source distribution |
Rich Lane | b658ddd | 2013-03-12 10:15:10 -0700 | [diff] [blame] | 5 | |
| 6 | # Automatically generated by LOXI from template util.py |
| 7 | # Do not modify |
| 8 | |
| 9 | import loxi |
| 10 | import const |
Rich Lane | c2ee4b8 | 2013-04-24 17:12:38 -0700 | [diff] [blame] | 11 | import struct |
Rich Lane | b658ddd | 2013-03-12 10:15:10 -0700 | [diff] [blame] | 12 | |
| 13 | def pretty_mac(mac): |
| 14 | return ':'.join(["%02x" % x for x in mac]) |
| 15 | |
| 16 | def pretty_ipv4(v): |
| 17 | return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF) |
| 18 | |
| 19 | def pretty_flags(v, flag_names): |
| 20 | set_flags = [] |
| 21 | for flag_name in flag_names: |
| 22 | flag_value = getattr(const, flag_name) |
| 23 | if v & flag_value == flag_value: |
| 24 | set_flags.append(flag_name) |
| 25 | elif v & flag_value: |
| 26 | set_flags.append('%s&%#x' % (flag_name, v & flag_value)) |
| 27 | v &= ~flag_value |
| 28 | if v: |
| 29 | set_flags.append("%#x" % v) |
| 30 | return '|'.join(set_flags) or '0' |
| 31 | |
| 32 | def pretty_wildcards(v): |
| 33 | if v == const.OFPFW_ALL: |
| 34 | return 'OFPFW_ALL' |
| 35 | flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST', |
| 36 | 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST', |
| 37 | 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP', |
| 38 | 'OFPFW_NW_TOS'] |
| 39 | return pretty_flags(v, flag_names) |
| 40 | |
| 41 | def pretty_port(v): |
| 42 | named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')] |
| 43 | for (k, v2) in named_ports: |
| 44 | if v == v2: |
| 45 | return k |
| 46 | return v |
Dan Talayco | f620225 | 2013-07-02 01:00:29 -0700 | [diff] [blame] | 47 | |
| 48 | def pack_port_no(value): |
| 49 | return struct.pack("!H", value) |
| 50 | |
| 51 | def unpack_port_no(reader): |
| 52 | return reader.read("!H")[0] |
| 53 | |
| 54 | def pack_fm_cmd(value): |
| 55 | return struct.pack("!H", value) |
| 56 | |
| 57 | def unpack_fm_cmd(reader): |
| 58 | return reader.read("!H")[0] |
| 59 | |
| 60 | def init_wc_bmap(): |
| 61 | return const.OFPFW_ALL |
| 62 | |
| 63 | def pack_wc_bmap(value): |
| 64 | return struct.pack("!L", value) |
| 65 | |
| 66 | def unpack_wc_bmap(reader): |
| 67 | return reader.read("!L")[0] |
| 68 | |
| 69 | def init_match_bmap(): |
| 70 | return const.OFPFW_ALL |
| 71 | |
| 72 | def pack_match_bmap(value): |
| 73 | return struct.pack("!L", value) |
| 74 | |
| 75 | def unpack_match_bmap(reader): |
| 76 | return reader.read("!L")[0] |
| 77 | |
| 78 | def pack_list(values): |
| 79 | return "".join([x.pack() for x in values]) |