Passed
Push — master ( 63c2ca...8a75fa )
by Vinicius
02:17 queued 13s
created

build.main   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Test Coverage

Coverage 64.29%

Importance

Changes 0
Metric Value
wmc 10
eloc 44
dl 0
loc 133
ccs 27
cts 42
cp 0.6429
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A Main.setup() 0 10 1
A Main.execute() 0 2 1
A Main.run_trace() 0 8 1
A Main.get_results() 0 8 1
A Main.shutdown() 0 6 1
A Main.list_settings() 0 15 1
A Main.get_result() 0 8 1
A Main.get_stats() 0 8 1
A Main.handle_packet_in() 0 12 2
1
"""Main module of amlight/sdntrace Kytos Network Application.
2
3
An OpenFlow Data Path Trace.
4
5
The AmLight SDNTrace is a Kytos Network Application allows users to trace a
6
path directly from the data plane. Originally written for Ryu
7
(github.com/amlight/sdntrace).
8
9
Steps:
10
    1 - User requests a trace using a specific flow characteristic,
11
        for example VLAN = 1000 and Dest IP address = 2.2.2.2
12
    2 - REST module inserts trace request in a queue provided by the
13
        TraceManager
14
    3 - The TraceManager runs the Tracer, basically sending PacketOuts
15
        and waiting for PacketIn until reaching a timeout
16
    4 - After Timeout, result is provided back to REST that provides it
17
        back to user
18
Dependencies:
19
    * - amlight/coloring Napp will color all switches
20
21
At this moment, OpenFlow 1.3 is supported.
22
"""
23
24
25 1
from flask import jsonify, request
26 1
from napps.amlight.sdntrace import settings
27 1
from napps.amlight.sdntrace.backends.of_parser import process_packet_in
28 1
from napps.amlight.sdntrace.shared.switches import Switches
29 1
from napps.amlight.sdntrace.tracing.trace_manager import TraceManager
30
31 1
from kytos.core import KytosNApp, rest
32 1
from kytos.core.helpers import listen_to
33
34 1
VERSION = "0.3"
35
36
37 1
class Main(KytosNApp):
38
    """Main class of amlight/sdntrace NApp.
39
40
    REST methods:
41
        /sdntrace/trace ['PUT'] - request a trace
42
        /sdntrace/trace ['GET'] - list of previous trace requests and results
43
        /sdntrace/trace/<trace_id> - get the results of trace requested
44
        /sdntrace/stats - Show the number of requests received and active
45
        /sdntrace/settings - list the settings
46
    """
47
48 1
    def setup(self):
49
        """Default Kytos/Napps setup call."""
50
51
        # Create list of switches
52 1
        self.switches = Switches(
53
            self.controller.switches
54
        )  # noqa: E501  pylint: disable=attribute-defined-outside-init
55
56
        # Instantiate TraceManager
57 1
        self.tracing = TraceManager(self.controller)  # pylint: disable=W0201
58
59 1
    def execute(self):
60
        """Kytos Napp execute method"""
61
62 1
    def shutdown(self):
63
        """Execute when your napp is unloaded.
64
65
        If you have some cleanup procedure, insert it here.
66
        """
67
        self.tracing.stop_traces()
68
69 1
    @listen_to("kytos/of_core.v0x04.messages.in.ofpt_packet_in")
70 1
    def handle_packet_in(self, event):
71
        """Receives OpenFlow PacketIn msgs and search from trace packets.
72
        If process_packet_in returns 0,0,0, it means it is not a probe
73
        packet. Otherwise, store the msg for later use by Tracers.
74
75
        Args:
76
            event (KycoPacketIn): Received Event
77
        """
78
        ethernet, in_port, switch = process_packet_in(event)
79
        if not isinstance(ethernet, int):
80
            self.tracing.queue_probe_packet(event, ethernet, in_port, switch)
81
82 1
    @rest("/trace", methods=["PUT"])
83 1
    def run_trace(self):
84
        """Submit a trace request
85
86
        Return:
87
            trace ID in JSON format
88
        """
89
        return jsonify(self.tracing.rest_new_trace(request.get_json()))
90
91 1
    @rest("/trace", methods=["GET"])
92 1
    def get_results(self):
93
        """List all traces performed so far.
94
95
        Return:
96
            rest_list_results in JSON format
97
        """
98
        return jsonify(self.tracing.rest_list_results())
99
100 1
    @rest("/trace/<trace_id>", methods=["GET"])
101 1
    def get_result(self, trace_id):
102
        """List All Traces performed since the Napp loaded
103
104
        Return:
105
            rest_get_result in JSON format
106
        """
107
        return jsonify(self.tracing.rest_get_result(trace_id))
108
109 1
    @rest("/stats", methods=["GET"])
110 1
    def get_stats(self):
111
        """Get statistics
112
113
        Return:
114
            rest_list_stats in JSON format
115
        """
116
        return jsonify(self.tracing.rest_list_stats())
117
118 1
    @staticmethod
119 1
    @rest("/settings", methods=["GET"])
120 1
    def list_settings():
121
        """List the SDNTrace settings
122
123
        Return:
124
            SETTINGS in JSON format
125
        """
126
        settings_dict = {}
127
        settings_dict["color_field"] = settings.COLOR_FIELD
128
        settings_dict["color_value"] = settings.COLOR_VALUE
129
        settings_dict["trace_interval"] = settings.TRACE_INTERVAL
130
        settings_dict["parallel_traces"] = settings.PARALLEL_TRACES
131
        settings_dict["sdntrace_version"] = VERSION
132
        return jsonify(settings_dict)
133