blob: c302d8cac1dee88a9fd8a23f70b7afe55ac5cde5 [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 Karthick1700e0e2016-10-06 18:16:57 -070016from CordContainer import Container, Onos, OnosStopWrapper, OnosCord, OnosCordStopWrapper, Quagga, QuaggaStopWrapper, 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
A R Karthick1878c4f2016-11-29 09:19:50 -080019from resource import getrlimit, RLIMIT_NOFILE
A R Karthick81acbff2016-06-17 14:45:16 -070020import daemon
21import xmlrpclib
22import os
A R Karthicke99ab5c2016-09-30 13:59:57 -070023import signal
A R Karthick81acbff2016-06-17 14:45:16 -070024import json
25import time
26import threading
Chetan Gaonker3533faa2016-04-25 17:50:14 -070027
A R Karthick81acbff2016-06-17 14:45:16 -070028##Server to handle container restart/stop requests from test container.
Chetan Gaonker3533faa2016-04-25 17:50:14 -070029##Used now to restart ONOS from vrouter test container
30
31CORD_TEST_HOST = '172.17.0.1'
32CORD_TEST_PORT = 25000
33
A R Karthick81acbff2016-06-17 14:45:16 -070034class CordTestServer(object):
35
A R Karthickbd9b8a32016-07-21 09:56:45 -070036 onos_cord = None
37
A R Karthickde6b9dc2016-11-29 17:46:16 -080038 def __restart_onos(self, node = None, config = None, timeout = 10):
A R Karthickbd9b8a32016-07-21 09:56:45 -070039 if self.onos_cord:
A R Karthick928ad622017-01-30 12:18:32 -080040 onos_config = '{}/network-cfg.json'.format(self.onos_cord.onos_config_dir)
A R Karthickd44cea12016-07-20 12:16:41 -070041 else:
42 onos_config = '{}/network-cfg.json'.format(Onos.host_config_dir)
A R Karthick81acbff2016-06-17 14:45:16 -070043 if config is None:
44 try:
45 os.unlink(onos_config)
46 except:
47 pass
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070048 print('Restarting ONOS')
A R Karthickbd9b8a32016-07-21 09:56:45 -070049 if self.onos_cord:
50 self.onos_cord.start(restart = True, network_cfg = config)
A R Karthickd44cea12016-07-20 12:16:41 -070051 else:
A R Karthickde6b9dc2016-11-29 17:46:16 -080052 Onos.restart_node(node = node, network_cfg = config, timeout = timeout)
A R Karthick81acbff2016-06-17 14:45:16 -070053 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070054
A R Karthick81acbff2016-06-17 14:45:16 -070055 def restart_onos(self, kwargs):
56 return self.__restart_onos(**kwargs)
57
A.R Karthick1700e0e2016-10-06 18:16:57 -070058 def __shutdown_onos(self, node = None):
59 if node is None:
60 node = Onos.NAME
61 OnosStopWrapper(node)
62 return 'DONE'
63
64 def shutdown_onos(self, kwargs):
65 return self.__shutdown_onos(**kwargs)
66
A.R Karthick2560f042016-11-30 14:38:52 -080067 def __restart_cluster(self, config = None, timeout = 10, setup = False):
68 Onos.restart_cluster(network_cfg = config, timeout = timeout, setup = setup)
69 return 'DONE'
70
71 def restart_cluster(self, kwargs):
72 return self.__restart_cluster(**kwargs)
73
A R Karthicke2c24bd2016-10-07 14:51:38 -070074 def __add_cluster_onos(self, count = 1, config = None):
75 Onos.add_cluster(count = count, network_cfg = config)
76 return 'DONE'
77
78 def add_cluster_onos(self, kwargs):
A R Karthickdb59cf72016-10-10 10:43:22 -070079 return self.__add_cluster_onos(**kwargs)
A R Karthicke2c24bd2016-10-07 14:51:38 -070080
A R Karthick81acbff2016-06-17 14:45:16 -070081 def __restart_quagga(self, config = None, boot_delay = 30 ):
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070082 config_file = Quagga.quagga_config_file
A R Karthick81acbff2016-06-17 14:45:16 -070083 if config is not None:
84 quagga_config = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config)
85 config_file = '{}/testrib_gen.conf'.format(Quagga.guest_quagga_config)
86 with open(quagga_config, 'w+') as fd:
87 fd.write(str(config))
88 print('Restarting QUAGGA with config file %s, delay %d' %(config_file, boot_delay))
A R Karthick07608ef2016-08-23 16:51:19 -070089 Quagga(prefix = Container.IMAGE_PREFIX, restart = True, config_file = config_file, boot_delay = boot_delay)
A R Karthick81acbff2016-06-17 14:45:16 -070090 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070091
A R Karthick81acbff2016-06-17 14:45:16 -070092 def restart_quagga(self, kwargs):
93 return self.__restart_quagga(**kwargs)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -070094
A R Karthick81acbff2016-06-17 14:45:16 -070095 def stop_quagga(self):
96 quaggaStop = QuaggaStopWrapper()
A R Karthick4a2362c2016-06-22 17:32:44 -070097 time.sleep(5)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070098 try:
A R Karthick81acbff2016-06-17 14:45:16 -070099 quagga_config_gen = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config)
100 os.unlink(quagga_config_gen)
101 except: pass
102 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700103
A R Karthickc3d80e22016-06-22 17:51:24 -0700104 def __run_shell_quagga(self, cmd = None):
105 ret = 0
106 if cmd is not None:
107 exec_cmd = 'docker exec {} {}'.format(Quagga.NAME, cmd)
108 ret = os.system(exec_cmd)
109 return ret
110
A R Karthicka013a272016-08-16 16:40:19 -0700111 def __run_shell(self, cmd = None):
112 ret = 0
113 if cmd is not None:
114 ret = os.system(cmd)
115 return ret
116
A R Karthickc3d80e22016-06-22 17:51:24 -0700117 def run_shell_quagga(self, kwargs):
118 return self.__run_shell_quagga(**kwargs)
119
A R Karthicka013a272016-08-16 16:40:19 -0700120 def run_shell(self, kwargs):
121 return self.__run_shell(**kwargs)
122
A R Karthick81acbff2016-06-17 14:45:16 -0700123 def restart_radius(self):
124 print('Restarting RADIUS Server')
A R Karthick07608ef2016-08-23 16:51:19 -0700125 Radius(prefix = Container.IMAGE_PREFIX, restart = True)
A R Karthick81acbff2016-06-17 14:45:16 -0700126 return 'DONE'
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700127
A R Karthicke99ab5c2016-09-30 13:59:57 -0700128 def shutdown(self):
129 print('Shutting down cord test server')
130 os.kill(0, signal.SIGKILL)
131 return 'DONE'
132
A R Karthick1878c4f2016-11-29 09:19:50 -0800133def find_files_by_path(*paths):
134 wanted = []
135 for p in paths:
136 try:
137 fd = os.open(p, os.O_RDONLY)
138 wanted.append(os.fstat(fd)[1:3])
139 finally:
140 os.close(fd)
141
142 def fd_wanted(fd):
143 try:
144 return os.fstat(fd)[1:3] in wanted
145 except OSError:
146 return False
147
148 max_fd = getrlimit(RLIMIT_NOFILE)[1]
149 return [ fd for fd in xrange(max_fd) if fd_wanted(fd) ]
150
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700151@nottest
A R Karthickbd82f362016-11-10 15:08:52 -0800152def cord_test_server_start(daemonize = True,
153 cord_test_host = CORD_TEST_HOST,
154 cord_test_port = CORD_TEST_PORT,
155 onos_cord = None,
156 foreground=False):
A R Karthick81acbff2016-06-17 14:45:16 -0700157 server = SimpleXMLRPCServer( (cord_test_host, cord_test_port) )
158 server.register_instance(CordTestServer())
A R Karthickbd9b8a32016-07-21 09:56:45 -0700159 CordTestServer.onos_cord = onos_cord
A R Karthick81acbff2016-06-17 14:45:16 -0700160 if daemonize is True:
A R Karthick1878c4f2016-11-29 09:19:50 -0800161 ##before daemonizing, preserve urandom needed by paramiko
162 preserve_list = find_files_by_path('/dev/urandom')
163 preserve_list.append(server)
164 d = daemon.DaemonContext(files_preserve = preserve_list,
A R Karthick81acbff2016-06-17 14:45:16 -0700165 detach_process = True)
166 with d:
167 reinitContainerClients()
168 server.serve_forever()
169 else:
A R Karthickbd82f362016-11-10 15:08:52 -0800170 if foreground:
171 try:
172 server.serve_forever()
173 except KeyboardInterrupt:
174 return server
175 else:
176 task = threading.Thread(target = server.serve_forever)
177 ##terminate when main thread exits
178 task.daemon = True
179 task.start()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700180 return server
181
182@nottest
183def cord_test_server_stop(server):
184 server.shutdown()
185 server.server_close()
186
187@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700188def get_cord_test_loc():
189 host = os.getenv('CORD_TEST_HOST', CORD_TEST_HOST)
190 port = int(os.getenv('CORD_TEST_PORT', CORD_TEST_PORT))
191 return host, port
192
193def rpc_server_instance():
194 '''Stateless'''
195 host, port = get_cord_test_loc()
196 rpc_server = 'http://{}:{}'.format(host, port)
197 return xmlrpclib.Server(rpc_server, allow_none = True)
198
199@nottest
200def __cord_test_onos_restart(**kwargs):
201 return rpc_server_instance().restart_onos(kwargs)
202
203@nottest
A R Karthickde6b9dc2016-11-29 17:46:16 -0800204def cord_test_onos_restart(node = None, config = None, timeout = 10):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700205 '''Send ONOS restart to server'''
A R Karthick6cc8b812016-12-09 10:24:40 -0800206 for i in range(3):
207 try:
208 data = __cord_test_onos_restart(node = node, config = config, timeout = timeout)
209 if data == 'DONE':
210 return True
211 except:
212 time.sleep(2)
213
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700214 return False
215
216@nottest
A.R Karthick1700e0e2016-10-06 18:16:57 -0700217def __cord_test_onos_shutdown(**kwargs):
218 return rpc_server_instance().shutdown_onos(kwargs)
219
220@nottest
221def cord_test_onos_shutdown(node = None):
222 data = __cord_test_onos_shutdown(node = node)
223 if data == 'DONE':
224 return True
225 return False
226
227@nottest
A.R Karthick2560f042016-11-30 14:38:52 -0800228def __cord_test_restart_cluster(**kwargs):
229 return rpc_server_instance().restart_cluster(kwargs)
230
231@nottest
232def cord_test_restart_cluster(config = None, timeout = 10, setup = False):
A R Karthick6cc8b812016-12-09 10:24:40 -0800233 for i in range(3):
234 try:
235 data = __cord_test_restart_cluster(config = config, timeout = timeout, setup = setup)
236 if data == 'DONE':
237 return True
238 except:
239 time.sleep(2)
240
A.R Karthick2560f042016-11-30 14:38:52 -0800241 return False
242
243@nottest
A R Karthicke2c24bd2016-10-07 14:51:38 -0700244def __cord_test_onos_add_cluster(**kwargs):
245 return rpc_server_instance().add_cluster_onos(kwargs)
246
247@nottest
248def cord_test_onos_add_cluster(count = 1, config = None):
249 data = __cord_test_onos_add_cluster(count = count, config = config)
250 if data == 'DONE':
251 return True
252 return False
253
254@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700255def __cord_test_quagga_restart(**kwargs):
256 return rpc_server_instance().restart_quagga(kwargs)
257
258@nottest
259def cord_test_quagga_restart(config = None, boot_delay = 30):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700260 '''Send QUAGGA restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700261 data = __cord_test_quagga_restart(config = config, boot_delay = boot_delay)
262 if data == 'DONE':
263 return True
264 return False
265
266@nottest
A R Karthickc3d80e22016-06-22 17:51:24 -0700267def __cord_test_quagga_shell(**kwargs):
268 return rpc_server_instance().run_shell_quagga(kwargs)
269
270@nottest
271def cord_test_quagga_shell(cmd = None):
272 '''Send QUAGGA shell cmd to server'''
273 return __cord_test_quagga_shell(cmd = cmd)
274
275@nottest
A R Karthicka013a272016-08-16 16:40:19 -0700276def __cord_test_shell(**kwargs):
277 return rpc_server_instance().run_shell(kwargs)
278
279@nottest
280def cord_test_shell(cmd = None):
281 '''Send shell cmd to run remotely'''
282 return __cord_test_shell(cmd = cmd)
283
284@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700285def cord_test_quagga_stop():
286 data = rpc_server_instance().stop_quagga()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700287 if data == 'DONE':
288 return True
289 return False
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700290
291@nottest
292def cord_test_radius_restart():
293 '''Send Radius server restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700294 data = rpc_server_instance().restart_radius()
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700295 if data == 'DONE':
296 return True
297 return False
A R Karthicke99ab5c2016-09-30 13:59:57 -0700298
299@nottest
300def cord_test_server_shutdown(host, port):
301 '''Shutdown the cord test server'''
302 rpc_server = 'http://{}:{}'.format(host, port)
303 try:
304 xmlrpclib.Server(rpc_server, allow_none = True).shutdown()
305 except: pass
306
307 return True