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}') |
Pau Espin Pedrol | 0f7f265 | 2020-07-13 12:01:10 +0200 | [diff] [blame] | 31 | OPC_RE = re.compile('[0-9a-fA-F]{32}') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 32 | MSISDN_RE = re.compile('[0-9]{1,15}') |
| 33 | |
| 34 | def match_re(name, regex, val): |
| 35 | while True: |
| 36 | if not isinstance(val, str): |
| 37 | break; |
| 38 | if not regex.fullmatch(val): |
| 39 | break; |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 40 | return True |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 41 | raise ValueError('Invalid %s: %r' % (name, val)) |
| 42 | |
| 43 | def band(val): |
Pau Espin Pedrol | 05a838e | 2018-03-27 19:15:41 +0200 | [diff] [blame] | 44 | if val in ('GSM-900', 'GSM-1800', 'GSM-1900'): |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 45 | return True |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 46 | raise ValueError('Unknown GSM band: %r' % val) |
| 47 | |
| 48 | def ipv4(val): |
| 49 | match_re('IPv4 address', IPV4_RE, val) |
| 50 | els = [int(el) for el in val.split('.')] |
| 51 | if not all([el >= 0 and el <= 255 for el in els]): |
| 52 | raise ValueError('Invalid IPv4 address: %r' % val) |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 53 | return True |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 54 | |
| 55 | def hwaddr(val): |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 56 | return match_re('hardware address', HWADDR_RE, val) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 57 | |
| 58 | def imsi(val): |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 59 | return match_re('IMSI', IMSI_RE, val) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 60 | |
| 61 | def ki(val): |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 62 | return match_re('KI', KI_RE, val) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 63 | |
Pau Espin Pedrol | 0f7f265 | 2020-07-13 12:01:10 +0200 | [diff] [blame] | 64 | def opc(val): |
| 65 | return match_re('OPC', OPC_RE, val) |
| 66 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 67 | def msisdn(val): |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 68 | return match_re('MSISDN', MSISDN_RE, val) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 69 | |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 70 | def auth_algo(val): |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 71 | if val not in util.ENUM_OSMO_AUTH_ALGO: |
Neels Hofmeyr | 0af893c | 2017-12-14 15:18:05 +0100 | [diff] [blame] | 72 | raise ValueError('Unknown Authentication Algorithm: %r' % val) |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 73 | return True |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 74 | |
Pau Espin Pedrol | fa9a6d3 | 2017-09-12 15:13:21 +0200 | [diff] [blame] | 75 | def uint(val): |
| 76 | n = int(val) |
| 77 | if n < 0: |
| 78 | raise ValueError('Positive value expected instead of %d' % n) |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 79 | return True |
Pau Espin Pedrol | fa9a6d3 | 2017-09-12 15:13:21 +0200 | [diff] [blame] | 80 | |
Pau Espin Pedrol | 8a3a7b5 | 2017-11-28 15:50:02 +0100 | [diff] [blame] | 81 | def uint8(val): |
| 82 | n = int(val) |
| 83 | if n < 0: |
| 84 | raise ValueError('Positive value expected instead of %d' % n) |
| 85 | if n > 255: # 2^8 - 1 |
| 86 | raise ValueError('Value %d too big, max value is 255' % n) |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 87 | return True |
Pau Espin Pedrol | 8a3a7b5 | 2017-11-28 15:50:02 +0100 | [diff] [blame] | 88 | |
Pau Espin Pedrol | 5e0c251 | 2017-11-06 18:40:23 +0100 | [diff] [blame] | 89 | def uint16(val): |
| 90 | n = int(val) |
| 91 | if n < 0: |
| 92 | raise ValueError('Positive value expected instead of %d' % n) |
| 93 | if n > 65535: # 2^16 - 1 |
| 94 | raise ValueError('Value %d too big, max value is 65535' % n) |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 95 | return True |
| 96 | |
| 97 | def bool_str(val): |
| 98 | # str2bool will raise an exception if unable to parse it |
| 99 | util.str2bool(val) |
| 100 | return True |
Pau Espin Pedrol | 5e0c251 | 2017-11-06 18:40:23 +0100 | [diff] [blame] | 101 | |
Pau Espin Pedrol | ead79ac | 2017-09-12 15:19:18 +0200 | [diff] [blame] | 102 | def times(val): |
| 103 | n = int(val) |
| 104 | if n < 1: |
| 105 | raise ValueError('Positive value >0 expected instead of %d' % n) |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 106 | return True |
Pau Espin Pedrol | ead79ac | 2017-09-12 15:19:18 +0200 | [diff] [blame] | 107 | |
Pau Espin Pedrol | 57497a6 | 2017-08-28 14:21:15 +0200 | [diff] [blame] | 108 | def cipher(val): |
| 109 | if val in ('a5_0', 'a5_1', 'a5_2', 'a5_3', 'a5_4', 'a5_5', 'a5_6', 'a5_7'): |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 110 | return True |
Pau Espin Pedrol | 57497a6 | 2017-08-28 14:21:15 +0200 | [diff] [blame] | 111 | raise ValueError('Unknown Cipher value: %r' % val) |
| 112 | |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 113 | def modem_feature(val): |
Pau Espin Pedrol | eae9c90 | 2020-03-31 11:12:39 +0200 | [diff] [blame] | 114 | if val in ('sms', 'gprs', 'voice', 'ussd', 'sim', '2g', '3g', '4g'): |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 115 | return True |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 116 | raise ValueError('Unknown Modem Feature: %r' % val) |
| 117 | |
Pau Espin Pedrol | c9b6376 | 2018-05-07 01:57:01 +0200 | [diff] [blame] | 118 | def phy_channel_config(val): |
| 119 | if val in ('CCCH', 'CCCH+SDCCH4', 'TCH/F', 'TCH/H', 'SDCCH8', 'PDCH', |
| 120 | 'TCH/F_PDCH', 'CCCH+SDCCH4+CBCH', 'SDCCH8+CBCH','TCH/F_TCH/H_PDCH'): |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 121 | return True |
Pau Espin Pedrol | c9b6376 | 2018-05-07 01:57:01 +0200 | [diff] [blame] | 122 | raise ValueError('Unknown Physical channel config: %r' % val) |
| 123 | |
Pau Espin Pedrol | 722e94e | 2018-08-22 11:01:32 +0200 | [diff] [blame] | 124 | def channel_allocator(val): |
| 125 | if val in ('ascending', 'descending'): |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 126 | return True |
Pau Espin Pedrol | 722e94e | 2018-08-22 11:01:32 +0200 | [diff] [blame] | 127 | raise ValueError('Unknown Channel Allocator Policy %r' % val) |
| 128 | |
Pau Espin Pedrol | 4f23ab5 | 2018-10-29 11:30:00 +0100 | [diff] [blame] | 129 | def gprs_mode(val): |
| 130 | if val in ('none', 'gprs', 'egprs'): |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 131 | return True |
Pau Espin Pedrol | 4f23ab5 | 2018-10-29 11:30:00 +0100 | [diff] [blame] | 132 | raise ValueError('Unknown GPRS mode %r' % val) |
| 133 | |
Pau Espin Pedrol | 5dc2459 | 2018-08-27 12:53:41 +0200 | [diff] [blame] | 134 | def codec(val): |
| 135 | if val in ('hr1', 'hr2', 'hr3', 'fr1', 'fr2', 'fr3'): |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 136 | return True |
Pau Espin Pedrol | 5dc2459 | 2018-08-27 12:53:41 +0200 | [diff] [blame] | 137 | raise ValueError('Unknown Codec value: %r' % val) |
| 138 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 139 | def osmo_trx_clock_ref(val): |
| 140 | if val in ('internal', 'external', 'gspdo'): |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 141 | return True |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 142 | raise ValueError('Unknown OsmoTRX clock reference value: %r' % val) |
| 143 | |
Pau Espin Pedrol | b693771 | 2020-02-27 18:02:20 +0100 | [diff] [blame] | 144 | def lte_transmission_mode(val): |
| 145 | n = int(val) |
| 146 | if n <= 4: |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 147 | return True |
Pau Espin Pedrol | b693771 | 2020-02-27 18:02:20 +0100 | [diff] [blame] | 148 | raise ValueError('LTE Transmission Mode %d not in expected range' % n) |
| 149 | |
Andre Puschmann | 2dcc431 | 2020-03-28 15:34:00 +0100 | [diff] [blame] | 150 | def duration(val): |
| 151 | if val.isdecimal() or val.endswith('m') or val.endswith('h'): |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 152 | return True |
Andre Puschmann | 2dcc431 | 2020-03-28 15:34:00 +0100 | [diff] [blame] | 153 | raise ValueError('Invalid duration value: %r' % val) |
| 154 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 155 | INT = 'int' |
| 156 | STR = 'str' |
Pau Espin Pedrol | fa9a6d3 | 2017-09-12 15:13:21 +0200 | [diff] [blame] | 157 | UINT = 'uint' |
Pau Espin Pedrol | 404e150 | 2017-08-22 11:17:43 +0200 | [diff] [blame] | 158 | BOOL_STR = 'bool_str' |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 159 | BAND = 'band' |
| 160 | IPV4 = 'ipv4' |
| 161 | HWADDR = 'hwaddr' |
| 162 | IMSI = 'imsi' |
| 163 | KI = 'ki' |
Pau Espin Pedrol | 0f7f265 | 2020-07-13 12:01:10 +0200 | [diff] [blame] | 164 | OPC = 'opc' |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 165 | MSISDN = 'msisdn' |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 166 | AUTH_ALGO = 'auth_algo' |
Pau Espin Pedrol | ead79ac | 2017-09-12 15:19:18 +0200 | [diff] [blame] | 167 | TIMES='times' |
Pau Espin Pedrol | 57497a6 | 2017-08-28 14:21:15 +0200 | [diff] [blame] | 168 | CIPHER = 'cipher' |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 169 | MODEM_FEATURE = 'modem_feature' |
Pau Espin Pedrol | c9b6376 | 2018-05-07 01:57:01 +0200 | [diff] [blame] | 170 | PHY_CHAN = 'chan' |
Pau Espin Pedrol | 722e94e | 2018-08-22 11:01:32 +0200 | [diff] [blame] | 171 | CHAN_ALLOCATOR = 'chan_allocator' |
Pau Espin Pedrol | 4f23ab5 | 2018-10-29 11:30:00 +0100 | [diff] [blame] | 172 | GPRS_MODE = 'gprs_mode' |
Pau Espin Pedrol | 5dc2459 | 2018-08-27 12:53:41 +0200 | [diff] [blame] | 173 | CODEC = 'codec' |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 174 | OSMO_TRX_CLOCK_REF = 'osmo_trx_clock_ref' |
Pau Espin Pedrol | b693771 | 2020-02-27 18:02:20 +0100 | [diff] [blame] | 175 | LTE_TRANSMISSION_MODE = 'lte_transmission_mode' |
Andre Puschmann | 2dcc431 | 2020-03-28 15:34:00 +0100 | [diff] [blame] | 176 | DURATION = 'duration' |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 177 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 178 | SCHEMA_TYPES = { |
| 179 | INT: int, |
| 180 | STR: str, |
Pau Espin Pedrol | fa9a6d3 | 2017-09-12 15:13:21 +0200 | [diff] [blame] | 181 | UINT: uint, |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 182 | BOOL_STR: bool_str, |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 183 | BAND: band, |
| 184 | IPV4: ipv4, |
| 185 | HWADDR: hwaddr, |
| 186 | IMSI: imsi, |
| 187 | KI: ki, |
Pau Espin Pedrol | 0f7f265 | 2020-07-13 12:01:10 +0200 | [diff] [blame] | 188 | OPC: opc, |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 189 | MSISDN: msisdn, |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 190 | AUTH_ALGO: auth_algo, |
Pau Espin Pedrol | ead79ac | 2017-09-12 15:19:18 +0200 | [diff] [blame] | 191 | TIMES: times, |
Pau Espin Pedrol | 57497a6 | 2017-08-28 14:21:15 +0200 | [diff] [blame] | 192 | CIPHER: cipher, |
Pau Espin Pedrol | ac18fd3 | 2017-08-31 18:49:47 +0200 | [diff] [blame] | 193 | MODEM_FEATURE: modem_feature, |
Pau Espin Pedrol | c9b6376 | 2018-05-07 01:57:01 +0200 | [diff] [blame] | 194 | PHY_CHAN: phy_channel_config, |
Pau Espin Pedrol | 722e94e | 2018-08-22 11:01:32 +0200 | [diff] [blame] | 195 | CHAN_ALLOCATOR: channel_allocator, |
Pau Espin Pedrol | 4f23ab5 | 2018-10-29 11:30:00 +0100 | [diff] [blame] | 196 | GPRS_MODE: gprs_mode, |
Pau Espin Pedrol | 5dc2459 | 2018-08-27 12:53:41 +0200 | [diff] [blame] | 197 | CODEC: codec, |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 198 | OSMO_TRX_CLOCK_REF: osmo_trx_clock_ref, |
Pau Espin Pedrol | b693771 | 2020-02-27 18:02:20 +0100 | [diff] [blame] | 199 | LTE_TRANSMISSION_MODE: lte_transmission_mode, |
Andre Puschmann | 2dcc431 | 2020-03-28 15:34:00 +0100 | [diff] [blame] | 200 | DURATION: duration, |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 203 | def add(dest, src): |
| 204 | if util.is_dict(dest): |
| 205 | if not util.is_dict(src): |
| 206 | raise ValueError('cannot add to dict a value of type: %r' % type(src)) |
| 207 | |
| 208 | for key, val in src.items(): |
| 209 | dest_val = dest.get(key) |
| 210 | if dest_val is None: |
| 211 | dest[key] = val |
| 212 | else: |
| 213 | log.ctx(key=key) |
| 214 | add(dest_val, val) |
| 215 | return |
| 216 | if util.is_list(dest): |
| 217 | if not util.is_list(src): |
| 218 | raise ValueError('cannot add to list a value of type: %r' % type(src)) |
| 219 | dest.extend(src) |
| 220 | return |
| 221 | if dest == src: |
| 222 | return |
| 223 | raise ValueError('cannot add dicts, conflicting items (values %r and %r)' |
| 224 | % (dest, src)) |
| 225 | |
| 226 | def combine(dest, src): |
| 227 | if util.is_dict(dest): |
| 228 | if not util.is_dict(src): |
| 229 | raise ValueError('cannot combine dict with a value of type: %r' % type(src)) |
| 230 | |
| 231 | for key, val in src.items(): |
| 232 | log.ctx(key=key) |
| 233 | dest_val = dest.get(key) |
| 234 | if dest_val is None: |
| 235 | dest[key] = val |
| 236 | else: |
| 237 | combine(dest_val, val) |
| 238 | return |
| 239 | if util.is_list(dest): |
| 240 | if not util.is_list(src): |
| 241 | raise ValueError('cannot combine list with a value of type: %r' % type(src)) |
| 242 | # Validate that all elements in both lists are of the same type: |
| 243 | t = util.list_validate_same_elem_type(src + dest) |
| 244 | if t is None: |
| 245 | return # both lists are empty, return |
| 246 | # For lists of complex objects, we expect them to be sorted lists: |
| 247 | if t in (dict, list, tuple): |
| 248 | for i in range(len(dest)): |
| 249 | log.ctx(idx=i) |
| 250 | src_it = src[i] if i < len(src) else util.empty_instance_type(t) |
| 251 | combine(dest[i], src_it) |
| 252 | for i in range(len(dest), len(src)): |
| 253 | log.ctx(idx=i) |
| 254 | dest.append(src[i]) |
| 255 | else: # for lists of basic elements, we handle them as unsorted sets: |
| 256 | for elem in src: |
| 257 | if elem not in dest: |
| 258 | dest.append(elem) |
| 259 | return |
| 260 | if dest == src: |
| 261 | return |
| 262 | raise ValueError('cannot combine dicts, conflicting items (values %r and %r)' |
| 263 | % (dest, src)) |
| 264 | |
| 265 | def replicate_times(d): |
| 266 | ''' |
| 267 | replicate items that have a "times" > 1 |
| 268 | |
| 269 | 'd' is a dict matching WANT_SCHEMA, which is the same as |
| 270 | the RESOURCES_SCHEMA, except each entity that can be reserved has a 'times' |
| 271 | field added, to indicate how many of those should be reserved. |
| 272 | ''' |
| 273 | d = copy.deepcopy(d) |
| 274 | for key, item_list in d.items(): |
| 275 | idx = 0 |
| 276 | while idx < len(item_list): |
| 277 | item = item_list[idx] |
| 278 | times = int(item.pop('times', 1)) |
| 279 | for j in range(1, times): |
| 280 | item_list.insert(idx + j, copy.deepcopy(item)) |
| 281 | idx += times |
| 282 | return d |
| 283 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 284 | def validate(config, schema): |
| 285 | '''Make sure the given config dict adheres to the schema. |
| 286 | The schema is a dict of 'dict paths' in dot-notation with permitted |
| 287 | value type. All leaf nodes are validated, nesting dicts are implicit. |
| 288 | |
| 289 | validate( { 'a': 123, 'b': { 'b1': 'foo', 'b2': [ 1, 2, 3 ] } }, |
| 290 | { 'a': int, |
| 291 | 'b.b1': str, |
| 292 | 'b.b2[]': int } ) |
| 293 | |
| 294 | Raise a ValueError in case the schema is violated. |
| 295 | ''' |
| 296 | |
| 297 | def validate_item(path, value, schema): |
| 298 | want_type = schema.get(path) |
| 299 | |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 300 | if util.is_list(value): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 301 | if want_type: |
| 302 | raise ValueError('config item is a list, should be %r: %r' % (want_type, path)) |
| 303 | path = path + '[]' |
| 304 | want_type = schema.get(path) |
| 305 | |
| 306 | if not want_type: |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 307 | if util.is_dict(value): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 308 | nest(path, value, schema) |
| 309 | return |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 310 | if util.is_list(value) and value: |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 311 | for list_v in value: |
| 312 | validate_item(path, list_v, schema) |
| 313 | return |
| 314 | raise ValueError('config item not known: %r' % path) |
| 315 | |
| 316 | if want_type not in SCHEMA_TYPES: |
| 317 | raise ValueError('unknown type %r at %r' % (want_type, path)) |
| 318 | |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 319 | if util.is_dict(value): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 320 | raise ValueError('config item is dict but should be a leaf node of type %r: %r' |
| 321 | % (want_type, path)) |
| 322 | |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 323 | if util.is_list(value): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 324 | for list_v in value: |
| 325 | validate_item(path, list_v, schema) |
| 326 | return |
| 327 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 328 | log.ctx(path) |
| 329 | type_validator = SCHEMA_TYPES.get(want_type) |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 330 | valid = type_validator(value) |
| 331 | if not valid: |
| 332 | raise ValueError('Invalid value %r for schema type \'%s\' (validator: %s)' % (value, want_type, type_validator.__name__)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 333 | |
| 334 | def nest(parent_path, config, schema): |
| 335 | if parent_path: |
| 336 | parent_path = parent_path + '.' |
| 337 | else: |
| 338 | parent_path = '' |
| 339 | for k,v in config.items(): |
| 340 | if not KEY_RE.fullmatch(k): |
| 341 | raise ValueError('invalid config key: %r' % k) |
| 342 | path = parent_path + k |
| 343 | validate_item(path, v, schema) |
| 344 | |
| 345 | nest(None, config, schema) |
| 346 | |
Pau Espin Pedrol | 3063730 | 2020-05-06 21:11:02 +0200 | [diff] [blame] | 347 | def config_to_schema_def(src, key_prefix): |
| 348 | 'Converts a yaml parsed config into a schema dictionary used by validate()' |
| 349 | if util.is_dict(src): |
| 350 | out_dict = {} |
| 351 | for key, val in src.items(): |
| 352 | list_token = '' |
| 353 | dict_token = '' |
| 354 | if util.is_list(val): |
| 355 | list_token = '[]' |
| 356 | assert len(val) == 1 |
| 357 | val = val[0] |
| 358 | if util.is_dict(val): |
| 359 | dict_token = '.' |
| 360 | tmp_out = config_to_schema_def(val, "%s%s%s%s" %(key_prefix, key, list_token, dict_token)) |
| 361 | out_dict = {**out_dict, **tmp_out} |
| 362 | return out_dict |
| 363 | |
| 364 | # base case: string |
| 365 | return {key_prefix: str(src)} |
| 366 | |
| 367 | |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 368 | def generate_schemas(): |
| 369 | "Generate supported schemas dynamically from objects" |
| 370 | obj_dir = '%s/../obj/' % os.path.dirname(os.path.abspath(__file__)) |
| 371 | for filename in os.listdir(obj_dir): |
| 372 | if not filename.endswith(".py"): |
| 373 | continue |
| 374 | module_name = 'osmo_gsm_tester.obj.%s' % filename[:-3] |
| 375 | util.run_python_file_method(module_name, 'on_register_schemas', False) |
| 376 | |
| 377 | |
| 378 | _RESOURCE_TYPES = ['ip_address', 'arfcn'] |
| 379 | |
| 380 | _RESOURCES_SCHEMA = { |
| 381 | 'ip_address[].addr': IPV4, |
| 382 | 'arfcn[].arfcn': INT, |
| 383 | 'arfcn[].band': BAND, |
| 384 | } |
| 385 | |
| 386 | _CONFIG_SCHEMA = {} |
| 387 | |
| 388 | _WANT_SCHEMA = None |
| 389 | _ALL_SCHEMA = None |
| 390 | |
Pau Espin Pedrol | d79e719 | 2020-05-21 15:40:57 +0200 | [diff] [blame] | 391 | def register_schema_types(schema_type_attr): |
| 392 | """Register schema types to be used by schema attributes. |
| 393 | For instance: register_resource_schema_attributes({ 'fruit': lambda val: val in ('banana', 'apple') }) |
| 394 | """ |
| 395 | global SCHEMA_TYPES |
| 396 | combine(SCHEMA_TYPES, schema_type_attr) |
| 397 | |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 398 | def register_resource_schema(obj_class_str, obj_attr_dict): |
| 399 | """Register schema attributes for a resource type. |
| 400 | For instance: register_resource_schema_attributes('modem', {'type': schema.STR, 'ki': schema.KI}) |
| 401 | """ |
| 402 | global _RESOURCES_SCHEMA |
| 403 | global _RESOURCE_TYPES |
| 404 | tmpdict = {} |
| 405 | for key, val in obj_attr_dict.items(): |
| 406 | new_key = '%s[].%s' % (obj_class_str, key) |
| 407 | tmpdict[new_key] = val |
| 408 | combine(_RESOURCES_SCHEMA, tmpdict) |
| 409 | if obj_class_str not in _RESOURCE_TYPES: |
| 410 | _RESOURCE_TYPES.append(obj_class_str) |
| 411 | |
| 412 | def register_config_schema(obj_class_str, obj_attr_dict): |
| 413 | """Register schema attributes to configure all instances of an object class. |
| 414 | For instance: register_resource_schema_attributes('bsc', {'net.codec_list[]': schema.CODEC}) |
| 415 | """ |
Pau Espin Pedrol | 3063730 | 2020-05-06 21:11:02 +0200 | [diff] [blame] | 416 | global _CONFIG_SCHEMA, _ALL_SCHEMA |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 417 | tmpdict = {} |
| 418 | for key, val in obj_attr_dict.items(): |
| 419 | new_key = '%s.%s' % (obj_class_str, key) |
| 420 | tmpdict[new_key] = val |
| 421 | combine(_CONFIG_SCHEMA, tmpdict) |
Pau Espin Pedrol | 3063730 | 2020-05-06 21:11:02 +0200 | [diff] [blame] | 422 | _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] | 423 | |
| 424 | def get_resources_schema(): |
| 425 | return _RESOURCES_SCHEMA; |
| 426 | |
| 427 | def get_want_schema(): |
| 428 | global _WANT_SCHEMA |
| 429 | if _WANT_SCHEMA is None: |
| 430 | _WANT_SCHEMA = util.dict_add( |
| 431 | dict([('%s[].times' % r, TIMES) for r in _RESOURCE_TYPES]), |
| 432 | get_resources_schema()) |
| 433 | return _WANT_SCHEMA |
| 434 | |
| 435 | def get_all_schema(): |
| 436 | global _ALL_SCHEMA |
| 437 | if _ALL_SCHEMA is None: |
| 438 | want_schema = get_want_schema() |
Pau Espin Pedrol | 8cfb5d0 | 2020-06-12 16:12:40 +0200 | [diff] [blame] | 439 | _ALL_SCHEMA = util.dict_add( |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 440 | dict([('config.%s' % key, val) for key, val in _CONFIG_SCHEMA.items()]), |
| 441 | dict([('resources.%s' % key, val) for key, val in want_schema.items()]), |
| 442 | dict([('modifiers.%s' % key, val) for key, val in want_schema.items()])) |
| 443 | return _ALL_SCHEMA |
| 444 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 445 | # vim: expandtab tabstop=4 shiftwidth=4 |