blob: 86f58f11c867ed3de63e4ddc10fdbf94e291c30f [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 */
Harald Weltea2663252018-09-10 10:21:44 +020015 function f_vty_set_prompts(TELNETasp_PT pt, charstring prompt_prefix := mp_prompt_prefix) {
Harald Welte553f6552018-01-24 18:50:53 +010016 var ASP_TelnetDynamicConfig vty_prompt[3] := {
17 {
18 prompt := {
19 id := 1,
Harald Weltea2663252018-09-10 10:21:44 +020020 prompt := prompt_prefix & VTY_VIEW_SUFFIX,
Harald Welte553f6552018-01-24 18:50:53 +010021 has_wildcards := false
22 }
23 }, {
24 prompt := {
25 id := 2,
Harald Weltea2663252018-09-10 10:21:44 +020026 prompt := prompt_prefix & VTY_ENABLE_SUFFIX,
Harald Welte553f6552018-01-24 18:50:53 +010027 has_wildcards := false
28 }
29 }, {
30 prompt := {
31 id := 3,
Harald Weltea2663252018-09-10 10:21:44 +020032 prompt := prompt_prefix & VTY_CFG_SUFFIX,
Harald Welte553f6552018-01-24 18:50:53 +010033 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 := "";
Stefan Sperling23b45972018-07-27 17:20:38 +020049 var integer fd;
Harald Welte8542cef2017-07-19 20:06:26 +020050 timer T := 2.0;
51
52 T.start;
53 alt {
Harald Weltea2663252018-09-10 10:21:44 +020054 [] pt.receive(pattern "\w+" & VTY_VIEW_SUFFIX) { };
55 [] pt.receive(pattern "\w+\# ") { };
Vadim Yanitskiyea247d52018-10-28 22:47:24 +070056 [] pt.receive(pattern "\w+\(*\)\# ") { };
Harald Welte553f6552018-01-24 18:50:53 +010057 [] pt.receive(t_vty_unknown) {
58 testcase.stop(fail, "VTY: Unknown Command");
59 };
Harald Welte8542cef2017-07-19 20:06:26 +020060 [] pt.receive(charstring:?) -> value rx { buf := buf & rx; repeat };
Stefan Sperling23b45972018-07-27 17:20:38 +020061 [] pt.receive(integer:?) -> value fd {
62 if (fd == -1) {
63 setverdict(fail, "VTY Telnet Connection Failure");
64 mtc.stop;
65 } else {
66 repeat; /* telnet connection succeeded */
67 }
68 }
Harald Welte553f6552018-01-24 18:50:53 +010069 [] T.timeout {
70 setverdict(fail, "VTY Timeout for prompt");
Daniel Willmanne4ff5372018-07-05 17:35:03 +020071 mtc.stop;
Harald Welte553f6552018-01-24 18:50:53 +010072 };
Harald Welte8542cef2017-07-19 20:06:26 +020073 }
74 T.stop;
75 return buf;
76 }
77
78 /* send a VTY command and obtain response until prompt is received */
79 function f_vty_transceive_ret(TELNETasp_PT pt, charstring tx) return charstring {
80 pt.send(tx);
81 return f_vty_wait_for_prompt(pt);
82 }
83
84 /* send a VTY command and obtain response until prompt is received */
85 function f_vty_transceive(TELNETasp_PT pt, charstring tx) {
Harald Welte930d0a72018-03-22 22:08:40 +010086 var charstring unused := f_vty_transceive_ret(pt, tx);
Harald Welte8542cef2017-07-19 20:06:26 +020087 }
88
89 type integer BtsNr (0..255);
90 type integer BtsTrxNr (0..255);
91 type integer BtsTimeslotNr (0..7);
92
93 type charstring BtsGprsMode ("none", "gprs", "egrps");
94
95 /* enter the'confiugration' mode of the VTY */
96 function f_vty_enter_config(TELNETasp_PT pt) {
97 f_vty_transceive(pt, "configure terminal")
98 }
99
100 function f_vty_enter_cfg_network(TELNETasp_PT pt) {
101 f_vty_enter_config(pt);
102 f_vty_transceive(pt, "network")
103 }
104
105 function f_vty_enter_cfg_bts(TELNETasp_PT pt, BtsNr bts := 0) {
106 f_vty_enter_cfg_network(pt);
107 f_vty_transceive(pt, "bts " & int2str(bts));
108 }
109
110 function f_vty_enter_cfg_trx(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0) {
111 f_vty_enter_cfg_bts(pt, bts);
112 f_vty_transceive(pt, "trx " & int2str(trx));
113 }
114
115 function f_vty_enter_cfg_ts(TELNETasp_PT pt, BtsNr bts := 0, BtsTrxNr trx := 0, BtsTimeslotNr ts) {
116 f_vty_enter_cfg_trx(pt, bts, trx);
117 f_vty_transceive(pt, "timeslot " & int2str(ts));
118 }
119
Harald Weltef640a012018-04-14 17:49:21 +0200120type record of charstring rof_charstring;
121function f_vty_config2(TELNETasp_PT pt, rof_charstring config_nodes, charstring cmd)
Harald Welte872ce172018-02-16 22:10:33 +0100122{
123 /* enter config mode; enter node */
124 f_vty_enter_config(pt);
Harald Weltef640a012018-04-14 17:49:21 +0200125 for (var integer i := 0; i < sizeof(config_nodes); i := i+1) {
126 f_vty_transceive(pt, config_nodes[i]);
127 }
Harald Welte872ce172018-02-16 22:10:33 +0100128 /* execute command */
129 f_vty_transceive(pt, cmd);
130 /* leave config mode */
131 f_vty_transceive(pt, "end");
132}
133
134
Harald Weltef640a012018-04-14 17:49:21 +0200135function f_vty_config(TELNETasp_PT pt, charstring config_node, charstring cmd)
136{
137 f_vty_config2(pt, {config_node}, cmd);
138}
139
Alexander Couzens98aa59e2018-06-12 13:45:59 +0200140function f_vty_transceive_match(TELNETasp_PT pt, charstring cmd, template charstring exp_ret) {
141 var charstring ret := f_vty_transceive_ret(pt, cmd);
142 if (not match(ret, exp_ret)) {
143 setverdict(fail, "Non-matching VTY response: ", ret);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200144 mtc.stop;
Alexander Couzens98aa59e2018-06-12 13:45:59 +0200145 }
146}
147
Alexander Couzens1e6d9902018-06-12 13:48:26 +0200148function f_vty_transceive_not_match(TELNETasp_PT pt, charstring cmd, template charstring exp_ret) {
149 var charstring ret := f_vty_transceive_ret(pt, cmd);
150 if (match(ret, exp_ret)) {
151 setverdict(fail, "Unexpected matching VTY response: ", ret);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200152 mtc.stop;
Alexander Couzens1e6d9902018-06-12 13:48:26 +0200153 }
154}
155
Harald Weltef640a012018-04-14 17:49:21 +0200156
Harald Welte8542cef2017-07-19 20:06:26 +0200157}