1
|
|
|
"""Classes used in the main application.""" # pylint: disable=too-many-lines |
2
|
1 |
|
from collections import OrderedDict |
3
|
1 |
|
from datetime import datetime |
4
|
1 |
|
from threading import Lock |
5
|
1 |
|
from uuid import uuid4 |
6
|
|
|
|
7
|
1 |
|
import requests |
8
|
1 |
|
from glom import glom |
9
|
1 |
|
from requests.exceptions import ConnectTimeout |
10
|
|
|
|
11
|
1 |
|
from kytos.core import log |
12
|
1 |
|
from kytos.core.common import EntityStatus, GenericEntity |
13
|
1 |
|
from kytos.core.exceptions import KytosNoTagAvailableError |
14
|
1 |
|
from kytos.core.helpers import get_time, now |
15
|
1 |
|
from kytos.core.interface import UNI |
16
|
1 |
|
from napps.kytos.mef_eline import controllers, settings |
17
|
1 |
|
from napps.kytos.mef_eline.exceptions import FlowModException, InvalidPath |
18
|
1 |
|
from napps.kytos.mef_eline.utils import (compare_endpoint_trace, |
19
|
|
|
compare_uni_out_trace, emit_event, |
20
|
|
|
map_evc_event_content, |
21
|
|
|
notify_link_available_tags, |
22
|
|
|
uni_to_str) |
23
|
|
|
|
24
|
1 |
|
from .path import DynamicPathManager, Path |
25
|
|
|
|
26
|
|
|
|
27
|
1 |
|
class EVCBase(GenericEntity): |
28
|
|
|
"""Class to represent a circuit.""" |
29
|
|
|
|
30
|
1 |
|
read_only_attributes = [ |
31
|
|
|
"creation_time", |
32
|
|
|
"active", |
33
|
|
|
"current_path", |
34
|
|
|
"failover_path", |
35
|
|
|
"_id", |
36
|
|
|
"archived", |
37
|
|
|
] |
38
|
1 |
|
attributes_requiring_redeploy = [ |
39
|
|
|
"primary_path", |
40
|
|
|
"backup_path", |
41
|
|
|
"dynamic_backup_path", |
42
|
|
|
"queue_id", |
43
|
|
|
"sb_priority", |
44
|
|
|
"primary_constraints", |
45
|
|
|
"secondary_constraints", |
46
|
|
|
"uni_a", |
47
|
|
|
"uni_z", |
48
|
|
|
] |
49
|
1 |
|
required_attributes = ["name", "uni_a", "uni_z"] |
50
|
|
|
|
51
|
1 |
|
def __init__(self, controller, **kwargs): |
52
|
|
|
"""Create an EVC instance with the provided parameters. |
53
|
|
|
|
54
|
|
|
Args: |
55
|
|
|
id(str): EVC identifier. Whether it's None an ID will be genereted. |
56
|
|
|
Only the first 14 bytes passed will be used. |
57
|
|
|
name: represents an EVC name.(Required) |
58
|
|
|
uni_a (UNI): Endpoint A for User Network Interface.(Required) |
59
|
|
|
uni_z (UNI): Endpoint Z for User Network Interface.(Required) |
60
|
|
|
start_date(datetime|str): Date when the EVC was registred. |
61
|
|
|
Default is now(). |
62
|
|
|
end_date(datetime|str): Final date that the EVC will be fineshed. |
63
|
|
|
Default is None. |
64
|
|
|
bandwidth(int): Bandwidth used by EVC instance. Default is 0. |
65
|
|
|
primary_links(list): Primary links used by evc. Default is [] |
66
|
|
|
backup_links(list): Backups links used by evc. Default is [] |
67
|
|
|
current_path(list): Circuit being used at the moment if this is an |
68
|
|
|
active circuit. Default is []. |
69
|
|
|
failover_path(list): Path being used to provide EVC protection via |
70
|
|
|
failover during link failures. Default is []. |
71
|
|
|
primary_path(list): primary circuit offered to user IF one or more |
72
|
|
|
links were provided. Default is []. |
73
|
|
|
backup_path(list): backup circuit offered to the user IF one or |
74
|
|
|
more links were provided. Default is []. |
75
|
|
|
dynamic_backup_path(bool): Enable computer backup path dynamically. |
76
|
|
|
Dafault is False. |
77
|
|
|
creation_time(datetime|str): datetime when the circuit should be |
78
|
|
|
activated. default is now(). |
79
|
|
|
enabled(Boolean): attribute to indicate the administrative state; |
80
|
|
|
default is False. |
81
|
|
|
active(Boolean): attribute to indicate the operational state; |
82
|
|
|
default is False. |
83
|
|
|
archived(Boolean): indicate the EVC has been deleted and is |
84
|
|
|
archived; default is False. |
85
|
|
|
owner(str): The EVC owner. Default is None. |
86
|
|
|
sb_priority(int): Service level provided in the request. |
87
|
|
|
Default is None. |
88
|
|
|
service_level(int): Service level provided. The higher the better. |
89
|
|
|
Default is 0. |
90
|
|
|
|
91
|
|
|
Raises: |
92
|
|
|
ValueError: raised when object attributes are invalid. |
93
|
|
|
|
94
|
|
|
""" |
95
|
1 |
|
self._validate(**kwargs) |
96
|
1 |
|
super().__init__() |
97
|
|
|
|
98
|
|
|
# required attributes |
99
|
1 |
|
self._id = kwargs.get("id", uuid4().hex)[:14] |
100
|
1 |
|
self.uni_a = kwargs.get("uni_a") |
101
|
1 |
|
self.uni_z = kwargs.get("uni_z") |
102
|
1 |
|
self.name = kwargs.get("name") |
103
|
|
|
|
104
|
|
|
# optional attributes |
105
|
1 |
|
self.start_date = get_time(kwargs.get("start_date")) or now() |
106
|
1 |
|
self.end_date = get_time(kwargs.get("end_date")) or None |
107
|
1 |
|
self.queue_id = kwargs.get("queue_id", None) |
108
|
|
|
|
109
|
1 |
|
self.bandwidth = kwargs.get("bandwidth", 0) |
110
|
1 |
|
self.primary_links = Path(kwargs.get("primary_links", [])) |
111
|
1 |
|
self.backup_links = Path(kwargs.get("backup_links", [])) |
112
|
1 |
|
self.current_path = Path(kwargs.get("current_path", [])) |
113
|
1 |
|
self.failover_path = Path(kwargs.get("failover_path", [])) |
114
|
1 |
|
self.primary_path = Path(kwargs.get("primary_path", [])) |
115
|
1 |
|
self.backup_path = Path(kwargs.get("backup_path", [])) |
116
|
1 |
|
self.dynamic_backup_path = kwargs.get("dynamic_backup_path", False) |
117
|
1 |
|
self.primary_constraints = kwargs.get("primary_constraints", {}) |
118
|
1 |
|
self.secondary_constraints = kwargs.get("secondary_constraints", {}) |
119
|
1 |
|
self.creation_time = get_time(kwargs.get("creation_time")) or now() |
120
|
1 |
|
self.owner = kwargs.get("owner", None) |
121
|
1 |
|
self.sb_priority = kwargs.get("sb_priority", None) or kwargs.get( |
122
|
|
|
"priority", None |
123
|
|
|
) |
124
|
1 |
|
self.service_level = kwargs.get("service_level", 0) |
125
|
1 |
|
self.circuit_scheduler = kwargs.get("circuit_scheduler", []) |
126
|
1 |
|
self.flow_removed_at = get_time(kwargs.get("flow_removed_at")) or None |
127
|
1 |
|
self.updated_at = get_time(kwargs.get("updated_at")) or now() |
128
|
1 |
|
self.execution_rounds = kwargs.get("execution_rounds", 0) |
129
|
|
|
|
130
|
1 |
|
self.current_links_cache = set() |
131
|
1 |
|
self.primary_links_cache = set() |
132
|
1 |
|
self.backup_links_cache = set() |
133
|
|
|
|
134
|
1 |
|
self.lock = Lock() |
135
|
|
|
|
136
|
1 |
|
self.archived = kwargs.get("archived", False) |
137
|
|
|
|
138
|
1 |
|
self.metadata = kwargs.get("metadata", {}) |
139
|
|
|
|
140
|
1 |
|
self._controller = controller |
141
|
1 |
|
self._mongo_controller = controllers.ELineController() |
142
|
|
|
|
143
|
1 |
|
if kwargs.get("active", False): |
144
|
1 |
|
self.activate() |
145
|
|
|
else: |
146
|
1 |
|
self.deactivate() |
147
|
|
|
|
148
|
1 |
|
if kwargs.get("enabled", False): |
149
|
1 |
|
self.enable() |
150
|
|
|
else: |
151
|
1 |
|
self.disable() |
152
|
|
|
|
153
|
|
|
# datetime of user request for a EVC (or datetime when object was |
154
|
|
|
# created) |
155
|
1 |
|
self.request_time = kwargs.get("request_time", now()) |
156
|
|
|
# dict with the user original request (input) |
157
|
1 |
|
self._requested = kwargs |
158
|
|
|
|
159
|
1 |
|
def sync(self): |
160
|
|
|
"""Sync this EVC in the MongoDB.""" |
161
|
1 |
|
self.updated_at = now() |
162
|
1 |
|
self._mongo_controller.upsert_evc(self.as_dict()) |
163
|
|
|
|
164
|
1 |
|
def update(self, **kwargs): |
165
|
|
|
"""Update evc attributes. |
166
|
|
|
|
167
|
|
|
This method will raises an error trying to change the following |
168
|
|
|
attributes: [name, uni_a and uni_z] |
169
|
|
|
|
170
|
|
|
Returns: |
171
|
|
|
the values for enable and a redeploy attribute, if exists and None |
172
|
|
|
otherwise |
173
|
|
|
Raises: |
174
|
|
|
ValueError: message with error detail. |
175
|
|
|
|
176
|
|
|
""" |
177
|
1 |
|
enable, redeploy = (None, None) |
178
|
1 |
|
uni_a = kwargs.get("uni_a") or self.uni_a |
179
|
1 |
|
uni_z = kwargs.get("uni_z") or self.uni_z |
180
|
1 |
|
self._validate_has_primary_or_dynamic( |
181
|
|
|
primary_path=kwargs.get("primary_path"), |
182
|
|
|
dynamic_backup_path=kwargs.get("dynamic_backup_path"), |
183
|
|
|
uni_a=uni_a, |
184
|
|
|
uni_z=uni_z, |
185
|
|
|
) |
186
|
1 |
|
for attribute, value in kwargs.items(): |
187
|
1 |
|
if attribute in self.read_only_attributes: |
188
|
1 |
|
raise ValueError(f"{attribute} can't be updated.") |
189
|
1 |
|
if not hasattr(self, attribute): |
190
|
1 |
|
raise ValueError(f'The attribute "{attribute}" is invalid.') |
191
|
1 |
|
if attribute in ("primary_path", "backup_path"): |
192
|
1 |
|
try: |
193
|
1 |
|
value.is_valid( |
194
|
|
|
uni_a.interface.switch, uni_z.interface.switch |
195
|
|
|
) |
196
|
1 |
|
except InvalidPath as exception: |
197
|
1 |
|
raise ValueError( # pylint: disable=raise-missing-from |
198
|
|
|
f"{attribute} is not a " f"valid path: {exception}" |
199
|
|
|
) |
200
|
1 |
|
for attribute, value in kwargs.items(): |
201
|
1 |
|
if attribute in ("enable", "enabled"): |
202
|
1 |
|
if value: |
203
|
1 |
|
self.enable() |
204
|
|
|
else: |
205
|
1 |
|
self.disable() |
206
|
1 |
|
enable = value |
207
|
|
|
else: |
208
|
1 |
|
setattr(self, attribute, value) |
209
|
1 |
|
if attribute in self.attributes_requiring_redeploy: |
210
|
1 |
|
redeploy = value |
211
|
1 |
|
self.sync() |
212
|
1 |
|
return enable, redeploy |
213
|
|
|
|
214
|
1 |
|
def set_flow_removed_at(self): |
215
|
|
|
"""Update flow_removed_at attribute.""" |
216
|
|
|
self.flow_removed_at = now() |
217
|
|
|
|
218
|
1 |
|
def has_recent_removed_flow(self, setting=settings): |
219
|
|
|
"""Check if any flow has been removed from the evc""" |
220
|
|
|
if self.flow_removed_at is None: |
221
|
|
|
return False |
222
|
|
|
res_seconds = (now() - self.flow_removed_at).seconds |
223
|
|
|
return res_seconds < setting.TIME_RECENT_DELETED_FLOWS |
224
|
|
|
|
225
|
1 |
|
def is_recent_updated(self, setting=settings): |
226
|
|
|
"""Check if the evc has been updated recently""" |
227
|
|
|
res_seconds = (now() - self.updated_at).seconds |
228
|
|
|
return res_seconds < setting.TIME_RECENT_UPDATED |
229
|
|
|
|
230
|
1 |
|
def __repr__(self): |
231
|
|
|
"""Repr method.""" |
232
|
1 |
|
return f"EVC({self._id}, {self.name})" |
233
|
|
|
|
234
|
1 |
|
def _validate(self, **kwargs): |
235
|
|
|
"""Do Basic validations. |
236
|
|
|
|
237
|
|
|
Verify required attributes: name, uni_a, uni_z |
238
|
|
|
Verify if the attributes uni_a and uni_z are valid. |
239
|
|
|
|
240
|
|
|
Raises: |
241
|
|
|
ValueError: message with error detail. |
242
|
|
|
|
243
|
|
|
""" |
244
|
1 |
|
for attribute in self.required_attributes: |
245
|
|
|
|
246
|
1 |
|
if attribute not in kwargs: |
247
|
1 |
|
raise ValueError(f"{attribute} is required.") |
248
|
|
|
|
249
|
1 |
|
if "uni" in attribute: |
250
|
1 |
|
uni = kwargs.get(attribute) |
251
|
1 |
|
if not isinstance(uni, UNI): |
252
|
|
|
raise ValueError(f"{attribute} is an invalid UNI.") |
253
|
|
|
|
254
|
1 |
|
if not uni.is_valid(): |
255
|
1 |
|
try: |
256
|
1 |
|
tag = uni.user_tag.value |
257
|
1 |
|
except AttributeError: |
258
|
|
|
tag = None |
259
|
1 |
|
message = f"VLAN tag {tag} is not available in {attribute}" |
260
|
|
|
raise ValueError(message) |
261
|
|
|
|
262
|
|
|
def _validate_has_primary_or_dynamic( |
263
|
|
|
self, |
264
|
|
|
primary_path=None, |
265
|
|
|
dynamic_backup_path=None, |
266
|
|
|
uni_a=None, |
267
|
1 |
|
uni_z=None, |
268
|
|
|
) -> None: |
269
|
|
|
"""Validate that it must have a primary path or allow dynamic paths.""" |
270
|
|
|
primary_path = ( |
271
|
|
|
primary_path |
272
|
1 |
|
if primary_path is not None |
273
|
|
|
else self.primary_path |
274
|
|
|
) |
275
|
|
|
dynamic_backup_path = ( |
276
|
|
|
dynamic_backup_path |
277
|
1 |
|
if dynamic_backup_path is not None |
278
|
1 |
|
else self.dynamic_backup_path |
279
|
1 |
|
) |
280
|
|
|
uni_a = uni_a if uni_a is not None else self.uni_a |
281
|
|
|
uni_z = uni_z if uni_z is not None else self.uni_z |
282
|
|
|
if ( |
283
|
|
|
not primary_path |
284
|
|
|
and not dynamic_backup_path |
285
|
1 |
|
and uni_a and uni_z |
286
|
1 |
|
and uni_a.interface.switch != uni_z.interface.switch |
287
|
|
|
): |
288
|
1 |
|
msg = "The EVC must have a primary path or allow dynamic paths." |
289
|
|
|
raise ValueError(msg) |
290
|
1 |
|
|
291
|
|
|
def __eq__(self, other): |
292
|
|
|
"""Override the default implementation.""" |
293
|
1 |
|
if not isinstance(other, EVC): |
294
|
1 |
|
return False |
295
|
1 |
|
|
296
|
1 |
|
attrs_to_compare = ["name", "uni_a", "uni_z", "owner", "bandwidth"] |
297
|
1 |
|
for attribute in attrs_to_compare: |
298
|
|
|
if getattr(other, attribute) != getattr(self, attribute): |
299
|
1 |
|
return False |
300
|
|
|
return True |
301
|
1 |
|
|
302
|
|
|
def is_intra_switch(self): |
303
|
1 |
|
"""Check if the UNIs are in the same switch.""" |
304
|
|
|
return self.uni_a.interface.switch == self.uni_z.interface.switch |
305
|
1 |
|
|
306
|
|
|
def shares_uni(self, other): |
307
|
|
|
"""Check if two EVCs share an UNI.""" |
308
|
|
|
if other.uni_a in (self.uni_a, self.uni_z) or other.uni_z in ( |
309
|
1 |
|
self.uni_a, |
310
|
|
|
self.uni_z, |
311
|
|
|
): |
312
|
1 |
|
return True |
313
|
|
|
return False |
314
|
1 |
|
|
315
|
|
|
def as_dict(self): |
316
|
|
|
"""Return a dictionary representing an EVC object.""" |
317
|
|
|
evc_dict = { |
318
|
|
|
"id": self.id, |
319
|
|
|
"name": self.name, |
320
|
|
|
"uni_a": self.uni_a.as_dict(), |
321
|
1 |
|
"uni_z": self.uni_z.as_dict(), |
322
|
|
|
} |
323
|
1 |
|
|
324
|
1 |
|
time_fmt = "%Y-%m-%dT%H:%M:%S" |
325
|
1 |
|
|
326
|
|
|
evc_dict["start_date"] = self.start_date |
327
|
1 |
|
if isinstance(self.start_date, datetime): |
328
|
1 |
|
evc_dict["start_date"] = self.start_date.strftime(time_fmt) |
329
|
1 |
|
|
330
|
|
|
evc_dict["end_date"] = self.end_date |
331
|
1 |
|
if isinstance(self.end_date, datetime): |
332
|
1 |
|
evc_dict["end_date"] = self.end_date.strftime(time_fmt) |
333
|
1 |
|
|
334
|
1 |
|
evc_dict["queue_id"] = self.queue_id |
335
|
1 |
|
evc_dict["bandwidth"] = self.bandwidth |
336
|
1 |
|
evc_dict["primary_links"] = self.primary_links.as_dict() |
337
|
1 |
|
evc_dict["backup_links"] = self.backup_links.as_dict() |
338
|
1 |
|
evc_dict["current_path"] = self.current_path.as_dict() |
339
|
1 |
|
evc_dict["failover_path"] = self.failover_path.as_dict() |
340
|
1 |
|
evc_dict["primary_path"] = self.primary_path.as_dict() |
341
|
|
|
evc_dict["backup_path"] = self.backup_path.as_dict() |
342
|
1 |
|
evc_dict["dynamic_backup_path"] = self.dynamic_backup_path |
343
|
1 |
|
evc_dict["metadata"] = self.metadata |
344
|
1 |
|
|
345
|
|
|
evc_dict["request_time"] = self.request_time |
346
|
1 |
|
if isinstance(self.request_time, datetime): |
347
|
1 |
|
evc_dict["request_time"] = self.request_time.strftime(time_fmt) |
348
|
|
|
|
349
|
1 |
|
time = self.creation_time.strftime(time_fmt) |
350
|
1 |
|
evc_dict["creation_time"] = time |
351
|
|
|
|
352
|
|
|
evc_dict["owner"] = self.owner |
353
|
|
|
evc_dict["circuit_scheduler"] = [ |
354
|
1 |
|
sc.as_dict() for sc in self.circuit_scheduler |
355
|
1 |
|
] |
356
|
1 |
|
|
357
|
1 |
|
evc_dict["active"] = self.is_active() |
358
|
1 |
|
evc_dict["enabled"] = self.is_enabled() |
359
|
1 |
|
evc_dict["archived"] = self.archived |
360
|
1 |
|
evc_dict["sb_priority"] = self.sb_priority |
361
|
1 |
|
evc_dict["service_level"] = self.service_level |
362
|
1 |
|
evc_dict["primary_constraints"] = self.primary_constraints |
363
|
|
|
evc_dict["secondary_constraints"] = self.secondary_constraints |
364
|
1 |
|
evc_dict["flow_removed_at"] = self.flow_removed_at |
365
|
|
|
evc_dict["updated_at"] = self.updated_at |
366
|
1 |
|
|
367
|
1 |
|
return evc_dict |
368
|
|
|
|
369
|
1 |
|
@property |
370
|
|
|
def id(self): # pylint: disable=invalid-name |
371
|
1 |
|
"""Return this EVC's ID.""" |
372
|
|
|
return self._id |
373
|
1 |
|
|
374
|
|
|
def archive(self): |
375
|
|
|
"""Archive this EVC on deletion.""" |
376
|
|
|
self.archived = True |
377
|
1 |
|
|
378
|
|
|
|
379
|
|
|
# pylint: disable=fixme, too-many-public-methods |
380
|
1 |
|
class EVCDeploy(EVCBase): |
381
|
|
|
"""Class to handle the deploy procedures.""" |
382
|
|
|
|
383
|
1 |
|
def create(self): |
384
|
|
|
"""Create a EVC.""" |
385
|
|
|
|
386
|
|
|
def discover_new_paths(self): |
387
|
|
|
"""Discover new paths to satisfy this circuit and deploy it.""" |
388
|
1 |
|
return DynamicPathManager.get_best_paths(self, |
389
|
|
|
**self.primary_constraints) |
390
|
|
|
|
391
|
|
|
def get_failover_path_candidates(self): |
392
|
|
|
"""Get failover paths to satisfy this EVC.""" |
393
|
|
|
# in the future we can return primary/backup paths as well |
394
|
|
|
# we just have to properly handle link_up and failover paths |
395
|
|
|
# if ( |
396
|
|
|
# self.is_using_primary_path() and |
397
|
1 |
|
# self.backup_path.status is EntityStatus.UP |
398
|
|
|
# ): |
399
|
1 |
|
# yield self.backup_path |
400
|
|
|
return DynamicPathManager.get_disjoint_paths(self, self.current_path) |
401
|
|
|
|
402
|
1 |
|
def change_path(self): |
403
|
|
|
"""Change EVC path.""" |
404
|
|
|
|
405
|
1 |
|
def reprovision(self): |
406
|
|
|
"""Force the EVC (re-)provisioning.""" |
407
|
1 |
|
|
408
|
|
|
def is_affected_by_link(self, link): |
409
|
1 |
|
"""Return True if this EVC has the given link on its current path.""" |
410
|
|
|
return link in self.current_path |
411
|
|
|
|
412
|
|
|
def link_affected_by_interface(self, interface): |
413
|
1 |
|
"""Return True if this EVC has the given link on its current path.""" |
414
|
|
|
return self.current_path.link_affected_by_interface(interface) |
415
|
1 |
|
|
416
|
|
|
def is_backup_path_affected_by_link(self, link): |
417
|
|
|
"""Return True if the backup path of this EVC uses the given link.""" |
418
|
1 |
|
return link in self.backup_path |
419
|
|
|
|
420
|
1 |
|
# pylint: disable=invalid-name |
421
|
|
|
def is_primary_path_affected_by_link(self, link): |
422
|
1 |
|
"""Return True if the primary path of this EVC uses the given link.""" |
423
|
|
|
return link in self.primary_path |
424
|
1 |
|
|
425
|
|
|
def is_failover_path_affected_by_link(self, link): |
426
|
1 |
|
"""Return True if this EVC has the given link on its failover path.""" |
427
|
|
|
return link in self.failover_path |
428
|
|
|
|
429
|
|
|
def is_eligible_for_failover_path(self): |
430
|
1 |
|
"""Verify if this EVC is eligible for failover path (EP029)""" |
431
|
|
|
# In the future this function can be augmented to consider |
432
|
|
|
# primary/backup, primary/dynamic, and other path combinations |
433
|
|
|
return ( |
434
|
|
|
self.dynamic_backup_path and |
435
|
1 |
|
not self.primary_path and not self.backup_path |
436
|
|
|
) |
437
|
1 |
|
|
438
|
|
|
def is_using_primary_path(self): |
439
|
1 |
|
"""Verify if the current deployed path is self.primary_path.""" |
440
|
|
|
return self.primary_path and (self.current_path == self.primary_path) |
441
|
1 |
|
|
442
|
|
|
def is_using_backup_path(self): |
443
|
1 |
|
"""Verify if the current deployed path is self.backup_path.""" |
444
|
|
|
return self.backup_path and (self.current_path == self.backup_path) |
445
|
1 |
|
|
446
|
|
|
def is_using_dynamic_path(self): |
447
|
|
|
"""Verify if the current deployed path is a dynamic path.""" |
448
|
|
|
if ( |
449
|
|
|
self.current_path |
450
|
|
|
and not self.is_using_primary_path() |
451
|
|
|
and not self.is_using_backup_path() |
452
|
1 |
|
and self.current_path.status == EntityStatus.UP |
453
|
|
|
): |
454
|
1 |
|
return True |
455
|
|
|
return False |
456
|
|
|
|
457
|
|
|
def deploy_to_backup_path(self): |
458
|
|
|
"""Deploy the backup path into the datapaths of this circuit. |
459
|
|
|
|
460
|
|
|
If the backup_path attribute is valid and up, this method will try to |
461
|
|
|
deploy this backup_path. |
462
|
|
|
|
463
|
|
|
If everything fails and dynamic_backup_path is True, then tries to |
464
|
1 |
|
deploy a dynamic path. |
465
|
|
|
""" |
466
|
|
|
# TODO: Remove flows from current (cookies) |
467
|
|
|
if self.is_using_backup_path(): |
468
|
1 |
|
# TODO: Log to say that cannot move backup to backup |
469
|
1 |
|
return True |
470
|
1 |
|
|
471
|
|
|
success = False |
472
|
1 |
|
if self.backup_path.status is EntityStatus.UP: |
473
|
1 |
|
success = self.deploy_to_path(self.backup_path) |
474
|
|
|
|
475
|
1 |
|
if success: |
476
|
1 |
|
return True |
477
|
|
|
|
478
|
|
|
if self.dynamic_backup_path or self.is_intra_switch(): |
479
|
|
|
return self.deploy_to_path() |
480
|
1 |
|
|
481
|
|
|
return False |
482
|
|
|
|
483
|
|
|
def deploy_to_primary_path(self): |
484
|
|
|
"""Deploy the primary path into the datapaths of this circuit. |
485
|
|
|
|
486
|
|
|
If the primary_path attribute is valid and up, this method will try to |
487
|
1 |
|
deploy this primary_path. |
488
|
|
|
""" |
489
|
|
|
# TODO: Remove flows from current (cookies) |
490
|
|
|
if self.is_using_primary_path(): |
491
|
1 |
|
# TODO: Log to say that cannot move primary to primary |
492
|
1 |
|
return True |
493
|
|
|
|
494
|
|
|
if self.primary_path.status is EntityStatus.UP: |
495
|
1 |
|
return self.deploy_to_path(self.primary_path) |
496
|
|
|
return False |
497
|
|
|
|
498
|
|
|
def deploy(self): |
499
|
|
|
"""Deploy EVC to best path. |
500
|
|
|
|
501
|
1 |
|
Best path can be the primary path, if available. If not, the backup |
502
|
1 |
|
path, and, if it is also not available, a dynamic path. |
503
|
1 |
|
""" |
504
|
1 |
|
if self.archived: |
505
|
1 |
|
return False |
506
|
1 |
|
self.enable() |
507
|
|
|
success = self.deploy_to_primary_path() |
508
|
1 |
|
if not success: |
509
|
1 |
|
success = self.deploy_to_backup_path() |
510
|
|
|
|
511
|
1 |
|
if success: |
512
|
|
|
emit_event(self._controller, "deployed", |
513
|
1 |
|
content=map_evc_event_content(self)) |
514
|
1 |
|
return success |
515
|
|
|
|
516
|
|
|
@staticmethod |
517
|
|
|
def get_path_status(path): |
518
|
|
|
"""Check for the current status of a path. |
519
|
1 |
|
|
520
|
1 |
|
If any link in this path is down, the path is considered down. |
521
|
|
|
""" |
522
|
1 |
|
if not path: |
523
|
1 |
|
return EntityStatus.DISABLED |
524
|
1 |
|
|
525
|
1 |
|
for link in path: |
526
|
|
|
if link.status is not EntityStatus.UP: |
527
|
|
|
return link.status |
528
|
|
|
return EntityStatus.UP |
529
|
|
|
|
530
|
1 |
|
# def discover_new_path(self): |
531
|
|
|
# # TODO: discover a new path to satisfy this circuit and deploy |
532
|
1 |
|
|
533
|
1 |
|
def remove(self): |
534
|
1 |
|
"""Remove EVC path and disable it.""" |
535
|
1 |
|
self.remove_current_flows() |
536
|
1 |
|
self.remove_failover_flows() |
537
|
|
|
self.disable() |
538
|
|
|
self.sync() |
539
|
1 |
|
emit_event(self._controller, "undeployed", |
540
|
|
|
content=map_evc_event_content(self)) |
541
|
|
|
|
542
|
|
|
def remove_failover_flows(self, exclude_uni_switches=True, |
543
|
|
|
force=True, sync=True) -> None: |
544
|
|
|
"""Remove failover_flows. |
545
|
|
|
|
546
|
|
|
By default, it'll exclude UNI switches, if mef_eline has already |
547
|
1 |
|
called remove_current_flows before then this minimizes the number |
548
|
1 |
|
of FlowMods and IO. |
549
|
1 |
|
""" |
550
|
1 |
|
if not self.failover_path: |
551
|
1 |
|
return |
552
|
1 |
|
switches, cookie, excluded = OrderedDict(), self.get_cookie(), set() |
553
|
1 |
|
links = set() |
554
|
1 |
|
if exclude_uni_switches: |
555
|
1 |
|
excluded.add(self.uni_a.interface.switch.id) |
556
|
1 |
|
excluded.add(self.uni_z.interface.switch.id) |
557
|
1 |
|
for link in self.failover_path: |
558
|
1 |
|
if link.endpoint_a.switch.id not in excluded: |
559
|
1 |
|
switches[link.endpoint_a.switch.id] = link.endpoint_a.switch |
560
|
1 |
|
links.add(link) |
561
|
1 |
|
if link.endpoint_b.switch.id not in excluded: |
562
|
1 |
|
switches[link.endpoint_b.switch.id] = link.endpoint_b.switch |
563
|
1 |
|
links.add(link) |
564
|
|
|
for switch in switches.values(): |
565
|
|
|
try: |
566
|
|
|
self._send_flow_mods( |
567
|
|
|
switch.id, |
568
|
|
|
[ |
569
|
|
|
{ |
570
|
|
|
"cookie": cookie, |
571
|
|
|
"cookie_mask": int(0xffffffffffffffff), |
572
|
|
|
} |
573
|
|
|
], |
574
|
|
|
"delete", |
575
|
|
|
force=force, |
576
|
|
|
) |
577
|
|
|
except FlowModException as err: |
578
|
|
|
log.error( |
579
|
1 |
|
f"Error removing flows from switch {switch.id} for" |
580
|
1 |
|
f"EVC {self}: {err}" |
581
|
1 |
|
) |
582
|
1 |
|
for link in links: |
583
|
1 |
|
link.make_tag_available(link.get_metadata("s_vlan")) |
584
|
1 |
|
link.remove_metadata("s_vlan") |
585
|
1 |
|
notify_link_available_tags(self._controller, link) |
586
|
|
|
self.failover_path = Path([]) |
587
|
1 |
|
if sync: |
588
|
|
|
self.sync() |
589
|
1 |
|
|
590
|
|
|
def remove_current_flows(self, current_path=None, force=True): |
591
|
1 |
|
"""Remove all flows from current path.""" |
592
|
1 |
|
switches = set() |
593
|
1 |
|
|
594
|
1 |
|
switches.add(self.uni_a.interface.switch) |
595
|
1 |
|
switches.add(self.uni_z.interface.switch) |
596
|
1 |
|
if not current_path: |
597
|
1 |
|
current_path = self.current_path |
598
|
|
|
for link in current_path: |
599
|
1 |
|
switches.add(link.endpoint_a.switch) |
600
|
|
|
switches.add(link.endpoint_b.switch) |
601
|
|
|
|
602
|
|
|
match = { |
603
|
|
|
"cookie": self.get_cookie(), |
604
|
1 |
|
"cookie_mask": int(0xffffffffffffffff) |
605
|
1 |
|
} |
606
|
1 |
|
|
607
|
1 |
|
for switch in switches: |
608
|
1 |
|
try: |
609
|
|
|
self._send_flow_mods(switch.id, [match], 'delete', force=force) |
610
|
|
|
except FlowModException as err: |
611
|
|
|
log.error( |
612
|
|
|
f"Error removing flows from switch {switch.id} for" |
613
|
1 |
|
f"EVC {self}: {err}" |
614
|
1 |
|
) |
615
|
1 |
|
|
616
|
1 |
|
current_path.make_vlans_available() |
617
|
1 |
|
for link in current_path: |
618
|
1 |
|
notify_link_available_tags(self._controller, link) |
619
|
|
|
self.current_path = Path([]) |
620
|
1 |
|
self.deactivate() |
621
|
|
|
self.sync() |
622
|
1 |
|
|
623
|
1 |
|
def remove_path_flows(self, path=None, force=True): |
624
|
|
|
"""Remove all flows from path.""" |
625
|
1 |
|
if not path: |
626
|
1 |
|
return |
627
|
1 |
|
|
628
|
1 |
|
dpid_flows_match = {} |
629
|
1 |
|
for dpid, flows in self._prepare_nni_flows(path).items(): |
630
|
|
|
dpid_flows_match.setdefault(dpid, []) |
631
|
|
|
for flow in flows: |
632
|
|
|
dpid_flows_match[dpid].append({ |
633
|
|
|
"cookie": flow["cookie"], |
634
|
1 |
|
"match": flow["match"], |
635
|
1 |
|
"cookie_mask": int(0xffffffffffffffff) |
636
|
1 |
|
}) |
637
|
1 |
|
for dpid, flows in self._prepare_uni_flows(path, skip_in=True).items(): |
638
|
|
|
dpid_flows_match.setdefault(dpid, []) |
639
|
|
|
for flow in flows: |
640
|
|
|
dpid_flows_match[dpid].append({ |
641
|
|
|
"cookie": flow["cookie"], |
642
|
|
|
"match": flow["match"], |
643
|
1 |
|
"cookie_mask": int(0xffffffffffffffff) |
644
|
1 |
|
}) |
645
|
1 |
|
|
646
|
1 |
|
for dpid, flows in dpid_flows_match.items(): |
647
|
1 |
|
try: |
648
|
|
|
self._send_flow_mods(dpid, flows, 'delete', force=force) |
649
|
|
|
except FlowModException as err: |
650
|
|
|
log.error( |
651
|
|
|
"Error removing failover flows: " |
652
|
1 |
|
f"dpid={dpid} evc={self} error={err}" |
653
|
1 |
|
) |
654
|
1 |
|
|
655
|
|
|
path.make_vlans_available() |
656
|
1 |
|
for link in path: |
657
|
1 |
|
notify_link_available_tags(self._controller, link) |
658
|
|
|
|
659
|
1 |
|
@staticmethod |
660
|
|
|
def links_zipped(path=None): |
661
|
1 |
|
"""Return an iterator which yields pairs of links in order.""" |
662
|
|
|
if not path: |
663
|
1 |
|
return [] |
664
|
|
|
return zip(path[:-1], path[1:]) |
665
|
1 |
|
|
666
|
1 |
|
def should_deploy(self, path=None): |
667
|
1 |
|
"""Verify if the circuit should be deployed.""" |
668
|
|
|
if not path: |
669
|
1 |
|
log.debug("Path is empty.") |
670
|
1 |
|
return False |
671
|
1 |
|
|
672
|
|
|
if not self.is_enabled(): |
673
|
1 |
|
log.debug(f"{self} is disabled.") |
674
|
1 |
|
return False |
675
|
1 |
|
|
676
|
|
|
if not self.is_active(): |
677
|
1 |
|
log.debug(f"{self} will be deployed.") |
678
|
|
|
return True |
679
|
1 |
|
|
680
|
|
|
return False |
681
|
|
|
|
682
|
|
|
def deploy_to_path(self, path=None): # pylint: disable=too-many-branches |
683
|
|
|
"""Install the flows for this circuit. |
684
|
|
|
|
685
|
|
|
Procedures to deploy: |
686
|
|
|
|
687
|
|
|
0. Remove current flows installed |
688
|
|
|
1. Decide if will deploy "path" or discover a new path |
689
|
|
|
2. Choose vlan |
690
|
|
|
3. Install NNI flows |
691
|
|
|
4. Install UNI flows |
692
|
|
|
5. Activate |
693
|
|
|
6. Update current_path |
694
|
1 |
|
7. Update links caches(primary, current, backup) |
695
|
1 |
|
|
696
|
1 |
|
""" |
697
|
1 |
|
self.remove_current_flows() |
698
|
1 |
|
use_path = path |
699
|
1 |
|
if self.should_deploy(use_path): |
700
|
1 |
|
try: |
701
|
1 |
|
use_path.choose_vlans() |
702
|
1 |
|
for link in use_path: |
703
|
|
|
notify_link_available_tags(self._controller, link) |
704
|
1 |
|
except KytosNoTagAvailableError: |
705
|
1 |
|
use_path = None |
706
|
|
|
else: |
707
|
1 |
|
for use_path in self.discover_new_paths(): |
708
|
1 |
|
if use_path is None: |
709
|
1 |
|
continue |
710
|
1 |
|
try: |
711
|
1 |
|
use_path.choose_vlans() |
712
|
1 |
|
for link in use_path: |
713
|
1 |
|
notify_link_available_tags(self._controller, link) |
714
|
|
|
break |
715
|
1 |
|
except KytosNoTagAvailableError: |
716
|
|
|
pass |
717
|
1 |
|
else: |
718
|
1 |
|
use_path = None |
719
|
1 |
|
|
720
|
1 |
|
try: |
721
|
1 |
|
if use_path: |
722
|
1 |
|
self._install_nni_flows(use_path) |
723
|
1 |
|
self._install_uni_flows(use_path) |
724
|
|
|
elif self.is_intra_switch(): |
725
|
1 |
|
use_path = Path() |
726
|
|
|
self._install_direct_uni_flows() |
727
|
|
|
else: |
728
|
1 |
|
log.warning( |
729
|
1 |
|
f"{self} was not deployed. " "No available path was found." |
730
|
1 |
|
) |
731
|
|
|
return False |
732
|
|
|
except FlowModException as err: |
733
|
1 |
|
log.error( |
734
|
1 |
|
f"Error deploying EVC {self} when calling flow_manager: {err}" |
735
|
1 |
|
) |
736
|
1 |
|
self.remove_current_flows(use_path) |
737
|
1 |
|
return False |
738
|
1 |
|
self.activate() |
739
|
1 |
|
self.current_path = use_path |
740
|
|
|
self.sync() |
741
|
1 |
|
log.info(f"{self} was deployed.") |
742
|
|
|
return True |
743
|
|
|
|
744
|
|
|
def setup_failover_path(self): |
745
|
|
|
"""Install flows for the failover path of this EVC. |
746
|
|
|
|
747
|
|
|
Procedures to deploy: |
748
|
|
|
|
749
|
|
|
0. Remove flows currently installed for failover_path (if any) |
750
|
|
|
1. Discover a disjoint path from current_path |
751
|
|
|
2. Choose vlans |
752
|
|
|
3. Install NNI flows |
753
|
|
|
4. Install UNI egress flows |
754
|
1 |
|
5. Update failover_path |
755
|
1 |
|
""" |
756
|
|
|
# Intra-switch EVCs have no failover_path |
757
|
|
|
if self.is_intra_switch(): |
758
|
1 |
|
return False |
759
|
1 |
|
|
760
|
|
|
# For not only setup failover path for totally dynamic EVCs |
761
|
1 |
|
if not self.is_eligible_for_failover_path(): |
762
|
1 |
|
return False |
763
|
1 |
|
|
764
|
1 |
|
reason = "" |
765
|
1 |
|
self.remove_path_flows(self.failover_path) |
766
|
1 |
|
for use_path in self.get_failover_path_candidates(): |
767
|
1 |
|
if not use_path: |
768
|
1 |
|
continue |
769
|
1 |
|
try: |
770
|
1 |
|
use_path.choose_vlans() |
771
|
1 |
|
for link in use_path: |
772
|
1 |
|
notify_link_available_tags(self._controller, link) |
773
|
|
|
break |
774
|
1 |
|
except KytosNoTagAvailableError: |
775
|
1 |
|
pass |
776
|
|
|
else: |
777
|
1 |
|
use_path = Path([]) |
778
|
1 |
|
reason = "No available path was found" |
779
|
1 |
|
|
780
|
1 |
|
try: |
781
|
1 |
|
if use_path: |
782
|
1 |
|
self._install_nni_flows(use_path) |
783
|
1 |
|
self._install_uni_flows(use_path, skip_in=True) |
784
|
|
|
except FlowModException as err: |
785
|
|
|
reason = "Error deploying failover path" |
786
|
1 |
|
log.error( |
787
|
1 |
|
f"{reason} for {self}. FlowManager error: {err}" |
788
|
|
|
) |
789
|
1 |
|
self.remove_path_flows(use_path) |
790
|
1 |
|
use_path = Path([]) |
791
|
|
|
|
792
|
1 |
|
self.failover_path = use_path |
793
|
1 |
|
self.sync() |
794
|
|
|
|
795
|
|
|
if not use_path: |
796
|
1 |
|
log.warning( |
797
|
1 |
|
f"Failover path for {self} was not deployed: {reason}" |
798
|
1 |
|
) |
799
|
|
|
return False |
800
|
1 |
|
log.info(f"Failover path for {self} was deployed.") |
801
|
|
|
return True |
802
|
|
|
|
803
|
|
|
def get_failover_flows(self): |
804
|
|
|
"""Return the flows needed to make the failover path active, i.e. the |
805
|
|
|
flows for ingress forwarding. |
806
|
|
|
|
807
|
|
|
Return: |
808
|
1 |
|
dict: A dict of flows indexed by the switch_id will be returned, or |
809
|
1 |
|
an empty dict if no failover_path is available. |
810
|
1 |
|
""" |
811
|
|
|
if not self.failover_path: |
812
|
1 |
|
return {} |
813
|
|
|
return self._prepare_uni_flows(self.failover_path, skip_out=True) |
814
|
1 |
|
|
815
|
1 |
|
def _prepare_direct_uni_flows(self): |
816
|
|
|
"""Prepare flows connecting two UNIs for intra-switch EVC.""" |
817
|
1 |
|
vlan_a = self.uni_a.user_tag.value if self.uni_a.user_tag else None |
818
|
1 |
|
vlan_z = self.uni_z.user_tag.value if self.uni_z.user_tag else None |
819
|
|
|
|
820
|
|
|
is_EVPL = (vlan_a is not None) |
821
|
|
|
flow_mod_az = self._prepare_flow_mod( |
822
|
1 |
|
self.uni_a.interface, self.uni_z.interface, |
823
|
1 |
|
self.queue_id, is_EVPL |
824
|
|
|
) |
825
|
|
|
is_EVPL = (vlan_z is not None) |
826
|
|
|
flow_mod_za = self._prepare_flow_mod( |
827
|
|
|
self.uni_z.interface, self.uni_a.interface, |
828
|
1 |
|
self.queue_id, is_EVPL |
829
|
1 |
|
) |
830
|
1 |
|
|
831
|
1 |
|
if vlan_a and vlan_z: |
832
|
|
|
flow_mod_az["match"]["dl_vlan"] = vlan_a |
833
|
|
|
flow_mod_za["match"]["dl_vlan"] = vlan_z |
834
|
1 |
|
if vlan_z != "any": |
835
|
|
|
flow_mod_az["actions"].insert( |
836
|
|
|
0, {"action_type": "set_vlan", "vlan_id": vlan_z} |
837
|
1 |
|
) |
838
|
1 |
|
if vlan_a != "any": |
839
|
1 |
|
flow_mod_za["actions"].insert( |
840
|
1 |
|
0, {"action_type": "set_vlan", "vlan_id": vlan_a} |
841
|
|
|
) |
842
|
|
|
elif vlan_a: |
843
|
1 |
|
flow_mod_az["match"]["dl_vlan"] = vlan_a |
844
|
1 |
|
flow_mod_az["actions"].insert(0, {"action_type": "pop_vlan"}) |
845
|
1 |
|
if vlan_a != "any": |
846
|
1 |
|
flow_mod_za["actions"].insert( |
847
|
|
|
0, {"action_type": "set_vlan", "vlan_id": vlan_a} |
848
|
|
|
) |
849
|
1 |
|
elif vlan_z: |
850
|
|
|
flow_mod_za["match"]["dl_vlan"] = vlan_z |
851
|
|
|
flow_mod_za["actions"].insert(0, {"action_type": "pop_vlan"}) |
852
|
|
|
if vlan_z != "any": |
853
|
1 |
|
flow_mod_az["actions"].insert( |
854
|
|
|
0, {"action_type": "set_vlan", "vlan_id": vlan_z} |
855
|
|
|
) |
856
|
|
|
return ( |
857
|
|
|
self.uni_a.interface.switch.id, [flow_mod_az, flow_mod_za] |
858
|
|
|
) |
859
|
1 |
|
|
860
|
1 |
|
def _install_direct_uni_flows(self): |
861
|
|
|
"""Install flows connecting two UNIs. |
862
|
1 |
|
|
863
|
|
|
This case happens when the circuit is between UNIs in the |
864
|
1 |
|
same switch. |
865
|
1 |
|
""" |
866
|
1 |
|
(dpid, flows) = self._prepare_direct_uni_flows() |
867
|
1 |
|
self._send_flow_mods(dpid, flows) |
868
|
|
|
|
869
|
1 |
|
def _prepare_nni_flows(self, path=None): |
870
|
|
|
"""Prepare NNI flows.""" |
871
|
1 |
|
nni_flows = OrderedDict() |
872
|
|
|
for incoming, outcoming in self.links_zipped(path): |
873
|
|
|
in_vlan = incoming.get_metadata("s_vlan").value |
874
|
|
|
out_vlan = outcoming.get_metadata("s_vlan").value |
875
|
|
|
|
876
|
|
|
flows = [] |
877
|
|
|
# Flow for one direction |
878
|
|
|
flows.append( |
879
|
|
|
self._prepare_nni_flow( |
880
|
|
|
incoming.endpoint_b, |
881
|
|
|
outcoming.endpoint_a, |
882
|
1 |
|
in_vlan, |
883
|
|
|
out_vlan, |
884
|
|
|
queue_id=self.queue_id, |
885
|
|
|
) |
886
|
|
|
) |
887
|
|
|
|
888
|
|
|
# Flow for the other direction |
889
|
|
|
flows.append( |
890
|
|
|
self._prepare_nni_flow( |
891
|
1 |
|
outcoming.endpoint_a, |
892
|
1 |
|
incoming.endpoint_b, |
893
|
|
|
out_vlan, |
894
|
1 |
|
in_vlan, |
895
|
|
|
queue_id=self.queue_id, |
896
|
1 |
|
) |
897
|
1 |
|
) |
898
|
|
|
nni_flows[incoming.endpoint_b.switch.id] = flows |
899
|
1 |
|
return nni_flows |
900
|
|
|
|
901
|
1 |
|
def _install_nni_flows(self, path=None): |
902
|
1 |
|
"""Install NNI flows.""" |
903
|
1 |
|
for dpid, flows in self._prepare_nni_flows(path).items(): |
904
|
1 |
|
self._send_flow_mods(dpid, flows) |
905
|
|
|
|
906
|
|
|
def _prepare_uni_flows(self, path=None, skip_in=False, skip_out=False): |
907
|
1 |
|
"""Prepare flows to install UNIs.""" |
908
|
1 |
|
uni_flows = {} |
909
|
|
|
if not path: |
910
|
1 |
|
log.info("install uni flows without path.") |
911
|
1 |
|
return uni_flows |
912
|
|
|
|
913
|
|
|
# Determine VLANs |
914
|
1 |
|
in_vlan_a = self.uni_a.user_tag.value if self.uni_a.user_tag else None |
915
|
|
|
out_vlan_a = path[0].get_metadata("s_vlan").value |
916
|
|
|
|
917
|
1 |
|
in_vlan_z = self.uni_z.user_tag.value if self.uni_z.user_tag else None |
918
|
1 |
|
out_vlan_z = path[-1].get_metadata("s_vlan").value |
919
|
|
|
|
920
|
|
|
# Flows for the first UNI |
921
|
|
|
flows_a = [] |
922
|
|
|
|
923
|
|
|
# Flow for one direction, pushing the service tag |
924
|
|
|
if not skip_in: |
925
|
|
|
push_flow = self._prepare_push_flow( |
926
|
1 |
|
self.uni_a.interface, |
927
|
|
|
path[0].endpoint_a, |
928
|
|
|
in_vlan_a, |
929
|
1 |
|
out_vlan_a, |
930
|
1 |
|
in_vlan_z, |
931
|
|
|
queue_id=self.queue_id, |
932
|
|
|
) |
933
|
|
|
flows_a.append(push_flow) |
934
|
|
|
|
935
|
|
|
# Flow for the other direction, popping the service tag |
936
|
1 |
|
if not skip_out: |
937
|
|
|
pop_flow = self._prepare_pop_flow( |
938
|
1 |
|
path[0].endpoint_a, |
939
|
|
|
self.uni_a.interface, |
940
|
|
|
out_vlan_a, |
941
|
1 |
|
queue_id=self.queue_id, |
942
|
|
|
) |
943
|
|
|
flows_a.append(pop_flow) |
944
|
1 |
|
|
945
|
1 |
|
uni_flows[self.uni_a.interface.switch.id] = flows_a |
946
|
|
|
|
947
|
|
|
# Flows for the second UNI |
948
|
|
|
flows_z = [] |
949
|
|
|
|
950
|
|
|
# Flow for one direction, pushing the service tag |
951
|
|
|
if not skip_in: |
952
|
|
|
push_flow = self._prepare_push_flow( |
953
|
1 |
|
self.uni_z.interface, |
954
|
|
|
path[-1].endpoint_b, |
955
|
|
|
in_vlan_z, |
956
|
1 |
|
out_vlan_z, |
957
|
1 |
|
in_vlan_a, |
958
|
|
|
queue_id=self.queue_id, |
959
|
|
|
) |
960
|
|
|
flows_z.append(push_flow) |
961
|
|
|
|
962
|
|
|
# Flow for the other direction, popping the service tag |
963
|
1 |
|
if not skip_out: |
964
|
|
|
pop_flow = self._prepare_pop_flow( |
965
|
1 |
|
path[-1].endpoint_b, |
966
|
|
|
self.uni_z.interface, |
967
|
1 |
|
out_vlan_z, |
968
|
|
|
queue_id=self.queue_id, |
969
|
1 |
|
) |
970
|
|
|
flows_z.append(pop_flow) |
971
|
1 |
|
|
972
|
|
|
uni_flows[self.uni_z.interface.switch.id] = flows_z |
973
|
1 |
|
|
974
|
1 |
|
return uni_flows |
975
|
|
|
|
976
|
1 |
|
def _install_uni_flows(self, path=None, skip_in=False, skip_out=False): |
977
|
1 |
|
"""Install UNI flows.""" |
978
|
|
|
uni_flows = self._prepare_uni_flows(path, skip_in, skip_out) |
979
|
|
|
|
980
|
|
|
for (dpid, flows) in uni_flows.items(): |
981
|
|
|
self._send_flow_mods(dpid, flows) |
982
|
|
|
|
983
|
|
|
@staticmethod |
984
|
|
|
def _send_flow_mods(dpid, flow_mods, command='flows', force=False): |
985
|
|
|
"""Send a flow_mod list to a specific switch. |
986
|
|
|
|
987
|
|
|
Args: |
988
|
1 |
|
dpid(str): The target of flows (i.e. Switch.id). |
989
|
|
|
flow_mods(dict): Python dictionary with flow_mods. |
990
|
1 |
|
command(str): By default is 'flows'. To remove a flow is 'remove'. |
991
|
1 |
|
force(bool): True to send via consistency check in case of errors |
992
|
1 |
|
|
993
|
1 |
|
""" |
994
|
|
|
|
995
|
1 |
|
endpoint = f"{settings.MANAGER_URL}/{command}/{dpid}" |
996
|
|
|
|
997
|
1 |
|
data = {"flows": flow_mods, "force": force} |
998
|
|
|
response = requests.post(endpoint, json=data) |
999
|
1 |
|
if response.status_code >= 400: |
1000
|
1 |
|
raise FlowModException(str(response.text)) |
1001
|
|
|
|
1002
|
1 |
|
def get_cookie(self): |
1003
|
1 |
|
"""Return the cookie integer from evc id.""" |
1004
|
|
|
return int(self.id, 16) + (settings.COOKIE_PREFIX << 56) |
1005
|
1 |
|
|
1006
|
|
|
@staticmethod |
1007
|
|
|
def get_id_from_cookie(cookie): |
1008
|
1 |
|
"""Return the evc id given a cookie value.""" |
1009
|
|
|
evc_id = cookie - (settings.COOKIE_PREFIX << 56) |
1010
|
|
|
return f"{evc_id:x}".zfill(14) |
1011
|
1 |
|
|
1012
|
|
|
def _prepare_flow_mod(self, in_interface, out_interface, |
1013
|
|
|
queue_id=None, is_EVPL=True): |
1014
|
|
|
"""Prepare a common flow mod.""" |
1015
|
|
|
default_actions = [ |
1016
|
1 |
|
{"action_type": "output", "port": out_interface.port_number} |
1017
|
|
|
] |
1018
|
|
|
if queue_id is not None: |
1019
|
|
|
default_actions.append( |
1020
|
|
|
{"action_type": "set_queue", "queue_id": queue_id} |
1021
|
1 |
|
) |
1022
|
|
|
|
1023
|
|
|
flow_mod = { |
1024
|
1 |
|
"match": {"in_port": in_interface.port_number}, |
1025
|
1 |
|
"cookie": self.get_cookie(), |
1026
|
|
|
"actions": default_actions, |
1027
|
1 |
|
} |
1028
|
1 |
|
if self.sb_priority: |
1029
|
|
|
flow_mod["priority"] = self.sb_priority |
1030
|
1 |
|
else: |
1031
|
|
|
if is_EVPL: |
1032
|
1 |
|
flow_mod["priority"] = settings.EVPL_SB_PRIORITY |
1033
|
1 |
|
else: |
1034
|
|
|
flow_mod["priority"] = settings.EPL_SB_PRIORITY |
1035
|
|
|
return flow_mod |
1036
|
1 |
|
|
1037
|
|
|
def _prepare_nni_flow(self, *args, queue_id=None): |
1038
|
1 |
|
"""Create NNI flows.""" |
1039
|
1 |
|
in_interface, out_interface, in_vlan, out_vlan = args |
1040
|
|
|
flow_mod = self._prepare_flow_mod( |
1041
|
1 |
|
in_interface, out_interface, queue_id |
1042
|
|
|
) |
1043
|
|
|
flow_mod["match"]["dl_vlan"] = in_vlan |
1044
|
1 |
|
if out_vlan != "any": |
1045
|
|
|
new_action = {"action_type": "set_vlan", "vlan_id": out_vlan} |
1046
|
|
|
flow_mod["actions"].insert(0, new_action) |
|
|
|
|
1047
|
|
|
|
1048
|
|
|
return flow_mod |
1049
|
|
|
|
1050
|
|
|
# pylint: disable=too-many-arguments |
1051
|
|
|
def _prepare_push_flow(self, *args, queue_id=None): |
1052
|
|
|
"""Prepare push flow. |
1053
|
|
|
|
1054
|
|
|
Arguments: |
1055
|
|
|
in_interface(str): Interface input. |
1056
|
|
|
out_interface(str): Interface output. |
1057
|
|
|
in_vlan(str): Vlan input. |
1058
|
|
|
out_vlan(str): Vlan output. |
1059
|
1 |
|
new_c_vlan(str): New client vlan. |
1060
|
1 |
|
|
1061
|
1 |
|
Return: |
1062
|
|
|
dict: An python dictionary representing a FlowMod |
1063
|
|
|
|
1064
|
|
|
""" |
1065
|
|
|
# assign all arguments |
1066
|
1 |
|
in_interface, out_interface, in_vlan, out_vlan, new_c_vlan = args |
1067
|
1 |
|
is_EVPL = (in_vlan is not None) |
1068
|
|
|
flow_mod = self._prepare_flow_mod( |
1069
|
1 |
|
in_interface, out_interface, queue_id, is_EVPL |
1070
|
1 |
|
) |
1071
|
|
|
|
1072
|
1 |
|
# the service tag must be always pushed |
1073
|
|
|
if out_vlan != "any": |
1074
|
1 |
|
new_action = {"action_type": "set_vlan", "vlan_id": out_vlan} |
1075
|
1 |
|
flow_mod["actions"].insert(0, new_action) |
1076
|
|
|
|
1077
|
1 |
|
new_action = {"action_type": "push_vlan", "tag_type": "s"} |
1078
|
1 |
|
flow_mod["actions"].insert(0, new_action) |
1079
|
1 |
|
|
1080
|
|
|
if in_vlan: |
1081
|
|
|
# if in_vlan is set, it must be included in the match |
1082
|
1 |
|
flow_mod["match"]["dl_vlan"] = in_vlan |
1083
|
1 |
|
if new_c_vlan: |
1084
|
1 |
|
# new_in_vlan is set, so an action to set it is necessary |
1085
|
|
|
if new_c_vlan != "any": |
1086
|
|
|
new_action = {"action_type": "set_vlan", "vlan_id": new_c_vlan} |
1087
|
1 |
|
flow_mod["actions"].insert(0, new_action) |
1088
|
1 |
|
if not in_vlan: |
1089
|
1 |
|
# new_in_vlan is set, but in_vlan is not, so there was no |
1090
|
|
|
# vlan set; then it is set now |
1091
|
1 |
|
new_action = {"action_type": "push_vlan", "tag_type": "c"} |
1092
|
|
|
flow_mod["actions"].insert(0, new_action) |
1093
|
|
|
elif in_vlan: |
1094
|
|
|
# in_vlan is set, but new_in_vlan is not, so the existing vlan |
1095
|
|
|
# must be removed |
1096
|
1 |
|
new_action = {"action_type": "pop_vlan"} |
1097
|
|
|
flow_mod["actions"].insert(0, new_action) |
1098
|
|
|
return flow_mod |
1099
|
1 |
|
|
1100
|
1 |
|
def _prepare_pop_flow( |
1101
|
1 |
|
self, in_interface, out_interface, out_vlan, queue_id=None |
1102
|
1 |
|
): |
1103
|
|
|
# pylint: disable=too-many-arguments |
1104
|
1 |
|
"""Prepare pop flow.""" |
1105
|
1 |
|
flow_mod = self._prepare_flow_mod( |
1106
|
|
|
in_interface, out_interface, queue_id |
1107
|
1 |
|
) |
1108
|
1 |
|
flow_mod["match"]["dl_vlan"] = out_vlan |
1109
|
|
|
new_action = {"action_type": "pop_vlan"} |
1110
|
|
|
flow_mod["actions"].insert(0, new_action) |
1111
|
|
|
return flow_mod |
1112
|
|
|
|
1113
|
|
|
@staticmethod |
1114
|
|
|
def run_sdntrace(uni): |
1115
|
|
|
"""Run SDN trace on control plane starting from EVC UNIs.""" |
1116
|
1 |
|
endpoint = f"{settings.SDN_TRACE_CP_URL}/trace" |
1117
|
1 |
|
data_uni = { |
1118
|
|
|
"trace": { |
1119
|
|
|
"switch": { |
1120
|
|
|
"dpid": uni.interface.switch.dpid, |
1121
|
1 |
|
"in_port": uni.interface.port_number, |
1122
|
1 |
|
} |
1123
|
1 |
|
} |
1124
|
1 |
|
} |
1125
|
1 |
|
if uni.user_tag: |
1126
|
|
|
data_uni["trace"]["eth"] = { |
1127
|
1 |
|
"dl_type": 0x8100, |
1128
|
1 |
|
"dl_vlan": uni.user_tag.value, |
1129
|
|
|
} |
1130
|
1 |
|
response = requests.put(endpoint, json=data_uni) |
1131
|
1 |
|
if response.status_code >= 400: |
1132
|
1 |
|
log.error(f"Failed to run sdntrace-cp: {response.text}") |
1133
|
1 |
|
return [] |
1134
|
|
|
return response.json().get('result', []) |
1135
|
|
|
|
1136
|
|
|
@staticmethod |
1137
|
|
|
def run_bulk_sdntraces(uni_list): |
1138
|
|
|
"""Run SDN traces on control plane starting from EVC UNIs.""" |
1139
|
|
|
endpoint = f"{settings.SDN_TRACE_CP_URL}/traces" |
1140
|
|
|
data = [] |
1141
|
1 |
|
for uni in uni_list: |
1142
|
1 |
|
data_uni = { |
1143
|
|
|
"trace": { |
1144
|
|
|
"switch": { |
1145
|
|
|
"dpid": uni.interface.switch.dpid, |
1146
|
1 |
|
"in_port": uni.interface.port_number, |
1147
|
1 |
|
} |
1148
|
1 |
|
} |
1149
|
|
|
} |
1150
|
|
|
if uni.user_tag: |
1151
|
|
|
data_uni["trace"]["eth"] = { |
1152
|
1 |
|
"dl_type": 0x8100, |
1153
|
1 |
|
"dl_vlan": uni.user_tag.value, |
1154
|
1 |
|
} |
1155
|
1 |
|
data.append(data_uni) |
1156
|
|
|
try: |
1157
|
1 |
|
response = requests.put(endpoint, json=data, timeout=30) |
1158
|
1 |
|
except ConnectTimeout as exception: |
1159
|
|
|
log.error(f"Request has timed out: {exception}") |
1160
|
1 |
|
|
1161
|
1 |
|
if response.status_code >= 400: |
1162
|
1 |
|
log.error(f"Failed to run sdntrace-cp: {response.text}") |
1163
|
|
|
return [] |
1164
|
|
|
return response.json() |
1165
|
|
|
|
1166
|
1 |
|
@staticmethod |
1167
|
1 |
|
def check_trace(circuit, circuit_by_traces): |
1168
|
1 |
|
"""Auxiliar function to check an individual trace""" |
1169
|
|
|
trace_a = circuit_by_traces[circuit.id]['trace_a'] |
1170
|
|
|
trace_z = circuit_by_traces[circuit.id]['trace_z'] |
1171
|
|
|
if ( |
1172
|
1 |
|
len(trace_a) != len(circuit.current_path) + 1 |
1173
|
1 |
|
or not compare_uni_out_trace(circuit.uni_z, trace_a[-1]) |
1174
|
|
|
): |
1175
|
1 |
|
log.warning(f"Invalid trace from uni_a: {trace_a}") |
1176
|
|
|
return False |
1177
|
|
|
if ( |
1178
|
1 |
|
len(trace_z) != len(circuit.current_path) + 1 |
1179
|
1 |
|
or not compare_uni_out_trace(circuit.uni_a, trace_z[-1]) |
1180
|
1 |
|
): |
1181
|
1 |
|
log.warning(f"Invalid trace from uni_z: {trace_z}") |
1182
|
|
|
return False |
1183
|
|
|
|
1184
|
|
|
for link, trace1, trace2 in zip(circuit.current_path, |
1185
|
|
|
trace_a[1:], |
1186
|
1 |
|
trace_z[:0:-1]): |
1187
|
1 |
|
metadata_vlan = None |
1188
|
1 |
|
if link.metadata: |
1189
|
|
|
metadata_vlan = glom(link.metadata, 's_vlan.value') |
1190
|
|
|
if compare_endpoint_trace( |
1191
|
|
|
link.endpoint_a, |
1192
|
|
|
metadata_vlan, |
1193
|
1 |
|
trace2 |
1194
|
1 |
|
) is False: |
1195
|
|
|
log.warning(f"Invalid trace from uni_a: {trace_a}") |
1196
|
1 |
|
return False |
1197
|
|
|
if compare_endpoint_trace( |
1198
|
1 |
|
link.endpoint_b, |
1199
|
1 |
|
metadata_vlan, |
1200
|
|
|
trace1 |
1201
|
1 |
|
) is False: |
1202
|
|
|
log.warning(f"Invalid trace from uni_z: {trace_z}") |
1203
|
1 |
|
return False |
1204
|
1 |
|
|
1205
|
1 |
|
return True |
1206
|
1 |
|
|
1207
|
|
|
@staticmethod |
1208
|
|
|
def check_list_traces(list_circuits): |
1209
|
1 |
|
"""Check if current_path is deployed comparing with SDN traces.""" |
1210
|
|
|
if not list_circuits: |
1211
|
1 |
|
return {} |
1212
|
1 |
|
uni_list = [] |
1213
|
1 |
|
circuit_data = {} |
1214
|
1 |
|
for circuit_id in list_circuits: |
1215
|
|
|
circuit = list_circuits[circuit_id] |
1216
|
|
|
# if a inter-switch EVC does not have current_path, it does not |
1217
|
|
|
# make sense to run sdntrace on it |
1218
|
1 |
|
if not circuit.is_intra_switch() and not circuit.current_path: |
1219
|
1 |
|
continue |
1220
|
|
|
uni_list.append(circuit.uni_a) |
1221
|
|
|
uni_list.append(circuit.uni_z) |
1222
|
|
|
interface_a = uni_to_str(circuit.uni_a) |
1223
|
|
|
circuit_data[interface_a] = { |
1224
|
1 |
|
'circuit_id': circuit.id, |
1225
|
|
|
'trace_name': 'trace_a' |
1226
|
1 |
|
} |
1227
|
1 |
|
interface_z = uni_to_str(circuit.uni_z) |
1228
|
|
|
circuit_data[interface_z] = { |
1229
|
1 |
|
'circuit_id': circuit.id, |
1230
|
1 |
|
'trace_name': 'trace_z' |
1231
|
1 |
|
} |
1232
|
1 |
|
|
1233
|
1 |
|
traces = EVCDeploy.run_bulk_sdntraces(uni_list) |
1234
|
1 |
|
|
1235
|
1 |
|
circuit_by_traces = {} |
1236
|
|
|
circuits_checked = {} |
1237
|
1 |
|
|
1238
|
1 |
|
for trace_switch in traces: |
1239
|
1 |
|
for trace in traces[trace_switch]: |
1240
|
1 |
|
id_trace = str(trace[0]['dpid']) + ':' + str(trace[0]['port']) |
1241
|
1 |
|
if 'vlan' in trace[0]: |
1242
|
1 |
|
id_trace += ':' + str(trace[0]['vlan']) |
1243
|
|
|
circuit_from_data = circuit_data.get(id_trace) |
1244
|
1 |
|
if circuit_from_data is None: |
1245
|
|
|
continue |
1246
|
1 |
|
circuit_id = circuit_from_data['circuit_id'] |
1247
|
|
|
trace_name = circuit_from_data['trace_name'] |
1248
|
|
|
circuit = list_circuits[circuit_id] |
1249
|
|
|
if circuit_id not in circuit_by_traces: |
1250
|
1 |
|
circuit_by_traces[circuit_id] = {} |
1251
|
|
|
circuit_by_traces[circuit_id][trace_name] = trace |
1252
|
|
|
|
1253
|
1 |
|
if 'trace_a' in circuit_by_traces[circuit_id] \ |
1254
|
|
|
and 'trace_z' in circuit_by_traces[circuit_id]: |
1255
|
|
|
circuits_checked[circuit_id] = EVCDeploy.check_trace( |
1256
|
1 |
|
circuit, circuit_by_traces |
1257
|
|
|
) |
1258
|
|
|
|
1259
|
|
|
return circuits_checked |
1260
|
1 |
|
|
1261
|
|
|
|
1262
|
1 |
|
class LinkProtection(EVCDeploy): |
1263
|
|
|
"""Class to handle link protection.""" |
1264
|
1 |
|
|
1265
|
|
|
def is_affected_by_link(self, link=None): |
1266
|
1 |
|
"""Verify if the current path is affected by link down event.""" |
1267
|
|
|
return self.current_path.is_affected_by_link(link) |
1268
|
1 |
|
|
1269
|
|
|
def is_using_primary_path(self): |
1270
|
1 |
|
"""Verify if the current deployed path is self.primary_path.""" |
1271
|
|
|
return self.current_path == self.primary_path |
1272
|
|
|
|
1273
|
|
|
def is_using_backup_path(self): |
1274
|
|
|
"""Verify if the current deployed path is self.backup_path.""" |
1275
|
|
|
return self.current_path == self.backup_path |
1276
|
|
|
|
1277
|
1 |
|
def is_using_dynamic_path(self): |
1278
|
|
|
"""Verify if the current deployed path is dynamic.""" |
1279
|
1 |
|
if ( |
1280
|
|
|
self.current_path |
1281
|
1 |
|
and not self.is_using_primary_path() |
1282
|
1 |
|
and not self.is_using_backup_path() |
1283
|
1 |
|
and self.current_path.status is EntityStatus.UP |
1284
|
|
|
): |
1285
|
1 |
|
return True |
1286
|
1 |
|
return False |
1287
|
|
|
|
1288
|
1 |
|
def deploy_to(self, path_name=None, path=None): |
1289
|
|
|
"""Create a deploy to path.""" |
1290
|
1 |
|
if self.current_path == path: |
1291
|
|
|
log.debug(f"{path_name} is equal to current_path.") |
1292
|
|
|
return True |
1293
|
|
|
|
1294
|
|
|
if path.status is EntityStatus.UP: |
1295
|
|
|
return self.deploy_to_path(path) |
1296
|
|
|
|
1297
|
1 |
|
return False |
1298
|
1 |
|
|
1299
|
|
|
def handle_link_up(self, link): |
1300
|
1 |
|
"""Handle circuit when link down. |
1301
|
1 |
|
|
1302
|
1 |
|
Args: |
1303
|
|
|
link(Link): Link affected by link.down event. |
1304
|
1 |
|
|
1305
|
1 |
|
""" |
1306
|
|
|
if self.is_using_primary_path(): |
1307
|
|
|
return True |
1308
|
|
|
|
1309
|
1 |
|
success = False |
1310
|
1 |
|
if self.primary_path.is_affected_by_link(link): |
1311
|
|
|
success = self.deploy_to_primary_path() |
1312
|
|
|
|
1313
|
|
|
if success: |
1314
|
1 |
|
return True |
1315
|
1 |
|
|
1316
|
|
|
# We tried to deploy(primary_path) without success. |
1317
|
|
|
# And in this case is up by some how. Nothing to do. |
1318
|
|
|
if self.is_using_backup_path() or self.is_using_dynamic_path(): |
1319
|
1 |
|
return True |
1320
|
1 |
|
|
1321
|
|
|
# In this case, probably the circuit is not being used and |
1322
|
1 |
|
# we can move to backup |
1323
|
1 |
|
if self.backup_path.is_affected_by_link(link): |
1324
|
|
|
success = self.deploy_to_backup_path() |
1325
|
1 |
|
|
1326
|
|
|
# In this case, the circuit is not being used and we should |
1327
|
1 |
|
# try a dynamic path |
1328
|
|
|
if not success and self.dynamic_backup_path: |
1329
|
1 |
|
success = self.deploy_to_path() |
1330
|
|
|
|
1331
|
|
|
if success: |
1332
|
|
|
emit_event(self._controller, "redeployed_link_up", |
1333
|
|
|
content=map_evc_event_content(self)) |
1334
|
|
|
return True |
1335
|
|
|
|
1336
|
1 |
|
return True |
1337
|
1 |
|
|
1338
|
1 |
|
def handle_link_down(self): |
1339
|
1 |
|
"""Handle circuit when link down. |
1340
|
1 |
|
|
1341
|
|
|
Returns: |
1342
|
1 |
|
bool: True if the re-deploy was successly otherwise False. |
1343
|
1 |
|
|
1344
|
|
|
""" |
1345
|
1 |
|
success = False |
1346
|
1 |
|
if self.is_using_primary_path(): |
1347
|
|
|
success = self.deploy_to_backup_path() |
1348
|
1 |
|
elif self.is_using_backup_path(): |
1349
|
1 |
|
success = self.deploy_to_primary_path() |
1350
|
1 |
|
|
1351
|
1 |
|
if not success and self.dynamic_backup_path: |
1352
|
|
|
success = self.deploy_to_path() |
1353
|
1 |
|
|
1354
|
|
|
if success: |
1355
|
|
|
log.debug(f"{self} deployed after link down.") |
1356
|
1 |
|
else: |
1357
|
|
|
self.deactivate() |
1358
|
|
|
self.current_path = Path([]) |
1359
|
|
|
self.sync() |
1360
|
|
|
log.debug(f"Failed to re-deploy {self} after link down.") |
1361
|
|
|
|
1362
|
|
|
return success |
1363
|
|
|
|
1364
|
|
|
|
1365
|
|
|
class EVC(LinkProtection): |
1366
|
|
|
"""Class that represents a E-Line Virtual Connection.""" |
1367
|
|
|
|