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 | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 15 | 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 Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 31 | def handle(self): |
| 32 | data = self.request.recv(1024).strip() |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 33 | 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 Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 41 | |
| 42 | class ThreadedTestServer(socketserver.ThreadingMixIn, socketserver.TCPServer): |
Chetan Gaonker | 02236ba | 2016-04-26 11:24:34 -0700 | [diff] [blame] | 43 | allow_reuse_address = True |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 44 | |
| 45 | @nottest |
| 46 | def 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 |
| 55 | def cord_test_server_stop(server): |
| 56 | server.shutdown() |
| 57 | server.server_close() |
| 58 | |
| 59 | @nottest |
| 60 | def 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 Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 66 | s.close() |
| 67 | if data == 'DONE': |
| 68 | return True |
| 69 | return False |
| 70 | |
| 71 | @nottest |
| 72 | def 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 Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 81 | if data == 'DONE': |
| 82 | return True |
| 83 | return False |