blob: bd8649b00481ac48e0e463af1be226e6a51d5273 [file] [log] [blame]
Neels Hofmeyr798e5922017-05-18 15:24:02 +02001# osmo_gsm_tester: specifics for running an osmo-bsc
2#
3# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Neels Hofmeyr <neels@hofmeyr.de>
6#
7# This program is free software: you can redistribute it and/or modify
Harald Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Neels Hofmeyr798e5922017-05-18 15:24:02 +02009# 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
Harald Welte27205342017-06-03 09:51:45 +020015# GNU General Public License for more details.
Neels Hofmeyr798e5922017-05-18 15:24:02 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Neels Hofmeyr798e5922017-05-18 15:24:02 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
Pau Espin Pedrol7bd71032017-12-11 15:02:26 +010021import re
Neels Hofmeyr798e5922017-05-18 15:24:02 +020022import pprint
Neels Hofmeyraf4e2312020-11-27 08:20:56 +010023import re
Neels Hofmeyr798e5922017-05-18 15:24:02 +020024
Pau Espin Pedrole1a58bd2020-04-10 20:46:07 +020025from ..core import log, util, config, template, process
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020026from ..core import schema
Neels Hofmeyraf4e2312020-11-27 08:20:56 +010027from . import osmo_ctrl, osmo_vty, pcap_recorder
Neels Hofmeyr798e5922017-05-18 15:24:02 +020028
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020029def on_register_schemas():
30 config_schema = {
31 'net.codec_list[]': schema.CODEC,
32 }
33 schema.register_config_schema('bsc', config_schema)
34
35
Neels Hofmeyr798e5922017-05-18 15:24:02 +020036class OsmoBsc(log.Origin):
Neels Hofmeyr798e5922017-05-18 15:24:02 +020037
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020038 def __init__(self, testenv, msc, mgw, stp, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020039 super().__init__(log.C_RUN, 'osmo-bsc_%s' % ip_address.get('addr'))
Pau Espin Pedrol58603672018-08-09 13:45:55 +020040 self.run_dir = None
41 self.config_file = None
42 self.process = None
43 self.encryption = None
44 self.rsl_ip = None
Pau Espin Pedrolfed578e2019-06-19 17:18:43 +020045 self.use_osmux = "off"
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020046 self.testenv = testenv
Neels Hofmeyr798e5922017-05-18 15:24:02 +020047 self.ip_address = ip_address
Neels Hofmeyr798e5922017-05-18 15:24:02 +020048 self.bts = []
49 self.msc = msc
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +010050 self.mgw = mgw
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010051 self.stp = stp
Neels Hofmeyraf4e2312020-11-27 08:20:56 +010052 self.vty = None
Neels Hofmeyr798e5922017-05-18 15:24:02 +020053
54 def start(self):
55 self.log('Starting osmo-bsc')
Pau Espin Pedrol2a2d8462020-05-11 10:56:52 +020056 self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name()))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020057 self.configure()
58
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020059 inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmo-bsc')))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020060
61 binary = inst.child('bin', 'osmo-bsc')
62 if not os.path.isfile(binary):
63 raise RuntimeError('Binary missing: %r' % binary)
64 lib = inst.child('lib')
65 if not os.path.isdir(lib):
66 raise RuntimeError('No lib/ in %r' % inst)
67
Pau Espin Pedrol16abefb2018-10-09 18:54:45 +020068 if self.rsl_ip and self.addr() != self.rsl_ip:
69 filter = 'host %s or host %s and port not 22' % (self.addr(), self.rsl_ip)
70 else:
71 filter = 'host %s and port not 22' % self.addr()
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020072 pcap_recorder.PcapRecorder(self.testenv, self.run_dir.new_dir('pcap'), None, filter)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020073
74 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
75
76 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
77 self.process = process.Process(self.name(), self.run_dir,
78 (binary, '-c',
79 os.path.abspath(self.config_file)),
80 env=env)
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020081 self.testenv.remember_to_stop(self.process)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020082 self.process.launch()
83
Neels Hofmeyraf4e2312020-11-27 08:20:56 +010084 self.vty = OsmoBscVty(self)
85 self.vty.connect()
86
Neels Hofmeyr798e5922017-05-18 15:24:02 +020087 def configure(self):
88 self.config_file = self.run_dir.new_file('osmo-bsc.cfg')
89 self.dbg(config_file=self.config_file)
90
91 values = dict(bsc=config.get_defaults('bsc'))
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020092 config.overlay(values, self.testenv.suite().config())
Neels Hofmeyr798e5922017-05-18 15:24:02 +020093 config.overlay(values, dict(bsc=dict(ip_address=self.ip_address)))
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +010094 config.overlay(values, self.mgw.conf_for_client())
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010095 config.overlay(values, self.stp.conf_for_client())
Neels Hofmeyr798e5922017-05-18 15:24:02 +020096
97 bts_list = []
98 for bts in self.bts:
99 bts_list.append(bts.conf_for_bsc())
100 config.overlay(values, dict(bsc=dict(net=dict(bts_list=bts_list))))
101
Pau Espin Pedrol387526a2017-08-24 15:12:59 +0200102 # runtime parameters:
103 if self.encryption is not None:
Pau Espin Pedrolabd556a2017-09-04 16:26:08 +0200104 encryption_vty = util.encryption2osmovty(self.encryption)
105 else:
106 encryption_vty = util.encryption2osmovty(values['bsc']['net']['encryption'])
107 config.overlay(values, dict(bsc=dict(net=dict(encryption=encryption_vty))))
Pau Espin Pedrolfed578e2019-06-19 17:18:43 +0200108 config.overlay(values, dict(bsc=dict(use_osmux=self.use_osmux)))
Pau Espin Pedrol387526a2017-08-24 15:12:59 +0200109
Pau Espin Pedrol63f2d472018-05-22 18:24:50 +0200110 if self.rsl_ip is not None:
111 config.overlay(values, dict(bsc=dict(net=dict(rsl_ip=self.rsl_ip))))
112
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200113 self.dbg('BSC CONFIG:\n' + pprint.pformat(values))
114
115 with open(self.config_file, 'w') as f:
116 r = template.render('osmo-bsc.cfg', values)
117 self.dbg(r)
118 f.write(r)
119
120 def addr(self):
121 return self.ip_address.get('addr')
122
Pau Espin Pedrol387526a2017-08-24 15:12:59 +0200123 def set_encryption(self, val):
124 self.encryption = val
125
Pau Espin Pedrol63f2d472018-05-22 18:24:50 +0200126 def set_rsl_ip(self, ip_addr):
127 '''Overwrite RSL IPaddr option sent to all BTS during OML config. Useful
128 for tests only willing to use osmo-bsc to do the OML setup but using
129 other external entities to test the RSL path, such as TTCN3 tests.'''
130 self.rsl_ip = ip_addr
131
Pau Espin Pedrolfed578e2019-06-19 17:18:43 +0200132 def set_use_osmux(self, use=False, force=False):
133 if not use:
134 self.use_osmux = "off"
135 else:
136 if not force:
137 self.use_osmux = "on"
138 else:
139 self.use_osmux = "only"
140
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200141 def bts_add(self, bts):
142 self.bts.append(bts)
143 bts.set_bsc(self)
144
Pau Espin Pedrol7bd71032017-12-11 15:02:26 +0100145 def bts_num(self, bts):
146 'Provide number id used by OsmoNITB to identify configured BTS'
147 # We take advantage from the fact that VTY code assigns VTY in ascending
148 # order through the bts nodes found. As we populate the config iterating
149 # over this list, we have a 1:1 match in indexes.
150 return self.bts.index(bts)
151
152 def bts_is_connected(self, bts):
153 return OsmoBscCtrl(self).bts_is_connected(self.bts_num(bts))
154
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200155 def running(self):
156 return not self.process.terminated()
157
Neels Hofmeyraf4e2312020-11-27 08:20:56 +0100158 def cleanup(self):
159 if self.vty is not None:
160 self.vty.disconnect()
161 self.vty = None
Pau Espin Pedrol7bd71032017-12-11 15:02:26 +0100162
163class OsmoBscCtrl(log.Origin):
164 PORT = 4249
165 BTS_OML_STATE_VAR = "bts.%d.oml-connection-state"
166 BTS_OML_STATE_RE = re.compile("GET_REPLY (\d+) bts.\d+.oml-connection-state (?P<oml_state>\w+)")
167
168 def __init__(self, bsc):
169 self.bsc = bsc
170 super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.bsc.addr(), OsmoBscCtrl.PORT))
171
172 def ctrl(self):
173 return osmo_ctrl.OsmoCtrl(self.bsc.addr(), OsmoBscCtrl.PORT)
174
175 def bts_is_connected(self, bts_num):
176 with self.ctrl() as ctrl:
177 ctrl.do_get(OsmoBscCtrl.BTS_OML_STATE_VAR % bts_num)
178 data = ctrl.receive()
179 while (len(data) > 0):
180 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
181 answer_str = answer.decode('utf-8')
182 answer_str = answer_str.replace('\n', ' ')
183 res = OsmoBscCtrl.BTS_OML_STATE_RE.match(answer_str)
184 if res:
185 oml_state = str(res.group('oml_state'))
186 if oml_state == 'connected':
187 return True
188 return False
189
Neels Hofmeyraf4e2312020-11-27 08:20:56 +0100190class OsmoBscVty(osmo_vty.OsmoVty):
191 def __init__(self, bsc, port=4242):
192 self.bsc = bsc
193 super().__init__(self.bsc.addr(), port)
194
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200195# vim: expandtab tabstop=4 shiftwidth=4