1 # Copyright (c) 2009, 2010, 2011 Nicira Networks
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 options, args = getopt.gnu_getopt(
163 argv[1:], 'h', ["help"] + ovs.daemon.LONG_OPTIONS)
164 except getopt.GetoptError, geo:
165 sys.stderr.write("%s: %s\n" % (ovs.util.PROGRAM_NAME, geo.msg))
168 for key, value in options:
169 if key in ['h', '--help']:
171 elif not ovs.daemon.parse_opt(key, value):
172 sys.stderr.write("%s: unhandled option %s\n"
173 % (ovs.util.PROGRAM_NAME, key))
176 commands = {"listen": (do_listen, 1),
177 "request": (do_request, 3),
178 "notify": (do_notify, 3),
179 "help": (usage, (0,))}
181 command_name = args[0]
183 if not command_name in commands:
184 sys.stderr.write("%s: unknown command \"%s\" "
185 "(use --help for help)\n" % (argv[0], command_name))
188 func, n_args = commands[command_name]
189 if type(n_args) == tuple:
190 if len(args) < n_args[0]:
191 sys.stderr.write("%s: \"%s\" requires at least %d arguments but "
193 % (argv[0], command_name, n_args, len(args)))
195 elif type(n_args) == int:
196 if len(args) != n_args:
197 sys.stderr.write("%s: \"%s\" requires %d arguments but %d "
199 % (argv[0], command_name, n_args, len(args)))
208 sys.stdout.write("""\
209 %s: JSON-RPC test utility for Python
210 usage: %s [OPTIONS] COMMAND [ARG...]
211 listen LOCAL listen for connections on LOCAL
212 request REMOTE METHOD PARAMS send request, print reply
213 notify REMOTE METHOD PARAMS send notification and exit
214 """ % (ovs.util.PROGRAM_NAME, ovs.util.PROGRAM_NAME))
215 ovs.stream.usage("JSON-RPC", True, True, True)
219 -h, --help display this help message
224 if __name__ == '__main__':