blob: 29a8ac51b6f02c4d980ec7cdf713c0afbd3011e5 [file] [log] [blame]
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +01001# osmo_gsm_tester: specifics for running an ip.access nanoBTS
2#
3# Copyright (C) 2018 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Pau Espin Pedrol <pespin@sysmocom.de>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU 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 General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
21import pprint
22import tempfile
23import re
24from abc import ABCMeta, abstractmethod
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +020025from . import log, config, util, template, process, pcap_recorder, bts, pcu
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010026from . import powersupply
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +020027from .event_loop import MainLoop
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010028
29class NanoBts(bts.Bts):
30
31 pwsup = None
32 _pcu = None
33##############
34# PROTECTED
35##############
36 def __init__(self, suite_run, conf):
37 if conf.get('addr') is None:
38 raise log.Error('No attribute addr provided in conf!')
Pau Espin Pedrole5194622018-05-07 13:36:58 +020039 super().__init__(suite_run, conf, 'nanobts_%s' % conf.get('addr'), 'nanobts')
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010040
41 def _configure(self):
42 if self.bsc is None:
43 raise log.Error('BTS needs to be added to a BSC or NITB before it can be configured')
44
45 pwsup_opt = self.conf.get('power_supply', {})
46 if not pwsup_opt:
47 raise log.Error('No power_supply attribute provided in conf!')
48 pwsup_type = pwsup_opt.get('type')
49 if not pwsup_type:
50 raise log.Error('No type attribute provided in power_supply conf!')
51
52 self.pwsup = powersupply.get_instance_by_type(pwsup_type, pwsup_opt)
53
54########################
55# PUBLIC - INTERNAL API
56########################
57
58 def conf_for_bsc(self):
Pau Espin Pedrole5194622018-05-07 13:36:58 +020059 values = self.conf_for_bsc_prepare()
Pau Espin Pedrolfef9c1c2018-03-27 19:20:47 +020060 # Hack until we have proper ARFCN resource allocation support (OS#2230)
61 band = values.get('band')
62 trx_list = values.get('trx_list')
63 if band == 'GSM-1900':
64 config.overlay(trx_list[0], { 'arfcn' : '531' })
65 elif band == 'GSM-900':
66 config.overlay(trx_list[0], { 'arfcn' : '50' })
67
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010068 config.overlay(values, { 'osmobsc_bts_type': 'nanobts' })
69
70 self.dbg(conf=values)
71 return values
72
73
74 def cleanup(self):
75 if self.pwsup:
76 self.dbg('Powering off NanoBTS')
77 self.pwsup.power_set(False)
78
79###################
80# PUBLIC (test API included)
81###################
82
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +020083 def start(self, keepalive=False):
Pau Espin Pedrola238ed92018-03-26 13:06:12 +020084 if self.conf.get('ipa_unit_id') is None:
85 raise log.Error('No attribute %s provided in conf!' % attr)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010086 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
87 self._configure()
88
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010089 unitid = int(self.conf.get('ipa_unit_id'))
90 bts_ip = self.remote_addr()
Pau Espin Pedrola238ed92018-03-26 13:06:12 +020091 # This fine for now, however concurrent tests using Nanobts may run into "address already in use" since dst is broadcast.
92 # Once concurrency is needed, a new config attr should be added to have an extra static IP assigned on the main-unit to each Nanobts resource.
93 local_bind_ip =util.dst_ip_get_local_bind(bts_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010094
95 # Make sure nanoBTS is powered and in a clean state:
96 self.pwsup.power_cycle(1.0)
97
Pau Espin Pedrola238ed92018-03-26 13:06:12 +020098 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None,
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010099 'host %s and port not 22' % self.remote_addr())
100
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200101 self.log('Finding nanobts %s, binding on %s...' % (bts_ip, local_bind_ip))
102 ipfind = AbisIpFind(self.suite_run, self.run_dir, local_bind_ip, 'preconf')
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100103 ipfind.start()
104 ipfind.wait_bts_ready(bts_ip)
105 running_unitid = ipfind.get_unitid_by_ip(bts_ip)
106 self.log('Found nanobts %s with unit_id %d' % (bts_ip, running_unitid))
107 ipfind.stop()
108
109 ipconfig = IpAccessConfig(self.suite_run, self.run_dir, bts_ip)
110 if running_unitid != unitid:
111 if not ipconfig.set_unit_id(unitid, False):
112 raise log.Error('Failed configuring unit id %d' % unitid)
113 # Apply OML IP and restart nanoBTS as it is required to apply the changes.
114 if not ipconfig.set_oml_ip(self.bsc.addr(), True):
115 raise log.Error('Failed configuring OML IP %s' % bts_ip)
116
117 # Let some time for BTS to restart. It takes much more than 20 secs, and
118 # this way we make sure we don't catch responses in abisip-find prior to
119 # BTS restarting.
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200120 MainLoop.sleep(self, 20)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100121
122 self.log('Starting to connect to', self.bsc)
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200123 ipfind = AbisIpFind(self.suite_run, self.run_dir, local_bind_ip, 'postconf')
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100124 ipfind.start()
125 ipfind.wait_bts_ready(bts_ip)
126 self.log('nanoBTS configured and running')
127 ipfind.stop()
128
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200129 MainLoop.wait(self, self.bsc.bts_is_connected, self, timeout=600)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100130 self.log('nanoBTS connected to BSC')
131
132 #According to roh, it can be configured to use a static IP in a permanent way:
133 # 1- use abisip-find to find the default address
134 # 2- use ./ipaccess-config --ip-address IP/MASK
135 # 3- use ./ipaccess-config --ip-gateway IP to set the IP of the main unit
136 # 4- use ./ipaccess-config --restart to restart and apply the changes
137
138 #Start must do the following:
139 # 1- use abisip-find to find the default address
140 # 2- use ./ipaccess-config --unit-id UNIT_ID
141 # 3- use ./ipaccess-config --oml-ip --restart to set the IP of the BSC and apply+restart.
142 # According to roh, using the 3 of them together was not reliable to work properly.
143
144 def ready_for_pcu(self):
145 """We don't really care as we use a Dummy PCU class."""
146 return True
147
148 def pcu(self):
149 if not self._pcu:
150 self._pcu = pcu.PcuDummy(self.suite_run, self, self.conf)
151 return self._pcu
152
153
154class AbisIpFind(log.Origin):
155 suite_run = None
156 parent_run_dir = None
157 run_dir = None
158 inst = None
159 env = None
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200160 bind_ip = None
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100161 proc = None
162
163 BIN_ABISIP_FIND = 'abisip-find'
164 BTS_UNIT_ID_RE = re.compile("Unit_ID='(?P<unit_id>\d+)/\d+/\d+'")
165
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200166 def __init__(self, suite_run, parent_run_dir, bind_ip, name_suffix):
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100167 super().__init__(log.C_RUN, AbisIpFind.BIN_ABISIP_FIND + '-' + name_suffix)
168 self.suite_run = suite_run
169 self.parent_run_dir = parent_run_dir
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200170 self.bind_ip = bind_ip
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100171 self.env = {}
172
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100173 def start(self):
174 self.run_dir = util.Dir(self.parent_run_dir.new_dir(self.name()))
175 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bsc')))
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200176
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100177 lib = self.inst.child('lib')
178 if not os.path.isdir(lib):
179 raise log.Error('No lib/ in %r' % self.inst)
180 ipfind_path = self.inst.child('bin', AbisIpFind.BIN_ABISIP_FIND)
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200181 if not os.path.isfile(ipfind_path):
182 raise RuntimeError('Binary missing: %r' % ipfind_path)
183
184 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
185 self.proc = process.Process(self.name(), self.run_dir,
Pau Espin Pedrol48fce862018-04-04 12:44:24 +0200186 (ipfind_path, '-i', '1', '-b', self.bind_ip),
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200187 env=env)
188 self.suite_run.remember_to_stop(self.proc)
189 self.proc.launch()
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100190
191 def stop(self):
192 self.suite_run.stop_process(self.proc)
193
194 def get_line_by_ip(self, ipaddr):
195 """Get latest line (more up to date) from abisip-find based on ip address."""
196 token = "IP_Address='%s'" % ipaddr
197 myline = None
198 for line in (self.proc.get_stdout() or '').splitlines():
199 if token in line:
200 myline = line
201 return myline
202
203 def get_unitid_by_ip(self, ipaddr):
204 line = self.get_line_by_ip(ipaddr)
205 if line is None:
206 return None
207 res = AbisIpFind.BTS_UNIT_ID_RE.search(line)
208 if res:
209 unit_id = int(res.group('unit_id'))
210 return unit_id
211 raise log.Error('abisip-find unit_id field for nanobts %s not found in %s' %(ipaddr, line))
212
213 def bts_ready(self, ipaddr):
214 return self.get_line_by_ip(ipaddr) is not None
215
216 def wait_bts_ready(self, ipaddr):
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200217 MainLoop.wait(self, self.bts_ready, ipaddr)
Pau Espin Pedrol48fce862018-04-04 12:44:24 +0200218 # There's a period of time after boot in which nanobts answers to
219 # abisip-find but tcp RSTs ipacces-config conns. Let's wait in here a
220 # bit more time to avoid failing after stating the BTS is ready.
221 MainLoop.sleep(self, 2)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100222
223class IpAccessConfig(log.Origin):
224 suite_run = None
225 parent_run_dir = None
226 run_dir = None
227 inst = None
228 env = None
229 bts_ip = None
230
231 BIN_IPACCESS_CONFIG = 'ipaccess-config'
232
233 def __init__(self, suite_run, parent_run_dir, bts_ip):
234 super().__init__(log.C_RUN, IpAccessConfig.BIN_IPACCESS_CONFIG)
235 self.suite_run = suite_run
236 self.parent_run_dir = parent_run_dir
237 self.bts_ip = bts_ip
238 self.env = {}
239
240 def launch_process(self, binary_name, *args):
241 binary = os.path.abspath(self.inst.child('bin', binary_name))
242 run_dir = self.run_dir.new_dir(binary_name)
243 if not os.path.isfile(binary):
244 raise RuntimeError('Binary missing: %r' % binary)
245 proc = process.Process(binary_name, run_dir,
246 (binary,) + args,
247 env=self.env)
248 proc.launch()
249 return proc
250
251 def run(self, name_suffix, *args):
252 self.run_dir = util.Dir(self.parent_run_dir.new_dir(self.name()+'-'+name_suffix))
253 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bsc')))
254 lib = self.inst.child('lib')
255 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
256 self.proc = self.launch_process(IpAccessConfig.BIN_IPACCESS_CONFIG, *args)
257 try:
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200258 MainLoop.wait(self, self.proc.terminated)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100259 except Exception as e:
260 self.proc.terminate()
261 raise e
262 return self.proc.result
263
264 def set_unit_id(self, unitid, restart=False):
265 if restart:
266 retcode = self.run('unitid', '--restart', '--unit-id', '%d/0/0' % unitid, self.bts_ip)
267 else:
268 retcode = self.run('unitid', '--unit-id', '%d/0/0' % unitid, self.bts_ip)
269 if retcode != 0:
270 log.err('ipaccess-config --unit-id %d/0/0 returned error code %d' % (unitid, retcode))
271 return retcode == 0
272
273 def set_oml_ip(self, omlip, restart=False):
274 if restart:
275 retcode = self.run('oml', '--restart', '--oml-ip', omlip, self.bts_ip)
276 else:
277 retcode = self.run('oml', '--oml-ip', omlip, self.bts_ip)
278 if retcode != 0:
279 self.error('ipaccess-config --oml-ip %s returned error code %d' % (omlip, retcode))
280 return retcode == 0
281
282# vim: expandtab tabstop=4 shiftwidth=4