blob: 823d79fbf73c13c536f57107aadc48f8f5b77378 [file] [log] [blame]
Harald Welte34b5a952019-05-27 11:54:11 +02001/* Osmocom VTY interface functions in TTCN-3
2 * (C) 2017-2018 Harald Welte <laforge@gnumonks.org>
3 * contributions by sysmocom - s.f.m.c. GmbH
4 * All rights reserved.
5 *
6 * Released under the terms of GNU General Public License, Version 2 or
7 * (at your option) any later version.
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
Harald Welte8542cef2017-07-19 20:06:26 +020012module Osmocom_VTY_Functions {
13 import from TELNETasp_PortType all;
Stefan Sperlingcff13562018-11-13 15:24:06 +010014 import from Osmocom_Types all;
Harald Welte8542cef2017-07-19 20:06:26 +020015
Harald Welte553f6552018-01-24 18:50:53 +010016 modulepar {
17 charstring mp_prompt_prefix := "OpenBSC";
18 }
Harald Welte8542cef2017-07-19 20:06:26 +020019
Harald Welte553f6552018-01-24 18:50:53 +010020 const charstring VTY_VIEW_SUFFIX := "> ";
21 const charstring VTY_ENABLE_SUFFIX := "# ";
22 const charstring VTY_CFG_SUFFIX := "(*)";
23
24 template charstring t_vty_unknown := pattern "*% Unknown command.";
Harald Welte8542cef2017-07-19 20:06:26 +020025
26 /* configure prompts in TELNETasp module */
Harald Weltea2663252018-09-10 10:21:44 +020027 function f_vty_set_prompts(TELNETasp_PT pt, charstring prompt_prefix := mp_prompt_prefix) {
Harald Welte553f6552018-01-24 18:50:53 +010028 var ASP_TelnetDynamicConfig vty_prompt[3] := {
29 {
30 prompt := {
31 id := 1,
Harald Weltea2663252018-09-10 10:21:44 +020032 prompt := prompt_prefix & VTY_VIEW_SUFFIX,
Harald Welte553f6552018-01-24 18:50:53 +010033 has_wildcards := false
34 }
35 }, {
36 prompt := {
37 id := 2,
Harald Weltea2663252018-09-10 10:21:44 +020038 prompt := prompt_prefix & VTY_ENABLE_SUFFIX,
Harald Welte553f6552018-01-24 18:50:53 +010039 has_wildcards := false
40 }
41 }, {
42 prompt := {
43 id := 3,
Harald Weltea2663252018-09-10 10:21:44 +020044 prompt := prompt_prefix & VTY_CFG_SUFFIX,
Harald Welte553f6552018-01-24 18:50:53 +010045 has_wildcards := true
46 }
47 }
48 };
49
Harald Welte8542cef2017-07-19 20:06:26 +020050 /* set some configuration that isn't possible to express
51 * in the config file due to syntactic restrictions (Who invents config
52 * files that don't permit regular expressions? */
53 for (var integer i := 0; i < sizeof(vty_prompt); i:= i + 1) {
Harald Welte553f6552018-01-24 18:50:53 +010054 pt.send(vty_prompt[i]);
Harald Welte8542cef2017-07-19 20:06:26 +020055 }
56 }
57
58 /* wait for any of the permitted prompts; buffer + return all intermediate output */
59 function f_vty_wait_for_prompt(TELNETasp_PT pt) return charstring {
Harald Welte8542cef2017-07-19 20:06:26 +020060 var charstring rx, buf := "";
Stefan Sperling23b45972018-07-27 17:20:38 +020061 var integer fd;
Harald Welte8542cef2017-07-19 20:06:26 +020062 timer T := 2.0;
63
64 T.start;
65 alt {
Pau Espin Pedrol1a4d4e92019-12-04 16:59:29 +010066 [] pt.receive(pattern "[\w-]+" & VTY_VIEW_SUFFIX) { };
67 [] pt.receive(pattern "[\w-]+\# ") { };
68 [] pt.receive(pattern "[\w-]+\(*\)\# ") { };
Harald Welte553f6552018-01-24 18:50:53 +010069 [] pt.receive(t_vty_unknown) {
70 testcase.stop(fail, "VTY: Unknown Command");
71 };
Harald Welte8542cef2017-07-19 20:06:26 +020072 [] pt.receive(charstring:?) -> value rx { buf := buf & rx; repeat };
Stefan Sperling23b45972018-07-27 17:20:38 +020073 [] pt.receive(integer:?) -> value fd {
74 if (fd == -1) {
75 setverdict(fail, "VTY Telnet Connection Failure");
76 mtc.stop;
77 } else {
78 repeat; /* telnet connection succeeded */
79 }
80 }
Harald Welte553f6552018-01-24 18:50:53 +010081 [] T.timeout {
82 setverdict(fail, "VTY Timeout for prompt");
Daniel Willmanne4ff5372018-07-05 17:35:03 +020083 mtc.stop;
Harald Welte553f6552018-01-24 18:50:53 +010084 };
Harald Welte8542cef2017-07-19 20:06:26 +020085 }
86 T.stop;
87 return buf;
88 }
89
90 /* send a VTY command and obtain response until prompt is received */
91 function f_vty_transceive_ret(TELNETasp_PT pt, charstring tx) return charstring {
92 pt.send(tx);
93 return f_vty_wait_for_prompt(pt);
94 }
95
96 /* send a VTY command and obtain response until prompt is received */
97 function f_vty_transceive(TELNETasp_PT pt, charstring tx) {
Harald Welte930d0a72018-03-22 22:08:40 +010098 var charstring unused := f_vty_transceive_ret(pt, tx);
Harald Welte8542cef2017-07-19 20:06:26 +020099 }
100
101 type integer BtsNr (0..255);
102 type integer BtsTrxNr (0..255);
103 type integer BtsTimeslotNr (0..7);
Philipp Maierd0e64b02019-03-13 14:15:23 +0100104 type integer MscNr (0..255);
Pau Espin Pedrolc675b612020-01-09 19:55:40 +0100105 type integer Cs7Nr (0..255);
Harald Welte8542cef2017-07-19 20:06:26 +0200106
107 type charstring BtsGprsMode ("none", "gprs", "egrps");
108
109 /* enter the'confiugration' mode of the VTY */
110 function f_vty_enter_config(TELNETasp_PT pt) {
111 f_vty_transceive(pt, "configure terminal")
112 }
113
114 function f_vty_enter_cfg_network(TELNETasp_PT pt) {
115 f_vty_enter_config(pt);
116 f_vty_transceive(pt, "network")
117 }
118
119 function f_vty_enter_cfg_bts(TELNETasp_PT pt, BtsNr bts := 0) {
120 f_vty_enter_cfg_network(pt);
121 f_vty_transceive(pt, "bts " & int2str(bts));
122 }
123
124 function f_vty_enter_cfg_trx(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0) {
125 f_vty_enter_cfg_bts(pt, bts);
126 f_vty_transceive(pt, "trx " & int2str(trx));
127 }
128
129 function f_vty_enter_cfg_ts(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0, BtsTimeslotNr ts) {
130 f_vty_enter_cfg_trx(pt, bts, trx);
131 f_vty_transceive(pt, "timeslot " & int2str(ts));
132 }
133
Philipp Maierd0e64b02019-03-13 14:15:23 +0100134 function f_vty_enter_cfg_msc(TELNETasp_PT pt, MscNr msc := 0) {
135 f_vty_enter_config(pt);
136 f_vty_transceive(pt, "msc " & int2str(msc));
137 }
138
Pau Espin Pedrolc675b612020-01-09 19:55:40 +0100139 function f_vty_enter_cfg_cs7_inst(TELNETasp_PT pt, Cs7Nr cs7_inst := 0) {
140 f_vty_enter_config(pt);
141 f_vty_transceive(pt, "cs7 instance " & int2str(cs7_inst));
142 }
143
Harald Weltef640a012018-04-14 17:49:21 +0200144type record of charstring rof_charstring;
145function f_vty_config2(TELNETasp_PT pt, rof_charstring config_nodes, charstring cmd)
Harald Welte872ce172018-02-16 22:10:33 +0100146{
147 /* enter config mode; enter node */
148 f_vty_enter_config(pt);
Harald Weltef640a012018-04-14 17:49:21 +0200149 for (var integer i := 0; i < sizeof(config_nodes); i := i+1) {
150 f_vty_transceive(pt, config_nodes[i]);
151 }
Harald Welte872ce172018-02-16 22:10:33 +0100152 /* execute command */
153 f_vty_transceive(pt, cmd);
154 /* leave config mode */
155 f_vty_transceive(pt, "end");
156}
157
158
Harald Weltef640a012018-04-14 17:49:21 +0200159function f_vty_config(TELNETasp_PT pt, charstring config_node, charstring cmd)
160{
161 f_vty_config2(pt, {config_node}, cmd);
162}
163
Alexander Couzens98aa59e2018-06-12 13:45:59 +0200164function f_vty_transceive_match(TELNETasp_PT pt, charstring cmd, template charstring exp_ret) {
165 var charstring ret := f_vty_transceive_ret(pt, cmd);
166 if (not match(ret, exp_ret)) {
167 setverdict(fail, "Non-matching VTY response: ", ret);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200168 mtc.stop;
Alexander Couzens98aa59e2018-06-12 13:45:59 +0200169 }
170}
171
Alexander Couzens1e6d9902018-06-12 13:48:26 +0200172function f_vty_transceive_not_match(TELNETasp_PT pt, charstring cmd, template charstring exp_ret) {
173 var charstring ret := f_vty_transceive_ret(pt, cmd);
174 if (match(ret, exp_ret)) {
175 setverdict(fail, "Unexpected matching VTY response: ", ret);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200176 mtc.stop;
Alexander Couzens1e6d9902018-06-12 13:48:26 +0200177 }
178}
179
Stefan Sperlingcff13562018-11-13 15:24:06 +0100180function f_vty_transceive_match_regex(TELNETasp_PT pt, charstring cmd, charstring regex, integer groupno) return charstring
181{
182 var charstring resp := f_vty_transceive_ret(pt, cmd);
183 return regexp(resp, regex, groupno);
184}
185
186function f_vty_transceive_match_regexp_retry(TELNETasp_PT pt, charstring cmd, charstring regex,
187 integer groupno, integer num_attempts, float retry_delay) return charstring
188{
189 while (num_attempts > 0) {
190 var charstring ret := f_vty_transceive_match_regex(pt, cmd, regex, groupno);
191 if (ret != "") {
192 return ret;
193 }
194 f_sleep(retry_delay);
195 num_attempts := num_attempts - 1;
196 }
197
198 setverdict(fail, "No matching VTY response for regular expression '", regex,
199 "' after ", num_attempts, " attempts." );
200 mtc.stop;
201}
Harald Weltef640a012018-04-14 17:49:21 +0200202
Harald Welte8542cef2017-07-19 20:06:26 +0200203}