blob: 12cfd7ab8dd0dc2ca425781bd69153d07310173c [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
Neels Hofmeyr0af893c2017-12-14 15:18:05 +010023from .util import is_dict, is_list, str2bool, ENUM_OSMO_AUTH_ALGO
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):
Pau Espin Pedrol05a838e2018-03-27 19:15:41 +020042 if val in ('GSM-900', 'GSM-1800', 'GSM-1900'):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020043 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
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +020064def auth_algo(val):
Neels Hofmeyr0af893c2017-12-14 15:18:05 +010065 if val not in ENUM_OSMO_AUTH_ALGO:
66 raise ValueError('Unknown Authentication Algorithm: %r' % val)
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +020067
Pau Espin Pedrolfa9a6d32017-09-12 15:13:21 +020068def uint(val):
69 n = int(val)
70 if n < 0:
71 raise ValueError('Positive value expected instead of %d' % n)
72
Pau Espin Pedrol8a3a7b52017-11-28 15:50:02 +010073def uint8(val):
74 n = int(val)
75 if n < 0:
76 raise ValueError('Positive value expected instead of %d' % n)
77 if n > 255: # 2^8 - 1
78 raise ValueError('Value %d too big, max value is 255' % n)
79
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +010080def uint16(val):
81 n = int(val)
82 if n < 0:
83 raise ValueError('Positive value expected instead of %d' % n)
84 if n > 65535: # 2^16 - 1
85 raise ValueError('Value %d too big, max value is 65535' % n)
86
Pau Espin Pedrolead79ac2017-09-12 15:19:18 +020087def times(val):
88 n = int(val)
89 if n < 1:
90 raise ValueError('Positive value >0 expected instead of %d' % n)
91
Pau Espin Pedrol57497a62017-08-28 14:21:15 +020092def cipher(val):
93 if val in ('a5_0', 'a5_1', 'a5_2', 'a5_3', 'a5_4', 'a5_5', 'a5_6', 'a5_7'):
94 return
95 raise ValueError('Unknown Cipher value: %r' % val)
96
Pau Espin Pedrolac18fd32017-08-31 18:49:47 +020097def modem_feature(val):
Pau Espin Pedrolbfd0b232018-03-13 18:32:57 +010098 if val in ('sms', 'gprs', 'voice', 'ussd', 'sim'):
Pau Espin Pedrolac18fd32017-08-31 18:49:47 +020099 return
100 raise ValueError('Unknown Modem Feature: %r' % val)
101
Pau Espin Pedrolc9b63762018-05-07 01:57:01 +0200102def phy_channel_config(val):
103 if val in ('CCCH', 'CCCH+SDCCH4', 'TCH/F', 'TCH/H', 'SDCCH8', 'PDCH',
104 'TCH/F_PDCH', 'CCCH+SDCCH4+CBCH', 'SDCCH8+CBCH','TCH/F_TCH/H_PDCH'):
105 return
106 raise ValueError('Unknown Physical channel config: %r' % val)
107
Pau Espin Pedrol722e94e2018-08-22 11:01:32 +0200108def channel_allocator(val):
109 if val in ('ascending', 'descending'):
110 return
111 raise ValueError('Unknown Channel Allocator Policy %r' % val)
112
Pau Espin Pedrol5dc24592018-08-27 12:53:41 +0200113def codec(val):
114 if val in ('hr1', 'hr2', 'hr3', 'fr1', 'fr2', 'fr3'):
115 return
116 raise ValueError('Unknown Codec value: %r' % val)
117
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200118def osmo_trx_clock_ref(val):
119 if val in ('internal', 'external', 'gspdo'):
120 return
121 raise ValueError('Unknown OsmoTRX clock reference value: %r' % val)
122
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200123INT = 'int'
124STR = 'str'
Pau Espin Pedrolfa9a6d32017-09-12 15:13:21 +0200125UINT = 'uint'
Pau Espin Pedrol404e1502017-08-22 11:17:43 +0200126BOOL_STR = 'bool_str'
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200127BAND = 'band'
128IPV4 = 'ipv4'
129HWADDR = 'hwaddr'
130IMSI = 'imsi'
131KI = 'ki'
132MSISDN = 'msisdn'
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200133AUTH_ALGO = 'auth_algo'
Pau Espin Pedrolead79ac2017-09-12 15:19:18 +0200134TIMES='times'
Pau Espin Pedrol57497a62017-08-28 14:21:15 +0200135CIPHER = 'cipher'
Pau Espin Pedrolac18fd32017-08-31 18:49:47 +0200136MODEM_FEATURE = 'modem_feature'
Pau Espin Pedrolc9b63762018-05-07 01:57:01 +0200137PHY_CHAN = 'chan'
Pau Espin Pedrol722e94e2018-08-22 11:01:32 +0200138CHAN_ALLOCATOR = 'chan_allocator'
Pau Espin Pedrol5dc24592018-08-27 12:53:41 +0200139CODEC = 'codec'
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200140OSMO_TRX_CLOCK_REF = 'osmo_trx_clock_ref'
Pau Espin Pedrolac18fd32017-08-31 18:49:47 +0200141
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200142SCHEMA_TYPES = {
143 INT: int,
144 STR: str,
Pau Espin Pedrolfa9a6d32017-09-12 15:13:21 +0200145 UINT: uint,
Pau Espin Pedrol404e1502017-08-22 11:17:43 +0200146 BOOL_STR: str2bool,
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200147 BAND: band,
148 IPV4: ipv4,
149 HWADDR: hwaddr,
150 IMSI: imsi,
151 KI: ki,
152 MSISDN: msisdn,
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200153 AUTH_ALGO: auth_algo,
Pau Espin Pedrolead79ac2017-09-12 15:19:18 +0200154 TIMES: times,
Pau Espin Pedrol57497a62017-08-28 14:21:15 +0200155 CIPHER: cipher,
Pau Espin Pedrolac18fd32017-08-31 18:49:47 +0200156 MODEM_FEATURE: modem_feature,
Pau Espin Pedrolc9b63762018-05-07 01:57:01 +0200157 PHY_CHAN: phy_channel_config,
Pau Espin Pedrol722e94e2018-08-22 11:01:32 +0200158 CHAN_ALLOCATOR: channel_allocator,
Pau Espin Pedrol5dc24592018-08-27 12:53:41 +0200159 CODEC: codec,
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200160 OSMO_TRX_CLOCK_REF: osmo_trx_clock_ref,
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200161 }
162
163def validate(config, schema):
164 '''Make sure the given config dict adheres to the schema.
165 The schema is a dict of 'dict paths' in dot-notation with permitted
166 value type. All leaf nodes are validated, nesting dicts are implicit.
167
168 validate( { 'a': 123, 'b': { 'b1': 'foo', 'b2': [ 1, 2, 3 ] } },
169 { 'a': int,
170 'b.b1': str,
171 'b.b2[]': int } )
172
173 Raise a ValueError in case the schema is violated.
174 '''
175
176 def validate_item(path, value, schema):
177 want_type = schema.get(path)
178
179 if is_list(value):
180 if want_type:
181 raise ValueError('config item is a list, should be %r: %r' % (want_type, path))
182 path = path + '[]'
183 want_type = schema.get(path)
184
185 if not want_type:
186 if is_dict(value):
187 nest(path, value, schema)
188 return
189 if is_list(value) and value:
190 for list_v in value:
191 validate_item(path, list_v, schema)
192 return
193 raise ValueError('config item not known: %r' % path)
194
195 if want_type not in SCHEMA_TYPES:
196 raise ValueError('unknown type %r at %r' % (want_type, path))
197
198 if is_dict(value):
199 raise ValueError('config item is dict but should be a leaf node of type %r: %r'
200 % (want_type, path))
201
202 if is_list(value):
203 for list_v in value:
204 validate_item(path, list_v, schema)
205 return
206
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200207 log.ctx(path)
208 type_validator = SCHEMA_TYPES.get(want_type)
209 type_validator(value)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200210
211 def nest(parent_path, config, schema):
212 if parent_path:
213 parent_path = parent_path + '.'
214 else:
215 parent_path = ''
216 for k,v in config.items():
217 if not KEY_RE.fullmatch(k):
218 raise ValueError('invalid config key: %r' % k)
219 path = parent_path + k
220 validate_item(path, v, schema)
221
222 nest(None, config, schema)
223
224# vim: expandtab tabstop=4 shiftwidth=4