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