blob: 2515354882369c6991b434e6c51de0ae8793e549 [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
8# it under the terms of the GNU Affero General Public License as
9# 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
15# GNU Affero General Public License for more details.
16#
17# You should have received a copy of the GNU Affero General Public License
18# 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 Hofmeyr3531a192017-03-28 14:30:28 +020022
Neels Hofmeyr1fe012e2017-05-14 15:45:05 +020023from . import log, util, config, template, process, osmo_ctrl, pcap_recorder
Neels Hofmeyr3531a192017-03-28 14:30:28 +020024
25class OsmoNitb(log.Origin):
26 suite_run = None
27 nitb_iface = None
28 run_dir = None
29 config_file = None
30 process = None
31 bts = None
32
33 def __init__(self, suite_run, nitb_iface):
34 self.suite_run = suite_run
35 self.nitb_iface = nitb_iface
36 self.set_log_category(log.C_RUN)
37 self.set_name('osmo-nitb_%s' % nitb_iface.get('addr'))
38 self.bts = []
39
40 def start(self):
41 self.log('Starting osmo-nitb')
42 self.run_dir = util.Dir(self.suite_run.trial.get_run_dir().new_dir(self.name()))
43 self.configure()
Your Name3c6673a2017-04-08 18:52:39 +020044 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-nitb')))
45 binary = inst.child('bin', 'osmo-nitb')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020046 if not os.path.isfile(binary):
47 raise RuntimeError('Binary missing: %r' % binary)
Your Name3c6673a2017-04-08 18:52:39 +020048 lib = inst.child('lib')
49 if not os.path.isdir(lib):
50 raise RuntimeError('No lib/ in %r' % inst)
Pau Espin Pedrole39c6f12017-05-08 16:21:38 +020051
Pau Espin Pedrol13143bc2017-05-08 17:07:28 +020052 iface = util.ip_to_iface(self.addr())
Neels Hofmeyr95fd6732017-05-15 14:03:07 +020053 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), iface,
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)
63 self.suite_run.remember_to_stop(self.process)
64 self.process.launch()
65
66 def configure(self):
67 self.config_file = self.run_dir.new_file('osmo-nitb.cfg')
68 self.dbg(config_file=self.config_file)
69
70 values = dict(nitb=config.get_defaults('nitb'))
71 config.overlay(values, self.suite_run.config())
72 config.overlay(values, dict(nitb_iface=self.nitb_iface))
73
74 bts_list = []
75 for bts in self.bts:
76 bts_list.append(bts.conf_for_nitb())
77 config.overlay(values, dict(nitb=dict(net=dict(bts_list=bts_list))))
78
79 self.dbg(conf=values)
80
81 with open(self.config_file, 'w') as f:
82 r = template.render('osmo-nitb.cfg', values)
83 self.dbg(r)
84 f.write(r)
85
86 def addr(self):
87 return self.nitb_iface.get('addr')
88
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +020089 def bts_add(self, bts):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020090 self.bts.append(bts)
91 bts.set_nitb(self)
92
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +020093 def subscriber_add(self, modem, msisdn=None):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020094 if msisdn is None:
95 msisdn = self.suite_run.resources_pool.next_msisdn(modem)
96 modem.set_msisdn(msisdn)
97 self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi())
98 with self:
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +020099 OsmoNitbCtrl(self).subscriber_add(modem.imsi(), msisdn, modem.ki())
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200100
101 def subscriber_attached(self, *modems):
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200102 return self.imsi_attached(*[m.imsi() for m in modems])
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200103
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200104 def imsi_attached(self, *imsis):
105 attached = self.imsi_list_attached()
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200106 self.dbg('attached:', attached)
107 return all([(imsi in attached) for imsi in imsis])
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200108
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200109 def imsi_list_attached(self):
110 with self:
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200111 return OsmoNitbCtrl(self).subscriber_list_active()
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200112
113 def running(self):
114 return not self.process.terminated()
115
116
117class OsmoNitbCtrl(log.Origin):
118 PORT = 4249
119 SUBSCR_MODIFY_VAR = 'subscriber-modify-v1'
120 SUBSCR_MODIFY_REPLY_RE = re.compile("SET_REPLY (\d+) %s OK" % SUBSCR_MODIFY_VAR)
121 SUBSCR_LIST_ACTIVE_VAR = 'subscriber-list-active-v1'
122
123 def __init__(self, nitb):
124 self.nitb = nitb
125 self.set_name('CTRL(%s:%d)' % (self.nitb.addr(), OsmoNitbCtrl.PORT))
126 self.set_child_of(nitb)
127
128 def ctrl(self):
129 return osmo_ctrl.OsmoCtrl(self.nitb.addr(), OsmoNitbCtrl.PORT)
130
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200131 def subscriber_add(self, imsi, msisdn, ki=None, algo=None):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200132 created = False
133 if ki and not algo:
134 algo = 'comp128v1'
135
136 if algo:
137 value = '%s,%s,%s,%s' % (imsi,msisdn,algo,ki)
138 else:
139 value = '%s,%s' % (imsi, msisdn)
140
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200141 with self.ctrl() as ctrl:
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200142 ctrl.do_set(OsmoNitbCtrl.SUBSCR_MODIFY_VAR, value)
143 data = ctrl.receive()
144 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
145 answer_str = answer.decode('utf-8')
146 res = OsmoNitbCtrl.SUBSCR_MODIFY_REPLY_RE.match(answer_str)
147 if not res:
148 raise RuntimeError('Cannot create subscriber %r (answer=%r)' % (imsi, answer_str))
149 self.dbg('Created subscriber', imsi=imsi, msisdn=msisdn)
150 return True
151
152 def subscriber_list_active(self):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200153 aslist_str = ""
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200154 with self.ctrl() as ctrl:
155 ctrl.do_get(OsmoNitbCtrl.SUBSCR_LIST_ACTIVE_VAR)
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200156 # This is legacy code from the old osmo-gsm-tester.
157 # looks like this doesn't work for long data.
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200158 data = ctrl.receive()
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200159 while (len(data) > 0):
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200160 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
161 answer_str = answer.decode('utf-8')
162 answer_str = answer_str.replace('\n', ' ')
163 aslist_str = answer_str
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200164 return aslist_str
165
166# vim: expandtab tabstop=4 shiftwidth=4