2aad5c54328e7bfa3bb3076a8faebb87f539a2a0
[cascardo/ovs.git] / tests / test-jsonrpc.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #undef NDEBUG
19 #include "jsonrpc.h"
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "command-line.h"
26 #include "daemon.h"
27 #include "json.h"
28 #include "ovstest.h"
29 #include "poll-loop.h"
30 #include "stream-ssl.h"
31 #include "stream.h"
32 #include "timeval.h"
33 #include "util.h"
34 #include "openvswitch/vlog.h"
35
36 OVS_NO_RETURN static void usage(void);
37 static void parse_options(int argc, char *argv[]);
38 static struct command *get_all_commands(void);
39
40 static void
41 test_jsonrpc_main(int argc, char *argv[])
42 {
43     proctitle_init(argc, argv);
44     set_program_name(argv[0]);
45     service_start(&argc, &argv);
46     parse_options(argc, argv);
47     run_command(argc - optind, argv + optind, get_all_commands());
48 }
49
50 static void
51 parse_options(int argc, char *argv[])
52 {
53     enum {
54         OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1,
55         DAEMON_OPTION_ENUMS
56     };
57     static const struct option long_options[] = {
58         {"verbose", optional_argument, NULL, 'v'},
59         {"help", no_argument, NULL, 'h'},
60         DAEMON_LONG_OPTIONS,
61         {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
62         STREAM_SSL_LONG_OPTIONS,
63         {NULL, 0, NULL, 0},
64     };
65     char *short_options = long_options_to_short_options(long_options);
66
67     for (;;) {
68         int c = getopt_long(argc, argv, short_options, long_options, NULL);
69         if (c == -1) {
70             break;
71         }
72
73         switch (c) {
74         case 'h':
75             usage();
76
77         case 'v':
78             vlog_set_verbosity(optarg);
79             break;
80
81         DAEMON_OPTION_HANDLERS
82
83         STREAM_SSL_OPTION_HANDLERS
84
85         case OPT_BOOTSTRAP_CA_CERT:
86             stream_ssl_set_ca_cert_file(optarg, true);
87             break;
88
89         case '?':
90             exit(EXIT_FAILURE);
91
92         default:
93             abort();
94         }
95     }
96     free(short_options);
97 }
98
99 static void
100 usage(void)
101 {
102     printf("%s: JSON-RPC test utility\n"
103            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
104            "  listen LOCAL             listen for connections on LOCAL\n"
105            "  request REMOTE METHOD PARAMS   send request, print reply\n"
106            "  notify REMOTE METHOD PARAMS  send notification and exit\n",
107            program_name, program_name);
108     stream_usage("JSON-RPC", true, true, true);
109     daemon_usage();
110     vlog_usage();
111     printf("\nOther options:\n"
112            "  -h, --help                  display this help message\n");
113     exit(EXIT_SUCCESS);
114 }
115 \f
116 /* Command helper functions. */
117
118 static struct json *
119 parse_json(const char *s)
120 {
121     struct json *json = json_from_string(s);
122     if (json->type == JSON_STRING) {
123         ovs_fatal(0, "\"%s\": %s", s, json->u.string);
124     }
125     return json;
126 }
127
128 static void
129 print_and_free_json(struct json *json)
130 {
131     char *string = json_to_string(json, JSSF_SORT);
132     json_destroy(json);
133     puts(string);
134     free(string);
135 }
136 \f
137 /* Command implementations. */
138
139 static int
140 handle_rpc(struct jsonrpc *rpc, struct jsonrpc_msg *msg, bool *done)
141 {
142     if (msg->type == JSONRPC_REQUEST) {
143         struct jsonrpc_msg *reply = NULL;
144         if (!strcmp(msg->method, "echo")) {
145             reply = jsonrpc_create_reply(json_clone(msg->params), msg->id);
146         } else {
147             struct json *error = json_object_create();
148             json_object_put_string(error, "error", "unknown method");
149             reply = jsonrpc_create_error(error, msg->id);
150             ovs_error(0, "unknown request %s", msg->method);
151         }
152         jsonrpc_send(rpc, reply);
153         return 0;
154     } else if (msg->type == JSONRPC_NOTIFY) {
155         if (!strcmp(msg->method, "shutdown")) {
156             *done = true;
157             return 0;
158         } else {
159             ovs_error(0, "unknown notification %s", msg->method);
160             return ENOTTY;
161         }
162     } else {
163         ovs_error(0, "unsolicited JSON-RPC reply or error");
164         return EPROTO;
165     }
166 }
167
168 static void
169 do_listen(int argc OVS_UNUSED, char *argv[])
170 {
171     struct pstream *pstream;
172     struct jsonrpc **rpcs;
173     size_t n_rpcs, allocated_rpcs;
174     bool done;
175     int error;
176
177     error = jsonrpc_pstream_open(argv[1], &pstream, DSCP_DEFAULT);
178     if (error) {
179         ovs_fatal(error, "could not listen on \"%s\"", argv[1]);
180     }
181
182     daemonize();
183
184     rpcs = NULL;
185     n_rpcs = allocated_rpcs = 0;
186     done = false;
187     for (;;) {
188         struct stream *stream;
189         size_t i;
190
191         /* Accept new connections. */
192         error = pstream_accept(pstream, &stream);
193         if (!error) {
194             if (n_rpcs >= allocated_rpcs) {
195                 rpcs = x2nrealloc(rpcs, &allocated_rpcs, sizeof *rpcs);
196             }
197             rpcs[n_rpcs++] = jsonrpc_open(stream);
198         } else if (error != EAGAIN) {
199             ovs_fatal(error, "pstream_accept failed");
200         }
201
202         /* Service existing connections. */
203         for (i = 0; i < n_rpcs; ) {
204             struct jsonrpc *rpc = rpcs[i];
205             struct jsonrpc_msg *msg;
206
207             jsonrpc_run(rpc);
208             if (!jsonrpc_get_backlog(rpc)) {
209                 error = jsonrpc_recv(rpc, &msg);
210                 if (!error) {
211                     error = handle_rpc(rpc, msg, &done);
212                     jsonrpc_msg_destroy(msg);
213                 } else if (error == EAGAIN) {
214                     error = 0;
215                 }
216             }
217
218             if (!error) {
219                 error = jsonrpc_get_status(rpc);
220             }
221             if (error) {
222                 jsonrpc_close(rpc);
223                 ovs_error(error, "connection closed");
224                 memmove(&rpcs[i], &rpcs[i + 1],
225                         (n_rpcs - i - 1) * sizeof *rpcs);
226                 n_rpcs--;
227             } else {
228                 i++;
229             }
230         }
231
232         /* Wait for something to do. */
233         if (done && !n_rpcs) {
234             break;
235         }
236         pstream_wait(pstream);
237         for (i = 0; i < n_rpcs; i++) {
238             struct jsonrpc *rpc = rpcs[i];
239
240             jsonrpc_wait(rpc);
241             if (!jsonrpc_get_backlog(rpc)) {
242                 jsonrpc_recv_wait(rpc);
243             }
244         }
245         poll_block();
246     }
247     free(rpcs);
248     pstream_close(pstream);
249 }
250
251 static void
252 do_request(int argc OVS_UNUSED, char *argv[])
253 {
254     struct jsonrpc_msg *msg;
255     struct jsonrpc *rpc;
256     struct json *params;
257     struct stream *stream;
258     const char *method;
259     char *string;
260     int error;
261
262     method = argv[2];
263     params = parse_json(argv[3]);
264     msg = jsonrpc_create_request(method, params, NULL);
265     string = jsonrpc_msg_is_valid(msg);
266     if (string) {
267         ovs_fatal(0, "not a valid JSON-RPC request: %s", string);
268     }
269
270     error = stream_open_block(jsonrpc_stream_open(argv[1], &stream,
271                               DSCP_DEFAULT), &stream);
272     if (error) {
273         ovs_fatal(error, "could not open \"%s\"", argv[1]);
274     }
275     rpc = jsonrpc_open(stream);
276
277     error = jsonrpc_send(rpc, msg);
278     if (error) {
279         ovs_fatal(error, "could not send request");
280     }
281
282     error = jsonrpc_recv_block(rpc, &msg);
283     if (error) {
284         ovs_fatal(error, "error waiting for reply");
285     }
286     print_and_free_json(jsonrpc_msg_to_json(msg));
287
288     jsonrpc_close(rpc);
289 }
290
291 static void
292 do_notify(int argc OVS_UNUSED, char *argv[])
293 {
294     struct jsonrpc_msg *msg;
295     struct jsonrpc *rpc;
296     struct json *params;
297     struct stream *stream;
298     const char *method;
299     char *string;
300     int error;
301
302     method = argv[2];
303     params = parse_json(argv[3]);
304     msg = jsonrpc_create_notify(method, params);
305     string = jsonrpc_msg_is_valid(msg);
306     if (string) {
307         ovs_fatal(0, "not a JSON RPC-valid notification: %s", string);
308     }
309
310     error = stream_open_block(jsonrpc_stream_open(argv[1], &stream,
311                               DSCP_DEFAULT), &stream);
312     if (error) {
313         ovs_fatal(error, "could not open \"%s\"", argv[1]);
314     }
315     rpc = jsonrpc_open(stream);
316
317     error = jsonrpc_send_block(rpc, msg);
318     if (error) {
319         ovs_fatal(error, "could not send notification");
320     }
321     jsonrpc_close(rpc);
322 }
323
324 static void
325 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
326 {
327     usage();
328 }
329
330 static struct command all_commands[] = {
331     { "listen", NULL, 1, 1, do_listen },
332     { "request", NULL, 3, 3, do_request },
333     { "notify", NULL, 3, 3, do_notify },
334     { "help", NULL, 0, INT_MAX, do_help },
335     { NULL, NULL, 0, 0, NULL },
336 };
337
338 static struct command *
339 get_all_commands(void)
340 {
341     return all_commands;
342 }
343
344 OVSTEST_REGISTER("test-jsonrpc", test_jsonrpc_main);