blob: 9410c9ee90ddca92388b63c690b7c9125c6fde86 [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";
Vadim Yanitskiybc1e66a2021-01-30 14:14:15 +010018 float mp_prompt_timeout := 2.0;
Harald Welte553f6552018-01-24 18:50:53 +010019 }
Harald Welte8542cef2017-07-19 20:06:26 +020020
Harald Welte553f6552018-01-24 18:50:53 +010021 const charstring VTY_VIEW_SUFFIX := "> ";
22 const charstring VTY_ENABLE_SUFFIX := "# ";
23 const charstring VTY_CFG_SUFFIX := "(*)";
24
25 template charstring t_vty_unknown := pattern "*% Unknown command.";
Harald Welte8542cef2017-07-19 20:06:26 +020026
27 /* configure prompts in TELNETasp module */
Harald Weltea2663252018-09-10 10:21:44 +020028 function f_vty_set_prompts(TELNETasp_PT pt, charstring prompt_prefix := mp_prompt_prefix) {
Harald Welte553f6552018-01-24 18:50:53 +010029 var ASP_TelnetDynamicConfig vty_prompt[3] := {
30 {
31 prompt := {
32 id := 1,
Harald Weltea2663252018-09-10 10:21:44 +020033 prompt := prompt_prefix & VTY_VIEW_SUFFIX,
Harald Welte553f6552018-01-24 18:50:53 +010034 has_wildcards := false
35 }
36 }, {
37 prompt := {
38 id := 2,
Harald Weltea2663252018-09-10 10:21:44 +020039 prompt := prompt_prefix & VTY_ENABLE_SUFFIX,
Harald Welte553f6552018-01-24 18:50:53 +010040 has_wildcards := false
41 }
42 }, {
43 prompt := {
44 id := 3,
Harald Weltea2663252018-09-10 10:21:44 +020045 prompt := prompt_prefix & VTY_CFG_SUFFIX,
Harald Welte553f6552018-01-24 18:50:53 +010046 has_wildcards := true
47 }
48 }
49 };
50
Harald Welte8542cef2017-07-19 20:06:26 +020051 /* set some configuration that isn't possible to express
52 * in the config file due to syntactic restrictions (Who invents config
53 * files that don't permit regular expressions? */
54 for (var integer i := 0; i < sizeof(vty_prompt); i:= i + 1) {
Harald Welte553f6552018-01-24 18:50:53 +010055 pt.send(vty_prompt[i]);
Harald Welte8542cef2017-07-19 20:06:26 +020056 }
57 }
58
59 /* wait for any of the permitted prompts; buffer + return all intermediate output */
Vadim Yanitskiy26e30aa2021-01-30 14:06:37 +010060 function f_vty_wait_for_prompt(TELNETasp_PT pt, boolean strict := true, charstring log_label := "(?)")
61 return charstring {
Harald Welte8542cef2017-07-19 20:06:26 +020062 var charstring rx, buf := "";
Stefan Sperling23b45972018-07-27 17:20:38 +020063 var integer fd;
Vadim Yanitskiybc1e66a2021-01-30 14:14:15 +010064 timer T;
Harald Welte8542cef2017-07-19 20:06:26 +020065
Vadim Yanitskiybc1e66a2021-01-30 14:14:15 +010066 T.start(mp_prompt_timeout);
Harald Welte8542cef2017-07-19 20:06:26 +020067 alt {
Vadim Yanitskiy26e30aa2021-01-30 14:06:37 +010068 [] pt.receive(pattern "[\w-]+" & VTY_VIEW_SUFFIX) { };
69 [] pt.receive(pattern "[\w-]+\# ") { };
70 [] pt.receive(pattern "[\w-]+\(*\)\# ") { };
71 [] pt.receive(t_vty_unknown) -> value rx {
72 if (strict) {
73 setverdict(fail, "VTY: Unknown Command: " & log_label);
Daniel Willmanne4ff5372018-07-05 17:35:03 +020074 mtc.stop;
Vadim Yanitskiy26e30aa2021-01-30 14:06:37 +010075 } else {
76 log("VTY: Unknown Command (ignored): " & log_label);
77 buf := buf & rx;
78 repeat;
79 }
80 };
81 [] pt.receive(charstring:?) -> value rx { buf := buf & rx; repeat };
82 [] pt.receive(integer:?) -> value fd {
83 if (fd == -1) {
84 setverdict(fail, "VTY Telnet Connection Failure: " & log_label);
85 mtc.stop;
86 } else {
87 repeat; /* telnet connection succeeded */
88 }
89 }
90 [] T.timeout {
91 setverdict(fail, "VTY Timeout for prompt: " & log_label);
92 mtc.stop;
93 };
Harald Welte8542cef2017-07-19 20:06:26 +020094 }
95 T.stop;
96 return buf;
97 }
98
99 /* send a VTY command and obtain response until prompt is received */
Neels Hofmeyr2fe5ad12020-08-09 21:50:25 +0000100 function f_vty_transceive_ret(TELNETasp_PT pt, charstring tx, boolean strict := true) return charstring {
Harald Welte8542cef2017-07-19 20:06:26 +0200101 pt.send(tx);
Neels Hofmeyr9ebabc82020-11-25 22:56:13 +0000102 return f_vty_wait_for_prompt(pt, strict, tx);
Harald Welte8542cef2017-07-19 20:06:26 +0200103 }
104
105 /* send a VTY command and obtain response until prompt is received */
Neels Hofmeyr2fe5ad12020-08-09 21:50:25 +0000106 function f_vty_transceive(TELNETasp_PT pt, charstring tx, boolean strict := true) {
107 var charstring unused := f_vty_transceive_ret(pt, tx, strict);
Harald Welte8542cef2017-07-19 20:06:26 +0200108 }
109
110 type integer BtsNr (0..255);
111 type integer BtsTrxNr (0..255);
112 type integer BtsTimeslotNr (0..7);
Philipp Maierd0e64b02019-03-13 14:15:23 +0100113 type integer MscNr (0..255);
Pau Espin Pedrolc675b612020-01-09 19:55:40 +0100114 type integer Cs7Nr (0..255);
Harald Welte8542cef2017-07-19 20:06:26 +0200115
116 type charstring BtsGprsMode ("none", "gprs", "egrps");
117
118 /* enter the'confiugration' mode of the VTY */
119 function f_vty_enter_config(TELNETasp_PT pt) {
120 f_vty_transceive(pt, "configure terminal")
121 }
122
123 function f_vty_enter_cfg_network(TELNETasp_PT pt) {
124 f_vty_enter_config(pt);
125 f_vty_transceive(pt, "network")
126 }
127
128 function f_vty_enter_cfg_bts(TELNETasp_PT pt, BtsNr bts := 0) {
129 f_vty_enter_cfg_network(pt);
130 f_vty_transceive(pt, "bts " & int2str(bts));
131 }
132
133 function f_vty_enter_cfg_trx(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0) {
134 f_vty_enter_cfg_bts(pt, bts);
135 f_vty_transceive(pt, "trx " & int2str(trx));
136 }
137
138 function f_vty_enter_cfg_ts(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0, BtsTimeslotNr ts) {
139 f_vty_enter_cfg_trx(pt, bts, trx);
140 f_vty_transceive(pt, "timeslot " & int2str(ts));
141 }
142
Philipp Maierd0e64b02019-03-13 14:15:23 +0100143 function f_vty_enter_cfg_msc(TELNETasp_PT pt, MscNr msc := 0) {
144 f_vty_enter_config(pt);
145 f_vty_transceive(pt, "msc " & int2str(msc));
146 }
147
Pau Espin Pedrolc675b612020-01-09 19:55:40 +0100148 function f_vty_enter_cfg_cs7_inst(TELNETasp_PT pt, Cs7Nr cs7_inst := 0) {
149 f_vty_enter_config(pt);
150 f_vty_transceive(pt, "cs7 instance " & int2str(cs7_inst));
151 }
152
Harald Weltef640a012018-04-14 17:49:21 +0200153type record of charstring rof_charstring;
Neels Hofmeyr1a1f8542020-11-25 23:39:46 +0000154function f_vty_config3(TELNETasp_PT pt, rof_charstring config_nodes, rof_charstring cmds)
Harald Welte872ce172018-02-16 22:10:33 +0100155{
156 /* enter config mode; enter node */
157 f_vty_enter_config(pt);
Harald Weltef640a012018-04-14 17:49:21 +0200158 for (var integer i := 0; i < sizeof(config_nodes); i := i+1) {
159 f_vty_transceive(pt, config_nodes[i]);
160 }
Neels Hofmeyr1a1f8542020-11-25 23:39:46 +0000161 /* execute commands */
162 for (var integer i := 0; i < sizeof(cmds); i := i+1) {
163 f_vty_transceive(pt, cmds[i]);
164 }
Harald Welte872ce172018-02-16 22:10:33 +0100165 /* leave config mode */
166 f_vty_transceive(pt, "end");
167}
168
Neels Hofmeyr1a1f8542020-11-25 23:39:46 +0000169function f_vty_config2(TELNETasp_PT pt, rof_charstring config_nodes, charstring cmd)
170{
171 f_vty_config3(pt, config_nodes, { cmd });
172}
Harald Welte872ce172018-02-16 22:10:33 +0100173
Harald Weltef640a012018-04-14 17:49:21 +0200174function f_vty_config(TELNETasp_PT pt, charstring config_node, charstring cmd)
175{
176 f_vty_config2(pt, {config_node}, cmd);
177}
178
Neels Hofmeyr2a5670b2020-11-25 23:39:57 +0000179function f_vty_cfg_bts(TELNETasp_PT pt, BtsNr bts := 0, rof_charstring cmds) {
180 f_vty_config3(pt, {"network", "bts " & int2str(bts)}, cmds);
181}
182
183function f_vty_cfg_msc(TELNETasp_PT pt, MscNr msc := 0, rof_charstring cmds) {
184 f_vty_config3(pt, {"msc " & int2str(msc)}, cmds);
185}
186
Alexander Couzens98aa59e2018-06-12 13:45:59 +0200187function f_vty_transceive_match(TELNETasp_PT pt, charstring cmd, template charstring exp_ret) {
188 var charstring ret := f_vty_transceive_ret(pt, cmd);
189 if (not match(ret, exp_ret)) {
190 setverdict(fail, "Non-matching VTY response: ", ret);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200191 mtc.stop;
Alexander Couzens98aa59e2018-06-12 13:45:59 +0200192 }
193}
194
Alexander Couzens1e6d9902018-06-12 13:48:26 +0200195function f_vty_transceive_not_match(TELNETasp_PT pt, charstring cmd, template charstring exp_ret) {
196 var charstring ret := f_vty_transceive_ret(pt, cmd);
197 if (match(ret, exp_ret)) {
198 setverdict(fail, "Unexpected matching VTY response: ", ret);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200199 mtc.stop;
Alexander Couzens1e6d9902018-06-12 13:48:26 +0200200 }
201}
202
Stefan Sperlingcff13562018-11-13 15:24:06 +0100203function f_vty_transceive_match_regex(TELNETasp_PT pt, charstring cmd, charstring regex, integer groupno) return charstring
204{
205 var charstring resp := f_vty_transceive_ret(pt, cmd);
206 return regexp(resp, regex, groupno);
207}
208
209function f_vty_transceive_match_regexp_retry(TELNETasp_PT pt, charstring cmd, charstring regex,
210 integer groupno, integer num_attempts, float retry_delay) return charstring
211{
212 while (num_attempts > 0) {
213 var charstring ret := f_vty_transceive_match_regex(pt, cmd, regex, groupno);
214 if (ret != "") {
215 return ret;
216 }
217 f_sleep(retry_delay);
218 num_attempts := num_attempts - 1;
219 }
220
221 setverdict(fail, "No matching VTY response for regular expression '", regex,
222 "' after ", num_attempts, " attempts." );
223 mtc.stop;
224}
Harald Weltef640a012018-04-14 17:49:21 +0200225
Harald Welte8542cef2017-07-19 20:06:26 +0200226}