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