-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_data.py
179 lines (153 loc) · 5.17 KB
/
validate_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#########################################################
#
# A function to validate that the data from the config
# file is valid. Will print an error message,
# and return an error code to calling program
#
# Version 01; 16 April 2019
# First pass
# Help from ID for the checking a set length idea
# Needs checking output ports, metric
# Design decision to seperate the config file parser
# and validation of the read data
#
# Version 02;
# reference material https://www.programiz.com/
# python-programming/methods/set/isdisjoint
# Fixed a bunch of for and if statement format errors
#
# Version 03: 17 April 2019
# more tidy up
#
# Version 04: 18 April 2019
# work on addtional validation
# changed error constants to variables - they are not
# constants, just variables and initialisation, and
# moved into module defintition
#
# Version 05:
# Adding doc tests:
# Router ID = sort of OK
# Moved the output-ports checking (port, metric, id)
# into signle for loop
#
# Version 06: 19 April 2019
# Removed the metric set (since we can have the same
# metric more than once) and checkng of it. All we
# need to check is the metric range is 1-15.
# Fixed issue with timer checking.
# Move the port reuse error code check to after the
# metric and id error code checks. (When break out of
# the for loop, the sets do not match)
# Need to check for empty lists being passed in
#
# Version 07: 19 April 2019:
# Switched to returning Boolean values
#
# Version 08: 20 April 2019:
# Moved doctests to their own unit test module
#########################################################
from routerbase import logger
# Router ID limits
MIN_ID = 1
MAX_ID = 64000
# Input / Output port limits
MIN_PORT = 1024
MAX_PORT = 64000
# Metric limits
INFINITY = 16
MIN_METRIC = 1
MAX_METRIC = INFINITY
# Timer ratios
PERIODIC_DEAD_RATIO = 6
PERIODIC_GARBAGE_RATIO = 4
def validate_data(router_id, input_ports, output_ports, timers):
id_error = False
input_port_error = False
output_port_error = False
metric_error = False
timers_error = False
# Check Router ID
if router_id is None:
id_error = True
else:
if (router_id < MIN_ID) or (router_id > MAX_ID):
id_error = True
if id_error:
logger("Router ID Configuration Error")
return False
# Check input ports
temp_input_set = set()
if input_ports is None:
input_port_error = True
else:
for a_port in input_ports:
if (a_port < MIN_PORT) or (a_port > MAX_PORT):
input_port_error = True
break
temp_input_set.add(a_port) # add port to a temporary list
# set error error if the length of temporary list
# is not the same as length of original port list
if input_ports is None or len(temp_input_set) != len(input_ports):
input_port_error = True
if input_port_error:
logger("Input Ports Configuration Error")
return False
# Check output ports
temp_output_port_set = set()
temp_id_set = set()
# temp_metric_set = set()
if output_ports is None:
logger("Output Ports Configuration Error: No output ports were given")
return False
else:
for an_item in output_ports:
# check port range
if (an_item[0] < MIN_PORT) or (an_item[0] > MAX_PORT):
logger("Output Ports Configuration Error: Port out of range")
return False
break
temp_output_port_set.add(an_item[0])
# check metric
if (an_item[1] < MIN_METRIC) or (an_item[1] > MAX_METRIC):
metric_error = True
break
# temp_metric_set.add(an_item[1])
# check id
if (an_item[2] < MIN_ID) or (an_item[2] > MAX_ID):
id_error = True
break
temp_id_set.add(an_item[2])
# if length of set of ports not same as the length of output-ports, hv error
if len(temp_output_port_set) != len(output_ports):
output_port_error = True
# check the set of output ports with set of input ports, if they
# are not disjoint (i.e. element(s) in common) we have error
if (temp_output_port_set.isdisjoint(temp_input_set)) is False:
output_port_error = True
if metric_error:
logger("Output Ports Configuration Error: Cost / Metric")
return False
# we might need this i.e. check if a cost / metric is missing?
# if len(temp_metric_set) != len(output_ports(2)):
# id_error = True
# do we need a check for missing ID?
if id_error:
logger("Output Ports Configuration Error: ID")
return False
if output_port_error:
logger("Output Ports Configuration Error: Port number re-use")
return False
# Check Timers
if len(timers) != 3:
timers_error = True
else:
if (timers[1] / timers[0]) != PERIODIC_DEAD_RATIO:
timers_error = True
if (timers[2] / timers[0]) != PERIODIC_GARBAGE_RATIO:
timers_error = True
if timers_error:
logger("Timers Configuration Error")
return False
# All good, yay! return a zero
return True