blob: 9105c2838e66d8e369f27241ee4b7fa60fb8639c [file] [log] [blame]
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +01001# 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
20import os
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010021import tempfile
22from abc import ABCMeta, abstractmethod
Holger Hans Peter Freytherd03acdf2018-09-23 18:06:48 +010023from . import log, bts, pcu_osmo
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010024
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010025class OsmoBts(bts.Bts, metaclass=ABCMeta):
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010026
27##############
28# PROTECTED
29##############
Pau Espin Pedrole5194622018-05-07 13:36:58 +020030 def __init__(self, suite_run, conf, name, defaults_cfg_name):
31 super().__init__(suite_run, conf, name, defaults_cfg_name)
Pau Espin Pedrol58603672018-08-09 13:45:55 +020032 self._pcu = None
33 self.proc_bts = None
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010034 if len(self.pcu_socket_path().encode()) > 107:
35 raise log.Error('Path for pcu socket is longer than max allowed len for unix socket path (107):', self.pcu_socket_path())
36
37########################
38# PUBLIC - INTERNAL API
39########################
40 @abstractmethod
41 def conf_for_bsc(self):
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010042 # coming from bts.Bts, we forward the implementation to children.
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010043 pass
44
45 @abstractmethod
46 def pcu_socket_path(self):
47 'Used by pcu objects to get path to socket.'
48 pass
49
50 @abstractmethod
51 def create_pcu(self):
52 'Used by base class. Subclass can create different pcu implementations.'
53 pass
54
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010055###################
56# PUBLIC (test API included)
57###################
58 @abstractmethod
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +020059 def start(self, keepalive=False):
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010060 # coming from bts.Bts, we forward the implementation to children.
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010061 pass
62
Pau Espin Pedrolf2e59722018-10-02 14:41:22 +020063 @abstractmethod
Pau Espin Pedrol4fbdc352018-03-08 17:47:35 +010064 def ready_for_pcu(self):
Pau Espin Pedrolf2e59722018-10-02 14:41:22 +020065 'Used by tests to know when BTS is prepared and PCU can be started.'
66 pass
Pau Espin Pedrol4fbdc352018-03-08 17:47:35 +010067
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010068 def pcu(self):
69 if self._pcu is None:
Pau Espin Pedrolc1b32782017-12-15 11:49:01 +010070 self._pcu = self.create_pcu()
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010071 return self._pcu
72
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010073class OsmoBtsMainUnit(OsmoBts, metaclass=ABCMeta):
74##############
75# PROTECTED
76##############
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010077
Pau Espin Pedrole5194622018-05-07 13:36:58 +020078 def __init__(self, suite_run, conf, name, defaults_cfg_name):
Pau Espin Pedrol58603672018-08-09 13:45:55 +020079 self.pcu_sk_tmp_dir = None
Pau Espin Pedrole5194622018-05-07 13:36:58 +020080 super().__init__(suite_run, conf, name, defaults_cfg_name)
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010081
82########################
83# PUBLIC - INTERNAL API
84########################
85 @abstractmethod
86 def conf_for_bsc(self):
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010087 # coming from bts.Bts, we forward the implementation to children.
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010088 pass
89
90 def cleanup(self):
91 if self.pcu_sk_tmp_dir:
92 try:
93 os.remove(self.pcu_socket_path())
94 except OSError:
95 pass
96 os.rmdir(self.pcu_sk_tmp_dir)
97
98 def create_pcu(self):
99 return pcu_osmo.OsmoPcu(self.suite_run, self, self.conf)
100
101 def pcu_socket_path(self):
102 if self.pcu_sk_tmp_dir is None:
103 self.pcu_sk_tmp_dir = tempfile.mkdtemp('', 'ogtpcusk')
104 return os.path.join(self.pcu_sk_tmp_dir, 'pcu_bts')
105
106###################
107# PUBLIC (test API included)
108###################
Pau Espin Pedrolf2e59722018-10-02 14:41:22 +0200109 def ready_for_pcu(self):
110 if not self.proc_bts or not self.proc_bts.is_running:
111 return False
112 return os.path.exists(self.pcu_socket_path())
113
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +0100114 @abstractmethod
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +0200115 def start(self, keepalive=False):
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100116 # coming from bts.Bts, we forward the implementation to children.
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +0100117 pass