Global replace of Nicira Networks.
[sliver-openvswitch.git] / tests / test-unixctl.py
1 # Copyright (c) 2012 Nicira, Inc.
2 #
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:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import argparse
16 import sys
17
18 import ovs.daemon
19 import ovs.unixctl
20
21 vlog = ovs.vlog.Vlog("test-unixctl")
22 exiting = False
23
24 def unixctl_exit(conn, unused_argv, aux):
25     assert aux == "aux_exit"
26     global exiting
27
28     exiting = True
29     conn.reply(None)
30
31
32 def unixctl_echo(conn, argv, aux):
33     assert aux == "aux_echo"
34     conn.reply(str(argv))
35
36
37 def unixctl_echo_error(conn, argv, aux):
38     assert aux ==  "aux_echo_error"
39     conn.reply_error(str(argv))
40
41
42 def unixctl_block(conn, unused_argv, unused_aux):
43     pass
44
45
46 def main():
47     parser = argparse.ArgumentParser(
48         description="Open vSwitch unixctl test program for Python")
49     parser.add_argument("--unixctl", help="UNIXCTL socket location or 'none'.")
50
51     ovs.daemon.add_args(parser)
52     ovs.vlog.add_args(parser)
53     args = parser.parse_args()
54     ovs.daemon.handle_args(args)
55     ovs.vlog.handle_args(args)
56
57     ovs.daemon.daemonize_start()
58     error, server = ovs.unixctl.UnixctlServer.create(args.unixctl)
59     if error:
60         ovs.util.ovs_fatal(error, "could not create unixctl server at %s"
61                            % args.unixctl, vlog)
62
63     ovs.unixctl.command_register("exit", "", 0, 0, unixctl_exit, "aux_exit")
64     ovs.unixctl.command_register("echo", "[arg ...]", 1, 2, unixctl_echo,
65                                  "aux_echo")
66     ovs.unixctl.command_register("echo_error", "[arg ...]", 1, 2,
67                                  unixctl_echo_error, "aux_echo_error")
68     ovs.unixctl.command_register("block", "", 0, 0, unixctl_block, None)
69     ovs.daemon.daemonize_complete()
70
71     vlog.info("Entering run loop.")
72     poller = ovs.poller.Poller()
73     while not exiting:
74         server.run()
75         server.wait(poller)
76         if exiting:
77             poller.immediate_wake()
78         poller.block()
79     server.close()
80
81
82 if __name__ == '__main__':
83     try:
84         main()
85     except SystemExit:
86         # Let system.exit() calls complete normally
87         raise
88     except:
89         vlog.exception("traceback")
90         sys.exit(ovs.daemon.RESTART_EXIT_CODE)