Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running an osmo-msc |
| 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 | |
Pau Espin Pedrol | 2d16f6f | 2017-05-30 15:33:57 +0200 | [diff] [blame] | 23 | from . import log, util, config, template, process, osmo_ctrl, pcap_recorder, smsc |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 24 | |
| 25 | class OsmoMsc(log.Origin): |
| 26 | suite_run = None |
| 27 | ip_address = None |
| 28 | run_dir = None |
| 29 | config_file = None |
| 30 | process = None |
| 31 | hlr = None |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 32 | config = None |
Pau Espin Pedrol | 2d16f6f | 2017-05-30 15:33:57 +0200 | [diff] [blame] | 33 | smsc = None |
Pau Espin Pedrol | ca126b1 | 2017-08-22 19:04:06 +0200 | [diff] [blame] | 34 | encryption = None |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 35 | |
| 36 | def __init__(self, suite_run, hlr, mgcpgw, ip_address): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 37 | super().__init__(log.C_RUN, 'osmo-msc_%s' % ip_address.get('addr')) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 38 | self.suite_run = suite_run |
| 39 | self.ip_address = ip_address |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 40 | self.hlr = hlr |
| 41 | self.mgcpgw = mgcpgw |
Pau Espin Pedrol | 2d16f6f | 2017-05-30 15:33:57 +0200 | [diff] [blame] | 42 | self.smsc = smsc.Smsc((ip_address.get('addr'), 2775)) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 43 | |
| 44 | def start(self): |
| 45 | self.log('Starting osmo-msc') |
Pau Espin Pedrol | d091233 | 2017-06-14 13:27:08 +0200 | [diff] [blame] | 46 | 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] | 47 | self.configure() |
| 48 | inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-msc'))) |
| 49 | binary = inst.child('bin', 'osmo-msc') |
| 50 | if not os.path.isfile(binary): |
| 51 | raise RuntimeError('Binary missing: %r' % binary) |
| 52 | lib = inst.child('lib') |
| 53 | if not os.path.isdir(lib): |
| 54 | raise RuntimeError('No lib/ in %r' % inst) |
| 55 | |
| 56 | iface = util.ip_to_iface(self.addr()) |
| 57 | pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), iface, |
| 58 | 'host %s and port not 22' % self.addr()) |
| 59 | |
| 60 | env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 61 | |
| 62 | self.dbg(run_dir=self.run_dir, binary=binary, env=env) |
| 63 | self.process = process.Process(self.name(), self.run_dir, |
| 64 | (binary, '-c', |
| 65 | os.path.abspath(self.config_file)), |
| 66 | env=env) |
| 67 | self.suite_run.remember_to_stop(self.process) |
| 68 | self.process.launch() |
| 69 | |
| 70 | def configure(self): |
| 71 | self.config_file = self.run_dir.new_file('osmo-msc.cfg') |
| 72 | self.dbg(config_file=self.config_file) |
| 73 | |
| 74 | values = dict(msc=config.get_defaults('msc')) |
| 75 | config.overlay(values, self.suite_run.config()) |
| 76 | config.overlay(values, dict(msc=dict(ip_address=self.ip_address))) |
| 77 | config.overlay(values, self.mgcpgw.conf_for_msc()) |
Neels Hofmeyr | 7b02ed0 | 2017-06-08 23:08:59 +0200 | [diff] [blame] | 78 | config.overlay(values, self.hlr.conf_for_msc()) |
Pau Espin Pedrol | 2d16f6f | 2017-05-30 15:33:57 +0200 | [diff] [blame] | 79 | config.overlay(values, self.smsc.get_config()) |
Pau Espin Pedrol | ca126b1 | 2017-08-22 19:04:06 +0200 | [diff] [blame] | 80 | |
| 81 | # runtime parameters: |
| 82 | if self.encryption is not None: |
| 83 | config.overlay(values, dict(msc=dict(net=dict(encryption=self.encryption)))) |
| 84 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 85 | self.config = values |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 86 | |
| 87 | self.dbg('MSC CONFIG:\n' + pprint.pformat(values)) |
| 88 | |
| 89 | with open(self.config_file, 'w') as f: |
| 90 | r = template.render('osmo-msc.cfg', values) |
| 91 | self.dbg(r) |
| 92 | f.write(r) |
| 93 | |
| 94 | def addr(self): |
| 95 | return self.ip_address.get('addr') |
| 96 | |
Pau Espin Pedrol | ca126b1 | 2017-08-22 19:04:06 +0200 | [diff] [blame] | 97 | def set_encryption(self, val): |
| 98 | self.encryption = val |
| 99 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 100 | def mcc(self): |
| 101 | return self.config['msc']['net']['mcc'] |
| 102 | |
| 103 | def mnc(self): |
| 104 | return self.config['msc']['net']['mnc'] |
| 105 | |
| 106 | def mcc_mnc(self): |
| 107 | return (self.mcc(), self.mnc()) |
| 108 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 109 | def subscriber_attached(self, *modems): |
| 110 | return self.imsi_attached(*[m.imsi() for m in modems]) |
| 111 | |
| 112 | def imsi_attached(self, *imsis): |
| 113 | attached = self.imsi_list_attached() |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 114 | log.dbg('attached:', attached) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 115 | return all([(imsi in attached) for imsi in imsis]) |
| 116 | |
| 117 | def imsi_list_attached(self): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 118 | return OsmoMscCtrl(self).subscriber_list_active() |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 119 | |
| 120 | def running(self): |
| 121 | return not self.process.terminated() |
| 122 | |
| 123 | |
| 124 | class OsmoMscCtrl(log.Origin): |
| 125 | PORT = 4255 |
| 126 | SUBSCR_LIST_ACTIVE_VAR = 'subscriber-list-active-v1' |
| 127 | |
| 128 | def __init__(self, msc): |
| 129 | self.msc = msc |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 130 | super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.msc.addr(), self.PORT)) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 131 | |
| 132 | def ctrl(self): |
| 133 | return osmo_ctrl.OsmoCtrl(self.msc.addr(), self.PORT) |
| 134 | |
| 135 | def subscriber_list_active(self): |
| 136 | aslist_str = "" |
| 137 | with self.ctrl() as ctrl: |
| 138 | ctrl.do_get(self.SUBSCR_LIST_ACTIVE_VAR) |
| 139 | # This is legacy code from the old osmo-gsm-tester. |
| 140 | # looks like this doesn't work for long data. |
| 141 | data = ctrl.receive() |
| 142 | while (len(data) > 0): |
| 143 | (answer, data) = ctrl.remove_ipa_ctrl_header(data) |
| 144 | answer_str = answer.decode('utf-8') |
| 145 | answer_str = answer_str.replace('\n', ' ') |
| 146 | aslist_str = answer_str |
| 147 | return aslist_str |
| 148 | |
| 149 | # vim: expandtab tabstop=4 shiftwidth=4 |