blob: 443b254c20e1ee7e97127e01378e0038e6577541 [file] [log] [blame]
Neels Hofmeyr798e5922017-05-18 15:24:02 +02001# 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 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 pprint
22
23from . import log, util, config, template, process, osmo_ctrl, pcap_recorder
24
25class 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 Pedrol0e57aad2017-05-29 14:25:22 +020032 config = None
Neels Hofmeyr798e5922017-05-18 15:24:02 +020033
34 def __init__(self, suite_run, hlr, mgcpgw, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020035 super().__init__(log.C_RUN, 'osmo-msc_%s' % ip_address.get('addr'))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020036 self.suite_run = suite_run
37 self.ip_address = ip_address
Neels Hofmeyr798e5922017-05-18 15:24:02 +020038 self.hlr = hlr
39 self.mgcpgw = mgcpgw
40
41 def start(self):
42 self.log('Starting osmo-msc')
43 self.run_dir = util.Dir(self.suite_run.trial.get_run_dir().new_dir(self.name()))
44 self.configure()
45 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-msc')))
46 binary = inst.child('bin', 'osmo-msc')
47 if not os.path.isfile(binary):
48 raise RuntimeError('Binary missing: %r' % binary)
49 lib = inst.child('lib')
50 if not os.path.isdir(lib):
51 raise RuntimeError('No lib/ in %r' % inst)
52
53 iface = util.ip_to_iface(self.addr())
54 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), iface,
55 'host %s and port not 22' % self.addr())
56
57 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
58
59 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
60 self.process = process.Process(self.name(), self.run_dir,
61 (binary, '-c',
62 os.path.abspath(self.config_file)),
63 env=env)
64 self.suite_run.remember_to_stop(self.process)
65 self.process.launch()
66
67 def configure(self):
68 self.config_file = self.run_dir.new_file('osmo-msc.cfg')
69 self.dbg(config_file=self.config_file)
70
71 values = dict(msc=config.get_defaults('msc'))
72 config.overlay(values, self.suite_run.config())
73 config.overlay(values, dict(msc=dict(ip_address=self.ip_address)))
74 config.overlay(values, self.mgcpgw.conf_for_msc())
Neels Hofmeyr7b02ed02017-06-08 23:08:59 +020075 config.overlay(values, self.hlr.conf_for_msc())
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +020076 self.config = values
Neels Hofmeyr798e5922017-05-18 15:24:02 +020077
78 self.dbg('MSC CONFIG:\n' + pprint.pformat(values))
79
80 with open(self.config_file, 'w') as f:
81 r = template.render('osmo-msc.cfg', values)
82 self.dbg(r)
83 f.write(r)
84
85 def addr(self):
86 return self.ip_address.get('addr')
87
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +020088 def mcc(self):
89 return self.config['msc']['net']['mcc']
90
91 def mnc(self):
92 return self.config['msc']['net']['mnc']
93
94 def mcc_mnc(self):
95 return (self.mcc(), self.mnc())
96
Neels Hofmeyr798e5922017-05-18 15:24:02 +020097 def subscriber_attached(self, *modems):
98 return self.imsi_attached(*[m.imsi() for m in modems])
99
100 def imsi_attached(self, *imsis):
101 attached = self.imsi_list_attached()
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200102 log.dbg('attached:', attached)
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200103 return all([(imsi in attached) for imsi in imsis])
104
105 def imsi_list_attached(self):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200106 return OsmoMscCtrl(self).subscriber_list_active()
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200107
108 def running(self):
109 return not self.process.terminated()
110
111
112class OsmoMscCtrl(log.Origin):
113 PORT = 4255
114 SUBSCR_LIST_ACTIVE_VAR = 'subscriber-list-active-v1'
115
116 def __init__(self, msc):
117 self.msc = msc
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200118 super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.msc.addr(), self.PORT))
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200119
120 def ctrl(self):
121 return osmo_ctrl.OsmoCtrl(self.msc.addr(), self.PORT)
122
123 def subscriber_list_active(self):
124 aslist_str = ""
125 with self.ctrl() as ctrl:
126 ctrl.do_get(self.SUBSCR_LIST_ACTIVE_VAR)
127 # This is legacy code from the old osmo-gsm-tester.
128 # looks like this doesn't work for long data.
129 data = ctrl.receive()
130 while (len(data) > 0):
131 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
132 answer_str = answer.decode('utf-8')
133 answer_str = answer_str.replace('\n', ' ')
134 aslist_str = answer_str
135 return aslist_str
136
137# vim: expandtab tabstop=4 shiftwidth=4