Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016-present Ciena Corporation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 16 | import SocketServer as socketserver |
| 17 | import threading |
| 18 | import socket |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 19 | from CordContainer import Onos, Quagga |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 20 | from nose.tools import nottest |
| 21 | |
| 22 | ##Server to handle container restart requests from test container. |
| 23 | ##Used now to restart ONOS from vrouter test container |
| 24 | |
| 25 | CORD_TEST_HOST = '172.17.0.1' |
| 26 | CORD_TEST_PORT = 25000 |
| 27 | |
| 28 | class CordTestServer(socketserver.BaseRequestHandler): |
| 29 | |
Chetan Gaonker | fd3d650 | 2016-05-03 13:23:07 -0700 | [diff] [blame] | 30 | def restart_onos(self, *args): |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 31 | print('Restarting ONOS') |
| 32 | onos = Onos(restart = True) |
| 33 | self.request.sendall('DONE') |
| 34 | |
Chetan Gaonker | fd3d650 | 2016-05-03 13:23:07 -0700 | [diff] [blame] | 35 | def restart_quagga(self, *args): |
| 36 | config_file = Quagga.quagga_config_file |
| 37 | boot_delay = 15 |
| 38 | if args: |
| 39 | config_file = args[0] |
| 40 | if len(args) > 1: |
| 41 | boot_delay = int(args[1]) |
| 42 | print('Restarting QUAGGA with config file %s, delay %d secs'%(config_file, boot_delay)) |
| 43 | quagga = Quagga(restart = True, config_file = config_file, boot_delay = boot_delay) |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 44 | self.request.sendall('DONE') |
| 45 | |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 46 | def restart_radius(self, *args): |
| 47 | print('Restarting RADIUS Server') |
| 48 | radius = Radius(restart = True) |
| 49 | self.request.sendall('DONE') |
| 50 | |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 51 | callback_table = { 'RESTART_ONOS' : restart_onos, |
| 52 | 'RESTART_QUAGGA' : restart_quagga, |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 53 | 'RESTART_RADIUS' : restart_radius, |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 56 | def handle(self): |
| 57 | data = self.request.recv(1024).strip() |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 58 | cmd = data.split()[0] |
| 59 | try: |
Chetan Gaonker | fd3d650 | 2016-05-03 13:23:07 -0700 | [diff] [blame] | 60 | #args = ' '.join(data.split()[1:]) |
| 61 | args = data.split()[1:] |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 62 | except: |
| 63 | args = None |
| 64 | |
| 65 | if self.callback_table.has_key(cmd): |
Chetan Gaonker | fd3d650 | 2016-05-03 13:23:07 -0700 | [diff] [blame] | 66 | self.callback_table[cmd](self, *args) |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 67 | |
| 68 | class ThreadedTestServer(socketserver.ThreadingMixIn, socketserver.TCPServer): |
Chetan Gaonker | 02236ba | 2016-04-26 11:24:34 -0700 | [diff] [blame] | 69 | allow_reuse_address = True |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 70 | |
| 71 | @nottest |
| 72 | def cord_test_server_start(): |
| 73 | server = ThreadedTestServer( (CORD_TEST_HOST, CORD_TEST_PORT), CordTestServer) |
| 74 | task = threading.Thread(target = server.serve_forever) |
| 75 | ##terminate when main thread exits |
| 76 | task.daemon = True |
| 77 | task.start() |
| 78 | return server |
| 79 | |
| 80 | @nottest |
| 81 | def cord_test_server_stop(server): |
| 82 | server.shutdown() |
| 83 | server.server_close() |
| 84 | |
| 85 | @nottest |
| 86 | def cord_test_onos_restart(): |
| 87 | '''Send ONOS restart to server''' |
| 88 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 89 | s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) ) |
| 90 | s.sendall('RESTART_ONOS\n') |
| 91 | data = s.recv(1024).strip() |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 92 | s.close() |
| 93 | if data == 'DONE': |
| 94 | return True |
| 95 | return False |
| 96 | |
| 97 | @nottest |
Chetan Gaonker | fd3d650 | 2016-05-03 13:23:07 -0700 | [diff] [blame] | 98 | def cord_test_quagga_restart(config_file = None, boot_delay = 30): |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 99 | '''Send QUAGGA restart to server''' |
| 100 | if config_file is None: |
| 101 | config_file = Quagga.quagga_config_file |
| 102 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 103 | s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) ) |
Chetan Gaonker | fd3d650 | 2016-05-03 13:23:07 -0700 | [diff] [blame] | 104 | s.sendall('RESTART_QUAGGA {0} {1}\n'.format(config_file, boot_delay)) |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 105 | data = s.recv(1024).strip() |
| 106 | s.close() |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 107 | if data == 'DONE': |
| 108 | return True |
| 109 | return False |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 110 | |
| 111 | @nottest |
| 112 | def cord_test_radius_restart(): |
| 113 | '''Send Radius server restart to server''' |
| 114 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 115 | s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) ) |
| 116 | s.sendall('RESTART_RADIUS\n') |
| 117 | data = s.recv(1024).strip() |
| 118 | s.close() |
| 119 | if data == 'DONE': |
| 120 | return True |
| 121 | return False |