blob: 4f097b0418ff247fcfccf3c46f9a568af3591135 [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 Karthick889d9652016-10-03 14:13:45 -070038 def __restart_onos(self, node = None, config = None):
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 Karthick889d9652016-10-03 14:13:45 -070052 Onos.restart_node(node = node, network_cfg = config)
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 Karthicke2c24bd2016-10-07 14:51:38 -070067 def __add_cluster_onos(self, count = 1, config = None):
68 Onos.add_cluster(count = count, network_cfg = config)
69 return 'DONE'
70
71 def add_cluster_onos(self, kwargs):
A R Karthickdb59cf72016-10-10 10:43:22 -070072 return self.__add_cluster_onos(**kwargs)
A R Karthicke2c24bd2016-10-07 14:51:38 -070073
A R Karthick81acbff2016-06-17 14:45:16 -070074 def __restart_quagga(self, config = None, boot_delay = 30 ):
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070075 config_file = Quagga.quagga_config_file
A R Karthick81acbff2016-06-17 14:45:16 -070076 if config is not None:
77 quagga_config = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config)
78 config_file = '{}/testrib_gen.conf'.format(Quagga.guest_quagga_config)
79 with open(quagga_config, 'w+') as fd:
80 fd.write(str(config))
81 print('Restarting QUAGGA with config file %s, delay %d' %(config_file, boot_delay))
A R Karthick07608ef2016-08-23 16:51:19 -070082 Quagga(prefix = Container.IMAGE_PREFIX, restart = True, config_file = config_file, boot_delay = boot_delay)
A R Karthick81acbff2016-06-17 14:45:16 -070083 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070084
A R Karthick81acbff2016-06-17 14:45:16 -070085 def restart_quagga(self, kwargs):
86 return self.__restart_quagga(**kwargs)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -070087
A R Karthick81acbff2016-06-17 14:45:16 -070088 def stop_quagga(self):
89 quaggaStop = QuaggaStopWrapper()
A R Karthick4a2362c2016-06-22 17:32:44 -070090 time.sleep(5)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070091 try:
A R Karthick81acbff2016-06-17 14:45:16 -070092 quagga_config_gen = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config)
93 os.unlink(quagga_config_gen)
94 except: pass
95 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070096
A R Karthickc3d80e22016-06-22 17:51:24 -070097 def __run_shell_quagga(self, cmd = None):
98 ret = 0
99 if cmd is not None:
100 exec_cmd = 'docker exec {} {}'.format(Quagga.NAME, cmd)
101 ret = os.system(exec_cmd)
102 return ret
103
A R Karthicka013a272016-08-16 16:40:19 -0700104 def __run_shell(self, cmd = None):
105 ret = 0
106 if cmd is not None:
107 ret = os.system(cmd)
108 return ret
109
A R Karthickc3d80e22016-06-22 17:51:24 -0700110 def run_shell_quagga(self, kwargs):
111 return self.__run_shell_quagga(**kwargs)
112
A R Karthicka013a272016-08-16 16:40:19 -0700113 def run_shell(self, kwargs):
114 return self.__run_shell(**kwargs)
115
A R Karthick81acbff2016-06-17 14:45:16 -0700116 def restart_radius(self):
117 print('Restarting RADIUS Server')
A R Karthick07608ef2016-08-23 16:51:19 -0700118 Radius(prefix = Container.IMAGE_PREFIX, restart = True)
A R Karthick81acbff2016-06-17 14:45:16 -0700119 return 'DONE'
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700120
A R Karthicke99ab5c2016-09-30 13:59:57 -0700121 def shutdown(self):
122 print('Shutting down cord test server')
123 os.kill(0, signal.SIGKILL)
124 return 'DONE'
125
A R Karthick1878c4f2016-11-29 09:19:50 -0800126def find_files_by_path(*paths):
127 wanted = []
128 for p in paths:
129 try:
130 fd = os.open(p, os.O_RDONLY)
131 wanted.append(os.fstat(fd)[1:3])
132 finally:
133 os.close(fd)
134
135 def fd_wanted(fd):
136 try:
137 return os.fstat(fd)[1:3] in wanted
138 except OSError:
139 return False
140
141 max_fd = getrlimit(RLIMIT_NOFILE)[1]
142 return [ fd for fd in xrange(max_fd) if fd_wanted(fd) ]
143
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700144@nottest
A R Karthickbd82f362016-11-10 15:08:52 -0800145def cord_test_server_start(daemonize = True,
146 cord_test_host = CORD_TEST_HOST,
147 cord_test_port = CORD_TEST_PORT,
148 onos_cord = None,
149 foreground=False):
A R Karthick81acbff2016-06-17 14:45:16 -0700150 server = SimpleXMLRPCServer( (cord_test_host, cord_test_port) )
151 server.register_instance(CordTestServer())
A R Karthickbd9b8a32016-07-21 09:56:45 -0700152 CordTestServer.onos_cord = onos_cord
A R Karthick81acbff2016-06-17 14:45:16 -0700153 if daemonize is True:
A R Karthick1878c4f2016-11-29 09:19:50 -0800154 ##before daemonizing, preserve urandom needed by paramiko
155 preserve_list = find_files_by_path('/dev/urandom')
156 preserve_list.append(server)
157 d = daemon.DaemonContext(files_preserve = preserve_list,
A R Karthick81acbff2016-06-17 14:45:16 -0700158 detach_process = True)
159 with d:
160 reinitContainerClients()
161 server.serve_forever()
162 else:
A R Karthickbd82f362016-11-10 15:08:52 -0800163 if foreground:
164 try:
165 server.serve_forever()
166 except KeyboardInterrupt:
167 return server
168 else:
169 task = threading.Thread(target = server.serve_forever)
170 ##terminate when main thread exits
171 task.daemon = True
172 task.start()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700173 return server
174
175@nottest
176def cord_test_server_stop(server):
177 server.shutdown()
178 server.server_close()
179
180@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700181def get_cord_test_loc():
182 host = os.getenv('CORD_TEST_HOST', CORD_TEST_HOST)
183 port = int(os.getenv('CORD_TEST_PORT', CORD_TEST_PORT))
184 return host, port
185
186def rpc_server_instance():
187 '''Stateless'''
188 host, port = get_cord_test_loc()
189 rpc_server = 'http://{}:{}'.format(host, port)
190 return xmlrpclib.Server(rpc_server, allow_none = True)
191
192@nottest
193def __cord_test_onos_restart(**kwargs):
194 return rpc_server_instance().restart_onos(kwargs)
195
196@nottest
A R Karthick889d9652016-10-03 14:13:45 -0700197def cord_test_onos_restart(node = None, config = None):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700198 '''Send ONOS restart to server'''
A R Karthick889d9652016-10-03 14:13:45 -0700199 data = __cord_test_onos_restart(node = node, config = config)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700200 if data == 'DONE':
201 return True
202 return False
203
204@nottest
A.R Karthick1700e0e2016-10-06 18:16:57 -0700205def __cord_test_onos_shutdown(**kwargs):
206 return rpc_server_instance().shutdown_onos(kwargs)
207
208@nottest
209def cord_test_onos_shutdown(node = None):
210 data = __cord_test_onos_shutdown(node = node)
211 if data == 'DONE':
212 return True
213 return False
214
215@nottest
A R Karthicke2c24bd2016-10-07 14:51:38 -0700216def __cord_test_onos_add_cluster(**kwargs):
217 return rpc_server_instance().add_cluster_onos(kwargs)
218
219@nottest
220def cord_test_onos_add_cluster(count = 1, config = None):
221 data = __cord_test_onos_add_cluster(count = count, config = config)
222 if data == 'DONE':
223 return True
224 return False
225
226@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700227def __cord_test_quagga_restart(**kwargs):
228 return rpc_server_instance().restart_quagga(kwargs)
229
230@nottest
231def cord_test_quagga_restart(config = None, boot_delay = 30):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700232 '''Send QUAGGA restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700233 data = __cord_test_quagga_restart(config = config, boot_delay = boot_delay)
234 if data == 'DONE':
235 return True
236 return False
237
238@nottest
A R Karthickc3d80e22016-06-22 17:51:24 -0700239def __cord_test_quagga_shell(**kwargs):
240 return rpc_server_instance().run_shell_quagga(kwargs)
241
242@nottest
243def cord_test_quagga_shell(cmd = None):
244 '''Send QUAGGA shell cmd to server'''
245 return __cord_test_quagga_shell(cmd = cmd)
246
247@nottest
A R Karthicka013a272016-08-16 16:40:19 -0700248def __cord_test_shell(**kwargs):
249 return rpc_server_instance().run_shell(kwargs)
250
251@nottest
252def cord_test_shell(cmd = None):
253 '''Send shell cmd to run remotely'''
254 return __cord_test_shell(cmd = cmd)
255
256@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700257def cord_test_quagga_stop():
258 data = rpc_server_instance().stop_quagga()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700259 if data == 'DONE':
260 return True
261 return False
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700262
263@nottest
264def cord_test_radius_restart():
265 '''Send Radius server restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700266 data = rpc_server_instance().restart_radius()
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700267 if data == 'DONE':
268 return True
269 return False
A R Karthicke99ab5c2016-09-30 13:59:57 -0700270
271@nottest
272def cord_test_server_shutdown(host, port):
273 '''Shutdown the cord test server'''
274 rpc_server = 'http://{}:{}'.format(host, port)
275 try:
276 xmlrpclib.Server(rpc_server, allow_none = True).shutdown()
277 except: pass
278
279 return True