1
|
|
|
"""Test main.""" |
2
|
|
|
|
3
|
|
|
from unittest import TestCase |
4
|
|
|
from unittest.mock import Mock |
5
|
|
|
|
6
|
|
|
from pyof.foundation.basic_types import DPID |
7
|
|
|
from pyof.utils import unpack |
8
|
|
|
from pyof.v0x01.controller2switch.stats_reply import StatsReply |
9
|
|
|
from pyof.v0x04.controller2switch.features_reply import FeaturesReply |
10
|
|
|
from pyof.v0x04.controller2switch.features_request import FeaturesRequest |
11
|
|
|
from pyof.v0x04.controller2switch.multipart_reply import MultipartReply |
12
|
|
|
|
13
|
|
|
from kytos.core import Controller |
14
|
|
|
from kytos.core.interface import TAG, UNI, Interface |
15
|
|
|
from kytos.core.config import KytosConfig |
16
|
|
|
from kytos.core.connection import Connection, ConnectionState |
17
|
|
|
from kytos.core.events import KytosEvent |
18
|
|
|
from kytos.core.switch import Switch |
19
|
|
|
from napps.kytos.of_core.main import Main |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class TestMain(TestCase): |
23
|
|
|
"""docstring for TestMain.""" |
24
|
|
|
|
25
|
|
|
def setUp(self): |
26
|
|
|
"""Execute steps before each tests. |
27
|
|
|
|
28
|
|
|
Set the server_name_url from kytos/of_core |
29
|
|
|
""" |
30
|
|
|
self.server_name_url = 'http://localhost:8181/api/kytos/of_core' |
31
|
|
|
self.controller = self._get_controller_mock() |
32
|
|
|
self.napp = Main(self.controller) |
33
|
|
|
self.patched_events = [] |
34
|
|
|
|
35
|
|
|
@staticmethod |
36
|
|
|
def _get_controller_mock(): |
37
|
|
|
"""Return a controller mock.""" |
38
|
|
|
options = KytosConfig().options['daemon'] |
39
|
|
|
controller = Controller(options) |
40
|
|
|
controller.log = Mock() |
41
|
|
|
return controller |
42
|
|
|
|
43
|
|
|
@staticmethod |
44
|
|
|
def _get_switch_mock(): |
45
|
|
|
"""Return a switch mock.""" |
46
|
|
|
switch = Switch('dpid') |
47
|
|
|
address = Mock() |
48
|
|
|
port = Mock() |
49
|
|
|
socket = Mock() |
50
|
|
|
switch.connection = Connection(address, port, socket) |
51
|
|
|
switch.connection.protocol.unpack = unpack |
52
|
|
|
return switch |
53
|
|
|
|
54
|
|
|
def _get_interface(self,interface_name,port, *args, **kwargs): |
55
|
|
|
"""Return a interface mock.""" |
56
|
|
|
switch1 = self._get_switch_mock |
57
|
|
|
switch1.connection = Mock() |
58
|
|
|
switch1.connection.protocol.version = 0x04 |
59
|
|
|
iface1 = Interface(interface_name,port, switch1, *args, **kwargs) |
60
|
|
|
return iface1 |
61
|
|
|
|
62
|
|
|
def test_get_event_listeners(self): |
63
|
|
|
"""Verify all event listeners registered.""" |
64
|
|
|
expected_events = [ |
65
|
|
|
'kytos/of_core.v0x01.messages.in.ofpt_stats_reply', |
66
|
|
|
'kytos/of_core.v0x0[14].messages.in.ofpt_features_reply', |
67
|
|
|
'kytos/of_core.v0x04.messages.in.ofpt_multipart_reply', |
68
|
|
|
'kytos/core.openflow.raw.in', |
69
|
|
|
'kytos/of_core.v0x0[14].messages.in.ofpt_echo_request', |
70
|
|
|
'kytos/of_core.v0x0[14].messages.out.ofpt_echo_reply', |
71
|
|
|
'kytos/of_core.v0x[0-9a-f]{2}.messages.in.hello_failed', |
72
|
|
|
'kytos/of_core.v0x0[14].messages.out.hello_failed', |
73
|
|
|
] |
74
|
|
|
|
75
|
|
|
actual_events = self.napp.listeners() |
76
|
|
|
for _event in expected_events: |
77
|
|
|
self.assertIn(_event, actual_events, '%s' % _event) |
78
|
|
|
|
79
|
|
|
def test_handle_stats_reply(self): |
80
|
|
|
"""Test handling stats reply message.""" |
81
|
|
|
event_name = 'kytos/of_core.v0x01.messages.in.ofpt_stats_reply' |
82
|
|
|
switch = self._get_switch_mock() |
83
|
|
|
switch.connection = Mock() |
84
|
|
|
switch.connection.protocol.version = 0x01 |
85
|
|
|
|
86
|
|
|
stats_data = b'\x01\x11\x00\x0c\x00\x00\x00\x01\x00\x01\x00\x01' |
87
|
|
|
stats_reply = StatsReply() |
88
|
|
|
stats_reply.unpack(stats_data[8:]) |
89
|
|
|
stats_event = KytosEvent(name=event_name, |
90
|
|
|
content={'source': switch.connection, |
91
|
|
|
'message': stats_reply}) |
92
|
|
|
self.napp.handle_stats_reply(stats_event) |
93
|
|
|
|
94
|
|
|
desc_stats_data = b'\x01\x11\x00\x0c\x00\x00\x00\x0e\x00\x00\x00\x00' |
95
|
|
|
desc_stats_reply = StatsReply() |
96
|
|
|
desc_stats_reply.unpack(desc_stats_data[8:]) |
97
|
|
|
desc_stats_event = KytosEvent(name=event_name, |
98
|
|
|
content={'source': switch.connection, |
99
|
|
|
'message': desc_stats_reply}) |
100
|
|
|
self.napp.handle_stats_reply(desc_stats_event) |
101
|
|
|
|
102
|
|
|
def test_handle_features_reply(self): |
103
|
|
|
"""Test handling features reply message.""" |
104
|
|
|
event_name = 'kytos/of_core.v0x0[14].messages.in.ofpt_features_reply' |
105
|
|
|
switch = self._get_switch_mock() |
106
|
|
|
switch.connection.protocol.version = 0x04 |
107
|
|
|
switch.connection.protocol.state = 'waiting_features_reply' |
108
|
|
|
switch.connection.state = ConnectionState.SETUP |
109
|
|
|
|
110
|
|
|
message = FeaturesReply(xid=3, |
111
|
|
|
datapath_id=DPID('00:00:00:00:00:00:00:01'), |
112
|
|
|
n_buffers=0, n_tables=254, auxiliary_id=0, |
113
|
|
|
capabilities=0x0000004f, reserved=0x00000000) |
114
|
|
|
|
115
|
|
|
event = KytosEvent(name=event_name, |
116
|
|
|
content={'source': switch.connection, |
117
|
|
|
'message': message}) |
118
|
|
|
self.napp.handle_features_reply(event) |
119
|
|
|
|
120
|
|
|
def test_handle_features_request_sent(self): |
121
|
|
|
"""Test handling features request sent message.""" |
122
|
|
|
event_name = 'kytos/of_core.v0x01.messages.out.ofpt_features_request' |
123
|
|
|
switch = self._get_switch_mock() |
124
|
|
|
switch.connection.protocol.version = 0x01 |
125
|
|
|
switch.connection.protocol.state = 'sending_features' |
126
|
|
|
|
127
|
|
|
data = b'\x04\x05\x00\x08\x00\x00\x00\x03' |
128
|
|
|
features_request = FeaturesRequest() |
129
|
|
|
features_request.unpack(data) |
130
|
|
|
|
131
|
|
|
stats_event = KytosEvent(name=event_name, |
132
|
|
|
content={'destination': switch.connection}) |
133
|
|
|
self.napp.handle_features_request_sent(stats_event) |
134
|
|
|
|
135
|
|
|
def test_handle_hello_raw_in(self): |
136
|
|
|
"""Test handling hello raw in message.""" |
137
|
|
|
event_name = 'kytos/core.openflow.raw.in' |
138
|
|
|
switch = self._get_switch_mock() |
139
|
|
|
switch.connection.protocol.version = 0x04 |
140
|
|
|
switch.connection.state = ConnectionState.NEW |
141
|
|
|
|
142
|
|
|
data = b'\x04\x00\x00\x10\x00\x00\x00\x3e' |
143
|
|
|
data += b'\x00\x01\x00\x08\x00\x00\x00\x10' |
144
|
|
|
|
145
|
|
|
stats_event = KytosEvent(name=event_name, |
146
|
|
|
content={'source': switch.connection, |
147
|
|
|
'new_data': data}) |
148
|
|
|
self.napp.handle_raw_in(stats_event) |
149
|
|
|
|
150
|
|
|
def test_handle_port_status_raw_in(self): |
151
|
|
|
"""Test handling port_status raw in message.""" |
152
|
|
|
event_name = 'kytos/core.openflow.raw.in' |
153
|
|
|
switch = self._get_switch_mock() |
154
|
|
|
switch.connection.protocol.version = 0x04 |
155
|
|
|
switch.connection.switch = self._get_switch_mock() |
156
|
|
|
switch.connection.state = ConnectionState.ESTABLISHED |
157
|
|
|
|
158
|
|
|
data = b'\x04\x0c\x00\x50\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00' |
159
|
|
|
data += b'\x00\x00\x00\x00\x01\x00\x00\x00\x00\x62\x43\xe5\xdb\x35\x0a' |
160
|
|
|
data += b'\x00\x00\x73\x31\x2d\x65\x74\x68\x31\x00\x00\x00\x00\x00\x00' |
161
|
|
|
data += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x08\x40' |
162
|
|
|
data += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x98\x96' |
163
|
|
|
data += b'\x80\x00\x00\x00\x00' |
164
|
|
|
|
165
|
|
|
stats_event = KytosEvent(name=event_name, |
166
|
|
|
content={'source': switch.connection, |
167
|
|
|
'new_data': data}) |
168
|
|
|
self.napp.handle_raw_in(stats_event) |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
|
172
|
|
|
def test_handle_multipart_reply(self): |
173
|
|
|
"""Test handling ofpt_multipart_reply.""" |
174
|
|
|
|
175
|
|
|
event_name = 'kytos/of_core.v0x04.messages.in.ofpt_multipart_reply' |
176
|
|
|
switch = self._get_switch_mock() |
177
|
|
|
switch.connection.switch = self._get_switch_mock() |
178
|
|
|
data = b'\x04\x13\x00\x68\xac\xc8\xdf\x58\x00\x01\x00\x00\x00\x00\x00\x00' |
179
|
|
|
data += b'\x00\x58\x00\x00\x00\x00\x00\x38\x25\xd9\x54\xc0\x03\xe8\x00\x00' |
180
|
|
|
data += b'\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
181
|
|
|
data += b'\x00\x00\x00\x00\x00\x00\x00\x12\x00\x00\x00\x00\x00\x00\x02\xf4' |
182
|
|
|
data += b'\x00\x01\x00\x10\x80\x00\x0a\x02\x88\xcc\x80\x00\x0c\x02\x1e\xd7' |
183
|
|
|
data += b'\x00\x04\x00\x18\x00\x00\x00\x00\x00\x00\x00\x10\xff\xff\xff\xfd' |
184
|
|
|
data += b'\xff\xff\x00\x00\x00\x00\x00\x00' |
185
|
|
|
|
186
|
|
|
multipart_reply = MultipartReply() |
187
|
|
|
multipart_reply.unpack(data[8:]) |
188
|
|
|
stats_event = KytosEvent(name=event_name, |
189
|
|
|
content={'source': switch.connection, |
190
|
|
|
'message': multipart_reply}) |
191
|
|
|
|
192
|
|
|
self.napp.handle_multipart_reply(stats_event) |
193
|
|
|
|
194
|
|
|
#test ofpmp_desc |
195
|
|
|
data = b'\x04\x12\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
196
|
|
|
multipart_desc = MultipartReply() |
197
|
|
|
multipart_desc.unpack(data[8:]) |
198
|
|
|
stats_desc_event = KytosEvent(name=event_name, |
199
|
|
|
content={'source': switch.connection, |
200
|
|
|
'message': multipart_desc}) |
201
|
|
|
|
202
|
|
|
self.napp.handle_multipart_reply(stats_desc_event) |
203
|
|
|
|
204
|
|
|
def test_handle_port_desc_multipart_reply(self): |
205
|
|
|
"""Test handling to ofpt_PORT_DESC.""" |
206
|
|
|
|
207
|
|
|
event_name = 'kytos/of_core.v0x04.messages.in.ofpt_multipart_reply' |
208
|
|
|
switch = self._get_switch_mock() |
209
|
|
|
switch.connection.switch = self._get_switch_mock() |
210
|
|
|
data=b'\x04\x13\x00\x90\x00\x00\x00\x00\x00\x0d\x00\x00\x00\x00\x00\x00' |
211
|
|
|
data+=b'\x00\x00\x00\x07\x00\x00\x00\x00\xf2\x0b\xa4\xd0\x3f\x70\x00\x00' |
212
|
|
|
data+=b'\x50\x6f\x72\x74\x37\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
213
|
|
|
data+=b'\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x28\x08\x00\x00\x28\x00' |
214
|
|
|
data+=b'\x00\x00\x28\x08\x00\x00\x28\x08\x00\x00\x13\x88\x00\x00\x13\x88' |
215
|
|
|
data+=b'\x00\x00\x00\x06\x00\x00\x00\x00\xf2\x0b\xa4\x7d\xf8\xea\x00\x00' |
216
|
|
|
data+=b'\x50\x6f\x72\x74\x36\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |
217
|
|
|
data+=b'\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x28\x08\x00\x00\x28\x00' |
218
|
|
|
data+=b'\x00\x00\x28\x08\x00\x00\x28\x08\x00\x00\x13\x88\x00\x00\x13\x88' |
219
|
|
|
|
220
|
|
|
port_desc = MultipartReply() |
221
|
|
|
port_desc.unpack(data[8:]) |
222
|
|
|
interface_1 = self._get_interface("interface1",6) |
223
|
|
|
interface_2 = self._get_interface("interface2",7) |
224
|
|
|
switch.connection.switch.interfaces = {6: interface_1,7:interface_2} |
225
|
|
|
|
226
|
|
|
stats_event = KytosEvent(name=event_name, |
227
|
|
|
content={'source': switch.connection, |
228
|
|
|
'message': port_desc}) |
229
|
|
|
self.napp.handle_multipart_reply(stats_event) |
230
|
|
|
|
231
|
|
|
#Send port_desc pack without interface |
232
|
|
|
switch = self._get_switch_mock() |
233
|
|
|
switch.connection.switch = self._get_switch_mock() |
234
|
|
|
stats_event = KytosEvent(name=event_name, |
235
|
|
|
content={'source': switch.connection, |
236
|
|
|
'message': port_desc}) |
237
|
|
|
|
238
|
|
|
self.napp.handle_multipart_reply(stats_event) |
239
|
|
|
|