2 from typing import Iterable
6 from onapsdk.aai.cloud_infrastructure import CloudRegion, Tenant
7 from onapsdk.aai.business import Customer, ServiceInstance, ServiceSubscription
8 from onapsdk.configuration import settings
9 from onapsdk.so.instantiation import VnfParameter
11 import onaptests.utils.exceptions as onap_test_exceptions
12 from ..base import YamlTemplateBaseStep
13 from .vnf_ala_carte import YamlTemplateVnfAlaCarteInstantiateStep
15 class YamlTemplateVfModuleAlaCarteInstantiateStep(YamlTemplateBaseStep):
16 """Instantiate vf module a'la carte using YAML template."""
18 def __init__(self, cleanup=False):
22 - YamlTemplateVnfAlaCarteInstantiateStep.
24 super().__init__(cleanup=cleanup)
26 self._yaml_template: dict = None
27 self._service_instance_name: str = None
28 self._service_instance: ServiceInstance = None
29 self.add_step(YamlTemplateVnfAlaCarteInstantiateStep(cleanup))
32 def yaml_template(self) -> dict:
33 """Step YAML template.
35 Load from file if it's a root step, get from parent otherwise.
38 dict: Step YAML template
42 if not self._yaml_template:
43 with open(settings.SERVICE_YAML_TEMPLATE, "r") as yaml_template:
44 self._yaml_template: dict = load(yaml_template)
45 return self._yaml_template
46 return self.parent.yaml_template
49 def service_name(self) -> str:
52 Get from YAML template if it's a root step, get from parent otherwise.
59 return next(iter(self.yaml_template.keys()))
60 return self.parent.service_name
63 def service_instance_name(self) -> str:
64 """Service instance name.
66 Generate using `service_name` and `uuid4()` function if it's a root step,
67 get from parent otherwise.
70 str: Service instance name
74 if not self._service_instance_name:
75 self._service_instance_name: str = f"{self.service_name}-{str(uuid4())}"
76 return self._service_instance_name
77 return self.parent.service_instance_name
79 def get_vnf_parameters(self, vnf_name: str) -> Iterable[VnfParameter]:
80 """Get VNF parameters from YAML template.
83 vnf_name (str): VNF name to get parameters for.
86 Iterator[Iterable[VnfParameter]]: VNF parameter
90 # workaround, as VNF name differs from model name (added " 0")
91 vnf_name=vnf_name.split()[0]
92 for vnf in self.yaml_template[self.service_name]["vnfs"]:
93 if vnf["vnf_name"] == vnf_name:
94 for vnf_parameter in vnf["vnf_parameters"]:
96 name=vnf_parameter["name"],
97 value=vnf_parameter["value"]
100 @YamlTemplateBaseStep.store_state
101 def execute(self) -> None:
102 """Instantiate Vf module.
105 - GLOBAL_CUSTOMER_ID.
108 Exception: Vf module instantiation failed
112 customer: Customer = Customer.get_by_global_customer_id(settings.GLOBAL_CUSTOMER_ID)
113 service_subscription: ServiceSubscription = customer.get_service_subscription_by_service_type(self.service_name)
114 self._service_instance: ServiceInstance = service_subscription.get_service_instance_by_name(self.service_instance_name)
115 cloud_region: CloudRegion = CloudRegion.get_by_id(
116 cloud_owner=settings.CLOUD_REGION_CLOUD_OWNER,
117 cloud_region_id=settings.CLOUD_REGION_ID,
119 tenant: Tenant = cloud_region.get_tenant(settings.TENANT_ID)
121 for vnf_instance in self._service_instance.vnf_instances:
122 # possible to have several moduels for 1 VNF
123 for vf_module in vnf_instance.vnf.vf_modules:
124 vf_module_instantiation = vnf_instance.add_vf_module(
128 self._service_instance_name,
129 vnf_parameters= self.get_vnf_parameters(vnf_instance.vnf.name))
130 while not vf_module_instantiation.finished:
132 if vf_module_instantiation.failed:
133 raise onap_test_exceptions.VfModuleInstantiateException
136 def cleanup(self) -> None:
137 """Cleanup Vf module.
140 Exception: Vf module cleaning failed
144 for vnf_instance in self._service_instance.vnf_instances:
145 self._logger.debug("VNF instance %s found in Service Instance ",
147 self._logger.info("Get VF Modules")
148 for vf_module in vnf_instance.vf_modules:
149 self._logger.info("Delete VF Module %s",
151 vf_module_deletion = vf_module.delete()
155 while not vf_module_deletion.finished and nb_try < nb_try_max:
156 self._logger.info("Wait for vf module deletion")
159 if vf_module_deletion.finished:
160 self._logger.info("VfModule %s deleted", vf_module.name)
162 self._logger.error("VfModule deletion %s failed", vf_module.name)
163 raise onap_test_exceptions.VfModuleCleanupException