blob: 56e5aaa8d1f638171f747a988772c13cb1a79405 [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 Pedrole1a58bd2020-04-10 20:46:07 +020022from ..core import log, config, schema
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010023
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020024def on_register_schemas():
25 resource_schema = {
26 'label': schema.STR,
27 'type': schema.STR,
28 'addr': schema.IPV4,
29 'band': schema.BAND,
30 'direct_pcu': schema.BOOL_STR,
31 'ciphers[]': schema.CIPHER,
32 'channel_allocator': schema.CHAN_ALLOCATOR,
33 'gprs_mode': schema.GPRS_MODE,
34 'num_trx': schema.UINT,
35 'max_trx': schema.UINT,
36 'trx_list[].addr': schema.IPV4,
37 'trx_list[].hw_addr': schema.HWADDR,
38 'trx_list[].net_device': schema.STR,
39 'trx_list[].nominal_power': schema.UINT,
40 'trx_list[].max_power_red': schema.UINT,
41 'trx_list[].timeslot_list[].phys_chan_config': schema.PHY_CHAN,
42 'trx_list[].power_supply.type': schema.STR,
43 'trx_list[].power_supply.device': schema.STR,
44 'trx_list[].power_supply.port': schema.STR,
45 }
46 schema.register_resource_schema('bts', resource_schema)
47
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010048class Bts(log.Origin, metaclass=ABCMeta):
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010049
50##############
51# PROTECTED
52##############
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020053 def __init__(self, testenv, conf, name, defaults_cfg_name):
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010054 super().__init__(log.C_RUN, name)
Pau Espin Pedrol58603672018-08-09 13:45:55 +020055 self.bsc = None
56 self.sgsn = None
57 self.lac = None
58 self.rac = None
59 self.cellid = None
60 self.bvci = None
61 self._num_trx = 1
62 self._max_trx = None
63 self.overlay_trx_list = []
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020064 self.testenv = testenv
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010065 self.conf = conf
Pau Espin Pedrole5194622018-05-07 13:36:58 +020066 self.defaults_cfg_name = defaults_cfg_name
Pau Espin Pedrol39df7f42018-05-07 13:49:33 +020067 self._init_num_trx()
68
69 def _resolve_bts_cfg(self, cfg_name):
70 res = None
71 val = config.get_defaults('bsc_bts').get(cfg_name)
72 if val is not None:
73 res = val
74 val = config.get_defaults(self.defaults_cfg_name).get(cfg_name)
75 if val is not None:
76 res = val
77 val = self.conf.get(cfg_name)
78 if val is not None:
79 res = val
80 return res
81
82 def _init_num_trx(self):
83 self._num_trx = 1
84 self._max_trx = None
85 val = self._resolve_bts_cfg('num_trx')
86 if val is not None:
87 self._num_trx = int(val)
88 val = self._resolve_bts_cfg('max_trx')
89 if val is not None:
90 self._max_trx = int(val)
91 self._validate_new_num_trx(self._num_trx)
92 self.overlay_trx_list = [Bts._new_default_trx_cfg() for trx in range(self._num_trx)]
93
94 def _validate_new_num_trx(self, num_trx):
95 if self._max_trx is not None and num_trx > self._max_trx:
96 raise log.Error('Amount of TRX requested is too high for maximum allowed: %u > %u' %(num_trx, self._max_trx))
97
98 @staticmethod
99 def _new_default_trx_cfg():
100 return {'timeslot_list':[{} for ts in range(8)]}
101
102 @staticmethod
103 def _trx_list_recreate(trx_list, new_size):
104 curr_len = len(trx_list)
105 if new_size < curr_len:
106 trx_list = trx_list[0:new_size]
107 elif new_size > curr_len:
108 for i in range(new_size - curr_len):
109 trx_list.append(Bts._new_default_trx_cfg())
110 return trx_list
Pau Espin Pedrole6999122018-05-08 14:38:24 +0200111
112 def conf_for_bsc_prepare(self):
113 values = config.get_defaults('bsc_bts')
Pau Espin Pedrol39df7f42018-05-07 13:49:33 +0200114 # Make sure the trx_list is adapted to num of trx configured at runtime
115 # to avoid overlay issues.
116 trx_list = values.get('trx_list')
117 if trx_list and len(trx_list) != self.num_trx():
118 values['trx_list'] = Bts._trx_list_recreate(trx_list, self.num_trx())
119
120 bts_defaults = config.get_defaults(self.defaults_cfg_name)
121 trx_list = bts_defaults.get('trx_list')
122 if trx_list and len(trx_list) != self.num_trx():
123 bts_defaults['trx_list'] = Bts._trx_list_recreate(trx_list, self.num_trx())
124
125 config.overlay(values, bts_defaults)
Pau Espin Pedrole6999122018-05-08 14:38:24 +0200126 if self.lac is not None:
127 config.overlay(values, { 'location_area_code': self.lac })
128 if self.rac is not None:
129 config.overlay(values, { 'routing_area_code': self.rac })
130 if self.cellid is not None:
131 config.overlay(values, { 'cell_identity': self.cellid })
132 if self.bvci is not None:
133 config.overlay(values, { 'bvci': self.bvci })
Pau Espin Pedrol698ad4c2018-07-27 16:15:59 +0200134
135 conf = copy.deepcopy(self.conf)
136 trx_list = conf.get('trx_list')
137 if trx_list and len(trx_list) != self.num_trx():
138 conf['trx_list'] = Bts._trx_list_recreate(trx_list, self.num_trx())
139 config.overlay(values, conf)
Pau Espin Pedrole6999122018-05-08 14:38:24 +0200140
141 sgsn_conf = {} if self.sgsn is None else self.sgsn.conf_for_client()
142 config.overlay(values, sgsn_conf)
Pau Espin Pedrol39df7f42018-05-07 13:49:33 +0200143
144 config.overlay(values, { 'trx_list': self.overlay_trx_list })
Pau Espin Pedrole6999122018-05-08 14:38:24 +0200145 return values
146
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100147########################
148# PUBLIC - INTERNAL API
149########################
150 @abstractmethod
151 def conf_for_bsc(self):
152 'Used by bsc objects to get path to socket.'
153 pass
154
155 def remote_addr(self):
156 return self.conf.get('addr')
157
Pau Espin Pedrol29b71322020-04-06 18:14:29 +0200158 def egprs_enabled(self):
159 return self.conf_for_bsc()['gprs_mode'] == 'egprs'
160
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100161 def cleanup(self):
162 'Nothing to do by default. Subclass can override if required.'
163 pass
164
Pau Espin Pedrola442cb82020-05-05 12:54:37 +0200165 def get_instance_by_type(testenv, conf):
Pau Espin Pedrol1ee5ec52020-05-04 17:16:39 +0200166 """Allocate a BTS child class based on type. Opts are passed to the newly created object."""
167 bts_type = conf.get('type')
168 if bts_type is None:
169 raise RuntimeError('BTS type is not defined!')
170
171 if bts_type == 'osmo-bts-sysmo':
172 from .bts_sysmo import SysmoBts
173 bts_class = SysmoBts
174 elif bts_type == 'osmo-bts-trx':
175 from .bts_osmotrx import OsmoBtsTrx
176 bts_class = OsmoBtsTrx
177 elif bts_type == 'osmo-bts-oc2g':
178 from .bts_oc2g import OsmoBtsOC2G
179 bts_class = OsmoBtsOC2G
180 elif bts_type == 'osmo-bts-octphy':
181 from .bts_octphy import OsmoBtsOctphy
182 bts_class = OsmoBtsOctphy
183 elif bts_type == 'osmo-bts-virtual':
184 from .bts_osmovirtual import OsmoBtsVirtual
185 bts_class = OsmoBtsVirtual
186 elif bts_type == 'nanobts':
187 from .bts_nanobts import NanoBts
188 bts_class = NanoBts
189 else:
190 raise log.Error('BTS type not supported:', bts_type)
Pau Espin Pedrola442cb82020-05-05 12:54:37 +0200191 return bts_class(testenv, conf)
Pau Espin Pedrol1ee5ec52020-05-04 17:16:39 +0200192
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100193###################
194# PUBLIC (test API included)
195###################
196 @abstractmethod
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +0200197 def start(self, keepalive=False):
198 '''Starts BTS. If keepalive is set, it will expect internal issues and
199 respawn related processes when detected'''
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100200 pass
201
202 @abstractmethod
203 def ready_for_pcu(self):
204 'True if the BTS is prepared to have a PCU connected, false otherwise'
205 pass
206
207 @abstractmethod
208 def pcu(self):
209 'Get the Pcu object associated with the BTS'
210 pass
211
Pau Espin Pedrolf6166142018-10-11 17:49:34 +0200212 def bts_type(self):
213 'Get the type of BTS'
214 return self.conf.get('type')
215
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100216 def set_bsc(self, bsc):
217 self.bsc = bsc
218
219 def set_sgsn(self, sgsn):
220 self.sgsn = sgsn
221
222 def set_lac(self, lac):
223 self.lac = lac
224
225 def set_rac(self, rac):
226 self.rac = rac
227
228 def set_cellid(self, cellid):
229 self.cellid = cellid
230
231 def set_bvci(self, bvci):
232 self.bvci = bvci
233
Pau Espin Pedrol39df7f42018-05-07 13:49:33 +0200234 def set_num_trx(self, num_trx):
235 assert num_trx > 0
236 self._validate_new_num_trx(num_trx)
237 if num_trx == self._num_trx:
238 return
239 self._num_trx = num_trx
240 self.overlay_trx_list = Bts._trx_list_recreate(self.overlay_trx_list, num_trx)
241
242 def num_trx(self):
243 return self._num_trx
244
245 def set_trx_phy_channel(self, trx_idx, ts_idx, config):
246 assert trx_idx < self._num_trx
247 assert ts_idx < 8
248 schema.phy_channel_config(config) # validation
249 self.overlay_trx_list[trx_idx]['timeslot_list'][ts_idx]['phys_chan_config'] = config
250
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100251# vim: expandtab tabstop=4 shiftwidth=4