blob: 2037d7132266d0fe605efd053943e8c7775c2abc [file] [log] [blame]
Chetan Gaonkercfcce782016-05-10 10:10:42 -07001#
2# 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
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#
Chetan Gaonker3533faa2016-04-25 17:50:14 -070016import SocketServer as socketserver
17import threading
18import socket
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070019from CordContainer import Onos, Quagga
Chetan Gaonker3533faa2016-04-25 17:50:14 -070020from nose.tools import nottest
21
22##Server to handle container restart requests from test container.
23##Used now to restart ONOS from vrouter test container
24
25CORD_TEST_HOST = '172.17.0.1'
26CORD_TEST_PORT = 25000
27
28class CordTestServer(socketserver.BaseRequestHandler):
29
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070030 def restart_onos(self, *args):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070031 print('Restarting ONOS')
32 onos = Onos(restart = True)
33 self.request.sendall('DONE')
34
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070035 def restart_quagga(self, *args):
36 config_file = Quagga.quagga_config_file
37 boot_delay = 15
38 if args:
39 config_file = args[0]
40 if len(args) > 1:
41 boot_delay = int(args[1])
42 print('Restarting QUAGGA with config file %s, delay %d secs'%(config_file, boot_delay))
43 quagga = Quagga(restart = True, config_file = config_file, boot_delay = boot_delay)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070044 self.request.sendall('DONE')
45
Chetan Gaonker7f4bf742016-05-04 15:56:08 -070046 def restart_radius(self, *args):
47 print('Restarting RADIUS Server')
48 radius = Radius(restart = True)
49 self.request.sendall('DONE')
50
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070051 callback_table = { 'RESTART_ONOS' : restart_onos,
52 'RESTART_QUAGGA' : restart_quagga,
Chetan Gaonker7f4bf742016-05-04 15:56:08 -070053 'RESTART_RADIUS' : restart_radius,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070054 }
55
Chetan Gaonker3533faa2016-04-25 17:50:14 -070056 def handle(self):
57 data = self.request.recv(1024).strip()
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070058 cmd = data.split()[0]
59 try:
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070060 #args = ' '.join(data.split()[1:])
61 args = data.split()[1:]
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070062 except:
63 args = None
64
65 if self.callback_table.has_key(cmd):
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070066 self.callback_table[cmd](self, *args)
Chetan Gaonker3533faa2016-04-25 17:50:14 -070067
68class ThreadedTestServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
Chetan Gaonker02236ba2016-04-26 11:24:34 -070069 allow_reuse_address = True
Chetan Gaonker3533faa2016-04-25 17:50:14 -070070
71@nottest
72def cord_test_server_start():
73 server = ThreadedTestServer( (CORD_TEST_HOST, CORD_TEST_PORT), CordTestServer)
74 task = threading.Thread(target = server.serve_forever)
75 ##terminate when main thread exits
76 task.daemon = True
77 task.start()
78 return server
79
80@nottest
81def cord_test_server_stop(server):
82 server.shutdown()
83 server.server_close()
84
85@nottest
86def cord_test_onos_restart():
87 '''Send ONOS restart to server'''
88 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
89 s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) )
90 s.sendall('RESTART_ONOS\n')
91 data = s.recv(1024).strip()
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070092 s.close()
93 if data == 'DONE':
94 return True
95 return False
96
97@nottest
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070098def cord_test_quagga_restart(config_file = None, boot_delay = 30):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070099 '''Send QUAGGA restart to server'''
100 if config_file is None:
101 config_file = Quagga.quagga_config_file
102 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
103 s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) )
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -0700104 s.sendall('RESTART_QUAGGA {0} {1}\n'.format(config_file, boot_delay))
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700105 data = s.recv(1024).strip()
106 s.close()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700107 if data == 'DONE':
108 return True
109 return False
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700110
111@nottest
112def cord_test_radius_restart():
113 '''Send Radius server restart to server'''
114 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
115 s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) )
116 s.sendall('RESTART_RADIUS\n')
117 data = s.recv(1024).strip()
118 s.close()
119 if data == 'DONE':
120 return True
121 return False