-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_loader.py
More file actions
176 lines (155 loc) · 7.36 KB
/
config_loader.py
File metadata and controls
176 lines (155 loc) · 7.36 KB
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
class Loader:
MIN_PORT = 1024
MAX_PORT = 64000
MIN_ROUTER_ID = 1
MAX_ROUTER_ID = 64000
INFINITY = 16
DEFAULT_UPDATE_PERIOD = 30
TIMEOUT_UPDATE_RATIO = 6
DELETION_UPDATE_RATIO = 4
def __init__(self, config_lines, router):
self.config_lines = config_lines
self.line_number = 1
# Map configuration settings to processing functions.
self.config_functions = {
"router-id": self.process_router_id,
"input-ports": self.process_input_ports,
"outputs": self.process_outputs,
"update-period": self.process_update_period
}
self.router = router
def load(self):
""" Load the configuration and set the router's variables """
for line in self.config_lines:
line = " ".join(line.split()) # Remove all leading, trailing, and consecutive whitespace.
# Ignore any lines that are comments.
if line and line[0] != "#":
parts = line.split(" ")
if parts[0] not in self.config_functions:
print("Unknown config setting: " + parts[0])
else:
# Process the line using the relevant function.
try:
self.config_functions[parts[0]](line)
except ValueError as value_error:
print("Error in configuration file on line", self.line_number)
print(value_error)
print()
exit(11)
self.line_number += 1
if self.router.update_period is None:
self.router.update_period = self.DEFAULT_UPDATE_PERIOD
self.process_timeouts()
if all([self.router.id, self.router.input_ports, self.router.outputs]):
print("Configuration loaded!")
print(self.get_pretty_config_values())
else:
print("Error in configuration file")
print("Incomplete configuration, 'router-id', 'input-ports', and 'outputs' required")
print(self.get_pretty_config_values())
print()
exit(10)
def get_pretty_config_values(self, full_config=True):
""" Get the router's values, obtained from the config file, in a nice format. """
values = "-" * 40 + "\n"
config_values = [
("Router ID", self.router.id)
]
if full_config:
config_values += [
("Input Ports", self.router.input_ports),
("Output Routers", self.router.outputs),
("Update Period", self.router.update_period),
("Timeout Length", self.router.timeout_length),
("Garbage Collection Timeout Length", self.router.deletion_length)
]
values += "\n".join([title + ": " + str(value) for title, value in config_values])
values += "\n" + "-" * 40
return values
def process_router_id(self, line):
""" Set the router's ID. """
parts = line.split(" ")
if len(parts) > 2:
raise ValueError("Invalid router-id: '" + " ".join(parts[1:]) + "', too many arguments")
elif len(parts) < 2:
raise ValueError("No router-id given")
self.router.id = self.validate_router_id(parts[1].strip())
def process_input_ports(self, line):
""" Set the input-ports for the router. """
parts = " ".join(line.split(" ")[1:]).split(",") # Remove 'input-ports' and split on commas.
if not any(parts):
raise ValueError("No input-ports given")
for port in parts:
port = port.strip()
self.router.input_ports.append(self.validate_port(port))
def process_outputs(self, line):
""" Set and format neighbor routers (outputs) and their costs/router-ids. """
parts = " ".join(line.split(" ")[1:]).split(",") # Remove 'outputs' and split on commas.
if not any(parts):
raise ValueError("No outputs given")
for output in parts:
output = output.strip()
output_parts = output.split("/")
if len(output_parts) != 3:
raise ValueError("Invalid output: '" + output + "', usage: port/cost/router-id")
output_port = self.validate_port(output_parts[0])
output_router_cost = self.validate_cost(output_parts[1])
output_router_id = self.validate_router_id(output_parts[2])
self.router.outputs[output_router_id] = (output_port, output_router_cost)
def process_update_period(self, line):
""" Set periodic update time and timeout duration for the router. """
parts = line.split(" ")
if len(parts) > 2:
raise ValueError("Invalid update-period: '" + " ".join(parts[1:]) + "', too many arguments")
elif len(parts) < 2:
raise ValueError("No update-period given")
self.router.update_period = self.validate_update_period(parts[1].strip())
self.process_timeouts()
def process_timeouts(self):
self.router.timeout_length = self.router.update_period * self.TIMEOUT_UPDATE_RATIO
deletion_after_timeout = self.router.update_period * self.DELETION_UPDATE_RATIO
self.router.deletion_length = self.router.timeout_length + deletion_after_timeout
def validate_router_id(self, router_id):
""" Validate a router-id. """
router_id = router_id.strip()
if not all([True if c.isdigit() else False for c in str(router_id)]):
raise ValueError("Invalid router-id '" + str(router_id) + "', not a positive integer")
router_id = int(router_id)
if router_id < self.MIN_ROUTER_ID or router_id > self.MAX_ROUTER_ID:
raise ValueError(
"Invalid router-id: '" + str(router_id) + "', not in range {}-{}".format(
self.MIN_ROUTER_ID, self.MAX_ROUTER_ID
)
)
return router_id
def validate_port(self, port):
""" Validate a port number. """
port = port.strip()
if not port or not all([True if c.isdigit() else False for c in str(port)]):
raise ValueError("Invalid port: '" + str(port) + "', not a positive integer")
port = int(port)
if port < self.MIN_PORT or port > self.MAX_PORT:
raise ValueError(
"Invalid port: '" + str(port) + "', not in range " + str(self.MIN_PORT) + "-" + str(self.MAX_PORT))
return port
def validate_cost(self, cost):
""" Validate a router cost. """
cost = cost.strip()
if not all([True if c.isdigit() or c == "-" else False for c in str(cost)]):
raise ValueError("Invalid cost: '" + str(cost) + "', not an integer")
cost = int(cost)
if cost < 0 or cost > self.INFINITY:
raise ValueError(
"Invalid cost '" + str(cost) + "', not in range {}-{}".format(
0, self.INFINITY
)
)
return cost
@staticmethod
def validate_update_period(update_period):
""" Validate a router update_period. """
update_period = update_period.strip()
if not all([True if c.isdigit() else False for c in str(update_period)]):
raise ValueError("Invalid update-period: '" + str(update_period) + "', not a positive integer")
update_period = int(update_period)
return update_period