Passed
Pull Request — master (#577)
by Gleyberson
02:14
created

PacketIn.__init__()   A

Complexity

Conditions 1

Size

Total Lines 28
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 28
ccs 9
cts 9
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nop 9
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
"""For packets received by the datapath and sent to the controller."""
2
3
# System imports
4 1
from enum import IntEnum
5
6 1
from pyof.foundation.base import GenericMessage
7 1
from pyof.foundation.basic_types import (
8
    BinaryData, Pad, UBInt8, UBInt16, UBInt32, UBInt64)
9 1
from pyof.v0x04.common.flow_match import Match, OxmOfbMatchField
10 1
from pyof.v0x04.common.header import Header, Type
11
12
# Third-party imports
13
14
15 1
__all__ = ('PacketIn', 'PacketInReason')
16
17
# Enums
18
19
20 1
class PacketInReason(IntEnum):
21
    """Reason why this packet is being sent to the controller."""
22
23
    #: matching flow (table-miss flow entry).
24 1
    OFPR_NO_MATCH = 0
25
    #: Action explicitly output to controller.
26 1
    OFPR_ACTION = 1
27
    #: Packet has invalid TTL.
28 1
    OFPR_INVALID_TTL = 2
29
30
31
# Classes
32
33
34 1
class PacketIn(GenericMessage):
35
    """Packet received on port (datapath -> controller)."""
36
37
    #: :class:`~pyof.v0x04.common.header.Header`: OpenFlow Header
38 1
    header = Header(message_type=Type.OFPT_PACKET_IN)
39
    #: ID assigned by datapath.
40 1
    buffer_id = UBInt32()
41
    #: Full length of frame.
42 1
    total_len = UBInt16()
43
    #: Reason packet is being sent (one of OFPR_*),
44 1
    reason = UBInt8(enum_ref=PacketInReason)
45
    #: ID of the table that was looked up.
46 1
    table_id = UBInt8()
47
    #: Cookie of the flow entry that was looked up.
48 1
    cookie = UBInt64()
49
    #: Packet metadata. Variable size.
50 1
    match = Match()
51
    #: Align to 64 bit + 16 bit
52 1
    pad = Pad(2)
53
    #: Ethernet frame whose length is inferred from header.length.
54
    #: The padding bytes preceding the Ethernet frame ensure that the IP
55
    #: header (if any) following the Ethernet header is 32-bit aligned.
56 1
    data = BinaryData()
57
58 1
    def __init__(self, xid=None, buffer_id=None, total_len=None, reason=None,
59
                 table_id=None, cookie=None, match=None, data=b''):
60
        """Assign parameters to object attributes.
61
62
        Args:
63
            xid (int): Header's xid.
64
            buffer_id (int): ID assigned by datapath.
65
            total_len (int): Full length of frame.
66
            reason (~pyof.v0x04.asynchronous.packet_in.PacketInReason):
67
                The reason why the packet is being sent
68
            table_id (int): ID of the table that was looked up
69
            cookie (int): Cookie of the flow entry that was looked up
70
            match (:class:`~pyof.v0x04.common.flow_match.Match`):
71
                Packet metadata with variable size.
72
            data (bytes): Ethernet frame, halfway through 32-bit word, so the
73
                IP header is 32-bit aligned. The amount of data is inferred
74
                from the length field in the header. Because of padding,
75
                offsetof(struct ofp_packet_in, data) ==
76
                sizeof(struct ofp_packet_in) - 2.
77
        """
78 1
        super().__init__(xid)
79 1
        self.buffer_id = buffer_id
80 1
        self.total_len = total_len
81 1
        self.reason = reason
82 1
        self.table_id = table_id
83 1
        self.cookie = cookie
84 1
        self.match = match
85 1
        self.data = data
86
87 1
    @property
88
    def in_port(self):
89
        """Retrieve the 'in_port' that generated the PacketIn.
90
91
        This method will look for the OXM_TLV with type OFPXMT_OFB_IN_PORT on
92
        the `oxm_match_fields` field from `match` field and return its value,
93
        if the OXM exists.
94
95
        Returns:
96
            The integer number of the 'in_port' that generated the PacketIn if
97
            it exists. Otherwise return None.
98
99
        """
100 1
        in_port = self.match.get_field(OxmOfbMatchField.OFPXMT_OFB_IN_PORT)
101
        return int.from_bytes(in_port, 'big')
102