Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 1 | # osmo_gsm_tester: base classes to share code among BTS subclasses. |
| 2 | # |
| 3 | # Copyright (C) 2016-2017 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 | |
| 20 | import os |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 21 | import tempfile |
| 22 | from abc import ABCMeta, abstractmethod |
Pau Espin Pedrol | e1a58bd | 2020-04-10 20:46:07 +0200 | [diff] [blame] | 23 | from ..core import log |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 24 | from ..core import schema |
Pau Espin Pedrol | e8bbcbf | 2020-04-10 19:51:31 +0200 | [diff] [blame] | 25 | from . import bts, pcu_osmo |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 26 | |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 27 | def on_register_schemas(): |
| 28 | resource_schema = { |
| 29 | 'ipa_unit_id': schema.UINT, |
| 30 | 'direct_pcu': schema.BOOL_STR, |
| 31 | 'channel_allocator': schema.CHAN_ALLOCATOR, |
| 32 | 'gprs_mode': schema.GPRS_MODE, |
| 33 | } |
| 34 | schema.register_resource_schema('bts', resource_schema) |
| 35 | |
Pau Espin Pedrol | 52ad3a6 | 2018-03-08 17:50:14 +0100 | [diff] [blame] | 36 | class OsmoBts(bts.Bts, metaclass=ABCMeta): |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 37 | |
| 38 | ############## |
| 39 | # PROTECTED |
| 40 | ############## |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 41 | def __init__(self, testenv, conf, name, defaults_cfg_name): |
| 42 | super().__init__(testenv, conf, name, defaults_cfg_name) |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 43 | self._pcu = None |
| 44 | self.proc_bts = None |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 45 | if len(self.pcu_socket_path().encode()) > 107: |
| 46 | raise log.Error('Path for pcu socket is longer than max allowed len for unix socket path (107):', self.pcu_socket_path()) |
| 47 | |
| 48 | ######################## |
| 49 | # PUBLIC - INTERNAL API |
| 50 | ######################## |
| 51 | @abstractmethod |
| 52 | def conf_for_bsc(self): |
Pau Espin Pedrol | 52ad3a6 | 2018-03-08 17:50:14 +0100 | [diff] [blame] | 53 | # coming from bts.Bts, we forward the implementation to children. |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 54 | pass |
| 55 | |
| 56 | @abstractmethod |
| 57 | def pcu_socket_path(self): |
| 58 | 'Used by pcu objects to get path to socket.' |
| 59 | pass |
| 60 | |
| 61 | @abstractmethod |
| 62 | def create_pcu(self): |
| 63 | 'Used by base class. Subclass can create different pcu implementations.' |
| 64 | pass |
| 65 | |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 66 | ################### |
| 67 | # PUBLIC (test API included) |
| 68 | ################### |
| 69 | @abstractmethod |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 70 | def start(self, keepalive=False): |
Pau Espin Pedrol | 52ad3a6 | 2018-03-08 17:50:14 +0100 | [diff] [blame] | 71 | # coming from bts.Bts, we forward the implementation to children. |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 72 | pass |
| 73 | |
Pau Espin Pedrol | f2e5972 | 2018-10-02 14:41:22 +0200 | [diff] [blame] | 74 | @abstractmethod |
Pau Espin Pedrol | 4fbdc35 | 2018-03-08 17:47:35 +0100 | [diff] [blame] | 75 | def ready_for_pcu(self): |
Pau Espin Pedrol | f2e5972 | 2018-10-02 14:41:22 +0200 | [diff] [blame] | 76 | 'Used by tests to know when BTS is prepared and PCU can be started.' |
| 77 | pass |
Pau Espin Pedrol | 4fbdc35 | 2018-03-08 17:47:35 +0100 | [diff] [blame] | 78 | |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 79 | def pcu(self): |
| 80 | if self._pcu is None: |
Pau Espin Pedrol | c1b3278 | 2017-12-15 11:49:01 +0100 | [diff] [blame] | 81 | self._pcu = self.create_pcu() |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 82 | return self._pcu |
| 83 | |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 84 | class OsmoBtsMainUnit(OsmoBts, metaclass=ABCMeta): |
| 85 | ############## |
| 86 | # PROTECTED |
| 87 | ############## |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 88 | |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 89 | def __init__(self, testenv, conf, name, defaults_cfg_name): |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 90 | self.pcu_sk_tmp_dir = None |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 91 | super().__init__(testenv, conf, name, defaults_cfg_name) |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 92 | |
| 93 | ######################## |
| 94 | # PUBLIC - INTERNAL API |
| 95 | ######################## |
| 96 | @abstractmethod |
| 97 | def conf_for_bsc(self): |
Pau Espin Pedrol | 52ad3a6 | 2018-03-08 17:50:14 +0100 | [diff] [blame] | 98 | # coming from bts.Bts, we forward the implementation to children. |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 99 | pass |
| 100 | |
| 101 | def cleanup(self): |
| 102 | if self.pcu_sk_tmp_dir: |
| 103 | try: |
| 104 | os.remove(self.pcu_socket_path()) |
| 105 | except OSError: |
| 106 | pass |
| 107 | os.rmdir(self.pcu_sk_tmp_dir) |
| 108 | |
| 109 | def create_pcu(self): |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 110 | return pcu_osmo.OsmoPcu(self.testenv, self, self.conf) |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 111 | |
| 112 | def pcu_socket_path(self): |
| 113 | if self.pcu_sk_tmp_dir is None: |
| 114 | self.pcu_sk_tmp_dir = tempfile.mkdtemp('', 'ogtpcusk') |
| 115 | return os.path.join(self.pcu_sk_tmp_dir, 'pcu_bts') |
| 116 | |
| 117 | ################### |
| 118 | # PUBLIC (test API included) |
| 119 | ################### |
Pau Espin Pedrol | f2e5972 | 2018-10-02 14:41:22 +0200 | [diff] [blame] | 120 | def ready_for_pcu(self): |
| 121 | if not self.proc_bts or not self.proc_bts.is_running: |
| 122 | return False |
| 123 | return os.path.exists(self.pcu_socket_path()) |
| 124 | |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 125 | @abstractmethod |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 126 | def start(self, keepalive=False): |
Pau Espin Pedrol | 52ad3a6 | 2018-03-08 17:50:14 +0100 | [diff] [blame] | 127 | # coming from bts.Bts, we forward the implementation to children. |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 128 | pass |