Passed
Pull Request — master (#618)
by Jose
02:25 queued 10s
created

pyof.utils.is_ofbac_bad_out_port()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nop 1
crap 1
1
"""General Unpack utils for python-openflow.
2
3
This package was moved from kytos/of_core for the purpose of creating a generic
4
method to perform package unpack independent of the OpenFlow version.
5
"""
6 1
from pyof import v0x01, v0x04
7 1
from pyof.foundation.exceptions import (
8
    UnpackException, UnsupportedVersionException)
9 1
from pyof.v0x01.common import utils as u_v0x01  # pylint: disable=unused-import
10 1
from pyof.v0x04.common import utils as u_v0x04  # pylint: disable=unused-import
11
12 1
PYOF_VERSION_LIBS = {0x01: v0x01,
13
                     0x04: v0x04}
14
15
16 1
def validate_packet(packet):
17
    """Check if packet is valid OF packet.
18
19
    Raises:
20
        UnpackException: If the packet is invalid.
21
22
    """
23 1
    if not isinstance(packet, bytes):
24
        raise UnpackException('invalid packet')
25
26 1
    packet_length = len(packet)
27
28 1
    if packet_length < 8 or packet_length > 2**16:
29
        raise UnpackException('invalid packet')
30
31 1
    if packet_length != int.from_bytes(packet[2:4], byteorder='big'):
32
        raise UnpackException('invalid packet')
33
34 1
    version = packet[0]
35 1
    if version == 0 or version >= 128:
36 1
        raise UnpackException('invalid packet')
37
38
39 1
def unpack(packet):
40
    """Unpack the OpenFlow Packet and returns a message.
41
42
    Args:
43
        packet: buffer with the openflow packet.
44
45
    Returns:
46
        GenericMessage: Message unpacked based on openflow packet.
47
48
    Raises:
49
        UnpackException: if the packet can't be unpacked.
50
51
    """
52 1
    validate_packet(packet)
53
54 1
    version = packet[0]
55 1
    try:
56 1
        pyof_lib = PYOF_VERSION_LIBS[version]
57
    except KeyError:
58
        raise UnpackException('Version not supported')
59
60 1
    try:
61 1
        message = pyof_lib.common.utils.unpack_message(packet)
62 1
        return message
63
    except (UnpackException, ValueError) as exception:
64
        raise UnpackException(exception)
65
66
67 1
def is_ofbac_bad_out_port(code):
68
    """Check if the code is a OFPBAC_BAD_OUT_PORT error."""
69 1
    errors = (v0x01.asynchronous.error_msg.BadActionCode.OFPBAC_BAD_OUT_PORT,
70
              v0x04.asynchronous.error_msg.BadActionCode.OFPBAC_BAD_OUT_PORT)
71 1
    return code in errors
72
73
74 1
def get_port_config_for_version(version):
75
    """Return port_config object to a specific version of python-openflow."""
76 1
    port_config = None
77
78 1
    if version == 0x01:
79 1
        port_config = v0x01.common.phy_port.PortConfig.OFPPC_NO_FWD
80 1
    elif version == 0x04:
81 1
        port_config = v0x04.common.port.PortConfig.OFPPC_NO_FWD
82
    else:
83 1
        raise UnsupportedVersionException(version)
84
85
    return port_config
86