diff options
Diffstat (limited to 'onap_data_provider/resources')
-rw-r--r-- | onap_data_provider/resources/sdc_properties_mixins.py | 109 | ||||
-rw-r--r-- | onap_data_provider/resources/service_resource.py | 36 | ||||
-rw-r--r-- | onap_data_provider/resources/xnf_resource.py | 14 |
3 files changed, 131 insertions, 28 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!" + ) diff --git a/onap_data_provider/resources/service_resource.py b/onap_data_provider/resources/service_resource.py index 8489982..ab05bde 100644 --- a/onap_data_provider/resources/service_resource.py +++ b/onap_data_provider/resources/service_resource.py @@ -14,26 +14,22 @@ See the License for the specific language governing permissions and limitations under the License. """ -from typing import Any, Dict, Mapping, Optional, Type +from typing import Any, Dict, List, Mapping, Optional, Type from onapsdk.sdc.pnf import Pnf # type: ignore -from onapsdk.sdc.properties import Property # type: ignore from onapsdk.sdc.sdc_resource import SdcResource # type: ignore from onapsdk.sdc.service import Service, ServiceInstantiationType # type: ignore from onapsdk.sdc.vf import Vf # type: ignore from onapsdk.sdc.vl import Vl # type: ignore from .resource import Resource +from .sdc_properties_mixins import SdcPropertiesMixins -class ServiceResource(Resource): +class ServiceResource(Resource, SdcPropertiesMixins): """Service resource class.""" - RESOURCES: Mapping[str, Type[SdcResource]] = { - "PNF": Pnf, - "VF": Vf, - "VL": Vl, - } + RESOURCES: Mapping[str, Type[SdcResource]] = {"PNF": Pnf, "VF": Vf, "VL": Vl} def __init__(self, data: Dict[str, Any]) -> None: """Initialize Service resource. @@ -59,17 +55,19 @@ class ServiceResource(Resource): ) service.add_resource(resource) component = service.get_component(resource) - for prop_key, prop_value in resource_data.get("properties", {}).items(): - prop = component.get_property(prop_key) - prop.value = prop_value - for property_data in self.data.get("properties", []): - service.add_property( - Property( - property_data["name"], - property_data["type"], - value=property_data.get("value"), - ) - ) + properties_data = resource_data.get("properties", []) + if properties_data and isinstance(properties_data, List): + self.set_properties(component, properties_data) + else: + # backward compatibility + for prop_key, prop_value in resource_data.get( + "properties", {} + ).items(): + prop = component.get_property(prop_key) + prop.value = prop_value + self.set_inputs(component, resource_data.get("inputs", [])) + self.set_properties(service, self.data.get("properties", [])) + self.set_inputs(service, self.data.get("inputs", [])) service.checkin() service.onboard() self._service = service diff --git a/onap_data_provider/resources/xnf_resource.py b/onap_data_provider/resources/xnf_resource.py index cada088..efede03 100644 --- a/onap_data_provider/resources/xnf_resource.py +++ b/onap_data_provider/resources/xnf_resource.py @@ -21,8 +21,10 @@ from onapsdk.sdc.vsp import Vsp # type: ignore from onapsdk.sdc.sdc_resource import SdcResource # type: ignore from onapsdk.sdc.properties import Property # type: ignore +from .sdc_properties_mixins import SdcPropertiesMixins -class XnfResource(ABC): + +class XnfResource(ABC, SdcPropertiesMixins): """Xnf resource class. Network function base class. @@ -49,12 +51,6 @@ class XnfResource(ABC): artifact_label=data["deployment_artifact"]["artifact_label"], artifact=data["deployment_artifact"]["artifact_file_name"], ) - for property_data in data.get("properties", []): - self._xnf.add_property( - Property( - name=property_data["name"], - property_type=property_data["type"], - value=property_data.get("value"), - ) - ) + self.set_properties(self._xnf, data.get("properties", [])) + self.set_inputs(self._xnf, data.get("inputs", [])) self._xnf.onboard() |