Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 1 | import SocketServer as socketserver |
| 2 | import threading |
| 3 | import socket |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 4 | from CordContainer import Onos, Quagga |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 5 | from 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 | |
| 10 | CORD_TEST_HOST = '172.17.0.1' |
| 11 | CORD_TEST_PORT = 25000 |
| 12 | |
| 13 | class CordTestServer(socketserver.BaseRequestHandler): |
| 14 | |
Chetan Gaonker | fd3d650 | 2016-05-03 13:23:07 -0700 | [diff] [blame] | 15 | def restart_onos(self, *args): |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 16 | print('Restarting ONOS') |
| 17 | onos = Onos(restart = True) |
| 18 | self.request.sendall('DONE') |
| 19 | |
Chetan Gaonker | fd3d650 | 2016-05-03 13:23:07 -0700 | [diff] [blame] | 20 | 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 Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 29 | self.request.sendall('DONE') |
| 30 | |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 31 | def restart_radius(self, *args): |
| 32 | print('Restarting RADIUS Server') |
| 33 | radius = Radius(restart = True) |
| 34 | self.request.sendall('DONE') |
| 35 | |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 36 | callback_table = { 'RESTART_ONOS' : restart_onos, |
| 37 | 'RESTART_QUAGGA' : restart_quagga, |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 38 | 'RESTART_RADIUS' : restart_radius, |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 41 | def handle(self): |
| 42 | data = self.request.recv(1024).strip() |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 43 | cmd = data.split()[0] |
| 44 | try: |
Chetan Gaonker | fd3d650 | 2016-05-03 13:23:07 -0700 | [diff] [blame] | 45 | #args = ' '.join(data.split()[1:]) |
| 46 | args = data.split()[1:] |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 47 | except: |
| 48 | args = None |
| 49 | |
| 50 | if self.callback_table.has_key(cmd): |
Chetan Gaonker | fd3d650 | 2016-05-03 13:23:07 -0700 | [diff] [blame] | 51 | self.callback_table[cmd](self, *args) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 52 | |
| 53 | class ThreadedTestServer(socketserver.ThreadingMixIn, socketserver.TCPServer): |
Chetan Gaonker | 02236ba | 2016-04-26 11:24:34 -0700 | [diff] [blame] | 54 | allow_reuse_address = True |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 55 | |
| 56 | @nottest |
| 57 | def 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 |
| 66 | def cord_test_server_stop(server): |
| 67 | server.shutdown() |
| 68 | server.server_close() |
| 69 | |
| 70 | @nottest |
| 71 | def 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 Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 77 | s.close() |
| 78 | if data == 'DONE': |
| 79 | return True |
| 80 | return False |
| 81 | |
| 82 | @nottest |
Chetan Gaonker | fd3d650 | 2016-05-03 13:23:07 -0700 | [diff] [blame] | 83 | def cord_test_quagga_restart(config_file = None, boot_delay = 30): |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 84 | '''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 Gaonker | fd3d650 | 2016-05-03 13:23:07 -0700 | [diff] [blame] | 89 | s.sendall('RESTART_QUAGGA {0} {1}\n'.format(config_file, boot_delay)) |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 90 | data = s.recv(1024).strip() |
| 91 | s.close() |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 92 | if data == 'DONE': |
| 93 | return True |
| 94 | return False |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 95 | |
| 96 | @nottest |
| 97 | def 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 |