You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ariatosca.apache.org by av...@apache.org on 2017/06/01 14:10:27 UTC

[11/16] incubator-ariatosca git commit: Add input model

Add input model


Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/3943ad0b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/3943ad0b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/3943ad0b

Branch: refs/heads/ARIA-180-convert-parameter-to-one-to-many
Commit: 3943ad0b921001ff8f7110618975f16264e1cff0
Parents: 74cc94e
Author: Avia Efrat <av...@gigaspaces.com>
Authored: Sun May 21 20:10:50 2017 +0300
Committer: Avia Efrat <av...@gigaspaces.com>
Committed: Thu Jun 1 11:51:02 2017 +0300

----------------------------------------------------------------------
 aria/modeling/models.py                         |  11 +-
 aria/modeling/orchestration.py                  |   2 +-
 aria/modeling/relationship.py                   |   6 +-
 aria/modeling/service_common.py                 | 202 ++++++++++++++++++-
 aria/modeling/service_instance.py               |  14 +-
 aria/modeling/service_template.py               |  14 +-
 .../simple_v1_0/modeling/__init__.py            |  29 +--
 tests/modeling/test_models.py                   |   4 +-
 tests/orchestrator/test_workflow_runner.py      |   8 +-
 9 files changed, 249 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3943ad0b/aria/modeling/models.py
----------------------------------------------------------------------
diff --git a/aria/modeling/models.py b/aria/modeling/models.py
index f3acca6..d7b417b 100644
--- a/aria/modeling/models.py
+++ b/aria/modeling/models.py
@@ -73,6 +73,7 @@ __all__ = (
 
     # Common service models
     'Parameter',
+    'Input'
     'Output'
     'Type',
     'Metadata',
@@ -211,10 +212,15 @@ class Parameter(aria_declarative_base, service_common.ParameterBase):
     pass
 
 
+class Input(aria_declarative_base, service_common.InputBase):
+    # Temporarily, until we will separate the Parameter model into Input, Output, Property and
+    # Attribute, Parameter will represent only Property and Attribute.
+    pass
+
+
 class Output(aria_declarative_base, service_common.OutputBase):
     # Temporarily, until we will separate the Parameter model into Input, Output, Property and
-    # Attribute, Parameter will represent Input, Property and Attribute, and Output will represent
-    # the outputs.
+    # Attribute, Parameter will represent only Property and Attribute.
     pass
 
 
@@ -285,6 +291,7 @@ models_to_register = [
 
     # Common service models
     Parameter,
+    Input,
     Output,
     Type,
     Metadata,

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3943ad0b/aria/modeling/orchestration.py
----------------------------------------------------------------------
diff --git a/aria/modeling/orchestration.py b/aria/modeling/orchestration.py
index 97de552..3753090 100644
--- a/aria/modeling/orchestration.py
+++ b/aria/modeling/orchestration.py
@@ -115,7 +115,7 @@ class ExecutionBase(ModelMixin):
 
     @declared_attr
     def inputs(cls):
-        return relationship.many_to_many(cls, 'parameter', prefix='inputs', dict_key='name')
+        return relationship.many_to_many(cls, 'input', dict_key='name')
 
     # region foreign keys
 

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3943ad0b/aria/modeling/relationship.py
----------------------------------------------------------------------
diff --git a/aria/modeling/relationship.py b/aria/modeling/relationship.py
index c4f4cf3..8644d42 100644
--- a/aria/modeling/relationship.py
+++ b/aria/modeling/relationship.py
@@ -287,8 +287,10 @@ def many_to_many(model_class,
 
     if prefix is not None:
         secondary_table_name = '{0}_{1}'.format(prefix, secondary_table_name)
-    if other_property is None:
-        other_property = '{0}_{1}'.format(prefix, formatting.pluralize(this_table))
+        if other_property is None:
+            other_property = '{0}_{1}'.format(prefix, formatting.pluralize(this_table))
+    elif other_property is None:
+        other_property = '{0}_{1}'.format(other_table, formatting.pluralize(this_table))
 
     secondary_table = _get_secondary_table(
         model_class.metadata,

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3943ad0b/aria/modeling/service_common.py
----------------------------------------------------------------------
diff --git a/aria/modeling/service_common.py b/aria/modeling/service_common.py
index 0f22dfb..0571ec9 100644
--- a/aria/modeling/service_common.py
+++ b/aria/modeling/service_common.py
@@ -412,9 +412,9 @@ class OutputBase(TemplateModelMixin, caching.HasCachedMethods):
         """
         Wraps an arbitrary value as a parameter. The type will be guessed via introspection.
 
-        :param name: Parameter name
+        :param name: Output name
         :type name: basestring
-        :param value: Parameter value
+        :param value: Output value
         :param description: Description (optional)
         :type description: basestring
         """
@@ -536,6 +536,204 @@ class TypeBase(InstanceModelMixin):
         return [self] + (self.parent.hierarchy if self.parent else [])
 
 
+# TODO dry this code. currently it is a copy of ParameterBase
+class InputBase(TemplateModelMixin, caching.HasCachedMethods):
+    """
+    Represents a typed value. The value can contain nested intrinsic functions.
+
+    This model can be used as the ``container_holder`` argument for :func:`functions.evaluate`.
+
+    :ivar name: Name
+    :vartype name: basestring
+    :ivar type_name: Type name
+    :vartype type_name: basestring
+    :ivar value: Value
+    :ivar description: Description
+    :vartype description: basestring
+    """
+
+    __tablename__ = 'input'
+
+    name = Column(Text)
+    type_name = Column(Text)
+    description = Column(Text)
+    _value = Column(PickleType)
+
+    @property
+    def value(self):
+        value = self._value
+        if value is not None:
+            evaluation = functions.evaluate(value, self)
+            if evaluation is not None:
+                value = evaluation.value
+        return value
+
+    @value.setter
+    def value(self, value):
+        self._value = value
+
+    @property
+    @caching.cachedmethod
+    def owner(self):
+        """
+        The sole owner of this parameter, which is another model that relates to it.
+
+        *All* parameters should have an owner model. In case this property method fails to find
+        it, it will raise a ValueError, which should signify an abnormal, orphaned parameter.
+        """
+
+        # Find first non-null relationship
+        for the_relationship in self.__mapper__.relationships:
+            v = getattr(self, the_relationship.key)
+            if v:
+                return v[0] # because we are many-to-many, the back reference will be a list
+
+        raise ValueError('orphaned input: does not have an owner: {0}'.format(self.name))
+
+
+    @property
+    @caching.cachedmethod
+    def container(self): # pylint: disable=too-many-return-statements,too-many-branches
+        """
+        The logical container for this parameter, which would be another model: service, node,
+        group, or policy (or their templates).
+
+        The logical container is equivalent to the ``SELF`` keyword used by intrinsic functions in
+        TOSCA.
+
+        *All* parameters should have a container model. In case this property method fails to find
+        it, it will raise a ValueError, which should signify an abnormal, orphaned parameter.
+        """
+
+        from . import models
+
+        container = self.owner
+
+        # Extract interface from operation
+        if isinstance(container, models.Operation):
+            container = container.interface
+        elif isinstance(container, models.OperationTemplate):
+            container = container.interface_template
+
+        # Extract from other models
+        if isinstance(container, models.Interface):
+            container = container.node or container.group or container.relationship
+        elif isinstance(container, models.InterfaceTemplate):
+            container = container.node_template or container.group_template \
+                        or container.relationship_template
+        elif isinstance(container, models.Capability) or isinstance(container, models.Artifact):
+            container = container.node
+        elif isinstance(container, models.CapabilityTemplate) \
+                or isinstance(container, models.ArtifactTemplate):
+            container = container.node_template
+        elif isinstance(container, models.Task):
+            container = container.actor
+
+        # Extract node from relationship
+        if isinstance(container, models.Relationship):
+            container = container.source_node
+        elif isinstance(container, models.RelationshipTemplate):
+            container = container.requirement_template.node_template
+
+        if container is not None:
+            return container
+
+        raise ValueError('orphaned input: does not have a container: {0}'.format(self.name))
+
+    @property
+    @caching.cachedmethod
+    def service(self):
+        """
+        The :class:`Service` containing this parameter, or None if not contained in a service.
+        """
+
+        from . import models
+        container = self.container
+        if isinstance(container, models.Service):
+            return container
+        elif hasattr(container, 'service'):
+            return container.service
+        return None
+
+    @property
+    @caching.cachedmethod
+    def service_template(self):
+        """
+        The :class:`ServiceTemplate` containing this parameter, or None if not contained in a
+        service template.
+        """
+
+        from . import models
+        container = self.container
+        if isinstance(container, models.ServiceTemplate):
+            return container
+        elif hasattr(container, 'service_template'):
+            return container.service_template
+        return None
+
+    @property
+    def as_raw(self):
+        return collections.OrderedDict((
+            ('name', self.name),
+            ('type_name', self.type_name),
+            ('value', self.value),
+            ('description', self.description)))
+
+    def instantiate(self, container):
+        from . import models
+        return models.Input(name=self.name,  # pylint: disable=unexpected-keyword-arg
+                            type_name=self.type_name,
+                            _value=self._value,
+                            description=self.description)
+
+    def coerce_values(self, report_issues):
+        value = self._value
+        if value is not None:
+            evaluation = functions.evaluate(value, self, report_issues)
+            if (evaluation is not None) and evaluation.final:
+                # A final evaluation can safely replace the existing value
+                self._value = evaluation.value
+
+    def dump(self):
+        context = ConsumptionContext.get_thread_local()
+        if self.type_name is not None:
+            console.puts('{0}: {1} ({2})'.format(
+                context.style.property(self.name),
+                context.style.literal(formatting.as_raw(self.value)),
+                context.style.type(self.type_name)))
+        else:
+            console.puts('{0}: {1}'.format(
+                context.style.property(self.name),
+                context.style.literal(formatting.as_raw(self.value))))
+        if self.description:
+            console.puts(context.style.meta(self.description))
+
+    def unwrap(self):
+        return self.name, self.value
+
+    @classmethod
+    def wrap(cls, name, value, description=None):
+        """
+        Wraps an arbitrary value as a parameter. The type will be guessed via introspection.
+
+        :param name: Input name
+        :type name: basestring
+        :param value: Input value
+        :param description: Description (optional)
+        :type description: basestring
+        """
+
+        from . import models
+        type_name = canonical_type_name(value)
+        if type_name is None:
+            type_name = full_type_name(value)
+        return models.Input(name=name, # pylint: disable=unexpected-keyword-arg
+                            type_name=type_name,
+                            value=value,
+                            description=description)
+
+
+
 class MetadataBase(TemplateModelMixin):
     """
     Custom values associated with the service.

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3943ad0b/aria/modeling/service_instance.py
----------------------------------------------------------------------
diff --git a/aria/modeling/service_instance.py b/aria/modeling/service_instance.py
index b04e296..b97e156 100644
--- a/aria/modeling/service_instance.py
+++ b/aria/modeling/service_instance.py
@@ -67,7 +67,7 @@ class ServiceBase(InstanceModelMixin):
     :ivar substitution: The entire service can appear as a node
     :vartype substitution: :class:`Substitution`
     :ivar inputs: Externally provided parameters
-    :vartype inputs: {basestring: :class:`Parameter`}
+    :vartype inputs: {basestring: :class:`Input`}
     :ivar outputs: These parameters are filled in after service installation
     :vartype outputs: {basestring: :class:`Output`}
     :ivar workflows: Custom workflows that can be performed on the service
@@ -171,7 +171,7 @@ class ServiceBase(InstanceModelMixin):
 
     @declared_attr
     def inputs(cls):
-        return relationship.many_to_many(cls, 'parameter', prefix='inputs', dict_key='name')
+        return relationship.many_to_many(cls, 'input', dict_key='name')
 
     @declared_attr
     def outputs(cls):
@@ -1492,8 +1492,8 @@ class InterfaceBase(InstanceModelMixin):
     :vartype type: :class:`Type`
     :ivar description: Human-readable description
     :vartype description: string
-    :ivar inputs: Parameters that can be used by all operations in the interface
-    :vartype inputs: {basestring: :class:`Parameter`}
+    :ivar inputs: Inputs that can be used by all operations in the interface
+    :vartype inputs: {basestring: :class:`Input`}
     :ivar operations: Operations
     :vartype operations: {basestring: :class:`Operation`}
     :ivar node: Containing node
@@ -1585,7 +1585,7 @@ class InterfaceBase(InstanceModelMixin):
 
     @declared_attr
     def inputs(cls):
-        return relationship.many_to_many(cls, 'parameter', prefix='inputs', dict_key='name')
+        return relationship.many_to_many(cls, 'input', dict_key='name')
 
     # endregion
 
@@ -1643,7 +1643,7 @@ class OperationBase(InstanceModelMixin):
     :vartype implementation: basestring
     :ivar dependencies: Dependency strings (interpreted by the plugin)
     :vartype dependencies: [basestring]
-    :ivar inputs: Parameters that can be used by this operation
+    :ivar inputs: Input that can be used by this operation
     :vartype inputs: {basestring: :class:`Parameter`}
     :ivar plugin: Associated plugin
     :vartype plugin: :class:`Plugin`
@@ -1732,7 +1732,7 @@ class OperationBase(InstanceModelMixin):
 
     @declared_attr
     def inputs(cls):
-        return relationship.many_to_many(cls, 'parameter', prefix='inputs', dict_key='name')
+        return relationship.many_to_many(cls, 'input', dict_key='name')
 
     @declared_attr
     def configuration(cls):

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3943ad0b/aria/modeling/service_template.py
----------------------------------------------------------------------
diff --git a/aria/modeling/service_template.py b/aria/modeling/service_template.py
index d1e79c9..0724d30 100644
--- a/aria/modeling/service_template.py
+++ b/aria/modeling/service_template.py
@@ -67,7 +67,7 @@ class ServiceTemplateBase(TemplateModelMixin):
     :ivar substitution_template: The entire service can appear as a node
     :vartype substitution_template: :class:`SubstitutionTemplate`
     :ivar inputs: Externally provided parameters
-    :vartype inputs: {basestring: :class:`Parameter`}
+    :vartype inputs: {basestring: :class:`Input`}
     :ivar outputs: These parameters are filled in after service installation
     :vartype outputs: {basestring: :class:`Output`}
     :ivar workflow_templates: Custom workflows that can be performed on the service
@@ -245,7 +245,7 @@ class ServiceTemplateBase(TemplateModelMixin):
 
     @declared_attr
     def inputs(cls):
-        return relationship.many_to_many(cls, 'parameter', prefix='inputs', dict_key='name')
+        return relationship.many_to_many(cls, 'input', dict_key='name')
 
     @declared_attr
     def outputs(cls):
@@ -1616,8 +1616,8 @@ class InterfaceTemplateBase(TemplateModelMixin):
     :vartype type: :class:`Type`
     :ivar description: Human-readable description
     :vartype description: basestring
-    :ivar inputs: Parameters that can be used by all operations in the interface
-    :vartype inputs: {basestring: :class:`Parameter`}
+    :ivar inputs: Inputs that can be used by all operations in the interface
+    :vartype inputs: {basestring: :class:`Input`}
     :ivar operation_templates: Operations
     :vartype operation_templates: {basestring: :class:`OperationTemplate`}
     :ivar node_template: Containing node template
@@ -1707,7 +1707,7 @@ class InterfaceTemplateBase(TemplateModelMixin):
 
     @declared_attr
     def inputs(cls):
-        return relationship.many_to_many(cls, 'parameter', prefix='inputs', dict_key='name')
+        return relationship.many_to_many(cls, 'input', dict_key='name')
 
     # endregion
 
@@ -1770,7 +1770,7 @@ class OperationTemplateBase(TemplateModelMixin):
     :vartype implementation: basestring
     :ivar dependencies: Dependency strings (interpreted by the plugin)
     :vartype dependencies: [basestring]
-    :ivar inputs: Parameters that can be used by this operation
+    :ivar inputs: Inputs that can be used by this operation
     :vartype inputs: {basestring: :class:`Parameter`}
     :ivar plugin_specification: Associated plugin
     :vartype plugin_specification: :class:`PluginSpecification`
@@ -1855,7 +1855,7 @@ class OperationTemplateBase(TemplateModelMixin):
 
     @declared_attr
     def inputs(cls):
-        return relationship.many_to_many(cls, 'parameter', prefix='inputs', dict_key='name')
+        return relationship.many_to_many(cls, 'input', dict_key='name')
 
     @declared_attr
     def configuration(cls):

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3943ad0b/extensions/aria_extension_tosca/simple_v1_0/modeling/__init__.py
----------------------------------------------------------------------
diff --git a/extensions/aria_extension_tosca/simple_v1_0/modeling/__init__.py b/extensions/aria_extension_tosca/simple_v1_0/modeling/__init__.py
index 1f298ac..0075824 100644
--- a/extensions/aria_extension_tosca/simple_v1_0/modeling/__init__.py
+++ b/extensions/aria_extension_tosca/simple_v1_0/modeling/__init__.py
@@ -36,7 +36,7 @@ from aria.modeling.models import (Type, ServiceTemplate, NodeTemplate,
                                   RequirementTemplate, RelationshipTemplate, CapabilityTemplate,
                                   GroupTemplate, PolicyTemplate, SubstitutionTemplate,
                                   SubstitutionTemplateMapping, InterfaceTemplate, OperationTemplate,
-                                  ArtifactTemplate, Metadata, Parameter, Output,
+                                  ArtifactTemplate, Metadata, Parameter, Input, Output,
                                   PluginSpecification)
 
 from .parameters import coerce_parameter_value
@@ -95,7 +95,8 @@ def create_service_template_model(context): # pylint: disable=too-many-locals,to
     topology_template = context.presentation.get('service_template', 'topology_template')
     if topology_template is not None:
         create_parameter_models_from_values(model.inputs,
-                                            topology_template._get_input_values(context))
+                                            topology_template._get_input_values(context),
+                                            model_class=Input)
         create_parameter_models_from_values(model.outputs,
                                             topology_template._get_output_values(context),
                                             model_class=Output)
@@ -359,10 +360,10 @@ def create_interface_template_model(context, service_template, interface):
     inputs = interface.inputs
     if inputs:
         for input_name, the_input in inputs.iteritems():
-            model.inputs[input_name] = Parameter(name=input_name, # pylint: disable=unexpected-keyword-arg
-                                                 type_name=the_input.value.type,
-                                                 value=the_input.value.value,
-                                                 description=the_input.value.description)
+            model.inputs[input_name] = Input(name=input_name, # pylint: disable=unexpected-keyword-arg
+                                             type_name=the_input.value.type,
+                                             value=the_input.value.value,
+                                             description=the_input.value.description)
 
     operations = interface.operations
     if operations:
@@ -427,10 +428,10 @@ def create_operation_template_model(context, service_template, operation):
     inputs = operation.inputs
     if inputs:
         for input_name, the_input in inputs.iteritems():
-            model.inputs[input_name] = Parameter(name=input_name, # pylint: disable=unexpected-keyword-arg
-                                                 type_name=the_input.value.type,
-                                                 value=the_input.value.value,
-                                                 description=the_input.value.description)
+            model.inputs[input_name] = Input(name=input_name, # pylint: disable=unexpected-keyword-arg
+                                             type_name=the_input.value.type,
+                                             value=the_input.value.value,
+                                             description=the_input.value.description)
 
     return model
 
@@ -521,10 +522,10 @@ def create_workflow_operation_template_model(context, service_template, policy):
         if prop_name == 'implementation':
             model.function = prop.value
         else:
-            model.inputs[prop_name] = Parameter(name=prop_name, # pylint: disable=unexpected-keyword-arg
-                                                type_name=prop.type,
-                                                value=prop.value,
-                                                description=prop.description)
+            model.inputs[prop_name] = Input(name=prop_name, # pylint: disable=unexpected-keyword-arg
+                                            type_name=prop.type,
+                                            value=prop.value,
+                                            description=prop.description)
 
     used_reserved_names = WORKFLOW_DECORATOR_RESERVED_ARGUMENTS.intersection(model.inputs.keys())
     if used_reserved_names:

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3943ad0b/tests/modeling/test_models.py
----------------------------------------------------------------------
diff --git a/tests/modeling/test_models.py b/tests/modeling/test_models.py
index df3aebd..10a7283 100644
--- a/tests/modeling/test_models.py
+++ b/tests/modeling/test_models.py
@@ -36,7 +36,7 @@ from aria.modeling.models import (
     Relationship,
     NodeTemplate,
     Node,
-    Parameter,
+    Input,
     Type
 )
 
@@ -622,7 +622,7 @@ class TestNodeHostAddress(object):
             service_template=storage.service_template.list()[0]
         )
         if host_address:
-            kwargs['properties'] = {'host_address': Parameter.wrap('host_address', host_address)}
+            kwargs['properties'] = {'host_address': Input.wrap('host_address', host_address)}
         node = NodeTemplate(**kwargs)
         storage.node_template.put(node)
         return node

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/3943ad0b/tests/orchestrator/test_workflow_runner.py
----------------------------------------------------------------------
diff --git a/tests/orchestrator/test_workflow_runner.py b/tests/orchestrator/test_workflow_runner.py
index 3646339..9d3698f 100644
--- a/tests/orchestrator/test_workflow_runner.py
+++ b/tests/orchestrator/test_workflow_runner.py
@@ -170,7 +170,7 @@ def test_execution_inputs_override_workflow_inputs(request):
     wf_inputs = {'input1': 'value1', 'input2': 'value2', 'input3': 5}
     mock_workflow = _setup_mock_workflow_in_service(
         request,
-        inputs=dict((name, models.Parameter.wrap(name, val)) for name, val
+        inputs=dict((name, models.Input.wrap(name, val)) for name, val
                     in wf_inputs.iteritems()))
 
     with mock.patch('aria.orchestrator.workflow_runner.Engine'):
@@ -195,7 +195,7 @@ def test_execution_inputs_undeclared_inputs(request):
 
 def test_execution_inputs_missing_required_inputs(request):
     mock_workflow = _setup_mock_workflow_in_service(
-        request, inputs={'required_input': models.Parameter.wrap('required_input', value=None)})
+        request, inputs={'required_input': models.Input.wrap('required_input', value=None)})
 
     with pytest.raises(modeling_exceptions.MissingRequiredParametersException):
         _create_workflow_runner(request, mock_workflow, inputs={})
@@ -203,7 +203,7 @@ def test_execution_inputs_missing_required_inputs(request):
 
 def test_execution_inputs_wrong_type_inputs(request):
     mock_workflow = _setup_mock_workflow_in_service(
-        request, inputs={'input': models.Parameter.wrap('input', 'value')})
+        request, inputs={'input': models.Input.wrap('input', 'value')})
 
     with pytest.raises(modeling_exceptions.ParametersOfWrongTypeException):
         _create_workflow_runner(request, mock_workflow, inputs={'input': 5})
@@ -224,7 +224,7 @@ def test_workflow_function_parameters(request, tmpdir):
     wf_inputs = {'output_path': output_path, 'input1': 'value1', 'input2': 'value2', 'input3': 5}
 
     mock_workflow = _setup_mock_workflow_in_service(
-        request, inputs=dict((name, models.Parameter.wrap(name, val)) for name, val
+        request, inputs=dict((name, models.Input.wrap(name, val)) for name, val
                              in wf_inputs.iteritems()))
 
     _create_workflow_runner(request, mock_workflow,