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