Skip to content

Commit

Permalink
code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
YufengXin committed Jan 9, 2025
1 parent dba13a3 commit 417bb75
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/sdx_datamodel/parsing/connectionhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def import_connection_data(self, data: dict) -> Connection:
bandwidth_required_obj = qos_metrics.get("min_bw")
if bandwidth_required_obj is not None:
bandwidth_required = bandwidth_required_obj.get("value")
latency_required_obj = qos_metrics.get("max_latency")
latency_required_obj = qos_metrics.get("max_delay")
if latency_required_obj is not None:
latency_required = latency_required_obj.get("value")

Expand Down
47 changes: 25 additions & 22 deletions src/sdx_datamodel/validation/connectionvalidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def is_valid(self) -> bool:

def validate(self, raise_error=True) -> [str]:
errors = self._validate_connection(self._connection)
if errors and raise_error:
raise ValueError("\n".join(errors))
# if errors and raise_error:
# raise ValueError("\n".join(errors))
return errors

def _validate_connection(self, conn: Connection):
Expand All @@ -56,19 +56,32 @@ def _validate_connection(self, conn: Connection):
:return: A list of any issues in the data.
"""

errors = []
errors += self._validate_object_defaults(conn)
errors += self._validate_port(conn.ingress_port, conn)
errors += self._validate_port(conn.egress_port, conn)
if hasattr(conn, "scheduling"):

if hasattr(conn, "start_time") or hasattr(conn, "end_time"):
errors += self._validate_time(conn.start_time, conn.end_time, conn)

if hasattr(conn, "qos_metrics"):
errors += self._validate_qos_metrics_value(conn.qos_metrics)
if hasattr(conn, "latency_required"):
errors += self._validate_qos_metrics_value(
"max_delay", conn.latency_required, 1000
)

if hasattr(conn, "bandwidth_required"):
errors += self._validate_qos_metrics_value(
"min_bw", conn.bandwidth_required, 100
)

if hasattr(conn, "max_number_oxps"):
errors += self._validate_qos_metrics_value(
"max_number_oxps", conn.bandwidth_required, 100
)
return errors

def _validate_qos_metrics_value(self, qos_metrics: dict):
def _validate_qos_metrics_value(self, metric, value, max_value):
"""
Validate that the QoS Metrics provided meets the XSD standards.
Expand All @@ -86,22 +99,10 @@ def _validate_qos_metrics_value(self, qos_metrics: dict):
"""
errors = []

if not isinstance(qos_metrics.max_delay, int):
errors.append(
f"{qos_metrics.max_delay} max_delay must be a number"
)
if not (0 <= qos_metrics.max_delay <= 1000):
errors.append(
f"{qos_metrics.max_delay} max_delay must be between 0 and 1000"
)
if not isinstance(qos_metrics.max_number_oxps, int):
errors.append(
f"{qos_metrics.max_number_oxps} max_number_oxps must be a number"
)
if not (0 <= qos_metrics.max_number_oxps <= 100):
errors.append(
f"{qos_metrics.max_number_oxps} max_number_oxps must be between 0 and 100"
)
if not isinstance(value, int):
errors.append(f"{value} {metric} must be a number")
if not (0 <= value <= max_value):
errors.append(f"{value} {metric} must be between 0 and 1000")

return errors

Expand Down Expand Up @@ -154,6 +155,8 @@ def _validate_time(self, start_time: str, end_time: str, conn: Connection):
errors = []
# if not match(ISO_TIME_FORMAT, time):
# errors.append(f"{time} time needs to be in full ISO format")
if not start_time:
start_time = datetime.now().isoformat()
try:
start_time_obj = datetime.fromisoformat(start_time)
if start_time_obj < datetime.now():
Expand Down
3 changes: 2 additions & 1 deletion tests/test_connection_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ def test_connection_invalid_qos_metrics(self):
],
"scheduling": {"end_time": "2023-12-30"},
"qos_metrics": {
"min_bw": {"value": 101},
"max_delay": {"value": 1001},
"max_number_oxps": {"value": 101},
},
}

validator = self._get_validator(connection_request)
self.assertTrue(validator.is_valid())
self.assertFalse(validator.is_valid())


if __name__ == "__main__":
Expand Down

0 comments on commit 417bb75

Please sign in to comment.