[
https://issues.apache.org/jira/browse/ARIA-105?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15901384#comment-15901384
]
ASF GitHub Bot commented on ARIA-105:
-------------------------------------
Github user mxmrlv commented on a diff in the pull request:
https://github.com/apache/incubator-ariatosca/pull/72#discussion_r104934259
--- Diff: aria/modeling/service_template.py ---
@@ -0,0 +1,1693 @@
+# 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.
+
+# pylint: disable=too-many-lines, no-self-argument, no-member,
abstract-method
+
+from __future__ import absolute_import # so we can import standard 'types'
+
+from types import FunctionType
+from datetime import datetime
+
+from sqlalchemy import (
+ Column,
+ Text,
+ Integer,
+ DateTime
+)
+from sqlalchemy.ext.declarative import declared_attr
+
+from ..parser import validation
+from ..utils import collections, formatting, console
+from .bases import TemplateModelMixin
+from . import (
+ utils,
+ types as modeling_types
+)
+
+
+class ServiceTemplateBase(TemplateModelMixin):
+ """
+ A service template is a source for creating :class:`Service` instances.
+
+ It is usually created by various DSL parsers, such as ARIA's TOSCA
extension. However, it can
+ also be created programmatically.
+
+ :ivar name: Name (unique for this ARIA installation)
+ :vartype name: basestring
+ :ivar description: Human-readable description
+ :vartype description: basestring
+ :ivar main_file_name: Filename of CSAR or YAML file from which this
service template was parsed
+ :vartype main_file_name: basestring
+ :ivar meta_data: Custom annotations
+ :vartype meta_data: {basestring: :class:`Metadata`}
+ :ivar node_templates: Templates for creating nodes
+ :vartype node_templates: [:class:`NodeTemplate`]
+ :ivar group_templates: Templates for creating groups
+ :vartype group_templates: [:class:`GroupTemplate`]
+ :ivar policy_templates: Templates for creating policies
+ :vartype policy_templates: [:class:`PolicyTemplate`]
+ :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`}
+ :ivar outputs: These parameters are filled in after service
installation
+ :vartype outputs: {basestring: :class:`Parameter`}
+ :ivar operation_templates: Custom operations that can be performed on
the service
+ :vartype operation_templates: {basestring: :class:`OperationTemplate`}
+ :ivar plugins: Plugins required by services
+ :vartype plugins: {basestring: :class:`Plugin`}
+ :ivar node_types: Base for the node type hierarchy
+ :vartype node_types: :class:`Type`
+ :ivar group_types: Base for the group type hierarchy
+ :vartype group_types: :class:`Type`
+ :ivar policy_types: Base for the policy type hierarchy
+ :vartype policy_types: :class:`Type`
+ :ivar relationship_types: Base for the relationship type hierarchy
+ :vartype relationship_types: :class:`Type`
+ :ivar capability_types: Base for the capability type hierarchy
+ :vartype capability_types: :class:`Type`
+ :ivar interface_types: Base for the interface type hierarchy
+ :vartype interface_types: :class:`Type`
+ :ivar artifact_types: Base for the artifact type hierarchy
+ :vartype artifact_types: :class:`Type`
+ :ivar plugins: Plugins required to be installed
+ :vartype plugins: {basestring: :class:`Plugin`}
+ :ivar created_at: Creation timestamp
+ :vartype created_at: :class:`datetime.datetime`
+ :ivar updated_at: Update timestamp
+ :vartype updated_at: :class:`datetime.datetime`
+
+ :ivar services: Instantiated services
+ :vartype services: [:class:`Service`]
+ """
+
+ __tablename__ = 'service_template'
+
+ description = Column(Text)
+ main_file_name = Column(Text)
+
+ @declared_attr
+ def meta_data(cls):
+ # Warning! We cannot use the attr name "metadata" because it's
used by SqlAlchemy!
+ return cls.many_to_many_relationship('metadata', dict_key='name')
+
+ @declared_attr
+ def node_templates(cls):
+ return cls.one_to_many_relationship('node_template')
+
+ @declared_attr
+ def group_templates(cls):
+ return cls.one_to_many_relationship('group_template')
+
+ @declared_attr
+ def policy_templates(cls):
+ return cls.one_to_many_relationship('policy_template')
+
+ @declared_attr
+ def substitution_template(cls):
+ return cls.one_to_one_relationship('substitution_template')
+
+ @declared_attr
+ def inputs(cls):
+ return cls.many_to_many_relationship('parameter',
table_prefix='inputs',
+ dict_key='name')
+
+ @declared_attr
+ def outputs(cls):
+ return cls.many_to_many_relationship('parameter',
table_prefix='outputs',
+ dict_key='name')
+
+ @declared_attr
+ def operation_templates(cls):
+ return cls.one_to_many_relationship('operation_template',
dict_key='name')
+
+ @declared_attr
+ def plugins(cls):
+ return cls.one_to_many_relationship('plugin')
+
+ @declared_attr
+ def node_types(cls):
+ return cls.one_to_one_relationship('type', key='node_type_fk',
backreference='')
+
+ @declared_attr
+ def group_types(cls):
+ return cls.one_to_one_relationship('type', key='group_type_fk',
backreference='')
+
+ @declared_attr
+ def policy_types(cls):
+ return cls.one_to_one_relationship('type', key='policy_type_fk',
backreference='')
+
+ @declared_attr
+ def relationship_types(cls):
+ return cls.one_to_one_relationship('type',
key='relationship_type_fk', backreference='')
+
+ @declared_attr
+ def capability_types(cls):
+ return cls.one_to_one_relationship('type',
key='capability_type_fk', backreference='')
+
+ @declared_attr
+ def interface_types(cls):
+ return cls.one_to_one_relationship('type',
key='interface_type_fk', backreference='')
+
+ @declared_attr
+ def artifact_types(cls):
+ return cls.one_to_one_relationship('type', key='artifact_type_fk',
backreference='')
+
+ # region orchestration
+
+ created_at = Column(DateTime, nullable=False, index=True)
+ updated_at = Column(DateTime)
+
+ # endregion
+
+ # region foreign keys
+
+ __private_fields__ = ['substitution_template_fk',
+ 'node_type_fk',
+ 'group_type_fk',
+ 'policy_type_fk',
+ 'relationship_type_fk',
+ 'capability_type_fk',
+ 'interface_type_fk',
+ 'artifact_type_fk']
+
+ # ServiceTemplate one-to-one to SubstitutionTemplate
+ @declared_attr
+ def substitution_template_fk(cls):
+ return cls.foreign_key('substitution_template', nullable=True)
+
+ # ServiceTemplate one-to-one to Type
+ @declared_attr
+ def node_type_fk(cls):
+ return cls.foreign_key('type', nullable=True)
+
+ # ServiceTemplate one-to-one to Type
+ @declared_attr
+ def group_type_fk(cls):
+ return cls.foreign_key('type', nullable=True)
+
+ # ServiceTemplate one-to-one to Type
+ @declared_attr
+ def policy_type_fk(cls):
+ return cls.foreign_key('type', nullable=True)
+
+ # ServiceTemplate one-to-one to Type
+ @declared_attr
+ def relationship_type_fk(cls):
+ return cls.foreign_key('type', nullable=True)
+
+ # ServiceTemplate one-to-one to Type
+ @declared_attr
+ def capability_type_fk(cls):
+ return cls.foreign_key('type', nullable=True)
+
+ # ServiceTemplate one-to-one to Type
+ @declared_attr
+ def interface_type_fk(cls):
+ return cls.foreign_key('type', nullable=True)
+
+ # ServiceTemplate one-to-one to Type
+ @declared_attr
+ def artifact_type_fk(cls):
+ return cls.foreign_key('type', nullable=True)
+
+ # endregion
+
+ def get_node_template(self, node_template_name):
+ if self.node_templates:
+ for node_template in self.node_templates:
+ if node_template.name == node_template_name:
+ return node_template
+ return None
+
+ def get_group_template(self, group_template_name):
+ if self.group_templates:
+ for group_template in self.group_templates:
+ if group_template.name == group_template_name:
+ return group_template
+ return None
+
+ @property
+ def as_raw(self):
+ return collections.OrderedDict((
+ ('description', self.description),
+ ('metadata', formatting.as_raw_dict(self.meta_data)),
+ ('node_templates',
formatting.as_raw_list(self.node_templates)),
+ ('group_templates',
formatting.as_raw_list(self.group_templates)),
+ ('policy_templates',
formatting.as_raw_list(self.policy_templates)),
+ ('substitution_template',
formatting.as_raw(self.substitution_template)),
+ ('inputs', formatting.as_raw_dict(self.inputs)),
+ ('outputs', formatting.as_raw_dict(self.outputs)),
+ ('operation_templates',
formatting.as_raw_list(self.operation_templates))))
+
+ @property
+ def types_as_raw(self):
+ return collections.OrderedDict((
+ ('node_types', formatting.as_raw(self.node_types)),
+ ('group_types', formatting.as_raw(self.group_types)),
+ ('policy_types', formatting.as_raw(self.policy_types)),
+ ('relationship_types',
formatting.as_raw(self.relationship_types)),
+ ('capability_types', formatting.as_raw(self.capability_types)),
+ ('interface_types', formatting.as_raw(self.interface_types)),
+ ('artifact_types', formatting.as_raw(self.artifact_types))))
+
+ def instantiate(self, context, container):
+ from . import models
+ now = datetime.now()
+ service = models.Service(created_at=now,
+ updated_at=now,
+
description=utils.deepcopy_with_locators(self.description),
+ service_template=self)
+ #service.name = '{0}_{1}'.format(self.name, service.id)
+
+ context.modeling.instance = service
+
+ utils.instantiate_dict(context, self, service.meta_data,
self.meta_data)
+
+ for node_template in self.node_templates:
+ for _ in range(node_template.default_instances):
+ node = node_template.instantiate(context, container)
+ service.nodes.append(node)
+
+ utils.instantiate_list(context, self, service.groups,
self.group_templates)
+ utils.instantiate_list(context, self, service.policies,
self.policy_templates)
+ utils.instantiate_dict(context, self, service.operations,
self.operation_templates)
+
+ if self.substitution_template is not None:
+ service.substitution =
self.substitution_template.instantiate(context, container)
+
+ utils.instantiate_dict(context, self, service.inputs, self.inputs)
+ utils.instantiate_dict(context, self, service.outputs,
self.outputs)
+
+ for name, the_input in context.modeling.inputs.iteritems():
+ if name not in service.inputs:
+ context.validation.report('input "{0}" is not
supported'.format(name))
+ else:
+ service.inputs[name].value = the_input
+
+ return service
+
+ def validate(self, context):
+ utils.validate_dict_values(context, self.meta_data)
+ utils.validate_list_values(context, self.node_templates)
+ utils.validate_list_values(context, self.group_templates)
+ utils.validate_list_values(context, self.policy_templates)
+ if self.substitution_template is not None:
+ self.substitution_template.validate(context)
+ utils.validate_dict_values(context, self.inputs)
+ utils.validate_dict_values(context, self.outputs)
+ utils.validate_dict_values(context, self.operation_templates)
+ if self.node_types is not None:
+ self.node_types.validate(context)
+ if self.group_types is not None:
+ self.group_types.validate(context)
+ if self.policy_types is not None:
+ self.policy_types.validate(context)
+ if self.relationship_types is not None:
+ self.relationship_types.validate(context)
+ if self.capability_types is not None:
+ self.capability_types.validate(context)
+ if self.interface_types is not None:
+ self.interface_types.validate(context)
+ if self.artifact_types is not None:
+ self.artifact_types.validate(context)
+
+ def coerce_values(self, context, container, report_issues):
+ utils.coerce_dict_values(context, container, self.meta_data,
report_issues)
+ utils.coerce_list_values(context, container, self.node_templates,
report_issues)
+ utils.coerce_list_values(context, container, self.group_templates,
report_issues)
+ utils.coerce_list_values(context, container,
self.policy_templates, report_issues)
+ if self.substitution_template is not None:
+ self.substitution_template.coerce_values(context, container,
report_issues)
+ utils.coerce_dict_values(context, container, self.inputs,
report_issues)
+ utils.coerce_dict_values(context, container, self.outputs,
report_issues)
+ utils.coerce_dict_values(context, container,
self.operation_templates, report_issues)
+
+ def dump(self, context):
+ if self.description is not None:
+ console.puts(context.style.meta(self.description))
+ utils.dump_dict_values(context, self.meta_data, 'Metadata')
+ for node_template in self.node_templates:
+ node_template.dump(context)
+ for group_template in self.group_templates:
+ group_template.dump(context)
+ for policy_template in self.policy_templates:
+ policy_template.dump(context)
+ if self.substitution_template is not None:
+ self.substitution_template.dump(context)
+ utils.dump_dict_values(context, self.inputs, 'Inputs')
+ utils.dump_dict_values(context, self.outputs, 'Outputs')
+ utils.dump_dict_values(context, self.operation_templates,
'Operation templates')
+
+ def dump_types(self, context):
--- End diff --
this as well
> Integrate new models into parser
> --------------------------------
>
> Key: ARIA-105
> URL: https://issues.apache.org/jira/browse/ARIA-105
> Project: AriaTosca
> Issue Type: Task
> Reporter: Ran Ziv
> Assignee: Tal Liron
>
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)