blob: ca0e24660fc49bff296d5d76c01bd01e15005ebc [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
32
33 def __init__(self, suite_run, hlr, mgcpgw, ip_address):
34 self.suite_run = suite_run
35 self.ip_address = ip_address
36 self.set_log_category(log.C_RUN)
37 self.set_name('osmo-msc_%s' % ip_address.get('addr'))
38 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())
75
76 self.dbg('MSC CONFIG:\n' + pprint.pformat(values))
77
78 with open(self.config_file, 'w') as f:
79 r = template.render('osmo-msc.cfg', values)
80 self.dbg(r)
81 f.write(r)
82
83 def addr(self):
84 return self.ip_address.get('addr')
85
86 def subscriber_attached(self, *modems):
87 return self.imsi_attached(*[m.imsi() for m in modems])
88
89 def imsi_attached(self, *imsis):
90 attached = self.imsi_list_attached()
91 self.dbg('attached:', attached)
92 return all([(imsi in attached) for imsi in imsis])
93
94 def imsi_list_attached(self):
95 with self:
96 return OsmoMscCtrl(self).subscriber_list_active()
97
98 def running(self):
99 return not self.process.terminated()
100
101
102class OsmoMscCtrl(log.Origin):
103 PORT = 4255
104 SUBSCR_LIST_ACTIVE_VAR = 'subscriber-list-active-v1'
105
106 def __init__(self, msc):
107 self.msc = msc
108 self.set_name('CTRL(%s:%d)' % (self.msc.addr(), self.PORT))
109 self.set_child_of(msc)
110
111 def ctrl(self):
112 return osmo_ctrl.OsmoCtrl(self.msc.addr(), self.PORT)
113
114 def subscriber_list_active(self):
115 aslist_str = ""
116 with self.ctrl() as ctrl:
117 ctrl.do_get(self.SUBSCR_LIST_ACTIVE_VAR)
118 # This is legacy code from the old osmo-gsm-tester.
119 # looks like this doesn't work for long data.
120 data = ctrl.receive()
121 while (len(data) > 0):
122 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
123 answer_str = answer.decode('utf-8')
124 answer_str = answer_str.replace('\n', ' ')
125 aslist_str = answer_str
126 return aslist_str
127
128# vim: expandtab tabstop=4 shiftwidth=4