blob: 192926c92d130b2ce98477a45cf0547ebe919cb6 [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):
Neels Hofmeyr798e5922017-05-18 15:24:02 +020026
Pau Espin Pedrol86ea02f2018-02-26 12:14:46 +010027 def __init__(self, suite_run, hlr, mgw, stp, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020028 super().__init__(log.C_RUN, 'osmo-msc_%s' % ip_address.get('addr'))
Pau Espin Pedrol58603672018-08-09 13:45:55 +020029 self.run_dir = None
30 self.config_file = None
31 self.process = None
32 self.config = None
33 self.encryption = None
34 self.authentication = None
Neels Hofmeyr798e5922017-05-18 15:24:02 +020035 self.suite_run = suite_run
36 self.ip_address = ip_address
Neels Hofmeyr798e5922017-05-18 15:24:02 +020037 self.hlr = hlr
Pau Espin Pedrol86ea02f2018-02-26 12:14:46 +010038 self.mgw = mgw
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010039 self.stp = stp
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020040 self.smsc = smsc.Smsc((ip_address.get('addr'), 2775))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020041
42 def start(self):
43 self.log('Starting osmo-msc')
Pau Espin Pedrold0912332017-06-14 13:27:08 +020044 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020045 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
Pau Espin Pedrol9cc1d082017-11-20 17:40:25 +010054 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None,
Neels Hofmeyr798e5922017-05-18 15:24:02 +020055 '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)))
Pau Espin Pedrol86ea02f2018-02-26 12:14:46 +010074 config.overlay(values, self.mgw.conf_for_client())
Pau Espin Pedrol5bbb3002017-11-23 11:07:02 +010075 config.overlay(values, self.hlr.conf_for_client())
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010076 config.overlay(values, self.stp.conf_for_client())
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020077 config.overlay(values, self.smsc.get_config())
Pau Espin Pedrolca126b12017-08-22 19:04:06 +020078
79 # runtime parameters:
80 if self.encryption is not None:
Pau Espin Pedrolabd556a2017-09-04 16:26:08 +020081 encryption_vty = util.encryption2osmovty(self.encryption)
82 else:
83 encryption_vty = util.encryption2osmovty(values['msc']['net']['encryption'])
84 config.overlay(values, dict(msc=dict(net=dict(encryption=encryption_vty))))
Pau Espin Pedrolb5a86142017-08-23 17:39:54 +020085 if self.authentication is not None:
86 config.overlay(values, dict(msc=dict(net=dict(authentication=self.authentication))))
87
Pau Espin Pedrolca126b12017-08-22 19:04:06 +020088
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +020089 self.config = values
Neels Hofmeyr798e5922017-05-18 15:24:02 +020090
91 self.dbg('MSC CONFIG:\n' + pprint.pformat(values))
92
93 with open(self.config_file, 'w') as f:
94 r = template.render('osmo-msc.cfg', values)
95 self.dbg(r)
96 f.write(r)
97
98 def addr(self):
99 return self.ip_address.get('addr')
100
Pau Espin Pedrolca126b12017-08-22 19:04:06 +0200101 def set_encryption(self, val):
102 self.encryption = val
103
Pau Espin Pedrolb5a86142017-08-23 17:39:54 +0200104 def set_authentication(self, val):
105 if val is None:
106 self.authroziation = None
107 return
108 self.authentication = "required" if val else "optional"
109
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200110 def mcc(self):
111 return self.config['msc']['net']['mcc']
112
113 def mnc(self):
114 return self.config['msc']['net']['mnc']
115
116 def mcc_mnc(self):
117 return (self.mcc(), self.mnc())
118
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200119 def subscriber_attached(self, *modems):
120 return self.imsi_attached(*[m.imsi() for m in modems])
121
122 def imsi_attached(self, *imsis):
123 attached = self.imsi_list_attached()
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200124 log.dbg('attached:', attached)
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200125 return all([(imsi in attached) for imsi in imsis])
126
127 def imsi_list_attached(self):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200128 return OsmoMscCtrl(self).subscriber_list_active()
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200129
130 def running(self):
131 return not self.process.terminated()
132
133
134class OsmoMscCtrl(log.Origin):
135 PORT = 4255
136 SUBSCR_LIST_ACTIVE_VAR = 'subscriber-list-active-v1'
137
138 def __init__(self, msc):
139 self.msc = msc
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200140 super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.msc.addr(), self.PORT))
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200141
142 def ctrl(self):
143 return osmo_ctrl.OsmoCtrl(self.msc.addr(), self.PORT)
144
145 def subscriber_list_active(self):
146 aslist_str = ""
147 with self.ctrl() as ctrl:
148 ctrl.do_get(self.SUBSCR_LIST_ACTIVE_VAR)
149 # This is legacy code from the old osmo-gsm-tester.
150 # looks like this doesn't work for long data.
151 data = ctrl.receive()
152 while (len(data) > 0):
153 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
154 answer_str = answer.decode('utf-8')
155 answer_str = answer_str.replace('\n', ' ')
156 aslist_str = answer_str
157 return aslist_str
158
159# vim: expandtab tabstop=4 shiftwidth=4