blob: 1751fa45febe2ee69a082f1d2374612047981c1c [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 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
35 DEFAULT_ADAPTER = 'ponsim'
36
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 Depatiec68b3ad2018-08-21 16:29:03 -040042
Gilles Depatie0bf31352019-02-04 13:48:41 -050043 def v_set_log_dirs(self, root_dir, voltha_dir, log_dir):
44 testCaseUtils.config_dirs(self, log_dir, root_dir, voltha_dir)
Gilles Depatieea423712019-04-12 16:39:12 -040045
46 def start_all_pods(self, adapter=DEFAULT_ADAPTER):
47 proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'start', adapter],
Gilles Depatiec68b3ad2018-08-21 16:29:03 -040048 stdout=subprocess.PIPE,
49 stderr=subprocess.PIPE)
Gilles Depatie84cb1e72018-10-26 12:41:33 -040050 output = proc1.communicate()[0]
51 print(output)
Gilles Depatie0bf31352019-02-04 13:48:41 -050052 proc1.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040053
Gilles Depatieea423712019-04-12 16:39:12 -040054 def stop_all_pods(self, adapter=DEFAULT_ADAPTER):
55 proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'stop', adapter],
Gilles Depatie84cb1e72018-10-26 12:41:33 -040056 stdout=subprocess.PIPE,
57 stderr=subprocess.PIPE)
58 output = proc1.communicate()[0]
59 print(output)
Gilles Depatie0bf31352019-02-04 13:48:41 -050060 proc1.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040061
Gilles Depatieea423712019-04-12 16:39:12 -040062 def reset_kube_adm(self, adapter=DEFAULT_ADAPTER):
63 proc1 = subprocess.Popen([testCaseUtils.get_dir(self, 'root') + '/build.sh', 'clear', adapter],
Gilles Depatie84cb1e72018-10-26 12:41:33 -040064 stdout=subprocess.PIPE,
65 stderr=subprocess.PIPE)
66 output = proc1.communicate()[0]
67 print(output)
Gilles Depatie0bf31352019-02-04 13:48:41 -050068 proc1.stdout.close()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040069
70 """
71 Because we are not deploying SEBA with XOS and NEM, and that a standalone Voltha
72 deployment is not common, in order to get flows to work, we need to alter Onos
73 NetCfg in two fashion.
74 One is to add to device list and the other is to add the missing Sadis section
75 """
Gilles Depatie0bf31352019-02-04 13:48:41 -050076 def alter_onos_net_cfg(self):
77 logging.info('Altering the Onos NetCfg to suit Voltha\'s needs')
78 logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
79 'http://localhost:30120/onos/v1/network/configuration/devices/ -d @%s/tests/atests/build/devices_json'
80 % testCaseUtils.get_dir(self, 'voltha'))
Gilles Depatie84cb1e72018-10-26 12:41:33 -040081 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
Gilles Depatie0bf31352019-02-04 13:48:41 -050082 'http://localhost:30120/onos/v1/network/configuration/devices/ -d @%s/tests/atests/build/devices_json'
83 % testCaseUtils.get_dir(self, 'voltha'))
84 logging.debug('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
85 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/sadis_json'
86 % testCaseUtils.get_dir(self, 'voltha'))
Gilles Depatie84cb1e72018-10-26 12:41:33 -040087 os.system('curl --user karaf:karaf -X POST -H "Content-Type: application/json" '
Gilles Depatie0bf31352019-02-04 13:48:41 -050088 'http://localhost:30120/onos/v1/network/configuration/apps/ -d @%s/tests/atests/build/sadis_json'
89 % testCaseUtils.get_dir(self, 'voltha'))
90
91 def collect_pod_logs(self):
Gilles Depatie1be639b2018-12-06 10:51:08 -050092 logging.info('Collect logs from all Pods')
Gilles Depatie0bf31352019-02-04 13:48:41 -050093 allRunningPods = get_all_running_pods()
Gilles Depatie84cb1e72018-10-26 12:41:33 -040094 for nsName in allRunningPods:
95 Namespace = nsName.get('NS')
Gilles Depatie0bf31352019-02-04 13:48:41 -050096 podName = nsName.get('Name')
Gilles Depatie82dd2022019-02-19 14:31:33 -050097 if 'onos' in podName:
Kailash8ae83ac2019-02-11 12:23:52 -080098 os.system('/usr/bin/kubectl logs -n %s -f %s onos > %s/%s.log 2>&1 &' %
Gilles Depatie0bf31352019-02-04 13:48:41 -050099 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
Gilles Depatie82dd2022019-02-19 14:31:33 -0500100 elif 'calico-node' in podName:
101 os.system('/usr/bin/kubectl logs -n %s -f %s calico-node > %s/%s.log 2>&1 &' %
102 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
103 else:
104 os.system('/usr/bin/kubectl logs -n %s -f %s > %s/%s.log 2>&1 &' %
105 (Namespace, podName, testCaseUtils.get_dir(self, 'log'), podName))
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400106
Gilles Depatie84cb1e72018-10-26 12:41:33 -0400107
Gilles Depatie0bf31352019-02-04 13:48:41 -0500108def get_all_running_pods():
109 allRunningPods = []
110 proc1 = subprocess.Popen(['/usr/bin/kubectl', 'get', 'pods', '--all-namespaces'],
111 stdout=subprocess.PIPE,
112 stderr=subprocess.PIPE)
113 proc2 = subprocess.Popen(['grep', '-v', 'NAMESPACE'], stdin=proc1.stdout,
114 stdout=subprocess.PIPE,
115 stderr=subprocess.PIPE)
116 proc1.stdout.close()
117 out, err = proc2.communicate()
118 print(out)
119 if out:
120 for line in out.split('\n'):
121 items = line.split()
122 if len(items) > 2:
123 nsName = dict()
124 nsName['NS'] = items[0]
125 nsName['Name'] = items[1]
126 allRunningPods.append(nsName)
127 return allRunningPods
Gilles Depatiec68b3ad2018-08-21 16:29:03 -0400128
Gilles Depatie0bf31352019-02-04 13:48:41 -0500129
Gilles Depatieea423712019-04-12 16:39:12 -0400130def voltha_initialize(root_dir, voltha_dir, log_dir, adapter):
Gilles Depatie0bf31352019-02-04 13:48:41 -0500131 voltha = VolthaMngr()
132 voltha.v_set_log_dirs(root_dir, voltha_dir, log_dir)
Gilles Depatieea423712019-04-12 16:39:12 -0400133 voltha.stop_all_pods(adapter)
134 voltha.reset_kube_adm(adapter)
135 voltha.start_all_pods(adapter)
Gilles Depatie0bf31352019-02-04 13:48:41 -0500136 voltha.alter_onos_net_cfg()
137 voltha.collect_pod_logs()