fef8b836433281a9cf14b2f1fc0d4f479fead21c
[testsuite/pythonsdk-tests.git] / src / onaptests / scenario / pnf_macro.py
1 """Instantiate service with PNF using SO macro flow."""
2 import logging
3 import time
4 from yaml import load
5
6 from xtesting.core import testcase
7 from onapsdk.configuration import settings
8 from onapsdk.exceptions import SDKException
9
10 from onaptests.steps.base import YamlTemplateBaseStep
11 from onaptests.steps.onboard.cds import CbaEnrichStep
12 from onaptests.steps.simulator.pnf_simulator_cnf.pnf_register import PnfSimulatorCnfRegisterStep
13 from onaptests.steps.instantiate.service_macro import YamlTemplateServiceMacroInstantiateStep
14 from onaptests.utils.exceptions import OnapTestException
15
16
17 class PnfMacroScenarioStep(YamlTemplateBaseStep):
18     """Step created to run scenarion and generate report."""
19
20     def __init__(self, cleanup=False):
21         """Initialize step.
22
23         Substeps:
24             - YamlTemplateServiceAlaCarteInstantiateStep.
25         """
26         super().__init__(cleanup=cleanup)
27         self._yaml_template: dict = None
28         self.add_step(PnfSimulatorCnfRegisterStep(
29             cleanup=settings.CLEANUP_FLAG
30         ))
31         self.add_step(CbaEnrichStep(
32             cleanup=settings.CLEANUP_FLAG
33         ))
34         self.add_step(YamlTemplateServiceMacroInstantiateStep(
35             cleanup=settings.CLEANUP_FLAG
36         ))
37
38     @property
39     def description(self) -> str:
40         """Step description.
41
42         Used for reports
43
44         Returns:
45             str: Step description
46
47         """
48         return "PNF macro scenario step"
49
50     @property
51     def component(self) -> str:
52         """Component name.
53
54         Name of component which step is related with.
55             Most is the name of ONAP component.
56
57         Returns:
58             str: Component name
59
60         """
61         return "PythonSDK-tests"
62
63     @property
64     def yaml_template(self) -> dict:
65         """YAML template abstract property.
66
67         Every YAML template step need to implement that property.
68
69         Returns:
70             dict: YAML template
71
72         """
73         if not self._yaml_template:
74             with open(settings.SERVICE_YAML_TEMPLATE, "r") as yaml_template:
75                 self._yaml_template: dict = load(yaml_template)
76         return self._yaml_template
77
78     @property
79     def service_name(self) -> dict:
80         """Service name.
81
82         Get from YAML template.
83
84         Returns:
85             str: Service name
86
87         """
88         return next(iter(self.yaml_template.keys()))
89
90     @property
91     def service_instance_name(self) -> str:
92         """Service instance name.
93
94         Returns:
95             str: Service instance name
96
97         """
98         return settings.SERVICE_INSTANCE_NAME
99
100
101 class PnfMacro(testcase.TestCase):
102     """Run PNF simulator and onboard then instantiate a service with PNF."""
103
104     __logger = logging.getLogger(__name__)
105
106     def __init__(self, **kwargs):
107         """Init Basic Network use case."""
108         if "case_name" not in kwargs:
109             kwargs["case_name"] = 'pnf_macro'
110         super().__init__(**kwargs)
111         self.__logger.debug("PnfMacro init started")
112         self.test = PnfMacroScenarioStep(cleanup=settings.CLEANUP_FLAG)
113
114     def run(self):
115         """Run PNF macro test."""
116         self.start_time = time.time()
117         try:
118             self.test.execute()
119             self.test.cleanup()
120             self.result = 100
121         except (OnapTestException, SDKException) as exc:
122             self.result = 0
123             self.__logger.error(exc.error_message)
124         finally:
125             self.stop_time = time.time()
126
127     def clean(self):
128         """Generate report."""
129         self.test.reports_collection.generate_report()