Passed
Push — master ( 75d007...4d9626 )
by Humberto
01:10 queued 12s
created

build.v0x01.utils   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

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

6 Functions

Rating   Name   Duplication   Size   Complexity  
A say_hello() 0 4 1
A handle_features_reply() 0 45 3
A update_flow_list() 0 15 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 1
from pyof.v0x01.controller2switch.common import ConfigFlag, FlowStatsRequest
3 1
from pyof.v0x01.controller2switch.set_config import SetConfig
4 1
from pyof.v0x01.controller2switch.stats_request import StatsRequest, StatsType
5 1
from pyof.v0x01.symmetric.echo_request import EchoRequest
6 1
from pyof.v0x01.symmetric.hello import Hello
7
8 1
from kytos.core import KytosEvent
9 1
from kytos.core.interface import Interface
10 1
from napps.kytos.of_core.utils import emit_message_out
11
12
13 1
def update_flow_list(controller, switch):
14
    """Request flow stats from switches.
15
16
    Args:
17
        controller(:class:`~kytos.core.controller.Controller`):
18
            the controller being used.
19
        switch(:class:`~kytos.core.switch.Switch`):
20
            target to send a stats request.
21
    """
22 1
    body = FlowStatsRequest()
23 1
    stats_request = StatsRequest(
24
        body_type=StatsType.OFPST_FLOW,
25
        body=body)
26
    # req.pack()
27 1
    emit_message_out(controller, switch.connection, stats_request)
28
29
30 1
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 1
    stats_request = StatsRequest(body_type=StatsType.OFPST_DESC)
40 1
    emit_message_out(controller, switch.connection, stats_request)
41
42
43 1
def handle_features_reply(controller, event):
44
    """Handle OF v0x01 features_reply message events.
45
46
    This is the end of the Handshake workflow of the OpenFlow Protocol.
47
    Parameters:
48
        controller (Controller): Controller being used.
49
        event (KytosEvent): Event with features reply message.
50
51
    """
52 1
    connection = event.source
53 1
    features_reply = event.content['message']
54 1
    dpid = features_reply.datapath_id.value
55
56 1
    switch = controller.get_switch_or_create(dpid=dpid,
57
                                             connection=connection)
58
59 1
    for port in features_reply.ports:
60 1
        interface = switch.get_interface_by_port_no(port.port_no.value)
61 1
        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 1
            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 1
        switch.update_interface(interface)
74 1
        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 1
        controller.buffers.app.put(port_event)
85
86 1
    switch.update_features(features_reply)
87 1
    return switch
88
89
90 1
def send_echo(controller, switch):
91
    """Send echo request to a datapath.
92
93
    Keep the connection alive through symmetric echoes.
94
    """
95 1
    echo = EchoRequest(data=b'kytosd_10')
96 1
    emit_message_out(controller, switch.connection, echo)
97
98
99 1
def send_set_config(controller, switch):
100
    """Send a SetConfig message after the OpenFlow handshake."""
101 1
    set_config = SetConfig()
102 1
    set_config.flags = ConfigFlag.OFPC_FRAG_NORMAL
103 1
    set_config.miss_send_len = 0xffff       # Send the whole packet
104 1
    emit_message_out(controller, switch.connection, set_config)
105
106
107 1
def say_hello(controller, connection):
108
    """Send back a Hello packet with the same version as the switch."""
109
    hello = Hello()
110
    emit_message_out(controller, connection, hello)
111