blob: 5b43642a2d752dcc644e4d8d203bc4575249a6e9 [file] [log] [blame]
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +01001# osmo_gsm_tester: base classes to share code among BTS subclasses.
2#
3# Copyright (C) 2018 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Pau Espin Pedrol <pespin@sysmocom.de>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as
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
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
Pau Espin Pedrol698ad4c2018-07-27 16:15:59 +020020import copy
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010021from abc import ABCMeta, abstractmethod
Pau Espin Pedrol680ba032020-10-14 14:49:05 +020022from ..core import log
23from ..core import config
24from ..core import schema
25from ..core import util
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010026
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020027def on_register_schemas():
28 resource_schema = {
29 'label': schema.STR,
30 'type': schema.STR,
31 'addr': schema.IPV4,
32 'band': schema.BAND,
33 'direct_pcu': schema.BOOL_STR,
34 'ciphers[]': schema.CIPHER,
35 'channel_allocator': schema.CHAN_ALLOCATOR,
36 'gprs_mode': schema.GPRS_MODE,
Pau Espin Pedrol680ba032020-10-14 14:49:05 +020037 'emergency_calls_allowed': schema.BOOL_STR,
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020038 'num_trx': schema.UINT,
39 'max_trx': schema.UINT,
40 'trx_list[].addr': schema.IPV4,
41 'trx_list[].hw_addr': schema.HWADDR,
42 'trx_list[].net_device': schema.STR,
43 'trx_list[].nominal_power': schema.UINT,
44 'trx_list[].max_power_red': schema.UINT,
45 'trx_list[].timeslot_list[].phys_chan_config': schema.PHY_CHAN,
46 'trx_list[].power_supply.type': schema.STR,
47 'trx_list[].power_supply.device': schema.STR,
48 'trx_list[].power_supply.port': schema.STR,
49 }
50 schema.register_resource_schema('bts', resource_schema)
51
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010052class Bts(log.Origin, metaclass=ABCMeta):
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010053
54##############
55# PROTECTED
56##############
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020057 def __init__(self, testenv, conf, name, defaults_cfg_name):
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010058 super().__init__(log.C_RUN, name)
Pau Espin Pedrol58603672018-08-09 13:45:55 +020059 self.bsc = None
60 self.sgsn = None
61 self.lac = None
62 self.rac = None
63 self.cellid = None
64 self.bvci = None
65 self._num_trx = 1
66 self._max_trx = None
67 self.overlay_trx_list = []
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020068 self.testenv = testenv
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010069 self.conf = conf
Pau Espin Pedrole5194622018-05-07 13:36:58 +020070 self.defaults_cfg_name = defaults_cfg_name
Pau Espin Pedrol39df7f42018-05-07 13:49:33 +020071 self._init_num_trx()
72
73 def _resolve_bts_cfg(self, cfg_name):
74 res = None
75 val = config.get_defaults('bsc_bts').get(cfg_name)
76 if val is not None:
77 res = val
78 val = config.get_defaults(self.defaults_cfg_name).get(cfg_name)
79 if val is not None:
80 res = val
81 val = self.conf.get(cfg_name)
82 if val is not None:
83 res = val
84 return res
85
86 def _init_num_trx(self):
87 self._num_trx = 1
88 self._max_trx = None
89 val = self._resolve_bts_cfg('num_trx')
90 if val is not None:
91 self._num_trx = int(val)
92 val = self._resolve_bts_cfg('max_trx')
93 if val is not None:
94 self._max_trx = int(val)
95 self._validate_new_num_trx(self._num_trx)
96 self.overlay_trx_list = [Bts._new_default_trx_cfg() for trx in range(self._num_trx)]
97
98 def _validate_new_num_trx(self, num_trx):
99 if self._max_trx is not None and num_trx > self._max_trx:
100 raise log.Error('Amount of TRX requested is too high for maximum allowed: %u > %u' %(num_trx, self._max_trx))
101
102 @staticmethod
103 def _new_default_trx_cfg():
104 return {'timeslot_list':[{} for ts in range(8)]}
105
106 @staticmethod
107 def _trx_list_recreate(trx_list, new_size):
108 curr_len = len(trx_list)
109 if new_size < curr_len:
110 trx_list = trx_list[0:new_size]
111 elif new_size > curr_len:
112 for i in range(new_size - curr_len):
113 trx_list.append(Bts._new_default_trx_cfg())
114 return trx_list
Pau Espin Pedrole6999122018-05-08 14:38:24 +0200115
116 def conf_for_bsc_prepare(self):
117 values = config.get_defaults('bsc_bts')
Pau Espin Pedrol39df7f42018-05-07 13:49:33 +0200118 # Make sure the trx_list is adapted to num of trx configured at runtime
119 # to avoid overlay issues.
120 trx_list = values.get('trx_list')
121 if trx_list and len(trx_list) != self.num_trx():
122 values['trx_list'] = Bts._trx_list_recreate(trx_list, self.num_trx())
123
124 bts_defaults = config.get_defaults(self.defaults_cfg_name)
125 trx_list = bts_defaults.get('trx_list')
126 if trx_list and len(trx_list) != self.num_trx():
127 bts_defaults['trx_list'] = Bts._trx_list_recreate(trx_list, self.num_trx())
128
129 config.overlay(values, bts_defaults)
Pau Espin Pedrole6999122018-05-08 14:38:24 +0200130 if self.lac is not None:
131 config.overlay(values, { 'location_area_code': self.lac })
132 if self.rac is not None:
133 config.overlay(values, { 'routing_area_code': self.rac })
134 if self.cellid is not None:
135 config.overlay(values, { 'cell_identity': self.cellid })
136 if self.bvci is not None:
137 config.overlay(values, { 'bvci': self.bvci })
Pau Espin Pedrol698ad4c2018-07-27 16:15:59 +0200138
Pau Espin Pedrol680ba032020-10-14 14:49:05 +0200139 config.overlay(values, { 'emergency_calls_allowed': util.str2bool(values.get('emergency_calls_allowed', 'false')) } )
140
Pau Espin Pedrol698ad4c2018-07-27 16:15:59 +0200141 conf = copy.deepcopy(self.conf)
142 trx_list = conf.get('trx_list')
143 if trx_list and len(trx_list) != self.num_trx():
144 conf['trx_list'] = Bts._trx_list_recreate(trx_list, self.num_trx())
145 config.overlay(values, conf)
Pau Espin Pedrole6999122018-05-08 14:38:24 +0200146
147 sgsn_conf = {} if self.sgsn is None else self.sgsn.conf_for_client()
148 config.overlay(values, sgsn_conf)
Pau Espin Pedrol39df7f42018-05-07 13:49:33 +0200149
150 config.overlay(values, { 'trx_list': self.overlay_trx_list })
Pau Espin Pedrole6999122018-05-08 14:38:24 +0200151 return values
152
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100153########################
154# PUBLIC - INTERNAL API
155########################
156 @abstractmethod
157 def conf_for_bsc(self):
158 'Used by bsc objects to get path to socket.'
159 pass
160
161 def remote_addr(self):
162 return self.conf.get('addr')
163
Pau Espin Pedrol29b71322020-04-06 18:14:29 +0200164 def egprs_enabled(self):
165 return self.conf_for_bsc()['gprs_mode'] == 'egprs'
166
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100167 def cleanup(self):
168 'Nothing to do by default. Subclass can override if required.'
169 pass
170
Pau Espin Pedrola442cb82020-05-05 12:54:37 +0200171 def get_instance_by_type(testenv, conf):
Pau Espin Pedrol1ee5ec52020-05-04 17:16:39 +0200172 """Allocate a BTS child class based on type. Opts are passed to the newly created object."""
173 bts_type = conf.get('type')
174 if bts_type is None:
175 raise RuntimeError('BTS type is not defined!')
176
177 if bts_type == 'osmo-bts-sysmo':
178 from .bts_sysmo import SysmoBts
179 bts_class = SysmoBts
180 elif bts_type == 'osmo-bts-trx':
181 from .bts_osmotrx import OsmoBtsTrx
182 bts_class = OsmoBtsTrx
183 elif bts_type == 'osmo-bts-oc2g':
184 from .bts_oc2g import OsmoBtsOC2G
185 bts_class = OsmoBtsOC2G
186 elif bts_type == 'osmo-bts-octphy':
187 from .bts_octphy import OsmoBtsOctphy
188 bts_class = OsmoBtsOctphy
189 elif bts_type == 'osmo-bts-virtual':
190 from .bts_osmovirtual import OsmoBtsVirtual
191 bts_class = OsmoBtsVirtual
192 elif bts_type == 'nanobts':
193 from .bts_nanobts import NanoBts
194 bts_class = NanoBts
195 else:
196 raise log.Error('BTS type not supported:', bts_type)
Pau Espin Pedrola442cb82020-05-05 12:54:37 +0200197 return bts_class(testenv, conf)
Pau Espin Pedrol1ee5ec52020-05-04 17:16:39 +0200198
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100199###################
200# PUBLIC (test API included)
201###################
202 @abstractmethod
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +0200203 def start(self, keepalive=False):
204 '''Starts BTS. If keepalive is set, it will expect internal issues and
205 respawn related processes when detected'''
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100206 pass
207
208 @abstractmethod
209 def ready_for_pcu(self):
210 'True if the BTS is prepared to have a PCU connected, false otherwise'
211 pass
212
213 @abstractmethod
214 def pcu(self):
215 'Get the Pcu object associated with the BTS'
216 pass
217
Pau Espin Pedrolf6166142018-10-11 17:49:34 +0200218 def bts_type(self):
219 'Get the type of BTS'
220 return self.conf.get('type')
221
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100222 def set_bsc(self, bsc):
223 self.bsc = bsc
224
225 def set_sgsn(self, sgsn):
226 self.sgsn = sgsn
227
228 def set_lac(self, lac):
229 self.lac = lac
230
231 def set_rac(self, rac):
232 self.rac = rac
233
234 def set_cellid(self, cellid):
235 self.cellid = cellid
236
237 def set_bvci(self, bvci):
238 self.bvci = bvci
239
Pau Espin Pedrol39df7f42018-05-07 13:49:33 +0200240 def set_num_trx(self, num_trx):
241 assert num_trx > 0
242 self._validate_new_num_trx(num_trx)
243 if num_trx == self._num_trx:
244 return
245 self._num_trx = num_trx
246 self.overlay_trx_list = Bts._trx_list_recreate(self.overlay_trx_list, num_trx)
247
248 def num_trx(self):
249 return self._num_trx
250
251 def set_trx_phy_channel(self, trx_idx, ts_idx, config):
252 assert trx_idx < self._num_trx
253 assert ts_idx < 8
254 schema.phy_channel_config(config) # validation
255 self.overlay_trx_list[trx_idx]['timeslot_list'][ts_idx]['phys_chan_config'] = config
256
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100257# vim: expandtab tabstop=4 shiftwidth=4