blob: a77b977fccdd34dca8148b553654ef85f383801c [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 Karthickd44cea12016-07-20 12:16:41 -070016from CordContainer import Container, Onos, OnosCord, Quagga, 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
22import json
23import time
24import threading
Chetan Gaonker3533faa2016-04-25 17:50:14 -070025
A R Karthick81acbff2016-06-17 14:45:16 -070026##Server to handle container restart/stop requests from test container.
Chetan Gaonker3533faa2016-04-25 17:50:14 -070027##Used now to restart ONOS from vrouter test container
28
29CORD_TEST_HOST = '172.17.0.1'
30CORD_TEST_PORT = 25000
A R Karthickd44cea12016-07-20 12:16:41 -070031g_onos_cord = None
Chetan Gaonker3533faa2016-04-25 17:50:14 -070032
A R Karthick81acbff2016-06-17 14:45:16 -070033class QuaggaStopWrapper(Container):
34 def __init__(self, name = Quagga.NAME, image = Quagga.IMAGE, tag = 'latest'):
35 super(QuaggaStopWrapper, self).__init__(name, image, tag = tag)
36 if self.exists():
37 self.kill()
Chetan Gaonker3533faa2016-04-25 17:50:14 -070038
A R Karthick81acbff2016-06-17 14:45:16 -070039class CordTestServer(object):
40
41 def __restart_onos(self, config = None):
A R Karthickd44cea12016-07-20 12:16:41 -070042 if g_onos_cord:
43 onos_config = '{}/network-cfg.json'.format(OnosCord.onos_config_dir)
44 else:
45 onos_config = '{}/network-cfg.json'.format(Onos.host_config_dir)
A R Karthick81acbff2016-06-17 14:45:16 -070046 if config is None:
47 try:
48 os.unlink(onos_config)
49 except:
50 pass
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070051 print('Restarting ONOS')
A R Karthickd44cea12016-07-20 12:16:41 -070052 if g_onos_cord:
53 g_onos_cord.start(restart = True, network_cfg = config)
54 else:
55 Onos(restart = True, network_cfg = config)
A R Karthick81acbff2016-06-17 14:45:16 -070056 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070057
A R Karthick81acbff2016-06-17 14:45:16 -070058 def restart_onos(self, kwargs):
59 return self.__restart_onos(**kwargs)
60
61 def __restart_quagga(self, config = None, boot_delay = 30 ):
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070062 config_file = Quagga.quagga_config_file
A R Karthick81acbff2016-06-17 14:45:16 -070063 if config is not None:
64 quagga_config = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config)
65 config_file = '{}/testrib_gen.conf'.format(Quagga.guest_quagga_config)
66 with open(quagga_config, 'w+') as fd:
67 fd.write(str(config))
68 print('Restarting QUAGGA with config file %s, delay %d' %(config_file, boot_delay))
69 Quagga(restart = True, config_file = config_file, boot_delay = boot_delay)
70 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070071
A R Karthick81acbff2016-06-17 14:45:16 -070072 def restart_quagga(self, kwargs):
73 return self.__restart_quagga(**kwargs)
Chetan Gaonker7f4bf742016-05-04 15:56:08 -070074
A R Karthick81acbff2016-06-17 14:45:16 -070075 def stop_quagga(self):
76 quaggaStop = QuaggaStopWrapper()
A R Karthick4a2362c2016-06-22 17:32:44 -070077 time.sleep(5)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070078 try:
A R Karthick81acbff2016-06-17 14:45:16 -070079 quagga_config_gen = '{}/testrib_gen.conf'.format(Quagga.host_quagga_config)
80 os.unlink(quagga_config_gen)
81 except: pass
82 return 'DONE'
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070083
A R Karthickc3d80e22016-06-22 17:51:24 -070084 def __run_shell_quagga(self, cmd = None):
85 ret = 0
86 if cmd is not None:
87 exec_cmd = 'docker exec {} {}'.format(Quagga.NAME, cmd)
88 ret = os.system(exec_cmd)
89 return ret
90
91 def run_shell_quagga(self, kwargs):
92 return self.__run_shell_quagga(**kwargs)
93
A R Karthick81acbff2016-06-17 14:45:16 -070094 def restart_radius(self):
95 print('Restarting RADIUS Server')
96 Radius(restart = True)
97 return 'DONE'
Chetan Gaonker3533faa2016-04-25 17:50:14 -070098
99@nottest
A R Karthickd44cea12016-07-20 12:16:41 -0700100def cord_test_server_start(daemonize = True, cord_test_host = CORD_TEST_HOST,
101 cord_test_port = CORD_TEST_PORT, onos_cord = None):
102 global g_onos_cord
A R Karthick81acbff2016-06-17 14:45:16 -0700103 server = SimpleXMLRPCServer( (cord_test_host, cord_test_port) )
104 server.register_instance(CordTestServer())
A R Karthickd44cea12016-07-20 12:16:41 -0700105 g_onos_cord = onos_cord
A R Karthick81acbff2016-06-17 14:45:16 -0700106 if daemonize is True:
107 d = daemon.DaemonContext(files_preserve = [server],
108 detach_process = True)
109 with d:
110 reinitContainerClients()
111 server.serve_forever()
112 else:
113 task = threading.Thread(target = server.serve_forever)
114 ##terminate when main thread exits
115 task.daemon = True
116 task.start()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700117 return server
118
119@nottest
120def cord_test_server_stop(server):
121 server.shutdown()
122 server.server_close()
123
124@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700125def get_cord_test_loc():
126 host = os.getenv('CORD_TEST_HOST', CORD_TEST_HOST)
127 port = int(os.getenv('CORD_TEST_PORT', CORD_TEST_PORT))
128 return host, port
129
130def rpc_server_instance():
131 '''Stateless'''
132 host, port = get_cord_test_loc()
133 rpc_server = 'http://{}:{}'.format(host, port)
134 return xmlrpclib.Server(rpc_server, allow_none = True)
135
136@nottest
137def __cord_test_onos_restart(**kwargs):
138 return rpc_server_instance().restart_onos(kwargs)
139
140@nottest
141def cord_test_onos_restart(config = None):
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700142 '''Send ONOS restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700143 data = __cord_test_onos_restart(config = config)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700144 if data == 'DONE':
145 return True
146 return False
147
148@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700149def __cord_test_quagga_restart(**kwargs):
150 return rpc_server_instance().restart_quagga(kwargs)
151
152@nottest
153def cord_test_quagga_restart(config = None, boot_delay = 30):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700154 '''Send QUAGGA restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700155 data = __cord_test_quagga_restart(config = config, boot_delay = boot_delay)
156 if data == 'DONE':
157 return True
158 return False
159
160@nottest
A R Karthickc3d80e22016-06-22 17:51:24 -0700161def __cord_test_quagga_shell(**kwargs):
162 return rpc_server_instance().run_shell_quagga(kwargs)
163
164@nottest
165def cord_test_quagga_shell(cmd = None):
166 '''Send QUAGGA shell cmd to server'''
167 return __cord_test_quagga_shell(cmd = cmd)
168
169@nottest
A R Karthick81acbff2016-06-17 14:45:16 -0700170def cord_test_quagga_stop():
171 data = rpc_server_instance().stop_quagga()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700172 if data == 'DONE':
173 return True
174 return False
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700175
176@nottest
177def cord_test_radius_restart():
178 '''Send Radius server restart to server'''
A R Karthick81acbff2016-06-17 14:45:16 -0700179 data = rpc_server_instance().restart_radius()
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700180 if data == 'DONE':
181 return True
182 return False