blob: f42bc6e3387ba34b81280717c27452a6c084c205 [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
25from . import log, config, util, template, process, event_loop, pcap_recorder, bts, pcu
26from . import powersupply
27
28class NanoBts(bts.Bts):
29
30 pwsup = None
31 _pcu = None
32##############
33# PROTECTED
34##############
35 def __init__(self, suite_run, conf):
36 if conf.get('addr') is None:
37 raise log.Error('No attribute addr provided in conf!')
38 super().__init__(suite_run, conf, 'nanobts_%s' % conf.get('addr'))
39
40 def _configure(self):
41 if self.bsc is None:
42 raise log.Error('BTS needs to be added to a BSC or NITB before it can be configured')
43
44 pwsup_opt = self.conf.get('power_supply', {})
45 if not pwsup_opt:
46 raise log.Error('No power_supply attribute provided in conf!')
47 pwsup_type = pwsup_opt.get('type')
48 if not pwsup_type:
49 raise log.Error('No type attribute provided in power_supply conf!')
50
51 self.pwsup = powersupply.get_instance_by_type(pwsup_type, pwsup_opt)
52
53########################
54# PUBLIC - INTERNAL API
55########################
56
57 def conf_for_bsc(self):
58 values = config.get_defaults('bsc_bts')
59 config.overlay(values, config.get_defaults('nanobts'))
60 if self.lac is not None:
61 config.overlay(values, { 'location_area_code': self.lac })
62 if self.rac is not None:
63 config.overlay(values, { 'routing_area_code': self.rac })
64 if self.cellid is not None:
65 config.overlay(values, { 'cell_identity': self.cellid })
66 if self.bvci is not None:
67 config.overlay(values, { 'bvci': self.bvci })
68 config.overlay(values, self.conf)
69
70 sgsn_conf = {} if self.sgsn is None else self.sgsn.conf_for_client()
71 config.overlay(values, sgsn_conf)
72
73 config.overlay(values, { 'osmobsc_bts_type': 'nanobts' })
74
75 self.dbg(conf=values)
76 return values
77
78
79 def cleanup(self):
80 if self.pwsup:
81 self.dbg('Powering off NanoBTS')
82 self.pwsup.power_set(False)
83
84###################
85# PUBLIC (test API included)
86###################
87
88 def start(self):
Pau Espin Pedrola238ed92018-03-26 13:06:12 +020089 if self.conf.get('ipa_unit_id') is None:
90 raise log.Error('No attribute %s provided in conf!' % attr)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010091 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
92 self._configure()
93
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010094 unitid = int(self.conf.get('ipa_unit_id'))
95 bts_ip = self.remote_addr()
Pau Espin Pedrola238ed92018-03-26 13:06:12 +020096 # This fine for now, however concurrent tests using Nanobts may run into "address already in use" since dst is broadcast.
97 # 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.
98 local_bind_ip =util.dst_ip_get_local_bind(bts_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010099
100 # Make sure nanoBTS is powered and in a clean state:
101 self.pwsup.power_cycle(1.0)
102
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200103 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None,
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100104 'host %s and port not 22' % self.remote_addr())
105
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200106 self.log('Finding nanobts %s, binding on %s...' % (bts_ip, local_bind_ip))
107 ipfind = AbisIpFind(self.suite_run, self.run_dir, local_bind_ip, 'preconf')
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100108 ipfind.start()
109 ipfind.wait_bts_ready(bts_ip)
110 running_unitid = ipfind.get_unitid_by_ip(bts_ip)
111 self.log('Found nanobts %s with unit_id %d' % (bts_ip, running_unitid))
112 ipfind.stop()
113
114 ipconfig = IpAccessConfig(self.suite_run, self.run_dir, bts_ip)
115 if running_unitid != unitid:
116 if not ipconfig.set_unit_id(unitid, False):
117 raise log.Error('Failed configuring unit id %d' % unitid)
118 # Apply OML IP and restart nanoBTS as it is required to apply the changes.
119 if not ipconfig.set_oml_ip(self.bsc.addr(), True):
120 raise log.Error('Failed configuring OML IP %s' % bts_ip)
121
122 # Let some time for BTS to restart. It takes much more than 20 secs, and
123 # this way we make sure we don't catch responses in abisip-find prior to
124 # BTS restarting.
125 event_loop.sleep(self, 20)
126
127 self.log('Starting to connect to', self.bsc)
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200128 ipfind = AbisIpFind(self.suite_run, self.run_dir, local_bind_ip, 'postconf')
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100129 ipfind.start()
130 ipfind.wait_bts_ready(bts_ip)
131 self.log('nanoBTS configured and running')
132 ipfind.stop()
133
134 event_loop.wait(self, self.bsc.bts_is_connected, self, timeout=600)
135 self.log('nanoBTS connected to BSC')
136
137 #According to roh, it can be configured to use a static IP in a permanent way:
138 # 1- use abisip-find to find the default address
139 # 2- use ./ipaccess-config --ip-address IP/MASK
140 # 3- use ./ipaccess-config --ip-gateway IP to set the IP of the main unit
141 # 4- use ./ipaccess-config --restart to restart and apply the changes
142
143 #Start must do the following:
144 # 1- use abisip-find to find the default address
145 # 2- use ./ipaccess-config --unit-id UNIT_ID
146 # 3- use ./ipaccess-config --oml-ip --restart to set the IP of the BSC and apply+restart.
147 # According to roh, using the 3 of them together was not reliable to work properly.
148
149 def ready_for_pcu(self):
150 """We don't really care as we use a Dummy PCU class."""
151 return True
152
153 def pcu(self):
154 if not self._pcu:
155 self._pcu = pcu.PcuDummy(self.suite_run, self, self.conf)
156 return self._pcu
157
158
159class AbisIpFind(log.Origin):
160 suite_run = None
161 parent_run_dir = None
162 run_dir = None
163 inst = None
164 env = None
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200165 bind_ip = None
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100166 proc = None
167
168 BIN_ABISIP_FIND = 'abisip-find'
169 BTS_UNIT_ID_RE = re.compile("Unit_ID='(?P<unit_id>\d+)/\d+/\d+'")
170
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200171 def __init__(self, suite_run, parent_run_dir, bind_ip, name_suffix):
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100172 super().__init__(log.C_RUN, AbisIpFind.BIN_ABISIP_FIND + '-' + name_suffix)
173 self.suite_run = suite_run
174 self.parent_run_dir = parent_run_dir
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200175 self.bind_ip = bind_ip
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100176 self.env = {}
177
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100178 def start(self):
179 self.run_dir = util.Dir(self.parent_run_dir.new_dir(self.name()))
180 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bsc')))
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200181
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100182 lib = self.inst.child('lib')
183 if not os.path.isdir(lib):
184 raise log.Error('No lib/ in %r' % self.inst)
185 ipfind_path = self.inst.child('bin', AbisIpFind.BIN_ABISIP_FIND)
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200186 if not os.path.isfile(ipfind_path):
187 raise RuntimeError('Binary missing: %r' % ipfind_path)
188
189 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
190 self.proc = process.Process(self.name(), self.run_dir,
191 (ipfind_path, '-b', self.bind_ip),
192 env=env)
193 self.suite_run.remember_to_stop(self.proc)
194 self.proc.launch()
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100195
196 def stop(self):
197 self.suite_run.stop_process(self.proc)
198
199 def get_line_by_ip(self, ipaddr):
200 """Get latest line (more up to date) from abisip-find based on ip address."""
201 token = "IP_Address='%s'" % ipaddr
202 myline = None
203 for line in (self.proc.get_stdout() or '').splitlines():
204 if token in line:
205 myline = line
206 return myline
207
208 def get_unitid_by_ip(self, ipaddr):
209 line = self.get_line_by_ip(ipaddr)
210 if line is None:
211 return None
212 res = AbisIpFind.BTS_UNIT_ID_RE.search(line)
213 if res:
214 unit_id = int(res.group('unit_id'))
215 return unit_id
216 raise log.Error('abisip-find unit_id field for nanobts %s not found in %s' %(ipaddr, line))
217
218 def bts_ready(self, ipaddr):
219 return self.get_line_by_ip(ipaddr) is not None
220
221 def wait_bts_ready(self, ipaddr):
222 event_loop.wait(self, self.bts_ready, ipaddr)
223
224
225class IpAccessConfig(log.Origin):
226 suite_run = None
227 parent_run_dir = None
228 run_dir = None
229 inst = None
230 env = None
231 bts_ip = None
232
233 BIN_IPACCESS_CONFIG = 'ipaccess-config'
234
235 def __init__(self, suite_run, parent_run_dir, bts_ip):
236 super().__init__(log.C_RUN, IpAccessConfig.BIN_IPACCESS_CONFIG)
237 self.suite_run = suite_run
238 self.parent_run_dir = parent_run_dir
239 self.bts_ip = bts_ip
240 self.env = {}
241
242 def launch_process(self, binary_name, *args):
243 binary = os.path.abspath(self.inst.child('bin', binary_name))
244 run_dir = self.run_dir.new_dir(binary_name)
245 if not os.path.isfile(binary):
246 raise RuntimeError('Binary missing: %r' % binary)
247 proc = process.Process(binary_name, run_dir,
248 (binary,) + args,
249 env=self.env)
250 proc.launch()
251 return proc
252
253 def run(self, name_suffix, *args):
254 self.run_dir = util.Dir(self.parent_run_dir.new_dir(self.name()+'-'+name_suffix))
255 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bsc')))
256 lib = self.inst.child('lib')
257 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
258 self.proc = self.launch_process(IpAccessConfig.BIN_IPACCESS_CONFIG, *args)
259 try:
260 event_loop.wait(self, self.proc.terminated)
261 except Exception as e:
262 self.proc.terminate()
263 raise e
264 return self.proc.result
265
266 def set_unit_id(self, unitid, restart=False):
267 if restart:
268 retcode = self.run('unitid', '--restart', '--unit-id', '%d/0/0' % unitid, self.bts_ip)
269 else:
270 retcode = self.run('unitid', '--unit-id', '%d/0/0' % unitid, self.bts_ip)
271 if retcode != 0:
272 log.err('ipaccess-config --unit-id %d/0/0 returned error code %d' % (unitid, retcode))
273 return retcode == 0
274
275 def set_oml_ip(self, omlip, restart=False):
276 if restart:
277 retcode = self.run('oml', '--restart', '--oml-ip', omlip, self.bts_ip)
278 else:
279 retcode = self.run('oml', '--oml-ip', omlip, self.bts_ip)
280 if retcode != 0:
281 self.error('ipaccess-config --oml-ip %s returned error code %d' % (omlip, retcode))
282 return retcode == 0
283
284# vim: expandtab tabstop=4 shiftwidth=4