Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 1 | module Osmocom_VTY_Functions { |
| 2 | import from TELNETasp_PortType all; |
| 3 | |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 4 | modulepar { |
| 5 | charstring mp_prompt_prefix := "OpenBSC"; |
| 6 | } |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 7 | |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 8 | const charstring VTY_VIEW_SUFFIX := "> "; |
| 9 | const charstring VTY_ENABLE_SUFFIX := "# "; |
| 10 | const charstring VTY_CFG_SUFFIX := "(*)"; |
| 11 | |
| 12 | template charstring t_vty_unknown := pattern "*% Unknown command."; |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 13 | |
| 14 | /* configure prompts in TELNETasp module */ |
| 15 | function f_vty_set_prompts(TELNETasp_PT pt) { |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 16 | var ASP_TelnetDynamicConfig vty_prompt[3] := { |
| 17 | { |
| 18 | prompt := { |
| 19 | id := 1, |
| 20 | prompt := mp_prompt_prefix & VTY_VIEW_SUFFIX, |
| 21 | has_wildcards := false |
| 22 | } |
| 23 | }, { |
| 24 | prompt := { |
| 25 | id := 2, |
| 26 | prompt := mp_prompt_prefix & VTY_ENABLE_SUFFIX, |
| 27 | has_wildcards := false |
| 28 | } |
| 29 | }, { |
| 30 | prompt := { |
| 31 | id := 3, |
| 32 | prompt := mp_prompt_prefix & VTY_CFG_SUFFIX, |
| 33 | has_wildcards := true |
| 34 | } |
| 35 | } |
| 36 | }; |
| 37 | |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 38 | /* set some configuration that isn't possible to express |
| 39 | * in the config file due to syntactic restrictions (Who invents config |
| 40 | * files that don't permit regular expressions? */ |
| 41 | for (var integer i := 0; i < sizeof(vty_prompt); i:= i + 1) { |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 42 | pt.send(vty_prompt[i]); |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | /* wait for any of the permitted prompts; buffer + return all intermediate output */ |
| 47 | function f_vty_wait_for_prompt(TELNETasp_PT pt) return charstring { |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 48 | var charstring rx, buf := ""; |
| 49 | timer T := 2.0; |
| 50 | |
| 51 | T.start; |
| 52 | alt { |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 53 | [] pt.receive(mp_prompt_prefix & VTY_VIEW_SUFFIX) { }; |
| 54 | [] pt.receive(mp_prompt_prefix & VTY_ENABLE_SUFFIX) { }; |
| 55 | [] pt.receive(pattern mp_prompt_prefix & VTY_CFG_SUFFIX) { }; |
| 56 | [] pt.receive(t_vty_unknown) { |
| 57 | testcase.stop(fail, "VTY: Unknown Command"); |
| 58 | }; |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 59 | [] pt.receive(charstring:?) -> value rx { buf := buf & rx; repeat }; |
Harald Welte | 553f655 | 2018-01-24 18:50:53 +0100 | [diff] [blame] | 60 | [] T.timeout { |
| 61 | setverdict(fail, "VTY Timeout for prompt"); |
| 62 | self.stop; |
| 63 | }; |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 64 | } |
| 65 | T.stop; |
| 66 | return buf; |
| 67 | } |
| 68 | |
| 69 | /* send a VTY command and obtain response until prompt is received */ |
| 70 | function f_vty_transceive_ret(TELNETasp_PT pt, charstring tx) return charstring { |
| 71 | pt.send(tx); |
| 72 | return f_vty_wait_for_prompt(pt); |
| 73 | } |
| 74 | |
| 75 | /* send a VTY command and obtain response until prompt is received */ |
| 76 | function f_vty_transceive(TELNETasp_PT pt, charstring tx) { |
Harald Welte | 930d0a7 | 2018-03-22 22:08:40 +0100 | [diff] [blame] | 77 | var charstring unused := f_vty_transceive_ret(pt, tx); |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | type integer BtsNr (0..255); |
| 81 | type integer BtsTrxNr (0..255); |
| 82 | type integer BtsTimeslotNr (0..7); |
| 83 | |
| 84 | type charstring BtsGprsMode ("none", "gprs", "egrps"); |
| 85 | |
| 86 | /* enter the'confiugration' mode of the VTY */ |
| 87 | function f_vty_enter_config(TELNETasp_PT pt) { |
| 88 | f_vty_transceive(pt, "configure terminal") |
| 89 | } |
| 90 | |
| 91 | function f_vty_enter_cfg_network(TELNETasp_PT pt) { |
| 92 | f_vty_enter_config(pt); |
| 93 | f_vty_transceive(pt, "network") |
| 94 | } |
| 95 | |
| 96 | function f_vty_enter_cfg_bts(TELNETasp_PT pt, BtsNr bts := 0) { |
| 97 | f_vty_enter_cfg_network(pt); |
| 98 | f_vty_transceive(pt, "bts " & int2str(bts)); |
| 99 | } |
| 100 | |
| 101 | function f_vty_enter_cfg_trx(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0) { |
| 102 | f_vty_enter_cfg_bts(pt, bts); |
| 103 | f_vty_transceive(pt, "trx " & int2str(trx)); |
| 104 | } |
| 105 | |
| 106 | function f_vty_enter_cfg_ts(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0, BtsTimeslotNr ts) { |
| 107 | f_vty_enter_cfg_trx(pt, bts, trx); |
| 108 | f_vty_transceive(pt, "timeslot " & int2str(ts)); |
| 109 | } |
| 110 | |
Harald Welte | 872ce17 | 2018-02-16 22:10:33 +0100 | [diff] [blame] | 111 | function f_vty_config(TELNETasp_PT pt, charstring config_node, charstring cmd) |
| 112 | { |
| 113 | /* enter config mode; enter node */ |
| 114 | f_vty_enter_config(pt); |
| 115 | f_vty_transceive(pt, config_node); |
| 116 | /* execute command */ |
| 117 | f_vty_transceive(pt, cmd); |
| 118 | /* leave config mode */ |
| 119 | f_vty_transceive(pt, "end"); |
| 120 | } |
| 121 | |
| 122 | |
Harald Welte | 8542cef | 2017-07-19 20:06:26 +0200 | [diff] [blame] | 123 | } |