5 from onapsdk.aai.cloud_infrastructure import CloudRegion, Tenant
6 from onapsdk.aai.business import Customer, ServiceInstance, ServiceSubscription
7 from onapsdk.aai.business.owning_entity import OwningEntity as AaiOwningEntity
8 from onapsdk.configuration import settings
9 from onapsdk.sdc.service import Service
10 from onapsdk.so.instantiation import ServiceInstantiation
11 from onapsdk.vid import Project
13 import onaptests.utils.exceptions as onap_test_exceptions
14 from ..base import BaseStep, YamlTemplateBaseStep
15 from ..cloud.connect_service_subscription_to_cloud_region import ConnectServiceSubToCloudRegionStep
16 from ..onboard.service import ServiceOnboardStep, YamlTemplateServiceOnboardStep
19 class ServiceAlaCarteInstantiateStep(BaseStep):
20 """Instantiate service a'la carte."""
22 def __init__(self, cleanup=False):
27 - ConnectServiceSubToCloudRegionStep.
29 super().__init__(cleanup=cleanup)
30 if not settings.ONLY_INSTANTIATE:
31 self.add_step(ServiceOnboardStep(cleanup))
32 self.add_step(ConnectServiceSubToCloudRegionStep(cleanup))
36 """Instantiate service.
41 - CLOUD_REGION_CLOUD_OWNER,
46 - SERVICE_INSTANCE_NAME.
49 service = Service(settings.SERVICE_NAME)
50 customer: Customer = Customer.get_by_global_customer_id(settings.GLOBAL_CUSTOMER_ID)
51 cloud_region: CloudRegion = CloudRegion.get_by_id(
52 cloud_owner=settings.CLOUD_REGION_CLOUD_OWNER,
53 cloud_region_id=settings.CLOUD_REGION_ID,
55 tenant: Tenant = cloud_region.get_tenant(settings.TENANT_ID)
57 owning_entity = AaiOwningEntity.get_by_owning_entity_name(
58 settings.OWNING_ENTITY)
60 self._logger.info("Owning entity not found, create it")
61 owning_entity = AaiOwningEntity.create(settings.OWNING_ENTITY)
62 vid_project = Project.create(settings.PROJECT)
64 service_instantiation = ServiceInstantiation.instantiate_so_ala_carte(
71 service_instance_name=settings.SERVICE_INSTANCE_NAME
73 while not service_instantiation.finished:
77 class YamlTemplateServiceAlaCarteInstantiateStep(YamlTemplateBaseStep):
78 """Instantiate service a'la carte using YAML template."""
80 def __init__(self, cleanup=False):
84 - YamlTemplateServiceOnboardStep,
85 - ConnectServiceSubToCloudRegionStep.
87 super().__init__(cleanup=cleanup)
88 self._yaml_template: dict = None
89 self._service_instance_name: str = None
90 self._service_instance: str = None
91 if not settings.ONLY_INSTANTIATE:
92 self.add_step(YamlTemplateServiceOnboardStep(cleanup))
93 self.add_step(ConnectServiceSubToCloudRegionStep(cleanup))
96 def yaml_template(self) -> dict:
97 """Step YAML template.
99 Load from file if it's a root step, get from parent otherwise.
102 dict: Step YAML template
106 if not self._yaml_template:
107 with open(settings.SERVICE_YAML_TEMPLATE, "r") as yaml_template:
108 self._yaml_template: dict = load(yaml_template)
109 return self._yaml_template
110 return self.parent.yaml_template
113 def service_name(self) -> str:
116 Get from YAML template if it's a root step, get from parent otherwise.
123 return next(iter(self.yaml_template.keys()))
124 return self.parent.service_name
127 def service_instance_name(self) -> str:
128 """Service instance name.
130 Generate using `service_name` and `uuid4()` function if it's a root step,
131 get from parent otherwise.
134 str: Service instance name
138 if not self._service_instance_name:
139 self._service_instance_name: str = f"{self.service_name}-{str(uuid4())}"
140 return self._service_instance_name
141 return self.parent.service_instance_name
143 @YamlTemplateBaseStep.store_state
145 """Instantiate service.
148 - GLOBAL_CUSTOMER_ID,
149 - CLOUD_REGION_CLOUD_OWNER,
156 Exception: Service instantiation failed
160 service = Service(self.service_name)
161 customer: Customer = Customer.get_by_global_customer_id(settings.GLOBAL_CUSTOMER_ID)
162 cloud_region: CloudRegion = CloudRegion.get_by_id(
163 cloud_owner=settings.CLOUD_REGION_CLOUD_OWNER,
164 cloud_region_id=settings.CLOUD_REGION_ID,
166 tenant: Tenant = cloud_region.get_tenant(settings.TENANT_ID)
168 owning_entity = AaiOwningEntity.get_by_owning_entity_name(
169 settings.OWNING_ENTITY)
171 self._logger.info("Owning entity not found, create it")
172 owning_entity = AaiOwningEntity.create(settings.OWNING_ENTITY)
173 vid_project = Project.create(settings.PROJECT)
175 # Before instantiating, be sure that the service has been distributed
176 self._logger.info("******** Check Service Distribution *******")
177 distribution_completed = False
180 while distribution_completed is False and nb_try < nb_try_max:
181 distribution_completed = service.distributed
182 if distribution_completed is True:
184 "Service Distribution for %s is sucessfully finished",
188 "Service Distribution for %s ongoing, Wait for 60 s",
193 if distribution_completed is False:
195 "Service Distribution for %s failed !!",service.name)
196 raise onap_test_exceptions.ServiceDistributionException
198 service_instantiation = ServiceInstantiation.instantiate_so_ala_carte(
205 service_instance_name=self.service_instance_name
207 while not service_instantiation.finished:
209 if service_instantiation.failed:
210 raise onap_test_exceptions.ServiceInstantiateException
212 service_subscription: ServiceSubscription = customer.get_service_subscription_by_service_type(self.service_name)
213 self._service_instance: ServiceInstance = service_subscription.get_service_instance_by_name(self.service_instance_name)
216 def cleanup(self) -> None:
220 Exception: Service cleaning failed
223 service_deletion = self._service_instance.delete()
226 while not service_deletion.finished and nb_try < nb_try_max:
227 self._logger.info("Wait for Service deletion")
230 if service_deletion.finished:
231 self._logger.info("Service %s deleted", self._service_instance_name)
233 self._logger.error("Service deletion %s failed", self._service_instance_name)
234 raise onap_test_exceptions.ServiceCleanupException