aboutsummaryrefslogtreecommitdiffstats
path: root/src/onaptests/scenario/pnf_macro.py
blob: 931a332ad325bc4db78afccaaea2091a3172fe82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Instantiate service with PNF using SO macro flow."""
import logging
import time
from yaml import load, SafeLoader

from xtesting.core import testcase
from onapsdk.configuration import settings
from onapsdk.exceptions import SDKException

from onaptests.steps.base import YamlTemplateBaseStep
from onaptests.steps.onboard.cds import CbaEnrichStep
from onaptests.steps.simulator.pnf_simulator_cnf.pnf_register import PnfSimulatorCnfRegisterStep
from onaptests.steps.instantiate.service_macro import YamlTemplateServiceMacroInstantiateStep
from onaptests.utils.exceptions import OnapTestException


class PnfMacroScenarioStep(YamlTemplateBaseStep):
    """Step created to run scenarion and generate report."""

    def __init__(self, cleanup=False):
        """Initialize step.

        Substeps:
            - YamlTemplateServiceAlaCarteInstantiateStep.
        """
        super().__init__(cleanup=cleanup)
        self._yaml_template: dict = None
        self.add_step(PnfSimulatorCnfRegisterStep(
            cleanup=cleanup
        ))
        self.add_step(CbaEnrichStep(
            cleanup=cleanup
        ))
        self.add_step(YamlTemplateServiceMacroInstantiateStep(
            cleanup=cleanup
        ))

    @property
    def description(self) -> str:
        """Step description.

        Used for reports

        Returns:
            str: Step description

        """
        return "PNF macro scenario step"

    @property
    def component(self) -> str:
        """Component name.

        Name of component which step is related with.
            Most is the name of ONAP component.

        Returns:
            str: Component name

        """
        return "PythonSDK-tests"

    @property
    def yaml_template(self) -> dict:
        """YAML template abstract property.

        Every YAML template step need to implement that property.

        Returns:
            dict: YAML template

        """
        if not self._yaml_template:
            with open(settings.SERVICE_YAML_TEMPLATE, "r") as yaml_template:
                self._yaml_template: dict = load(yaml_template, SafeLoader)
        return self._yaml_template

    @property
    def model_yaml_template(self) -> dict:
        return {}

    @property
    def service_name(self) -> dict:
        """Service name.

        Get from YAML template.

        Returns:
            str: Service name

        """
        return next(iter(self.yaml_template.keys()))

    @property
    def service_instance_name(self) -> str:
        """Service instance name.

        Returns:
            str: Service instance name

        """
        return settings.SERVICE_INSTANCE_NAME


class PnfMacro(testcase.TestCase):
    """Run PNF simulator and onboard then instantiate a service with PNF."""

    __logger = logging.getLogger(__name__)

    def __init__(self, **kwargs):
        """Init Basic Network use case."""
        if "case_name" not in kwargs:
            kwargs["case_name"] = 'pnf_macro'
        super().__init__(**kwargs)
        self.__logger.debug("PnfMacro init started")
        self.test = PnfMacroScenarioStep(cleanup=settings.CLEANUP_FLAG)

    def run(self):
        """Run PNF macro test."""
        self.start_time = time.time()
        try:
            self.test.execute()
            self.test.cleanup()
            self.result = 100
        except OnapTestException as exc:
            self.result = 0
            self.__logger.error(exc.error_message)
        except SDKException:
            self.result = 0
            self.__logger.error("SDK Exception")
        finally:
            self.stop_time = time.time()

    def clean(self):
        """Generate report."""
        self.test.reports_collection.generate_report()