blob: 736fe8f1c432ed401edc3c8594f3203ff57c67f0 [file] [log] [blame]
Neels Hofmeyr798e5922017-05-18 15:24:02 +02001# osmo_gsm_tester: specifics for running an osmo-bsc
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
23from . import log, util, config, template, process, osmo_ctrl, pcap_recorder
24
25class OsmoBsc(log.Origin):
26 suite_run = None
27 ip_address = None
28 run_dir = None
29 config_file = None
30 process = None
31 bts = None
Pau Espin Pedrol387526a2017-08-24 15:12:59 +020032 encryption = None
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +010033 mgw = None
Neels Hofmeyr798e5922017-05-18 15:24:02 +020034
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +010035 def __init__(self, suite_run, msc, mgw, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020036 super().__init__(log.C_RUN, 'osmo-bsc_%s' % ip_address.get('addr'))
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.bts = []
40 self.msc = msc
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +010041 self.mgw = mgw
Neels Hofmeyr798e5922017-05-18 15:24:02 +020042
43 def start(self):
44 self.log('Starting osmo-bsc')
Pau Espin Pedrold0912332017-06-14 13:27:08 +020045 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020046 self.configure()
47
Neels Hofmeyr36e04042017-09-04 16:49:17 +020048 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bsc')))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020049
50 binary = inst.child('bin', 'osmo-bsc')
51 if not os.path.isfile(binary):
52 raise RuntimeError('Binary missing: %r' % binary)
53 lib = inst.child('lib')
54 if not os.path.isdir(lib):
55 raise RuntimeError('No lib/ in %r' % inst)
56
57 iface = util.ip_to_iface(self.addr())
58 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), iface,
59 'host %s and port not 22' % self.addr())
60
61 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
62
63 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
64 self.process = process.Process(self.name(), self.run_dir,
65 (binary, '-c',
66 os.path.abspath(self.config_file)),
67 env=env)
68 self.suite_run.remember_to_stop(self.process)
69 self.process.launch()
70
71 def configure(self):
72 self.config_file = self.run_dir.new_file('osmo-bsc.cfg')
73 self.dbg(config_file=self.config_file)
74
75 values = dict(bsc=config.get_defaults('bsc'))
76 config.overlay(values, self.suite_run.config())
77 config.overlay(values, dict(bsc=dict(ip_address=self.ip_address)))
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +010078 config.overlay(values, self.mgw.conf_for_client())
Neels Hofmeyr798e5922017-05-18 15:24:02 +020079
80 bts_list = []
81 for bts in self.bts:
82 bts_list.append(bts.conf_for_bsc())
83 config.overlay(values, dict(bsc=dict(net=dict(bts_list=bts_list))))
84
Pau Espin Pedrol387526a2017-08-24 15:12:59 +020085 # runtime parameters:
86 if self.encryption is not None:
Pau Espin Pedrolabd556a2017-09-04 16:26:08 +020087 encryption_vty = util.encryption2osmovty(self.encryption)
88 else:
89 encryption_vty = util.encryption2osmovty(values['bsc']['net']['encryption'])
90 config.overlay(values, dict(bsc=dict(net=dict(encryption=encryption_vty))))
Pau Espin Pedrol387526a2017-08-24 15:12:59 +020091
Neels Hofmeyr798e5922017-05-18 15:24:02 +020092 self.dbg('BSC CONFIG:\n' + pprint.pformat(values))
93
94 with open(self.config_file, 'w') as f:
95 r = template.render('osmo-bsc.cfg', values)
96 self.dbg(r)
97 f.write(r)
98
99 def addr(self):
100 return self.ip_address.get('addr')
101
Pau Espin Pedrol387526a2017-08-24 15:12:59 +0200102 def set_encryption(self, val):
103 self.encryption = val
104
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200105 def bts_add(self, bts):
106 self.bts.append(bts)
107 bts.set_bsc(self)
108
109 def running(self):
110 return not self.process.terminated()
111
112# vim: expandtab tabstop=4 shiftwidth=4