Coverage for flexnetsim/link.py: 100%
61 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-22 20:03 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-22 20:03 +0000
1import numpy as np
4class Link():
5 """
6 The Link class is used to represent a Link between two Nodes in an Optical Fiber Network inside the simulator.
8 Args:
9 id (int): The desired Id number used to identify the Link. By default -1.
11 length (float): The desired length (assumed in meters [m]) of the Link.
13 slots (int): The desired amount of slots availabel for connection allocations for the Link to have.
14 """
16 def __init__(self, id=-1, length=100.0, slots=320):
17 self.__id = id
19 if length <= 0:
20 raise ValueError("Cannot create a link with non-positive length.")
21 else:
22 self.__length = length
24 if slots < 1:
25 raise ValueError("Cannot create a link with ", slots, " slots.")
26 else:
27 self.__slots = np.zeros(slots, dtype=bool)
29 self.__src = -1
30 self.__dst = -1
32 @property
33 def id(self):
34 return self.__id
36 @id.setter
37 def id(self, id):
38 if self.__id != -1:
39 raise ValueError(
40 "Cannot set Id to a Link with Id different than -1.")
41 else:
42 self.__id = id
44 @property
45 def length(self):
46 return self.__length
48 @length.setter
49 def length(self, length):
50 if (length <= 0):
51 raise ValueError("Cannot set a link with non-positive length.")
52 self.__length = length
54 @property
55 def slots(self):
56 return self.__slots
58 @property
59 def src(self):
60 return self.__src
62 @src.setter
63 def src(self, src):
64 self.__src = src
66 @property
67 def dst(self):
68 return self.__dst
70 @dst.setter
71 def dst(self, dst):
72 self.__dst = dst
74 def slots_number(self):
75 return len(self.__slots)
77 def set_slots(self, slots: int):
78 if (slots < 1):
79 raise ValueError("Cannot set a link with ", slots, " slots.")
81 if (np.any(self.__slots)):
82 raise ValueError(
83 "Cannot change slots number if at least one slot is active.")
85 np.resize(self.__slots, slots)
87 def get_slot(self, pos: int):
88 if (pos < 0) or (pos >= self.slots_number()):
89 raise ValueError("Cannot get slot in position out of bounds.")
90 return self.__slots[pos]
92 def set_slot(self, pos: int, value: bool):
93 if (pos < 0 or pos >= self.slots_number()):
94 raise ValueError("Cannot set slot in position out of bounds.")
96 if(self.get_slot(pos) == value):
97 raise ValueError("Slot already setted in desired state.")
99 self.__slots[pos] = value