| # |
| # Copyright 2012, 2013, Big Switch Networks, Inc. |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| # not use this file except in compliance with the License. You may obtain |
| # a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| # License for the specific language governing permissions and limitations |
| # under the License. |
| |
| # Automatically generated by LOXI from template util.py |
| # Do not modify |
| |
| import loxi |
| import const |
| |
| def unpack_array(deserializer, element_size, buf): |
| """ |
| Deserialize an array of fixed length elements. |
| The deserializer function should take a buffer and return the new object. |
| """ |
| if len(buf) % element_size != 0: raise loxi.ProtocolError("invalid array length") |
| n = len(buf) / element_size |
| return [deserializer(buffer(buf, i*element_size, element_size)) for i in range(n)] |
| |
| def pretty_mac(mac): |
| return ':'.join(["%02x" % x for x in mac]) |
| |
| def pretty_ipv4(v): |
| return "%d.%d.%d.%d" % ((v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF) |
| |
| def pretty_flags(v, flag_names): |
| set_flags = [] |
| for flag_name in flag_names: |
| flag_value = getattr(const, flag_name) |
| if v & flag_value == flag_value: |
| set_flags.append(flag_name) |
| elif v & flag_value: |
| set_flags.append('%s&%#x' % (flag_name, v & flag_value)) |
| v &= ~flag_value |
| if v: |
| set_flags.append("%#x" % v) |
| return '|'.join(set_flags) or '0' |
| |
| def pretty_wildcards(v): |
| if v == const.OFPFW_ALL: |
| return 'OFPFW_ALL' |
| flag_names = ['OFPFW_IN_PORT', 'OFPFW_DL_VLAN', 'OFPFW_DL_SRC', 'OFPFW_DL_DST', |
| 'OFPFW_DL_TYPE', 'OFPFW_NW_PROTO', 'OFPFW_TP_SRC', 'OFPFW_TP_DST', |
| 'OFPFW_NW_SRC_MASK', 'OFPFW_NW_DST_MASK', 'OFPFW_DL_VLAN_PCP', |
| 'OFPFW_NW_TOS'] |
| return pretty_flags(v, flag_names) |
| |
| def pretty_port(v): |
| named_ports = [(k,v2) for (k,v2) in const.__dict__.iteritems() if k.startswith('OFPP_')] |
| for (k, v2) in named_ports: |
| if v == v2: |
| return k |
| return v |