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