Total Complexity | 6 |
Total Lines | 50 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | """Module with identifier types for Links and Interfaces""" |
||
2 | |||
3 | 1 | import hashlib |
|
4 | 1 | from typing import Tuple |
|
5 | |||
6 | |||
7 | 1 | class InterfaceID(str): |
|
8 | """Interface Identifier""" |
||
9 | |||
10 | 1 | __slots__ = ("switch", "port") |
|
11 | |||
12 | 1 | def __new__(cls, switch: str, port: int): |
|
13 | 1 | return super().__new__(cls, f"{switch}:{port}") |
|
14 | |||
15 | 1 | def __init__(self, switch: str, port: int): |
|
16 | # Used for sorting, but can be accessed |
||
17 | 1 | self.switch = switch |
|
18 | 1 | self.port = port |
|
19 | 1 | super().__init__() |
|
20 | |||
21 | 1 | def __lt__(self, other): |
|
22 | # Ensures that instances are sortable in a way that maintains backwards |
||
23 | # compatibility when creating instances of LinkID |
||
24 | 1 | dpid_a = self.switch |
|
25 | 1 | port_a = self.port |
|
26 | 1 | dpid_b = other.switch |
|
27 | 1 | port_b = other.port |
|
28 | 1 | if dpid_a < dpid_b: |
|
29 | 1 | return True |
|
30 | 1 | return dpid_a == dpid_b and port_a < port_b |
|
31 | |||
32 | |||
33 | 1 | class LinkID(str): |
|
34 | """Link Identifier""" |
||
35 | |||
36 | 1 | def __new__(cls, interface_1: str, interface_2: str): |
|
37 | 1 | intf_1 = InterfaceID(*LinkID.intf_id_to_tuple(interface_1)) |
|
38 | 1 | intf_2 = InterfaceID(*LinkID.intf_id_to_tuple(interface_2)) |
|
39 | 1 | raw_str = ":".join(sorted((intf_1, intf_2))) |
|
40 | 1 | digest = hashlib.sha256(raw_str.encode('utf-8')).hexdigest() |
|
41 | 1 | return super().__new__(cls, digest) |
|
42 | |||
43 | 1 | @staticmethod |
|
44 | 1 | def intf_id_to_tuple(interface_id: str) -> Tuple[str, int]: |
|
45 | """Map interface id as a switch, port tuple.""" |
||
46 | 1 | values = interface_id.split(":") |
|
47 | 1 | switch_id = ":".join(values[:-1]) |
|
48 | 1 | port = int(values[-1]) |
|
49 | return switch_id, port |
||
50 |