blob: cf280f65952d33deca1b0dff608f082912039ffe [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
19import daemon
20import xmlrpclib
21import os
A R Karthicke99ab5c2016-09-30 13:59:57 -070022import signal
A R Karthick81acbff2016-06-17 14:45:16 -070023import json
24import time
25import threading
Chetan Gaonker3533faa2016-04-25 17:50:14 -070026
A R Karthick81acbff2016-06-17 14:45:16 -070027##Server to handle container restart/stop requests from test container.
Chetan Gaonker3533faa2016-04-25 17:50:14 -070028##Used now to restart ONOS from vrouter test container
29
30CORD_TEST_HOST = '172.17.0.1'
31CORD_TEST_PORT = 25000
32
A R Karthick81acbff2016-06-17 14:45:16 -070033class CordTestServer(object):
34
A R Karthickbd9b8a32016-07-21 09:56:45 -070035 onos_cord = None
36
A R Karthick889d9652016-10-03 14:13:45 -070037 def __restart_onos(self, node = None, config = None):
A R Karthickbd9b8a32016-07-21 09:56:45 -070038 if self.onos_cord:
A R Karthickd44cea12016-07-20 12:16:41 -070039 onos_config = '{}/network-cfg.json'.format(OnosCord.onos_config_dir)
40 else:
41 onos_config = '{}/network-cfg.json'.format(Onos.host_config_dir)
A R Karthick81acbff2016-06-17 14:45:16 -070042 if config is None:
43 try:
44 os.unlink(onos_config)
45 except:
46 pass
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070047 print('Restarting ONOS')
A R Karthickbd9b8a32016-07-21 09:56:45 -070048 if self.onos_cord:
49 self.onos_cord.start(restart = True, network_cfg = config)
A R Karthickd44cea12016-07-20 12:16:41 -070050 else:
A R Karthick889d9652016-10-03 14:13:45 -070051 Onos.restart_node(node = node, network_cfg = config)
A R Karthick81acbff2016-06-17 14:45:16 -070052 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070053
A R Karthick81acbff2016-06-17 14:45:16 -070054 def restart_onos(self, kwargs):
55 return self.__restart_onos(**kwargs)
56
A.R Karthick1700e0e2016-10-06 18:16:57 -070057 def __shutdown_onos(self, node = None):
58 if node is None:
59 node = Onos.NAME
60 OnosStopWrapper(node)
61 return 'DONE'
62
63 def shutdown_onos(self, kwargs):
64 return self.__shutdown_onos(**kwargs)
65
A R Karthick81acbff2016-06-17 14:45:16 -070066 def __restart_quagga(self, config = None, boot_delay = 30 ):
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070067 config_file = Quagga.quagga_config_file
A R Karthick81acbff2016-06-17 14:45:16 -070068 if config is not None:
69 quagga_config = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config)
70 config_file = '{}/testrib_gen.conf'.format(Quagga.guest_quagga_config)
71 with open(quagga_config, 'w+') as fd:
72 fd.write(str(config))
73 print('Restarting QUAGGA with config file %s, delay %d' %(config_file, boot_delay))
A R Karthick07608ef2016-08-23 16:51:19 -070074 Quagga(prefix = Container.IMAGE_PREFIX, restart = True, config_file = config_file, boot_delay = boot_delay)
A R Karthick81acbff2016-06-17 14:45:16 -070075 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070076
A R Karthick81acbff2016-06-17 14:45:16 -070077 def restart_quagga(self, kwargs):
78 return self.__restart_quagga(**kwargs)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -070079
A R Karthick81acbff2016-06-17 14:45:16 -070080 def stop_quagga(self):
81 quaggaStop = QuaggaStopWrapper()
A R Karthick4a2362c2016-06-22 17:32:44 -070082 time.sleep(5)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070083 try:
A R Karthick81acbff2016-06-17 14:45:16 -070084 quagga_config_gen = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config)
85 os.unlink(quagga_config_gen)
86 except: pass
87 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070088
A R Karthickc3d80e22016-06-22 17:51:24 -070089 def __run_shell_quagga(self, cmd = None):
90 ret = 0
91 if cmd is not None:
92 exec_cmd = 'docker exec {} {}'.format(Quagga.NAME, cmd)
93 ret = os.system(exec_cmd)
94 return ret
95
A R Karthicka013a272016-08-16 16:40:19 -070096 def __run_shell(self, cmd = None):
97 ret = 0
98 if cmd is not None:
99 ret = os.system(cmd)
100 return ret
101
A R Karthickc3d80e22016-06-22 17:51:24 -0700102 def run_shell_quagga(self, kwargs):
103 return self.__run_shell_quagga(**kwargs)
104
A R Karthicka013a272016-08-16 16:40:19 -0700105 def run_shell(self, kwargs):
106 return self.__run_shell(**kwargs)
107
A R Karthick81acbff2016-06-17 14:45:16 -0700108 def restart_radius(self):
109 print('Restarting RADIUS Server')
A R Karthick07608ef2016-08-23 16:51:19 -0700110 Radius(prefix = Container.IMAGE_PREFIX, restart = True)
A R Karthick81acbff2016-06-17 14:45:16 -0700111 return 'DONE'
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700112
A R Karthicke99ab5c2016-09-30 13:59:57 -0700113 def shutdown(self):
114 print('Shutting down cord test server')
115 os.kill(0, signal.SIGKILL)
116 return 'DONE'
117
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700118@nottest
A R Karthickd44cea12016-07-20 12:16:41 -0700119def cord_test_server_start(daemonize = True, cord_test_host = CORD_TEST_HOST,
120 cord_test_port = CORD_TEST_PORT, onos_cord = None):
A R Karthick81acbff2016-06-17 14:45:16 -0700121 server = SimpleXMLRPCServer( (cord_test_host, cord_test_port) )
122 server.register_instance(CordTestServer())
A R Karthickbd9b8a32016-07-21 09:56:45 -0700123 CordTestServer.onos_cord = onos_cord
A R Karthick81acbff2016-06-17 14:45:16 -0700124 if daemonize is True:
125 d = daemon.DaemonContext(files_preserve = [server],
126 detach_process = True)
127 with d:
128 reinitContainerClients()
129 server.serve_forever()
130 else:
131 task = threading.Thread(target = server.serve_forever)
132 ##terminate when main thread exits
133 task.daemon = True
134 task.start()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700135 return server
136
137@nottest
138def cord_test_server_stop(server):
139 server.shutdown()
140 server.server_close()
141
142@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700143def get_cord_test_loc():
144 host = os.getenv('CORD_TEST_HOST', CORD_TEST_HOST)
145 port = int(os.getenv('CORD_TEST_PORT', CORD_TEST_PORT))
146 return host, port
147
148def rpc_server_instance():
149 '''Stateless'''
150 host, port = get_cord_test_loc()
151 rpc_server = 'http://{}:{}'.format(host, port)
152 return xmlrpclib.Server(rpc_server, allow_none = True)
153
154@nottest
155def __cord_test_onos_restart(**kwargs):
156 return rpc_server_instance().restart_onos(kwargs)
157
158@nottest
A R Karthick889d9652016-10-03 14:13:45 -0700159def cord_test_onos_restart(node = None, config = None):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700160 '''Send ONOS restart to server'''
A R Karthick889d9652016-10-03 14:13:45 -0700161 data = __cord_test_onos_restart(node = node, config = config)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700162 if data == 'DONE':
163 return True
164 return False
165
166@nottest
A.R Karthick1700e0e2016-10-06 18:16:57 -0700167def __cord_test_onos_shutdown(**kwargs):
168 return rpc_server_instance().shutdown_onos(kwargs)
169
170@nottest
171def cord_test_onos_shutdown(node = None):
172 data = __cord_test_onos_shutdown(node = node)
173 if data == 'DONE':
174 return True
175 return False
176
177@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700178def __cord_test_quagga_restart(**kwargs):
179 return rpc_server_instance().restart_quagga(kwargs)
180
181@nottest
182def cord_test_quagga_restart(config = None, boot_delay = 30):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700183 '''Send QUAGGA restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700184 data = __cord_test_quagga_restart(config = config, boot_delay = boot_delay)
185 if data == 'DONE':
186 return True
187 return False
188
189@nottest
A R Karthickc3d80e22016-06-22 17:51:24 -0700190def __cord_test_quagga_shell(**kwargs):
191 return rpc_server_instance().run_shell_quagga(kwargs)
192
193@nottest
194def cord_test_quagga_shell(cmd = None):
195 '''Send QUAGGA shell cmd to server'''
196 return __cord_test_quagga_shell(cmd = cmd)
197
198@nottest
A R Karthicka013a272016-08-16 16:40:19 -0700199def __cord_test_shell(**kwargs):
200 return rpc_server_instance().run_shell(kwargs)
201
202@nottest
203def cord_test_shell(cmd = None):
204 '''Send shell cmd to run remotely'''
205 return __cord_test_shell(cmd = cmd)
206
207@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700208def cord_test_quagga_stop():
209 data = rpc_server_instance().stop_quagga()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700210 if data == 'DONE':
211 return True
212 return False
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700213
214@nottest
215def cord_test_radius_restart():
216 '''Send Radius server restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700217 data = rpc_server_instance().restart_radius()
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700218 if data == 'DONE':
219 return True
220 return False
A R Karthicke99ab5c2016-09-30 13:59:57 -0700221
222@nottest
223def cord_test_server_shutdown(host, port):
224 '''Shutdown the cord test server'''
225 rpc_server = 'http://{}:{}'.format(host, port)
226 try:
227 xmlrpclib.Server(rpc_server, allow_none = True).shutdown()
228 except: pass
229
230 return True