1 # Copyright (c) 2009, 2010, 2011 Nicira, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
27 def handle_rpc(rpc, msg):
31 if msg.type == ovs.jsonrpc.Message.T_REQUEST:
32 if msg.method == "echo":
33 reply = ovs.jsonrpc.Message.create_reply(msg.params, msg.id)
35 reply = ovs.jsonrpc.Message.create_error(
36 {"error": "unknown method"}, msg.id)
37 sys.stderr.write("unknown request %s" % msg.method)
38 elif msg.type == ovs.jsonrpc.Message.T_NOTIFY:
39 if msg.method == "shutdown":
42 rpc.error(errno.ENOTTY)
43 sys.stderr.write("unknown notification %s" % msg.method)
45 rpc.error(errno.EPROTO)
46 sys.stderr.write("unsolicited JSON-RPC reply or error\n")
54 error, pstream = ovs.stream.PassiveStream.open(name)
56 sys.stderr.write("could not listen on \"%s\": %s\n"
57 % (name, os.strerror(error)))
60 ovs.daemon.daemonize()
65 # Accept new connections.
66 error, stream = pstream.accept()
68 rpcs.append(ovs.jsonrpc.Connection(stream))
69 elif error != errno.EAGAIN:
70 sys.stderr.write("PassiveStream.accept() failed\n")
73 # Service existing connections.
79 if not rpc.get_backlog():
80 error, msg = rpc.recv()
82 if handle_rpc(rpc, msg):
85 error = rpc.get_status()
89 rpcs = [rpc for rpc in rpcs if not rpc in dead_rpcs]
94 poller = ovs.poller.Poller()
98 if not rpc.get_backlog():
104 def do_request(name, method, params_string):
105 params = ovs.json.from_string(params_string)
106 msg = ovs.jsonrpc.Message.create_request(method, params)
109 sys.stderr.write("not a valid JSON-RPC request: %s\n" % s)
112 error, stream = ovs.stream.Stream.open_block(ovs.stream.Stream.open(name))
114 sys.stderr.write("could not open \"%s\": %s\n"
115 % (name, os.strerror(error)))
118 rpc = ovs.jsonrpc.Connection(stream)
120 error = rpc.send(msg)
122 sys.stderr.write("could not send request: %s\n" % os.strerror(error))
125 error, msg = rpc.recv_block()
127 sys.stderr.write("error waiting for reply: %s\n" % os.strerror(error))
130 print ovs.json.to_string(msg.to_json())
135 def do_notify(name, method, params_string):
136 params = ovs.json.from_string(params_string)
137 msg = ovs.jsonrpc.Message.create_notify(method, params)
140 sys.stderr.write("not a valid JSON-RPC notification: %s\n" % s)
143 error, stream = ovs.stream.Stream.open_block(ovs.stream.Stream.open(name))
145 sys.stderr.write("could not open \"%s\": %s\n"
146 % (name, os.strerror(error)))
149 rpc = ovs.jsonrpc.Connection(stream)
151 error = rpc.send_block(msg)
153 sys.stderr.write("could not send notification: %s\n"
154 % os.strerror(error))
162 parser = argparse.ArgumentParser(
163 description="JSON-RPC test utility for Python.",
164 formatter_class=argparse.RawDescriptionHelpFormatter)
166 commands = {"listen": (do_listen, 1),
167 "request": (do_request, 3),
168 "notify": (do_notify, 3),
169 "help": (parser.print_help, (0,))}
171 group_description = """\
172 listen LOCAL listen for connections on LOCAL
173 request REMOTE METHOD PARAMS send request, print reply
174 notify REMOTE METHOD PARAMS send notification and exit
175 """ + ovs.stream.usage("JSON-RPC")
177 group = parser.add_argument_group(title="Commands",
178 description=group_description)
179 group.add_argument('command', metavar="COMMAND", nargs=1,
180 choices=commands, help="Command to use.")
181 group.add_argument('command_args', metavar="ARG", nargs='*',
182 help="Arguments to COMMAND.")
184 ovs.daemon.add_args(parser)
185 args = parser.parse_args()
186 ovs.daemon.handle_args(args)
188 command_name = args.command[0]
189 args = args.command_args
190 if not command_name in commands:
191 sys.stderr.write("%s: unknown command \"%s\" "
192 "(use --help for help)\n" % (argv[0], command_name))
195 func, n_args = commands[command_name]
196 if type(n_args) == tuple:
197 if len(args) < n_args[0]:
198 sys.stderr.write("%s: \"%s\" requires at least %d arguments but "
200 % (argv[0], command_name, n_args, len(args)))
202 elif type(n_args) == int:
203 if len(args) != n_args:
204 sys.stderr.write("%s: \"%s\" requires %d arguments but %d "
206 % (argv[0], command_name, n_args, len(args)))
214 if __name__ == '__main__':