Passed
Pull Request — master (#233)
by Vinicius
08:40 queued 05:16
created

EVCDeploy.deploy_to_primary_path()   A

Complexity

Conditions 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

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