From 2c82ef6d6c2bfbcb46ed69f002d07672c5081b9d Mon Sep 17 00:00:00 2001
From: gifrerenom <lluis.gifre@cttc.es>
Date: Tue, 16 May 2023 17:05:15 +0000
Subject: [PATCH] Service component:

- Implemented unitary test for gRPC method RecomputeConnections
---
 .../tests/descriptors_recompute_conns.json    | 239 ++++++++++++++++++
 .../tests/test_unitary_recompute_conns.py     | 101 ++++++++
 2 files changed, 340 insertions(+)
 create mode 100644 src/service/tests/descriptors_recompute_conns.json
 create mode 100644 src/service/tests/test_unitary_recompute_conns.py

diff --git a/src/service/tests/descriptors_recompute_conns.json b/src/service/tests/descriptors_recompute_conns.json
new file mode 100644
index 000000000..dd571ccb6
--- /dev/null
+++ b/src/service/tests/descriptors_recompute_conns.json
@@ -0,0 +1,239 @@
+{
+    "contexts": [
+        {"context_id": {"context_uuid": {"uuid": "admin"}}}
+    ],
+    "topologies": [
+        {"topology_id": {"context_id": {"context_uuid": {"uuid": "admin"}}, "topology_uuid": {"uuid": "admin"}}}
+    ],
+    "devices": [
+        {
+            "device_id": {"device_uuid": {"uuid": "R1"}}, "device_type": "emu-packet-router", "device_drivers": [0],
+            "device_endpoints": [], "device_operational_status": 1, "device_config": {"config_rules": [
+                {"action": 1, "custom": {"resource_key": "_connect/address", "resource_value": "127.0.0.1"}},
+                {"action": 1, "custom": {"resource_key": "_connect/port", "resource_value": "0"}},
+                {"action": 1, "custom": {"resource_key": "_connect/settings", "resource_value": {"endpoints": [
+                    {"sample_types": [], "type": "copper", "uuid": "1/1"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/2"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/3"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/4"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/5"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/6"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/1"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/2"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/3"}
+                ]}}}
+            ]}
+        },
+        {
+            "device_id": {"device_uuid": {"uuid": "R2"}}, "device_type": "emu-packet-router", "device_drivers": [0],
+            "device_endpoints": [], "device_operational_status": 1, "device_config": {"config_rules": [
+                {"action": 1, "custom": {"resource_key": "_connect/address", "resource_value": "127.0.0.1"}},
+                {"action": 1, "custom": {"resource_key": "_connect/port", "resource_value": "0"}},
+                {"action": 1, "custom": {"resource_key": "_connect/settings", "resource_value": {"endpoints": [
+                    {"sample_types": [], "type": "copper", "uuid": "1/1"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/2"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/3"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/4"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/5"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/6"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/1"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/2"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/3"}
+                ]}}}
+            ]}
+        },
+        {
+            "device_id": {"device_uuid": {"uuid": "R3"}}, "device_type": "emu-packet-router", "device_drivers": [0],
+            "device_endpoints": [], "device_operational_status": 1, "device_config": {"config_rules": [
+                {"action": 1, "custom": {"resource_key": "_connect/address", "resource_value": "127.0.0.1"}},
+                {"action": 1, "custom": {"resource_key": "_connect/port", "resource_value": "0"}},
+                {"action": 1, "custom": {"resource_key": "_connect/settings", "resource_value": {"endpoints": [
+                    {"sample_types": [], "type": "copper", "uuid": "1/1"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/2"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/3"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/4"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/5"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/6"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/1"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/2"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/3"}
+                ]}}}
+            ]}
+        },
+        {
+            "device_id": {"device_uuid": {"uuid": "R4"}}, "device_type": "emu-packet-router", "device_drivers": [0],
+            "device_endpoints": [], "device_operational_status": 1, "device_config": {"config_rules": [
+                {"action": 1, "custom": {"resource_key": "_connect/address", "resource_value": "127.0.0.1"}},
+                {"action": 1, "custom": {"resource_key": "_connect/port", "resource_value": "0"}},
+                {"action": 1, "custom": {"resource_key": "_connect/settings", "resource_value": {"endpoints": [
+                    {"sample_types": [], "type": "copper", "uuid": "1/1"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/2"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/3"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/4"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/5"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/6"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/1"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/2"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/3"}
+                ]}}}
+            ]}
+        },
+        {
+            "device_id": {"device_uuid": {"uuid": "R5"}}, "device_type": "emu-packet-router", "device_drivers": [0],
+            "device_endpoints": [], "device_operational_status": 1, "device_config": {"config_rules": [
+                {"action": 1, "custom": {"resource_key": "_connect/address", "resource_value": "127.0.0.1"}},
+                {"action": 1, "custom": {"resource_key": "_connect/port", "resource_value": "0"}},
+                {"action": 1, "custom": {"resource_key": "_connect/settings", "resource_value": {"endpoints": [
+                    {"sample_types": [], "type": "copper", "uuid": "1/1"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/2"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/3"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/4"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/5"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/6"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/1"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/2"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/3"}
+                ]}}}
+            ]}
+        },
+        {
+            "device_id": {"device_uuid": {"uuid": "R6"}}, "device_type": "emu-packet-router", "device_drivers": [0],
+            "device_endpoints": [], "device_operational_status": 1, "device_config": {"config_rules": [
+                {"action": 1, "custom": {"resource_key": "_connect/address", "resource_value": "127.0.0.1"}},
+                {"action": 1, "custom": {"resource_key": "_connect/port", "resource_value": "0"}},
+                {"action": 1, "custom": {"resource_key": "_connect/settings", "resource_value": {"endpoints": [
+                    {"sample_types": [], "type": "copper", "uuid": "1/1"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/2"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/3"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/4"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/5"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/6"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/1"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/2"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/3"}
+                ]}}}
+            ]}
+        },
+        {
+            "device_id": {"device_uuid": {"uuid": "R7"}}, "device_type": "emu-packet-router", "device_drivers": [0],
+            "device_endpoints": [], "device_operational_status": 1, "device_config": {"config_rules": [
+                {"action": 1, "custom": {"resource_key": "_connect/address", "resource_value": "127.0.0.1"}},
+                {"action": 1, "custom": {"resource_key": "_connect/port", "resource_value": "0"}},
+                {"action": 1, "custom": {"resource_key": "_connect/settings", "resource_value": {"endpoints": [
+                    {"sample_types": [], "type": "copper", "uuid": "1/1"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/2"},
+                    {"sample_types": [], "type": "copper", "uuid": "1/3"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/1"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/2"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/3"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/4"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/5"},
+                    {"sample_types": [], "type": "copper", "uuid": "2/6"}
+                ]}}}
+            ]}
+        }
+    ],
+    "links": [
+        {"link_id": {"link_uuid": {"uuid": "R1==R2"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R1"}}, "endpoint_uuid": {"uuid": "2/1"}},
+            {"device_id": {"device_uuid": {"uuid": "R2"}}, "endpoint_uuid": {"uuid": "2/2"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R1==R6"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R1"}}, "endpoint_uuid": {"uuid": "2/2"}},
+            {"device_id": {"device_uuid": {"uuid": "R6"}}, "endpoint_uuid": {"uuid": "2/1"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R1==R7"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R1"}}, "endpoint_uuid": {"uuid": "2/3"}},
+            {"device_id": {"device_uuid": {"uuid": "R7"}}, "endpoint_uuid": {"uuid": "2/1"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R2==R1"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R2"}}, "endpoint_uuid": {"uuid": "2/2"}},
+            {"device_id": {"device_uuid": {"uuid": "R1"}}, "endpoint_uuid": {"uuid": "2/1"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R2==R3"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R2"}}, "endpoint_uuid": {"uuid": "2/1"}},
+            {"device_id": {"device_uuid": {"uuid": "R3"}}, "endpoint_uuid": {"uuid": "2/2"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R3==R2"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R3"}}, "endpoint_uuid": {"uuid": "2/2"}},
+            {"device_id": {"device_uuid": {"uuid": "R2"}}, "endpoint_uuid": {"uuid": "2/1"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R3==R4"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R3"}}, "endpoint_uuid": {"uuid": "2/1"}},
+            {"device_id": {"device_uuid": {"uuid": "R4"}}, "endpoint_uuid": {"uuid": "2/2"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R3==R7"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R3"}}, "endpoint_uuid": {"uuid": "2/3"}},
+            {"device_id": {"device_uuid": {"uuid": "R7"}}, "endpoint_uuid": {"uuid": "2/3"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R4==R3"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R4"}}, "endpoint_uuid": {"uuid": "2/2"}},
+            {"device_id": {"device_uuid": {"uuid": "R3"}}, "endpoint_uuid": {"uuid": "2/1"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R4==R5"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R4"}}, "endpoint_uuid": {"uuid": "2/1"}},
+            {"device_id": {"device_uuid": {"uuid": "R5"}}, "endpoint_uuid": {"uuid": "2/2"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R5==R4"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R5"}}, "endpoint_uuid": {"uuid": "2/2"}},
+            {"device_id": {"device_uuid": {"uuid": "R4"}}, "endpoint_uuid": {"uuid": "2/1"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R5==R6"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R5"}}, "endpoint_uuid": {"uuid": "2/1"}},
+            {"device_id": {"device_uuid": {"uuid": "R6"}}, "endpoint_uuid": {"uuid": "2/2"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R5==R7"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R5"}}, "endpoint_uuid": {"uuid": "2/3"}},
+            {"device_id": {"device_uuid": {"uuid": "R7"}}, "endpoint_uuid": {"uuid": "2/5"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R6==R1"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R6"}}, "endpoint_uuid": {"uuid": "2/1"}},
+            {"device_id": {"device_uuid": {"uuid": "R1"}}, "endpoint_uuid": {"uuid": "2/2"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R6==R5"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R6"}}, "endpoint_uuid": {"uuid": "2/2"}},
+            {"device_id": {"device_uuid": {"uuid": "R5"}}, "endpoint_uuid": {"uuid": "2/1"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R7==R1"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R7"}}, "endpoint_uuid": {"uuid": "2/1"}},
+            {"device_id": {"device_uuid": {"uuid": "R1"}}, "endpoint_uuid": {"uuid": "2/3"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R7==R3"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R7"}}, "endpoint_uuid": {"uuid": "2/3"}},
+            {"device_id": {"device_uuid": {"uuid": "R3"}}, "endpoint_uuid": {"uuid": "2/3"}}
+        ]},
+        {"link_id": {"link_uuid": {"uuid": "R7==R5"}}, "link_endpoint_ids": [
+            {"device_id": {"device_uuid": {"uuid": "R7"}}, "endpoint_uuid": {"uuid": "2/5"}},
+            {"device_id": {"device_uuid": {"uuid": "R5"}}, "endpoint_uuid": {"uuid": "2/3"}}
+        ]}
+    ],
+    "services": [
+        {
+            "service_id": {
+                "context_id": {"context_uuid": {"uuid": "admin"}}, "service_uuid": {"uuid": "test-svc"}
+            },
+            "service_type": 2,
+            "service_status": {"service_status": 1},
+            "service_endpoint_ids": [
+                {"device_id":{"device_uuid":{"uuid":"R1"}},"endpoint_uuid":{"uuid":"1/1"}},
+                {"device_id":{"device_uuid":{"uuid":"R4"}},"endpoint_uuid":{"uuid":"1/1"}}
+            ],
+            "service_constraints": [
+                {"sla_capacity": {"capacity_gbps": 10.0}},
+                {"sla_latency": {"e2e_latency_ms": 15.2}}
+            ],
+            "service_config": {"config_rules": [
+                {"action": 1, "custom": {"resource_key": "/settings", "resource_value": {
+                    "address_families": ["IPV4"], "bgp_as": 65000, "bgp_route_target": "65000:123",
+                    "mtu": 1512, "vlan_id": 111
+                }}},
+                {"action": 1, "custom": {"resource_key": "/device[R1]/endpoint[1/1]/settings", "resource_value": {
+                    "sub_interface_index": 0, "vlan_id": 111
+                }}},
+                {"action": 1, "custom": {"resource_key": "/device[R4]/endpoint[1/1]/settings", "resource_value": {
+                    "sub_interface_index": 0, "vlan_id": 111
+                }}}
+            ]}
+        }
+    ]
+}
diff --git a/src/service/tests/test_unitary_recompute_conns.py b/src/service/tests/test_unitary_recompute_conns.py
new file mode 100644
index 000000000..3c87eae9e
--- /dev/null
+++ b/src/service/tests/test_unitary_recompute_conns.py
@@ -0,0 +1,101 @@
+# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import logging, pytest
+from common.Constants import DEFAULT_CONTEXT_NAME
+from common.proto.context_pb2 import ContextId, Service
+from common.tools.descriptor.Loader import DescriptorLoader, check_descriptor_load_results, validate_empty_scenario
+from common.tools.grpc.Tools import grpc_message_to_json_string
+from common.tools.object_factory.Context import json_context_id
+from context.client.ContextClient import ContextClient
+from device.client.DeviceClient import DeviceClient
+from service.client.ServiceClient import ServiceClient
+
+LOGGER = logging.getLogger(__name__)
+LOGGER.setLevel(logging.DEBUG)
+
+DESCRIPTOR_FILE = 'src/service/tests/descriptors_recompute_conns.json'
+ADMIN_CONTEXT_ID = ContextId(**json_context_id(DEFAULT_CONTEXT_NAME))
+
+@pytest.fixture(scope='session')
+def context_client():
+    _client = ContextClient()
+    yield _client
+    _client.close()
+
+@pytest.fixture(scope='session')
+def device_client():
+    _client = DeviceClient()
+    yield _client
+    _client.close()
+
+@pytest.fixture(scope='session')
+def service_client():
+    _client = ServiceClient()
+    yield _client
+    _client.close()
+
+
+def test_service_recompute_connection(
+    context_client : ContextClient, # pylint: disable=redefined-outer-name
+    device_client  : DeviceClient,  # pylint: disable=redefined-outer-name
+    service_client : ServiceClient, # pylint: disable=redefined-outer-name
+) -> None:
+    
+    # ===== Setup scenario =============================================================================================
+    validate_empty_scenario(context_client)
+
+    # Load descriptors and validate the base scenario
+    descriptor_loader = DescriptorLoader(
+        descriptors_file=DESCRIPTOR_FILE, context_client=context_client, device_client=device_client,
+        service_client=service_client)
+    results = descriptor_loader.process()
+    check_descriptor_load_results(results, descriptor_loader)
+    descriptor_loader.validate()
+
+
+    # ===== Recompute Connection =======================================================================================
+    response = context_client.ListServices(ADMIN_CONTEXT_ID)
+    LOGGER.info('Services[{:d}] = {:s}'.format(len(response.services), grpc_message_to_json_string(response)))
+    assert len(response.services) == 1
+    service = response.services[0]
+    service_id = service.service_id
+
+    response = context_client.ListConnections(service_id)
+    LOGGER.info('  ServiceId[{:s}] => Connections[{:d}] = {:s}'.format(
+        grpc_message_to_json_string(service_id), len(response.connections), grpc_message_to_json_string(response)))
+    assert len(response.connections) == 1 # 1 connection per service
+    str_old_connections = grpc_message_to_json_string(response)
+
+    request = Service()
+    request.CopyFrom(service)
+    del request.service_endpoint_ids[:]         # pylint: disable=no-member
+    del request.service_constraints[:]          # pylint: disable=no-member
+    del request.service_config.config_rules[:]  # pylint: disable=no-member
+    service_client.RecomputeConnections(request)
+
+    response = context_client.ListConnections(service_id)
+    LOGGER.info('  ServiceId[{:s}] => Connections[{:d}] = {:s}'.format(
+        grpc_message_to_json_string(service_id), len(response.connections), grpc_message_to_json_string(response)))
+    assert len(response.connections) == 1 # 1 connection per service
+    str_new_connections = grpc_message_to_json_string(response)
+
+    assert str_old_connections != str_new_connections
+
+
+    # ===== Cleanup scenario ===========================================================================================
+    # Validate and unload the base scenario
+    descriptor_loader.validate()
+    descriptor_loader.unload()
+    validate_empty_scenario(context_client)
-- 
GitLab