ariatosca-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From AviaE <...@git.apache.org>
Subject [GitHub] incubator-ariatosca pull request #189: ARIA-174 Refactor instantiation phase
Date Thu, 03 Aug 2017 09:37:35 GMT
Github user AviaE commented on a diff in the pull request:

    https://github.com/apache/incubator-ariatosca/pull/189#discussion_r131089434
  
    --- Diff: aria/orchestrator/topology/template_handler.py ---
    @@ -0,0 +1,597 @@
    +# Licensed to the Apache Software Foundation (ASF) under one or more
    +# contributor license agreements.  See the NOTICE file distributed with
    +# this work for additional information regarding copyright ownership.
    +# The ASF licenses this file to You 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 datetime import datetime
    +
    +from ...utils import formatting
    +from ...modeling import utils as modeling_utils
    +from . import utils, common
    +
    +
    +class ServiceTemplate(common._TemplateHandlerMixin):
    +    def dump(self, out_stream):
    +        if self._model.description is not None:
    +            out_stream.write(out_stream.meta(self._model.description))
    +        self._topology.dump(self._model.meta_data, out_stream, 'Metadata')
    +        self._topology.dump(self._model.node_templates, out_stream)
    +        self._topology.dump(self._model.group_templates, out_stream)
    +        self._topology.dump(self._model.policy_templates, out_stream)
    +        self._topology.dump(self._model.substitution_template, out_stream)
    +        self._topology.dump(self._model.inputs, out_stream, 'Inputs')
    +        self._topology.dump(self._model.outputs, out_stream, 'Outputs')
    +        self._topology.dump(self._model.workflow_templates, out_stream, 'Workflow templates')
    +
    +    def coerce(self, **kwargs):
    +        self._coerce(self._model.meta_data,
    +                     self._model.node_templates,
    +                     self._model.group_templates,
    +                     self._model.policy_templates,
    +                     self._model.substitution_template,
    +                     self._model.inputs,
    +                     self._model.outputs,
    +                     self._model.workflow_templates,
    +                     **kwargs)
    +
    +    def instantiate(self, instance_cls, inputs=None):                               
               # pylint: disable=arguments-differ
    +        now = datetime.now()
    +
    +        modeling_utils.validate_no_undeclared_inputs(
    +            declared_inputs=self._model.inputs, supplied_inputs=inputs or {})
    +        modeling_utils.validate_required_inputs_are_supplied(
    +            declared_inputs=self._model.inputs, supplied_inputs=inputs or {})
    +
    +        service = instance_cls(
    +            created_at=now,
    +            updated_at=now,
    +            description=utils.deepcopy_with_locators(self._model.description),
    +            service_template=self._model,
    +            inputs=modeling_utils.merge_parameter_values(inputs, self._model.inputs)
    +        )
    +
    +        for plugin_specification in self._model.plugin_specifications.itervalues():
    +            if plugin_specification.enabled and self._topology._model_storage:
    +                if utils.resolve_plugin_specification(plugin_specification,
    +                                                      self._topology.model_storage.plugin.list()):
    +                    plugin = plugin_specification.plugin
    +                    service.plugins[plugin.name] = plugin
    +                else:
    +                    self._topology.report('specified plugin not found: {0}'.format(
    +                        plugin_specification.name), level=self._topology.Issue.EXTERNAL)
    +        service.meta_data = self._topology.instantiate(self._model.meta_data)
    +
    +        for node_template in self._model.node_templates.itervalues():
    +            for _ in range(self._scaling(node_template)['default_instances']):
    +                node = self._topology.instantiate(node_template)
    +                service.nodes[node.name] = node
    +
    +        service.groups = self._topology.instantiate(self._model.group_templates)
    +        service.policies = self._topology.instantiate(self._model.policy_templates)
    +        service.workflows = self._topology.instantiate(self._model.workflow_templates)
    +        service.substitution = self._topology.instantiate(self._model.substitution_template)
    +        service.outputs = self._topology.instantiate(self._model.outputs)
    +
    +        return service
    +
    +    def _scaling(self, node_template):
    +        scaling = {}
    +
    +        def extract_property(properties, name):
    +            if name in scaling:
    +                return
    +            prop = properties.get(name)
    +            if (prop is not None) and (prop.type_name == 'integer') and (prop.value is
not None):
    +                scaling[name] = prop.value
    +
    +        def extract_properties(properties):
    +            extract_property(properties, 'min_instances')
    +            extract_property(properties, 'max_instances')
    +            extract_property(properties, 'default_instances')
    +
    +        # From our scaling capabilities
    +        for capability_template in node_template.capability_templates.itervalues():
    +            if capability_template.type.role == 'scaling':
    +                extract_properties(capability_template.properties)
    +
    +        # From service scaling policies
    +        for policy_template in node_template.service_template.policy_templates.itervalues():
    +            if policy_template.type.role == 'scaling':
    +                if policy_template.is_for_node_template(node_template.name):
    +                    extract_properties(policy_template.properties)
    +
    +        # Defaults
    +        scaling.setdefault('min_instances', 0)
    +        scaling.setdefault('max_instances', 1)
    +        scaling.setdefault('default_instances', 1)
    +
    +        # Validate
    +        # pylint: disable=too-many-boolean-expressions
    +        if (scaling['min_instances'] < 0 or
    +                scaling['max_instances'] < 0 or
    +                scaling['default_instances'] < 0 or
    +                scaling['max_instances'] < scaling['min_instances'] or
    +                scaling['default_instances'] < scaling['min_instances'] or
    +                scaling['default_instances'] > scaling['max_instances']):
    +            self._topology.report(
    +                'invalid scaling parameters for node template "{0}": min={min_instances},
max='
    +                '{max_instances}, default={default_instances}'.format(self._model.name,
**scaling),
    +                level=self._topology.Issue.BETWEEN_TYPES)
    +
    +        return scaling
    +
    +    def validate(self):
    +        self._topology.validate(self._model.meta_data)
    +        self._topology.validate(self._model.node_templates)
    +        self._topology.validate(self._model.group_templates)
    +        self._topology.validate(self._model.policy_templates)
    +        self._topology.validate(self._model.substitution_template)
    +        self._topology.validate(self._model.inputs)
    +        self._topology.validate(self._model.outputs)
    +        self._topology.validate(self._model.workflow_templates)
    +        self._topology.validate(self._model.node_types)
    +        self._topology.validate(self._model.group_types)
    +        self._topology.validate(self._model.policy_types)
    +        self._topology.validate(self._model.relationship_types)
    +        self._topology.validate(self._model.capability_types)
    +        self._topology.validate(self._model.interface_types)
    +        self._topology.validate(self._model.artifact_types)
    +
    +
    +class ArtifactTemplate(common._TemplateHandlerMixin):
    +    def dump(self, out_stream):
    +        out_stream.write(out_stream.node(self._model.name))
    +        if self._model.description:
    +            out_stream.write(out_stream.meta(self._model.description))
    +        with out_stream.indent():
    +            out_stream.write('Artifact type: {0}'.format(out_stream.type(self._model.type.name)))
    +            out_stream.write('Source path: {0}'.format(out_stream.literal(self._model.source_path)))
    +            if self._model.target_path is not None:
    +                out_stream.write('Target path: {0}'.format(out_stream.literal(
    +                    self._model.target_path)))
    +            if self._model.repository_url is not None:
    +                out_stream.write('Repository URL: {0}'.format(
    +                    out_stream.literal(self._model.repository_url)))
    +            if self._model.repository_credential:
    +                out_stream.write('Repository credential: {0}'.format(
    +                    out_stream.literal(self._model.repository_credential)))
    +            self._topology.dump(self._model.properties, out_stream, 'Properties')
    +
    +    def coerce(self, **kwargs):
    +        self._topology.coerce(self._model.properties, **kwargs)
    +
    +    def instantiate(self, instance_cls):
    +        return instance_cls(
    +            name=self._model.name,
    +            type=self._model.type,
    +            description=utils.deepcopy_with_locators(self._model.description),
    +            source_path=self._model.source_path,
    +            target_path=self._model.target_path,
    +            repository_url=self._model.repository_url,
    +            repository_credential=self._model.repository_credential,
    +            artifact_template=self._model)
    +
    +    def validate(self):
    +        self._topology.validate(self._model.properties)
    +
    +
    +class CapabilityTemplate(common._TemplateHandlerMixin):
    +    def dump(self, out_stream):
    +        out_stream.write(out_stream.node(self._model.name))
    +        if self._model.description:
    +            out_stream.write(out_stream.meta(self._model.description))
    +        with out_stream.indent():
    +            out_stream.write('Type: {0}'.format(out_stream.type(self._model.type.name)))
    +            out_stream.write(
    +                'Occurrences: {0:d}{1}'.format(
    +                    self._model.min_occurrences or 0,
    +                    ' to {0:d}'.format(self._model.max_occurrences)
    +                    if self._model.max_occurrences is not None
    +                    else ' or more'))
    +            if self._model.valid_source_node_types:
    +                out_stream.write('Valid source node types: {0}'.format(
    +                    ', '.join((str(out_stream.type(v.name))
    +                               for v in self._model.valid_source_node_types))))
    +            self._topology.dump(self._model.properties, out_stream, 'Properties')
    +
    +    def coerce(self):
    +        self._topology.coerce(self._model.properties)
    +
    +    def instantiate(self, instance_cls):
    +        return instance_cls(name=self._model.name,
    +                            type=self._model.type,
    +                            min_occurrences=self._model.min_occurrences,
    +                            max_occurrences=self._model.max_occurrences,
    +                            occurrences=0,
    +                            capability_template=self._model)
    +
    +    def validate(self):
    +        self._topology.validate(self._model.properties)
    +
    +
    +class RequirementTemplate(common._TemplateHandlerMixin):
    +    def dump(self, out_stream):
    +        if self._model.name:
    +            out_stream.write(out_stream.node(self._model.name))
    +        else:
    +            out_stream.write('Requirement:')
    +        with out_stream.indent():
    +            if self._model.target_node_type is not None:
    +                out_stream.write('Target node type: {0}'.format(
    +                    out_stream.type(self._model.target_node_type.name)))
    +            elif self._model.target_node_template is not None:
    +                out_stream.write('Target node template: {0}'.format(
    +                    out_stream.node(self._model.target_node_template.name)))
    +            if self._model.target_capability_type is not None:
    +                out_stream.write('Target capability type: {0}'.format(
    +                    out_stream.type(self._model.target_capability_type.name)))
    +            elif self._model.target_capability_name is not None:
    +                out_stream.write('Target capability name: {0}'.format(
    +                    out_stream.node(self._model.target_capability_name)))
    +            if self._model.target_node_template_constraints:
    +                out_stream.write('Target node template constraints:')
    +                with out_stream.indent():
    +                    for constraint in self._model.target_node_template_constraints:
    +                        out_stream.write(out_stream.literal(constraint))
    +            if self._model.relationship_template:
    +                out_stream.write('Relationship:')
    +                with out_stream.indent():
    +                    self._topology.dump(self._model.relationship_template, out_stream)
    +
    +    def coerce(self, **kwargs):
    +        self._topology.coerce(self._model.relationship_template, **kwargs)
    +
    +    def instantiate(self, instance_cls):
    +        return instance_cls(name=self._model.name,
    +                            type=self._model.type,
    +                            min_occurrences=self._model.min_occurrences,
    +                            max_occurrences=self._model.max_occurrences,
    +                            occurrences=0,
    --- End diff --
    
    Should it be 1? This is a requirement template.
    And do we even instantiate this class? In the `_init_map` it is mapped to `None`.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message