Passed
Push — master ( e932ef...36cf0a )
by Vinicius
02:30 queued 14s
created

build.main   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 65.12%

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 134
ccs 28
cts 43
cp 0.6512
rs 10
c 0
b 0
f 0
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A Main.run_trace() 0 8 1
A Main.get_results() 0 8 1
A Main.get_result() 0 8 1
A Main.shutdown() 0 6 1
A Main.setup() 0 11 1
A Main.execute() 0 2 1
A Main.list_settings() 0 15 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.0 and 1.3 are 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, log, 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 1
        log.info("Starting Kytos SDNTrace App version %s!", VERSION)
51
52
        # Create list of switches
53 1
        self.switches = Switches(
54
            self.controller.switches
55
        )  # noqa: E501  pylint: disable=attribute-defined-outside-init
56
57
        # Instantiate TraceManager
58 1
        self.tracing = TraceManager(self.controller)  # pylint: disable=W0201
59
60 1
    def execute(self):
61
        """Kytos Napp execute method"""
62
63 1
    def shutdown(self):
64
        """Execute when your napp is unloaded.
65
66
        If you have some cleanup procedure, insert it here.
67
        """
68
        self.tracing.stop_traces()
69
70 1
    @listen_to("kytos/of_core.v0x0[14].messages.in.ofpt_packet_in")
71 1
    def handle_packet_in(self, event):
72
        """Receives OpenFlow PacketIn msgs and search from trace packets.
73
        If process_packet_in returns 0,0,0, it means it is not a probe
74
        packet. Otherwise, store the msg for later use by Tracers.
75
76
        Args:
77
            event (KycoPacketIn): Received Event
78
        """
79
        ethernet, in_port, switch = process_packet_in(event)
80
        if not isinstance(ethernet, int):
81
            self.tracing.queue_probe_packet(event, ethernet, in_port, switch)
82
83 1
    @rest("/trace", methods=["PUT"])
84 1
    def run_trace(self):
85
        """Submit a trace request
86
87
        Return:
88
            trace ID in JSON format
89
        """
90
        return jsonify(self.tracing.rest_new_trace(request.get_json()))
91
92 1
    @rest("/trace", methods=["GET"])
93 1
    def get_results(self):
94
        """List all traces performed so far.
95
96
        Return:
97
            rest_list_results in JSON format
98
        """
99
        return jsonify(self.tracing.rest_list_results())
100
101 1
    @rest("/trace/<trace_id>", methods=["GET"])
102 1
    def get_result(self, trace_id):
103
        """List All Traces performed since the Napp loaded
104
105
        Return:
106
            rest_get_result in JSON format
107
        """
108
        return jsonify(self.tracing.rest_get_result(trace_id))
109
110 1
    @rest("/stats", methods=["GET"])
111 1
    def get_stats(self):
112
        """Get statistics
113
114
        Return:
115
            rest_list_stats in JSON format
116
        """
117
        return jsonify(self.tracing.rest_list_stats())
118
119 1
    @staticmethod
120 1
    @rest("/settings", methods=["GET"])
121 1
    def list_settings():
122
        """List the SDNTrace settings
123
124
        Return:
125
            SETTINGS in JSON format
126
        """
127
        settings_dict = {}
128
        settings_dict["color_field"] = settings.COLOR_FIELD
129
        settings_dict["color_value"] = settings.COLOR_VALUE
130
        settings_dict["trace_interval"] = settings.TRACE_INTERVAL
131
        settings_dict["parallel_traces"] = settings.PARALLEL_TRACES
132
        settings_dict["sdntrace_version"] = VERSION
133
        return jsonify(settings_dict)
134