|
| 1 | +from datetime import datetime |
1 | 2 | from swsscommon import swsscommon |
| 3 | +from xcvrd.xcvrd_utilities.utils import XCVRDUtils |
2 | 4 |
|
3 | 5 | class DBUtils: |
4 | 6 | """ |
5 | 7 | This class contains utility functions to interact with the redis database. |
6 | 8 | """ |
7 | | - def __init__(self, logger): |
| 9 | + NEVER = "never" |
| 10 | + NOT_AVAILABLE = "N/A" |
| 11 | + |
| 12 | + def __init__(self, sfp_obj_dict, port_mapping, task_stopping_event, logger): |
| 13 | + self.sfp_obj_dict = sfp_obj_dict |
| 14 | + self.port_mapping = port_mapping |
| 15 | + self.task_stopping_event = task_stopping_event |
| 16 | + self.xcvrd_utils = XCVRDUtils(sfp_obj_dict, logger) |
8 | 17 | self.logger = logger |
9 | 18 |
|
10 | | - """ |
11 | | - Updates the metadata tables for flag table |
12 | | - As part of the metadata update, the following tables are updated: |
13 | | - - Change Count Table |
14 | | - - Last Set Time Table |
15 | | - - Last Clear Time Table |
16 | | - """ |
17 | | - def update_flag_metadata_tables(self, logical_port_name, field_name, current_value, |
| 19 | + def post_diagnostic_values_to_db(self, logical_port_name, table, get_values_func, |
| 20 | + db_cache=None, beautify_func=None, enable_flat_memory_check=False): |
| 21 | + """ |
| 22 | + Posts the diagnostic values to the database. |
| 23 | +
|
| 24 | + Args: |
| 25 | + logical_port_name (str): Logical port name. |
| 26 | + table (object): Database table object. |
| 27 | + get_values_func (function): Function to get diagnostic values. |
| 28 | + db_cache (dict, optional): Cache for diagnostic values. |
| 29 | + beautify_func (function, optional): Function to beautify the diagnostic values. Defaults to self.beautify_info_dict. |
| 30 | + enable_flat_memory_check (bool, optional): Flag to check for flat memory support. Defaults to False. |
| 31 | + """ |
| 32 | + physical_port = self._validate_and_get_physical_port(logical_port_name, enable_flat_memory_check) |
| 33 | + if physical_port is None: |
| 34 | + return |
| 35 | + |
| 36 | + try: |
| 37 | + if db_cache is not None and physical_port in db_cache: |
| 38 | + # If cache is enabled and diagnostic values are in cache, just read from cache, no need read from EEPROM |
| 39 | + diagnostic_values_dict = db_cache[physical_port] |
| 40 | + else: |
| 41 | + diagnostic_values_dict = get_values_func(physical_port) |
| 42 | + if db_cache is not None: |
| 43 | + # If cache is enabled, put diagnostic values to cache |
| 44 | + db_cache[physical_port] = diagnostic_values_dict |
| 45 | + if diagnostic_values_dict is not None: |
| 46 | + if not diagnostic_values_dict: |
| 47 | + return |
| 48 | + |
| 49 | + # Use the provided beautify function or default to self.beautify_info_dict |
| 50 | + (beautify_func or self.beautify_info_dict)(diagnostic_values_dict) |
| 51 | + fvs = swsscommon.FieldValuePairs( |
| 52 | + [(k, v) for k, v in diagnostic_values_dict.items()] + |
| 53 | + [("last_update_time", self.get_current_time())] |
| 54 | + ) |
| 55 | + table.set(logical_port_name, fvs) |
| 56 | + |
| 57 | + except NotImplementedError: |
| 58 | + self.logger.log_error(f"Post port diagnostic values to db failed for {logical_port_name} " |
| 59 | + "as functionality is not implemented") |
| 60 | + return |
| 61 | + |
| 62 | + def _validate_and_get_physical_port(self, logical_port_name, enable_flat_memory_check=False): |
| 63 | + """ |
| 64 | + Validates the logical port and retrieves the corresponding physical port. |
| 65 | +
|
| 66 | + Validation Steps: |
| 67 | + 1. Ensures `task_stopping_event` is not set. |
| 68 | + 2. Checks if the logical port maps to a physical port. |
| 69 | + 3. Checks if the physical port has an associated SFP object. |
| 70 | + 4. Checks if the transceiver is present. |
| 71 | + 5. (Optional) Ensures the transceiver is not flat memory if `enable_flat_memory_check` is True. |
| 72 | +
|
| 73 | + If any of these checks fail, an error message is logged and `None` is returned. |
| 74 | + If all checks pass, the physical port number is returned. |
| 75 | +
|
| 76 | + Args: |
| 77 | + logical_port_name (str): Logical port name. |
| 78 | + enable_flat_memory_check (bool): Flag to check for flat memory support. |
| 79 | +
|
| 80 | + Returns: |
| 81 | + int: The physical port number if validation succeeds, or None if validation fails. |
| 82 | + """ |
| 83 | + if self.task_stopping_event.is_set(): |
| 84 | + return None |
| 85 | + |
| 86 | + pport_list = self.port_mapping.get_logical_to_physical(logical_port_name) |
| 87 | + if not pport_list: |
| 88 | + self.logger.log_error(f"Validate and get physical port failed for {logical_port_name} " |
| 89 | + "as no physical port found") |
| 90 | + return None |
| 91 | + |
| 92 | + physical_port = pport_list[0] |
| 93 | + |
| 94 | + if physical_port not in self.sfp_obj_dict: |
| 95 | + self.logger.log_error(f"Validate and get physical port failed for {logical_port_name} " |
| 96 | + "as no sfp object found") |
| 97 | + return None |
| 98 | + |
| 99 | + if not self.xcvrd_utils.get_transceiver_presence(physical_port): |
| 100 | + return None |
| 101 | + |
| 102 | + if enable_flat_memory_check and self.xcvrd_utils.is_transceiver_flat_memory(physical_port): |
| 103 | + return None |
| 104 | + |
| 105 | + return physical_port |
| 106 | + |
| 107 | + def _update_flag_metadata_tables(self, logical_port_name, curr_flag_dict, |
18 | 108 | flag_values_dict_update_time, |
19 | 109 | flag_value_table, |
20 | 110 | flag_change_count_table, flag_last_set_time_table, flag_last_clear_time_table, |
21 | 111 | table_name_for_logging): |
| 112 | + """ |
| 113 | + Updates the metadata tables for a flag table. |
| 114 | +
|
| 115 | + This method compares the current flag values with the values stored in the database. |
| 116 | + If there are changes, it updates the metadata tables accordingly, including: |
| 117 | + - Change count |
| 118 | + - Last set time |
| 119 | + - Last clear time |
| 120 | +
|
| 121 | + Args: |
| 122 | + logical_port_name (str): Logical port name. |
| 123 | + curr_flag_dict (dict): Current flag values. |
| 124 | + flag_values_dict_update_time (str): Timestamp of the update. |
| 125 | + flag_value_table (swsscommon.Table): Table containing flag values. |
| 126 | + flag_change_count_table (swsscommon.Table): Table for change counts. |
| 127 | + flag_last_set_time_table (swsscommon.Table): Table for last set times. |
| 128 | + flag_last_clear_time_table (swsscommon.Table): Table for last clear times. |
| 129 | + table_name_for_logging (str): Name of the table for logging purposes. |
| 130 | + """ |
22 | 131 | if flag_value_table is None: |
23 | 132 | self.logger.log_error(f"flag_value_table {table_name_for_logging} is None for port {logical_port_name}") |
24 | 133 | return |
25 | 134 |
|
| 135 | + # Retrieve existing flag values from the database |
26 | 136 | found, db_flags_value_dict = flag_value_table.get(logical_port_name) |
27 | | - # Table is empty, this is the first update to the metadata tables (this also means that the transceiver was detected for the first time) |
28 | | - # Initialize the change count to 0 and last set and clear times to 'never' |
29 | 137 | if not found: |
30 | | - flag_change_count_table.set(logical_port_name, swsscommon.FieldValuePairs([(field_name, '0')])) |
31 | | - flag_last_set_time_table.set(logical_port_name, swsscommon.FieldValuePairs([(field_name, 'never')])) |
32 | | - flag_last_clear_time_table.set(logical_port_name, swsscommon.FieldValuePairs([(field_name, 'never')])) |
| 138 | + # Initialize metadata tables for the first update |
| 139 | + self._initialize_metadata_tables(logical_port_name, curr_flag_dict, |
| 140 | + flag_change_count_table, flag_last_set_time_table, flag_last_clear_time_table) |
33 | 141 | return |
34 | | - else: |
35 | | - db_flags_value_dict = dict(db_flags_value_dict) |
36 | 142 |
|
37 | | - # No metadata update required if the value is 'N/A' |
38 | | - if str(current_value).strip() == 'N/A': |
39 | | - return |
| 143 | + db_flags_value_dict = dict(db_flags_value_dict) |
40 | 144 |
|
41 | | - # Update metadata if the value of flag has changed from the previous value |
42 | | - if field_name in db_flags_value_dict and db_flags_value_dict[field_name] != str(current_value): |
43 | | - found, db_change_count_dict = flag_change_count_table.get(logical_port_name) |
44 | | - if not found: |
45 | | - self.logger.log_error(f"Failed to get the change count for table {table_name_for_logging} port {logical_port_name}") |
46 | | - return |
47 | | - db_change_count_dict = dict(db_change_count_dict) |
48 | | - db_change_count = int(db_change_count_dict[field_name]) |
49 | | - db_change_count += 1 |
50 | | - flag_change_count_table.set(logical_port_name, swsscommon.FieldValuePairs([(field_name, str(db_change_count))])) |
51 | | - if current_value: |
52 | | - flag_last_set_time_table.set(logical_port_name, swsscommon.FieldValuePairs([(field_name, flag_values_dict_update_time)])) |
53 | | - else: |
54 | | - flag_last_clear_time_table.set(logical_port_name, swsscommon.FieldValuePairs([(field_name, flag_values_dict_update_time)])) |
| 145 | + # Update metadata for each flag |
| 146 | + for flag_key, curr_flag_value in curr_flag_dict.items(): |
| 147 | + if str(curr_flag_value).strip() == self.NOT_AVAILABLE: |
| 148 | + continue # Skip "N/A" values |
| 149 | + |
| 150 | + if flag_key in db_flags_value_dict and db_flags_value_dict[flag_key] != str(curr_flag_value): |
| 151 | + self._update_flag_metadata(logical_port_name, flag_key, curr_flag_value, |
| 152 | + flag_values_dict_update_time, flag_change_count_table, |
| 153 | + flag_last_set_time_table, flag_last_clear_time_table, |
| 154 | + table_name_for_logging) |
55 | 155 |
|
56 | 156 | def beautify_info_dict(self, info_dict): |
57 | 157 | for k, v in info_dict.items(): |
58 | 158 | if not isinstance(v, str): |
59 | 159 | info_dict[k] = str(v) |
| 160 | + |
| 161 | + def get_current_time(self, time_format="%a %b %d %H:%M:%S %Y"): |
| 162 | + """ |
| 163 | + Returns the current time in the specified format (UTC time). |
| 164 | +
|
| 165 | + Args: |
| 166 | + time_format (str): The format in which to return the time. Defaults to "Day Mon DD HH:MM:SS YYYY". |
| 167 | +
|
| 168 | + Returns: |
| 169 | + str: The current time in UTC. |
| 170 | + """ |
| 171 | + return datetime.utcnow().strftime(time_format) |
| 172 | + |
| 173 | + def _initialize_metadata_tables(self, logical_port_name, curr_flag_dict, |
| 174 | + flag_change_count_table, flag_last_set_time_table, |
| 175 | + flag_last_clear_time_table): |
| 176 | + """ |
| 177 | + Initializes metadata tables for the first update. |
| 178 | +
|
| 179 | + Args: |
| 180 | + logical_port_name (str): Logical port name. |
| 181 | + curr_flag_dict (dict): Current flag values. |
| 182 | + flag_change_count_table (swsscommon.Table): Table for change counts. |
| 183 | + flag_last_set_time_table (swsscommon.Table): Table for last set times. |
| 184 | + flag_last_clear_time_table (swsscommon.Table): Table for last clear times. |
| 185 | + """ |
| 186 | + for key in curr_flag_dict.keys(): |
| 187 | + flag_change_count_table.set(logical_port_name, swsscommon.FieldValuePairs([(key, '0')])) |
| 188 | + flag_last_set_time_table.set(logical_port_name, swsscommon.FieldValuePairs([(key, self.NEVER)])) |
| 189 | + flag_last_clear_time_table.set(logical_port_name, swsscommon.FieldValuePairs([(key, self.NEVER)])) |
| 190 | + |
| 191 | + def _update_flag_metadata(self, logical_port_name, flag_key, curr_flag_value, |
| 192 | + flag_values_dict_update_time, flag_change_count_table, |
| 193 | + flag_last_set_time_table, flag_last_clear_time_table, |
| 194 | + table_name_for_logging): |
| 195 | + """ |
| 196 | + Updates metadata for a single flag. |
| 197 | +
|
| 198 | + Args: |
| 199 | + logical_port_name (str): Logical port name. |
| 200 | + flag_key (str): The flag key. |
| 201 | + curr_flag_value (str): The current flag value. |
| 202 | + flag_values_dict_update_time (str): Timestamp of the update. |
| 203 | + flag_change_count_table (swsscommon.Table): Table for change counts. |
| 204 | + flag_last_set_time_table (swsscommon.Table): Table for last set times. |
| 205 | + flag_last_clear_time_table (swsscommon.Table): Table for last clear times. |
| 206 | + table_name_for_logging (str): Name of the table for logging purposes. |
| 207 | + """ |
| 208 | + # Retrieve the current change count |
| 209 | + found, db_change_count_dict = flag_change_count_table.get(logical_port_name) |
| 210 | + if not found: |
| 211 | + self.logger.log_warning(f"Failed to get the change count for table {table_name_for_logging} port {logical_port_name}") |
| 212 | + return |
| 213 | + |
| 214 | + db_change_count_dict = dict(db_change_count_dict) |
| 215 | + db_change_count = int(db_change_count_dict.get(flag_key, 0)) + 1 |
| 216 | + |
| 217 | + # Update the change count |
| 218 | + flag_change_count_table.set(logical_port_name, swsscommon.FieldValuePairs([(flag_key, str(db_change_count))])) |
| 219 | + |
| 220 | + # Update the last set or clear time |
| 221 | + if curr_flag_value: |
| 222 | + flag_last_set_time_table.set(logical_port_name, swsscommon.FieldValuePairs([(flag_key, flag_values_dict_update_time)])) |
| 223 | + else: |
| 224 | + flag_last_clear_time_table.set(logical_port_name, swsscommon.FieldValuePairs([(flag_key, flag_values_dict_update_time)])) |
0 commit comments