aboutsummaryrefslogtreecommitdiffstats
path: root/src/onaptests/steps/onboard/vendor.py
blob: 6ea05cd1926f2b82b6fc8e34d00c3ac2fdb0844f (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
import onapsdk.constants as const
from onapsdk.configuration import settings
from onapsdk.sdc.vendor import Vendor

from ..base import BaseStep, YamlTemplateBaseStep


class VendorOnboardStep(BaseStep):
    """Vendor onboard step."""

    def __init__(self):
        """Initialize step."""
        super().__init__(cleanup=settings.CLEANUP_FLAG)

    @property
    def description(self) -> str:
        """Step description."""
        return "Onboard vendor in SDC."

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

    def check_preconditions(self, cleanup=False) -> bool:
        if not super().check_preconditions(cleanup):
            return False
        if cleanup:
            return settings.SDC_CLEANUP
        return True

    @BaseStep.store_state
    def execute(self):
        """Onboard vendor.

        Use settings values:
         - VENDOR_NAME.

        """
        super().execute()
        vendor: Vendor = Vendor(name=settings.VENDOR_NAME)
        vendor.onboard()

    @BaseStep.store_state(cleanup=True)
    def cleanup(self) -> None:
        vendor: Vendor = Vendor(name=settings.VENDOR_NAME)
        if vendor.exists():
            if vendor.status == const.CERTIFIED:
                vendor.archive()
            vendor.delete()
        super().cleanup()


class YamlTemplateVendorOnboardStep(YamlTemplateBaseStep):
    """Vendor onboard using YAML template step."""

    def __init__(self):
        """Initialize step. """
        super().__init__(cleanup=settings.CLEANUP_FLAG)

    @property
    def description(self) -> str:
        """Step description."""
        return "Onboard vendor described in YAML file in SDC."

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

    def check_preconditions(self, cleanup=False) -> bool:
        if not super().check_preconditions(cleanup):
            return False
        if cleanup:
            return settings.SDC_CLEANUP
        return True

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

        Get YAML template from parent.

        Returns:
            dict: YAML template

        """
        if settings.MODEL_YAML_TEMPLATE:
            return self.model_yaml_template
        return self.parent.yaml_template

    @property
    def model_yaml_template(self) -> dict:
        """Model YAML template.

        Get model YAML template from parent.

        Returns:
            dict: YAML template

        """
        return self.parent.model_yaml_template

    @YamlTemplateBaseStep.store_state
    def execute(self):
        """Onboard Vsps from YAML template.

        Use settings values:
         - VENDOR_NAME.
        """
        super().execute()
        if "vnfs" in self.yaml_template:
            for vnf in self.yaml_template["vnfs"]:
                vendor: Vendor = Vendor(name=f"{vnf['vnf_name']}")
                vendor.onboard()
        elif "pnfs" in self.yaml_template:
            for pnf in self.yaml_template["pnfs"]:
                vendor: Vendor = Vendor(name=f"{pnf['pnf_name']}")
                vendor.onboard()

    def _cleanup_vendor(self, name):
        vendor: Vendor = Vendor(name=name)
        if vendor.exists():
            if vendor.status == const.CERTIFIED:
                vendor.archive()
            vendor.delete()

    @YamlTemplateBaseStep.store_state(cleanup=True)
    def cleanup(self) -> None:
        if "vnfs" in self.yaml_template:
            for vnf in self.yaml_template["vnfs"]:
                self._cleanup_vendor(f"{vnf['vnf_name']}")
        elif "pnfs" in self.yaml_template:
            for pnf in self.yaml_template["pnfs"]:
                self._cleanup_vendor(f"{pnf['pnf_name']}")
        super().cleanup()