ariatosca-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From emblempar...@apache.org
Subject [1/4] incubator-ariatosca git commit: Modeling integation complete -- except testing ; )
Date Wed, 01 Mar 2017 22:10:26 GMT
Repository: incubator-ariatosca
Updated Branches:
  refs/heads/ARIA-105-integrate-modeling 0150da178 -> 58aac8da2


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/58aac8da/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 9945438..44e3cc5 100644
--- a/extensions/aria_extension_tosca/simple_v1_0/modeling/__init__.py
+++ b/extensions/aria_extension_tosca/simple_v1_0/modeling/__init__.py
@@ -23,24 +23,13 @@ from aria.modeling.models import (ServiceTemplate, NodeTemplate,
                                   GroupTemplate, PolicyTemplate, SubstitutionTemplate,
                                   SubstitutionTemplateMapping, InterfaceTemplate, OperationTemplate,
                                   ArtifactTemplate, Metadata, Parameter, Plugin)
-from aria.utils.console import puts, Colored
-from aria.utils.formatting import safe_repr
-
-#from aria.modeling.model import (Type, RelationshipType, PolicyType, ServiceTemplate, NodeTemplate,
-#                                 RequirementTemplate, RelationshipTemplate, CapabilityTemplate,
-#                                 GroupTemplate, PolicyTemplate, SubstitutionTemplate,
-#                                 MappingTemplate, InterfaceTemplate, OperationTemplate,
-#                                 ArtifactTemplate, Metadata, Parameter)
 
 from ..data_types import coerce_value
 
-#from platform import node
-
 
-def create_service_model(context): # pylint: disable=too-many-locals,too-many-branches
-    now = datetime.now()
-    model = ServiceTemplate(created_at=now,
-                            updated_at=now)
+def create_service_template_model(context): # pylint: disable=too-many-locals,too-many-branches
+    model = ServiceTemplate(created_at=datetime.now(),
+                            main_file_name='')
 
     model.description = context.presentation.get('service_template', 'description', 'value')
 
@@ -68,11 +57,11 @@ def create_service_model(context): # pylint: disable=too-many-locals,too-many-br
     create_types(context,
                  context.modeling.relationship_types,
                  context.presentation.get('service_template', 'relationship_types'),
-                 create_relationship_type)
+                 create_relationship_type_model)
     create_types(context,
                  context.modeling.policy_types,
                  context.presentation.get('service_template', 'policy_types'),
-                 create_policy_type)
+                 create_policy_type_model)
     create_types(context,
                  context.modeling.artifact_types,
                  context.presentation.get('service_template', 'artifact_types'))
@@ -83,57 +72,78 @@ def create_service_model(context): # pylint: disable=too-many-locals,too-many-br
     # Topology template
     topology_template = context.presentation.get('service_template', 'topology_template')
     if topology_template is not None:
-        create_properties_from_values(model.inputs, topology_template._get_input_values(context))
-        create_properties_from_values(model.outputs, topology_template._get_output_values(context))
+        create_parameter_models_from_values(model.inputs,
+                                            topology_template._get_input_values(context))
+        create_parameter_models_from_values(model.outputs,
+                                            topology_template._get_output_values(context))
 
-    # Policies
+    # Policy templates
     # (We need to do this before node and group templates, because we need plugins populated)
     policies = context.presentation.get('service_template', 'topology_template', 'policies')
     if policies:
         for policy in policies.itervalues():
-            model.policy_templates.append(create_policy_template(context, policy))
+            model.policy_templates.append(create_policy_template_model(context, policy))
             
             if context.modeling.policy_types.get_role(policy.type) == 'plugin':
-                model.plugins[policy._name] = create_plugin(context, policy)
+                model.plugins[policy._name] = create_plugin_model(context, policy)
 
     # Node templates
     node_templates = context.presentation.get('service_template', 'topology_template',
                                               'node_templates')
     if node_templates:
         for node_template in node_templates.itervalues():
-            model.node_templates.append(create_node_template(context, model, node_template))
+            model.node_templates.append(create_node_template_model(context, model, node_template))
 
-    # Groups
+    # Group templates
     groups = context.presentation.get('service_template', 'topology_template', 'groups')
     if groups:
         for group in groups.itervalues():
-            model.group_templates.append(create_group_template(context, model, group))
+            model.group_templates.append(create_group_template_model(context, model, group))
 
-    # Substitution
+    # Substitution template
     substitution_mappings = context.presentation.get('service_template', 'topology_template',
                                                      'substitution_mappings')
-    if substitution_mappings is not None:
-        substitution_template = SubstitutionTemplate(node_type_name=substitution_mappings.node_type)
-        capabilities = substitution_mappings.capabilities
-        if capabilities:
-            for mapped_capability_name, capability in capabilities.iteritems():
-                substitution_template.mappings[mapped_capability_name] = \
-                    SubstitutionTemplateMapping(mapped_name='capability.' + mapped_capability_name,
-                                                node_template_name=capability.node_template,
-                                                name='capability.' + capability.capability)
-        requirements = substitution_mappings.requirements
-        if requirements:
-            for mapped_requirement_name, requirement in requirements.iteritems():
-                substitution_template.mappings[mapped_requirement_name] = \
-                    SubstitutionTemplateMapping(mapped_name='requirement.' + mapped_requirement_name,
-                                                node_template_name=requirement.node_template,
-                                                name='requirement.' + requirement.requirement)
-        model.substitution_template = substitution_template
+    if substitution_mappings:
+        model.substitution_template = create_substituion_template_model(context, model,
+                                                                        substitution_mappings)
 
     return model
 
 
-def create_plugin(context, policy):
+def create_substituion_template_model(context, service_template, substitution_mappings):
+    model = SubstitutionTemplate(node_type_name=substitution_mappings.node_type)
+
+    capabilities = substitution_mappings.capabilities
+    if capabilities:
+        for mapped_capability_name, capability in capabilities.iteritems():
+            name = 'capability.' + mapped_capability_name
+            node_template_model = service_template.get_node_template(capability.node_template)
+            capability_template_model = \
+                node_template_model.capability_templates[capability.capability]
+            model.mappings[name] = \
+                SubstitutionTemplateMapping(name=name,
+                                            node_template=node_template_model,
+                                            capability_template=capability_template_model)
+
+    requirements = substitution_mappings.requirements
+    if requirements:
+        for mapped_requirement_name, requirement in requirements.iteritems():
+            name = 'requirement.' + mapped_requirement_name
+            node_template_model = service_template.get_node_template(requirement.node_template)
+            requirement_template_model = None
+            for a_model in node_template_model.requirement_templates:
+                if a_model.name == requirement.requirement:
+                    requirement_template_model = a_model
+                    break
+            model.mappings[name] = \
+                SubstitutionTemplateMapping(name=name,
+                                            node_template=node_template_model,
+                                            requirement_template=requirement_template_model)
+
+    return model
+
+
+def create_plugin_model(context, policy):
     properties = policy.properties
 
     def get(name):
@@ -157,7 +167,8 @@ def create_plugin(context, policy):
 
     return model
 
-def create_node_template(context, service_template, node_template):
+
+def create_node_template_model(context, service_template, node_template):
     node_type = node_template._get_type(context)
     model = NodeTemplate(name=node_template._name, type_name=node_type._name)
     
@@ -167,27 +178,29 @@ def create_node_template(context, service_template, node_template):
     if node_template.description:
         model.description = node_template.description.value
 
-    create_properties_from_values(model.properties, node_template._get_property_values(context))
-    create_interface_templates(context, service_template, model.interface_templates,
-                               node_template._get_interfaces(context))
+    create_parameter_models_from_values(model.properties,
+                                        node_template._get_property_values(context))
+    create_interface_template_models(context, service_template, model.interface_templates,
+                                     node_template._get_interfaces(context))
 
     artifacts = node_template._get_artifacts(context)
     if artifacts:
         for artifact_name, artifact in artifacts.iteritems():
-            model.artifact_templates[artifact_name] = create_artifact_template(context, artifact)
+            model.artifact_templates[artifact_name] = create_artifact_template_model(context,
+                                                                                     artifact)
 
     requirements = node_template._get_requirements(context)
     if requirements:
         for _, requirement in requirements:
-            model.requirement_templates.append(create_requirement_template(context,
-                                                                           service_template,
-                                                                           requirement))
+            model.requirement_templates.append(create_requirement_template_model(context,
+                                                                                 service_template,
+                                                                                 requirement))
 
     capabilities = node_template._get_capabilities(context)
     if capabilities:
         for capability_name, capability in capabilities.iteritems():
-            model.capability_templates[capability_name] = create_capability_template(context,
-                                                                                     capability)
+            model.capability_templates[capability_name] = \
+                create_capability_template_model(context, capability)
 
     if model.target_node_template_constraints:
         model.target_node_template_constraints = []
@@ -197,7 +210,7 @@ def create_node_template(context, service_template, node_template):
     return model
 
 
-def create_interface_template(context, service_template, interface):
+def create_interface_template_model(context, service_template, interface):
     interface_type = interface._get_type(context)
     model = InterfaceTemplate(name=interface._name, type_name=interface_type._name)
 
@@ -215,14 +228,13 @@ def create_interface_template(context, service_template, interface):
     operations = interface.operations
     if operations:
         for operation_name, operation in operations.iteritems():
-            model.operation_templates[operation_name] = create_operation_template(context,
-                                                                                  service_template,
-                                                                                  operation)
+            model.operation_templates[operation_name] = \
+                create_operation_template_model(context, service_template, operation)
 
     return model if model.operation_templates else None
 
 
-def create_operation_template(context, service_template, operation): # pylint: disable=unused-argument
+def create_operation_template_model(context, service_template, operation): # pylint: disable=unused-argument
     model = OperationTemplate(name=operation._name)
 
     if operation.description:
@@ -231,7 +243,7 @@ def create_operation_template(context, service_template, operation): #
pylint: d
     implementation = operation.implementation
     if (implementation is not None) and operation.implementation.primary:
         model.plugin, model.implementation = \
-            _parse_implementation(context, service_template, operation.implementation.primary)
+            parse_implementation_string(context, service_template, operation.implementation.primary)
 
         dependencies = implementation.dependencies
         if dependencies is not None:
@@ -245,69 +257,10 @@ def create_operation_template(context, service_template, operation):
# pylint: d
                                                  str_value=str(the_input.value.value),
                                                  description=the_input.value.description)
 
-    # Dry
-    implementation = model.implementation
-    model.implementation = '{0}.{1}'.format(__name__, '_dry_node')
-    model.inputs['_implementation'] = Parameter(name='_implementation',
-                                                type_name='str',
-                                                str_value=implementation)
-    model.inputs['_plugin'] = Parameter(name='_plugin',
-                                        type_name='str',
-                                        str_value=model.plugin.name
-                                            if model.plugin is not None
-                                            else None)
-
     return model
 
-from aria.orchestrator.decorators import operation
-from threading import RLock
-
-_TERMINAL_LOCK = RLock()
-
-@operation
-def _dry_node(ctx, _plugin, _implementation, **kwargs):
-    with _TERMINAL_LOCK:
-        puts('> node instance: %s' % Colored.red(ctx.node.name))
-        _dump_implementation(_plugin, _implementation)
-
 
-@operation
-def _dry_relationship(ctx, _plugin, _implementation, **kwargs):
-    with _TERMINAL_LOCK:
-        puts('> relationship instance: %s -> %s' % (
-            Colored.red(ctx.relationship.source_node.name),
-            Colored.red(ctx.relationship.target_node.name)))
-        _dump_implementation(_plugin, _implementation)
-
-
-def _dump_implementation(plugin, implementation):
-    if plugin:
-        puts('  plugin: %s' % Colored.magenta(plugin))
-    if implementation:
-        puts('  implementation: %s' % Colored.yellow(safe_repr(implementation)))
-
-
-def _parse_implementation(context, service_template, implementation):
-    if not implementation:
-        return None, ''
-
-    index = implementation.find('>')
-    if index == -1:
-        return None, implementation
-    plugin_name = implementation[:index].strip()
-    
-    if plugin_name == 'execution':
-        plugin = None
-    else:
-        plugin = service_template.plugins.get(plugin_name)
-        if plugin is None:
-            raise ValueError('unknown plugin: "{0}"'.format(plugin_name))
-
-    implementation = implementation[index+1:].strip()
-    return plugin, implementation
-
-
-def create_artifact_template(context, artifact):
+def create_artifact_template_model(context, artifact):
     model = ArtifactTemplate(name=artifact._name, type_name=artifact.type,
                              source_path=artifact.file)
 
@@ -325,12 +278,12 @@ def create_artifact_template(context, artifact):
             for k, v in credential.iteritems():
                 model.repository_credential[k] = v
 
-    create_properties_from_values(model.properties, artifact._get_property_values(context))
+    create_parameter_models_from_values(model.properties, artifact._get_property_values(context))
 
     return model
 
 
-def create_requirement_template(context, service_template, requirement):
+def create_requirement_template_model(context, service_template, requirement):
     model = {'name': requirement._name}
 
     node, node_variant = requirement._get_node(context)
@@ -356,21 +309,21 @@ def create_requirement_template(context, service_template, requirement):
 
     relationship = requirement.relationship
     if relationship is not None:
-        model.relationship_template = create_relationship_template(context, service_template,
-                                                                   relationship)
+        model.relationship_template = \
+            create_relationship_template_model(context, service_template, relationship)
 
     return model
 
 
-def create_relationship_type(context, relationship_type): # pylint: disable=unused-argument
+def create_relationship_type_model(context, relationship_type): # pylint: disable=unused-argument
     return RelationshipType(relationship_type._name)
 
 
-def create_policy_type(context, policy_type): # pylint: disable=unused-argument
+def create_policy_type_model(context, policy_type): # pylint: disable=unused-argument
     return PolicyType(policy_type._name)
 
 
-def create_relationship_template(context, service_template, relationship):
+def create_relationship_template_model(context, service_template, relationship):
     relationship_type, relationship_type_variant = relationship._get_type(context)
     if relationship_type_variant == 'relationship_type':
         model = RelationshipTemplate(type_name=relationship_type._name)
@@ -382,14 +335,14 @@ def create_relationship_template(context, service_template, relationship):
         if relationship_template.description:
             model.description = relationship_template.description.value
 
-    create_properties_from_assignments(model.properties, relationship.properties)
-    create_interface_templates(context, service_template, model.interface_templates,
-                               relationship.interfaces)
+    create_parameter_models_from_assignments(model.properties, relationship.properties)
+    create_interface_template_models(context, service_template, model.interface_templates,
+                                     relationship.interfaces)
 
     return model
 
 
-def create_capability_template(context, capability):
+def create_capability_template_model(context, capability):
     capability_type = capability._get_type(context)
     model = CapabilityTemplate(name=capability._name, type_name=capability_type._name)
 
@@ -406,21 +359,21 @@ def create_capability_template(context, capability):
     if valid_source_types:
         model.valid_source_node_type_names = valid_source_types
 
-    create_properties_from_assignments(model.properties, capability.properties)
+    create_parameter_models_from_assignments(model.properties, capability.properties)
 
     return model
 
 
-def create_group_template(context, service_template, group):
+def create_group_template_model(context, service_template, group):
     group_type = group._get_type(context)
     model = GroupTemplate(name=group._name, type_name=group_type._name)
 
     if group.description:
         model.description = group.description.value
 
-    create_properties_from_values(model.properties, group._get_property_values(context))
-    create_interface_templates(context, service_template, model.interface_templates,
-                               group._get_interfaces(context))
+    create_parameter_models_from_values(model.properties, group._get_property_values(context))
+    create_interface_template_models(context, service_template, model.interface_templates,
+                                     group._get_interfaces(context))
 
     members = group.members
     if members:
@@ -431,14 +384,14 @@ def create_group_template(context, service_template, group):
     return model
 
 
-def create_policy_template(context, policy):
+def create_policy_template_model(context, policy):
     policy_type = policy._get_type(context)
     model = PolicyTemplate(name=policy._name, type_name=policy_type._name)
 
     if policy.description:
         model.description = policy.description.value
 
-    create_properties_from_values(model.properties, policy._get_property_values(context))
+    create_parameter_models_from_values(model.properties, policy._get_property_values(context))
 
     node_templates, groups = policy._get_targets(context)
     if node_templates:
@@ -486,7 +439,7 @@ def create_types(context, root, types, normalize=None):
                         container.children.append(model)
 
 
-def create_properties_from_values(properties, source_properties):
+def create_parameter_models_from_values(properties, source_properties):
     if source_properties:
         for property_name, prop in source_properties.iteritems():
             properties[property_name] = Parameter(name=property_name,
@@ -495,7 +448,7 @@ def create_properties_from_values(properties, source_properties):
                                                   description=prop.description)
 
 
-def create_properties_from_assignments(properties, source_properties):
+def create_parameter_models_from_assignments(properties, source_properties):
     if source_properties:
         for property_name, prop in source_properties.iteritems():
             properties[property_name] = Parameter(name=property_name,
@@ -504,10 +457,10 @@ def create_properties_from_assignments(properties, source_properties):
                                                   description=prop.value.description)
 
 
-def create_interface_templates(context, service_template, interfaces, source_interfaces):
+def create_interface_template_models(context, service_template, interfaces, source_interfaces):
     if source_interfaces:
         for interface_name, interface in source_interfaces.iteritems():
-            interface = create_interface_template(context, service_template, interface)
+            interface = create_interface_template_model(context, service_template, interface)
             if interface is not None:
                 interfaces[interface_name] = interface
 
@@ -653,3 +606,23 @@ def create_constraint_clause_lambda(context, node_filter, constraint_clause,
pro
         return pattern
 
     return None
+
+
+def parse_implementation_string(context, service_template, implementation):
+    if not implementation:
+        return None, ''
+
+    index = implementation.find('>')
+    if index == -1:
+        return None, implementation
+    plugin_name = implementation[:index].strip()
+    
+    if plugin_name == 'execution':
+        plugin = None
+    else:
+        plugin = service_template.plugins.get(plugin_name)
+        if plugin is None:
+            raise ValueError('unknown plugin: "{0}"'.format(plugin_name))
+
+    implementation = implementation[index+1:].strip()
+    return plugin, implementation

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/58aac8da/extensions/aria_extension_tosca/simple_v1_0/presenter.py
----------------------------------------------------------------------
diff --git a/extensions/aria_extension_tosca/simple_v1_0/presenter.py b/extensions/aria_extension_tosca/simple_v1_0/presenter.py
index 0809014..eee5769 100644
--- a/extensions/aria_extension_tosca/simple_v1_0/presenter.py
+++ b/extensions/aria_extension_tosca/simple_v1_0/presenter.py
@@ -19,7 +19,7 @@ from aria.parser.presentation import Presenter
 
 from .functions import (Concat, Token, GetInput, GetProperty, GetAttribute, GetOperationOutput,
                         GetNodesOfType, GetArtifact)
-from .modeling import create_service_model
+from .modeling import create_service_template_model
 from .templates import ServiceTemplate
 
 class ToscaSimplePresenter1_0(Presenter): # pylint: disable=invalid-name
@@ -75,5 +75,5 @@ class ToscaSimplePresenter1_0(Presenter): # pylint: disable=invalid-name
         return FrozenList(import_locations) if import_locations else EMPTY_READ_ONLY_LIST
 
     @cachedmethod
-    def _get_service_model(self, context): # pylint: disable=no-self-use
-        return create_service_model(context)
+    def _get_model(self, context): # pylint: disable=no-self-use
+        return create_service_template_model(context)


Mime
View raw message