ofproto: Rename "private.h" to "ofproto->provider.h".
[openvswitch] / tests / test-reconnect.py
1 # Copyright (c) 2009, 2010 Nicira Networks.
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 errno
16 import logging
17 import sys
18
19 import ovs.reconnect
20
21 def do_enable(arg):
22     r.enable(now)
23
24 def do_disable(arg):
25     r.disable(now)
26
27 def do_force_reconnect(arg):
28     r.force_reconnect(now)
29
30 def error_from_string(s):
31     if not s:
32         return 0
33     elif s == "ECONNREFUSED":
34         return errno.ECONNREFUSED
35     elif s == "EOF":
36         return EOF
37     else:
38         sys.stderr.write("unknown error '%s'\n" % s)
39         sys.exit(1)
40
41 def do_disconnected(arg):
42     r.disconnected(now, error_from_string(arg))
43     
44 def do_connecting(arg):
45     r.connecting(now)
46
47 def do_connect_failed(arg):
48     r.connect_failed(now, error_from_string(arg))
49
50 def do_connected(arg):
51     r.connected(now)
52
53 def do_received(arg):
54     r.received(now)
55
56 def do_run(arg):
57     global now
58     if arg is not None:
59         now += int(arg)
60
61     action = r.run(now)
62     if action is None:
63         pass
64     elif action == ovs.reconnect.CONNECT:
65         print "  should connect"
66     elif action == ovs.reconnect.DISCONNECT:
67         print "  should disconnect"
68     elif action == ovs.reconnect.PROBE:
69         print "  should send probe"
70     else:
71         assert False
72
73 def do_advance(arg):
74     global now
75     now += int(arg)
76
77 def do_timeout(arg):
78     global now
79     timeout = r.timeout(now)
80     if timeout >= 0:
81         print "  advance %d ms" % timeout
82         now += timeout
83     else:
84         print "  no timeout"
85
86 def do_set_max_tries(arg):
87     r.set_max_tries(int(arg))
88
89 def diff_stats(old, new, delta):
90     if (old.state != new.state or
91         old.state_elapsed != new.state_elapsed or
92         old.backoff != new.backoff):
93         print("  in %s for %d ms (%d ms backoff)"
94               % (new.state, new.state_elapsed, new.backoff))
95
96     if (old.creation_time != new.creation_time or
97         old.last_received != new.last_received or
98         old.last_connected != new.last_connected):
99         print("  created %d, last received %d, last connected %d"
100               % (new.creation_time, new.last_received, new.last_connected))
101
102     if (old.n_successful_connections != new.n_successful_connections or
103         old.n_attempted_connections != new.n_attempted_connections or
104         old.seqno != new.seqno):
105         print("  %d successful connections out of %d attempts, seqno %d"
106               % (new.n_successful_connections, new.n_attempted_connections,
107                  new.seqno))
108
109     if (old.is_connected != new.is_connected):
110         if new.is_connected:
111             negate = ""
112         else:
113             negate = "dis"
114         print("  %sconnected" % negate)
115
116     if (old.last_connected != new.last_connected or
117         (new.msec_since_connect != None and
118          old.msec_since_connect != new.msec_since_connect - delta) or
119         (old.total_connected_duration != new.total_connected_duration - delta and
120          not (old.total_connected_duration == 0 and
121               new.total_connected_duration == 0))):
122         print("  last connected %d ms ago, connected %d ms total"
123               % (new.msec_since_connect, new.total_connected_duration))
124
125     if (old.last_disconnected != new.last_disconnected or
126         (new.msec_since_disconnect != None and
127          old.msec_since_disconnect != new.msec_since_disconnect - delta)):
128         print("  disconnected at %d ms (%d ms ago)"
129               % (new.last_disconnected, new.msec_since_disconnect))
130
131 def do_set_passive(arg):
132     r.set_passive(True, now)
133
134 def do_listening(arg):
135     r.listening(now)
136
137 def do_listen_error(arg):
138     r.listen_error(now, int(arg))
139
140 def main():
141     commands = {
142         "enable": do_enable,
143         "disable": do_disable,
144         "force-reconnect": do_force_reconnect,
145         "disconnected": do_disconnected,
146         "connecting": do_connecting,
147         "connect-failed": do_connect_failed,
148         "connected": do_connected,
149         "received": do_received,
150         "run": do_run,
151         "advance": do_advance,
152         "timeout": do_timeout,
153         "set-max-tries": do_set_max_tries,
154         "passive": do_set_passive,
155         "listening": do_listening,
156         "listen-error": do_listen_error
157     }
158
159     logging.basicConfig(level=logging.CRITICAL)
160
161     global now
162     global r
163
164     now = 1000
165     r = ovs.reconnect.Reconnect(now)
166     r.set_name("remote")
167     prev = r.get_stats(now)
168     print "### t=%d ###" % now
169     old_time = now
170     old_max_tries = r.get_max_tries()
171     while True:
172         line = sys.stdin.readline()
173         if line == "":
174             break
175
176         print line[:-1]
177         if line[0] == "#":
178             continue
179
180         args = line.split()
181         if len(args) == 0:
182             continue
183
184         command = args[0]
185         if len(args) > 1:
186             op = args[1]
187         else:
188             op = None
189         commands[command](op)
190
191         if old_time != now:
192             print
193             print "### t=%d ###" % now
194
195         cur = r.get_stats(now)
196         diff_stats(prev, cur, now - old_time)
197         prev = cur
198         if r.get_max_tries() != old_max_tries:
199             old_max_tries = r.get_max_tries()
200             print "  %d tries left" % old_max_tries
201
202         old_time = now
203
204 if __name__ == '__main__':
205     main()
206
207