Commit 7ed17e45 authored by Pablo Armingol's avatar Pablo Armingol
Browse files

refactor: replace base64 encoding with SHA-1 hashing for node and interface...

refactor: replace base64 encoding with SHA-1 hashing for node and interface identifiers in YangHandler
parent c393cc9c
Loading
Loading
Loading
Loading
+8 −7
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import hashlib
import json
import logging
import os
@@ -107,9 +108,10 @@ class YangHandler:
        node = network.create_path(f'node[node-id="{sap_device}"]')
        node.create_path('node-id', sap_device)

        # Add supporting-node pointing to urn:tfs:network:admin and device node name (using urn:tfs:node:<dev.name>)
        # Add supporting-node pointing to urn:tfs:network:admin and device node name (obfuscated with SHA-1)
        supporting_network_id = "urn:tfs:network:admin"
        supporting_node_id = f"urn:tfs:node:{device_name}"
        hashed_dev = hashlib.sha1(device_name.encode('utf-8')).hexdigest()
        supporting_node_id = f"urn:tfs:node:{hashed_dev}"
        supporting_nd = node.create_path(f'supporting-node[network-ref="{supporting_network_id}"][node-ref="{supporting_node_id}"]')
        supporting_nd.create_path('network-ref', supporting_network_id)
        supporting_nd.create_path('node-ref', supporting_node_id)
@@ -138,7 +140,6 @@ class YangHandler:
                    self._create_termination_point(node, sap_device, config.custom.resource_value, interface_name)

    def _create_termination_point(self, node: Any, sap_device: str, resource_value: str, interface_name: str) -> None:
        import base64
        config_data = json.loads(resource_value)
        ip_addresses = self._extract_ip_addresses(config_data)
        if ip_addresses:
@@ -146,13 +147,13 @@ class YangHandler:
            service = node.create_path(f'ietf-sap-ntw:service[service-type="ietf-vpn-common:l3vpn"]')

            # Proprietary obfuscated ID of the interface name:
            encoded_if = base64.b64encode(interface_name.encode('utf-8')).decode('utf-8').replace('=', '')
            final_sap_id = f"{sap_device}-{encoded_if}"
            hashed_if = hashlib.sha1(interface_name.encode('utf-8')).hexdigest()
            final_sap_id = f"{sap_device}-{hashed_if}"

            sap = service.create_path(f'sap[sap-id="{final_sap_id}"]')
            
            # Add parent-termination-point pointing to the physical tp-id
            parent_tp = f"urn:tfs:tp:{interface_name}"
            # Add parent-termination-point pointing to the physical tp-id (obfuscated with SHA-1)
            parent_tp = f"urn:tfs:tp:{hashed_if}"
            sap.create_path('parent-termination-point', parent_tp)
            # sap.create_path('peer-sap-id', "NOT_IMPLEMENTED") # OPTIONAL FIELD
            sap_status = sap.create_path('sap-status')