aboutsummaryrefslogtreecommitdiffstats
path: root/onap_data_provider/resources/resource_creator.py
blob: de9ee9bbc3f879ef08bf53fbc0a6985335eab99c (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
"""Resource creator module."""
from __future__ import annotations

"""
   Copyright 2021 Deutsche Telekom AG

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""

from onap_data_provider.resources.platform_resource import PlatformResource
import typing
from abc import ABC

from .aai_service_resource import AaiServiceResource
from .cloud_region_resource import CloudRegionResource
from .complex_resource import ComplexResource
from .customer_resource import CustomerResource
from .line_of_business_resource import LineOfBusinessResource
from .msb_k8s_definition import MsbK8SDefinitionResource
from .owning_entity_resource import OwningEntityResource
from .pnf_resource import PnfResource
from .project_resource import ProjectResource
from .service_resource import ServiceResource
from .service_instance_resource import (
    ServiceInstanceResource,
    ServiceInstanceResource_1_1,
)
from .vendor_resource import VendorResource
from .vnf_resource import VnfResource
from .vsp_resource import VspResource
from .data_dictionary_resource import DataDictionarySetResource
from ..versions import VersionsEnum

if typing.TYPE_CHECKING:
    from .resource import Resource


class ResourceCreator(ABC):
    """Resource creator.

    Provides a method to create `Resource` instances.
    """

    RESOURCES_TYPES_DICT: typing.Mapping[
        str, typing.Mapping[VersionsEnum, typing.Type[Resource]]
    ] = {
        "aai-service": {
            VersionsEnum.NONE: AaiServiceResource,
            VersionsEnum.V1_0: AaiServiceResource,
            VersionsEnum.V1_1: AaiServiceResource,
        },
        "cloud-region": {
            VersionsEnum.NONE: CloudRegionResource,
            VersionsEnum.V1_0: CloudRegionResource,
            VersionsEnum.V1_1: CloudRegionResource,
        },
        "complex": {
            VersionsEnum.NONE: ComplexResource,
            VersionsEnum.V1_0: ComplexResource,
            VersionsEnum.V1_1: ComplexResource,
        },
        "customer": {
            VersionsEnum.NONE: CustomerResource,
            VersionsEnum.V1_0: CustomerResource,
            VersionsEnum.V1_1: CustomerResource,
        },
        "vsp": {
            VersionsEnum.NONE: VspResource,
            VersionsEnum.V1_0: VspResource,
            VersionsEnum.V1_1: VspResource,
        },
        "service": {
            VersionsEnum.NONE: ServiceResource,
            VersionsEnum.V1_0: ServiceResource,
            VersionsEnum.V1_1: ServiceResource,
        },
        "vendor": {
            VersionsEnum.NONE: VendorResource,
            VersionsEnum.V1_0: VendorResource,
            VersionsEnum.V1_1: VendorResource,
        },
        "pnf": {
            VersionsEnum.NONE: PnfResource,
            VersionsEnum.V1_0: PnfResource,
            VersionsEnum.V1_1: PnfResource,
        },
        "vnf": {
            VersionsEnum.NONE: VnfResource,
            VersionsEnum.V1_0: VnfResource,
            VersionsEnum.V1_1: VnfResource,
        },
        "service-instance": {
            VersionsEnum.NONE: ServiceInstanceResource,
            VersionsEnum.V1_0: ServiceInstanceResource,
            VersionsEnum.V1_1: ServiceInstanceResource_1_1,
        },
        "line-of-business": {
            VersionsEnum.NONE: LineOfBusinessResource,
            VersionsEnum.V1_0: LineOfBusinessResource,
            VersionsEnum.V1_1: LineOfBusinessResource,
        },
        "project": {
            VersionsEnum.NONE: ProjectResource,
            VersionsEnum.V1_0: ProjectResource,
            VersionsEnum.V1_1: ProjectResource,
        },
        "platform": {
            VersionsEnum.NONE: PlatformResource,
            VersionsEnum.V1_0: PlatformResource,
            VersionsEnum.V1_1: PlatformResource,
        },
        "owning-entity": {
            VersionsEnum.NONE: OwningEntityResource,
            VersionsEnum.V1_0: OwningEntityResource,
            VersionsEnum.V1_1: OwningEntityResource,
        },
        "msb-k8s-definition": {
            VersionsEnum.NONE: MsbK8SDefinitionResource,
            VersionsEnum.V1_0: MsbK8SDefinitionResource,
            VersionsEnum.V1_1: MsbK8SDefinitionResource,
        },
        "data-dictionaries": {
            VersionsEnum.NONE: DataDictionarySetResource,
            VersionsEnum.V1_0: DataDictionarySetResource,
            VersionsEnum.V1_1: DataDictionarySetResource,
        },
    }

    @classmethod
    def create(
        cls,
        resource_type: str,
        data: typing.Dict[str, typing.Any],
        version: VersionsEnum,
    ) -> Resource:
        """Resources factory method.

        Based on provided `resource_type` creates `Resource` subclass.

        Supported `resource_type` values:
         - aai-service: AaiServiceResource
         - cloud-region: CloudRegionResource
         - complex: ComplexResource
         - customer: CustomerResource
         - vsp: VspResource
         - service: ServiceResource
         - vendor: VendorResource
         - pnf: PnfResource
         - vnf: VnfResource
         - service-instance: ServiceInstanceResource
         - line-of-business: LineOfBusinessResource
         - project: ProjectResource
         - platform: PlatformResource
         - owning-entity: OwningEntityResource
         - msb-k8s-definition: MsbK8SDefinitionResource
         - data-dictionaries: DataDictionarySetResource

        Args:
            resource_type (str): Resource type to create
            data (typing.Dict[str, typing.Any]): Resource data

        Raises:
            ValueError: Not support `resource_type` value provided.

        Returns:
            Resource: Created `Resource` subclass instance.

        """
        try:
            return cls.RESOURCES_TYPES_DICT[resource_type][version](data)
        except KeyError as key_error:
            raise ValueError(
                "Invalid resource type provided: %d", resource_type
            ) from key_error