blob: d450494bfe98b2acf19395116739b341696412f4 [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
Gilles Depatie493ea242019-05-21 14:38:08 -040026import time
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040027
Gilles Depatie0bf31352019-02-04 13:48:41 -050028
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
Gilles Depatie493ea242019-05-21 14:38:08 -040035 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
Gilles Depatiec9a26cf2019-05-10 15:43:12 -040042
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
Gilles Depatie493ea242019-05-21 14:38:08 -040049 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
Gilles Depatie493ea242019-05-21 14:38:08 -040057 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
Gilles Depatie493ea242019-05-21 14:38:08 -040065 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'))
Gilles Depatie88c281a2019-07-30 16:17:03 -040093 logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
94 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/dhcpl2relay_json'
95 % testCaseUtils.get_dir(self, 'voltha'))
96 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
97 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/dhcpl2relay_json'
98 % testCaseUtils.get_dir(self, 'voltha'))
Gilles Depatie0bf31352019-02-04 13:48:41 -050099
100 def collect_pod_logs(self):
Gilles Depatie1be639b2018-12-06 10:51:08 -0500101 logging.info('Collect logs from all Pods')
Gilles Depatie0bf31352019-02-04 13:48:41 -0500102 allRunningPods = get_all_running_pods()
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400103 for nsName in allRunningPods:
104 Namespace = nsName.get('NS')
Gilles Depatie0bf31352019-02-04 13:48:41 -0500105 podName = nsName.get('Name')
Gilles Depatie82dd2022019-02-19 14:31:33 -0500106 if 'onos' in podName:
Kailash8ae83ac2019-02-11 12:23:52 -0800107 os.system('/usr/bin/kubectl logs -n %s -f %s onos > %s/%s.log 2>&1 &' %
Gilles Depatie0bf31352019-02-04 13:48:41 -0500108 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
Gilles Depatie82dd2022019-02-19 14:31:33 -0500109 elif 'calico-node' in podName:
Gilles Depatiec9a26cf2019-05-10 15:43:12 -0400110 os.system('/usr/bin/kubectl logs -n %s -f %s calico-node > %s/%s.log 2>&1 &' %
111 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
Gilles Depatie82dd2022019-02-19 14:31:33 -0500112 else:
113 os.system('/usr/bin/kubectl logs -n %s -f %s > %s/%s.log 2>&1 &' %
114 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400115
Gilles Depatiec9a26cf2019-05-10 15:43:12 -0400116 def discover_freeradius_pod_name(self):
117 self.__radiusName = testCaseUtils.extract_pod_name('freeradius').strip()
118 logging.info('freeradius Name = %s' % self.__radiusName)
119
120 def discover_freeradius_ip_addr(self):
121 ipAddr = testCaseUtils.extract_radius_ip_addr(self.__radiusName)
122 assert ipAddr, 'No IP address listed for freeradius'
123 self.__radiusIp = ipAddr.strip()
124 logging.info('freeradius IP = %s' % self.__radiusIp)
125
126 def prepare_current_freeradius_ip(self):
127 status = testCaseUtils.modify_radius_ip_in_json_using_sed(self, self.__radiusIp)
128 assert (status == 0), 'Setting Radius Ip in Json File did not return Success'
129
130 def alter_freeradius_ip_in_onos_aaa_application_configuration(self):
131 logging.info('Altering the Onos NetCfg AAA apps with Freeradius IP address')
132 logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
133 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/aaa_json'
134 % testCaseUtils.get_dir(self, 'voltha'))
135 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
136 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/aaa_json'
137 % testCaseUtils.get_dir(self, 'voltha'))
138
Gilles Depatie493ea242019-05-21 14:38:08 -0400139 def activate_aaa_app_in_onos(self):
140 logging.info('Activating AAA Application on Onos')
141 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
142 'voltha_aaa_application_activate.log', 'app activate aaa')
143 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'Activated', 'voltha_aaa_application_activate.log')
144 assert statusLines, 'AAA Application failed to be Activated'
145
146 def deactivate_aaa_app_in_onos(self):
147 logging.info('Deactivating AAA Application on Onos')
148 testCaseUtils.send_command_to_onos_cli(testCaseUtils.get_dir(self, 'log'),
149 'voltha_aaa_application_deactivate.log', 'app deactivate aaa')
150 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'Deactivated', 'voltha_aaa_application_deactivate.log')
151 assert statusLines, 'AAA Application failed to be Deactivated'
152
Gilles Depatiec9a26cf2019-05-10 15:43:12 -0400153
Gilles Depatie0bf31352019-02-04 13:48:41 -0500154def get_all_running_pods():
155 allRunningPods = []
156 proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'],
157 stdout=subprocess.PIPE,
158 stderr=subprocess.PIPE)
159 proc2 = subprocess.Popen(['grep', '-v', 'NAMESPACE'], stdin=proc1.stdout,
160 stdout=subprocess.PIPE,
161 stderr=subprocess.PIPE)
162 proc1.stdout.close()
163 out, err = proc2.communicate()
164 print(out)
165 if out:
166 for line in out.split('\n'):
167 items = line.split()
168 if len(items) > 2:
169 nsName = dict()
170 nsName['NS'] = items[0]
171 nsName['Name'] = items[1]
172 allRunningPods.append(nsName)
173 return allRunningPods
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400174
Gilles Depatie0bf31352019-02-04 13:48:41 -0500175
Gilles Depatie493ea242019-05-21 14:38:08 -0400176def voltha_initialize(root_dir, voltha_dir, log_dir, simtype):
Gilles Depatie0bf31352019-02-04 13:48:41 -0500177 voltha = VolthaMngr()
178 voltha.v_set_log_dirs(root_dir, voltha_dir, log_dir)
Gilles Depatie493ea242019-05-21 14:38:08 -0400179 voltha.stop_all_pods(simtype)
180 voltha.reset_kube_adm(simtype)
181 voltha.start_all_pods(simtype)
Gilles Depatie0bf31352019-02-04 13:48:41 -0500182 voltha.alter_onos_net_cfg()
183 voltha.collect_pod_logs()
Gilles Depatiec9a26cf2019-05-10 15:43:12 -0400184 voltha.discover_freeradius_pod_name()
185 voltha.discover_freeradius_ip_addr()
186 voltha.prepare_current_freeradius_ip()
187 voltha.alter_freeradius_ip_in_onos_aaa_application_configuration()
Gilles Depatie493ea242019-05-21 14:38:08 -0400188 voltha.deactivate_aaa_app_in_onos()
189 time.sleep(5)
190 voltha.activate_aaa_app_in_onos()