blob: 806b36b16440d9b27065cc67be39a5580700334d [file] [log] [blame]
Chetan Gaonker3533faa2016-04-25 17:50:14 -07001import SocketServer as socketserver
2import threading
3import socket
Chetan Gaonker6cf6e472016-04-26 14:41:51 -07004from CordContainer import Onos, Quagga
Chetan Gaonker3533faa2016-04-25 17:50:14 -07005from nose.tools import nottest
6
7##Server to handle container restart requests from test container.
8##Used now to restart ONOS from vrouter test container
9
10CORD_TEST_HOST = '172.17.0.1'
11CORD_TEST_PORT = 25000
12
13class CordTestServer(socketserver.BaseRequestHandler):
14
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070015 def restart_onos(self, args):
16 print('Restarting ONOS')
17 onos = Onos(restart = True)
18 self.request.sendall('DONE')
19
20 def restart_quagga(self, args):
21 if args is None:
22 args = Quagga.quagga_config_file
23 print('Restarting QUAGGA with config file %s'%args)
24 quagga = Quagga(restart = True, config_file = args)
25 self.request.sendall('DONE')
26
27 callback_table = { 'RESTART_ONOS' : restart_onos,
28 'RESTART_QUAGGA' : restart_quagga,
29 }
30
Chetan Gaonker3533faa2016-04-25 17:50:14 -070031 def handle(self):
32 data = self.request.recv(1024).strip()
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070033 cmd = data.split()[0]
34 try:
35 args = ' '.join(data.split()[1:])
36 except:
37 args = None
38
39 if self.callback_table.has_key(cmd):
40 self.callback_table[cmd](self, args)
Chetan Gaonker3533faa2016-04-25 17:50:14 -070041
42class ThreadedTestServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
Chetan Gaonker02236ba2016-04-26 11:24:34 -070043 allow_reuse_address = True
Chetan Gaonker3533faa2016-04-25 17:50:14 -070044
45@nottest
46def cord_test_server_start():
47 server = ThreadedTestServer( (CORD_TEST_HOST, CORD_TEST_PORT), CordTestServer)
48 task = threading.Thread(target = server.serve_forever)
49 ##terminate when main thread exits
50 task.daemon = True
51 task.start()
52 return server
53
54@nottest
55def cord_test_server_stop(server):
56 server.shutdown()
57 server.server_close()
58
59@nottest
60def cord_test_onos_restart():
61 '''Send ONOS restart to server'''
62 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
63 s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) )
64 s.sendall('RESTART_ONOS\n')
65 data = s.recv(1024).strip()
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070066 s.close()
67 if data == 'DONE':
68 return True
69 return False
70
71@nottest
72def cord_test_quagga_restart(config_file = None):
73 '''Send QUAGGA restart to server'''
74 if config_file is None:
75 config_file = Quagga.quagga_config_file
76 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
77 s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) )
78 s.sendall('RESTART_QUAGGA {}\n'.format(config_file))
79 data = s.recv(1024).strip()
80 s.close()
Chetan Gaonker3533faa2016-04-25 17:50:14 -070081 if data == 'DONE':
82 return True
83 return False