Repository: incubator-ariatosca Updated Branches: refs/heads/ARIA-358-restrict-substitution-mapping-types 8be32e156 -> 842395fb0 (forced update)
ARIA-358 Restrict node template types in a substituting template Within a substituting template (a service template that contains a `substitution_mappings` section), a node template cannot have a type that is a descendant of the `node_type` field of the `substitution_mappings` section Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/842395fb Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/842395fb Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/842395fb Branch: refs/heads/ARIA-358-restrict-substitution-mapping-types Commit: 842395fb0a17e7ea100d20f94a56db7a44ed4e7e Parents: 969149b Author: Avia Efrat <a...@gigaspaces.com> Authored: Thu Sep 7 14:14:22 2017 +0300 Committer: Avia Efrat <a...@gigaspaces.com> Committed: Sun Sep 10 17:49:19 2017 +0300 ---------------------------------------------------------------------- .../simple_v1_0/modeling/node_templates.py | 34 ++++++++++++++++++++ .../simple_v1_0/templates.py | 5 +++ .../aria_extension_tosca/simple_v1_0/types.py | 9 +++--- .../node-cellar/node-cellar.yaml | 7 ++++ 4 files changed, 51 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/842395fb/extensions/aria_extension_tosca/simple_v1_0/modeling/node_templates.py ---------------------------------------------------------------------- diff --git a/extensions/aria_extension_tosca/simple_v1_0/modeling/node_templates.py b/extensions/aria_extension_tosca/simple_v1_0/modeling/node_templates.py new file mode 100644 index 0000000..4e1a37b --- /dev/null +++ b/extensions/aria_extension_tosca/simple_v1_0/modeling/node_templates.py @@ -0,0 +1,34 @@ +# 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. + + +def validate_type_in_regards_to_substitution_mapping(context, node_template): + """ + Reports iff the node is in a substituting template, and is derived from the substituting type + + :param context: :class:`aria.parser.consumption.context.ConsumptionContext` + :param node_template: :class:`aria_extension_tosca.v1_0.templates.NodeTemplate` + """ + substitution_mappings = node_template._container.substitution_mappings + if not substitution_mappings: + return + substitution_type = substitution_mappings._get_type(context) + node_type = node_template._get_type(context) + if substitution_type._is_descendant(context, node_type): + context.validation.report( + 'type {0} of node template {1} is a descendant of substitution type {2}. in a service ' + 'template that contains a substitution_mapping section, you cannot define a node ' + 'template that descends from the substitution mapping type.'.format( + node_type._name, node_template._name, substitution_type._name)) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/842395fb/extensions/aria_extension_tosca/simple_v1_0/templates.py ---------------------------------------------------------------------- diff --git a/extensions/aria_extension_tosca/simple_v1_0/templates.py b/extensions/aria_extension_tosca/simple_v1_0/templates.py index 3c36bb8..4505878 100644 --- a/extensions/aria_extension_tosca/simple_v1_0/templates.py +++ b/extensions/aria_extension_tosca/simple_v1_0/templates.py @@ -33,6 +33,7 @@ from .modeling.capabilities import get_template_capabilities from .modeling.artifacts import get_inherited_artifact_definitions from .modeling.policies import get_policy_targets from .modeling.copy import get_default_raw_from_copy +from .modeling.node_templates import validate_type_in_regards_to_substitution_mapping from .presentation.extensible import ExtensiblePresentation from .presentation.field_validators import copy_validator, policy_targets_validator from .presentation.types import (convert_name_to_full_type_name, get_type_by_name) @@ -179,6 +180,9 @@ class NodeTemplate(ExtensiblePresentation): def _get_artifacts(self, context): return FrozenDict(get_inherited_artifact_definitions(context, self)) + def _validate_type_in_regards_to_substitution_mapping(self, context): + validate_type_in_regards_to_substitution_mapping(context, self) + def _validate(self, context): super(NodeTemplate, self)._validate(context) self._get_property_values(context) @@ -186,6 +190,7 @@ class NodeTemplate(ExtensiblePresentation): self._get_capabilities(context) self._get_interfaces(context) self._get_artifacts(context) + self._validate_type_in_regards_to_substitution_mapping(context) def _dump(self, context): self._dump_content(context, ( http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/842395fb/extensions/aria_extension_tosca/simple_v1_0/types.py ---------------------------------------------------------------------- diff --git a/extensions/aria_extension_tosca/simple_v1_0/types.py b/extensions/aria_extension_tosca/simple_v1_0/types.py index 0241917..01187df 100644 --- a/extensions/aria_extension_tosca/simple_v1_0/types.py +++ b/extensions/aria_extension_tosca/simple_v1_0/types.py @@ -619,12 +619,13 @@ class NodeType(ExtensiblePresentation): return get_parent_presentation(context, self, convert_name_to_full_type_name, 'node_types') @cachedmethod - def _is_descendant(self, context, the_type): - if the_type is None: + def _is_descendant(self, context, other_type): + """returns True iff `other_type` is a descendant of the represented node type""" + if other_type is None: return False - elif the_type._name == self._name: + elif other_type._name == self._name: return True - return self._is_descendant(context, the_type._get_parent(context)) + return self._is_descendant(context, other_type._get_parent(context)) @cachedmethod def _get_properties(self, context): http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/842395fb/tests/resources/service-templates/tosca-simple-1.0/node-cellar/node-cellar.yaml ---------------------------------------------------------------------- diff --git a/tests/resources/service-templates/tosca-simple-1.0/node-cellar/node-cellar.yaml b/tests/resources/service-templates/tosca-simple-1.0/node-cellar/node-cellar.yaml index 5a46532..cf39ee4 100644 --- a/tests/resources/service-templates/tosca-simple-1.0/node-cellar/node-cellar.yaml +++ b/tests/resources/service-templates/tosca-simple-1.0/node-cellar/node-cellar.yaml @@ -81,6 +81,13 @@ node_types: capability: tosca.capabilities.Endpoint.Database node: mongodb.Database + Subsystem: + derived_from: tosca.nodes.Root + requirements: + host: tosca.Capabilities.Container + capabilities: + app_endpoint: tosca.capabilities.Endpoint.Public + topology_template: inputs: