blob: ec9c3b86ab6be63fdf50b2d2c2bcae40263be194 [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
24from . import log, util, config, template, process, osmo_ctrl, pcap_recorder
25
26class OsmoBsc(log.Origin):
Neels Hofmeyr798e5922017-05-18 15:24:02 +020027
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010028 def __init__(self, suite_run, msc, mgw, stp, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020029 super().__init__(log.C_RUN, 'osmo-bsc_%s' % ip_address.get('addr'))
Pau Espin Pedrol58603672018-08-09 13:45:55 +020030 self.run_dir = None
31 self.config_file = None
32 self.process = None
33 self.encryption = None
34 self.rsl_ip = None
Neels Hofmeyr798e5922017-05-18 15:24:02 +020035 self.suite_run = suite_run
36 self.ip_address = ip_address
Neels Hofmeyr798e5922017-05-18 15:24:02 +020037 self.bts = []
38 self.msc = msc
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +010039 self.mgw = mgw
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010040 self.stp = stp
Neels Hofmeyr798e5922017-05-18 15:24:02 +020041
42 def start(self):
43 self.log('Starting osmo-bsc')
Pau Espin Pedrold0912332017-06-14 13:27:08 +020044 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020045 self.configure()
46
Neels Hofmeyr36e04042017-09-04 16:49:17 +020047 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bsc')))
Neels Hofmeyr798e5922017-05-18 15:24:02 +020048
49 binary = inst.child('bin', 'osmo-bsc')
50 if not os.path.isfile(binary):
51 raise RuntimeError('Binary missing: %r' % binary)
52 lib = inst.child('lib')
53 if not os.path.isdir(lib):
54 raise RuntimeError('No lib/ in %r' % inst)
55
Pau Espin Pedrol16abefb2018-10-09 18:54:45 +020056 if self.rsl_ip and self.addr() != self.rsl_ip:
57 filter = 'host %s or host %s and port not 22' % (self.addr(), self.rsl_ip)
58 else:
59 filter = 'host %s and port not 22' % self.addr()
60 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None, filter)
Neels Hofmeyr798e5922017-05-18 15:24:02 +020061
62 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
63
64 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
65 self.process = process.Process(self.name(), self.run_dir,
66 (binary, '-c',
67 os.path.abspath(self.config_file)),
68 env=env)
69 self.suite_run.remember_to_stop(self.process)
70 self.process.launch()
71
72 def configure(self):
73 self.config_file = self.run_dir.new_file('osmo-bsc.cfg')
74 self.dbg(config_file=self.config_file)
75
76 values = dict(bsc=config.get_defaults('bsc'))
77 config.overlay(values, self.suite_run.config())
78 config.overlay(values, dict(bsc=dict(ip_address=self.ip_address)))
Pau Espin Pedrol386b78d2017-11-09 13:02:09 +010079 config.overlay(values, self.mgw.conf_for_client())
Pau Espin Pedrol1e1d3812017-11-16 18:06:37 +010080 config.overlay(values, self.stp.conf_for_client())
Neels Hofmeyr798e5922017-05-18 15:24:02 +020081
82 bts_list = []
83 for bts in self.bts:
84 bts_list.append(bts.conf_for_bsc())
85 config.overlay(values, dict(bsc=dict(net=dict(bts_list=bts_list))))
86
Pau Espin Pedrol387526a2017-08-24 15:12:59 +020087 # runtime parameters:
88 if self.encryption is not None:
Pau Espin Pedrolabd556a2017-09-04 16:26:08 +020089 encryption_vty = util.encryption2osmovty(self.encryption)
90 else:
91 encryption_vty = util.encryption2osmovty(values['bsc']['net']['encryption'])
92 config.overlay(values, dict(bsc=dict(net=dict(encryption=encryption_vty))))
Pau Espin Pedrol387526a2017-08-24 15:12:59 +020093
Pau Espin Pedrol63f2d472018-05-22 18:24:50 +020094 if self.rsl_ip is not None:
95 config.overlay(values, dict(bsc=dict(net=dict(rsl_ip=self.rsl_ip))))
96
Neels Hofmeyr798e5922017-05-18 15:24:02 +020097 self.dbg('BSC CONFIG:\n' + pprint.pformat(values))
98
99 with open(self.config_file, 'w') as f:
100 r = template.render('osmo-bsc.cfg', values)
101 self.dbg(r)
102 f.write(r)
103
104 def addr(self):
105 return self.ip_address.get('addr')
106
Pau Espin Pedrol387526a2017-08-24 15:12:59 +0200107 def set_encryption(self, val):
108 self.encryption = val
109
Pau Espin Pedrol63f2d472018-05-22 18:24:50 +0200110 def set_rsl_ip(self, ip_addr):
111 '''Overwrite RSL IPaddr option sent to all BTS during OML config. Useful
112 for tests only willing to use osmo-bsc to do the OML setup but using
113 other external entities to test the RSL path, such as TTCN3 tests.'''
114 self.rsl_ip = ip_addr
115
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200116 def bts_add(self, bts):
117 self.bts.append(bts)
118 bts.set_bsc(self)
119
Pau Espin Pedrol7bd71032017-12-11 15:02:26 +0100120 def bts_num(self, bts):
121 'Provide number id used by OsmoNITB to identify configured BTS'
122 # We take advantage from the fact that VTY code assigns VTY in ascending
123 # order through the bts nodes found. As we populate the config iterating
124 # over this list, we have a 1:1 match in indexes.
125 return self.bts.index(bts)
126
127 def bts_is_connected(self, bts):
128 return OsmoBscCtrl(self).bts_is_connected(self.bts_num(bts))
129
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200130 def running(self):
131 return not self.process.terminated()
132
Pau Espin Pedrol7bd71032017-12-11 15:02:26 +0100133
134class OsmoBscCtrl(log.Origin):
135 PORT = 4249
136 BTS_OML_STATE_VAR = "bts.%d.oml-connection-state"
137 BTS_OML_STATE_RE = re.compile("GET_REPLY (\d+) bts.\d+.oml-connection-state (?P<oml_state>\w+)")
138
139 def __init__(self, bsc):
140 self.bsc = bsc
141 super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.bsc.addr(), OsmoBscCtrl.PORT))
142
143 def ctrl(self):
144 return osmo_ctrl.OsmoCtrl(self.bsc.addr(), OsmoBscCtrl.PORT)
145
146 def bts_is_connected(self, bts_num):
147 with self.ctrl() as ctrl:
148 ctrl.do_get(OsmoBscCtrl.BTS_OML_STATE_VAR % bts_num)
149 data = ctrl.receive()
150 while (len(data) > 0):
151 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
152 answer_str = answer.decode('utf-8')
153 answer_str = answer_str.replace('\n', ' ')
154 res = OsmoBscCtrl.BTS_OML_STATE_RE.match(answer_str)
155 if res:
156 oml_state = str(res.group('oml_state'))
157 if oml_state == 'connected':
158 return True
159 return False
160
Neels Hofmeyr798e5922017-05-18 15:24:02 +0200161# vim: expandtab tabstop=4 shiftwidth=4