blob: 877db2928a30db9811c347e9a8164bca6ee4f4ee [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
Neels Hofmeyraf4e2312020-11-27 08:20:56 +010023import re
Neels Hofmeyr798e5922017-05-18 15:24:02 +020024
Pau Espin Pedrole1a58bd2020-04-10 20:46:07 +020025from ..core import log, util, config, template, process
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020026from ..core import schema
Neels Hofmeyraf4e2312020-11-27 08:20:56 +010027from . import osmo_ctrl, osmo_vty, pcap_recorder
Neels Hofmeyr798e5922017-05-18 15:24:02 +020028
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020029def on_register_schemas():
30 config_schema = {
31 'net.codec_list[]': schema.CODEC,
32 }
33 schema.register_config_schema('bsc', config_schema)
34
35
Neels Hofmeyr798e5922017-05-18 15:24:02 +020036class OsmoBsc(log.Origin):
Neels Hofmeyr798e5922017-05-18 15:24:02 +020037
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020038 def __init__(self, testenv, msc, mgw, stp, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020039 super().__init__(log.C_RUN, 'osmo-bsc_%s' % ip_address.get('addr'))
Pau Espin Pedrol58603672018-08-09 13:45:55 +020040 self.run_dir = None
41 self.config_file = None
42 self.process = None
43 self.encryption = None
44 self.rsl_ip = None
Pau Espin Pedrolfed578e2019-06-19 17:18:43 +020045 self.use_osmux = "off"
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020046 self.testenv = testenv
Neels Hofmeyr798e5922017-05-18 15:24:02 +020047 self.ip_address = ip_address
Neels Hofmeyr798e5922017-05-18 15:24:02 +020048 self.bts = []
49 self.msc = msc
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +010050 self.mgw = mgw
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010051 self.stp = stp
Neels Hofmeyraf4e2312020-11-27 08:20:56 +010052 self.vty = None
Neels Hofmeyr012a17d2020-12-08 17:36:04 +010053 self.ctrl = None
Neels Hofmeyr798e5922017-05-18 15:24:02 +020054
55 def start(self):
56 self.log('Starting osmo-bsc')
Pau Espin Pedrol2a2d8462020-05-11 10:56:52 +020057 self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name()))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020058 self.configure()
59
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020060 inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmo-bsc')))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020061
62 binary = inst.child('bin', 'osmo-bsc')
63 if not os.path.isfile(binary):
64 raise RuntimeError('Binary missing: %r' % binary)
65 lib = inst.child('lib')
66 if not os.path.isdir(lib):
67 raise RuntimeError('No lib/ in %r' % inst)
68
Pau Espin Pedrol16abefb2018-10-09 18:54:45 +020069 if self.rsl_ip and self.addr() != self.rsl_ip:
70 filter = 'host %s or host %s and port not 22' % (self.addr(), self.rsl_ip)
71 else:
72 filter = 'host %s and port not 22' % self.addr()
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020073 pcap_recorder.PcapRecorder(self.testenv, self.run_dir.new_dir('pcap'), None, filter)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020074
75 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
76
77 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
78 self.process = process.Process(self.name(), self.run_dir,
79 (binary, '-c',
80 os.path.abspath(self.config_file)),
81 env=env)
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020082 self.testenv.remember_to_stop(self.process)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020083 self.process.launch()
84
Neels Hofmeyraf4e2312020-11-27 08:20:56 +010085 self.vty = OsmoBscVty(self)
86 self.vty.connect()
87
Neels Hofmeyr012a17d2020-12-08 17:36:04 +010088 self.ctrl = OsmoBscCtrl(self)
89 self.ctrl.connect()
90
Neels Hofmeyr798e5922017-05-18 15:24:02 +020091 def configure(self):
92 self.config_file = self.run_dir.new_file('osmo-bsc.cfg')
93 self.dbg(config_file=self.config_file)
94
95 values = dict(bsc=config.get_defaults('bsc'))
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020096 config.overlay(values, self.testenv.suite().config())
Neels Hofmeyr798e5922017-05-18 15:24:02 +020097 config.overlay(values, dict(bsc=dict(ip_address=self.ip_address)))
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +010098 config.overlay(values, self.mgw.conf_for_client())
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010099 config.overlay(values, self.stp.conf_for_client())
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200100
101 bts_list = []
102 for bts in self.bts:
103 bts_list.append(bts.conf_for_bsc())
104 config.overlay(values, dict(bsc=dict(net=dict(bts_list=bts_list))))
105
Pau Espin Pedrol387526a2017-08-24 15:12:59 +0200106 # runtime parameters:
107 if self.encryption is not None:
Pau Espin Pedrolabd556a2017-09-04 16:26:08 +0200108 encryption_vty = util.encryption2osmovty(self.encryption)
109 else:
110 encryption_vty = util.encryption2osmovty(values['bsc']['net']['encryption'])
111 config.overlay(values, dict(bsc=dict(net=dict(encryption=encryption_vty))))
Pau Espin Pedrolfed578e2019-06-19 17:18:43 +0200112 config.overlay(values, dict(bsc=dict(use_osmux=self.use_osmux)))
Pau Espin Pedrol387526a2017-08-24 15:12:59 +0200113
Pau Espin Pedrol63f2d472018-05-22 18:24:50 +0200114 if self.rsl_ip is not None:
115 config.overlay(values, dict(bsc=dict(net=dict(rsl_ip=self.rsl_ip))))
116
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200117 self.dbg('BSC CONFIG:\n' + pprint.pformat(values))
118
119 with open(self.config_file, 'w') as f:
120 r = template.render('osmo-bsc.cfg', values)
121 self.dbg(r)
122 f.write(r)
123
124 def addr(self):
125 return self.ip_address.get('addr')
126
Pau Espin Pedrol387526a2017-08-24 15:12:59 +0200127 def set_encryption(self, val):
128 self.encryption = val
129
Pau Espin Pedrol63f2d472018-05-22 18:24:50 +0200130 def set_rsl_ip(self, ip_addr):
131 '''Overwrite RSL IPaddr option sent to all BTS during OML config. Useful
132 for tests only willing to use osmo-bsc to do the OML setup but using
133 other external entities to test the RSL path, such as TTCN3 tests.'''
134 self.rsl_ip = ip_addr
135
Pau Espin Pedrolfed578e2019-06-19 17:18:43 +0200136 def set_use_osmux(self, use=False, force=False):
137 if not use:
138 self.use_osmux = "off"
139 else:
140 if not force:
141 self.use_osmux = "on"
142 else:
143 self.use_osmux = "only"
144
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200145 def bts_add(self, bts):
146 self.bts.append(bts)
147 bts.set_bsc(self)
148
Pau Espin Pedrol7bd71032017-12-11 15:02:26 +0100149 def bts_num(self, bts):
150 'Provide number id used by OsmoNITB to identify configured BTS'
151 # We take advantage from the fact that VTY code assigns VTY in ascending
152 # order through the bts nodes found. As we populate the config iterating
153 # over this list, we have a 1:1 match in indexes.
154 return self.bts.index(bts)
155
Neels Hofmeyr012a17d2020-12-08 17:36:04 +0100156 def bts_is_connected(self, bts):
157 return self.ctrl.bts_is_connected(self.bts_num(bts))
Pau Espin Pedrol7bd71032017-12-11 15:02:26 +0100158
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200159 def running(self):
160 return not self.process.terminated()
161
Neels Hofmeyraf4e2312020-11-27 08:20:56 +0100162 def cleanup(self):
163 if self.vty is not None:
164 self.vty.disconnect()
165 self.vty = None
Neels Hofmeyr012a17d2020-12-08 17:36:04 +0100166 if self.ctrl is not None:
167 self.ctrl.disconnect()
168 self.ctrl = None
Neels Hofmeyr5b04ef22020-12-01 04:48:38 +0100169
170class OsmoBscCtrl(osmo_ctrl.OsmoCtrl):
171 def __init__(self, bsc, port=4249):
172 self.bsc = bsc
173 super().__init__(bsc.addr(), port)
Pau Espin Pedrol7bd71032017-12-11 15:02:26 +0100174
175 def bts_is_connected(self, bts_num):
Neels Hofmeyr5b04ef22020-12-01 04:48:38 +0100176 return self.get_var('bts.%d.oml-connection-state' % bts_num) == 'connected'
Pau Espin Pedrol7bd71032017-12-11 15:02:26 +0100177
Neels Hofmeyraf4e2312020-11-27 08:20:56 +0100178class OsmoBscVty(osmo_vty.OsmoVty):
179 def __init__(self, bsc, port=4242):
180 self.bsc = bsc
181 super().__init__(self.bsc.addr(), port)
182
Neels Hofmeyrca155b62020-11-27 08:20:56 +0100183 def get_active_lchans(self):
184 lchan_summary = self.cmd('show lchan summary')
185
186 re_lchan_summary = re.compile('BTS ([0-9]+), TRX ([0-9]+), Timeslot ([0-9]+) *([^,]*), Lchan ([0-9]+),.* State ([A-Za-z_]+).*')
187 active_lchans = set()
188 for line in lchan_summary:
189 m = re_lchan_summary.match(line)
190 if m:
191 bts, trx, ts, lchan_type, subslot, state = m.groups()
192 active_lchans.add('%s-%s-%s-%s %s %s' % (bts, trx, ts, subslot, lchan_type, state))
193 if not active_lchans:
194 self.dbg('No active lchans')
195 else:
196 self.dbg('Active lchans:\n|', '\n| '.join(active_lchans), '\n');
197 return active_lchans
198
199 def active_lchans_match(self, expected=[], not_expected=[]):
200 active_lchans = self.get_active_lchans()
201 matches = []
202 mismatches = []
203
204 for expected_lchan in expected:
205 found = False
206 for active_lchan in active_lchans:
207 if active_lchan.startswith(expected_lchan):
208 found = True
209 break
210 if found:
211 matches.append(expected_lchan)
212 else:
213 mismatches.append('missing: ' + expected_lchan)
214
215 for not_expected_lchan in not_expected:
216 found = False
217 for active_lchan in active_lchans:
218 if active_lchan.startswith(not_expected_lchan):
219 found = True
220 break
221 if not found:
222 matches.append('not: ' + not_expected_lchan)
223 else:
224 mismatches.append('unexpected: ' + not_expected_lchan)
225
226 if matches:
227 self.log('Found matching lchan activity (%d of %d requirements):' % (len(matches), len(expected) + len(not_expected)), matches)
228 if mismatches:
229 self.err('Found unexpected lchan activity (%d of %d requirements):' % (len(mismatches), len(expected) + len(not_expected)), mismatches)
230 return not mismatches
231
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200232# vim: expandtab tabstop=4 shiftwidth=4