blob: 588c432b9632852c364b6312b0174c9c1010463a [file] [log] [blame]
Neels Hofmeyr3531a192017-03-28 14:30:28 +02001# osmo_gsm_tester: validate dict structures
2#
3# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Neels Hofmeyr <neels@hofmeyr.de>
6#
7# This program is free software: you can redistribute it and/or modify
Harald Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Neels Hofmeyr3531a192017-03-28 14:30:28 +02009# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte27205342017-06-03 09:51:45 +020015# GNU General Public License for more details.
Neels Hofmeyr3531a192017-03-28 14:30:28 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Neels Hofmeyr3531a192017-03-28 14:30:28 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import re
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020021import os
Neels Hofmeyr3531a192017-03-28 14:30:28 +020022
23from . import log
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020024from . import util
Neels Hofmeyr3531a192017-03-28 14:30:28 +020025
26KEY_RE = re.compile('[a-zA-Z][a-zA-Z0-9_]*')
27IPV4_RE = re.compile('([0-9]{1,3}.){3}[0-9]{1,3}')
28HWADDR_RE = re.compile('([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}')
29IMSI_RE = re.compile('[0-9]{6,15}')
30KI_RE = re.compile('[0-9a-fA-F]{32}')
31MSISDN_RE = re.compile('[0-9]{1,15}')
32
33def match_re(name, regex, val):
34 while True:
35 if not isinstance(val, str):
36 break;
37 if not regex.fullmatch(val):
38 break;
39 return
40 raise ValueError('Invalid %s: %r' % (name, val))
41
42def band(val):
Pau Espin Pedrol05a838e2018-03-27 19:15:41 +020043 if val in ('GSM-900', 'GSM-1800', 'GSM-1900'):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020044 return
45 raise ValueError('Unknown GSM band: %r' % val)
46
47def ipv4(val):
48 match_re('IPv4 address', IPV4_RE, val)
49 els = [int(el) for el in val.split('.')]
50 if not all([el >= 0 and el <= 255 for el in els]):
51 raise ValueError('Invalid IPv4 address: %r' % val)
52
53def hwaddr(val):
54 match_re('hardware address', HWADDR_RE, val)
55
56def imsi(val):
57 match_re('IMSI', IMSI_RE, val)
58
59def ki(val):
60 match_re('KI', KI_RE, val)
61
62def msisdn(val):
63 match_re('MSISDN', MSISDN_RE, val)
64
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +020065def auth_algo(val):
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020066 if val not in util.ENUM_OSMO_AUTH_ALGO:
Neels Hofmeyr0af893c2017-12-14 15:18:05 +010067 raise ValueError('Unknown Authentication Algorithm: %r' % val)
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +020068
Pau Espin Pedrolfa9a6d32017-09-12 15:13:21 +020069def uint(val):
70 n = int(val)
71 if n < 0:
72 raise ValueError('Positive value expected instead of %d' % n)
73
Pau Espin Pedrol8a3a7b52017-11-28 15:50:02 +010074def uint8(val):
75 n = int(val)
76 if n < 0:
77 raise ValueError('Positive value expected instead of %d' % n)
78 if n > 255: # 2^8 - 1
79 raise ValueError('Value %d too big, max value is 255' % n)
80
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +010081def uint16(val):
82 n = int(val)
83 if n < 0:
84 raise ValueError('Positive value expected instead of %d' % n)
85 if n > 65535: # 2^16 - 1
86 raise ValueError('Value %d too big, max value is 65535' % n)
87
Pau Espin Pedrolead79ac2017-09-12 15:19:18 +020088def times(val):
89 n = int(val)
90 if n < 1:
91 raise ValueError('Positive value >0 expected instead of %d' % n)
92
Pau Espin Pedrol57497a62017-08-28 14:21:15 +020093def cipher(val):
94 if val in ('a5_0', 'a5_1', 'a5_2', 'a5_3', 'a5_4', 'a5_5', 'a5_6', 'a5_7'):
95 return
96 raise ValueError('Unknown Cipher value: %r' % val)
97
Pau Espin Pedrolac18fd32017-08-31 18:49:47 +020098def modem_feature(val):
Pau Espin Pedroleae9c902020-03-31 11:12:39 +020099 if val in ('sms', 'gprs', 'voice', 'ussd', 'sim', '2g', '3g', '4g'):
Pau Espin Pedrolac18fd32017-08-31 18:49:47 +0200100 return
101 raise ValueError('Unknown Modem Feature: %r' % val)
102
Pau Espin Pedrolc9b63762018-05-07 01:57:01 +0200103def phy_channel_config(val):
104 if val in ('CCCH', 'CCCH+SDCCH4', 'TCH/F', 'TCH/H', 'SDCCH8', 'PDCH',
105 'TCH/F_PDCH', 'CCCH+SDCCH4+CBCH', 'SDCCH8+CBCH','TCH/F_TCH/H_PDCH'):
106 return
107 raise ValueError('Unknown Physical channel config: %r' % val)
108
Pau Espin Pedrol722e94e2018-08-22 11:01:32 +0200109def channel_allocator(val):
110 if val in ('ascending', 'descending'):
111 return
112 raise ValueError('Unknown Channel Allocator Policy %r' % val)
113
Pau Espin Pedrol4f23ab52018-10-29 11:30:00 +0100114def gprs_mode(val):
115 if val in ('none', 'gprs', 'egprs'):
116 return
117 raise ValueError('Unknown GPRS mode %r' % val)
118
Pau Espin Pedrol5dc24592018-08-27 12:53:41 +0200119def codec(val):
120 if val in ('hr1', 'hr2', 'hr3', 'fr1', 'fr2', 'fr3'):
121 return
122 raise ValueError('Unknown Codec value: %r' % val)
123
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200124def osmo_trx_clock_ref(val):
125 if val in ('internal', 'external', 'gspdo'):
126 return
127 raise ValueError('Unknown OsmoTRX clock reference value: %r' % val)
128
Pau Espin Pedrolb6937712020-02-27 18:02:20 +0100129def lte_transmission_mode(val):
130 n = int(val)
131 if n <= 4:
132 return
133 raise ValueError('LTE Transmission Mode %d not in expected range' % n)
134
Andre Puschmann2dcc4312020-03-28 15:34:00 +0100135def duration(val):
136 if val.isdecimal() or val.endswith('m') or val.endswith('h'):
137 return
138 raise ValueError('Invalid duration value: %r' % val)
139
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200140INT = 'int'
141STR = 'str'
Pau Espin Pedrolfa9a6d32017-09-12 15:13:21 +0200142UINT = 'uint'
Pau Espin Pedrol404e1502017-08-22 11:17:43 +0200143BOOL_STR = 'bool_str'
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200144BAND = 'band'
145IPV4 = 'ipv4'
146HWADDR = 'hwaddr'
147IMSI = 'imsi'
148KI = 'ki'
149MSISDN = 'msisdn'
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200150AUTH_ALGO = 'auth_algo'
Pau Espin Pedrolead79ac2017-09-12 15:19:18 +0200151TIMES='times'
Pau Espin Pedrol57497a62017-08-28 14:21:15 +0200152CIPHER = 'cipher'
Pau Espin Pedrolac18fd32017-08-31 18:49:47 +0200153MODEM_FEATURE = 'modem_feature'
Pau Espin Pedrolc9b63762018-05-07 01:57:01 +0200154PHY_CHAN = 'chan'
Pau Espin Pedrol722e94e2018-08-22 11:01:32 +0200155CHAN_ALLOCATOR = 'chan_allocator'
Pau Espin Pedrol4f23ab52018-10-29 11:30:00 +0100156GPRS_MODE = 'gprs_mode'
Pau Espin Pedrol5dc24592018-08-27 12:53:41 +0200157CODEC = 'codec'
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200158OSMO_TRX_CLOCK_REF = 'osmo_trx_clock_ref'
Pau Espin Pedrolb6937712020-02-27 18:02:20 +0100159LTE_TRANSMISSION_MODE = 'lte_transmission_mode'
Andre Puschmann2dcc4312020-03-28 15:34:00 +0100160DURATION = 'duration'
Pau Espin Pedrolac18fd32017-08-31 18:49:47 +0200161
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200162SCHEMA_TYPES = {
163 INT: int,
164 STR: str,
Pau Espin Pedrolfa9a6d32017-09-12 15:13:21 +0200165 UINT: uint,
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +0200166 BOOL_STR: util.str2bool,
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200167 BAND: band,
168 IPV4: ipv4,
169 HWADDR: hwaddr,
170 IMSI: imsi,
171 KI: ki,
172 MSISDN: msisdn,
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200173 AUTH_ALGO: auth_algo,
Pau Espin Pedrolead79ac2017-09-12 15:19:18 +0200174 TIMES: times,
Pau Espin Pedrol57497a62017-08-28 14:21:15 +0200175 CIPHER: cipher,
Pau Espin Pedrolac18fd32017-08-31 18:49:47 +0200176 MODEM_FEATURE: modem_feature,
Pau Espin Pedrolc9b63762018-05-07 01:57:01 +0200177 PHY_CHAN: phy_channel_config,
Pau Espin Pedrol722e94e2018-08-22 11:01:32 +0200178 CHAN_ALLOCATOR: channel_allocator,
Pau Espin Pedrol4f23ab52018-10-29 11:30:00 +0100179 GPRS_MODE: gprs_mode,
Pau Espin Pedrol5dc24592018-08-27 12:53:41 +0200180 CODEC: codec,
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200181 OSMO_TRX_CLOCK_REF: osmo_trx_clock_ref,
Pau Espin Pedrolb6937712020-02-27 18:02:20 +0100182 LTE_TRANSMISSION_MODE: lte_transmission_mode,
Andre Puschmann2dcc4312020-03-28 15:34:00 +0100183 DURATION: duration,
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200184 }
185
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +0200186def add(dest, src):
187 if util.is_dict(dest):
188 if not util.is_dict(src):
189 raise ValueError('cannot add to dict a value of type: %r' % type(src))
190
191 for key, val in src.items():
192 dest_val = dest.get(key)
193 if dest_val is None:
194 dest[key] = val
195 else:
196 log.ctx(key=key)
197 add(dest_val, val)
198 return
199 if util.is_list(dest):
200 if not util.is_list(src):
201 raise ValueError('cannot add to list a value of type: %r' % type(src))
202 dest.extend(src)
203 return
204 if dest == src:
205 return
206 raise ValueError('cannot add dicts, conflicting items (values %r and %r)'
207 % (dest, src))
208
209def combine(dest, src):
210 if util.is_dict(dest):
211 if not util.is_dict(src):
212 raise ValueError('cannot combine dict with a value of type: %r' % type(src))
213
214 for key, val in src.items():
215 log.ctx(key=key)
216 dest_val = dest.get(key)
217 if dest_val is None:
218 dest[key] = val
219 else:
220 combine(dest_val, val)
221 return
222 if util.is_list(dest):
223 if not util.is_list(src):
224 raise ValueError('cannot combine list with a value of type: %r' % type(src))
225 # Validate that all elements in both lists are of the same type:
226 t = util.list_validate_same_elem_type(src + dest)
227 if t is None:
228 return # both lists are empty, return
229 # For lists of complex objects, we expect them to be sorted lists:
230 if t in (dict, list, tuple):
231 for i in range(len(dest)):
232 log.ctx(idx=i)
233 src_it = src[i] if i < len(src) else util.empty_instance_type(t)
234 combine(dest[i], src_it)
235 for i in range(len(dest), len(src)):
236 log.ctx(idx=i)
237 dest.append(src[i])
238 else: # for lists of basic elements, we handle them as unsorted sets:
239 for elem in src:
240 if elem not in dest:
241 dest.append(elem)
242 return
243 if dest == src:
244 return
245 raise ValueError('cannot combine dicts, conflicting items (values %r and %r)'
246 % (dest, src))
247
248def replicate_times(d):
249 '''
250 replicate items that have a "times" > 1
251
252 'd' is a dict matching WANT_SCHEMA, which is the same as
253 the RESOURCES_SCHEMA, except each entity that can be reserved has a 'times'
254 field added, to indicate how many of those should be reserved.
255 '''
256 d = copy.deepcopy(d)
257 for key, item_list in d.items():
258 idx = 0
259 while idx < len(item_list):
260 item = item_list[idx]
261 times = int(item.pop('times', 1))
262 for j in range(1, times):
263 item_list.insert(idx + j, copy.deepcopy(item))
264 idx += times
265 return d
266
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200267def validate(config, schema):
268 '''Make sure the given config dict adheres to the schema.
269 The schema is a dict of 'dict paths' in dot-notation with permitted
270 value type. All leaf nodes are validated, nesting dicts are implicit.
271
272 validate( { 'a': 123, 'b': { 'b1': 'foo', 'b2': [ 1, 2, 3 ] } },
273 { 'a': int,
274 'b.b1': str,
275 'b.b2[]': int } )
276
277 Raise a ValueError in case the schema is violated.
278 '''
279
280 def validate_item(path, value, schema):
281 want_type = schema.get(path)
282
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +0200283 if util.is_list(value):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200284 if want_type:
285 raise ValueError('config item is a list, should be %r: %r' % (want_type, path))
286 path = path + '[]'
287 want_type = schema.get(path)
288
289 if not want_type:
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +0200290 if util.is_dict(value):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200291 nest(path, value, schema)
292 return
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +0200293 if util.is_list(value) and value:
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200294 for list_v in value:
295 validate_item(path, list_v, schema)
296 return
297 raise ValueError('config item not known: %r' % path)
298
299 if want_type not in SCHEMA_TYPES:
300 raise ValueError('unknown type %r at %r' % (want_type, path))
301
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +0200302 if util.is_dict(value):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200303 raise ValueError('config item is dict but should be a leaf node of type %r: %r'
304 % (want_type, path))
305
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +0200306 if util.is_list(value):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200307 for list_v in value:
308 validate_item(path, list_v, schema)
309 return
310
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200311 log.ctx(path)
312 type_validator = SCHEMA_TYPES.get(want_type)
313 type_validator(value)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200314
315 def nest(parent_path, config, schema):
316 if parent_path:
317 parent_path = parent_path + '.'
318 else:
319 parent_path = ''
320 for k,v in config.items():
321 if not KEY_RE.fullmatch(k):
322 raise ValueError('invalid config key: %r' % k)
323 path = parent_path + k
324 validate_item(path, v, schema)
325
326 nest(None, config, schema)
327
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +0200328def generate_schemas():
329 "Generate supported schemas dynamically from objects"
330 obj_dir = '%s/../obj/' % os.path.dirname(os.path.abspath(__file__))
331 for filename in os.listdir(obj_dir):
332 if not filename.endswith(".py"):
333 continue
334 module_name = 'osmo_gsm_tester.obj.%s' % filename[:-3]
335 util.run_python_file_method(module_name, 'on_register_schemas', False)
336
337
338_RESOURCE_TYPES = ['ip_address', 'arfcn']
339
340_RESOURCES_SCHEMA = {
341 'ip_address[].addr': IPV4,
342 'arfcn[].arfcn': INT,
343 'arfcn[].band': BAND,
344 }
345
346_CONFIG_SCHEMA = {}
347
348_WANT_SCHEMA = None
349_ALL_SCHEMA = None
350
351def register_resource_schema(obj_class_str, obj_attr_dict):
352 """Register schema attributes for a resource type.
353 For instance: register_resource_schema_attributes('modem', {'type': schema.STR, 'ki': schema.KI})
354 """
355 global _RESOURCES_SCHEMA
356 global _RESOURCE_TYPES
357 tmpdict = {}
358 for key, val in obj_attr_dict.items():
359 new_key = '%s[].%s' % (obj_class_str, key)
360 tmpdict[new_key] = val
361 combine(_RESOURCES_SCHEMA, tmpdict)
362 if obj_class_str not in _RESOURCE_TYPES:
363 _RESOURCE_TYPES.append(obj_class_str)
364
365def register_config_schema(obj_class_str, obj_attr_dict):
366 """Register schema attributes to configure all instances of an object class.
367 For instance: register_resource_schema_attributes('bsc', {'net.codec_list[]': schema.CODEC})
368 """
369 global _CONFIG_SCHEMA
370 tmpdict = {}
371 for key, val in obj_attr_dict.items():
372 new_key = '%s.%s' % (obj_class_str, key)
373 tmpdict[new_key] = val
374 combine(_CONFIG_SCHEMA, tmpdict)
375
376def get_resources_schema():
377 return _RESOURCES_SCHEMA;
378
379def get_want_schema():
380 global _WANT_SCHEMA
381 if _WANT_SCHEMA is None:
382 _WANT_SCHEMA = util.dict_add(
383 dict([('%s[].times' % r, TIMES) for r in _RESOURCE_TYPES]),
384 get_resources_schema())
385 return _WANT_SCHEMA
386
387def get_all_schema():
388 global _ALL_SCHEMA
389 if _ALL_SCHEMA is None:
390 want_schema = get_want_schema()
391 _ALL_SCHEMA = util.dict_add({ 'defaults.timeout': STR },
392 dict([('config.%s' % key, val) for key, val in _CONFIG_SCHEMA.items()]),
393 dict([('resources.%s' % key, val) for key, val in want_schema.items()]),
394 dict([('modifiers.%s' % key, val) for key, val in want_schema.items()]))
395 return _ALL_SCHEMA
396
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200397# vim: expandtab tabstop=4 shiftwidth=4