blob: a9f4c24aef9c1d6c091c1d50fa6bcb2dfc6c9dfb [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 */
Vadim Yanitskiy26e30aa2021-01-30 14:06:37 +010059 function f_vty_wait_for_prompt(TELNETasp_PT pt, boolean strict := true, charstring log_label := "(?)")
60 return charstring {
Harald Welte8542cef2017-07-19 20:06:26 +020061 var charstring rx, buf := "";
Stefan Sperling23b45972018-07-27 17:20:38 +020062 var integer fd;
Harald Welte8542cef2017-07-19 20:06:26 +020063 timer T := 2.0;
64
65 T.start;
66 alt {
Vadim Yanitskiy26e30aa2021-01-30 14:06:37 +010067 [] pt.receive(pattern "[\w-]+" & VTY_VIEW_SUFFIX) { };
68 [] pt.receive(pattern "[\w-]+\# ") { };
69 [] pt.receive(pattern "[\w-]+\(*\)\# ") { };
70 [] pt.receive(t_vty_unknown) -> value rx {
71 if (strict) {
72 setverdict(fail, "VTY: Unknown Command: " & log_label);
Daniel Willmanne4ff5372018-07-05 17:35:03 +020073 mtc.stop;
Vadim Yanitskiy26e30aa2021-01-30 14:06:37 +010074 } else {
75 log("VTY: Unknown Command (ignored): " & log_label);
76 buf := buf & rx;
77 repeat;
78 }
79 };
80 [] pt.receive(charstring:?) -> value rx { buf := buf & rx; repeat };
81 [] pt.receive(integer:?) -> value fd {
82 if (fd == -1) {
83 setverdict(fail, "VTY Telnet Connection Failure: " & log_label);
84 mtc.stop;
85 } else {
86 repeat; /* telnet connection succeeded */
87 }
88 }
89 [] T.timeout {
90 setverdict(fail, "VTY Timeout for prompt: " & log_label);
91 mtc.stop;
92 };
Harald Welte8542cef2017-07-19 20:06:26 +020093 }
94 T.stop;
95 return buf;
96 }
97
98 /* send a VTY command and obtain response until prompt is received */
Neels Hofmeyr2fe5ad12020-08-09 21:50:25 +000099 function f_vty_transceive_ret(TELNETasp_PT pt, charstring tx, boolean strict := true) return charstring {
Harald Welte8542cef2017-07-19 20:06:26 +0200100 pt.send(tx);
Neels Hofmeyr9ebabc82020-11-25 22:56:13 +0000101 return f_vty_wait_for_prompt(pt, strict, tx);
Harald Welte8542cef2017-07-19 20:06:26 +0200102 }
103
104 /* send a VTY command and obtain response until prompt is received */
Neels Hofmeyr2fe5ad12020-08-09 21:50:25 +0000105 function f_vty_transceive(TELNETasp_PT pt, charstring tx, boolean strict := true) {
106 var charstring unused := f_vty_transceive_ret(pt, tx, strict);
Harald Welte8542cef2017-07-19 20:06:26 +0200107 }
108
109 type integer BtsNr (0..255);
110 type integer BtsTrxNr (0..255);
111 type integer BtsTimeslotNr (0..7);
Philipp Maierd0e64b02019-03-13 14:15:23 +0100112 type integer MscNr (0..255);
Pau Espin Pedrolc675b612020-01-09 19:55:40 +0100113 type integer Cs7Nr (0..255);
Harald Welte8542cef2017-07-19 20:06:26 +0200114
115 type charstring BtsGprsMode ("none", "gprs", "egrps");
116
117 /* enter the'confiugration' mode of the VTY */
118 function f_vty_enter_config(TELNETasp_PT pt) {
119 f_vty_transceive(pt, "configure terminal")
120 }
121
122 function f_vty_enter_cfg_network(TELNETasp_PT pt) {
123 f_vty_enter_config(pt);
124 f_vty_transceive(pt, "network")
125 }
126
127 function f_vty_enter_cfg_bts(TELNETasp_PT pt, BtsNr bts := 0) {
128 f_vty_enter_cfg_network(pt);
129 f_vty_transceive(pt, "bts " & int2str(bts));
130 }
131
132 function f_vty_enter_cfg_trx(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0) {
133 f_vty_enter_cfg_bts(pt, bts);
134 f_vty_transceive(pt, "trx " & int2str(trx));
135 }
136
137 function f_vty_enter_cfg_ts(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0, BtsTimeslotNr ts) {
138 f_vty_enter_cfg_trx(pt, bts, trx);
139 f_vty_transceive(pt, "timeslot " & int2str(ts));
140 }
141
Philipp Maierd0e64b02019-03-13 14:15:23 +0100142 function f_vty_enter_cfg_msc(TELNETasp_PT pt, MscNr msc := 0) {
143 f_vty_enter_config(pt);
144 f_vty_transceive(pt, "msc " & int2str(msc));
145 }
146
Pau Espin Pedrolc675b612020-01-09 19:55:40 +0100147 function f_vty_enter_cfg_cs7_inst(TELNETasp_PT pt, Cs7Nr cs7_inst := 0) {
148 f_vty_enter_config(pt);
149 f_vty_transceive(pt, "cs7 instance " & int2str(cs7_inst));
150 }
151
Harald Weltef640a012018-04-14 17:49:21 +0200152type record of charstring rof_charstring;
Neels Hofmeyr1a1f8542020-11-25 23:39:46 +0000153function f_vty_config3(TELNETasp_PT pt, rof_charstring config_nodes, rof_charstring cmds)
Harald Welte872ce172018-02-16 22:10:33 +0100154{
155 /* enter config mode; enter node */
156 f_vty_enter_config(pt);
Harald Weltef640a012018-04-14 17:49:21 +0200157 for (var integer i := 0; i < sizeof(config_nodes); i := i+1) {
158 f_vty_transceive(pt, config_nodes[i]);
159 }
Neels Hofmeyr1a1f8542020-11-25 23:39:46 +0000160 /* execute commands */
161 for (var integer i := 0; i < sizeof(cmds); i := i+1) {
162 f_vty_transceive(pt, cmds[i]);
163 }
Harald Welte872ce172018-02-16 22:10:33 +0100164 /* leave config mode */
165 f_vty_transceive(pt, "end");
166}
167
Neels Hofmeyr1a1f8542020-11-25 23:39:46 +0000168function f_vty_config2(TELNETasp_PT pt, rof_charstring config_nodes, charstring cmd)
169{
170 f_vty_config3(pt, config_nodes, { cmd });
171}
Harald Welte872ce172018-02-16 22:10:33 +0100172
Harald Weltef640a012018-04-14 17:49:21 +0200173function f_vty_config(TELNETasp_PT pt, charstring config_node, charstring cmd)
174{
175 f_vty_config2(pt, {config_node}, cmd);
176}
177
Neels Hofmeyr2a5670b2020-11-25 23:39:57 +0000178function f_vty_cfg_bts(TELNETasp_PT pt, BtsNr bts := 0, rof_charstring cmds) {
179 f_vty_config3(pt, {"network", "bts " & int2str(bts)}, cmds);
180}
181
182function f_vty_cfg_msc(TELNETasp_PT pt, MscNr msc := 0, rof_charstring cmds) {
183 f_vty_config3(pt, {"msc " & int2str(msc)}, cmds);
184}
185
Alexander Couzens98aa59e2018-06-12 13:45:59 +0200186function f_vty_transceive_match(TELNETasp_PT pt, charstring cmd, template charstring exp_ret) {
187 var charstring ret := f_vty_transceive_ret(pt, cmd);
188 if (not match(ret, exp_ret)) {
189 setverdict(fail, "Non-matching VTY response: ", ret);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200190 mtc.stop;
Alexander Couzens98aa59e2018-06-12 13:45:59 +0200191 }
192}
193
Alexander Couzens1e6d9902018-06-12 13:48:26 +0200194function f_vty_transceive_not_match(TELNETasp_PT pt, charstring cmd, template charstring exp_ret) {
195 var charstring ret := f_vty_transceive_ret(pt, cmd);
196 if (match(ret, exp_ret)) {
197 setverdict(fail, "Unexpected matching VTY response: ", ret);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200198 mtc.stop;
Alexander Couzens1e6d9902018-06-12 13:48:26 +0200199 }
200}
201
Stefan Sperlingcff13562018-11-13 15:24:06 +0100202function f_vty_transceive_match_regex(TELNETasp_PT pt, charstring cmd, charstring regex, integer groupno) return charstring
203{
204 var charstring resp := f_vty_transceive_ret(pt, cmd);
205 return regexp(resp, regex, groupno);
206}
207
208function f_vty_transceive_match_regexp_retry(TELNETasp_PT pt, charstring cmd, charstring regex,
209 integer groupno, integer num_attempts, float retry_delay) return charstring
210{
211 while (num_attempts > 0) {
212 var charstring ret := f_vty_transceive_match_regex(pt, cmd, regex, groupno);
213 if (ret != "") {
214 return ret;
215 }
216 f_sleep(retry_delay);
217 num_attempts := num_attempts - 1;
218 }
219
220 setverdict(fail, "No matching VTY response for regular expression '", regex,
221 "' after ", num_attempts, " attempts." );
222 mtc.stop;
223}
Harald Weltef640a012018-04-14 17:49:21 +0200224
Harald Welte8542cef2017-07-19 20:06:26 +0200225}