atlanticwave-sdx / datamodel

Parsing and validation library for AtlanticWave SDX.
https://www.atlanticwave-sdx.net
MIT License
0 stars 1 forks source link

Importing connection request data mutates it #148

Open sajith opened 1 month ago

sajith commented 1 month ago

Consider this connection request:

request = {
    "name": "new-connection",
    "description": "a test circuit",
    "id": "7ab36e45-c041-4883-aceb-f391efa5866a",
    "endpoints": [
        {"port_id": "urn:sdx:port:amlight.net:A1:1", "vlan": "777"},
        {"port_id": "urn:sdx:port:amlight:B1:1", "vlan": "55:90"},
    ],
}

Passing this into ConnectionHandler.import_connection_data() can mutate the original request. Speficially, endpoints[n].port_id gets renamed to endpoints.id and endpoints[n].vlan becomes endpoints[n].vlan_range.

import pprint
from sdx_datamodel.parsing.connectionhandler import ConnectionHandler

ConnectionHandler().import_connection_data(request)
pprint.pprint(request)

The above code prints:

{'description': 'a test circuit',
 'endpoints': [{'id': 'urn:sdx:port:amlight.net:A1:1',
                'name': 'urn:sdx:port:amlight.net:A1:1',
                'vlan_range': 777},
               {'id': 'urn:sdx:port:amlight:B1:1',
                'name': 'urn:sdx:port:amlight:B1:1',
                'vlan_range': '55:90'}],
 'id': '7ab36e45-c041-4883-aceb-f391efa5866a',
 'name': 'new-connection'}

This is surprising behavior and mutating data like this makes it harder to reason about program behavior. Further, we'll be unable to access the original request, should we need it (such as when needing to look up disallowed VLANs: https://github.com/atlanticwave-sdx/pce/issues/208). Although we still have the data we need in a slightly different form, switching things in this manner is probably not the best way to do this.

(ConnectionHandler._make_port() is the likely culprit. Perhaps using Pydantic to validate connection requests would be a good idea.)