From 4153e3cad6368ff685caedf5e98c9a630d0e4b18 Mon Sep 17 00:00:00 2001
From: gifrerenom <lluis.gifre@cttc.es>
Date: Tue, 14 Nov 2023 16:22:14 +0000
Subject: [PATCH] Context component:

- Fixed inspection of Link attribute presence
---
 src/context/service/database/Link.py |  3 ++-
 src/context/tests/test_link.py       | 12 ++++++++----
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/context/service/database/Link.py b/src/context/service/database/Link.py
index afebbb537..4ca3cee68 100644
--- a/src/context/service/database/Link.py
+++ b/src/context/service/database/Link.py
@@ -102,7 +102,8 @@ def link_set(db_engine : Engine, messagebroker : MessageBroker, request : Link)
     total_capacity_gbps, used_capacity_gbps = None, None
     if request.HasField('attributes'):
         attributes = request.attributes
-        attribute_names = set([field.name for field in attributes._fields])
+        # In proto3, HasField() does not work for scalar fields, using ListFields() instead.
+        attribute_names = set([field.name for field,_ in attributes.ListFields()])
         if 'total_capacity_gbps' in attribute_names:
             total_capacity_gbps = attributes.total_capacity_gbps
         if 'used_capacity_gbps' in attribute_names:
diff --git a/src/context/tests/test_link.py b/src/context/tests/test_link.py
index d27ff6e4a..8b07f0230 100644
--- a/src/context/tests/test_link.py
+++ b/src/context/tests/test_link.py
@@ -96,7 +96,8 @@ def test_link(context_client : ContextClient) -> None:
     assert response.name == LINK_R1_R2_NAME
     assert len(response.link_endpoint_ids) == 2
     assert response.HasField('attributes')
-    attribute_names = set([field.name for field in response.attributes._fields])
+    # In proto3, HasField() does not work for scalar fields, using ListFields() instead.
+    attribute_names = set([field.name for field,_ in response.attributes.ListFields()])
     assert 'total_capacity_gbps' in attribute_names
     assert abs(response.attributes.total_capacity_gbps - 100) < 1.e-12
     assert 'used_capacity_gbps' in attribute_names
@@ -134,7 +135,8 @@ def test_link(context_client : ContextClient) -> None:
     assert response.name == new_link_name
     assert len(response.link_endpoint_ids) == 2
     assert response.HasField('attributes')
-    attribute_names = set([field.name for field in response.attributes._fields])
+    # In proto3, HasField() does not work for scalar fields, using ListFields() instead.
+    attribute_names = set([field.name for field,_ in response.attributes.ListFields()])
     assert 'total_capacity_gbps' in attribute_names
     assert abs(response.attributes.total_capacity_gbps - 200) < 1.e-12
     assert 'used_capacity_gbps' in attribute_names
@@ -152,9 +154,11 @@ def test_link(context_client : ContextClient) -> None:
     assert len(response.links[0].link_endpoint_ids) == 2
     assert len(response.links[0].link_endpoint_ids) == 2
     assert response.links[0].HasField('attributes')
-    assert response.links[0].attributes.HasField('total_capacity_gbps')
+    # In proto3, HasField() does not work for scalar fields, using ListFields() instead.
+    attribute_names = set([field.name for field,_ in response.links[0].attributes.ListFields()])
+    assert 'total_capacity_gbps' in attribute_names
     assert abs(response.links[0].attributes.total_capacity_gbps - 200) < 1.e-12
-    assert response.links[0].attributes.HasField('used_capacity_gbps')
+    assert 'used_capacity_gbps' in attribute_names
     assert abs(response.links[0].attributes.used_capacity_gbps - 50) < 1.e-12
 
     # ----- Check relation was created ---------------------------------------------------------------------------------
-- 
GitLab