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

HelloElemHeader.update_length()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nop 1
crap 1
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 1
        super().__init__()
45 1
        self.element_type = element_type
46 1
        self.length = length
47 1
        self.content = content
48
49 1 View Code Duplication
    def pack(self, value=None):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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 1
        if value is None:
60 1
            self.update_length()
61 1
            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 1
        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 1
        length = UBInt16()
88 1
        length.unpack(buff, offset=offset+2)
89
90 1
        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