Test Failed
Pull Request — master (#653)
by
unknown
04:15
created

build.models.evc.EVCDeploy.check_trace()   D

Complexity

Conditions 12

Size

Total Lines 72
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 12.4202

Importance

Changes 0
Metric Value
cc 12
eloc 55
nop 9
dl 0
loc 72
ccs 24
cts 28
cp 0.8571
crap 12.4202
rs 4.8
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like build.models.evc.EVCDeploy.check_trace() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
"""Classes used in the main application."""  # pylint: disable=too-many-lines
2 1
import traceback
3 1
from collections import OrderedDict, defaultdict
4 1
from copy import deepcopy
5 1
from datetime import datetime
6 1
from operator import eq, ne
7 1
from threading import Lock
8 1
from typing import Union
9 1
from uuid import uuid4
10
11 1
import httpx
12 1
from glom import glom
13 1
from tenacity import (retry, retry_if_exception_type, stop_after_attempt,
14
                      wait_combine, wait_fixed, wait_random)
15
16 1
from kytos.core import log
17 1
from kytos.core.common import EntityStatus, GenericEntity
18 1
from kytos.core.exceptions import KytosNoTagAvailableError, KytosTagError
19 1
from kytos.core.helpers import get_time, now
20 1
from kytos.core.interface import UNI, Interface, TAGRange
21 1
from kytos.core.link import Link
22 1
from kytos.core.retry import before_sleep
23 1
from kytos.core.tag_ranges import range_difference
24 1
from napps.kytos.mef_eline import controllers, settings
25 1
from napps.kytos.mef_eline.exceptions import (ActivationError,
26
                                              DuplicatedNoTagUNI,
27
                                              EVCPathNotInstalled,
28
                                              FlowModException, InvalidPath)
29 1
from napps.kytos.mef_eline.utils import (_does_uni_affect_evc,
30
                                         check_disabled_component,
31
                                         compare_endpoint_trace,
32
                                         compare_uni_out_trace, emit_event,
33
                                         make_uni_list, map_dl_vlan,
34
                                         map_evc_event_content,
35
                                         merge_flow_dicts)
36
37 1
from .path import DynamicPathManager, Path
38
39
40 1
class EVCBase(GenericEntity):
41
    """Class to represent a circuit."""
42
43 1
    attributes_requiring_redeploy = [
44
        "primary_path",
45
        "backup_path",
46
        "dynamic_backup_path",
47
        "queue_id",
48
        "sb_priority",
49
        "primary_constraints",
50
        "secondary_constraints",
51
        "uni_a",
52
        "uni_z",
53
    ]
54 1
    required_attributes = ["name", "uni_a", "uni_z"]
55
56 1
    updatable_attributes = {
57
        "uni_a",
58
        "uni_z",
59
        "name",
60
        "start_date",
61
        "end_date",
62
        "queue_id",
63
        "bandwidth",
64
        "primary_path",
65
        "backup_path",
66
        "dynamic_backup_path",
67
        "primary_constraints",
68
        "secondary_constraints",
69
        "owner",
70
        "sb_priority",
71
        "service_level",
72
        "circuit_scheduler",
73
        "metadata",
74
        "enabled",
75
        "max_paths",
76
    }
77
78
    # pylint: disable=too-many-statements
79 1
    def __init__(self, controller, **kwargs):
80
        """Create an EVC instance with the provided parameters.
81
82
        Args:
83
            id(str): EVC identifier. Whether it's None an ID will be genereted.
84
                     Only the first 14 bytes passed will be used.
85
            name: represents an EVC name.(Required)
86
            uni_a (UNI): Endpoint A for User Network Interface.(Required)
87
            uni_z (UNI): Endpoint Z for User Network Interface.(Required)
88
            start_date(datetime|str): Date when the EVC was registred.
89
                                      Default is now().
90
            end_date(datetime|str): Final date that the EVC will be fineshed.
91
                                    Default is None.
92
            bandwidth(int): Bandwidth used by EVC instance. Default is 0.
93
            primary_links(list): Primary links used by evc. Default is []
94
            backup_links(list): Backups links used by evc. Default is []
95
            current_path(list): Circuit being used at the moment if this is an
96
                                active circuit. Default is [].
97
            failover_path(list): Path being used to provide EVC protection via
98
                                failover during link failures. Default is [].
99
            primary_path(list): primary circuit offered to user IF one or more
100
                                links were provided. Default is [].
101
            backup_path(list): backup circuit offered to the user IF one or
102
                               more links were provided. Default is [].
103
            dynamic_backup_path(bool): Enable computer backup path dynamically.
104
                                       Dafault is False.
105
            creation_time(datetime|str): datetime when the circuit should be
106
                                         activated. default is now().
107
            enabled(Boolean): attribute to indicate the administrative state;
108
                              default is False.
109
            active(Boolean): attribute to indicate the operational state;
110
                             default is False.
111
            archived(Boolean): indicate the EVC has been deleted and is
112
                               archived; default is False.
113
            owner(str): The EVC owner. Default is None.
114
            sb_priority(int): Service level provided in the request.
115
                              Default is None.
116
            service_level(int): Service level provided. The higher the better.
117
                                Default is 0.
118
119
        Raises:
120
            ValueError: raised when object attributes are invalid.
121
122
        """
123 1
        self._controller = controller
124 1
        self._validate(**kwargs)
125 1
        super().__init__()
126
127
        # required attributes
128 1
        self._id = kwargs.get("id", uuid4().hex)[:14]
129 1
        self.uni_a: UNI = kwargs.get("uni_a")
130 1
        self.uni_z: UNI = kwargs.get("uni_z")
131 1
        self.name = kwargs.get("name")
132
133
        # optional attributes
134 1
        self.start_date = get_time(kwargs.get("start_date")) or now()
135 1
        self.end_date = get_time(kwargs.get("end_date")) or None
136 1
        self.queue_id = kwargs.get("queue_id", -1)
137
138 1
        self.bandwidth = kwargs.get("bandwidth", 0)
139 1
        self.primary_links = Path(kwargs.get("primary_links", []))
140 1
        self.backup_links = Path(kwargs.get("backup_links", []))
141 1
        self.current_path = Path(kwargs.get("current_path", []))
142 1
        self.failover_path = Path(kwargs.get("failover_path", []))
143 1
        self.primary_path = Path(kwargs.get("primary_path", []))
144 1
        self.backup_path = Path(kwargs.get("backup_path", []))
145 1
        self.dynamic_backup_path = kwargs.get("dynamic_backup_path", False)
146 1
        self.primary_constraints = kwargs.get("primary_constraints", {})
147 1
        self.secondary_constraints = kwargs.get("secondary_constraints", {})
148 1
        self.creation_time = get_time(kwargs.get("creation_time")) or now()
149 1
        self.owner = kwargs.get("owner", None)
150 1
        self.sb_priority = kwargs.get("sb_priority", None) or kwargs.get(
151
            "priority", None
152
        )
153 1
        self.service_level = kwargs.get("service_level", 0)
154 1
        self.circuit_scheduler = kwargs.get("circuit_scheduler", [])
155 1
        self.flow_removed_at = get_time(kwargs.get("flow_removed_at")) or None
156 1
        self.updated_at = get_time(kwargs.get("updated_at")) or now()
157 1
        self.execution_rounds = kwargs.get("execution_rounds", 0)
158 1
        self.current_links_cache = set()
159 1
        self.primary_links_cache = set()
160 1
        self.backup_links_cache = set()
161 1
        self.affected_by_link_at = get_time("0001-01-01T00:00:00")
162 1
        self.old_path = Path([])
163 1
        self.max_paths = kwargs.get("max_paths", 2)
164
165 1
        self.lock = Lock()
166
167 1
        self.archived = kwargs.get("archived", False)
168
169 1
        self.metadata = kwargs.get("metadata", {})
170
171 1
        self._mongo_controller = controllers.ELineController()
172
173 1
        if kwargs.get("active", False):
174 1
            self.activate()
175
        else:
176 1
            self.deactivate()
177
178 1
        if kwargs.get("enabled", False):
179 1
            self.enable()
180
        else:
181 1
            self.disable()
182
183
        # datetime of user request for a EVC (or datetime when object was
184
        # created)
185 1
        self.request_time = kwargs.get("request_time", now())
186
        # dict with the user original request (input)
187 1
        self._requested = kwargs
188
189
        # Special cases: No tag, any, untagged
190 1
        self.special_cases = {None, "4096/4096", 0}
191 1
        self.table_group = kwargs.get("table_group")
192
193 1
    def sync(self, keys: set = None):
194
        """Sync this EVC in the MongoDB."""
195 1
        self.updated_at = now()
196 1
        if keys:
197 1
            self._mongo_controller.update_evc(self.as_dict(keys))
198 1
            return
199 1
        self._mongo_controller.upsert_evc(self.as_dict())
200
201 1
    def _get_unis_use_tags(self, **kwargs) -> tuple[UNI, UNI]:
202
        """Obtain both UNIs (uni_a, uni_z).
203
        If a UNI is changing, verify tags"""
204 1
        uni_a = kwargs.get("uni_a", None)
205 1
        uni_a_flag = False
206 1
        if uni_a and uni_a != self.uni_a:
207 1
            uni_a_flag = True
208 1
            self._use_uni_vlan(uni_a, uni_dif=self.uni_a)
209
210 1
        uni_z = kwargs.get("uni_z", None)
211 1
        if uni_z and uni_z != self.uni_z:
212 1
            try:
213 1
                self._use_uni_vlan(uni_z, uni_dif=self.uni_z)
214 1
                self.make_uni_vlan_available(self.uni_z, uni_dif=uni_z)
215 1
            except KytosTagError as err:
216 1
                if uni_a_flag:
217 1
                    self.make_uni_vlan_available(uni_a, uni_dif=self.uni_a)
218 1
                raise err
219
        else:
220 1
            uni_z = self.uni_z
221
222 1
        if uni_a_flag:
223 1
            self.make_uni_vlan_available(self.uni_a, uni_dif=uni_a)
224
        else:
225 1
            uni_a = self.uni_a
226 1
        return uni_a, uni_z
227
228 1
    def update(self, **kwargs):
229
        """Update evc attributes.
230
231
        This method will raises an error trying to change the following
232
        attributes: [creation_time, active, current_path, failover_path,
233
        _id, archived]
234
        [name, uni_a and uni_z]
235
236
        Returns:
237
            the values for enable and a redeploy attribute, if exists and None
238
            otherwise
239
        Raises:
240
            ValueError: message with error detail.
241
242
        """
243 1
        enable, redeploy = (None, None)
244 1
        if not self._tag_lists_equal(**kwargs):
245 1
            raise ValueError(
246
                "UNI_A and UNI_Z tag lists should be the same."
247
            )
248 1
        uni_a, uni_z = self._get_unis_use_tags(**kwargs)
249 1
        check_disabled_component(uni_a, uni_z)
250 1
        self._validate_has_primary_or_dynamic(
251
            primary_path=kwargs.get("primary_path"),
252
            dynamic_backup_path=kwargs.get("dynamic_backup_path"),
253
            uni_a=uni_a,
254
            uni_z=uni_z,
255
        )
256 1
        for attribute, value in kwargs.items():
257 1
            if attribute not in self.updatable_attributes:
258 1
                raise ValueError(f"{attribute} can't be updated.")
259 1
            if attribute in ("primary_path", "backup_path"):
260 1
                try:
261 1
                    value.is_valid(
262
                        uni_a.interface.switch, uni_z.interface.switch
263
                    )
264 1
                except InvalidPath as exception:
265 1
                    raise ValueError(  # pylint: disable=raise-missing-from
266
                        f"{attribute} is not a " f"valid path: {exception}"
267
                    )
268 1
        for attribute, value in kwargs.items():
269 1
            if attribute == "enabled":
270 1
                if value:
271 1
                    self.enable()
272
                else:
273 1
                    self.disable()
274 1
                enable = value
275
            else:
276 1
                setattr(self, attribute, value)
277 1
                if attribute in self.attributes_requiring_redeploy:
278 1
                    redeploy = True
279 1
        self.sync(set(kwargs.keys()))
280 1
        return enable, redeploy
281
282 1
    def set_flow_removed_at(self):
283
        """Update flow_removed_at attribute."""
284
        self.flow_removed_at = now()
285
286 1
    def has_recent_removed_flow(self, setting=settings):
287
        """Check if any flow has been removed from the evc"""
288
        if self.flow_removed_at is None:
289
            return False
290
        res_seconds = (now() - self.flow_removed_at).seconds
291
        return res_seconds < setting.TIME_RECENT_DELETED_FLOWS
292
293 1
    def is_recent_updated(self, setting=settings):
294
        """Check if the evc has been updated recently"""
295
        res_seconds = (now() - self.updated_at).seconds
296
        return res_seconds < setting.TIME_RECENT_UPDATED
297
298 1
    def __repr__(self):
299
        """Repr method."""
300 1
        return f"EVC({self._id}, {self.name})"
301
302 1
    def _validate(self, **kwargs):
303
        """Do Basic validations.
304
305
        Verify required attributes: name, uni_a, uni_z
306
307
        Raises:
308
            ValueError: message with error detail.
309
310
        """
311 1
        for attribute in self.required_attributes:
312
313 1
            if attribute not in kwargs:
314 1
                raise ValueError(f"{attribute} is required.")
315
316 1
            if "uni" in attribute:
317 1
                uni = kwargs.get(attribute)
318 1
                if not isinstance(uni, UNI):
319
                    raise ValueError(f"{attribute} is an invalid UNI.")
320
321 1
    def _tag_lists_equal(self, **kwargs):
322
        """Verify that tag lists are the same."""
323 1
        uni_a = kwargs.get("uni_a") or self.uni_a
324 1
        uni_z = kwargs.get("uni_z") or self.uni_z
325 1
        uni_a_list = uni_z_list = False
326 1
        if (uni_a.user_tag and isinstance(uni_a.user_tag, TAGRange)):
327 1
            uni_a_list = True
328 1
        if (uni_z.user_tag and isinstance(uni_z.user_tag, TAGRange)):
329 1
            uni_z_list = True
330 1
        if uni_a_list and uni_z_list:
331 1
            return uni_a.user_tag.value == uni_z.user_tag.value
332 1
        return uni_a_list == uni_z_list
333
334 1
    def _validate_has_primary_or_dynamic(
335
        self,
336
        primary_path=None,
337
        dynamic_backup_path=None,
338
        uni_a=None,
339
        uni_z=None,
340
    ) -> None:
341
        """Validate that it must have a primary path or allow dynamic paths."""
342 1
        primary_path = (
343
            primary_path
344
            if primary_path is not None
345
            else self.primary_path
346
        )
347 1
        dynamic_backup_path = (
348
            dynamic_backup_path
349
            if dynamic_backup_path is not None
350
            else self.dynamic_backup_path
351
        )
352 1
        uni_a = uni_a if uni_a is not None else self.uni_a
353 1
        uni_z = uni_z if uni_z is not None else self.uni_z
354 1
        if (
355
            not primary_path
356
            and not dynamic_backup_path
357
            and uni_a and uni_z
358
            and uni_a.interface.switch != uni_z.interface.switch
359
        ):
360 1
            msg = "The EVC must have a primary path or allow dynamic paths."
361 1
            raise ValueError(msg)
362
363 1
    def __eq__(self, other):
364
        """Override the default implementation."""
365 1
        if not isinstance(other, EVC):
366
            return False
367
368 1
        attrs_to_compare = ["name", "uni_a", "uni_z", "owner", "bandwidth"]
369 1
        for attribute in attrs_to_compare:
370 1
            if getattr(other, attribute) != getattr(self, attribute):
371 1
                return False
372 1
        return True
373
374 1
    def is_intra_switch(self):
375
        """Check if the UNIs are in the same switch."""
376 1
        return self.uni_a.interface.switch == self.uni_z.interface.switch
377
378 1
    def check_no_tag_duplicate(self, other_uni: UNI):
379
        """Check if a no tag UNI is duplicated."""
380 1
        if other_uni in (self.uni_a, self.uni_z):
381 1
            msg = f"UNI with interface {other_uni.interface.id} is"\
382
                  f" duplicated with {self}."
383 1
            raise DuplicatedNoTagUNI(msg)
384
385 1
    def as_dict(self, keys: set = None):
386
        """Return a dictionary representing an EVC object.
387
            keys: Only fields on this variable will be
388
                  returned in the dictionary"""
389 1
        evc_dict = {
390
            "id": self.id,
391
            "name": self.name,
392
            "uni_a": self.uni_a.as_dict(),
393
            "uni_z": self.uni_z.as_dict(),
394
        }
395
396 1
        time_fmt = "%Y-%m-%dT%H:%M:%S"
397
398 1
        evc_dict["start_date"] = self.start_date
399 1
        if isinstance(self.start_date, datetime):
400 1
            evc_dict["start_date"] = self.start_date.strftime(time_fmt)
401
402 1
        evc_dict["end_date"] = self.end_date
403 1
        if isinstance(self.end_date, datetime):
404 1
            evc_dict["end_date"] = self.end_date.strftime(time_fmt)
405
406 1
        evc_dict["queue_id"] = self.queue_id
407 1
        evc_dict["bandwidth"] = self.bandwidth
408 1
        evc_dict["primary_links"] = self.primary_links.as_dict()
409 1
        evc_dict["backup_links"] = self.backup_links.as_dict()
410 1
        evc_dict["current_path"] = self.current_path.as_dict()
411 1
        evc_dict["failover_path"] = self.failover_path.as_dict()
412 1
        evc_dict["primary_path"] = self.primary_path.as_dict()
413 1
        evc_dict["backup_path"] = self.backup_path.as_dict()
414 1
        evc_dict["dynamic_backup_path"] = self.dynamic_backup_path
415 1
        evc_dict["metadata"] = self.metadata
416
417 1
        evc_dict["request_time"] = self.request_time
418 1
        if isinstance(self.request_time, datetime):
419 1
            evc_dict["request_time"] = self.request_time.strftime(time_fmt)
420
421 1
        time = self.creation_time.strftime(time_fmt)
422 1
        evc_dict["creation_time"] = time
423
424 1
        evc_dict["owner"] = self.owner
425 1
        evc_dict["circuit_scheduler"] = [
426
            sc.as_dict() for sc in self.circuit_scheduler
427
        ]
428
429 1
        evc_dict["active"] = self.is_active()
430 1
        evc_dict["enabled"] = self.is_enabled()
431 1
        evc_dict["archived"] = self.archived
432 1
        evc_dict["sb_priority"] = self.sb_priority
433 1
        evc_dict["service_level"] = self.service_level
434 1
        evc_dict["primary_constraints"] = self.primary_constraints
435 1
        evc_dict["secondary_constraints"] = self.secondary_constraints
436 1
        evc_dict["flow_removed_at"] = self.flow_removed_at
437 1
        evc_dict["updated_at"] = self.updated_at
438 1
        evc_dict["max_paths"] = self.max_paths
439
440 1
        if keys:
441 1
            selected = {}
442 1
            for key in keys:
443 1
                selected[key] = evc_dict[key]
444 1
            selected["id"] = evc_dict["id"]
445 1
            return selected
446 1
        return evc_dict
447
448 1
    @property
449 1
    def id(self):  # pylint: disable=invalid-name
450
        """Return this EVC's ID."""
451 1
        return self._id
452
453 1
    def archive(self):
454
        """Archive this EVC on deletion."""
455 1
        self.archived = True
456
457 1
    def _use_uni_vlan(
458
        self,
459
        uni: UNI,
460
        uni_dif: Union[None, UNI] = None
461
    ):
462
        """Use tags from UNI"""
463 1
        if uni.user_tag is None:
464 1
            return
465 1
        tag = uni.user_tag.value
466 1
        tag_type = uni.user_tag.tag_type
467 1
        if (uni_dif and isinstance(tag, list) and
468
                isinstance(uni_dif.user_tag.value, list)):
469 1
            tag = range_difference(tag, uni_dif.user_tag.value)
470 1
            if not tag:
471 1
                return
472 1
        uni.interface.use_tags(
473
            self._controller, tag, tag_type, use_lock=True, check_order=False
474
        )
475
476 1
    def make_uni_vlan_available(
477
        self,
478
        uni: UNI,
479
        uni_dif: Union[None, UNI] = None,
480
    ):
481
        """Make available tag from UNI"""
482 1
        if uni.user_tag is None:
483 1
            return
484 1
        tag = uni.user_tag.value
485 1
        tag_type = uni.user_tag.tag_type
486 1
        if (uni_dif and isinstance(tag, list) and
487
                isinstance(uni_dif.user_tag.value, list)):
488 1
            tag = range_difference(tag, uni_dif.user_tag.value)
489 1
            if not tag:
490
                return
491 1
        try:
492 1
            conflict = uni.interface.make_tags_available(
493
                self._controller, tag, tag_type, use_lock=True,
494
                check_order=False
495
            )
496 1
        except KytosTagError as err:
497 1
            log.error(f"Error in {self}: {err}")
498 1
            return
499 1
        if conflict:
500 1
            intf = uni.interface.id
501 1
            log.warning(f"Tags {conflict} was already available in {intf}")
502
503 1
    def remove_uni_tags(self):
504
        """Remove both UNI usage of a tag"""
505 1
        self.make_uni_vlan_available(self.uni_a)
506 1
        self.make_uni_vlan_available(self.uni_z)
507
508
509
# pylint: disable=fixme, too-many-public-methods
510 1
class EVCDeploy(EVCBase):
511
    """Class to handle the deploy procedures."""
512
513 1
    def create(self):
514
        """Create a EVC."""
515
516 1
    def discover_new_paths(self):
517
        """Discover new paths to satisfy this circuit and deploy it."""
518
        return DynamicPathManager.get_best_paths(self, self.max_paths,
519
                                                 **self.primary_constraints)
520
521 1
    def get_failover_path_candidates(self):
522
        """Get failover paths to satisfy this EVC."""
523
        # in the future we can return primary/backup paths as well
524
        # we just have to properly handle link_up and failover paths
525
        # if (
526
        #     self.is_using_primary_path() and
527
        #     self.backup_path.status is EntityStatus.UP
528
        # ):
529
        #     yield self.backup_path
530 1
        return DynamicPathManager.get_disjoint_paths(self, self.current_path)
531
532 1
    def change_path(self):
533
        """Change EVC path."""
534
535 1
    def reprovision(self):
536
        """Force the EVC (re-)provisioning."""
537
538 1
    def is_affected_by_link(self, link):
539
        """Return True if this EVC has the given link on its current path."""
540 1
        return link in self.current_path
541
542 1
    def link_affected_by_interface(self, interface):
543
        """Return True if this EVC has the given link on its current path."""
544
        return self.current_path.link_affected_by_interface(interface)
545
546 1
    def is_backup_path_affected_by_link(self, link):
547
        """Return True if the backup path of this EVC uses the given link."""
548 1
        return link in self.backup_path
549
550
    # pylint: disable=invalid-name
551 1
    def is_primary_path_affected_by_link(self, link):
552
        """Return True if the primary path of this EVC uses the given link."""
553 1
        return link in self.primary_path
554
555 1
    def is_failover_path_affected_by_link(self, link):
556
        """Return True if this EVC has the given link on its failover path."""
557 1
        return link in self.failover_path
558
559 1
    def is_eligible_for_failover_path(self):
560
        """Verify if this EVC is eligible for failover path (EP029)"""
561
        # In the future this function can be augmented to consider
562
        # primary/backup, primary/dynamic, and other path combinations
563 1
        return (
564
            self.dynamic_backup_path and
565
            not self.primary_path and not self.backup_path
566
        )
567
568 1
    def is_using_primary_path(self):
569
        """Verify if the current deployed path is self.primary_path."""
570 1
        return self.primary_path and (self.current_path == self.primary_path)
571
572 1
    def is_using_backup_path(self):
573
        """Verify if the current deployed path is self.backup_path."""
574 1
        return self.backup_path and (self.current_path == self.backup_path)
575
576 1
    def is_using_dynamic_path(self):
577
        """Verify if the current deployed path is a dynamic path."""
578 1
        if (
579
            self.current_path
580
            and not self.is_using_primary_path()
581
            and not self.is_using_backup_path()
582
            and self.current_path.status == EntityStatus.UP
583
        ):
584
            return True
585 1
        return False
586
587 1
    def deploy_to_backup_path(self, old_path_dict: dict = None):
588
        """Deploy the backup path into the datapaths of this circuit.
589
590
        If the backup_path attribute is valid and up, this method will try to
591
        deploy this backup_path.
592
593
        If everything fails and dynamic_backup_path is True, then tries to
594
        deploy a dynamic path.
595
        """
596
        # TODO: Remove flows from current (cookies)
597 1
        if self.is_using_backup_path():
598
            # TODO: Log to say that cannot move backup to backup
599
            return True
600
601 1
        success = False
602 1
        if self.backup_path.status is EntityStatus.UP:
603 1
            success = self.deploy_to_path(self.backup_path, old_path_dict)
604
605 1
        if success:
606 1
            return True
607
608 1
        if self.dynamic_backup_path or self.is_intra_switch():
609 1
            return self.deploy_to_path(old_path_dict=old_path_dict)
610
611
        return False
612
613 1
    def deploy_to_primary_path(self, old_path_dict: dict = None):
614
        """Deploy the primary path into the datapaths of this circuit.
615
616
        If the primary_path attribute is valid and up, this method will try to
617
        deploy this primary_path.
618
        """
619
        # TODO: Remove flows from current (cookies)
620 1
        if self.is_using_primary_path():
621
            # TODO: Log to say that cannot move primary to primary
622
            return True
623
624 1
        if self.primary_path.status is EntityStatus.UP:
625 1
            return self.deploy_to_path(self.primary_path, old_path_dict)
626
        return False
627
628 1
    def deploy(self, old_path_dict: dict = None):
629
        """Deploy EVC to best path.
630
631
        Best path can be the primary path, if available. If not, the backup
632
        path, and, if it is also not available, a dynamic path.
633
        """
634 1
        if self.archived:
635 1
            return False
636 1
        self.enable()
637 1
        success = self.deploy_to_primary_path(old_path_dict)
638 1
        if not success:
639 1
            success = self.deploy_to_backup_path(old_path_dict)
640
641 1
        if success:
642 1
            emit_event(self._controller, "deployed",
643
                       content=map_evc_event_content(self))
644 1
        return success
645
646 1
    @staticmethod
647 1
    def get_path_status(path):
648
        """Check for the current status of a path.
649
650
        If any link in this path is down, the path is considered down.
651
        """
652 1
        if not path:
653 1
            return EntityStatus.DISABLED
654
655 1
        for link in path:
656 1
            if link.status is not EntityStatus.UP:
657 1
                return link.status
658 1
        return EntityStatus.UP
659
660
    #    def discover_new_path(self):
661
    #        # TODO: discover a new path to satisfy this circuit and deploy
662
663 1
    def remove(self):
664
        """Remove EVC path and disable it."""
665 1
        self.remove_current_flows(sync=False)
666 1
        self.remove_failover_flows(sync=False)
667 1
        self.disable()
668 1
        self.sync()
669 1
        emit_event(self._controller, "undeployed",
670
                   content=map_evc_event_content(self))
671
672 1
    def remove_failover_flows(self, exclude_uni_switches=True,
673
                              force=True, sync=True) -> None:
674
        """Remove failover_flows.
675
676
        By default, it'll exclude UNI switches, if mef_eline has already
677
        called remove_current_flows before then this minimizes the number
678
        of FlowMods and IO.
679
        """
680 1
        if not self.failover_path:
681 1
            return
682 1
        switches, cookie, excluded = set(), self.get_cookie(), set()
683 1
        if exclude_uni_switches:
684 1
            excluded.add(self.uni_a.interface.switch.id)
685 1
            excluded.add(self.uni_z.interface.switch.id)
686 1
        for link in self.failover_path:
687 1
            if link.endpoint_a.switch.id not in excluded:
688 1
                switches.add(link.endpoint_a.switch.id)
689 1
            if link.endpoint_b.switch.id not in excluded:
690 1
                switches.add(link.endpoint_b.switch.id)
691 1
        flow_mods = {
692
            "switches": list(switches),
693
            "flows": [{
694
                "cookie": cookie,
695
                "cookie_mask": int(0xffffffffffffffff),
696
                "owner": "mef_eline",
697
            }]
698
        }
699 1
        try:
700 1
            self._send_flow_mods(
701
                flow_mods,
702
                "delete",
703
                force=force,
704
            )
705
        except FlowModException as err:
706
            log.error(f"Error deleting {self} failover_path flows, {err}")
707 1
        try:
708 1
            self.failover_path.make_vlans_available(self._controller)
709
        except KytosTagError as err:
710
            log.error(f"Error removing {self} failover_path: {err}")
711 1
        self.failover_path = Path([])
712 1
        if sync:
713 1
            self.sync()
714
715 1
    def remove_current_flows(
716
        self,
717
        current_path=None,
718
        force=True,
719
        sync=True,
720
        return_path=False
721
    ) -> dict[str, int]:
722
        """Remove all flows from current path or path intended for
723
         current path if exists."""
724 1
        switches, old_path_dict = set(), {}
725 1
        current_path = self.current_path if not current_path else current_path
726 1
        if not current_path and not self.is_intra_switch():
727 1
            return {}
728
729 1
        if return_path:
730 1
            for link in self.current_path:
731 1
                s_vlan = link.metadata.get("s_vlan")
732 1
                if s_vlan:
733 1
                    old_path_dict[link.id] = s_vlan.value
734
735 1
        for link in current_path:
736 1
            switches.add(link.endpoint_a.switch.id)
737 1
            switches.add(link.endpoint_b.switch.id)
738 1
        switches.add(self.uni_a.interface.switch.id)
739 1
        switches.add(self.uni_z.interface.switch.id)
740 1
        flow_mods = {
741
            "switches": list(switches),
742
            "flows": [{
743
                "cookie": self.get_cookie(),
744
                "cookie_mask": int(0xffffffffffffffff),
745
                "owner": "mef_eline",
746
            }]
747
        }
748
749 1
        try:
750 1
            self._send_flow_mods(flow_mods, "delete", force=force)
751 1
        except FlowModException as err:
752 1
            log.error(f"Error deleting {self} current_path flows, {err}")
753
754 1
        try:
755 1
            current_path.make_vlans_available(self._controller)
756 1
        except KytosTagError as err:
757 1
            log.error(f"Error removing {self} current_path: {err}")
758 1
        self.current_path = Path([])
759 1
        self.deactivate()
760 1
        if sync:
761 1
            self.sync()
762 1
        return old_path_dict
763
764 1
    def remove_path_flows(
765
        self, path=None, force=True
766
    ) -> dict[str, list[dict]]:
767
        """Remove all flows from path, and return the removed flows."""
768 1
        dpid_flows_match: dict[str, dict] = defaultdict(lambda: {"flows": []})
769 1
        out_flows: dict[str, list[dict]] = defaultdict(list)
770
771 1
        if not path:
772 1
            return dpid_flows_match
773
774 1
        try:
775 1
            nni_flows = self._prepare_nni_flows(path)
776
        # pylint: disable=broad-except
777
        except Exception:
778
            err = traceback.format_exc()
779
            log.error(f"Fail to remove NNI failover flows for {self}: {err}")
780
            nni_flows = {}
781
782 1
        for dpid, flows in nni_flows.items():
783 1
            for flow in flows:
784 1
                flow_mod = {
785
                    "cookie": flow["cookie"],
786
                    "match": flow["match"],
787
                    "owner": "mef_eline",
788
                    "cookie_mask": int(0xffffffffffffffff)
789
                }
790 1
                dpid_flows_match[dpid]["flows"].append(flow_mod)
791 1
                out_flows[dpid].append(flow_mod)
792
793 1
        try:
794 1
            uni_flows = self._prepare_uni_flows(path, skip_in=True)
795
        # pylint: disable=broad-except
796
        except Exception:
797
            err = traceback.format_exc()
798
            log.error(f"Fail to remove UNI failover flows for {self}: {err}")
799
            uni_flows = {}
800
801 1
        for dpid, flows in uni_flows.items():
802 1
            for flow in flows:
803 1
                flow_mod = {
804
                    "cookie": flow["cookie"],
805
                    "match": flow["match"],
806
                    "owner": "mef_eline",
807
                    "cookie_mask": int(0xffffffffffffffff)
808
                }
809 1
                dpid_flows_match[dpid]["flows"].append(flow_mod)
810 1
                out_flows[dpid].append(flow_mod)
811
812 1
        try:
813 1
            self._send_flow_mods(
814
                dpid_flows_match, 'delete', force=force, by_switch=True
815
            )
816 1
        except FlowModException as err:
817 1
            log.error(
818
                f"Error deleting {self} path flows, path:{path}, error={err}"
819
            )
820
821 1
        try:
822 1
            path.make_vlans_available(self._controller)
823
        except KytosTagError as err:
824
            log.error(f"Error removing {self} path: {err}")
825
826 1
        return out_flows
827
828 1
    @staticmethod
829 1
    def links_zipped(path=None):
830
        """Return an iterator which yields pairs of links in order."""
831 1
        if not path:
832 1
            return []
833 1
        return zip(path[:-1], path[1:])
834
835 1
    def should_deploy(self, path=None):
836
        """Verify if the circuit should be deployed."""
837 1
        if not path:
838 1
            log.debug("Path is empty.")
839 1
            return False
840
841 1
        if not self.is_enabled():
842 1
            log.debug(f"{self} is disabled.")
843 1
            return False
844
845 1
        if not self.is_active():
846 1
            log.debug(f"{self} will be deployed.")
847 1
            return True
848
849 1
        return False
850
851 1
    @staticmethod
852 1
    def is_uni_interface_active(
853
        *interfaces: Interface
854
    ) -> tuple[bool, dict]:
855
        """Whether UNIs are active and their status & status_reason."""
856 1
        active = True
857 1
        bad_interfaces = [
858
            interface
859
            for interface in interfaces
860
            if interface.status != EntityStatus.UP
861
        ]
862 1
        if bad_interfaces:
863 1
            active = False
864 1
            interfaces = bad_interfaces
865 1
        return active, {
866
            interface.id: {
867
                'status': interface.status.value,
868
                'status_reason': interface.status_reason,
869
            }
870
            for interface in interfaces
871
        }
872
873 1
    def try_to_activate(self) -> bool:
874
        """Try to activate the EVC."""
875 1
        if self.is_intra_switch():
876 1
            return self._try_to_activate_intra_evc()
877 1
        return self._try_to_activate_inter_evc()
878
879 1
    def _try_to_activate_intra_evc(self) -> bool:
880
        """Try to activate intra EVC."""
881 1
        intf_a, intf_z = self.uni_a.interface, self.uni_z.interface
882 1
        is_active, reason = self.is_uni_interface_active(intf_a, intf_z)
883 1
        if not is_active:
884 1
            raise ActivationError(
885
                f"Won't be able to activate {self} due to UNIs: {reason}"
886
            )
887 1
        self.activate()
888 1
        return True
889
890 1
    def _try_to_activate_inter_evc(self) -> bool:
891
        """Try to activate inter EVC."""
892 1
        intf_a, intf_z = self.uni_a.interface, self.uni_z.interface
893 1
        is_active, reason = self.is_uni_interface_active(intf_a, intf_z)
894 1
        if not is_active:
895 1
            raise ActivationError(
896
                f"Won't be able to activate {self} due to UNIs: {reason}"
897
            )
898 1
        if self.current_path.status != EntityStatus.UP:
899 1
            raise ActivationError(
900
                f"Won't be able to activate {self} due to current_path "
901
                f"status {self.current_path.status}"
902
            )
903 1
        self.activate()
904 1
        return True
905
906
    # pylint: disable=too-many-branches, too-many-statements
907 1
    def deploy_to_path(self, path=None, old_path_dict: dict = None):
908
        """Install the flows for this circuit.
909
910
        Procedures to deploy:
911
912
        0. Remove current flows installed
913
        1. Decide if will deploy "path" or discover a new path
914
        2. Choose vlan
915
        3. Install NNI flows
916
        4. Install UNI flows
917
        5. Activate
918
        6. Update current_path
919
        7. Update links caches(primary, current, backup)
920
921
        """
922 1
        self.remove_current_flows(sync=False)
923 1
        use_path = path or Path([])
924 1
        if not old_path_dict:
925 1
            old_path_dict = {}
926 1
        tag_errors = []
927 1
        no_valid_path = False
928 1
        if self.should_deploy(use_path):
929 1
            try:
930 1
                use_path.choose_vlans(self._controller, old_path_dict)
931 1
            except KytosNoTagAvailableError as e:
932 1
                tag_errors.append(str(e))
933 1
                use_path = None
934
        else:
935 1
            for use_path in self.discover_new_paths():
936 1
                if use_path is None:
937 1
                    no_valid_path = True
938 1
                    continue
939 1
                try:
940 1
                    use_path.choose_vlans(self._controller, old_path_dict)
941 1
                    break
942 1
                except KytosNoTagAvailableError as e:
943 1
                    tag_errors.append(str(e))
944
            else:
945 1
                use_path = None
946
947 1
        try:
948 1
            if use_path:
949 1
                self._install_flows(use_path)
950 1
            elif self.is_intra_switch():
951 1
                use_path = Path()
952 1
                self._install_direct_uni_flows()
953
            else:
954 1
                no_path_msg = "No available path was found."
955 1
                if no_valid_path:
956 1
                    no_path_msg = "No valid path was found, "\
957
                                  "try increasing `max_paths`"\
958
                                 f" from {self.max_paths}."
959 1
                msg = f"{self} was not deployed. {no_path_msg}"
960 1
                if tag_errors:
961 1
                    msg = self.add_tag_errors(msg, tag_errors)
962 1
                    log.error(msg)
963
                else:
964 1
                    log.warning(msg)
965 1
                return False
966 1
        except EVCPathNotInstalled as err:
967 1
            log.error(
968
                f"Error deploying EVC {self} when calling flow_manager: {err}"
969
            )
970 1
            self.remove_current_flows(use_path, sync=True)
971 1
            return False
972
973 1
        self.current_path = use_path
974 1
        msg = f"{self} was deployed."
975 1
        try:
976 1
            self.try_to_activate()
977
        except ActivationError as exc:
978
            msg = f"{msg} {str(exc)}"
979 1
        self.sync()
980 1
        log.info(msg)
981 1
        return True
982
983 1
    def try_setup_failover_path(
984
        self,
985
        wait=settings.DEPLOY_EVCS_INTERVAL,
986
        warn_if_not_path=True
987
    ):
988
        """Try setup failover_path whenever possible."""
989 1
        if (
990
                self.failover_path or not self.current_path
991
                or not self.is_active()
992
                ):
993 1
            return
994 1
        if (now() - self.affected_by_link_at).seconds >= wait:
995 1
            self.setup_failover_path(warn_if_not_path)
996 1
997
    # pylint: disable=too-many-statements
998
    def setup_failover_path(self, warn_if_not_path=True):
999 1
        """Install flows for the failover path of this EVC.
1000
1001
        Procedures to deploy:
1002
1003
        0. Remove flows currently installed for failover_path (if any)
1004
        1. Discover a disjoint path from current_path
1005
        2. Choose vlans
1006
        3. Install NNI flows
1007
        4. Install UNI egress flows
1008
        5. Update failover_path
1009
        """
1010
        # Intra-switch EVCs have no failover_path
1011
        if self.is_intra_switch():
1012 1
            return False
1013 1
1014
        # For not only setup failover path for totally dynamic EVCs
1015
        if not self.is_eligible_for_failover_path():
1016 1
            return False
1017 1
1018
        out_new_flows: dict[str, list[dict]] = {}
1019 1
        reason = ""
1020 1
        tag_errors = []
1021 1
        out_removed_flows = self.remove_path_flows(self.failover_path)
1022 1
        self.failover_path = Path([])
1023 1
1024
        for use_path in self.get_failover_path_candidates():
1025 1
            if not use_path:
1026 1
                continue
1027 1
            try:
1028 1
                use_path.choose_vlans(self._controller)
1029 1
                break
1030 1
            except KytosNoTagAvailableError as e:
1031 1
                tag_errors.append(str(e))
1032 1
        else:
1033
            use_path = Path([])
1034 1
            reason = "No available path was found"
1035 1
1036
        try:
1037 1
            if use_path:
1038 1
                out_new_flows = self._install_flows(
1039 1
                    use_path, skip_in=True
1040
                )
1041
        except EVCPathNotInstalled as err:
1042 1
            reason = "Error deploying failover path"
1043 1
            log.error(
1044 1
                f"{reason} for {self}. FlowManager error: {err}"
1045
            )
1046
            _rmed_flows = self.remove_path_flows(use_path)
1047 1
            out_removed_flows = merge_flow_dicts(
1048 1
                out_removed_flows, _rmed_flows
1049
            )
1050
            use_path = Path([])
1051 1
1052
        self.failover_path = use_path
1053 1
        self.sync()
1054 1
1055
        if out_new_flows or out_removed_flows:
1056 1
            emit_event(self._controller, "failover_deployed", content={
1057 1
                self.id: map_evc_event_content(
1058
                    self,
1059
                    flows=deepcopy(out_new_flows),
1060
                    removed_flows=deepcopy(out_removed_flows),
1061
                    error_reason=reason,
1062
                    current_path=self.current_path.as_dict(),
1063
                )
1064
            })
1065
1066
        if not use_path:
1067 1
            msg = f"Failover path for {self} was not deployed: {reason}."
1068 1
            if tag_errors:
1069 1
                msg = self.add_tag_errors(msg, tag_errors)
1070 1
                log.error(msg)
1071 1
            elif warn_if_not_path:
1072 1
                log.warning(msg)
1073 1
            return False
1074 1
        log.info(f"Failover path for {self} was deployed.")
1075 1
        return True
1076 1
1077
    @staticmethod
1078 1
    def add_tag_errors(msg: str, tag_errors: list):
1079 1
        """Add to msg the tag errors ecountered when chossing path."""
1080
        path = ['path', 'paths']
1081 1
        was = ['was', 'were']
1082 1
        message = ['message', 'messages']
1083 1
1084
        # Choose either singular(0) or plural(1) words
1085
        n = 1
1086 1
        if len(tag_errors) == 1:
1087 1
            n = 0
1088 1
1089
        msg += f" {len(tag_errors)} {path[n]} {was[n]} rejected"
1090 1
        msg += f" with {message[n]}: {tag_errors}"
1091 1
        return msg
1092 1
1093
    def get_failover_flows(self):
1094 1
        """Return the flows needed to make the failover path active, i.e. the
1095
        flows for ingress forwarding.
1096
1097
        Return:
1098
            dict: A dict of flows indexed by the switch_id will be returned, or
1099
                an empty dict if no failover_path is available.
1100
        """
1101
        if not self.failover_path:
1102 1
            return {}
1103 1
        return self._prepare_uni_flows(self.failover_path, skip_out=True)
1104 1
1105
    # pylint: disable=too-many-branches
1106
    def _prepare_direct_uni_flows(self):
1107 1
        """Prepare flows connecting two UNIs for intra-switch EVC."""
1108
        vlan_a = self._get_value_from_uni_tag(self.uni_a)
1109 1
        vlan_z = self._get_value_from_uni_tag(self.uni_z)
1110 1
1111
        flow_mod_az = self._prepare_flow_mod(
1112 1
            self.uni_a.interface, self.uni_z.interface,
1113
            self.queue_id, vlan_a
1114
        )
1115
        flow_mod_za = self._prepare_flow_mod(
1116 1
            self.uni_z.interface, self.uni_a.interface,
1117
            self.queue_id, vlan_z
1118
        )
1119
1120 View Code Duplication
        if not isinstance(vlan_z, list) and vlan_z not in self.special_cases:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1121 1
            flow_mod_az["actions"].insert(
1122 1
                0, {"action_type": "set_vlan", "vlan_id": vlan_z}
1123
            )
1124
            if not vlan_a:
1125 1
                flow_mod_az["actions"].insert(
1126 1
                    0, {"action_type": "push_vlan", "tag_type": "c"}
1127
                )
1128
            if vlan_a == 0:
1129 1
                flow_mod_za["actions"].insert(0, {"action_type": "pop_vlan"})
1130 1
        elif vlan_a == 0 and vlan_z == "4096/4096":
1131 1
            flow_mod_za["actions"].insert(0, {"action_type": "pop_vlan"})
1132 1
1133 View Code Duplication
        if not isinstance(vlan_a, list) and vlan_a not in self.special_cases:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1134 1
            flow_mod_za["actions"].insert(
1135 1
                    0, {"action_type": "set_vlan", "vlan_id": vlan_a}
1136
                )
1137
            if not vlan_z:
1138 1
                flow_mod_za["actions"].insert(
1139 1
                    0, {"action_type": "push_vlan", "tag_type": "c"}
1140
                )
1141
            if vlan_z == 0:
1142 1
                flow_mod_az["actions"].insert(0, {"action_type": "pop_vlan"})
1143 1
        elif vlan_a == "4096/4096" and vlan_z == 0:
1144 1
            flow_mod_az["actions"].insert(0, {"action_type": "pop_vlan"})
1145 1
1146
        flows = []
1147 1
        if isinstance(vlan_a, list):
1148 1
            for mask_a in vlan_a:
1149 1
                flow_aux = deepcopy(flow_mod_az)
1150 1
                flow_aux["match"]["dl_vlan"] = mask_a
1151 1
                flows.append(flow_aux)
1152 1
        else:
1153
            if vlan_a is not None:
1154 1
                flow_mod_az["match"]["dl_vlan"] = vlan_a
1155 1
            flows.append(flow_mod_az)
1156 1
1157
        if isinstance(vlan_z, list):
1158 1
            for mask_z in vlan_z:
1159 1
                flow_aux = deepcopy(flow_mod_za)
1160 1
                flow_aux["match"]["dl_vlan"] = mask_z
1161 1
                flows.append(flow_aux)
1162 1
        else:
1163
            if vlan_z is not None:
1164 1
                flow_mod_za["match"]["dl_vlan"] = vlan_z
1165 1
            flows.append(flow_mod_za)
1166 1
        return (
1167 1
            self.uni_a.interface.switch.id, flows
1168
        )
1169
1170
    def _install_direct_uni_flows(self):
1171 1
        """Install flows connecting two UNIs.
1172
1173
        This case happens when the circuit is between UNIs in the
1174
        same switch.
1175
        """
1176
        (dpid, flows) = self._prepare_direct_uni_flows()
1177 1
        flow_mods = {"switches": [dpid], "flows": flows}
1178 1
        try:
1179 1
            self._send_flow_mods(flow_mods, "install")
1180 1
        except FlowModException as err:
1181 1
            raise EVCPathNotInstalled(str(err)) from err
1182 1
1183
    def _prepare_nni_flows(self, path=None):
1184 1
        """Prepare NNI flows."""
1185
        nni_flows = OrderedDict()
1186 1
        previous = self.uni_a.interface.switch.dpid
1187 1
        for incoming, outcoming in self.links_zipped(path):
1188 1
            in_vlan = incoming.get_metadata("s_vlan").value
1189 1
            out_vlan = outcoming.get_metadata("s_vlan").value
1190 1
            in_endpoint = self.get_endpoint_by_id(incoming, previous, ne)
1191 1
            out_endpoint = self.get_endpoint_by_id(
1192 1
                outcoming, in_endpoint.switch.id, eq
1193
            )
1194
1195
            flows = []
1196 1
            # Flow for one direction
1197
            flows.append(
1198 1
                self._prepare_nni_flow(
1199
                    in_endpoint,
1200
                    out_endpoint,
1201
                    in_vlan,
1202
                    out_vlan,
1203
                    queue_id=self.queue_id,
1204
                )
1205
            )
1206
1207
            # Flow for the other direction
1208
            flows.append(
1209 1
                self._prepare_nni_flow(
1210
                    out_endpoint,
1211
                    in_endpoint,
1212
                    out_vlan,
1213
                    in_vlan,
1214
                    queue_id=self.queue_id,
1215
                )
1216
            )
1217
            previous = in_endpoint.switch.id
1218 1
            nni_flows[in_endpoint.switch.id] = flows
1219 1
        return nni_flows
1220 1
1221
    def _install_flows(
1222 1
        self, path=None, skip_in=False, skip_out=False
1223
    ) -> dict[str, list[dict]]:
1224
        """Install uni and nni flows"""
1225
        flows_by_switch = defaultdict(lambda: {"flows": []})
1226 1
        new_flows = defaultdict(list)
1227 1
        for dpid, flows in self._prepare_nni_flows(path).items():
1228 1
            flows_by_switch[dpid]["flows"].extend(flows)
1229 1
            new_flows[dpid].extend(flows)
1230 1
        for dpid, flows in self._prepare_uni_flows(
1231 1
            path, skip_in, skip_out
1232
        ).items():
1233
            flows_by_switch[dpid]["flows"].extend(flows)
1234 1
            new_flows[dpid].extend(flows)
1235 1
1236
        try:
1237 1
            self._send_flow_mods(flows_by_switch, "install", by_switch=True)
1238 1
        except FlowModException as err:
1239 1
            raise EVCPathNotInstalled(str(err)) from err
1240 1
1241
        return new_flows
1242 1
1243
    @staticmethod
1244 1
    def _get_value_from_uni_tag(uni: UNI):
1245 1
        """Returns the value from tag. In case of any and untagged
1246
        it should return 4096/4096 and 0 respectively"""
1247
        special = {"any": "4096/4096", "untagged": 0}
1248 1
        if uni.user_tag:
1249 1
            value = uni.user_tag.value
1250 1
            if isinstance(value, list):
1251 1
                return uni.user_tag.mask_list
1252 1
            return special.get(value, value)
1253 1
        return None
1254 1
1255
    # pylint: disable=too-many-locals
1256
    def _prepare_uni_flows(self, path=None, skip_in=False, skip_out=False):
1257 1
        """Prepare flows to install UNIs."""
1258
        uni_flows = {}
1259 1
        if not path:
1260 1
            log.info("install uni flows without path.")
1261
            return uni_flows
1262
1263
        # Determine VLANs
1264
        in_vlan_a = self._get_value_from_uni_tag(self.uni_a)
1265 1
        out_vlan_a = path[0].get_metadata("s_vlan").value
1266 1
1267
        in_vlan_z = self._get_value_from_uni_tag(self.uni_z)
1268 1
        out_vlan_z = path[-1].get_metadata("s_vlan").value
1269 1
1270
        # Get endpoints from path
1271
        endpoint_a = self.get_endpoint_by_id(
1272 1
            path[0], self.uni_a.interface.switch.id, eq
1273
        )
1274
        endpoint_z = self.get_endpoint_by_id(
1275 1
            path[-1], self.uni_z.interface.switch.id, eq
1276
        )
1277
1278
        # Flows for the first UNI
1279
        flows_a = []
1280 1
1281
        # Flow for one direction, pushing the service tag
1282
        if not skip_in:
1283 1
            if isinstance(in_vlan_a, list):
1284 1
                for in_mask_a in in_vlan_a:
1285 1
                    push_flow = self._prepare_push_flow(
1286 1
                        self.uni_a.interface,
1287
                        endpoint_a,
1288
                        in_mask_a,
1289
                        out_vlan_a,
1290
                        in_vlan_z,
1291
                        queue_id=self.queue_id,
1292
                    )
1293
                    flows_a.append(push_flow)
1294 1
            else:
1295
                push_flow = self._prepare_push_flow(
1296
                    self.uni_a.interface,
1297
                    endpoint_a,
1298
                    in_vlan_a,
1299
                    out_vlan_a,
1300
                    in_vlan_z,
1301
                    queue_id=self.queue_id,
1302
                )
1303
                flows_a.append(push_flow)
1304
1305
        # Flow for the other direction, popping the service tag
1306
        if not skip_out:
1307 1
            pop_flow = self._prepare_pop_flow(
1308 1
                endpoint_a,
1309
                self.uni_a.interface,
1310
                out_vlan_a,
1311
                queue_id=self.queue_id,
1312
            )
1313
            flows_a.append(pop_flow)
1314 1
1315
        uni_flows[self.uni_a.interface.switch.id] = flows_a
1316 1
1317
        # Flows for the second UNI
1318
        flows_z = []
1319 1
1320
        # Flow for one direction, pushing the service tag
1321
        if not skip_in:
1322 1
            if isinstance(in_vlan_z, list):
1323 1
                for in_mask_z in in_vlan_z:
1324 1
                    push_flow = self._prepare_push_flow(
1325 1
                        self.uni_z.interface,
1326
                        endpoint_z,
1327
                        in_mask_z,
1328
                        out_vlan_z,
1329
                        in_vlan_a,
1330
                        queue_id=self.queue_id,
1331
                    )
1332
                    flows_z.append(push_flow)
1333 1
            else:
1334
                push_flow = self._prepare_push_flow(
1335
                    self.uni_z.interface,
1336
                    endpoint_z,
1337
                    in_vlan_z,
1338
                    out_vlan_z,
1339
                    in_vlan_a,
1340
                    queue_id=self.queue_id,
1341
                )
1342
                flows_z.append(push_flow)
1343
1344
        # Flow for the other direction, popping the service tag
1345
        if not skip_out:
1346 1
            pop_flow = self._prepare_pop_flow(
1347 1
                endpoint_z,
1348
                self.uni_z.interface,
1349
                out_vlan_z,
1350
                queue_id=self.queue_id,
1351
            )
1352
            flows_z.append(pop_flow)
1353 1
1354
        uni_flows[self.uni_z.interface.switch.id] = flows_z
1355 1
1356
        return uni_flows
1357 1
1358
    @staticmethod
1359 1
    @retry(
1360 1
        stop=stop_after_attempt(3),
1361
        wait=wait_combine(wait_fixed(3), wait_random(min=2, max=7)),
1362
        retry=retry_if_exception_type(FlowModException),
1363
        before_sleep=before_sleep,
1364
        reraise=True,
1365
    )
1366
    def _send_flow_mods(
1367 1
        data_content: dict,
1368
        command="install",
1369
        force=False,
1370
        by_switch=False
1371
    ):
1372
        """Send a flow_mod list to a specific switch.
1373
1374
        Args:
1375
            dpid(str): The target of flows (i.e. Switch.id).
1376
            flow_mods(dict): Python dictionary with flow_mods.
1377
            command(str): By default is 'flows'. To remove a flow is 'remove'.
1378
            force(bool): True to send via consistency check in case of errors.
1379
            by_switch(bool): True to send to 'flows_by_switch' request instead.
1380
        """
1381
        if by_switch:
1382 1
            endpoint = f"{settings.MANAGER_URL}/flows_by_switch/?force={force}"
1383 1
        else:
1384
            endpoint = f"{settings.MANAGER_URL}/flows"
1385 1
            data_content["force"] = force
1386 1
        try:
1387 1
            if command == "install":
1388 1
                res = httpx.post(endpoint, json=data_content, timeout=30)
1389 1
            elif command == "delete":
1390 1
                res = httpx.request(
1391 1
                    "DELETE", endpoint, json=data_content, timeout=30
1392
                )
1393
        except httpx.RequestError as err:
1394 1
            raise FlowModException(str(err)) from err
1395 1
        if res.is_server_error or res.status_code >= 400:
1396 1
            raise FlowModException(res.text)
1397 1
1398
    def get_cookie(self):
1399 1
        """Return the cookie integer from evc id."""
1400
        return int(self.id, 16) + (settings.COOKIE_PREFIX << 56)
1401 1
1402
    @staticmethod
1403 1
    def get_id_from_cookie(cookie):
1404 1
        """Return the evc id given a cookie value."""
1405
        evc_id = cookie - (settings.COOKIE_PREFIX << 56)
1406 1
        return f"{evc_id:x}".zfill(14)
1407 1
1408
    def set_flow_table_group_id(self, flow_mod: dict, vlan) -> dict:
1409 1
        """Set table_group and table_id"""
1410
        table_group = "epl" if vlan is None else "evpl"
1411 1
        flow_mod["table_group"] = table_group
1412 1
        flow_mod["table_id"] = self.table_group[table_group]
1413 1
        return flow_mod
1414 1
1415
    @staticmethod
1416 1
    def get_priority(vlan):
1417 1
        """Return priority value depending on vlan value"""
1418
        if isinstance(vlan, list):
1419 1
            return settings.EVPL_SB_PRIORITY
1420 1
        if vlan not in {None, "4096/4096", 0}:
1421 1
            return settings.EVPL_SB_PRIORITY
1422 1
        if vlan == 0:
1423 1
            return settings.UNTAGGED_SB_PRIORITY
1424 1
        if vlan == "4096/4096":
1425 1
            return settings.ANY_SB_PRIORITY
1426 1
        return settings.EPL_SB_PRIORITY
1427 1
1428
    def _prepare_flow_mod(self, in_interface, out_interface,
1429 1
                          queue_id=None, vlan=True):
1430
        """Prepare a common flow mod."""
1431
        default_actions = [
1432 1
            {"action_type": "output", "port": out_interface.port_number}
1433
        ]
1434
        queue_id = settings.QUEUE_ID if queue_id == -1 else queue_id
1435 1
        if queue_id is not None:
1436 1
            default_actions.append(
1437 1
                {"action_type": "set_queue", "queue_id": queue_id}
1438
            )
1439
1440
        flow_mod = {
1441 1
            "match": {"in_port": in_interface.port_number},
1442
            "cookie": self.get_cookie(),
1443
            "actions": default_actions,
1444
            "owner": "mef_eline",
1445
        }
1446
1447
        self.set_flow_table_group_id(flow_mod, vlan)
1448 1
        if self.sb_priority:
1449 1
            flow_mod["priority"] = self.sb_priority
1450 1
        else:
1451
            flow_mod["priority"] = self.get_priority(vlan)
1452 1
        return flow_mod
1453 1
1454
    def _prepare_nni_flow(self, *args, queue_id=None):
1455 1
        """Create NNI flows."""
1456
        in_interface, out_interface, in_vlan, out_vlan = args
1457 1
        flow_mod = self._prepare_flow_mod(
1458 1
            in_interface, out_interface, queue_id
1459
        )
1460
        flow_mod["match"]["dl_vlan"] = in_vlan
1461 1
        new_action = {"action_type": "set_vlan", "vlan_id": out_vlan}
1462 1
        flow_mod["actions"].insert(0, new_action)
1463 1
1464
        return flow_mod
1465 1
1466
    def _prepare_push_flow(self, *args, queue_id=None):
1467 1
        """Prepare push flow.
1468
1469
        Arguments:
1470
            in_interface(str): Interface input.
1471
            out_interface(str): Interface output.
1472
            in_vlan(int,str,None): Vlan input.
1473
            out_vlan(str): Vlan output.
1474
            new_c_vlan(int,str,list,None): New client vlan.
1475
1476
        Return:
1477
            dict: An python dictionary representing a FlowMod
1478
1479
        """
1480
        # assign all arguments
1481
        in_interface, out_interface, in_vlan, out_vlan, new_c_vlan = args
1482 1
        vlan_pri = in_vlan if not isinstance(new_c_vlan, list) else new_c_vlan
1483 1
        flow_mod = self._prepare_flow_mod(
1484 1
            in_interface, out_interface, queue_id, vlan_pri
1485
        )
1486
        # the service tag must be always pushed
1487
        new_action = {"action_type": "set_vlan", "vlan_id": out_vlan}
1488 1
        flow_mod["actions"].insert(0, new_action)
1489 1
1490
        new_action = {"action_type": "push_vlan", "tag_type": "s"}
1491 1
        flow_mod["actions"].insert(0, new_action)
1492 1
1493
        if in_vlan is not None:
1494 1
            # if in_vlan is set, it must be included in the match
1495
            flow_mod["match"]["dl_vlan"] = in_vlan
1496 1
1497
        if (not isinstance(new_c_vlan, list) and in_vlan != new_c_vlan and
1498 1
                new_c_vlan not in self.special_cases):
1499
            # new_in_vlan is an integer but zero, action to set is required
1500
            new_action = {"action_type": "set_vlan", "vlan_id": new_c_vlan}
1501 1
            flow_mod["actions"].insert(0, new_action)
1502 1
1503
        if in_vlan not in self.special_cases and new_c_vlan == 0:
1504 1
            # # new_in_vlan is an integer but zero and new_c_vlan does not
1505
            # a pop action is required
1506
            new_action = {"action_type": "pop_vlan"}
1507 1
            flow_mod["actions"].insert(0, new_action)
1508 1
1509
        elif in_vlan == "4096/4096" and new_c_vlan == 0:
1510 1
            # if in_vlan match with any tags and new_c_vlan does not
1511
            # a pop action is required
1512
            new_action = {"action_type": "pop_vlan"}
1513 1
            flow_mod["actions"].insert(0, new_action)
1514 1
1515
        elif (not in_vlan and
1516 1
                (not isinstance(new_c_vlan, list) and
1517
                 new_c_vlan not in self.special_cases)):
1518
            # new_in_vlan is an integer but zero and in_vlan is not set
1519
            # then it is set now
1520
            new_action = {"action_type": "push_vlan", "tag_type": "c"}
1521 1
            flow_mod["actions"].insert(0, new_action)
1522 1
1523
        return flow_mod
1524 1
1525
    def _prepare_pop_flow(
1526 1
        self, in_interface, out_interface, out_vlan, queue_id=None
1527
    ):
1528
        # pylint: disable=too-many-arguments
1529
        """Prepare pop flow."""
1530
        flow_mod = self._prepare_flow_mod(
1531 1
            in_interface, out_interface, queue_id
1532
        )
1533
        flow_mod["match"]["dl_vlan"] = out_vlan
1534 1
        new_action = {"action_type": "pop_vlan"}
1535 1
        flow_mod["actions"].insert(0, new_action)
1536 1
        return flow_mod
1537 1
1538
    @staticmethod
1539 1
    def run_bulk_sdntraces(
1540 1
        uni_list: list[tuple[Interface, Union[str, int, None]]]
1541
    ) -> dict:
1542
        """Run SDN traces on control plane starting from EVC UNIs."""
1543
        endpoint = f"{settings.SDN_TRACE_CP_URL}/traces"
1544 1
        data = []
1545 1
        for interface, tag_value in uni_list:
1546 1
            data_uni = {
1547 1
                "trace": {
1548
                            "switch": {
1549
                                "dpid": interface.switch.dpid,
1550
                                "in_port": interface.port_number,
1551
                            }
1552
                        }
1553
                }
1554
            if tag_value:
1555 1
                uni_dl_vlan = map_dl_vlan(tag_value)
1556 1
                if uni_dl_vlan:
1557 1
                    data_uni["trace"]["eth"] = {
1558 1
                                            "dl_type": 0x8100,
1559
                                            "dl_vlan": uni_dl_vlan,
1560
                                            }
1561
            data.append(data_uni)
1562 1
        try:
1563 1
            response = httpx.put(endpoint, json=data, timeout=30)
1564 1
        except httpx.TimeoutException as exception:
1565 1
            log.error(f"Request has timed out: {exception}")
1566 1
            return {"result": []}
1567 1
        if response.status_code >= 400:
1568 1
            log.error(f"Failed to run sdntrace-cp: {response.text}")
1569 1
            return {"result": []}
1570 1
        return response.json()
1571 1
1572
    # pylint: disable=too-many-return-statements, too-many-arguments
1573
    @staticmethod
1574 1
    def check_trace(
1575 1
        evc_id: str,
1576
        evc_name: str,
1577
        tag_a: Union[None, int, str],
1578
        tag_z: Union[None, int, str],
1579
        interface_a: Interface,
1580
        interface_z: Interface,
1581
        current_path: list,
1582
        trace_a: list,
1583
        trace_z: list
1584
    ) -> bool:
1585
        """Auxiliar function to check an individual trace"""
1586
        if (
1587 1
            len(trace_a) != len(current_path) + 1
1588
            or not compare_uni_out_trace(tag_z, interface_z, trace_a[-1])
1589
        ):
1590
            log.warning(f"From EVC({evc_id}) named '{evc_name}'. "
1591 1
                        f"Invalid trace from uni_a: {trace_a}")
1592
            return False
1593 1
        if (
1594 1
            len(trace_z) != len(current_path) + 1
1595
            or not compare_uni_out_trace(tag_a, interface_a, trace_z[-1])
1596
        ):
1597
            log.warning(f"From EVC({evc_id}) named '{evc_name}'. "
1598 1
                        f"Invalid trace from uni_z: {trace_z}")
1599
            return False
1600 1
1601
        if not current_path:
1602 1
            return True
1603
1604
        first_link, trace_path_begin, trace_path_end = current_path[0], [], []
1605 1
        if (
1606 1
            first_link.endpoint_a.switch.id == trace_a[0]["dpid"]
1607
        ):
1608
            trace_path_begin, trace_path_end = trace_a, trace_z
1609 1
        elif (
1610 1
            first_link.endpoint_a.switch.id == trace_z[0]["dpid"]
1611
        ):
1612
            trace_path_begin, trace_path_end = trace_z, trace_a
1613 1
        else:
1614
            msg = (
1615
                f"first link {first_link} endpoint_a didn't match the first "
1616
                f"step of trace_a {trace_a} or trace_z {trace_z}"
1617
            )
1618
            log.warning(msg)
1619
            return False
1620
1621
        for link, trace1, trace2 in zip(current_path,
1622 1
                                        trace_path_begin[1:],
1623
                                        trace_path_end[:0:-1]):
1624
            metadata_vlan = None
1625 1
            if link.metadata:
1626 1
                metadata_vlan = glom(link.metadata, 's_vlan.value')
1627 1
            if compare_endpoint_trace(
1628 1
                                        link.endpoint_a,
1629
                                        metadata_vlan,
1630
                                        trace2
1631
                                    ) is False:
1632
                log.warning(f"From EVC({evc_id}) named '{evc_name}'. "
1633 1
                            f"Invalid trace from uni_a: {trace_a}")
1634
                return False
1635 1
            if compare_endpoint_trace(
1636 1
                                        link.endpoint_b,
1637
                                        metadata_vlan,
1638
                                        trace1
1639
                                    ) is False:
1640
                log.warning(f"From EVC({evc_id}) named '{evc_name}'. "
1641 1
                            f"Invalid trace from uni_z: {trace_z}")
1642
                return False
1643 1
1644
        return True
1645 1
1646
    @staticmethod
1647 1
    def check_range(circuit, traces: list) -> bool:
1648 1
        """Check traces when for UNI with TAGRange"""
1649
        check = True
1650 1
        for i, mask in enumerate(circuit.uni_a.user_tag.mask_list):
1651 1
            trace_a = traces[i*2]
1652 1
            trace_z = traces[i*2+1]
1653 1
            check &= EVCDeploy.check_trace(
1654 1
                circuit.id, circuit.name,
1655
                mask, mask,
1656
                circuit.uni_a.interface,
1657
                circuit.uni_z.interface,
1658
                circuit.current_path,
1659
                trace_a, trace_z,
1660
            )
1661
        return check
1662 1
1663
    @staticmethod
1664 1
    def check_list_traces(list_circuits: list) -> dict:
1665 1
        """Check if current_path is deployed comparing with SDN traces."""
1666
        if not list_circuits:
1667 1
            return {}
1668 1
        uni_list = make_uni_list(list_circuits)
1669 1
        traces = EVCDeploy.run_bulk_sdntraces(uni_list)["result"]
1670 1
1671
        if not traces:
1672 1
            return {}
1673 1
1674
        try:
1675 1
            circuits_checked = {}
1676 1
            i = 0
1677 1
            for circuit in list_circuits:
1678 1
                if isinstance(circuit.uni_a.user_tag, TAGRange):
1679 1
                    length = len(circuit.uni_a.user_tag.mask_list)
1680 1
                    circuits_checked[circuit.id] = EVCDeploy.check_range(
1681 1
                        circuit, traces[i:i+length*2]
1682
                    )
1683
                    i += length*2
1684 1
                else:
1685
                    trace_a = traces[i]
1686 1
                    trace_z = traces[i+1]
1687 1
                    tag_a = None
1688 1
                    if circuit.uni_a.user_tag:
1689 1
                        tag_a = circuit.uni_a.user_tag.value
1690 1
                    tag_z = None
1691 1
                    if circuit.uni_z.user_tag:
1692 1
                        tag_z = circuit.uni_z.user_tag.value
1693 1
                    circuits_checked[circuit.id] = EVCDeploy.check_trace(
1694 1
                        circuit.id, circuit.name,
1695
                        tag_a, tag_z,
1696
                        circuit.uni_a.interface,
1697
                        circuit.uni_z.interface,
1698
                        circuit.current_path,
1699
                        trace_a, trace_z
1700
                    )
1701
                    i += 2
1702 1
        except IndexError as err:
1703 1
            log.error(
1704 1
                f"Bulk sdntraces returned fewer items than expected."
1705
                f"Error = {err}"
1706
            )
1707
            return {}
1708 1
1709
        return circuits_checked
1710 1
1711
    @staticmethod
1712 1
    def get_endpoint_by_id(
1713 1
        link: Link,
1714
        id_: str,
1715
        operator: Union[eq, ne]
1716
    ) -> Interface:
1717
        """Return endpoint from link
1718
        either equal(eq) or not equal(ne) to id"""
1719
        if operator(link.endpoint_a.switch.id, id_):
1720 1
            return link.endpoint_a
1721 1
        return link.endpoint_b
1722 1
1723
1724
class LinkProtection(EVCDeploy):
1725 1
    """Class to handle link protection."""
1726
1727
    def is_affected_by_link(self, link=None):
1728 1
        """Verify if the current path is affected by link down event."""
1729
        return self.current_path.is_affected_by_link(link)
1730
1731
    def is_using_primary_path(self):
1732 1
        """Verify if the current deployed path is self.primary_path."""
1733
        return self.current_path == self.primary_path
1734 1
1735
    def is_using_backup_path(self):
1736 1
        """Verify if the current deployed path is self.backup_path."""
1737
        return self.current_path == self.backup_path
1738 1
1739
    def is_using_dynamic_path(self):
1740 1
        """Verify if the current deployed path is dynamic."""
1741
        if (
1742 1
            self.current_path
1743
            and not self.is_using_primary_path()
1744
            and not self.is_using_backup_path()
1745
            and self.current_path.status is EntityStatus.UP
1746
        ):
1747
            return True
1748
        return False
1749 1
1750
    def handle_link_up(self, link=None, interface=None):
1751 1
        """Handle circuit when link up.
1752
1753
        Args:
1754
            link(Link): Link affected by link.up event.
1755
1756
        """
1757
        condition_pairs = [
1758 1
            (
1759
                lambda me: me.is_using_primary_path(),
1760
                lambda _: (True, 'nothing')
1761
            ),
1762
            (
1763
                lambda me: me.is_intra_switch(),
1764
                lambda _: (True, 'nothing')
1765
            ),
1766
            (
1767
                lambda me: me.primary_path.is_affected_by_link(link),
1768
                lambda me: (me.deploy_to_primary_path(), 'redeploy')
1769
            ),
1770
            # For this special case, it reached this point because interface
1771
            # was previously confirmed to be a UNI and both UNI are UP
1772
            (
1773
                lambda me: (me.primary_path.status == EntityStatus.UP
1774
                            and interface),
1775
                lambda me: (me.deploy_to_primary_path(), 'redeploy')
1776
            ),
1777
            (
1778
                lambda me: (me.backup_path.status == EntityStatus.UP
1779
                            and interface),
1780
                lambda me: (me.deploy_to_backup_path(), 'redeploy')
1781
            ),
1782
            # We tried to deploy(primary_path) without success.
1783
            # And in this case is up by some how. Nothing to do.
1784
            (
1785
                lambda me: me.is_using_backup_path(),
1786
                lambda _: (True, 'nothing')
1787
            ),
1788
            (
1789
                lambda me:  me.is_using_dynamic_path(),
1790
                lambda _: (True, 'nothing')
1791
            ),
1792
            # In this case, probably the circuit is not being used and
1793
            # we can move to backup
1794
            (
1795
                lambda me: me.backup_path.is_affected_by_link(link),
1796
                lambda me: (me.deploy_to_backup_path(), 'redeploy')
1797
            ),
1798
            # In this case, the circuit is not being used and we should
1799
            # try a dynamic path
1800
            (
1801
                lambda me: me.dynamic_backup_path and not me.is_active(),
1802
                lambda me: (me.deploy_to_path(), 'redeploy')
1803
            )
1804
        ]
1805
        for predicate, action in condition_pairs:
1806 1
            if not predicate(self):
1807 1
                continue
1808 1
            success, succcess_type = action(self)
1809 1
            if success:
1810 1
                if succcess_type == 'redeploy':
1811 1
                    emit_event(
1812 1
                        self._controller,
1813
                        "redeployed_link_up",
1814
                        content=map_evc_event_content(self)
1815
                    )
1816
                return True
1817 1
        return False
1818 1
1819
    def handle_link_down(self):
1820 1
        """Handle circuit when link down.
1821
1822
        Returns:
1823
            bool: True if the re-deploy was successly otherwise False.
1824
1825
        """
1826
        success = False
1827 1
        if self.is_using_primary_path():
1828 1
            success = self.deploy_to_backup_path()
1829 1
        elif self.is_using_backup_path():
1830 1
            success = self.deploy_to_primary_path()
1831 1
1832
        if not success and self.dynamic_backup_path:
1833 1
            success = self.deploy_to_path()
1834 1
1835
        if success:
1836 1
            log.debug(f"{self} deployed after link down.")
1837 1
        else:
1838
            self.remove_current_flows(sync=False)
1839 1
            self.deactivate()
1840 1
            self.sync()
1841 1
            log.debug(f"Failed to re-deploy {self} after link down.")
1842 1
1843
        return success
1844 1
1845
    def are_unis_active(self) -> bool:
1846 1
        """Determine whether this EVC should be active"""
1847
        interface_a = self.uni_a.interface
1848 1
        interface_z = self.uni_z.interface
1849 1
        active, _ = self.is_uni_interface_active(interface_a, interface_z)
1850 1
        return active
1851 1
1852
    def try_to_handle_uni_as_link_up(self, interface: Interface) -> bool:
1853 1
        """Try to handle UNI as link_up to trigger deployment."""
1854
        if (
1855
            self.current_path.status != EntityStatus.UP
1856
            and not self.is_intra_switch()
1857
        ):
1858
            succeeded = self.handle_link_up(interface=interface)
1859
            if succeeded:
1860
                msg = (
1861
                    f"Activated {self} due to successful "
1862
                    f"deployment triggered by {interface}"
1863
                )
1864
            else:
1865
                msg = (
1866
                    f"Couldn't activate {self} due to unsuccessful "
1867
                    f"deployment triggered by {interface}"
1868
                )
1869
            log.info(msg)
1870
            return True
1871
        return False
1872
1873
    def handle_interface_link_up(self, interface: Interface):
1874 1
        """
1875
        Handler for interface link_up events
1876
        """
1877
        if not _does_uni_affect_evc(self, interface, "up"):
1878 1
            return
1879 1
        if self.try_to_handle_uni_as_link_up(interface):
1880 1
            return
1881
1882
        interface_dicts = {
1883 1
            interface.id: {
1884
                'status': interface.status.value,
1885
                'status_reason': interface.status_reason,
1886
            }
1887
            for interface in (self.uni_a.interface, self.uni_z.interface)
1888
        }
1889
        try:
1890 1
            self.try_to_activate()
1891 1
            log.info(
1892 1
                f"Activating {self}. Interfaces: "
1893
                f"{interface_dicts}."
1894
            )
1895
            emit_event(self._controller, "uni_active_updated",
1896 1
                       content=map_evc_event_content(self))
1897
            self.sync()
1898 1
        except ActivationError as exc:
1899
            # On this ctx, no ActivationError isn't expected since the
1900
            # activation pre-requisites states were checked, so handled as err
1901
            log.error(f"ActivationError: {str(exc)} when handling {interface}")
1902
1903
    def handle_interface_link_down(self, interface):
1904 1
        """
1905
        Handler for interface link_down events
1906
        """
1907
        if not _does_uni_affect_evc(self, interface, "down"):
1908 1
            return
1909 1
        interface_dicts = {
1910 1
            interface.id: {
1911
                'status': interface.status.value,
1912
                'status_reason': interface.status_reason,
1913
            }
1914
            for interface in (self.uni_a.interface, self.uni_z.interface)
1915
            if interface.status != EntityStatus.UP
1916
        }
1917
        self.deactivate()
1918 1
        log.info(
1919 1
            f"Deactivating {self}. Interfaces: "
1920
            f"{interface_dicts}."
1921
        )
1922
        emit_event(self._controller, "uni_active_updated",
1923 1
                   content=map_evc_event_content(self))
1924
        self.sync()
1925 1
1926
1927
class EVC(LinkProtection):
1928
    """Class that represents a E-Line Virtual Connection."""
1929