Rich Lane | b658ddd | 2013-03-12 10:15:10 -0700 | [diff] [blame^] | 1 | # |
| 2 | # Copyright 2012, 2013, Big Switch Networks, Inc. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | # Automatically generated by LOXI from template util.py |
| 17 | # Do not modify |
| 18 | |
| 19 | import loxi |
| 20 | import const |
| 21 | |
| 22 | def unpack_array(deserializer, element_size, buf): |
| 23 | """ |
| 24 | Deserialize an array of fixed length elements. |
| 25 | The deserializer function should take a buffer and return the new object. |
| 26 | """ |
| 27 | if len(buf) % element_size != 0: raise loxi.ProtocolError("invalid array length") |
| 28 | n = len(buf) / element_size |
| 29 | return [deserializer(buffer(buf, i*element_size, element_size)) for i in range(n)] |
| 30 | |
| 31 | def pretty_mac(mac): |
| 32 | return ':'.join(["%02x" % x for x in mac]) |
| 33 | |
| 34 | def pretty_ipv4(v): |
| 35 | return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF) |
| 36 | |
| 37 | def pretty_flags(v, flag_names): |
| 38 | set_flags = [] |
| 39 | for flag_name in flag_names: |
| 40 | flag_value = getattr(const, flag_name) |
| 41 | if v & flag_value == flag_value: |
| 42 | set_flags.append(flag_name) |
| 43 | elif v & flag_value: |
| 44 | set_flags.append('%s&%#x' % (flag_name, v & flag_value)) |
| 45 | v &= ~flag_value |
| 46 | if v: |
| 47 | set_flags.append("%#x" % v) |
| 48 | return '|'.join(set_flags) or '0' |
| 49 | |
| 50 | def pretty_wildcards(v): |
| 51 | if v == const.OFPFW_ALL: |
| 52 | return 'OFPFW_ALL' |
| 53 | flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST', |
| 54 | 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST', |
| 55 | 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP', |
| 56 | 'OFPFW_NW_TOS'] |
| 57 | return pretty_flags(v, flag_names) |
| 58 | |
| 59 | def pretty_port(v): |
| 60 | named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')] |
| 61 | for (k, v2) in named_ports: |
| 62 | if v == v2: |
| 63 | return k |
| 64 | return v |