Passed
Pull Request — master (#433)
by
unknown
03:42
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 25
CRAP Score 12.3775

Importance

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