blob: 4c9b9cd6ea0a2098a8395c60741e068f2e3a468e [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
21
22from . import log
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020023from .util import is_dict, is_list, str2bool
Neels Hofmeyr3531a192017-03-28 14:30:28 +020024
25KEY_RE = re.compile('[a-zA-Z][a-zA-Z0-9_]*')
26IPV4_RE = re.compile('([0-9]{1,3}.){3}[0-9]{1,3}')
27HWADDR_RE = re.compile('([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}')
28IMSI_RE = re.compile('[0-9]{6,15}')
29KI_RE = re.compile('[0-9a-fA-F]{32}')
30MSISDN_RE = re.compile('[0-9]{1,15}')
31
32def match_re(name, regex, val):
33 while True:
34 if not isinstance(val, str):
35 break;
36 if not regex.fullmatch(val):
37 break;
38 return
39 raise ValueError('Invalid %s: %r' % (name, val))
40
41def band(val):
42 if val in ('GSM-1800', 'GSM-1900'):
43 return
44 raise ValueError('Unknown GSM band: %r' % val)
45
46def ipv4(val):
47 match_re('IPv4 address', IPV4_RE, val)
48 els = [int(el) for el in val.split('.')]
49 if not all([el >= 0 and el <= 255 for el in els]):
50 raise ValueError('Invalid IPv4 address: %r' % val)
51
52def hwaddr(val):
53 match_re('hardware address', HWADDR_RE, val)
54
55def imsi(val):
56 match_re('IMSI', IMSI_RE, val)
57
58def ki(val):
59 match_re('KI', KI_RE, val)
60
61def msisdn(val):
62 match_re('MSISDN', MSISDN_RE, val)
63
64INT = 'int'
65STR = 'str'
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020066BOOL_STR = 'bool_str'
Neels Hofmeyr3531a192017-03-28 14:30:28 +020067BAND = 'band'
68IPV4 = 'ipv4'
69HWADDR = 'hwaddr'
70IMSI = 'imsi'
71KI = 'ki'
72MSISDN = 'msisdn'
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020073TRX_REMOTE_IP = 'trx_remote_ip'
Neels Hofmeyr3531a192017-03-28 14:30:28 +020074SCHEMA_TYPES = {
75 INT: int,
76 STR: str,
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020077 BOOL_STR: str2bool,
Neels Hofmeyr3531a192017-03-28 14:30:28 +020078 BAND: band,
79 IPV4: ipv4,
80 HWADDR: hwaddr,
81 IMSI: imsi,
82 KI: ki,
83 MSISDN: msisdn,
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020084 TRX_REMOTE_IP: ipv4,
Neels Hofmeyr3531a192017-03-28 14:30:28 +020085 }
86
87def validate(config, schema):
88 '''Make sure the given config dict adheres to the schema.
89 The schema is a dict of 'dict paths' in dot-notation with permitted
90 value type. All leaf nodes are validated, nesting dicts are implicit.
91
92 validate( { 'a': 123, 'b': { 'b1': 'foo', 'b2': [ 1, 2, 3 ] } },
93 { 'a': int,
94 'b.b1': str,
95 'b.b2[]': int } )
96
97 Raise a ValueError in case the schema is violated.
98 '''
99
100 def validate_item(path, value, schema):
101 want_type = schema.get(path)
102
103 if is_list(value):
104 if want_type:
105 raise ValueError('config item is a list, should be %r: %r' % (want_type, path))
106 path = path + '[]'
107 want_type = schema.get(path)
108
109 if not want_type:
110 if is_dict(value):
111 nest(path, value, schema)
112 return
113 if is_list(value) and value:
114 for list_v in value:
115 validate_item(path, list_v, schema)
116 return
117 raise ValueError('config item not known: %r' % path)
118
119 if want_type not in SCHEMA_TYPES:
120 raise ValueError('unknown type %r at %r' % (want_type, path))
121
122 if is_dict(value):
123 raise ValueError('config item is dict but should be a leaf node of type %r: %r'
124 % (want_type, path))
125
126 if is_list(value):
127 for list_v in value:
128 validate_item(path, list_v, schema)
129 return
130
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200131 log.ctx(path)
132 type_validator = SCHEMA_TYPES.get(want_type)
133 type_validator(value)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200134
135 def nest(parent_path, config, schema):
136 if parent_path:
137 parent_path = parent_path + '.'
138 else:
139 parent_path = ''
140 for k,v in config.items():
141 if not KEY_RE.fullmatch(k):
142 raise ValueError('invalid config key: %r' % k)
143 path = parent_path + k
144 validate_item(path, v, schema)
145
146 nest(None, config, schema)
147
148# vim: expandtab tabstop=4 shiftwidth=4