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