A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 1 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 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 |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 7 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 9 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 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 | # |
A.R Karthick | 1700e0e | 2016-10-06 18:16:57 -0700 | [diff] [blame] | 16 | from CordContainer import Container, Onos, OnosStopWrapper, OnosCord, OnosCordStopWrapper, Quagga, QuaggaStopWrapper, Radius, reinitContainerClients |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 17 | from nose.tools import nottest |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 18 | from SimpleXMLRPCServer import SimpleXMLRPCServer |
| 19 | import daemon |
| 20 | import xmlrpclib |
| 21 | import os |
A R Karthick | e99ab5c | 2016-09-30 13:59:57 -0700 | [diff] [blame] | 22 | import signal |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 23 | import json |
| 24 | import time |
| 25 | import threading |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 26 | |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 27 | ##Server to handle container restart/stop requests from test container. |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 28 | ##Used now to restart ONOS from vrouter test container |
| 29 | |
| 30 | CORD_TEST_HOST = '172.17.0.1' |
| 31 | CORD_TEST_PORT = 25000 |
| 32 | |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 33 | class CordTestServer(object): |
| 34 | |
A R Karthick | bd9b8a3 | 2016-07-21 09:56:45 -0700 | [diff] [blame] | 35 | onos_cord = None |
| 36 | |
A R Karthick | 889d965 | 2016-10-03 14:13:45 -0700 | [diff] [blame] | 37 | def __restart_onos(self, node = None, config = None): |
A R Karthick | bd9b8a3 | 2016-07-21 09:56:45 -0700 | [diff] [blame] | 38 | if self.onos_cord: |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 39 | onos_config = '{}/network-cfg.json'.format(OnosCord.onos_config_dir) |
| 40 | else: |
| 41 | onos_config = '{}/network-cfg.json'.format(Onos.host_config_dir) |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 42 | if config is None: |
| 43 | try: |
| 44 | os.unlink(onos_config) |
| 45 | except: |
| 46 | pass |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 47 | print('Restarting ONOS') |
A R Karthick | bd9b8a3 | 2016-07-21 09:56:45 -0700 | [diff] [blame] | 48 | if self.onos_cord: |
| 49 | self.onos_cord.start(restart = True, network_cfg = config) |
A R Karthick | d44cea1 | 2016-07-20 12:16:41 -0700 | [diff] [blame] | 50 | else: |
A R Karthick | 889d965 | 2016-10-03 14:13:45 -0700 | [diff] [blame] | 51 | Onos.restart_node(node = node, network_cfg = config) |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 52 | return 'DONE' |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 53 | |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 54 | def restart_onos(self, kwargs): |
| 55 | return self.__restart_onos(**kwargs) |
| 56 | |
A.R Karthick | 1700e0e | 2016-10-06 18:16:57 -0700 | [diff] [blame] | 57 | def __shutdown_onos(self, node = None): |
| 58 | if node is None: |
| 59 | node = Onos.NAME |
| 60 | OnosStopWrapper(node) |
| 61 | return 'DONE' |
| 62 | |
| 63 | def shutdown_onos(self, kwargs): |
| 64 | return self.__shutdown_onos(**kwargs) |
| 65 | |
A R Karthick | e2c24bd | 2016-10-07 14:51:38 -0700 | [diff] [blame] | 66 | def __add_cluster_onos(self, count = 1, config = None): |
| 67 | Onos.add_cluster(count = count, network_cfg = config) |
| 68 | return 'DONE' |
| 69 | |
| 70 | def add_cluster_onos(self, kwargs): |
A R Karthick | db59cf7 | 2016-10-10 10:43:22 -0700 | [diff] [blame] | 71 | return self.__add_cluster_onos(**kwargs) |
A R Karthick | e2c24bd | 2016-10-07 14:51:38 -0700 | [diff] [blame] | 72 | |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 73 | def __restart_quagga(self, config = None, boot_delay = 30 ): |
Chetan Gaonker | fd3d650 | 2016-05-03 13:23:07 -0700 | [diff] [blame] | 74 | config_file = Quagga.quagga_config_file |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 75 | if config is not None: |
| 76 | quagga_config = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config) |
| 77 | config_file = '{}/testrib_gen.conf'.format(Quagga.guest_quagga_config) |
| 78 | with open(quagga_config, 'w+') as fd: |
| 79 | fd.write(str(config)) |
| 80 | print('Restarting QUAGGA with config file %s, delay %d' %(config_file, boot_delay)) |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 81 | Quagga(prefix = Container.IMAGE_PREFIX, restart = True, config_file = config_file, boot_delay = boot_delay) |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 82 | return 'DONE' |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 83 | |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 84 | def restart_quagga(self, kwargs): |
| 85 | return self.__restart_quagga(**kwargs) |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 86 | |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 87 | def stop_quagga(self): |
| 88 | quaggaStop = QuaggaStopWrapper() |
A R Karthick | 4a2362c | 2016-06-22 17:32:44 -0700 | [diff] [blame] | 89 | time.sleep(5) |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 90 | try: |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 91 | quagga_config_gen = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config) |
| 92 | os.unlink(quagga_config_gen) |
| 93 | except: pass |
| 94 | return 'DONE' |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 95 | |
A R Karthick | c3d80e2 | 2016-06-22 17:51:24 -0700 | [diff] [blame] | 96 | def __run_shell_quagga(self, cmd = None): |
| 97 | ret = 0 |
| 98 | if cmd is not None: |
| 99 | exec_cmd = 'docker exec {} {}'.format(Quagga.NAME, cmd) |
| 100 | ret = os.system(exec_cmd) |
| 101 | return ret |
| 102 | |
A R Karthick | a013a27 | 2016-08-16 16:40:19 -0700 | [diff] [blame] | 103 | def __run_shell(self, cmd = None): |
| 104 | ret = 0 |
| 105 | if cmd is not None: |
| 106 | ret = os.system(cmd) |
| 107 | return ret |
| 108 | |
A R Karthick | c3d80e2 | 2016-06-22 17:51:24 -0700 | [diff] [blame] | 109 | def run_shell_quagga(self, kwargs): |
| 110 | return self.__run_shell_quagga(**kwargs) |
| 111 | |
A R Karthick | a013a27 | 2016-08-16 16:40:19 -0700 | [diff] [blame] | 112 | def run_shell(self, kwargs): |
| 113 | return self.__run_shell(**kwargs) |
| 114 | |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 115 | def restart_radius(self): |
| 116 | print('Restarting RADIUS Server') |
A R Karthick | 07608ef | 2016-08-23 16:51:19 -0700 | [diff] [blame] | 117 | Radius(prefix = Container.IMAGE_PREFIX, restart = True) |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 118 | return 'DONE' |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 119 | |
A R Karthick | e99ab5c | 2016-09-30 13:59:57 -0700 | [diff] [blame] | 120 | def shutdown(self): |
| 121 | print('Shutting down cord test server') |
| 122 | os.kill(0, signal.SIGKILL) |
| 123 | return 'DONE' |
| 124 | |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 125 | @nottest |
A R Karthick | bd82f36 | 2016-11-10 15:08:52 -0800 | [diff] [blame] | 126 | def cord_test_server_start(daemonize = True, |
| 127 | cord_test_host = CORD_TEST_HOST, |
| 128 | cord_test_port = CORD_TEST_PORT, |
| 129 | onos_cord = None, |
| 130 | foreground=False): |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 131 | server = SimpleXMLRPCServer( (cord_test_host, cord_test_port) ) |
| 132 | server.register_instance(CordTestServer()) |
A R Karthick | bd9b8a3 | 2016-07-21 09:56:45 -0700 | [diff] [blame] | 133 | CordTestServer.onos_cord = onos_cord |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 134 | if daemonize is True: |
| 135 | d = daemon.DaemonContext(files_preserve = [server], |
| 136 | detach_process = True) |
| 137 | with d: |
| 138 | reinitContainerClients() |
| 139 | server.serve_forever() |
| 140 | else: |
A R Karthick | bd82f36 | 2016-11-10 15:08:52 -0800 | [diff] [blame] | 141 | if foreground: |
| 142 | try: |
| 143 | server.serve_forever() |
| 144 | except KeyboardInterrupt: |
| 145 | return server |
| 146 | else: |
| 147 | task = threading.Thread(target = server.serve_forever) |
| 148 | ##terminate when main thread exits |
| 149 | task.daemon = True |
| 150 | task.start() |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 151 | return server |
| 152 | |
| 153 | @nottest |
| 154 | def cord_test_server_stop(server): |
| 155 | server.shutdown() |
| 156 | server.server_close() |
| 157 | |
| 158 | @nottest |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 159 | def get_cord_test_loc(): |
| 160 | host = os.getenv('CORD_TEST_HOST', CORD_TEST_HOST) |
| 161 | port = int(os.getenv('CORD_TEST_PORT', CORD_TEST_PORT)) |
| 162 | return host, port |
| 163 | |
| 164 | def rpc_server_instance(): |
| 165 | '''Stateless''' |
| 166 | host, port = get_cord_test_loc() |
| 167 | rpc_server = 'http://{}:{}'.format(host, port) |
| 168 | return xmlrpclib.Server(rpc_server, allow_none = True) |
| 169 | |
| 170 | @nottest |
| 171 | def __cord_test_onos_restart(**kwargs): |
| 172 | return rpc_server_instance().restart_onos(kwargs) |
| 173 | |
| 174 | @nottest |
A R Karthick | 889d965 | 2016-10-03 14:13:45 -0700 | [diff] [blame] | 175 | def cord_test_onos_restart(node = None, config = None): |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 176 | '''Send ONOS restart to server''' |
A R Karthick | 889d965 | 2016-10-03 14:13:45 -0700 | [diff] [blame] | 177 | data = __cord_test_onos_restart(node = node, config = config) |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 178 | if data == 'DONE': |
| 179 | return True |
| 180 | return False |
| 181 | |
| 182 | @nottest |
A.R Karthick | 1700e0e | 2016-10-06 18:16:57 -0700 | [diff] [blame] | 183 | def __cord_test_onos_shutdown(**kwargs): |
| 184 | return rpc_server_instance().shutdown_onos(kwargs) |
| 185 | |
| 186 | @nottest |
| 187 | def cord_test_onos_shutdown(node = None): |
| 188 | data = __cord_test_onos_shutdown(node = node) |
| 189 | if data == 'DONE': |
| 190 | return True |
| 191 | return False |
| 192 | |
| 193 | @nottest |
A R Karthick | e2c24bd | 2016-10-07 14:51:38 -0700 | [diff] [blame] | 194 | def __cord_test_onos_add_cluster(**kwargs): |
| 195 | return rpc_server_instance().add_cluster_onos(kwargs) |
| 196 | |
| 197 | @nottest |
| 198 | def cord_test_onos_add_cluster(count = 1, config = None): |
| 199 | data = __cord_test_onos_add_cluster(count = count, config = config) |
| 200 | if data == 'DONE': |
| 201 | return True |
| 202 | return False |
| 203 | |
| 204 | @nottest |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 205 | def __cord_test_quagga_restart(**kwargs): |
| 206 | return rpc_server_instance().restart_quagga(kwargs) |
| 207 | |
| 208 | @nottest |
| 209 | def cord_test_quagga_restart(config = None, boot_delay = 30): |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 210 | '''Send QUAGGA restart to server''' |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 211 | data = __cord_test_quagga_restart(config = config, boot_delay = boot_delay) |
| 212 | if data == 'DONE': |
| 213 | return True |
| 214 | return False |
| 215 | |
| 216 | @nottest |
A R Karthick | c3d80e2 | 2016-06-22 17:51:24 -0700 | [diff] [blame] | 217 | def __cord_test_quagga_shell(**kwargs): |
| 218 | return rpc_server_instance().run_shell_quagga(kwargs) |
| 219 | |
| 220 | @nottest |
| 221 | def cord_test_quagga_shell(cmd = None): |
| 222 | '''Send QUAGGA shell cmd to server''' |
| 223 | return __cord_test_quagga_shell(cmd = cmd) |
| 224 | |
| 225 | @nottest |
A R Karthick | a013a27 | 2016-08-16 16:40:19 -0700 | [diff] [blame] | 226 | def __cord_test_shell(**kwargs): |
| 227 | return rpc_server_instance().run_shell(kwargs) |
| 228 | |
| 229 | @nottest |
| 230 | def cord_test_shell(cmd = None): |
| 231 | '''Send shell cmd to run remotely''' |
| 232 | return __cord_test_shell(cmd = cmd) |
| 233 | |
| 234 | @nottest |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 235 | def cord_test_quagga_stop(): |
| 236 | data = rpc_server_instance().stop_quagga() |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 237 | if data == 'DONE': |
| 238 | return True |
| 239 | return False |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 240 | |
| 241 | @nottest |
| 242 | def cord_test_radius_restart(): |
| 243 | '''Send Radius server restart to server''' |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 244 | data = rpc_server_instance().restart_radius() |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 245 | if data == 'DONE': |
| 246 | return True |
| 247 | return False |
A R Karthick | e99ab5c | 2016-09-30 13:59:57 -0700 | [diff] [blame] | 248 | |
| 249 | @nottest |
| 250 | def cord_test_server_shutdown(host, port): |
| 251 | '''Shutdown the cord test server''' |
| 252 | rpc_server = 'http://{}:{}'.format(host, port) |
| 253 | try: |
| 254 | xmlrpclib.Server(rpc_server, allow_none = True).shutdown() |
| 255 | except: pass |
| 256 | |
| 257 | return True |