blob: c3602605c4c68c8e031f5662d84d108e3a5264f7 [file] [log] [blame]
Chetan Gaonkercb122cc2016-05-10 10:58:34 -07001#!/usr/bin/env python
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002#
3# Copyright 2016-present Ciena Corporation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
Chetan Gaonker3533faa2016-04-25 17:50:14 -070017import SocketServer as socketserver
18import threading
19import socket
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070020from CordContainer import Onos, Quagga
Chetan Gaonker3533faa2016-04-25 17:50:14 -070021from nose.tools import nottest
22
23##Server to handle container restart requests from test container.
24##Used now to restart ONOS from vrouter test container
25
26CORD_TEST_HOST = '172.17.0.1'
27CORD_TEST_PORT = 25000
28
29class CordTestServer(socketserver.BaseRequestHandler):
30
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070031 def restart_onos(self, *args):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070032 print('Restarting ONOS')
33 onos = Onos(restart = True)
34 self.request.sendall('DONE')
35
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070036 def restart_quagga(self, *args):
37 config_file = Quagga.quagga_config_file
38 boot_delay = 15
39 if args:
40 config_file = args[0]
41 if len(args) > 1:
42 boot_delay = int(args[1])
43 print('Restarting QUAGGA with config file %s, delay %d secs'%(config_file, boot_delay))
44 quagga = Quagga(restart = True, config_file = config_file, boot_delay = boot_delay)
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070045 self.request.sendall('DONE')
46
Chetan Gaonker7f4bf742016-05-04 15:56:08 -070047 def restart_radius(self, *args):
48 print('Restarting RADIUS Server')
49 radius = Radius(restart = True)
50 self.request.sendall('DONE')
51
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070052 callback_table = { 'RESTART_ONOS' : restart_onos,
53 'RESTART_QUAGGA' : restart_quagga,
Chetan Gaonker7f4bf742016-05-04 15:56:08 -070054 'RESTART_RADIUS' : restart_radius,
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070055 }
56
Chetan Gaonker3533faa2016-04-25 17:50:14 -070057 def handle(self):
58 data = self.request.recv(1024).strip()
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070059 cmd = data.split()[0]
60 try:
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070061 #args = ' '.join(data.split()[1:])
62 args = data.split()[1:]
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070063 except:
64 args = None
65
66 if self.callback_table.has_key(cmd):
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070067 self.callback_table[cmd](self, *args)
Chetan Gaonker3533faa2016-04-25 17:50:14 -070068
69class ThreadedTestServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
Chetan Gaonker02236ba2016-04-26 11:24:34 -070070 allow_reuse_address = True
Chetan Gaonker3533faa2016-04-25 17:50:14 -070071
72@nottest
73def cord_test_server_start():
74 server = ThreadedTestServer( (CORD_TEST_HOST, CORD_TEST_PORT), CordTestServer)
75 task = threading.Thread(target = server.serve_forever)
76 ##terminate when main thread exits
77 task.daemon = True
78 task.start()
79 return server
80
81@nottest
82def cord_test_server_stop(server):
83 server.shutdown()
84 server.server_close()
85
86@nottest
87def cord_test_onos_restart():
88 '''Send ONOS restart to server'''
89 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
90 s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) )
91 s.sendall('RESTART_ONOS\n')
92 data = s.recv(1024).strip()
Chetan Gaonker6cf6e472016-04-26 14:41:51 -070093 s.close()
94 if data == 'DONE':
95 return True
96 return False
97
98@nottest
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -070099def cord_test_quagga_restart(config_file = None, boot_delay = 30):
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700100 '''Send QUAGGA restart to server'''
101 if config_file is None:
102 config_file = Quagga.quagga_config_file
103 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
104 s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) )
Chetan Gaonkerfd3d6502016-05-03 13:23:07 -0700105 s.sendall('RESTART_QUAGGA {0} {1}\n'.format(config_file, boot_delay))
Chetan Gaonker6cf6e472016-04-26 14:41:51 -0700106 data = s.recv(1024).strip()
107 s.close()
Chetan Gaonker3533faa2016-04-25 17:50:14 -0700108 if data == 'DONE':
109 return True
110 return False
Chetan Gaonker7f4bf742016-05-04 15:56:08 -0700111
112@nottest
113def cord_test_radius_restart():
114 '''Send Radius server restart to server'''
115 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
116 s.connect( (CORD_TEST_HOST, CORD_TEST_PORT) )
117 s.sendall('RESTART_RADIUS\n')
118 data = s.recv(1024).strip()
119 s.close()
120 if data == 'DONE':
121 return True
122 return False