diff options
author | Michal Jagiello <michal.jagiello@t-mobile.pl> | 2022-01-25 14:20:34 +0000 |
---|---|---|
committer | Michal Jagiello <michal.jagiello@t-mobile.pl> | 2022-01-27 12:03:46 +0000 |
commit | 9cc10c174cc0be43df44f27fd9a43bb8675695a9 (patch) | |
tree | 538bb76d3338a25db67bdf47379e3142d0429915 /onap_data_provider/resources/sdc_properties_mixins.py | |
parent | b1112188466a5712cd41f1a88b5f4bcf31949d15 (diff) |
Update properties of the SDC models
Issue-ID: INT-2053
Signed-off-by: Michal Jagiello <michal.jagiello@t-mobile.pl>
Change-Id: I44ea3ed17948c37698aaa1d2c3a5b83011e0b90a
Author: Theo Schönen <H.Schoenen@telekom.de>
Diffstat (limited to 'onap_data_provider/resources/sdc_properties_mixins.py')
-rw-r--r-- | onap_data_provider/resources/sdc_properties_mixins.py | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/onap_data_provider/resources/sdc_properties_mixins.py b/onap_data_provider/resources/sdc_properties_mixins.py new file mode 100644 index 0000000..9a2b625 --- /dev/null +++ b/onap_data_provider/resources/sdc_properties_mixins.py @@ -0,0 +1,109 @@ +"""SDC properties mixins module.""" +""" + 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 typing import Any, List, Union + +from onapsdk.exceptions import SDKException, ValidationError, ParameterError # type: ignore +from onapsdk.sdc.component import Component # type: ignore +from onapsdk.sdc.properties import Property # type: ignore +from onapsdk.sdc.sdc_resource import SdcResource # type: ignore + + +class SdcPropertiesMixins: + """Mixins class for properties handling. + + Mixin class for propoerties preparation for SdcResources and Components. + """ + + def set_properties( + self, propresource: Union[SdcResource, Component], data: List[Any] + ) -> None: + """Set properties an SdcResource. + + Args: + sdcresource (SdcResource): the SdcResource the properties should belong to + data (Dict[List, Any]): Data needed to create resource. + + Raises ValidationError + """ + for property_data in data: + + if any( + (prop.name == property_data["name"] for prop in propresource.properties) + ): + prop = propresource.get_property(property_data["name"]) + prop.value = property_data.get("value") + else: + proptype = property_data.get("type") + if proptype is None: + raise ValidationError( + f"New Property '{str(property_data['name'])}' is missing a type!" + ) + + property = Property( + name=property_data["name"], + property_type=proptype, + value=property_data.get("value"), + ) + try: + propresource.add_property(property) + except SDKException: + raise ParameterError( + f"Creation of new property '{str(property_data['name'])}' " + f"for resourceclass '{str(propresource.__class__.__name__)}' is not provided yet!" + ) + + def set_inputs( + self, propresource: Union[SdcResource, Component], data: List[Any] + ) -> None: + """Set inputs of an SdcResource. + + Args: + sdcresource (SdcResource): the SdcResource the inputs should belong to + data (Dict[str, Any]): Data needed to create resource. + + Raises ValidationError + """ + for property_data in data: + + if any( + (prop.name == property_data["name"] for prop in propresource.inputs) + ): + propresource.set_input_default_value( + propresource.get_input(property_data["name"]), + property_data.get("value"), + ) + else: + proptype = property_data.get("type") + if proptype is None: + raise ValidationError( + "New input '{0}' is missing a type!".format( + str(property_data["name"]) + ) + ) + + property = Property( + name=property_data["name"], + property_type=proptype, + value=property_data.get("value"), + ) + try: + propresource.add_property(property) + propresource.declare_input(property) + except SDKException: + raise ParameterError( + f"Creation of new input '{str(property_data['name'])}' is not provided yet!" + ) |