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