diff --git a/simulation-system/libs/csle-collector/src/csle_collector/client_manager/dao/arrival_config.py b/simulation-system/libs/csle-collector/src/csle_collector/client_manager/dao/arrival_config.py index d8367a54d..121d0d318 100644 --- a/simulation-system/libs/csle-collector/src/csle_collector/client_manager/dao/arrival_config.py +++ b/simulation-system/libs/csle-collector/src/csle_collector/client_manager/dao/arrival_config.py @@ -1,8 +1,9 @@ -from csle_collector.client_manager.dao.client_arrival_type import ClientArrivalType from typing import Dict, Any +from csle_collector.client_manager.dao.client_arrival_type import ClientArrivalType +from csle_base.json_serializable import JSONSerializable -class ArrivalConfig(): +class ArrivalConfig(JSONSerializable): """ Abstract arrival configuration class """ @@ -24,3 +25,28 @@ def to_dict(self) -> Dict[str, Any]: d = {} d["client_arrival_type"] = self.client_arrival_type return d + + @staticmethod + def from_dict(d: Dict[str, Any]) -> "ArrivalConfig": + """ + Converts a dict representation to an instance + + :param d: the dict to convert + :return: the created instance + """ + obj = ArrivalConfig(client_arrival_type=d["client_arrival_type"]) + return obj + + @staticmethod + def from_json_file(json_file_path: str) -> "ArrivalConfig": + """ + Reads a json file and converts it to a DTO + + :param json_file_path: the json file path + :return: the converted DTO + """ + import io + import json + with io.open(json_file_path, 'r') as f: + json_str = f.read() + return ArrivalConfig.from_dict(json.loads(json_str)) diff --git a/simulation-system/libs/csle-collector/src/csle_collector/client_manager/dao/constant_arrival_config.py b/simulation-system/libs/csle-collector/src/csle_collector/client_manager/dao/constant_arrival_config.py index 34f7881f2..14ddfd28f 100644 --- a/simulation-system/libs/csle-collector/src/csle_collector/client_manager/dao/constant_arrival_config.py +++ b/simulation-system/libs/csle-collector/src/csle_collector/client_manager/dao/constant_arrival_config.py @@ -45,21 +45,14 @@ def from_dict(d: Dict[str, Any]) -> "ConstantArrivalConfig": :param d: the dict to convert :return: the created instance """ - if "lamb" not in d: - obj = ConstantArrivalConfig(lamb=None) - return obj - # obj = ConstantArrivalConfig(lamb=1.0) - else: - obj = ConstantArrivalConfig(lamb=d["lamb"]) - return obj + obj = ConstantArrivalConfig(lamb=d["lamb"]) + return obj def to_grpc_object(self) -> csle_collector.client_manager.client_manager_pb2.ConstantArrivalConfigDTO: """ :return: a GRPC serializable version of the object """ - return csle_collector.client_manager.client_manager_pb2.ConstantArrivalConfigDTO( - lamb=self.lamb - ) + return csle_collector.client_manager.client_manager_pb2.ConstantArrivalConfigDTO(lamb=self.lamb) @staticmethod def from_grpc_object(obj: csle_collector.client_manager.client_manager_pb2.ConstantArrivalConfigDTO) \ diff --git a/simulation-system/libs/csle-common/src/csle_common/dao/emulation_config/network_service.py b/simulation-system/libs/csle-common/src/csle_common/dao/emulation_config/network_service.py index ae6500fd2..7bee4e235 100644 --- a/simulation-system/libs/csle-common/src/csle_common/dao/emulation_config/network_service.py +++ b/simulation-system/libs/csle-common/src/csle_common/dao/emulation_config/network_service.py @@ -31,8 +31,7 @@ def __str__(self) -> str: cr = [] if self.credentials is not None: cr = list(map(lambda x: str(x), self.credentials)) - return "protocol:{}, port:{}, name:{}, credentials: {}".format(self.protocol, self.port, self.name, - cr) + return f"protocol: {self.protocol}, port: {self.port}, name: {self.name}, cr: {cr}" def to_dict(self) -> Dict[str, Any]: """ @@ -57,21 +56,17 @@ def from_dict(d: Dict[str, Any]) -> "NetworkService": :return: a dto representation of the object """ + credentials = None if d["credentials"] is not None: - dto = NetworkService(name=d["name"], port=d["port"], protocol=d["protocol"], - credentials=list(map(lambda x: Credential.from_dict(x), d["credentials"]))) - else: - dto = NetworkService(name=d["name"], port=d["port"], protocol=d["protocol"], - credentials=None) + credentials = list(map(lambda x: Credential.from_dict(x), d["credentials"])) + dto = NetworkService(name=d["name"], port=d["port"], protocol=d["protocol"], credentials=credentials) return dto def copy(self) -> "NetworkService": """ :return: a copy of the DTO """ - return NetworkService( - protocol=self.protocol, port=self.port, name=self.name, credentials=self.credentials - ) + return NetworkService(protocol=self.protocol, port=self.port, name=self.name, credentials=self.credentials) @staticmethod def pw_vuln_services(): diff --git a/simulation-system/libs/csle-rest-api/src/csle_rest_api/resources/emulations/routes.py b/simulation-system/libs/csle-rest-api/src/csle_rest_api/resources/emulations/routes.py index 65e6798b4..c967ffcc0 100644 --- a/simulation-system/libs/csle-rest-api/src/csle_rest_api/resources/emulations/routes.py +++ b/simulation-system/libs/csle-rest-api/src/csle_rest_api/resources/emulations/routes.py @@ -117,7 +117,7 @@ def emulation_by_id(emulation_id: int) -> Tuple[Response, int]: """ requires_admin = False if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_DELETE or \ - request.method == api_constants.MGMT_WEBAPP.HTTP_REST_POST: + request.method == api_constants.MGMT_WEBAPP.HTTP_REST_POST: requires_admin = True authorized = rest_api_util.check_if_user_is_authorized(request=request, requires_admin=requires_admin) @@ -129,8 +129,7 @@ def emulation_by_id(emulation_id: int) -> Tuple[Response, int]: config = MetastoreFacade.get_config(id=1) for node in config.cluster_config.cluster_nodes: running_emulation_names = running_emulation_names + list(ClusterController.list_all_running_emulations( - ip=node.ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT - ).runningEmulations) + ip=node.ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT).runningEmulations) em_dict = {} if em is not None: if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_GET or \ @@ -140,17 +139,16 @@ def emulation_by_id(emulation_id: int) -> Tuple[Response, int]: em.running = True for exec in executions: exec.emulation_env_config.running = True - if MetastoreFacade.get_emulation_image(emulation_name=em.name) is not None: - em_name, img = MetastoreFacade.get_emulation_image(emulation_name=em.name) + em_img = MetastoreFacade.get_emulation_image(emulation_name=em.name) + if em_img is not None: + em_name, img = em_img em.image = base64.b64encode(img).decode() for exec in executions: exec.emulation_env_config.image = base64.b64encode(img).decode() else: - img = img = MetastoreFacade.get_emulation_image(emulation_name=em.name) - em.image = img + em.image = "" for exec in executions: - exec.emulation_env_config.image = img - + exec.emulation_env_config.image = "" em_dict = em.to_dict() em_dict[api_constants.MGMT_WEBAPP.EXECUTIONS_SUBRESOURCE] = list(map(lambda x: x.to_dict(), executions)) if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_POST: @@ -222,6 +220,8 @@ def get_execution_of_emulation(emulation_id: int, execution_id: int) -> Tuple[Re requires_admin = True elif request.method == api_constants.MGMT_WEBAPP.HTTP_REST_GET: requires_admin = False + else: + raise ValueError(f"HTTP method: {request.method} not supported") authorized = rest_api_util.check_if_user_is_authorized(request=request, requires_admin=requires_admin) if authorized is not None: return authorized @@ -258,8 +258,7 @@ def monitor_emulation(emulation_id: int, execution_id: int, minutes: int) -> Tup :param minutes: the number of minutes past to collect data from :return: the collected data """ - authorized = rest_api_util.check_if_user_is_authorized(request=request, - requires_admin=False) + authorized = rest_api_util.check_if_user_is_authorized(request=request, requires_admin=False) if authorized is not None: return authorized @@ -274,8 +273,7 @@ def monitor_emulation(emulation_id: int, execution_id: int, minutes: int) -> Tup time_series = ClusterController.get_execution_time_series_data( ip=execution.emulation_env_config.kafka_config.container.physical_host_ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, minutes=minutes, - ip_first_octet=execution.ip_first_octet, emulation=execution.emulation_env_config.name - ) + ip_first_octet=execution.ip_first_octet, emulation=execution.emulation_env_config.name) time_series = time_series.to_dict() response = jsonify(time_series) response.headers.add(api_constants.MGMT_WEBAPP.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*") diff --git a/simulation-system/libs/csle-rest-api/tests/test_resources_docker.py b/simulation-system/libs/csle-rest-api/tests/test_resources_docker.py index 7b98bdddd..f3a9d6477 100644 --- a/simulation-system/libs/csle-rest-api/tests/test_resources_docker.py +++ b/simulation-system/libs/csle-rest-api/tests/test_resources_docker.py @@ -1,21 +1,16 @@ import json - import csle_common.constants.constants as constants import pytest import pytest_mock -from csle_cluster.cluster_manager.cluster_manager_pb2 import ( - NodeStatusDTO, - ServiceStatusDTO, -) +from csle_cluster.cluster_manager.cluster_manager_pb2 import NodeStatusDTO, ServiceStatusDTO from csle_common.dao.emulation_config.config import Config - import csle_rest_api.constants.constants as api_constants from csle_rest_api.rest_api import create_app class TestResourcesDockerStatusSuite: """ - Test suite for /cluster_status url + Test suite for /docker url """ @pytest.fixture @@ -28,7 +23,7 @@ def flask_app(self): return create_app(static_folder="../../../../../management-system/csle-mgmt-webapp/build") @pytest.fixture - def config(self, mocker, example_config: Config): + def config(self, mocker: pytest_mock.MockFixture, example_config: Config): """ Pytest fixture for mocking the get_config function diff --git a/simulation-system/libs/csle-rest-api/tests/test_resources_emulations.py b/simulation-system/libs/csle-rest-api/tests/test_resources_emulations.py index 2557be36f..6abd29c9f 100644 --- a/simulation-system/libs/csle-rest-api/tests/test_resources_emulations.py +++ b/simulation-system/libs/csle-rest-api/tests/test_resources_emulations.py @@ -230,9 +230,9 @@ def emulations_ids_in_names(self, mocker: pytest_mock.MockFixture): :param mocker: the pytest mocker object :return: The mocked function """ - def list_emulations_ids() -> List[Tuple]: - list_dict = [(10, "a")] - return list_dict + def list_emulations_ids() -> List[Tuple[int, str]]: + list_tuple = [(10, "a")] + return list_tuple list_emulations_ids_mocker = mocker.MagicMock(side_effect=list_emulations_ids ) return list_emulations_ids_mocker @@ -333,9 +333,9 @@ def emulations_ids_not_in_names(self, mocker: pytest_mock.MockFixture): :param mocker: the pytest mocker object :return: The mocked function """ - def list_emulations_ids() -> List[Tuple]: - list_dict = [(10, "q")] - return list_dict + def list_emulations_ids() -> List[Tuple[int, str]]: + list_tuple = [(10, "q")] + return list_tuple list_emulations_ids_mocker = mocker.MagicMock(side_effect=list_emulations_ids ) return list_emulations_ids_mocker