|
1
|
|
|
""" This module creates the trace result to be exported via REST. |
|
2
|
|
|
Each active trace will instantiate a FormatRest and publish it. |
|
3
|
|
|
""" |
|
4
|
1 |
|
from datetime import datetime |
|
5
|
1 |
|
from napps.amlight.sdntrace.shared.switches import Switches |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
1 |
|
class FormatRest: |
|
9
|
|
|
""" This module creates the trace result to be exported via REST. |
|
10
|
|
|
Each active trace will instantiate a FormatRest and publish it. |
|
11
|
|
|
""" |
|
12
|
|
|
|
|
13
|
1 |
|
def __init__(self): |
|
14
|
1 |
|
self.start_time = self.current_time() |
|
15
|
|
|
|
|
16
|
1 |
|
@staticmethod |
|
17
|
1 |
|
def current_time(): |
|
18
|
|
|
""" Return the current time using datetime format""" |
|
19
|
1 |
|
return datetime.now() |
|
20
|
|
|
|
|
21
|
1 |
|
def get_time(self, to_str=True): |
|
22
|
|
|
""" Get tracing total trace per trace step. |
|
23
|
|
|
|
|
24
|
|
|
Args: |
|
25
|
|
|
to_str: if the result should be provided in string |
|
26
|
|
|
otherwise, would be datetime format |
|
27
|
|
|
Return: |
|
28
|
|
|
total time from current time and start time |
|
29
|
|
|
""" |
|
30
|
1 |
|
time_diff = self.current_time() - self.start_time |
|
31
|
1 |
|
return str(time_diff) if to_str else time_diff |
|
32
|
|
|
|
|
33
|
1 |
|
def add_trace_step(self, trace_result, trace_type, reason='done', |
|
34
|
|
|
dpid=None, port=None, msg="none"): |
|
35
|
|
|
""" Used to create the new REST result.vOnly this method |
|
36
|
|
|
should write to self.trace_result |
|
37
|
|
|
|
|
38
|
|
|
Args: |
|
39
|
|
|
trace_result: variable with results |
|
40
|
|
|
trace_type: type of trace (intra or inter-domain) |
|
41
|
|
|
reason: reason in case trace_type == last |
|
42
|
|
|
dpid: switch's dpid |
|
43
|
|
|
port: switch's OpenFlow port_no |
|
44
|
|
|
msg: message in case of reason == error |
|
45
|
|
|
""" |
|
46
|
1 |
|
step = dict() |
|
47
|
1 |
|
step["type"] = trace_type |
|
48
|
|
|
|
|
49
|
1 |
|
switch = Switches().get_switch(dpid) if dpid else None |
|
50
|
|
|
|
|
51
|
1 |
|
if trace_type == 'starting': |
|
52
|
1 |
|
step["dpid"] = switch.dpid |
|
53
|
1 |
|
step["port"] = port |
|
54
|
1 |
|
step["time"] = str(self.start_time) |
|
55
|
1 |
|
elif trace_type == 'trace': |
|
56
|
1 |
|
step["dpid"] = switch.dpid |
|
57
|
1 |
|
step["port"] = port |
|
58
|
1 |
|
step["time"] = self.get_time() |
|
59
|
1 |
|
elif trace_type == 'last': |
|
60
|
1 |
|
step["reason"] = reason |
|
61
|
1 |
|
step["msg"] = msg |
|
62
|
1 |
|
step["time"] = self.get_time() |
|
63
|
|
|
|
|
64
|
|
|
# Add to trace_result array by reference |
|
65
|
|
|
trace_result.append(step) |
|
66
|
|
|
|