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