|
1
|
|
|
"""Main module of kytos/mef_eline Kytos Network Application. |
|
2
|
|
|
|
|
3
|
|
|
NApp to provision circuits from user request. |
|
4
|
|
|
""" |
|
5
|
2 |
|
from flask import jsonify, request |
|
6
|
|
|
|
|
7
|
2 |
|
from kytos.core import KytosNApp, log, rest |
|
8
|
2 |
|
from kytos.core.events import KytosEvent |
|
9
|
2 |
|
from kytos.core.helpers import listen_to |
|
10
|
2 |
|
from kytos.core.interface import TAG, UNI |
|
11
|
2 |
|
from kytos.core.link import Link |
|
12
|
2 |
|
from napps.kytos.mef_eline.models import EVC, DynamicPathManager |
|
13
|
2 |
|
from napps.kytos.mef_eline.scheduler import CircuitSchedule, Scheduler |
|
14
|
2 |
|
from napps.kytos.mef_eline.storehouse import StoreHouse |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
2 |
|
class Main(KytosNApp): |
|
18
|
|
|
"""Main class of amlight/mef_eline NApp. |
|
19
|
|
|
|
|
20
|
|
|
This class is the entry point for this napp. |
|
21
|
|
|
""" |
|
22
|
|
|
|
|
23
|
2 |
|
def setup(self): |
|
24
|
|
|
"""Replace the '__init__' method for the KytosNApp subclass. |
|
25
|
|
|
|
|
26
|
|
|
The setup method is automatically called by the controller when your |
|
27
|
|
|
application is loaded. |
|
28
|
|
|
|
|
29
|
|
|
So, if you have any setup routine, insert it here. |
|
30
|
|
|
""" |
|
31
|
|
|
# object used to scheduler circuit events |
|
32
|
2 |
|
self.sched = Scheduler() |
|
33
|
|
|
|
|
34
|
|
|
# object to save and load circuits |
|
35
|
2 |
|
self.storehouse = StoreHouse(self.controller) |
|
36
|
|
|
|
|
37
|
|
|
# set the controller that will manager the dynamic paths |
|
38
|
2 |
|
DynamicPathManager.set_controller(self.controller) |
|
39
|
|
|
|
|
40
|
2 |
|
def execute(self): |
|
41
|
|
|
"""Execute once when the napp is running.""" |
|
42
|
|
|
|
|
43
|
2 |
|
def shutdown(self): |
|
44
|
|
|
"""Execute when your napp is unloaded. |
|
45
|
|
|
|
|
46
|
|
|
If you have some cleanup procedure, insert it here. |
|
47
|
|
|
""" |
|
48
|
|
|
|
|
49
|
2 |
|
@rest('/v2/evc/', methods=['GET']) |
|
50
|
|
|
def list_circuits(self): |
|
51
|
|
|
"""Endpoint to return all circuits stored.""" |
|
52
|
2 |
|
circuits = self.storehouse.get_data() |
|
53
|
2 |
|
if not circuits: |
|
54
|
2 |
|
return jsonify({}), 200 |
|
55
|
|
|
|
|
56
|
2 |
|
return jsonify(circuits), 200 |
|
57
|
|
|
|
|
58
|
2 |
|
@rest('/v2/evc/<circuit_id>', methods=['GET']) |
|
59
|
|
|
def get_circuit(self, circuit_id): |
|
60
|
|
|
"""Endpoint to return a circuit based on id.""" |
|
61
|
2 |
|
circuits = self.storehouse.get_data() |
|
62
|
|
|
|
|
63
|
2 |
|
if circuit_id in circuits: |
|
64
|
2 |
|
result = circuits[circuit_id] |
|
65
|
2 |
|
status = 200 |
|
66
|
|
|
else: |
|
67
|
2 |
|
result = {'response': f'circuit_id {circuit_id} not found'} |
|
68
|
2 |
|
status = 400 |
|
69
|
|
|
|
|
70
|
2 |
|
return jsonify(result), status |
|
71
|
|
|
|
|
72
|
2 |
|
@rest('/v2/evc/', methods=['POST']) |
|
73
|
|
|
def create_circuit(self): |
|
74
|
|
|
"""Try to create a new circuit. |
|
75
|
|
|
|
|
76
|
|
|
Firstly, for EVPL: E-Line NApp verifies if UNI_A's requested C-VID and |
|
77
|
|
|
UNI_Z's requested C-VID are available from the interfaces' pools. This |
|
78
|
|
|
is checked when creating the UNI object. |
|
79
|
|
|
|
|
80
|
|
|
Then, E-Line NApp requests a primary and a backup path to the |
|
81
|
|
|
Pathfinder NApp using the attributes primary_links and backup_links |
|
82
|
|
|
submitted via REST |
|
83
|
|
|
|
|
84
|
|
|
# For each link composing paths in #3: |
|
85
|
|
|
# - E-Line NApp requests a S-VID available from the link VLAN pool. |
|
86
|
|
|
# - Using the S-VID obtained, generate abstract flow entries to be |
|
87
|
|
|
# sent to FlowManager |
|
88
|
|
|
|
|
89
|
|
|
Push abstract flow entries to FlowManager and FlowManager pushes |
|
90
|
|
|
OpenFlow entries to datapaths |
|
91
|
|
|
|
|
92
|
|
|
E-Line NApp generates an event to notify all Kytos NApps of a new EVC |
|
93
|
|
|
creation |
|
94
|
|
|
|
|
95
|
|
|
Finnaly, notify user of the status of its request. |
|
96
|
|
|
""" |
|
97
|
|
|
# Try to create the circuit object |
|
98
|
2 |
|
data = request.get_json() |
|
99
|
|
|
|
|
100
|
2 |
|
if not data: |
|
101
|
2 |
|
return jsonify("Bad request: The request do not have a json."), 400 |
|
102
|
|
|
|
|
103
|
2 |
|
try: |
|
104
|
2 |
|
evc = self.evc_from_dict(data) |
|
105
|
|
|
except ValueError as exception: |
|
106
|
|
|
return jsonify("Bad request: {}".format(exception)), 400 |
|
107
|
|
|
|
|
108
|
|
|
# verify duplicated evc |
|
109
|
2 |
|
if self.is_duplicated_evc(evc): |
|
110
|
|
|
return jsonify("Not Acceptable: This evc already exists."), 409 |
|
111
|
|
|
|
|
112
|
|
|
# save circuit |
|
113
|
2 |
|
self.storehouse.save_evc(evc) |
|
114
|
|
|
|
|
115
|
|
|
# Schedule the circuit deploy |
|
116
|
2 |
|
self.sched.add(evc) |
|
117
|
|
|
|
|
118
|
|
|
# Circuit has no schedule, deploy now |
|
119
|
2 |
|
if not evc.circuit_scheduler: |
|
120
|
2 |
|
evc.deploy() |
|
121
|
|
|
|
|
122
|
|
|
# Notify users |
|
123
|
2 |
|
event = KytosEvent(name='kytos.mef_eline.created', |
|
124
|
|
|
content=evc.as_dict()) |
|
125
|
2 |
|
self.controller.buffers.app.put(event) |
|
126
|
|
|
|
|
127
|
2 |
|
return jsonify({"circuit_id": evc.id}), 201 |
|
128
|
|
|
|
|
129
|
2 |
|
@rest('/v2/evc/<circuit_id>', methods=['PATCH']) |
|
130
|
|
|
def update(self, circuit_id): |
|
131
|
|
|
"""Update a circuit based on payload. |
|
132
|
|
|
|
|
133
|
|
|
The EVC required attributes can't be updated. |
|
134
|
|
|
""" |
|
135
|
|
|
data = request.get_json() |
|
136
|
|
|
circuits = self.storehouse.get_data() |
|
137
|
|
|
|
|
138
|
|
|
if circuit_id not in circuits: |
|
139
|
|
|
result = {'response': f'circuit_id {circuit_id} not found'} |
|
140
|
|
|
return jsonify(result), 404 |
|
141
|
|
|
|
|
142
|
|
|
try: |
|
143
|
|
|
evc = self.evc_from_dict(circuits.get(circuit_id)) |
|
144
|
|
|
evc.update(**data) |
|
145
|
|
|
self.storehouse.save_evc(evc) |
|
146
|
|
|
result = {evc.id: evc.as_dict()} |
|
147
|
|
|
status = 200 |
|
148
|
|
|
except ValueError as exception: |
|
149
|
|
|
result = "Bad request: {}".format(exception) |
|
150
|
|
|
status = 400 |
|
151
|
|
|
|
|
152
|
|
|
return jsonify(result), status |
|
153
|
|
|
|
|
154
|
2 |
|
@rest('/v2/evc/<circuit_id>', methods=['DELETE']) |
|
155
|
|
|
def delete_circuit(self, circuit_id): |
|
156
|
|
|
"""Remove a circuit. |
|
157
|
|
|
|
|
158
|
|
|
First, flows are removed from the switches, then the EVC is |
|
159
|
|
|
disabled. |
|
160
|
|
|
""" |
|
161
|
|
|
circuits = self.storehouse.get_data() |
|
162
|
|
|
log.info("Removing %s" % circuit_id) |
|
163
|
|
|
evc = self.evc_from_dict(circuits.get(circuit_id)) |
|
164
|
|
|
evc.remove_current_flows() |
|
165
|
|
|
evc.disable() |
|
166
|
|
|
self.storehouse.save_evc(evc) |
|
167
|
|
|
|
|
168
|
|
|
return jsonify("Circuit removed"), 200 |
|
169
|
|
|
|
|
170
|
2 |
|
def is_duplicated_evc(self, evc): |
|
171
|
|
|
"""Verify if the circuit given is duplicated with the stored evcs. |
|
172
|
|
|
|
|
173
|
|
|
Args: |
|
174
|
|
|
evc (EVC): circuit to be analysed. |
|
175
|
|
|
|
|
176
|
|
|
Returns: |
|
177
|
|
|
boolean: True if the circuit is duplicated, otherwise False. |
|
178
|
|
|
|
|
179
|
|
|
""" |
|
180
|
2 |
|
for circuit_dict in self.storehouse.get_data().values(): |
|
181
|
|
|
try: |
|
182
|
|
|
circuit = self.evc_from_dict(circuit_dict) |
|
183
|
|
|
except ValueError: |
|
184
|
|
|
continue |
|
185
|
|
|
|
|
186
|
|
|
if circuit == evc: |
|
187
|
|
|
return True |
|
188
|
|
|
|
|
189
|
2 |
|
return False |
|
190
|
|
|
|
|
191
|
2 |
|
@listen_to('kytos/topology.link_up') |
|
192
|
|
|
def handle_link_up(self, event): |
|
193
|
|
|
"""Change circuit when link is up or end_maintenance.""" |
|
194
|
|
|
evc = None |
|
195
|
|
|
|
|
196
|
|
|
for data in self.storehouse.get_data().values(): |
|
197
|
|
|
try: |
|
198
|
|
|
evc = self.evc_from_dict(data) |
|
199
|
|
|
except ValueError as _exception: |
|
200
|
|
|
log.debug(f'{data.get("id")} can not be provisioning yet.') |
|
201
|
|
|
continue |
|
202
|
|
|
|
|
203
|
|
|
if evc.schedule_active: |
|
204
|
|
|
evc.handle_link_up(event.content['link']) |
|
205
|
|
|
|
|
206
|
2 |
|
@listen_to('kytos/topology.link_down') |
|
207
|
|
|
def handle_link_down(self, event): |
|
208
|
|
|
"""Change circuit when link is down or under_mantenance.""" |
|
209
|
|
|
evc = None |
|
210
|
|
|
|
|
211
|
|
|
for data in self.storehouse.get_data().values(): |
|
212
|
|
|
try: |
|
213
|
|
|
evc = self.evc_from_dict(data) |
|
214
|
|
|
except ValueError as _exception: |
|
215
|
|
|
log.debug(f'{data.get("id")} can not be provisioned yet.') |
|
216
|
|
|
continue |
|
217
|
|
|
|
|
218
|
|
|
if evc.is_active() and \ |
|
219
|
|
|
evc.is_affected_by_link(event.content['link']): |
|
220
|
|
|
log.info('handling evc %s' % evc) |
|
221
|
|
|
evc.handle_link_down() |
|
222
|
|
|
|
|
223
|
2 |
|
def evc_from_dict(self, evc_dict): |
|
224
|
|
|
"""Convert some dict values to instance of EVC classes. |
|
225
|
|
|
|
|
226
|
|
|
This method will convert: [UNI, Link] |
|
227
|
|
|
""" |
|
228
|
2 |
|
data = evc_dict.copy() # Do not modify the original dict |
|
229
|
|
|
|
|
230
|
2 |
|
for attribute, value in data.items(): |
|
231
|
|
|
|
|
232
|
2 |
|
if 'uni' in attribute: |
|
233
|
2 |
|
try: |
|
234
|
2 |
|
data[attribute] = self.uni_from_dict(value) |
|
235
|
|
|
except ValueError as exc: |
|
236
|
|
|
raise ValueError(f'Error creating UNI: {exc}') |
|
237
|
|
|
|
|
238
|
2 |
|
if attribute == 'circuit_scheduler': |
|
239
|
|
|
data[attribute] = [] |
|
240
|
|
|
for schedule in value: |
|
241
|
|
|
data[attribute].append(CircuitSchedule.from_dict(schedule)) |
|
242
|
|
|
|
|
243
|
2 |
|
if 'link' in attribute: |
|
244
|
|
|
if value: |
|
245
|
|
|
data[attribute] = self.link_from_dict(value) |
|
246
|
|
|
|
|
247
|
2 |
|
if 'path' in attribute and attribute != 'dynamic_backup_path': |
|
248
|
|
|
if value: |
|
249
|
|
|
data[attribute] = [self.link_from_dict(link) |
|
250
|
|
|
for link in value] |
|
251
|
|
|
|
|
252
|
2 |
|
return EVC(self.controller, **data) |
|
253
|
|
|
|
|
254
|
2 |
|
def uni_from_dict(self, uni_dict): |
|
255
|
|
|
"""Return a UNI object from python dict.""" |
|
256
|
|
|
if uni_dict is None: |
|
257
|
|
|
return False |
|
258
|
|
|
|
|
259
|
|
|
interface_id = uni_dict.get("interface_id") |
|
260
|
|
|
interface = self.controller.get_interface_by_id(interface_id) |
|
261
|
|
|
if interface is None: |
|
262
|
|
|
raise ValueError(f'Could not instantiate interface {interface_id}') |
|
263
|
|
|
|
|
264
|
|
|
tag_dict = uni_dict.get("tag") |
|
265
|
|
|
tag = TAG.from_dict(tag_dict) |
|
266
|
|
|
if tag is False: |
|
267
|
|
|
raise ValueError(f'Could not instantiate tag from dict {tag_dict}') |
|
268
|
|
|
|
|
269
|
|
|
uni = UNI(interface, tag) |
|
270
|
|
|
|
|
271
|
|
|
return uni |
|
272
|
|
|
|
|
273
|
2 |
|
def link_from_dict(self, link_dict): |
|
274
|
|
|
"""Return a Link object from python dict.""" |
|
275
|
|
|
id_a = link_dict.get('endpoint_a').get('id') |
|
276
|
|
|
id_b = link_dict.get('endpoint_b').get('id') |
|
277
|
|
|
|
|
278
|
|
|
endpoint_a = self.controller.get_interface_by_id(id_a) |
|
279
|
|
|
endpoint_b = self.controller.get_interface_by_id(id_b) |
|
280
|
|
|
|
|
281
|
|
|
link = Link(endpoint_a, endpoint_b) |
|
282
|
|
|
if 'metadata' in link_dict: |
|
283
|
|
|
link.extend_metadata(link_dict.get('metadata')) |
|
284
|
|
|
|
|
285
|
|
|
s_vlan = link.get_metadata('s_vlan') |
|
286
|
|
|
if s_vlan: |
|
287
|
|
|
tag = TAG.from_dict(s_vlan) |
|
288
|
|
|
if tag is False: |
|
289
|
|
|
error_msg = f'Could not instantiate tag from dict {s_vlan}' |
|
290
|
|
|
raise ValueError(error_msg) |
|
291
|
|
|
link.update_metadata('s_vlan', tag) |
|
292
|
|
|
return link |
|
293
|
|
|
|