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