blob: b4bf2475b9505fe5b3da91ca6a93b9e36ebe1a9a [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
Neels Hofmeyr1fe012e2017-05-14 15:45:05 +020024from . import log, util, config, template, process, osmo_ctrl, pcap_recorder
Neels Hofmeyr3531a192017-03-28 14:30:28 +020025
26class OsmoNitb(log.Origin):
27 suite_run = None
Neels Hofmeyr76d81032017-05-18 18:35:32 +020028 ip_address = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020029 run_dir = None
30 config_file = None
31 process = None
32 bts = None
33
Neels Hofmeyr76d81032017-05-18 18:35:32 +020034 def __init__(self, suite_run, ip_address):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020035 self.suite_run = suite_run
Neels Hofmeyr76d81032017-05-18 18:35:32 +020036 self.ip_address = ip_address
Neels Hofmeyr3531a192017-03-28 14:30:28 +020037 self.set_log_category(log.C_RUN)
Neels Hofmeyr76d81032017-05-18 18:35:32 +020038 self.set_name('osmo-nitb_%s' % ip_address.get('addr'))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020039 self.bts = []
40
41 def start(self):
42 self.log('Starting osmo-nitb')
43 self.run_dir = util.Dir(self.suite_run.trial.get_run_dir().new_dir(self.name()))
44 self.configure()
Your Name3c6673a2017-04-08 18:52:39 +020045 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-nitb')))
46 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 Pedrol13143bc2017-05-08 17:07:28 +020053 iface = util.ip_to_iface(self.addr())
Neels Hofmeyr95fd6732017-05-15 14:03:07 +020054 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), iface,
Pau Espin Pedrol8cd347b2017-05-12 16:24:21 +020055 'host %s and port not 22' % self.addr())
Pau Espin Pedrol13143bc2017-05-08 17:07:28 +020056
Pau Espin Pedrole39c6f12017-05-08 16:21:38 +020057 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
58
Neels Hofmeyr3531a192017-03-28 14:30:28 +020059 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-nitb.cfg')
69 self.dbg(config_file=self.config_file)
70
71 values = dict(nitb=config.get_defaults('nitb'))
72 config.overlay(values, self.suite_run.config())
Neels Hofmeyr4f59ba62017-05-22 20:04:05 +020073 config.overlay(values, dict(nitb=dict(ip_address=self.ip_address)))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020074
75 bts_list = []
76 for bts in self.bts:
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020077 bts_list.append(bts.conf_for_bsc())
Neels Hofmeyr3531a192017-03-28 14:30:28 +020078 config.overlay(values, dict(nitb=dict(net=dict(bts_list=bts_list))))
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +020079 self.config = values
Neels Hofmeyr3531a192017-03-28 14:30:28 +020080
Neels Hofmeyr943c81d2017-05-22 20:16:03 +020081 self.dbg('NITB CONFIG:\n' + pprint.pformat(values))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020082
83 with open(self.config_file, 'w') as f:
84 r = template.render('osmo-nitb.cfg', values)
85 self.dbg(r)
86 f.write(r)
87
88 def addr(self):
Neels Hofmeyr76d81032017-05-18 18:35:32 +020089 return self.ip_address.get('addr')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020090
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +020091 def bts_add(self, bts):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020092 self.bts.append(bts)
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020093 bts.set_bsc(self)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020094
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +020095 def mcc(self):
96 return self.config['nitb']['net']['mcc']
97
98 def mnc(self):
99 return self.config['nitb']['net']['mnc']
100
101 def mcc_mnc(self):
102 return (self.mcc(), self.mnc())
103
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200104 def subscriber_add(self, modem, msisdn=None):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200105 if msisdn is None:
106 msisdn = self.suite_run.resources_pool.next_msisdn(modem)
107 modem.set_msisdn(msisdn)
108 self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi())
109 with self:
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200110 OsmoNitbCtrl(self).subscriber_add(modem.imsi(), msisdn, modem.ki())
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200111
112 def subscriber_attached(self, *modems):
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200113 return self.imsi_attached(*[m.imsi() for m in modems])
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200114
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200115 def imsi_attached(self, *imsis):
116 attached = self.imsi_list_attached()
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200117 self.dbg('attached:', attached)
118 return all([(imsi in attached) for imsi in imsis])
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200119
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200120 def imsi_list_attached(self):
121 with self:
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200122 return OsmoNitbCtrl(self).subscriber_list_active()
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200123
124 def running(self):
125 return not self.process.terminated()
126
127
128class OsmoNitbCtrl(log.Origin):
129 PORT = 4249
130 SUBSCR_MODIFY_VAR = 'subscriber-modify-v1'
131 SUBSCR_MODIFY_REPLY_RE = re.compile("SET_REPLY (\d+) %s OK" % SUBSCR_MODIFY_VAR)
132 SUBSCR_LIST_ACTIVE_VAR = 'subscriber-list-active-v1'
133
134 def __init__(self, nitb):
135 self.nitb = nitb
136 self.set_name('CTRL(%s:%d)' % (self.nitb.addr(), OsmoNitbCtrl.PORT))
137 self.set_child_of(nitb)
138
139 def ctrl(self):
140 return osmo_ctrl.OsmoCtrl(self.nitb.addr(), OsmoNitbCtrl.PORT)
141
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200142 def subscriber_add(self, imsi, msisdn, ki=None, algo=None):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200143 created = False
144 if ki and not algo:
145 algo = 'comp128v1'
146
147 if algo:
148 value = '%s,%s,%s,%s' % (imsi,msisdn,algo,ki)
149 else:
150 value = '%s,%s' % (imsi, msisdn)
151
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200152 with self.ctrl() as ctrl:
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200153 ctrl.do_set(OsmoNitbCtrl.SUBSCR_MODIFY_VAR, value)
154 data = ctrl.receive()
155 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
156 answer_str = answer.decode('utf-8')
157 res = OsmoNitbCtrl.SUBSCR_MODIFY_REPLY_RE.match(answer_str)
158 if not res:
159 raise RuntimeError('Cannot create subscriber %r (answer=%r)' % (imsi, answer_str))
160 self.dbg('Created subscriber', imsi=imsi, msisdn=msisdn)
161 return True
162
163 def subscriber_list_active(self):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200164 aslist_str = ""
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200165 with self.ctrl() as ctrl:
166 ctrl.do_get(OsmoNitbCtrl.SUBSCR_LIST_ACTIVE_VAR)
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200167 # This is legacy code from the old osmo-gsm-tester.
168 # looks like this doesn't work for long data.
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200169 data = ctrl.receive()
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200170 while (len(data) > 0):
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200171 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
172 answer_str = answer.decode('utf-8')
173 answer_str = answer_str.replace('\n', ' ')
174 aslist_str = answer_str
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200175 return aslist_str
176
177# vim: expandtab tabstop=4 shiftwidth=4