blob: f94322dcee5505331c6069c7f2d35f5a9bf2d7c2 [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 Depatiec68b3ad2018-08-21 16:29:03 -040026
Gilles Depatie0bf31352019-02-04 13:48:41 -050027
Gilles Depatie84cb1e72018-10-26 12:41:33 -040028class VolthaMngr(object):
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040029
30 """
31 This class implements voltha startup/shutdown callable helper functions
32 """
Gilles Depatieea423712019-04-12 16:39:12 -040033
34 DEFAULT_ADAPTER = 'ponsim'
35
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040036 def __init__(self):
Gilles Depatie0bf31352019-02-04 13:48:41 -050037 self.dirs = dict()
38 self.dirs['root'] = None
39 self.dirs['voltha'] = None
40 self.dirs['log'] = None
Gilles Depatiec9a26cf2019-05-10 15:43:12 -040041
42 self.__radiusName = None
43 self.__radiusIp = None
44
Gilles Depatie0bf31352019-02-04 13:48:41 -050045 def v_set_log_dirs(self, root_dir, voltha_dir, log_dir):
46 testCaseUtils.config_dirs(self, log_dir, root_dir, voltha_dir)
Gilles Depatieea423712019-04-12 16:39:12 -040047
48 def start_all_pods(self, adapter=DEFAULT_ADAPTER):
49 proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'start', adapter],
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040050 stdout=subprocess.PIPE,
51 stderr=subprocess.PIPE)
Gilles Depatie84cb1e72018-10-26 12:41:33 -040052 output = proc1.communicate()[0]
53 print(output)
Gilles Depatie0bf31352019-02-04 13:48:41 -050054 proc1.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040055
Gilles Depatieea423712019-04-12 16:39:12 -040056 def stop_all_pods(self, adapter=DEFAULT_ADAPTER):
57 proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'stop', adapter],
Gilles Depatie84cb1e72018-10-26 12:41:33 -040058 stdout=subprocess.PIPE,
59 stderr=subprocess.PIPE)
60 output = proc1.communicate()[0]
61 print(output)
Gilles Depatie0bf31352019-02-04 13:48:41 -050062 proc1.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040063
Gilles Depatieea423712019-04-12 16:39:12 -040064 def reset_kube_adm(self, adapter=DEFAULT_ADAPTER):
65 proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'clear', adapter],
Gilles Depatie84cb1e72018-10-26 12:41:33 -040066 stdout=subprocess.PIPE,
67 stderr=subprocess.PIPE)
68 output = proc1.communicate()[0]
69 print(output)
Gilles Depatie0bf31352019-02-04 13:48:41 -050070 proc1.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040071
72 """
73 Because we are not deploying SEBA with XOS and NEM, and that a standalone Voltha
74 deployment is not common, in order to get flows to work, we need to alter Onos
75 NetCfg in two fashion.
76 One is to add to device list and the other is to add the missing Sadis section
77 """
Gilles Depatie0bf31352019-02-04 13:48:41 -050078 def alter_onos_net_cfg(self):
79 logging.info('Altering the Onos NetCfg to suit Voltha\'s needs')
80 logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
81 'http://localhost:30120/onos/v1/network/configuration/devices/ -d @%s/tests/atests/build/devices_json'
82 % testCaseUtils.get_dir(self, 'voltha'))
Gilles Depatie84cb1e72018-10-26 12:41:33 -040083 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
Gilles Depatie0bf31352019-02-04 13:48:41 -050084 'http://localhost:30120/onos/v1/network/configuration/devices/ -d @%s/tests/atests/build/devices_json'
85 % testCaseUtils.get_dir(self, 'voltha'))
86 logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
87 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/sadis_json'
88 % testCaseUtils.get_dir(self, 'voltha'))
Gilles Depatie84cb1e72018-10-26 12:41:33 -040089 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
Gilles Depatie0bf31352019-02-04 13:48:41 -050090 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/sadis_json'
91 % testCaseUtils.get_dir(self, 'voltha'))
92
93 def collect_pod_logs(self):
Gilles Depatie1be639b2018-12-06 10:51:08 -050094 logging.info('Collect logs from all Pods')
Gilles Depatie0bf31352019-02-04 13:48:41 -050095 allRunningPods = get_all_running_pods()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040096 for nsName in allRunningPods:
97 Namespace = nsName.get('NS')
Gilles Depatie0bf31352019-02-04 13:48:41 -050098 podName = nsName.get('Name')
Gilles Depatie82dd2022019-02-19 14:31:33 -050099 if 'onos' in podName:
Kailash8ae83ac2019-02-11 12:23:52 -0800100 os.system('/usr/bin/kubectl logs -n %s -f %s onos > %s/%s.log 2>&1 &' %
Gilles Depatie0bf31352019-02-04 13:48:41 -0500101 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
Gilles Depatie82dd2022019-02-19 14:31:33 -0500102 elif 'calico-node' in podName:
Gilles Depatiec9a26cf2019-05-10 15:43:12 -0400103 os.system('/usr/bin/kubectl logs -n %s -f %s calico-node > %s/%s.log 2>&1 &' %
104 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
Gilles Depatie82dd2022019-02-19 14:31:33 -0500105 else:
106 os.system('/usr/bin/kubectl logs -n %s -f %s > %s/%s.log 2>&1 &' %
107 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400108
Gilles Depatiec9a26cf2019-05-10 15:43:12 -0400109 def discover_freeradius_pod_name(self):
110 self.__radiusName = testCaseUtils.extract_pod_name('freeradius').strip()
111 logging.info('freeradius Name = %s' % self.__radiusName)
112
113 def discover_freeradius_ip_addr(self):
114 ipAddr = testCaseUtils.extract_radius_ip_addr(self.__radiusName)
115 assert ipAddr, 'No IP address listed for freeradius'
116 self.__radiusIp = ipAddr.strip()
117 logging.info('freeradius IP = %s' % self.__radiusIp)
118
119 def prepare_current_freeradius_ip(self):
120 status = testCaseUtils.modify_radius_ip_in_json_using_sed(self, self.__radiusIp)
121 assert (status == 0), 'Setting Radius Ip in Json File did not return Success'
122
123 def alter_freeradius_ip_in_onos_aaa_application_configuration(self):
124 logging.info('Altering the Onos NetCfg AAA apps with Freeradius IP address')
125 logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
126 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/aaa_json'
127 % testCaseUtils.get_dir(self, 'voltha'))
128 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
129 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/aaa_json'
130 % testCaseUtils.get_dir(self, 'voltha'))
131
132
Gilles Depatie0bf31352019-02-04 13:48:41 -0500133def get_all_running_pods():
134 allRunningPods = []
135 proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'],
136 stdout=subprocess.PIPE,
137 stderr=subprocess.PIPE)
138 proc2 = subprocess.Popen(['grep', '-v', 'NAMESPACE'], stdin=proc1.stdout,
139 stdout=subprocess.PIPE,
140 stderr=subprocess.PIPE)
141 proc1.stdout.close()
142 out, err = proc2.communicate()
143 print(out)
144 if out:
145 for line in out.split('\n'):
146 items = line.split()
147 if len(items) > 2:
148 nsName = dict()
149 nsName['NS'] = items[0]
150 nsName['Name'] = items[1]
151 allRunningPods.append(nsName)
152 return allRunningPods
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400153
Gilles Depatie0bf31352019-02-04 13:48:41 -0500154
Gilles Depatieea423712019-04-12 16:39:12 -0400155def voltha_initialize(root_dir, voltha_dir, log_dir, adapter):
Gilles Depatie0bf31352019-02-04 13:48:41 -0500156 voltha = VolthaMngr()
157 voltha.v_set_log_dirs(root_dir, voltha_dir, log_dir)
Gilles Depatieea423712019-04-12 16:39:12 -0400158 voltha.stop_all_pods(adapter)
159 voltha.reset_kube_adm(adapter)
160 voltha.start_all_pods(adapter)
Gilles Depatie0bf31352019-02-04 13:48:41 -0500161 voltha.alter_onos_net_cfg()
162 voltha.collect_pod_logs()
Gilles Depatiec9a26cf2019-05-10 15:43:12 -0400163 voltha.discover_freeradius_pod_name()
164 voltha.discover_freeradius_ip_addr()
165 voltha.prepare_current_freeradius_ip()
166 voltha.alter_freeradius_ip_in_onos_aaa_application_configuration()