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