blob: 3b97814e48218a66739874620bd98ff5afe8fb05 [file] [log] [blame]
A R Karthick81acbff2016-06-17 14:45:16 -07001#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002# 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 Karthick81acbff2016-06-17 14:45:16 -07007#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07008# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick81acbff2016-06-17 14:45:16 -07009#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070010# 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 Karthickd44cea12016-07-20 12:16:41 -070016from CordContainer import Container, Onos, OnosCord, Quagga, Radius, reinitContainerClients
Chetan Gaonker3533faa2016-04-25 17:50:14 -070017from nose.tools import nottest
A R Karthick81acbff2016-06-17 14:45:16 -070018from SimpleXMLRPCServer import SimpleXMLRPCServer
19import daemon
20import xmlrpclib
21import os
22import json
23import time
24import threading
Chetan Gaonker3533faa2016-04-25 17:50:14 -070025
A R Karthick81acbff2016-06-17 14:45:16 -070026##Server to handle container restart/stop requests from test container.
Chetan Gaonker3533faa2016-04-25 17:50:14 -070027##Used now to restart ONOS from vrouter test container
28
29CORD_TEST_HOST = '172.17.0.1'
30CORD_TEST_PORT = 25000
31
A R Karthick81acbff2016-06-17 14:45:16 -070032class 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 Gaonker3533faa2016-04-25 17:50:14 -070037
A R Karthick81acbff2016-06-17 14:45:16 -070038class CordTestServer(object):
39
A R Karthickbd9b8a32016-07-21 09:56:45 -070040 onos_cord = None
41
A R Karthick81acbff2016-06-17 14:45:16 -070042 def __restart_onos(self, config = None):
A R Karthickbd9b8a32016-07-21 09:56:45 -070043 if self.onos_cord:
A R Karthickd44cea12016-07-20 12:16:41 -070044 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 Karthick81acbff2016-06-17 14:45:16 -070047 if config is None:
48 try:
49 os.unlink(onos_config)
50 except:
51 pass
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070052 print('Restarting ONOS')
A R Karthickbd9b8a32016-07-21 09:56:45 -070053 if self.onos_cord:
54 self.onos_cord.start(restart = True, network_cfg = config)
A R Karthickd44cea12016-07-20 12:16:41 -070055 else:
56 Onos(restart = True, network_cfg = config)
A R Karthick81acbff2016-06-17 14:45:16 -070057 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070058
A R Karthick81acbff2016-06-17 14:45:16 -070059 def restart_onos(self, kwargs):
60 return self.__restart_onos(**kwargs)
61
62 def __restart_quagga(self, config = None, boot_delay = 30 ):
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070063 config_file = Quagga.quagga_config_file
A R Karthick81acbff2016-06-17 14:45:16 -070064 if config is not None:
65 quagga_config = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config)
66 config_file = '{}/testrib_gen.conf'.format(Quagga.guest_quagga_config)
67 with open(quagga_config, 'w+') as fd:
68 fd.write(str(config))
69 print('Restarting QUAGGA with config file %s, delay %d' %(config_file, boot_delay))
70 Quagga(restart = True, config_file = config_file, boot_delay = boot_delay)
71 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070072
A R Karthick81acbff2016-06-17 14:45:16 -070073 def restart_quagga(self, kwargs):
74 return self.__restart_quagga(**kwargs)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -070075
A R Karthick81acbff2016-06-17 14:45:16 -070076 def stop_quagga(self):
77 quaggaStop = QuaggaStopWrapper()
A R Karthick4a2362c2016-06-22 17:32:44 -070078 time.sleep(5)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070079 try:
A R Karthick81acbff2016-06-17 14:45:16 -070080 quagga_config_gen = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config)
81 os.unlink(quagga_config_gen)
82 except: pass
83 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070084
A R Karthickc3d80e22016-06-22 17:51:24 -070085 def __run_shell_quagga(self, cmd = None):
86 ret = 0
87 if cmd is not None:
88 exec_cmd = 'docker exec {} {}'.format(Quagga.NAME, cmd)
89 ret = os.system(exec_cmd)
90 return ret
91
A R Karthicka013a272016-08-16 16:40:19 -070092 def __run_shell(self, cmd = None):
93 ret = 0
94 if cmd is not None:
95 ret = os.system(cmd)
96 return ret
97
A R Karthickc3d80e22016-06-22 17:51:24 -070098 def run_shell_quagga(self, kwargs):
99 return self.__run_shell_quagga(**kwargs)
100
A R Karthicka013a272016-08-16 16:40:19 -0700101 def run_shell(self, kwargs):
102 return self.__run_shell(**kwargs)
103
A R Karthick81acbff2016-06-17 14:45:16 -0700104 def restart_radius(self):
105 print('Restarting RADIUS Server')
106 Radius(restart = True)
107 return 'DONE'
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700108
109@nottest
A R Karthickd44cea12016-07-20 12:16:41 -0700110def cord_test_server_start(daemonize = True, cord_test_host = CORD_TEST_HOST,
111 cord_test_port = CORD_TEST_PORT, onos_cord = None):
A R Karthick81acbff2016-06-17 14:45:16 -0700112 server = SimpleXMLRPCServer( (cord_test_host, cord_test_port) )
113 server.register_instance(CordTestServer())
A R Karthickbd9b8a32016-07-21 09:56:45 -0700114 CordTestServer.onos_cord = onos_cord
A R Karthick81acbff2016-06-17 14:45:16 -0700115 if daemonize is True:
116 d = daemon.DaemonContext(files_preserve = [server],
117 detach_process = True)
118 with d:
119 reinitContainerClients()
120 server.serve_forever()
121 else:
122 task = threading.Thread(target = server.serve_forever)
123 ##terminate when main thread exits
124 task.daemon = True
125 task.start()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700126 return server
127
128@nottest
129def cord_test_server_stop(server):
130 server.shutdown()
131 server.server_close()
132
133@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700134def get_cord_test_loc():
135 host = os.getenv('CORD_TEST_HOST', CORD_TEST_HOST)
136 port = int(os.getenv('CORD_TEST_PORT', CORD_TEST_PORT))
137 return host, port
138
139def rpc_server_instance():
140 '''Stateless'''
141 host, port = get_cord_test_loc()
142 rpc_server = 'http://{}:{}'.format(host, port)
143 return xmlrpclib.Server(rpc_server, allow_none = True)
144
145@nottest
146def __cord_test_onos_restart(**kwargs):
147 return rpc_server_instance().restart_onos(kwargs)
148
149@nottest
150def cord_test_onos_restart(config = None):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700151 '''Send ONOS restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700152 data = __cord_test_onos_restart(config = config)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700153 if data == 'DONE':
154 return True
155 return False
156
157@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700158def __cord_test_quagga_restart(**kwargs):
159 return rpc_server_instance().restart_quagga(kwargs)
160
161@nottest
162def cord_test_quagga_restart(config = None, boot_delay = 30):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700163 '''Send QUAGGA restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700164 data = __cord_test_quagga_restart(config = config, boot_delay = boot_delay)
165 if data == 'DONE':
166 return True
167 return False
168
169@nottest
A R Karthickc3d80e22016-06-22 17:51:24 -0700170def __cord_test_quagga_shell(**kwargs):
171 return rpc_server_instance().run_shell_quagga(kwargs)
172
173@nottest
174def cord_test_quagga_shell(cmd = None):
175 '''Send QUAGGA shell cmd to server'''
176 return __cord_test_quagga_shell(cmd = cmd)
177
178@nottest
A R Karthicka013a272016-08-16 16:40:19 -0700179def __cord_test_shell(**kwargs):
180 return rpc_server_instance().run_shell(kwargs)
181
182@nottest
183def cord_test_shell(cmd = None):
184 '''Send shell cmd to run remotely'''
185 return __cord_test_shell(cmd = cmd)
186
187@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700188def cord_test_quagga_stop():
189 data = rpc_server_instance().stop_quagga()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700190 if data == 'DONE':
191 return True
192 return False
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700193
194@nottest
195def cord_test_radius_restart():
196 '''Send Radius server restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700197 data = rpc_server_instance().restart_radius()
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700198 if data == 'DONE':
199 return True
200 return False