Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 1 | # 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 Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 8 | # it under the terms of the GNU General Public License as |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 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 |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 15 | # GNU General Public License for more details. |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 16 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU General Public License |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import os |
| 21 | import pprint |
| 22 | |
| 23 | from . import log, util, config, template, process, osmo_ctrl, pcap_recorder |
| 24 | |
| 25 | class OsmoBsc(log.Origin): |
| 26 | suite_run = None |
| 27 | ip_address = None |
| 28 | run_dir = None |
| 29 | config_file = None |
| 30 | process = None |
| 31 | bts = None |
Pau Espin Pedrol | 387526a | 2017-08-24 15:12:59 +0200 | [diff] [blame] | 32 | encryption = None |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 33 | |
| 34 | def __init__(self, suite_run, msc, ip_address): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 35 | super().__init__(log.C_RUN, 'osmo-bsc_%s' % ip_address.get('addr')) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 36 | self.suite_run = suite_run |
| 37 | self.ip_address = ip_address |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 38 | self.bts = [] |
| 39 | self.msc = msc |
| 40 | |
| 41 | def start(self): |
| 42 | self.log('Starting osmo-bsc') |
Pau Espin Pedrol | d091233 | 2017-06-14 13:27:08 +0200 | [diff] [blame] | 43 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 44 | self.configure() |
| 45 | |
Neels Hofmeyr | 36e0404 | 2017-09-04 16:49:17 +0200 | [diff] [blame] | 46 | inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bsc'))) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 47 | |
| 48 | binary = inst.child('bin', 'osmo-bsc') |
| 49 | if not os.path.isfile(binary): |
| 50 | raise RuntimeError('Binary missing: %r' % binary) |
| 51 | lib = inst.child('lib') |
| 52 | if not os.path.isdir(lib): |
| 53 | raise RuntimeError('No lib/ in %r' % inst) |
| 54 | |
| 55 | iface = util.ip_to_iface(self.addr()) |
| 56 | pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), iface, |
| 57 | 'host %s and port not 22' % self.addr()) |
| 58 | |
| 59 | env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 60 | |
| 61 | self.dbg(run_dir=self.run_dir, binary=binary, env=env) |
| 62 | self.process = process.Process(self.name(), self.run_dir, |
| 63 | (binary, '-c', |
| 64 | os.path.abspath(self.config_file)), |
| 65 | env=env) |
| 66 | self.suite_run.remember_to_stop(self.process) |
| 67 | self.process.launch() |
| 68 | |
| 69 | def configure(self): |
| 70 | self.config_file = self.run_dir.new_file('osmo-bsc.cfg') |
| 71 | self.dbg(config_file=self.config_file) |
| 72 | |
| 73 | values = dict(bsc=config.get_defaults('bsc')) |
| 74 | config.overlay(values, self.suite_run.config()) |
| 75 | config.overlay(values, dict(bsc=dict(ip_address=self.ip_address))) |
| 76 | |
| 77 | bts_list = [] |
| 78 | for bts in self.bts: |
| 79 | bts_list.append(bts.conf_for_bsc()) |
| 80 | config.overlay(values, dict(bsc=dict(net=dict(bts_list=bts_list)))) |
| 81 | |
Pau Espin Pedrol | 387526a | 2017-08-24 15:12:59 +0200 | [diff] [blame] | 82 | # runtime parameters: |
| 83 | if self.encryption is not None: |
| 84 | config.overlay(values, dict(bsc=dict(net=dict(encryption=self.encryption)))) |
| 85 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 86 | self.dbg('BSC CONFIG:\n' + pprint.pformat(values)) |
| 87 | |
| 88 | with open(self.config_file, 'w') as f: |
| 89 | r = template.render('osmo-bsc.cfg', values) |
| 90 | self.dbg(r) |
| 91 | f.write(r) |
| 92 | |
| 93 | def addr(self): |
| 94 | return self.ip_address.get('addr') |
| 95 | |
Pau Espin Pedrol | 387526a | 2017-08-24 15:12:59 +0200 | [diff] [blame] | 96 | def set_encryption(self, val): |
| 97 | self.encryption = val |
| 98 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 99 | def bts_add(self, bts): |
| 100 | self.bts.append(bts) |
| 101 | bts.set_bsc(self) |
| 102 | |
| 103 | def running(self): |
| 104 | return not self.process.terminated() |
| 105 | |
| 106 | # vim: expandtab tabstop=4 shiftwidth=4 |