|
| 1 | +""" |
| 2 | + cmis_enhanced_lpo.py |
| 3 | +
|
| 4 | + API for Arista Enhanced LPO modules. |
| 5 | +""" |
| 6 | + |
| 7 | +from ..public.cmis import CmisApi |
| 8 | +from ...fields import arista_lpo_consts as lpo |
| 9 | + |
| 10 | +# LPO_CAPABILITY is C1h:128. Bits 2, 3, and 4 advertise OER, VMA, and OMA support. |
| 11 | +_OER_CAPABILITY_MASK = 0x04 |
| 12 | +_VMA_CAPABILITY_MASK = 0x08 |
| 13 | +_OMA_CAPABILITY_MASK = 0x10 |
| 14 | + |
| 15 | +_VMA_FLAG_FIELDS = [ |
| 16 | + lpo.LPO_TX_HOST_INPUT_VMA_HIGH_ALARM_FLAG, |
| 17 | + lpo.LPO_TX_HOST_INPUT_VMA_LOW_ALARM_FLAG, |
| 18 | + lpo.LPO_TX_HOST_INPUT_VMA_HIGH_WARNING_FLAG, |
| 19 | + lpo.LPO_TX_HOST_INPUT_VMA_LOW_WARNING_FLAG, |
| 20 | +] |
| 21 | + |
| 22 | +_OMA_FLAG_FIELDS = [ |
| 23 | + lpo.LPO_RX_INPUT_OMA_HIGH_ALARM_FLAG, |
| 24 | + lpo.LPO_RX_INPUT_OMA_LOW_ALARM_FLAG, |
| 25 | + lpo.LPO_RX_INPUT_OMA_HIGH_WARNING_FLAG, |
| 26 | + lpo.LPO_RX_INPUT_OMA_LOW_WARNING_FLAG, |
| 27 | +] |
| 28 | + |
| 29 | +_VMA_TX_FIELDS = [ |
| 30 | + lpo.LPO_HOST_INPUT_VMA_TX1, |
| 31 | + lpo.LPO_HOST_INPUT_VMA_TX2, |
| 32 | + lpo.LPO_HOST_INPUT_VMA_TX3, |
| 33 | + lpo.LPO_HOST_INPUT_VMA_TX4, |
| 34 | + lpo.LPO_HOST_INPUT_VMA_TX5, |
| 35 | + lpo.LPO_HOST_INPUT_VMA_TX6, |
| 36 | + lpo.LPO_HOST_INPUT_VMA_TX7, |
| 37 | + lpo.LPO_HOST_INPUT_VMA_TX8, |
| 38 | +] |
| 39 | + |
| 40 | +_OMA_RX_FIELDS = [ |
| 41 | + lpo.LPO_INPUT_OMA_RX1, |
| 42 | + lpo.LPO_INPUT_OMA_RX2, |
| 43 | + lpo.LPO_INPUT_OMA_RX3, |
| 44 | + lpo.LPO_INPUT_OMA_RX4, |
| 45 | + lpo.LPO_INPUT_OMA_RX5, |
| 46 | + lpo.LPO_INPUT_OMA_RX6, |
| 47 | + lpo.LPO_INPUT_OMA_RX7, |
| 48 | + lpo.LPO_INPUT_OMA_RX8, |
| 49 | +] |
| 50 | + |
| 51 | + |
| 52 | +_VMA_THRESHOLD_FIELDS = [ |
| 53 | + lpo.LPO_TX_HOST_INPUT_VMA_HIGH_ALARM_THRESHOLD, |
| 54 | + lpo.LPO_TX_HOST_INPUT_VMA_LOW_ALARM_THRESHOLD, |
| 55 | + lpo.LPO_TX_HOST_INPUT_VMA_HIGH_WARNING_THRESHOLD, |
| 56 | + lpo.LPO_TX_HOST_INPUT_VMA_LOW_WARNING_THRESHOLD, |
| 57 | +] |
| 58 | + |
| 59 | +_OMA_THRESHOLD_FIELDS = [ |
| 60 | + lpo.LPO_RX_INPUT_OMA_HIGH_ALARM_THRESHOLD, |
| 61 | + lpo.LPO_RX_INPUT_OMA_LOW_ALARM_THRESHOLD, |
| 62 | + lpo.LPO_RX_INPUT_OMA_HIGH_WARNING_THRESHOLD, |
| 63 | + lpo.LPO_RX_INPUT_OMA_LOW_WARNING_THRESHOLD, |
| 64 | +] |
| 65 | + |
| 66 | + |
| 67 | +def _value_or_na(value): |
| 68 | + """Return the API fallback for decoded fields that failed to read.""" |
| 69 | + return value if value is not None else 'N/A' |
| 70 | + |
| 71 | + |
| 72 | +def _read_value_or_na(xcvr_eeprom, field): |
| 73 | + """Read an already-decoded EEPROM field and normalize read failures.""" |
| 74 | + return _value_or_na(xcvr_eeprom.read(field)) |
| 75 | + |
| 76 | + |
| 77 | +def _set_na(output, fields): |
| 78 | + """Populate unsupported scalar fields without reading optional registers.""" |
| 79 | + for field in fields: |
| 80 | + output[field] = 'N/A' |
| 81 | + |
| 82 | + |
| 83 | +def _set_lane_flag_na(output, fields): |
| 84 | + """Populate unsupported per-lane flag fields with the API fallback value.""" |
| 85 | + for field in fields: |
| 86 | + for lane in range(1, 9): |
| 87 | + output["{}{}".format(field, lane)] = 'N/A' |
| 88 | + |
| 89 | + |
| 90 | +def _lane_flag_na_dict(field): |
| 91 | + """Build the per-lane fallback shape expected by decoded flag fields.""" |
| 92 | + flags = {} |
| 93 | + _set_lane_flag_na(flags, [field]) |
| 94 | + return flags |
| 95 | + |
| 96 | + |
| 97 | +def _is_capability_supported(capabilities, mask): |
| 98 | + """Check whether an optional LPO capability bit is advertised.""" |
| 99 | + return bool(capabilities & mask) |
| 100 | + |
| 101 | + |
| 102 | +class CmisEnhancedLpoApi(CmisApi): |
| 103 | + def _read_if_supported(self, field, capabilities, mask): |
| 104 | + """Read optional info fields only when the capability bit is set.""" |
| 105 | + if not _is_capability_supported(capabilities, mask): |
| 106 | + return 'N/A' |
| 107 | + return _read_value_or_na(self.xcvr_eeprom, field) |
| 108 | + |
| 109 | + def get_transceiver_info(self): |
| 110 | + xcvr_info = super().get_transceiver_info() |
| 111 | + if xcvr_info is None: |
| 112 | + return None |
| 113 | + capabilities = self.xcvr_eeprom.read(lpo.LPO_CAPABILITY) |
| 114 | + if capabilities is None: |
| 115 | + return None |
| 116 | + |
| 117 | + xcvr_info.update({ |
| 118 | + lpo.LPO_TX_POLARITY_INVERTED: _read_value_or_na(self.xcvr_eeprom, lpo.LPO_TX_POLARITY_INVERTED), |
| 119 | + lpo.LPO_RX_POLARITY_INVERTED: _read_value_or_na(self.xcvr_eeprom, lpo.LPO_RX_POLARITY_INVERTED), |
| 120 | + lpo.LPO_TX_HOST_INPUT_VMA_MON_ACCURACY: self._read_if_supported( |
| 121 | + lpo.LPO_TX_HOST_INPUT_VMA_MON_ACCURACY, capabilities, _VMA_CAPABILITY_MASK |
| 122 | + ), |
| 123 | + lpo.LPO_RX_INPUT_OMA_MON_ACCURACY: self._read_if_supported( |
| 124 | + lpo.LPO_RX_INPUT_OMA_MON_ACCURACY, capabilities, _OMA_CAPABILITY_MASK |
| 125 | + ), |
| 126 | + lpo.LPO_TX_OUTER_EXTINCTION_RATIO_MAX: self._read_if_supported( |
| 127 | + lpo.LPO_TX_OUTER_EXTINCTION_RATIO_MAX, capabilities, _OER_CAPABILITY_MASK |
| 128 | + ), |
| 129 | + }) |
| 130 | + return xcvr_info |
| 131 | + |
| 132 | + def get_transceiver_dom_real_value(self): |
| 133 | + trans_dom = super().get_transceiver_dom_real_value() |
| 134 | + if trans_dom is None: |
| 135 | + return None |
| 136 | + capabilities = self.xcvr_eeprom.read(lpo.LPO_CAPABILITY) |
| 137 | + if capabilities is None: |
| 138 | + return None |
| 139 | + |
| 140 | + if _is_capability_supported(capabilities, _VMA_CAPABILITY_MASK): |
| 141 | + for field in _VMA_TX_FIELDS: |
| 142 | + trans_dom[field] = _read_value_or_na(self.xcvr_eeprom, field) |
| 143 | + else: |
| 144 | + _set_na(trans_dom, _VMA_TX_FIELDS) |
| 145 | + |
| 146 | + if _is_capability_supported(capabilities, _OMA_CAPABILITY_MASK): |
| 147 | + for field in _OMA_RX_FIELDS: |
| 148 | + trans_dom[field] = _read_value_or_na(self.xcvr_eeprom, field) |
| 149 | + else: |
| 150 | + _set_na(trans_dom, _OMA_RX_FIELDS) |
| 151 | + |
| 152 | + return trans_dom |
| 153 | + |
| 154 | + def get_transceiver_threshold_info(self): |
| 155 | + threshold_dict = super().get_transceiver_threshold_info() |
| 156 | + if threshold_dict is None: |
| 157 | + return None |
| 158 | + capabilities = self.xcvr_eeprom.read(lpo.LPO_CAPABILITY) |
| 159 | + if capabilities is None: |
| 160 | + return None |
| 161 | + |
| 162 | + if _is_capability_supported(capabilities, _VMA_CAPABILITY_MASK): |
| 163 | + for field in _VMA_THRESHOLD_FIELDS: |
| 164 | + threshold_dict[field] = _read_value_or_na(self.xcvr_eeprom, field) |
| 165 | + else: |
| 166 | + _set_na(threshold_dict, _VMA_THRESHOLD_FIELDS) |
| 167 | + |
| 168 | + if _is_capability_supported(capabilities, _OMA_CAPABILITY_MASK): |
| 169 | + for field in _OMA_THRESHOLD_FIELDS: |
| 170 | + threshold_dict[field] = _read_value_or_na(self.xcvr_eeprom, field) |
| 171 | + else: |
| 172 | + _set_na(threshold_dict, _OMA_THRESHOLD_FIELDS) |
| 173 | + |
| 174 | + return threshold_dict |
| 175 | + |
| 176 | + def get_transceiver_dom_flags(self): |
| 177 | + dom_flag_dict = super().get_transceiver_dom_flags() |
| 178 | + if dom_flag_dict is None: |
| 179 | + return None |
| 180 | + capabilities = self.xcvr_eeprom.read(lpo.LPO_CAPABILITY) |
| 181 | + if capabilities is None: |
| 182 | + return None |
| 183 | + |
| 184 | + if _is_capability_supported(capabilities, _VMA_CAPABILITY_MASK): |
| 185 | + for field in _VMA_FLAG_FIELDS: |
| 186 | + flags = self.xcvr_eeprom.read(field) |
| 187 | + dom_flag_dict.update(flags if flags is not None else _lane_flag_na_dict(field)) |
| 188 | + else: |
| 189 | + _set_lane_flag_na(dom_flag_dict, _VMA_FLAG_FIELDS) |
| 190 | + |
| 191 | + if _is_capability_supported(capabilities, _OMA_CAPABILITY_MASK): |
| 192 | + for field in _OMA_FLAG_FIELDS: |
| 193 | + flags = self.xcvr_eeprom.read(field) |
| 194 | + dom_flag_dict.update(flags if flags is not None else _lane_flag_na_dict(field)) |
| 195 | + else: |
| 196 | + _set_lane_flag_na(dom_flag_dict, _OMA_FLAG_FIELDS) |
| 197 | + |
| 198 | + return dom_flag_dict |
0 commit comments