Coverage for flexnetsim/link.py: 100%

61 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-22 20:03 +0000

1import numpy as np 

2 

3 

4class Link(): 

5 """ 

6 The Link class is used to represent a Link between two Nodes in an Optical Fiber Network inside the simulator. 

7 

8 Args: 

9 id (int): The desired Id number used to identify the Link. By default -1. 

10 

11 length (float): The desired length (assumed in meters [m]) of the Link. 

12 

13 slots (int): The desired amount of slots availabel for connection allocations for the Link to have. 

14 """ 

15 

16 def __init__(self, id=-1, length=100.0, slots=320): 

17 self.__id = id 

18 

19 if length <= 0: 

20 raise ValueError("Cannot create a link with non-positive length.") 

21 else: 

22 self.__length = length 

23 

24 if slots < 1: 

25 raise ValueError("Cannot create a link with ", slots, " slots.") 

26 else: 

27 self.__slots = np.zeros(slots, dtype=bool) 

28 

29 self.__src = -1 

30 self.__dst = -1 

31 

32 @property 

33 def id(self): 

34 return self.__id 

35 

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 

43 

44 @property 

45 def length(self): 

46 return self.__length 

47 

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 

53 

54 @property 

55 def slots(self): 

56 return self.__slots 

57 

58 @property 

59 def src(self): 

60 return self.__src 

61 

62 @src.setter 

63 def src(self, src): 

64 self.__src = src 

65 

66 @property 

67 def dst(self): 

68 return self.__dst 

69 

70 @dst.setter 

71 def dst(self, dst): 

72 self.__dst = dst 

73 

74 def slots_number(self): 

75 return len(self.__slots) 

76 

77 def set_slots(self, slots: int): 

78 if (slots < 1): 

79 raise ValueError("Cannot set a link with ", slots, " slots.") 

80 

81 if (np.any(self.__slots)): 

82 raise ValueError( 

83 "Cannot change slots number if at least one slot is active.") 

84 

85 np.resize(self.__slots, slots) 

86 

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] 

91 

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.") 

95 

96 if(self.get_slot(pos) == value): 

97 raise ValueError("Slot already setted in desired state.") 

98 

99 self.__slots[pos] = value