Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 1 | import SocketServer as socketserver |
| 2 | import threading |
| 3 | import socket |
| 4 | from CordContainer import Onos |
| 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 | |
| 15 | def handle(self): |
| 16 | data = self.request.recv(1024).strip() |
| 17 | if data == 'RESTART_ONOS': |
| 18 | print('Restarting ONOS') |
| 19 | onos = Onos(restart = True) |
| 20 | self.request.sendall('DONE') |
| 21 | |
| 22 | class ThreadedTestServer(socketserver.ThreadingMixIn, socketserver.TCPServer): |
| 23 | pass |
| 24 | |
| 25 | @nottest |
| 26 | def cord_test_server_start(): |
| 27 | server = ThreadedTestServer( (CORD_TEST_HOST, CORD_TEST_PORT), CordTestServer) |
| 28 | task = threading.Thread(target = server.serve_forever) |
| 29 | ##terminate when main thread exits |
| 30 | task.daemon = True |
| 31 | task.start() |
| 32 | return server |
| 33 | |
| 34 | @nottest |
| 35 | def cord_test_server_stop(server): |
| 36 | server.shutdown() |
| 37 | server.server_close() |
| 38 | |
| 39 | @nottest |
| 40 | def cord_test_onos_restart(): |
| 41 | '''Send ONOS restart to server''' |
| 42 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 43 | s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) ) |
| 44 | s.sendall('RESTART_ONOS\n') |
| 45 | data = s.recv(1024).strip() |
| 46 | if data == 'DONE': |
| 47 | return True |
| 48 | return False |