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"; |
| 18 | } |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 19 | |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 20 | 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 Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 25 | |
| 26 | /* configure prompts in TELNETasp module */ |
Harald Welte | a266325 | 2018-09-10 10:21:44 +0200 | [diff] [blame] | 27 | 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] | 28 | var ASP_TelnetDynamicConfig vty_prompt[3] := { |
| 29 | { |
| 30 | prompt := { |
| 31 | id := 1, |
Harald Welte | a266325 | 2018-09-10 10:21:44 +0200 | [diff] [blame] | 32 | prompt := prompt_prefix & VTY_VIEW_SUFFIX, |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 33 | has_wildcards := false |
| 34 | } |
| 35 | }, { |
| 36 | prompt := { |
| 37 | id := 2, |
Harald Welte | a266325 | 2018-09-10 10:21:44 +0200 | [diff] [blame] | 38 | prompt := prompt_prefix & VTY_ENABLE_SUFFIX, |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 39 | has_wildcards := false |
| 40 | } |
| 41 | }, { |
| 42 | prompt := { |
| 43 | id := 3, |
Harald Welte | a266325 | 2018-09-10 10:21:44 +0200 | [diff] [blame] | 44 | prompt := prompt_prefix & VTY_CFG_SUFFIX, |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 45 | has_wildcards := true |
| 46 | } |
| 47 | } |
| 48 | }; |
| 49 | |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 50 | /* 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 Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 54 | pt.send(vty_prompt[i]); |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | /* wait for any of the permitted prompts; buffer + return all intermediate output */ |
Neels Hofmeyr | 9ebabc8 | 2020-11-25 22:56:13 +0000 | [diff] [blame] | 59 | function f_vty_wait_for_prompt(TELNETasp_PT pt, boolean strict := true, charstring log_label := "(?)") return charstring { |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 60 | var charstring rx, buf := ""; |
Stefan Sperling | 23b4597 | 2018-07-27 17:20:38 +0200 | [diff] [blame] | 61 | var integer fd; |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 62 | timer T := 2.0; |
| 63 | |
| 64 | T.start; |
| 65 | alt { |
Pau Espin Pedrol | 1a4d4e9 | 2019-12-04 16:59:29 +0100 | [diff] [blame] | 66 | [] pt.receive(pattern "[\w-]+" & VTY_VIEW_SUFFIX) { }; |
| 67 | [] pt.receive(pattern "[\w-]+\# ") { }; |
| 68 | [] pt.receive(pattern "[\w-]+\(*\)\# ") { }; |
Neels Hofmeyr | 5c59a8f | 2020-08-12 00:55:50 +0000 | [diff] [blame] | 69 | [] pt.receive(t_vty_unknown) -> value rx { |
Neels Hofmeyr | 2fe5ad1 | 2020-08-09 21:50:25 +0000 | [diff] [blame] | 70 | if (strict) { |
Neels Hofmeyr | 9ebabc8 | 2020-11-25 22:56:13 +0000 | [diff] [blame] | 71 | setverdict(fail, "VTY: Unknown Command: " & log_label); |
Neels Hofmeyr | 345a15f | 2020-08-12 00:56:56 +0000 | [diff] [blame] | 72 | mtc.stop; |
Neels Hofmeyr | 2fe5ad1 | 2020-08-09 21:50:25 +0000 | [diff] [blame] | 73 | } else { |
Neels Hofmeyr | 9ebabc8 | 2020-11-25 22:56:13 +0000 | [diff] [blame] | 74 | log("VTY: Unknown Command (ignored): " & log_label); |
Neels Hofmeyr | e4b0a66 | 2020-08-12 00:55:50 +0000 | [diff] [blame] | 75 | buf := buf & rx; |
| 76 | repeat; |
Neels Hofmeyr | 2fe5ad1 | 2020-08-09 21:50:25 +0000 | [diff] [blame] | 77 | } |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 78 | }; |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 79 | [] pt.receive(charstring:?) -> value rx { buf := buf & rx; repeat }; |
Stefan Sperling | 23b4597 | 2018-07-27 17:20:38 +0200 | [diff] [blame] | 80 | [] pt.receive(integer:?) -> value fd { |
| 81 | if (fd == -1) { |
Neels Hofmeyr | 9ebabc8 | 2020-11-25 22:56:13 +0000 | [diff] [blame] | 82 | setverdict(fail, "VTY Telnet Connection Failure: " & log_label); |
Stefan Sperling | 23b4597 | 2018-07-27 17:20:38 +0200 | [diff] [blame] | 83 | mtc.stop; |
| 84 | } else { |
| 85 | repeat; /* telnet connection succeeded */ |
| 86 | } |
| 87 | } |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 88 | [] T.timeout { |
Neels Hofmeyr | 9ebabc8 | 2020-11-25 22:56:13 +0000 | [diff] [blame] | 89 | setverdict(fail, "VTY Timeout for prompt: " & log_label); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 90 | mtc.stop; |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 91 | }; |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 92 | } |
| 93 | T.stop; |
| 94 | return buf; |
| 95 | } |
| 96 | |
| 97 | /* send a VTY command and obtain response until prompt is received */ |
Neels Hofmeyr | 2fe5ad1 | 2020-08-09 21:50:25 +0000 | [diff] [blame] | 98 | 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] | 99 | pt.send(tx); |
Neels Hofmeyr | 9ebabc8 | 2020-11-25 22:56:13 +0000 | [diff] [blame] | 100 | return f_vty_wait_for_prompt(pt, strict, tx); |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* send a VTY command and obtain response until prompt is received */ |
Neels Hofmeyr | 2fe5ad1 | 2020-08-09 21:50:25 +0000 | [diff] [blame] | 104 | function f_vty_transceive(TELNETasp_PT pt, charstring tx, boolean strict := true) { |
| 105 | var charstring unused := f_vty_transceive_ret(pt, tx, strict); |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | type integer BtsNr (0..255); |
| 109 | type integer BtsTrxNr (0..255); |
| 110 | type integer BtsTimeslotNr (0..7); |
Philipp Maier | d0e64b0 | 2019-03-13 14:15:23 +0100 | [diff] [blame] | 111 | type integer MscNr (0..255); |
Pau Espin Pedrol | c675b61 | 2020-01-09 19:55:40 +0100 | [diff] [blame] | 112 | type integer Cs7Nr (0..255); |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 113 | |
| 114 | type charstring BtsGprsMode ("none", "gprs", "egrps"); |
| 115 | |
| 116 | /* enter the'confiugration' mode of the VTY */ |
| 117 | function f_vty_enter_config(TELNETasp_PT pt) { |
| 118 | f_vty_transceive(pt, "configure terminal") |
| 119 | } |
| 120 | |
| 121 | function f_vty_enter_cfg_network(TELNETasp_PT pt) { |
| 122 | f_vty_enter_config(pt); |
| 123 | f_vty_transceive(pt, "network") |
| 124 | } |
| 125 | |
| 126 | function f_vty_enter_cfg_bts(TELNETasp_PT pt, BtsNr bts := 0) { |
| 127 | f_vty_enter_cfg_network(pt); |
| 128 | f_vty_transceive(pt, "bts " & int2str(bts)); |
| 129 | } |
| 130 | |
| 131 | function f_vty_enter_cfg_trx(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0) { |
| 132 | f_vty_enter_cfg_bts(pt, bts); |
| 133 | f_vty_transceive(pt, "trx " & int2str(trx)); |
| 134 | } |
| 135 | |
| 136 | function f_vty_enter_cfg_ts(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0, BtsTimeslotNr ts) { |
| 137 | f_vty_enter_cfg_trx(pt, bts, trx); |
| 138 | f_vty_transceive(pt, "timeslot " & int2str(ts)); |
| 139 | } |
| 140 | |
Philipp Maier | d0e64b0 | 2019-03-13 14:15:23 +0100 | [diff] [blame] | 141 | function f_vty_enter_cfg_msc(TELNETasp_PT pt, MscNr msc := 0) { |
| 142 | f_vty_enter_config(pt); |
| 143 | f_vty_transceive(pt, "msc " & int2str(msc)); |
| 144 | } |
| 145 | |
Pau Espin Pedrol | c675b61 | 2020-01-09 19:55:40 +0100 | [diff] [blame] | 146 | function f_vty_enter_cfg_cs7_inst(TELNETasp_PT pt, Cs7Nr cs7_inst := 0) { |
| 147 | f_vty_enter_config(pt); |
| 148 | f_vty_transceive(pt, "cs7 instance " & int2str(cs7_inst)); |
| 149 | } |
| 150 | |
Harald Welte | f640a01 | 2018-04-14 17:49:21 +0200 | [diff] [blame] | 151 | type record of charstring rof_charstring; |
Neels Hofmeyr | 1a1f854 | 2020-11-25 23:39:46 +0000 | [diff] [blame] | 152 | 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] | 153 | { |
| 154 | /* enter config mode; enter node */ |
| 155 | f_vty_enter_config(pt); |
Harald Welte | f640a01 | 2018-04-14 17:49:21 +0200 | [diff] [blame] | 156 | for (var integer i := 0; i < sizeof(config_nodes); i := i+1) { |
| 157 | f_vty_transceive(pt, config_nodes[i]); |
| 158 | } |
Neels Hofmeyr | 1a1f854 | 2020-11-25 23:39:46 +0000 | [diff] [blame] | 159 | /* execute commands */ |
| 160 | for (var integer i := 0; i < sizeof(cmds); i := i+1) { |
| 161 | f_vty_transceive(pt, cmds[i]); |
| 162 | } |
Harald Welte | 872ce17 | 2018-02-16 22:10:33 +0100 | [diff] [blame] | 163 | /* leave config mode */ |
| 164 | f_vty_transceive(pt, "end"); |
| 165 | } |
| 166 | |
Neels Hofmeyr | 1a1f854 | 2020-11-25 23:39:46 +0000 | [diff] [blame] | 167 | function f_vty_config2(TELNETasp_PT pt, rof_charstring config_nodes, charstring cmd) |
| 168 | { |
| 169 | f_vty_config3(pt, config_nodes, { cmd }); |
| 170 | } |
Harald Welte | 872ce17 | 2018-02-16 22:10:33 +0100 | [diff] [blame] | 171 | |
Harald Welte | f640a01 | 2018-04-14 17:49:21 +0200 | [diff] [blame] | 172 | function f_vty_config(TELNETasp_PT pt, charstring config_node, charstring cmd) |
| 173 | { |
| 174 | f_vty_config2(pt, {config_node}, cmd); |
| 175 | } |
| 176 | |
Neels Hofmeyr | 2a5670b | 2020-11-25 23:39:57 +0000 | [diff] [blame] | 177 | function f_vty_cfg_bts(TELNETasp_PT pt, BtsNr bts := 0, rof_charstring cmds) { |
| 178 | f_vty_config3(pt, {"network", "bts " & int2str(bts)}, cmds); |
| 179 | } |
| 180 | |
| 181 | function f_vty_cfg_msc(TELNETasp_PT pt, MscNr msc := 0, rof_charstring cmds) { |
| 182 | f_vty_config3(pt, {"msc " & int2str(msc)}, cmds); |
| 183 | } |
| 184 | |
Alexander Couzens | 98aa59e | 2018-06-12 13:45:59 +0200 | [diff] [blame] | 185 | function f_vty_transceive_match(TELNETasp_PT pt, charstring cmd, template charstring exp_ret) { |
| 186 | var charstring ret := f_vty_transceive_ret(pt, cmd); |
| 187 | if (not match(ret, exp_ret)) { |
| 188 | setverdict(fail, "Non-matching VTY response: ", ret); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 189 | mtc.stop; |
Alexander Couzens | 98aa59e | 2018-06-12 13:45:59 +0200 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Alexander Couzens | 1e6d990 | 2018-06-12 13:48:26 +0200 | [diff] [blame] | 193 | function f_vty_transceive_not_match(TELNETasp_PT pt, charstring cmd, template charstring exp_ret) { |
| 194 | var charstring ret := f_vty_transceive_ret(pt, cmd); |
| 195 | if (match(ret, exp_ret)) { |
| 196 | setverdict(fail, "Unexpected matching VTY response: ", ret); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 197 | mtc.stop; |
Alexander Couzens | 1e6d990 | 2018-06-12 13:48:26 +0200 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Stefan Sperling | cff1356 | 2018-11-13 15:24:06 +0100 | [diff] [blame] | 201 | function f_vty_transceive_match_regex(TELNETasp_PT pt, charstring cmd, charstring regex, integer groupno) return charstring |
| 202 | { |
| 203 | var charstring resp := f_vty_transceive_ret(pt, cmd); |
| 204 | return regexp(resp, regex, groupno); |
| 205 | } |
| 206 | |
| 207 | function f_vty_transceive_match_regexp_retry(TELNETasp_PT pt, charstring cmd, charstring regex, |
| 208 | integer groupno, integer num_attempts, float retry_delay) return charstring |
| 209 | { |
| 210 | while (num_attempts > 0) { |
| 211 | var charstring ret := f_vty_transceive_match_regex(pt, cmd, regex, groupno); |
| 212 | if (ret != "") { |
| 213 | return ret; |
| 214 | } |
| 215 | f_sleep(retry_delay); |
| 216 | num_attempts := num_attempts - 1; |
| 217 | } |
| 218 | |
| 219 | setverdict(fail, "No matching VTY response for regular expression '", regex, |
| 220 | "' after ", num_attempts, " attempts." ); |
| 221 | mtc.stop; |
| 222 | } |
Harald Welte | f640a01 | 2018-04-14 17:49:21 +0200 | [diff] [blame] | 223 | |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 224 | } |