blob: 9a734b7dd8dfa65bc0b386f7f3808f8c9f0a47fc [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
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
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
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
16
A R Karthick81acbff2016-06-17 14:45:16 -070017#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070018# Copyright 2016-present Ciena Corporation
19#
20# Licensed under the Apache License, Version 2.0 (the "License");
21# you may not use this file except in compliance with the License.
22# You may obtain a copy of the License at
A R Karthick81acbff2016-06-17 14:45:16 -070023#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070024# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick81acbff2016-06-17 14:45:16 -070025#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070026# Unless required by applicable law or agreed to in writing, software
27# distributed under the License is distributed on an "AS IS" BASIS,
28# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29# See the License for the specific language governing permissions and
30# limitations under the License.
31#
A.R Karthick1700e0e2016-10-06 18:16:57 -070032from CordContainer import Container, Onos, OnosStopWrapper, OnosCord, OnosCordStopWrapper, Quagga, QuaggaStopWrapper, Radius, reinitContainerClients
Chetan Gaonker3533faa2016-04-25 17:50:14 -070033from nose.tools import nottest
A R Karthick81acbff2016-06-17 14:45:16 -070034from SimpleXMLRPCServer import SimpleXMLRPCServer
A R Karthick1878c4f2016-11-29 09:19:50 -080035from resource import getrlimit, RLIMIT_NOFILE
A R Karthick81acbff2016-06-17 14:45:16 -070036import daemon
37import xmlrpclib
38import os
A R Karthicke99ab5c2016-09-30 13:59:57 -070039import signal
A R Karthick81acbff2016-06-17 14:45:16 -070040import json
41import time
42import threading
Chetan Gaonker3533faa2016-04-25 17:50:14 -070043
A R Karthick81acbff2016-06-17 14:45:16 -070044##Server to handle container restart/stop requests from test container.
Chetan Gaonker3533faa2016-04-25 17:50:14 -070045##Used now to restart ONOS from vrouter test container
46
47CORD_TEST_HOST = '172.17.0.1'
48CORD_TEST_PORT = 25000
49
A R Karthick81acbff2016-06-17 14:45:16 -070050class CordTestServer(object):
51
A R Karthickbd9b8a32016-07-21 09:56:45 -070052 onos_cord = None
53
A R Karthickde6b9dc2016-11-29 17:46:16 -080054 def __restart_onos(self, node = None, config = None, timeout = 10):
A R Karthickbd9b8a32016-07-21 09:56:45 -070055 if self.onos_cord:
A R Karthick928ad622017-01-30 12:18:32 -080056 onos_config = '{}/network-cfg.json'.format(self.onos_cord.onos_config_dir)
A R Karthickd44cea12016-07-20 12:16:41 -070057 else:
58 onos_config = '{}/network-cfg.json'.format(Onos.host_config_dir)
A R Karthick81acbff2016-06-17 14:45:16 -070059 if config is None:
60 try:
61 os.unlink(onos_config)
62 except:
63 pass
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070064 print('Restarting ONOS')
A R Karthickbd9b8a32016-07-21 09:56:45 -070065 if self.onos_cord:
66 self.onos_cord.start(restart = True, network_cfg = config)
A R Karthickd44cea12016-07-20 12:16:41 -070067 else:
A R Karthickde6b9dc2016-11-29 17:46:16 -080068 Onos.restart_node(node = node, network_cfg = config, timeout = timeout)
A R Karthick81acbff2016-06-17 14:45:16 -070069 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070070
A R Karthick81acbff2016-06-17 14:45:16 -070071 def restart_onos(self, kwargs):
72 return self.__restart_onos(**kwargs)
73
A.R Karthick1700e0e2016-10-06 18:16:57 -070074 def __shutdown_onos(self, node = None):
75 if node is None:
76 node = Onos.NAME
77 OnosStopWrapper(node)
78 return 'DONE'
79
80 def shutdown_onos(self, kwargs):
81 return self.__shutdown_onos(**kwargs)
82
A.R Karthick2560f042016-11-30 14:38:52 -080083 def __restart_cluster(self, config = None, timeout = 10, setup = False):
84 Onos.restart_cluster(network_cfg = config, timeout = timeout, setup = setup)
85 return 'DONE'
86
87 def restart_cluster(self, kwargs):
88 return self.__restart_cluster(**kwargs)
89
A R Karthicke2c24bd2016-10-07 14:51:38 -070090 def __add_cluster_onos(self, count = 1, config = None):
91 Onos.add_cluster(count = count, network_cfg = config)
92 return 'DONE'
93
94 def add_cluster_onos(self, kwargs):
A R Karthickdb59cf72016-10-10 10:43:22 -070095 return self.__add_cluster_onos(**kwargs)
A R Karthicke2c24bd2016-10-07 14:51:38 -070096
A R Karthick81acbff2016-06-17 14:45:16 -070097 def __restart_quagga(self, config = None, boot_delay = 30 ):
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070098 config_file = Quagga.quagga_config_file
A R Karthick81acbff2016-06-17 14:45:16 -070099 if config is not None:
100 quagga_config = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config)
101 config_file = '{}/testrib_gen.conf'.format(Quagga.guest_quagga_config)
102 with open(quagga_config, 'w+') as fd:
103 fd.write(str(config))
104 print('Restarting QUAGGA with config file %s, delay %d' %(config_file, boot_delay))
A R Karthick07608ef2016-08-23 16:51:19 -0700105 Quagga(prefix = Container.IMAGE_PREFIX, restart = True, config_file = config_file, boot_delay = boot_delay)
A R Karthick81acbff2016-06-17 14:45:16 -0700106 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700107
A R Karthick81acbff2016-06-17 14:45:16 -0700108 def restart_quagga(self, kwargs):
109 return self.__restart_quagga(**kwargs)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700110
A R Karthick81acbff2016-06-17 14:45:16 -0700111 def stop_quagga(self):
112 quaggaStop = QuaggaStopWrapper()
A R Karthick4a2362c2016-06-22 17:32:44 -0700113 time.sleep(5)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700114 try:
A R Karthick81acbff2016-06-17 14:45:16 -0700115 quagga_config_gen = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config)
116 os.unlink(quagga_config_gen)
117 except: pass
118 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700119
A R Karthickc3d80e22016-06-22 17:51:24 -0700120 def __run_shell_quagga(self, cmd = None):
121 ret = 0
122 if cmd is not None:
123 exec_cmd = 'docker exec {} {}'.format(Quagga.NAME, cmd)
124 ret = os.system(exec_cmd)
125 return ret
126
A R Karthicka013a272016-08-16 16:40:19 -0700127 def __run_shell(self, cmd = None):
128 ret = 0
129 if cmd is not None:
130 ret = os.system(cmd)
131 return ret
132
A R Karthickc3d80e22016-06-22 17:51:24 -0700133 def run_shell_quagga(self, kwargs):
134 return self.__run_shell_quagga(**kwargs)
135
A R Karthicka013a272016-08-16 16:40:19 -0700136 def run_shell(self, kwargs):
137 return self.__run_shell(**kwargs)
138
A R Karthick81acbff2016-06-17 14:45:16 -0700139 def restart_radius(self):
140 print('Restarting RADIUS Server')
A R Karthick07608ef2016-08-23 16:51:19 -0700141 Radius(prefix = Container.IMAGE_PREFIX, restart = True)
A R Karthick81acbff2016-06-17 14:45:16 -0700142 return 'DONE'
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700143
A R Karthicke99ab5c2016-09-30 13:59:57 -0700144 def shutdown(self):
145 print('Shutting down cord test server')
146 os.kill(0, signal.SIGKILL)
147 return 'DONE'
148
A R Karthick1878c4f2016-11-29 09:19:50 -0800149def find_files_by_path(*paths):
150 wanted = []
151 for p in paths:
152 try:
153 fd = os.open(p, os.O_RDONLY)
154 wanted.append(os.fstat(fd)[1:3])
155 finally:
156 os.close(fd)
157
158 def fd_wanted(fd):
159 try:
160 return os.fstat(fd)[1:3] in wanted
161 except OSError:
162 return False
163
164 max_fd = getrlimit(RLIMIT_NOFILE)[1]
165 return [ fd for fd in xrange(max_fd) if fd_wanted(fd) ]
166
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700167@nottest
A R Karthickbd82f362016-11-10 15:08:52 -0800168def cord_test_server_start(daemonize = True,
169 cord_test_host = CORD_TEST_HOST,
170 cord_test_port = CORD_TEST_PORT,
171 onos_cord = None,
172 foreground=False):
A R Karthick81acbff2016-06-17 14:45:16 -0700173 server = SimpleXMLRPCServer( (cord_test_host, cord_test_port) )
174 server.register_instance(CordTestServer())
A R Karthickbd9b8a32016-07-21 09:56:45 -0700175 CordTestServer.onos_cord = onos_cord
A R Karthick81acbff2016-06-17 14:45:16 -0700176 if daemonize is True:
A R Karthick1878c4f2016-11-29 09:19:50 -0800177 ##before daemonizing, preserve urandom needed by paramiko
178 preserve_list = find_files_by_path('/dev/urandom')
179 preserve_list.append(server)
180 d = daemon.DaemonContext(files_preserve = preserve_list,
A R Karthick81acbff2016-06-17 14:45:16 -0700181 detach_process = True)
182 with d:
183 reinitContainerClients()
184 server.serve_forever()
185 else:
A R Karthickbd82f362016-11-10 15:08:52 -0800186 if foreground:
187 try:
188 server.serve_forever()
189 except KeyboardInterrupt:
190 return server
191 else:
192 task = threading.Thread(target = server.serve_forever)
193 ##terminate when main thread exits
194 task.daemon = True
195 task.start()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700196 return server
197
198@nottest
199def cord_test_server_stop(server):
200 server.shutdown()
201 server.server_close()
202
203@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700204def get_cord_test_loc():
205 host = os.getenv('CORD_TEST_HOST', CORD_TEST_HOST)
206 port = int(os.getenv('CORD_TEST_PORT', CORD_TEST_PORT))
207 return host, port
208
209def rpc_server_instance():
210 '''Stateless'''
211 host, port = get_cord_test_loc()
212 rpc_server = 'http://{}:{}'.format(host, port)
213 return xmlrpclib.Server(rpc_server, allow_none = True)
214
215@nottest
216def __cord_test_onos_restart(**kwargs):
217 return rpc_server_instance().restart_onos(kwargs)
218
219@nottest
A R Karthickde6b9dc2016-11-29 17:46:16 -0800220def cord_test_onos_restart(node = None, config = None, timeout = 10):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700221 '''Send ONOS restart to server'''
A R Karthick6cc8b812016-12-09 10:24:40 -0800222 for i in range(3):
223 try:
224 data = __cord_test_onos_restart(node = node, config = config, timeout = timeout)
225 if data == 'DONE':
226 return True
227 except:
228 time.sleep(2)
229
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700230 return False
231
232@nottest
A.R Karthick1700e0e2016-10-06 18:16:57 -0700233def __cord_test_onos_shutdown(**kwargs):
234 return rpc_server_instance().shutdown_onos(kwargs)
235
236@nottest
237def cord_test_onos_shutdown(node = None):
238 data = __cord_test_onos_shutdown(node = node)
239 if data == 'DONE':
240 return True
241 return False
242
243@nottest
A.R Karthick2560f042016-11-30 14:38:52 -0800244def __cord_test_restart_cluster(**kwargs):
245 return rpc_server_instance().restart_cluster(kwargs)
246
247@nottest
248def cord_test_restart_cluster(config = None, timeout = 10, setup = False):
A R Karthick6cc8b812016-12-09 10:24:40 -0800249 for i in range(3):
250 try:
251 data = __cord_test_restart_cluster(config = config, timeout = timeout, setup = setup)
252 if data == 'DONE':
253 return True
254 except:
255 time.sleep(2)
256
A.R Karthick2560f042016-11-30 14:38:52 -0800257 return False
258
259@nottest
A R Karthicke2c24bd2016-10-07 14:51:38 -0700260def __cord_test_onos_add_cluster(**kwargs):
261 return rpc_server_instance().add_cluster_onos(kwargs)
262
263@nottest
264def cord_test_onos_add_cluster(count = 1, config = None):
265 data = __cord_test_onos_add_cluster(count = count, config = config)
266 if data == 'DONE':
267 return True
268 return False
269
270@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700271def __cord_test_quagga_restart(**kwargs):
272 return rpc_server_instance().restart_quagga(kwargs)
273
274@nottest
275def cord_test_quagga_restart(config = None, boot_delay = 30):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700276 '''Send QUAGGA restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700277 data = __cord_test_quagga_restart(config = config, boot_delay = boot_delay)
278 if data == 'DONE':
279 return True
280 return False
281
282@nottest
A R Karthickc3d80e22016-06-22 17:51:24 -0700283def __cord_test_quagga_shell(**kwargs):
284 return rpc_server_instance().run_shell_quagga(kwargs)
285
286@nottest
287def cord_test_quagga_shell(cmd = None):
288 '''Send QUAGGA shell cmd to server'''
289 return __cord_test_quagga_shell(cmd = cmd)
290
291@nottest
A R Karthicka013a272016-08-16 16:40:19 -0700292def __cord_test_shell(**kwargs):
293 return rpc_server_instance().run_shell(kwargs)
294
295@nottest
296def cord_test_shell(cmd = None):
297 '''Send shell cmd to run remotely'''
298 return __cord_test_shell(cmd = cmd)
299
300@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700301def cord_test_quagga_stop():
302 data = rpc_server_instance().stop_quagga()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700303 if data == 'DONE':
304 return True
305 return False
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700306
307@nottest
308def cord_test_radius_restart():
309 '''Send Radius server restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700310 data = rpc_server_instance().restart_radius()
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700311 if data == 'DONE':
312 return True
313 return False
A R Karthicke99ab5c2016-09-30 13:59:57 -0700314
315@nottest
316def cord_test_server_shutdown(host, port):
317 '''Shutdown the cord test server'''
318 rpc_server = 'http://{}:{}'.format(host, port)
319 try:
320 xmlrpclib.Server(rpc_server, allow_none = True).shutdown()
321 except: pass
322
323 return True