c5308d65709490031f7e4c0fd105ef50516f3f89
[testsuite/pythonsdk-tests.git] / src / onaptests / steps / instantiate / sdnc_service.py
1 from onapsdk.sdnc.services import Service
2 from onapsdk.configuration import settings
3 from onapsdk.exceptions import APIError
4
5 from ..base import BaseStep
6
7 from onaptests.utils.exceptions import OnapTestException
8
9
10 class ServiceCreateStep(BaseStep):
11     """Service creation step."""
12
13     def __init__(self, service: Service = None, cleanup: bool = False):
14         """Initialize step."""
15         super().__init__(cleanup=cleanup)
16         self.service = service
17
18     @property
19     def description(self) -> str:
20         """Step description."""
21         return "Create SDNC service."
22
23     @property
24     def component(self) -> str:
25         """Component name."""
26         return "SDNC"
27
28     @BaseStep.store_state
29     def execute(self):
30         """Create service at SDNC."""
31         self._logger.info("Create new service instance in SDNC by GR-API")
32         super().execute()
33         try:
34             self.service = Service(
35                 service_instance_id=settings.SERVICE_ID,
36                 service_status=settings.SERVICE_STATUS,
37                 service_data=settings.SERVICE_DATA
38             )
39             self.service.create()
40             self._logger.info("SDNC service is created.")
41         except APIError:
42             raise OnapTestException("SDNC service creation failed.")
43
44     @BaseStep.store_state()
45     def cleanup(self) -> None:
46         """Cleanup Service."""
47         if self.service is not None:
48             self.service.delete()
49             self._logger.info("SDNC service is deleted.")
50         super().cleanup()
51
52
53 class UpdateSdncService(BaseStep):
54     """Service update step.
55
56     The step needs in an existing SDNC service as a prerequisite.
57     """
58
59     def __init__(self, cleanup=False):
60         """Initialize step.
61
62         Sub steps:
63             - ServiceCreateStep.
64         """
65         super().__init__(cleanup=cleanup)
66         self.add_step(ServiceCreateStep(cleanup=cleanup))
67
68     @property
69     def description(self) -> str:
70         """Step description.
71
72         Used for reports
73
74         Returns:
75             str: Step description
76
77         """
78         return "Update SDNC service"
79
80     @property
81     def component(self) -> str:
82         """Component name.
83
84         Name of component which step is related with.
85             Most is the name of ONAP component.
86
87         Returns:
88             str: Component name
89
90         """
91         return "SDNC"
92
93     @BaseStep.store_state
94     def execute(self):
95         self._logger.info("Get existing SDNC service instance and update it over GR-API")
96         super().execute()
97         try:
98             service = Service.get(settings.SERVICE_ID)
99             service.service_status = settings.SERVICE_CHANGED_STATUS
100             service.service_data = settings.SERVICE_CHANGED_DATA
101             service.update()
102             self._logger.info("SDNC service update is checked.")
103         except APIError:
104             raise OnapTestException("SDNC service update is failed.")