blob: fd5d41f6468bd45c4235b302a7f32fc6f3b8609b [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 Pedrole8bbcbf2020-04-10 19:51:31 +020023from .core import log, util, config, template, process
24from . import osmo_ctrl, pcap_recorder, smsc
Neels Hofmeyr798e5922017-05-18 15:24:02 +020025
26class OsmoMsc(log.Origin):
Neels Hofmeyr798e5922017-05-18 15:24:02 +020027
Pau Espin Pedrol86ea02f2018-02-26 12:14:46 +010028 def __init__(self, suite_run, hlr, mgw, stp, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020029 super().__init__(log.C_RUN, 'osmo-msc_%s' % ip_address.get('addr'))
Pau Espin Pedrol58603672018-08-09 13:45:55 +020030 self.run_dir = None
31 self.config_file = None
32 self.process = None
33 self.config = None
34 self.encryption = None
35 self.authentication = None
Pau Espin Pedrolfed578e2019-06-19 17:18:43 +020036 self.use_osmux = "off"
Neels Hofmeyr798e5922017-05-18 15:24:02 +020037 self.suite_run = suite_run
38 self.ip_address = ip_address
Neels Hofmeyr798e5922017-05-18 15:24:02 +020039 self.hlr = hlr
Pau Espin Pedrol86ea02f2018-02-26 12:14:46 +010040 self.mgw = mgw
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010041 self.stp = stp
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020042 self.smsc = smsc.Smsc((ip_address.get('addr'), 2775))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020043
44 def start(self):
45 self.log('Starting osmo-msc')
Pau Espin Pedrold0912332017-06-14 13:27:08 +020046 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020047 self.configure()
48 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-msc')))
49 binary = inst.child('bin', 'osmo-msc')
50 if not os.path.isfile(binary):
51 raise RuntimeError('Binary missing: %r' % binary)
52 lib = inst.child('lib')
53 if not os.path.isdir(lib):
54 raise RuntimeError('No lib/ in %r' % inst)
55
Pau Espin Pedrol9cc1d082017-11-20 17:40:25 +010056 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None,
Neels Hofmeyr798e5922017-05-18 15:24:02 +020057 'host %s and port not 22' % self.addr())
58
59 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
60
61 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
62 self.process = process.Process(self.name(), self.run_dir,
63 (binary, '-c',
64 os.path.abspath(self.config_file)),
65 env=env)
66 self.suite_run.remember_to_stop(self.process)
67 self.process.launch()
68
69 def configure(self):
70 self.config_file = self.run_dir.new_file('osmo-msc.cfg')
71 self.dbg(config_file=self.config_file)
72
73 values = dict(msc=config.get_defaults('msc'))
74 config.overlay(values, self.suite_run.config())
75 config.overlay(values, dict(msc=dict(ip_address=self.ip_address)))
Pau Espin Pedrol86ea02f2018-02-26 12:14:46 +010076 config.overlay(values, self.mgw.conf_for_client())
Pau Espin Pedrol5bbb3002017-11-23 11:07:02 +010077 config.overlay(values, self.hlr.conf_for_client())
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010078 config.overlay(values, self.stp.conf_for_client())
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020079 config.overlay(values, self.smsc.get_config())
Pau Espin Pedrolca126b12017-08-22 19:04:06 +020080
81 # runtime parameters:
82 if self.encryption is not None:
Pau Espin Pedrolabd556a2017-09-04 16:26:08 +020083 encryption_vty = util.encryption2osmovty(self.encryption)
84 else:
85 encryption_vty = util.encryption2osmovty(values['msc']['net']['encryption'])
86 config.overlay(values, dict(msc=dict(net=dict(encryption=encryption_vty))))
Pau Espin Pedrolb5a86142017-08-23 17:39:54 +020087 if self.authentication is not None:
88 config.overlay(values, dict(msc=dict(net=dict(authentication=self.authentication))))
Pau Espin Pedrolfed578e2019-06-19 17:18:43 +020089 config.overlay(values, dict(msc=dict(use_osmux=self.use_osmux)))
Pau Espin Pedrolb5a86142017-08-23 17:39:54 +020090
Pau Espin Pedrolca126b12017-08-22 19:04:06 +020091
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +020092 self.config = values
Neels Hofmeyr798e5922017-05-18 15:24:02 +020093
94 self.dbg('MSC CONFIG:\n' + pprint.pformat(values))
95
96 with open(self.config_file, 'w') as f:
97 r = template.render('osmo-msc.cfg', values)
98 self.dbg(r)
99 f.write(r)
100
101 def addr(self):
102 return self.ip_address.get('addr')
103
Pau Espin Pedrolca126b12017-08-22 19:04:06 +0200104 def set_encryption(self, val):
105 self.encryption = val
106
Pau Espin Pedrolb5a86142017-08-23 17:39:54 +0200107 def set_authentication(self, val):
108 if val is None:
109 self.authroziation = None
110 return
111 self.authentication = "required" if val else "optional"
112
Pau Espin Pedrolfed578e2019-06-19 17:18:43 +0200113 def set_use_osmux(self, use=False, force=False):
114 if not use:
115 self.use_osmux = "off"
116 else:
117 if not force:
118 self.use_osmux = "on"
119 else:
120 self.use_osmux = "only"
121
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200122 def mcc(self):
123 return self.config['msc']['net']['mcc']
124
125 def mnc(self):
126 return self.config['msc']['net']['mnc']
127
128 def mcc_mnc(self):
129 return (self.mcc(), self.mnc())
130
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200131 def subscriber_attached(self, *modems):
132 return self.imsi_attached(*[m.imsi() for m in modems])
133
134 def imsi_attached(self, *imsis):
135 attached = self.imsi_list_attached()
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200136 log.dbg('attached:', attached)
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200137 return all([(imsi in attached) for imsi in imsis])
138
139 def imsi_list_attached(self):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200140 return OsmoMscCtrl(self).subscriber_list_active()
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200141
142 def running(self):
143 return not self.process.terminated()
144
145
146class OsmoMscCtrl(log.Origin):
147 PORT = 4255
148 SUBSCR_LIST_ACTIVE_VAR = 'subscriber-list-active-v1'
149
150 def __init__(self, msc):
151 self.msc = msc
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200152 super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.msc.addr(), self.PORT))
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200153
154 def ctrl(self):
155 return osmo_ctrl.OsmoCtrl(self.msc.addr(), self.PORT)
156
157 def subscriber_list_active(self):
158 aslist_str = ""
159 with self.ctrl() as ctrl:
160 ctrl.do_get(self.SUBSCR_LIST_ACTIVE_VAR)
161 # This is legacy code from the old osmo-gsm-tester.
162 # looks like this doesn't work for long data.
163 data = ctrl.receive()
164 while (len(data) > 0):
165 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
166 answer_str = answer.decode('utf-8')
167 answer_str = answer_str.replace('\n', ' ')
168 aslist_str = answer_str
169 return aslist_str
170
171# vim: expandtab tabstop=4 shiftwidth=4