|
| 1 | +# COMPATIBLE_TERMINATION_TYPES = { |
| 2 | +# "circuittermination": ["interface", "frontport", "rearport", "circuittermination"], |
| 3 | +# "consoleport": ["consoleserverport", "frontport", "rearport"], |
| 4 | +# "consoleserverport": ["consoleport", "frontport", "rearport"], |
| 5 | +# "interface": ["interface", "circuittermination", "frontport", "rearport"], |
| 6 | +# "frontport": [ |
| 7 | +# "consoleport", |
| 8 | +# "consoleserverport", |
| 9 | +# "interface", |
| 10 | +# "frontport", |
| 11 | +# "rearport", |
| 12 | +# "circuittermination", |
| 13 | +# ], |
| 14 | +# "powerfeed": ["powerport"], |
| 15 | +# "poweroutlet": ["powerport"], |
| 16 | +# "powerport": ["poweroutlet", "powerfeed"], |
| 17 | +# "rearport": [ |
| 18 | +# "consoleport", |
| 19 | +# "consoleserverport", |
| 20 | +# "interface", |
| 21 | +# "frontport", |
| 22 | +# "rearport", |
| 23 | +# "circuittermination", |
| 24 | +# ], |
| 25 | +# } |
| 26 | +"""DCIM data related functions.""" |
| 27 | + |
| 28 | +from nautobot.dcim.constants import COMPATIBLE_TERMINATION_TYPES |
| 29 | + |
| 30 | +from nautobot_netbox_importer.base import ContentTypeStr, Uid |
| 31 | +from nautobot_netbox_importer.generator.source import DiffSyncModel, SourceAdapter, SourceModelWrapper |
| 32 | + |
| 33 | +# TBD: Move to base.py |
| 34 | +DUMMY_UID = "dummy" |
| 35 | + |
| 36 | +_FALLBACK_TERMINATION_TYPE = "circuittermination" |
| 37 | +_CIRCUIT_MODELS = {"circuittermination"} |
| 38 | + |
| 39 | + |
| 40 | +def _get_first_compatible_termination_type(stripped_type: str) -> ContentTypeStr: |
| 41 | + """Determine the first compatible termination type for a given termination. |
| 42 | +
|
| 43 | + This function identifies the first compatible termination type based on the |
| 44 | + given termination string, falling back to '_FALLBACK_TERMINATION_TYPE' if |
| 45 | + no compatibility is found. |
| 46 | +
|
| 47 | + Args: |
| 48 | + stripped_type (str): The termination type with 'dcim.' prefix removed |
| 49 | +
|
| 50 | + Returns: |
| 51 | + str: The compatible termination type with 'dcim.' prefix |
| 52 | +
|
| 53 | + Examples: |
| 54 | + >>> _get_first_compatible_termination_type("interface") |
| 55 | + 'dcim.interface' |
| 56 | +
|
| 57 | + >>> _get_first_compatible_termination_type("poweroutlet") |
| 58 | + 'dcim.powerport' |
| 59 | +
|
| 60 | + >>> _get_first_compatible_termination_type("unknown") |
| 61 | + 'circuits.circuittermination' |
| 62 | + """ |
| 63 | + |
| 64 | + def get_type(model_name: str) -> ContentTypeStr: |
| 65 | + return f"circuits.{model_name}" if model_name in _CIRCUIT_MODELS else f"dcim.{model_name}" |
| 66 | + |
| 67 | + if stripped_type not in COMPATIBLE_TERMINATION_TYPES: |
| 68 | + return get_type(_FALLBACK_TERMINATION_TYPE) |
| 69 | + |
| 70 | + types = COMPATIBLE_TERMINATION_TYPES[stripped_type] |
| 71 | + if _FALLBACK_TERMINATION_TYPE in types: |
| 72 | + return get_type(_FALLBACK_TERMINATION_TYPE) |
| 73 | + |
| 74 | + return get_type(types[0]) |
| 75 | + |
| 76 | + |
| 77 | +def _get_termination(uid: Uid, type_: ContentTypeStr, other_type: ContentTypeStr) -> tuple[Uid, ContentTypeStr]: |
| 78 | + """Determine the appropriate termination for a cable side. |
| 79 | +
|
| 80 | + This function evaluates cable termination data and returns correct termination information |
| 81 | + based on compatibility rules. |
| 82 | +
|
| 83 | + Args: |
| 84 | + uid (Uid): UID of the current termination |
| 85 | + type_ (ContentTypeStr): Type of the current termination |
| 86 | + other_type (ContentTypeStr): Type of the opposite termination |
| 87 | +
|
| 88 | + Returns: |
| 89 | + tuple[str, str]: A tuple containing (termination_id, termination_type) |
| 90 | +
|
| 91 | + Examples: |
| 92 | + >>> _get_termination("123", "dcim.interface", "dcim.frontport") |
| 93 | + ('123', 'dcim.interface') |
| 94 | +
|
| 95 | + >>> _get_termination("", "dcim.interface", "dcim.poweroutlet") |
| 96 | + ('dummy', 'circuit.circuittermination') |
| 97 | +
|
| 98 | + >>> _get_termination("123", "", "dcim.frontport") |
| 99 | + ('123', 'dcim.interface') |
| 100 | +
|
| 101 | + >>> _get_termination("123", "dcim.interface", "") |
| 102 | + ('123', 'dcim.interface') |
| 103 | +
|
| 104 | + >>> _get_termination("", "", "") |
| 105 | + ('dummy', 'circuit.circuittermination') |
| 106 | +
|
| 107 | + >>> _get_termination("456", "dcim.powerport", "dcim.poweroutlet") |
| 108 | + ('456', 'dcim.powerport') |
| 109 | + """ |
| 110 | + type_stripped = type_.split(".")[1] if type_ else "" |
| 111 | + other_stripped = other_type.split(".")[1] if other_type else "" |
| 112 | + first_compatible = _get_first_compatible_termination_type(other_stripped) |
| 113 | + |
| 114 | + if not type_: |
| 115 | + uid = DUMMY_UID |
| 116 | + type_ = first_compatible |
| 117 | + |
| 118 | + if not uid: |
| 119 | + uid = DUMMY_UID |
| 120 | + |
| 121 | + if not other_type: |
| 122 | + return uid, type_ |
| 123 | + |
| 124 | + if type_stripped in COMPATIBLE_TERMINATION_TYPES and other_stripped in COMPATIBLE_TERMINATION_TYPES.get( |
| 125 | + type_stripped, [] |
| 126 | + ): |
| 127 | + return uid, type_ |
| 128 | + |
| 129 | + return DUMMY_UID, first_compatible |
| 130 | + |
| 131 | + |
| 132 | +def _update_cable_termination(wrapper: SourceModelWrapper, cable: DiffSyncModel, side: str) -> None: |
| 133 | + """Update cable termination information for a specific side. |
| 134 | +
|
| 135 | + This function retrieves termination data for the specified side of a cable, determines |
| 136 | + the appropriate termination using _get_termination(), and updates the cable if needed. |
| 137 | +
|
| 138 | + Args: |
| 139 | + wrapper (SourceModelWrapper): Model wrapper containing field definitions |
| 140 | + cable (DiffSyncModel): The cable model to update |
| 141 | + side (str): Which side of the cable to update ('a' or 'b') |
| 142 | + """ |
| 143 | + uid = getattr(cable, f"termination_{side}_id", "") |
| 144 | + type_ = getattr(cable, f"termination_{side}_type", "") |
| 145 | + other_type = getattr(cable, f"termination_{'b' if side == 'a' else 'a'}_type", "") |
| 146 | + |
| 147 | + result_uid, result_type = _get_termination(uid, type_, other_type) |
| 148 | + |
| 149 | + if result_uid == uid and result_type == type_: |
| 150 | + return |
| 151 | + |
| 152 | + adapter = wrapper.adapter |
| 153 | + |
| 154 | + field = wrapper.fields[f"termination_{side}_type"] |
| 155 | + field.set_nautobot_value(cable, adapter.get_nautobot_content_type_uid(result_type)) |
| 156 | + |
| 157 | + field.add_issue( |
| 158 | + "UpdatedCableTermination", |
| 159 | + f"Cable termination {side.upper()} updated from {uid}, {type_} to {result_uid}, {result_type}", |
| 160 | + cable, |
| 161 | + ) |
| 162 | + |
| 163 | + if result_uid != DUMMY_UID: |
| 164 | + return |
| 165 | + |
| 166 | + type_wrapper = adapter.get_or_create_wrapper(result_type) |
| 167 | + pk = type_wrapper.get_pk_from_uid(result_uid) |
| 168 | + wrapper.add_reference(type_wrapper, pk) |
| 169 | + |
| 170 | + field = wrapper.fields[f"termination_{side}_id"] |
| 171 | + field.set_nautobot_value(cable, pk) |
| 172 | + |
| 173 | + # TBD: Cached dummy records needs to be improved to contain required values |
| 174 | + type_wrapper.cache_record({"id": DUMMY_UID}) |
| 175 | + |
| 176 | + |
| 177 | +def fix_cables(adapter: SourceAdapter) -> None: |
| 178 | + """Fix cables by ensuring proper terminations. |
| 179 | +
|
| 180 | + This function processes all cables from the source adapter and validates/fixes |
| 181 | + termination information for both sides of each cable. |
| 182 | +
|
| 183 | + Args: |
| 184 | + adapter (SourceAdapter): The source adapter containing cable data |
| 185 | + """ |
| 186 | + wrapper = adapter.get_or_create_wrapper("dcim.cable") |
| 187 | + |
| 188 | + for cable in adapter.get_all(wrapper.nautobot.diffsync_class): |
| 189 | + _update_cable_termination(wrapper, cable, "a") |
| 190 | + _update_cable_termination(wrapper, cable, "b") |
0 commit comments