blob: a978b719711464b4c2acc6bc44fdce6c6797aed3 [file] [log] [blame]
Neels Hofmeyr3531a192017-03-28 14:30:28 +02001# osmo_gsm_tester: specifics for running an osmo-nitb
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 Hofmeyr3531a192017-03-28 14:30:28 +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 Hofmeyr3531a192017-03-28 14:30:28 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Neels Hofmeyr3531a192017-03-28 14:30:28 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
Neels Hofmeyr3531a192017-03-28 14:30:28 +020021import re
Neels Hofmeyr943c81d2017-05-22 20:16:03 +020022import pprint
Neels Hofmeyr3531a192017-03-28 14:30:28 +020023
Pau Espin Pedrole1a58bd2020-04-10 20:46:07 +020024from ..core import log, util, config, template, process
Pau Espin Pedrole8bbcbf2020-04-10 19:51:31 +020025from . import osmo_ctrl, pcap_recorder, smsc
Neels Hofmeyr3531a192017-03-28 14:30:28 +020026
27class OsmoNitb(log.Origin):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020028
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020029 def __init__(self, testenv, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020030 super().__init__(log.C_RUN, 'osmo-nitb_%s' % ip_address.get('addr'))
Pau Espin Pedrol58603672018-08-09 13:45:55 +020031 self.run_dir = None
32 self.config_file = None
33 self.process = None
34 self.encryption = None
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020035 self.testenv = testenv
Neels Hofmeyr76d81032017-05-18 18:35:32 +020036 self.ip_address = ip_address
Neels Hofmeyr3531a192017-03-28 14:30:28 +020037 self.bts = []
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020038 self.smsc = smsc.Smsc((ip_address.get('addr'), 2775))
Neels Hofmeyr012a17d2020-12-08 17:36:04 +010039 self.ctrl = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020040
41 def start(self):
42 self.log('Starting osmo-nitb')
Pau Espin Pedrol2a2d8462020-05-11 10:56:52 +020043 self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name()))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020044 self.configure()
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020045 inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmo-nitb')))
Your Name3c6673a2017-04-08 18:52:39 +020046 binary = inst.child('bin', 'osmo-nitb')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020047 if not os.path.isfile(binary):
48 raise RuntimeError('Binary missing: %r' % binary)
Your Name3c6673a2017-04-08 18:52:39 +020049 lib = inst.child('lib')
50 if not os.path.isdir(lib):
51 raise RuntimeError('No lib/ in %r' % inst)
Pau Espin Pedrole39c6f12017-05-08 16:21:38 +020052
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020053 pcap_recorder.PcapRecorder(self.testenv, self.run_dir.new_dir('pcap'), None,
Pau Espin Pedrol8cd347b2017-05-12 16:24:21 +020054 'host %s and port not 22' % self.addr())
Pau Espin Pedrol13143bc2017-05-08 17:07:28 +020055
Pau Espin Pedrole39c6f12017-05-08 16:21:38 +020056 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
57
Neels Hofmeyr3531a192017-03-28 14:30:28 +020058 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
59 self.process = process.Process(self.name(), self.run_dir,
60 (binary, '-c',
61 os.path.abspath(self.config_file)),
62 env=env)
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020063 self.testenv.remember_to_stop(self.process)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020064 self.process.launch()
65
Neels Hofmeyr012a17d2020-12-08 17:36:04 +010066 self.ctrl = OsmoNitbCtrl(self)
67 self.ctrl.connect()
68
Neels Hofmeyr3531a192017-03-28 14:30:28 +020069 def configure(self):
70 self.config_file = self.run_dir.new_file('osmo-nitb.cfg')
71 self.dbg(config_file=self.config_file)
72
73 values = dict(nitb=config.get_defaults('nitb'))
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020074 config.overlay(values, self.testenv.suite().config())
Neels Hofmeyr4f59ba62017-05-22 20:04:05 +020075 config.overlay(values, dict(nitb=dict(ip_address=self.ip_address)))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020076
77 bts_list = []
78 for bts in self.bts:
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020079 bts_list.append(bts.conf_for_bsc())
Neels Hofmeyr3531a192017-03-28 14:30:28 +020080 config.overlay(values, dict(nitb=dict(net=dict(bts_list=bts_list))))
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020081 config.overlay(values, self.smsc.get_config())
Pau Espin Pedrolca126b12017-08-22 19:04:06 +020082
83 # runtime parameters:
84 if self.encryption is not None:
Pau Espin Pedrolabd556a2017-09-04 16:26:08 +020085 encryption_vty = util.encryption2osmovty(self.encryption)
86 else:
87 encryption_vty = util.encryption2osmovty(values['nitb']['net']['encryption'])
88 config.overlay(values, dict(nitb=dict(net=dict(encryption=encryption_vty))))
Pau Espin Pedrolca126b12017-08-22 19:04:06 +020089
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +020090 self.config = values
Neels Hofmeyr3531a192017-03-28 14:30:28 +020091
Neels Hofmeyr943c81d2017-05-22 20:16:03 +020092 self.dbg('NITB CONFIG:\n' + pprint.pformat(values))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020093
94 with open(self.config_file, 'w') as f:
95 r = template.render('osmo-nitb.cfg', values)
96 self.dbg(r)
97 f.write(r)
98
99 def addr(self):
Neels Hofmeyr76d81032017-05-18 18:35:32 +0200100 return self.ip_address.get('addr')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200101
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200102 def bts_add(self, bts):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200103 self.bts.append(bts)
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +0200104 bts.set_bsc(self)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200105
Pau Espin Pedrolca126b12017-08-22 19:04:06 +0200106 def set_encryption(self, val):
107 self.encryption = val
108
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200109 def mcc(self):
110 return self.config['nitb']['net']['mcc']
111
112 def mnc(self):
113 return self.config['nitb']['net']['mnc']
114
115 def mcc_mnc(self):
116 return (self.mcc(), self.mnc())
117
Pau Espin Pedrolc859eb32017-12-11 15:01:13 +0100118 def bts_num(self, bts):
119 'Provide number id used by OsmoNITB to identify configured BTS'
120 # We take advantage from the fact that VTY code assigns VTY in ascending
121 # order through the bts nodes found. As we populate the config iterating
122 # over this list, we have a 1:1 match in indexes.
123 return self.bts.index(bts)
124
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200125 def subscriber_add(self, modem, msisdn=None, algo=None):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200126 if msisdn is None:
Pau Espin Pedrol83a2fdc2020-10-15 16:33:47 +0200127 msisdn = modem.msisdn()
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200128
129 if not algo:
130 alg_str = modem.auth_algo()
131 if not alg_str or alg_str == 'none':
132 algo = None
133 elif alg_str == 'comp128v1':
134 algo = 'comp128v1'
135 elif alg_str == 'xor':
136 algo = 'xor'
137 if algo is not None and not modem.ki():
138 raise log.Error("Auth algo %r selected and no KI specified" % algo)
139
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200140 self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi())
Neels Hofmeyr012a17d2020-12-08 17:36:04 +0100141 return self.ctrl.subscriber_add(modem.imsi(), msisdn, modem.ki(), algo)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200142
Pau Espin Pedrole1a26222017-08-23 15:52:14 +0200143 def subscriber_delete(self, modem):
144 self.log('Delete subscriber', imsi=modem.imsi())
Neels Hofmeyr012a17d2020-12-08 17:36:04 +0100145 return self.ctrl.subscriber_delete(modem.imsi())
Pau Espin Pedrole1a26222017-08-23 15:52:14 +0200146
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200147 def subscriber_attached(self, *modems):
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200148 return self.imsi_attached(*[m.imsi() for m in modems])
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200149
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200150 def imsi_attached(self, *imsis):
151 attached = self.imsi_list_attached()
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200152 self.dbg('attached:', attached)
153 return all([(imsi in attached) for imsi in imsis])
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200154
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200155 def imsi_list_attached(self):
Neels Hofmeyr012a17d2020-12-08 17:36:04 +0100156 return self.ctrl.subscriber_list_active()
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200157
Pau Espin Pedrolc859eb32017-12-11 15:01:13 +0100158 def bts_is_connected(self, bts):
Neels Hofmeyr012a17d2020-12-08 17:36:04 +0100159 return self.ctrl.bts_is_connected(self.bts_num(bts))
Pau Espin Pedrolc859eb32017-12-11 15:01:13 +0100160
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200161 def running(self):
162 return not self.process.terminated()
163
Neels Hofmeyr012a17d2020-12-08 17:36:04 +0100164 def cleanup(self):
165 if self.ctrl is not None:
166 self.ctrl.disconnect()
167 self.ctrl = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200168
Neels Hofmeyr5b04ef22020-12-01 04:48:38 +0100169class OsmoNitbCtrl(osmo_ctrl.OsmoCtrl):
170 def __init__(self, nitb, port=4249):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200171 self.nitb = nitb
Neels Hofmeyr5b04ef22020-12-01 04:48:38 +0100172 super().__init__(nitb.addr(), port)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200173
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200174 def subscriber_add(self, imsi, msisdn, ki=None, algo=None):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200175 if algo:
176 value = '%s,%s,%s,%s' % (imsi,msisdn,algo,ki)
177 else:
178 value = '%s,%s' % (imsi, msisdn)
179
Neels Hofmeyr5b04ef22020-12-01 04:48:38 +0100180 assert self.set_var('subscriber-modify-v1', value) == 'OK'
181 self.dbg('Created subscriber', imsi=imsi, msisdn=msisdn)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200182
Pau Espin Pedrole1a26222017-08-23 15:52:14 +0200183 def subscriber_delete(self, imsi):
Neels Hofmeyr5b04ef22020-12-01 04:48:38 +0100184 assert self.set_var('subscriber-delete-v1', imsi) == 'Removed'
185 self.dbg('Deleted subscriber', imsi=imsi)
Pau Espin Pedrole1a26222017-08-23 15:52:14 +0200186
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200187 def subscriber_list_active(self):
Neels Hofmeyr5b04ef22020-12-01 04:48:38 +0100188 return self.get_var('subscriber-list-active-v1').replace('\n', ' ')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200189
Pau Espin Pedrolc859eb32017-12-11 15:01:13 +0100190 def bts_is_connected(self, bts_num):
Neels Hofmeyr5b04ef22020-12-01 04:48:38 +0100191 return self.get_var('bts.%d.oml-connection-state' % bts_num) == 'connected'
Pau Espin Pedrolc859eb32017-12-11 15:01:13 +0100192
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200193# vim: expandtab tabstop=4 shiftwidth=4