FlowMod.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 38
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 38
ccs 12
cts 12
cp 1
rs 9.65
c 0
b 0
f 0
cc 1
nop 12
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
"""Modifications to the flow table from the controller."""
2
3
# System imports
4 1
from enum import IntEnum
5
6 1
from pyof.foundation.base import GenericBitMask, GenericMessage
7 1
from pyof.foundation.basic_types import UBInt16, UBInt32, UBInt64
8 1
from pyof.v0x01.common.action import ListOfActions
9 1
from pyof.v0x01.common.constants import NO_BUFFER
10
# Local source tree imports
11 1
from pyof.v0x01.common.flow_match import Match
12 1
from pyof.v0x01.common.header import Header, Type
13 1
from pyof.v0x01.common.phy_port import Port
14
15
# Third-party imports
16
17 1
__all__ = ('FlowMod', 'FlowModCommand', 'FlowModFlags')
18
19
# Enums
20
21
22 1
class FlowModCommand(IntEnum):
23
    """List the possible commands for a flow."""
24
25
    #: New flow
26 1
    OFPFC_ADD = 0
27
    #: Modify all flows
28 1
    OFPFC_MODIFY = 1
29
    #: Modify entry strictly matching wildcards
30 1
    OFPFC_MODIFY_STRICT = 2
31
    #: Delete all matching flows
32 1
    OFPFC_DELETE = 3
33
    #: Strictly match wildcards and priority
34 1
    OFPFC_DELETE_STRICT = 4
35
36
37 1
class FlowModFlags(GenericBitMask):
38
    """Types to be used in Flags field."""
39
40
    #: Send flow removed message when flow expires or is deleted
41 1
    OFPFF_SEND_FLOW_REM = 1 << 0
42
    #: Check for overlapping entries first
43 1
    OFPFF_CHECK_OVERLAP = 1 << 1
44
    #: Remark this is for emergency
45 1
    OFPFF_EMERG = 1 << 2
46
47
48
# Classes
49 1
class FlowMod(GenericMessage):
50
    """Modifies the flow table from the controller."""
51
52 1
    header = Header(message_type=Type.OFPT_FLOW_MOD)
53 1
    match = Match()
54 1
    cookie = UBInt64()
55 1
    command = UBInt16(enum_ref=FlowModCommand)
56 1
    idle_timeout = UBInt16()
57 1
    hard_timeout = UBInt16()
58 1
    priority = UBInt16()
59 1
    buffer_id = UBInt32()
60 1
    out_port = UBInt16(enum_ref=Port)
61 1
    flags = UBInt16(enum_ref=FlowModFlags)
62 1
    actions = ListOfActions()
63
64 1
    def __init__(self, xid=None, match=None, cookie=0, command=None,
65
                 idle_timeout=0, hard_timeout=0, priority=0,
66
                 buffer_id=NO_BUFFER, out_port=Port.OFPP_NONE,
67
                 flags=FlowModFlags.OFPFF_SEND_FLOW_REM, actions=None):
68
        """Create a FlowMod with the optional parameters below.
69
70
        Args:
71
            xid (int): xid to be used on the message header.
72
            match (~pyof.v0x01.common.flow_match.Match): Fields to match.
73
            cookie (int): Opaque controller-issued identifier.
74
            command (~pyof.v0x01.controller2switch.flow_mod.FlowModCommand):
75
                One of OFPFC_*.
76
            idle_timeout (int): Idle time before discarding (seconds).
77
            hard_timeout (int): Max time before discarding (seconds).
78
            priority (int): Priority level of flow entry.
79
            buffer_idle (int): Buffered packet to apply to (or -1).
80
                Not meaningful for OFPFC_DELETE*.
81
            out_port (~pyof.v0x01.common.phy_port.Port):
82
                For OFPFC_DELETE* commands, require matching entries to include
83
                this as an output port. A value of OFPP_NONE indicates no
84
                restriction.
85
            flags (~pyof.v0x01.controller2switch.flow_mod.FlowModFlags):
86
                One of OFPFF_*.
87
            actions (~pyof.v0x01.common.action.ListOfActions):
88
                The action length is inferred from the length field in the
89
                header.
90
        """
91 1
        super().__init__(xid)
92 1
        self.match = match or Match()
93 1
        self.cookie = cookie
94 1
        self.command = command
95 1
        self.idle_timeout = idle_timeout
96 1
        self.hard_timeout = hard_timeout
97 1
        self.priority = priority
98 1
        self.buffer_id = buffer_id
99 1
        self.out_port = out_port
100 1
        self.flags = flags
101
        self.actions = actions or []
102