blob: c882f588bd15ba54e0752a4fcc9f842cf959bc98 [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 Karthicke2c24bd2016-10-07 14:51:38 -070066 def __add_cluster_onos(self, count = 1, config = None):
67 Onos.add_cluster(count = count, network_cfg = config)
68 return 'DONE'
69
70 def add_cluster_onos(self, kwargs):
A R Karthickdb59cf72016-10-10 10:43:22 -070071 return self.__add_cluster_onos(**kwargs)
A R Karthicke2c24bd2016-10-07 14:51:38 -070072
A R Karthick81acbff2016-06-17 14:45:16 -070073 def __restart_quagga(self, config = None, boot_delay = 30 ):
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070074 config_file = Quagga.quagga_config_file
A R Karthick81acbff2016-06-17 14:45:16 -070075 if config is not None:
76 quagga_config = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config)
77 config_file = '{}/testrib_gen.conf'.format(Quagga.guest_quagga_config)
78 with open(quagga_config, 'w+') as fd:
79 fd.write(str(config))
80 print('Restarting QUAGGA with config file %s, delay %d' %(config_file, boot_delay))
A R Karthick07608ef2016-08-23 16:51:19 -070081 Quagga(prefix = Container.IMAGE_PREFIX, restart = True, config_file = config_file, boot_delay = boot_delay)
A R Karthick81acbff2016-06-17 14:45:16 -070082 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070083
A R Karthick81acbff2016-06-17 14:45:16 -070084 def restart_quagga(self, kwargs):
85 return self.__restart_quagga(**kwargs)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -070086
A R Karthick81acbff2016-06-17 14:45:16 -070087 def stop_quagga(self):
88 quaggaStop = QuaggaStopWrapper()
A R Karthick4a2362c2016-06-22 17:32:44 -070089 time.sleep(5)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070090 try:
A R Karthick81acbff2016-06-17 14:45:16 -070091 quagga_config_gen = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config)
92 os.unlink(quagga_config_gen)
93 except: pass
94 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070095
A R Karthickc3d80e22016-06-22 17:51:24 -070096 def __run_shell_quagga(self, cmd = None):
97 ret = 0
98 if cmd is not None:
99 exec_cmd = 'docker exec {} {}'.format(Quagga.NAME, cmd)
100 ret = os.system(exec_cmd)
101 return ret
102
A R Karthicka013a272016-08-16 16:40:19 -0700103 def __run_shell(self, cmd = None):
104 ret = 0
105 if cmd is not None:
106 ret = os.system(cmd)
107 return ret
108
A R Karthickc3d80e22016-06-22 17:51:24 -0700109 def run_shell_quagga(self, kwargs):
110 return self.__run_shell_quagga(**kwargs)
111
A R Karthicka013a272016-08-16 16:40:19 -0700112 def run_shell(self, kwargs):
113 return self.__run_shell(**kwargs)
114
A R Karthick81acbff2016-06-17 14:45:16 -0700115 def restart_radius(self):
116 print('Restarting RADIUS Server')
A R Karthick07608ef2016-08-23 16:51:19 -0700117 Radius(prefix = Container.IMAGE_PREFIX, restart = True)
A R Karthick81acbff2016-06-17 14:45:16 -0700118 return 'DONE'
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700119
A R Karthicke99ab5c2016-09-30 13:59:57 -0700120 def shutdown(self):
121 print('Shutting down cord test server')
122 os.kill(0, signal.SIGKILL)
123 return 'DONE'
124
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700125@nottest
A R Karthickd44cea12016-07-20 12:16:41 -0700126def cord_test_server_start(daemonize = True, cord_test_host = CORD_TEST_HOST,
127 cord_test_port = CORD_TEST_PORT, onos_cord = None):
A R Karthick81acbff2016-06-17 14:45:16 -0700128 server = SimpleXMLRPCServer( (cord_test_host, cord_test_port) )
129 server.register_instance(CordTestServer())
A R Karthickbd9b8a32016-07-21 09:56:45 -0700130 CordTestServer.onos_cord = onos_cord
A R Karthick81acbff2016-06-17 14:45:16 -0700131 if daemonize is True:
132 d = daemon.DaemonContext(files_preserve = [server],
133 detach_process = True)
134 with d:
135 reinitContainerClients()
136 server.serve_forever()
137 else:
138 task = threading.Thread(target = server.serve_forever)
139 ##terminate when main thread exits
140 task.daemon = True
141 task.start()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700142 return server
143
144@nottest
145def cord_test_server_stop(server):
146 server.shutdown()
147 server.server_close()
148
149@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700150def get_cord_test_loc():
151 host = os.getenv('CORD_TEST_HOST', CORD_TEST_HOST)
152 port = int(os.getenv('CORD_TEST_PORT', CORD_TEST_PORT))
153 return host, port
154
155def rpc_server_instance():
156 '''Stateless'''
157 host, port = get_cord_test_loc()
158 rpc_server = 'http://{}:{}'.format(host, port)
159 return xmlrpclib.Server(rpc_server, allow_none = True)
160
161@nottest
162def __cord_test_onos_restart(**kwargs):
163 return rpc_server_instance().restart_onos(kwargs)
164
165@nottest
A R Karthick889d9652016-10-03 14:13:45 -0700166def cord_test_onos_restart(node = None, config = None):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700167 '''Send ONOS restart to server'''
A R Karthick889d9652016-10-03 14:13:45 -0700168 data = __cord_test_onos_restart(node = node, config = config)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700169 if data == 'DONE':
170 return True
171 return False
172
173@nottest
A.R Karthick1700e0e2016-10-06 18:16:57 -0700174def __cord_test_onos_shutdown(**kwargs):
175 return rpc_server_instance().shutdown_onos(kwargs)
176
177@nottest
178def cord_test_onos_shutdown(node = None):
179 data = __cord_test_onos_shutdown(node = node)
180 if data == 'DONE':
181 return True
182 return False
183
184@nottest
A R Karthicke2c24bd2016-10-07 14:51:38 -0700185def __cord_test_onos_add_cluster(**kwargs):
186 return rpc_server_instance().add_cluster_onos(kwargs)
187
188@nottest
189def cord_test_onos_add_cluster(count = 1, config = None):
190 data = __cord_test_onos_add_cluster(count = count, config = config)
191 if data == 'DONE':
192 return True
193 return False
194
195@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700196def __cord_test_quagga_restart(**kwargs):
197 return rpc_server_instance().restart_quagga(kwargs)
198
199@nottest
200def cord_test_quagga_restart(config = None, boot_delay = 30):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700201 '''Send QUAGGA restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700202 data = __cord_test_quagga_restart(config = config, boot_delay = boot_delay)
203 if data == 'DONE':
204 return True
205 return False
206
207@nottest
A R Karthickc3d80e22016-06-22 17:51:24 -0700208def __cord_test_quagga_shell(**kwargs):
209 return rpc_server_instance().run_shell_quagga(kwargs)
210
211@nottest
212def cord_test_quagga_shell(cmd = None):
213 '''Send QUAGGA shell cmd to server'''
214 return __cord_test_quagga_shell(cmd = cmd)
215
216@nottest
A R Karthicka013a272016-08-16 16:40:19 -0700217def __cord_test_shell(**kwargs):
218 return rpc_server_instance().run_shell(kwargs)
219
220@nottest
221def cord_test_shell(cmd = None):
222 '''Send shell cmd to run remotely'''
223 return __cord_test_shell(cmd = cmd)
224
225@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700226def cord_test_quagga_stop():
227 data = rpc_server_instance().stop_quagga()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700228 if data == 'DONE':
229 return True
230 return False
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700231
232@nottest
233def cord_test_radius_restart():
234 '''Send Radius server restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700235 data = rpc_server_instance().restart_radius()
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700236 if data == 'DONE':
237 return True
238 return False
A R Karthicke99ab5c2016-09-30 13:59:57 -0700239
240@nottest
241def cord_test_server_shutdown(host, port):
242 '''Shutdown the cord test server'''
243 rpc_server = 'http://{}:{}'.format(host, port)
244 try:
245 xmlrpclib.Server(rpc_server, allow_none = True).shutdown()
246 except: pass
247
248 return True