Skip to content

Commit

Permalink
fix: select a S2 version compatible with TNO RM (#103)
Browse files Browse the repository at this point in the history
* fix: select a S2 version compatible with TNO RM

Signed-off-by: Victor Garcia Reolid <victor@seita.nl>

* feat: create function get_latest_compatible_version to find te latest compatible version to use in a S2 connection.

Signed-off-by: Victor Garcia Reolid <victor@seita.nl>

* test: fix test where the selected version is the latest compatible version (the one provided by the RM).

Signed-off-by: Victor Garcia Reolid <victor@seita.nl>

---------

Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
  • Loading branch information
victorgarcia98 authored Feb 2, 2025
1 parent 20981c1 commit 8a75aba
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/flexmeasures_client/s2/cem.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@
from flexmeasures_client.client import FlexMeasuresClient
from flexmeasures_client.s2 import Handler, register
from flexmeasures_client.s2.control_types import ControlTypeHandler
from flexmeasures_client.s2.utils import get_reception_status, get_unique_id
from flexmeasures_client.s2.utils import (
get_latest_compatible_version,
get_reception_status,
get_unique_id,
)


class CEM(Handler):
__version__ = "0.1.0" # TODO: find the right version that we will use
__version__ = "0.2.0-beta"

_resource_manager_details: ResourceManagerDetails

Expand Down Expand Up @@ -227,8 +231,15 @@ def handle_handshake(self, message: Handshake):
# `selected_protocol_version` that matches the one of the RM
# TODO: Return a TBD "CloseConnection" message to close the connection

latest_compatible_version = get_latest_compatible_version(
message.supported_protocol_versions,
self.__version__,
self._logger,
)

return HandshakeResponse(
message_id=get_unique_id(), selected_protocol_version=self.__version__
message_id=get_unique_id(),
selected_protocol_version=str(latest_compatible_version),
)

@register(ResourceManagerDetails)
Expand Down
40 changes: 40 additions & 0 deletions src/flexmeasures_client/s2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from uuid import uuid4

import pydantic
from packaging.version import Version

try:
from s2python.common import ReceptionStatus, ReceptionStatusValues
Expand Down Expand Up @@ -80,3 +81,42 @@ def get_reception_status(
return ReceptionStatus(
subject_message_id=str(subject_message.message_id), status=status
)


def is_version_supported(v1: Version, v2: Version) -> bool:
return v1 >= v2


def get_latest_compatible_version(supported_versions, current_version, logger):
"""
Determines the latest compatible version based on supported protocol versions.
:param supported_versions: List of supported protocol versions (strings).
:param current_version: Current version of the system (string).
:param logger: Optional logger instance.
:return: Latest compatible version (Version object) or current_version if none
found.
"""

# RM didn't provide supported versions
if not supported_versions or len(supported_versions) == 0:
logger.warning("RM didn't provide any supported version")
return Version(current_version)

# Convert supported versions to Version objects and sort in descending order
rm_versions = sorted((Version(v) for v in supported_versions), reverse=True)
cem_version = Version(current_version)

# Find the latest compatible version
latest_compatible_version = next(
(v for v in rm_versions if is_version_supported(v, cem_version)), None
)

if latest_compatible_version is None:
logger.warning(
f"There are no compatible S2 versions supported both by the "
f"RM ({rm_versions}) and CEM ({cem_version})"
)
return cem_version

return latest_compatible_version
2 changes: 1 addition & 1 deletion tests/s2/test_cem.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def test_handshake(rm_handshake):
response["message_type"] == "HandshakeResponse"
), "response message_type should be HandshakeResponse"
assert (
response["selected_protocol_version"] == "0.1.0"
response["selected_protocol_version"] == "1.0.0"
), "CEM selected protocol version should be supported by the Resource Manager"


Expand Down
56 changes: 56 additions & 0 deletions tests/s2/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import logging

import pytest
from packaging.version import Version

from flexmeasures_client.s2.utils import get_latest_compatible_version


@pytest.mark.parametrize(
"supported_versions, current_version, expected_version",
[
([], "2.3.4", "2.3.4"), # No supported versions → return current_version
(None, "2.3.4", "2.3.4"), # No supported versions → return current_version
(
["1.2.3", "1.2.5", "1.2.9"],
"1.2.4",
"1.2.9",
), # Pick highest supported (>= 1.2.4)
(["2.3.0", "2.3.5", "2.3.9"], "2.3.1", "2.3.9"), # Pick latest >= 2.3.1
(
["1.4.0", "1.3.0", "1.2.3"],
"1.2.4",
"1.4.0",
), # Pick highest overall (>= 1.2.4)
(
["1.1.0", "1.2.1"],
"2.3.4",
"2.3.4",
), # No compatible version (higher major) → return current_version
(
["2.3.9-alpha", "2.3.5", "2.3.9"],
"2.3.1",
"2.3.9",
), # Pre-release ignored, pick latest valid
(
["2.1.0+build123", "2.1.4"],
"2.1.2",
"2.1.4",
), # Build metadata ignored, pick latest valid
],
)
def test_get_latest_compatible_version(
supported_versions, current_version, expected_version, caplog
):
caplog.set_level(logging.WARNING) # Capture warnings

latest_version = get_latest_compatible_version(
supported_versions, current_version, logger=logging.getLogger()
)

assert str(latest_version) == expected_version

if not supported_versions:
assert "RM didn't provide any supported version" in caplog.text
elif latest_version == Version(current_version):
assert "There are no compatible S2 versions" in caplog.text

0 comments on commit 8a75aba

Please sign in to comment.