Passed
Pull Request — master (#618)
by Jose
01:51
created

pyof.utils.validate_packet()   B

Complexity

Conditions 7

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.9936

Importance

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