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 |
Pau Espin Pedrol | 7bd7103 | 2017-12-11 15:02:26 +0100 | [diff] [blame] | 21 | import re |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 22 | import pprint |
| 23 | |
| 24 | from . import log, util, config, template, process, osmo_ctrl, pcap_recorder |
| 25 | |
| 26 | class OsmoBsc(log.Origin): |
| 27 | suite_run = None |
| 28 | ip_address = None |
| 29 | run_dir = None |
| 30 | config_file = None |
| 31 | process = None |
| 32 | bts = None |
Pau Espin Pedrol | 387526a | 2017-08-24 15:12:59 +0200 | [diff] [blame] | 33 | encryption = None |
Pau Espin Pedrol | 63f2d47 | 2018-05-22 18:24:50 +0200 | [diff] [blame] | 34 | rsl_ip = None |
Pau Espin Pedrol | 386b78d | 2017-11-09 13:02:09 +0100 | [diff] [blame] | 35 | mgw = None |
Pau Espin Pedrol | 1e1d381 | 2017-11-16 18:06:37 +0100 | [diff] [blame] | 36 | stp = None |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 37 | |
Pau Espin Pedrol | 1e1d381 | 2017-11-16 18:06:37 +0100 | [diff] [blame] | 38 | def __init__(self, suite_run, msc, mgw, stp, ip_address): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 39 | super().__init__(log.C_RUN, 'osmo-bsc_%s' % ip_address.get('addr')) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 40 | self.suite_run = suite_run |
| 41 | self.ip_address = ip_address |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 42 | self.bts = [] |
| 43 | self.msc = msc |
Pau Espin Pedrol | 386b78d | 2017-11-09 13:02:09 +0100 | [diff] [blame] | 44 | self.mgw = mgw |
Pau Espin Pedrol | 1e1d381 | 2017-11-16 18:06:37 +0100 | [diff] [blame] | 45 | self.stp = stp |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 46 | |
| 47 | def start(self): |
| 48 | self.log('Starting osmo-bsc') |
Pau Espin Pedrol | d091233 | 2017-06-14 13:27:08 +0200 | [diff] [blame] | 49 | 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] | 50 | self.configure() |
| 51 | |
Neels Hofmeyr | 36e0404 | 2017-09-04 16:49:17 +0200 | [diff] [blame] | 52 | 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] | 53 | |
| 54 | binary = inst.child('bin', 'osmo-bsc') |
| 55 | if not os.path.isfile(binary): |
| 56 | raise RuntimeError('Binary missing: %r' % binary) |
| 57 | lib = inst.child('lib') |
| 58 | if not os.path.isdir(lib): |
| 59 | raise RuntimeError('No lib/ in %r' % inst) |
| 60 | |
Pau Espin Pedrol | 9cc1d08 | 2017-11-20 17:40:25 +0100 | [diff] [blame] | 61 | pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None, |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 62 | 'host %s and port not 22' % self.addr()) |
| 63 | |
| 64 | env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 65 | |
| 66 | self.dbg(run_dir=self.run_dir, binary=binary, env=env) |
| 67 | self.process = process.Process(self.name(), self.run_dir, |
| 68 | (binary, '-c', |
| 69 | os.path.abspath(self.config_file)), |
| 70 | env=env) |
| 71 | self.suite_run.remember_to_stop(self.process) |
| 72 | self.process.launch() |
| 73 | |
| 74 | def configure(self): |
| 75 | self.config_file = self.run_dir.new_file('osmo-bsc.cfg') |
| 76 | self.dbg(config_file=self.config_file) |
| 77 | |
| 78 | values = dict(bsc=config.get_defaults('bsc')) |
| 79 | config.overlay(values, self.suite_run.config()) |
| 80 | config.overlay(values, dict(bsc=dict(ip_address=self.ip_address))) |
Pau Espin Pedrol | 386b78d | 2017-11-09 13:02:09 +0100 | [diff] [blame] | 81 | config.overlay(values, self.mgw.conf_for_client()) |
Pau Espin Pedrol | 1e1d381 | 2017-11-16 18:06:37 +0100 | [diff] [blame] | 82 | config.overlay(values, self.stp.conf_for_client()) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 83 | |
| 84 | bts_list = [] |
| 85 | for bts in self.bts: |
| 86 | bts_list.append(bts.conf_for_bsc()) |
| 87 | config.overlay(values, dict(bsc=dict(net=dict(bts_list=bts_list)))) |
| 88 | |
Pau Espin Pedrol | 387526a | 2017-08-24 15:12:59 +0200 | [diff] [blame] | 89 | # runtime parameters: |
| 90 | if self.encryption is not None: |
Pau Espin Pedrol | abd556a | 2017-09-04 16:26:08 +0200 | [diff] [blame] | 91 | encryption_vty = util.encryption2osmovty(self.encryption) |
| 92 | else: |
| 93 | encryption_vty = util.encryption2osmovty(values['bsc']['net']['encryption']) |
| 94 | config.overlay(values, dict(bsc=dict(net=dict(encryption=encryption_vty)))) |
Pau Espin Pedrol | 387526a | 2017-08-24 15:12:59 +0200 | [diff] [blame] | 95 | |
Pau Espin Pedrol | 63f2d47 | 2018-05-22 18:24:50 +0200 | [diff] [blame] | 96 | if self.rsl_ip is not None: |
| 97 | config.overlay(values, dict(bsc=dict(net=dict(rsl_ip=self.rsl_ip)))) |
| 98 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 99 | self.dbg('BSC CONFIG:\n' + pprint.pformat(values)) |
| 100 | |
| 101 | with open(self.config_file, 'w') as f: |
| 102 | r = template.render('osmo-bsc.cfg', values) |
| 103 | self.dbg(r) |
| 104 | f.write(r) |
| 105 | |
| 106 | def addr(self): |
| 107 | return self.ip_address.get('addr') |
| 108 | |
Pau Espin Pedrol | 387526a | 2017-08-24 15:12:59 +0200 | [diff] [blame] | 109 | def set_encryption(self, val): |
| 110 | self.encryption = val |
| 111 | |
Pau Espin Pedrol | 63f2d47 | 2018-05-22 18:24:50 +0200 | [diff] [blame] | 112 | def set_rsl_ip(self, ip_addr): |
| 113 | '''Overwrite RSL IPaddr option sent to all BTS during OML config. Useful |
| 114 | for tests only willing to use osmo-bsc to do the OML setup but using |
| 115 | other external entities to test the RSL path, such as TTCN3 tests.''' |
| 116 | self.rsl_ip = ip_addr |
| 117 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 118 | def bts_add(self, bts): |
| 119 | self.bts.append(bts) |
| 120 | bts.set_bsc(self) |
| 121 | |
Pau Espin Pedrol | 7bd7103 | 2017-12-11 15:02:26 +0100 | [diff] [blame] | 122 | def bts_num(self, bts): |
| 123 | 'Provide number id used by OsmoNITB to identify configured BTS' |
| 124 | # We take advantage from the fact that VTY code assigns VTY in ascending |
| 125 | # order through the bts nodes found. As we populate the config iterating |
| 126 | # over this list, we have a 1:1 match in indexes. |
| 127 | return self.bts.index(bts) |
| 128 | |
| 129 | def bts_is_connected(self, bts): |
| 130 | return OsmoBscCtrl(self).bts_is_connected(self.bts_num(bts)) |
| 131 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 132 | def running(self): |
| 133 | return not self.process.terminated() |
| 134 | |
Pau Espin Pedrol | 7bd7103 | 2017-12-11 15:02:26 +0100 | [diff] [blame] | 135 | |
| 136 | class OsmoBscCtrl(log.Origin): |
| 137 | PORT = 4249 |
| 138 | BTS_OML_STATE_VAR = "bts.%d.oml-connection-state" |
| 139 | BTS_OML_STATE_RE = re.compile("GET_REPLY (\d+) bts.\d+.oml-connection-state (?P<oml_state>\w+)") |
| 140 | |
| 141 | def __init__(self, bsc): |
| 142 | self.bsc = bsc |
| 143 | super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.bsc.addr(), OsmoBscCtrl.PORT)) |
| 144 | |
| 145 | def ctrl(self): |
| 146 | return osmo_ctrl.OsmoCtrl(self.bsc.addr(), OsmoBscCtrl.PORT) |
| 147 | |
| 148 | def bts_is_connected(self, bts_num): |
| 149 | with self.ctrl() as ctrl: |
| 150 | ctrl.do_get(OsmoBscCtrl.BTS_OML_STATE_VAR % bts_num) |
| 151 | data = ctrl.receive() |
| 152 | while (len(data) > 0): |
| 153 | (answer, data) = ctrl.remove_ipa_ctrl_header(data) |
| 154 | answer_str = answer.decode('utf-8') |
| 155 | answer_str = answer_str.replace('\n', ' ') |
| 156 | res = OsmoBscCtrl.BTS_OML_STATE_RE.match(answer_str) |
| 157 | if res: |
| 158 | oml_state = str(res.group('oml_state')) |
| 159 | if oml_state == 'connected': |
| 160 | return True |
| 161 | return False |
| 162 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 163 | # vim: expandtab tabstop=4 shiftwidth=4 |