aboutsummaryrefslogtreecommitdiffstats
path: root/src/onaptests/steps/instantiate/sdnc_service.py
blob: 0c10591b177f301de0cba2d06e3e60bda8d65f6d (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import base64
import logging
from typing import Dict

import mysql.connector as mysql
from kubernetes import client, config
from onapsdk.configuration import settings
from onapsdk.exceptions import APIError
from onapsdk.sdnc import VfModulePreload
from onapsdk.sdnc.preload import PreloadInformation
from onapsdk.sdnc.sdnc_element import SdncElement
from onapsdk.sdnc.services import Service
from onapsdk.utils.headers_creator import headers_sdnc_creator

from onaptests.scenario.scenario_base import BaseScenarioStep
from onaptests.steps.base import BaseStep
from onaptests.utils.exceptions import (EnvironmentPreparationException,
                                        OnapTestException)


class BaseSdncStep(BaseStep):
    """Basic SDNC step."""

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

    @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 "SDNC"


class CheckSdncDbStep(BaseSdncStep):
    """Check MariaDB connection status."""

    SDNC_QUERY = "SELECT * FROM svc_logic LIMIT 1;"
    SDNC_DATABASE = "sdnctl"
    SDNC_DB_LOGIN = "login"
    SDNC_DB_PASSWORD = "password"

    def __init__(self):
        """Initialize step."""
        super().__init__(cleanup=BaseStep.HAS_NO_CLEANUP)
        self.login = None
        self.password = None

    @property
    def description(self) -> str:
        """Step description."""
        return "Check MariaDB connection."

    def get_database_credentials(self):
        """Resolve SDNC datbase credentials from k8s secret."""

        if settings.IN_CLUSTER:
            config.load_incluster_config()
        else:
            config.load_kube_config(config_file=settings.K8S_CONFIG)
        api_instance = client.CoreV1Api()
        try:
            secret = api_instance.read_namespaced_secret(
                settings.SDNC_SECRET_NAME, settings.K8S_ONAP_NAMESPACE)
            if secret.data:
                if (self.SDNC_DB_LOGIN in secret.data and self.SDNC_DB_PASSWORD in secret.data):
                    login_base64 = secret.data[self.SDNC_DB_LOGIN]
                    self.login = base64.b64decode(login_base64).decode("utf-8")
                    password_base64 = secret.data[self.SDNC_DB_PASSWORD]
                    self.password = base64.b64decode(password_base64).decode("utf-8")
                else:
                    raise EnvironmentPreparationException(
                        "Login key or password key not found in secret")
            else:
                raise EnvironmentPreparationException("Secret data not found in secret")
        except client.rest.ApiException as e:
            self.login = None
            self.password = None
            raise EnvironmentPreparationException("Error accessing secret") from e

    @BaseStep.store_state
    def execute(self) -> None:
        """Check MariaDB connection."""
        super().execute()
        self.get_database_credentials()
        conn = None
        try:
            conn = mysql.connect(
                database=settings.DATABASE,
                host=settings.DB_PRIMARY_HOST,
                port=settings.DB_PORT,
                user=self.login,
                password=self.password)
            cursor = conn.cursor()
            cursor.execute(settings.QUERY)
        except Exception as e:
            raise OnapTestException("Cannot connect to SDNC Database") from e
        finally:
            if conn:
                try:
                    conn.close()
                except Exception:
                    pass


class ServiceCreateStep(BaseSdncStep):
    """Service creation step."""

    def __init__(self, service: Service = None):
        """Initialize step."""
        super().__init__(cleanup=settings.CLEANUP_FLAG)
        self.service = service

    @property
    def description(self) -> str:
        """Step description."""
        return "Create SDNC service."

    @BaseStep.store_state
    def execute(self) -> None:
        """Create service at SDNC."""
        super().execute()
        self._logger.info("Create new service instance in SDNC by GR-API")
        try:
            self.service = Service(
                service_instance_id=settings.SERVICE_ID,
                service_status=settings.SERVICE_STATUS,
                service_data=settings.SERVICE_DATA
            )
            self.service.create()
            self._logger.info("SDNC service is created.")
        except APIError as exc:
            if exc.response_status_code == 409:
                self._logger.warning("SDNC service already exists.")
            else:
                raise OnapTestException("SDNC service creation failed.") from exc

    @BaseStep.store_state(cleanup=True)
    def cleanup(self) -> None:
        """Cleanup Service."""
        if self.service is not None:
            self.service.delete()
            self._logger.info("SDNC service is deleted.")
        super().cleanup()


class UpdateSdncService(BaseSdncStep):
    """Service update step.

    The step needs in an existing SDNC service as a prerequisite.
    """

    def __init__(self):
        """Initialize step.

        Sub steps:
            - ServiceCreateStep.
        """
        super().__init__(cleanup=BaseStep.HAS_NO_CLEANUP)
        self.add_step(ServiceCreateStep())

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

        Used for reports

        Returns:
            str: Step description

        """
        return "Update SDNC service"

    @BaseSdncStep.store_state
    def execute(self) -> None:
        super().execute()
        self._logger.info("Get existing SDNC service instance and update it over GR-API")
        try:
            service = Service.get(settings.SERVICE_ID)
            service.service_status = settings.SERVICE_CHANGED_STATUS
            service.service_data = settings.SERVICE_CHANGED_DATA
            service.update()
            self._logger.info("SDNC service update is completed.")
        except APIError as exc:
            raise OnapTestException("SDNC service update is failed.") from exc


class UploadVfModulePreloadStep(BaseSdncStep):
    """Upload preload information for VfModule.

    Upload preload information for VfModule over GR-API.
    """

    def __init__(self):
        """Initialize step."""
        super().__init__(cleanup=BaseStep.HAS_NO_CLEANUP)

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

        Used for reports

        Returns:
            str: Step description

        """
        return "Upload Preload information for VfModule"

    @BaseSdncStep.store_state
    def execute(self) -> None:
        super().execute()
        self._logger.info("Upload VfModule preload information over GR-API")
        VfModulePreload.upload_vf_module_preload(
            {
                "vnf_name": settings.VNF_NAME,
                "vnf_type": settings.VNF_TYPE
            },
            settings.VF_MODULE_NAME,
            None
        )


class CheckSdncHealthStep(BaseSdncStep, SdncElement):
    """Check SDNC Health API response."""

    headers: Dict[str, str] = headers_sdnc_creator(SdncElement.headers)

    def __init__(self):
        """Initialize step."""
        super().__init__(cleanup=BaseStep.HAS_NO_CLEANUP)

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

        Used for reports

        Returns:
            str: Step description

        """
        return "Check SDNC Health API response."

    @BaseSdncStep.store_state
    def execute(self) -> None:
        super().execute()
        result = self.send_message_json(
            "POST",
            "SDNC SLI API Healthcheck",
            f"{self.base_url}/restconf/operations/SLI-API:healthcheck")
        message = ""
        if result and result["output"]:
            if result["output"]["response-code"] == "200":
                return
            message = result["output"]["response-message"]
        raise OnapTestException("SDNC is not healthy. %s" % message)


class GetSdncPreloadStep(BaseSdncStep):
    """Get preload information from SDNC.

    Get preload information from SDNC over GR-API.
    """

    __logger = logging.getLogger(__name__)

    def __init__(self):
        """Initialize step.

        Sub steps:
            - UploadVfModulePreloadStep.
        """
        super().__init__(cleanup=BaseStep.HAS_NO_CLEANUP)
        self.add_step(UploadVfModulePreloadStep())

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

        Used for reports

        Returns:
            str: Step description

        """
        return "Get Preload information"

    @BaseSdncStep.store_state
    def execute(self) -> None:
        super().execute()
        self._logger.info("Get existing SDNC service instance and update it over GR-API")
        preloads = PreloadInformation.get_all()
        for preload_information in preloads:
            self.__logger.debug(preload_information)


class TestSdncStep(BaseScenarioStep):
    """Top level step for SDNC tests."""

    def __init__(self, full: bool = True):
        """Initialize step.

        Args:
            full (bool): If the API logic calls should be executed
        Sub steps:
            - CheckSdncDbStep
            - UpdateSdncService
            - GetSdncPreloadStep
        """
        super().__init__(cleanup=BaseStep.HAS_NO_CLEANUP)
        self.add_step(CheckSdncDbStep())
        self.add_step(CheckSdncHealthStep())
        if full:
            self.add_step(UpdateSdncService())
            self.add_step(GetSdncPreloadStep())

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

        Used for reports

        Returns:
            str: Step description

        """
        return "Test SDNC functionality"

    @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 "SDNC"