|
1
|
|
|
"""Classes used in the main application.""" |
|
2
|
2 |
|
from datetime import datetime |
|
3
|
2 |
|
from uuid import uuid4 |
|
4
|
|
|
|
|
5
|
2 |
|
import requests |
|
6
|
|
|
|
|
7
|
2 |
|
from kytos.core import log |
|
8
|
2 |
|
from kytos.core.common import EntityStatus, GenericEntity |
|
9
|
2 |
|
from kytos.core.helpers import get_time, now |
|
10
|
2 |
|
from kytos.core.interface import UNI |
|
11
|
2 |
|
from kytos.core.link import Link |
|
12
|
2 |
|
from napps.kytos.mef_eline import settings |
|
13
|
2 |
|
from napps.kytos.mef_eline.storehouse import StoreHouse |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
2 |
|
class Path(list, GenericEntity): |
|
17
|
|
|
"""Class to represent a Path.""" |
|
18
|
|
|
|
|
19
|
2 |
|
def __eq__(self, other=None): |
|
20
|
|
|
"""Compare paths.""" |
|
21
|
2 |
|
if not other or not isinstance(other, Path): |
|
22
|
2 |
|
return False |
|
23
|
2 |
|
return super().__eq__(other) |
|
24
|
|
|
|
|
25
|
2 |
|
def is_affected_by_link(self, link=None): |
|
26
|
|
|
"""Verify if the current path is affected by link.""" |
|
27
|
2 |
|
if not link: |
|
28
|
|
|
return False |
|
29
|
2 |
|
return link in self |
|
30
|
|
|
|
|
31
|
2 |
|
def link_affected_by_interface(self, interface=None): |
|
32
|
|
|
"""Return the link using this interface, if any, or None otherwise.""" |
|
33
|
|
|
if not interface: |
|
34
|
|
|
return None |
|
35
|
|
|
for link in self: |
|
36
|
|
|
if interface in (link.endpoint_a, link.endpoint_b): |
|
37
|
|
|
return link |
|
38
|
|
|
return None |
|
39
|
|
|
|
|
40
|
2 |
|
def choose_vlans(self): |
|
41
|
|
|
"""Choose the VLANs to be used for the circuit.""" |
|
42
|
2 |
|
for link in self: |
|
43
|
2 |
|
tag = link.get_next_available_tag() |
|
44
|
2 |
|
link.use_tag(tag) |
|
45
|
2 |
|
link.add_metadata('s_vlan', tag) |
|
46
|
|
|
|
|
47
|
2 |
|
def make_vlans_available(self): |
|
48
|
|
|
"""Make the VLANs used in a path available when undeployed.""" |
|
49
|
2 |
|
for link in self: |
|
50
|
2 |
|
link.make_tag_available(link.get_metadata('s_vlan')) |
|
51
|
2 |
|
link.remove_metadata('s_vlan') |
|
52
|
|
|
|
|
53
|
2 |
|
@property |
|
54
|
|
|
def status(self): |
|
55
|
|
|
"""Check for the status of a path. |
|
56
|
|
|
|
|
57
|
|
|
If any link in this path is down, the path is considered down. |
|
58
|
|
|
""" |
|
59
|
2 |
|
if not self: |
|
60
|
2 |
|
return EntityStatus.DISABLED |
|
61
|
|
|
|
|
62
|
2 |
|
endpoint = '%s/%s' % (settings.TOPOLOGY_URL, 'links') |
|
63
|
2 |
|
api_reply = requests.get(endpoint) |
|
64
|
2 |
|
if api_reply.status_code != getattr(requests.codes, 'ok'): |
|
65
|
|
|
log.error('Failed to get links at %s. Returned %s', |
|
66
|
|
|
endpoint, api_reply.status_code) |
|
67
|
|
|
return None |
|
68
|
2 |
|
links = api_reply.json()['links'] |
|
69
|
2 |
|
for path_link in self: |
|
70
|
2 |
|
try: |
|
71
|
2 |
|
link = links[path_link.id] |
|
72
|
|
|
except KeyError: |
|
73
|
|
|
return EntityStatus.DOWN |
|
74
|
2 |
|
if link['active'] is False: |
|
75
|
2 |
|
return EntityStatus.DOWN |
|
76
|
2 |
|
return EntityStatus.UP |
|
77
|
|
|
|
|
78
|
2 |
|
def as_dict(self): |
|
79
|
|
|
"""Return list comprehension of links as_dict.""" |
|
80
|
2 |
|
return [link.as_dict() for link in self if link] |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
2 |
|
class DynamicPathManager: |
|
84
|
|
|
"""Class to handle and create paths.""" |
|
85
|
|
|
|
|
86
|
2 |
|
controller = None |
|
87
|
|
|
|
|
88
|
2 |
|
@classmethod |
|
89
|
2 |
|
def set_controller(cls, controller=None): |
|
90
|
|
|
"""Set the controller to discovery news paths.""" |
|
91
|
2 |
|
cls.controller = controller |
|
92
|
|
|
|
|
93
|
2 |
|
@staticmethod |
|
94
|
|
|
def get_paths(circuit): |
|
95
|
|
|
"""Get a valid path for the circuit from the Pathfinder.""" |
|
96
|
|
|
endpoint = settings.PATHFINDER_URL |
|
97
|
|
|
request_data = {"source": circuit.uni_a.interface.id, |
|
98
|
|
|
"destination": circuit.uni_z.interface.id} |
|
99
|
|
|
api_reply = requests.post(endpoint, json=request_data) |
|
100
|
|
|
|
|
101
|
|
|
if api_reply.status_code != getattr(requests.codes, 'ok'): |
|
102
|
|
|
log.error("Failed to get paths at %s. Returned %s", |
|
103
|
|
|
endpoint, api_reply.status_code) |
|
104
|
|
|
return None |
|
105
|
|
|
reply_data = api_reply.json() |
|
106
|
|
|
return reply_data.get('paths') |
|
107
|
|
|
|
|
108
|
2 |
|
@staticmethod |
|
109
|
|
|
def _clear_path(path): |
|
110
|
|
|
"""Remove switches from a path, returning only interfaces.""" |
|
111
|
|
|
return [endpoint for endpoint in path if len(endpoint) > 23] |
|
112
|
|
|
|
|
113
|
2 |
|
@classmethod |
|
114
|
|
|
def get_best_path(cls, circuit): |
|
115
|
|
|
"""Return the best path available for a circuit, if exists.""" |
|
116
|
|
|
paths = cls.get_paths(circuit) |
|
117
|
|
|
if paths: |
|
118
|
|
|
return cls.create_path(cls.get_paths(circuit)[0]['hops']) |
|
119
|
|
|
return None |
|
120
|
|
|
|
|
121
|
2 |
|
@classmethod |
|
122
|
|
|
def create_path(cls, path): |
|
123
|
|
|
"""Return the path containing only the interfaces.""" |
|
124
|
|
|
new_path = Path() |
|
125
|
|
|
clean_path = cls._clear_path(path) |
|
126
|
|
|
|
|
127
|
|
|
if len(clean_path) % 2: |
|
128
|
|
|
return None |
|
129
|
|
|
|
|
130
|
|
|
for link in zip(clean_path[1:-1:2], clean_path[2::2]): |
|
131
|
|
|
interface_a = cls.controller.get_interface_by_id(link[0]) |
|
132
|
|
|
interface_b = cls.controller.get_interface_by_id(link[1]) |
|
133
|
|
|
if interface_a is None or interface_b is None: |
|
134
|
|
|
return None |
|
135
|
|
|
new_path.append(Link(interface_a, interface_b)) |
|
136
|
|
|
|
|
137
|
|
|
return new_path |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
2 |
|
class EVCBase(GenericEntity): |
|
141
|
|
|
"""Class to represent a circuit.""" |
|
142
|
|
|
|
|
143
|
2 |
|
unique_attributes = ['name', 'uni_a', 'uni_z'] |
|
144
|
|
|
|
|
145
|
2 |
|
def __init__(self, controller, **kwargs): |
|
146
|
|
|
"""Create an EVC instance with the provided parameters. |
|
147
|
|
|
|
|
148
|
|
|
Args: |
|
149
|
|
|
id(str): EVC identifier. Whether it's None an ID will be genereted. |
|
150
|
|
|
name: represents an EVC name.(Required) |
|
151
|
|
|
uni_a (UNI): Endpoint A for User Network Interface.(Required) |
|
152
|
|
|
uni_z (UNI): Endpoint Z for User Network Interface.(Required) |
|
153
|
|
|
start_date(datetime|str): Date when the EVC was registred. |
|
154
|
|
|
Default is now(). |
|
155
|
|
|
end_date(datetime|str): Final date that the EVC will be fineshed. |
|
156
|
|
|
Default is None. |
|
157
|
|
|
bandwidth(int): Bandwidth used by EVC instance. Default is 0. |
|
158
|
|
|
primary_links(list): Primary links used by evc. Default is [] |
|
159
|
|
|
backup_links(list): Backups links used by evc. Default is [] |
|
160
|
|
|
current_path(list): Circuit being used at the moment if this is an |
|
161
|
|
|
active circuit. Default is []. |
|
162
|
|
|
primary_path(list): primary circuit offered to user IF one or more |
|
163
|
|
|
links were provided. Default is []. |
|
164
|
|
|
backup_path(list): backup circuit offered to the user IF one or |
|
165
|
|
|
more links were provided. Default is []. |
|
166
|
|
|
dynamic_backup_path(bool): Enable computer backup path dynamically. |
|
167
|
|
|
Dafault is False. |
|
168
|
|
|
creation_time(datetime|str): datetime when the circuit should be |
|
169
|
|
|
activated. default is now(). |
|
170
|
|
|
enabled(Boolean): attribute to indicate the administrative state; |
|
171
|
|
|
default is False. |
|
172
|
|
|
active(Boolean): attribute to indicate the operational state; |
|
173
|
|
|
default is False. |
|
174
|
|
|
archived(Boolean): indicate the EVC has been deleted and is |
|
175
|
|
|
archived; default is False. |
|
176
|
|
|
owner(str): The EVC owner. Default is None. |
|
177
|
|
|
priority(int): Service level provided in the request. Default is 0. |
|
178
|
|
|
|
|
179
|
|
|
Raises: |
|
180
|
|
|
ValueError: raised when object attributes are invalid. |
|
181
|
|
|
|
|
182
|
|
|
""" |
|
183
|
2 |
|
self._validate(**kwargs) |
|
184
|
2 |
|
super().__init__() |
|
185
|
|
|
|
|
186
|
|
|
# required attributes |
|
187
|
2 |
|
self._id = kwargs.get('id', uuid4().hex) |
|
188
|
2 |
|
self.uni_a = kwargs.get('uni_a') |
|
189
|
2 |
|
self.uni_z = kwargs.get('uni_z') |
|
190
|
2 |
|
self.name = kwargs.get('name') |
|
191
|
|
|
|
|
192
|
|
|
# optional attributes |
|
193
|
2 |
|
self.start_date = get_time(kwargs.get('start_date')) or now() |
|
194
|
2 |
|
self.end_date = get_time(kwargs.get('end_date')) or None |
|
195
|
|
|
|
|
196
|
2 |
|
self.bandwidth = kwargs.get('bandwidth', 0) |
|
197
|
2 |
|
self.primary_links = Path(kwargs.get('primary_links', [])) |
|
198
|
2 |
|
self.backup_links = Path(kwargs.get('backup_links', [])) |
|
199
|
2 |
|
self.current_path = Path(kwargs.get('current_path', [])) |
|
200
|
2 |
|
self.primary_path = Path(kwargs.get('primary_path', [])) |
|
201
|
2 |
|
self.backup_path = Path(kwargs.get('backup_path', [])) |
|
202
|
2 |
|
self.dynamic_backup_path = kwargs.get('dynamic_backup_path', False) |
|
203
|
2 |
|
self.creation_time = get_time(kwargs.get('creation_time')) or now() |
|
204
|
2 |
|
self.owner = kwargs.get('owner', None) |
|
205
|
2 |
|
self.priority = kwargs.get('priority', 0) |
|
206
|
2 |
|
self.circuit_scheduler = kwargs.get('circuit_scheduler', []) |
|
207
|
|
|
|
|
208
|
2 |
|
self.current_links_cache = set() |
|
209
|
2 |
|
self.primary_links_cache = set() |
|
210
|
2 |
|
self.backup_links_cache = set() |
|
211
|
|
|
|
|
212
|
2 |
|
self.archived = kwargs.get('archived', False) |
|
213
|
|
|
|
|
214
|
2 |
|
self._storehouse = StoreHouse(controller) |
|
215
|
|
|
|
|
216
|
2 |
|
if kwargs.get('active', False): |
|
217
|
2 |
|
self.activate() |
|
218
|
|
|
else: |
|
219
|
2 |
|
self.deactivate() |
|
220
|
|
|
|
|
221
|
2 |
|
if kwargs.get('enabled', False): |
|
222
|
2 |
|
self.enable() |
|
223
|
|
|
else: |
|
224
|
2 |
|
self.disable() |
|
225
|
|
|
|
|
226
|
|
|
# datetime of user request for a EVC (or datetime when object was |
|
227
|
|
|
# created) |
|
228
|
2 |
|
self.request_time = kwargs.get('request_time', now()) |
|
229
|
|
|
# dict with the user original request (input) |
|
230
|
2 |
|
self._requested = kwargs |
|
231
|
|
|
|
|
232
|
2 |
|
def sync(self): |
|
233
|
|
|
"""Sync this EVC in the storehouse.""" |
|
234
|
2 |
|
self._storehouse.save_evc(self) |
|
235
|
2 |
|
log.info(f'EVC {self.id} was synced to the storehouse.') |
|
236
|
|
|
|
|
237
|
2 |
|
def update(self, **kwargs): |
|
238
|
|
|
"""Update evc attributes. |
|
239
|
|
|
|
|
240
|
|
|
This method will raises an error trying to change the following |
|
241
|
|
|
attributes: [name, uni_a and uni_z] |
|
242
|
|
|
|
|
243
|
|
|
Raises: |
|
244
|
|
|
ValueError: message with error detail. |
|
245
|
|
|
|
|
246
|
|
|
""" |
|
247
|
2 |
|
for attribute, value in kwargs.items(): |
|
248
|
2 |
|
if attribute in self.unique_attributes: |
|
249
|
2 |
|
raise ValueError(f'{attribute} can\'t be be updated.') |
|
250
|
2 |
|
if hasattr(self, attribute): |
|
251
|
2 |
|
setattr(self, attribute, value) |
|
252
|
|
|
else: |
|
253
|
|
|
raise ValueError(f'The attribute "{attribute}" is invalid.') |
|
254
|
2 |
|
self.sync() |
|
255
|
|
|
|
|
256
|
2 |
|
def __repr__(self): |
|
257
|
|
|
"""Repr method.""" |
|
258
|
2 |
|
return f"EVC({self._id}, {self.name})" |
|
259
|
|
|
|
|
260
|
2 |
|
def _validate(self, **kwargs): |
|
261
|
|
|
"""Do Basic validations. |
|
262
|
|
|
|
|
263
|
|
|
Verify required attributes: name, uni_a, uni_z |
|
264
|
|
|
Verify if the attributes uni_a and uni_z are valid. |
|
265
|
|
|
|
|
266
|
|
|
Raises: |
|
267
|
|
|
ValueError: message with error detail. |
|
268
|
|
|
|
|
269
|
|
|
""" |
|
270
|
2 |
|
for attribute in self.unique_attributes: |
|
271
|
|
|
|
|
272
|
2 |
|
if attribute not in kwargs: |
|
273
|
2 |
|
raise ValueError(f'{attribute} is required.') |
|
274
|
|
|
|
|
275
|
2 |
|
if 'uni' in attribute: |
|
276
|
2 |
|
uni = kwargs.get(attribute) |
|
277
|
2 |
|
if not isinstance(uni, UNI): |
|
278
|
|
|
raise ValueError(f'{attribute} is an invalid UNI.') |
|
279
|
|
|
|
|
280
|
2 |
|
if not uni.is_valid(): |
|
281
|
2 |
|
tag = uni.user_tag.value |
|
282
|
2 |
|
message = f'VLAN tag {tag} is not available in {attribute}' |
|
283
|
2 |
|
raise ValueError(message) |
|
284
|
|
|
|
|
285
|
2 |
|
def __eq__(self, other): |
|
286
|
|
|
"""Override the default implementation.""" |
|
287
|
2 |
|
if not isinstance(other, EVC): |
|
288
|
|
|
return False |
|
289
|
|
|
|
|
290
|
2 |
|
attrs_to_compare = ['name', 'uni_a', 'uni_z', 'owner', 'bandwidth'] |
|
291
|
2 |
|
for attribute in attrs_to_compare: |
|
292
|
2 |
|
if getattr(other, attribute) != getattr(self, attribute): |
|
293
|
2 |
|
return False |
|
294
|
2 |
|
return True |
|
295
|
|
|
|
|
296
|
2 |
|
def as_dict(self): |
|
297
|
|
|
"""Return a dictionary representing an EVC object.""" |
|
298
|
2 |
|
evc_dict = {"id": self.id, "name": self.name, |
|
299
|
|
|
"uni_a": self.uni_a.as_dict(), |
|
300
|
|
|
"uni_z": self.uni_z.as_dict()} |
|
301
|
|
|
|
|
302
|
2 |
|
time_fmt = "%Y-%m-%dT%H:%M:%S" |
|
303
|
|
|
|
|
304
|
2 |
|
evc_dict["start_date"] = self.start_date |
|
305
|
2 |
|
if isinstance(self.start_date, datetime): |
|
306
|
2 |
|
evc_dict["start_date"] = self.start_date.strftime(time_fmt) |
|
307
|
|
|
|
|
308
|
2 |
|
evc_dict["end_date"] = self.end_date |
|
309
|
2 |
|
if isinstance(self.end_date, datetime): |
|
310
|
2 |
|
evc_dict["end_date"] = self.end_date.strftime(time_fmt) |
|
311
|
|
|
|
|
312
|
2 |
|
evc_dict['bandwidth'] = self.bandwidth |
|
313
|
2 |
|
evc_dict['primary_links'] = self.primary_links.as_dict() |
|
314
|
2 |
|
evc_dict['backup_links'] = self.backup_links.as_dict() |
|
315
|
2 |
|
evc_dict['current_path'] = self.current_path.as_dict() |
|
316
|
2 |
|
evc_dict['primary_path'] = self.primary_path.as_dict() |
|
317
|
2 |
|
evc_dict['backup_path'] = self.backup_path.as_dict() |
|
318
|
2 |
|
evc_dict['dynamic_backup_path'] = self.dynamic_backup_path |
|
319
|
|
|
|
|
320
|
|
|
# if self._requested: |
|
321
|
|
|
# request_dict = self._requested.copy() |
|
322
|
|
|
# request_dict['uni_a'] = request_dict['uni_a'].as_dict() |
|
323
|
|
|
# request_dict['uni_z'] = request_dict['uni_z'].as_dict() |
|
324
|
|
|
# request_dict['circuit_scheduler'] = self.circuit_scheduler |
|
325
|
|
|
# evc_dict['_requested'] = request_dict |
|
326
|
|
|
|
|
327
|
2 |
|
evc_dict["request_time"] = self.request_time |
|
328
|
2 |
|
if isinstance(self.request_time, datetime): |
|
329
|
2 |
|
evc_dict["request_time"] = self.request_time.strftime(time_fmt) |
|
330
|
|
|
|
|
331
|
2 |
|
time = self.creation_time.strftime(time_fmt) |
|
332
|
2 |
|
evc_dict['creation_time'] = time |
|
333
|
|
|
|
|
334
|
2 |
|
evc_dict['owner'] = self.owner |
|
335
|
2 |
|
evc_dict['circuit_scheduler'] = [sc.as_dict() |
|
336
|
|
|
for sc in self.circuit_scheduler] |
|
337
|
|
|
|
|
338
|
2 |
|
evc_dict['active'] = self.is_active() |
|
339
|
2 |
|
evc_dict['enabled'] = self.is_enabled() |
|
340
|
2 |
|
evc_dict['archived'] = self.archived |
|
341
|
2 |
|
evc_dict['priority'] = self.priority |
|
342
|
|
|
|
|
343
|
2 |
|
return evc_dict |
|
344
|
|
|
|
|
345
|
2 |
|
@property |
|
346
|
|
|
def id(self): # pylint: disable=invalid-name |
|
347
|
|
|
"""Return this EVC's ID.""" |
|
348
|
2 |
|
return self._id |
|
349
|
|
|
|
|
350
|
2 |
|
def archive(self): |
|
351
|
|
|
"""Archive this EVC on deletion.""" |
|
352
|
|
|
self.archived = True |
|
353
|
|
|
|
|
354
|
|
|
|
|
355
|
|
|
# pylint: disable=fixme, too-many-public-methods |
|
356
|
2 |
|
class EVCDeploy(EVCBase): |
|
357
|
|
|
"""Class to handle the deploy procedures.""" |
|
358
|
|
|
|
|
359
|
2 |
|
def create(self): |
|
360
|
|
|
"""Create a EVC.""" |
|
361
|
|
|
|
|
362
|
2 |
|
def discover_new_path(self): |
|
363
|
|
|
"""Discover a new path to satisfy this circuit and deploy.""" |
|
364
|
2 |
|
return DynamicPathManager.get_best_path(self) |
|
365
|
|
|
|
|
366
|
2 |
|
def change_path(self): |
|
367
|
|
|
"""Change EVC path.""" |
|
368
|
|
|
|
|
369
|
2 |
|
def reprovision(self): |
|
370
|
|
|
"""Force the EVC (re-)provisioning.""" |
|
371
|
|
|
|
|
372
|
2 |
|
def is_affected_by_link(self, link): |
|
373
|
|
|
"""Return True if this EVC has the given link on its current path.""" |
|
374
|
|
|
return link in self.current_path |
|
375
|
|
|
|
|
376
|
2 |
|
def link_affected_by_interface(self, interface): |
|
377
|
|
|
"""Return True if this EVC has the given link on its current path.""" |
|
378
|
|
|
return self.current_path.link_affected_by_interface(interface) |
|
379
|
|
|
|
|
380
|
2 |
|
def is_backup_path_affected_by_link(self, link): |
|
381
|
|
|
"""Return True if the backup path of this EVC uses the given link.""" |
|
382
|
|
|
return link in self.backup_path |
|
383
|
|
|
|
|
384
|
|
|
# pylint: disable=invalid-name |
|
385
|
2 |
|
def is_primary_path_affected_by_link(self, link): |
|
386
|
|
|
"""Return True if the primary path of this EVC uses the given link.""" |
|
387
|
|
|
return link in self.primary_path |
|
388
|
|
|
|
|
389
|
2 |
|
def is_using_primary_path(self): |
|
390
|
|
|
"""Verify if the current deployed path is self.primary_path.""" |
|
391
|
|
|
return self.primary_path and (self.current_path == self.primary_path) |
|
392
|
|
|
|
|
393
|
2 |
|
def is_using_backup_path(self): |
|
394
|
|
|
"""Verify if the current deployed path is self.backup_path.""" |
|
395
|
|
|
return self.backup_path and (self.current_path == self.backup_path) |
|
396
|
|
|
|
|
397
|
2 |
|
def is_using_dynamic_path(self): |
|
398
|
|
|
"""Verify if the current deployed path is a dynamic path.""" |
|
399
|
|
|
if self.current_path and \ |
|
400
|
|
|
not self.is_using_primary_path() and \ |
|
401
|
|
|
not self.is_using_backup_path() and \ |
|
402
|
|
|
self.current_path.status == EntityStatus.UP: |
|
403
|
|
|
return True |
|
404
|
|
|
return False |
|
405
|
|
|
|
|
406
|
2 |
|
def deploy_to_backup_path(self): |
|
407
|
|
|
"""Deploy the backup path into the datapaths of this circuit. |
|
408
|
|
|
|
|
409
|
|
|
If the backup_path attribute is valid and up, this method will try to |
|
410
|
|
|
deploy this backup_path. |
|
411
|
|
|
|
|
412
|
|
|
If everything fails and dynamic_backup_path is True, then tries to |
|
413
|
|
|
deploy a dynamic path. |
|
414
|
|
|
""" |
|
415
|
|
|
# TODO: Remove flows from current (cookies) |
|
416
|
2 |
|
if self.is_using_backup_path(): |
|
417
|
|
|
# TODO: Log to say that cannot move backup to backup |
|
418
|
|
|
return True |
|
419
|
|
|
|
|
420
|
2 |
|
success = False |
|
421
|
2 |
|
if self.backup_path.status is EntityStatus.UP: |
|
422
|
2 |
|
success = self.deploy_to_path(self.backup_path) |
|
423
|
|
|
|
|
424
|
2 |
|
if success: |
|
425
|
2 |
|
return True |
|
426
|
|
|
|
|
427
|
2 |
|
if self.dynamic_backup_path: |
|
428
|
2 |
|
return self.deploy_to_path() |
|
429
|
|
|
|
|
430
|
2 |
|
return False |
|
431
|
|
|
|
|
432
|
2 |
|
def deploy_to_primary_path(self): |
|
433
|
|
|
"""Deploy the primary path into the datapaths of this circuit. |
|
434
|
|
|
|
|
435
|
|
|
If the primary_path attribute is valid and up, this method will try to |
|
436
|
|
|
deploy this primary_path. |
|
437
|
|
|
""" |
|
438
|
|
|
# TODO: Remove flows from current (cookies) |
|
439
|
2 |
|
if self.is_using_primary_path(): |
|
440
|
|
|
# TODO: Log to say that cannot move primary to primary |
|
441
|
|
|
return True |
|
442
|
|
|
|
|
443
|
2 |
|
if self.primary_path.status is EntityStatus.UP: |
|
444
|
2 |
|
return self.deploy_to_path(self.primary_path) |
|
445
|
2 |
|
return False |
|
446
|
|
|
|
|
447
|
2 |
|
def deploy(self): |
|
448
|
|
|
"""Deploy EVC to best path. |
|
449
|
|
|
|
|
450
|
|
|
Best path can be the primary path, if available. If not, the backup |
|
451
|
|
|
path, and, if it is also not available, a dynamic path. |
|
452
|
|
|
""" |
|
453
|
2 |
|
if self.archived: |
|
454
|
|
|
return False |
|
455
|
2 |
|
self.enable() |
|
456
|
2 |
|
success = self.deploy_to_primary_path() |
|
457
|
2 |
|
if not success: |
|
458
|
2 |
|
success = self.deploy_to_backup_path() |
|
459
|
|
|
|
|
460
|
2 |
|
return success |
|
461
|
|
|
|
|
462
|
2 |
|
@staticmethod |
|
463
|
|
|
def get_path_status(path): |
|
464
|
|
|
"""Check for the current status of a path. |
|
465
|
|
|
|
|
466
|
|
|
If any link in this path is down, the path is considered down. |
|
467
|
|
|
""" |
|
468
|
|
|
if not path: |
|
469
|
|
|
return EntityStatus.DISABLED |
|
470
|
|
|
|
|
471
|
|
|
for link in path: |
|
472
|
|
|
if link.status is not EntityStatus.UP: |
|
473
|
|
|
return link.status |
|
474
|
|
|
return EntityStatus.UP |
|
475
|
|
|
|
|
476
|
|
|
# def discover_new_path(self): |
|
477
|
|
|
# # TODO: discover a new path to satisfy this circuit and deploy |
|
478
|
|
|
|
|
479
|
2 |
|
def remove(self): |
|
480
|
|
|
"""Remove EVC path and disable it.""" |
|
481
|
|
|
self.remove_current_flows() |
|
482
|
|
|
self.disable() |
|
483
|
|
|
self.sync() |
|
484
|
|
|
|
|
485
|
2 |
|
def remove_current_flows(self): |
|
486
|
|
|
"""Remove all flows from current path.""" |
|
487
|
2 |
|
switches = set() |
|
488
|
|
|
|
|
489
|
2 |
|
for link in self.current_path: |
|
490
|
2 |
|
switches.add(link.endpoint_a.switch) |
|
491
|
2 |
|
switches.add(link.endpoint_b.switch) |
|
492
|
|
|
|
|
493
|
2 |
|
match = {'cookie': self.get_cookie(), |
|
494
|
|
|
'cookie_mask': 18446744073709551615} |
|
495
|
|
|
|
|
496
|
2 |
|
for switch in switches: |
|
497
|
2 |
|
self._send_flow_mods(switch, [match], 'delete') |
|
498
|
|
|
|
|
499
|
2 |
|
self.current_path.make_vlans_available() |
|
500
|
2 |
|
self.current_path = Path([]) |
|
501
|
2 |
|
self.deactivate() |
|
502
|
2 |
|
self.sync() |
|
503
|
|
|
|
|
504
|
2 |
|
@staticmethod |
|
505
|
2 |
|
def links_zipped(path=None): |
|
506
|
|
|
"""Return an iterator which yields pairs of links in order.""" |
|
507
|
2 |
|
if not path: |
|
508
|
|
|
return [] |
|
509
|
2 |
|
return zip(path[:-1], path[1:]) |
|
510
|
|
|
|
|
511
|
2 |
|
def should_deploy(self, path=None): |
|
512
|
|
|
"""Verify if the circuit should be deployed.""" |
|
513
|
2 |
|
if not path: |
|
514
|
2 |
|
log.debug("Path is empty.") |
|
515
|
2 |
|
return False |
|
516
|
|
|
|
|
517
|
2 |
|
if not self.is_enabled(): |
|
518
|
2 |
|
log.debug(f'{self} is disabled.') |
|
519
|
2 |
|
return False |
|
520
|
|
|
|
|
521
|
2 |
|
if not self.is_active(): |
|
522
|
2 |
|
log.debug(f'{self} will be deployed.') |
|
523
|
2 |
|
return True |
|
524
|
|
|
|
|
525
|
2 |
|
return False |
|
526
|
|
|
|
|
527
|
2 |
|
def deploy_to_path(self, path=None): |
|
528
|
|
|
"""Install the flows for this circuit. |
|
529
|
|
|
|
|
530
|
|
|
Procedures to deploy: |
|
531
|
|
|
|
|
532
|
|
|
0. Remove current flows installed |
|
533
|
|
|
1. Decide if will deploy "path" or discover a new path |
|
534
|
|
|
2. Choose vlan |
|
535
|
|
|
3. Install NNI flows |
|
536
|
|
|
4. Install UNI flows |
|
537
|
|
|
5. Activate |
|
538
|
|
|
6. Update current_path |
|
539
|
|
|
7. Update links caches(primary, current, backup) |
|
540
|
|
|
|
|
541
|
|
|
""" |
|
542
|
2 |
|
self.remove_current_flows() |
|
543
|
2 |
|
if not self.should_deploy(path): |
|
544
|
2 |
|
path = self.discover_new_path() |
|
545
|
2 |
|
if not path: |
|
546
|
2 |
|
return False |
|
547
|
|
|
|
|
548
|
2 |
|
path.choose_vlans() |
|
549
|
2 |
|
self._install_nni_flows(path) |
|
550
|
2 |
|
self._install_uni_flows(path) |
|
551
|
2 |
|
self.activate() |
|
552
|
2 |
|
self.current_path = path |
|
553
|
2 |
|
self.sync() |
|
554
|
2 |
|
log.info(f"{self} was deployed.") |
|
555
|
2 |
|
return True |
|
556
|
|
|
|
|
557
|
2 |
|
def _install_nni_flows(self, path=None): |
|
558
|
|
|
"""Install NNI flows.""" |
|
559
|
2 |
|
for incoming, outcoming in self.links_zipped(path): |
|
560
|
2 |
|
in_vlan = incoming.get_metadata('s_vlan').value |
|
561
|
2 |
|
out_vlan = outcoming.get_metadata('s_vlan').value |
|
562
|
|
|
|
|
563
|
2 |
|
flows = [] |
|
564
|
|
|
# Flow for one direction |
|
565
|
2 |
|
flows.append(self._prepare_nni_flow(incoming.endpoint_b, |
|
566
|
|
|
outcoming.endpoint_a, |
|
567
|
|
|
in_vlan, out_vlan)) |
|
568
|
|
|
|
|
569
|
|
|
# Flow for the other direction |
|
570
|
2 |
|
flows.append(self._prepare_nni_flow(outcoming.endpoint_a, |
|
571
|
|
|
incoming.endpoint_b, |
|
572
|
|
|
out_vlan, in_vlan)) |
|
573
|
2 |
|
self._send_flow_mods(incoming.endpoint_b.switch, flows) |
|
574
|
|
|
|
|
575
|
2 |
|
def _install_uni_flows(self, path=None): |
|
576
|
|
|
"""Install UNI flows.""" |
|
577
|
2 |
|
if not path: |
|
578
|
|
|
log.info('install uni flows without path.') |
|
579
|
|
|
return |
|
580
|
|
|
|
|
581
|
|
|
# Determine VLANs |
|
582
|
2 |
|
in_vlan_a = self.uni_a.user_tag.value if self.uni_a.user_tag else None |
|
583
|
2 |
|
out_vlan_a = path[0].get_metadata('s_vlan').value |
|
584
|
|
|
|
|
585
|
2 |
|
in_vlan_z = self.uni_z.user_tag.value if self.uni_z.user_tag else None |
|
586
|
2 |
|
out_vlan_z = path[-1].get_metadata('s_vlan').value |
|
587
|
|
|
|
|
588
|
|
|
# Flows for the first UNI |
|
589
|
2 |
|
flows_a = [] |
|
590
|
|
|
|
|
591
|
|
|
# Flow for one direction, pushing the service tag |
|
592
|
2 |
|
push_flow = self._prepare_push_flow(self.uni_a.interface, |
|
593
|
|
|
path[0].endpoint_a, |
|
594
|
|
|
in_vlan_a, out_vlan_a, in_vlan_z) |
|
595
|
2 |
|
flows_a.append(push_flow) |
|
596
|
|
|
|
|
597
|
|
|
# Flow for the other direction, popping the service tag |
|
598
|
2 |
|
pop_flow = self._prepare_pop_flow(path[0].endpoint_a, |
|
599
|
|
|
self.uni_a.interface, out_vlan_a) |
|
600
|
2 |
|
flows_a.append(pop_flow) |
|
601
|
|
|
|
|
602
|
2 |
|
self._send_flow_mods(self.uni_a.interface.switch, flows_a) |
|
603
|
|
|
|
|
604
|
|
|
# Flows for the second UNI |
|
605
|
2 |
|
flows_z = [] |
|
606
|
|
|
|
|
607
|
|
|
# Flow for one direction, pushing the service tag |
|
608
|
2 |
|
push_flow = self._prepare_push_flow(self.uni_z.interface, |
|
609
|
|
|
path[-1].endpoint_b, |
|
610
|
|
|
in_vlan_z, out_vlan_z, in_vlan_a) |
|
611
|
2 |
|
flows_z.append(push_flow) |
|
612
|
|
|
|
|
613
|
|
|
# Flow for the other direction, popping the service tag |
|
614
|
2 |
|
pop_flow = self._prepare_pop_flow(path[-1].endpoint_b, |
|
615
|
|
|
self.uni_z.interface, out_vlan_z) |
|
616
|
2 |
|
flows_z.append(pop_flow) |
|
617
|
|
|
|
|
618
|
2 |
|
self._send_flow_mods(self.uni_z.interface.switch, flows_z) |
|
619
|
|
|
|
|
620
|
2 |
|
@staticmethod |
|
621
|
2 |
|
def _send_flow_mods(switch, flow_mods, command='flows'): |
|
622
|
|
|
"""Send a flow_mod list to a specific switch. |
|
623
|
|
|
|
|
624
|
|
|
Args: |
|
625
|
|
|
switch(Switch): The target of flows. |
|
626
|
|
|
flow_mods(dict): Python dictionary with flow_mods. |
|
627
|
|
|
command(str): By default is 'flows'. To remove a flow is 'remove'. |
|
628
|
|
|
|
|
629
|
|
|
""" |
|
630
|
2 |
|
endpoint = f'{settings.MANAGER_URL}/{command}/{switch.id}' |
|
631
|
|
|
|
|
632
|
2 |
|
data = {"flows": flow_mods} |
|
633
|
2 |
|
requests.post(endpoint, json=data) |
|
634
|
|
|
|
|
635
|
2 |
|
def get_cookie(self): |
|
636
|
|
|
"""Return the cookie integer from evc id.""" |
|
637
|
2 |
|
value = self.id[len(self.id)//2:] |
|
638
|
2 |
|
return int(value, 16) |
|
639
|
|
|
|
|
640
|
2 |
|
def _prepare_flow_mod(self, in_interface, out_interface): |
|
641
|
|
|
"""Prepare a common flow mod.""" |
|
642
|
2 |
|
default_action = {"action_type": "output", |
|
643
|
|
|
"port": out_interface.port_number} |
|
644
|
|
|
|
|
645
|
2 |
|
flow_mod = {"match": {"in_port": in_interface.port_number}, |
|
646
|
|
|
"cookie": self.get_cookie(), |
|
647
|
|
|
"actions": [default_action]} |
|
648
|
|
|
|
|
649
|
2 |
|
return flow_mod |
|
650
|
|
|
|
|
651
|
2 |
|
def _prepare_nni_flow(self, |
|
652
|
|
|
in_interface, out_interface, in_vlan, out_vlan): |
|
653
|
|
|
"""Create NNI flows.""" |
|
654
|
2 |
|
flow_mod = self._prepare_flow_mod(in_interface, out_interface) |
|
655
|
2 |
|
flow_mod['match']['dl_vlan'] = in_vlan |
|
656
|
|
|
|
|
657
|
2 |
|
new_action = {"action_type": "set_vlan", |
|
658
|
|
|
"vlan_id": out_vlan} |
|
659
|
2 |
|
flow_mod["actions"].insert(0, new_action) |
|
660
|
|
|
|
|
661
|
2 |
|
return flow_mod |
|
662
|
|
|
|
|
663
|
2 |
|
def _prepare_push_flow(self, *args): |
|
664
|
|
|
"""Prepare push flow. |
|
665
|
|
|
|
|
666
|
|
|
Arguments: |
|
667
|
|
|
in_interface(str): Interface input. |
|
668
|
|
|
out_interface(str): Interface output. |
|
669
|
|
|
in_vlan(str): Vlan input. |
|
670
|
|
|
out_vlan(str): Vlan output. |
|
671
|
|
|
new_in_vlan(str): Interface input. |
|
672
|
|
|
|
|
673
|
|
|
Return: |
|
674
|
|
|
dict: An python dictionary representing a FlowMod |
|
675
|
|
|
|
|
676
|
|
|
""" |
|
677
|
|
|
# assign all arguments |
|
678
|
2 |
|
in_interface, out_interface, in_vlan, out_vlan, new_in_vlan = args |
|
679
|
|
|
|
|
680
|
2 |
|
flow_mod = self._prepare_flow_mod(in_interface, out_interface) |
|
681
|
2 |
|
flow_mod['match']['dl_vlan'] = in_vlan |
|
682
|
|
|
|
|
683
|
2 |
|
new_action = {"action_type": "set_vlan", |
|
684
|
|
|
"vlan_id": out_vlan} |
|
685
|
2 |
|
flow_mod["actions"].insert(0, new_action) |
|
686
|
|
|
|
|
687
|
2 |
|
new_action = {"action_type": "push_vlan", |
|
688
|
|
|
"tag_type": "s"} |
|
689
|
2 |
|
flow_mod["actions"].insert(0, new_action) |
|
690
|
|
|
|
|
691
|
2 |
|
new_action = {"action_type": "set_vlan", |
|
692
|
|
|
"vlan_id": new_in_vlan} |
|
693
|
2 |
|
flow_mod["actions"].insert(0, new_action) |
|
694
|
|
|
|
|
695
|
2 |
|
return flow_mod |
|
696
|
|
|
|
|
697
|
2 |
|
def _prepare_pop_flow(self, in_interface, out_interface, in_vlan): |
|
698
|
|
|
"""Prepare pop flow.""" |
|
699
|
2 |
|
flow_mod = self._prepare_flow_mod(in_interface, out_interface) |
|
700
|
2 |
|
flow_mod['match']['dl_vlan'] = in_vlan |
|
701
|
2 |
|
new_action = {"action_type": "pop_vlan"} |
|
702
|
2 |
|
flow_mod["actions"].insert(0, new_action) |
|
703
|
2 |
|
return flow_mod |
|
704
|
|
|
|
|
705
|
|
|
|
|
706
|
2 |
|
class LinkProtection(EVCDeploy): |
|
707
|
|
|
"""Class to handle link protection.""" |
|
708
|
|
|
|
|
709
|
2 |
|
def is_affected_by_link(self, link=None): |
|
710
|
|
|
"""Verify if the current path is affected by link down event.""" |
|
711
|
|
|
return self.current_path.is_affected_by_link(link) |
|
712
|
|
|
|
|
713
|
2 |
|
def is_using_primary_path(self): |
|
714
|
|
|
"""Verify if the current deployed path is self.primary_path.""" |
|
715
|
2 |
|
return self.current_path == self.primary_path |
|
716
|
|
|
|
|
717
|
2 |
|
def is_using_backup_path(self): |
|
718
|
|
|
"""Verify if the current deployed path is self.backup_path.""" |
|
719
|
2 |
|
return self.current_path == self.backup_path |
|
720
|
|
|
|
|
721
|
2 |
|
def is_using_dynamic_path(self): |
|
722
|
|
|
"""Verify if the current deployed path is dynamic.""" |
|
723
|
2 |
|
if self.current_path and \ |
|
724
|
|
|
not self.is_using_primary_path() and \ |
|
725
|
|
|
not self.is_using_backup_path() and \ |
|
726
|
|
|
self.current_path.status is EntityStatus.UP: |
|
727
|
|
|
return True |
|
728
|
2 |
|
return False |
|
729
|
|
|
|
|
730
|
2 |
|
def deploy_to(self, path_name=None, path=None): |
|
731
|
|
|
"""Create a deploy to path.""" |
|
732
|
2 |
|
if self.current_path == path: |
|
733
|
2 |
|
log.debug(f'{path_name} is equal to current_path.') |
|
734
|
2 |
|
return True |
|
735
|
|
|
|
|
736
|
2 |
|
if path.status is EntityStatus.UP: |
|
737
|
2 |
|
return self.deploy_to_path(path) |
|
738
|
|
|
|
|
739
|
2 |
|
return False |
|
740
|
|
|
|
|
741
|
2 |
|
def handle_link_up(self, link): |
|
742
|
|
|
"""Handle circuit when link down. |
|
743
|
|
|
|
|
744
|
|
|
Args: |
|
745
|
|
|
link(Link): Link affected by link.down event. |
|
746
|
|
|
|
|
747
|
|
|
""" |
|
748
|
2 |
|
if self.is_using_primary_path(): |
|
749
|
2 |
|
return True |
|
750
|
|
|
|
|
751
|
2 |
|
success = False |
|
752
|
2 |
|
if self.primary_path.is_affected_by_link(link): |
|
753
|
2 |
|
success = self.deploy_to_primary_path() |
|
754
|
|
|
|
|
755
|
2 |
|
if success: |
|
756
|
2 |
|
return True |
|
757
|
|
|
|
|
758
|
|
|
# We tried to deploy(primary_path) without success. |
|
759
|
|
|
# And in this case is up by some how. Nothing to do. |
|
760
|
2 |
|
if self.is_using_backup_path() or self.is_using_dynamic_path(): |
|
761
|
|
|
return True |
|
762
|
|
|
|
|
763
|
|
|
# In this case, probably the circuit is not being used and |
|
764
|
|
|
# we can move to backup |
|
765
|
2 |
|
if self.backup_path.is_affected_by_link(link): |
|
766
|
2 |
|
success = self.deploy_to_backup_path() |
|
767
|
|
|
|
|
768
|
2 |
|
if success: |
|
769
|
2 |
|
return True |
|
770
|
|
|
|
|
771
|
|
|
# In this case, the circuit is not being used and we should |
|
772
|
|
|
# try a dynamic path |
|
773
|
|
|
if self.dynamic_backup_path: |
|
774
|
|
|
return self.deploy_to_path() |
|
775
|
|
|
|
|
776
|
|
|
return True |
|
777
|
|
|
|
|
778
|
2 |
|
def handle_link_down(self): |
|
779
|
|
|
"""Handle circuit when link down. |
|
780
|
|
|
|
|
781
|
|
|
Returns: |
|
782
|
|
|
bool: True if the re-deploy was successly otherwise False. |
|
783
|
|
|
|
|
784
|
|
|
""" |
|
785
|
2 |
|
success = False |
|
786
|
2 |
|
if self.is_using_primary_path(): |
|
787
|
2 |
|
success = self.deploy_to_backup_path() |
|
788
|
2 |
|
elif self.is_using_backup_path(): |
|
789
|
2 |
|
success = self.deploy_to_primary_path() |
|
790
|
|
|
|
|
791
|
2 |
|
if not success and self.dynamic_backup_path: |
|
792
|
2 |
|
success = self.deploy_to_path() |
|
793
|
|
|
|
|
794
|
2 |
|
if success: |
|
795
|
2 |
|
log.debug(f"{self} deployed after link down.") |
|
796
|
|
|
else: |
|
797
|
2 |
|
self.deactivate() |
|
798
|
2 |
|
self.current_path = Path([]) |
|
799
|
2 |
|
self.sync() |
|
800
|
2 |
|
log.debug(f'Failed to re-deploy {self} after link down.') |
|
801
|
|
|
|
|
802
|
2 |
|
return success |
|
803
|
|
|
|
|
804
|
|
|
|
|
805
|
2 |
|
class EVC(LinkProtection): |
|
806
|
|
|
"""Class that represents a E-Line Virtual Connection.""" |
|
807
|
|
|
|