Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 1 | # 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 Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 8 | # it under the terms of the GNU General Public License as |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 9 | # 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 Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 15 | # GNU General Public License for more details. |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 16 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU General Public License |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import re |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 21 | import os |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 22 | |
| 23 | from . import log |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 24 | from . import util |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 25 | |
Pau Espin Pedrol | e0b8990 | 2020-05-06 16:28:01 +0200 | [diff] [blame] | 26 | KEY_RE = re.compile('[a-zA-Z0-9][a-zA-Z0-9_]*') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 27 | IPV4_RE = re.compile('([0-9]{1,3}.){3}[0-9]{1,3}') |
| 28 | HWADDR_RE = re.compile('([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}') |
| 29 | IMSI_RE = re.compile('[0-9]{6,15}') |
| 30 | KI_RE = re.compile('[0-9a-fA-F]{32}') |
| 31 | MSISDN_RE = re.compile('[0-9]{1,15}') |
| 32 | |
| 33 | def 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 | |
| 42 | def band(val): |
Pau Espin Pedrol | 05a838e | 2018-03-27 19:15:41 +0200 | [diff] [blame] | 43 | if val in ('GSM-900', 'GSM-1800', 'GSM-1900'): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 44 | return |
| 45 | raise ValueError('Unknown GSM band: %r' % val) |
| 46 | |
| 47 | def 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 | |
| 53 | def hwaddr(val): |
| 54 | match_re('hardware address', HWADDR_RE, val) |
| 55 | |
| 56 | def imsi(val): |
| 57 | match_re('IMSI', IMSI_RE, val) |
| 58 | |
| 59 | def ki(val): |
| 60 | match_re('KI', KI_RE, val) |
| 61 | |
| 62 | def msisdn(val): |
| 63 | match_re('MSISDN', MSISDN_RE, val) |
| 64 | |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 65 | def auth_algo(val): |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 66 | if val not in util.ENUM_OSMO_AUTH_ALGO: |
Neels Hofmeyr | 0af893c | 2017-12-14 15:18:05 +0100 | [diff] [blame] | 67 | raise ValueError('Unknown Authentication Algorithm: %r' % val) |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 68 | |
Pau Espin Pedrol | fa9a6d3 | 2017-09-12 15:13:21 +0200 | [diff] [blame] | 69 | def uint(val): |
| 70 | n = int(val) |
| 71 | if n < 0: |
| 72 | raise ValueError('Positive value expected instead of %d' % n) |
| 73 | |
Pau Espin Pedrol | 8a3a7b5 | 2017-11-28 15:50:02 +0100 | [diff] [blame] | 74 | def 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 Pedrol | 5e0c251 | 2017-11-06 18:40:23 +0100 | [diff] [blame] | 81 | def 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 Pedrol | ead79ac | 2017-09-12 15:19:18 +0200 | [diff] [blame] | 88 | def times(val): |
| 89 | n = int(val) |
| 90 | if n < 1: |
| 91 | raise ValueError('Positive value >0 expected instead of %d' % n) |
| 92 | |
Pau Espin Pedrol | 57497a6 | 2017-08-28 14:21:15 +0200 | [diff] [blame] | 93 | def 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 Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 98 | def modem_feature(val): |
Pau Espin Pedrol | eae9c90 | 2020-03-31 11:12:39 +0200 | [diff] [blame] | 99 | if val in ('sms', 'gprs', 'voice', 'ussd', 'sim', '2g', '3g', '4g'): |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 100 | return |
| 101 | raise ValueError('Unknown Modem Feature: %r' % val) |
| 102 | |
Pau Espin Pedrol | c9b6376 | 2018-05-07 01:57:01 +0200 | [diff] [blame] | 103 | def 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 Pedrol | 722e94e | 2018-08-22 11:01:32 +0200 | [diff] [blame] | 109 | def channel_allocator(val): |
| 110 | if val in ('ascending', 'descending'): |
| 111 | return |
| 112 | raise ValueError('Unknown Channel Allocator Policy %r' % val) |
| 113 | |
Pau Espin Pedrol | 4f23ab5 | 2018-10-29 11:30:00 +0100 | [diff] [blame] | 114 | def gprs_mode(val): |
| 115 | if val in ('none', 'gprs', 'egprs'): |
| 116 | return |
| 117 | raise ValueError('Unknown GPRS mode %r' % val) |
| 118 | |
Pau Espin Pedrol | 5dc2459 | 2018-08-27 12:53:41 +0200 | [diff] [blame] | 119 | def 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 Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 124 | def 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 Pedrol | b693771 | 2020-02-27 18:02:20 +0100 | [diff] [blame] | 129 | def 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 Puschmann | 2dcc431 | 2020-03-28 15:34:00 +0100 | [diff] [blame] | 135 | def 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 Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 140 | INT = 'int' |
| 141 | STR = 'str' |
Pau Espin Pedrol | fa9a6d3 | 2017-09-12 15:13:21 +0200 | [diff] [blame] | 142 | UINT = 'uint' |
Pau Espin Pedrol | 404e150 | 2017-08-22 11:17:43 +0200 | [diff] [blame] | 143 | BOOL_STR = 'bool_str' |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 144 | BAND = 'band' |
| 145 | IPV4 = 'ipv4' |
| 146 | HWADDR = 'hwaddr' |
| 147 | IMSI = 'imsi' |
| 148 | KI = 'ki' |
| 149 | MSISDN = 'msisdn' |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 150 | AUTH_ALGO = 'auth_algo' |
Pau Espin Pedrol | ead79ac | 2017-09-12 15:19:18 +0200 | [diff] [blame] | 151 | TIMES='times' |
Pau Espin Pedrol | 57497a6 | 2017-08-28 14:21:15 +0200 | [diff] [blame] | 152 | CIPHER = 'cipher' |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 153 | MODEM_FEATURE = 'modem_feature' |
Pau Espin Pedrol | c9b6376 | 2018-05-07 01:57:01 +0200 | [diff] [blame] | 154 | PHY_CHAN = 'chan' |
Pau Espin Pedrol | 722e94e | 2018-08-22 11:01:32 +0200 | [diff] [blame] | 155 | CHAN_ALLOCATOR = 'chan_allocator' |
Pau Espin Pedrol | 4f23ab5 | 2018-10-29 11:30:00 +0100 | [diff] [blame] | 156 | GPRS_MODE = 'gprs_mode' |
Pau Espin Pedrol | 5dc2459 | 2018-08-27 12:53:41 +0200 | [diff] [blame] | 157 | CODEC = 'codec' |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 158 | OSMO_TRX_CLOCK_REF = 'osmo_trx_clock_ref' |
Pau Espin Pedrol | b693771 | 2020-02-27 18:02:20 +0100 | [diff] [blame] | 159 | LTE_TRANSMISSION_MODE = 'lte_transmission_mode' |
Andre Puschmann | 2dcc431 | 2020-03-28 15:34:00 +0100 | [diff] [blame] | 160 | DURATION = 'duration' |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 161 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 162 | SCHEMA_TYPES = { |
| 163 | INT: int, |
| 164 | STR: str, |
Pau Espin Pedrol | fa9a6d3 | 2017-09-12 15:13:21 +0200 | [diff] [blame] | 165 | UINT: uint, |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 166 | BOOL_STR: util.str2bool, |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 167 | BAND: band, |
| 168 | IPV4: ipv4, |
| 169 | HWADDR: hwaddr, |
| 170 | IMSI: imsi, |
| 171 | KI: ki, |
| 172 | MSISDN: msisdn, |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 173 | AUTH_ALGO: auth_algo, |
Pau Espin Pedrol | ead79ac | 2017-09-12 15:19:18 +0200 | [diff] [blame] | 174 | TIMES: times, |
Pau Espin Pedrol | 57497a6 | 2017-08-28 14:21:15 +0200 | [diff] [blame] | 175 | CIPHER: cipher, |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 176 | MODEM_FEATURE: modem_feature, |
Pau Espin Pedrol | c9b6376 | 2018-05-07 01:57:01 +0200 | [diff] [blame] | 177 | PHY_CHAN: phy_channel_config, |
Pau Espin Pedrol | 722e94e | 2018-08-22 11:01:32 +0200 | [diff] [blame] | 178 | CHAN_ALLOCATOR: channel_allocator, |
Pau Espin Pedrol | 4f23ab5 | 2018-10-29 11:30:00 +0100 | [diff] [blame] | 179 | GPRS_MODE: gprs_mode, |
Pau Espin Pedrol | 5dc2459 | 2018-08-27 12:53:41 +0200 | [diff] [blame] | 180 | CODEC: codec, |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 181 | OSMO_TRX_CLOCK_REF: osmo_trx_clock_ref, |
Pau Espin Pedrol | b693771 | 2020-02-27 18:02:20 +0100 | [diff] [blame] | 182 | LTE_TRANSMISSION_MODE: lte_transmission_mode, |
Andre Puschmann | 2dcc431 | 2020-03-28 15:34:00 +0100 | [diff] [blame] | 183 | DURATION: duration, |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 186 | def 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 | |
| 209 | def 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 | |
| 248 | def 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 Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 267 | def 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 Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 283 | if util.is_list(value): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 284 | 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 Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 290 | if util.is_dict(value): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 291 | nest(path, value, schema) |
| 292 | return |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 293 | if util.is_list(value) and value: |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 294 | 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 Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 302 | if util.is_dict(value): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 303 | raise ValueError('config item is dict but should be a leaf node of type %r: %r' |
| 304 | % (want_type, path)) |
| 305 | |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 306 | if util.is_list(value): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 307 | for list_v in value: |
| 308 | validate_item(path, list_v, schema) |
| 309 | return |
| 310 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 311 | log.ctx(path) |
| 312 | type_validator = SCHEMA_TYPES.get(want_type) |
| 313 | type_validator(value) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 314 | |
| 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 Pedrol | 3063730 | 2020-05-06 21:11:02 +0200 | [diff] [blame] | 328 | def config_to_schema_def(src, key_prefix): |
| 329 | 'Converts a yaml parsed config into a schema dictionary used by validate()' |
| 330 | if util.is_dict(src): |
| 331 | out_dict = {} |
| 332 | for key, val in src.items(): |
| 333 | list_token = '' |
| 334 | dict_token = '' |
| 335 | if util.is_list(val): |
| 336 | list_token = '[]' |
| 337 | assert len(val) == 1 |
| 338 | val = val[0] |
| 339 | if util.is_dict(val): |
| 340 | dict_token = '.' |
| 341 | tmp_out = config_to_schema_def(val, "%s%s%s%s" %(key_prefix, key, list_token, dict_token)) |
| 342 | out_dict = {**out_dict, **tmp_out} |
| 343 | return out_dict |
| 344 | |
| 345 | # base case: string |
| 346 | return {key_prefix: str(src)} |
| 347 | |
| 348 | |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 349 | def generate_schemas(): |
| 350 | "Generate supported schemas dynamically from objects" |
| 351 | obj_dir = '%s/../obj/' % os.path.dirname(os.path.abspath(__file__)) |
| 352 | for filename in os.listdir(obj_dir): |
| 353 | if not filename.endswith(".py"): |
| 354 | continue |
| 355 | module_name = 'osmo_gsm_tester.obj.%s' % filename[:-3] |
| 356 | util.run_python_file_method(module_name, 'on_register_schemas', False) |
| 357 | |
| 358 | |
| 359 | _RESOURCE_TYPES = ['ip_address', 'arfcn'] |
| 360 | |
| 361 | _RESOURCES_SCHEMA = { |
| 362 | 'ip_address[].addr': IPV4, |
| 363 | 'arfcn[].arfcn': INT, |
| 364 | 'arfcn[].band': BAND, |
| 365 | } |
| 366 | |
| 367 | _CONFIG_SCHEMA = {} |
| 368 | |
| 369 | _WANT_SCHEMA = None |
| 370 | _ALL_SCHEMA = None |
| 371 | |
| 372 | def register_resource_schema(obj_class_str, obj_attr_dict): |
| 373 | """Register schema attributes for a resource type. |
| 374 | For instance: register_resource_schema_attributes('modem', {'type': schema.STR, 'ki': schema.KI}) |
| 375 | """ |
| 376 | global _RESOURCES_SCHEMA |
| 377 | global _RESOURCE_TYPES |
| 378 | tmpdict = {} |
| 379 | for key, val in obj_attr_dict.items(): |
| 380 | new_key = '%s[].%s' % (obj_class_str, key) |
| 381 | tmpdict[new_key] = val |
| 382 | combine(_RESOURCES_SCHEMA, tmpdict) |
| 383 | if obj_class_str not in _RESOURCE_TYPES: |
| 384 | _RESOURCE_TYPES.append(obj_class_str) |
| 385 | |
| 386 | def register_config_schema(obj_class_str, obj_attr_dict): |
| 387 | """Register schema attributes to configure all instances of an object class. |
| 388 | For instance: register_resource_schema_attributes('bsc', {'net.codec_list[]': schema.CODEC}) |
| 389 | """ |
Pau Espin Pedrol | 3063730 | 2020-05-06 21:11:02 +0200 | [diff] [blame] | 390 | global _CONFIG_SCHEMA, _ALL_SCHEMA |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 391 | tmpdict = {} |
| 392 | for key, val in obj_attr_dict.items(): |
| 393 | new_key = '%s.%s' % (obj_class_str, key) |
| 394 | tmpdict[new_key] = val |
| 395 | combine(_CONFIG_SCHEMA, tmpdict) |
Pau Espin Pedrol | 3063730 | 2020-05-06 21:11:02 +0200 | [diff] [blame] | 396 | _ALL_SCHEMA = None # reset _ALL_SCHEMA so it is re-generated next time it's requested. |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 397 | |
| 398 | def get_resources_schema(): |
| 399 | return _RESOURCES_SCHEMA; |
| 400 | |
| 401 | def get_want_schema(): |
| 402 | global _WANT_SCHEMA |
| 403 | if _WANT_SCHEMA is None: |
| 404 | _WANT_SCHEMA = util.dict_add( |
| 405 | dict([('%s[].times' % r, TIMES) for r in _RESOURCE_TYPES]), |
| 406 | get_resources_schema()) |
| 407 | return _WANT_SCHEMA |
| 408 | |
| 409 | def get_all_schema(): |
| 410 | global _ALL_SCHEMA |
| 411 | if _ALL_SCHEMA is None: |
| 412 | want_schema = get_want_schema() |
| 413 | _ALL_SCHEMA = util.dict_add({ 'defaults.timeout': STR }, |
| 414 | dict([('config.%s' % key, val) for key, val in _CONFIG_SCHEMA.items()]), |
| 415 | dict([('resources.%s' % key, val) for key, val in want_schema.items()]), |
| 416 | dict([('modifiers.%s' % key, val) for key, val in want_schema.items()])) |
| 417 | return _ALL_SCHEMA |
| 418 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 419 | # vim: expandtab tabstop=4 shiftwidth=4 |