Harald Welte | 34b5a95 | 2019-05-27 11:54:11 +0200 | [diff] [blame] | 1 | /* 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 Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 12 | module Osmocom_VTY_Functions { |
| 13 | import from TELNETasp_PortType all; |
Stefan Sperling | cff1356 | 2018-11-13 15:24:06 +0100 | [diff] [blame] | 14 | import from Osmocom_Types all; |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 15 | |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 16 | modulepar { |
| 17 | charstring mp_prompt_prefix := "OpenBSC"; |
Vadim Yanitskiy | bc1e66a | 2021-01-30 14:14:15 +0100 | [diff] [blame] | 18 | float mp_prompt_timeout := 2.0; |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 19 | } |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 20 | |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 21 | 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 Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 26 | |
| 27 | /* configure prompts in TELNETasp module */ |
Harald Welte | a266325 | 2018-09-10 10:21:44 +0200 | [diff] [blame] | 28 | function f_vty_set_prompts(TELNETasp_PT pt, charstring prompt_prefix := mp_prompt_prefix) { |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 29 | var ASP_TelnetDynamicConfig vty_prompt[3] := { |
| 30 | { |
| 31 | prompt := { |
| 32 | id := 1, |
Harald Welte | a266325 | 2018-09-10 10:21:44 +0200 | [diff] [blame] | 33 | prompt := prompt_prefix & VTY_VIEW_SUFFIX, |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 34 | has_wildcards := false |
| 35 | } |
| 36 | }, { |
| 37 | prompt := { |
| 38 | id := 2, |
Harald Welte | a266325 | 2018-09-10 10:21:44 +0200 | [diff] [blame] | 39 | prompt := prompt_prefix & VTY_ENABLE_SUFFIX, |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 40 | has_wildcards := false |
| 41 | } |
| 42 | }, { |
| 43 | prompt := { |
| 44 | id := 3, |
Harald Welte | a266325 | 2018-09-10 10:21:44 +0200 | [diff] [blame] | 45 | prompt := prompt_prefix & VTY_CFG_SUFFIX, |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 46 | has_wildcards := true |
| 47 | } |
| 48 | } |
| 49 | }; |
| 50 | |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 51 | /* 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 Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 55 | pt.send(vty_prompt[i]); |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | /* wait for any of the permitted prompts; buffer + return all intermediate output */ |
Vadim Yanitskiy | 26e30aa | 2021-01-30 14:06:37 +0100 | [diff] [blame] | 60 | function f_vty_wait_for_prompt(TELNETasp_PT pt, boolean strict := true, charstring log_label := "(?)") |
| 61 | return charstring { |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 62 | var charstring rx, buf := ""; |
Stefan Sperling | 23b4597 | 2018-07-27 17:20:38 +0200 | [diff] [blame] | 63 | var integer fd; |
Vadim Yanitskiy | bc1e66a | 2021-01-30 14:14:15 +0100 | [diff] [blame] | 64 | timer T; |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 65 | |
Vadim Yanitskiy | bc1e66a | 2021-01-30 14:14:15 +0100 | [diff] [blame] | 66 | T.start(mp_prompt_timeout); |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 67 | alt { |
Vadim Yanitskiy | 26e30aa | 2021-01-30 14:06:37 +0100 | [diff] [blame] | 68 | [] 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 Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 74 | mtc.stop; |
Vadim Yanitskiy | 26e30aa | 2021-01-30 14:06:37 +0100 | [diff] [blame] | 75 | } 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 Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 94 | } |
| 95 | T.stop; |
| 96 | return buf; |
| 97 | } |
| 98 | |
| 99 | /* send a VTY command and obtain response until prompt is received */ |
Neels Hofmeyr | 2fe5ad1 | 2020-08-09 21:50:25 +0000 | [diff] [blame] | 100 | function f_vty_transceive_ret(TELNETasp_PT pt, charstring tx, boolean strict := true) return charstring { |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 101 | pt.send(tx); |
Neels Hofmeyr | 9ebabc8 | 2020-11-25 22:56:13 +0000 | [diff] [blame] | 102 | return f_vty_wait_for_prompt(pt, strict, tx); |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* send a VTY command and obtain response until prompt is received */ |
Neels Hofmeyr | 2fe5ad1 | 2020-08-09 21:50:25 +0000 | [diff] [blame] | 106 | function f_vty_transceive(TELNETasp_PT pt, charstring tx, boolean strict := true) { |
| 107 | var charstring unused := f_vty_transceive_ret(pt, tx, strict); |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | type integer BtsNr (0..255); |
| 111 | type integer BtsTrxNr (0..255); |
| 112 | type integer BtsTimeslotNr (0..7); |
Philipp Maier | d0e64b0 | 2019-03-13 14:15:23 +0100 | [diff] [blame] | 113 | type integer MscNr (0..255); |
Pau Espin Pedrol | c675b61 | 2020-01-09 19:55:40 +0100 | [diff] [blame] | 114 | type integer Cs7Nr (0..255); |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 115 | |
| 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 Maier | d0e64b0 | 2019-03-13 14:15:23 +0100 | [diff] [blame] | 143 | 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 Pedrol | c675b61 | 2020-01-09 19:55:40 +0100 | [diff] [blame] | 148 | 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 Welte | f640a01 | 2018-04-14 17:49:21 +0200 | [diff] [blame] | 153 | type record of charstring rof_charstring; |
Neels Hofmeyr | 1a1f854 | 2020-11-25 23:39:46 +0000 | [diff] [blame] | 154 | function f_vty_config3(TELNETasp_PT pt, rof_charstring config_nodes, rof_charstring cmds) |
Harald Welte | 872ce17 | 2018-02-16 22:10:33 +0100 | [diff] [blame] | 155 | { |
| 156 | /* enter config mode; enter node */ |
| 157 | f_vty_enter_config(pt); |
Harald Welte | f640a01 | 2018-04-14 17:49:21 +0200 | [diff] [blame] | 158 | for (var integer i := 0; i < sizeof(config_nodes); i := i+1) { |
| 159 | f_vty_transceive(pt, config_nodes[i]); |
| 160 | } |
Neels Hofmeyr | 1a1f854 | 2020-11-25 23:39:46 +0000 | [diff] [blame] | 161 | /* execute commands */ |
| 162 | for (var integer i := 0; i < sizeof(cmds); i := i+1) { |
| 163 | f_vty_transceive(pt, cmds[i]); |
| 164 | } |
Harald Welte | 872ce17 | 2018-02-16 22:10:33 +0100 | [diff] [blame] | 165 | /* leave config mode */ |
| 166 | f_vty_transceive(pt, "end"); |
| 167 | } |
| 168 | |
Neels Hofmeyr | 1a1f854 | 2020-11-25 23:39:46 +0000 | [diff] [blame] | 169 | function f_vty_config2(TELNETasp_PT pt, rof_charstring config_nodes, charstring cmd) |
| 170 | { |
| 171 | f_vty_config3(pt, config_nodes, { cmd }); |
| 172 | } |
Harald Welte | 872ce17 | 2018-02-16 22:10:33 +0100 | [diff] [blame] | 173 | |
Harald Welte | f640a01 | 2018-04-14 17:49:21 +0200 | [diff] [blame] | 174 | function f_vty_config(TELNETasp_PT pt, charstring config_node, charstring cmd) |
| 175 | { |
| 176 | f_vty_config2(pt, {config_node}, cmd); |
| 177 | } |
| 178 | |
Neels Hofmeyr | 2a5670b | 2020-11-25 23:39:57 +0000 | [diff] [blame] | 179 | function 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 | |
| 183 | function 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 Couzens | 98aa59e | 2018-06-12 13:45:59 +0200 | [diff] [blame] | 187 | function 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 Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 191 | mtc.stop; |
Alexander Couzens | 98aa59e | 2018-06-12 13:45:59 +0200 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Alexander Couzens | 1e6d990 | 2018-06-12 13:48:26 +0200 | [diff] [blame] | 195 | function 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 Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 199 | mtc.stop; |
Alexander Couzens | 1e6d990 | 2018-06-12 13:48:26 +0200 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Stefan Sperling | cff1356 | 2018-11-13 15:24:06 +0100 | [diff] [blame] | 203 | function 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 | |
| 209 | function 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 Welte | f640a01 | 2018-04-14 17:49:21 +0200 | [diff] [blame] | 225 | |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 226 | } |