blob: 06ddfbab0bf3a2c90e5217a043f447408fd31e19 [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 Gaonkerfd3d6502016-05-03 13:23:07 -070015 def restart_onos(self, *args):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070016 print('Restarting ONOS')
17 onos = Onos(restart = True)
18 self.request.sendall('DONE')
19
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070020 def restart_quagga(self, *args):
21 config_file = Quagga.quagga_config_file
22 boot_delay = 15
23 if args:
24 config_file = args[0]
25 if len(args) > 1:
26 boot_delay = int(args[1])
27 print('Restarting QUAGGA with config file %s, delay %d secs'%(config_file, boot_delay))
28 quagga = Quagga(restart = True, config_file = config_file, boot_delay = boot_delay)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070029 self.request.sendall('DONE')
30
Chetan Gaonker7f4bf742016-05-04 15:56:08 -070031 def restart_radius(self, *args):
32 print('Restarting RADIUS Server')
33 radius = Radius(restart = True)
34 self.request.sendall('DONE')
35
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070036 callback_table = { 'RESTART_ONOS' : restart_onos,
37 'RESTART_QUAGGA' : restart_quagga,
Chetan Gaonker7f4bf742016-05-04 15:56:08 -070038 'RESTART_RADIUS' : restart_radius,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070039 }
40
Chetan Gaonker3533faa2016-04-25 17:50:14 -070041 def handle(self):
42 data = self.request.recv(1024).strip()
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070043 cmd = data.split()[0]
44 try:
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070045 #args = ' '.join(data.split()[1:])
46 args = data.split()[1:]
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070047 except:
48 args = None
49
50 if self.callback_table.has_key(cmd):
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070051 self.callback_table[cmd](self, *args)
Chetan Gaonker3533faa2016-04-25 17:50:14 -070052
53class ThreadedTestServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
Chetan Gaonker02236ba2016-04-26 11:24:34 -070054 allow_reuse_address = True
Chetan Gaonker3533faa2016-04-25 17:50:14 -070055
56@nottest
57def cord_test_server_start():
58 server = ThreadedTestServer( (CORD_TEST_HOST, CORD_TEST_PORT), CordTestServer)
59 task = threading.Thread(target = server.serve_forever)
60 ##terminate when main thread exits
61 task.daemon = True
62 task.start()
63 return server
64
65@nottest
66def cord_test_server_stop(server):
67 server.shutdown()
68 server.server_close()
69
70@nottest
71def cord_test_onos_restart():
72 '''Send ONOS restart to server'''
73 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
74 s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) )
75 s.sendall('RESTART_ONOS\n')
76 data = s.recv(1024).strip()
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070077 s.close()
78 if data == 'DONE':
79 return True
80 return False
81
82@nottest
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070083def cord_test_quagga_restart(config_file = None, boot_delay = 30):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070084 '''Send QUAGGA restart to server'''
85 if config_file is None:
86 config_file = Quagga.quagga_config_file
87 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
88 s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) )
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070089 s.sendall('RESTART_QUAGGA {0} {1}\n'.format(config_file, boot_delay))
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070090 data = s.recv(1024).strip()
91 s.close()
Chetan Gaonker3533faa2016-04-25 17:50:14 -070092 if data == 'DONE':
93 return True
94 return False
Chetan Gaonker7f4bf742016-05-04 15:56:08 -070095
96@nottest
97def cord_test_radius_restart():
98 '''Send Radius server restart to server'''
99 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
100 s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) )
101 s.sendall('RESTART_RADIUS\n')
102 data = s.recv(1024).strip()
103 s.close()
104 if data == 'DONE':
105 return True
106 return False