blob: de17261a6b974208f6628ed2b632e2245577e046 [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 Karthickd44cea12016-07-20 12:16:41 -070040 onos_config = '{}/network-cfg.json'.format(OnosCord.onos_config_dir)
41 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 Karthickde6b9dc2016-11-29 17:46:16 -0800206 data = __cord_test_onos_restart(node = node, config = config, timeout = timeout)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700207 if data == 'DONE':
208 return True
209 return False
210
211@nottest
A.R Karthick1700e0e2016-10-06 18:16:57 -0700212def __cord_test_onos_shutdown(**kwargs):
213 return rpc_server_instance().shutdown_onos(kwargs)
214
215@nottest
216def cord_test_onos_shutdown(node = None):
217 data = __cord_test_onos_shutdown(node = node)
218 if data == 'DONE':
219 return True
220 return False
221
222@nottest
A.R Karthick2560f042016-11-30 14:38:52 -0800223def __cord_test_restart_cluster(**kwargs):
224 return rpc_server_instance().restart_cluster(kwargs)
225
226@nottest
227def cord_test_restart_cluster(config = None, timeout = 10, setup = False):
228 data = __cord_test_restart_cluster(config = config, timeout = timeout, setup = setup)
229 if data == 'DONE':
230 return True
231 return False
232
233@nottest
A R Karthicke2c24bd2016-10-07 14:51:38 -0700234def __cord_test_onos_add_cluster(**kwargs):
235 return rpc_server_instance().add_cluster_onos(kwargs)
236
237@nottest
238def cord_test_onos_add_cluster(count = 1, config = None):
239 data = __cord_test_onos_add_cluster(count = count, config = config)
240 if data == 'DONE':
241 return True
242 return False
243
244@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700245def __cord_test_quagga_restart(**kwargs):
246 return rpc_server_instance().restart_quagga(kwargs)
247
248@nottest
249def cord_test_quagga_restart(config = None, boot_delay = 30):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700250 '''Send QUAGGA restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700251 data = __cord_test_quagga_restart(config = config, boot_delay = boot_delay)
252 if data == 'DONE':
253 return True
254 return False
255
256@nottest
A R Karthickc3d80e22016-06-22 17:51:24 -0700257def __cord_test_quagga_shell(**kwargs):
258 return rpc_server_instance().run_shell_quagga(kwargs)
259
260@nottest
261def cord_test_quagga_shell(cmd = None):
262 '''Send QUAGGA shell cmd to server'''
263 return __cord_test_quagga_shell(cmd = cmd)
264
265@nottest
A R Karthicka013a272016-08-16 16:40:19 -0700266def __cord_test_shell(**kwargs):
267 return rpc_server_instance().run_shell(kwargs)
268
269@nottest
270def cord_test_shell(cmd = None):
271 '''Send shell cmd to run remotely'''
272 return __cord_test_shell(cmd = cmd)
273
274@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700275def cord_test_quagga_stop():
276 data = rpc_server_instance().stop_quagga()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700277 if data == 'DONE':
278 return True
279 return False
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700280
281@nottest
282def cord_test_radius_restart():
283 '''Send Radius server restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700284 data = rpc_server_instance().restart_radius()
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700285 if data == 'DONE':
286 return True
287 return False
A R Karthicke99ab5c2016-09-30 13:59:57 -0700288
289@nottest
290def cord_test_server_shutdown(host, port):
291 '''Shutdown the cord test server'''
292 rpc_server = 'http://{}:{}'.format(host, port)
293 try:
294 xmlrpclib.Server(rpc_server, allow_none = True).shutdown()
295 except: pass
296
297 return True