blob: b6ebf8ad4c5ad2d8339ea90804c2ad864b0411be [file] [log] [blame]
Neels Hofmeyr798e5922017-05-18 15:24:02 +02001# 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 Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Neels Hofmeyr798e5922017-05-18 15:24:02 +02009# 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 Welte27205342017-06-03 09:51:45 +020015# GNU General Public License for more details.
Neels Hofmeyr798e5922017-05-18 15:24:02 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Neels Hofmeyr798e5922017-05-18 15:24:02 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
21import re
22import pprint
Pau Espin Pedrol6610dfe2017-08-23 17:02:38 +020023import sqlite3
Neels Hofmeyr798e5922017-05-18 15:24:02 +020024
25from . import log, util, config, template, process, osmo_ctrl, pcap_recorder
26
27class OsmoHlr(log.Origin):
28 suite_run = None
29 ip_address = None
30 run_dir = None
31 config_file = None
32 process = None
33 next_subscriber_id = 1
34
Pau Espin Pedrolabc8fa52017-08-22 20:04:42 +020035 AUTH_ALGO_NONE = 0
36 AUTH_ALGO_XOR = 1
37 AUTH_ALGO_COMP128v1 = 2
38
Neels Hofmeyr798e5922017-05-18 15:24:02 +020039 def __init__(self, suite_run, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020040 super().__init__(log.C_RUN, 'osmo-hlr_%s' % ip_address.get('addr'))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020041 self.suite_run = suite_run
42 self.ip_address = ip_address
Neels Hofmeyr798e5922017-05-18 15:24:02 +020043 self.bts = []
44
45 def start(self):
46 self.log('Starting osmo-hlr')
Pau Espin Pedrold0912332017-06-14 13:27:08 +020047 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020048 self.configure()
49
50 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-hlr')))
51
52 binary = inst.child('bin', 'osmo-hlr')
53 if not os.path.isfile(binary):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020054 raise log.Error('Binary missing:', binary)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020055 lib = inst.child('lib')
56 if not os.path.isdir(lib):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020057 raise log.Error('No lib/ in', inst)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020058
59 # bootstrap an empty hlr.db
60 self.db_file = self.run_dir.new_file('hlr.db')
61 sql_input = inst.child('share/doc/osmo-hlr/hlr.sql')
62 if not os.path.isfile(sql_input):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020063 raise log.Error('hlr.sql missing:', sql_input)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020064 self.run_local('create_hlr_db', ('/bin/sh', '-c', 'sqlite3 %r < %r' % (self.db_file, sql_input)))
65
Pau Espin Pedrol9cc1d082017-11-20 17:40:25 +010066 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None,
Neels Hofmeyr798e5922017-05-18 15:24:02 +020067 '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 Hofmeyr1a7a3f02017-06-10 01:18:27 +0200102 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 Hofmeyr798e5922017-05-18 15:24:02 +0200109
Pau Espin Pedrolabc8fa52017-08-22 20:04:42 +0200110 def subscriber_add(self, modem, msisdn=None, algo=None):
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200111 if msisdn is None:
112 msisdn = self.suite_run.resources_pool.next_msisdn(modem)
113 modem.set_msisdn(msisdn)
114 subscriber_id = self.next_subscriber_id
115 self.next_subscriber_id += 1
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200116
Pau Espin Pedrolabc8fa52017-08-22 20:04:42 +0200117 if not algo:
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200118 alg_str = modem.auth_algo()
119 if alg_str is None or alg_str == 'none':
120 algo = self.AUTH_ALGO_NONE
121 elif alg_str == 'comp128v1':
122 algo = self.AUTH_ALGO_COMP128v1
123 elif alg_str == 'xor':
124 algo = self.AUTH_ALGO_XOR
125 if algo != self.AUTH_ALGO_NONE and not modem.ki():
126 raise log.Error("Auth algo %r selected and no KI specified" % algo)
127
Pau Espin Pedrolabc8fa52017-08-22 20:04:42 +0200128 self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi(), subscriber_id=subscriber_id, algo=algo)
Pau Espin Pedrol6610dfe2017-08-23 17:02:38 +0200129 conn = sqlite3.connect(self.db_file)
130 try:
131 c = conn.cursor()
132 c.execute('insert into subscriber (id, imsi, msisdn) values (?, ?, ?)',
133 (subscriber_id, modem.imsi(), modem.msisdn,))
134 c.execute('insert into auc_2g (subscriber_id, algo_id_2g, ki) values (?, ?, ?)',
135 (subscriber_id, algo, modem.ki(),))
136 conn.commit()
137 finally:
138 conn.close()
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200139 return subscriber_id
140
Pau Espin Pedrol0755cdb2017-08-23 17:03:48 +0200141 def subscriber_delete(self, modem):
142 self.log('Add subscriber', imsi=modem.imsi())
143 conn = sqlite3.connect(self.db_file)
144 try:
145 c = conn.cursor()
146 c.execute('select id from subscriber where imsi = ?', (modem.imsi(),))
147 subscriber_id = c.fetchone()[0]
148 c.execute('delete from subscriber where id = ?', (subscriber_id,))
149 c.execute('delete from auc_2g where subscriber_id = ?', (subscriber_id,))
150 conn.commit()
151 finally:
152 conn.close()
153
Neels Hofmeyr7b02ed02017-06-08 23:08:59 +0200154 def conf_for_msc(self):
155 return dict(hlr=dict(ip_address=self.ip_address))
156
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200157# vim: expandtab tabstop=4 shiftwidth=4