blob: 046ef942e4c921d96a8d32288243fa0a81cef8c0 [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
Pau Espin Pedrol7bd71032017-12-11 15:02:26 +010021import re
Neels Hofmeyr798e5922017-05-18 15:24:02 +020022import pprint
23
Pau Espin Pedrole1a58bd2020-04-10 20:46:07 +020024from ..core import log, util, config, template, process
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020025from ..core import schema
Pau Espin Pedrole8bbcbf2020-04-10 19:51:31 +020026from . import osmo_ctrl, pcap_recorder
Neels Hofmeyr798e5922017-05-18 15:24:02 +020027
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020028def on_register_schemas():
29 config_schema = {
30 'net.codec_list[]': schema.CODEC,
31 }
32 schema.register_config_schema('bsc', config_schema)
33
34
Neels Hofmeyr798e5922017-05-18 15:24:02 +020035class OsmoBsc(log.Origin):
Neels Hofmeyr798e5922017-05-18 15:24:02 +020036
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010037 def __init__(self, suite_run, msc, mgw, stp, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020038 super().__init__(log.C_RUN, 'osmo-bsc_%s' % ip_address.get('addr'))
Pau Espin Pedrol58603672018-08-09 13:45:55 +020039 self.run_dir = None
40 self.config_file = None
41 self.process = None
42 self.encryption = None
43 self.rsl_ip = None
Pau Espin Pedrolfed578e2019-06-19 17:18:43 +020044 self.use_osmux = "off"
Neels Hofmeyr798e5922017-05-18 15:24:02 +020045 self.suite_run = suite_run
46 self.ip_address = ip_address
Neels Hofmeyr798e5922017-05-18 15:24:02 +020047 self.bts = []
48 self.msc = msc
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +010049 self.mgw = mgw
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010050 self.stp = stp
Neels Hofmeyr798e5922017-05-18 15:24:02 +020051
52 def start(self):
53 self.log('Starting osmo-bsc')
Pau Espin Pedrold0912332017-06-14 13:27:08 +020054 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020055 self.configure()
56
Neels Hofmeyr36e04042017-09-04 16:49:17 +020057 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bsc')))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020058
59 binary = inst.child('bin', 'osmo-bsc')
60 if not os.path.isfile(binary):
61 raise RuntimeError('Binary missing: %r' % binary)
62 lib = inst.child('lib')
63 if not os.path.isdir(lib):
64 raise RuntimeError('No lib/ in %r' % inst)
65
Pau Espin Pedrol16abefb2018-10-09 18:54:45 +020066 if self.rsl_ip and self.addr() != self.rsl_ip:
67 filter = 'host %s or host %s and port not 22' % (self.addr(), self.rsl_ip)
68 else:
69 filter = 'host %s and port not 22' % self.addr()
70 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None, filter)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020071
72 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
73
74 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
75 self.process = process.Process(self.name(), self.run_dir,
76 (binary, '-c',
77 os.path.abspath(self.config_file)),
78 env=env)
79 self.suite_run.remember_to_stop(self.process)
80 self.process.launch()
81
82 def configure(self):
83 self.config_file = self.run_dir.new_file('osmo-bsc.cfg')
84 self.dbg(config_file=self.config_file)
85
86 values = dict(bsc=config.get_defaults('bsc'))
87 config.overlay(values, self.suite_run.config())
88 config.overlay(values, dict(bsc=dict(ip_address=self.ip_address)))
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +010089 config.overlay(values, self.mgw.conf_for_client())
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010090 config.overlay(values, self.stp.conf_for_client())
Neels Hofmeyr798e5922017-05-18 15:24:02 +020091
92 bts_list = []
93 for bts in self.bts:
94 bts_list.append(bts.conf_for_bsc())
95 config.overlay(values, dict(bsc=dict(net=dict(bts_list=bts_list))))
96
Pau Espin Pedrol387526a2017-08-24 15:12:59 +020097 # runtime parameters:
98 if self.encryption is not None:
Pau Espin Pedrolabd556a2017-09-04 16:26:08 +020099 encryption_vty = util.encryption2osmovty(self.encryption)
100 else:
101 encryption_vty = util.encryption2osmovty(values['bsc']['net']['encryption'])
102 config.overlay(values, dict(bsc=dict(net=dict(encryption=encryption_vty))))
Pau Espin Pedrolfed578e2019-06-19 17:18:43 +0200103 config.overlay(values, dict(bsc=dict(use_osmux=self.use_osmux)))
Pau Espin Pedrol387526a2017-08-24 15:12:59 +0200104
Pau Espin Pedrol63f2d472018-05-22 18:24:50 +0200105 if self.rsl_ip is not None:
106 config.overlay(values, dict(bsc=dict(net=dict(rsl_ip=self.rsl_ip))))
107
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200108 self.dbg('BSC CONFIG:\n' + pprint.pformat(values))
109
110 with open(self.config_file, 'w') as f:
111 r = template.render('osmo-bsc.cfg', values)
112 self.dbg(r)
113 f.write(r)
114
115 def addr(self):
116 return self.ip_address.get('addr')
117
Pau Espin Pedrol387526a2017-08-24 15:12:59 +0200118 def set_encryption(self, val):
119 self.encryption = val
120
Pau Espin Pedrol63f2d472018-05-22 18:24:50 +0200121 def set_rsl_ip(self, ip_addr):
122 '''Overwrite RSL IPaddr option sent to all BTS during OML config. Useful
123 for tests only willing to use osmo-bsc to do the OML setup but using
124 other external entities to test the RSL path, such as TTCN3 tests.'''
125 self.rsl_ip = ip_addr
126
Pau Espin Pedrolfed578e2019-06-19 17:18:43 +0200127 def set_use_osmux(self, use=False, force=False):
128 if not use:
129 self.use_osmux = "off"
130 else:
131 if not force:
132 self.use_osmux = "on"
133 else:
134 self.use_osmux = "only"
135
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200136 def bts_add(self, bts):
137 self.bts.append(bts)
138 bts.set_bsc(self)
139
Pau Espin Pedrol7bd71032017-12-11 15:02:26 +0100140 def bts_num(self, bts):
141 'Provide number id used by OsmoNITB to identify configured BTS'
142 # We take advantage from the fact that VTY code assigns VTY in ascending
143 # order through the bts nodes found. As we populate the config iterating
144 # over this list, we have a 1:1 match in indexes.
145 return self.bts.index(bts)
146
147 def bts_is_connected(self, bts):
148 return OsmoBscCtrl(self).bts_is_connected(self.bts_num(bts))
149
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200150 def running(self):
151 return not self.process.terminated()
152
Pau Espin Pedrol7bd71032017-12-11 15:02:26 +0100153
154class OsmoBscCtrl(log.Origin):
155 PORT = 4249
156 BTS_OML_STATE_VAR = "bts.%d.oml-connection-state"
157 BTS_OML_STATE_RE = re.compile("GET_REPLY (\d+) bts.\d+.oml-connection-state (?P<oml_state>\w+)")
158
159 def __init__(self, bsc):
160 self.bsc = bsc
161 super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.bsc.addr(), OsmoBscCtrl.PORT))
162
163 def ctrl(self):
164 return osmo_ctrl.OsmoCtrl(self.bsc.addr(), OsmoBscCtrl.PORT)
165
166 def bts_is_connected(self, bts_num):
167 with self.ctrl() as ctrl:
168 ctrl.do_get(OsmoBscCtrl.BTS_OML_STATE_VAR % bts_num)
169 data = ctrl.receive()
170 while (len(data) > 0):
171 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
172 answer_str = answer.decode('utf-8')
173 answer_str = answer_str.replace('\n', ' ')
174 res = OsmoBscCtrl.BTS_OML_STATE_RE.match(answer_str)
175 if res:
176 oml_state = str(res.group('oml_state'))
177 if oml_state == 'connected':
178 return True
179 return False
180
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200181# vim: expandtab tabstop=4 shiftwidth=4