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 | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 16 | from CordContainer import Container, Onos, 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): |
| 33 | def __init__(self, name = Quagga.NAME, image = Quagga.IMAGE, tag = 'latest'): |
| 34 | super(QuaggaStopWrapper, self).__init__(name, image, tag = tag) |
| 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 | |
| 40 | def __restart_onos(self, config = None): |
| 41 | onos_config = '{}/network-cfg.json'.format(Onos.host_config_dir) |
| 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 | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 48 | Onos(restart = True, network_cfg = config) |
| 49 | return 'DONE' |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 50 | |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 51 | def restart_onos(self, kwargs): |
| 52 | return self.__restart_onos(**kwargs) |
| 53 | |
| 54 | def __restart_quagga(self, config = None, boot_delay = 30 ): |
Chetan Gaonker | fd3d650 | 2016-05-03 13:23:07 -0700 | [diff] [blame] | 55 | config_file = Quagga.quagga_config_file |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 56 | if config is not None: |
| 57 | quagga_config = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config) |
| 58 | config_file = '{}/testrib_gen.conf'.format(Quagga.guest_quagga_config) |
| 59 | with open(quagga_config, 'w+') as fd: |
| 60 | fd.write(str(config)) |
| 61 | print('Restarting QUAGGA with config file %s, delay %d' %(config_file, boot_delay)) |
| 62 | Quagga(restart = True, config_file = config_file, boot_delay = boot_delay) |
| 63 | return 'DONE' |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 64 | |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 65 | def restart_quagga(self, kwargs): |
| 66 | return self.__restart_quagga(**kwargs) |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 67 | |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 68 | def stop_quagga(self): |
| 69 | quaggaStop = QuaggaStopWrapper() |
A R Karthick | 4a2362c | 2016-06-22 17:32:44 -0700 | [diff] [blame^] | 70 | time.sleep(5) |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 71 | try: |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 72 | quagga_config_gen = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config) |
| 73 | os.unlink(quagga_config_gen) |
| 74 | except: pass |
| 75 | return 'DONE' |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 76 | |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 77 | def restart_radius(self): |
| 78 | print('Restarting RADIUS Server') |
| 79 | Radius(restart = True) |
| 80 | return 'DONE' |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 81 | |
| 82 | @nottest |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 83 | def cord_test_server_start(daemonize = True, cord_test_host = CORD_TEST_HOST, cord_test_port = CORD_TEST_PORT): |
| 84 | server = SimpleXMLRPCServer( (cord_test_host, cord_test_port) ) |
| 85 | server.register_instance(CordTestServer()) |
| 86 | if daemonize is True: |
| 87 | d = daemon.DaemonContext(files_preserve = [server], |
| 88 | detach_process = True) |
| 89 | with d: |
| 90 | reinitContainerClients() |
| 91 | server.serve_forever() |
| 92 | else: |
| 93 | task = threading.Thread(target = server.serve_forever) |
| 94 | ##terminate when main thread exits |
| 95 | task.daemon = True |
| 96 | task.start() |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 97 | return server |
| 98 | |
| 99 | @nottest |
| 100 | def cord_test_server_stop(server): |
| 101 | server.shutdown() |
| 102 | server.server_close() |
| 103 | |
| 104 | @nottest |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 105 | def get_cord_test_loc(): |
| 106 | host = os.getenv('CORD_TEST_HOST', CORD_TEST_HOST) |
| 107 | port = int(os.getenv('CORD_TEST_PORT', CORD_TEST_PORT)) |
| 108 | return host, port |
| 109 | |
| 110 | def rpc_server_instance(): |
| 111 | '''Stateless''' |
| 112 | host, port = get_cord_test_loc() |
| 113 | rpc_server = 'http://{}:{}'.format(host, port) |
| 114 | return xmlrpclib.Server(rpc_server, allow_none = True) |
| 115 | |
| 116 | @nottest |
| 117 | def __cord_test_onos_restart(**kwargs): |
| 118 | return rpc_server_instance().restart_onos(kwargs) |
| 119 | |
| 120 | @nottest |
| 121 | def cord_test_onos_restart(config = None): |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 122 | '''Send ONOS restart to server''' |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 123 | data = __cord_test_onos_restart(config = config) |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 124 | if data == 'DONE': |
| 125 | return True |
| 126 | return False |
| 127 | |
| 128 | @nottest |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 129 | def __cord_test_quagga_restart(**kwargs): |
| 130 | return rpc_server_instance().restart_quagga(kwargs) |
| 131 | |
| 132 | @nottest |
| 133 | def cord_test_quagga_restart(config = None, boot_delay = 30): |
Chetan Gaonker | 6cf6e47 | 2016-04-26 14:41:51 -0700 | [diff] [blame] | 134 | '''Send QUAGGA restart to server''' |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 135 | data = __cord_test_quagga_restart(config = config, boot_delay = boot_delay) |
| 136 | if data == 'DONE': |
| 137 | return True |
| 138 | return False |
| 139 | |
| 140 | @nottest |
| 141 | def cord_test_quagga_stop(): |
| 142 | data = rpc_server_instance().stop_quagga() |
Chetan Gaonker | 3533faa | 2016-04-25 17:50:14 -0700 | [diff] [blame] | 143 | if data == 'DONE': |
| 144 | return True |
| 145 | return False |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 146 | |
| 147 | @nottest |
| 148 | def cord_test_radius_restart(): |
| 149 | '''Send Radius server restart to server''' |
A R Karthick | 81acbff | 2016-06-17 14:45:16 -0700 | [diff] [blame] | 150 | data = rpc_server_instance().restart_radius() |
Chetan Gaonker | 7f4bf74 | 2016-05-04 15:56:08 -0700 | [diff] [blame] | 151 | if data == 'DONE': |
| 152 | return True |
| 153 | return False |