aboutsummaryrefslogtreecommitdiffstats
path: root/src/onapsdk/sdc/category_management.py
blob: c2d63b467587519b904c114025f910f12127dbe4 (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
"""SDC category management module."""
#   Copyright 2022 Orange, 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.

import json
from abc import ABC, abstractmethod
from typing import Any, Dict, List

from onapsdk.configuration import settings
from onapsdk.exceptions import ResourceNotFound
from onapsdk.sdc import SDC
from onapsdk.utils.headers_creator import headers_sdc_generic


class BaseCategory(SDC, ABC):  # pylint: disable=too-many-instance-attributes
    """Base SDC category class.

    It's SDC admin resource, has no common properties with
        SDC resourcer or elements, so SDC class can't be it's
        base class.

    """

    SDC_ADMIN_USER = "demo"

    def __init__(self, name: str) -> None:
        """Service category initialization.

        Args:
            name (str): Service category name.

        """
        super().__init__(name)
        self.normalized_name: str = None
        self.unique_id: str = None
        self.icons: List[str] = None
        self.subcategories: List[Dict[str, str]] = None
        self.version: str = None
        self.owner_id: str = None
        self.empty: bool = None
        self.type: str = None

    @classmethod
    def _get_all_url(cls) -> str:
        """Get URL for all categories in SDC."""
        return f"{cls.base_front_url}/sdc1/feProxy/rest/v1/setup/ui"

    @classmethod
    def _base_url(cls) -> str:
        """Give back the base url of Sdc."""
        return f"{settings.SDC_FE_URL}/sdc1/feProxy/rest/v1/category"

    @classmethod
    def headers(cls) -> Dict[str, str]:
        """Headers used for category management.

        It uses SDC admin user.

        Returns:
            Dict[str, str]: Headers

        """
        return headers_sdc_generic(super().headers, user=cls.SDC_ADMIN_USER)

    @classmethod
    def get_all(cls, **kwargs) -> List['SDC']:
        """
        Get the categories list created in SDC.

        Returns:
            the list of the categories

        """
        return super().get_all(headers=cls.headers())

    @classmethod
    def import_from_sdc(cls, values: Dict[str, Any]) -> 'BaseCategory':
        """
        Import category object from SDC.

        Args:
            values (Dict[str, Any]): dict to parse returned from SDC.

        """
        category_obj = cls(name=values["name"])
        category_obj.normalized_name = values["normalizedName"]
        category_obj.unique_id = values["uniqueId"]
        category_obj.icons = values["icons"]
        category_obj.subcategories = values["subcategories"]
        category_obj.version = values["version"]
        category_obj.owner_id = values["ownerId"]
        category_obj.empty = values["empty"]
        return category_obj

    @classmethod
    @abstractmethod
    def category_name(cls) -> str:
        """Class category name.

        Used for logs.

        Returns:
            str: Category name

        """

    @classmethod
    def get(cls, name: str) -> "BaseCategory":
        """Get category with given name.

        Raises:
            ResourceNotFound: Category with given name does not exist

        Returns:
            BaseCategory: BaseCategory instance

        """
        category_obj: "BaseCategory" = cls(name)
        if category_obj.exists():
            return category_obj
        msg = f"{cls.category_name()} with \"{name}\" name does not exist."
        raise ResourceNotFound(msg)

    @classmethod
    def create(cls, name: str) -> "BaseCategory":
        """Create category instance.

        Checks if category with given name exists and if it already
            exists just returns category with given name.

        Returns:
            BaseCategory: Created category instance

        """
        category_obj: "BaseCategory" = cls(name)
        if category_obj.exists():
            return category_obj
        cls.send_message_json("POST",
                              f"Create {name} {cls.category_name()}",
                              cls._base_create_url(),
                              data=json.dumps({"name": name}),
                              headers=cls.headers())
        category_obj.exists()
        return category_obj

    def _copy_object(self, obj: 'BaseCategory') -> None:
        """
        Copy relevant properties from object.

        Args:
            obj (BaseCategory): the object to "copy"

        """
        self.name = obj.name
        self.normalized_name = obj.normalized_name
        self.unique_id = obj.unique_id
        self.icons = obj.icons
        self.subcategories = obj.subcategories
        self.version = obj.version
        self.owner_id = obj.owner_id
        self.empty = obj.empty


class ResourceCategory(BaseCategory):
    """Resource category class."""

    @classmethod
    def _get_objects_list(cls,
                          result: Dict[str, Any]) -> List[Dict[str, Any]]:
        """
        Get list of resource categories.

        Args:
            result (List[Dict[str, Any]]): the result returned by SDC
                in a list of dicts

        Raises:
            KeyError: Invalid result dictionary

        """
        return result["categories"]["resourceCategories"]

    @classmethod
    def _base_create_url(cls) -> str:
        """Url to create resource category.

        Returns:
            str: Creation url

        """
        return f"{cls._base_url()}/resources"

    @classmethod
    def category_name(cls) -> str:
        """Resource category name.

        Used for logging.

        Returns:
            str: Resource category name

        """
        return "Resource Category"

    @classmethod
    def get(cls, name: str, subcategory: str = None) -> "ResourceCategory":  # pylint: disable=arguments-differ
        """Get resource category with given name.

        It returns resource category with all subcategories by default. You can
            get resource category with only one subcategory if you provide it's
            name as `subcategory` parameter.

        Args:
            name (str): Resource category name.
            subcategory (str, optional): Name of subcategory. Defaults to None.

        Raises:
            ResourceNotFound: Subcategory with given name does not exist

        Returns:
            BaseCategory: BaseCategory instance

        """
        category_obj: "ResourceCategory" = super().get(name=name)
        if not subcategory:
            return category_obj
        filtered_subcategories: Dict[str, str] = list(filter(lambda x: x["name"] == subcategory,
                                                             category_obj.subcategories))
        if not filtered_subcategories:
            raise ResourceNotFound(f"Subcategory {subcategory} does not exist.")
        category_obj.subcategories = filtered_subcategories
        return category_obj


class ServiceCategory(BaseCategory):
    """Service category class."""

    @classmethod
    def _get_objects_list(cls,
                          result: Dict[str, Any]) -> List[Dict[str, Any]]:
        """
        Get list of service categories.

        Args:
            result (List[Dict[str, Any]]): the result returned by SDC
                in a list of dicts

        Raises:
            KeyError: Invalid result dictionary

        """
        return result["categories"]["serviceCategories"]

    @classmethod
    def _base_create_url(cls) -> str:
        """Url to create service category.

        Returns:
            str: Creation url

        """
        return f"{cls._base_url()}/services"

    @classmethod
    def category_name(cls) -> str:
        """Service category name.

        Used for logging.

        Returns:
            str: Service category name

        """
        return "Service Category"