diff options
Diffstat (limited to 'src/onaptests/steps/onboard/service.py')
-rw-r--r-- | src/onaptests/steps/onboard/service.py | 106 |
1 files changed, 44 insertions, 62 deletions
diff --git a/src/onaptests/steps/onboard/service.py b/src/onaptests/steps/onboard/service.py index 738454f..a9a3b14 100644 --- a/src/onaptests/steps/onboard/service.py +++ b/src/onaptests/steps/onboard/service.py @@ -1,16 +1,14 @@ -import time from typing import Any, Dict +from yaml import SafeLoader, load -import onapsdk.constants as onapsdk_const from onapsdk.configuration import settings -from onapsdk.exceptions import APIError, ResourceNotFound -from onapsdk.sdc.component import Component -from onapsdk.sdc.pnf import Pnf -from onapsdk.sdc.properties import ComponentProperty -from onapsdk.sdc.service import Service, ServiceInstantiationType -from onapsdk.sdc.vf import Vf -from onapsdk.sdc.vl import Vl -from yaml import SafeLoader, load +from onapsdk.exceptions import ResourceNotFound +from onapsdk.sdc2.pnf import Pnf +from onapsdk.sdc2.component_instance import ComponentInstance, ComponentInstanceInput +from onapsdk.sdc2.sdc_resource import LifecycleOperation, LifecycleState +from onapsdk.sdc2.service import Service, ServiceInstantiationType +from onapsdk.sdc2.vf import Vf +from onapsdk.sdc2.vl import Vl from ..base import BaseStep, YamlTemplateBaseStep from .pnf import PnfOnboardStep, YamlTemplatePnfOnboardStep @@ -62,10 +60,13 @@ class ServiceOnboardStep(BaseStep): """ super().execute() - service: Service = Service(name=settings.SERVICE_NAME, - instantiation_type=settings.SERVICE_INSTANTIATION_TYPE) - if not service.created(): - service.create() + try: + service: Service = Service.get_by_name(name=settings.SERVICE_NAME) + if service.distributed: + return + except ResourceNotFound: + service = Service.create(name=settings.SERVICE_NAME, + instantiation_type=settings.SERVICE_INSTANTIATION_TYPE) if settings.VL_NAME != "": vl: Vl = Vl(name=settings.VL_NAME) service.add_resource(vl) @@ -75,29 +76,19 @@ class ServiceOnboardStep(BaseStep): if settings.PNF_NAME != "": pnf: Pnf = Pnf(name=settings.PNF_NAME) service.add_resource(pnf) - # If the service is already distributed, do not try to checkin/onboard (replay of tests) - # checkin is done if needed - # If service is replayed, no need to try to re-onboard the model - # Double check because of: - # https://gitlab.com/Orange-OpenSource/lfn/onap/python-onapsdk/-/issues/176 - if not service.distributed and service.status != onapsdk_const.DISTRIBUTED: - if service.status == onapsdk_const.DRAFT: - try: - service.checkin() - except (APIError, ResourceNotFound): - # Retry as checkin may be a bit long - # Temp workaround to avoid internal race in SDC - time.sleep(10) - service.checkin() - service.onboard() + if service.lifecycle_state != LifecycleState.CERTIFIED: + service.lifecycle_operation(LifecycleOperation.CERTIFY) + service.distribute() @BaseStep.store_state def cleanup(self) -> None: """Cleanup service onboard step.""" - service: Service = Service(name=settings.SERVICE_NAME) - if service.exists(): + try: + service: Service = Service.get_by_name(name=settings.SERVICE_NAME) service.archive() service.delete() + except ResourceNotFound: + self._logger.info(f"Service {settings.SERVICE_NAME} not found") super().cleanup() @@ -181,26 +172,17 @@ class YamlTemplateServiceOnboardStep(YamlTemplateBaseStep): self.yaml_template[self.service_name]["instantiation_type"]) else: instantiation_type: ServiceInstantiationType = ServiceInstantiationType.A_LA_CARTE - service: Service = Service(name=self.service_name, instantiation_type=instantiation_type) - if not service.created(): - service.create() + try: + service: Service = Service.get_by_name(name=self.service_name) + if service.distributed: + return + except ResourceNotFound: + service = Service.create(name=self.service_name, instantiation_type=instantiation_type) self.declare_resources(service) self.assign_properties(service) - # If the service is already distributed, do not try to checkin/onboard (replay of tests) - # checkin is done if needed - # If service is replayed, no need to try to re-onboard the model - # Double check because of: - # https://gitlab.com/Orange-OpenSource/lfn/onap/python-onapsdk/-/issues/176 - if not service.distributed and service.status != onapsdk_const.DISTRIBUTED: - if service.status == onapsdk_const.DRAFT: - try: - service.checkin() - except (APIError, ResourceNotFound): - # Retry as checkin may be a bit long - # Temp workaround to avoid internal race in SDC - time.sleep(10) - service.checkin() - service.onboard() + if service.lifecycle_state != LifecycleState.CERTIFIED: + service.lifecycle_operation(LifecycleOperation.CERTIFY) + service.distribute() def declare_resources(self, service: Service) -> None: """Declare resources. @@ -213,15 +195,15 @@ class YamlTemplateServiceOnboardStep(YamlTemplateBaseStep): """ if "networks" in self.yaml_template[self.service_name]: for net in self.yaml_template[self.service_name]["networks"]: - vl: Vl = Vl(name=net['vl_name']) + vl: Vl = Vl.get_by_name(name=net['vl_name']) service.add_resource(vl) if "vnfs" in self.yaml_template[self.service_name]: for vnf in self.yaml_template[self.service_name]["vnfs"]: - vf: Vf = Vf(name=vnf["vnf_name"]) + vf: Vf = Vf.get_by_name(name=vnf["vnf_name"]) service.add_resource(vf) if "pnfs" in self.yaml_template[self.service_name]: for pnf in self.yaml_template[self.service_name]["pnfs"]: - pnf_obj: Pnf = Pnf(name=pnf["pnf_name"]) + pnf_obj: Pnf = Pnf.get_by_name(name=pnf["pnf_name"]) service.add_resource(pnf_obj) def assign_properties(self, service: Service) -> None: @@ -237,24 +219,22 @@ class YamlTemplateServiceOnboardStep(YamlTemplateBaseStep): if "networks" in self.yaml_template[self.service_name]: for net in self.yaml_template[self.service_name]["networks"]: if "properties" in net: - vl: Vl = Vl(name=net['vl_name']) - vl_component: Component = service.get_component(vl) + vl_component: ComponentInstance = service.get_component_by_name(net['vl_name']) self.assign_properties_to_component(vl_component, net["properties"]) if "vnfs" in self.yaml_template[self.service_name]: for vnf in self.yaml_template[self.service_name]["vnfs"]: if "properties" in vnf: - vf: Vf = Vf(name=vnf["vnf_name"]) - vf_component: Component = service.get_component(vf) + vf_component: ComponentInstance = service.get_component_by_name(vnf["vnf_name"]) self.assign_properties_to_component(vf_component, vnf["properties"]) if "pnfs" in self.yaml_template[self.service_name]: for pnf in self.yaml_template[self.service_name]["pnfs"]: if "properties" in pnf: - pnf_obj: Pnf = Pnf(name=pnf["pnf_name"]) - pnf_component: Component = service.get_component(pnf_obj) + pnf_component: ComponentInstance = \ + service.get_component_by_name(pnf["pnf_name"]) self.assign_properties_to_component(pnf_component, pnf["properties"]) def assign_properties_to_component(self, - component: Component, + component: ComponentInstance, component_properties: Dict[str, Any]) -> None: """Assign properties to component. @@ -264,14 +244,16 @@ class YamlTemplateServiceOnboardStep(YamlTemplateBaseStep): """ for property_name, property_value in component_properties.items(): - prop: ComponentProperty = component.get_property(property_name) + prop: ComponentInstanceInput = component.get_input_by_name(property_name) prop.value = property_value @YamlTemplateBaseStep.store_state(cleanup=True) def cleanup(self) -> None: """Cleanup service onboard step.""" - service: Service = Service(name=self.service_name) - if service.exists(): + try: + service: Service = Service.get_by_name(name=self.service_name) service.archive() service.delete() + except ResourceNotFound: + self._logger.info(f"Service {self.service_name} not found") super().cleanup() |