pyof.v0x01.common.header   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 3
eloc 45
dl 0
loc 96
ccs 40
cts 42
cp 0.9524
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Header.__init__() 0 12 1
A Header.__str__() 0 3 1
A Header.__repr__() 0 5 1
1
"""Defines Header classes and related items."""
2
3
# System imports
4 1
from enum import IntEnum
5
6
# Local source tree imports
7 1
from pyof.foundation.base import GenericStruct
8 1
from pyof.foundation.basic_types import UBInt8, UBInt16, UBInt32
9 1
from pyof.v0x01.common.constants import OFP_VERSION
10
11
# Third-party imports
12
13 1
__all__ = ('Header', 'Type')
14
15
# Enums
16
17
18 1
class Type(IntEnum):
19
    """Enumeration of Message Types."""
20
21
    # Symetric/Immutable messages
22 1
    OFPT_HELLO = 0
23 1
    OFPT_ERROR = 1
24 1
    OFPT_ECHO_REQUEST = 2
25 1
    OFPT_ECHO_REPLY = 3
26 1
    OFPT_VENDOR = 4
27
28
    # Switch configuration messages
29
    # Controller/Switch messages
30 1
    OFPT_FEATURES_REQUEST = 5
31 1
    OFPT_FEATURES_REPLY = 6
32 1
    OFPT_GET_CONFIG_REQUEST = 7
33 1
    OFPT_GET_CONFIG_REPLY = 8
34 1
    OFPT_SET_CONFIG = 9
35
36
    # Async messages
37 1
    OFPT_PACKET_IN = 10
38 1
    OFPT_FLOW_REMOVED = 11
39 1
    OFPT_PORT_STATUS = 12
40
41
    # Controller command messages
42
    # Controller/switch message
43 1
    OFPT_PACKET_OUT = 13
44 1
    OFPT_FLOW_MOD = 14
45 1
    OFPT_PORT_MOD = 15
46
47
    # Statistics messages
48
    # Controller/Switch message
49 1
    OFPT_STATS_REQUEST = 16
50 1
    OFPT_STATS_REPLY = 17
51
52
    # Barrier messages
53
    # Controller/Switch message
54 1
    OFPT_BARRIER_REQUEST = 18
55 1
    OFPT_BARRIER_REPLY = 19
56
57
    # Queue Configuration messages
58
    # Controller/Switch message
59 1
    OFPT_QUEUE_GET_CONFIG_REQUEST = 20
60 1
    OFPT_QUEUE_GET_CONFIG_REPLY = 21
61
62
63
# Classes
64
65
66 1
class Header(GenericStruct):
67
    """Representation of an OpenFlow message Header."""
68
69 1
    version = UBInt8(OFP_VERSION)
70 1
    message_type = UBInt8(enum_ref=Type)
71 1
    length = UBInt16()
72 1
    xid = UBInt32()
73
74 1
    def __init__(self, message_type=None, length=None, xid=None):
75
        """Create a Header with the optional parameters below.
76
77
        Args:
78
            message_type (~pyof.v0x01.common.header.Type): Type of the message.
79
            xid (int): ID of the message. Defaults to a random integer.
80
            length (int): Length of the message, including the header itself.
81
        """
82 1
        super().__init__()
83 1
        self.message_type = message_type
84 1
        self.length = length
85 1
        self.xid = xid
86
87 1
    def __str__(self):
88
        """Get just the header type. Eg.: 'OFPT_SET_CONFIG'."""
89
        return self.message_type.name
90
91 1
    def __repr__(self):
92
        """Show a full representation of the Header, including version."""
93
        return "%s(message_type=%s, length=%r, xid=%r, version=%r)" \
94
            % (self.__class__.__name__, self.message_type, self.length,
95
               self.xid, self.version)
96