Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 1 | # |
| 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 | """ |
| 18 | vOLT-HA Start/Stop module |
| 19 | """ |
| 20 | |
| 21 | |
| 22 | import os |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 23 | import subprocess |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 24 | import testCaseUtils |
Gilles Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 25 | import logging |
Kailash | 90d4c05 | 2019-06-25 09:47:19 -0700 | [diff] [blame] | 26 | import time |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 27 | |
Gilles Depatie | ea42371 | 2019-04-12 16:39:12 -0400 | [diff] [blame] | 28 | |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 29 | class VolthaMngr(object): |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 30 | |
| 31 | """ |
| 32 | This class implements voltha startup/shutdown callable helper functions |
| 33 | """ |
Gilles Depatie | ea42371 | 2019-04-12 16:39:12 -0400 | [diff] [blame] | 34 | |
Kailash | 90d4c05 | 2019-06-25 09:47:19 -0700 | [diff] [blame] | 35 | DEFAULT_SIMTYPE = 'ponsim' |
Gilles Depatie | ea42371 | 2019-04-12 16:39:12 -0400 | [diff] [blame] | 36 | |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 37 | def __init__(self): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 38 | self.dirs = dict() |
| 39 | self.dirs['root'] = None |
| 40 | self.dirs['voltha'] = None |
| 41 | self.dirs['log'] = None |
Kailash | 90d4c05 | 2019-06-25 09:47:19 -0700 | [diff] [blame] | 42 | |
| 43 | self.__radiusName = None |
| 44 | self.__radiusIp = None |
| 45 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 46 | def v_set_log_dirs(self, root_dir, voltha_dir, log_dir): |
| 47 | testCaseUtils.config_dirs(self, log_dir, root_dir, voltha_dir) |
Gilles Depatie | ea42371 | 2019-04-12 16:39:12 -0400 | [diff] [blame] | 48 | |
Kailash | 90d4c05 | 2019-06-25 09:47:19 -0700 | [diff] [blame] | 49 | def start_all_pods(self, simtype=DEFAULT_SIMTYPE): |
| 50 | proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'start', simtype], |
Gilles Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 51 | stdout=subprocess.PIPE, |
| 52 | stderr=subprocess.PIPE) |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 53 | output = proc1.communicate()[0] |
| 54 | print(output) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 55 | proc1.stdout.close() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 56 | |
Kailash | 90d4c05 | 2019-06-25 09:47:19 -0700 | [diff] [blame] | 57 | def stop_all_pods(self, simtype=DEFAULT_SIMTYPE): |
| 58 | proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'stop', simtype], |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 59 | stdout=subprocess.PIPE, |
| 60 | stderr=subprocess.PIPE) |
| 61 | output = proc1.communicate()[0] |
| 62 | print(output) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 63 | proc1.stdout.close() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 64 | |
Kailash | 90d4c05 | 2019-06-25 09:47:19 -0700 | [diff] [blame] | 65 | def reset_kube_adm(self, simtype=DEFAULT_SIMTYPE): |
| 66 | proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'clear', simtype], |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 67 | stdout=subprocess.PIPE, |
| 68 | stderr=subprocess.PIPE) |
| 69 | output = proc1.communicate()[0] |
| 70 | print(output) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 71 | proc1.stdout.close() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 72 | |
| 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 Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 79 | 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 Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 84 | os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" ' |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 85 | '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 Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 90 | os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" ' |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 91 | '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 Depatie | 1be639b | 2018-12-06 10:51:08 -0500 | [diff] [blame] | 95 | logging.info('Collect logs from all Pods') |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 96 | allRunningPods = get_all_running_pods() |
Gilles Depatie | 84cb1e7 | 2018-10-26 12:41:33 -0400 | [diff] [blame] | 97 | for nsName in allRunningPods: |
| 98 | Namespace = nsName.get('NS') |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 99 | podName = nsName.get('Name') |
Gilles Depatie | 82dd202 | 2019-02-19 14:31:33 -0500 | [diff] [blame] | 100 | if 'onos' in podName: |
Kailash | 8ae83ac | 2019-02-11 12:23:52 -0800 | [diff] [blame] | 101 | os.system('/usr/bin/kubectl logs -n %s -f %s onos > %s/%s.log 2>&1 &' % |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 102 | (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName)) |
Gilles Depatie | 82dd202 | 2019-02-19 14:31:33 -0500 | [diff] [blame] | 103 | elif 'calico-node' in podName: |
Kailash | 90d4c05 | 2019-06-25 09:47:19 -0700 | [diff] [blame] | 104 | 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 Depatie | 82dd202 | 2019-02-19 14:31:33 -0500 | [diff] [blame] | 106 | 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 Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 109 | |
Kailash | 90d4c05 | 2019-06-25 09:47:19 -0700 | [diff] [blame] | 110 | 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 Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 148 | def 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 Depatie | c68b3ad | 2018-08-21 16:29:03 -0400 | [diff] [blame] | 168 | |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 169 | |
Kailash | 90d4c05 | 2019-06-25 09:47:19 -0700 | [diff] [blame] | 170 | def voltha_initialize(root_dir, voltha_dir, log_dir, simtype): |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 171 | voltha = VolthaMngr() |
| 172 | voltha.v_set_log_dirs(root_dir, voltha_dir, log_dir) |
Kailash | 90d4c05 | 2019-06-25 09:47:19 -0700 | [diff] [blame] | 173 | voltha.stop_all_pods(simtype) |
| 174 | voltha.reset_kube_adm(simtype) |
| 175 | voltha.start_all_pods(simtype) |
Gilles Depatie | 0bf3135 | 2019-02-04 13:48:41 -0500 | [diff] [blame] | 176 | voltha.alter_onos_net_cfg() |
| 177 | voltha.collect_pod_logs() |
Kailash | 90d4c05 | 2019-06-25 09:47:19 -0700 | [diff] [blame] | 178 | 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() |