Passed
Push — master ( 667973...d237fc )
by Beraldo
01:46
created

build.v0x01.utils   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 109
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0
wmc 8

6 Functions

Rating   Name   Duplication   Size   Complexity  
A handle_features_reply() 0 47 3
A update_flow_list() 0 15 1
A say_hello() 0 4 1
A send_set_config() 0 6 1
A send_echo() 0 7 1
A send_desc_request() 0 11 1
1
"""Utilities module for of_core OpenFlow v0x01 operations"""
2
from kytos.core import KytosEvent
3
from kytos.core.interface import Interface
4
5
from napps.kytos.of_core.utils import emit_message_out
6
7
from pyof.v0x01.controller2switch.common import ConfigFlag, FlowStatsRequest
8
from pyof.v0x01.controller2switch.set_config import SetConfig
9
from pyof.v0x01.controller2switch.stats_request import StatsRequest, StatsType
10
from pyof.v0x01.symmetric.echo_request import EchoRequest
11
from pyof.v0x01.symmetric.hello import Hello
12
13
14
def update_flow_list(controller, switch):
15
    """Method responsible for request stats of flow to switches.
16
17
    Args:
18
        controller(:class:`~kytos.core.controller.Controller`):
19
            the controller being used.
20
        switch(:class:`~kytos.core.switch.Switch`):
21
            target to send a stats request.
22
    """
23
    body = FlowStatsRequest()
24
    stats_request = StatsRequest(
25
        body_type=StatsType.OFPST_FLOW,
26
        body=body)
27
    # req.pack()
28
    emit_message_out(controller, switch.connection, stats_request)
29
30
def send_desc_request(controller, switch):
31
    """Send a description request to the switch.
32
33
    Args:
34
        controller(:class:`~kytos.core.controller.Controller`):
35
            the controller being used.
36
        switch(:class:`~kytos.core.switch.Switch`):
37
            target to send a stats request.
38
    """
39
    stats_request = StatsRequest(body_type=StatsType.OFPST_DESC)
40
    emit_message_out(controller, switch.connection, stats_request)
41
42
def handle_features_reply(controller, event):
43
    """Handle OF v0x01 features_reply message events.
44
45
    This is the end of the Handshake workflow of the OpenFlow Protocol.
46
47
    Parameters:
48
        controller (Controller): Controller being used.
49
        event (KytosEvent): Event with features reply message.
50
    """
51
52
    connection = event.source
53
    features_reply = event.content['message']
54
    dpid = features_reply.datapath_id.value
55
56
    switch = controller.get_switch_or_create(dpid=dpid,
57
                                             connection=connection)
58
59
    for port in features_reply.ports:
60
        interface = switch.get_interface_by_port_no(port.port_no.value)
61
        if interface:
62
            interface.name = port.name.value
63
            interface.address = port.hw_addr.value
64
            interface.state = port.state.value
65
            interface.features = port.curr
66
        else:
67
            interface = Interface(name=port.name.value,
68
                                  address=port.hw_addr.value,
69
                                  port_number=port.port_no.value,
70
                                  switch=switch,
71
                                  state=port.state.value,
72
                                  features=port.curr)
73
        switch.update_interface(interface)
74
        port_event = KytosEvent(name='kytos/of_core.switch.port.created',
75
                                content={
76
                                    'switch': switch.id,
77
                                    'port': port.port_no.value,
78
                                    'port_description': {
79
                                        'alias': port.name.value,
80
                                        'mac': port.hw_addr.value,
81
                                        'state': port.state.value
82
                                        }
83
                                    })
84
        controller.buffers.app.put(port_event)
85
86
    switch.update_features(features_reply)
87
88
    return switch
89
90
def send_echo(controller, switch):
91
    """Send echo request to a datapath.
92
93
    Keep the connection alive through symmetric echoes.
94
    """
95
    echo = EchoRequest(data=b'kytosd_10')
96
    emit_message_out(controller, switch.connection, echo)
97
98
def send_set_config(controller, switch):
99
    """Send a SetConfig message after the OpenFlow handshake."""
100
    set_config = SetConfig()
101
    set_config.flags = ConfigFlag.OFPC_FRAG_NORMAL
102
    set_config.miss_send_len = 0xffff #Send the whole packet
103
    emit_message_out (controller, switch.connection, set_config)
104
105
def say_hello(controller, connection):
106
    """Send back a Hello packet with the same version as the switch."""
107
    hello = Hello()
108
    emit_message_out(controller, connection, hello)
109