Test Failed
Pull Request — master (#618)
by Jose
02:30
created

pyof.utils.is_OFBAC_BAD_OUT_PORT()   A

Complexity

Conditions 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 6.7968

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 1
cts 4
cp 0.25
rs 10
c 0
b 0
f 0
cc 3
nop 1
crap 6.7968
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 1
def is_OFBAC_BAD_OUT_PORT(code):
70
    """This method check if the code is a OFPBAC_BAD_OUT_PORT."""
71
    if (code == BadActionCode01.OFPBAC_BAD_OUT_PORT or
72
        code == BadActionCode04.OFPBAC_BAD_OUT_PORT):
73
        return True
74
    else:
75
        return False
76
77 1
def get_port_config_for_version(version):
78
    """This method return port_config object using a specific version."""
79
    if version == 0x01:
80
        return PortConfig01.OFPPC_NO_FWD
81
    elif version == 0x04:
82
        return PortConfig04.OFPPC_NO_FWD
83
    else:
84
        return None
85