Test Failed
Pull Request — master (#175)
by Italo Valcy
03:23
created

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

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

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