1
|
|
|
"""Module with Kytos Events.""" |
2
|
2 |
|
import json |
3
|
2 |
|
from datetime import datetime |
4
|
2 |
|
from uuid import uuid4 |
5
|
|
|
|
6
|
2 |
|
from kytos.core.helpers import now |
7
|
|
|
|
8
|
|
|
|
9
|
2 |
|
class KytosEvent: |
10
|
|
|
"""Base Event class. |
11
|
|
|
|
12
|
|
|
The event data will be passed in the `content` attribute, which should be a |
13
|
|
|
dictionary. |
14
|
|
|
""" |
15
|
|
|
|
16
|
2 |
|
def __init__(self, name=None, content=None, trace_parent=None): |
17
|
|
|
"""Create an event to be published. |
18
|
|
|
|
19
|
|
|
Args: |
20
|
|
|
name (string): The name of the event. You should prepend it with |
21
|
|
|
the name of the napp. |
22
|
|
|
content (dict): Dictionary with any extra data for the event. |
23
|
|
|
trace_parent (object): APM TraceParent for distributed tracing, |
24
|
|
|
if you have APM enabled, @listen_to will |
25
|
|
|
set the root parent, and then you have to |
26
|
|
|
pass the trace_parent to subsequent |
27
|
|
|
correlated KytosEvent(s). |
28
|
|
|
""" |
29
|
2 |
|
self.name = name |
30
|
2 |
|
self.content = content if content is not None else {} |
31
|
2 |
|
self.timestamp = now() |
32
|
2 |
|
self.reinjections = 0 |
33
|
|
|
|
34
|
|
|
# pylint: disable=invalid-name |
35
|
2 |
|
self.id = uuid4() |
36
|
2 |
|
self.trace_parent = trace_parent |
37
|
|
|
|
38
|
2 |
|
def __str__(self): |
39
|
2 |
|
return self.name |
40
|
|
|
|
41
|
2 |
|
def __repr__(self): |
42
|
2 |
|
return f"KytosEvent({self.name!r}, {self.content!r})" |
43
|
|
|
|
44
|
2 |
|
def as_dict(self): |
45
|
|
|
"""Return KytosEvent as a dict.""" |
46
|
2 |
|
return {'id': str(self.id), 'name': self.name, 'content': self.content, |
47
|
|
|
'timestamp': self.timestamp, 'reinjections': self.reinjections} |
48
|
|
|
|
49
|
2 |
|
def as_json(self): |
50
|
|
|
"""Return KytosEvent as json.""" |
51
|
2 |
|
as_dict = self.as_dict() |
52
|
2 |
|
timestamp = datetime.strftime(as_dict['timestamp'], |
53
|
|
|
'%Y-%m-%dT%H:%M:%S') |
54
|
2 |
|
as_dict['timestamp'] = timestamp |
55
|
2 |
|
try: |
56
|
2 |
|
return json.dumps(as_dict) |
57
|
|
|
except TypeError: |
58
|
|
|
as_dict['content'] = str(as_dict['content']) |
59
|
|
|
return json.dumps(as_dict) |
60
|
|
|
|
61
|
2 |
|
@property |
62
|
|
|
def destination(self): |
63
|
|
|
"""Return the destination of KytosEvent.""" |
64
|
2 |
|
return self.content.get('destination') |
65
|
|
|
|
66
|
2 |
|
def set_destination(self, destination): |
67
|
|
|
"""Update the destination of KytosEvent. |
68
|
|
|
|
69
|
|
|
Args: |
70
|
|
|
destination (string): destination of KytosEvent. |
71
|
|
|
""" |
72
|
2 |
|
self.content['destination'] = destination |
73
|
|
|
|
74
|
2 |
|
@property |
75
|
|
|
def source(self): |
76
|
|
|
"""Return the source of KytosEvent.""" |
77
|
2 |
|
return self.content.get('source') |
78
|
|
|
|
79
|
2 |
|
def set_source(self, source): |
80
|
|
|
"""Update the source of KytosEvent. |
81
|
|
|
|
82
|
|
|
Args: |
83
|
|
|
source (string): source of KytosEvent. |
84
|
|
|
""" |
85
|
2 |
|
self.content['source'] = source |
86
|
|
|
|
87
|
2 |
|
@property |
88
|
|
|
def message(self): |
89
|
|
|
"""Return the message carried by the event if it exists. |
90
|
|
|
|
91
|
|
|
If there is any OpenFlow message on the event it'll be stored on |
92
|
|
|
the 'message' key of the 'content' attribute. |
93
|
|
|
|
94
|
|
|
Returns: |
95
|
|
|
A python-openflow message instance if it exists, None otherwise. |
96
|
|
|
|
97
|
|
|
""" |
98
|
2 |
|
try: |
99
|
2 |
|
return self.content['message'] |
100
|
2 |
|
except KeyError: |
101
|
|
|
return None |
102
|
|
|
|