class DecodingError(Exception):
    """Error on message decoding."""
    pass class EncodingError(Exception):
    """Error on message encoding."""
    pass def create_request_by_name(self, name, *args, **kwargs):
    return self.registry[name + "Req"](*args, **kwargs) PICMG_IDENTIFIER = 0x00
FRU_ACTIVATION_FRU_ACTIVATE = 0x1
FRU_ACTIVATION_FRU_DEACTIVATE = 0x0
LINK_INTERFACE_BASE = 0x0
LINK_INTERFACE_FABRIC = 0x1
LINK_INTERFACE_UPDATE_CHANNEL = 0x2
LINK_TYPE_BASE = 0x01
LINK_TYPE_ETHERNET_FABRIC = 0x02
LINK_TYPE_INFINIBAND_FABRIC = 0x03
LINK_TYPE_STARFABRIC_FABRIC = 0x04
LINK_TYPE_PCIEXPRESS_FABRIC = 0x05
LINK_TYPE_OEM0 = 0xf0
LINK_TYPE_OEM1 = 0xf1
LINK_TYPE_OEM2 = 0xf2
LINK_TYPE_OEM3 = 0xf3
LINK_TYPE_EXT_BASE0 = 0x00
LINK_TYPE_EXT_BASE1 = 0x01
LINK_SIGNALING_CLASS_BASIC = 0
LINK_SIGNALING_CLASS_10_3125_GBD = 3
LINK_TYPE_EXT_ETHERNET_FIX1000_BX = 0x0
LINK_TYPE_EXT_ETHERNET_FIX10G_BX4 = 0x1
LINK_TYPE_EXT_ETHERNET_FCPI = 0x02
LINK_TYPE_EXT_ETHERNET_FIX1000_KX = 0x3
LINK_TYPE_EXT_ETHERNET_FIX10G_KX4 = 0x4
LINK_TYPE_EXT_ETHERNET_FIX10G_KR = 0x0
LINK_TYPE_EXT_ETHERNET_FIX40G_KR4 = 0x1
LINK_TYPE_EXT_OEM_LINK_TYPE_EXT_0 = 0x00
LINK_FLAGS_LANE0 = 0x01
LINK_FLAGS_LANE0123 = 0x0f
LINK_STATE_DISABLE = 0
LINK_STATE_ENABLE = 1
FRU_CONTROL_COLD_RESET = 0x00
FRU_CONTROL_WARM_RESET = 0x01
FRU_CONTROL_GRACEFUL_REBOOT = 0x02
FRU_CONTROL_ISSUE_DIAGNOSTIC_INTERRUPT = 0x03
FRU_CONTROL_QUIESCED = 0x04
LED_COLOR_BLUE = 0x01
LED_COLOR_RED = 0x02
LED_COLOR_GREEN = 0x03
LED_COLOR_AMBER = 0x04
LED_COLOR_ORANGE = 0x05
LED_COLOR_WHITE = 0x06
LED_FUNCTION_OFF = 0x00
LED_FUNCTION_BLINKING_RANGE = list(range(0x01, 0xfa))
LED_FUNCTION_LAMP_TEST = 0xfb
LED_FUNCTION_ON = 0xff
LED_STATE_LOCAL_CONTROL = 0
LED_STATE_OVERRIDE = 1
LED_STATE_LAMP_TEST = 2
class PicmgIdentifier(UnsignedInt):
class PicmgMessage(Message):
class GetPicmgPropertiesReq(PicmgMessage):
class GetPicmgPropertiesRsp(PicmgMessage):
class GetAddressInfoReq(PicmgMessage):
class GetAddressInfoRsp(PicmgMessage):
class GetShelfAddressInfoReq(PicmgMessage):
class GetShelfAddressInfoRsp(PicmgMessage):
class FruControlReq(PicmgMessage):
class FruControlRsp(PicmgMessage):
class GetFruControlCapabilitiesReq(PicmgMessage):
class GetFruControlCapabilitiesRsp(PicmgMessage):
class SetFruActivationPolicyReq(PicmgMessage):
class SetFruActivationPolicyRsp(PicmgMessage):
class GetFruActivationPolicyReq(PicmgMessage):
class GetFruActivationPolicyRsp(PicmgMessage):
class SetFruActivationReq(PicmgMessage):
class SetFruActivationRsp(PicmgMessage):
class GetDeviceLocatorRecordIdReq(PicmgMessage):
class GetDeviceLocatorRecordIdRsp(PicmgMessage):
class GetFruLedPropertiesReq(PicmgMessage):
class GetFruLedPropertiesRsp(PicmgMessage):
class GetFruLedColorCapabilitiesReq(PicmgMessage):
class GetFruLedColorCapabilitiesRsp(PicmgMessage):
class GetPowerLevelReq(PicmgMessage):
class GetPowerLevelRsp(PicmgMessage):
class GetFanSpeedPropertiesReq(PicmgMessage):
class GetFanSpeedPropertiesRsp(PicmgMessage):
class SetFanLevelReq(PicmgMessage):
class SetFanLevelRsp(PicmgMessage):
class GetFanLevelReq(PicmgMessage):
class GetFanLevelRsp(PicmgMessage):
class SetFruLedStateReq(PicmgMessage):
class SetFruLedStateRsp(PicmgMessage):
class GetFruLedStateReq(PicmgMessage):
class GetFruLedStateRsp(PicmgMessage):
class SetPortStateReq(PicmgMessage):
class SetPortStateRsp(PicmgMessage):
class GetPortStateReq(PicmgMessage):
class GetPortStateRsp(PicmgMessage):
class SetSignalingClassReq(PicmgMessage):
class SetSignalingClassRsp(PicmgMessage):
class GetSignalingClassReq(PicmgMessage):
class GetSignalingClassRsp(PicmgMessage):
class GetLocationInformationReq(PicmgMessage):
class GetLocationInformationRsp(PicmgMessage):
class GetPowerChannelStatusReq(PicmgMessage):
class GetPowerChannelStatusRsp(PicmgMessage):
class GetTelcoAlarmCapabilityReq(PicmgMessage):
class GetTelcoAlarmCapabilityRsp(PicmgMessage):
    def __init__(self, name='picmg_identifier'):
    def _cond_override(obj):
    def _cond_lamp_test(obj): def check_completion_code(cc):
    if cc != constants.CC_OK:
        raise CompletionCodeError(cc) class State(DefaultProperties, ResponseDecoder):
    """This is a container that represents a state.

    The state can have default
    properties that are created by init.
    """

    def __init__(self, rsp=None):
        DefaultProperties.__init__(self)
        ResponseDecoder.__init__(self, rsp) FRU_CONTROL_COLD_RESET = 0x00 FRU_CONTROL_WARM_RESET = 0x01 FRU_CONTROL_GRACEFUL_REBOOT = 0x02 FRU_CONTROL_ISSUE_DIAGNOSTIC_INTERRUPT = 0x03 FRU_ACTIVATION_FRU_ACTIVATE = 0x1 FRU_ACTIVATION_FRU_DEACTIVATE = 0x0
from .errors import DecodingError, EncodingError
from .msgs import create_request_by_name
from .msgs import picmg
from .utils import check_completion_code
from .state import State
from .msgs.picmg import \
        FRU_CONTROL_COLD_RESET, FRU_CONTROL_WARM_RESET, \
        FRU_CONTROL_GRACEFUL_REBOOT, FRU_CONTROL_ISSUE_DIAGNOSTIC_INTERRUPT, \
        FRU_ACTIVATION_FRU_ACTIVATE, FRU_ACTIVATION_FRU_DEACTIVATE
         SIGNALING_CLASS_BASIC,
         'FC-PI'),
        (TYPE_ETHERNET_FABRIC,
         TYPE_EXT_ETHERNET_FIX1000_KX,
         SIGNALING_CLASS_BASIC,
         'Fixed 1000BASE-KX'),
        (TYPE_ETHERNET_FABRIC,
         TYPE_EXT_ETHERNET_FIX10G_KX4,
         SIGNALING_CLASS_BASIC,
         'Fixed 10GBASE-KX4'),
        (TYPE_ETHERNET_FABRIC,
         TYPE_EXT_ETHERNET_FIX10G_KR,
         SIGNALING_CLASS_10_3125_GBD,
         'Fixed 10GBASE-KR'),
        (TYPE_ETHERNET_FABRIC,
         TYPE_EXT_ETHERNET_FIX40G_KR4,
         SIGNALING_CLASS_10_3125_GBD,
         'Fixed 40GBASE-KR4'),
    ]

    def get_link_type_string(self, link_type, ext, cls=0):
        for desc in self.LINK_TYPE_DESCR_STRING:
            if desc[0] == link_type and desc[1] == ext and desc[2] == cls:
                return desc[3]
        return 'unknown'


class PowerLevel(State):

    def _from_response(self, rsp):
        self.dynamic_power_configuration = \
                rsp.properties.dynamic_power_configuration
        self.power_level = rsp.properties.power_level
        self.delay_to_stable = rsp.delay_to_stable_power
        self.power_mulitplier = rsp.power_multiplier
        self.power_levels = rsp.power_draw


class FanSpeedProperties(State):

    def _from_response(self, rsp):
        self.minimum_speed_level = rsp.minimum_speed_level
        self.maximum_speed_level = rsp.maximum_speed_level
        self.normal_operation_level = rsp.normal_operation_level
        self.local_control_supported = rsp.properties.local_control_supported


class LedState(State):
    COLOR_BLUE = picmg.LED_COLOR_BLUE
    COLOR_RED = picmg.LED_COLOR_RED
    COLOR_GREEN = picmg.LED_COLOR_GREEN
    COLOR_AMBER = picmg.LED_COLOR_AMBER
    COLOR_ORANGE = picmg.LED_COLOR_ORANGE
    COLOR_WHITE = picmg.LED_COLOR_WHITE

    FUNCTION_OFF = 1
    FUNCTION_BLINKING = 2
    FUNCTION_ON = 3
    FUNCTION_LAMP_TEST = 4

    __properties__ = [
        # (property, description)
        ('fru_id', ''),
        ('led_id', ''),
        ('local_state_available', ''),
        ('override_enabled', ''),
        ('lamp_test_enabled', ''),
        ('local_function', ''),
        ('local_off_duration', ''),
        ('local_on_duration', ''),
        ('local_color', ''),
        ('override_function', ''),
        ('override_off_duration', ''),
        ('override_on_duration', ''),
        ('override_color', ''),
        ('lamp_test_duration', ''),
    ]

    def __init__(self, rsp=None, fru_id=None, led_id=None, color=None,
                 function=None):
        super(LedState, self).__init__(rsp)
        if fru_id is not None:
            self.fru_id = fru_id
        if led_id is not None:
            self.led_id = led_id
        if color is not None:
            self.override_color = color
        if function is not None:
            self.override_function = function

    def __str__(self):
        string = '[flags '
        string += self.local_state_available and ' LOCAL_STATE' or ''
        string += self.override_enabled and ' OVR_EN' or ''
        string += self.lamp_test_enabled and ' LAMP_TEST_EN' or ''
        if not self.local_state_available and not self.override_enabled \
                and not self.lamp_test_enabled:
            string += ' NONE'
        if self.local_state_available:
            string += ' local_function %s local_color %s' % (
                self.local_function, self.local_color)
        if self.override_enabled:
            string += ' override_function %s override_color %s' % (
                self.override_function, self.override_color)
        string += ']'
        return string

    def _from_response(self, res):
        self.local_state_available = bool(res.led_states.local_avail)
        self.override_enabled = bool(res.led_states.override_en)
        self.lamp_test_enabled = bool(res.led_states.lamp_test_en)

        if res.local_function == picmg.LED_FUNCTION_OFF:
            self.local_function = self.FUNCTION_OFF
        elif res.local_function == picmg.LED_FUNCTION_ON:
            self.local_function = self.FUNCTION_ON
        elif res.local_function in picmg.LED_FUNCTION_BLINKING_RANGE:
            self.local_function = self.FUNCTION_BLINKING
            self.local_off_duration = res.local_function * 10
            if res.local_on_duration not in picmg.LED_FUNCTION_BLINKING_RANGE:

                raise DecodingError()