Passed
Push — master ( e83ad1...928ac1 )
by Beraldo
01:26
created

HelloElemHeader.pack()   A

Complexity

Conditions 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 9.0292

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 19
rs 9.4285
c 1
b 0
f 0
ccs 1
cts 8
cp 0.125
cc 3
crap 9.0292
1
"""Defines Hello message."""
2
3
# System imports
4
5 1
from enum import IntEnum
6
7 1
from pyof.foundation.base import GenericMessage, GenericStruct
8 1
from pyof.foundation.basic_types import BinaryData, FixedTypeList, UBInt16
9 1
from pyof.foundation.exceptions import PackException
10 1
from pyof.v0x04.common.header import Header, Type
11
12
# Third-party imports
13
14 1
__all__ = ('Hello', 'HelloElemHeader', 'HelloElemType', 'ListOfHelloElements')
15
16
# Enums
17
18
19 1
class HelloElemType(IntEnum):
20
    """Hello element types."""
21
22
    #: Bitmap of version supported.
23 1
    OFPHET_VERSIONBITMAP = 1
24
25
26
# Classes
27
28
29 1
class HelloElemHeader(GenericStruct):
30
    """Common header for all Hello Elements."""
31
32 1
    element_type = UBInt16()
33 1
    length = UBInt16()
34 1
    content = BinaryData()
35
36 1
    def __init__(self, element_type=None, length=None, content=b''):
37
        """Create a HelloElemHeader with the optional parameters below.
38
39
        Args:
40
            element_type: One of OFPHET_*.
41
            length: Length in bytes of the element, including this header,
42
                excluding padding.
43
        """
44
        super().__init__()
45
        self.element_type = element_type
46
        self.length = length
47
        self.content = content
48
49 1
    def pack(self, value=None):
50
        """Update the length and pack the massege into binary data.
51
52
        Returns:
53
            bytes: A binary data that represents the Message.
54
55
        Raises:
56
            Exception: If there are validation errors.
57
58
        """
59
        if value is None:
60
            self.update_length()
61
            return super().pack()
62
        elif isinstance(value, type(self)):
63
            return value.pack()
64
        else:
65
            msg = "{} is not an instance of {}".format(value,
66
                                                       type(self).__name__)
67
            raise PackException(msg)
68
69 1
    def update_length(self):
70
        """Update length attribute."""
71
        self.length = self.get_size()
72
73 1
    def unpack(self, buff=None, offset=0):
74
        """Unpack *buff* into this object.
75
76
        This method will convert a binary data into a readable value according
77
        to the attribute format.
78
79
        Args:
80
            buff (bytes): Binary buffer.
81
            offset (int): Where to begin unpacking.
82
83
        Raises:
84
            :exc:`~.exceptions.UnpackException`: If unpack fails.
85
86
        """
87
        length = UBInt16()
88
        length.unpack(buff, offset=offset+2)
89
90
        super().unpack(buff[:offset+length.value], offset)
91
92
93 1
class ListOfHelloElements(FixedTypeList):
94
    """List of Hello elements.
95
96
    Represented by instances of HelloElemHeader and used on Hello
97
    objects.
98
    """
99
100 1
    def __init__(self, items=None):
101
        """Create a ListOfHelloElements with the optional parameters below.
102
103
        Args:
104
            items (HelloElemHeader): Instance or a list of instances.
105
        """
106 1
        super().__init__(pyof_class=HelloElemHeader, items=items)
107
108
109 1
class Hello(GenericMessage):
110
    """OpenFlow Hello Message OFPT_HELLO.
111
112
    This message includes zero or more hello elements having variable size.
113
    Unknown element types must be ignored/skipped, to allow for future
114
    extensions.
115
    """
116
117 1
    header = Header(message_type=Type.OFPT_HELLO)
118
    #: Hello element list
119 1
    elements = ListOfHelloElements()
120
121 1
    def __init__(self, xid=None, elements=None):
122
        """Create a Hello with the optional parameters below.
123
124
        Args:
125
            xid (int): xid to be used on the message header.
126
            elements: List of elements - 0 or more
127
        """
128 1
        super().__init__(xid)
129
        self.elements = elements
130