Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 1 | module Osmocom_VTY_Functions { |
| 2 | import from TELNETasp_PortType all; |
Stefan Sperling | cff1356 | 2018-11-13 15:24:06 +0100 | [diff] [blame] | 3 | import from Osmocom_Types all; |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 4 | |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 5 | modulepar { |
| 6 | charstring mp_prompt_prefix := "OpenBSC"; |
| 7 | } |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 8 | |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 9 | const charstring VTY_VIEW_SUFFIX := "> "; |
| 10 | const charstring VTY_ENABLE_SUFFIX := "# "; |
| 11 | const charstring VTY_CFG_SUFFIX := "(*)"; |
| 12 | |
| 13 | template charstring t_vty_unknown := pattern "*% Unknown command."; |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 14 | |
| 15 | /* configure prompts in TELNETasp module */ |
Harald Welte | a266325 | 2018-09-10 10:21:44 +0200 | [diff] [blame] | 16 | 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] | 17 | var ASP_TelnetDynamicConfig vty_prompt[3] := { |
| 18 | { |
| 19 | prompt := { |
| 20 | id := 1, |
Harald Welte | a266325 | 2018-09-10 10:21:44 +0200 | [diff] [blame] | 21 | prompt := prompt_prefix & VTY_VIEW_SUFFIX, |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 22 | has_wildcards := false |
| 23 | } |
| 24 | }, { |
| 25 | prompt := { |
| 26 | id := 2, |
Harald Welte | a266325 | 2018-09-10 10:21:44 +0200 | [diff] [blame] | 27 | prompt := prompt_prefix & VTY_ENABLE_SUFFIX, |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 28 | has_wildcards := false |
| 29 | } |
| 30 | }, { |
| 31 | prompt := { |
| 32 | id := 3, |
Harald Welte | a266325 | 2018-09-10 10:21:44 +0200 | [diff] [blame] | 33 | prompt := prompt_prefix & VTY_CFG_SUFFIX, |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 34 | has_wildcards := true |
| 35 | } |
| 36 | } |
| 37 | }; |
| 38 | |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 39 | /* set some configuration that isn't possible to express |
| 40 | * in the config file due to syntactic restrictions (Who invents config |
| 41 | * files that don't permit regular expressions? */ |
| 42 | for (var integer i := 0; i < sizeof(vty_prompt); i:= i + 1) { |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 43 | pt.send(vty_prompt[i]); |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
| 47 | /* wait for any of the permitted prompts; buffer + return all intermediate output */ |
| 48 | function f_vty_wait_for_prompt(TELNETasp_PT pt) return charstring { |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 49 | var charstring rx, buf := ""; |
Stefan Sperling | 23b4597 | 2018-07-27 17:20:38 +0200 | [diff] [blame] | 50 | var integer fd; |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 51 | timer T := 2.0; |
| 52 | |
| 53 | T.start; |
| 54 | alt { |
Harald Welte | a266325 | 2018-09-10 10:21:44 +0200 | [diff] [blame] | 55 | [] pt.receive(pattern "\w+" & VTY_VIEW_SUFFIX) { }; |
| 56 | [] pt.receive(pattern "\w+\# ") { }; |
Vadim Yanitskiy | ea247d5 | 2018-10-28 22:47:24 +0700 | [diff] [blame] | 57 | [] pt.receive(pattern "\w+\(*\)\# ") { }; |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 58 | [] pt.receive(t_vty_unknown) { |
| 59 | testcase.stop(fail, "VTY: Unknown Command"); |
| 60 | }; |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 61 | [] pt.receive(charstring:?) -> value rx { buf := buf & rx; repeat }; |
Stefan Sperling | 23b4597 | 2018-07-27 17:20:38 +0200 | [diff] [blame] | 62 | [] pt.receive(integer:?) -> value fd { |
| 63 | if (fd == -1) { |
| 64 | setverdict(fail, "VTY Telnet Connection Failure"); |
| 65 | mtc.stop; |
| 66 | } else { |
| 67 | repeat; /* telnet connection succeeded */ |
| 68 | } |
| 69 | } |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 70 | [] T.timeout { |
| 71 | setverdict(fail, "VTY Timeout for prompt"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 72 | mtc.stop; |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 73 | }; |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 74 | } |
| 75 | T.stop; |
| 76 | return buf; |
| 77 | } |
| 78 | |
| 79 | /* send a VTY command and obtain response until prompt is received */ |
| 80 | function f_vty_transceive_ret(TELNETasp_PT pt, charstring tx) return charstring { |
| 81 | pt.send(tx); |
| 82 | return f_vty_wait_for_prompt(pt); |
| 83 | } |
| 84 | |
| 85 | /* send a VTY command and obtain response until prompt is received */ |
| 86 | function f_vty_transceive(TELNETasp_PT pt, charstring tx) { |
Harald Welte | 930d0a7 | 2018-03-22 22:08:40 +0100 | [diff] [blame] | 87 | var charstring unused := f_vty_transceive_ret(pt, tx); |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | type integer BtsNr (0..255); |
| 91 | type integer BtsTrxNr (0..255); |
| 92 | type integer BtsTimeslotNr (0..7); |
| 93 | |
| 94 | type charstring BtsGprsMode ("none", "gprs", "egrps"); |
| 95 | |
| 96 | /* enter the'confiugration' mode of the VTY */ |
| 97 | function f_vty_enter_config(TELNETasp_PT pt) { |
| 98 | f_vty_transceive(pt, "configure terminal") |
| 99 | } |
| 100 | |
| 101 | function f_vty_enter_cfg_network(TELNETasp_PT pt) { |
| 102 | f_vty_enter_config(pt); |
| 103 | f_vty_transceive(pt, "network") |
| 104 | } |
| 105 | |
| 106 | function f_vty_enter_cfg_bts(TELNETasp_PT pt, BtsNr bts := 0) { |
| 107 | f_vty_enter_cfg_network(pt); |
| 108 | f_vty_transceive(pt, "bts " & int2str(bts)); |
| 109 | } |
| 110 | |
| 111 | function f_vty_enter_cfg_trx(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0) { |
| 112 | f_vty_enter_cfg_bts(pt, bts); |
| 113 | f_vty_transceive(pt, "trx " & int2str(trx)); |
| 114 | } |
| 115 | |
| 116 | function f_vty_enter_cfg_ts(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0, BtsTimeslotNr ts) { |
| 117 | f_vty_enter_cfg_trx(pt, bts, trx); |
| 118 | f_vty_transceive(pt, "timeslot " & int2str(ts)); |
| 119 | } |
| 120 | |
Harald Welte | f640a01 | 2018-04-14 17:49:21 +0200 | [diff] [blame] | 121 | type record of charstring rof_charstring; |
| 122 | function f_vty_config2(TELNETasp_PT pt, rof_charstring config_nodes, charstring cmd) |
Harald Welte | 872ce17 | 2018-02-16 22:10:33 +0100 | [diff] [blame] | 123 | { |
| 124 | /* enter config mode; enter node */ |
| 125 | f_vty_enter_config(pt); |
Harald Welte | f640a01 | 2018-04-14 17:49:21 +0200 | [diff] [blame] | 126 | for (var integer i := 0; i < sizeof(config_nodes); i := i+1) { |
| 127 | f_vty_transceive(pt, config_nodes[i]); |
| 128 | } |
Harald Welte | 872ce17 | 2018-02-16 22:10:33 +0100 | [diff] [blame] | 129 | /* execute command */ |
| 130 | f_vty_transceive(pt, cmd); |
| 131 | /* leave config mode */ |
| 132 | f_vty_transceive(pt, "end"); |
| 133 | } |
| 134 | |
| 135 | |
Harald Welte | f640a01 | 2018-04-14 17:49:21 +0200 | [diff] [blame] | 136 | function f_vty_config(TELNETasp_PT pt, charstring config_node, charstring cmd) |
| 137 | { |
| 138 | f_vty_config2(pt, {config_node}, cmd); |
| 139 | } |
| 140 | |
Alexander Couzens | 98aa59e | 2018-06-12 13:45:59 +0200 | [diff] [blame] | 141 | function f_vty_transceive_match(TELNETasp_PT pt, charstring cmd, template charstring exp_ret) { |
| 142 | var charstring ret := f_vty_transceive_ret(pt, cmd); |
| 143 | if (not match(ret, exp_ret)) { |
| 144 | setverdict(fail, "Non-matching VTY response: ", ret); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 145 | mtc.stop; |
Alexander Couzens | 98aa59e | 2018-06-12 13:45:59 +0200 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Alexander Couzens | 1e6d990 | 2018-06-12 13:48:26 +0200 | [diff] [blame] | 149 | function f_vty_transceive_not_match(TELNETasp_PT pt, charstring cmd, template charstring exp_ret) { |
| 150 | var charstring ret := f_vty_transceive_ret(pt, cmd); |
| 151 | if (match(ret, exp_ret)) { |
| 152 | setverdict(fail, "Unexpected matching VTY response: ", ret); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 153 | mtc.stop; |
Alexander Couzens | 1e6d990 | 2018-06-12 13:48:26 +0200 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Stefan Sperling | cff1356 | 2018-11-13 15:24:06 +0100 | [diff] [blame] | 157 | function f_vty_transceive_match_regex(TELNETasp_PT pt, charstring cmd, charstring regex, integer groupno) return charstring |
| 158 | { |
| 159 | var charstring resp := f_vty_transceive_ret(pt, cmd); |
| 160 | return regexp(resp, regex, groupno); |
| 161 | } |
| 162 | |
| 163 | function f_vty_transceive_match_regexp_retry(TELNETasp_PT pt, charstring cmd, charstring regex, |
| 164 | integer groupno, integer num_attempts, float retry_delay) return charstring |
| 165 | { |
| 166 | while (num_attempts > 0) { |
| 167 | var charstring ret := f_vty_transceive_match_regex(pt, cmd, regex, groupno); |
| 168 | if (ret != "") { |
| 169 | return ret; |
| 170 | } |
| 171 | f_sleep(retry_delay); |
| 172 | num_attempts := num_attempts - 1; |
| 173 | } |
| 174 | |
| 175 | setverdict(fail, "No matching VTY response for regular expression '", regex, |
| 176 | "' after ", num_attempts, " attempts." ); |
| 177 | mtc.stop; |
| 178 | } |
Harald Welte | f640a01 | 2018-04-14 17:49:21 +0200 | [diff] [blame] | 179 | |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 180 | } |