Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running an osmo-hlr |
| 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 re |
| 22 | import pprint |
| 23 | |
| 24 | from . import log, util, config, template, process, osmo_ctrl, pcap_recorder |
| 25 | |
| 26 | class OsmoHlr(log.Origin): |
| 27 | suite_run = None |
| 28 | ip_address = None |
| 29 | run_dir = None |
| 30 | config_file = None |
| 31 | process = None |
| 32 | next_subscriber_id = 1 |
| 33 | |
Pau Espin Pedrol | abc8fa5 | 2017-08-22 20:04:42 +0200 | [diff] [blame^] | 34 | AUTH_ALGO_NONE = 0 |
| 35 | AUTH_ALGO_XOR = 1 |
| 36 | AUTH_ALGO_COMP128v1 = 2 |
| 37 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 38 | def __init__(self, suite_run, ip_address): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 39 | super().__init__(log.C_RUN, 'osmo-hlr_%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 | |
| 44 | def start(self): |
| 45 | self.log('Starting osmo-hlr') |
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 | |
| 49 | inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-hlr'))) |
| 50 | |
| 51 | binary = inst.child('bin', 'osmo-hlr') |
| 52 | if not os.path.isfile(binary): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 53 | raise log.Error('Binary missing:', binary) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 54 | lib = inst.child('lib') |
| 55 | if not os.path.isdir(lib): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 56 | raise log.Error('No lib/ in', inst) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 57 | |
| 58 | # bootstrap an empty hlr.db |
| 59 | self.db_file = self.run_dir.new_file('hlr.db') |
| 60 | sql_input = inst.child('share/doc/osmo-hlr/hlr.sql') |
| 61 | if not os.path.isfile(sql_input): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 62 | raise log.Error('hlr.sql missing:', sql_input) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 63 | self.run_local('create_hlr_db', ('/bin/sh', '-c', 'sqlite3 %r < %r' % (self.db_file, sql_input))) |
| 64 | |
| 65 | iface = util.ip_to_iface(self.addr()) |
| 66 | pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), iface, |
| 67 | 'host %s' % self.addr()) |
| 68 | |
| 69 | env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 70 | |
| 71 | self.dbg(run_dir=self.run_dir, binary=binary, env=env) |
| 72 | self.process = process.Process(self.name(), self.run_dir, |
| 73 | (binary, |
| 74 | '-c', os.path.abspath(self.config_file), |
| 75 | '--database', self.db_file), |
| 76 | env=env) |
| 77 | self.suite_run.remember_to_stop(self.process) |
| 78 | self.process.launch() |
| 79 | |
| 80 | def configure(self): |
| 81 | self.config_file = self.run_dir.new_file('osmo-hlr.cfg') |
| 82 | self.dbg(config_file=self.config_file) |
| 83 | |
| 84 | values = dict(hlr=config.get_defaults('hlr')) |
| 85 | config.overlay(values, self.suite_run.config()) |
| 86 | config.overlay(values, dict(hlr=dict(ip_address=self.ip_address))) |
| 87 | |
| 88 | self.dbg('HLR CONFIG:\n' + pprint.pformat(values)) |
| 89 | |
| 90 | with open(self.config_file, 'w') as f: |
| 91 | r = template.render('osmo-hlr.cfg', values) |
| 92 | self.dbg(r) |
| 93 | f.write(r) |
| 94 | |
| 95 | def addr(self): |
| 96 | return self.ip_address.get('addr') |
| 97 | |
| 98 | def running(self): |
| 99 | return not self.process.terminated() |
| 100 | |
| 101 | def run_local(self, name, popen_args): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 102 | run_dir = self.run_dir.new_dir(name) |
| 103 | proc = process.Process(name, run_dir, popen_args) |
| 104 | proc.launch() |
| 105 | proc.wait() |
| 106 | if proc.result != 0: |
| 107 | log.ctx(proc) |
| 108 | raise log.Error('Exited in error') |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 109 | |
| 110 | def run_sql_file(self, name, sql_file): |
| 111 | self.run_local(name, ('/bin/sh', '-c', 'sqlite3 %r < %r' % (self.db_file, sql_file))) |
| 112 | |
| 113 | def run_sql(self, name, sql): |
| 114 | self.dbg('SQL:', repr(sql)) |
| 115 | sql_file = self.run_dir.new_file(name + '.sql') |
| 116 | with open(sql_file, 'w') as f: |
| 117 | f.write(sql) |
| 118 | self.run_sql_file(name, sql_file) |
| 119 | |
Pau Espin Pedrol | abc8fa5 | 2017-08-22 20:04:42 +0200 | [diff] [blame^] | 120 | def subscriber_add(self, modem, msisdn=None, algo=None): |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 121 | if msisdn is None: |
| 122 | msisdn = self.suite_run.resources_pool.next_msisdn(modem) |
| 123 | modem.set_msisdn(msisdn) |
| 124 | subscriber_id = self.next_subscriber_id |
| 125 | self.next_subscriber_id += 1 |
Pau Espin Pedrol | abc8fa5 | 2017-08-22 20:04:42 +0200 | [diff] [blame^] | 126 | if not algo: |
| 127 | algo = self.AUTH_ALGO_COMP128v1 if modem.ki() else self.AUTH_ALGO_NONE |
| 128 | self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi(), subscriber_id=subscriber_id, algo=algo) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 129 | self.run_sql('add_subscriber', |
| 130 | 'insert into subscriber (id, imsi, msisdn) values (%r, %r, %r);' |
| 131 | % (subscriber_id, modem.imsi(), modem.msisdn)) |
Pau Espin Pedrol | abc8fa5 | 2017-08-22 20:04:42 +0200 | [diff] [blame^] | 132 | self.run_sql('add_subscriber', |
| 133 | 'insert into auc_2g (subscriber_id, algo_id_2g, ki) values (%r, %r, %r);' |
| 134 | % (subscriber_id, algo, modem.ki())) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 135 | return subscriber_id |
| 136 | |
Neels Hofmeyr | 7b02ed0 | 2017-06-08 23:08:59 +0200 | [diff] [blame] | 137 | def conf_for_msc(self): |
| 138 | return dict(hlr=dict(ip_address=self.ip_address)) |
| 139 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 140 | # vim: expandtab tabstop=4 shiftwidth=4 |