blob: 3ff2d6a53bd3aab36936133ef4918df7f6886469 [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
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020023from . import log, util, config, template, process, osmo_ctrl, pcap_recorder, smsc
Neels Hofmeyr798e5922017-05-18 15:24:02 +020024
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
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020033 smsc = None
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010034 stp = None
Pau Espin Pedrolca126b12017-08-22 19:04:06 +020035 encryption = None
Pau Espin Pedrolb5a86142017-08-23 17:39:54 +020036 authentication = None
Neels Hofmeyr798e5922017-05-18 15:24:02 +020037
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010038 def __init__(self, suite_run, hlr, mgcpgw, stp, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020039 super().__init__(log.C_RUN, 'osmo-msc_%s' % ip_address.get('addr'))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020040 self.suite_run = suite_run
41 self.ip_address = ip_address
Neels Hofmeyr798e5922017-05-18 15:24:02 +020042 self.hlr = hlr
43 self.mgcpgw = mgcpgw
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010044 self.stp = stp
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020045 self.smsc = smsc.Smsc((ip_address.get('addr'), 2775))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020046
47 def start(self):
48 self.log('Starting osmo-msc')
Pau Espin Pedrold0912332017-06-14 13:27:08 +020049 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020050 self.configure()
51 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-msc')))
52 binary = inst.child('bin', 'osmo-msc')
53 if not os.path.isfile(binary):
54 raise RuntimeError('Binary missing: %r' % binary)
55 lib = inst.child('lib')
56 if not os.path.isdir(lib):
57 raise RuntimeError('No lib/ in %r' % inst)
58
Pau Espin Pedrol9cc1d082017-11-20 17:40:25 +010059 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None,
Neels Hofmeyr798e5922017-05-18 15:24:02 +020060 'host %s and port not 22' % self.addr())
61
62 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
63
64 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
65 self.process = process.Process(self.name(), self.run_dir,
66 (binary, '-c',
67 os.path.abspath(self.config_file)),
68 env=env)
69 self.suite_run.remember_to_stop(self.process)
70 self.process.launch()
71
72 def configure(self):
73 self.config_file = self.run_dir.new_file('osmo-msc.cfg')
74 self.dbg(config_file=self.config_file)
75
76 values = dict(msc=config.get_defaults('msc'))
77 config.overlay(values, self.suite_run.config())
78 config.overlay(values, dict(msc=dict(ip_address=self.ip_address)))
79 config.overlay(values, self.mgcpgw.conf_for_msc())
Pau Espin Pedrol5bbb3002017-11-23 11:07:02 +010080 config.overlay(values, self.hlr.conf_for_client())
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010081 config.overlay(values, self.stp.conf_for_client())
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020082 config.overlay(values, self.smsc.get_config())
Pau Espin Pedrolca126b12017-08-22 19:04:06 +020083
84 # runtime parameters:
85 if self.encryption is not None:
Pau Espin Pedrolabd556a2017-09-04 16:26:08 +020086 encryption_vty = util.encryption2osmovty(self.encryption)
87 else:
88 encryption_vty = util.encryption2osmovty(values['msc']['net']['encryption'])
89 config.overlay(values, dict(msc=dict(net=dict(encryption=encryption_vty))))
Pau Espin Pedrolb5a86142017-08-23 17:39:54 +020090 if self.authentication is not None:
91 config.overlay(values, dict(msc=dict(net=dict(authentication=self.authentication))))
92
Pau Espin Pedrolca126b12017-08-22 19:04:06 +020093
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +020094 self.config = values
Neels Hofmeyr798e5922017-05-18 15:24:02 +020095
96 self.dbg('MSC CONFIG:\n' + pprint.pformat(values))
97
98 with open(self.config_file, 'w') as f:
99 r = template.render('osmo-msc.cfg', values)
100 self.dbg(r)
101 f.write(r)
102
103 def addr(self):
104 return self.ip_address.get('addr')
105
Pau Espin Pedrolca126b12017-08-22 19:04:06 +0200106 def set_encryption(self, val):
107 self.encryption = val
108
Pau Espin Pedrolb5a86142017-08-23 17:39:54 +0200109 def set_authentication(self, val):
110 if val is None:
111 self.authroziation = None
112 return
113 self.authentication = "required" if val else "optional"
114
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200115 def mcc(self):
116 return self.config['msc']['net']['mcc']
117
118 def mnc(self):
119 return self.config['msc']['net']['mnc']
120
121 def mcc_mnc(self):
122 return (self.mcc(), self.mnc())
123
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200124 def subscriber_attached(self, *modems):
125 return self.imsi_attached(*[m.imsi() for m in modems])
126
127 def imsi_attached(self, *imsis):
128 attached = self.imsi_list_attached()
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200129 log.dbg('attached:', attached)
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200130 return all([(imsi in attached) for imsi in imsis])
131
132 def imsi_list_attached(self):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200133 return OsmoMscCtrl(self).subscriber_list_active()
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200134
135 def running(self):
136 return not self.process.terminated()
137
138
139class OsmoMscCtrl(log.Origin):
140 PORT = 4255
141 SUBSCR_LIST_ACTIVE_VAR = 'subscriber-list-active-v1'
142
143 def __init__(self, msc):
144 self.msc = msc
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200145 super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.msc.addr(), self.PORT))
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200146
147 def ctrl(self):
148 return osmo_ctrl.OsmoCtrl(self.msc.addr(), self.PORT)
149
150 def subscriber_list_active(self):
151 aslist_str = ""
152 with self.ctrl() as ctrl:
153 ctrl.do_get(self.SUBSCR_LIST_ACTIVE_VAR)
154 # This is legacy code from the old osmo-gsm-tester.
155 # looks like this doesn't work for long data.
156 data = ctrl.receive()
157 while (len(data) > 0):
158 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
159 answer_str = answer.decode('utf-8')
160 answer_str = answer_str.replace('\n', ' ')
161 aslist_str = answer_str
162 return aslist_str
163
164# vim: expandtab tabstop=4 shiftwidth=4