Enhanced SDNC test to cover SVC logic check
[testsuite/pythonsdk-tests.git] / src / onaptests / steps / instantiate / sdnc_service.py
1 from onapsdk.configuration import settings
2 from onapsdk.exceptions import APIError
3 from onapsdk.sdnc import VfModulePreload
4 from onapsdk.sdnc.preload import PreloadInformation
5 from onapsdk.sdnc.services import Service
6 from onaptests.utils.exceptions import OnapTestException
7
8 from ..base import BaseStep
9
10
11 class BaseSdncStep(BaseStep):
12     """Basic SDNC step."""
13
14     def __init__(self, cleanup: bool = False):
15         """Initialize step."""
16         super().__init__(cleanup=cleanup)
17
18     @property
19     def component(self) -> str:
20         """Component name.
21
22         Name of component which step is related with.
23             Most is the name of ONAP component.
24
25         Returns:
26             str: Component name
27
28         """
29         return "SDNC"
30
31
32 class ServiceCreateStep(BaseSdncStep):
33     """Service creation step."""
34
35     def __init__(self, service: Service = None, cleanup: bool = False):
36         """Initialize step."""
37         super().__init__(cleanup=cleanup)
38         self.service = service
39
40     @property
41     def description(self) -> str:
42         """Step description."""
43         return "Create SDNC service."
44
45     @BaseStep.store_state
46     def execute(self):
47         """Create service at SDNC."""
48         super().execute()
49         self._logger.info("Create new service instance in SDNC by GR-API")
50         try:
51             self.service = Service(
52                 service_instance_id=settings.SERVICE_ID,
53                 service_status=settings.SERVICE_STATUS,
54                 service_data=settings.SERVICE_DATA
55             )
56             self.service.create()
57             self._logger.info("SDNC service is created.")
58         except APIError as exc:
59             if exc.response_status_code == 409:
60                 self._logger.warning("SDNC service already exists.")
61             else:
62                 raise OnapTestException("SDNC service creation failed.")
63
64     @BaseStep.store_state()
65     def cleanup(self) -> None:
66         """Cleanup Service."""
67         if self.service is not None:
68             self.service.delete()
69             self._logger.info("SDNC service is deleted.")
70         super().cleanup()
71
72
73 class UpdateSdncService(BaseSdncStep):
74     """Service update step.
75
76     The step needs in an existing SDNC service as a prerequisite.
77     """
78
79     def __init__(self, cleanup=False):
80         """Initialize step.
81
82         Sub steps:
83             - ServiceCreateStep.
84         """
85         super().__init__(cleanup=cleanup)
86         self.add_step(ServiceCreateStep(cleanup=cleanup))
87
88     @property
89     def description(self) -> str:
90         """Step description.
91
92         Used for reports
93
94         Returns:
95             str: Step description
96
97         """
98         return "Update SDNC service"
99
100     @BaseStep.store_state
101     def execute(self):
102         super().execute()
103         self._logger.info("Get existing SDNC service instance and update it over GR-API")
104         try:
105             service = Service.get(settings.SERVICE_ID)
106             service.service_status = settings.SERVICE_CHANGED_STATUS
107             service.service_data = settings.SERVICE_CHANGED_DATA
108             service.update()
109             self._logger.info("SDNC service update is completed.")
110         except APIError:
111             raise OnapTestException("SDNC service update is failed.")
112
113
114 class UploadVfModulePreloadStep(BaseSdncStep):
115     """Upload preload information for VfModule.
116
117     Upload preload information for VfModule over GR-API.
118     """
119
120     def __init__(self, cleanup=False):
121         """Initialize step."""
122         super().__init__(cleanup=cleanup)
123
124     @property
125     def description(self) -> str:
126         """Step description.
127
128         Used for reports
129
130         Returns:
131             str: Step description
132
133         """
134         return "Upload Preload information for VfModule"
135
136     @BaseStep.store_state
137     def execute(self):
138         super().execute()
139         self._logger.info("Upload VfModule preload information over GR-API")
140         VfModulePreload.upload_vf_module_preload(
141             {
142                 "vnf_name": settings.VNF_NAME,
143                 "vnf_type": settings.VNF_TYPE
144             },
145             settings.VF_MODULE_NAME,
146             None
147         )
148
149
150 class GetSdncPreloadStep(BaseSdncStep):
151     """Get preload information from SDNC.
152
153     Get preload information from SDNC over GR-API.
154     """
155
156     def __init__(self, cleanup=False):
157         """Initialize step.
158
159         Sub steps:
160             - UploadVfModulePreloadStep.
161         """
162         super().__init__(cleanup=cleanup)
163         self.add_step(UploadVfModulePreloadStep(cleanup=cleanup))
164
165     @property
166     def description(self) -> str:
167         """Step description.
168
169         Used for reports
170
171         Returns:
172             str: Step description
173
174         """
175         return "Get Preload information"
176
177     @BaseStep.store_state
178     def execute(self):
179         super().execute()
180         self._logger.info("Get existing SDNC service instance and update it over GR-API")
181         preloads = PreloadInformation.get_all()
182         for preload_information in preloads:
183             print(preload_information)
184
185
186 class TestSdncStep(BaseStep):
187     """Top level step for SDNC tests."""
188
189     def __init__(self, cleanup=False):
190         """Initialize step.
191
192         Sub steps:
193             - UpdateSdncService.
194         """
195         super().__init__(cleanup=cleanup)
196         self.add_step(
197             UpdateSdncService(cleanup=cleanup)
198         )
199         self.add_step(
200             GetSdncPreloadStep(cleanup=cleanup)
201         )
202
203     @property
204     def description(self) -> str:
205         """Step description.
206
207         Used for reports
208
209         Returns:
210             str: Step description
211
212         """
213         return "Test SDNC functionality scenario step"
214
215     @property
216     def component(self) -> str:
217         """Component name.
218
219         Name of component which step is related with.
220             Most is the name of ONAP component.
221
222         Returns:
223             str: Component name
224
225         """
226         return "PythonSDK-tests"