Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running an osmo-nitb |
| 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 |
| 8 | # it under the terms of the GNU Affero 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 Affero General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU Affero General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import os |
| 21 | import random |
| 22 | import re |
| 23 | import socket |
| 24 | |
| 25 | from . import log, util, config, template, process, osmo_ctrl |
| 26 | |
| 27 | class OsmoNitb(log.Origin): |
| 28 | suite_run = None |
| 29 | nitb_iface = None |
| 30 | run_dir = None |
| 31 | config_file = None |
| 32 | process = None |
| 33 | bts = None |
| 34 | |
| 35 | def __init__(self, suite_run, nitb_iface): |
| 36 | self.suite_run = suite_run |
| 37 | self.nitb_iface = nitb_iface |
| 38 | self.set_log_category(log.C_RUN) |
| 39 | self.set_name('osmo-nitb_%s' % nitb_iface.get('addr')) |
| 40 | self.bts = [] |
| 41 | |
| 42 | def start(self): |
| 43 | self.log('Starting osmo-nitb') |
| 44 | self.run_dir = util.Dir(self.suite_run.trial.get_run_dir().new_dir(self.name())) |
| 45 | self.configure() |
Your Name | 3c6673a | 2017-04-08 18:52:39 +0200 | [diff] [blame] | 46 | inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-nitb'))) |
| 47 | binary = inst.child('bin', 'osmo-nitb') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 48 | if not os.path.isfile(binary): |
| 49 | raise RuntimeError('Binary missing: %r' % binary) |
Your Name | 3c6673a | 2017-04-08 18:52:39 +0200 | [diff] [blame] | 50 | lib = inst.child('lib') |
| 51 | if not os.path.isdir(lib): |
| 52 | raise RuntimeError('No lib/ in %r' % inst) |
| 53 | env = { 'LD_LIBRARY_PATH': lib } |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 54 | self.dbg(run_dir=self.run_dir, binary=binary, env=env) |
| 55 | self.process = process.Process(self.name(), self.run_dir, |
| 56 | (binary, '-c', |
| 57 | os.path.abspath(self.config_file)), |
| 58 | env=env) |
| 59 | self.suite_run.remember_to_stop(self.process) |
| 60 | self.process.launch() |
| 61 | |
| 62 | def configure(self): |
| 63 | self.config_file = self.run_dir.new_file('osmo-nitb.cfg') |
| 64 | self.dbg(config_file=self.config_file) |
| 65 | |
| 66 | values = dict(nitb=config.get_defaults('nitb')) |
| 67 | config.overlay(values, self.suite_run.config()) |
| 68 | config.overlay(values, dict(nitb_iface=self.nitb_iface)) |
| 69 | |
| 70 | bts_list = [] |
| 71 | for bts in self.bts: |
| 72 | bts_list.append(bts.conf_for_nitb()) |
| 73 | config.overlay(values, dict(nitb=dict(net=dict(bts_list=bts_list)))) |
| 74 | |
| 75 | self.dbg(conf=values) |
| 76 | |
| 77 | with open(self.config_file, 'w') as f: |
| 78 | r = template.render('osmo-nitb.cfg', values) |
| 79 | self.dbg(r) |
| 80 | f.write(r) |
| 81 | |
| 82 | def addr(self): |
| 83 | return self.nitb_iface.get('addr') |
| 84 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 85 | def bts_add(self, bts): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 86 | self.bts.append(bts) |
| 87 | bts.set_nitb(self) |
| 88 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 89 | def subscriber_add(self, modem, msisdn=None): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 90 | if msisdn is None: |
| 91 | msisdn = self.suite_run.resources_pool.next_msisdn(modem) |
| 92 | modem.set_msisdn(msisdn) |
| 93 | self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi()) |
| 94 | with self: |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 95 | OsmoNitbCtrl(self).subscriber_add(modem.imsi(), msisdn, modem.ki()) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 96 | |
| 97 | def subscriber_attached(self, *modems): |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 98 | return self.imsi_attached(*[m.imsi() for m in modems]) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 99 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 100 | def imsi_attached(self, *imsis): |
| 101 | attached = self.imsi_list_attached() |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 102 | self.dbg('attached:', attached) |
| 103 | return all([(imsi in attached) for imsi in imsis]) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 104 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 105 | def imsi_list_attached(self): |
| 106 | with self: |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 107 | return OsmoNitbCtrl(self).subscriber_list_active() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 108 | |
| 109 | def running(self): |
| 110 | return not self.process.terminated() |
| 111 | |
| 112 | |
| 113 | class OsmoNitbCtrl(log.Origin): |
| 114 | PORT = 4249 |
| 115 | SUBSCR_MODIFY_VAR = 'subscriber-modify-v1' |
| 116 | SUBSCR_MODIFY_REPLY_RE = re.compile("SET_REPLY (\d+) %s OK" % SUBSCR_MODIFY_VAR) |
| 117 | SUBSCR_LIST_ACTIVE_VAR = 'subscriber-list-active-v1' |
| 118 | |
| 119 | def __init__(self, nitb): |
| 120 | self.nitb = nitb |
| 121 | self.set_name('CTRL(%s:%d)' % (self.nitb.addr(), OsmoNitbCtrl.PORT)) |
| 122 | self.set_child_of(nitb) |
| 123 | |
| 124 | def ctrl(self): |
| 125 | return osmo_ctrl.OsmoCtrl(self.nitb.addr(), OsmoNitbCtrl.PORT) |
| 126 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 127 | def subscriber_add(self, imsi, msisdn, ki=None, algo=None): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 128 | created = False |
| 129 | if ki and not algo: |
| 130 | algo = 'comp128v1' |
| 131 | |
| 132 | if algo: |
| 133 | value = '%s,%s,%s,%s' % (imsi,msisdn,algo,ki) |
| 134 | else: |
| 135 | value = '%s,%s' % (imsi, msisdn) |
| 136 | |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 137 | with self.ctrl() as ctrl: |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 138 | ctrl.do_set(OsmoNitbCtrl.SUBSCR_MODIFY_VAR, value) |
| 139 | data = ctrl.receive() |
| 140 | (answer, data) = ctrl.remove_ipa_ctrl_header(data) |
| 141 | answer_str = answer.decode('utf-8') |
| 142 | res = OsmoNitbCtrl.SUBSCR_MODIFY_REPLY_RE.match(answer_str) |
| 143 | if not res: |
| 144 | raise RuntimeError('Cannot create subscriber %r (answer=%r)' % (imsi, answer_str)) |
| 145 | self.dbg('Created subscriber', imsi=imsi, msisdn=msisdn) |
| 146 | return True |
| 147 | |
| 148 | def subscriber_list_active(self): |
| 149 | var = 'subscriber-list-active-v1' |
| 150 | aslist_str = "" |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 151 | with self.ctrl() as ctrl: |
| 152 | ctrl.do_get(OsmoNitbCtrl.SUBSCR_LIST_ACTIVE_VAR) |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 153 | # This is legacy code from the old osmo-gsm-tester. |
| 154 | # looks like this doesn't work for long data. |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 155 | data = ctrl.receive() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 156 | while (len(data) > 0): |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 157 | (answer, data) = ctrl.remove_ipa_ctrl_header(data) |
| 158 | answer_str = answer.decode('utf-8') |
| 159 | answer_str = answer_str.replace('\n', ' ') |
| 160 | aslist_str = answer_str |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 161 | return aslist_str |
| 162 | |
| 163 | # vim: expandtab tabstop=4 shiftwidth=4 |