blob: 8629b9bf808df4c82e0f1f4e65060d5625e98854 [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
21import pprint
22import tempfile
23from abc import ABCMeta, abstractmethod
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +020024from . import log, config, util, template, process, bts, pcu_osmo
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010025
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010026class OsmoBts(bts.Bts, metaclass=ABCMeta):
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010027 proc_bts = None
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010028 _pcu = None
29
30##############
31# PROTECTED
32##############
Pau Espin Pedrole5194622018-05-07 13:36:58 +020033 def __init__(self, suite_run, conf, name, defaults_cfg_name):
34 super().__init__(suite_run, conf, name, defaults_cfg_name)
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010035 if len(self.pcu_socket_path().encode()) > 107:
36 raise log.Error('Path for pcu socket is longer than max allowed len for unix socket path (107):', self.pcu_socket_path())
37
Pau Espin Pedrole5194622018-05-07 13:36:58 +020038 def conf_for_bsc_prepare(self):
Pau Espin Pedrol530681f2018-05-07 02:26:18 +020039 values = config.get_defaults('bsc_bts')
Pau Espin Pedrole5194622018-05-07 13:36:58 +020040 config.overlay(values, config.get_defaults(self.defaults_cfg_name))
Pau Espin Pedrol530681f2018-05-07 02:26:18 +020041 if self.lac is not None:
42 config.overlay(values, { 'location_area_code': self.lac })
43 if self.rac is not None:
44 config.overlay(values, { 'routing_area_code': self.rac })
45 if self.cellid is not None:
46 config.overlay(values, { 'cell_identity': self.cellid })
47 if self.bvci is not None:
48 config.overlay(values, { 'bvci': self.bvci })
49 config.overlay(values, self.conf)
50
51 sgsn_conf = {} if self.sgsn is None else self.sgsn.conf_for_client()
52 config.overlay(values, sgsn_conf)
53 return values
54
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010055########################
56# PUBLIC - INTERNAL API
57########################
58 @abstractmethod
59 def conf_for_bsc(self):
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
63 @abstractmethod
64 def pcu_socket_path(self):
65 'Used by pcu objects to get path to socket.'
66 pass
67
68 @abstractmethod
69 def create_pcu(self):
70 'Used by base class. Subclass can create different pcu implementations.'
71 pass
72
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010073###################
74# PUBLIC (test API included)
75###################
76 @abstractmethod
77 def start(self):
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +010078 # coming from bts.Bts, we forward the implementation to children.
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010079 pass
80
Pau Espin Pedrol4fbdc352018-03-08 17:47:35 +010081 def ready_for_pcu(self):
82 if not self.proc_bts or not self.proc_bts.is_running:
83 return False
84 return 'BTS is up' in (self.proc_bts.get_stderr() or '')
85
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010086 def pcu(self):
87 if self._pcu is None:
Pau Espin Pedrolc1b32782017-12-15 11:49:01 +010088 self._pcu = self.create_pcu()
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010089 return self._pcu
90
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010091class OsmoBtsMainUnit(OsmoBts, metaclass=ABCMeta):
92##############
93# PROTECTED
94##############
95 pcu_sk_tmp_dir = None
96
Pau Espin Pedrole5194622018-05-07 13:36:58 +020097 def __init__(self, suite_run, conf, name, defaults_cfg_name):
98 super().__init__(suite_run, conf, name, defaults_cfg_name)
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010099
100########################
101# PUBLIC - INTERNAL API
102########################
103 @abstractmethod
104 def conf_for_bsc(self):
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100105 # coming from bts.Bts, we forward the implementation to children.
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +0100106 pass
107
108 def cleanup(self):
109 if self.pcu_sk_tmp_dir:
110 try:
111 os.remove(self.pcu_socket_path())
112 except OSError:
113 pass
114 os.rmdir(self.pcu_sk_tmp_dir)
115
116 def create_pcu(self):
117 return pcu_osmo.OsmoPcu(self.suite_run, self, self.conf)
118
119 def pcu_socket_path(self):
120 if self.pcu_sk_tmp_dir is None:
121 self.pcu_sk_tmp_dir = tempfile.mkdtemp('', 'ogtpcusk')
122 return os.path.join(self.pcu_sk_tmp_dir, 'pcu_bts')
123
124###################
125# PUBLIC (test API included)
126###################
127 @abstractmethod
128 def start(self):
Pau Espin Pedrol52ad3a62018-03-08 17:50:14 +0100129 # coming from bts.Bts, we forward the implementation to children.
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +0100130 pass