blob: 9258fa5877a68df88e8f61d54f64c436e47efe2a [file] [log] [blame]
Harald Welte8542cef2017-07-19 20:06:26 +02001module Osmocom_VTY_Functions {
2 import from TELNETasp_PortType all;
3
Harald Welte553f6552018-01-24 18:50:53 +01004 modulepar {
5 charstring mp_prompt_prefix := "OpenBSC";
6 }
Harald Welte8542cef2017-07-19 20:06:26 +02007
Harald Welte553f6552018-01-24 18:50:53 +01008 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 Welte8542cef2017-07-19 20:06:26 +020013
14 /* configure prompts in TELNETasp module */
15 function f_vty_set_prompts(TELNETasp_PT pt) {
Harald Welte553f6552018-01-24 18:50:53 +010016 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 Welte8542cef2017-07-19 20:06:26 +020038 /* 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 Welte553f6552018-01-24 18:50:53 +010042 pt.send(vty_prompt[i]);
Harald Welte8542cef2017-07-19 20:06:26 +020043 }
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 Welte8542cef2017-07-19 20:06:26 +020048 var charstring rx, buf := "";
49 timer T := 2.0;
50
51 T.start;
52 alt {
Harald Welte553f6552018-01-24 18:50:53 +010053 [] 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 Welte8542cef2017-07-19 20:06:26 +020059 [] pt.receive(charstring:?) -> value rx { buf := buf & rx; repeat };
Harald Welte553f6552018-01-24 18:50:53 +010060 [] T.timeout {
61 setverdict(fail, "VTY Timeout for prompt");
62 self.stop;
63 };
Harald Welte8542cef2017-07-19 20:06:26 +020064 }
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 Welte930d0a72018-03-22 22:08:40 +010077 var charstring unused := f_vty_transceive_ret(pt, tx);
Harald Welte8542cef2017-07-19 20:06:26 +020078 }
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 Welte872ce172018-02-16 22:10:33 +0100111function 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 Welte8542cef2017-07-19 20:06:26 +0200123}