]>
git.proxmox.com Git - mirror_ovs.git/blob - tests/test-jsonrpc.py
1 # Copyright (c) 2009, 2010 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.
26 def handle_rpc(rpc
, msg
):
30 if msg
.type == ovs
.jsonrpc
.Message
.T_REQUEST
:
31 if msg
.method
== "echo":
32 reply
= ovs
.jsonrpc
.Message
.create_reply(msg
.params
, msg
.id)
34 reply
= ovs
.jsonrpc
.Message
.create_error(
35 {"error": "unknown method"}, msg
.id)
36 sys
.stderr
.write("unknown request %s" % msg
.method
)
37 elif msg
.type == ovs
.jsonrpc
.Message
.T_NOTIFY
:
38 if msg
.method
== "shutdown":
41 rpc
.error(errno
.ENOTTY
)
42 sys
.stderr
.write("unknown notification %s" % msg
.method
)
44 rpc
.error(errno
.EPROTO
)
45 sys
.stderr
.write("unsolicited JSON-RPC reply or error\n")
52 ovs
.daemon
.die_if_already_running()
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():
103 def do_request(name
, method
, params_string
):
104 params
= ovs
.json
.from_string(params_string
)
105 msg
= ovs
.jsonrpc
.Message
.create_request(method
, params
)
108 sys
.stderr
.write("not a valid JSON-RPC request: %s\n" % s
)
111 error
, stream
= ovs
.stream
.Stream
.open_block(ovs
.stream
.Stream
.open(name
))
113 sys
.stderr
.write("could not open \"%s\": %s\n"
114 % (name
, os
.strerror(error
)))
117 rpc
= ovs
.jsonrpc
.Connection(stream
)
119 error
= rpc
.send(msg
)
121 sys
.stderr
.write("could not send request: %s\n" % os
.strerror(error
))
124 error
, msg
= rpc
.recv_block()
126 sys
.stderr
.write("error waiting for reply: %s\n" % os
.strerror(error
))
129 print ovs
.json
.to_string(msg
.to_json())
133 def do_notify(name
, method
, params_string
):
134 params
= ovs
.json
.from_string(params_string
)
135 msg
= ovs
.jsonrpc
.Message
.create_notify(method
, params
)
138 sys
.stderr
.write("not a valid JSON-RPC notification: %s\n" % s
)
141 error
, stream
= ovs
.stream
.Stream
.open_block(ovs
.stream
.Stream
.open(name
))
143 sys
.stderr
.write("could not open \"%s\": %s\n"
144 % (name
, os
.strerror(error
)))
147 rpc
= ovs
.jsonrpc
.Connection(stream
)
149 error
= rpc
.send_block(msg
)
151 sys
.stderr
.write("could not send notification: %s\n"
152 % os
.strerror(error
))
159 options
, args
= getopt
.gnu_getopt(
160 argv
[1:], 'h', ["help"] + ovs
.daemon
.LONG_OPTIONS
)
161 except getopt
.GetoptError
, geo
:
162 sys
.stderr
.write("%s: %s\n" % (ovs
.util
.PROGRAM_NAME
, geo
.msg
))
165 for key
, value
in options
:
166 if key
in ['h', '--help']:
168 elif not ovs
.daemon
.parse_opt(key
, value
):
169 sys
.stderr
.write("%s: unhandled option %s\n"
170 % (ovs
.util
.PROGRAM_NAME
, key
))
173 commands
= {"listen": (do_listen
, 1),
174 "request": (do_request
, 3),
175 "notify": (do_notify
, 3),
176 "help": (usage
, (0,))}
178 command_name
= args
[0]
180 if not command_name
in commands
:
181 sys
.stderr
.write("%s: unknown command \"%s\" "
182 "(use --help for help)\n" % (argv0
, command_name
))
185 func
, n_args
= commands
[command_name
]
186 if type(n_args
) == tuple:
187 if len(args
) < n_args
[0]:
188 sys
.stderr
.write("%s: \"%s\" requires at least %d arguments but "
190 % (argv0
, command_name
, n_args
, len(args
)))
192 elif type(n_args
) == int:
193 if len(args
) != n_args
:
194 sys
.stderr
.write("%s: \"%s\" requires %d arguments but %d "
196 % (argv0
, command_name
, n_args
, len(args
)))
204 sys
.stdout
.write("""\
205 %s: JSON-RPC test utility for Python
206 usage: %s [OPTIONS] COMMAND [ARG...]
207 listen LOCAL listen for connections on LOCAL
208 request REMOTE METHOD PARAMS send request, print reply
209 notify REMOTE METHOD PARAMS send notification and exit
210 """ % (ovs
.util
.PROGRAM_NAME
, ovs
.util
.PROGRAM_NAME
))
211 ovs
.stream
.usage("JSON-RPC", True, True, True)
215 -h, --help display this help message
219 if __name__
== '__main__':