diff --git a/platform/pddf/i2c/utils/pddf_util.py b/platform/pddf/i2c/utils/pddf_util.py index 299ec18babd9..fdefc8933c15 100755 --- a/platform/pddf/i2c/utils/pddf_util.py +++ b/platform/pddf/i2c/utils/pddf_util.py @@ -15,7 +15,6 @@ switch-nonpddf : switch to per platform, non-pddf mode """ -import commands import logging import getopt import os @@ -120,7 +119,7 @@ def my_log(txt): def log_os_system(cmd, show): logging.info('Run :'+cmd) - status, output = commands.getstatusoutput(cmd) + status, output = subprocess.getstatusoutput(cmd) my_log (cmd +"with result:" + str(status)) my_log (" output:"+output) if status: @@ -138,33 +137,9 @@ def driver_check(): return False return True - -# Returns platform and HW SKU -def get_platform_and_hwsku(): - try: - proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY], - stdout=subprocess.PIPE, - shell=False, - stderr=subprocess.STDOUT) - stdout = proc.communicate()[0] - proc.wait() - platform = stdout.rstrip('\n') - - proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-d', '-v', HWSKU_KEY], - stdout=subprocess.PIPE, - shell=False, - stderr=subprocess.STDOUT) - stdout = proc.communicate()[0] - proc.wait() - hwsku = stdout.rstrip('\n') - except OSError, e: - raise OSError("Cannot detect platform") - - return (platform, hwsku) - def get_path_to_device(): # Get platform and hwsku - (platform, hwsku) = get_platform_and_hwsku() + (platform, hwsku) = pddf_obj.get_platform_and_hwsku() # Load platform module from source platform_path = "/".join([PLATFORM_ROOT_PATH, platform]) @@ -193,14 +168,20 @@ def config_pddf_utils(): log_os_system('mv '+SONIC_PLATFORM_BSP_WHL_PKG+' '+SONIC_PLATFORM_BSP_WHL_PKG_BK, 1) # PDDF whl package exist ... this must be the whl package created from # PDDF 2.0 ref API classes and some changes on top of it ... install it + log_os_system('sync', 1) shutil.copy(SONIC_PLATFORM_PDDF_WHL_PKG, SONIC_PLATFORM_BSP_WHL_PKG) + log_os_system('sync', 1) print("Attemting to install the PDDF sonic_platform wheel package ...") - status, output = log_os_system("pip3 install "+ SONIC_PLATFORM_BSP_WHL_PKG, 1) - if status: - print("Error: Failed to install {}".format(SONIC_PLATFORM_BSP_WHL_PKG)) - return status + if os.path.getsize(SONIC_PLATFORM_BSP_WHL_PKG) != 0: + status, output = log_os_system("pip3 install "+ SONIC_PLATFORM_BSP_WHL_PKG, 1) + if status: + print("Error: Failed to install {}".format(SONIC_PLATFORM_BSP_WHL_PKG)) + return status + else: + print("Successfully installed {} package".format(SONIC_PLATFORM_BSP_WHL_PKG)) else: - print("Successfully installed {} package".format(SONIC_PLATFORM_BSP_WHL_PKG)) + print("Error: Failed to copy {} properly. Exiting ...".format(SONIC_PLATFORM_PDDF_WHL_PKG)) + return -1 else: # PDDF with platform APIs 1.5 must be supported device_plugin_path = "/".join([device_path, "plugins"]) @@ -228,13 +209,17 @@ def config_pddf_utils(): if status: print("Error: Unable to uninstall BSP sonic-platform whl package") return status - print("Attemting to install the PDDF sonic_platform wheel package ...") - status, output = log_os_system("pip3 install "+ SONIC_PLATFORM_BSP_WHL_PKG, 1) - if status: - print("Error: Failed to install {}".format(SONIC_PLATFORM_BSP_WHL_PKG)) - return status + print("Attempting to install the PDDF sonic_platform wheel package ...") + if os.path.getsize(SONIC_PLATFORM_BSP_WHL_PKG) != 0: + status, output = log_os_system("pip3 install "+ SONIC_PLATFORM_BSP_WHL_PKG, 1) + if status: + print("Error: Failed to install {}".format(SONIC_PLATFORM_BSP_WHL_PKG)) + return status + else: + print("Successfully installed {} package".format(SONIC_PLATFORM_BSP_WHL_PKG)) else: - print("Successfully installed {} package".format(SONIC_PLATFORM_BSP_WHL_PKG)) + print("Error: Failed to copy {} properly. Exiting ...".format(SONIC_PLATFORM_PDDF_WHL_PKG)) + return -1 else: # system rebooted in pddf mode print("System rebooted in PDDF mode, hence keeping the PDDF 2.0 classes") @@ -353,6 +338,16 @@ def driver_install(): if status: print("Error: pddf_pre_driver_install script failed with error %d"%status) return status + # For debug + print(output) + + # Removes the perm_kos first, then reload them in a proper sequence + for mod in perm_kos: + cmd = "modprobe -rq " + mod + status, output = log_os_system(cmd, 1) + if status: + print("driver_install: Unable to unload {}".format(mod)) + # Don't exit but continue log_os_system("depmod", 1) for i in range(0,len(kos)): diff --git a/platform/pddf/i2c/utils/pddfparse.py b/platform/pddf/i2c/utils/pddfparse.py index f9ce8fca0b42..9d4b82d7594f 100755 --- a/platform/pddf/i2c/utils/pddfparse.py +++ b/platform/pddf/i2c/utils/pddfparse.py @@ -2,7 +2,6 @@ import argparse import glob import json -from jsonschema import validate import os import re import subprocess @@ -10,13 +9,13 @@ import time import unicodedata -bmc_cache={} -cache={} +bmc_cache = {} +cache = {} SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen' HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku' PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform' -dirname=os.path.dirname(os.path.realpath(__file__)) +dirname = os.path.dirname(os.path.realpath(__file__)) color_map = { "STATUS_LED_COLOR_GREEN" : "green", @@ -31,8 +30,6 @@ } - - class PddfParse(): def __init__(self): if not os.path.exists("/usr/share/sonic/platform"): @@ -41,16 +38,14 @@ def __init__(self): try: with open('/usr/share/sonic/platform/pddf/pddf-device.json') as f: - self.data = json.load(f) + self.data = json.load(f) except IOError: if os.path.exists('/usr/share/sonic/platform'): os.unlink("/usr/share/sonic/platform") raise Exception('PDDF JSON file not found. PDDF is not supported on this platform') - - self.data_sysfs_obj={} - self.sysfs_obj={} - + self.data_sysfs_obj = {} + self.sysfs_obj = {} # Returns platform and HW SKU def get_platform_and_hwsku(self): @@ -58,6 +53,7 @@ def get_platform_and_hwsku(self): proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY], stdout=subprocess.PIPE, shell=False, + universal_newlines=True, stderr=subprocess.STDOUT) stdout = proc.communicate()[0] proc.wait() @@ -66,11 +62,12 @@ def get_platform_and_hwsku(self): proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-d', '-v', HWSKU_KEY], stdout=subprocess.PIPE, shell=False, + universal_newlines=True, stderr=subprocess.STDOUT) stdout = proc.communicate()[0] proc.wait() hwsku = stdout.rstrip('\n') - except OSError, e: + except OSError as e: raise OSError("Cannot detect platform") return (platform, hwsku) @@ -80,76 +77,81 @@ def get_platform_and_hwsku(self): ################################################################################################################### def runcmd(self, cmd): rc = os.system(cmd) - if rc!=0: - print "%s -- command failed"%cmd + if rc != 0: + print("%s -- command failed" % cmd) return rc def get_dev_idx(self, dev, ops): - parent=dev['dev_info']['virt_parent'] - pdev=self.data[parent] - - return pdev['dev_attr']['dev_idx'] + parent = dev['dev_info']['virt_parent'] + pdev = self.data[parent] + return pdev['dev_attr']['dev_idx'] - def get_path(self, target, attr): + def get_paths(self, target, attr): aa = target + attr if aa in cache: return cache[aa] - string = None + strings = [] p = re.search(r'\d+$', target) if p is None: - for bb in filter(re.compile(target).search,self.data.keys()): - path = self.dev_parse(self.data[bb], { "cmd": "show_attr", "target":bb, "attr":attr }) - if path != "": - string = path + for bb in filter(re.compile(target).search, self.data.keys()): + paths = self.dev_parse(self.data[bb], {"cmd": "show_attr", "target": bb, "attr": attr}) + if paths: + strings.extend(paths) else: if target in self.data.keys(): - path = self.dev_parse(self.data[target], { "cmd": "show_attr", "target":target, "attr":attr }) - if path != "": - string = path + paths = self.dev_parse(self.data[target], {"cmd": "show_attr", "target": target, "attr": attr}) + if paths: + strings.extend(paths) + cache[aa] = strings + return strings - if string is not None: - string = string.rstrip() - - cache[aa]=string - return string - + def get_path(self, target, attr): + nodes = self.get_paths(target, attr) + if nodes: + if len(nodes)==1: + return nodes[0] + # CAREFULL!!! If more than one paths are expected, use get_paths + else: + return nodes[0] + else: + return None def get_device_type(self, key): - if not key in self.data.keys(): - return None + if key not in self.data.keys(): + return None return self.data[key]['dev_info']['device_type'] def get_platform(self): return self.data['PLATFORM'] def get_num_psu_fans(self, dev): - if not dev in self.data.keys(): + if dev not in self.data.keys(): return 0 - if not 'num_psu_fans' in self.data[dev]['dev_attr']: + if 'num_psu_fans' not in self.data[dev]['dev_attr']: return 0 - + return self.data[dev]['dev_attr']['num_psu_fans'] - def get_led_path(self): - return ("pddf/devices/led") + def get_led_path(self): + return ("pddf/devices/led") - def get_led_cur_state_path(self): - return ("pddf/devices/led/cur_state") + def get_led_cur_state_path(self): + return ("pddf/devices/led/cur_state") - def get_led_color(self): - color_f="/sys/kernel/pddf/devices/led/cur_state/color" + def get_led_color(self): + color_f = "/sys/kernel/pddf/devices/led/cur_state/color" try: - with open(color_f, 'r') as f: - color = f.read().strip("\r\n") + with open(color_f, 'r') as f: + color = f.read().strip("\r\n") except IOError: - return ("Error") + return ("Error") - return (color_map[color]) + return (color_map[color]) ################################################################################################################### # CREATE DEFS @@ -162,291 +164,291 @@ def create_device(self, attr, path, ops): else: val = attr[key] - cmd="echo '%s' > /sys/kernel/%s/%s"%(val, path, key) - ret=self.runcmd(cmd) - if ret!=0: + cmd = "echo '%s' > /sys/kernel/%s/%s" % (val, path, key) + ret = self.runcmd(cmd) + if ret != 0: return ret return ret def create_psu_i2c_device(self, dev, ops): - create_ret = 0 + create_ret = [] + ret = 0 if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['PSU']: - create_ret = self.create_device(dev['i2c']['topo_info'], "pddf/devices/psu/i2c", ops) - if create_ret!=0: - return create_ret - cmd= "echo '%s' > /sys/kernel/pddf/devices/psu/i2c/i2c_name"%(dev['dev_info']['device_name']) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret - cmd= "echo '%s' > /sys/kernel/pddf/devices/psu/i2c/psu_idx"%( self.get_dev_idx(dev, ops)) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + ret = self.create_device(dev['i2c']['topo_info'], "pddf/devices/psu/i2c", ops) + if ret != 0: + return create_ret.append(ret) + cmd = "echo '%s' > /sys/kernel/pddf/devices/psu/i2c/i2c_name" % (dev['dev_info']['device_name']) + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) + cmd = "echo '%s' > /sys/kernel/pddf/devices/psu/i2c/psu_idx" % (self.get_dev_idx(dev, ops)) + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) for attr in dev['i2c']['attr_list']: - create_ret = self.create_device(attr, "pddf/devices/psu/i2c", ops) - if create_ret!=0: - return create_ret - cmd= "echo 'add' > /sys/kernel/pddf/devices/psu/i2c/attr_ops" - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + ret = self.create_device(attr, "pddf/devices/psu/i2c", ops) + if ret != 0: + return create_ret.append(ret) + cmd = "echo 'add' > /sys/kernel/pddf/devices/psu/i2c/attr_ops" + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) cmd = "echo 'add' > /sys/kernel/pddf/devices/psu/i2c/dev_ops" - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) else: - cmd = "echo %s 0x%x > /sys/bus/i2c/devices/i2c-%d/new_device" % (dev['i2c']['topo_info']['dev_type'], + cmd = "echo %s 0x%x > /sys/bus/i2c/devices/i2c-%d/new_device" % (dev['i2c']['topo_info']['dev_type'], int(dev['i2c']['topo_info']['dev_addr'], 0), int(dev['i2c']['topo_info']['parent_bus'], 0)) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret - - - return create_ret - + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) + return create_ret.append(ret) def create_psu_bmc_device(self, dev, ops): - print "" - + return [0] def create_psu_device(self, dev, ops): - return self.create_psu_i2c_device(dev, ops ) - + return self.create_psu_i2c_device(dev, ops) + def create_fan_device(self, dev, ops): - create_ret = 0 + create_ret = [] + ret = 0 if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['FAN']: - create_ret = self.create_device(dev['i2c']['topo_info'], "pddf/devices/fan/i2c", ops) - if create_ret!=0: - return create_ret - cmd= "echo '%s' > /sys/kernel/pddf/devices/fan/i2c/i2c_name"%(dev['dev_info']['device_name']) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret - create_ret = self.create_device(dev['i2c']['dev_attr'], "pddf/devices/fan/i2c", ops) - if create_ret!=0: - return create_ret + ret = self.create_device(dev['i2c']['topo_info'], "pddf/devices/fan/i2c", ops) + if ret != 0: + return create_ret.append(ret) + cmd = "echo '%s' > /sys/kernel/pddf/devices/fan/i2c/i2c_name" % (dev['dev_info']['device_name']) + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) + ret = self.create_device(dev['i2c']['dev_attr'], "pddf/devices/fan/i2c", ops) + if ret != 0: + return create_ret.append(ret) for attr in dev['i2c']['attr_list']: - create_ret = self.create_device(attr, "pddf/devices/fan/i2c", ops) - if create_ret!=0: - return create_ret - cmd= "echo 'add' > /sys/kernel/pddf/devices/fan/i2c/attr_ops" - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret - - cmd= "echo 'add' > /sys/kernel/pddf/devices/fan/i2c/dev_ops" - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + ret = self.create_device(attr, "pddf/devices/fan/i2c", ops) + if ret != 0: + return create_ret.append(ret) + cmd = "echo 'add' > /sys/kernel/pddf/devices/fan/i2c/attr_ops" + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) + + cmd = "echo 'add' > /sys/kernel/pddf/devices/fan/i2c/dev_ops" + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) else: - cmd= "echo %s 0x%x > /sys/bus/i2c/devices/i2c-%d/new_device" % (dev['i2c']['topo_info']['dev_type'], + cmd = "echo %s 0x%x > /sys/bus/i2c/devices/i2c-%d/new_device" % (dev['i2c']['topo_info']['dev_type'], int(dev['i2c']['topo_info']['dev_addr'], 0), int(dev['i2c']['topo_info']['parent_bus'], 0)) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) - return create_ret + return create_ret.append(ret) def create_temp_sensor_device(self, dev, ops): - create_ret = 0 - # NO PDDF driver for temp_sensors device - cmd= "echo %s 0x%x > /sys/bus/i2c/devices/i2c-%d/new_device" % (dev['i2c']['topo_info']['dev_type'], - int(dev['i2c']['topo_info']['dev_addr'], 0), int(dev['i2c']['topo_info']['parent_bus'], 0)) - create_ret = self.runcmd(cmd) - return create_ret - - - + create_ret = [] + ret = 0 + # NO PDDF driver for temp_sensors device + cmd = "echo %s 0x%x > /sys/bus/i2c/devices/i2c-%d/new_device" % (dev['i2c']['topo_info']['dev_type'], + int(dev['i2c']['topo_info']['dev_addr'], 0), int(dev['i2c']['topo_info']['parent_bus'], 0)) + ret = self.runcmd(cmd) + return create_ret.append(ret) def create_cpld_device(self, dev, ops): - create_ret = 0 + create_ret = [] + ret = 0 if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['CPLD']: - create_ret = self.create_device(dev['i2c']['topo_info'], "pddf/devices/cpld", ops) - if create_ret!=0: - return create_ret - - cmd= "echo '%s' > /sys/kernel/pddf/devices/cpld/i2c_name"%(dev['dev_info']['device_name']) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + ret = self.create_device(dev['i2c']['topo_info'], "pddf/devices/cpld", ops) + if ret != 0: + return create_ret.append(ret) + + cmd = "echo '%s' > /sys/kernel/pddf/devices/cpld/i2c_name" % (dev['dev_info']['device_name']) + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) # TODO: If attributes are provided then, use 'self.create_device' for them too - cmd= "echo 'add' > /sys/kernel/pddf/devices/cpld/dev_ops" - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + cmd = "echo 'add' > /sys/kernel/pddf/devices/cpld/dev_ops" + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) else: - cmd= "echo %s 0x%x > /sys/bus/i2c/devices/i2c-%d/new_device" % (dev['i2c']['topo_info']['dev_type'], + cmd = "echo %s 0x%x > /sys/bus/i2c/devices/i2c-%d/new_device" % (dev['i2c']['topo_info']['dev_type'], int(dev['i2c']['topo_info']['dev_addr'], 0), int(dev['i2c']['topo_info']['parent_bus'], 0)) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) - return create_ret + return create_ret.append(ret) def create_cpldmux_device(self, dev, ops): - create_ret = 0 - create_ret = self.create_device(dev['i2c']['topo_info'], "pddf/devices/cpldmux", ops) - if create_ret!=0: - return create_ret - cmd= "echo '%s' > /sys/kernel/pddf/devices/mux/i2c_name"%(dev['dev_info']['device_name']) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + create_ret = [] + ret = 0 + ret = self.create_device(dev['i2c']['topo_info'], "pddf/devices/cpldmux", ops) + if ret != 0: + return create_ret.append(ret) + cmd = "echo '%s' > /sys/kernel/pddf/devices/mux/i2c_name" % (dev['dev_info']['device_name']) + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) self.create_device(dev['i2c']['dev_attr'], "pddf/devices/cpldmux", ops) - # Parse channel info + # Parse channel info for chan in dev['i2c']['channel']: self.create_device(chan, "pddf/devices/cpldmux", ops) - cmd="echo 'add' > /sys/kernel/pddf/devices/cpldmux/chan_ops" - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + cmd = "echo 'add' > /sys/kernel/pddf/devices/cpldmux/chan_ops" + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) - cmd= "echo 'add' > /sys/kernel/pddf/devices/cpldmux/dev_ops" - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + cmd = "echo 'add' > /sys/kernel/pddf/devices/cpldmux/dev_ops" + ret = self.runcmd(cmd) + return create_ret.append(ret) def create_gpio_device(self, dev, ops): - create_ret = 0 - create_ret = self.create_device(dev['i2c']['topo_info'], "pddf/devices/gpio", ops) - if create_ret!=0: - return create_ret - cmd= "echo '%s' > /sys/kernel/pddf/devices/gpio/i2c_name"%(dev['dev_info']['device_name']) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret - create_ret = self.create_device(dev['i2c']['dev_attr'], "pddf/devices/gpio", ops) - if create_ret!=0: - return create_ret - cmd= "echo 'add' > /sys/kernel/pddf/devices/gpio/dev_ops" - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret - + create_ret = [] + ret = 0 + ret = self.create_device(dev['i2c']['topo_info'], "pddf/devices/gpio", ops) + if ret != 0: + return create_ret.append(ret) + cmd = "echo '%s' > /sys/kernel/pddf/devices/gpio/i2c_name" % (dev['dev_info']['device_name']) + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) + ret = self.create_device(dev['i2c']['dev_attr'], "pddf/devices/gpio", ops) + if ret != 0: + return create_ret.append(ret) + cmd = "echo 'add' > /sys/kernel/pddf/devices/gpio/dev_ops" + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) + time.sleep(2) base = dev['i2c']['dev_attr']['gpio_base'] for inst in dev['i2c']['ports']: - if inst['port_num']!="": + if inst['port_num'] != "": port_no = int(base, 16) + int(inst['port_num']) - cmd= "echo %d > /sys/class/gpio/export"%port_no - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret - if inst['direction']!="": - cmd= "echo %s >/sys/class/gpio/gpio%d/direction"%(inst['direction'], port_no) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret - if inst['value']!="": + cmd = "echo %d > /sys/class/gpio/export" % port_no + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) + if inst['direction'] != "": + cmd = "echo %s >/sys/class/gpio/gpio%d/direction" % (inst['direction'], port_no) + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) + if inst['value'] != "": for i in inst['value'].split(','): - cmd= "echo %s >/sys/class/gpio/gpio%d/value"%(i.rstrip(), port_no) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + cmd = "echo %s >/sys/class/gpio/gpio%d/value" % (i.rstrip(), port_no) + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) - return create_ret + return create_ret.append(ret) def create_mux_device(self, dev, ops): - create_ret = 0 - create_ret = self.create_device(dev['i2c']['topo_info'], "pddf/devices/mux", ops) - if create_ret!=0: - return create_ret - cmd= "echo '%s' > /sys/kernel/pddf/devices/mux/i2c_name"%(dev['dev_info']['device_name']) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + create_ret = [] + ret = 0 + ret = self.create_device(dev['i2c']['topo_info'], "pddf/devices/mux", ops) + if ret != 0: + return create_ret.append(ret) + cmd = "echo '%s' > /sys/kernel/pddf/devices/mux/i2c_name" % (dev['dev_info']['device_name']) + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) self.create_device(dev['i2c']['dev_attr'], "pddf/devices/mux", ops) - cmd= "echo 'add' > /sys/kernel/pddf/devices/mux/dev_ops" - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret - + cmd = "echo 'add' > /sys/kernel/pddf/devices/mux/dev_ops" + ret = self.runcmd(cmd) + return create_ret.append(ret) def create_xcvr_i2c_device(self, dev, ops): - create_ret = 0 + create_ret = [] + ret = 0 if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['PORT_MODULE']: self.create_device(dev['i2c']['topo_info'], "pddf/devices/xcvr/i2c", ops) - cmd= "echo '%s' > /sys/kernel/pddf/devices/xcvr/i2c/i2c_name"%(dev['dev_info']['device_name']) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret - cmd="echo '%s' > /sys/kernel/pddf/devices/xcvr/i2c/dev_idx"%( self.get_dev_idx(dev, ops)) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + cmd = "echo '%s' > /sys/kernel/pddf/devices/xcvr/i2c/i2c_name" % (dev['dev_info']['device_name']) + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) + cmd = "echo '%s' > /sys/kernel/pddf/devices/xcvr/i2c/dev_idx" % (self.get_dev_idx(dev, ops)) + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) for attr in dev['i2c']['attr_list']: self.create_device(attr, "pddf/devices/xcvr/i2c", ops) - cmd="echo 'add' > /sys/kernel/pddf/devices/xcvr/i2c/attr_ops" - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret - - cmd="echo 'add' > /sys/kernel/pddf/devices/xcvr/i2c/dev_ops" - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + cmd = "echo 'add' > /sys/kernel/pddf/devices/xcvr/i2c/attr_ops" + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) + + cmd = "echo 'add' > /sys/kernel/pddf/devices/xcvr/i2c/dev_ops" + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) else: - cmd="echo %s 0x%x > /sys/bus/i2c/devices/i2c-%d/new_device" % (dev['i2c']['topo_info']['dev_type'], + cmd = "echo %s 0x%x > /sys/bus/i2c/devices/i2c-%d/new_device" % (dev['i2c']['topo_info']['dev_type'], int(dev['i2c']['topo_info']['dev_addr'], 0), int(dev['i2c']['topo_info']['parent_bus'], 0)) - create_ret = self.runcmd(cmd) - #print "\n" - if create_ret!=0: - return create_ret + ret = self.runcmd(cmd) + # print("\n") + if ret != 0: + return create_ret.append(ret) # Add port name port_name_sysfs = '/sys/bus/i2c/devices/{}-00{:02x}/port_name'.format( - int(dev['i2c']['topo_info']['parent_bus'], 0),int(dev['i2c']['topo_info']['dev_addr'], 0)) + int(dev['i2c']['topo_info']['parent_bus'], 0), int(dev['i2c']['topo_info']['dev_addr'], 0)) if os.path.exists(port_name_sysfs): - cmd="echo {} > /sys/bus/i2c/devices/{}-00{:02x}/port_name".format( - dev['dev_info']['virt_parent'].lower(), int(dev['i2c']['topo_info']['parent_bus'], 0), - int(dev['i2c']['topo_info']['dev_addr'], 0)) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + cmd = "echo {} > /sys/bus/i2c/devices/{}-00{:02x}/port_name".format( + dev['dev_info']['virt_parent'].lower(), int(dev['i2c']['topo_info']['parent_bus'], 0), + int(dev['i2c']['topo_info']['dev_addr'], 0)) + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) - return create_ret + return create_ret.append(ret) def create_xcvr_bmc_device(self, dev, ops): - print "" + return [0] def create_xcvr_device(self, dev, ops): - return self.create_xcvr_i2c_device(dev, ops ) + return self.create_xcvr_i2c_device(dev, ops) def create_sysstatus_device(self, dev, ops): - create_ret = 0 + create_ret = [] + ret = 0 for attr in dev['attr_list']: self.create_device(attr, "pddf/devices/sysstatus", ops) - cmd= "echo 'add' > /sys/kernel/pddf/devices/sysstatus/attr_ops" - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + cmd = "echo 'add' > /sys/kernel/pddf/devices/sysstatus/attr_ops" + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) def create_eeprom_device(self, dev, ops): - create_ret = 0 + create_ret = [] + ret = 0 if "EEPROM" in self.data['PLATFORM']['pddf_dev_types'] and \ dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['EEPROM']: self.create_device(dev['i2c']['topo_info'], "pddf/devices/eeprom/i2c", ops) - cmd= "echo '%s' > /sys/kernel/pddf/devices/eeprom/i2c/i2c_name"%(dev['dev_info']['device_name']) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + cmd = "echo '%s' > /sys/kernel/pddf/devices/eeprom/i2c/i2c_name" % (dev['dev_info']['device_name']) + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) self.create_device(dev['i2c']['dev_attr'], "pddf/devices/eeprom/i2c", ops) cmd = "echo 'add' > /sys/kernel/pddf/devices/eeprom/i2c/dev_ops" - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) else: - cmd= "echo %s 0x%x > /sys/bus/i2c/devices/i2c-%d/new_device" % (dev['i2c']['topo_info']['dev_type'], + cmd = "echo %s 0x%x > /sys/bus/i2c/devices/i2c-%d/new_device" % (dev['i2c']['topo_info']['dev_type'], int(dev['i2c']['topo_info']['dev_addr'], 0), int(dev['i2c']['topo_info']['parent_bus'], 0)) - create_ret = self.runcmd(cmd) - if create_ret!=0: - return create_ret + ret = self.runcmd(cmd) + if ret != 0: + return create_ret.append(ret) - return create_ret + return create_ret.append(ret) ################################################################################################################### # DELETE DEFS @@ -454,13 +456,13 @@ def create_eeprom_device(self, dev, ops): def delete_eeprom_device(self, dev, ops): if "EEPROM" in self.data['PLATFORM']['pddf_dev_types'] and \ dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['EEPROM']: - cmd= "echo '%s' > /sys/kernel/pddf/devices/eeprom/i2c/i2c_name"%(dev['dev_info']['device_name']) + cmd = "echo '%s' > /sys/kernel/pddf/devices/eeprom/i2c/i2c_name" % (dev['dev_info']['device_name']) self.runcmd(cmd) cmd = "echo 'delete' > /sys/kernel/pddf/devices/eeprom/i2c/dev_ops" self.runcmd(cmd) else: - cmd= "echo 0x%x > /sys/bus/i2c/devices/i2c-%d/delete_device" % (int(dev['i2c']['topo_info']['dev_addr'], 0), - int(dev['i2c']['topo_info']['parent_bus'], 0)) + cmd = "echo 0x%x > /sys/bus/i2c/devices/i2c-%d/delete_device" % ( + int(dev['i2c']['topo_info']['dev_addr'], 0), int(dev['i2c']['topo_info']['parent_bus'], 0)) self.runcmd(cmd) def delete_sysstatus_device(self, dev, ops): @@ -470,13 +472,13 @@ def delete_sysstatus_device(self, dev, ops): def delete_xcvr_i2c_device(self, dev, ops): if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['PORT_MODULE']: - cmd= "echo '%s' > /sys/kernel/pddf/devices/xcvr/i2c/i2c_name"%(dev['dev_info']['device_name']) + cmd = "echo '%s' > /sys/kernel/pddf/devices/xcvr/i2c/i2c_name" % (dev['dev_info']['device_name']) self.runcmd(cmd) - cmd="echo 'delete' > /sys/kernel/pddf/devices/xcvr/i2c/dev_ops" + cmd = "echo 'delete' > /sys/kernel/pddf/devices/xcvr/i2c/dev_ops" self.runcmd(cmd) else: - cmd="echo 0x%x > /sys/bus/i2c/devices/i2c-%d/delete_device" % (int(dev['i2c']['topo_info']['dev_addr'], 0), - int(dev['i2c']['topo_info']['parent_bus'], 0)) + cmd = "echo 0x%x > /sys/bus/i2c/devices/i2c-%d/delete_device" % ( + int(dev['i2c']['topo_info']['dev_addr'], 0), int(dev['i2c']['topo_info']['parent_bus'], 0)) self.runcmd(cmd) def delete_xcvr_device(self, dev, ops): @@ -484,75 +486,74 @@ def delete_xcvr_device(self, dev, ops): return def delete_gpio_device(self, dev, ops): - cmd= "echo '%s' > /sys/kernel/pddf/devices/gpio/i2c_name"%(dev['dev_info']['device_name']) - self.runcmd(cmd) - cmd= "echo 'delete' > /sys/kernel/pddf/devices/gpio/dev_ops" - self.runcmd(cmd) + cmd = "echo '%s' > /sys/kernel/pddf/devices/gpio/i2c_name" % (dev['dev_info']['device_name']) + self.runcmd(cmd) + cmd = "echo 'delete' > /sys/kernel/pddf/devices/gpio/dev_ops" + self.runcmd(cmd) def delete_mux_device(self, dev, ops): - cmd= "echo '%s' > /sys/kernel/pddf/devices/mux/i2c_name"%(dev['dev_info']['device_name']) - self.runcmd(cmd) - cmd= "echo 'delete' > /sys/kernel/pddf/devices/mux/dev_ops" - self.runcmd(cmd) + cmd = "echo '%s' > /sys/kernel/pddf/devices/mux/i2c_name" % (dev['dev_info']['device_name']) + self.runcmd(cmd) + cmd = "echo 'delete' > /sys/kernel/pddf/devices/mux/dev_ops" + self.runcmd(cmd) def delete_cpld_device(self, dev, ops): if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['CPLD']: - cmd= "echo '%s' > /sys/kernel/pddf/devices/cpld/i2c_name"%(dev['dev_info']['device_name']) + cmd = "echo '%s' > /sys/kernel/pddf/devices/cpld/i2c_name" % (dev['dev_info']['device_name']) self.runcmd(cmd) - cmd= "echo 'delete' > /sys/kernel/pddf/devices/cpld/dev_ops" + cmd = "echo 'delete' > /sys/kernel/pddf/devices/cpld/dev_ops" self.runcmd(cmd) else: - cmd= "echo 0x%x > /sys/bus/i2c/devices/i2c-%d/delete_device" % (int(dev['i2c']['topo_info']['dev_addr'], 0), - int(dev['i2c']['topo_info']['parent_bus'], 0)) + cmd = "echo 0x%x > /sys/bus/i2c/devices/i2c-%d/delete_device" % ( + int(dev['i2c']['topo_info']['dev_addr'], 0), int(dev['i2c']['topo_info']['parent_bus'], 0)) self.runcmd(cmd) def delete_cpldmux_device(self, dev, ops): if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['CPLDMUX']: - cmd= "echo '%s' > /sys/kernel/pddf/devices/cpldmux/i2c_name"%(dev['dev_info']['device_name']) + cmd = "echo '%s' > /sys/kernel/pddf/devices/cpldmux/i2c_name" % (dev['dev_info']['device_name']) self.runcmd(cmd) - cmd= "echo 'delete' > /sys/kernel/pddf/devices/cpldmux/dev_ops" + cmd = "echo 'delete' > /sys/kernel/pddf/devices/cpldmux/dev_ops" self.runcmd(cmd) def delete_temp_sensor_device(self, dev, ops): # NO PDDF driver for temp_sensors device - cmd= "echo 0x%x > /sys/bus/i2c/devices/i2c-%d/delete_device" % (int(dev['i2c']['topo_info']['dev_addr'], 0), - int(dev['i2c']['topo_info']['parent_bus'], 0)) + cmd = "echo 0x%x > /sys/bus/i2c/devices/i2c-%d/delete_device" % ( + int(dev['i2c']['topo_info']['dev_addr'], 0), int(dev['i2c']['topo_info']['parent_bus'], 0)) self.runcmd(cmd) def delete_fan_device(self, dev, ops): if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['FAN']: - cmd= "echo '%s' > /sys/kernel/pddf/devices/fan/i2c/i2c_name"%(dev['dev_info']['device_name']) + cmd = "echo '%s' > /sys/kernel/pddf/devices/fan/i2c/i2c_name" % (dev['dev_info']['device_name']) self.runcmd(cmd) - cmd= "echo 'delete' > /sys/kernel/pddf/devices/fan/i2c/dev_ops" + cmd = "echo 'delete' > /sys/kernel/pddf/devices/fan/i2c/dev_ops" self.runcmd(cmd) else: - cmd= "echo 0x%x > /sys/bus/i2c/devices/i2c-%d/delete_device" % (int(dev['i2c']['topo_info']['dev_addr'], 0), - int(dev['i2c']['topo_info']['parent_bus'], 0)) + cmd = "echo 0x%x > /sys/bus/i2c/devices/i2c-%d/delete_device" % ( + int(dev['i2c']['topo_info']['dev_addr'], 0), int(dev['i2c']['topo_info']['parent_bus'], 0)) self.runcmd(cmd) def delete_psu_i2c_device(self, dev, ops): if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['PSU']: - cmd= "echo '%s' > /sys/kernel/pddf/devices/psu/i2c/i2c_name"%(dev['dev_info']['device_name']) + cmd = "echo '%s' > /sys/kernel/pddf/devices/psu/i2c/i2c_name" % (dev['dev_info']['device_name']) self.runcmd(cmd) cmd = "echo 'delete' > /sys/kernel/pddf/devices/psu/i2c/dev_ops" self.runcmd(cmd) else: - cmd = "echo 0x%x > /sys/bus/i2c/devices/i2c-%d/delete_device" %(int(dev['i2c']['topo_info']['dev_addr'], 0), - int(dev['i2c']['topo_info']['parent_bus'], 0)) + cmd = "echo 0x%x > /sys/bus/i2c/devices/i2c-%d/delete_device" % ( + int(dev['i2c']['topo_info']['dev_addr'], 0), int(dev['i2c']['topo_info']['parent_bus'], 0)) self.runcmd(cmd) def delete_psu_device(self, dev, ops): - self.delete_psu_i2c_device(dev, ops ) - return - + self.delete_psu_i2c_device(dev, ops) + return ################################################################################################################### # SHOW ATTRIBIUTES DEFS ################################################################################################################### def is_led_device_configured(self, device_name, attr_name): if device_name in self.data.keys(): - attr_list=self.data[device_name]['i2c']['attr_list'] + attr_list = self.data[device_name]['i2c']['attr_list'] for attr in attr_list: if attr['attr_name'].strip() == attr_name.strip(): return (True) @@ -560,23 +561,32 @@ def is_led_device_configured(self, device_name, attr_name): def show_device_sysfs(self, dev, ops): - parent=dev['dev_info']['device_parent'] - pdev=self.data[parent] + parent = dev['dev_info']['device_parent'] + pdev = self.data[parent] if pdev['dev_info']['device_parent'] == 'SYSTEM': - return "/sys/bus/i2c/devices/"+"i2c-%d"%int(pdev['i2c']['topo_info']['dev_addr'], 0) + if 'topo_info' in pdev['i2c']: + return "/sys/bus/i2c/devices/"+"i2c-%d"%int(pdev['i2c']['topo_info']['dev_addr'], 0) + else: + return "/sys/bus/i2c/devices" return self.show_device_sysfs(pdev, ops) + "/" + "i2c-%d" % int(dev['i2c']['topo_info']['parent_bus'], 0) + def get_gpio_attr_path(self, dev, offset): + base = int(dev['i2c']['dev_attr']['gpio_base'], 16) + port_num = base + int(offset, 16) + gpio_name = 'gpio'+str(port_num) + path = '/sys/class/gpio/'+gpio_name+'/value' + return path - # This is alid for 'at24' type of EEPROM devices. Only one attribtue 'eeprom' + # 'at24' type of EEPROM devices have only one attribtue 'eeprom' def show_attr_eeprom_device(self, dev, ops): - str = "" - attr_name=ops['attr'] - attr_list=dev['i2c']['attr_list'] - KEY="eeprom" - dsysfs_path="" + ret = [] + attr_name = ops['attr'] + attr_list = dev['i2c']['attr_list'] + KEY = "eeprom" + dsysfs_path = "" - if not KEY in self.data_sysfs_obj: - self.data_sysfs_obj[KEY]=[] + if KEY not in self.data_sysfs_obj: + self.data_sysfs_obj[KEY] = [] for attr in attr_list: if attr_name == attr['attr_name'] or attr_name == 'all': @@ -585,119 +595,134 @@ def show_attr_eeprom_device(self, dev, ops): else: real_name = attr['attr_name'] - dsysfs_path = self.show_device_sysfs(dev, ops)+"/%d-00%x"%(int(dev['i2c']['topo_info']['parent_bus'],0), - int(dev['i2c']['topo_info']['dev_addr'], 0))+"/%s"%real_name - if not dsysfs_path in self.data_sysfs_obj[KEY]: + dsysfs_path = self.show_device_sysfs(dev, ops) + \ + "/%d-00%x" % (int(dev['i2c']['topo_info']['parent_bus'], 0), + int(dev['i2c']['topo_info']['dev_addr'], 0)) + \ + "/%s" % real_name + if dsysfs_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(dsysfs_path) - str += dsysfs_path+"\n" - return str + ret.append(dsysfs_path) + return ret def show_attr_gpio_device(self, dev, ops): - ret = "" - KEY="gpio" - if not KEY in self.data_sysfs_obj: - self.data_sysfs_obj[KEY]=[] + ret = [] + KEY = "gpio" + if KEY not in self.data_sysfs_obj: + self.data_sysfs_obj[KEY] = [] return ret def show_attr_mux_device(self, dev, ops): - ret = "" - KEY="mux" - if not KEY in self.data_sysfs_obj: - self.data_sysfs_obj[KEY]=[] + ret = [] + KEY = "mux" + if KEY not in self.data_sysfs_obj: + self.data_sysfs_obj[KEY] = [] return ret def show_attr_psu_i2c_device(self, dev, ops): - target=ops['target'] - attr_name=ops['attr'] - ret = "" - KEY="psu" - dsysfs_path="" + target = ops['target'] + attr_name = ops['attr'] + ret = [] + KEY = "psu" + dsysfs_path = "" - if not KEY in self.data_sysfs_obj: - self.data_sysfs_obj[KEY]=[] + if KEY not in self.data_sysfs_obj: + self.data_sysfs_obj[KEY] = [] - if target == 'all' or target == dev['dev_info']['virt_parent'] : - attr_list=dev['i2c']['attr_list'] + if target == 'all' or target == dev['dev_info']['virt_parent']: + attr_list = dev['i2c']['attr_list'] if 'i2c' in dev else [] for attr in attr_list: - if attr_name == attr['attr_name'] or attr_name == 'all' : + if attr_name == attr['attr_name'] or attr_name == 'all': if 'attr_devtype' in attr.keys() and attr['attr_devtype'] == "gpio": # Check and enable the gpio from class - gpio_dev = self.data[attr['attr_devname']] - base = int(gpio_dev['i2c']['dev_attr']['gpio_base'], 16) - port_num = base + int(attr['attr_offset'], 16) - gpio_name = 'gpio'+str(port_num) - attr_path = '/sys/class/gpio/'+gpio_name+'/value' + attr_path = self.get_gpio_attr_path(self.data[attr['attr_devname']], attr['attr_offset']) if (os.path.exists(attr_path)): - if not attr_path in self.data_sysfs_obj[KEY]: + if attr_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(attr_path) - ret += attr_path + '\n' + ret.append(attr_path) else: if 'drv_attr_name' in attr.keys(): real_name = attr['drv_attr_name'] + real_dev = dev + elif 'attr_devattr' in attr.keys(): + real_name = attr['attr_devattr'] + real_devname = attr['attr_devname'] if 'attr_devname' in attr.keys() else '' + real_dev = self.data[real_devname] else: real_name = attr['attr_name'] + real_dev = dev - dsysfs_path = self.show_device_sysfs(dev, ops) + \ - "/%d-00%x"%(int(dev['i2c']['topo_info']['parent_bus'], 0), - int(dev['i2c']['topo_info']['dev_addr'], 0)) + \ - "/%s"%real_name - if not dsysfs_path in self.data_sysfs_obj[KEY]: + dsysfs_path = self.show_device_sysfs(real_dev, ops) + \ + "/%d-00%x" % (int(real_dev['i2c']['topo_info']['parent_bus'], 0), + int(real_dev['i2c']['topo_info']['dev_addr'], 0)) + \ + "/%s" % real_name + if dsysfs_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(dsysfs_path) - ret += dsysfs_path+"\n" + ret.append(dsysfs_path) return ret def show_attr_psu_device(self, dev, ops): - return self.show_attr_psu_i2c_device(dev, ops ) - + return self.show_attr_psu_i2c_device(dev, ops) def show_attr_fan_device(self, dev, ops): - ret_str = "" - attr_name=ops['attr'] - attr_list=dev['i2c']['attr_list'] - KEY="fan" - dsysfs_path="" - - if not KEY in self.data_sysfs_obj: - self.data_sysfs_obj[KEY]=[] + ret = [] + attr_name = ops['attr'] + attr_list = dev['i2c']['attr_list'] if 'i2c' in dev else [] + KEY = "fan" + dsysfs_path = "" + if KEY not in self.data_sysfs_obj: + self.data_sysfs_obj[KEY] = [] for attr in attr_list: if attr_name == attr['attr_name'] or attr_name == 'all': - if 'drv_attr_name' in attr.keys(): - real_name = attr['drv_attr_name'] + if 'attr_devtype' in attr.keys() and attr['attr_devtype'] == "gpio": + # Check and enable the gpio from class + attr_path = self.get_gpio_attr_path(self.data[attr['attr_devname']], attr['attr_offset']) + if (os.path.exists(attr_path)): + if attr_path not in self.data_sysfs_obj[KEY]: + self.data_sysfs_obj[KEY].append(attr_path) + ret.append(attr_path) else: - real_name = attr['attr_name'] - - dsysfs_path= self.show_device_sysfs(dev, ops) + \ - "/%d-00%x" %(int(dev['i2c']['topo_info']['parent_bus'], 0), - int(dev['i2c']['topo_info']['dev_addr'], 0)) + \ - "/%s"%real_name - if not dsysfs_path in self.data_sysfs_obj[KEY]: - self.data_sysfs_obj[KEY].append(dsysfs_path) - ret_str += dsysfs_path+"\n" - return ret_str + if 'drv_attr_name' in attr.keys(): + real_name = attr['drv_attr_name'] + real_dev = dev + elif 'attr_devattr' in attr.keys(): + real_name = attr['attr_devattr'] + real_devname = attr['attr_devname'] if 'attr_devname' in attr.keys() else '' + real_dev = self.data[real_devname] + else: + real_name = attr['attr_name'] + real_dev = dev + + dsysfs_path = self.show_device_sysfs(real_dev, ops) + \ + "/%d-00%x" % (int(real_dev['i2c']['topo_info']['parent_bus'], 0), + int(real_dev['i2c']['topo_info']['dev_addr'], 0)) + \ + "/%s" % real_name + if dsysfs_path not in self.data_sysfs_obj[KEY]: + self.data_sysfs_obj[KEY].append(dsysfs_path) + ret.append(dsysfs_path) + return ret # This is only valid for LM75 def show_attr_temp_sensor_device(self, dev, ops): - ret_str = "" - attr_name=ops['attr'] - attr_list=dev['i2c']['attr_list'] - KEY="temp-sensors" - dsysfs_path="" - - if not KEY in self.data_sysfs_obj: - self.data_sysfs_obj[KEY]=[] + ret = [] + attr_name = ops['attr'] + attr_list = dev['i2c']['attr_list'] if 'i2c' in dev else [] + KEY = "temp-sensors" + dsysfs_path = "" + if KEY not in self.data_sysfs_obj: + self.data_sysfs_obj[KEY] = [] for attr in attr_list: if attr_name == attr['attr_name'] or attr_name == 'all': path = self.show_device_sysfs(dev, ops) + \ - "/%d-00%x/" %(int(dev['i2c']['topo_info']['parent_bus'], 0), - int(dev['i2c']['topo_info']['dev_addr'], 0)) + "/%d-00%x/" % (int(dev['i2c']['topo_info']['parent_bus'], 0), + int(dev['i2c']['topo_info']['dev_addr'], 0)) if 'drv_attr_name' in attr.keys(): real_name = attr['drv_attr_name'] else: @@ -705,87 +730,105 @@ def show_attr_temp_sensor_device(self, dev, ops): if (os.path.exists(path)): full_path = glob.glob(path + 'hwmon/hwmon*/' + real_name)[0] - dsysfs_path=full_path - if not dsysfs_path in self.data_sysfs_obj[KEY]: + dsysfs_path = full_path + if dsysfs_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(dsysfs_path) - ret_str += full_path + "\n" - return ret_str + ret.append(full_path) + return ret def show_attr_sysstatus_device(self, dev, ops): - ret = "" - attr_name=ops['attr'] - attr_list=dev['attr_list'] - KEY="sys-status" - dsysfs_path="" - - if not KEY in self.data_sysfs_obj: - self.data_sysfs_obj[KEY]=[] + ret = [] + attr_name = ops['attr'] + attr_list = dev['attr_list'] + KEY = "sys-status" + dsysfs_path = "" + if KEY not in self.data_sysfs_obj: + self.data_sysfs_obj[KEY] = [] for attr in attr_list: - if attr_name == attr['attr_name'] or attr_name == 'all': - dsysfs_path = "/sys/kernel/pddf/devices/sysstatus/sysstatus_data/" + attr['attr_name'] - if not dsysfs_path in self.data_sysfs_obj[KEY]: - self.data_sysfs_obj[KEY].append(dsysfs_path) - ret += dsysfs_path+"\n" + if attr_name == attr['attr_name'] or attr_name == 'all': + dsysfs_path = "/sys/kernel/pddf/devices/sysstatus/sysstatus_data/" + attr['attr_name'] + if dsysfs_path not in self.data_sysfs_obj[KEY]: + self.data_sysfs_obj[KEY].append(dsysfs_path) + ret.append(dsysfs_path) return ret def show_attr_xcvr_i2c_device(self, dev, ops): - target=ops['target'] - attr_name=ops['attr'] - ret = "" + target = ops['target'] + attr_name = ops['attr'] + ret = [] dsysfs_path = "" - KEY="xcvr" - if not KEY in self.data_sysfs_obj: - self.data_sysfs_obj[KEY]=[] + KEY = "xcvr" + if KEY not in self.data_sysfs_obj: + self.data_sysfs_obj[KEY] = [] - if target == 'all' or target == dev['dev_info']['virt_parent'] : - attr_list=dev['i2c']['attr_list'] + if target == 'all' or target == dev['dev_info']['virt_parent']: + attr_list = dev['i2c']['attr_list'] for attr in attr_list: - if attr_name == attr['attr_name'] or attr_name == 'all' : + if attr_name == attr['attr_name'] or attr_name == 'all': if 'attr_devtype' in attr.keys() and attr['attr_devtype'] == "gpio": # Check and enable the gpio from class - gpio_dev = self.data[attr['attr_devname']] - base = int(gpio_dev['i2c']['dev_attr']['gpio_base'], 16) - port_num = base + int(attr['attr_offset'], 16) - gpio_name = 'gpio'+str(port_num) - attr_path = '/sys/class/gpio/'+gpio_name+'/value' + attr_path = self.get_gpio_attr_path(self.data[attr['attr_devname']], attr['attr_offset']) if (os.path.exists(attr_path)): - if not attr_path in self.data_sysfs_obj[KEY]: + if attr_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(attr_path) - ret += attr_path + '\n' + ret.append(attr_path) else: if 'drv_attr_name' in attr.keys(): real_name = attr['drv_attr_name'] + real_dev = dev + elif 'attr_devattr' in attr.keys(): + real_name = attr['attr_devattr'] + real_devname = attr['attr_devname'] if 'attr_devname' in attr.keys() else '' + real_dev = self.data[real_devname] else: real_name = attr['attr_name'] + real_dev = dev - dsysfs_path = self.show_device_sysfs(dev, ops) + \ - "/%d-00%x" %(int(dev['i2c']['topo_info']['parent_bus'], 0), - int(dev['i2c']['topo_info']['dev_addr'], 0)) + \ - "/%s"%real_name - if not dsysfs_path in self.data_sysfs_obj[KEY]: + dsysfs_path = self.show_device_sysfs(real_dev, ops) + \ + "/%d-00%x" % (int(real_dev['i2c']['topo_info']['parent_bus'], 0), + int(real_dev['i2c']['topo_info']['dev_addr'], 0)) + \ + "/%s" % real_name + if dsysfs_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(dsysfs_path) - ret += dsysfs_path+"\n" + ret.append(dsysfs_path) return ret def show_attr_xcvr_device(self, dev, ops): - return self.show_attr_xcvr_i2c_device(dev, ops ) + return self.show_attr_xcvr_i2c_device(dev, ops) def show_attr_cpld_device(self, dev, ops): - ret = "" - KEY="cpld" - if not KEY in self.data_sysfs_obj: + ret = [] + KEY = "cpld" + if KEY not in self.data_sysfs_obj: + self.data_sysfs_obj[KEY] = [] + + return ret + + def show_attr_fpgai2c_device(self, dev, ops): + ret = [] + KEY="fpgai2c" + if KEY not in self.data_sysfs_obj: self.data_sysfs_obj[KEY]=[] return ret def show_attr_cpldmux_device(self, dev, ops): - ret = "" - KEY="cpldmux" - if not KEY in self.data_sysfs_obj: + ret = [] + KEY = "cpldmux" + if KEY not in self.data_sysfs_obj: + self.data_sysfs_obj[KEY] = [] + + return ret + + + def show_attr_fpgapci_device(self, dev, ops): + ret = [] + KEY="fpgapci" + if KEY not in self.data_sysfs_obj: self.data_sysfs_obj[KEY]=[] return ret @@ -795,25 +838,25 @@ def show_attr_cpldmux_device(self, dev, ops): # SHOW DEFS ################################################################################################################### def check_led_cmds(self, key, ops): - name = ops['target']+'_LED' - if (ops['target']=='config' or ops['attr']=='all') or \ - (name==self.data[key]['dev_info']['device_name'] and - ops['attr']==self.data[key]['dev_attr']['index']): - return (True) - else: - return (False) + name = ops['target']+'_LED' + if (ops['target'] == 'config' or ops['attr'] == 'all') or \ + (name == self.data[key]['dev_info']['device_name'] and + ops['attr'] == self.data[key]['dev_attr']['index']): + return (True) + else: + return (False) def dump_sysfs_obj(self, obj, key_type): - if (key_type == 'keys'): - for key in obj.keys(): - print key - return + if (key_type == 'keys'): + for key in obj.keys(): + print(key) + return - for key in obj: - if (key == key_type or key_type == 'all'): - print key+":" - for entry in obj[key]: - print "\t"+entry + for key in obj: + if (key == key_type or key_type == 'all'): + print(key+":") + for entry in obj[key]: + print("\t"+entry) def add_list_sysfs_obj(self, obj, KEY, list): for sysfs in list: @@ -821,160 +864,153 @@ def add_list_sysfs_obj(self, obj, KEY, list): obj[KEY].append(sysfs) def sysfs_attr(self, key, value, path, obj, obj_key): - sysfs_path="/sys/kernel/%s/%s"%(path, key) - if not sysfs_path in obj[obj_key]: - obj[obj_key].append(sysfs_path) - + sysfs_path = "/sys/kernel/%s/%s" % (path, key) + if not sysfs_path in obj[obj_key]: + obj[obj_key].append(sysfs_path) def sysfs_device(self, attr, path, obj, obj_key): - for key in attr.keys(): - sysfs_path="/sys/kernel/%s/%s"%(path, key) - if not sysfs_path in obj[obj_key]: - obj[obj_key].append(sysfs_path) + for key in attr.keys(): + sysfs_path = "/sys/kernel/%s/%s" % (path, key) + if not sysfs_path in obj[obj_key]: + obj[obj_key].append(sysfs_path) def show_eeprom_device(self, dev, ops): - return - + return def show_mux_device(self, dev, ops): - KEY ='mux' - if not KEY in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - self.sysfs_device(dev['i2c']['topo_info'], "pddf/devices/mux", self.sysfs_obj, KEY) - self.sysfs_device(dev['i2c']['dev_attr'], "pddf/devices/mux", self.sysfs_obj, KEY) - sysfs_path= "/sys/kernel/pddf/devices/mux/dev_ops" - if not sysfs_path in self.sysfs_obj[KEY]: - self.sysfs_obj[KEY].append(sysfs_path) - list=['/sys/kernel/pddf/devices/mux/i2c_type', - '/sys/kernel/pddf/devices/mux/i2c_name', - '/sys/kernel/pddf/devices/mux/error'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) + KEY = 'mux' + if not KEY in self.sysfs_obj: + self.sysfs_obj[KEY] = [] + self.sysfs_device(dev['i2c']['topo_info'], "pddf/devices/mux", self.sysfs_obj, KEY) + self.sysfs_device(dev['i2c']['dev_attr'], "pddf/devices/mux", self.sysfs_obj, KEY) + sysfs_path = "/sys/kernel/pddf/devices/mux/dev_ops" + if not sysfs_path in self.sysfs_obj[KEY]: + self.sysfs_obj[KEY].append(sysfs_path) + list = ['/sys/kernel/pddf/devices/mux/i2c_type', + '/sys/kernel/pddf/devices/mux/i2c_name', + '/sys/kernel/pddf/devices/mux/error'] + self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) def show_gpio_device(self, dev, ops): - KEY ='gpio' - if not KEY in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - self.sysfs_device(dev['i2c']['topo_info'], "pddf/devices/gpio", self.sysfs_obj, KEY) - self.sysfs_device(dev['i2c']['dev_attr'], "pddf/devices/gpio", self.sysfs_obj, KEY) - sysfs_path= "/sys/kernel/pddf/devices/gpio/dev_ops" - if not sysfs_path in self.sysfs_obj[KEY]: - self.sysfs_obj[KEY].append(sysfs_path) - list=['/sys/kernel/pddf/devices/gpio/i2c_type', - '/sys/kernel/pddf/devices/gpio/i2c_name', - '/sys/kernel/pddf/devices/gpio/error'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) - + KEY = 'gpio' + if not KEY in self.sysfs_obj: + self.sysfs_obj[KEY] = [] + self.sysfs_device(dev['i2c']['topo_info'], "pddf/devices/gpio", self.sysfs_obj, KEY) + self.sysfs_device(dev['i2c']['dev_attr'], "pddf/devices/gpio", self.sysfs_obj, KEY) + sysfs_path = "/sys/kernel/pddf/devices/gpio/dev_ops" + if not sysfs_path in self.sysfs_obj[KEY]: + self.sysfs_obj[KEY].append(sysfs_path) + list = ['/sys/kernel/pddf/devices/gpio/i2c_type', + '/sys/kernel/pddf/devices/gpio/i2c_name', + '/sys/kernel/pddf/devices/gpio/error'] + self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) def show_psu_i2c_device(self, dev, ops): - KEY ='psu' - path='pddf/devices/psu/i2c' + KEY = 'psu' + path = 'pddf/devices/psu/i2c' if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['PSU']: if not KEY in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - self.sysfs_device(dev['i2c']['topo_info'], path, self.sysfs_obj, KEY) - sysfs_path = "/sys/kernel/pddf/devices/psu/i2c/psu_idx" - self.sysfs_obj[KEY].append(sysfs_path) + self.sysfs_obj[KEY] = [] + self.sysfs_device(dev['i2c']['topo_info'], path, self.sysfs_obj, KEY) + sysfs_path = "/sys/kernel/pddf/devices/psu/i2c/psu_idx" + self.sysfs_obj[KEY].append(sysfs_path) - for attr in dev['i2c']['attr_list']: - self.sysfs_device(attr, "pddf/devices/psu/i2c", self.sysfs_obj, KEY) - sysfs_path = "/sys/kernel/pddf/devices/psu/i2c/dev_ops" - if not sysfs_path in self.sysfs_obj[KEY]: - self.sysfs_obj[KEY].append(sysfs_path) - list=['/sys/kernel/pddf/devices/psu/i2c/i2c_type', - '/sys/kernel/pddf/devices/fan/i2c/i2c_name', - '/sys/kernel/pddf/devices/psu/i2c/error', - '/sys/kernel/pddf/devices/psu/i2c/attr_ops'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) + for attr in dev['i2c']['attr_list']: + self.sysfs_device(attr, "pddf/devices/psu/i2c", self.sysfs_obj, KEY) + sysfs_path = "/sys/kernel/pddf/devices/psu/i2c/dev_ops" + if not sysfs_path in self.sysfs_obj[KEY]: + self.sysfs_obj[KEY].append(sysfs_path) + list = ['/sys/kernel/pddf/devices/psu/i2c/i2c_type', + '/sys/kernel/pddf/devices/fan/i2c/i2c_name', + '/sys/kernel/pddf/devices/psu/i2c/error', + '/sys/kernel/pddf/devices/psu/i2c/attr_ops'] + self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) def show_psu_device(self, dev, ops): - self.show_psu_i2c_device(dev, ops ) - return + self.show_psu_i2c_device(dev, ops) + return def show_client_device(self): - KEY ='client' + KEY = 'client' if not KEY in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - list=['/sys/kernel/pddf/devices/showall'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) - + self.sysfs_obj[KEY] = [] + list = ['/sys/kernel/pddf/devices/showall'] + self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) def show_fan_device(self, dev, ops): - KEY ='fan' - path='pddf/devices/fan/i2c' + KEY = 'fan' + path = 'pddf/devices/fan/i2c' if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['FAN']: if not KEY in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - - self.sysfs_device(dev['i2c']['topo_info'], path, self.sysfs_obj, KEY) - self.sysfs_device(dev['i2c']['dev_attr'], path, self.sysfs_obj, KEY) - for attr in dev['i2c']['attr_list']: - self.sysfs_device(attr, path, self.sysfs_obj, KEY) - list=['/sys/kernel/pddf/devices/fan/i2c/i2c_type', - '/sys/kernel/pddf/devices/fan/i2c/i2c_name', - '/sys/kernel/pddf/devices/fan/i2c/error', - '/sys/kernel/pddf/devices/fan/i2c/attr_ops', - '/sys/kernel/pddf/devices/fan/i2c/dev_ops'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) + self.sysfs_obj[KEY] = [] + self.sysfs_device(dev['i2c']['topo_info'], path, self.sysfs_obj, KEY) + self.sysfs_device(dev['i2c']['dev_attr'], path, self.sysfs_obj, KEY) + for attr in dev['i2c']['attr_list']: + self.sysfs_device(attr, path, self.sysfs_obj, KEY) + list = ['/sys/kernel/pddf/devices/fan/i2c/i2c_type', + '/sys/kernel/pddf/devices/fan/i2c/i2c_name', + '/sys/kernel/pddf/devices/fan/i2c/error', + '/sys/kernel/pddf/devices/fan/i2c/attr_ops', + '/sys/kernel/pddf/devices/fan/i2c/dev_ops'] + self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) def show_temp_sensor_device(self, dev, ops): - return + return def show_sysstatus_device(self, dev, ops): - KEY ='sysstatus' + KEY = 'sysstatus' if not KEY in self.sysfs_obj: self.sysfs_obj[KEY] = [] for attr in dev['attr_list']: - self.sysfs_device(attr, "pddf/devices/sysstatus", self.sysfs_obj, KEY) - sysfs_path= "/sys/kernel/pddf/devices/sysstatus/attr_ops" - if not sysfs_path in self.sysfs_obj[KEY]: - self.sysfs_obj[KEY].append(sysfs_path) - + self.sysfs_device(attr, "pddf/devices/sysstatus", self.sysfs_obj, KEY) + sysfs_path = "/sys/kernel/pddf/devices/sysstatus/attr_ops" + if not sysfs_path in self.sysfs_obj[KEY]: + self.sysfs_obj[KEY].append(sysfs_path) def show_xcvr_i2c_device(self, dev, ops): - KEY ='xcvr' + KEY = 'xcvr' if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['PORT_MODULE']: if not KEY in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - self.sysfs_device(dev['i2c']['topo_info'], "pddf/devices/xcvr/i2c", self.sysfs_obj, KEY) - - for attr in dev['i2c']['attr_list']: - self.sysfs_device(attr, "pddf/devices/xcvr/i2c", self.sysfs_obj, KEY) - sysfs_path = "/sys/kernel/pddf/devices/xcvr/i2c/dev_ops" - if not sysfs_path in self.sysfs_obj[KEY]: - self.sysfs_obj[KEY].append(sysfs_path) - list=['/sys/kernel/pddf/devices/xcvr/i2c/i2c_type', - '/sys/kernel/pddf/devices/xcvr/i2c/i2c_name', - '/sys/kernel/pddf/devices/xcvr/i2c/error', - '/sys/kernel/pddf/devices/xcvr/i2c/attr_ops'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) + self.sysfs_obj[KEY] = [] + self.sysfs_device(dev['i2c']['topo_info'], "pddf/devices/xcvr/i2c", self.sysfs_obj, KEY) + for attr in dev['i2c']['attr_list']: + self.sysfs_device(attr, "pddf/devices/xcvr/i2c", self.sysfs_obj, KEY) + sysfs_path = "/sys/kernel/pddf/devices/xcvr/i2c/dev_ops" + if not sysfs_path in self.sysfs_obj[KEY]: + self.sysfs_obj[KEY].append(sysfs_path) + list = ['/sys/kernel/pddf/devices/xcvr/i2c/i2c_type', + '/sys/kernel/pddf/devices/xcvr/i2c/i2c_name', + '/sys/kernel/pddf/devices/xcvr/i2c/error', + '/sys/kernel/pddf/devices/xcvr/i2c/attr_ops'] + self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) def show_xcvr_device(self, dev, ops): - self.show_xcvr_i2c_device(dev, ops ) - return + self.show_xcvr_i2c_device(dev, ops) + return def show_cpld_device(self, dev, ops): - KEY ='cpld' + KEY = 'cpld' if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['CPLD']: if not KEY in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - self.sysfs_device(dev['i2c']['topo_info'], "pddf/devices/cpld", self.sysfs_obj, KEY) - sysfs_path= "/sys/kernel/pddf/devices/cpld/dev_ops" - if not sysfs_path in self.sysfs_obj[KEY]: - self.sysfs_obj[KEY].append(sysfs_path) - list=['/sys/kernel/pddf/devices/cpld/i2c_type', - '/sys/kernel/pddf/devices/cpld/i2c_name', - '/sys/kernel/pddf/devices/cpld/error'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) + self.sysfs_obj[KEY] = [] + self.sysfs_device(dev['i2c']['topo_info'], "pddf/devices/cpld", self.sysfs_obj, KEY) + sysfs_path = "/sys/kernel/pddf/devices/cpld/dev_ops" + if not sysfs_path in self.sysfs_obj[KEY]: + self.sysfs_obj[KEY].append(sysfs_path) + list = ['/sys/kernel/pddf/devices/cpld/i2c_type', + '/sys/kernel/pddf/devices/cpld/i2c_name', + '/sys/kernel/pddf/devices/cpld/error'] + self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) def show_led_platform_device(self, key, ops): - if ops['attr']=='all' or ops['attr']=='PLATFORM': - KEY='platform' + if ops['attr'] == 'all' or ops['attr'] == 'PLATFORM': + KEY = 'platform' if not KEY in self.sysfs_obj: self.sysfs_obj[KEY] = [] - path='pddf/devices/platform' + path = 'pddf/devices/platform' self.sysfs_attr('num_psus', self.data['PLATFORM']['num_psus'], path, self.sysfs_obj, KEY) self.sysfs_attr('num_fantrays', self.data['PLATFORM']['num_fantrays'], path, self.sysfs_obj, KEY) @@ -991,7 +1027,7 @@ def show_led_device(self, key, ops): path,self.sysfs_obj, KEY) self.sysfs_device(self.data[key]['dev_attr'], path, self.sysfs_obj, KEY) for attr_key in attr.keys(): - attr_path="pddf/devices/led/" + attr['attr_name'] + attr_path = "pddf/devices/led/" + attr['attr_name'] if (attr_key != 'attr_name' and attr_key != 'swpld_addr' and attr_key != 'swpld_addr_offset'): self.sysfs_attr(attr_key, attr[attr_key], attr_path, self.sysfs_obj, KEY) sysfs_path="/sys/kernel/pddf/devices/led/dev_ops" @@ -1011,7 +1047,7 @@ def validate_xcvr_device(self, dev, ops): if 'attr_name' in attr.keys() and 'eeprom' in attr.values(): ret_val = "xcvr validation success" else: - print "xcvr validation Failed" + print("xcvr validation Failed") return elif dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['PORT_MODULE']: @@ -1019,9 +1055,9 @@ def validate_xcvr_device(self, dev, ops): if attr.get("attr_name") in dev_attribs: ret_val = "Success" else: - print "xcvr validation Failed" + print("xcvr validation Failed") return - print ret_val + print(ret_val) def validate_eeprom_device(self, dev, ops): devtype_list = ['24c02'] @@ -1034,7 +1070,7 @@ def validate_eeprom_device(self, dev, ops): for attr in dev['i2c']['attr_list']: if attr.get("attr_name") in dev_attribs: ret_val = "eeprom success" - print ret_val + print(ret_val) def validate_mux_device(self, dev, ops): devtype_list = ['pca9548', 'pca954x'] @@ -1045,7 +1081,7 @@ def validate_mux_device(self, dev, ops): for attr in dev['i2c']['channel']: if attr.get("chn") in dev_channels: ret_val = "Mux success" - print ret_val + print(ret_val) def validate_cpld_device(self, dev, ops): devtype_list = ['i2c_cpld'] @@ -1053,20 +1089,20 @@ def validate_cpld_device(self, dev, ops): if dev['i2c']['topo_info']['dev_type'] in devtype_list: ret_val = "cpld success" - print ret_val + print(ret_val) def validate_sysstatus_device(self, dev, ops): dev_attribs = ['board_info', 'cpld1_version', 'power_module_status', 'system_reset5', - 'system_reset6', 'system_reset7', 'misc1', 'cpld2_version', 'cpld3_version' - ] + 'system_reset6', 'system_reset7', 'misc1', 'cpld2_version', 'cpld3_version' + ] ret_val = "sysstatus failed" if dev['dev_info']['device_type'] == "SYSSTAT": - for attr in dev['attr_list']: - if attr.get("attr_name") in dev_attribs: - ret_val = "sysstatus success" - print ret_val + for attr in dev['attr_list']: + if attr.get("attr_name") in dev_attribs: + ret_val = "sysstatus success" + print(ret_val) def validate_temp_sensor_device(self, dev, ops): devtype_list = ['lm75'] @@ -1078,21 +1114,21 @@ def validate_temp_sensor_device(self, dev, ops): for attr in dev['i2c']['attr_list']: if attr.get("attr_name") in dev_attribs: ret_val = "tempsensor success" - print ret_val + print(ret_val) def validate_fan_device(self, dev, ops): ret_val = "fan failed" if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['FAN']: - if dev['i2c']['dev_attr']['num_fan'] is not None: + if dev['i2c']['dev_attr']['num_fantrays'] is not None: ret_val = "fan success" - print ret_val + print(ret_val) def validate_psu_device(self, dev, ops): dev_attribs = ['psu_present', 'psu_model_name', 'psu_power_good', 'psu_mfr_id', 'psu_serial_num', - 'psu_fan_dir', 'psu_v_out', 'psu_i_out', 'psu_p_out', 'psu_fan1_speed_rpm' - ] + 'psu_fan_dir', 'psu_v_out', 'psu_i_out', 'psu_p_out', 'psu_fan1_speed_rpm' + ] ret_val = "psu failed" if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['PSU']: @@ -1102,167 +1138,167 @@ def validate_psu_device(self, dev, ops): if attr.get("attr_offset") is not None: if attr.get("attr_mask") is not None: if attr.get("attr_len") is not None: - ret_val = "psu success" + ret_val = "psu success" else: ret_val = "psu failed" - print ret_val + print(ret_val) ################################################################################################################### # SPYTEST ################################################################################################################### def verify_attr(self, key, attr, path): - node="/sys/kernel/%s/%s"%(path, key) - try: - with open(node, 'r') as f: - status = f.read() - except IOError: - print "PDDF_VERIFY_ERR: IOError: node:%s key:%s"%(node, key) - return + node = "/sys/kernel/%s/%s" % (path, key) + try: + with open(node, 'r') as f: + status = f.read() + except IOError: + print("PDDF_VERIFY_ERR: IOError: node:%s key:%s" % (node, key)) + return - status=status.rstrip("\n\r") - if attr[key]!=status: - print "PDDF_VERIFY_ERR: node: %s switch:%s"%(node, status) + status = status.rstrip("\n\r") + if attr[key] != status: + print("PDDF_VERIFY_ERR: node: %s switch:%s" % (node, status)) def verify_device(self, attr, path, ops): - for key in attr.keys(): - self.verify_attr(key, attr, path) - + for key in attr.keys(): + self.verify_attr(key, attr, path) def get_led_device(self, device_name): self.create_attr('device_name', self.data[device_name]['dev_info']['device_name'], "pddf/devices/led") self.create_attr('index', self.data[device_name]['dev_attr']['index'], "pddf/devices/led") - cmd="echo 'verify' > /sys/kernel/pddf/devices/led/dev_ops" + cmd = "echo 'verify' > /sys/kernel/pddf/devices/led/dev_ops" self.runcmd(cmd) def validate_sysfs_creation(self, obj, validate_type): - dir = '/sys/kernel/pddf/devices/'+validate_type - if (os.path.exists(dir) or validate_type=='client'): - for sysfs in obj[validate_type]: - if(not os.path.exists(sysfs)): - print "[SYSFS FILE] " + sysfs + ": does not exist" - else: - print "[SYSFS DIR] " + dir + ": does not exist" + dir = '/sys/kernel/pddf/devices/'+validate_type + if (os.path.exists(dir) or validate_type == 'client'): + for sysfs in obj[validate_type]: + if(not os.path.exists(sysfs)): + print("[SYSFS FILE] " + sysfs + ": does not exist") + else: + print("[SYSFS DIR] " + dir + ": does not exist") def validate_dsysfs_creation(self, obj, validate_type): if validate_type in obj.keys(): # There is a possibility that some components dont have any device-self.data attr if not obj[validate_type]: - print "[SYSFS ATTR] for " + validate_type + ": empty " + print("[SYSFS ATTR] for " + validate_type + ": empty ") else: - for sysfs in obj[validate_type]: - if(not os.path.exists(sysfs)): - print "[SYSFS FILE] " + sysfs + ": does not exist" + for sysfs in obj[validate_type]: + if(not os.path.exists(sysfs)): + print("[SYSFS FILE] " + sysfs + ": does not exist") else: - print "[SYSFS KEY] for " + validate_type + ": not configured" + print("[SYSFS KEY] for " + validate_type + ": not configured") def verify_sysfs_data(self, verify_type): - if (verify_type=='LED'): + if (verify_type == 'LED'): for key in self.data.keys(): if key != 'PLATFORM': - attr=self.data[key]['dev_info'] + attr = self.data[key]['dev_info'] if attr['device_type'] == 'LED': self.get_led_device(key) self.verify_attr('device_name', self.data[key]['dev_info'], "pddf/devices/led") self.verify_attr('index', self.data[key]['dev_attr'], "pddf/devices/led") for attr in self.data[key]['i2c']['attr_list']: - path="pddf/devices/led/" + attr['attr_name'] + path = "pddf/devices/led/" + attr['attr_name'] for entry in attr.keys(): if (entry != 'attr_name' and entry != 'swpld_addr' and entry != 'swpld_addr_offset'): - self.verify_attr(entry, attr, path) - if ( entry == 'swpld_addr' or entry == 'swpld_addr_offset'): - self.verify_attr(entry, attr, 'pddf/devices/led') + self.verify_attr(entry, attr, path) + if (entry == 'swpld_addr' or entry == 'swpld_addr_offset'): + self.verify_attr(entry, attr, 'pddf/devices/led') def schema_validation(self, validate_type): - process_validate_type = 0 - for key in self.data.keys(): - if (key != 'PLATFORM'): - temp_obj={} - schema_list=[] + process_validate_type = 0 + for key in self.data.keys(): + if (key != 'PLATFORM'): + temp_obj = {} + schema_list = [] + try: + device_type = self.data[key]["dev_info"]["device_type"] + except Exception as e: + print("dev_info or device_type ERROR: " + key) + print(e) + + if validate_type == 'mismatch': + process_validate_type = 1 + device_type = "PSU" + schema_file = "/usr/local/bin/schema/FAN.schema" + schema_list.append(schema_file) + elif validate_type == 'missing': + process_validate_type = 1 + schema_file = "/usr/local/bin/schema/PLATFORM.schema" + schema_list.append(schema_file) + + elif validate_type == 'empty': + process_validate_type = 1 + if not device_type: + print("Empty device_type for " + key) + continue + elif (validate_type == 'all' or validate_type == device_type): + process_validate_type = 1 + if "bmc" in self.data[key].keys(): + schema_file = "/usr/local/bin/schema/"+device_type + "_BMC.schema" + schema_list.append(schema_file) + + if "i2c" in self.data[key].keys(): + schema_file = "/usr/local/bin/schema/"+device_type + ".schema" + schema_list.append(schema_file) + if device_type: + temp_obj[device_type] = self.data[key] + for schema_file in schema_list: + if (os.path.exists(schema_file)): + print("Validate " + schema_file + ";" + key) + json_data = json.dumps(temp_obj) + with open(schema_file, 'r') as f: + schema = json.load(f) try: - device_type=self.data[key]["dev_info"]["device_type"] + from jsonschema import validate + validate(temp_obj, schema) except Exception as e: - print "dev_info or device_type ERROR: " + key - print e - - if validate_type == 'mismatch': - process_validate_type = 1 - device_type="PSU" - schema_file="/usr/local/bin/schema/FAN.schema" - schema_list.append(schema_file) - elif validate_type == 'missing': - process_validate_type = 1 - schema_file="/usr/local/bin/schema/PLATFORM.schema" - schema_list.append(schema_file) - - elif validate_type == 'empty': - process_validate_type = 1 - if not device_type: - print "Empty device_type for " + key - continue - elif (validate_type=='all' or validate_type==device_type): - process_validate_type = 1 - if "bmc" in self.data[key].keys(): - schema_file="/usr/local/bin/schema/"+device_type + "_BMC.schema" - schema_list.append(schema_file) - - if "i2c" in self.data[key].keys(): - schema_file="/usr/local/bin/schema/"+device_type + ".schema" - schema_list.append(schema_file) - if device_type: - temp_obj[device_type]=self.data[key] - for schema_file in schema_list: - if (os.path.exists(schema_file)): - print "Validate " + schema_file + ";" + key - json_data=json.dumps(temp_obj) - with open(schema_file, 'r') as f: - schema=json.load(f) - try: - validate(temp_obj, schema) - except Exception as e: - print "Validation ERROR: " + schema_file + ";" + key - if validate_type == 'mismatch': - return - else: - print e - else: - print "ERROR Missing File: " + schema_file - if not process_validate_type: - print "device_type: " + validate_type + " not configured" + print("Validation ERROR: " + schema_file + ";" + key) + if validate_type == 'mismatch': + return + else: + print(e) + else: + print("ERROR Missing File: " + schema_file) + if not process_validate_type: + print("device_type: " + validate_type + " not configured") def modules_validation(self, validate_type): kos = [] supported_type = False - module_validation_status=[] + module_validation_status = [] - if validate_type == "bmc": - kos=['ipmi_devintf', 'ipmi_si', 'ipmi_msghandler'] - validate_type = 'ipmi' + if validate_type == "bmc": + kos = ['ipmi_devintf', 'ipmi_si', 'ipmi_msghandler'] + validate_type = 'ipmi' else: - # generate the KOS list from pddf device JSON file - kos.extend(self.data['PLATFORM']['pddf_kos']) + # generate the KOS list from pddf device JSON file + kos.extend(self.data['PLATFORM']['pddf_kos']) - if 'custom_kos' in self.data['PLATFORM']: - kos.extend(self.data['PLATFORM']['custom_kos']) + if 'custom_kos' in self.data['PLATFORM']: + kos.extend(self.data['PLATFORM']['custom_kos']) for mod in kos: if validate_type in mod or validate_type == "pddf": - supported_type=True - cmd = "lsmod | grep " + mod - try: - subprocess.check_output(cmd, shell=True) - except Exception as e: - module_validation_status.append(mod) + supported_type = True + cmd = "lsmod | grep " + mod + try: + subprocess.check_output(cmd, shell=True) + except Exception as e: + module_validation_status.append(mod) if supported_type: if module_validation_status: - module_validation_status.append(":ERROR not loaded") - print str(module_validation_status)[1:-1] + module_validation_status.append(":ERROR not loaded") + print(str(module_validation_status)[1:-1]) else: - print "Loaded" + print("Loaded") else: - print validate_type + " not configured" + print(validate_type + " not configured" ) @@ -1272,428 +1308,386 @@ def modules_validation(self, validate_type): # PARSE DEFS ################################################################################################################### def psu_parse(self, dev, ops): - parse_str="" - ret="" - for ifce in dev['i2c']['interface']: - ret=getattr(self, ops['cmd']+"_psu_device")(self.data[ifce['dev']], ops ) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + ret = [] + for ifce in (dev['i2c']['interface'] if 'i2c' in dev else []): + val = getattr(self, ops['cmd']+"_psu_device")(self.data[ifce['dev']], ops) + if val: + if str(val[0]).isdigit(): + if val[0] != 0: # in case if 'create' functions - print "{}_psu_device failed".format(ops['cmd']) - return ret - else: - pass + print("{}_psu_device failed for {}".format(ops['cmd'], ifce['dev'])) + return val else: # in case of 'show_attr' functions - parse_str+=ret - return parse_str + ret.extend(val) + return ret def fan_parse(self, dev, ops): - parse_str="" - ret=getattr(self, ops['cmd']+"_fan_device")(dev, ops ) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + ret = [] + ret = getattr(self, ops['cmd']+"_fan_device")(dev, ops) + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print "{}_fan_device failed".format(ops['cmd']) - return ret - else: - pass - else: - # in case of 'show_attr' functions - parse_str+=ret + print("{}_fan_device failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) - return parse_str + return ret def temp_sensor_parse(self, dev, ops): - parse_str="" - ret=getattr(self, ops['cmd']+"_temp_sensor_device")(dev, ops ) - if not ret is None: - if str(ret).isdigit() : - if ret!=0: + ret = [] + ret = getattr(self, ops['cmd']+"_temp_sensor_device")(dev, ops) + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print "{}_temp_sensor_device failed".format(ops['cmd']) - return ret - else: - pass - else: - # in case of 'show_attr' functions - parse_str+=ret + print("{}_temp_sensor_device failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) - return parse_str + return ret def cpld_parse(self, dev, ops): - parse_str = "" + ret = [] ret = getattr(self, ops['cmd']+"_cpld_device")(dev, ops) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print "{}_cpld_device failed".format(ops['cmd']) + print("{}_cpld_device failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) + + return ret + + def fpgai2c_parse(self, dev, ops): + val = [] + ret = getattr(self, ops['cmd']+"_fpgai2c_device")(dev, ops) + if ret: + if str(ret[0]).isdigit(): + if ret[0]!=0: + # in case if 'create' functions + print("{}_fpgai2c_device failed".format(ops['cmd'])) return ret - else: - pass else: # in case of 'show_attr' functions - parse_str+=ret + val.extend(ret) - return parse_str + return val def cpldmux_parse(self, dev, ops): - parse_str = "" + val = [] ret = getattr(self, ops['cmd']+"_cpldmux_device")(dev, ops) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print "{}_cpldmux_device() cmd failed".format(ops['cmd']) + print("{}_cpldmux_device() cmd failed".format(ops['cmd'])) return ret - else: - pass else: - parse_str += ret + val.extend(ret) for chan in dev['i2c']['channel']: for device in chan['dev']: - ret = self.dev_parse(self.data[device], ops) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + ret = self.dev_parse(self.data[device], ops) + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions return ret - else: - pass else: - parse_str += ret + val.extend(ret) - return parse_str + return val def cpldmux_parse_reverse(self, dev, ops): - parse_str = "" + val = [] for chan in reversed(dev['i2c']['channel']): for device in reversed(chan['dev']): - ret = self.dev_parse(self.data[device], ops) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + ret = self.dev_parse(self.data[device], ops) + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions return ret - else: - pass else: - parse_str += ret + val.extend(ret) ret = getattr(self, ops['cmd']+"_cpldmux_device")(dev, ops) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print "{}_cpldmux_device() cmd failed".format(ops['cmd']) + print("{}_cpldmux_device() cmd failed".format(ops['cmd'])) return ret - else: - pass else: - parse_str += ret - - return parse_str + val.extend(ret) + return val - def sysstatus_parse(self, dev,ops): - parse_str = "" + def sysstatus_parse(self, dev, ops): + ret = [] ret = getattr(self, ops['cmd']+"_sysstatus_device")(dev, ops) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print "{}_sysstatus_device failed".format(ops['cmd']) - return ret - else: - pass - else: - # in case of 'show_attr' functions - parse_str+=ret + print("{}_sysstatus_device failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) - return parse_str + return ret def gpio_parse(self, dev, ops): - parse_str = "" + ret = [] ret = getattr(self, ops['cmd']+"_gpio_device")(dev, ops) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print "{}_temp_sensor_device failed".format(ops['cmd']) - return ret - else: - pass - else: - # in case of 'show_attr' functions - parse_str += ret - - return parse_str + print("{}_gpio_device failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) + return ret def mux_parse(self, dev, ops): - parse_str = "" + val = [] ret = getattr(self, ops['cmd']+"_mux_device")(dev, ops) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print "{}_mux_device() cmd failed".format(ops['cmd']) + print("{}_mux_device() cmd failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) return ret - else: - pass else: - parse_str += ret + val.extend(ret) for ch in dev['i2c']['channel']: - ret = self.dev_parse(self.data[ch['dev']], ops) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + ret = self.dev_parse(self.data[ch['dev']], ops) + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions return ret - else: - pass else: - parse_str += ret - return parse_str + val.extend(ret) + return val def mux_parse_reverse(self, dev, ops): - parse_str = "" + val = [] for ch in reversed(dev['i2c']['channel']): - ret = self.dev_parse(self.data[ch['dev']], ops) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + ret = self.dev_parse(self.data[ch['dev']], ops) + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions return ret - else: - pass else: - parse_str += ret + val.extend(ret) ret = getattr(self, ops['cmd']+"_mux_device")(dev, ops) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print "{}_mux_device() cmd failed".format(ops['cmd']) + print("{}_mux_device() cmd failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) return ret - else: - pass else: - parse_str += ret - - return parse_str + val.extend(ret) + return val def eeprom_parse(self, dev, ops): - parse_str = "" + ret = [] ret = getattr(self, ops['cmd']+"_eeprom_device")(dev, ops) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print "{}_eeprom_device() cmd failed".format(ops['cmd']) - return ret - else: - pass - else: - parse_str += ret + print("{}_eeprom_device() cmd failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) - return parse_str + return ret def optic_parse(self, dev, ops): - parse_str="" - ret="" + val = [] for ifce in dev['i2c']['interface']: - ret=getattr(self, ops['cmd']+"_xcvr_device")(self.data[ifce['dev']], ops ) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + ret = getattr(self, ops['cmd']+"_xcvr_device")(self.data[ifce['dev']], ops) + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print "{}_eeprom_device() cmd failed".format(ops['cmd']) + print("{}_xcvr_device() cmd failed for {}".format(ops['cmd'], ifce['dev'])) return ret - else: - pass else: - parse_str+=ret - return parse_str + val.extend(ret) + return val def cpu_parse(self, bus, ops): - parse_str = "" + val = [] for dev in bus['i2c']['CONTROLLERS']: dev1 = self.data[dev['dev']] for d in dev1['i2c']['DEVICES']: - ret=self.dev_parse(self.data[d['dev']], ops) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + ret = self.dev_parse(self.data[d['dev']], ops) + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions return ret - else: - pass else: - parse_str += ret - return parse_str + val.extend(ret) + return val def cpu_parse_reverse(self, bus, ops): - parse_str = "" + val = [] for dev in reversed(bus['i2c']['CONTROLLERS']): dev1 = self.data[dev['dev']] for d in reversed(dev1['i2c']['DEVICES']): - ret=self.dev_parse(self.data[d['dev']], ops) - if not ret is None: - if str(ret).isdigit(): - if ret!=0: + ret = self.dev_parse(self.data[d['dev']], ops) + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions return ret - else: - pass else: - parse_str += ret - return parse_str + val.extend(ret) + return val + + # 'create' and 'show_attr' ops returns an array + # 'delete', 'show' and 'validate' ops return None def dev_parse(self, dev, ops): - attr=dev['dev_info'] - if attr['device_type'] == 'CPU': - if ops['cmd']=='delete': - return self.cpu_parse_reverse(dev, ops) - else: - return self.cpu_parse(dev, ops) + attr=dev['dev_info'] + if attr['device_type'] == 'CPU': + if ops['cmd']=='delete': + return self.cpu_parse_reverse(dev, ops) + else: + return self.cpu_parse(dev, ops) - if attr['device_type'] == 'EEPROM': - return self.eeprom_parse(dev, ops) + if attr['device_type'] == 'EEPROM': + return self.eeprom_parse(dev, ops) - if attr['device_type'] == 'MUX': - if ops['cmd']=='delete': - return self.mux_parse_reverse(dev, ops) - else: - return self.mux_parse(dev, ops) + if attr['device_type'] == 'MUX': + if ops['cmd']=='delete': + return self.mux_parse_reverse(dev, ops) + else: + return self.mux_parse(dev, ops) - if attr['device_type'] == 'GPIO': - return self.gpio_parse(dev, ops) + if attr['device_type'] == 'GPIO': + return self.gpio_parse(dev, ops) - if attr['device_type'] == 'PSU': - return self.psu_parse(dev, ops) + if attr['device_type'] == 'PSU': + return self.psu_parse(dev, ops) - if attr['device_type'] == 'FAN': - return self.fan_parse(dev, ops) + if attr['device_type'] == 'FAN': + return self.fan_parse(dev, ops) - if attr['device_type'] == 'TEMP_SENSOR': - return self.temp_sensor_parse(dev, ops) + if attr['device_type'] == 'TEMP_SENSOR': + return self.temp_sensor_parse(dev, ops) - if attr['device_type'] == 'SFP' or attr['device_type'] == 'SFP28' or \ - attr['device_type'] == 'QSFP' or attr['device_type'] == 'QSFP28': - return self.optic_parse(dev, ops) + if attr['device_type'] == 'SFP' or attr['device_type'] == 'SFP+' or attr['device_type'] == 'SFP28' or \ + attr['device_type'] == 'QSFP' or attr['device_type'] == 'QSFP+' or attr['device_type'] == 'QSFP28' or \ + attr['device_type'] == 'QSFP-DD': + return self.optic_parse(dev, ops) - if attr['device_type'] == 'CPLD': - return self.cpld_parse(dev, ops) + if attr['device_type'] == 'CPLD': + return self.cpld_parse(dev, ops) - if attr['device_type'] == 'CPLDMUX': - if ops['cmd']=='delete': - return self.cpldmux_parse_reverse(dev, ops) - else: - return self.cpldmux_parse(dev, ops) + if attr['device_type'] == 'CPLDMUX': + if ops['cmd'] == 'delete': + return self.cpldmux_parse_reverse(dev, ops) + else: + return self.cpldmux_parse(dev, ops) - if attr['device_type'] == 'SYSSTAT': - return self.sysstatus_parse(dev,ops) + if attr['device_type'] == 'SYSSTAT': + return self.sysstatus_parse(dev, ops) def is_supported_sysled_state(self, sysled_name, sysled_state): - if not sysled_name in self.data.keys(): - return False, "[FAILED] " + sysled_name + " is not configured" - for attr in self.data[sysled_name]['i2c']['attr_list']: - if attr['attr_name'] == sysled_state: - return True, "[PASS] supported" - return False, "[FAILED]: Invalid color" + if not sysled_name in self.data.keys(): + return False, "[FAILED] " + sysled_name + " is not configured" + for attr in self.data[sysled_name]['i2c']['attr_list']: + if attr['attr_name'] == sysled_state: + return True, "[PASS] supported" + return False, "[FAILED]: Invalid color" def create_attr(self, key, value, path): - cmd = "echo '%s' > /sys/kernel/%s/%s"%(value, path, key) - self.runcmd(cmd) + cmd = "echo '%s' > /sys/kernel/%s/%s" % (value, path, key) + self.runcmd(cmd) def create_led_platform_device(self, key, ops): - if ops['attr']=='all' or ops['attr']=='PLATFORM': - path='pddf/devices/platform' - self.create_attr('num_psus', self.data['PLATFORM']['num_psus'], path) - self.create_attr('num_fantrays', self.data['PLATFORM']['num_fantrays'], path) + if ops['attr'] == 'all' or ops['attr'] == 'PLATFORM': + path = 'pddf/devices/platform' + self.create_attr('num_psus', self.data['PLATFORM']['num_psus'], path) + self.create_attr('num_fantrays', self.data['PLATFORM']['num_fantrays'], path) def create_led_device(self, key, ops): - if ops['attr']=='all' or ops['attr']==self.data[key]['dev_info']['device_name']: - path="pddf/devices/led" - for attr in self.data[key]['i2c']['attr_list']: - self.create_attr('device_name', self.data[key]['dev_info']['device_name'], path) - self.create_device(self.data[key]['dev_attr'], path, ops) - for attr_key in attr.keys(): - if (attr_key == 'swpld_addr_offset' or attr_key == 'swpld_addr'): - self.create_attr(attr_key, attr[attr_key], path) - elif (attr_key != 'attr_name' and attr_key != 'descr' and - attr_key != 'attr_devtype' and attr_key != 'attr_devname'): - state_path=path+'/state_attr' - self.create_attr(attr_key, attr[attr_key],state_path) - cmd="echo '" + attr['attr_name']+"' > /sys/kernel/pddf/devices/led/dev_ops" - self.runcmd(cmd) + if ops['attr']=='all' or ops['attr']==self.data[key]['dev_info']['device_name']: + path="pddf/devices/led" + for attr in self.data[key]['i2c']['attr_list']: + self.create_attr('device_name', self.data[key]['dev_info']['device_name'], path) + self.create_device(self.data[key]['dev_attr'], path, ops) + for attr_key in attr.keys(): + if (attr_key == 'swpld_addr_offset' or attr_key == 'swpld_addr'): + self.create_attr(attr_key, attr[attr_key], path) + elif (attr_key != 'attr_name' and attr_key != 'descr' and + attr_key != 'attr_devtype' and attr_key != 'attr_devname'): + state_path=path+'/state_attr' + self.create_attr(attr_key, attr[attr_key],state_path) + cmd="echo '" + attr['attr_name']+"' > /sys/kernel/pddf/devices/led/dev_ops" + self.runcmd(cmd) def led_parse(self, ops): - getattr(self, ops['cmd']+"_led_platform_device")("PLATFORM", ops) - for key in self.data.keys(): - if key != 'PLATFORM' and 'dev_info' in self.data[key]: - attr=self.data[key]['dev_info'] - if attr['device_type'] == 'LED': - getattr(self, ops['cmd']+"_led_device")(key, ops) + getattr(self, ops['cmd']+"_led_platform_device")("PLATFORM", ops) + for key in self.data.keys(): + if key != 'PLATFORM' and 'dev_info' in self.data[key]: + attr = self.data[key]['dev_info'] + if attr['device_type'] == 'LED': + getattr(self, ops['cmd']+"_led_device")(key, ops) def get_device_list(self, list, type): - for key in self.data.keys(): - if key != 'PLATFORM' and 'dev_info' in self.data[key]: - attr=self.data[key]['dev_info'] - if attr['device_type'] == type: - list.append(self.data[key]) + for key in self.data.keys(): + if key != 'PLATFORM' and 'dev_info' in self.data[key]: + attr = self.data[key]['dev_info'] + if attr['device_type'] == type: + list.append(self.data[key]) def create_pddf_devices(self): - self.led_parse({ "cmd": "create", "target":"all", "attr":"all" }) + self.led_parse({"cmd": "create", "target": "all", "attr": "all"}) create_ret = 0 - create_ret = self.dev_parse(self.data['SYSTEM'], { "cmd": "create", "target":"all", "attr":"all" } ) - if create_ret!=0: - return create_ret + ret = self.dev_parse(self.data['SYSTEM'], {"cmd": "create", "target": "all", "attr": "all"}) + if ret: + if ret[0] != 0: + return ret[0] if 'SYSSTATUS' in self.data: - create_ret = self.dev_parse(self.data['SYSSTATUS'], { "cmd": "create", "target":"all", "attr":"all" } ) - if create_ret!=0: - return create_ret - + ret = self.dev_parse(self.data['SYSSTATUS'], {"cmd": "create", "target": "all", "attr": "all"}) + if ret: + if ret[0] != 0: + return ret[0] + return create_ret + + def delete_pddf_devices(self): - self.dev_parse(self.data['SYSTEM'], { "cmd": "delete", "target":"all", "attr":"all" } ) + self.dev_parse(self.data['SYSTEM'], {"cmd": "delete", "target": "all", "attr": "all"}) if 'SYSSTATUS' in self.data: - self.dev_parse(self.data['SYSSTATUS'], { "cmd": "delete", "target":"all", "attr":"all" } ) + self.dev_parse(self.data['SYSSTATUS'], {"cmd": "delete", "target": "all", "attr": "all"}) def populate_pddf_sysfsobj(self): - self.dev_parse(self.data['SYSTEM'], { "cmd": "show", "target":"all", "attr":"all" } ) + self.dev_parse(self.data['SYSTEM'], {"cmd": "show", "target": "all", "attr": "all"}) if 'SYSSTATUS' in self.data: - self.dev_parse(self.data['SYSSTATUS'], { "cmd": "show", "target":"all", "attr":"all" } ) - self.led_parse({ "cmd": "show", "target":"all", "attr":"all" }) + self.dev_parse(self.data['SYSSTATUS'], {"cmd": "show", "target": "all", "attr": "all"}) + self.led_parse({"cmd": "show", "target": "all", "attr": "all"}) self.show_client_device() def cli_dump_dsysfs(self, component): - self.dev_parse(self.data['SYSTEM'], { "cmd": "show_attr", "target":"all", "attr":"all" } ) + self.dev_parse(self.data['SYSTEM'], {"cmd": "show_attr", "target": "all", "attr": "all"}) if 'SYSSTATUS' in self.data: - self.dev_parse(self.data['SYSSTATUS'], { "cmd": "show_attr", "target":"all", "attr":"all" } ) + self.dev_parse(self.data['SYSSTATUS'], {"cmd": "show_attr", "target": "all", "attr": "all"}) if component in self.data_sysfs_obj: return self.data_sysfs_obj[component] else: - return None - + return None def validate_pddf_devices(self, *args): - self.populate_pddf_sysfsobj() - v_ops = { 'cmd': 'validate', 'target':'all', 'attr':'all' } - self.dev_parse(self.data['SYSTEM'], v_ops ) + self.populate_pddf_sysfsobj() + v_ops = {'cmd': 'validate', 'target': 'all', 'attr': 'all'} + self.dev_parse(self.data['SYSTEM'], v_ops) ################################################################################################################## # BMC APIs @@ -1707,48 +1701,48 @@ def populate_bmc_cache_db(self, bmc_attr): delim = None o_list = subprocess.check_output(bmc_cmd, shell=True).strip().split('\n') - bmc_cache[bmc_cmd]={} - bmc_cache[bmc_cmd]['time']=time.time() + bmc_cache[bmc_cmd] = {} + bmc_cache[bmc_cmd]['time'] = time.time() for entry in o_list: name = entry.split(delim)[0].strip() - bmc_cache[bmc_cmd][name]=entry + bmc_cache[bmc_cmd][name] = entry def non_raw_ipmi_get_request(self, bmc_attr): - bmc_db_update_time=1 + bmc_db_update_time = 1 value = 'N/A' bmc_cmd = str(bmc_attr['bmc_cmd']).strip() field_name = str(bmc_attr['field_name']).strip() - field_pos= int(bmc_attr['field_pos'])-1 - + field_pos = int(bmc_attr['field_pos'])-1 + if 'delimiter' in bmc_attr.keys(): delim = str(bmc_attr['delimiter']).strip() else: delim = None if not bmc_cmd in bmc_cache: - self.populate_bmc_cache_db(bmc_attr) + self.populate_bmc_cache_db(bmc_attr) else: - now = time.time() - if (int(now - bmc_cache[bmc_cmd]['time']) > bmc_db_update_time): - self.populate_bmc_cache_db(bmc_attr) + now = time.time() + if (int(now - bmc_cache[bmc_cmd]['time']) > bmc_db_update_time): + self.populate_bmc_cache_db(bmc_attr) try: - data=bmc_cache[bmc_cmd][field_name] - value = data.split(delim)[field_pos].strip() + data = bmc_cache[bmc_cmd][field_name] + value = data.split(delim)[field_pos].strip() except Exception as e: - pass + pass if 'mult' in bmc_attr.keys() and not value.isalpha(): if value.isalpha(): value = 0.0 value = float(value) * float(bmc_attr['mult']) - + return str(value) def raw_ipmi_get_request(self, bmc_attr): value = 'N/A' - cmd = bmc_attr['bmc_cmd'] + " 2>/dev/null" - if bmc_attr['type'] == 'raw': + cmd = bmc_attr['bmc_cmd'] + " 2>/dev/null" + if bmc_attr['type'] == 'raw': try: value = subprocess.check_output(cmd, shell=True).strip() except Exception as e: @@ -1758,7 +1752,7 @@ def raw_ipmi_get_request(self, bmc_attr): value = str(int(value, 16)) return value - if bmc_attr['type'] == 'mask': + if bmc_attr['type'] == 'mask': mask = int(bmc_attr['mask'].encode('utf-8'), 16) try: value = subprocess.check_output(cmd, shell=True).strip() @@ -1768,10 +1762,10 @@ def raw_ipmi_get_request(self, bmc_attr): # value should either be '1' or '0' if value != 'N/A': value = '1' if bool(int(value, 16) & mask) else '0' - + return value - if bmc_attr['type'] == 'ascii': + if bmc_attr['type'] == 'ascii': try: value = subprocess.check_output(cmd, shell=True) except Exception as e: @@ -1779,19 +1773,19 @@ def raw_ipmi_get_request(self, bmc_attr): if value != 'N/A': tmp = ''.join(chr(int(i, 16)) for i in value.split()) - tmp = "".join(i for i in unicode(tmp) if unicodedata.category(i)[0]!="C") + tmp = "".join(i for i in unicode(tmp) if unicodedata.category(i)[0] != "C") value = str(tmp) return (value) - return value + return value - def bmc_get_cmd(self, bmc_attr): - if int(bmc_attr['raw']) == 1: + def bmc_get_cmd(self, bmc_attr): + if int(bmc_attr['raw']) == 1: value = self.raw_ipmi_get_request(bmc_attr) - else: + else: value = self.non_raw_ipmi_get_request(bmc_attr) - return (value) + return (value) def non_raw_ipmi_set_request(self, bmc_attr, val): value = 'N/A' @@ -1801,14 +1795,14 @@ def non_raw_ipmi_set_request(self, bmc_attr, val): def raw_ipmi_set_request(self, bmc_attr, val): value = 'N/A' # TODO: Implement this - return value + return value - def bmc_set_cmd(self, bmc_attr, val): - if int(bmc_attr['raw']) == 1: + def bmc_set_cmd(self, bmc_attr, val): + if int(bmc_attr['raw']) == 1: value = self.raw_ipmi_set_request(bmc_attr, val) - else: + else: value = self.non_raw_ipmi_set_request(bmc_attr, val) - return (value) + return (value) # bmc-based attr: return attr obj # non-bmc-based attr: return empty obj @@ -1821,43 +1815,43 @@ def check_bmc_based_attr(self, device_name, attr_name): return attr # Required attr_name is not supported in BMC object return {} - return None + return None def get_attr_name_output(self, device_name, attr_name): - bmc_attr = self.check_bmc_based_attr(device_name, attr_name) - output={"mode":"", "status":""} - + bmc_attr = self.check_bmc_based_attr(device_name, attr_name) + output = {"mode": "", "status": ""} + if bmc_attr is not None: - if bmc_attr=={}: - return {} - output['mode']="bmc" - output['status']=self.bmc_get_cmd(bmc_attr) + if bmc_attr == {}: + return {} + output['mode'] = "bmc" + output['status'] = self.bmc_get_cmd(bmc_attr) else: - output['mode']="i2c" - node = self.get_path(device_name, attr_name) - if node is None: - return {} - try: - with open(node, 'r') as f: - output['status'] = f.read() - except IOError: - return {} + output['mode'] = "i2c" + node = self.get_path(device_name, attr_name) + if node is None: + return {} + try: + with open(node, 'r') as f: + output['status'] = f.read() + except IOError: + return {} return output def set_attr_name_output(self, device_name, attr_name, val): - bmc_attr = self.check_bmc_based_attr(device_name, attr_name) - output={"mode":"", "status":""} - + bmc_attr = self.check_bmc_based_attr(device_name, attr_name) + output = {"mode": "", "status": ""} + if bmc_attr is not None: - if bmc_attr=={}: - return {} - output['mode']="bmc" - output['status']=False # No set operation allowed for BMC attributes as they are handled by BMC itself + if bmc_attr == {}: + return {} + output['mode'] = "bmc" + output['status'] = False # No set operation allowed for BMC attributes as they are handled by BMC itself else: - output['mode']="i2c" + output['mode'] = "i2c" node = self.get_path(device_name, attr_name) if node is None: - return {} + return {} try: with open(node, 'w') as f: f.write(str(val)) @@ -1886,7 +1880,7 @@ def main(): try: pddf_obj = PddfParse() except Exception as e: - print "%s" % str(e) + print("%s" % str(e)) sys.exit() if args.create: @@ -1894,39 +1888,40 @@ def main(): if args.sysfs: if args.sysfs[0] == 'all': - pddf_obj.populate_pddf_sysfsobj() + pddf_obj.populate_pddf_sysfsobj() if args.sysfs[0] == 'print': - pddf_obj.populate_pddf_sysfsobj() - pddf_obj.dump_sysfs_obj(pddf_obj.sysfs_obj, args.sysfs[1]) + pddf_obj.populate_pddf_sysfsobj() + pddf_obj.dump_sysfs_obj(pddf_obj.sysfs_obj, args.sysfs[1]) if args.sysfs[0] == 'validate': - pddf_obj.populate_pddf_sysfsobj() - pddf_obj.validate_sysfs_creation(pddf_obj.sysfs_obj, args.sysfs[1]) + pddf_obj.populate_pddf_sysfsobj() + pddf_obj.validate_sysfs_creation(pddf_obj.sysfs_obj, args.sysfs[1]) if args.sysfs[0] == 'verify': - pddf_obj.verify_sysfs_data(args.sysfs[1]) - + pddf_obj.verify_sysfs_data(args.sysfs[1]) if args.dsysfs: - if args.dsysfs[0] == 'validate': - pddf_obj.dev_parse(pddf_obj.data['SYSTEM'], { "cmd": "show_attr", "target":"all", "attr":"all" } ) + if args.dsysfs[0] == 'validate': + pddf_obj.dev_parse(pddf_obj.data['SYSTEM'], {"cmd": "show_attr", "target": "all", "attr": "all"}) if 'SYSSTATUS' in pddf_obj.data: - pddf_obj.dev_parse(pddf_obj.data['SYSSTATUS'], { "cmd": "show_attr", "target":"all", "attr":"all" } ) + pddf_obj.dev_parse(pddf_obj.data['SYSSTATUS'], {"cmd": "show_attr", "target": "all", "attr": "all"}) pddf_obj.validate_dsysfs_creation(pddf_obj.data_sysfs_obj, args.dsysfs[1]) elif args.dsysfs[0] == 'print': - pddf_obj.dev_parse(pddf_obj.data['SYSTEM'], { "cmd": "show_attr", "target":"all", "attr":"all" } ) + pddf_obj.dev_parse(pddf_obj.data['SYSTEM'], {"cmd": "show_attr", "target": "all", "attr": "all"}) if 'SYSSTATUS' in pddf_obj.data: - pddf_obj.dev_parse(pddf_obj.data['SYSSTATUS'], { "cmd": "show_attr", "target":"all", "attr":"all" } ) + pddf_obj.dev_parse(pddf_obj.data['SYSSTATUS'], {"cmd": "show_attr", "target": "all", "attr": "all"}) pddf_obj.dump_sysfs_obj(pddf_obj.data_sysfs_obj, args.dsysfs[1]) elif args.dsysfs[0] == 'all': - ret = pddf_obj.dev_parse(pddf_obj.data['SYSTEM'], { "cmd": "show_attr", "target":"all", "attr":"all" } ) + ret = pddf_obj.dev_parse(pddf_obj.data['SYSTEM'], {"cmd": "show_attr", "target": "all", "attr": "all"}) if 'SYSSTATUS' in pddf_obj.data: - ret += pddf_obj.dev_parse(pddf_obj.data['SYSSTATUS'], { "cmd": "show_attr", "target":"all", - "attr":"all" } ) + ret += pddf_obj.dev_parse(pddf_obj.data['SYSSTATUS'], {"cmd": "show_attr", "target": "all", + "attr": "all"}) pddf_obj.dump_sysfs_obj(pddf_obj.data_sysfs_obj, 'all') else: - pddf_obj.dev_parse(pddf_obj.data[args.dsysfs[0]], { "cmd": "show_attr", "target":args.dsysfs[0], - "attr":args.dsysfs[1] }) + val = pddf_obj.dev_parse(pddf_obj.data[args.dsysfs[0]], {"cmd": "show_attr", "target": args.dsysfs[0], + "attr": args.dsysfs[1]}) + for i in val: + print(i) if args.delete: pddf_obj.delete_pddf_devices() @@ -1944,6 +1939,5 @@ def main(): pddf_obj.modules_validation(args.modules[0]) - -if __name__ == "__main__" : - main() +if __name__ == "__main__": + main() diff --git a/platform/pddf/platform-api-pddf-base.dep b/platform/pddf/platform-api-pddf-base.dep index aaaedb068384..3c1f0d874814 100644 --- a/platform/pddf/platform-api-pddf-base.dep +++ b/platform/pddf/platform-api-pddf-base.dep @@ -1,5 +1,5 @@ -MPATH := $($(PDDF_PLATFORM_API_BASE_PY2)_SRC_PATH) +MPATH := $($(PDDF_PLATFORM_API_BASE_PY3)_SRC_PATH) DEP_FILES := $(SONIC_COMMON_FILES_LIST) platform/pddf/platform-api-pddf-base.mk platform/pddf/platform-api-pddf-base.dep DEP_FILES += $(SONIC_COMMON_BASE_FILES_LIST) DEP_FILES += $(shell git ls-files $(MPATH)) diff --git a/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_chassis.py b/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_chassis.py old mode 100755 new mode 100644 index 9d0d963347ba..0ba2e8902c9c --- a/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_chassis.py +++ b/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_chassis.py @@ -32,9 +32,9 @@ def __init__(self, pddf_data=None, pddf_plugin_data=None): self.plugin_data = pddf_plugin_data if pddf_plugin_data else None if not self.pddf_obj or not self.plugin_data: try: - from . import pddfparse + from . import pddfapi import json - self.pddf_obj = pddfparse.PddfParse() + self.pddf_obj = pddfapi.PddfApi() with open('/usr/share/sonic/platform/pddf/pd-plugin.json') as pd: self.plugin_data = json.load(pd) except Exception as e: @@ -43,7 +43,12 @@ def __init__(self, pddf_data=None, pddf_plugin_data=None): self.platform_inventory = self.pddf_obj.get_platform() # Initialize EEPROM - self.sys_eeprom = Eeprom(self.pddf_obj, self.plugin_data) + try: + self._eeprom = Eeprom(self.pddf_obj, self.plugin_data) + except Exception as err: + sys.stderr.write("Unable to initialize syseeprom - {}".format(repr(err))) + # Dont exit as we dont want failure in loading other components + # FANs for i in range(self.platform_inventory['num_fantrays']): @@ -66,28 +71,6 @@ def __init__(self, pddf_data=None, pddf_plugin_data=None): thermal = Thermal(i, self.pddf_obj, self.plugin_data) self._thermal_list.append(thermal) - # SYSTEM LED Test Cases - """ - #comment out test cases - sys_led_list= { "LOC":0, - "DIAG":0, - "FAN":0, - "SYS":0, - "PSU1":0, - "PSU2":1 - } - - for led in sys_led_list: - color=self.get_system_led(led, sys_led_list[led]) - print color - - self.set_system_led("LOC_LED","STATUS_LED_COLOR_GREEN") - color=self.get_system_led("LOC_LED") - print "Set Green: " + color - self.set_system_led("LOC_LED", "STATUS_LED_COLOR_OFF") - color=self.get_system_led("LOC_LED") - print "Set off: " + color - """ def get_name(self): """ @@ -95,7 +78,7 @@ def get_name(self): Returns: string: The name of the chassis """ - return self.sys_eeprom.modelstr() + return self._eeprom.modelstr() def get_presence(self): """ @@ -111,7 +94,7 @@ def get_model(self): Returns: string: Model/part number of chassis """ - return self.sys_eeprom.part_number_str() + return self._eeprom.part_number_str() def get_serial(self): """ @@ -119,7 +102,7 @@ def get_serial(self): Returns: string: Serial number of chassis """ - return self.sys_eeprom.serial_str() + return self._eeprom.serial_str() def get_status(self): """ @@ -138,7 +121,7 @@ def get_base_mac(self): A string containing the MAC address in the format 'XX:XX:XX:XX:XX:XX' """ - return self.sys_eeprom.base_mac_addr() + return self._eeprom.base_mac_addr() def get_serial_number(self): """ @@ -148,7 +131,7 @@ def get_serial_number(self): A string containing the hardware serial number for this chassis. """ - return self.sys_eeprom.serial_number_str() + return self._eeprom.serial_number_str() def get_system_eeprom_info(self): """ @@ -158,7 +141,7 @@ def get_system_eeprom_info(self): OCP ONIE TlvInfo EEPROM format and values are their corresponding values. """ - return self.sys_eeprom.system_eeprom_info() + return self._eeprom.system_eeprom_info() def get_reboot_cause(self): """ @@ -173,263 +156,39 @@ def get_reboot_cause(self): """ raise NotImplementedError - def get_component_name_list(self): - """ - Retrieves a list of the names of components available on the chassis (e.g., BIOS, CPLD, FPGA, etc.) - - Returns: - A list containing the names of components available on the chassis - """ - return self._component_name_list - - def get_firmware_version(self, component_name): - """ - Retrieves platform-specific hardware/firmware versions for chassis - componenets such as BIOS, CPLD, FPGA, etc. - Args: - component_name: A string, the component name. - - Returns: - A string containing platform-specific component versions - """ - raise NotImplementedError - - def install_component_firmware(self, component_name, image_path): - """ - Install firmware to component - Args: - component_name: A string, the component name. - image_path: A string, path to firmware image. - - Returns: - A boolean, True if install was successful, False if not - """ - raise NotImplementedError + ############################################## + # Component methods + ############################################## ############################################## # Module methods ############################################## + # All module methods are part of chassis_base.py + # if they need to be overwritten, define them here - def get_num_modules(self): - """ - Retrieves the number of modules available on this chassis - - Returns: - An integer, the number of modules available on this chassis - """ - return len(self._module_list) - - def get_all_modules(self): - """ - Retrieves all modules available on this chassis - - Returns: - A list of objects derived from ModuleBase representing all - modules available on this chassis - """ - return self._module_list - - def get_module(self, index): - """ - Retrieves module represented by (0-based) index - - Args: - index: An integer, the index (0-based) of the module to - retrieve - - Returns: - An object dervied from ModuleBase representing the specified - module - """ - module = None - - try: - module = self._module_list[index] - except IndexError: - sys.stderr.write("Module index {} out of range (0-{})\n".format( - index, len(self._module_list)-1)) - - return module ############################################## # Fan methods ############################################## - - def get_num_fans(self): - """ - Retrieves the number of fans available on this chassis - - Returns: - An integer, the number of fan modules available on this chassis - """ - return len(self._fan_list) - - def get_all_fans(self): - """ - Retrieves all fan modules available on this chassis - - Returns: - A list of objects derived from FanBase representing all fan - modules available on this chassis - """ - return self._fan_list - - def get_fan(self, index): - """ - Retrieves fan module represented by (0-based) index - - Args: - index: An integer, the index (0-based) of the fan module to - retrieve - - Returns: - An object dervied from FanBase representing the specified fan - module - """ - fan = None - - try: - fan = self._fan_list[index] - except IndexError: - sys.stderr.write("Fan index {} out of range (0-{})\n".format( - index, len(self._fan_list)-1)) - - return fan + # All fan methods are part of chassis_base.py + # if they need to be overwritten, define them here ############################################## # PSU methods ############################################## - - def get_num_psus(self): - """ - Retrieves the number of power supply units available on this chassis - - Returns: - An integer, the number of power supply units available on this - chassis - """ - return len(self._psu_list) - - def get_all_psus(self): - """ - Retrieves all power supply units available on this chassis - - Returns: - A list of objects derived from PsuBase representing all power - supply units available on this chassis - """ - return self._psu_list - - def get_psu(self, index): - """ - Retrieves power supply unit represented by (0-based) index - - Args: - index: An integer, the index (0-based) of the power supply unit to - retrieve - - Returns: - An object dervied from PsuBase representing the specified power - supply unit - """ - psu = None - - try: - psu = self._psu_list[index] - except IndexError: - sys.stderr.write("PSU index {} out of range (0-{})\n".format( - index, len(self._psu_list)-1)) - - return psu + # All psu methods are part of chassis_base.py + # if they need to be overwritten, define them here ############################################## # THERMAL methods ############################################## - - def get_num_thermals(self): - """ - Retrieves the number of thermals available on this chassis - - Returns: - An integer, the number of thermals available on this chassis - """ - return len(self._thermal_list) - - def get_all_thermals(self): - """ - Retrieves all thermals available on this chassis - - Returns: - A list of objects derived from ThermalBase representing all thermals - available on this chassis - """ - return self._thermal_list - - def get_thermal(self, index): - """ - Retrieves thermal unit represented by (0-based) index - - Args: - index: An integer, the index (0-based) of the thermal to - retrieve - - Returns: - An object dervied from ThermalBase representing the specified thermal - """ - thermal = None - - try: - thermal = self._thermal_list[index] - except IndexError: - sys.stderr.write("THERMAL index {} out of range (0-{})\n".format( - index, len(self._thermal_list)-1)) - - return thermal + # All thermal methods are part of chassis_base.py + # if they need to be overwritten, define them here ############################################## # SFP methods ############################################## - - def get_num_sfps(self): - """ - Retrieves the number of sfps available on this chassis - - Returns: - An integer, the number of sfps available on this chassis - """ - return len(self._sfp_list) - - def get_all_sfps(self): - """ - Retrieves all sfps available on this chassis - - Returns: - A list of objects derived from SfpBase representing all sfps - available on this chassis - """ - return self._sfp_list - - def get_sfp(self, index): - """ - Retrieves sfp represented by (0-based) index - - Args: - index: An integer, the index (0-based) of the sfp to retrieve. - The index should be the sequence of a physical port in a chassis, - starting from 0. - For example, 0 for Ethernet0, 1 for Ethernet4 and so on. - - Returns: - An object dervied from SfpBase representing the specified sfp - """ - sfp = None - - try: - sfp = self._sfp_list[index] - except IndexError: - sys.stderr.write("SFP index {} out of range (0-{})\n".format( - index, len(self._sfp_list)-1)) - - return sfp + # All sfp methods are part of chassis_base.py + # if they need to be overwritten, define them here ############################################## # System LED methods @@ -464,47 +223,3 @@ def get_system_led(self, led_device_name): ############################################## # Other methods ############################################## - - def get_watchdog(self): - """ - Retreives hardware watchdog device on this chassis - - Returns: - An object derived from WatchdogBase representing the hardware - watchdog device - """ - return self._watchdog - - def get_eeprom(self): - """ - Retreives eeprom device on this chassis - - Returns: - An object derived from WatchdogBase representing the hardware - eeprom device - """ - return self.sys_eeprom - - def get_change_event(self, timeout=0): - """ - Returns a nested dictionary containing all devices which have - experienced a change at chassis level - - Args: - timeout: Timeout in milliseconds (optional). If timeout == 0, - this method will block until a change is detected. - Returns: - (bool, dict): - - True if call successful, False if not; - - A nested dictionary where key is a device type, - value is a dictionary with key:value pairs in the format of - {'device_id':'device_event'}, - where device_id is the device ID for this device and - device_event, - status='1' represents device inserted, - status='0' represents device removed. - Ex. {'fan':{'0':'0', '2':'1'}, 'sfp':{'11':'0'}} - indicates that fan 0 has been removed, fan 2 - has been inserted and sfp 11 has been removed. - """ - raise NotImplementedError diff --git a/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_fan.py b/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_fan.py old mode 100755 new mode 100644 diff --git a/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_platform.py b/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_platform.py old mode 100755 new mode 100644 index c9c3a8d851ac..b7adecc312d5 --- a/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_platform.py +++ b/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_platform.py @@ -8,7 +8,7 @@ try: import json - from . import pddfparse + from . import pddfapi from sonic_platform_base.platform_base import PlatformBase from sonic_platform.chassis import Chassis except ImportError as e: @@ -24,7 +24,7 @@ class PddfPlatform(PlatformBase): def __init__(self): # Initialize the JSON data - self.pddf_data = pddfparse.PddfParse() + self.pddf_data = pddfapi.PddfApi() with open('/usr/share/sonic/platform/pddf/pd-plugin.json') as pd: self.pddf_plugin_data = json.load(pd) diff --git a/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_psu.py b/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_psu.py old mode 100755 new mode 100644 diff --git a/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_sfp.py b/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_sfp.py old mode 100755 new mode 100644 diff --git a/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_thermal.py b/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddf_thermal.py old mode 100755 new mode 100644 diff --git a/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddfparse.py b/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddfapi.py old mode 100755 new mode 100644 similarity index 53% rename from platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddfparse.py rename to platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddfapi.py index 6ca59206fd56..dea0258e72f5 --- a/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddfparse.py +++ b/platform/pddf/platform-api-pddf-base/sonic_platform_pddf_base/pddfapi.py @@ -1,7 +1,6 @@ #!/usr/bin/env python import glob import json -from jsonschema import validate import os import re import subprocess @@ -29,7 +28,7 @@ } -class PddfParse(): +class PddfApi(): def __init__(self): if not os.path.exists("/usr/share/sonic/platform"): self.platform, self.hwsku = self.get_platform_and_hwsku() @@ -46,7 +45,6 @@ def __init__(self): self.sysfs_obj = {} # Returns platform and HW SKU - def get_platform_and_hwsku(self): try: proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY], @@ -84,30 +82,38 @@ def get_dev_idx(self, dev, ops): return pdev['dev_attr']['dev_idx'] - def get_path(self, target, attr): + def get_paths(self, target, attr): aa = target + attr if aa in cache: return cache[aa] - string = None + strings = [] p = re.search(r'\d+$', target) if p is None: for bb in filter(re.compile(target).search, self.data.keys()): - path = self.dev_parse(self.data[bb], {"cmd": "show_attr", "target": bb, "attr": attr}) - if path != "": - string = path + paths = self.dev_parse(self.data[bb], {"cmd": "show_attr", "target": bb, "attr": attr}) + if paths: + strings.extend(paths) else: if target in self.data.keys(): - path = self.dev_parse(self.data[target], {"cmd": "show_attr", "target": target, "attr": attr}) - if path != "": - string = path + paths = self.dev_parse(self.data[target], {"cmd": "show_attr", "target": target, "attr": attr}) + if paths: + strings.extend(paths) - if string is not None: - string = string.rstrip() + cache[aa] = strings + return strings - cache[aa] = string - return string + def get_path(self, target, attr): + nodes = self.get_paths(target, attr) + if nodes: + if len(nodes)==1: + return nodes[0] + # CAREFULL!!! If more than one paths are expected, use get_paths + else: + return nodes[0] + else: + return None def get_device_type(self, key): if key not in self.data.keys(): @@ -173,7 +179,7 @@ def get_led_color_from_gpio(self, led_device_name): status = "[FAILED] {} does not exist".format(attr_path) return (status) cmd = 'cat ' + attr_path - gpio_value = subprocess.check_output(cmd, shell=True) + gpio_value = subprocess.check_output(cmd, shell=True, universal_newlines=True) value |= int(gpio_value) << bit bit += 1 @@ -205,8 +211,8 @@ def set_led_color_from_gpio(self, led_device_name, color): i = 0 while i <= max_bit: _value = (value >> i) & 1 - base_offset += i - attr_path = self.get_gpio_attr_path(self.data[attr['attr_devname']], hex(base_offset)) + offset = base_offset + i + attr_path = self.get_gpio_attr_path(self.data[attr['attr_devname']], hex(offset)) i += 1 try: cmd = "echo {} > {}".format(_value, attr_path) @@ -230,11 +236,11 @@ def get_system_led_color(self, led_device_name): status = "[FAILED] " + led_device_name + " is not configured" return (status) - type = self.get_led_color_devtype(led_device_name) + dtype = self.get_led_color_devtype(led_device_name) - if type == 'gpio': + if dtype == 'gpio': color = self.get_led_color_from_gpio(led_device_name) - elif type == 'cpld': + elif dtype == 'cpld': color = self.get_led_color_from_cpld(led_device_name) return color @@ -244,9 +250,9 @@ def set_system_led_color(self, led_device_name, color): print(msg) return (result) - type = self.get_led_color_devtype(led_device_name) + dtype = self.get_led_color_devtype(led_device_name) - if type == 'gpio': + if dtype == 'gpio': return (self.set_led_color_from_gpio(led_device_name, color)) else: return (self.set_led_color_from_cpld(led_device_name, color)) @@ -266,13 +272,22 @@ def show_device_sysfs(self, dev, ops): parent = dev['dev_info']['device_parent'] pdev = self.data[parent] if pdev['dev_info']['device_parent'] == 'SYSTEM': - return "/sys/bus/i2c/devices/"+"i2c-%d" % int(pdev['i2c']['topo_info']['dev_addr'], 0) + if 'topo_info' in pdev['i2c']: + return "/sys/bus/i2c/devices/"+"i2c-%d"%int(pdev['i2c']['topo_info']['dev_addr'], 0) + else: + return "/sys/bus/i2c/devices" return self.show_device_sysfs(pdev, ops) + "/" + "i2c-%d" % int(dev['i2c']['topo_info']['parent_bus'], 0) - # This is alid for 'at24' type of EEPROM devices. Only one attribtue 'eeprom' + def get_gpio_attr_path(self, dev, offset): + base = int(dev['i2c']['dev_attr']['gpio_base'], 16) + port_num = base + int(offset, 16) + gpio_name = 'gpio'+str(port_num) + path = '/sys/class/gpio/'+gpio_name+'/value' + return path + # This is alid for 'at24' type of EEPROM devices. Only one attribtue 'eeprom' def show_attr_eeprom_device(self, dev, ops): - str = "" + ret = [] attr_name = ops['attr'] attr_list = dev['i2c']['attr_list'] KEY = "eeprom" @@ -294,11 +309,11 @@ def show_attr_eeprom_device(self, dev, ops): "/%s" % real_name if dsysfs_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(dsysfs_path) - str += dsysfs_path+"\n" - return str + ret.append(dsysfs_path) + return ret def show_attr_gpio_device(self, dev, ops): - ret = "" + ret = [] KEY = "gpio" if KEY not in self.data_sysfs_obj: self.data_sysfs_obj[KEY] = [] @@ -306,24 +321,17 @@ def show_attr_gpio_device(self, dev, ops): return ret def show_attr_mux_device(self, dev, ops): - ret = "" + ret = [] KEY = "mux" if KEY not in self.data_sysfs_obj: self.data_sysfs_obj[KEY] = [] return ret - def get_gpio_attr_path(self, dev, offset): - base = int(dev['i2c']['dev_attr']['gpio_base'], 16) - port_num = base + int(offset, 16) - gpio_name = 'gpio'+str(port_num) - path = '/sys/class/gpio/'+gpio_name+'/value' - return path - def show_attr_psu_i2c_device(self, dev, ops): target = ops['target'] attr_name = ops['attr'] - ret = "" + ret = [] KEY = "psu" dsysfs_path = "" @@ -331,7 +339,7 @@ def show_attr_psu_i2c_device(self, dev, ops): self.data_sysfs_obj[KEY] = [] if target == 'all' or target == dev['dev_info']['virt_parent']: - attr_list = dev['i2c']['attr_list'] + attr_list = dev['i2c']['attr_list'] if 'i2c' in dev else [] for attr in attr_list: if attr_name == attr['attr_name'] or attr_name == 'all': if 'attr_devtype' in attr.keys() and attr['attr_devtype'] == "gpio": @@ -340,29 +348,35 @@ def show_attr_psu_i2c_device(self, dev, ops): if (os.path.exists(attr_path)): if attr_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(attr_path) - ret += attr_path + '\n' + ret.append(attr_path) else: if 'drv_attr_name' in attr.keys(): real_name = attr['drv_attr_name'] + real_dev = dev + elif 'attr_devattr' in attr.keys(): + real_name = attr['attr_devattr'] + real_devname = attr['attr_devname'] if 'attr_devname' in attr.keys() else '' + real_dev = self.data[real_devname] else: real_name = attr['attr_name'] + real_dev = dev - dsysfs_path = self.show_device_sysfs(dev, ops) + \ - "/%d-00%x" % (int(dev['i2c']['topo_info']['parent_bus'], 0), - int(dev['i2c']['topo_info']['dev_addr'], 0)) + \ + dsysfs_path = self.show_device_sysfs(real_dev, ops) + \ + "/%d-00%x" % (int(real_dev['i2c']['topo_info']['parent_bus'], 0), + int(real_dev['i2c']['topo_info']['dev_addr'], 0)) + \ "/%s" % real_name if dsysfs_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(dsysfs_path) - ret += dsysfs_path+"\n" + ret.append(dsysfs_path) return ret def show_attr_psu_device(self, dev, ops): return self.show_attr_psu_i2c_device(dev, ops) def show_attr_fan_device(self, dev, ops): - ret = "" + ret = [] attr_name = ops['attr'] - attr_list = dev['i2c']['attr_list'] + attr_list = dev['i2c']['attr_list'] if 'i2c' in dev else [] KEY = "fan" dsysfs_path = "" @@ -377,27 +391,35 @@ def show_attr_fan_device(self, dev, ops): if (os.path.exists(attr_path)): if attr_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(attr_path) - ret += attr_path + '\n' + ret.append(attr_path) else: if 'drv_attr_name' in attr.keys(): real_name = attr['drv_attr_name'] + real_dev = dev + elif 'attr_devattr' in attr.keys(): + real_name = attr['attr_devattr'] + real_devname = attr['attr_devname'] if 'attr_devname' in attr.keys() else '' + real_dev = self.data[real_devname] else: real_name = attr['attr_name'] + real_dev = dev - dsysfs_path = self.show_device_sysfs(dev, ops) + \ - "/%d-00%x" % (int(dev['i2c']['topo_info']['parent_bus'], 0), - int(dev['i2c']['topo_info']['dev_addr'], 0)) + \ + dsysfs_path = self.show_device_sysfs(real_dev, ops) + \ + "/%d-00%x" % (int(real_dev['i2c']['topo_info']['parent_bus'], 0), + int(real_dev['i2c']['topo_info']['dev_addr'], 0)) + \ "/%s" % real_name if dsysfs_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(dsysfs_path) - ret += dsysfs_path+"\n" + ret.append(dsysfs_path) return ret # This is only valid for LM75 def show_attr_temp_sensor_device(self, dev, ops): - str = "" + ret = [] + if 'i2c' not in dev.keys(): + return ret attr_name = ops['attr'] - attr_list = dev['i2c']['attr_list'] + attr_list = dev['i2c']['attr_list'] if 'i2c' in dev else [] KEY = "temp-sensors" dsysfs_path = "" @@ -418,11 +440,11 @@ def show_attr_temp_sensor_device(self, dev, ops): dsysfs_path = full_path if dsysfs_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(dsysfs_path) - str += full_path + "\n" - return str + ret.append(full_path) + return ret def show_attr_sysstatus_device(self, dev, ops): - ret = "" + ret = [] attr_name = ops['attr'] attr_list = dev['attr_list'] KEY = "sys-status" @@ -436,13 +458,13 @@ def show_attr_sysstatus_device(self, dev, ops): dsysfs_path = "/sys/kernel/pddf/devices/sysstatus/sysstatus_data/" + attr['attr_name'] if dsysfs_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(dsysfs_path) - ret += dsysfs_path+"\n" + ret.append(dsysfs_path) return ret def show_attr_xcvr_i2c_device(self, dev, ops): target = ops['target'] attr_name = ops['attr'] - ret = "" + ret = [] dsysfs_path = "" KEY = "xcvr" if KEY not in self.data_sysfs_obj: @@ -458,342 +480,39 @@ def show_attr_xcvr_i2c_device(self, dev, ops): if (os.path.exists(attr_path)): if attr_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(attr_path) - ret += attr_path + '\n' + ret.append(attr_path) else: if 'drv_attr_name' in attr.keys(): real_name = attr['drv_attr_name'] + real_dev = dev + elif 'attr_devattr' in attr.keys(): + real_name = attr['attr_devattr'] + real_devname = attr['attr_devname'] if 'attr_devname' in attr.keys() else '' + real_dev = self.data[real_devname] else: real_name = attr['attr_name'] + real_dev = dev - dsysfs_path = self.show_device_sysfs(dev, ops) + \ - "/%d-00%x" % (int(dev['i2c']['topo_info']['parent_bus'], 0), - int(dev['i2c']['topo_info']['dev_addr'], 0)) + \ + dsysfs_path = self.show_device_sysfs(real_dev, ops) + \ + "/%d-00%x" % (int(real_dev['i2c']['topo_info']['parent_bus'], 0), + int(real_dev['i2c']['topo_info']['dev_addr'], 0)) + \ "/%s" % real_name if dsysfs_path not in self.data_sysfs_obj[KEY]: self.data_sysfs_obj[KEY].append(dsysfs_path) - ret += dsysfs_path+"\n" + ret.append(dsysfs_path) return ret def show_attr_xcvr_device(self, dev, ops): return self.show_attr_xcvr_i2c_device(dev, ops) def show_attr_cpld_device(self, dev, ops): - ret = "" + ret = [] KEY = "cpld" if KEY not in self.data_sysfs_obj: self.data_sysfs_obj[KEY] = [] return ret - ################################################################################################################### - # SHOW DEFS - ################################################################################################################### - - def check_led_cmds(self, key, ops): - name = ops['target']+'_LED' - if (ops['target'] == 'config' or ops['attr'] == 'all') or \ - (name == self.data[key]['dev_info']['device_name'] and - ops['attr'] == self.data[key]['dev_attr']['index']): - return (True) - else: - return (False) - - def dump_sysfs_obj(self, obj, key_type): - if (key_type == 'keys'): - for key in obj.keys(): - print(key) - return - - for key in obj: - if (key == key_type or key_type == 'all'): - print(key+":") - for entry in obj[key]: - print("\t"+entry) - - def add_list_sysfs_obj(self, obj, KEY, list): - for sysfs in list: - if sysfs not in obj[KEY]: - obj[KEY].append(sysfs) - - def sysfs_attr(self, key, value, path, obj, obj_key): - sysfs_path = "/sys/kernel/%s/%s" % (path, key) - if sysfs_path not in obj[obj_key]: - obj[obj_key].append(sysfs_path) - - def sysfs_device(self, attr, path, obj, obj_key): - for key in attr.keys(): - sysfs_path = "/sys/kernel/%s/%s" % (path, key) - if sysfs_path not in obj[obj_key]: - obj[obj_key].append(sysfs_path) - - def show_eeprom_device(self, dev, ops): - return - - def show_mux_device(self, dev, ops): - KEY = 'mux' - if KEY not in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - self.sysfs_device(dev['i2c']['topo_info'], "pddf/devices/mux", self.sysfs_obj, KEY) - self.sysfs_device(dev['i2c']['dev_attr'], "pddf/devices/mux", self.sysfs_obj, KEY) - sysfs_path = "/sys/kernel/pddf/devices/mux/dev_ops" - if sysfs_path not in self.sysfs_obj[KEY]: - self.sysfs_obj[KEY].append(sysfs_path) - list = ['/sys/kernel/pddf/devices/mux/i2c_type', - '/sys/kernel/pddf/devices/mux/i2c_name', - '/sys/kernel/pddf/devices/mux/error'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) - - def show_gpio_device(self, dev, ops): - KEY = 'gpio' - if KEY not in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - self.sysfs_device(dev['i2c']['topo_info'], "pddf/devices/gpio", self.sysfs_obj, KEY) - self.sysfs_device(dev['i2c']['dev_attr'], "pddf/devices/gpio", self.sysfs_obj, KEY) - sysfs_path = "/sys/kernel/pddf/devices/gpio/dev_ops" - if sysfs_path not in self.sysfs_obj[KEY]: - self.sysfs_obj[KEY].append(sysfs_path) - list = ['/sys/kernel/pddf/devices/gpio/i2c_type', - '/sys/kernel/pddf/devices/gpio/i2c_name', - '/sys/kernel/pddf/devices/gpio/error'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) - - def show_psu_i2c_device(self, dev, ops): - KEY = 'psu' - path = 'pddf/devices/psu/i2c' - if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['PSU']: - if KEY not in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - self.sysfs_device(dev['i2c']['topo_info'], path, self.sysfs_obj, KEY) - sysfs_path = "/sys/kernel/pddf/devices/psu/i2c/psu_idx" - self.sysfs_obj[KEY].append(sysfs_path) - - for attr in dev['i2c']['attr_list']: - self.sysfs_device(attr, "pddf/devices/psu/i2c", self.sysfs_obj, KEY) - sysfs_path = "/sys/kernel/pddf/devices/psu/i2c/dev_ops" - if sysfs_path not in self.sysfs_obj[KEY]: - self.sysfs_obj[KEY].append(sysfs_path) - list = ['/sys/kernel/pddf/devices/psu/i2c/i2c_type', - '/sys/kernel/pddf/devices/fan/i2c/i2c_name', - '/sys/kernel/pddf/devices/psu/i2c/error', - '/sys/kernel/pddf/devices/psu/i2c/attr_ops'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) - - def show_psu_device(self, dev, ops): - self.show_psu_i2c_device(dev, ops) - return - - def show_client_device(self): - KEY = 'client' - if KEY not in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - list = ['/sys/kernel/pddf/devices/showall'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) - - def show_fan_device(self, dev, ops): - KEY = 'fan' - path = 'pddf/devices/fan/i2c' - if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['FAN']: - if KEY not in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - - self.sysfs_device(dev['i2c']['topo_info'], path, self.sysfs_obj, KEY) - self.sysfs_device(dev['i2c']['dev_attr'], path, self.sysfs_obj, KEY) - for attr in dev['i2c']['attr_list']: - self.sysfs_device(attr, path, self.sysfs_obj, KEY) - list = ['/sys/kernel/pddf/devices/fan/i2c/i2c_type', - '/sys/kernel/pddf/devices/fan/i2c/i2c_name', - '/sys/kernel/pddf/devices/fan/i2c/error', - '/sys/kernel/pddf/devices/fan/i2c/attr_ops', - '/sys/kernel/pddf/devices/fan/i2c/dev_ops'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) - - def show_temp_sensor_device(self, dev, ops): - return - - def show_sysstatus_device(self, dev, ops): - KEY = 'sysstatus' - if KEY not in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - for attr in dev['attr_list']: - self.sysfs_device(attr, "pddf/devices/sysstatus", self.sysfs_obj, KEY) - sysfs_path = "/sys/kernel/pddf/devices/sysstatus/attr_ops" - if sysfs_path not in self.sysfs_obj[KEY]: - self.sysfs_obj[KEY].append(sysfs_path) - - def show_xcvr_i2c_device(self, dev, ops): - KEY = 'xcvr' - if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['PORT_MODULE']: - if KEY not in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - self.sysfs_device(dev['i2c']['topo_info'], "pddf/devices/xcvr/i2c", self.sysfs_obj, KEY) - - for attr in dev['i2c']['attr_list']: - self.sysfs_device(attr, "pddf/devices/xcvr/i2c", self.sysfs_obj, KEY) - sysfs_path = "/sys/kernel/pddf/devices/xcvr/i2c/dev_ops" - if sysfs_path not in self.sysfs_obj[KEY]: - self.sysfs_obj[KEY].append(sysfs_path) - list = ['/sys/kernel/pddf/devices/xcvr/i2c/i2c_type', - '/sys/kernel/pddf/devices/xcvr/i2c/i2c_name', - '/sys/kernel/pddf/devices/xcvr/i2c/error', - '/sys/kernel/pddf/devices/xcvr/i2c/attr_ops'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) - - def show_xcvr_device(self, dev, ops): - self.show_xcvr_i2c_device(dev, ops) - return - - def show_cpld_device(self, dev, ops): - KEY = 'cpld' - if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['CPLD']: - if KEY not in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - self.sysfs_device(dev['i2c']['topo_info'], "pddf/devices/cpld", self.sysfs_obj, KEY) - sysfs_path = "/sys/kernel/pddf/devices/cpld/dev_ops" - if sysfs_path not in self.sysfs_obj[KEY]: - self.sysfs_obj[KEY].append(sysfs_path) - list = ['/sys/kernel/pddf/devices/cpld/i2c_type', - '/sys/kernel/pddf/devices/cpld/i2c_name', - '/sys/kernel/pddf/devices/cpld/error'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) - - def show_led_platform_device(self, key, ops): - if ops['attr'] == 'all' or ops['attr'] == 'PLATFORM': - KEY = 'platform' - if KEY not in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - path = 'pddf/devices/platform' - self.sysfs_attr('num_psus', self.data['PLATFORM']['num_psus'], path, self.sysfs_obj, KEY) - self.sysfs_attr('num_fantrays', self.data['PLATFORM']['num_fantrays'], path, self.sysfs_obj, KEY) - - def show_led_device(self, key, ops): - if self.check_led_cmds(key, ops): - KEY = 'led' - if KEY not in self.sysfs_obj: - self.sysfs_obj[KEY] = [] - path = "pddf/devices/led" - for attr in self.data[key]['i2c']['attr_list']: - self.sysfs_attr('device_name', self.data[key]['dev_info']['device_name'], path, self.sysfs_obj, KEY) - self.sysfs_attr('swpld_addr', self.data[key]['dev_info']['device_name'], path, self.sysfs_obj, KEY) - self.sysfs_attr('swpld_addr_offset', self.data[key]['dev_info']['device_name'], path, - self.sysfs_obj, KEY) - self.sysfs_device(self.data[key]['dev_attr'], path, self.sysfs_obj, KEY) - for attr_key in attr.keys(): - attr_path = "pddf/devices/led/" + attr['attr_name'] - if (attr_key != 'attr_name' and attr_key != 'swpld_addr' and attr_key != 'swpld_addr_offset'): - self.sysfs_attr(attr_key, attr[attr_key], attr_path, self.sysfs_obj, KEY) - sysfs_path = "/sys/kernel/pddf/devices/led/dev_ops" - if sysfs_path not in self.sysfs_obj[KEY]: - self.sysfs_obj[KEY].append(sysfs_path) - list = ['/sys/kernel/pddf/devices/led/cur_state/color', - '/sys/kernel/pddf/devices/led/cur_state/color_state'] - self.add_list_sysfs_obj(self.sysfs_obj, KEY, list) - - def validate_xcvr_device(self, dev, ops): - devtype_list = ['optoe1', 'optoe2'] - dev_attribs = ['xcvr_present', 'xcvr_reset', 'xcvr_intr_status', 'xcvr_lpmode'] - ret_val = "xcvr validation failed" - - if dev['i2c']['topo_info']['dev_type'] in devtype_list: - for attr in dev['i2c']['attr_list']: - if 'attr_name' in attr.keys() and 'eeprom' in attr.values(): - ret_val = "xcvr validation success" - else: - print("xcvr validation Failed") - return - - elif dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['PORT_MODULE']: - for attr in dev['i2c']['attr_list']: - if attr.get("attr_name") in dev_attribs: - ret_val = "Success" - else: - print("xcvr validation Failed") - return - print(ret_val) - - def validate_eeprom_device(self, dev, ops): - devtype_list = ['24c02'] - dev_access_mode = ['BLOCK', 'BYTE'] - dev_attribs = ['eeprom'] - ret_val = "eeprom failed" - - if dev['i2c']['topo_info']['dev_type'] in devtype_list: - if dev['i2c']['dev_attr']['access_mode'] in dev_access_mode: - for attr in dev['i2c']['attr_list']: - if attr.get("attr_name") in dev_attribs: - ret_val = "eeprom success" - print(ret_val) - - def validate_mux_device(self, dev, ops): - devtype_list = ['pca9548', 'pca954x'] - dev_channels = ["0", "1", "2", "3", "4", "5", "6", "7"] - ret_val = "mux failed" - - if dev['i2c']['topo_info']['dev_type'] in devtype_list: - for attr in dev['i2c']['channel']: - if attr.get("chn") in dev_channels: - ret_val = "Mux success" - print(ret_val) - - def validate_cpld_device(self, dev, ops): - devtype_list = ['i2c_cpld'] - ret_val = "cpld failed" - - if dev['i2c']['topo_info']['dev_type'] in devtype_list: - ret_val = "cpld success" - print(ret_val) - - def validate_sysstatus_device(self, dev, ops): - dev_attribs = ['board_info', 'cpld1_version', 'power_module_status', 'system_reset5', - 'system_reset6', 'system_reset7', 'misc1', 'cpld2_version', 'cpld3_version' - ] - ret_val = "sysstatus failed" - - if dev['dev_info']['device_type'] == "SYSSTAT": - for attr in dev['attr_list']: - if attr.get("attr_name") in dev_attribs: - ret_val = "sysstatus success" - print(ret_val) - - def validate_temp_sensor_device(self, dev, ops): - devtype_list = ['lm75'] - dev_attribs = ['temp1_max', 'temp1_max_hyst', 'temp1_input'] - ret_val = "temp sensor failed" - - if dev['dev_info']['device_type'] == "TEMP_SENSOR": - if dev['i2c']['topo_info']['dev_type'] in devtype_list: - for attr in dev['i2c']['attr_list']: - if attr.get("attr_name") in dev_attribs: - ret_val = "tempsensor success" - print(ret_val) - - def validate_fan_device(self, dev, ops): - ret_val = "fan failed" - - if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['FAN']: - if dev['i2c']['dev_attr']['num_fan'] is not None: - ret_val = "fan success" - - print(ret_val) - - def validate_psu_device(self, dev, ops): - dev_attribs = ['psu_present', 'psu_model_name', 'psu_power_good', 'psu_mfr_id', 'psu_serial_num', - 'psu_fan_dir', 'psu_v_out', 'psu_i_out', 'psu_p_out', 'psu_fan1_speed_rpm' - ] - ret_val = "psu failed" - - if dev['i2c']['topo_info']['dev_type'] in self.data['PLATFORM']['pddf_dev_types']['PSU']: - for attr in dev['i2c']['attr_list']: - if attr.get("attr_name") in dev_attribs: - if attr.get("attr_devaddr") is not None: - if attr.get("attr_offset") is not None: - if attr.get("attr_mask") is not None: - if attr.get("attr_len") is not None: - ret_val = "psu success" - else: - ret_val = "psu failed" - - print(ret_val) - ################################################################################################################### # SPYTEST ################################################################################################################### @@ -858,63 +577,6 @@ def verify_sysfs_data(self, verify_type): if (entry == 'swpld_addr' or entry == 'swpld_addr_offset'): self.verify_attr(entry, attr, 'pddf/devices/led') - def schema_validation(self, validate_type): - process_validate_type = 0 - for key in self.data.keys(): - if (key != 'PLATFORM'): - temp_obj = {} - schema_list = [] - try: - device_type = self.data[key]["dev_info"]["device_type"] - except Exception as e: - print("dev_info or device_type ERROR: " + key) - print(e) - - if validate_type == 'mismatch': - process_validate_type = 1 - device_type = "PSU" - schema_file = "/usr/local/bin/schema/FAN.schema" - schema_list.append(schema_file) - elif validate_type == 'missing': - process_validate_type = 1 - schema_file = "/usr/local/bin/schema/PLATFORM.schema" - schema_list.append(schema_file) - - elif validate_type == 'empty': - process_validate_type = 1 - if not device_type: - print("Empty device_type for " + key) - continue - elif (validate_type == 'all' or validate_type == device_type): - process_validate_type = 1 - if "bmc" in self.data[key].keys(): - schema_file = "/usr/local/bin/schema/"+device_type + "_BMC.schema" - schema_list.append(schema_file) - - if "i2c" in self.data[key].keys(): - schema_file = "/usr/local/bin/schema/"+device_type + ".schema" - schema_list.append(schema_file) - if device_type: - temp_obj[device_type] = self.data[key] - for schema_file in schema_list: - if (os.path.exists(schema_file)): - print("Validate " + schema_file + ";" + key) - json_data = json.dumps(temp_obj) - with open(schema_file, 'r') as f: - schema = json.load(f) - try: - validate(temp_obj, schema) - except Exception as e: - print("Validation ERROR: " + schema_file + ";" + key) - if validate_type == 'mismatch': - return - else: - print(e) - else: - print("ERROR Missing File: " + schema_file) - if not process_validate_type: - print("device_type: " + validate_type + " not configured") - def modules_validation(self, validate_type): kos = [] supported_type = False @@ -935,7 +597,7 @@ def modules_validation(self, validate_type): supported_type = True cmd = "lsmod | grep " + mod try: - subprocess.check_output(cmd, shell=True) + subprocess.check_output(cmd, shell=True, universal_newlines=True) except Exception as e: module_validation_status.append(mod) if supported_type: @@ -952,229 +614,176 @@ def modules_validation(self, validate_type): ################################################################################################################### def psu_parse(self, dev, ops): - parse_str = "" - ret = "" - for ifce in dev['i2c']['interface']: - ret = getattr(self, ops['cmd']+"_psu_device")(self.data[ifce['dev']], ops) - if ret is not None: - if str(ret).isdigit(): - if ret != 0: + ret = [] + for ifce in (dev['i2c']['interface'] if 'i2c' in dev else []): + val = getattr(self, ops['cmd']+"_psu_device")(self.data[ifce['dev']], ops) + if val: + if str(val[0]).isdigit(): + if val[0] != 0: # in case if 'create' functions - print("{}_psu_device failed".format(ops['cmd'])) - return ret - else: - pass + print("{}_psu_device failed for {}".format(ops['cmd'], ifce['dev'])) + return val else: # in case of 'show_attr' functions - parse_str += ret - return parse_str + ret.extend(val) + return ret def fan_parse(self, dev, ops): - parse_str = "" + ret = [] ret = getattr(self, ops['cmd']+"_fan_device")(dev, ops) - if ret is not None: - if str(ret).isdigit(): - if ret != 0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print("{}_fan_device failed".format(ops['cmd'])) - return ret - else: - pass - else: - # in case of 'show_attr' functions - parse_str += ret + print("{}_fan_device failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) - return parse_str + return ret def temp_sensor_parse(self, dev, ops): - parse_str = "" + ret = [] ret = getattr(self, ops['cmd']+"_temp_sensor_device")(dev, ops) - if ret is not None: - if str(ret).isdigit(): - if ret != 0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print("{}_temp_sensor_device failed".format(ops['cmd'])) - return ret - else: - pass - else: - # in case of 'show_attr' functions - parse_str += ret + print("{}_temp_sensor_device failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) - return parse_str + return ret def cpld_parse(self, dev, ops): - parse_str = "" + ret = [] ret = getattr(self, ops['cmd']+"_cpld_device")(dev, ops) - if ret is not None: - if str(ret).isdigit(): - if ret != 0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print("{}_cpld_device failed".format(ops['cmd'])) - return ret - else: - pass - else: - # in case of 'show_attr' functions - parse_str += ret + print("{}_cpld_device failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) - return parse_str + return ret def sysstatus_parse(self, dev, ops): - parse_str = "" + ret = [] ret = getattr(self, ops['cmd']+"_sysstatus_device")(dev, ops) - if ret is not None: - if str(ret).isdigit(): - if ret != 0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print("{}_sysstatus_device failed".format(ops['cmd'])) - return ret - else: - pass - else: - # in case of 'show_attr' functions - parse_str += ret + print("{}_sysstatus_device failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) - return parse_str + return ret def gpio_parse(self, dev, ops): - parse_str = "" + ret = [] ret = getattr(self, ops['cmd']+"_gpio_device")(dev, ops) - if ret is not None: - if str(ret).isdigit(): - if ret != 0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print("{}_temp_sensor_device failed".format(ops['cmd'])) - return ret - else: - pass - else: - # in case of 'show_attr' functions - parse_str += ret + print("{}_gpio_device failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) - return parse_str + return ret def mux_parse(self, dev, ops): - parse_str = "" + val = [] ret = getattr(self, ops['cmd']+"_mux_device")(dev, ops) - if ret is not None: - if str(ret).isdigit(): - if ret != 0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print("{}_mux_device() cmd failed".format(ops['cmd'])) + print("{}_mux_device() cmd failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) return ret - else: - pass else: - parse_str += ret + val.extend(ret) for ch in dev['i2c']['channel']: ret = self.dev_parse(self.data[ch['dev']], ops) - if ret is not None: - if str(ret).isdigit(): - if ret != 0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions return ret - else: - pass else: - parse_str += ret - return parse_str + val.extend(ret) + return val def mux_parse_reverse(self, dev, ops): - parse_str = "" + val = [] for ch in reversed(dev['i2c']['channel']): ret = self.dev_parse(self.data[ch['dev']], ops) - if ret is not None: - if str(ret).isdigit(): - if ret != 0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions return ret - else: - pass else: - parse_str += ret + val.extend(ret) ret = getattr(self, ops['cmd']+"_mux_device")(dev, ops) - if ret is not None: - if str(ret).isdigit(): - if ret != 0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print("{}_mux_device() cmd failed".format(ops['cmd'])) + print("{}_mux_device() cmd failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) return ret - else: - pass else: - parse_str += ret + val.extend(ret) - return parse_str + return val def eeprom_parse(self, dev, ops): - parse_str = "" + ret = [] ret = getattr(self, ops['cmd']+"_eeprom_device")(dev, ops) - if ret is not None: - if str(ret).isdigit(): - if ret != 0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print("{}_eeprom_device() cmd failed".format(ops['cmd'])) - return ret - else: - pass - else: - parse_str += ret + print("{}_eeprom_device() cmd failed for {}".format(ops['cmd'], dev['dev_info']['device_name'])) - return parse_str + return ret def optic_parse(self, dev, ops): - parse_str = "" - ret = "" + val = [] for ifce in dev['i2c']['interface']: ret = getattr(self, ops['cmd']+"_xcvr_device")(self.data[ifce['dev']], ops) - if ret is not None: - if str(ret).isdigit(): - if ret != 0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions - print("{}_eeprom_device() cmd failed".format(ops['cmd'])) + print("{}_xcvr_device() cmd failed for {}".format(ops['cmd'], ifce['dev'])) return ret - else: - pass else: - parse_str += ret - return parse_str + val.extend(ret) + return val def cpu_parse(self, bus, ops): - parse_str = "" + val = [] for dev in bus['i2c']['CONTROLLERS']: dev1 = self.data[dev['dev']] for d in dev1['i2c']['DEVICES']: ret = self.dev_parse(self.data[d['dev']], ops) - if ret is not None: - if str(ret).isdigit(): - if ret != 0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions return ret - else: - pass else: - parse_str += ret - return parse_str + val.extend(ret) + return val def cpu_parse_reverse(self, bus, ops): - parse_str = "" + val = [] for dev in reversed(bus['i2c']['CONTROLLERS']): dev1 = self.data[dev['dev']] for d in dev1['i2c']['DEVICES']: ret = self.dev_parse(self.data[d['dev']], ops) - if ret is not None: - if str(ret).isdigit(): - if ret != 0: + if ret: + if str(ret[0]).isdigit(): + if ret[0] != 0: # in case if 'create' functions return ret - else: - pass else: - parse_str += ret - return parse_str + val.extend(ret) + return val def dev_parse(self, dev, ops): attr = dev['dev_info'] @@ -1206,6 +815,7 @@ def dev_parse(self, dev, ops): return self.temp_sensor_parse(dev, ops) if attr['device_type'] == 'SFP' or attr['device_type'] == 'QSFP' or \ + attr['device_type'] == 'SFP+' or attr['device_type'] == 'QSFP+' or \ attr['device_type'] == 'SFP28' or attr['device_type'] == 'QSFP28' or \ attr['device_type'] == 'QSFP-DD': return self.optic_parse(dev, ops) @@ -1228,28 +838,6 @@ def create_attr(self, key, value, path): cmd = "echo '%s' > /sys/kernel/%s/%s" % (value, path, key) self.runcmd(cmd) - def create_led_platform_device(self, key, ops): - if ops['attr'] == 'all' or ops['attr'] == 'PLATFORM': - path = 'pddf/devices/platform' - self.create_attr('num_psus', self.data['PLATFORM']['num_psus'], path) - self.create_attr('num_fantrays', self.data['PLATFORM']['num_fantrays'], path) - - def create_led_device(self, key, ops): - if ops['attr'] == 'all' or ops['attr'] == self.data[key]['dev_info']['device_name']: - path = "pddf/devices/led" - for attr in self.data[key]['i2c']['attr_list']: - self.create_attr('device_name', self.data[key]['dev_info']['device_name'], path) - self.create_device(self.data[key]['dev_attr'], path, ops) - for attr_key in attr.keys(): - if (attr_key == 'swpld_addr_offset' or attr_key == 'swpld_addr'): - self.create_attr(attr_key, attr[attr_key], path) - elif (attr_key != 'attr_name' and attr_key != 'descr' and attr_key != 'attr_devtype' and - attr_key != 'attr_devname'): - state_path = path+'/state_attr' - self.create_attr(attr_key, attr[attr_key], state_path) - cmd = "echo '" + attr['attr_name']+"' > /sys/kernel/pddf/devices/led/dev_ops" - self.runcmd(cmd) - def led_parse(self, ops): getattr(self, ops['cmd']+"_led_platform_device")("PLATFORM", ops) for key in self.data.keys(): @@ -1265,29 +853,6 @@ def get_device_list(self, list, type): if attr['device_type'] == type: list.append(self.data[key]) - def create_pddf_devices(self): - self.led_parse({"cmd": "create", "target": "all", "attr": "all"}) - create_ret = 0 - create_ret = self.dev_parse(self.data['SYSTEM'], {"cmd": "create", "target": "all", "attr": "all"}) - if create_ret != 0: - return create_ret - if 'SYSSTATUS' in self.data: - create_ret = self.dev_parse(self.data['SYSSTATUS'], {"cmd": "create", "target": "all", "attr": "all"}) - if create_ret != 0: - return create_ret - - def delete_pddf_devices(self): - self.dev_parse(self.data['SYSTEM'], {"cmd": "delete", "target": "all", "attr": "all"}) - if 'SYSSTATUS' in self.data: - self.dev_parse(self.data['SYSSTATUS'], {"cmd": "delete", "target": "all", "attr": "all"}) - - def populate_pddf_sysfsobj(self): - self.dev_parse(self.data['SYSTEM'], {"cmd": "show", "target": "all", "attr": "all"}) - if 'SYSSTATUS' in self.data: - self.dev_parse(self.data['SYSSTATUS'], {"cmd": "show", "target": "all", "attr": "all"}) - self.led_parse({"cmd": "show", "target": "all", "attr": "all"}) - self.show_client_device() - def cli_dump_dsysfs(self, component): self.dev_parse(self.data['SYSTEM'], {"cmd": "show_attr", "target": "all", "attr": "all"}) if 'SYSSTATUS' in self.data: @@ -1297,23 +862,32 @@ def cli_dump_dsysfs(self, component): else: return None - def validate_pddf_devices(self, *args): - self.populate_pddf_sysfsobj() - v_ops = {'cmd': 'validate', 'target': 'all', 'attr': 'all'} - self.dev_parse(self.data['SYSTEM'], v_ops) - ################################################################################################################### # BMC APIs ################################################################################################################### def populate_bmc_cache_db(self, bmc_attr): bmc_cmd = str(bmc_attr['bmc_cmd']).strip() - - o_list = subprocess.check_output(bmc_cmd, shell=True).strip().split('\n') - bmc_cache[bmc_cmd] = {} - bmc_cache[bmc_cmd]['time'] = time.time() + field_name = str(bmc_attr['field_name']).strip() + field_pos= int(bmc_attr['field_pos'])-1 + + sdr_dump_file = "/usr/local/sdr_dump" + __bmc_cmd = bmc_cmd + if 'ipmitool' in bmc_cmd: + if not os.path.isfile(sdr_dump_file): + sdr_dump_cmd = "ipmitool sdr dump " + sdr_dump_file + subprocess.check_output(sdr_dump_cmd, shell=True, universal_newlines=True) + dump_cmd = "ipmitool -S " + sdr_dump_file + __bmc_cmd = __bmc_cmd.replace("ipmitool", dump_cmd, 1) + o_list = subprocess.check_output(__bmc_cmd, shell=True, universal_newlines=True).strip().split('\n') + bmc_cache[bmc_cmd]={} + bmc_cache[bmc_cmd]['time']=time.time() for entry in o_list: - name = entry.split()[0] - bmc_cache[bmc_cmd][name] = entry + if 'separator' in bmc_attr.keys(): + name = str(entry.split(bmc_attr['separator'])[0]).strip() + else: + name = str(entry.split()[0]).strip() + + bmc_cache[bmc_cmd][name]=entry def non_raw_ipmi_get_request(self, bmc_attr): bmc_db_update_time = 1 @@ -1330,16 +904,19 @@ def non_raw_ipmi_get_request(self, bmc_attr): self.populate_bmc_cache_db(bmc_attr) try: - data = bmc_cache[bmc_cmd][field_name] - value = data.split()[field_pos] + data=bmc_cache[bmc_cmd][field_name] + if 'separator' in bmc_attr: + value = data.split(bmc_attr['separator'])[field_pos].strip() + else: + value = data.split()[field_pos].strip() except Exception as e: - pass + pass - if 'mult' in bmc_attr.keys() and not value.isalpha(): - if value.isalpha(): + if 'mult' in bmc_attr.keys(): + if value.replace('.','',1).strip().isdigit(): + value = float(value) * float(bmc_attr['mult']) + else: value = 0.0 - value = float(value) * float(bmc_attr['mult']) - return str(value) def raw_ipmi_get_request(self, bmc_attr): @@ -1347,7 +924,7 @@ def raw_ipmi_get_request(self, bmc_attr): cmd = bmc_attr['bmc_cmd'] + " 2>/dev/null" if bmc_attr['type'] == 'raw': try: - value = subprocess.check_output(cmd, shell=True).strip() + value = subprocess.check_output(cmd, shell=True, universal_newlines=True).strip() except Exception as e: pass @@ -1358,7 +935,7 @@ def raw_ipmi_get_request(self, bmc_attr): if bmc_attr['type'] == 'mask': mask = int(bmc_attr['mask'].encode('utf-8'), 16) try: - value = subprocess.check_output(cmd, shell=True).strip() + value = subprocess.check_output(cmd, shell=True, universal_newlines=True).strip() except Exception as e: pass @@ -1369,7 +946,7 @@ def raw_ipmi_get_request(self, bmc_attr): if bmc_attr['type'] == 'ascii': try: - value = subprocess.check_output(cmd, shell=True) + value = subprocess.check_output(cmd, shell=True, universal_newlines=True).strip() except Exception as e: pass @@ -1423,18 +1000,19 @@ def get_attr_name_output(self, device_name, attr_name): bmc_attr = self.check_bmc_based_attr(device_name, attr_name) output = {"mode": "", "status": ""} - if bmc_attr is not None: - if bmc_attr == {}: - return {} - output['mode'] = "bmc" - output['status'] = self.bmc_get_cmd(bmc_attr) + if bmc_attr is not None and bmc_attr!={}: + output['mode']="bmc" + output['status']=self.bmc_get_cmd(bmc_attr) else: - output['mode'] = "i2c" + # bmc_attr is either None or {}. In both the cases, its highly likely that the attribute + # is i2c based + output['mode']="i2c" node = self.get_path(device_name, attr_name) if node is None: return {} try: - with open(node, 'r') as f: + # Seen some errors in case of unencodable characters hence ignoring them in python3 + with open(node, 'r', errors='ignore') as f: output['status'] = f.read() except IOError: return {}