aboutsummaryrefslogtreecommitdiffstats
path: root/src/onapsdk/sdc/properties.py
blob: f7e07a063b0ba48f31cb6bd1eaf117cc60eb820c (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
"""Service properties 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.
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional

from onapsdk.exceptions import ParameterError


@dataclass
class Input:
    """Property input dataclass."""

    unique_id: str
    input_type: str
    name: str
    sdc_resource: "SdcResource"
    _default_value: Optional[Any] = field(repr=False, default=None)

    @property
    def default_value(self) -> Any:
        """Input default value.

        Returns:
            Any: Input default value

        """
        return self._default_value

    @default_value.setter
    def default_value(self, value: Any) -> None:
        """Set input default value.

        Use related sdc_resource "set_input_default_value"
            method to set default value in SDC. We use that
            because different types of SDC resources has different
            urls to call SDC API.

        Args:
            value (Any): Default value to set

        """
        self.sdc_resource.set_input_default_value(self, value)
        self._default_value = value

@dataclass
class NestedInput:
    """Dataclass used for nested input declaration."""

    sdc_resource: "SdcResource"
    input_obj: Input


class Property:  # pylint: disable=too-many-instance-attributes, too-few-public-methods
    """Service property class."""

    def __init__(self,  # pylint: disable=too-many-arguments
                 name: str,
                 property_type: str,
                 description: Optional[str] = None,
                 unique_id: Optional[str] = None,
                 parent_unique_id: Optional[str] = None,
                 sdc_resource: Optional["SdcResource"] = None,
                 value: Optional[Any] = None,
                 get_input_values: Optional[List[Dict[str, str]]] = None) -> None:
        """Property class initialization.

        Args:
            property_type (str): [description]
            description (Optional[str], optional): [description]. Defaults to None.
            unique_id (Optional[str], optional): [description]. Defaults to None.
            parent_unique_id (Optional[str], optional): [description]. Defaults to None.
            sdc_resource (Optional[, optional): [description]. Defaults to None.
            value (Optional[Any], optional): [description]. Defaults to None.
            get_input_values (Optional[List[Dict[str, str]]], optional): [description].
                Defaults to None.
        """
        self.name: str = name
        self.property_type: str = property_type
        self.description: str = description
        self.unique_id: str = unique_id
        self.parent_unique_id: str = parent_unique_id
        self.sdc_resource: "SdcResource" = sdc_resource
        self._value: Any = value
        self.get_input_values: List[Dict[str, str]] = get_input_values

    def __repr__(self) -> str:
        """Property object human readable representation.

        Returns:
            str: Property human readable representation

        """
        return f"Property(name={self.name}, property_type={self.property_type})"

    def __eq__(self, obj: "Property") -> bool:
        """Check if two Property object are equal.

        Args:
            obj (Property): Object to compare

        Returns:
            bool: True if objects are equal, False otherwise

        """
        return self.name == obj.name and self.property_type == obj.property_type

    @property
    def input(self) -> Input:
        """Property input.

        Returns property Input object.
            Returns None if property has no associated input.

        Raises:
            ParameterError: Input has no associated SdcResource

            ParameterError: Input for given property does not exits.
                It shouldn't ever happen, but it's possible if after you
                get property object someone delete input.

        Returns:
            Input: Property input object.

        """
        if not self.sdc_resource:
            raise ParameterError("Property has no associated SdcResource")
        if not self.get_input_values:
            return None
        try:
            return next(filter(lambda x: x.unique_id == self.get_input_values[0].get("inputId"),
                               self.sdc_resource.inputs))
        except StopIteration:
            raise ParameterError("Property input does not exist")

    @property
    def value(self) -> Any:
        """Value property.

        Get property value.

        Returns:
            Any: Property value

        """
        return self._value

    @value.setter
    def value(self, val: Any) -> Any:
        if self.sdc_resource:
            self.sdc_resource.set_property_value(self, val)
        self._value = val


@dataclass
class ComponentProperty:
    """Component property dataclass.

    Component properties are inputs objects in SDC, but in logic
        it's a property.

    """

    unique_id: str
    property_type: str
    name: str
    component: "Component"
    _value: Optional[Any] = field(repr=False, default=None)

    @property
    def value(self) -> Any:
        """Property value getter.

        Returns:
            Any: Property value

        """
        return self._value

    @value.setter
    def value(self, val: Any) -> None:
        """Property value setter.

        Set value both in an object and in SDC using it's HTTP API.

        Args:
            val (Any): Property value to set

        """
        self.component.set_property_value(self, val)
        self._value = val