|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# ledd |
| 4 | +# |
| 5 | +# Front-panel LED control daemon for SONiC |
| 6 | +# |
| 7 | + |
| 8 | +try: |
| 9 | + import ast |
| 10 | + import getopt |
| 11 | + import os |
| 12 | + import imp |
| 13 | + import signal |
| 14 | + import subprocess |
| 15 | + import swsssdk |
| 16 | + import sys |
| 17 | + import syslog |
| 18 | + import time |
| 19 | +except ImportError, e: |
| 20 | + raise ImportError (str(e) + " - required module not found") |
| 21 | + |
| 22 | +#============================= Constants ============================= |
| 23 | + |
| 24 | +VERSION = '1.0' |
| 25 | + |
| 26 | +USAGE_HELP=""" |
| 27 | +Usage: ledd [options] |
| 28 | +
|
| 29 | +Options: |
| 30 | + -h,--help Print this usage statement and exit |
| 31 | + -v,--version Print version information and exit |
| 32 | +""" |
| 33 | + |
| 34 | +SYSLOG_IDENTIFIER = "ledd" |
| 35 | + |
| 36 | +LED_MODULE_NAME = "led_control" |
| 37 | +LED_CLASS_NAME = "LedControl" |
| 38 | + |
| 39 | +SONIC_CFGGEN = "/usr/local/bin/sonic-cfggen" |
| 40 | +MINIGRAPH_FILE = "/etc/sonic/minigraph.xml" |
| 41 | +HWSKU_KEY = "minigraph_hwsku" |
| 42 | +PLATFORM_KEY = "platform" |
| 43 | + |
| 44 | +PLATFORM_ROOT = "/usr/share/sonic/device" |
| 45 | + |
| 46 | +PORT_TABLE_PREFIX = "PORT_TABLE:" |
| 47 | + |
| 48 | +led_control = None |
| 49 | + |
| 50 | +#========================== Syslog wrappers ========================== |
| 51 | + |
| 52 | +def log_info(msg): |
| 53 | + syslog.openlog(SYSLOG_IDENTIFIER) |
| 54 | + syslog.syslog(syslog.LOG_INFO, msg) |
| 55 | + syslog.closelog() |
| 56 | + |
| 57 | +def log_warning(msg): |
| 58 | + syslog.openlog(SYSLOG_IDENTIFIER) |
| 59 | + syslog.syslog(syslog.LOG_WARNING, msg) |
| 60 | + syslog.closelog() |
| 61 | + |
| 62 | +def log_error(msg): |
| 63 | + syslog.openlog(SYSLOG_IDENTIFIER) |
| 64 | + syslog.syslog(syslog.LOG_ERR, msg) |
| 65 | + syslog.closelog() |
| 66 | + |
| 67 | +#========================== Signal Handling ========================== |
| 68 | + |
| 69 | +def signal_handler(sig, frame): |
| 70 | + if sig == signal.SIGHUP: |
| 71 | + log_info("Caught SIGHUP - ignoring...\n") |
| 72 | + return |
| 73 | + elif sig == signal.SIGINT: |
| 74 | + log_info("Caught SIGINT - exiting...\n") |
| 75 | + sys.exit(0) |
| 76 | + elif sig == signal.SIGTERM: |
| 77 | + log_info("Caught SIGTERM - exiting...\n") |
| 78 | + sys.exit(0) |
| 79 | + else: |
| 80 | + log_warning("Caught unhandled signal '" + sig + "'") |
| 81 | + |
| 82 | +#============ Functions to load platform-specific classes ============ |
| 83 | + |
| 84 | +# Returns platform and HW SKU |
| 85 | +def get_platform_and_hwsku(): |
| 86 | + try: |
| 87 | + proc = subprocess.Popen([SONIC_CFGGEN, '-v', PLATFORM_KEY], |
| 88 | + stdout=subprocess.PIPE, |
| 89 | + shell=False, |
| 90 | + stderr=subprocess.STDOUT) |
| 91 | + stdout = proc.communicate()[0] |
| 92 | + proc.wait() |
| 93 | + platform = stdout.rstrip('\n') |
| 94 | + |
| 95 | + proc = subprocess.Popen([SONIC_CFGGEN, '-m', MINIGRAPH_FILE, '-v', HWSKU_KEY], |
| 96 | + stdout=subprocess.PIPE, |
| 97 | + shell=False, |
| 98 | + stderr=subprocess.STDOUT) |
| 99 | + stdout = proc.communicate()[0] |
| 100 | + proc.wait() |
| 101 | + hwsku = stdout.rstrip('\n') |
| 102 | + except OSError, e: |
| 103 | + log_error("Cannot detect platform") |
| 104 | + raise OSError("Cannot detect platform") |
| 105 | + |
| 106 | + return (platform, hwsku) |
| 107 | + |
| 108 | + |
| 109 | +# Loads platform-specific LED control module from source |
| 110 | +def load_platform_led_control_module(): |
| 111 | + global led_control |
| 112 | + |
| 113 | + # Get platform and hwsku |
| 114 | + (platform, hwsku) = get_platform_and_hwsku() |
| 115 | + |
| 116 | + # Load platform module from source |
| 117 | + platform_path = '/'.join([PLATFORM_ROOT, platform]) |
| 118 | + hwsku_path = '/'.join([platform_path, hwsku]) |
| 119 | + |
| 120 | + module_file = '/'.join([platform_path, 'plugins', LED_MODULE_NAME + '.py']) |
| 121 | + |
| 122 | + # If we can't locate a platform-specific module, exit gracefully, assuming this |
| 123 | + # platform utilizes a hardware-based LED control solution |
| 124 | + if not os.path.isfile(module_file): |
| 125 | + log_info("Failed to locate platform-specific %s module. Exiting..." % LED_MODULE_NAME) |
| 126 | + sys.exit(0) |
| 127 | + |
| 128 | + try: |
| 129 | + module = imp.load_source(LED_MODULE_NAME, module_file) |
| 130 | + except IOError, e: |
| 131 | + log_error("Failed to load platform module '%s': %s" % (LED_MODULE_NAME, str(e))) |
| 132 | + return -1 |
| 133 | + |
| 134 | + log_info("Loaded module '%s'." % LED_MODULE_NAME) |
| 135 | + |
| 136 | + try: |
| 137 | + led_control_class = getattr(module, LED_CLASS_NAME) |
| 138 | + led_control = led_control_class() |
| 139 | + except AttributeError, e: |
| 140 | + log_error("Failed to instantiate '%s' class: %s" % (LED_CLASS_NAME, str(e))) |
| 141 | + return -2 |
| 142 | + |
| 143 | + log_info("Instantiated class '%s.%s'." % (LED_MODULE_NAME, LED_CLASS_NAME)) |
| 144 | + |
| 145 | + return 0 |
| 146 | + |
| 147 | +#=============================== Main ================================ |
| 148 | + |
| 149 | +def main(): |
| 150 | + port_status_dict = {} |
| 151 | + |
| 152 | + log_info("Starting up...") |
| 153 | + |
| 154 | + if not os.geteuid() == 0: |
| 155 | + log_error("Must be root to run this daemon") |
| 156 | + print "Error: Must be root to run this daemon" |
| 157 | + sys.exit(1) |
| 158 | + |
| 159 | + # Parse options if provided |
| 160 | + if (len(sys.argv) > 1): |
| 161 | + try: |
| 162 | + options, remainder = getopt.getopt(sys.argv[1:], |
| 163 | + 'hv', |
| 164 | + ['help', |
| 165 | + 'version']) |
| 166 | + except getopt.GetoptError, e: |
| 167 | + print e |
| 168 | + print USAGE_HELP |
| 169 | + sys.exit(2) |
| 170 | + |
| 171 | + for opt, arg in options: |
| 172 | + if opt == '--help' or opt == '-h': |
| 173 | + print USAGE_HELP |
| 174 | + sys.exit(0) |
| 175 | + elif opt == '--version' or opt == '-v': |
| 176 | + print 'ledd version ' + VERSION |
| 177 | + sys.exit(0) |
| 178 | + |
| 179 | + # Register our signal handlers |
| 180 | + signal.signal(signal.SIGHUP, signal_handler) |
| 181 | + signal.signal(signal.SIGINT, signal_handler) |
| 182 | + signal.signal(signal.SIGTERM, signal_handler) |
| 183 | + |
| 184 | + # Load platform-specific LedControl class |
| 185 | + err = load_platform_led_control_module() |
| 186 | + if err != 0: |
| 187 | + sys.exit(1) |
| 188 | + |
| 189 | + # Connect to APPL_DB using SwSS SDK |
| 190 | + swss = swsssdk.SonicV2Connector() |
| 191 | + swss.connect(swss.APPL_DB) |
| 192 | + |
| 193 | + # Loop forever |
| 194 | + while True: |
| 195 | + # TODO: Move dictionary creation and population out of while loop. Do it only once |
| 196 | + # and subscribe for notifications from DB when ports come or go. Add/remove |
| 197 | + # dictionary entries as necessary. |
| 198 | + |
| 199 | + # Get a list of all ports from the database |
| 200 | + port_table_keys = swss.keys(swss.APPL_DB, PORT_TABLE_PREFIX + '*'); |
| 201 | + |
| 202 | + # Create a dictionary of <sonic_port_name>:<link_status> containing all ports |
| 203 | + # Initially set all statuses to 'down' |
| 204 | + for key in port_table_keys: |
| 205 | + # Remove table name prefix |
| 206 | + port_name = key[len(PORT_TABLE_PREFIX):] |
| 207 | + |
| 208 | + # TODO: Once the DB is fixed and this 'ConfigDone' entry is gone, remove this check |
| 209 | + if port_name == 'ConfigDone': |
| 210 | + continue |
| 211 | + |
| 212 | + port_status_dict[port_name] = 'down' |
| 213 | + led_control.port_link_state_change(port_name, port_status_dict[port_name]) |
| 214 | + |
| 215 | + for (key, value) in port_status_dict.iteritems(): |
| 216 | + status = swss.get(swss.APPL_DB, PORT_TABLE_PREFIX + key, 'oper_status') |
| 217 | + |
| 218 | + # If the status has changed, update it in our dictionary and report to our plugin |
| 219 | + if value != status: |
| 220 | + port_status_dict[key] = status |
| 221 | + led_control.port_link_state_change(key, status) |
| 222 | + |
| 223 | + time.sleep(1) |
| 224 | + pass |
| 225 | + |
| 226 | +if __name__ == '__main__': |
| 227 | + main() |
| 228 | + |
0 commit comments