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
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))
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.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 @@
     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 @@
         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, (
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 @@
         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):
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..4e1695b 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,14 @@
           capability: tosca.capabilities.Endpoint.Database
           node: mongodb.Database
 
+  Subsystem:
+    derived_from: tosca.nodes.Root
+    requirements:
+      - host:
+          capability: tosca.capabilities.Container
+    capabilities:
+      app_endpoint: tosca.capabilities.Endpoint.Public
+
 topology_template:
 
   inputs:
@@ -324,8 +332,7 @@
         enabled: false
 
   substitution_mappings:
-
-    node_type: tosca.nodes.WebApplication
+    node_type: Subsystem
     requirements:
       host: [ node_cellar, host ] # doesn't really make sense; just for testing
     capabilities: