blob: 84e937f0ffce8da217155d447f9a56f3b28d45df [file] [log] [blame]
Gilles Depatiec68b3ad2018-08-21 16:29:03 -04001#
2# Copyright 2018 the original author or authors.
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#
16
17"""
18vOLT-HA Start/Stop module
19"""
20
21
22import os
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040023import subprocess
Gilles Depatie84cb1e72018-10-26 12:41:33 -040024import testCaseUtils
Gilles Depatie1be639b2018-12-06 10:51:08 -050025import logging
Kailash90d4c052019-06-25 09:47:19 -070026import time
Gilles Depatie0bf31352019-02-04 13:48:41 -050027
Gilles Depatieea423712019-04-12 16:39:12 -040028
Gilles Depatie84cb1e72018-10-26 12:41:33 -040029class VolthaMngr(object):
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040030
31 """
32 This class implements voltha startup/shutdown callable helper functions
33 """
Gilles Depatieea423712019-04-12 16:39:12 -040034
Kailash90d4c052019-06-25 09:47:19 -070035 DEFAULT_SIMTYPE = 'ponsim'
Gilles Depatieea423712019-04-12 16:39:12 -040036
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040037 def __init__(self):
Gilles Depatie0bf31352019-02-04 13:48:41 -050038 self.dirs = dict()
39 self.dirs['root'] = None
40 self.dirs['voltha'] = None
41 self.dirs['log'] = None
Kailash90d4c052019-06-25 09:47:19 -070042
43 self.__radiusName = None
44 self.__radiusIp = None
45
Gilles Depatie0bf31352019-02-04 13:48:41 -050046 def v_set_log_dirs(self, root_dir, voltha_dir, log_dir):
47 testCaseUtils.config_dirs(self, log_dir, root_dir, voltha_dir)
Gilles Depatieea423712019-04-12 16:39:12 -040048
Kailash90d4c052019-06-25 09:47:19 -070049 def start_all_pods(self, simtype=DEFAULT_SIMTYPE):
50 proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'start', simtype],
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040051 stdout=subprocess.PIPE,
52 stderr=subprocess.PIPE)
Gilles Depatie84cb1e72018-10-26 12:41:33 -040053 output = proc1.communicate()[0]
54 print(output)
Gilles Depatie0bf31352019-02-04 13:48:41 -050055 proc1.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040056
Kailash90d4c052019-06-25 09:47:19 -070057 def stop_all_pods(self, simtype=DEFAULT_SIMTYPE):
58 proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'stop', simtype],
Gilles Depatie84cb1e72018-10-26 12:41:33 -040059 stdout=subprocess.PIPE,
60 stderr=subprocess.PIPE)
61 output = proc1.communicate()[0]
62 print(output)
Gilles Depatie0bf31352019-02-04 13:48:41 -050063 proc1.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040064
Kailash90d4c052019-06-25 09:47:19 -070065 def reset_kube_adm(self, simtype=DEFAULT_SIMTYPE):
66 proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'clear', simtype],
Gilles Depatie84cb1e72018-10-26 12:41:33 -040067 stdout=subprocess.PIPE,
68 stderr=subprocess.PIPE)
69 output = proc1.communicate()[0]
70 print(output)
Gilles Depatie0bf31352019-02-04 13:48:41 -050071 proc1.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040072
73 """
74 Because we are not deploying SEBA with XOS and NEM, and that a standalone Voltha
75 deployment is not common, in order to get flows to work, we need to alter Onos
76 NetCfg in two fashion.
77 One is to add to device list and the other is to add the missing Sadis section
78 """
Gilles Depatie0bf31352019-02-04 13:48:41 -050079 def alter_onos_net_cfg(self):
80 logging.info('Altering the Onos NetCfg to suit Voltha\'s needs')
81 logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
82 'http://localhost:30120/onos/v1/network/configuration/devices/ -d @%s/tests/atests/build/devices_json'
83 % testCaseUtils.get_dir(self, 'voltha'))
Gilles Depatie84cb1e72018-10-26 12:41:33 -040084 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
Gilles Depatie0bf31352019-02-04 13:48:41 -050085 'http://localhost:30120/onos/v1/network/configuration/devices/ -d @%s/tests/atests/build/devices_json'
86 % testCaseUtils.get_dir(self, 'voltha'))
87 logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
88 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/sadis_json'
89 % testCaseUtils.get_dir(self, 'voltha'))
Gilles Depatie84cb1e72018-10-26 12:41:33 -040090 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
Gilles Depatie0bf31352019-02-04 13:48:41 -050091 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/sadis_json'
92 % testCaseUtils.get_dir(self, 'voltha'))
93
94 def collect_pod_logs(self):
Gilles Depatie1be639b2018-12-06 10:51:08 -050095 logging.info('Collect logs from all Pods')
Gilles Depatie0bf31352019-02-04 13:48:41 -050096 allRunningPods = get_all_running_pods()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040097 for nsName in allRunningPods:
98 Namespace = nsName.get('NS')
Gilles Depatie0bf31352019-02-04 13:48:41 -050099 podName = nsName.get('Name')
Gilles Depatie82dd2022019-02-19 14:31:33 -0500100 if 'onos' in podName:
Kailash8ae83ac2019-02-11 12:23:52 -0800101 os.system('/usr/bin/kubectl logs -n %s -f %s onos > %s/%s.log 2>&1 &' %
Gilles Depatie0bf31352019-02-04 13:48:41 -0500102 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
Gilles Depatie82dd2022019-02-19 14:31:33 -0500103 elif 'calico-node' in podName:
Kailash90d4c052019-06-25 09:47:19 -0700104 os.system('/usr/bin/kubectl logs -n %s -f %s calico-node > %s/%s.log 2>&1 &' %
105 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
Gilles Depatie82dd2022019-02-19 14:31:33 -0500106 else:
107 os.system('/usr/bin/kubectl logs -n %s -f %s > %s/%s.log 2>&1 &' %
108 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400109
Kailash90d4c052019-06-25 09:47:19 -0700110 def discover_freeradius_pod_name(self):
111 self.__radiusName = testCaseUtils.extract_pod_name('freeradius').strip()
112 logging.info('freeradius Name = %s' % self.__radiusName)
113
114 def discover_freeradius_ip_addr(self):
115 ipAddr = testCaseUtils.extract_radius_ip_addr(self.__radiusName)
116 assert ipAddr, 'No IP address listed for freeradius'
117 self.__radiusIp = ipAddr.strip()
118 logging.info('freeradius IP = %s' % self.__radiusIp)
119
120 def prepare_current_freeradius_ip(self):
121 status = testCaseUtils.modify_radius_ip_in_json_using_sed(self, self.__radiusIp)
122 assert (status == 0), 'Setting Radius Ip in Json File did not return Success'
123
124 def alter_freeradius_ip_in_onos_aaa_application_configuration(self):
125 logging.info('Altering the Onos NetCfg AAA apps with Freeradius IP address')
126 logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
127 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/aaa_json'
128 % testCaseUtils.get_dir(self, 'voltha'))
129 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
130 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/aaa_json'
131 % testCaseUtils.get_dir(self, 'voltha'))
132
133 def activate_aaa_app_in_onos(self):
134 logging.info('Activating AAA Application on Onos')
135 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
136 'voltha_aaa_application_activate.log', 'app activate aaa')
137 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'Activated', 'voltha_aaa_application_activate.log')
138 assert statusLines, 'AAA Application failed to be Activated'
139
140 def deactivate_aaa_app_in_onos(self):
141 logging.info('Deactivating AAA Application on Onos')
142 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
143 'voltha_aaa_application_deactivate.log', 'app deactivate aaa')
144 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'Deactivated', 'voltha_aaa_application_deactivate.log')
145 assert statusLines, 'AAA Application failed to be Deactivated'
146
147
Gilles Depatie0bf31352019-02-04 13:48:41 -0500148def get_all_running_pods():
149 allRunningPods = []
150 proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'],
151 stdout=subprocess.PIPE,
152 stderr=subprocess.PIPE)
153 proc2 = subprocess.Popen(['grep', '-v', 'NAMESPACE'], stdin=proc1.stdout,
154 stdout=subprocess.PIPE,
155 stderr=subprocess.PIPE)
156 proc1.stdout.close()
157 out, err = proc2.communicate()
158 print(out)
159 if out:
160 for line in out.split('\n'):
161 items = line.split()
162 if len(items) > 2:
163 nsName = dict()
164 nsName['NS'] = items[0]
165 nsName['Name'] = items[1]
166 allRunningPods.append(nsName)
167 return allRunningPods
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400168
Gilles Depatie0bf31352019-02-04 13:48:41 -0500169
Kailash90d4c052019-06-25 09:47:19 -0700170def voltha_initialize(root_dir, voltha_dir, log_dir, simtype):
Gilles Depatie0bf31352019-02-04 13:48:41 -0500171 voltha = VolthaMngr()
172 voltha.v_set_log_dirs(root_dir, voltha_dir, log_dir)
Kailash90d4c052019-06-25 09:47:19 -0700173 voltha.stop_all_pods(simtype)
174 voltha.reset_kube_adm(simtype)
175 voltha.start_all_pods(simtype)
Gilles Depatie0bf31352019-02-04 13:48:41 -0500176 voltha.alter_onos_net_cfg()
177 voltha.collect_pod_logs()
Kailash90d4c052019-06-25 09:47:19 -0700178 voltha.discover_freeradius_pod_name()
179 voltha.discover_freeradius_ip_addr()
180 voltha.prepare_current_freeradius_ip()
181 voltha.alter_freeradius_ip_in_onos_aaa_application_configuration()
182 voltha.deactivate_aaa_app_in_onos()
183 time.sleep(5)
184 voltha.activate_aaa_app_in_onos()