|
3 | 3 | # Name: startup_tsa_tsb.py, version: 1.0
|
4 | 4 | #
|
5 | 5 | # Description: Module contains the definitions to the VOQ Startup TSA-TSB service
|
6 |
| - |
| 6 | +from sonic_py_common import multi_asic, device_info |
| 7 | +from sonic_py_common.logger import Logger |
7 | 8 | import subprocess
|
8 | 9 | import sys, getopt
|
9 | 10 | from threading import Timer
|
10 |
| -import os.path |
11 |
| - |
12 |
| -def getPlatform(): |
13 |
| - platform_key = "DEVICE_METADATA['localhost']['platform']" |
14 |
| - platform = (subprocess.check_output(['sonic-cfggen', '-d', '-v', platform_key.replace('"',"'")]).strip()).decode() |
15 |
| - return platform |
| 11 | +import os |
16 | 12 |
|
17 |
| -def getNumAsics(): |
18 |
| - platform = getPlatform() |
19 |
| - asic_config_file = '/usr/share/sonic/device/{}/asic.conf'.format(platform) |
20 |
| - file = open(asic_config_file, 'r') |
21 |
| - Lines = file.readlines() |
22 |
| - for line in Lines: |
23 |
| - field = line.split('=')[0].strip() |
24 |
| - if field == "NUM_ASIC": |
25 |
| - return line.split('=')[1].strip() |
26 |
| - return 0 |
| 13 | +# Global Logger class instance |
| 14 | +logger = Logger("startup_tsa_tsb") |
| 15 | +logger.set_min_log_priority_info() |
27 | 16 |
|
28 |
| -def getTsbTimerInterval(): |
29 |
| - platform = getPlatform() |
| 17 | +def get_tsb_timer_interval(): |
| 18 | + platform = device_info.get_platform() |
30 | 19 | conf_file = '/usr/share/sonic/device/{}/startup-tsa-tsb.conf'.format(platform)
|
31 | 20 | file = open(conf_file, 'r')
|
32 | 21 | Lines = file.readlines()
|
33 | 22 | for line in Lines:
|
34 |
| - field = line.split('=')[0].strip() |
35 |
| - if field == "STARTUP_TSB_TIMER": |
36 |
| - return line.split('=')[1].strip() |
| 23 | + field = line.split('=')[0].strip() |
| 24 | + if field == "STARTUP_TSB_TIMER": |
| 25 | + return line.split('=')[1].strip() |
37 | 26 | return 0
|
38 | 27 |
|
39 |
| -def getSonicConfig(ns, config_name): |
40 |
| - return subprocess.check_output(['sonic-cfggen', '-d', '-v', config_name.replace('"', "'"), '-n', ns.replace('"', "'")]).strip() |
| 28 | +def get_sonic_config(ns, config_name): |
| 29 | + if ns == "": |
| 30 | + return subprocess.check_output(['sonic-cfggen', '-d', '-v', config_name.replace('"', "'"), ]).strip() |
| 31 | + else: |
| 32 | + return subprocess.check_output(['sonic-cfggen', '-d', '-v', config_name.replace('"', "'"), '-n', ns.replace('"', "'")]).strip() |
41 | 33 |
|
42 |
| -def getSubRole(asic_ns): |
| 34 | +def get_sub_role(asic_ns): |
43 | 35 | sub_role_config = "DEVICE_METADATA['localhost']['sub_role']"
|
44 |
| - sub_role = (getSonicConfig(asic_ns, sub_role_config)).decode() |
| 36 | + sub_role = (get_sonic_config(asic_ns, sub_role_config)).decode() |
45 | 37 | return sub_role
|
46 | 38 |
|
47 |
| -def getTsaConfig(asic_ns): |
| 39 | +def get_tsa_config(asic_ns): |
48 | 40 | tsa_config = 'BGP_DEVICE_GLOBAL.STATE.tsa_enabled'
|
49 |
| - tsa_ena = (getSonicConfig(asic_ns, tsa_config)).decode() |
50 |
| - print('{}: {} - CONFIG_DB.{} : {}'.format(__file__, asic_ns, tsa_config, tsa_ena)) |
| 41 | + tsa_ena = (get_sonic_config(asic_ns, tsa_config)).decode() |
| 42 | + if asic_ns == "": |
| 43 | + logger.log_info('CONFIG_DB.{} : {}'.format(tsa_config, tsa_ena)) |
| 44 | + else: |
| 45 | + logger.log_info('{} - CONFIG_DB.{} : {}'.format(asic_ns, tsa_config, tsa_ena)) |
51 | 46 | return tsa_ena
|
52 | 47 |
|
53 |
| -def get_tsa_status(): |
54 |
| - asic_num = getNumAsics() |
55 |
| - counter = 0 |
56 |
| - for asic_id in range(int(asic_num)): |
57 |
| - asic_ns = 'asic{}'.format(asic_id) |
58 |
| - sub_role = getSubRole(asic_ns) |
59 |
| - if sub_role == 'FrontEnd': |
60 |
| - tsa_enabled = getTsaConfig(asic_ns) |
61 |
| - if tsa_enabled == 'false': |
62 |
| - counter += 1 |
63 |
| - if counter == int(asic_num): |
64 |
| - return True; |
| 48 | +def get_tsa_status(num_asics): |
| 49 | + if num_asics > 1: |
| 50 | + counter = 0 |
| 51 | + for asic_id in range(int(asic_num)): |
| 52 | + asic_ns = 'asic{}'.format(asic_id) |
| 53 | + sub_role = get_sub_role(asic_ns) |
| 54 | + if sub_role == 'FrontEnd': |
| 55 | + tsa_enabled = get_tsa_config(asic_ns) |
| 56 | + if tsa_enabled == 'false': |
| 57 | + counter += 1 |
| 58 | + if counter == int(asic_num): |
| 59 | + return True; |
65 | 60 | else:
|
66 |
| - return False; |
| 61 | + tsa_enabled = get_tsa_config("") |
| 62 | + if tsa_enabled == 'false': |
| 63 | + return True; |
| 64 | + return False; |
67 | 65 |
|
68 | 66 | def config_tsa():
|
69 |
| - tsa_ena = get_tsa_status() |
| 67 | + num_asics = multi_asic.get_num_asics() |
| 68 | + tsa_ena = get_tsa_status(num_asics) |
70 | 69 | if tsa_ena == True:
|
71 |
| - print("{}: Configuring TSA".format(__file__)) |
72 |
| - subprocess.check_output(['TSA']).strip() |
| 70 | + logger.log_info("Configuring TSA") |
| 71 | + subprocess.check_output(['TSA']).strip() |
73 | 72 | else:
|
74 |
| - print("{}: Either TSA is already configured or switch sub_role is not Frontend - not configuring TSA".format(__file__)) |
| 73 | + if num_asics > 1: |
| 74 | + logger.log_info("Either TSA is already configured or switch sub_role is not Frontend - not configuring TSA") |
| 75 | + else: |
| 76 | + logger.log_info("Either TSA is already configured - not configuring TSA") |
75 | 77 | return tsa_ena
|
76 | 78 |
|
77 | 79 | def config_tsb():
|
78 |
| - print("startup_tsa_tsb: Configuring TSB") |
| 80 | + logger.log_info("startup_tsa_tsb: Configuring TSB") |
79 | 81 | subprocess.check_output(['TSB']).strip()
|
80 | 82 | tsb_issued = True
|
81 | 83 | return
|
82 | 84 |
|
83 | 85 | def start_tsb_timer(interval):
|
84 | 86 | global timer
|
85 |
| - print("{}: Starting timer with interval {} seconds to configure TSB".format(__file__, interval)) |
| 87 | + logger.log_info("Starting timer with interval {} seconds to configure TSB".format(interval)) |
86 | 88 | timer = Timer(int(interval), config_tsb)
|
87 | 89 | timer.start()
|
88 | 90 | timer.join()
|
89 | 91 | return
|
90 | 92 |
|
91 | 93 | def print_usage():
|
92 |
| - print ("Usage: startup_tsa_tsb.py [options] command") |
93 |
| - print ("options:") |
94 |
| - print(" -h | --help : this help message") |
95 |
| - print("command:") |
96 |
| - print("start : start the TSA/TSB") |
97 |
| - print("stop : stop the TSA/TSB") |
| 94 | + logger.log_info("Usage: startup_tsa_tsb.py [options] command") |
| 95 | + logger.log_info("options:") |
| 96 | + logger.log_info(" -h | --help : this help message") |
| 97 | + logger.log_info("command:") |
| 98 | + logger.log_info("start : start the TSA/TSB") |
| 99 | + logger.log_info("stop : stop the TSA/TSB") |
98 | 100 | return
|
99 | 101 |
|
100 |
| -def start_tsa_tsb(timer): |
| 102 | +def reset_env_variables(): |
| 103 | + logger.log_info("Resetting environment variable") |
| 104 | + os.environ.pop('STARTED_BY_TSA_TSB_SERVICE') |
| 105 | + return |
101 | 106 |
|
| 107 | +def start_tsa_tsb(timer): |
102 | 108 | #Configure TSA if it was not configured already in CONFIG_DB
|
103 | 109 | tsa_enabled = config_tsa()
|
104 | 110 | if tsa_enabled == True:
|
105 |
| - #Start the timer to configure TSB |
106 |
| - start_tsb_timer(timer) |
| 111 | + #Start the timer to configure TSB |
| 112 | + start_tsb_timer(timer) |
107 | 113 | return
|
108 | 114 |
|
109 | 115 | def stop_tsa_tsb():
|
110 |
| - #for future use |
| 116 | + reset_env_variables() |
111 | 117 | return
|
112 | 118 |
|
113 | 119 | def main():
|
114 |
| - platform = getPlatform() |
| 120 | + platform = device_info.get_platform() |
115 | 121 | conf_file = '/usr/share/sonic/device/{}/startup-tsa-tsb.conf'.format(platform)
|
116 | 122 | #This check should be moved to service file or make this feature as configurable.
|
117 | 123 | #Adding it here for now.
|
118 | 124 | if not os.path.exists(conf_file):
|
119 |
| - print ("{} does not exist, exiting the service".format(conf_file)) |
120 |
| - return |
| 125 | + logger.log_info("{} does not exist, exiting the service".format(conf_file)) |
| 126 | + return |
121 | 127 | if len(sys.argv) <= 1:
|
122 |
| - print_usage() |
123 |
| - return |
| 128 | + print_usage() |
| 129 | + return |
124 | 130 |
|
125 | 131 | # parse command line options:
|
126 | 132 | try:
|
127 |
| - opts, args = getopt.getopt(sys.argv[1:], 'h:', ['help' ]) |
| 133 | + opts, args = getopt.getopt(sys.argv[1:], 'h:', ['help' ]) |
128 | 134 | except getopt.GetoptError:
|
129 |
| - print_usage() |
130 |
| - return |
| 135 | + print_usage() |
| 136 | + return |
131 | 137 |
|
132 | 138 | for opt, arg in opts:
|
133 |
| - if opt in ("-h", "--help"): |
134 |
| - print_usage() |
135 |
| - return |
| 139 | + if opt in ("-h", "--help"): |
| 140 | + print_usage() |
| 141 | + return |
136 | 142 |
|
137 | 143 | for arg in args:
|
138 | 144 | if arg == 'start':
|
139 |
| - tsb_timer = getTsbTimerInterval() |
140 |
| - start_tsa_tsb(tsb_timer) |
| 145 | + tsb_timer = get_tsb_timer_interval() |
| 146 | + start_tsa_tsb(tsb_timer) |
141 | 147 | elif arg == 'stop':
|
142 |
| - stop_tsa_tsb() |
| 148 | + stop_tsa_tsb() |
143 | 149 | else:
|
144 |
| - print_usage() |
145 |
| - return |
| 150 | + print_usage() |
| 151 | + return |
146 | 152 |
|
147 | 153 | return
|
148 | 154 |
|
|
0 commit comments