aboutsummaryrefslogtreecommitdiffstats
path: root/onap_data_provider/property_tag
diff options
context:
space:
mode:
Diffstat (limited to 'onap_data_provider/property_tag')
-rw-r--r--onap_data_provider/property_tag/__init__.py21
-rw-r--r--onap_data_provider/property_tag/base.py73
-rw-r--r--onap_data_provider/property_tag/properties_getter.py64
-rw-r--r--onap_data_provider/property_tag/sdc_service.py52
4 files changed, 210 insertions, 0 deletions
diff --git a/onap_data_provider/property_tag/__init__.py b/onap_data_provider/property_tag/__init__.py
new file mode 100644
index 0000000..1e88be1
--- /dev/null
+++ b/onap_data_provider/property_tag/__init__.py
@@ -0,0 +1,21 @@
+"""Resource property tag package.
+
+There the package modules are going to be implemented.
+Each module represents one resource which can be used with "!onap_resource_property" tag.
+
+"""
+"""
+ Copyright 2022 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.
+""" \ No newline at end of file
diff --git a/onap_data_provider/property_tag/base.py b/onap_data_provider/property_tag/base.py
new file mode 100644
index 0000000..7ae7102
--- /dev/null
+++ b/onap_data_provider/property_tag/base.py
@@ -0,0 +1,73 @@
+"""Base module for property tag classes."""
+"""
+ Copyright 2022 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 abc import ABC
+from typing import Any, Type
+
+from onapsdk.onap_service import OnapService # type: ignore
+
+
+class BasePropertyTagResource(ABC):
+ """Base property tag resource class.
+
+ Abstract class which all resource classes should inherit from.
+ Subclasses has to implement `resource` property to get the valid object
+ using Python ONAP SDK classes.
+
+ Subclass could also implement `__init__` method to get more attributes from
+ the user which uses the tag.
+
+ """
+
+ def __init__(self, property_name: str) -> None:
+ """Init property tag resource object.
+
+ Args:
+ property_name (str): Name of the property to get
+
+ """
+ self.property_name: str = property_name
+
+ @property
+ def resource(self) -> OnapService:
+ """Resource property abstract method.
+
+ Returns an object from which the property is going to be get.
+
+ Raises:
+ NotImplementedError: That method is an abstract one
+
+ Returns:
+ OnapService: Any OnapService subclass
+
+ """
+ raise NotImplementedError
+
+ @property
+ def resource_property(self) -> Any:
+ """Resource property.
+
+ Using `getattr` function get the property from resource.
+
+ Returns:
+ Any: Property value
+
+ Raises:
+ AttributeError: Resource has no property with given name.
+
+ """
+ return getattr(self.resource, self.property_name)
diff --git a/onap_data_provider/property_tag/properties_getter.py b/onap_data_provider/property_tag/properties_getter.py
new file mode 100644
index 0000000..df29314
--- /dev/null
+++ b/onap_data_provider/property_tag/properties_getter.py
@@ -0,0 +1,64 @@
+"""Properties getter module."""
+"""
+ Copyright 2022 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 logging
+from typing import Any, Mapping, Type, TYPE_CHECKING
+
+from .base import BasePropertyTagResource
+from .sdc_service import SdcServicePropertyTagResource
+
+if TYPE_CHECKING:
+ from typing import Any, Mapping
+
+
+class PropertiesGetter:
+ """Properties getter class.
+
+ Uses to get values of properties from already existing ONAP resources.
+
+ Contains a mapper to select right class to get the values from.
+ """
+
+ RESOURCES: Mapping[str, Type[BasePropertyTagResource]] = {
+ "service": SdcServicePropertyTagResource
+ }
+
+ @classmethod
+ def get_property(cls, resource_type: str, *args: str) -> Any:
+ """Get property class method.
+
+ Maps the input `resource_type` into `BasePropertyTagResource` class
+ and get it's `resource_property` property
+
+ Args:
+ resource_type (str): Type of the resource - uses by a mapper
+ *args (str): Args to be passed to the service class init
+
+ Raises:
+ ValueError: Given resource type is not supported
+
+ Returns:
+ Any: Resource property value
+
+ """
+ try:
+ return cls.RESOURCES[resource_type](*args).resource_property
+ except KeyError:
+ msg = f"Resource type \"{resource_type}\" is not supported"
+ logging.error(msg)
+ raise ValueError(msg)
+
diff --git a/onap_data_provider/property_tag/sdc_service.py b/onap_data_provider/property_tag/sdc_service.py
new file mode 100644
index 0000000..b8eca80
--- /dev/null
+++ b/onap_data_provider/property_tag/sdc_service.py
@@ -0,0 +1,52 @@
+"""SDC property module used by tag to get resource's property"""
+"""
+ Copyright 2022 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 Optional
+
+from onapsdk.onap_service import OnapService # type: ignore
+from onapsdk.sdc.service import Service # type: ignore
+
+from .base import BasePropertyTagResource
+
+
+class SdcServicePropertyTagResource(BasePropertyTagResource):
+ """Class to get property from SDC service module objects."""
+
+ def __init__(self, property_name: str, service_name: str, version: Optional[str] = None) -> None:
+ """Initialize object.
+
+ Get the name of the property to get, serivce name and optional version of the service.
+
+ Args:
+ property_name (str): Property name
+ service_name (str): Service name
+ version (Optional[str], optional): Optional version. If no version is given
+ the latest version of the service will be loaded. Defaults to None.
+ """
+ super().__init__(property_name)
+ self.service_name: str = service_name
+ self.version: Optional[str] = version
+
+ @property
+ def resource(self) -> Service:
+ """Service resource.
+
+ Returns:
+ Service: Service with the provided name and version.
+
+ """
+ return Service(self.service_name, version=self.version)