diff options
author | sheetal ghadge <sheetal.ghadge@t-systems.com> | 2024-01-09 12:31:51 +0530 |
---|---|---|
committer | sheetal ghadge <sheetal.ghadge@t-systems.com> | 2024-01-09 12:32:18 +0530 |
commit | 38a8f5e676c267e91fb017c40de96dd598be88cf (patch) | |
tree | dc082bf8c3fac2bb486c92ab202dba94e0ee620a | |
parent | 042cdc1e6310cddd92f0e60ffc840917a207b570 (diff) |
change to delete service instance from aai:Updated service.py & nbi.py module
Issue-ID: TEST-413
Change-Id: If2e670e9ab3e819099376148620b47b36adb051d
Signed-off-by: sheetal ghadge <sheetal.ghadge@t-systems.com>
-rw-r--r-- | src/onapsdk/aai/business/service.py | 54 | ||||
-rw-r--r-- | src/onapsdk/nbi/nbi.py | 12 | ||||
-rw-r--r-- | tests/test_aai_service_instance.py | 28 | ||||
-rw-r--r-- | tests/test_nbi.py | 12 |
4 files changed, 80 insertions, 26 deletions
diff --git a/src/onapsdk/aai/business/service.py b/src/onapsdk/aai/business/service.py index 9c2e7f8..d0f7876 100644 --- a/src/onapsdk/aai/business/service.py +++ b/src/onapsdk/aai/business/service.py @@ -131,10 +131,10 @@ class ServiceInstance(Instance): # pylint: disable=too-many-instance-attributes def _get_related_instance(self, related_instance_class: Union[Type[NetworkInstance], - Type[VnfInstance]], - relationship_related_to_type: str) -> Iterator[\ - Union[NetworkInstance, - VnfInstance]]: + Type[VnfInstance]], + relationship_related_to_type: str) -> Iterator[ \ + Union[NetworkInstance, + VnfInstance]]: """Iterate through related service instances. This is method which for given `relationship_related_to_type` creates iterator @@ -161,7 +161,7 @@ class ServiceInstance(Instance): # pylint: disable=too-many-instance-attributes raise ParameterError(msg) for relationship in self.relationships: if relationship.related_to == relationship_related_to_type: - yield related_instance_class.create_from_api_response(\ + yield related_instance_class.create_from_api_response( \ self.send_message_json("GET", (f"Get {self.instance_id} " f"{related_instance_class.__class__.__name__}"), @@ -169,7 +169,8 @@ class ServiceInstance(Instance): # pylint: disable=too-many-instance-attributes self) @classmethod - def create(cls, service_subscription: "ServiceSubscription", # NOSONAR # pylint: disable=too-many-arguments, too-many-locals + def create(cls, service_subscription: "ServiceSubscription", + # NOSONAR # pylint: disable=too-many-arguments, too-many-locals instance_id: str, instance_name: str = None, service_type: str = None, @@ -256,9 +257,9 @@ class ServiceInstance(Instance): # pylint: disable=too-many-instance-attributes input_parameters ) cls.send_message("PUT", - f"Create service instance {instance_id} for "\ + f"Create service instance {instance_id} for " \ f"{service_subscription.service_type} service subscription", - f"{service_subscription.url}/service-instances/service-instance/"\ + f"{service_subscription.url}/service-instances/service-instance/" \ f"{instance_id}", data=jinja_env() .get_template("aai_service_instance_create.json.j2") @@ -526,3 +527,40 @@ class ServiceInstance(Instance): # pylint: disable=too-many-instance-attributes """ self._logger.debug("Delete %s service instance", self.instance_id) return ServiceDeletionRequest.send_request(self, a_la_carte) + + def delete_from_aai(self, + service_subscription: "ServiceSubscription", + instance_id: str, + service_type: str = None) -> "ServiceInstance": + """Send request to AAI to delete service instance. + + Args: + service_subscription (ServiceSubscription): service subscription which is belongs to + instance_id (str): Uniquely identifies this instance of a service + service_type (str, optional): String capturing type of service.Defaults to None. + + """ + customer_instance = service_subscription.customer + customer_id = customer_instance.global_customer_id + + # calling GET api to get resource_version of service instance + response = self.send_message_json("GET", + f"GET service instance {instance_id} for ", + f"{self.base_url}{self.api_version}/business/" + f"customers/customer/" + f"{customer_id}/service-subscriptions/" + f"service-subscription/" + f"{service_type}/service-instances/service-instance/" + f"{instance_id}") + + resource_version = str(response.get('resource-version', '')) + + # calling delete api to delete service instance from AAI + self.send_message("DELETE", + f"Delete service instance {instance_id} for ", + f"{self.base_url}{self.api_version}/business/customers/customer/" + f"{customer_id}/service-subscriptions/" + f"service-subscription/" + f"{service_type}/service-instances/service-instance/" + f"{instance_id}?" + f"resource-version={resource_version}") diff --git a/src/onapsdk/nbi/nbi.py b/src/onapsdk/nbi/nbi.py index ffcd62c..3af85f6 100644 --- a/src/onapsdk/nbi/nbi.py +++ b/src/onapsdk/nbi/nbi.py @@ -170,9 +170,9 @@ class Service(Nbi): super().__init__() self.name: str = name self.service_id: str = service_id - self._service_specification_name: str = service_specification_name + self.service_specification_name: str = service_specification_name self._service_specification_id: str = service_specification_id - self._customer_id: str = customer_id + self.customer_id: str = customer_id self.customer_role: str = customer_role self.href: str = href @@ -185,8 +185,8 @@ class Service(Nbi): """ return (f"Service(name={self.name}, service_id={self.service_id}, " f"service_specification_id={self._service_specification_id}, " - f"service_specification_name={self._service_specification_name}, " - f"customer_id={self._customer_id}, " + f"service_specification_name={self.service_specification_name}, " + f"customer_id={self.customer_id}, " f"customer_role={self.customer_role}, " f"href={self.href})") @@ -223,9 +223,9 @@ class Service(Nbi): Customer: Customer object """ - if not self._customer_id: + if not self.customer_id: return None - return Customer.get_by_global_customer_id(self._customer_id) + return Customer.get_by_global_customer_id(self.customer_id) @property def service_specification(self) -> Optional[ServiceSpecification]: diff --git a/tests/test_aai_service_instance.py b/tests/test_aai_service_instance.py index 9f84dfd..3b758d8 100644 --- a/tests/test_aai_service_instance.py +++ b/tests/test_aai_service_instance.py @@ -21,7 +21,6 @@ from onapsdk.so.deletion import ServiceDeletionRequest from onapsdk.so.instantiation import NetworkInstantiation, VnfInstantiation from onapsdk.exceptions import StatusError - RELATIONSHIPS_VNF = { "relationship": [ { @@ -33,7 +32,6 @@ RELATIONSHIPS_VNF = { ] } - RELATIONSHIPS_NETWORK = { "relationship": [ { @@ -45,7 +43,6 @@ RELATIONSHIPS_NETWORK = { ] } - NETWORK_INSTANCE = { 'network-id': '49dab38b-3a5b-47e5-9cd6-b8d069d6109d', 'network-name': 'Python_ONAP_SDK_network_instance_0b4308ca-3fe0-4af1-9c4e-ed2c816b9496', @@ -157,11 +154,10 @@ NETWORK_INSTANCE = { } } - COUNT = { - "results":[ + "results": [ { - "service-instance":29 + "service-instance": 29 } ] } @@ -269,7 +265,27 @@ def test_service_instance_sdc_service(mock_service_get_by_unique_uuid): si.sdc_service mock_service_get_by_unique_uuid.assert_called_once_with("1234") + @mock.patch.object(ServiceInstance, "send_message_json") def test_service_instance_count(mock_send_message_json): mock_send_message_json.return_value = COUNT assert ServiceInstance.count(service_subscription=mock.MagicMock()) + + +@mock.patch.object(ServiceInstance, "send_message") +def test_service_deletion_request(mock_service_deletion_send_message): + mock_instance = mock.MagicMock() + mock_instance.instance_id = "test_instance_id" + + mock_service_subscription = mock.MagicMock() + mock_instance.service_subscription = mock_service_subscription + + mock_service_type = mock.MagicMock() + mock_instance.service_type = "test_service_type" + + mock_self = mock.MagicMock() + + service_instance_deletion = ServiceInstance. \ + delete_from_aai(self=mock_self, instance_id=mock_instance, + service_type=mock_service_type, + service_subscription=mock_service_subscription) diff --git a/tests/test_nbi.py b/tests/test_nbi.py index 66b3e93..97aef13 100644 --- a/tests/test_nbi.py +++ b/tests/test_nbi.py @@ -364,16 +364,16 @@ def test_service_get_all(mock_service_specification_get_by_id, assert service.name == "test6" assert service.service_id == "5c855390-7c39-4fe4-b164-2029b09de57c" - assert service._service_specification_name == "testService9" + assert service.service_specification_name == "testService9" assert service._service_specification_id == "125727ad-8660-423e-b4a1-99cd4a749f45" - assert service._customer_id == "generic" + assert service.customer_id == "generic" assert service.customer_role == "ONAPcustomer" assert service.href == "service/5c855390-7c39-4fe4-b164-2029b09de57c" assert service.customer is not None - mock_customer_get_by_id.assert_called_once_with(service._customer_id) + mock_customer_get_by_id.assert_called_once_with(service.customer_id) - service._customer_id = None + service.customer_id = None assert service.customer is None assert service.service_specification is not None @@ -390,9 +390,9 @@ def test_service_get_all(mock_service_specification_get_by_id, assert service.name == "test7" assert service.service_id == "6a855390-7c39-4fe4-b164-2029b09de57d" - assert service._service_specification_name == "testService9" + assert service.service_specification_name == "testService9" assert service._service_specification_id == "125727ad-8660-423e-b4a1-99cd4a749f45" - assert service._customer_id == "test_customer" + assert service.customer_id == "test_customer" assert service.customer_role == "ONAPcustomer" assert service.href == "service/6a855390-7c39-4fe4-b164-2029b09de57d" |