Test Failed
Pull Request — master (#603)
by
unknown
02:35
created

StatsRequest._get_body_class()   A

Complexity

Conditions 5

Size

Total Lines 11
Code Lines 9

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 1
CRAP Score 22.5589

Importance

Changes 0
Metric Value
eloc 9
dl 11
loc 11
ccs 1
cts 9
cp 0.1111
rs 9.3333
c 0
b 0
f 0
cc 5
nop 1
crap 22.5589
1
"""Query the datapath about its current state."""
2
3
# System imports
4
5
# Third-party imports
6
7 1
from importlib import import_module
8
9 1
from pyof.foundation.base import GenericMessage
10 1
from pyof.foundation.basic_types import BinaryData, FixedTypeList, UBInt16
11
# Local imports
12 1
from pyof.v0x01.common.header import Header, Type
13 1
from pyof.v0x01.controller2switch.common import StatsType
14
15 1
__all__ = ('StatsRequest',)
16
17
18 1
class StatsRequest(GenericMessage):
19
    """Request statistics to switch."""
20
21
    #: OpenFlow :class:`~pyof.v0x01.common.header.Header`
22 1
    header = Header(message_type=Type.OFPT_STATS_REQUEST)
23 1
    body_type = UBInt16(enum_ref=StatsType)
24 1
    flags = UBInt16()
25 1
    body = BinaryData()
26
27 1
    def __init__(self, xid=None, body_type=None, flags=0, body=b''):
28
        """Create a StatsRequest with the optional parameters below.
29
30
        Args:
31
            xid (int): xid to be used on the message header.
32
            body_type (StatsType): One of the OFPST_* constants.
33
            flags (int): OFPSF_REQ_* flags (none yet defined).
34
            body (BinaryData): Body of the request.
35
        """
36
        super().__init__(xid)
37
        self.body_type = body_type
38
        self.flags = flags
39
        self.body = body
40
41 1
    def pack(self, value=None):
42
        """Pack according to :attr:`body_type`.
43
44
        Make `body` a binary pack before packing this object. Then, restore
45
        body.
46
        """
47
        backup = self.body
48
        if not value:
49
            value = self.body
50
51
        if hasattr(value, 'pack'):
52
            self.body = value.pack()
53
        stats_request_packed = super().pack()
54
55
        self.body = backup
56
        return stats_request_packed
57
58 1
    def unpack(self, buff, offset=0):
59
        """Unpack according to :attr:`body_type`."""
60
        super().unpack(buff)
61
62
        class_name = self._get_body_class()
63
        buff = self.body.value
64
        self.body = FixedTypeList(pyof_class=class_name)
65
        self.body.unpack(buff)
66
67 1 View Code Duplication
    def _get_body_class(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
68
        if isinstance(self.body_type, (int, UBInt16)):
69
            self.body_type = self.body_type.enum_ref(self.body_type.value)
70
71
        module = import_module('pyof.v0x01.controller2switch.common')
72
        body_name = self.body_type.name.replace('OFPST_', '').title()
73
74
        for class_name in module.__all__:
75
            if 'Request' in class_name and body_name in class_name:
76
                return getattr(module, class_name)
77
        return None
78