Test Failed
Pull Request — master (#214)
by
unknown
06:41
created

build.models.evc.EVCBase.get_priority()   A

Complexity

Conditions 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

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